electron/lib/browser/api/protocol.ts
Samuel Attard b02748e607
build: ensure that electron/lib/browser can only use browser or common imports (#24512)
* build: ensure that electron/lib/browser can only use browser or common imports

* chore: fix linting
2020-07-13 09:58:49 -07:00

29 lines
872 B
TypeScript

import { app, session } from 'electron/main';
// Global protocol APIs.
const protocol = process._linkedBinding('electron_browser_protocol');
// Fallback protocol APIs of default session.
Object.setPrototypeOf(protocol, new Proxy({}, {
get (_target, property) {
if (!app.isReady()) return;
const protocol = session.defaultSession!.protocol;
if (!Object.prototype.hasOwnProperty.call(protocol, property)) return;
// Returning a native function directly would throw error.
return (...args: any[]) => (protocol[property as keyof Electron.Protocol] as Function)(...args);
},
ownKeys () {
if (!app.isReady()) return [];
return Object.getOwnPropertyNames(Object.getPrototypeOf(session.defaultSession!.protocol));
},
getOwnPropertyDescriptor () {
return { configurable: true, enumerable: true };
}
}));
export default protocol;