electron/lib/browser/api/protocol.ts

30 lines
872 B
TypeScript
Raw Normal View History

import { app, session } from 'electron/main';
2016-01-12 02:40:23 +00:00
// Global protocol APIs.
const protocol = process._linkedBinding('electron_browser_protocol');
2016-01-12 02:40:23 +00:00
// Fallback protocol APIs of default session.
Object.setPrototypeOf(protocol, new Proxy({}, {
get (_target, property) {
2020-03-20 20:28:31 +00:00
if (!app.isReady()) return;
2020-03-20 20:28:31 +00:00
const protocol = session.defaultSession!.protocol;
2020-03-26 17:34:32 +00:00
if (!Object.prototype.hasOwnProperty.call(protocol, property)) return;
// Returning a native function directly would throw error.
2020-03-20 20:28:31 +00:00
return (...args: any[]) => (protocol[property as keyof Electron.Protocol] as Function)(...args);
},
ownKeys () {
2020-03-20 20:28:31 +00:00
if (!app.isReady()) return [];
2020-03-20 20:28:31 +00:00
return Object.getOwnPropertyNames(Object.getPrototypeOf(session.defaultSession!.protocol));
},
getOwnPropertyDescriptor () {
2020-03-20 20:28:31 +00:00
return { configurable: true, enumerable: true };
}
2020-03-20 20:28:31 +00:00
}));
2020-03-20 20:28:31 +00:00
export default protocol;