@zyno-io/ts-server-foundation 26.802.804 → 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.
@@ -8,9 +8,11 @@ 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
- 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\":\"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}]}]]", []);
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}]}]]", []);
14
16
  let _SrpcConflictErrorMetadata_1;
15
17
  class SrpcConflictError extends Error {
16
18
  constructor() {
@@ -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, _reason, clearConnectTimeout) {
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,14 +311,17 @@ 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
  }
324
+ this.logTraffic('inbound', message);
301
325
  if (message.pingPong) {
302
326
  this.lastPongMs = Date.now();
303
327
  if (this.awaitingActivation?.ws === ws && this.awaitingActivation.generation === generation)
@@ -310,6 +334,7 @@ class SrpcClient {
310
334
  return;
311
335
  if (message.byteStreamOperation) {
312
336
  if (!this.validRemoteByteStreamOperation(message.byteStreamOperation)) {
337
+ this.logger.warn('SRPC client received an invalid byte-stream operation', { srpc: this.logData() });
313
338
  this.closeGenerationWithError(ws, generation, 'badArg', 'Invalid byte stream ID');
314
339
  return;
315
340
  }
@@ -317,6 +342,7 @@ class SrpcClient {
317
342
  return;
318
343
  }
319
344
  if (!message.requestId) {
345
+ this.logger.warn('SRPC client received a message without a request ID', { srpc: this.logData() });
320
346
  this.closeGenerationWithError(ws, generation, 'badArg', 'Invalid request ID');
321
347
  return;
322
348
  }
@@ -327,6 +353,14 @@ class SrpcClient {
327
353
  const bytes = frameBytes;
328
354
  const pressure = this.handlerPressureByGeneration.get(generation) ?? { requests: 0, bytes: 0 };
329
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
+ });
330
364
  this.closeGenerationWithError(ws, generation, 'badArg', 'Too many in-flight server requests');
331
365
  return;
332
366
  }
@@ -354,12 +388,16 @@ class SrpcClient {
354
388
  const accepted = SrpcByteStream_1.SrpcByteStream.writeReceiver(this, op.streamId, op.write.chunk);
355
389
  this.updateByteStreamBufferedBytes(generation, op.streamId, SrpcByteStream_1.SrpcByteStream.getReceiverBufferedBytes(this, op.streamId));
356
390
  if (!accepted && wasBackpressured) {
391
+ this.logger?.warn('SRPC client byte-stream receiver is not draining', { srpc: { ...this.logData(), byteStreamId: op.streamId } });
357
392
  SrpcByteStream_1.SrpcByteStream.abortReceiver(this, op.streamId, new Error(`SRPC byte stream ${op.streamId} receiver is not draining`));
358
393
  this.clearByteStreamPressure(generation, op.streamId);
359
394
  return;
360
395
  }
361
396
  if (!accepted) {
362
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
+ });
363
401
  SrpcByteStream_1.SrpcByteStream.abortReceiver(this, op.streamId, new Error('Too many backpressured SRPC byte streams'));
364
402
  this.clearByteStreamPressure(generation, op.streamId);
365
403
  return;
@@ -370,6 +408,9 @@ class SrpcClient {
370
408
  state.backpressured.delete(op.streamId);
371
409
  }
372
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
+ });
373
414
  SrpcByteStream_1.SrpcByteStream.abortReceiver(this, op.streamId, new Error('Too many buffered SRPC byte stream bytes'));
374
415
  this.clearByteStreamPressure(generation, op.streamId);
375
416
  }
@@ -386,8 +427,15 @@ class SrpcClient {
386
427
  handleReply(ws, generation, requestId, message) {
387
428
  const queueItem = this.requestQueue.get(requestId);
388
429
  if (!queueItem) {
389
- 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
+ });
390
434
  return;
435
+ }
436
+ this.logger?.warn('SRPC client received a reply for an unknown request', {
437
+ srpc: { ...this.logData(), requestId: logSafeText(requestId) }
438
+ });
391
439
  this.closeGenerationWithError(ws, generation, 'badArg', 'Unknown request ID');
392
440
  return;
393
441
  }
@@ -403,23 +451,31 @@ class SrpcClient {
403
451
  const requestData = message[key];
404
452
  if (requestData == null)
405
453
  continue;
406
- try {
407
- const result = await this.handlerRequestId.run({ requestId, generation }, () => handlerMeta.handler(requestData));
408
- this.writeMessage({
409
- requestId,
410
- reply: true,
411
- [handlerMeta.resultType]: result
412
- }, generation);
413
- }
414
- catch (error) {
415
- this.writeMessage({
416
- requestId,
417
- reply: true,
418
- ...(0, types_1.serializeSrpcError)(error)
419
- }, generation);
420
- }
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);
421
476
  return;
422
477
  }
478
+ this.logger?.error('Unhandled SRPC server message type', { srpc: { ...this.logData(), requestId: logSafeText(requestId) } });
423
479
  this.writeMessage({ requestId, reply: true, error: 'Unhandled message type' }, generation);
424
480
  }
425
481
  generateWsUrl() {
@@ -478,7 +534,7 @@ class SrpcClient {
478
534
  return this.serverMessage.decode(toBuffer(data));
479
535
  }
480
536
  catch (error) {
481
- this.logger.error('Failed to decode SRPC message', error);
537
+ this.logger.error('Failed to decode SRPC message', error, { srpc: this.logData() });
482
538
  return null;
483
539
  }
484
540
  }
@@ -488,25 +544,27 @@ class SrpcClient {
488
544
  encoded = (0, types_1.encodeSrpcMessage)(this.clientMessage, message);
489
545
  }
490
546
  catch (error) {
491
- this.logger.error('Failed to encode SRPC message', error);
547
+ this.logger.error('Failed to encode SRPC message', error, { srpc: this.logData() });
492
548
  return false;
493
549
  }
494
- return this.writeEncodedMessage(encoded, generation);
550
+ return this.writeEncodedMessage(message, encoded, generation);
495
551
  }
496
- writeEncodedMessage(encoded, generation = this.generation) {
552
+ writeEncodedMessage(message, encoded, generation = this.generation) {
497
553
  const ws = this.ws;
498
554
  if (!ws || !this.isCurrent(ws, generation) || ws.readyState !== ws_1.default.OPEN)
499
555
  return false;
500
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 } });
501
558
  this.closeGenerationWithError(ws, generation, 'badArg', 'sRPC stream outgoing buffer limit exceeded');
502
559
  return false;
503
560
  }
504
561
  try {
505
562
  ws.send(encoded);
563
+ this.logTraffic('outbound', message);
506
564
  return true;
507
565
  }
508
566
  catch (error) {
509
- this.logger.warn('Failed to send SRPC message', error);
567
+ this.logger.warn('Failed to send SRPC message', error, { srpc: this.logData() });
510
568
  this.closeGenerationWithError(ws, generation, 'badArg', 'Failed to send SRPC message');
511
569
  return false;
512
570
  }
@@ -515,8 +573,16 @@ class SrpcClient {
515
573
  const ws = this.ws;
516
574
  if (!ws || !this.isCurrent(ws, generation) || ws.readyState !== ws_1.default.OPEN)
517
575
  return Promise.reject(new Error('Failed to send SRPC message: not connected'));
518
- const encoded = (0, types_1.encodeSrpcMessage)(this.clientMessage, message);
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
+ }
519
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 } });
520
586
  this.closeGenerationWithError(ws, generation, 'badArg', 'sRPC stream outgoing buffer limit exceeded');
521
587
  return Promise.reject(new types_1.SrpcBackpressureError('sRPC stream outgoing buffer limit exceeded'));
522
588
  }
@@ -524,30 +590,51 @@ class SrpcClient {
524
590
  try {
525
591
  ws.send(encoded, error => {
526
592
  if (error) {
593
+ this.logger.warn('Failed to send SRPC message', error, { srpc: this.logData() });
527
594
  this.closeGenerationWithError(ws, generation, 'badArg', 'Failed to send SRPC message');
528
595
  reject(error);
529
596
  }
530
597
  else if (!this.isCurrent(ws, generation))
531
598
  reject(new Error('Failed to send SRPC message: generation revoked'));
532
- else
599
+ else {
600
+ this.logTraffic('outbound', message);
533
601
  resolve();
602
+ }
534
603
  });
535
604
  }
536
605
  catch (error) {
606
+ this.logger.warn('Failed to send SRPC message', error, { srpc: this.logData() });
537
607
  this.closeGenerationWithError(ws, generation, 'badArg', 'Failed to send SRPC message');
538
608
  reject(error instanceof Error ? error : new Error(String(error)));
539
609
  }
540
610
  });
541
611
  }
542
612
  currentByteStream = this.createByteStream(0);
613
+ logTraffic(direction, message) {
614
+ const options = this.clientOptions?.logTraffic;
615
+ if (!options)
616
+ return;
617
+ const bodies = typeof options === 'object' && options.bodies === true;
618
+ this.logger.info('SRPC traffic', {
619
+ direction,
620
+ clientId: this.clientId,
621
+ messageTypes: (0, types_1.srpcMessageTypes)(message),
622
+ ...(bodies ? { body: message } : {})
623
+ });
624
+ }
625
+ logData() {
626
+ return { clientId: this.clientId, streamId: this.streamId, generation: this.generation };
627
+ }
543
628
  get byteStream() {
544
629
  return this.resolveByteStream();
545
630
  }
546
631
  createByteStream(generation) {
632
+ const streamId = this.streamId;
547
633
  return {
548
- parentStreamId: '',
634
+ parentStreamId: streamId,
549
635
  remoteSenderIdParity: 0,
550
636
  receiverBufferChanged: (streamId, bufferedBytes) => this.updateByteStreamBufferedBytes(generation, streamId, bufferedBytes),
637
+ logDebug: (message, data) => this.logger?.debug(message, { srpc: { clientId: this.clientId, streamId, generation, ...data } }),
551
638
  write: (streamId, chunk) => this.writeMessageAsync({ byteStreamOperation: { streamId, write: { chunk: new Uint8Array(chunk) } } }, generation),
552
639
  finish: streamId => this.writeMessage({ byteStreamOperation: { streamId, finish: {} } }, generation),
553
640
  destroy: (streamId, error) => this.writeMessage({ byteStreamOperation: { streamId, destroy: { error: (0, SrpcByteStream_1.byteStreamDestroyReason)(error) } } }, generation),
@@ -591,7 +678,7 @@ class SrpcClient {
591
678
  handler();
592
679
  }
593
680
  catch (error) {
594
- this.logger.error('SRPC byte stream disconnect handler failed', error);
681
+ this.logger.error('SRPC byte stream disconnect handler failed', error, { srpc: this.logData() });
595
682
  }
596
683
  }
597
684
  }
@@ -616,52 +703,69 @@ class SrpcClient {
616
703
  const requestType = `${prefix}Request`;
617
704
  const resultType = `${prefix}Response`;
618
705
  const requestId = (0, helpers_1.uuid7)();
619
- let encoded;
620
- try {
621
- encoded = (0, types_1.encodeSrpcMessage)(this.clientMessage, { requestId, [requestType]: data });
622
- }
623
- catch (error) {
624
- return Promise.reject(error);
625
- }
626
- const encodedBytes = encoded.byteLength;
627
- if (encodedBytes > this.maxMessageBytes)
628
- return Promise.reject(new types_1.SrpcBackpressureError('sRPC request exceeds message limit'));
629
- if (this.requestQueue.size >= this.maxPendingRequests || this.pendingRequestBytes + encodedBytes > this.maxPendingRequestBytes) {
630
- return Promise.reject(new types_1.SrpcBackpressureError('Too many pending SRPC requests'));
631
- }
632
- return new Promise((resolve, reject) => {
633
- const timeout = setTimeout(() => {
634
- if (!this.requestQueue.delete(requestId))
635
- return;
636
- this.requestBytes.delete(requestId);
637
- this.addLateReplyTombstone(requestId);
638
- reject(new types_1.SrpcIndeterminateDeliveryError(this.clientId, new Error(`Request timeout after ${timeoutMs}ms`)));
639
- }, timeoutMs);
640
- this.requestQueue.set(requestId, {
641
- exp: Date.now() + timeoutMs,
642
- resolve: response => {
643
- clearTimeout(timeout);
644
- this.requestBytes.delete(requestId);
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
+ });
645
751
  const result = response[resultType];
646
752
  if (result == null)
647
- reject(new Error('Invalid response from server'));
648
- else
649
- resolve(result);
650
- },
651
- reject: error => {
652
- clearTimeout(timeout);
653
- this.requestBytes.delete(requestId);
654
- reject(error);
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;
655
767
  }
656
768
  });
657
- this.requestBytes.set(requestId, encodedBytes);
658
- const sent = this.writeEncodedMessage(encoded, this.generation);
659
- if (!sent) {
660
- this.requestQueue.delete(requestId);
661
- this.requestBytes.delete(requestId);
662
- clearTimeout(timeout);
663
- reject(new Error('Failed to send request: not connected'));
664
- }
665
769
  });
666
770
  }
667
771
  isCurrent(ws, generation) {
@@ -805,6 +909,28 @@ function toBuffer(data) {
805
909
  return Buffer.concat(data);
806
910
  return Buffer.from(data);
807
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
+ }
808
934
  function configuredPositiveInteger(value, fallback) {
809
935
  return value != null && Number.isSafeInteger(value) && value > 0 ? value : fallback;
810
936
  }
@@ -852,5 +978,5 @@ function compareCodeUnits(a, b) {
852
978
  function isPlainObject(value) {
853
979
  return typeof value === 'object' && value !== null && !Array.isArray(value);
854
980
  }
855
- exports.__tsfTypeAliases = JSON.parse("[1,{\"SrpcClientOptions\":{\"kind\":18,\"typeName\":\"SrpcClientOptions\",\"types\":[{\"kind\":20,\"name\":\"enableReconnect\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"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}]}}]")[1];
981
+ exports.__tsfTypeAliases = JSON.parse("[1,{\"SrpcClientOptions\":{\"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}]}}]")[1];
856
982
  //# sourceMappingURL=SrpcClient.js.map