@replit/river 0.23.12 → 0.23.13

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 (74) hide show
  1. package/dist/chunk-2FNLANTJ.js +327 -0
  2. package/dist/chunk-2FNLANTJ.js.map +1 -0
  3. package/dist/{chunk-3AW3IXVD.js → chunk-4PVU7J25.js} +1 -21
  4. package/dist/chunk-4PVU7J25.js.map +1 -0
  5. package/dist/{chunk-H6KTH6W6.js → chunk-4QZOW4DH.js} +2 -2
  6. package/dist/{chunk-7RUKEUKE.js → chunk-ES4XO2XD.js} +2 -2
  7. package/dist/{chunk-7RUKEUKE.js.map → chunk-ES4XO2XD.js.map} +1 -1
  8. package/dist/{chunk-XZ6IOBM5.js → chunk-KFTGQ3QC.js} +2 -2
  9. package/dist/chunk-KFTGQ3QC.js.map +1 -0
  10. package/dist/chunk-S4DUN7KK.js +455 -0
  11. package/dist/chunk-S4DUN7KK.js.map +1 -0
  12. package/dist/{chunk-HDBVL7EF.js → chunk-SX6HI63Q.js} +2 -2
  13. package/dist/chunk-XM656KMN.js +408 -0
  14. package/dist/chunk-XM656KMN.js.map +1 -0
  15. package/dist/chunk-ZUKDZY54.js +271 -0
  16. package/dist/chunk-ZUKDZY54.js.map +1 -0
  17. package/dist/client-dd5c9dd0.d.ts +52 -0
  18. package/dist/codec/index.js +20 -2
  19. package/dist/codec/index.js.map +1 -1
  20. package/dist/{connection-8debd45f.d.ts → connection-39816c00.d.ts} +1 -1
  21. package/dist/{connection-581558f8.d.ts → connection-40318f22.d.ts} +1 -1
  22. package/dist/{transport-47af1c81.d.ts → handshake-e428d1c8.d.ts} +88 -153
  23. package/dist/{index-60f03cb7.d.ts → index-ea74cdbb.d.ts} +1 -1
  24. package/dist/logging/index.d.cts +1 -1
  25. package/dist/logging/index.d.ts +1 -1
  26. package/dist/router/index.cjs +1 -1
  27. package/dist/router/index.cjs.map +1 -1
  28. package/dist/router/index.d.cts +8 -6
  29. package/dist/router/index.d.ts +8 -6
  30. package/dist/router/index.js +2 -2
  31. package/dist/server-ebf80863.d.ts +24 -0
  32. package/dist/{services-ca72c9f8.d.ts → services-f406b3aa.d.ts} +3 -2
  33. package/dist/transport/impls/uds/client.cjs +192 -164
  34. package/dist/transport/impls/uds/client.cjs.map +1 -1
  35. package/dist/transport/impls/uds/client.d.cts +6 -5
  36. package/dist/transport/impls/uds/client.d.ts +6 -5
  37. package/dist/transport/impls/uds/client.js +6 -4
  38. package/dist/transport/impls/uds/client.js.map +1 -1
  39. package/dist/transport/impls/uds/server.cjs +240 -212
  40. package/dist/transport/impls/uds/server.cjs.map +1 -1
  41. package/dist/transport/impls/uds/server.d.cts +6 -5
  42. package/dist/transport/impls/uds/server.d.ts +6 -5
  43. package/dist/transport/impls/uds/server.js +8 -6
  44. package/dist/transport/impls/uds/server.js.map +1 -1
  45. package/dist/transport/impls/ws/client.cjs +194 -166
  46. package/dist/transport/impls/ws/client.cjs.map +1 -1
  47. package/dist/transport/impls/ws/client.d.cts +6 -5
  48. package/dist/transport/impls/ws/client.d.ts +6 -5
  49. package/dist/transport/impls/ws/client.js +6 -4
  50. package/dist/transport/impls/ws/client.js.map +1 -1
  51. package/dist/transport/impls/ws/server.cjs +194 -166
  52. package/dist/transport/impls/ws/server.cjs.map +1 -1
  53. package/dist/transport/impls/ws/server.d.cts +4 -3
  54. package/dist/transport/impls/ws/server.d.ts +4 -3
  55. package/dist/transport/impls/ws/server.js +8 -6
  56. package/dist/transport/impls/ws/server.js.map +1 -1
  57. package/dist/transport/index.cjs +138 -104
  58. package/dist/transport/index.cjs.map +1 -1
  59. package/dist/transport/index.d.cts +4 -2
  60. package/dist/transport/index.d.ts +4 -2
  61. package/dist/transport/index.js +14 -8
  62. package/dist/util/testHelpers.cjs +2 -6
  63. package/dist/util/testHelpers.cjs.map +1 -1
  64. package/dist/util/testHelpers.d.cts +5 -4
  65. package/dist/util/testHelpers.d.ts +5 -4
  66. package/dist/util/testHelpers.js +4 -5
  67. package/dist/util/testHelpers.js.map +1 -1
  68. package/package.json +1 -1
  69. package/dist/chunk-3AW3IXVD.js.map +0 -1
  70. package/dist/chunk-VRU4IKRT.js +0 -1392
  71. package/dist/chunk-VRU4IKRT.js.map +0 -1
  72. package/dist/chunk-XZ6IOBM5.js.map +0 -1
  73. /package/dist/{chunk-H6KTH6W6.js.map → chunk-4QZOW4DH.js.map} +0 -0
  74. /package/dist/{chunk-HDBVL7EF.js.map → chunk-SX6HI63Q.js.map} +0 -0
@@ -1,12 +1,14 @@
1
- import { S as SerializedServerSchema, A as AnyServiceSchemaMap, I as InstantiatedServiceSchemaMap, P as PayloadType, b as Result, R as RiverError } from '../services-ca72c9f8.js';
2
- export { C as Client, E as Err, O as Ok, z as Output, k as ProcErrors, g as ProcHandler, h as ProcInit, i as ProcInput, j as ProcOutput, l as ProcType, a as Procedure, p as ProcedureMap, d as ProcedureResult, q as RPCProcedure, y as ResultUnwrapErr, x as ResultUnwrapOk, w as RiverErrorSchema, c as RiverUncaughtSchema, o as SerializedProcedureSchema, n as SerializedServiceSchema, e as Service, f as ServiceConfiguration, m as ServiceSchema, t as StreamProcedure, r as SubscriptionProcedure, v as UNCAUGHT_ERROR, U as UploadProcedure, V as ValidProcType, u as createClient, s as serializeSchema } from '../services-ca72c9f8.js';
1
+ import { S as SerializedServerSchema, A as AnyServiceSchemaMap, I as InstantiatedServiceSchemaMap, P as PayloadType, b as Result, R as RiverError } from '../services-f406b3aa.js';
2
+ export { C as Client, E as Err, O as Ok, z as Output, k as ProcErrors, g as ProcHandler, h as ProcInit, i as ProcInput, j as ProcOutput, l as ProcType, a as Procedure, p as ProcedureMap, d as ProcedureResult, q as RPCProcedure, y as ResultUnwrapErr, x as ResultUnwrapOk, w as RiverErrorSchema, c as RiverUncaughtSchema, o as SerializedProcedureSchema, n as SerializedServiceSchema, e as Service, f as ServiceConfiguration, m as ServiceSchema, t as StreamProcedure, r as SubscriptionProcedure, v as UNCAUGHT_ERROR, U as UploadProcedure, V as ValidProcType, u as createClient, s as serializeSchema } from '../services-f406b3aa.js';
3
3
  import { Static } from '@sinclair/typebox';
4
- import { d as ServerTransport, C as Connection, f as ServerHandshakeOptions, b as ServiceContext } from '../transport-47af1c81.js';
5
- export { g as ParsedMetadata, h as ServiceContextWithState, i as ServiceContextWithTransportInfo, j as createClientHandshakeOptions, k as createServerHandshakeOptions } from '../transport-47af1c81.js';
6
4
  import { Pushable } from 'it-pushable';
7
- import '../index-60f03cb7.js';
5
+ import { C as Connection, m as ServerHandshakeOptions, i as ServiceContext } from '../handshake-e428d1c8.js';
6
+ export { n as ParsedMetadata, o as ServiceContextWithState, p as ServiceContextWithTransportInfo, q as createClientHandshakeOptions, r as createServerHandshakeOptions } from '../handshake-e428d1c8.js';
7
+ import { S as ServerTransport } from '../server-ebf80863.js';
8
+ import '../index-ea74cdbb.js';
8
9
  import '@sinclair/typebox/value';
9
10
  import '@opentelemetry/api';
11
+ import '../client-dd5c9dd0.js';
10
12
  import '../types-3e5768ec.js';
11
13
 
12
14
  interface ServerBreakage {
@@ -81,6 +83,6 @@ declare function createServer<Services extends AnyServiceSchemaMap>(transport: S
81
83
  extendedContext?: Omit<ServiceContext, 'state'>;
82
84
  }>): Server<Services>;
83
85
 
84
- var version = "0.23.12";
86
+ var version = "0.23.13";
85
87
 
86
88
  export { DiffOptions, PayloadBreakage, PayloadType, ProcedureBreakage, version as RIVER_VERSION, Result, RiverError, SerializedServerSchema, Server, ServerBreakage, ServiceBreakage, ServiceContext, createServer, diffServerSchema };
@@ -1,12 +1,14 @@
1
- import { S as SerializedServerSchema, A as AnyServiceSchemaMap, I as InstantiatedServiceSchemaMap, P as PayloadType, b as Result, R as RiverError } from '../services-ca72c9f8.js';
2
- export { C as Client, E as Err, O as Ok, z as Output, k as ProcErrors, g as ProcHandler, h as ProcInit, i as ProcInput, j as ProcOutput, l as ProcType, a as Procedure, p as ProcedureMap, d as ProcedureResult, q as RPCProcedure, y as ResultUnwrapErr, x as ResultUnwrapOk, w as RiverErrorSchema, c as RiverUncaughtSchema, o as SerializedProcedureSchema, n as SerializedServiceSchema, e as Service, f as ServiceConfiguration, m as ServiceSchema, t as StreamProcedure, r as SubscriptionProcedure, v as UNCAUGHT_ERROR, U as UploadProcedure, V as ValidProcType, u as createClient, s as serializeSchema } from '../services-ca72c9f8.js';
1
+ import { S as SerializedServerSchema, A as AnyServiceSchemaMap, I as InstantiatedServiceSchemaMap, P as PayloadType, b as Result, R as RiverError } from '../services-f406b3aa.js';
2
+ export { C as Client, E as Err, O as Ok, z as Output, k as ProcErrors, g as ProcHandler, h as ProcInit, i as ProcInput, j as ProcOutput, l as ProcType, a as Procedure, p as ProcedureMap, d as ProcedureResult, q as RPCProcedure, y as ResultUnwrapErr, x as ResultUnwrapOk, w as RiverErrorSchema, c as RiverUncaughtSchema, o as SerializedProcedureSchema, n as SerializedServiceSchema, e as Service, f as ServiceConfiguration, m as ServiceSchema, t as StreamProcedure, r as SubscriptionProcedure, v as UNCAUGHT_ERROR, U as UploadProcedure, V as ValidProcType, u as createClient, s as serializeSchema } from '../services-f406b3aa.js';
3
3
  import { Static } from '@sinclair/typebox';
4
- import { d as ServerTransport, C as Connection, f as ServerHandshakeOptions, b as ServiceContext } from '../transport-47af1c81.js';
5
- export { g as ParsedMetadata, h as ServiceContextWithState, i as ServiceContextWithTransportInfo, j as createClientHandshakeOptions, k as createServerHandshakeOptions } from '../transport-47af1c81.js';
6
4
  import { Pushable } from 'it-pushable';
7
- import '../index-60f03cb7.js';
5
+ import { C as Connection, m as ServerHandshakeOptions, i as ServiceContext } from '../handshake-e428d1c8.js';
6
+ export { n as ParsedMetadata, o as ServiceContextWithState, p as ServiceContextWithTransportInfo, q as createClientHandshakeOptions, r as createServerHandshakeOptions } from '../handshake-e428d1c8.js';
7
+ import { S as ServerTransport } from '../server-ebf80863.js';
8
+ import '../index-ea74cdbb.js';
8
9
  import '@sinclair/typebox/value';
9
10
  import '@opentelemetry/api';
11
+ import '../client-dd5c9dd0.js';
10
12
  import '../types-3e5768ec.js';
11
13
 
12
14
  interface ServerBreakage {
@@ -81,6 +83,6 @@ declare function createServer<Services extends AnyServiceSchemaMap>(transport: S
81
83
  extendedContext?: Omit<ServiceContext, 'state'>;
82
84
  }>): Server<Services>;
83
85
 
84
- var version = "0.23.12";
86
+ var version = "0.23.13";
85
87
 
86
88
  export { DiffOptions, PayloadBreakage, PayloadType, ProcedureBreakage, version as RIVER_VERSION, Result, RiverError, SerializedServerSchema, Server, ServerBreakage, ServiceBreakage, ServiceContext, createServer, diffServerSchema };
@@ -11,10 +11,10 @@ import {
11
11
  createServerHandshakeOptions,
12
12
  diffServerSchema,
13
13
  serializeSchema
14
- } from "../chunk-XZ6IOBM5.js";
14
+ } from "../chunk-KFTGQ3QC.js";
15
15
  import {
16
16
  version
17
- } from "../chunk-7RUKEUKE.js";
17
+ } from "../chunk-ES4XO2XD.js";
18
18
  export {
19
19
  Err,
20
20
  Ok,
@@ -0,0 +1,24 @@
1
+ import { C as Connection, T as Transport, l as ServerTransportOptions, m as ServerHandshakeOptions, S as Session, n as ParsedMetadata, c as ProvidedServerTransportOptions } from './handshake-e428d1c8.js';
2
+ import { c as TransportClientId } from './index-ea74cdbb.js';
3
+
4
+ declare abstract class ServerTransport<ConnType extends Connection> extends Transport<ConnType> {
5
+ /**
6
+ * The options for this transport.
7
+ */
8
+ protected options: ServerTransportOptions;
9
+ /**
10
+ * Optional handshake options for the server.
11
+ */
12
+ handshakeExtensions?: ServerHandshakeOptions;
13
+ /**
14
+ * A map of session handshake data for each session.
15
+ */
16
+ sessionHandshakeMetadata: WeakMap<Session<ConnType>, ParsedMetadata>;
17
+ constructor(clientId: TransportClientId, providedOptions?: ProvidedServerTransportOptions);
18
+ extendHandshake(options: ServerHandshakeOptions): void;
19
+ protected handleConnection(conn: ConnType): void;
20
+ private validateHandshakeMetadata;
21
+ receiveHandshakeRequestMessage(data: Uint8Array, conn: ConnType): Promise<Session<ConnType> | false>;
22
+ }
23
+
24
+ export { ServerTransport as S };
@@ -1,7 +1,8 @@
1
1
  import { Static, TObject, TUnion, TString, TSchema, TNever, TLiteral } from '@sinclair/typebox';
2
- import { c as ClientTransport, C as Connection, l as ClientHandshakeOptions, i as ServiceContextWithTransportInfo } from './transport-47af1c81.js';
3
2
  import { Pushable } from 'it-pushable';
4
- import { T as TransportClientId } from './index-60f03cb7.js';
3
+ import { C as Connection, k as ClientHandshakeOptions, p as ServiceContextWithTransportInfo } from './handshake-e428d1c8.js';
4
+ import { c as TransportClientId } from './index-ea74cdbb.js';
5
+ import { C as ClientTransport } from './client-dd5c9dd0.js';
5
6
 
6
7
  type AsyncIter<T> = AsyncGenerator<T, T>;
7
8
  /**
@@ -110,7 +110,7 @@ function isAck(controlFlag) {
110
110
  var import_api = require("@opentelemetry/api");
111
111
 
112
112
  // package.json
113
- var version = "0.23.12";
113
+ var version = "0.23.13";
114
114
 
115
115
  // tracing/index.ts
116
116
  function getPropagationContext(ctx) {
@@ -543,6 +543,154 @@ var UdsConnection = class extends Connection {
543
543
  }
544
544
  };
545
545
 
546
+ // transport/client.ts
547
+ var import_api4 = require("@opentelemetry/api");
548
+
549
+ // codec/json.ts
550
+ var encoder = new TextEncoder();
551
+ var decoder = new TextDecoder();
552
+ function uint8ArrayToBase64(uint8Array) {
553
+ let binary = "";
554
+ uint8Array.forEach((byte) => {
555
+ binary += String.fromCharCode(byte);
556
+ });
557
+ return btoa(binary);
558
+ }
559
+ function base64ToUint8Array(base64) {
560
+ const binaryString = atob(base64);
561
+ const uint8Array = new Uint8Array(binaryString.length);
562
+ for (let i = 0; i < binaryString.length; i++) {
563
+ uint8Array[i] = binaryString.charCodeAt(i);
564
+ }
565
+ return uint8Array;
566
+ }
567
+ var NaiveJsonCodec = {
568
+ toBuffer: (obj) => {
569
+ return encoder.encode(
570
+ JSON.stringify(obj, function replacer(key) {
571
+ const val = this[key];
572
+ if (val instanceof Uint8Array) {
573
+ return { $t: uint8ArrayToBase64(val) };
574
+ } else {
575
+ return val;
576
+ }
577
+ })
578
+ );
579
+ },
580
+ fromBuffer: (buff) => {
581
+ try {
582
+ const parsed = JSON.parse(
583
+ decoder.decode(buff),
584
+ function reviver(_key, val) {
585
+ if (val?.$t) {
586
+ return base64ToUint8Array(val.$t);
587
+ } else {
588
+ return val;
589
+ }
590
+ }
591
+ );
592
+ if (typeof parsed === "object")
593
+ return parsed;
594
+ return null;
595
+ } catch {
596
+ return null;
597
+ }
598
+ }
599
+ };
600
+
601
+ // transport/options.ts
602
+ var defaultTransportOptions = {
603
+ heartbeatIntervalMs: 1e3,
604
+ heartbeatsUntilDead: 2,
605
+ sessionDisconnectGraceMs: 5e3,
606
+ codec: NaiveJsonCodec
607
+ };
608
+ var defaultConnectionRetryOptions = {
609
+ baseIntervalMs: 250,
610
+ maxJitterMs: 200,
611
+ maxBackoffMs: 32e3,
612
+ attemptBudgetCapacity: 5,
613
+ budgetRestoreIntervalMs: 200
614
+ };
615
+ var defaultClientTransportOptions = {
616
+ ...defaultTransportOptions,
617
+ ...defaultConnectionRetryOptions
618
+ };
619
+ var defaultServerTransportOptions = {
620
+ ...defaultTransportOptions
621
+ };
622
+
623
+ // transport/rateLimit.ts
624
+ var LeakyBucketRateLimit = class {
625
+ budgetConsumed;
626
+ intervalHandles;
627
+ options;
628
+ constructor(options) {
629
+ this.options = options;
630
+ this.budgetConsumed = /* @__PURE__ */ new Map();
631
+ this.intervalHandles = /* @__PURE__ */ new Map();
632
+ }
633
+ getBackoffMs(user) {
634
+ if (!this.budgetConsumed.has(user))
635
+ return 0;
636
+ const exponent = Math.max(0, this.getBudgetConsumed(user) - 1);
637
+ const jitter = Math.floor(Math.random() * this.options.maxJitterMs);
638
+ const backoffMs = Math.min(
639
+ this.options.baseIntervalMs * 2 ** exponent,
640
+ this.options.maxBackoffMs
641
+ );
642
+ return backoffMs + jitter;
643
+ }
644
+ get totalBudgetRestoreTime() {
645
+ return this.options.budgetRestoreIntervalMs * this.options.attemptBudgetCapacity;
646
+ }
647
+ consumeBudget(user) {
648
+ this.stopLeak(user);
649
+ this.budgetConsumed.set(user, this.getBudgetConsumed(user) + 1);
650
+ }
651
+ getBudgetConsumed(user) {
652
+ return this.budgetConsumed.get(user) ?? 0;
653
+ }
654
+ hasBudget(user) {
655
+ return this.getBudgetConsumed(user) < this.options.attemptBudgetCapacity;
656
+ }
657
+ startRestoringBudget(user) {
658
+ if (this.intervalHandles.has(user)) {
659
+ return;
660
+ }
661
+ const restoreBudgetForUser = () => {
662
+ const currentBudget = this.budgetConsumed.get(user);
663
+ if (!currentBudget) {
664
+ this.stopLeak(user);
665
+ return;
666
+ }
667
+ const newBudget = currentBudget - 1;
668
+ if (newBudget === 0) {
669
+ this.budgetConsumed.delete(user);
670
+ return;
671
+ }
672
+ this.budgetConsumed.set(user, newBudget);
673
+ };
674
+ const intervalHandle = setInterval(
675
+ restoreBudgetForUser,
676
+ this.options.budgetRestoreIntervalMs
677
+ );
678
+ this.intervalHandles.set(user, intervalHandle);
679
+ }
680
+ stopLeak(user) {
681
+ if (!this.intervalHandles.has(user)) {
682
+ return;
683
+ }
684
+ clearInterval(this.intervalHandles.get(user));
685
+ this.intervalHandles.delete(user);
686
+ }
687
+ close() {
688
+ for (const user of this.intervalHandles.keys()) {
689
+ this.stopLeak(user);
690
+ }
691
+ }
692
+ };
693
+
546
694
  // transport/transport.ts
547
695
  var import_value = require("@sinclair/typebox/value");
548
696
 
@@ -636,159 +784,8 @@ var EventDispatcher = class {
636
784
  }
637
785
  };
638
786
 
639
- // util/stringify.ts
640
- function coerceErrorString(err) {
641
- if (err instanceof Error) {
642
- return err.message || "unknown reason";
643
- }
644
- return `[coerced to error] ${String(err)}`;
645
- }
646
-
647
- // transport/rateLimit.ts
648
- var LeakyBucketRateLimit = class {
649
- budgetConsumed;
650
- intervalHandles;
651
- options;
652
- constructor(options) {
653
- this.options = options;
654
- this.budgetConsumed = /* @__PURE__ */ new Map();
655
- this.intervalHandles = /* @__PURE__ */ new Map();
656
- }
657
- getBackoffMs(user) {
658
- if (!this.budgetConsumed.has(user))
659
- return 0;
660
- const exponent = Math.max(0, this.getBudgetConsumed(user) - 1);
661
- const jitter = Math.floor(Math.random() * this.options.maxJitterMs);
662
- const backoffMs = Math.min(
663
- this.options.baseIntervalMs * 2 ** exponent,
664
- this.options.maxBackoffMs
665
- );
666
- return backoffMs + jitter;
667
- }
668
- get totalBudgetRestoreTime() {
669
- return this.options.budgetRestoreIntervalMs * this.options.attemptBudgetCapacity;
670
- }
671
- consumeBudget(user) {
672
- this.stopLeak(user);
673
- this.budgetConsumed.set(user, this.getBudgetConsumed(user) + 1);
674
- }
675
- getBudgetConsumed(user) {
676
- return this.budgetConsumed.get(user) ?? 0;
677
- }
678
- hasBudget(user) {
679
- return this.getBudgetConsumed(user) < this.options.attemptBudgetCapacity;
680
- }
681
- startRestoringBudget(user) {
682
- if (this.intervalHandles.has(user)) {
683
- return;
684
- }
685
- const restoreBudgetForUser = () => {
686
- const currentBudget = this.budgetConsumed.get(user);
687
- if (!currentBudget) {
688
- this.stopLeak(user);
689
- return;
690
- }
691
- const newBudget = currentBudget - 1;
692
- if (newBudget === 0) {
693
- this.budgetConsumed.delete(user);
694
- return;
695
- }
696
- this.budgetConsumed.set(user, newBudget);
697
- };
698
- const intervalHandle = setInterval(
699
- restoreBudgetForUser,
700
- this.options.budgetRestoreIntervalMs
701
- );
702
- this.intervalHandles.set(user, intervalHandle);
703
- }
704
- stopLeak(user) {
705
- if (!this.intervalHandles.has(user)) {
706
- return;
707
- }
708
- clearInterval(this.intervalHandles.get(user));
709
- this.intervalHandles.delete(user);
710
- }
711
- close() {
712
- for (const user of this.intervalHandles.keys()) {
713
- this.stopLeak(user);
714
- }
715
- }
716
- };
717
-
718
- // codec/json.ts
719
- var encoder = new TextEncoder();
720
- var decoder = new TextDecoder();
721
- function uint8ArrayToBase64(uint8Array) {
722
- let binary = "";
723
- uint8Array.forEach((byte) => {
724
- binary += String.fromCharCode(byte);
725
- });
726
- return btoa(binary);
727
- }
728
- function base64ToUint8Array(base64) {
729
- const binaryString = atob(base64);
730
- const uint8Array = new Uint8Array(binaryString.length);
731
- for (let i = 0; i < binaryString.length; i++) {
732
- uint8Array[i] = binaryString.charCodeAt(i);
733
- }
734
- return uint8Array;
735
- }
736
- var NaiveJsonCodec = {
737
- toBuffer: (obj) => {
738
- return encoder.encode(
739
- JSON.stringify(obj, function replacer(key) {
740
- const val = this[key];
741
- if (val instanceof Uint8Array) {
742
- return { $t: uint8ArrayToBase64(val) };
743
- } else {
744
- return val;
745
- }
746
- })
747
- );
748
- },
749
- fromBuffer: (buff) => {
750
- try {
751
- const parsed = JSON.parse(
752
- decoder.decode(buff),
753
- function reviver(_key, val) {
754
- if (val?.$t) {
755
- return base64ToUint8Array(val.$t);
756
- } else {
757
- return val;
758
- }
759
- }
760
- );
761
- if (typeof parsed === "object")
762
- return parsed;
763
- return null;
764
- } catch {
765
- return null;
766
- }
767
- }
768
- };
769
-
770
787
  // transport/transport.ts
771
788
  var import_api3 = require("@opentelemetry/api");
772
- var defaultTransportOptions = {
773
- heartbeatIntervalMs: 1e3,
774
- heartbeatsUntilDead: 2,
775
- sessionDisconnectGraceMs: 5e3,
776
- codec: NaiveJsonCodec
777
- };
778
- var defaultConnectionRetryOptions = {
779
- baseIntervalMs: 250,
780
- maxJitterMs: 200,
781
- maxBackoffMs: 32e3,
782
- attemptBudgetCapacity: 5,
783
- budgetRestoreIntervalMs: 200
784
- };
785
- var defaultClientTransportOptions = {
786
- ...defaultTransportOptions,
787
- ...defaultConnectionRetryOptions
788
- };
789
- var defaultServerTransportOptions = {
790
- ...defaultTransportOptions
791
- };
792
789
  var Transport = class {
793
790
  /**
794
791
  * The status of the transport.
@@ -943,6 +940,16 @@ var Transport = class {
943
940
  * @param connectedTo The peer we are connected to.
944
941
  */
945
942
  onDisconnect(conn, session) {
943
+ if (session.connection !== void 0 && session.connection.id !== conn.id) {
944
+ session.telemetry.span.addEvent("onDisconnect race");
945
+ this.log?.warn("onDisconnect race", {
946
+ clientId: this.clientId,
947
+ ...session.loggingMetadata,
948
+ ...conn.loggingMetadata,
949
+ tags: ["invariant-violation"]
950
+ });
951
+ return;
952
+ }
946
953
  conn.telemetry?.span.end();
947
954
  this.eventDispatcher.dispatchEvent("connectionStatus", {
948
955
  status: "disconnect",
@@ -950,6 +957,16 @@ var Transport = class {
950
957
  });
951
958
  session.connection = void 0;
952
959
  session.beginGrace(() => {
960
+ if (session.connection !== void 0) {
961
+ session.telemetry.span.addEvent("session grace period race");
962
+ this.log?.warn("session grace period race", {
963
+ clientId: this.clientId,
964
+ ...session.loggingMetadata,
965
+ ...conn.loggingMetadata,
966
+ tags: ["invariant-violation"]
967
+ });
968
+ return;
969
+ }
953
970
  session.telemetry.span.addEvent("session grace period expired");
954
971
  this.deleteSession({
955
972
  session,
@@ -1117,6 +1134,17 @@ var Transport = class {
1117
1134
  return this.status;
1118
1135
  }
1119
1136
  };
1137
+
1138
+ // util/stringify.ts
1139
+ function coerceErrorString(err) {
1140
+ if (err instanceof Error) {
1141
+ return err.message || "unknown reason";
1142
+ }
1143
+ return `[coerced to error] ${String(err)}`;
1144
+ }
1145
+
1146
+ // transport/client.ts
1147
+ var import_value2 = require("@sinclair/typebox/value");
1120
1148
  var ClientTransport = class extends Transport {
1121
1149
  /**
1122
1150
  * The options for this transport.
@@ -1177,7 +1205,7 @@ var ClientTransport = class extends Transport {
1177
1205
  const parsed = this.parseMsg(data2, conn);
1178
1206
  if (!parsed) {
1179
1207
  conn.telemetry?.span.setStatus({
1180
- code: import_api3.SpanStatusCode.ERROR,
1208
+ code: import_api4.SpanStatusCode.ERROR,
1181
1209
  message: "message parse failure"
1182
1210
  });
1183
1211
  conn.close();
@@ -1208,7 +1236,7 @@ var ClientTransport = class extends Transport {
1208
1236
  });
1209
1237
  conn.addErrorListener((err) => {
1210
1238
  conn.telemetry?.span.setStatus({
1211
- code: import_api3.SpanStatusCode.ERROR,
1239
+ code: import_api4.SpanStatusCode.ERROR,
1212
1240
  message: "connection error"
1213
1241
  });
1214
1242
  this.log?.warn(
@@ -1226,7 +1254,7 @@ var ClientTransport = class extends Transport {
1226
1254
  const parsed = this.parseMsg(data, conn);
1227
1255
  if (!parsed) {
1228
1256
  conn.telemetry?.span.setStatus({
1229
- code: import_api3.SpanStatusCode.ERROR,
1257
+ code: import_api4.SpanStatusCode.ERROR,
1230
1258
  message: "non-transport message"
1231
1259
  });
1232
1260
  this.protocolError(
@@ -1235,9 +1263,9 @@ var ClientTransport = class extends Transport {
1235
1263
  );
1236
1264
  return false;
1237
1265
  }
1238
- if (!import_value.Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
1266
+ if (!import_value2.Value.Check(ControlMessageHandshakeResponseSchema, parsed.payload)) {
1239
1267
  conn.telemetry?.span.setStatus({
1240
- code: import_api3.SpanStatusCode.ERROR,
1268
+ code: import_api4.SpanStatusCode.ERROR,
1241
1269
  message: "invalid handshake response"
1242
1270
  });
1243
1271
  this.log?.warn(`received invalid handshake resp`, {
@@ -1246,7 +1274,7 @@ var ClientTransport = class extends Transport {
1246
1274
  connectedTo: parsed.from,
1247
1275
  transportMessage: parsed,
1248
1276
  validationErrors: [
1249
- ...import_value.Value.Errors(
1277
+ ...import_value2.Value.Errors(
1250
1278
  ControlMessageHandshakeResponseSchema,
1251
1279
  parsed.payload
1252
1280
  )
@@ -1260,7 +1288,7 @@ var ClientTransport = class extends Transport {
1260
1288
  }
1261
1289
  if (!parsed.payload.status.ok) {
1262
1290
  conn.telemetry?.span.setStatus({
1263
- code: import_api3.SpanStatusCode.ERROR,
1291
+ code: import_api4.SpanStatusCode.ERROR,
1264
1292
  message: "handshake rejected"
1265
1293
  });
1266
1294
  this.log?.warn(`received handshake rejection`, {
@@ -1282,7 +1310,7 @@ var ClientTransport = class extends Transport {
1282
1310
  closeHandshakingConnection: true
1283
1311
  });
1284
1312
  conn.telemetry?.span.setStatus({
1285
- code: import_api3.SpanStatusCode.ERROR,
1313
+ code: import_api4.SpanStatusCode.ERROR,
1286
1314
  message: "session id mismatch"
1287
1315
  });
1288
1316
  this.log?.warn(`handshake from ${parsed.from} session id mismatch`, {
@@ -1382,7 +1410,7 @@ var ClientTransport = class extends Transport {
1382
1410
  } catch (err) {
1383
1411
  const errStr = coerceErrorString(err);
1384
1412
  span.recordException(errStr);
1385
- span.setStatus({ code: import_api3.SpanStatusCode.ERROR });
1413
+ span.setStatus({ code: import_api4.SpanStatusCode.ERROR });
1386
1414
  throw err;
1387
1415
  } finally {
1388
1416
  span.end();
@@ -1433,13 +1461,13 @@ var ClientTransport = class extends Transport {
1433
1461
  let metadata = void 0;
1434
1462
  if (this.handshakeExtensions) {
1435
1463
  metadata = await this.handshakeExtensions.construct();
1436
- if (!import_value.Value.Check(this.handshakeExtensions.schema, metadata)) {
1464
+ if (!import_value2.Value.Check(this.handshakeExtensions.schema, metadata)) {
1437
1465
  this.log?.error(`constructed handshake metadata did not match schema`, {
1438
1466
  ...conn.loggingMetadata,
1439
1467
  clientId: this.clientId,
1440
1468
  connectedTo: to,
1441
1469
  validationErrors: [
1442
- ...import_value.Value.Errors(this.handshakeExtensions.schema, metadata)
1470
+ ...import_value2.Value.Errors(this.handshakeExtensions.schema, metadata)
1443
1471
  ],
1444
1472
  tags: ["invariant-violation"]
1445
1473
  });
@@ -1448,7 +1476,7 @@ var ClientTransport = class extends Transport {
1448
1476
  "handshake metadata did not match schema"
1449
1477
  );
1450
1478
  conn.telemetry?.span.setStatus({
1451
- code: import_api3.SpanStatusCode.ERROR,
1479
+ code: import_api4.SpanStatusCode.ERROR,
1452
1480
  message: "handshake meta mismatch"
1453
1481
  });
1454
1482
  return false;