-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdio.ts
More file actions
81 lines (72 loc) · 2.24 KB
/
Copy pathstdio.ts
File metadata and controls
81 lines (72 loc) · 2.24 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
import { createInterface } from 'node:readline';
import type { Readable, Writable } from 'node:stream';
import { once } from 'node:events';
import {
decodeProtocolRequest,
encodeProtocolMessage,
type ProtocolNotification,
type ProtocolRequest,
type ProtocolResponse,
} from '@deepcode/protocol';
import type { AppServer } from './server.js';
type OutboundMessage = ProtocolResponse | ProtocolNotification;
export class ProtocolLineWriter {
private tail = Promise.resolve();
private failure: unknown;
private pending = 0;
constructor(
private readonly output: Writable,
private readonly maxPending = 1024,
) {}
enqueue(message: OutboundMessage): Promise<void> {
if (this.pending >= this.maxPending && isTransientDelta(message)) {
return Promise.resolve();
}
this.pending++;
const task = this.tail.then(async () => {
if (this.failure) throw this.failure;
const accepted = this.output.write(`${encodeProtocolMessage(message)}\n`);
if (!accepted) await once(this.output, 'drain');
});
this.tail = task.catch((error) => {
this.failure = error;
});
void task.then(
() => this.pending--,
() => this.pending--,
);
return task;
}
async flush(): Promise<void> {
await this.tail;
if (this.failure) throw this.failure;
}
}
export async function serveStdio(
server: AppServer,
input: Readable,
destination: Writable | ProtocolLineWriter,
): Promise<void> {
const writer =
destination instanceof ProtocolLineWriter ? destination : new ProtocolLineWriter(destination);
const lines = createInterface({ input, crlfDelay: Infinity });
for await (const line of lines) {
if (!line.trim()) continue;
let request: ProtocolRequest;
try {
request = decodeProtocolRequest(line);
} catch (error) {
await writer.enqueue({
id: null,
error: { code: 'parse_error', message: (error as Error).message ?? String(error) },
});
continue;
}
await writer.enqueue(await server.handle(request));
}
await server.shutdown();
await writer.flush();
}
function isTransientDelta(message: OutboundMessage): boolean {
return 'method' in message && message.method === 'event' && message.params.type === 'item.delta';
}