@zyno-io/ts-server-foundation 26.802.817 → 26.802.1639
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/devconsole/server.d.ts.map +1 -1
- package/dist/src/devconsole/server.js +1 -2
- package/dist/src/devconsole/server.js.map +1 -1
- package/dist/src/http/request.js +1 -1
- package/dist/src/http/response.js +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/services/mesh-client/mesh-srpc-server.js +1 -1
- package/dist/src/srpc/SrpcByteStream.d.ts +2 -0
- package/dist/src/srpc/SrpcByteStream.d.ts.map +1 -1
- package/dist/src/srpc/SrpcByteStream.js +19 -0
- package/dist/src/srpc/SrpcByteStream.js.map +1 -1
- package/dist/src/srpc/SrpcClient.d.ts +1 -0
- package/dist/src/srpc/SrpcClient.d.ts.map +1 -1
- package/dist/src/srpc/SrpcClient.js +181 -72
- package/dist/src/srpc/SrpcClient.js.map +1 -1
- package/dist/src/srpc/SrpcServer.d.ts +4 -1
- package/dist/src/srpc/SrpcServer.d.ts.map +1 -1
- package/dist/src/srpc/SrpcServer.js +279 -141
- package/dist/src/srpc/SrpcServer.js.map +1 -1
- package/dist/src/srpc/index.js +1 -1
- package/dist/src/srpc/types.d.ts +2 -2
- package/dist/src/srpc/types.d.ts.map +1 -1
- package/dist/src/srpc/types.js +17 -1
- package/dist/src/srpc/types.js.map +1 -1
- package/docs/content/srpc.md +6 -6
- package/package.json +1 -1
|
@@ -8,6 +8,8 @@ const node_async_hooks_1 = require("node:async_hooks");
|
|
|
8
8
|
const ws_1 = tslib_1.__importDefault(require("ws"));
|
|
9
9
|
const app_1 = require("../app");
|
|
10
10
|
const helpers_1 = require("../helpers");
|
|
11
|
+
const services_1 = require("../services");
|
|
12
|
+
const telemetry_1 = require("../telemetry");
|
|
11
13
|
const SrpcByteStream_1 = require("./SrpcByteStream");
|
|
12
14
|
const types_1 = require("./types");
|
|
13
15
|
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":2},{\"kind\":18,\"types\":[{\"kind\":20,\"name\":\"supersede\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true}]},{\"kind\":22,\"type\":{\"kind\":3}},{\"kind\":3},{\"kind\":18,\"typeName\":\"IByteStream\",\"types\":[{\"kind\":20,\"name\":\"parentStreamId\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"remoteSenderIdParity\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":0},{\"kind\":10,\"literal\":1}]},{\"kind\":4}]},\"optional\":true}]},{\"kind\":2,\"typeName\":\"() =\\u003e void\"},{\"kind\":2,\"typeName\":\"P\"},{\"kind\":2,\"typeName\":\"(data: HandlerRequestData\\u003cTServerOutput, P\\u003e) =\\u003e Promise\\u003cResponseData\\u003cTClientInput, P\\u003e\\u003e | ResponseData\\u003cTClientInput, P\\u003e\"},{\"kind\":2,\"typeName\":\"(cause: SrpcDisconnectCause) =\\u003e void\"},{\"kind\":0},{\"kind\":18,\"typeName\":\"ResponseData\\u003cTServerOutput, P\\u003e\",\"types\":[{\"kind\":20,\"name\":\"\u00FE@toStringTag@64\",\"type\":{\"kind\":6},\"optional\":false}]},{\"kind\":18,\"typeName\":\"ISrpcLogger\",\"types\":[]},{\"kind\":6},{\"kind\":18,\"typeName\":\"SrpcMessageFns\",\"types\":[]},{\"kind\":17,\"typeName\":\"SrpcMeta\"},{\"kind\":18,\"typeName\":\"SrpcClientOptions\",\"types\":[{\"kind\":20,\"name\":\"enableReconnect\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"logTraffic\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":18,\"typeName\":\"SrpcTrafficLoggingOptions\",\"types\":[{\"kind\":20,\"name\":\"bodies\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true}]}],\"typeName\":\"SrpcTrafficLogging\"},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"authAudience\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"senderAnnouncements\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"maxPendingRequests\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"maxPendingRequestBytes\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"maxInFlightServerRequests\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"maxInFlightServerRequestBytes\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"maxBufferedBytes\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"maxMessageBytes\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"connectTimeoutMs\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"lateReplyTombstoneTtlMs\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true}]}]]", []);
|
|
@@ -102,7 +104,7 @@ class SrpcClient {
|
|
|
102
104
|
handler('supersede');
|
|
103
105
|
}
|
|
104
106
|
catch (error) {
|
|
105
|
-
this.logger.error('SRPC disconnect handler failed', error);
|
|
107
|
+
this.logger.error('SRPC disconnect handler failed', error, { srpc: { ...this.logData(), cause: 'supersede' } });
|
|
106
108
|
}
|
|
107
109
|
}
|
|
108
110
|
}
|
|
@@ -122,6 +124,7 @@ class SrpcClient {
|
|
|
122
124
|
this.byteStreamsByGeneration.set(generation, this.currentByteStream);
|
|
123
125
|
this.currentByteStream.parentStreamId = this.streamId;
|
|
124
126
|
SrpcByteStream_1.SrpcByteStream.init({ byteStream: this.currentByteStream }, { startId: 1, step: 2 });
|
|
127
|
+
this.logger.info('Connecting SRPC client', { srpc: { ...this.logData(), supersede: this.supersede } });
|
|
125
128
|
const ws = new ws_1.default(this.generateWsUrl(), { maxPayload: this.maxMessageBytes });
|
|
126
129
|
ws.binaryType = 'nodebuffer';
|
|
127
130
|
this.ws = ws;
|
|
@@ -135,9 +138,11 @@ class SrpcClient {
|
|
|
135
138
|
connectTimeout = setTimeout(() => {
|
|
136
139
|
if (!this.isCurrent(ws, generation))
|
|
137
140
|
return;
|
|
141
|
+
this.logger.warn('SRPC client connection timed out', { srpc: this.logData() });
|
|
138
142
|
this.revokeGeneration(ws, generation, 'timeout', false, new Error('Connection failed: timeout'));
|
|
139
143
|
ws.close();
|
|
140
144
|
}, this.connectTimeoutMs);
|
|
145
|
+
ws.once('open', () => this.logger.debug('SRPC WebSocket connection opened; waiting for handshake', { srpc: this.logData() }));
|
|
141
146
|
ws.once('message', data => this.handleInitialHandshake(ws, generation, data, clearConnectTimeout));
|
|
142
147
|
ws.on('close', (code, reason) => this.handleClose(ws, generation, code, reason, clearConnectTimeout));
|
|
143
148
|
ws.on('error', error => this.handleError(ws, generation, error, clearConnectTimeout));
|
|
@@ -161,6 +166,7 @@ class SrpcClient {
|
|
|
161
166
|
clearInterval(this.pingInterval);
|
|
162
167
|
this.pingInterval = undefined;
|
|
163
168
|
if (ws && this.isCurrent(ws, generation)) {
|
|
169
|
+
this.logger.debug('Disconnecting SRPC client', { srpc: this.logData() });
|
|
164
170
|
this.revokeGeneration(ws, generation, 'disconnect', true, new Error('Connection closed by client'));
|
|
165
171
|
}
|
|
166
172
|
ws?.close(1000, 'Client disconnect');
|
|
@@ -168,6 +174,7 @@ class SrpcClient {
|
|
|
168
174
|
triggerConnectionCheck() {
|
|
169
175
|
if (!this.isConnected)
|
|
170
176
|
return;
|
|
177
|
+
this.logger.debug('Triggering SRPC connection health check', { srpc: this.logData() });
|
|
171
178
|
this.lastPongMs = Date.now() - 20000;
|
|
172
179
|
this.writeMessage({ pingPong: {} });
|
|
173
180
|
}
|
|
@@ -176,16 +183,18 @@ class SrpcClient {
|
|
|
176
183
|
return;
|
|
177
184
|
const message = this.decodeMessage(data);
|
|
178
185
|
if (!message?.pingPong) {
|
|
186
|
+
this.logger.warn('SRPC client received an invalid initial handshake frame', { srpc: this.logData() });
|
|
179
187
|
this.closeGenerationWithError(ws, generation, 'badArg', 'Expected handshake ping');
|
|
180
188
|
return;
|
|
181
189
|
}
|
|
182
190
|
this.lastPongMs = Date.now();
|
|
191
|
+
this.logger.debug('SRPC client received initial handshake ping', { srpc: this.logData() });
|
|
183
192
|
if (!this.writeMessage({ pingPong: {} }, generation) || !this.isCurrent(ws, generation))
|
|
184
193
|
return;
|
|
185
194
|
this.awaitingActivation = { ws, generation, clearConnectTimeout };
|
|
186
195
|
ws.on('message', msgData => {
|
|
187
196
|
void this.handleMessage(ws, generation, msgData).catch(error => {
|
|
188
|
-
this.logger.warn('Failed to dispatch SRPC message', error);
|
|
197
|
+
this.logger.warn('Failed to dispatch SRPC message', error, { srpc: this.logData() });
|
|
189
198
|
this.closeGenerationWithError(ws, generation, 'badArg', 'Invalid message format');
|
|
190
199
|
});
|
|
191
200
|
});
|
|
@@ -202,24 +211,33 @@ class SrpcClient {
|
|
|
202
211
|
this.connectResolve?.();
|
|
203
212
|
this.connectResolve = undefined;
|
|
204
213
|
this.connectReject = undefined;
|
|
214
|
+
this.logger.info('SRPC client stream established', { srpc: this.logData() });
|
|
205
215
|
for (const handler of this.streamConnectionHandlers) {
|
|
206
216
|
try {
|
|
207
217
|
handler();
|
|
208
218
|
}
|
|
209
219
|
catch (error) {
|
|
210
|
-
this.logger.error('SRPC connection handler failed', error);
|
|
220
|
+
this.logger.error('SRPC connection handler failed', error, { srpc: this.logData() });
|
|
211
221
|
}
|
|
212
222
|
}
|
|
213
223
|
}
|
|
214
|
-
handleClose(ws, generation, code,
|
|
224
|
+
handleClose(ws, generation, code, reason, clearConnectTimeout) {
|
|
215
225
|
if (!this.isCurrent(ws, generation))
|
|
216
226
|
return;
|
|
217
227
|
clearConnectTimeout();
|
|
218
228
|
const cause = parseDisconnectCause(code);
|
|
219
229
|
if (cause === 'conflict') {
|
|
230
|
+
this.logger.warn('SRPC client connection rejected because its client ID is already active', {
|
|
231
|
+
srpc: { ...this.logData(), code, reason: logSafeText(reason.toString('utf8'), 123) }
|
|
232
|
+
});
|
|
220
233
|
this.revokeGeneration(ws, generation, cause, true, new SrpcConflictError());
|
|
221
234
|
return;
|
|
222
235
|
}
|
|
236
|
+
if (!this.intentionalDisconnect) {
|
|
237
|
+
this.logger.info('SRPC client stream ended', {
|
|
238
|
+
srpc: { ...this.logData(), cause, code, reason: logSafeText(reason.toString('utf8'), 123) }
|
|
239
|
+
});
|
|
240
|
+
}
|
|
223
241
|
this.revokeGeneration(ws, generation, cause);
|
|
224
242
|
}
|
|
225
243
|
handleError(ws, generation, error, clearConnectTimeout) {
|
|
@@ -227,7 +245,7 @@ class SrpcClient {
|
|
|
227
245
|
return;
|
|
228
246
|
clearConnectTimeout();
|
|
229
247
|
if (!this.intentionalDisconnect)
|
|
230
|
-
this.logger.warn('SRPC WebSocket error', error);
|
|
248
|
+
this.logger.warn('SRPC WebSocket error', error, { srpc: this.logData() });
|
|
231
249
|
this.revokeGeneration(ws, generation, 'disconnect');
|
|
232
250
|
ws.terminate();
|
|
233
251
|
}
|
|
@@ -254,7 +272,7 @@ class SrpcClient {
|
|
|
254
272
|
handler(cause);
|
|
255
273
|
}
|
|
256
274
|
catch (error) {
|
|
257
|
-
this.logger.error('SRPC disconnect handler failed', error);
|
|
275
|
+
this.logger.error('SRPC disconnect handler failed', error, { srpc: { ...this.logData(), cause } });
|
|
258
276
|
}
|
|
259
277
|
}
|
|
260
278
|
}
|
|
@@ -270,6 +288,7 @@ class SrpcClient {
|
|
|
270
288
|
queueReconnect() {
|
|
271
289
|
if (!this.enableReconnect)
|
|
272
290
|
return;
|
|
291
|
+
this.logger.debug('Scheduling SRPC client reconnect', { srpc: this.logData() });
|
|
273
292
|
this.reconnectionTimeout = setTimeout(() => {
|
|
274
293
|
this.reconnectionTimeout = undefined;
|
|
275
294
|
this.connect({ supersede: this.hasEstablishedGeneration }).catch(() => { });
|
|
@@ -279,8 +298,10 @@ class SrpcClient {
|
|
|
279
298
|
doPingPong() {
|
|
280
299
|
if (this.lastPongMs < Date.now() - 75000) {
|
|
281
300
|
const ws = this.ws;
|
|
282
|
-
if (ws)
|
|
301
|
+
if (ws) {
|
|
302
|
+
this.logger.warn('SRPC client pong timed out', { srpc: this.logData() });
|
|
283
303
|
this.closeGenerationWithError(ws, this.generation, 'timeout', 'Pong timeout');
|
|
304
|
+
}
|
|
284
305
|
return;
|
|
285
306
|
}
|
|
286
307
|
this.writeMessage({ pingPong: {} });
|
|
@@ -290,11 +311,13 @@ class SrpcClient {
|
|
|
290
311
|
return;
|
|
291
312
|
const frameBytes = toBuffer(data).byteLength;
|
|
292
313
|
if (frameBytes > this.maxMessageBytes) {
|
|
314
|
+
this.logger.warn('SRPC client received a message exceeding its configured limit', { srpc: { ...this.logData(), frameBytes } });
|
|
293
315
|
this.closeGenerationWithError(ws, generation, 'badArg', 'SRPC message too large');
|
|
294
316
|
return;
|
|
295
317
|
}
|
|
296
318
|
const message = this.decodeMessage(data);
|
|
297
319
|
if (!message) {
|
|
320
|
+
this.logger.warn('SRPC client failed to decode a message', { srpc: this.logData() });
|
|
298
321
|
this.closeGenerationWithError(ws, generation, 'badArg', 'Invalid message format');
|
|
299
322
|
return;
|
|
300
323
|
}
|
|
@@ -311,6 +334,7 @@ class SrpcClient {
|
|
|
311
334
|
return;
|
|
312
335
|
if (message.byteStreamOperation) {
|
|
313
336
|
if (!this.validRemoteByteStreamOperation(message.byteStreamOperation)) {
|
|
337
|
+
this.logger.warn('SRPC client received an invalid byte-stream operation', { srpc: this.logData() });
|
|
314
338
|
this.closeGenerationWithError(ws, generation, 'badArg', 'Invalid byte stream ID');
|
|
315
339
|
return;
|
|
316
340
|
}
|
|
@@ -318,6 +342,7 @@ class SrpcClient {
|
|
|
318
342
|
return;
|
|
319
343
|
}
|
|
320
344
|
if (!message.requestId) {
|
|
345
|
+
this.logger.warn('SRPC client received a message without a request ID', { srpc: this.logData() });
|
|
321
346
|
this.closeGenerationWithError(ws, generation, 'badArg', 'Invalid request ID');
|
|
322
347
|
return;
|
|
323
348
|
}
|
|
@@ -328,6 +353,14 @@ class SrpcClient {
|
|
|
328
353
|
const bytes = frameBytes;
|
|
329
354
|
const pressure = this.handlerPressureByGeneration.get(generation) ?? { requests: 0, bytes: 0 };
|
|
330
355
|
if (pressure.requests >= this.maxInFlightServerRequests || pressure.bytes + bytes > this.maxInFlightServerRequestBytes) {
|
|
356
|
+
this.logger.warn('SRPC client exceeded its in-flight server request limit', {
|
|
357
|
+
srpc: {
|
|
358
|
+
...this.logData(),
|
|
359
|
+
requestId: logSafeText(message.requestId),
|
|
360
|
+
inFlightRequests: pressure.requests,
|
|
361
|
+
inFlightBytes: pressure.bytes
|
|
362
|
+
}
|
|
363
|
+
});
|
|
331
364
|
this.closeGenerationWithError(ws, generation, 'badArg', 'Too many in-flight server requests');
|
|
332
365
|
return;
|
|
333
366
|
}
|
|
@@ -355,12 +388,16 @@ class SrpcClient {
|
|
|
355
388
|
const accepted = SrpcByteStream_1.SrpcByteStream.writeReceiver(this, op.streamId, op.write.chunk);
|
|
356
389
|
this.updateByteStreamBufferedBytes(generation, op.streamId, SrpcByteStream_1.SrpcByteStream.getReceiverBufferedBytes(this, op.streamId));
|
|
357
390
|
if (!accepted && wasBackpressured) {
|
|
391
|
+
this.logger?.warn('SRPC client byte-stream receiver is not draining', { srpc: { ...this.logData(), byteStreamId: op.streamId } });
|
|
358
392
|
SrpcByteStream_1.SrpcByteStream.abortReceiver(this, op.streamId, new Error(`SRPC byte stream ${op.streamId} receiver is not draining`));
|
|
359
393
|
this.clearByteStreamPressure(generation, op.streamId);
|
|
360
394
|
return;
|
|
361
395
|
}
|
|
362
396
|
if (!accepted) {
|
|
363
397
|
if (state.backpressured.size >= MaxBackpressuredByteStreams) {
|
|
398
|
+
this.logger?.warn('SRPC client has too many backpressured byte streams', {
|
|
399
|
+
srpc: { ...this.logData(), byteStreamId: op.streamId, backpressuredStreams: state.backpressured.size }
|
|
400
|
+
});
|
|
364
401
|
SrpcByteStream_1.SrpcByteStream.abortReceiver(this, op.streamId, new Error('Too many backpressured SRPC byte streams'));
|
|
365
402
|
this.clearByteStreamPressure(generation, op.streamId);
|
|
366
403
|
return;
|
|
@@ -371,6 +408,9 @@ class SrpcClient {
|
|
|
371
408
|
state.backpressured.delete(op.streamId);
|
|
372
409
|
}
|
|
373
410
|
if (this.totalByteStreamBufferedBytes(state) > this.maxBufferedBytes) {
|
|
411
|
+
this.logger?.warn('SRPC client byte-stream buffer limit exceeded', {
|
|
412
|
+
srpc: { ...this.logData(), byteStreamId: op.streamId, bufferedBytes: this.totalByteStreamBufferedBytes(state) }
|
|
413
|
+
});
|
|
374
414
|
SrpcByteStream_1.SrpcByteStream.abortReceiver(this, op.streamId, new Error('Too many buffered SRPC byte stream bytes'));
|
|
375
415
|
this.clearByteStreamPressure(generation, op.streamId);
|
|
376
416
|
}
|
|
@@ -387,8 +427,15 @@ class SrpcClient {
|
|
|
387
427
|
handleReply(ws, generation, requestId, message) {
|
|
388
428
|
const queueItem = this.requestQueue.get(requestId);
|
|
389
429
|
if (!queueItem) {
|
|
390
|
-
if (this.isLateReply(requestId))
|
|
430
|
+
if (this.isLateReply(requestId)) {
|
|
431
|
+
this.logger?.debug('Ignoring reply for an expired SRPC client request', {
|
|
432
|
+
srpc: { ...this.logData(), requestId: logSafeText(requestId) }
|
|
433
|
+
});
|
|
391
434
|
return;
|
|
435
|
+
}
|
|
436
|
+
this.logger?.warn('SRPC client received a reply for an unknown request', {
|
|
437
|
+
srpc: { ...this.logData(), requestId: logSafeText(requestId) }
|
|
438
|
+
});
|
|
392
439
|
this.closeGenerationWithError(ws, generation, 'badArg', 'Unknown request ID');
|
|
393
440
|
return;
|
|
394
441
|
}
|
|
@@ -404,23 +451,31 @@ class SrpcClient {
|
|
|
404
451
|
const requestData = message[key];
|
|
405
452
|
if (requestData == null)
|
|
406
453
|
continue;
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
454
|
+
const trace = (0, types_1.isValidSrpcTrace)(message.trace) ? message.trace : undefined;
|
|
455
|
+
const logMeta = { ...this.logData(), requestId: logSafeText(requestId), requestType: key, traceId: trace?.traceId };
|
|
456
|
+
const response = await (0, telemetry_1.withRemoteSpan)('srpc:handleServerRequest', trace, logMeta, () => (0, services_1.withLoggerContext)({ srpc: logMeta }, async () => {
|
|
457
|
+
try {
|
|
458
|
+
this.logger?.info('SRPC server request received');
|
|
459
|
+
const result = await this.handlerRequestId.run({ requestId, generation }, () => handlerMeta.handler(requestData));
|
|
460
|
+
this.logger?.info('SRPC server request processed');
|
|
461
|
+
return { [handlerMeta.resultType]: result };
|
|
462
|
+
}
|
|
463
|
+
catch (error) {
|
|
464
|
+
if (error instanceof types_1.SrpcError && error.isUserError) {
|
|
465
|
+
this.logger?.info('SRPC server request returned a user error', {
|
|
466
|
+
srpc: { ...logMeta, error: logSafeText(error.message) }
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
this.logger?.warn('SRPC server request failed', error);
|
|
471
|
+
}
|
|
472
|
+
throw error;
|
|
473
|
+
}
|
|
474
|
+
})).catch(error => ({ ...(0, types_1.serializeSrpcError)(error) }));
|
|
475
|
+
this.writeMessage({ requestId, reply: true, ...response }, generation);
|
|
422
476
|
return;
|
|
423
477
|
}
|
|
478
|
+
this.logger?.error('Unhandled SRPC server message type', { srpc: { ...this.logData(), requestId: logSafeText(requestId) } });
|
|
424
479
|
this.writeMessage({ requestId, reply: true, error: 'Unhandled message type' }, generation);
|
|
425
480
|
}
|
|
426
481
|
generateWsUrl() {
|
|
@@ -479,7 +534,7 @@ class SrpcClient {
|
|
|
479
534
|
return this.serverMessage.decode(toBuffer(data));
|
|
480
535
|
}
|
|
481
536
|
catch (error) {
|
|
482
|
-
this.logger.error('Failed to decode SRPC message', error);
|
|
537
|
+
this.logger.error('Failed to decode SRPC message', error, { srpc: this.logData() });
|
|
483
538
|
return null;
|
|
484
539
|
}
|
|
485
540
|
}
|
|
@@ -489,7 +544,7 @@ class SrpcClient {
|
|
|
489
544
|
encoded = (0, types_1.encodeSrpcMessage)(this.clientMessage, message);
|
|
490
545
|
}
|
|
491
546
|
catch (error) {
|
|
492
|
-
this.logger.error('Failed to encode SRPC message', error);
|
|
547
|
+
this.logger.error('Failed to encode SRPC message', error, { srpc: this.logData() });
|
|
493
548
|
return false;
|
|
494
549
|
}
|
|
495
550
|
return this.writeEncodedMessage(message, encoded, generation);
|
|
@@ -499,6 +554,7 @@ class SrpcClient {
|
|
|
499
554
|
if (!ws || !this.isCurrent(ws, generation) || ws.readyState !== ws_1.default.OPEN)
|
|
500
555
|
return false;
|
|
501
556
|
if (encoded.byteLength > this.maxMessageBytes || (ws.bufferedAmount ?? 0) + encoded.byteLength > this.maxBufferedBytes) {
|
|
557
|
+
this.logger.warn('SRPC client outgoing buffer limit exceeded', { srpc: { ...this.logData(), messageBytes: encoded.byteLength } });
|
|
502
558
|
this.closeGenerationWithError(ws, generation, 'badArg', 'sRPC stream outgoing buffer limit exceeded');
|
|
503
559
|
return false;
|
|
504
560
|
}
|
|
@@ -508,7 +564,7 @@ class SrpcClient {
|
|
|
508
564
|
return true;
|
|
509
565
|
}
|
|
510
566
|
catch (error) {
|
|
511
|
-
this.logger.warn('Failed to send SRPC message', error);
|
|
567
|
+
this.logger.warn('Failed to send SRPC message', error, { srpc: this.logData() });
|
|
512
568
|
this.closeGenerationWithError(ws, generation, 'badArg', 'Failed to send SRPC message');
|
|
513
569
|
return false;
|
|
514
570
|
}
|
|
@@ -517,8 +573,16 @@ class SrpcClient {
|
|
|
517
573
|
const ws = this.ws;
|
|
518
574
|
if (!ws || !this.isCurrent(ws, generation) || ws.readyState !== ws_1.default.OPEN)
|
|
519
575
|
return Promise.reject(new Error('Failed to send SRPC message: not connected'));
|
|
520
|
-
|
|
576
|
+
let encoded;
|
|
577
|
+
try {
|
|
578
|
+
encoded = (0, types_1.encodeSrpcMessage)(this.clientMessage, message);
|
|
579
|
+
}
|
|
580
|
+
catch (error) {
|
|
581
|
+
this.logger.error('Failed to encode SRPC message', error, { srpc: this.logData() });
|
|
582
|
+
return Promise.reject(error);
|
|
583
|
+
}
|
|
521
584
|
if (encoded.byteLength > this.maxMessageBytes || (ws.bufferedAmount ?? 0) + encoded.byteLength > this.maxBufferedBytes) {
|
|
585
|
+
this.logger.warn('SRPC client outgoing buffer limit exceeded', { srpc: { ...this.logData(), messageBytes: encoded.byteLength } });
|
|
522
586
|
this.closeGenerationWithError(ws, generation, 'badArg', 'sRPC stream outgoing buffer limit exceeded');
|
|
523
587
|
return Promise.reject(new types_1.SrpcBackpressureError('sRPC stream outgoing buffer limit exceeded'));
|
|
524
588
|
}
|
|
@@ -526,6 +590,7 @@ class SrpcClient {
|
|
|
526
590
|
try {
|
|
527
591
|
ws.send(encoded, error => {
|
|
528
592
|
if (error) {
|
|
593
|
+
this.logger.warn('Failed to send SRPC message', error, { srpc: this.logData() });
|
|
529
594
|
this.closeGenerationWithError(ws, generation, 'badArg', 'Failed to send SRPC message');
|
|
530
595
|
reject(error);
|
|
531
596
|
}
|
|
@@ -538,6 +603,7 @@ class SrpcClient {
|
|
|
538
603
|
});
|
|
539
604
|
}
|
|
540
605
|
catch (error) {
|
|
606
|
+
this.logger.warn('Failed to send SRPC message', error, { srpc: this.logData() });
|
|
541
607
|
this.closeGenerationWithError(ws, generation, 'badArg', 'Failed to send SRPC message');
|
|
542
608
|
reject(error instanceof Error ? error : new Error(String(error)));
|
|
543
609
|
}
|
|
@@ -556,14 +622,19 @@ class SrpcClient {
|
|
|
556
622
|
...(bodies ? { body: message } : {})
|
|
557
623
|
});
|
|
558
624
|
}
|
|
625
|
+
logData() {
|
|
626
|
+
return { clientId: this.clientId, streamId: this.streamId, generation: this.generation };
|
|
627
|
+
}
|
|
559
628
|
get byteStream() {
|
|
560
629
|
return this.resolveByteStream();
|
|
561
630
|
}
|
|
562
631
|
createByteStream(generation) {
|
|
632
|
+
const streamId = this.streamId;
|
|
563
633
|
return {
|
|
564
|
-
parentStreamId:
|
|
634
|
+
parentStreamId: streamId,
|
|
565
635
|
remoteSenderIdParity: 0,
|
|
566
636
|
receiverBufferChanged: (streamId, bufferedBytes) => this.updateByteStreamBufferedBytes(generation, streamId, bufferedBytes),
|
|
637
|
+
logDebug: (message, data) => this.logger?.debug(message, { srpc: { clientId: this.clientId, streamId, generation, ...data } }),
|
|
567
638
|
write: (streamId, chunk) => this.writeMessageAsync({ byteStreamOperation: { streamId, write: { chunk: new Uint8Array(chunk) } } }, generation),
|
|
568
639
|
finish: streamId => this.writeMessage({ byteStreamOperation: { streamId, finish: {} } }, generation),
|
|
569
640
|
destroy: (streamId, error) => this.writeMessage({ byteStreamOperation: { streamId, destroy: { error: (0, SrpcByteStream_1.byteStreamDestroyReason)(error) } } }, generation),
|
|
@@ -607,7 +678,7 @@ class SrpcClient {
|
|
|
607
678
|
handler();
|
|
608
679
|
}
|
|
609
680
|
catch (error) {
|
|
610
|
-
this.logger.error('SRPC byte stream disconnect handler failed', error);
|
|
681
|
+
this.logger.error('SRPC byte stream disconnect handler failed', error, { srpc: this.logData() });
|
|
611
682
|
}
|
|
612
683
|
}
|
|
613
684
|
}
|
|
@@ -632,53 +703,69 @@ class SrpcClient {
|
|
|
632
703
|
const requestType = `${prefix}Request`;
|
|
633
704
|
const resultType = `${prefix}Response`;
|
|
634
705
|
const requestId = (0, helpers_1.uuid7)();
|
|
635
|
-
const
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
706
|
+
const spanMeta = { ...this.logData(), requestId, requestType };
|
|
707
|
+
return (0, telemetry_1.withSpan)('srpc:invokeServer', spanMeta, () => {
|
|
708
|
+
const trace = toWireTrace((0, telemetry_1.getTraceContext)());
|
|
709
|
+
const logMeta = { ...spanMeta, traceId: trace?.traceId };
|
|
710
|
+
return (0, services_1.withLoggerContext)({ srpc: logMeta }, async () => {
|
|
711
|
+
try {
|
|
712
|
+
this.logger?.info('Requesting SRPC server invocation');
|
|
713
|
+
const message = { requestId, trace, [requestType]: data };
|
|
714
|
+
const encoded = (0, types_1.encodeSrpcMessage)(this.clientMessage, message);
|
|
715
|
+
const encodedBytes = encoded.byteLength;
|
|
716
|
+
if (encodedBytes > this.maxMessageBytes)
|
|
717
|
+
throw new types_1.SrpcBackpressureError('sRPC request exceeds message limit');
|
|
718
|
+
if (this.requestQueue.size >= this.maxPendingRequests || this.pendingRequestBytes + encodedBytes > this.maxPendingRequestBytes) {
|
|
719
|
+
throw new types_1.SrpcBackpressureError('Too many pending SRPC requests');
|
|
720
|
+
}
|
|
721
|
+
const response = await new Promise((resolve, reject) => {
|
|
722
|
+
const timeout = setTimeout(() => {
|
|
723
|
+
if (!this.requestQueue.delete(requestId))
|
|
724
|
+
return;
|
|
725
|
+
this.requestBytes.delete(requestId);
|
|
726
|
+
this.addLateReplyTombstone(requestId);
|
|
727
|
+
reject(new types_1.SrpcIndeterminateDeliveryError(this.clientId, new Error(`Request timeout after ${timeoutMs}ms`)));
|
|
728
|
+
}, timeoutMs);
|
|
729
|
+
this.requestQueue.set(requestId, {
|
|
730
|
+
exp: Date.now() + timeoutMs,
|
|
731
|
+
resolve: response => {
|
|
732
|
+
clearTimeout(timeout);
|
|
733
|
+
this.requestBytes.delete(requestId);
|
|
734
|
+
resolve(response);
|
|
735
|
+
},
|
|
736
|
+
reject: error => {
|
|
737
|
+
clearTimeout(timeout);
|
|
738
|
+
this.requestBytes.delete(requestId);
|
|
739
|
+
reject(error);
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
this.requestBytes.set(requestId, encodedBytes);
|
|
743
|
+
const sent = this.writeEncodedMessage(message, encoded, this.generation);
|
|
744
|
+
if (!sent) {
|
|
745
|
+
this.requestQueue.delete(requestId);
|
|
746
|
+
this.requestBytes.delete(requestId);
|
|
747
|
+
clearTimeout(timeout);
|
|
748
|
+
reject(new Error('Failed to send request: not connected'));
|
|
749
|
+
}
|
|
750
|
+
});
|
|
662
751
|
const result = response[resultType];
|
|
663
752
|
if (result == null)
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
753
|
+
throw new Error('Invalid response from server');
|
|
754
|
+
this.logger?.info('SRPC server invocation completed');
|
|
755
|
+
return result;
|
|
756
|
+
}
|
|
757
|
+
catch (error) {
|
|
758
|
+
if (error instanceof types_1.SrpcError) {
|
|
759
|
+
this.logger?.[error.isUserError ? 'info' : 'warn']('SRPC server invocation returned a remote error', {
|
|
760
|
+
srpc: { ...logMeta, error: logSafeText(error.message), userError: error.isUserError === true }
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
else {
|
|
764
|
+
this.logger?.warn('SRPC server invocation failed', error);
|
|
765
|
+
}
|
|
766
|
+
throw error;
|
|
672
767
|
}
|
|
673
768
|
});
|
|
674
|
-
this.requestBytes.set(requestId, encodedBytes);
|
|
675
|
-
const sent = this.writeEncodedMessage(message, encoded, this.generation);
|
|
676
|
-
if (!sent) {
|
|
677
|
-
this.requestQueue.delete(requestId);
|
|
678
|
-
this.requestBytes.delete(requestId);
|
|
679
|
-
clearTimeout(timeout);
|
|
680
|
-
reject(new Error('Failed to send request: not connected'));
|
|
681
|
-
}
|
|
682
769
|
});
|
|
683
770
|
}
|
|
684
771
|
isCurrent(ws, generation) {
|
|
@@ -822,6 +909,28 @@ function toBuffer(data) {
|
|
|
822
909
|
return Buffer.concat(data);
|
|
823
910
|
return Buffer.from(data);
|
|
824
911
|
}
|
|
912
|
+
function toWireTrace(traceContext) {
|
|
913
|
+
if (!traceContext)
|
|
914
|
+
return undefined;
|
|
915
|
+
return {
|
|
916
|
+
traceId: traceContext.traceId,
|
|
917
|
+
spanId: traceContext.spanId,
|
|
918
|
+
traceFlags: traceContext.traceFlags
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
function logSafeText(value, maxBytes = 256) {
|
|
922
|
+
if (typeof value !== 'string')
|
|
923
|
+
return undefined;
|
|
924
|
+
const printable = value.replace(/[\u0000-\u001f\u007f-\u009f]/g, '?');
|
|
925
|
+
if (Buffer.byteLength(printable) <= maxBytes)
|
|
926
|
+
return printable;
|
|
927
|
+
const suffix = '…';
|
|
928
|
+
const prefixBytes = Math.max(0, maxBytes - Buffer.byteLength(suffix));
|
|
929
|
+
let truncated = Buffer.from(printable).subarray(0, prefixBytes).toString('utf8');
|
|
930
|
+
while (Buffer.byteLength(truncated) > prefixBytes)
|
|
931
|
+
truncated = truncated.slice(0, -1);
|
|
932
|
+
return `${truncated}${suffix}`;
|
|
933
|
+
}
|
|
825
934
|
function configuredPositiveInteger(value, fallback) {
|
|
826
935
|
return value != null && Number.isSafeInteger(value) && value > 0 ? value : fallback;
|
|
827
936
|
}
|