-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathStdUtils.ts
More file actions
71 lines (66 loc) · 2.53 KB
/
Copy pathStdUtils.ts
File metadata and controls
71 lines (66 loc) · 2.53 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
import {Readable, Writable} from "node:stream";
import {Emitter} from "vscode-jsonrpc/node";
import type {DataCallback, Disposable, Message, MessageReader, MessageWriter, PartialMessageInfo} from "vscode-jsonrpc/node";
import * as acp from "@agentclientprotocol/sdk";
//TODO ask to include proper jsonrpc field and remove
export function createJSONRPCWriter(writable: Writable): MessageWriter {
return {
async write(msg: Message) {
try {
if (msg && typeof msg === 'object') {
// remove jsonrpc for the server
msg = {...msg};
delete (msg as any).jsonrpc;
}
writable.write(JSON.stringify(msg) + '\n');
} catch {/* ignore */
}
},
end() {
writable.end();
},
onError: new Emitter<[Error, Message | undefined, number | undefined]>().event,
onClose: new Emitter<void>().event,
dispose() { }
};
}
//TODO ask to include proper jsonrpc field and remove
export function createJSONRPCReader(readable: Readable): MessageReader {
return {
listen(callback: DataCallback): Disposable {
let buf = '';
const onData = (chunk: Buffer) => {
buf += chunk.toString();
for (;;) {
const i = buf.indexOf('\n');
if (i < 0) break;
const line = buf.slice(0, i).trim();
buf = buf.slice(i + 1);
if (!line) continue;
try {
const msg = JSON.parse(line);
if (msg && typeof msg === 'object' && msg.jsonrpc === undefined) {
msg.jsonrpc = '2.0';
}
callback(msg);
} catch {/* ignore malformed lines; they're still logged above */}
}
};
readable.on('data', onData);
return {
dispose() {
readable.off('data', onData);
}
}
},
onError: new Emitter<Error>().event,
onClose: new Emitter<void>().event,
onPartialMessage: new Emitter<PartialMessageInfo>().event,
dispose() {}
}
}
export function createJsonStream(readable: Readable, writable: Writable){
const input = Writable.toWeb(writable);
const output = Readable.toWeb(readable) as ReadableStream<Uint8Array>;
return acp.ndJsonStream(input, output);
}