-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnm_standalone_test.js
114 lines (96 loc) · 2.89 KB
/
nm_standalone_test.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
105
106
107
108
109
110
111
112
113
114
// How to test the different hosts #2
// https://github.com/guest271314/NativeMessagingHosts/discussions/2
// deno -A nm_standalone_test.js ./nm_nodejs.js native-messaging-extension://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/
const [path, allowed_origin] = Deno.args;
const command = new Deno.Command(path, {
args: [allowed_origin],
stdout: "piped",
stdin: "piped",
});
const subprocess = command.spawn();
const buffer = new ArrayBuffer(0, { maxByteLength: 1024 ** 2 });
const view = new DataView(buffer);
const encoder = new TextEncoder();
const decoder = new TextDecoder();
function encodeMessage(message) {
return encoder.encode(JSON.stringify(message));
}
async function* getMessage(readable) {
let messageLength = 0;
let readOffset = 0;
for await (let message of readable) {
if (buffer.byteLength === 0 && messageLength === 0) {
buffer.resize(4);
for (let i = 0; i < 4; i++) {
view.setUint8(i, message[i]);
}
messageLength = view.getUint32(0, true);
console.log({ messageLength });
message = message.subarray(4);
buffer.resize(0);
}
if (message.length) {
buffer.resize(buffer.byteLength + message.length);
for (let i = 0; i < message.length; i++, readOffset++) {
view.setUint8(readOffset, message[i]);
}
if (buffer.byteLength === messageLength) {
yield new Uint8Array(buffer);
messageLength = 0;
readOffset = 0;
buffer.resize(0);
}
}
}
}
(async () => {
try {
for await (const message of getMessage(subprocess.stdout)) {
console.log(JSON.parse(decoder.decode(message)));
}
} catch (e) {
console.log(e.message);
Deno.exit();
}
})();
let data = encodeMessage(Array(209715));
await new Blob([
new Uint8Array(new Uint32Array([data.length]).buffer),
data,
])
.stream()
.pipeTo(subprocess.stdin, { preventClose: true });
await new Promise((resolve) => setTimeout(resolve, 20));
data = encodeMessage("test");
await new Blob([
new Uint8Array(new Uint32Array([data.length]).buffer),
data,
])
.stream()
.pipeTo(subprocess.stdin, { preventClose: true });
await new Promise((resolve) => setTimeout(resolve, 20));
data = encodeMessage("");
await new Blob([
new Uint8Array(new Uint32Array([data.length]).buffer),
data,
])
.stream()
.pipeTo(subprocess.stdin, { preventClose: true });
await new Promise((resolve) => setTimeout(resolve, 20));
data = encodeMessage(1);
await new Blob([
new Uint8Array(new Uint32Array([data.length]).buffer),
data,
])
.stream()
.pipeTo(subprocess.stdin, { preventClose: true });
await new Promise((resolve) => setTimeout(resolve, 20));
data = encodeMessage(new Uint8Array([97]));
await new Blob([
new Uint8Array(new Uint32Array([data.length]).buffer),
data,
])
.stream()
.pipeTo(subprocess.stdin, { preventClose: true});
await new Promise((resolve) => setTimeout(resolve, 20));
subprocess.kill("SIGTERM");