-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
104 lines (97 loc) · 2.67 KB
/
index.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// IMPORTS
import wasmBin from "./dist/interceptor.json";
import "./dist/wasm_exec.js";
// GLOBALS
let l8Ready = false;
let callbackObjectArray = [];
let layer8;
// UTILITY FUNCS
const decode = (encoded) => {
var str = atob(encoded);
var bytes = new Uint8Array(str.length);
for (var i = 0; i < str.length; i++) {
bytes[i] = str.charCodeAt(i);
}
return bytes.buffer;
};
function illGetBackToYou(_func_name, _resolve, _reject, _args) {
callbackObjectArray.push({
func_name: _func_name,
resolve: _resolve,
reject: _reject,
args: _args,
});
}
function triggerCallbacks() {
callbackObjectArray.forEach(async (callbackObject) => {
const func = callbackObject.func_name.split(".").reduce((acc, cur) => acc[cur], window);
const resolve = callbackObject.resolve;
const reject = callbackObject.reject;
const args = callbackObject.args;
try {
resolve(await func(...args));
} catch (error) {
reject(`Call to Layer8.${func.name} failed: ${error}`);
}
});
}
// MODULE LOAD & INITIAZLIZE
const go = new window.Go();
const importObject = go.importObject;
WebAssembly.instantiate(decode(wasmBin), importObject).then((result) => {
go.run(result.instance);
l8Ready = true;
layer8 = window.layer8;
triggerCallbacks();
});
// EXPORTS
export default {
testWASM: async (arg) => {
if (l8Ready) {
return await layer8.testWASM(arg);
}
return new Promise((resolve, reject) => {
illGetBackToYou("layer8.testWASM", resolve, reject, [arg]);
})
},
persistenceCheck: async () => {
if (l8Ready) {
return await layer8.persistenceCheck();
}
return new Promise((resolve, reject) => {
illGetBackToYou("layer8.persistenceCheck", resolve, reject, null);
})
},
initEncryptedTunnel: async (...arg) => {
if (l8Ready) {
return await layer8.initEncryptedTunnel(...arg);
}
return new Promise((resolve, reject) => {
illGetBackToYou("layer8.initEncryptedTunnel", resolve, reject, [...arg]);
})
},
checkEncryptedTunnel: async () => {
if (l8Ready) {
return await layer8.checkEncryptedTunnel();
}
return new Promise((resolve, reject) => {
illGetBackToYou("layer8.checkEncryptedTunnel", resolve, reject, null);
})
},
fetch: async (url, ...args) => {
if (l8Ready) {
return await layer8.fetch(url, ...args);
}
return new Promise((resolve, reject) => {
illGetBackToYou("layer8.fetch", resolve, reject, [url, ...args]);
})
},
static: async (url) => {
if (l8Ready) {
return await layer8.static(url);
}
return new Promise((resolve, reject) => {
illGetBackToYou("layer8.static", resolve, reject, [url]);
})
}
};