-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnm_bun.js
66 lines (61 loc) · 1.68 KB
/
nm_bun.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
#!/usr/bin/env -S ./bun run --no-install --hot
// Bun Native Messaging host
// guest271314, 10-9-2022
async function getMessage() {
const { promise, resolve } = Promise.withResolvers();
// https://github.com/simov/native-messaging/blob/8e99d2a345ae94426a502d05aa5d57b966f6bc78/protocol.js
let messageLength = 0,
bytesWritten = 0,
input = [];
process.stdin.on("readable", () => {
let chunk;
while ((chunk = process.stdin.read())) {
// Set message value length once
if (messageLength === 0) {
[messageLength] = new Uint32Array(chunk.buffer.slice(0, 4));
chunk = chunk.subarray(4);
}
// Store accrued message length read
bytesWritten += chunk.length;
input.push(...chunk);
if (bytesWritten === messageLength) {
// Send accrued message from client back to client
resolve(new Uint8Array(input));
}
}
});
return await promise;
}
function sendMessage(json) {
// https://github.com/denoland/deno/discussions/17236#discussioncomment-4566134
const header = new Uint32Array([json.length]);
/*
// Long form
const header = new Uint32Array([
((uint32) =>
// https://stackoverflow.com/a/58288413
(uint32[3] << 24)
| (uint32[2] << 16)
| (uint32[1] << 8)
| (uint32[0])
)(Array.from({
length: 4,
}, (_,index)=>(json.length >> (index * 8)) & 0xff)
)
]);
*/
process.stdout.write(new Uint8Array(header.buffer));
process.stdout.write(json);
Bun.gc(true);
}
async function main() {
while (true) {
try {
const message = await getMessage();
sendMessage(message);
} catch (e) {
process.exit();
}
}
}
main();