2020-07-13 16:58:49 +00:00
|
|
|
import { app, session } from 'electron/main';
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-07-12 10:27:15 +00:00
|
|
|
// Global protocol APIs.
|
2020-06-23 03:32:45 +00:00
|
|
|
const protocol = process._linkedBinding('electron_browser_protocol');
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-07-12 10:27:15 +00:00
|
|
|
// Fallback protocol APIs of default session.
|
2019-02-12 14:22:33 +00:00
|
|
|
Object.setPrototypeOf(protocol, new Proxy({}, {
|
2019-03-07 20:56:02 +00:00
|
|
|
get (_target, property) {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!app.isReady()) return;
|
2016-07-12 10:39:06 +00:00
|
|
|
|
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;
|
2016-07-12 10:39:06 +00:00
|
|
|
|
|
|
|
// 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);
|
2016-07-12 10:27:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
ownKeys () {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (!app.isReady()) return [];
|
2020-12-16 07:56:53 +00:00
|
|
|
return Reflect.ownKeys(session.defaultSession!.protocol);
|
|
|
|
},
|
2016-07-12 10:39:06 +00:00
|
|
|
|
2020-12-16 07:56:53 +00:00
|
|
|
has: (target, property: string) => {
|
|
|
|
if (!app.isReady()) return false;
|
|
|
|
return Reflect.has(session.defaultSession!.protocol, property);
|
2016-07-12 10:27:15 +00:00
|
|
|
},
|
2016-06-16 23:23:08 +00:00
|
|
|
|
2019-03-07 20:56:02 +00:00
|
|
|
getOwnPropertyDescriptor () {
|
2020-03-20 20:28:31 +00:00
|
|
|
return { configurable: true, enumerable: true };
|
2016-07-12 10:27:15 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
}));
|
2019-02-12 14:22:33 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
export default protocol;
|