@replit/river 0.213.1 → 0.215.0

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.
Files changed (55) hide show
  1. package/dist/{adapter-Cp7_gIVA.d.ts → adapter-CgF7vQPu.d.ts} +3 -2
  2. package/dist/{adapter-DjiEwOYi.d.cts → adapter-IGrG4KLL.d.cts} +3 -2
  3. package/dist/{chunk-X4PP65DW.js → chunk-RDTQZ7HO.js} +58 -18
  4. package/dist/chunk-RDTQZ7HO.js.map +1 -0
  5. package/dist/{chunk-2A645F27.js → chunk-VXRAFUA3.js} +178 -54
  6. package/dist/chunk-VXRAFUA3.js.map +1 -0
  7. package/dist/{client-Dw0JBBs3.d.ts → client-BhwlY0-L.d.ts} +2 -2
  8. package/dist/{client-DXJRow2s.d.cts → client-Dk4H7qeg.d.cts} +2 -2
  9. package/dist/codec/index.cjs.map +1 -1
  10. package/dist/codec/index.d.cts +3 -3
  11. package/dist/codec/index.d.ts +3 -3
  12. package/dist/codec/index.js +2 -2
  13. package/dist/{connection-Dzkqj18h.d.cts → connection-CS00EWuS.d.cts} +3 -3
  14. package/dist/{connection-C2lYgRh0.d.ts → connection-DU9v3y8a.d.ts} +3 -3
  15. package/dist/{index-D9R6UTMl.d.cts → index-CHCzvZ9D.d.cts} +1 -1
  16. package/dist/{index-CSM8soK7.d.ts → index-uF0cBM7z.d.ts} +1 -1
  17. package/dist/logging/index.d.cts +2 -2
  18. package/dist/logging/index.d.ts +2 -2
  19. package/dist/{message-Dlsh5WDF.d.cts → message-aABg0s5M.d.cts} +12 -1
  20. package/dist/{message-Dlsh5WDF.d.ts → message-aABg0s5M.d.ts} +12 -1
  21. package/dist/router/index.cjs +60 -17
  22. package/dist/router/index.cjs.map +1 -1
  23. package/dist/router/index.d.cts +10 -10
  24. package/dist/router/index.d.ts +10 -10
  25. package/dist/router/index.js +1 -1
  26. package/dist/{server-BDSYa-CO.d.cts → server-BR0DZaWi.d.cts} +4 -4
  27. package/dist/{server-DFOzjvLh.d.ts → server-uNzkzIRh.d.ts} +4 -4
  28. package/dist/{services-C656NVV3.d.cts → services-B5SY771g.d.ts} +20 -4
  29. package/dist/{services-ChTb4jmc.d.ts → services-DBv2nmly.d.cts} +20 -4
  30. package/dist/testUtil/index.cjs +179 -53
  31. package/dist/testUtil/index.cjs.map +1 -1
  32. package/dist/testUtil/index.d.cts +7 -7
  33. package/dist/testUtil/index.d.ts +7 -7
  34. package/dist/testUtil/index.js +4 -2
  35. package/dist/testUtil/index.js.map +1 -1
  36. package/dist/transport/impls/ws/client.cjs +166 -53
  37. package/dist/transport/impls/ws/client.cjs.map +1 -1
  38. package/dist/transport/impls/ws/client.d.cts +6 -6
  39. package/dist/transport/impls/ws/client.d.ts +6 -6
  40. package/dist/transport/impls/ws/client.js +2 -2
  41. package/dist/transport/impls/ws/server.cjs +133 -53
  42. package/dist/transport/impls/ws/server.cjs.map +1 -1
  43. package/dist/transport/impls/ws/server.d.cts +6 -6
  44. package/dist/transport/impls/ws/server.d.ts +6 -6
  45. package/dist/transport/impls/ws/server.js +2 -2
  46. package/dist/transport/index.cjs +177 -53
  47. package/dist/transport/index.cjs.map +1 -1
  48. package/dist/transport/index.d.cts +7 -7
  49. package/dist/transport/index.d.ts +7 -7
  50. package/dist/transport/index.js +2 -2
  51. package/dist/{transport-CxT7y8Qk.d.cts → transport-BOL2p5s-.d.ts} +70 -64
  52. package/dist/{transport-pdbkDzmJ.d.ts → transport-D3jzhFSi.d.cts} +70 -64
  53. package/package.json +1 -1
  54. package/dist/chunk-2A645F27.js.map +0 -1
  55. package/dist/chunk-X4PP65DW.js.map +0 -1
@@ -1,14 +1,14 @@
1
- import { A as AnyServiceSchemaMap, M as MaybeDisposable, P as ProcedureHandlerContext, a as PayloadType, I as InstantiatedServiceSchemaMap, b as AnyProcedure } from '../services-C656NVV3.cjs';
2
- export { J as BaseErrorSchemaType, G as CANCEL_CODE, L as Client, N as Err, T as ErrResult, F as INVALID_REQUEST_CODE, O as Ok, X as OkResult, h as ProcErrors, d as ProcHandler, e as ProcInit, f as ProcRequest, g as ProcResponse, i as ProcType, z as Procedure, B as ProcedureErrorSchemaType, r as ProcedureMap, R as RPCProcedure, y as RawReadable, v as Readable, x as ReadableBrokenError, w as ReadableResult, H as ReaderErrorSchema, _ as ResponseData, Q as Result, Z as ResultUnwrapErr, Y as ResultUnwrapOk, m as SerializedProcedureSchema, q as SerializedProcedureSchemaProtocolv1, k as SerializedServerSchema, o as SerializedServerSchemaProtocolv1, l as SerializedServiceSchema, p as SerializedServiceSchemaProtocolv1, S as Service, c as ServiceConfiguration, u as StreamProcedure, t as SubscriptionProcedure, D as UNCAUGHT_ERROR_CODE, E as UNEXPECTED_DISCONNECT_CODE, U as UploadProcedure, V as ValidProcType, W as Writable, K as createClient, j as createServiceSchema, C as flattenErrorType, s as serializeSchema, n as serializeSchemaV1Compat } from '../services-C656NVV3.cjs';
1
+ import { A as AnyServiceSchemaMap, M as MaybeDisposable, P as ProcedureHandlerContext, a as PayloadType, I as InstantiatedServiceSchemaMap, b as AnyProcedure } from '../services-DBv2nmly.cjs';
2
+ export { J as BaseErrorSchemaType, G as CANCEL_CODE, L as Client, N as Err, T as ErrResult, F as INVALID_REQUEST_CODE, O as Ok, X as OkResult, h as ProcErrors, d as ProcHandler, e as ProcInit, f as ProcRequest, g as ProcResponse, i as ProcType, z as Procedure, B as ProcedureErrorSchemaType, r as ProcedureMap, R as RPCProcedure, y as RawReadable, v as Readable, x as ReadableBrokenError, w as ReadableResult, H as ReaderErrorSchema, _ as ResponseData, Q as Result, Z as ResultUnwrapErr, Y as ResultUnwrapOk, m as SerializedProcedureSchema, q as SerializedProcedureSchemaProtocolv1, k as SerializedServerSchema, o as SerializedServerSchemaProtocolv1, l as SerializedServiceSchema, p as SerializedServiceSchemaProtocolv1, S as Service, c as ServiceConfiguration, u as StreamProcedure, t as SubscriptionProcedure, D as UNCAUGHT_ERROR_CODE, E as UNEXPECTED_DISCONNECT_CODE, U as UploadProcedure, V as ValidProcType, W as Writable, K as createClient, j as createServiceSchema, C as flattenErrorType, s as serializeSchema, n as serializeSchemaV1Compat } from '../services-DBv2nmly.cjs';
3
3
  import { TSchema, Static } from '@sinclair/typebox';
4
- import { c as TransportClientId, b as OpaqueTransportMessage } from '../message-Dlsh5WDF.cjs';
5
- import { C as Connection, q as ServerHandshakeOptions } from '../transport-CxT7y8Qk.cjs';
6
- export { r as createClientHandshakeOptions, s as createServerHandshakeOptions } from '../transport-CxT7y8Qk.cjs';
7
- import { S as ServerTransport } from '../server-BDSYa-CO.cjs';
4
+ import { c as TransportClientId, b as OpaqueTransportMessage } from '../message-aABg0s5M.cjs';
5
+ import { C as Connection, q as ServerHandshakeOptions } from '../transport-D3jzhFSi.cjs';
6
+ export { r as createClientHandshakeOptions, s as createServerHandshakeOptions } from '../transport-D3jzhFSi.cjs';
7
+ import { S as ServerTransport } from '../server-BR0DZaWi.cjs';
8
8
  import '@opentelemetry/api';
9
- import '../client-DXJRow2s.cjs';
10
- import '../index-D9R6UTMl.cjs';
11
- import '../adapter-DjiEwOYi.cjs';
9
+ import '../client-Dk4H7qeg.cjs';
10
+ import '../index-CHCzvZ9D.cjs';
11
+ import '../adapter-IGrG4KLL.cjs';
12
12
 
13
13
  type StreamId = string;
14
14
  /**
@@ -74,6 +74,6 @@ declare function createServer<MetadataSchema extends TSchema, ParsedMetadata ext
74
74
  middlewares?: Array<Middleware>;
75
75
  }>): Server<Context, ParsedMetadata, Services>;
76
76
 
77
- var version = "0.213.1";
77
+ var version = "0.215.0";
78
78
 
79
79
  export { MaybeDisposable, type Middleware, type MiddlewareContext, type MiddlewareParam, PayloadType, ProcedureHandlerContext, version as RIVER_VERSION, type Server, createServer };
@@ -1,14 +1,14 @@
1
- import { A as AnyServiceSchemaMap, M as MaybeDisposable, P as ProcedureHandlerContext, a as PayloadType, I as InstantiatedServiceSchemaMap, b as AnyProcedure } from '../services-ChTb4jmc.js';
2
- export { J as BaseErrorSchemaType, G as CANCEL_CODE, L as Client, N as Err, T as ErrResult, F as INVALID_REQUEST_CODE, O as Ok, X as OkResult, h as ProcErrors, d as ProcHandler, e as ProcInit, f as ProcRequest, g as ProcResponse, i as ProcType, z as Procedure, B as ProcedureErrorSchemaType, r as ProcedureMap, R as RPCProcedure, y as RawReadable, v as Readable, x as ReadableBrokenError, w as ReadableResult, H as ReaderErrorSchema, _ as ResponseData, Q as Result, Z as ResultUnwrapErr, Y as ResultUnwrapOk, m as SerializedProcedureSchema, q as SerializedProcedureSchemaProtocolv1, k as SerializedServerSchema, o as SerializedServerSchemaProtocolv1, l as SerializedServiceSchema, p as SerializedServiceSchemaProtocolv1, S as Service, c as ServiceConfiguration, u as StreamProcedure, t as SubscriptionProcedure, D as UNCAUGHT_ERROR_CODE, E as UNEXPECTED_DISCONNECT_CODE, U as UploadProcedure, V as ValidProcType, W as Writable, K as createClient, j as createServiceSchema, C as flattenErrorType, s as serializeSchema, n as serializeSchemaV1Compat } from '../services-ChTb4jmc.js';
1
+ import { A as AnyServiceSchemaMap, M as MaybeDisposable, P as ProcedureHandlerContext, a as PayloadType, I as InstantiatedServiceSchemaMap, b as AnyProcedure } from '../services-B5SY771g.js';
2
+ export { J as BaseErrorSchemaType, G as CANCEL_CODE, L as Client, N as Err, T as ErrResult, F as INVALID_REQUEST_CODE, O as Ok, X as OkResult, h as ProcErrors, d as ProcHandler, e as ProcInit, f as ProcRequest, g as ProcResponse, i as ProcType, z as Procedure, B as ProcedureErrorSchemaType, r as ProcedureMap, R as RPCProcedure, y as RawReadable, v as Readable, x as ReadableBrokenError, w as ReadableResult, H as ReaderErrorSchema, _ as ResponseData, Q as Result, Z as ResultUnwrapErr, Y as ResultUnwrapOk, m as SerializedProcedureSchema, q as SerializedProcedureSchemaProtocolv1, k as SerializedServerSchema, o as SerializedServerSchemaProtocolv1, l as SerializedServiceSchema, p as SerializedServiceSchemaProtocolv1, S as Service, c as ServiceConfiguration, u as StreamProcedure, t as SubscriptionProcedure, D as UNCAUGHT_ERROR_CODE, E as UNEXPECTED_DISCONNECT_CODE, U as UploadProcedure, V as ValidProcType, W as Writable, K as createClient, j as createServiceSchema, C as flattenErrorType, s as serializeSchema, n as serializeSchemaV1Compat } from '../services-B5SY771g.js';
3
3
  import { TSchema, Static } from '@sinclair/typebox';
4
- import { c as TransportClientId, b as OpaqueTransportMessage } from '../message-Dlsh5WDF.js';
5
- import { C as Connection, q as ServerHandshakeOptions } from '../transport-pdbkDzmJ.js';
6
- export { r as createClientHandshakeOptions, s as createServerHandshakeOptions } from '../transport-pdbkDzmJ.js';
7
- import { S as ServerTransport } from '../server-DFOzjvLh.js';
4
+ import { c as TransportClientId, b as OpaqueTransportMessage } from '../message-aABg0s5M.js';
5
+ import { C as Connection, q as ServerHandshakeOptions } from '../transport-BOL2p5s-.js';
6
+ export { r as createClientHandshakeOptions, s as createServerHandshakeOptions } from '../transport-BOL2p5s-.js';
7
+ import { S as ServerTransport } from '../server-uNzkzIRh.js';
8
8
  import '@opentelemetry/api';
9
- import '../client-Dw0JBBs3.js';
10
- import '../index-CSM8soK7.js';
11
- import '../adapter-Cp7_gIVA.js';
9
+ import '../client-BhwlY0-L.js';
10
+ import '../index-uF0cBM7z.js';
11
+ import '../adapter-CgF7vQPu.js';
12
12
 
13
13
  type StreamId = string;
14
14
  /**
@@ -74,6 +74,6 @@ declare function createServer<MetadataSchema extends TSchema, ParsedMetadata ext
74
74
  middlewares?: Array<Middleware>;
75
75
  }>): Server<Context, ParsedMetadata, Services>;
76
76
 
77
- var version = "0.213.1";
77
+ var version = "0.215.0";
78
78
 
79
79
  export { MaybeDisposable, type Middleware, type MiddlewareContext, type MiddlewareParam, PayloadType, ProcedureHandlerContext, version as RIVER_VERSION, type Server, createServer };
@@ -18,7 +18,7 @@ import {
18
18
  serializeSchema,
19
19
  serializeSchemaV1Compat,
20
20
  version
21
- } from "../chunk-X4PP65DW.js";
21
+ } from "../chunk-RDTQZ7HO.js";
22
22
  export {
23
23
  CANCEL_CODE,
24
24
  Err,
@@ -1,8 +1,8 @@
1
- import { C as Connection, t as CommonSession, d as SessionState, u as CommonSessionProps, T as Transport, v as ServerTransportOptions, q as ServerHandshakeOptions, w as ServerSession, c as ProvidedServerTransportOptions, D as DeleteSessionOptions } from './transport-CxT7y8Qk.cjs';
2
- import { b as OpaqueTransportMessage, H as HandshakeErrorResponseCodes, P as ProtocolVersion, a as TransportMessage, c as TransportClientId } from './message-Dlsh5WDF.cjs';
1
+ import { C as Connection, t as CommonSession, d as SessionState, u as CommonSessionProps, T as Transport, v as ServerTransportOptions, q as ServerHandshakeOptions, w as ServerSession, c as ProvidedServerTransportOptions, D as DeleteSessionOptions } from './transport-D3jzhFSi.cjs';
2
+ import { b as OpaqueTransportMessage, H as HandshakeErrorResponseCodes, P as ProtocolVersion, a as TransportMessage, c as TransportClientId } from './message-aABg0s5M.cjs';
3
3
  import { Static, TSchema } from '@sinclair/typebox';
4
- import { T as Tags } from './index-D9R6UTMl.cjs';
5
- import { S as SendResult } from './adapter-DjiEwOYi.cjs';
4
+ import { T as Tags } from './index-CHCzvZ9D.cjs';
5
+ import { S as SendResult } from './adapter-IGrG4KLL.cjs';
6
6
 
7
7
  interface SessionWaitingForHandshakeListeners {
8
8
  onConnectionErrored: (err: unknown) => void;
@@ -1,8 +1,8 @@
1
- import { C as Connection, t as CommonSession, d as SessionState, u as CommonSessionProps, T as Transport, v as ServerTransportOptions, q as ServerHandshakeOptions, w as ServerSession, c as ProvidedServerTransportOptions, D as DeleteSessionOptions } from './transport-pdbkDzmJ.js';
2
- import { b as OpaqueTransportMessage, H as HandshakeErrorResponseCodes, P as ProtocolVersion, a as TransportMessage, c as TransportClientId } from './message-Dlsh5WDF.js';
1
+ import { C as Connection, t as CommonSession, d as SessionState, u as CommonSessionProps, T as Transport, v as ServerTransportOptions, q as ServerHandshakeOptions, w as ServerSession, c as ProvidedServerTransportOptions, D as DeleteSessionOptions } from './transport-BOL2p5s-.js';
2
+ import { b as OpaqueTransportMessage, H as HandshakeErrorResponseCodes, P as ProtocolVersion, a as TransportMessage, c as TransportClientId } from './message-aABg0s5M.js';
3
3
  import { Static, TSchema } from '@sinclair/typebox';
4
- import { T as Tags } from './index-CSM8soK7.js';
5
- import { S as SendResult } from './adapter-Cp7_gIVA.js';
4
+ import { T as Tags } from './index-uF0cBM7z.js';
5
+ import { S as SendResult } from './adapter-CgF7vQPu.js';
6
6
 
7
7
  interface SessionWaitingForHandshakeListeners {
8
8
  onConnectionErrored: (err: unknown) => void;
@@ -1,9 +1,9 @@
1
1
  import * as _sinclair_typebox from '@sinclair/typebox';
2
- import { TNever, TObject, TLiteral, TEnum, TString, TSchema, TUnion, Static } from '@sinclair/typebox';
2
+ import { TObject, TLiteral, TEnum, TString, TSchema, TNever, TUnion, Static } from '@sinclair/typebox';
3
3
  import { Span } from '@opentelemetry/api';
4
- import { c as TransportClientId } from './message-Dlsh5WDF.cjs';
5
- import { C as Connection, n as ClientHandshakeOptions, z as SessionId } from './transport-CxT7y8Qk.cjs';
6
- import { C as ClientTransport } from './client-DXJRow2s.cjs';
4
+ import { c as TransportClientId } from './message-aABg0s5M.js';
5
+ import { C as Connection, n as ClientHandshakeOptions, z as SessionId } from './transport-BOL2p5s-.js';
6
+ import { C as ClientTransport } from './client-BhwlY0-L.js';
7
7
 
8
8
  /**
9
9
  * {@link UNCAUGHT_ERROR_CODE} is the code that is used when an error is thrown
@@ -406,6 +406,22 @@ type ProcedureHandlerContext<State, Context, ParsedMetadata> = Context & {
406
406
  * the river documentation to understand the difference between the two concepts.
407
407
  */
408
408
  cancel: (message?: string) => ErrResult<Static<typeof CancelErrorSchema>>;
409
+ /**
410
+ * Register a cleanup function that will run after the procedure handler
411
+ * completes (whether it returns normally, throws, or is cancelled).
412
+ * Cleanup functions run in reverse registration order (LIFO) and each
413
+ * cleanup is awaited before the next one starts.
414
+ *
415
+ * Prefer this over registering async cleanup work on `signal`'s 'abort'
416
+ * event. Abort signal callbacks fire synchronously and do not await async
417
+ * work, so multiple async callbacks will interlace their execution
418
+ * (coroutine-like behavior) rather than running sequentially to completion.
419
+ * `deferCleanup` guarantees each cleanup finishes before the next begins.
420
+ *
421
+ * If a cleanup function throws, the error is recorded on the cleanup span
422
+ * but remaining cleanups continue to run.
423
+ */
424
+ deferCleanup: (fn: () => void | Promise<void>) => void;
409
425
  /**
410
426
  * This signal is a standard [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
411
427
  * triggered when the procedure invocation is done. This signal tracks the invocation/request finishing
@@ -1,9 +1,9 @@
1
1
  import * as _sinclair_typebox from '@sinclair/typebox';
2
- import { TNever, TObject, TLiteral, TEnum, TString, TSchema, TUnion, Static } from '@sinclair/typebox';
2
+ import { TObject, TLiteral, TEnum, TString, TSchema, TNever, TUnion, Static } from '@sinclair/typebox';
3
3
  import { Span } from '@opentelemetry/api';
4
- import { c as TransportClientId } from './message-Dlsh5WDF.js';
5
- import { C as Connection, n as ClientHandshakeOptions, z as SessionId } from './transport-pdbkDzmJ.js';
6
- import { C as ClientTransport } from './client-Dw0JBBs3.js';
4
+ import { c as TransportClientId } from './message-aABg0s5M.cjs';
5
+ import { C as Connection, n as ClientHandshakeOptions, z as SessionId } from './transport-D3jzhFSi.cjs';
6
+ import { C as ClientTransport } from './client-Dk4H7qeg.cjs';
7
7
 
8
8
  /**
9
9
  * {@link UNCAUGHT_ERROR_CODE} is the code that is used when an error is thrown
@@ -406,6 +406,22 @@ type ProcedureHandlerContext<State, Context, ParsedMetadata> = Context & {
406
406
  * the river documentation to understand the difference between the two concepts.
407
407
  */
408
408
  cancel: (message?: string) => ErrResult<Static<typeof CancelErrorSchema>>;
409
+ /**
410
+ * Register a cleanup function that will run after the procedure handler
411
+ * completes (whether it returns normally, throws, or is cancelled).
412
+ * Cleanup functions run in reverse registration order (LIFO) and each
413
+ * cleanup is awaited before the next one starts.
414
+ *
415
+ * Prefer this over registering async cleanup work on `signal`'s 'abort'
416
+ * event. Abort signal callbacks fire synchronously and do not await async
417
+ * work, so multiple async callbacks will interlace their execution
418
+ * (coroutine-like behavior) rather than running sequentially to completion.
419
+ * `deferCleanup` guarantees each cleanup finishes before the next begins.
420
+ *
421
+ * If a cleanup function throws, the error is recorded on the cleanup span
422
+ * but remaining cleanups continue to run.
423
+ */
424
+ deferCleanup: (fn: () => void | Promise<void>) => void;
409
425
  /**
410
426
  * This signal is a standard [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
411
427
  * triggered when the procedure invocation is done. This signal tracks the invocation/request finishing
@@ -360,6 +360,7 @@ var IdentifiedSession = class extends CommonSession {
360
360
  telemetry;
361
361
  to;
362
362
  protocolVersion;
363
+ listeners;
363
364
  /**
364
365
  * Index of the message we will send next (excluding handshake)
365
366
  */
@@ -383,7 +384,8 @@ var IdentifiedSession = class extends CommonSession {
383
384
  telemetry,
384
385
  log,
385
386
  protocolVersion,
386
- seqSent: messagesSent
387
+ seqSent: messagesSent,
388
+ listeners
387
389
  } = props;
388
390
  super(props);
389
391
  this.id = id;
@@ -395,6 +397,7 @@ var IdentifiedSession = class extends CommonSession {
395
397
  this.log = log;
396
398
  this.protocolVersion = protocolVersion;
397
399
  this.seqSent = messagesSent;
400
+ this.listeners = listeners;
398
401
  }
399
402
  get loggingMetadata() {
400
403
  const metadata = {
@@ -411,7 +414,7 @@ var IdentifiedSession = class extends CommonSession {
411
414
  }
412
415
  return metadata;
413
416
  }
414
- constructMsg(partialMsg) {
417
+ encodeMsg(partialMsg) {
415
418
  const msg = {
416
419
  ...partialMsg,
417
420
  id: generateId(),
@@ -420,18 +423,37 @@ var IdentifiedSession = class extends CommonSession {
420
423
  seq: this.seq,
421
424
  ack: this.ack
422
425
  };
426
+ const encoded = this.codec.toBuffer(msg);
427
+ if (!encoded.ok) {
428
+ this.listeners.onMessageSendFailure(
429
+ { ...partialMsg, seq: this.seq },
430
+ encoded.reason
431
+ );
432
+ return encoded;
433
+ }
423
434
  this.seq++;
424
- return msg;
435
+ return {
436
+ ok: true,
437
+ value: {
438
+ id: msg.id,
439
+ seq: msg.seq,
440
+ msg: partialMsg,
441
+ data: encoded.value
442
+ }
443
+ };
425
444
  }
426
445
  nextSeq() {
427
446
  return this.sendBuffer.length > 0 ? this.sendBuffer[0].seq : this.seq;
428
447
  }
429
448
  send(msg) {
430
- const constructedMsg = this.constructMsg(msg);
431
- this.sendBuffer.push(constructedMsg);
449
+ const encodeResult = this.encodeMsg(msg);
450
+ if (!encodeResult.ok) {
451
+ return encodeResult;
452
+ }
453
+ this.sendBuffer.push(encodeResult.value);
432
454
  return {
433
455
  ok: true,
434
- value: constructedMsg.id
456
+ value: encodeResult.value.id
435
457
  };
436
458
  }
437
459
  _handleStateExit() {
@@ -464,23 +486,6 @@ var IdentifiedSessionWithGracePeriod = class extends IdentifiedSession {
464
486
  super._handleClose();
465
487
  }
466
488
  };
467
- function sendMessage(conn, codec, msg) {
468
- const buff = codec.toBuffer(msg);
469
- if (!buff.ok) {
470
- return buff;
471
- }
472
- const sent = conn.send(buff.value);
473
- if (!sent) {
474
- return {
475
- ok: false,
476
- reason: "failed to send message"
477
- };
478
- }
479
- return {
480
- ok: true,
481
- value: msg.id
482
- };
483
- }
484
489
 
485
490
  // transport/sessionStateMachine/SessionConnecting.ts
486
491
  var SessionConnecting = class extends IdentifiedSessionWithGracePeriod {
@@ -559,7 +564,7 @@ function coerceErrorString(err) {
559
564
  }
560
565
 
561
566
  // package.json
562
- var version = "0.213.1";
567
+ var version = "0.215.0";
563
568
 
564
569
  // tracing/index.ts
565
570
  function getPropagationContext(ctx) {
@@ -642,7 +647,21 @@ var SessionWaitingForHandshake = class extends CommonSession {
642
647
  this.listeners.onHandshake(parsedMsgRes.value);
643
648
  };
644
649
  sendHandshake(msg) {
645
- return sendMessage(this.conn, this.codec, msg);
650
+ const buff = this.codec.toBuffer(msg);
651
+ if (!buff.ok) {
652
+ return buff;
653
+ }
654
+ const sent = this.conn.send(buff.value);
655
+ if (!sent) {
656
+ return {
657
+ ok: false,
658
+ reason: "failed to send handshake"
659
+ };
660
+ }
661
+ return {
662
+ ok: true,
663
+ value: msg.id
664
+ };
646
665
  }
647
666
  _handleStateExit() {
648
667
  this.conn.removeDataListener();
@@ -691,7 +710,21 @@ var SessionHandshaking = class extends IdentifiedSessionWithGracePeriod {
691
710
  this.listeners.onHandshake(parsedMsgRes.value);
692
711
  };
693
712
  sendHandshake(msg) {
694
- return sendMessage(this.conn, this.codec, msg);
713
+ const buff = this.codec.toBuffer(msg);
714
+ if (!buff.ok) {
715
+ return buff;
716
+ }
717
+ const sent = this.conn.send(buff.value);
718
+ if (!sent) {
719
+ return {
720
+ ok: false,
721
+ reason: "failed to send handshake"
722
+ };
723
+ }
724
+ return {
725
+ ok: true,
726
+ value: msg.id
727
+ };
695
728
  }
696
729
  _handleStateExit() {
697
730
  super._handleStateExit();
@@ -726,28 +759,35 @@ var SessionConnected = class extends IdentifiedSession {
726
759
  }
727
760
  this.startMissingHeartbeatTimeout();
728
761
  }
729
- assertSendOrdering(constructedMsg) {
730
- if (constructedMsg.seq > this.seqSent + 1) {
731
- const msg = `invariant violation: would have sent out of order msg (seq: ${constructedMsg.seq}, expected: ${this.seqSent} + 1)`;
762
+ assertSendOrdering(encodedMsg) {
763
+ if (encodedMsg.seq > this.seqSent + 1) {
764
+ const msg = `invariant violation: would have sent out of order msg (seq: ${encodedMsg.seq}, expected: ${this.seqSent} + 1)`;
732
765
  this.log?.error(msg, {
733
766
  ...this.loggingMetadata,
734
- transportMessage: constructedMsg,
735
767
  tags: ["invariant-violation"]
736
768
  });
737
769
  throw new Error(msg);
738
770
  }
739
771
  }
740
772
  send(msg) {
741
- const constructedMsg = this.constructMsg(msg);
742
- this.assertSendOrdering(constructedMsg);
743
- this.sendBuffer.push(constructedMsg);
744
- const res = sendMessage(this.conn, this.codec, constructedMsg);
745
- if (!res.ok) {
746
- this.listeners.onMessageSendFailure(constructedMsg, res.reason);
747
- return res;
773
+ const encodeResult = this.encodeMsg(msg);
774
+ if (!encodeResult.ok) {
775
+ return encodeResult;
776
+ }
777
+ const encodedMsg = encodeResult.value;
778
+ this.assertSendOrdering(encodedMsg);
779
+ this.sendBuffer.push(encodedMsg);
780
+ const sent = this.conn.send(encodedMsg.data);
781
+ if (!sent) {
782
+ const reason = "failed to send message";
783
+ this.listeners.onMessageSendFailure(
784
+ { ...encodedMsg.msg, seq: encodedMsg.seq },
785
+ reason
786
+ );
787
+ return { ok: false, reason };
748
788
  }
749
- this.seqSent = constructedMsg.seq;
750
- return res;
789
+ this.seqSent = encodedMsg.seq;
790
+ return { ok: true, value: encodedMsg.id };
751
791
  }
752
792
  constructor(props) {
753
793
  super(props);
@@ -765,10 +805,14 @@ var SessionConnected = class extends IdentifiedSession {
765
805
  );
766
806
  for (const msg of this.sendBuffer) {
767
807
  this.assertSendOrdering(msg);
768
- const res = sendMessage(this.conn, this.codec, msg);
769
- if (!res.ok) {
770
- this.listeners.onMessageSendFailure(msg, res.reason);
771
- return res;
808
+ const sent = this.conn.send(msg.data);
809
+ if (!sent) {
810
+ const reason = "failed to send buffered message";
811
+ this.listeners.onMessageSendFailure(
812
+ { ...msg.msg, seq: msg.seq },
813
+ reason
814
+ );
815
+ return { ok: false, reason };
772
816
  }
773
817
  this.seqSent = msg.seq;
774
818
  }
@@ -1176,6 +1220,17 @@ var Transport = class {
1176
1220
  const noConnectionSession = SessionStateGraph.transition.ConnectingToNoConnection(session, {
1177
1221
  onSessionGracePeriodElapsed: () => {
1178
1222
  this.onSessionGracePeriodElapsed(noConnectionSession);
1223
+ },
1224
+ onMessageSendFailure: (msg, reason) => {
1225
+ this.log?.error(`failed to send message: ${reason}`, {
1226
+ ...noConnectionSession.loggingMetadata,
1227
+ transportMessage: msg
1228
+ });
1229
+ this.protocolError({
1230
+ type: ProtocolError.MessageSendFailure,
1231
+ message: reason
1232
+ });
1233
+ this.deleteSession(noConnectionSession, { unhealthy: true });
1179
1234
  }
1180
1235
  });
1181
1236
  this.updateSession(noConnectionSession);
@@ -1183,18 +1238,32 @@ var Transport = class {
1183
1238
  }
1184
1239
  onConnClosed(session) {
1185
1240
  let noConnectionSession;
1241
+ const listeners = {
1242
+ onSessionGracePeriodElapsed: () => {
1243
+ this.onSessionGracePeriodElapsed(noConnectionSession);
1244
+ },
1245
+ onMessageSendFailure: (msg, reason) => {
1246
+ this.log?.error(`failed to send message: ${reason}`, {
1247
+ ...noConnectionSession.loggingMetadata,
1248
+ transportMessage: msg
1249
+ });
1250
+ this.protocolError({
1251
+ type: ProtocolError.MessageSendFailure,
1252
+ message: reason
1253
+ });
1254
+ this.deleteSession(noConnectionSession, { unhealthy: true });
1255
+ }
1256
+ };
1186
1257
  if (session.state === "Handshaking" /* Handshaking */) {
1187
- noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(session, {
1188
- onSessionGracePeriodElapsed: () => {
1189
- this.onSessionGracePeriodElapsed(noConnectionSession);
1190
- }
1191
- });
1258
+ noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(
1259
+ session,
1260
+ listeners
1261
+ );
1192
1262
  } else {
1193
- noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(session, {
1194
- onSessionGracePeriodElapsed: () => {
1195
- this.onSessionGracePeriodElapsed(noConnectionSession);
1196
- }
1197
- });
1263
+ noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(
1264
+ session,
1265
+ listeners
1266
+ );
1198
1267
  }
1199
1268
  this.updateSession(noConnectionSession);
1200
1269
  return noConnectionSession;
@@ -1355,6 +1424,17 @@ var ClientTransport = class extends Transport {
1355
1424
  {
1356
1425
  onSessionGracePeriodElapsed: () => {
1357
1426
  this.onSessionGracePeriodElapsed(session);
1427
+ },
1428
+ onMessageSendFailure: (msg, reason) => {
1429
+ this.log?.error(`failed to send message: ${reason}`, {
1430
+ ...session.loggingMetadata,
1431
+ transportMessage: msg
1432
+ });
1433
+ this.protocolError({
1434
+ type: ProtocolError.MessageSendFailure,
1435
+ message: reason
1436
+ });
1437
+ this.deleteSession(session, { unhealthy: true });
1358
1438
  }
1359
1439
  },
1360
1440
  this.options,
@@ -1419,6 +1499,17 @@ var ClientTransport = class extends Transport {
1419
1499
  },
1420
1500
  onSessionGracePeriodElapsed: () => {
1421
1501
  this.onSessionGracePeriodElapsed(handshakingSession);
1502
+ },
1503
+ onMessageSendFailure: (msg, reason) => {
1504
+ this.log?.error(`failed to send message: ${reason}`, {
1505
+ ...handshakingSession.loggingMetadata,
1506
+ transportMessage: msg
1507
+ });
1508
+ this.protocolError({
1509
+ type: ProtocolError.MessageSendFailure,
1510
+ message: reason
1511
+ });
1512
+ this.deleteSession(handshakingSession, { unhealthy: true });
1422
1513
  }
1423
1514
  }
1424
1515
  );
@@ -1580,6 +1671,17 @@ var ClientTransport = class extends Transport {
1580
1671
  },
1581
1672
  onSessionGracePeriodElapsed: () => {
1582
1673
  this.onSessionGracePeriodElapsed(backingOffSession);
1674
+ },
1675
+ onMessageSendFailure: (msg, reason) => {
1676
+ this.log?.error(`failed to send message: ${reason}`, {
1677
+ ...backingOffSession.loggingMetadata,
1678
+ transportMessage: msg
1679
+ });
1680
+ this.protocolError({
1681
+ type: ProtocolError.MessageSendFailure,
1682
+ message: reason
1683
+ });
1684
+ this.deleteSession(backingOffSession, { unhealthy: true });
1583
1685
  }
1584
1686
  }
1585
1687
  );
@@ -1643,6 +1745,17 @@ var ClientTransport = class extends Transport {
1643
1745
  },
1644
1746
  onSessionGracePeriodElapsed: () => {
1645
1747
  this.onSessionGracePeriodElapsed(connectingSession);
1748
+ },
1749
+ onMessageSendFailure: (msg, reason) => {
1750
+ this.log?.error(`failed to send message: ${reason}`, {
1751
+ ...connectingSession.loggingMetadata,
1752
+ transportMessage: msg
1753
+ });
1754
+ this.protocolError({
1755
+ type: ProtocolError.MessageSendFailure,
1756
+ message: reason
1757
+ });
1758
+ this.deleteSession(connectingSession, { unhealthy: true });
1646
1759
  }
1647
1760
  }
1648
1761
  );
@@ -1973,6 +2086,17 @@ var ServerTransport = class extends Transport {
1973
2086
  {
1974
2087
  onSessionGracePeriodElapsed: () => {
1975
2088
  this.onSessionGracePeriodElapsed(noConnectionSession);
2089
+ },
2090
+ onMessageSendFailure: (msg2, reason) => {
2091
+ this.log?.error(`failed to send message: ${reason}`, {
2092
+ ...noConnectionSession.loggingMetadata,
2093
+ transportMessage: msg2
2094
+ });
2095
+ this.protocolError({
2096
+ type: ProtocolError.MessageSendFailure,
2097
+ message: reason
2098
+ });
2099
+ this.deleteSession(noConnectionSession, { unhealthy: true });
1976
2100
  }
1977
2101
  }
1978
2102
  );
@@ -2823,6 +2947,8 @@ function dummySession() {
2823
2947
  "server",
2824
2948
  {
2825
2949
  onSessionGracePeriodElapsed: () => {
2950
+ },
2951
+ onMessageSendFailure: () => {
2826
2952
  }
2827
2953
  },
2828
2954
  testingSessionOptions,