返回 DeepSeek-Reasonix
rpc.ts
根目录 / desktop / electron / src / main / rpc.ts
1 export const MAX_FRAME_BYTES = 64 * 1024 * 1024;
2
3 export class OversizeFrameError extends Error {
4 constructor(bytes: number, limit: number) {
5 super(`protocol frame of ${bytes} bytes exceeds the ${limit} byte limit`);
6 this.name = "OversizeFrameError";
7 }
8 }
9
10 export class LineDecoder {
11 private chunks: Buffer[] = [];
12 private buffered = 0;
13
14 constructor(private readonly limit = MAX_FRAME_BYTES) {}
15
16 push(chunk: Buffer): string[] {
17 const lines: string[] = [];
18 let start = 0;
19 for (let i = 0; i < chunk.length; i++) {
20 if (chunk[i] !== 0x0a) continue;
21 const tail = chunk.subarray(start, i);
22 const total = this.buffered + tail.length;
23 if (total > this.limit) {
24 this.reset();
25 throw new OversizeFrameError(total, this.limit);
26 }
27 const line = this.chunks.length ? Buffer.concat([...this.chunks, tail]).toString("utf8") : tail.toString("utf8");
28 this.reset();
29 lines.push(line.endsWith("\r") ? line.slice(0, -1) : line);
30 start = i + 1;
31 }
32 if (start < chunk.length) {
33 const rest = chunk.subarray(start);
34 this.buffered += rest.length;
35 if (this.buffered > this.limit) {
36 const bytes = this.buffered;
37 this.reset();
38 throw new OversizeFrameError(bytes, this.limit);
39 }
40 this.chunks.push(Buffer.from(rest));
41 }
42 return lines;
43 }
44
45 private reset(): void {
46 this.chunks = [];
47 this.buffered = 0;
48 }
49 }
50
51 export class RpcError extends Error {
52 constructor(readonly code: number, message: string, readonly data?: unknown) {
53 super(message);
54 this.name = "RpcError";
55 }
56 }
57
58 export interface RpcTransport {
59 write(line: string): void;
60 }
61
62 export interface RpcHandlers {
63 onRequest(method: string, params: unknown): Promise<unknown>;
64 onNotification(method: string, params: unknown): void;
65 onProtocolError?(kind: "non-json" | "invalid" | "orphan-response", line: string): void;
66 }
67
68 interface Pending {
69 resolve(value: unknown): void;
70 reject(error: Error): void;
71 timer: NodeJS.Timeout | null;
72 }
73
74 function isRecord(value: unknown): value is Record<string, unknown> {
75 return typeof value === "object" && value !== null && !Array.isArray(value);
76 }
77
78 export class RpcClient {
79 private nextId = 1;
80 private readonly pending = new Map<number, Pending>();
81 private readonly decoder: LineDecoder;
82 private closedWith: Error | null = null;
83 readonly stats = { ignoredLines: 0, orphanResponses: 0 };
84
85 constructor(private readonly transport: RpcTransport, private readonly handlers: RpcHandlers, private readonly limit = MAX_FRAME_BYTES) {
86 this.decoder = new LineDecoder(limit);
87 }
88
89 get closed(): boolean {
90 return this.closedWith !== null;
91 }
92
93 get pendingCount(): number {
94 return this.pending.size;
95 }
96
97 // Feeds raw stdout bytes; throws OversizeFrameError when the stream cannot
98 // be resynchronised, after rejecting everything in flight.
99 feed(chunk: Buffer): void {
100 let lines: string[];
101 try {
102 lines = this.decoder.push(chunk);
103 } catch (error) {
104 this.close(error instanceof Error ? error : new Error(String(error)));
105 throw error;
106 }
107 for (const line of lines) this.dispatch(line);
108 }
109
110 request(method: string, params: unknown, timeoutMs?: number): Promise<unknown> {
111 if (this.closedWith) return Promise.reject(this.closedWith);
112 const id = this.nextId++;
113 return new Promise((resolve, reject) => {
114 const timer = timeoutMs && timeoutMs > 0
115 ? setTimeout(() => {
116 this.pending.delete(id);
117 reject(new RpcError(-32000, `${method} timed out after ${timeoutMs} ms`));
118 }, timeoutMs)
119 : null;
120 this.pending.set(id, { resolve, reject, timer });
121 try {
122 this.writeFrame({ jsonrpc: "2.0", id, method, params });
123 } catch (error) {
124 this.pending.delete(id);
125 if (timer) clearTimeout(timer);
126 reject(error instanceof Error ? error : new Error(String(error)));
127 }
128 });
129 }
130
131 notify(method: string, params: unknown): void {
132 if (this.closedWith) return;
133 this.writeFrame({ jsonrpc: "2.0", method, params });
134 }
135
136 private writeFrame(frame: Record<string, unknown>): void {
137 const line = JSON.stringify(frame) + "\n";
138 const bytes = Buffer.byteLength(line);
139 if (bytes > this.limit) throw new OversizeFrameError(bytes, this.limit);
140 this.transport.write(line);
141 }
142
143 close(error: Error): void {
144 if (this.closedWith) return;
145 this.closedWith = error;
146 for (const [id, entry] of this.pending) {
147 this.pending.delete(id);
148 if (entry.timer) clearTimeout(entry.timer);
149 entry.reject(error);
150 }
151 }
152
153 private dispatch(line: string): void {
154 if (line.trim() === "") return;
155 let frame: unknown;
156 try {
157 frame = JSON.parse(line);
158 } catch {
159 this.stats.ignoredLines++;
160 this.handlers.onProtocolError?.("non-json", line);
161 return;
162 }
163 if (!isRecord(frame) || frame.jsonrpc !== "2.0") {
164 this.stats.ignoredLines++;
165 this.handlers.onProtocolError?.("invalid", line);
166 return;
167 }
168 if (typeof frame.method === "string") {
169 if (frame.id === undefined || frame.id === null) {
170 this.handlers.onNotification(frame.method, frame.params);
171 return;
172 }
173 this.serve(frame.id as number | string, frame.method, frame.params);
174 return;
175 }
176 if (typeof frame.id !== "number") {
177 this.stats.ignoredLines++;
178 this.handlers.onProtocolError?.("invalid", line);
179 return;
180 }
181 const entry = this.pending.get(frame.id);
182 if (!entry) {
183 this.stats.orphanResponses++;
184 this.handlers.onProtocolError?.("orphan-response", line);
185 return;
186 }
187 this.pending.delete(frame.id);
188 if (entry.timer) clearTimeout(entry.timer);
189 if (isRecord(frame.error)) {
190 const code = typeof frame.error.code === "number" ? frame.error.code : -32000;
191 const message = typeof frame.error.message === "string" ? frame.error.message : "unknown error";
192 entry.reject(new RpcError(code, message, frame.error.data));
193 return;
194 }
195 entry.resolve(frame.result);
196 }
197
198 private serve(id: number | string, method: string, params: unknown): void {
199 this.handlers.onRequest(method, params).then(
200 (result) => this.reply({ jsonrpc: "2.0", id, result: result === undefined ? null : result }),
201 (error: unknown) => {
202 const code = error instanceof RpcError ? error.code : -32000;
203 const message = error instanceof Error ? error.message : String(error);
204 this.reply({ jsonrpc: "2.0", id, error: { code, message, ...(error instanceof RpcError && error.data !== undefined ? { data: error.data } : {}) } });
205 },
206 );
207 }
208
209 private reply(frame: Record<string, unknown>): void {
210 if (this.closedWith) return;
211 try {
212 this.transport.write(JSON.stringify(frame) + "\n");
213 } catch {
214 // The transport owner observes the broken pipe through the process exit.
215 }
216 }
217 }
218
218 lines TYPESCRIPT