Skip to content

Commit

Permalink
@electron 10.0.0-beta.2 => 10.0.0-beta.3: fix protocol registering calls
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimiry committed Jun 15, 2020
1 parent eed1810 commit f1393f8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/electron-main/bootstrap/app-ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {registerWebFolderFileProtocol} from "src/electron-main/protocol";
import {setUpPowerMonitorNotification} from "src/electron-main/power-monitor";

export async function appReadyHandler(ctx: Context): Promise<void> {
await registerWebFolderFileProtocol(ctx, getDefaultSession());
registerWebFolderFileProtocol(ctx, getDefaultSession());

await initSession(ctx, getDefaultSession());

Expand Down
72 changes: 34 additions & 38 deletions src/electron-main/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {promisify} from "util";

import {Context} from "src/electron-main/model";
import {WEB_PROTOCOL_SCHEME} from "src/shared/constants";
import {resolveOrRejectIfError} from "src/shared/util";

const fsAsync = {
stat: promisify(fs.stat),
Expand All @@ -31,27 +30,25 @@ export function registerStandardSchemes(ctx: Context): void {
}

// TODO electron: get rid of "baseURLForDataURL" workaround, see https://github.com/electron/electron/issues/20700
export async function registerWebFolderFileProtocol(ctx: Context, session: Session): Promise<void> {
export function registerWebFolderFileProtocol(ctx: Context, session: Session): void {
const webPath = path.join(ctx.locations.appDir, "./web");

return new Promise((resolve, reject) => {
session.protocol.registerFileProtocol(
WEB_PROTOCOL_SCHEME,
(request, callback) => {
const url = new URL(request.url);
const resource = path.normalize(
path.join(webPath, url.host, url.pathname),
);

if (!pathIsInside(resource, webPath)) {
throw new Error(`Forbidden file system resource "${resource}"`);
}

callback({path: resource});
},
resolveOrRejectIfError(resolve, reject),
);
});
const scheme = WEB_PROTOCOL_SCHEME;
const registered = session.protocol.registerFileProtocol(
scheme,
(request, callback) => {
const url = new URL(request.url);
const resource = path.normalize(
path.join(webPath, url.host, url.pathname),
);
if (!pathIsInside(resource, webPath)) {
throw new Error(`Forbidden file system resource "${resource}"`);
}
callback({path: resource});
},
);
if (!registered) {
throw new Error(`Failed to register "${scheme}" protocol mapped to "${webPath}" directory`);
}
}

async function resolveFileSystemResourceLocation(
Expand Down Expand Up @@ -89,22 +86,21 @@ export async function registerSessionProtocols(ctx: DeepReadonly<Context>, sessi
await app.whenReady();

for (const {scheme, directory} of ctx.locations.protocolBundles) {
await new Promise((resolve, reject) => {
session.protocol.registerBufferProtocol(
scheme,
async (request, callback) => {
const file = await resolveFileSystemResourceLocation(directory, request);
const data = await fsAsync.readFile(file);
const mimeType = mimeTypes.lookup(path.basename(file));
const result = mimeType
? {data, mimeType}
: data;

callback(result);
},
resolveOrRejectIfError(resolve, reject),
);
});
const registered = session.protocol.registerBufferProtocol(
scheme,
async (request, callback) => {
const file = await resolveFileSystemResourceLocation(directory, request);
const data = await fsAsync.readFile(file);
const mimeType = mimeTypes.lookup(path.basename(file));
const result = mimeType
? {data, mimeType}
: data;

callback(result);
},
);
if (!registered) {
throw new Error(`Failed to register "${scheme}" protocol mapped to "${directory}" directory`);
}
}
}

10 changes: 0 additions & 10 deletions src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,3 @@ export const resolvePackagedWebClientApp: (
return result;
}
)();

export const resolveOrRejectIfError = (resolve: () => void, reject: (error: Error) => void) => {
return (error: Error): void => {
if (error) {
reject(error);
return;
}
resolve();
};
};

0 comments on commit f1393f8

Please sign in to comment.