-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnm_typescript.ts
More file actions
165 lines (137 loc) · 4.78 KB
/
Copy pathnm_typescript.ts
File metadata and controls
165 lines (137 loc) · 4.78 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! TypeScript Native Messaging host
//! guest271314, 7-28-2024
//!
//! #!/usr/bin/env -S /home/user/bin/bun -b --expose-gc
//! #!/usr/bin/env -S /home/user/bin/deno -A --v8-flags="--expose-gc"
//! #!/usr/bin/env -S /home/user/bin/node --expose-gc
//!
//! Source JavaScript: https://github.com/guest271314/NativeMessagingHosts/blob/main/nm_host.js
//! https://github.com/microsoft/TypeScript/issues/62546#issuecomment-3374526284
import * as process from "node:process";
declare function gc(): void;
const stdin: NodeJS.ReadStream & { fd: 0 } = process.stdin;
const stdout: NodeJS.WriteStream & { fd: 1 } = process.stdout;
const exit: (n: number) => void = process.exit;
// Cast stdout to 'any' briefly to access internal _handle, or use a type guard
// (stdin as any)?._handle?.setBlocking?.(true);
// (stdout as any)?._handle?.setBlocking?.(true);
const buffer: ArrayBuffer = new ArrayBuffer(0, {
maxByteLength: 1024 ** 2 * 64,
});
const encoder: TextEncoder = new TextEncoder();
let totalMessageLength: number = 0;
let currentMessageLength: number = 0;
function encodeMessage(message: object): Uint8Array<ArrayBuffer> {
return encoder.encode(JSON.stringify(message));
}
async function* getMessage(): AsyncGenerator<Uint8Array<ArrayBuffer>> {
for await (const data of stdin) {
const chunk: Uint8Array<ArrayBuffer> = data;
if (
buffer.byteLength === 0 && totalMessageLength === 0 &&
currentMessageLength === 0
) {
totalMessageLength = chunk[3] << 24 | chunk[2] << 16 | chunk[1] << 8 | chunk[0];
(buffer as ArrayBuffer).resize(totalMessageLength);
const message: Uint8Array<ArrayBuffer> = chunk.subarray(4);
new Uint8Array(buffer).set(message, currentMessageLength);
currentMessageLength += message.length;
} else {
if (currentMessageLength < totalMessageLength) {
const u8: Uint8Array<ArrayBuffer> = new Uint8Array(buffer);
u8.set(chunk, currentMessageLength);
currentMessageLength += chunk.length;
}
}
if (currentMessageLength === totalMessageLength) {
yield new Uint8Array(buffer);
if (stdout?.writableNeedDrain) {
await new Promise((resolve) => {
stdout?.once("drain", resolve);
});
}
currentMessageLength = 0;
totalMessageLength = 0;
(buffer as ArrayBuffer).resize(0);
if (typeof gc === "function") gc();
}
}
}
function sendMessage(message: Uint8Array<ArrayBuffer>): void {
const COMMA: number = 44;
const OPEN_BRACKET: number = 91;
const CLOSE_BRACKET: number = 93;
const CHUNK_SIZE: number = 1024 * 1024;
if (message.length <= CHUNK_SIZE) {
stdout.write(new Uint8Array(new Uint32Array([message.length]).buffer));
stdout.write(message);
return;
}
let index: number = 0;
while (index < message.length) {
let splitIndex: number;
let searchStart: number = index + CHUNK_SIZE - 8;
if (searchStart >= message.length) {
splitIndex = message.length;
} else {
splitIndex = message.indexOf(COMMA, searchStart);
if (splitIndex === -1) {
splitIndex = message.length;
}
}
const rawChunk: Uint8Array<ArrayBuffer> = message.subarray(
index,
splitIndex,
);
const startByte: number = rawChunk[0];
const endByte: number = rawChunk[rawChunk.length - 1];
let prepend: number | null = null;
let append: number | null = null;
if (startByte === OPEN_BRACKET && endByte !== CLOSE_BRACKET) {
append = CLOSE_BRACKET;
} else if (startByte === COMMA) {
prepend = OPEN_BRACKET;
if (endByte !== CLOSE_BRACKET) {
append = CLOSE_BRACKET;
}
}
let bodyLength: number = rawChunk.length;
let sourceOffset: number = 0;
if (startByte === COMMA) {
sourceOffset = 1;
bodyLength -= 1;
}
const totalLength: number = 4 + (prepend !== null ? 1 : 0) + bodyLength +
(append !== null ? 1 : 0);
const output: Uint8Array<ArrayBuffer> = new Uint8Array(totalLength);
const dataPayloadLen: number = totalLength - 4;
output[0] = (dataPayloadLen >> 0) & 0xff;
output[1] = (dataPayloadLen >> 8) & 0xff;
output[2] = (dataPayloadLen >> 16) & 0xff;
output[3] = (dataPayloadLen >> 24) & 0xff;
let cursor: number = 4;
if (prepend !== null) {
output[cursor] = prepend;
cursor++;
} else if (startByte === COMMA) {
output[cursor] = OPEN_BRACKET;
cursor++;
}
output.set(rawChunk.subarray(sourceOffset), cursor);
cursor += bodyLength;
if (append !== null) {
output[cursor] = append;
}
stdout.write(output);
index = splitIndex;
}
}
async function main(): Promise<void> {
for await (const message of getMessage()) {
sendMessage(message);
}
}
main().catch((e: Error) => {
sendMessage(encodeMessage({ error: e.message }));
exit(1);
});