forked from open-source-labs/Swell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
64 lines (62 loc) · 1.89 KB
/
preload.js
File metadata and controls
64 lines (62 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { ipcRenderer, contextBridge } = require("electron");
const apiObj = {
send: (channel, ...data) => {
// allowlist channels SENDING to Main
const allowedChannels = [
"check-for-update",
"confirm-clear-history",
"export-collection",
"fatalError",
"import-collection",
"import-proto",
"open-http",
"close-http",
"open-gql",
"open-grpc",
"protoParserFunc-request",
"quit-and-install",
"uncaughtException",
"introspect",
"open-ws",
"send-ws",
"close-ws",
];
if (allowedChannels.includes(channel)) {
ipcRenderer.send(channel, ...data);
}
},
receive: (channel, cb) => {
// allowlist channels
const allowedChannels = [
"add-collection",
"clear-history-response",
"message",
"proto-info",
"protoParserFunc-return",
"reply-gql",
"reqResUpdate",
"introspect-reply",
"update-connectionArray",
];
if (allowedChannels.includes(channel)) {
ipcRenderer.on(channel, (event, ...args) => cb(...args));
}
},
removeAllListeners: (channel, cb) => {
// allowlist channels
const allowedChannels = ["reqResUpdate", "reply-gql"];
if (allowedChannels.includes(channel)) {
ipcRenderer.removeAllListeners(channel, (event, ...args) => cb(...args));
}
},
};
// this is because we need to have context isolation to be false for spectron tests to run, but context bridge only runs if context isolation is true
// basically we are assigning certain node functionality (require, ipcRenderer) to the window object in an UN-isolated context only for testing
// security is reduced for testing, but remains sturdy otherwise
if (process.env.NODE_ENV === "test") {
console.log("made it into here");
window.electronRequire = require;
window.api = apiObj;
} else {
contextBridge.exposeInMainWorld("api", apiObj);
}