desktop: remove custom protocol interception for https requests (#2426)

This commit is contained in:
Muhammad Ali
2023-04-27 10:13:37 +05:00
committed by GitHub
parent cae36487f9
commit a2ac882a05

View File

@@ -22,7 +22,6 @@ import { isDevelopment, getPath } from "./utils";
import { createReadStream } from "fs";
import { extname, normalize } from "path";
import { logger } from "./logger";
import { Blob } from "buffer";
import { URL } from "url";
const FILE_NOT_FOUND = -6;
@@ -68,30 +67,7 @@ function registerProtocol() {
mimeType: extensionToMimeType[fileExtension]
});
} else {
var response;
try {
const body = await getBody(request);
response = await fetch(request.url, {
...request,
body,
headers: {
...request.headers,
origin: `${PROTOCOL}://${HOSTNAME}/`
},
redirect: "manual"
});
} catch (e) {
console.error(e);
logger.error(`Error sending request to `, request.url, "Error: ", e);
callback({ statusCode: 400 });
return;
}
callback({
statusCode: response.status,
data: response.body,
headers: Object.fromEntries(response.headers.entries()),
mimeType: response.headers.get("Content-Type")
});
protocol.uninterceptProtocol(PROTOCOL);
}
}
);
@@ -103,35 +79,11 @@ function registerProtocol() {
);
}
const bypassedRoutes = ["/notes/index_v14.json", "/notes/welcome-web"];
const bypassedRoutes = [];
function shouldInterceptRequest(url) {
let shouldIntercept = url.hostname === HOSTNAME;
return shouldIntercept && !bypassedRoutes.includes(url.pathname);
}
/**
*
* @param {Electron.ProtocolRequest} request
*/
async function getBody(request) {
/**
* @type {Electron.Session}
*/
const session = globalThis.window.webContents.session;
const blobParts = [];
if (!request.uploadData || !request.uploadData.length) return null;
for (let data of request.uploadData) {
if (data.type === "rawData") {
blobParts.push(new Uint8Array(data.bytes));
} else if (data.type === "blob") {
const buffer = await session.getBlobData(data.blobUUID);
blobParts.push(new Uint8Array(buffer));
}
}
const blob = new Blob(blobParts);
return await blob.arrayBuffer();
}
const PROTOCOL_URL = `${PROTOCOL}://${HOSTNAME}/`;
export { registerProtocol, PROTOCOL_URL };