-
Notifications
You must be signed in to change notification settings - Fork 0
/
nm_typescript.ts
119 lines (106 loc) · 3.52 KB
/
nm_typescript.ts
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
115
116
117
118
119
/*
#!/usr/bin/env -S /home/user/deno -A
#!/usr/bin/env -S /home/user/bun run
#!/usr/bin/env -S /home/user/node --experimental-transform-types
TypeScript Native Messaging host
guest271314, 7-28-2024
*/
/*
declare let readable: NodeJS.ReadStream & {
fd: 0;
} | ReadableStream<Uint8Array>, writable: WritableStream<Uint8Array>, exit: () => void;
declare function encodeMessage(message: object): Uint8Array;
declare function getMessage(): AsyncGenerator<Uint8Array>;
declare function sendMessage(message: Uint8Array): Promise<void>;
export { encodeMessage, exit, getMessage, readable, sendMessage, writable, };
*/
// Source JavaScript: https://github.com/guest271314/NativeMessagingHosts/blob/main/nm_host.js
//
// Convert JavaScript to TypeScript, no obvious equivalent with tsc
// https://www.codeconvert.ai/javascript-to-typescript-converter
//
// Resizable ArrayBuffer supported by tsc Version 5.7.0-dev.20241019
/**
* /// <reference types="https://raw.githubusercontent.com/microsoft/TypeScript/2ac4cb78d6930302eb0a55d07f154a2b0597ae32/src/lib/es2024.arraybuffer.d.ts" />
*/
import process from "node:process";
const runtime: string = navigator.userAgent;
const buffer: ArrayBuffer = new ArrayBuffer(0, { maxByteLength: 1024 ** 2 });
const view: DataView = new DataView(buffer);
const encoder: TextEncoder = new TextEncoder();
let readable: NodeJS.ReadStream & { fd: 0 } | ReadableStream<Uint8Array>,
writable: WritableStream<Uint8Array>,
exit: () => void = () => {};
if (runtime.startsWith("Deno")) {
// @ts-ignore Deno
({ readable } = Deno.stdin);
// @ts-ignore Deno
({ writable } = Deno.stdout);
// @ts-ignore Deno
({ exit } = Deno);
}
if (runtime.startsWith("Node")) {
readable = process.stdin;
writable = new WritableStream({
write(value) {
process.stdout.write(value);
},
}, new CountQueuingStrategy({ highWaterMark: Infinity }));
({ exit } = process);
}
if (runtime.startsWith("Bun")) {
// @ts-ignore Bun
readable = Bun.file("/dev/stdin").stream();
writable = new WritableStream<Uint8Array>({
async write(value) {
// @ts-ignore Bun
await Bun.write(Bun.stdout, value);
},
}, new CountQueuingStrategy({ highWaterMark: Infinity }));
({ exit } = process);
}
function encodeMessage(message: object): Uint8Array {
return encoder.encode(JSON.stringify(message));
}
async function* getMessage(): AsyncGenerator<Uint8Array> {
let messageLength: number = 0;
let readOffset: number = 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);
message = message.subarray(4);
buffer.resize(0);
}
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 function sendMessage(message: Uint8Array): Promise<void> {
await new Blob([
new Uint8Array(new Uint32Array([message.length]).buffer),
message,
])
.stream()
.pipeTo(writable, { preventClose: true });
}
try {
for await (const message of getMessage()) {
await sendMessage(message);
}
} catch (e: any) {
sendMessage(encodeMessage(e.message));
exit();
}
export { encodeMessage, exit, getMessage, readable, sendMessage, writable };