-
Notifications
You must be signed in to change notification settings - Fork 0
/
nm_deno.js
41 lines (36 loc) · 1.05 KB
/
nm_deno.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
#!/usr/bin/env -S ./deno run --v8-flags="--expose-gc,--jitless"
// Deno Native Messaging host
// guest271314, 10-5-2022
// https://github.com/denoland/deno/discussions/17236#discussioncomment-4566134
// https://github.com/saghul/txiki.js/blob/master/src/js/core/tjs/eval-stdin.js
async function readFullAsync(length) {
const buffer = new Uint8Array(65536);
const data = [];
let n = null;
while (data.length < length && (n = await Deno.stdin.read(buffer))) {
data.push(...buffer.subarray(0, n));
}
return new Uint8Array(data);
}
async function getMessage() {
const header = new Uint32Array(1);
await Deno.stdin.read(new Uint8Array(header.buffer));
return readFullAsync(header[0]);
}
async function sendMessage(message) {
const header = new Uint32Array([message.length]);
await Deno.stdout.write(new Uint8Array(header.buffer));
await Deno.stdout.write(message);
}
async function main() {
while (true) {
const message = await getMessage();
await sendMessage(message);
gc();
}
}
try {
main();
} catch (e) {
Deno.exit();
}