@replit/river 0.25.1 → 0.26.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 (54) hide show
  1. package/dist/{chunk-D5O3ERJU.js → chunk-5FDAIAQ5.js} +3 -3
  2. package/dist/chunk-5FDAIAQ5.js.map +1 -0
  3. package/dist/{chunk-5CNNIOAO.js → chunk-5S64PXTU.js} +102 -95
  4. package/dist/chunk-5S64PXTU.js.map +1 -0
  5. package/dist/{chunk-PCBPPTXH.js → chunk-7ETNUCOL.js} +54 -62
  6. package/dist/chunk-7ETNUCOL.js.map +1 -0
  7. package/dist/{chunk-SGSRNAWJ.js → chunk-BNNELZM4.js} +2 -2
  8. package/dist/{chunk-SGSRNAWJ.js.map → chunk-BNNELZM4.js.map} +1 -1
  9. package/dist/{chunk-SZ5NBBX7.js → chunk-CCUYKR5C.js} +14 -4
  10. package/dist/chunk-CCUYKR5C.js.map +1 -0
  11. package/dist/{chunk-YM5Y4NAT.js → chunk-JSU2KACV.js} +199 -117
  12. package/dist/chunk-JSU2KACV.js.map +1 -0
  13. package/dist/{chunk-MBMEJIPU.js → chunk-KP4UB5NW.js} +2 -2
  14. package/dist/{client-1321630c.d.ts → client-162c509c.d.ts} +4 -2
  15. package/dist/{connection-bd907ca6.d.ts → connection-6a404bb8.d.ts} +1 -1
  16. package/dist/{handshake-3772d7ca.d.ts → handshake-3342bb94.d.ts} +132 -84
  17. package/dist/logging/index.d.cts +1 -1
  18. package/dist/logging/index.d.ts +1 -1
  19. package/dist/{message-e6c560fd.d.ts → message-1a434848.d.ts} +2 -1
  20. package/dist/router/index.cjs +12 -3
  21. package/dist/router/index.cjs.map +1 -1
  22. package/dist/router/index.d.cts +8 -8
  23. package/dist/router/index.d.ts +8 -8
  24. package/dist/router/index.js +2 -2
  25. package/dist/{server-f0fd2b98.d.ts → server-1b695374.d.ts} +9 -4
  26. package/dist/{services-8d14ae16.d.ts → services-c17f7eff.d.ts} +3 -3
  27. package/dist/transport/impls/ws/client.cjs +302 -207
  28. package/dist/transport/impls/ws/client.cjs.map +1 -1
  29. package/dist/transport/impls/ws/client.d.cts +4 -4
  30. package/dist/transport/impls/ws/client.d.ts +4 -4
  31. package/dist/transport/impls/ws/client.js +5 -5
  32. package/dist/transport/impls/ws/server.cjs +253 -174
  33. package/dist/transport/impls/ws/server.cjs.map +1 -1
  34. package/dist/transport/impls/ws/server.d.cts +4 -4
  35. package/dist/transport/impls/ws/server.d.ts +4 -4
  36. package/dist/transport/impls/ws/server.js +5 -5
  37. package/dist/transport/index.cjs +351 -264
  38. package/dist/transport/index.cjs.map +1 -1
  39. package/dist/transport/index.d.cts +4 -4
  40. package/dist/transport/index.d.ts +4 -4
  41. package/dist/transport/index.js +5 -5
  42. package/dist/util/testHelpers.cjs +208 -117
  43. package/dist/util/testHelpers.cjs.map +1 -1
  44. package/dist/util/testHelpers.d.cts +6 -5
  45. package/dist/util/testHelpers.d.ts +6 -5
  46. package/dist/util/testHelpers.js +6 -3
  47. package/dist/util/testHelpers.js.map +1 -1
  48. package/package.json +14 -13
  49. package/dist/chunk-5CNNIOAO.js.map +0 -1
  50. package/dist/chunk-D5O3ERJU.js.map +0 -1
  51. package/dist/chunk-PCBPPTXH.js.map +0 -1
  52. package/dist/chunk-SZ5NBBX7.js.map +0 -1
  53. package/dist/chunk-YM5Y4NAT.js.map +0 -1
  54. /package/dist/{chunk-MBMEJIPU.js.map → chunk-KP4UB5NW.js.map} +0 -0
@@ -5,10 +5,10 @@ import {
5
5
  import {
6
6
  SessionStateGraph,
7
7
  defaultTransportOptions
8
- } from "./chunk-YM5Y4NAT.js";
8
+ } from "./chunk-JSU2KACV.js";
9
9
  import {
10
10
  generateId
11
- } from "./chunk-SZ5NBBX7.js";
11
+ } from "./chunk-CCUYKR5C.js";
12
12
 
13
13
  // transport/events.ts
14
14
  var ProtocolError = {
@@ -274,4 +274,4 @@ export {
274
274
  Transport,
275
275
  Connection
276
276
  };
277
- //# sourceMappingURL=chunk-D5O3ERJU.js.map
277
+ //# sourceMappingURL=chunk-5FDAIAQ5.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../transport/events.ts","../transport/transport.ts","../transport/connection.ts"],"sourcesContent":["import { Connection } from './connection';\nimport { OpaqueTransportMessage } from './message';\nimport { Session, SessionState } from './sessionStateMachine';\nimport { TransportStatus } from './transport';\n\nexport const ProtocolError = {\n RetriesExceeded: 'conn_retry_exceeded',\n HandshakeFailed: 'handshake_failed',\n MessageOrderingViolated: 'message_ordering_violated',\n} as const;\n\nexport type ProtocolErrorType =\n (typeof ProtocolError)[keyof typeof ProtocolError];\n\nexport interface EventMap {\n message: OpaqueTransportMessage;\n sessionStatus: {\n status: 'connect' | 'disconnect';\n session: Session<Connection>;\n };\n sessionTransition:\n | { state: SessionState.Connected }\n | { state: SessionState.Handshaking }\n | { state: SessionState.Connecting }\n | { state: SessionState.BackingOff }\n | { state: SessionState.NoConnection };\n protocolError: {\n type: ProtocolErrorType;\n message: string;\n };\n transportStatus: {\n status: TransportStatus;\n };\n}\n\nexport type EventTypes = keyof EventMap;\nexport type EventHandler<K extends EventTypes> = (\n event: EventMap[K],\n) => unknown;\n\nexport class EventDispatcher<T extends EventTypes> {\n private eventListeners: { [K in T]?: Set<EventHandler<K>> } = {};\n\n removeAllListeners() {\n this.eventListeners = {};\n }\n\n numberOfListeners<K extends T>(eventType: K) {\n return this.eventListeners[eventType]?.size ?? 0;\n }\n\n addEventListener<K extends T>(eventType: K, handler: EventHandler<K>) {\n if (!this.eventListeners[eventType]) {\n this.eventListeners[eventType] = new Set();\n }\n\n this.eventListeners[eventType]?.add(handler);\n }\n\n removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>) {\n const handlers = this.eventListeners[eventType];\n if (handlers) {\n this.eventListeners[eventType]?.delete(handler);\n }\n }\n\n dispatchEvent<K extends T>(eventType: K, event: EventMap[K]) {\n const handlers = this.eventListeners[eventType];\n if (handlers) {\n // copying ensures that adding more listeners in a handler doesn't\n // affect the current dispatch.\n const copy = [...handlers];\n for (const handler of copy) {\n handler(event);\n }\n }\n }\n}\n","import {\n OpaqueTransportMessage,\n TransportClientId,\n PartialTransportMessage,\n} from './message';\nimport {\n BaseLogger,\n LogFn,\n Logger,\n LoggingLevel,\n createLogProxy,\n} from '../logging/log';\nimport {\n EventDispatcher,\n EventHandler,\n EventMap,\n EventTypes,\n ProtocolErrorType,\n} from './events';\nimport {\n ProvidedTransportOptions,\n TransportOptions,\n defaultTransportOptions,\n} from './options';\nimport {\n SessionConnected,\n SessionConnecting,\n SessionHandshaking,\n SessionNoConnection,\n SessionState,\n} from './sessionStateMachine';\nimport { Connection } from './connection';\nimport { Session, SessionStateGraph } from './sessionStateMachine/transitions';\n\n/**\n * Represents the possible states of a transport.\n * @property {'open'} open - The transport is open and operational (note that this doesn't mean it is actively connected)\n * @property {'closed'} closed - The transport is permanently closed and cannot be reopened.\n */\nexport type TransportStatus = 'open' | 'closed';\n\n/**\n * Transports manage the lifecycle (creation/deletion) of sessions\n *\n * ```plaintext\n * ▲\n * incoming │\n * messages │\n * ▼\n * ┌─────────────┐ 1:N ┌───────────┐ 1:1* ┌────────────┐\n * │ Transport │ ◄─────► │ Session │ ◄─────► │ Connection │\n * └─────────────┘ └───────────┘ └────────────┘\n * ▲ * (may or may not be initialized yet)\n * │\n * ▼\n * ┌───────────┐\n * │ Message │\n * │ Listeners │\n * └───────────┘\n * ```\n * @abstract\n */\nexport abstract class Transport<ConnType extends Connection> {\n /**\n * The status of the transport.\n */\n private status: TransportStatus;\n\n /**\n * The client ID of this transport.\n */\n clientId: TransportClientId;\n\n /**\n * The event dispatcher for handling events of type EventTypes.\n */\n eventDispatcher: EventDispatcher<EventTypes>;\n\n /**\n * The options for this transport.\n */\n protected options: TransportOptions;\n log?: Logger;\n\n sessions: Map<TransportClientId, Session<ConnType>>;\n\n /**\n * Creates a new Transport instance.\n * @param codec The codec used to encode and decode messages.\n * @param clientId The client ID of this transport.\n */\n constructor(\n clientId: TransportClientId,\n providedOptions?: ProvidedTransportOptions,\n ) {\n this.options = { ...defaultTransportOptions, ...providedOptions };\n this.eventDispatcher = new EventDispatcher();\n this.clientId = clientId;\n this.status = 'open';\n this.sessions = new Map();\n }\n\n bindLogger(fn: LogFn | Logger, level?: LoggingLevel) {\n // construct logger from fn\n if (typeof fn === 'function') {\n this.log = createLogProxy(new BaseLogger(fn, level));\n return;\n }\n\n // object case, just assign\n this.log = createLogProxy(fn);\n }\n\n /**\n * Called when a message is received by this transport.\n * You generally shouldn't need to override this in downstream transport implementations.\n * @param msg The received message.\n */\n protected handleMsg(msg: OpaqueTransportMessage) {\n if (this.getStatus() !== 'open') return;\n this.eventDispatcher.dispatchEvent('message', msg);\n }\n\n /**\n * Adds a listener to this transport.\n * @param the type of event to listen for\n * @param handler The message handler to add.\n */\n addEventListener<K extends EventTypes, T extends EventHandler<K>>(\n type: K,\n handler: T,\n ): void {\n this.eventDispatcher.addEventListener(type, handler);\n }\n\n /**\n * Removes a listener from this transport.\n * @param the type of event to un-listen on\n * @param handler The message handler to remove.\n */\n removeEventListener<K extends EventTypes, T extends EventHandler<K>>(\n type: K,\n handler: T,\n ): void {\n this.eventDispatcher.removeEventListener(type, handler);\n }\n\n /**\n * Sends a message over this transport, delegating to the appropriate connection to actually\n * send the message.\n * @param msg The message to send.\n * @returns The ID of the sent message or undefined if it wasn't sent\n */\n abstract send(to: TransportClientId, msg: PartialTransportMessage): string;\n\n protected protocolError(type: ProtocolErrorType, message: string) {\n this.eventDispatcher.dispatchEvent('protocolError', { type, message });\n }\n\n /**\n * Default close implementation for transports. You should override this in the downstream\n * implementation if you need to do any additional cleanup and call super.close() at the end.\n * Closes the transport. Any messages sent while the transport is closed will be silently discarded.\n */\n close() {\n this.status = 'closed';\n\n for (const session of this.sessions.values()) {\n this.deleteSession(session);\n }\n\n this.eventDispatcher.dispatchEvent('transportStatus', {\n status: this.status,\n });\n\n this.eventDispatcher.removeAllListeners();\n\n this.log?.info(`manually closed transport`, { clientId: this.clientId });\n }\n\n getStatus(): TransportStatus {\n return this.status;\n }\n\n protected updateSession<S extends Session<ConnType>>(session: S): S {\n const activeSession = this.sessions.get(session.to);\n if (activeSession && activeSession.id !== session.id) {\n const msg = `attempt to transition active session for ${session.to} but active session (${activeSession.id}) is different from handle (${session.id})`;\n throw new Error(msg);\n }\n\n this.sessions.set(session.to, session);\n\n if (!activeSession) {\n this.eventDispatcher.dispatchEvent('sessionStatus', {\n status: 'connect',\n session: session,\n });\n }\n\n this.eventDispatcher.dispatchEvent('sessionTransition', {\n state: session.state,\n session: session,\n } as EventMap['sessionTransition']);\n\n return session;\n }\n\n // state transitions\n protected deleteSession(session: Session<ConnType>) {\n session.log?.info(`closing session ${session.id}`, session.loggingMetadata);\n\n this.eventDispatcher.dispatchEvent('sessionStatus', {\n status: 'disconnect',\n session: session,\n });\n\n session.close();\n this.sessions.delete(session.to);\n }\n\n // common listeners\n protected onSessionGracePeriodElapsed(session: SessionNoConnection) {\n this.log?.warn(\n `session to ${session.to} grace period elapsed, closing`,\n session.loggingMetadata,\n );\n\n this.deleteSession(session);\n }\n\n protected onConnectingFailed(\n session: SessionConnecting<ConnType>,\n ): SessionNoConnection {\n // transition to no connection\n const noConnectionSession =\n SessionStateGraph.transition.ConnectingToNoConnection(session, {\n onSessionGracePeriodElapsed: () => {\n this.onSessionGracePeriodElapsed(noConnectionSession);\n },\n });\n\n return this.updateSession(noConnectionSession);\n }\n\n protected onConnClosed(\n session: SessionHandshaking<ConnType> | SessionConnected<ConnType>,\n ): SessionNoConnection {\n // transition to no connection\n let noConnectionSession: SessionNoConnection;\n if (session.state === SessionState.Handshaking) {\n noConnectionSession =\n SessionStateGraph.transition.HandshakingToNoConnection(session, {\n onSessionGracePeriodElapsed: () => {\n this.onSessionGracePeriodElapsed(noConnectionSession);\n },\n });\n } else {\n noConnectionSession =\n SessionStateGraph.transition.ConnectedToNoConnection(session, {\n onSessionGracePeriodElapsed: () => {\n this.onSessionGracePeriodElapsed(noConnectionSession);\n },\n });\n }\n\n return this.updateSession(noConnectionSession);\n }\n}\n","import { TelemetryInfo } from '../tracing';\nimport { MessageMetadata } from '../logging';\nimport { generateId } from './id';\n\n/**\n * A connection is the actual raw underlying transport connection.\n * It’s responsible for dispatching to/from the actual connection itself\n * This should be instantiated as soon as the client/server has a connection\n * It’s tied to the lifecycle of the underlying transport connection (i.e. if the WS drops, this connection should be deleted)\n */\nexport abstract class Connection {\n id: string;\n telemetry?: TelemetryInfo;\n constructor() {\n this.id = `conn-${generateId()}`; // for debugging, no collision safety needed\n }\n\n get loggingMetadata(): MessageMetadata {\n const metadata: MessageMetadata = { connId: this.id };\n const spanContext = this.telemetry?.span.spanContext();\n\n if (this.telemetry?.span.isRecording() && spanContext) {\n metadata.telemetry = {\n traceId: spanContext.traceId,\n spanId: spanContext.spanId,\n };\n }\n\n return metadata;\n }\n\n // can't use event emitter because we need this to work in both node + browser\n private _dataListeners = new Set<(msg: Uint8Array) => void>();\n private _closeListeners = new Set<() => void>();\n private _errorListeners = new Set<(err: Error) => void>();\n\n get dataListeners() {\n return [...this._dataListeners];\n }\n\n get closeListeners() {\n return [...this._closeListeners];\n }\n\n get errorListeners() {\n return [...this._errorListeners];\n }\n\n /**\n * Handle adding a callback for when a message is received.\n * @param msg The message that was received.\n */\n addDataListener(cb: (msg: Uint8Array) => void) {\n this._dataListeners.add(cb);\n }\n\n removeDataListener(cb: (msg: Uint8Array) => void): void {\n this._dataListeners.delete(cb);\n }\n\n /**\n * Handle adding a callback for when the connection is closed.\n * This should also be called if an error happens and after notifying all the error listeners.\n * @param cb The callback to call when the connection is closed.\n */\n addCloseListener(cb: () => void): void {\n this._closeListeners.add(cb);\n }\n\n removeCloseListener(cb: () => void): void {\n this._closeListeners.delete(cb);\n }\n\n /**\n * Handle adding a callback for when an error is received.\n * This should only be used for this.logging errors, all cleanup\n * should be delegated to addCloseListener.\n *\n * The implementer should take care such that the implemented\n * connection will call both the close and error callbacks\n * on an error.\n *\n * @param cb The callback to call when an error is received.\n */\n addErrorListener(cb: (err: Error) => void): void {\n this._errorListeners.add(cb);\n }\n\n removeErrorListener(cb: (err: Error) => void): void {\n this._errorListeners.delete(cb);\n }\n\n /**\n * Sends a message over the connection.\n * @param msg The message to send.\n * @returns true if the message was sent, false otherwise.\n */\n abstract send(msg: Uint8Array): boolean;\n\n /**\n * Closes the connection.\n */\n abstract close(): void;\n}\n"],"mappings":";;;;;;;;;;;;;AAKO,IAAM,gBAAgB;AAAA,EAC3B,iBAAiB;AAAA,EACjB,iBAAiB;AAAA,EACjB,yBAAyB;AAC3B;AA+BO,IAAM,kBAAN,MAA4C;AAAA,EACzC,iBAAsD,CAAC;AAAA,EAE/D,qBAAqB;AACnB,SAAK,iBAAiB,CAAC;AAAA,EACzB;AAAA,EAEA,kBAA+B,WAAc;AAC3C,WAAO,KAAK,eAAe,SAAS,GAAG,QAAQ;AAAA,EACjD;AAAA,EAEA,iBAA8B,WAAc,SAA0B;AACpE,QAAI,CAAC,KAAK,eAAe,SAAS,GAAG;AACnC,WAAK,eAAe,SAAS,IAAI,oBAAI,IAAI;AAAA,IAC3C;AAEA,SAAK,eAAe,SAAS,GAAG,IAAI,OAAO;AAAA,EAC7C;AAAA,EAEA,oBAAiC,WAAc,SAA0B;AACvE,UAAM,WAAW,KAAK,eAAe,SAAS;AAC9C,QAAI,UAAU;AACZ,WAAK,eAAe,SAAS,GAAG,OAAO,OAAO;AAAA,IAChD;AAAA,EACF;AAAA,EAEA,cAA2B,WAAc,OAAoB;AAC3D,UAAM,WAAW,KAAK,eAAe,SAAS;AAC9C,QAAI,UAAU;AAGZ,YAAM,OAAO,CAAC,GAAG,QAAQ;AACzB,iBAAW,WAAW,MAAM;AAC1B,gBAAQ,KAAK;AAAA,MACf;AAAA,IACF;AAAA,EACF;AACF;;;ACfO,IAAe,YAAf,MAAsD;AAAA;AAAA;AAAA;AAAA,EAInD;AAAA;AAAA;AAAA;AAAA,EAKR;AAAA;AAAA;AAAA;AAAA,EAKA;A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AA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,iBAAiB,IAAsB;AACrC,SAAK,gBAAgB,IAAI,EAAE;AAAA,EAC7B;AAAA,EAEA,oBAAoB,IAAsB;AACxC,SAAK,gBAAgB,OAAO,EAAE;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,iBAAiB,IAAgC;AAC/C,SAAK,gBAAgB,IAAI,EAAE;AAAA,EAC7B;AAAA,EAEA,oBAAoB,IAAgC;AAClD,SAAK,gBAAgB,OAAO,EAAE;AAAA,EAChC;AAaF;","names":[]}
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  ProtocolError,
3
3
  Transport
4
- } from "./chunk-D5O3ERJU.js";
4
+ } from "./chunk-5FDAIAQ5.js";
5
5
  import {
6
- SessionStateGraph,
6
+ ClientSessionStateGraph,
7
7
  defaultClientTransportOptions
8
- } from "./chunk-YM5Y4NAT.js";
8
+ } from "./chunk-JSU2KACV.js";
9
9
  import {
10
10
  ControlMessageHandshakeResponseSchema,
11
11
  HandshakeErrorRetriableResponseCodes,
@@ -13,7 +13,7 @@ import {
13
13
  getPropagationContext,
14
14
  handshakeRequestMessage,
15
15
  tracing_default
16
- } from "./chunk-SZ5NBBX7.js";
16
+ } from "./chunk-CCUYKR5C.js";
17
17
 
18
18
  // transport/client.ts
19
19
  import { SpanStatusCode } from "@opentelemetry/api";
@@ -21,17 +21,17 @@ import { SpanStatusCode } from "@opentelemetry/api";
21
21
  // transport/rateLimit.ts
22
22
  var LeakyBucketRateLimit = class {
23
23
  budgetConsumed;
24
- intervalHandles;
24
+ intervalHandle;
25
25
  options;
26
26
  constructor(options) {
27
27
  this.options = options;
28
- this.budgetConsumed = /* @__PURE__ */ new Map();
29
- this.intervalHandles = /* @__PURE__ */ new Map();
28
+ this.budgetConsumed = 0;
30
29
  }
31
- getBackoffMs(user) {
32
- if (!this.budgetConsumed.has(user))
30
+ getBackoffMs() {
31
+ if (this.getBudgetConsumed() === 0) {
33
32
  return 0;
34
- const exponent = Math.max(0, this.getBudgetConsumed(user) - 1);
33
+ }
34
+ const exponent = Math.max(0, this.getBudgetConsumed() - 1);
35
35
  const jitter = Math.floor(Math.random() * this.options.maxJitterMs);
36
36
  const backoffMs = Math.min(
37
37
  this.options.baseIntervalMs * 2 ** exponent,
@@ -42,50 +42,46 @@ var LeakyBucketRateLimit = class {
42
42
  get totalBudgetRestoreTime() {
43
43
  return this.options.budgetRestoreIntervalMs * this.options.attemptBudgetCapacity;
44
44
  }
45
- consumeBudget(user) {
46
- this.stopLeak(user);
47
- this.budgetConsumed.set(user, this.getBudgetConsumed(user) + 1);
45
+ consumeBudget() {
46
+ this.stopLeak();
47
+ this.budgetConsumed = this.getBudgetConsumed() + 1;
48
48
  }
49
- getBudgetConsumed(user) {
50
- return this.budgetConsumed.get(user) ?? 0;
49
+ getBudgetConsumed() {
50
+ return this.budgetConsumed;
51
51
  }
52
- hasBudget(user) {
53
- return this.getBudgetConsumed(user) < this.options.attemptBudgetCapacity;
52
+ hasBudget() {
53
+ return this.getBudgetConsumed() < this.options.attemptBudgetCapacity;
54
54
  }
55
- startRestoringBudget(user) {
56
- if (this.intervalHandles.has(user)) {
55
+ startRestoringBudget() {
56
+ if (this.intervalHandle) {
57
57
  return;
58
58
  }
59
59
  const restoreBudgetForUser = () => {
60
- const currentBudget = this.budgetConsumed.get(user);
60
+ const currentBudget = this.budgetConsumed;
61
61
  if (!currentBudget) {
62
- this.stopLeak(user);
62
+ this.stopLeak();
63
63
  return;
64
64
  }
65
65
  const newBudget = currentBudget - 1;
66
66
  if (newBudget === 0) {
67
- this.budgetConsumed.delete(user);
68
67
  return;
69
68
  }
70
- this.budgetConsumed.set(user, newBudget);
69
+ this.budgetConsumed = newBudget;
71
70
  };
72
- const intervalHandle = setInterval(
71
+ this.intervalHandle = setInterval(
73
72
  restoreBudgetForUser,
74
73
  this.options.budgetRestoreIntervalMs
75
74
  );
76
- this.intervalHandles.set(user, intervalHandle);
77
75
  }
78
- stopLeak(user) {
79
- if (!this.intervalHandles.has(user)) {
76
+ stopLeak() {
77
+ if (!this.intervalHandle) {
80
78
  return;
81
79
  }
82
- clearInterval(this.intervalHandles.get(user));
83
- this.intervalHandles.delete(user);
80
+ clearInterval(this.intervalHandle);
81
+ this.intervalHandle = void 0;
84
82
  }
85
83
  close() {
86
- for (const user of this.intervalHandles.keys()) {
87
- this.stopLeak(user);
88
- }
84
+ this.stopLeak();
89
85
  }
90
86
  };
91
87
 
@@ -108,8 +104,10 @@ var ClientTransport = class extends Transport {
108
104
  * Optional handshake options for this client.
109
105
  */
110
106
  handshakeExtensions;
107
+ sessions;
111
108
  constructor(clientId, providedOptions) {
112
109
  super(clientId, providedOptions);
110
+ this.sessions = /* @__PURE__ */ new Map();
113
111
  this.options = {
114
112
  ...defaultClientTransportOptions,
115
113
  ...providedOptions
@@ -141,7 +139,7 @@ var ClientTransport = class extends Transport {
141
139
  return session.send(msg);
142
140
  }
143
141
  createUnconnectedSession(to) {
144
- const session = SessionStateGraph.entrypoints.NoConnection(
142
+ const session = ClientSessionStateGraph.entrypoint(
145
143
  to,
146
144
  this.clientId,
147
145
  {
@@ -167,40 +165,44 @@ var ClientTransport = class extends Transport {
167
165
  return noConnectionSession;
168
166
  }
169
167
  onConnectionEstablished(session, conn) {
170
- const handshakingSession = SessionStateGraph.transition.ConnectingToHandshaking(session, conn, {
171
- onConnectionErrored: (err) => {
172
- const errStr = coerceErrorString(err);
173
- this.log?.error(
174
- `connection to ${handshakingSession.to} errored during handshake: ${errStr}`,
175
- handshakingSession.loggingMetadata
176
- );
177
- },
178
- onConnectionClosed: () => {
179
- this.log?.warn(
180
- `connection to ${handshakingSession.to} closed during handshake`,
181
- handshakingSession.loggingMetadata
182
- );
183
- this.onConnClosed(handshakingSession);
184
- },
185
- onHandshake: (msg) => {
186
- this.onHandshakeResponse(handshakingSession, msg);
187
- },
188
- onInvalidHandshake: (reason) => {
189
- this.log?.error(
190
- `invalid handshake: ${reason}`,
191
- handshakingSession.loggingMetadata
192
- );
193
- this.deleteSession(session);
194
- this.protocolError(ProtocolError.HandshakeFailed, reason);
195
- },
196
- onHandshakeTimeout: () => {
197
- this.log?.error(
198
- `connection to ${handshakingSession.to} timed out during handshake`,
199
- handshakingSession.loggingMetadata
200
- );
201
- this.onConnClosed(handshakingSession);
168
+ const handshakingSession = ClientSessionStateGraph.transition.ConnectingToHandshaking(
169
+ session,
170
+ conn,
171
+ {
172
+ onConnectionErrored: (err) => {
173
+ const errStr = coerceErrorString(err);
174
+ this.log?.error(
175
+ `connection to ${handshakingSession.to} errored during handshake: ${errStr}`,
176
+ handshakingSession.loggingMetadata
177
+ );
178
+ },
179
+ onConnectionClosed: () => {
180
+ this.log?.warn(
181
+ `connection to ${handshakingSession.to} closed during handshake`,
182
+ handshakingSession.loggingMetadata
183
+ );
184
+ this.onConnClosed(handshakingSession);
185
+ },
186
+ onHandshake: (msg) => {
187
+ this.onHandshakeResponse(handshakingSession, msg);
188
+ },
189
+ onInvalidHandshake: (reason) => {
190
+ this.log?.error(
191
+ `invalid handshake: ${reason}`,
192
+ handshakingSession.loggingMetadata
193
+ );
194
+ this.deleteSession(session);
195
+ this.protocolError(ProtocolError.HandshakeFailed, reason);
196
+ },
197
+ onHandshakeTimeout: () => {
198
+ this.log?.error(
199
+ `connection to ${handshakingSession.to} timed out during handshake`,
200
+ handshakingSession.loggingMetadata
201
+ );
202
+ this.onConnClosed(handshakingSession);
203
+ }
202
204
  }
203
- });
205
+ );
204
206
  this.updateSession(handshakingSession);
205
207
  void this.sendHandshake(handshakingSession);
206
208
  return handshakingSession;
@@ -255,7 +257,7 @@ var ClientTransport = class extends Transport {
255
257
  ...session.loggingMetadata,
256
258
  transportMessage: msg
257
259
  });
258
- const connectedSession = SessionStateGraph.transition.HandshakingToConnected(session, {
260
+ const connectedSession = ClientSessionStateGraph.transition.HandshakingToConnected(session, {
259
261
  onConnectionErrored: (err) => {
260
262
  const errStr = coerceErrorString(err);
261
263
  this.log?.warn(
@@ -277,7 +279,7 @@ var ClientTransport = class extends Transport {
277
279
  }
278
280
  });
279
281
  this.updateSession(connectedSession);
280
- this.retryBudget.startRestoringBudget(connectedSession.to);
282
+ this.retryBudget.startRestoringBudget();
281
283
  }
282
284
  /**
283
285
  * Manually attempts to connect to a client.
@@ -300,44 +302,49 @@ var ClientTransport = class extends Transport {
300
302
  );
301
303
  return;
302
304
  }
303
- if (!this.retryBudget.hasBudget(to)) {
304
- const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
305
+ if (!this.retryBudget.hasBudget()) {
306
+ const budgetConsumed = this.retryBudget.getBudgetConsumed();
305
307
  const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
306
308
  this.log?.error(errMsg, session.loggingMetadata);
307
309
  this.protocolError(ProtocolError.RetriesExceeded, errMsg);
308
310
  return;
309
311
  }
310
- let sleep = Promise.resolve();
311
- const backoffMs = this.retryBudget.getBackoffMs(to);
312
- if (backoffMs > 0) {
313
- sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
314
- }
312
+ const backoffMs = this.retryBudget.getBackoffMs();
315
313
  this.log?.info(
316
314
  `attempting connection to ${to} (${backoffMs}ms backoff)`,
317
315
  session.loggingMetadata
318
316
  );
319
- this.retryBudget.consumeBudget(to);
320
- const reconnectPromise = tracing_default.startActiveSpan("connect", async (span) => {
321
- try {
322
- span.addEvent("backoff", { backoffMs });
323
- await sleep;
324
- if (this.getStatus() !== "open") {
325
- throw new Error("transport state is no longer open");
317
+ this.retryBudget.consumeBudget();
318
+ const backingOffSession = ClientSessionStateGraph.transition.NoConnectionToBackingOff(
319
+ session,
320
+ backoffMs,
321
+ {
322
+ onBackoffFinished: () => {
323
+ const reconnectPromise = tracing_default.startActiveSpan(
324
+ "connect",
325
+ async (span) => {
326
+ try {
327
+ return await this.createNewOutgoingConnection(to);
328
+ } catch (err) {
329
+ const errStr = coerceErrorString(err);
330
+ span.recordException(errStr);
331
+ span.setStatus({ code: SpanStatusCode.ERROR });
332
+ throw err;
333
+ } finally {
334
+ span.end();
335
+ }
336
+ }
337
+ );
338
+ this.onBackoffFinished(backingOffSession, reconnectPromise);
326
339
  }
327
- span.addEvent("connecting");
328
- return await this.createNewOutgoingConnection(to);
329
- } catch (err) {
330
- const errStr = coerceErrorString(err);
331
- span.recordException(errStr);
332
- span.setStatus({ code: SpanStatusCode.ERROR });
333
- throw err;
334
- } finally {
335
- span.end();
336
340
  }
337
- });
338
- const connectingSession = SessionStateGraph.transition.NoConnectionToConnecting(
341
+ );
342
+ this.updateSession(backingOffSession);
343
+ }
344
+ onBackoffFinished(session, connPromise) {
345
+ const connectingSession = ClientSessionStateGraph.transition.BackingOffToConnecting(
339
346
  session,
340
- reconnectPromise,
347
+ connPromise,
341
348
  {
342
349
  onConnectionEstablished: (conn) => {
343
350
  this.log?.debug(
@@ -396,4 +403,4 @@ var ClientTransport = class extends Transport {
396
403
  export {
397
404
  ClientTransport
398
405
  };
399
- //# sourceMappingURL=chunk-5CNNIOAO.js.map
406
+ //# sourceMappingURL=chunk-5S64PXTU.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../transport/client.ts","../transport/rateLimit.ts"],"sourcesContent":["import { SpanStatusCode } from '@opentelemetry/api';\nimport { ClientHandshakeOptions } from '../router/handshake';\nimport {\n ControlMessageHandshakeResponseSchema,\n HandshakeErrorRetriableResponseCodes,\n OpaqueTransportMessage,\n PartialTransportMessage,\n TransportClientId,\n handshakeRequestMessage,\n} from './message';\nimport {\n ClientTransportOptions,\n ProvidedClientTransportOptions,\n defaultClientTransportOptions,\n} from './options';\nimport { LeakyBucketRateLimit } from './rateLimit';\nimport { Transport } from './transport';\nimport { coerceErrorString } from '../util/stringify';\nimport { ProtocolError } from './events';\nimport { Value } from '@sinclair/typebox/value';\nimport tracer, { getPropagationContext } from '../tracing';\nimport { Connection } from './connection';\nimport { MessageMetadata } from '../logging';\nimport { SessionConnecting } from './sessionStateMachine/SessionConnecting';\nimport { SessionHandshaking } from './sessionStateMachine/SessionHandshaking';\nimport { SessionConnected } from './sessionStateMachine/SessionConnected';\nimport {\n ClientSession,\n ClientSessionStateGraph,\n} from './sessionStateMachine/transitions';\nimport { SessionState } from './sessionStateMachine/common';\nimport { SessionNoConnection } from './sessionStateMachine/SessionNoConnection';\nimport { SessionBackingOff } from './sessionStateMachine/SessionBackingOff';\n\nexport abstract class ClientTransport<\n ConnType extends Connection,\n> extends Transport<ConnType> {\n /**\n * The options for this transport.\n */\n protected options: ClientTransportOptions;\n\n retryBudget: LeakyBucketRateLimit;\n\n /**\n * A flag indicating whether the transport should automatically reconnect\n * when a connection is dropped.\n * Realistically, this should always be true for clients unless you are writing\n * tests or a special case where you don't want to reconnect.\n */\n reconnectOnConnectionDrop = true;\n\n /**\n * Optional handshake options for this client.\n */\n handshakeExtensions?: ClientHandshakeOptions;\n\n sessions: Map<TransportClientId, ClientSession<ConnType>>;\n\n constructor(\n clientId: TransportClientId,\n providedOptions?: ProvidedClientTransportOptions,\n ) {\n super(clientId, providedOptions);\n this.sessions = new Map();\n this.options = {\n ...defaultClientTransportOptions,\n ...providedOptions,\n };\n this.retryBudget = new LeakyBucketRateLimit(this.options);\n }\n\n extendHandshake(options: ClientHandshakeOptions) {\n this.handshakeExtensions = options;\n }\n\n /**\n * Abstract method that creates a new {@link Connection} object.\n * This should call {@link handleConnection} when the connection is created.\n * The downstream client implementation needs to implement this.\n *\n * @param to The client ID of the node to connect to.\n * @returns The new connection object.\n */\n protected abstract createNewOutgoingConnection(\n to: TransportClientId,\n ): Promise<ConnType>;\n\n private tryReconnecting(to: string) {\n if (this.reconnectOnConnectionDrop && this.getStatus() === 'open') {\n this.connect(to);\n }\n }\n\n send(to: string, msg: PartialTransportMessage): string {\n if (this.getStatus() === 'closed') {\n const err = 'transport is closed, cant send';\n this.log?.error(err, {\n clientId: this.clientId,\n transportMessage: msg,\n tags: ['invariant-violation'],\n });\n\n throw new Error(err);\n }\n\n let session = this.sessions.get(to);\n if (!session) {\n session = this.createUnconnectedSession(to);\n }\n\n return session.send(msg);\n }\n\n private createUnconnectedSession(to: string): SessionNoConnection {\n const session = ClientSessionStateGraph.entrypoint(\n to,\n this.clientId,\n {\n onSessionGracePeriodElapsed: () => {\n this.onSessionGracePeriodElapsed(session);\n },\n },\n this.options,\n this.log,\n );\n\n this.updateSession(session);\n return session;\n }\n\n // listeners\n protected onConnectingFailed(session: SessionConnecting<ConnType>) {\n const noConnectionSession = super.onConnectingFailed(session);\n this.tryReconnecting(noConnectionSession.to);\n return noConnectionSession;\n }\n\n protected onConnClosed(\n session: SessionHandshaking<ConnType> | SessionConnected<ConnType>,\n ) {\n const noConnectionSession = super.onConnClosed(session);\n this.tryReconnecting(noConnectionSession.to);\n return noConnectionSession;\n }\n\n protected onConnectionEstablished(\n session: SessionConnecting<ConnType>,\n conn: ConnType,\n ): SessionHandshaking<ConnType> {\n // transition to handshaking\n const handshakingSession =\n ClientSessionStateGraph.transition.ConnectingToHandshaking(\n session,\n conn,\n {\n onConnectionErrored: (err) => {\n // just log, when we error we also emit close\n const errStr = coerceErrorString(err);\n this.log?.error(\n `connection to ${handshakingSession.to} errored during handshake: ${errStr}`,\n handshakingSession.loggingMetadata,\n );\n },\n onConnectionClosed: () => {\n this.log?.warn(\n `connection to ${handshakingSession.to} closed during handshake`,\n handshakingSession.loggingMetadata,\n );\n this.onConnClosed(handshakingSession);\n },\n onHandshake: (msg) => {\n this.onHandshakeResponse(handshakingSession, msg);\n },\n onInvalidHandshake: (reason) => {\n this.log?.error(\n `invalid handshake: ${reason}`,\n handshakingSession.loggingMetadata,\n );\n this.deleteSession(session);\n this.protocolError(ProtocolError.HandshakeFailed, reason);\n },\n onHandshakeTimeout: () => {\n this.log?.error(\n `connection to ${handshakingSession.to} timed out during handshake`,\n handshakingSession.loggingMetadata,\n );\n this.onConnClosed(handshakingSession);\n },\n },\n );\n\n this.updateSession(handshakingSession);\n void this.sendHandshake(handshakingSession);\n return handshakingSession;\n }\n\n private rejectHandshakeResponse(\n session: SessionHandshaking<ConnType>,\n reason: string,\n metadata: MessageMetadata,\n ) {\n session.conn.telemetry?.span.setStatus({\n code: SpanStatusCode.ERROR,\n message: reason,\n });\n\n this.log?.warn(reason, metadata);\n this.deleteSession(session);\n }\n\n protected onHandshakeResponse(\n session: SessionHandshaking<ConnType>,\n msg: OpaqueTransportMessage,\n ) {\n // invariant: msg is a handshake response\n if (!Value.Check(ControlMessageHandshakeResponseSchema, msg.payload)) {\n const reason = `received invalid handshake response`;\n this.rejectHandshakeResponse(session, reason, {\n ...session.loggingMetadata,\n transportMessage: msg,\n validationErrors: [\n ...Value.Errors(ControlMessageHandshakeResponseSchema, msg.payload),\n ],\n });\n return;\n }\n\n // invariant: handshake response should be ok\n if (!msg.payload.status.ok) {\n // TODO: remove conditional check after we know code is always present\n const retriable = msg.payload.status.code\n ? Value.Check(\n HandshakeErrorRetriableResponseCodes,\n msg.payload.status.code,\n )\n : false;\n\n const reason = `handshake failed: ${msg.payload.status.reason}`;\n this.rejectHandshakeResponse(session, reason, {\n ...session.loggingMetadata,\n transportMessage: msg,\n });\n\n if (retriable) {\n this.tryReconnecting(session.to);\n } else {\n this.deleteSession(session);\n this.protocolError(ProtocolError.HandshakeFailed, reason);\n }\n\n return;\n }\n\n // invariant: session id should match between client + server\n if (msg.payload.status.sessionId !== session.id) {\n const reason = `session id mismatch: expected ${session.id}, got ${msg.payload.status.sessionId}`;\n this.rejectHandshakeResponse(session, reason, {\n ...session.loggingMetadata,\n transportMessage: msg,\n });\n return;\n }\n\n // transition to connected!\n this.log?.info(`handshake from ${msg.from} ok`, {\n ...session.loggingMetadata,\n transportMessage: msg,\n });\n\n const connectedSession =\n ClientSessionStateGraph.transition.HandshakingToConnected(session, {\n onConnectionErrored: (err) => {\n // just log, when we error we also emit close\n const errStr = coerceErrorString(err);\n this.log?.warn(\n `connection to ${connectedSession.to} errored: ${errStr}`,\n connectedSession.loggingMetadata,\n );\n },\n onConnectionClosed: () => {\n this.log?.info(\n `connection to ${connectedSession.to} closed`,\n connectedSession.loggingMetadata,\n );\n this.onConnClosed(connectedSession);\n },\n onMessage: (msg) => this.handleMsg(msg),\n onInvalidMessage: (reason) => {\n this.deleteSession(connectedSession);\n this.protocolError(ProtocolError.MessageOrderingViolated, reason);\n },\n });\n\n this.updateSession(connectedSession);\n this.retryBudget.startRestoringBudget();\n }\n\n /**\n * Manually attempts to connect to a client.\n * @param to The client ID of the node to connect to.\n */\n connect(to: TransportClientId) {\n // create a new session if one does not exist\n let session = this.sessions.get(to);\n session ??= this.createUnconnectedSession(to);\n\n if (session.state !== SessionState.NoConnection) {\n // already trying to connect\n this.log?.debug(\n `session to ${to} has state ${session.state}, skipping connect attempt`,\n session.loggingMetadata,\n );\n return;\n }\n\n if (this.getStatus() !== 'open') {\n this.log?.info(\n `transport state is no longer open, cancelling attempt to connect to ${to}`,\n session.loggingMetadata,\n );\n return;\n }\n\n // check budget\n if (!this.retryBudget.hasBudget()) {\n const budgetConsumed = this.retryBudget.getBudgetConsumed();\n const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;\n this.log?.error(errMsg, session.loggingMetadata);\n this.protocolError(ProtocolError.RetriesExceeded, errMsg);\n return;\n }\n\n const backoffMs = this.retryBudget.getBackoffMs();\n\n this.log?.info(\n `attempting connection to ${to} (${backoffMs}ms backoff)`,\n session.loggingMetadata,\n );\n\n this.retryBudget.consumeBudget();\n const backingOffSession =\n ClientSessionStateGraph.transition.NoConnectionToBackingOff(\n session,\n backoffMs,\n {\n onBackoffFinished: () => {\n const reconnectPromise = tracer.startActiveSpan(\n 'connect',\n async (span) => {\n try {\n return await this.createNewOutgoingConnection(to);\n } catch (err) {\n // rethrow the error so that the promise is rejected\n // as it was before we wrapped it in a span\n const errStr = coerceErrorString(err);\n span.recordException(errStr);\n span.setStatus({ code: SpanStatusCode.ERROR });\n throw err;\n } finally {\n span.end();\n }\n },\n );\n\n this.onBackoffFinished(backingOffSession, reconnectPromise);\n },\n },\n );\n\n this.updateSession(backingOffSession);\n }\n\n protected onBackoffFinished(\n session: SessionBackingOff,\n connPromise: Promise<ConnType>,\n ) {\n // transition to connecting\n const connectingSession =\n ClientSessionStateGraph.transition.BackingOffToConnecting(\n session,\n connPromise,\n {\n onConnectionEstablished: (conn) => {\n this.log?.debug(\n `connection to ${connectingSession.to} established`,\n connectingSession.loggingMetadata,\n );\n\n // cast here because conn can't be narrowed to ConnType\n // in the callback due to variance rules\n this.onConnectionEstablished(connectingSession, conn as ConnType);\n },\n onConnectionFailed: (error: unknown) => {\n const errStr = coerceErrorString(error);\n this.log?.error(\n `error connecting to ${connectingSession.to}: ${errStr}`,\n connectingSession.loggingMetadata,\n );\n this.onConnectingFailed(connectingSession);\n },\n onConnectionTimeout: () => {\n this.log?.error(\n `connection to ${connectingSession.to} timed out`,\n connectingSession.loggingMetadata,\n );\n this.onConnectingFailed(connectingSession);\n },\n },\n );\n\n this.updateSession(connectingSession);\n }\n\n private async sendHandshake(session: SessionHandshaking<ConnType>) {\n let metadata: unknown = undefined;\n\n if (this.handshakeExtensions) {\n metadata = await this.handshakeExtensions.construct();\n }\n\n const requestMsg = handshakeRequestMessage({\n from: this.clientId,\n to: session.to,\n sessionId: session.id,\n expectedSessionState: {\n nextExpectedSeq: session.ack,\n nextSentSeq: session.nextSeq(),\n },\n metadata,\n tracing: getPropagationContext(session.telemetry.ctx),\n });\n\n this.log?.debug(`sending handshake request to ${session.to}`, {\n ...session.loggingMetadata,\n transportMessage: requestMsg,\n });\n\n session.sendHandshake(requestMsg);\n }\n\n close() {\n this.retryBudget.close();\n super.close();\n }\n}\n","/**\n * Options to control the backoff and retry behavior of the client transport's connection behaviour.\n *\n * River implements exponential backoff with jitter to prevent flooding the server\n * when there's an issue with connection establishment.\n *\n * The backoff is calculated via the following:\n * backOff = min(jitter + {@link baseIntervalMs} * 2 ^ budget_consumed, {@link maxBackoffMs})\n *\n * We use a leaky bucket rate limit with a budget of {@link attemptBudgetCapacity} reconnection attempts.\n * Budget only starts to restore after a successful handshake at a rate of one budget per {@link budgetRestoreIntervalMs}.\n */\nexport interface ConnectionRetryOptions {\n /**\n * The base interval to wait before retrying a connection.\n */\n baseIntervalMs: number;\n\n /**\n * The maximum random jitter to add to the total backoff time.\n */\n maxJitterMs: number;\n\n /**\n * The maximum amount of time to wait before retrying a connection.\n * This does not include the jitter.\n */\n maxBackoffMs: number;\n\n /**\n * The max number of times to attempt a connection before a successful handshake.\n * This persists across connections but starts restoring budget after a successful handshake.\n * The restoration interval depends on {@link budgetRestoreIntervalMs}\n */\n attemptBudgetCapacity: number;\n\n /**\n * After a successful connection attempt, how long to wait before we restore a single budget.\n */\n budgetRestoreIntervalMs: number;\n}\n\nexport class LeakyBucketRateLimit {\n private budgetConsumed: number;\n private intervalHandle?: ReturnType<typeof setInterval>;\n private readonly options: ConnectionRetryOptions;\n\n constructor(options: ConnectionRetryOptions) {\n this.options = options;\n this.budgetConsumed = 0;\n }\n\n getBackoffMs() {\n if (this.getBudgetConsumed() === 0) {\n return 0;\n }\n\n const exponent = Math.max(0, this.getBudgetConsumed() - 1);\n const jitter = Math.floor(Math.random() * this.options.maxJitterMs);\n const backoffMs = Math.min(\n this.options.baseIntervalMs * 2 ** exponent,\n this.options.maxBackoffMs,\n );\n\n return backoffMs + jitter;\n }\n\n get totalBudgetRestoreTime() {\n return (\n this.options.budgetRestoreIntervalMs * this.options.attemptBudgetCapacity\n );\n }\n\n consumeBudget() {\n // If we're consuming again, let's ensure that we're not leaking\n this.stopLeak();\n this.budgetConsumed = this.getBudgetConsumed() + 1;\n }\n\n getBudgetConsumed() {\n return this.budgetConsumed;\n }\n\n hasBudget() {\n return this.getBudgetConsumed() < this.options.attemptBudgetCapacity;\n }\n\n startRestoringBudget() {\n if (this.intervalHandle) {\n return;\n }\n\n const restoreBudgetForUser = () => {\n const currentBudget = this.budgetConsumed;\n if (!currentBudget) {\n this.stopLeak();\n return;\n }\n\n const newBudget = currentBudget - 1;\n if (newBudget === 0) {\n return;\n }\n\n this.budgetConsumed = newBudget;\n };\n\n this.intervalHandle = setInterval(\n restoreBudgetForUser,\n this.options.budgetRestoreIntervalMs,\n );\n }\n\n private stopLeak() {\n if (!this.intervalHandle) {\n return;\n }\n\n clearInterval(this.intervalHandle);\n this.intervalHandle = undefined;\n }\n\n close() {\n this.stopLeak();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA,SAAS,sBAAsB;;;AC0CxB,IAAM,uBAAN,MAA2B;AAAA,EACxB;AAAA,EACA;AAAA,EACS;AAAA,EAEjB,YAAY,SAAiC;AAC3C,SAAK,UAAU;AACf,SAAK,iBAAiB;AAAA,EACxB;AAAA,EAEA,eAAe;AACb,QAAI,KAAK,kBAAkB,MAAM,GAAG;AAClC,aAAO;AAAA,IACT;AAEA,UAAM,WAAW,KAAK,IAAI,GAAG,KAAK,kBAAkB,IAAI,CAAC;AACzD,UAAM,SAAS,KAAK,MAAM,KAAK,OAAO,IAAI,KAAK,QAAQ,WAAW;AAClE,UAAM,YAAY,KAAK;AAAA,MACrB,KAAK,QAAQ,iBAAiB,KAAK;AAAA,MACnC,KAAK,QAAQ;AAAA,IACf;AAEA,WAAO,YAAY;AAAA,EACrB;AAAA,EAEA,IAAI,yBAAyB;AAC3B,WACE,KAAK,QAAQ,0BAA0B,KAAK,QAAQ;AAAA,EAExD;AAAA,EAEA,gBAAgB;AAEd,SAAK,SAAS;AACd,SAAK,iBAAiB,KAAK,kBAAkB,IAAI;AAAA,EACnD;AAAA,EAEA,oBAAoB;AAClB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,YAAY;AACV,WAAO,KAAK,kBAAkB,IAAI,KAAK,QAAQ;AAAA,EACjD;AAAA,EAEA,uBAAuB;AACrB,QAAI,KAAK,gBAAgB;AACvB;AAAA,IACF;AAEA,UAAM,u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;AAAA,EAEA,QAAQ;AACN,SAAK,YAAY,MAAM;AACvB,UAAM,MAAM;AAAA,EACd;AACF;","names":["msg"]}
@@ -1,17 +1,18 @@
1
1
  import {
2
2
  ProtocolError,
3
3
  Transport
4
- } from "./chunk-D5O3ERJU.js";
4
+ } from "./chunk-5FDAIAQ5.js";
5
5
  import {
6
- SessionStateGraph,
6
+ ServerSessionStateGraph,
7
7
  defaultServerTransportOptions
8
- } from "./chunk-YM5Y4NAT.js";
8
+ } from "./chunk-JSU2KACV.js";
9
9
  import {
10
10
  ControlMessageHandshakeRequestSchema,
11
+ HandshakeErrorCustomHandlerFatalResponseCodes,
11
12
  PROTOCOL_VERSION,
12
13
  coerceErrorString,
13
14
  handshakeResponseMessage
14
- } from "./chunk-SZ5NBBX7.js";
15
+ } from "./chunk-CCUYKR5C.js";
15
16
 
16
17
  // transport/server.ts
17
18
  import { SpanStatusCode } from "@opentelemetry/api";
@@ -29,9 +30,11 @@ var ServerTransport = class extends Transport {
29
30
  * A map of session handshake data for each session.
30
31
  */
31
32
  sessionHandshakeMetadata = /* @__PURE__ */ new Map();
33
+ sessions = /* @__PURE__ */ new Map();
32
34
  pendingSessions = /* @__PURE__ */ new Set();
33
35
  constructor(clientId, providedOptions) {
34
36
  super(clientId, providedOptions);
37
+ this.sessions = /* @__PURE__ */ new Map();
35
38
  this.options = {
36
39
  ...defaultServerTransportOptions,
37
40
  ...providedOptions
@@ -82,7 +85,7 @@ var ServerTransport = class extends Transport {
82
85
  clientId: this.clientId
83
86
  });
84
87
  let receivedHandshake = false;
85
- const pendingSession = SessionStateGraph.entrypoints.WaitingForHandshake(
88
+ const pendingSession = ServerSessionStateGraph.entrypoint(
86
89
  this.clientId,
87
90
  conn,
88
91
  {
@@ -236,26 +239,15 @@ var ServerTransport = class extends Transport {
236
239
  );
237
240
  return;
238
241
  }
239
- if (oldSession.state === "Connected" /* Connected */) {
240
- const noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(oldSession, {
241
- onSessionGracePeriodElapsed: () => {
242
- this.onSessionGracePeriodElapsed(noConnectionSession);
243
- }
244
- });
245
- oldSession = noConnectionSession;
246
- } else if (oldSession.state === "Handshaking" /* Handshaking */) {
247
- const noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(oldSession, {
248
- onSessionGracePeriodElapsed: () => {
249
- this.onSessionGracePeriodElapsed(noConnectionSession);
250
- }
251
- });
252
- oldSession = noConnectionSession;
253
- } else if (oldSession.state === "Connecting" /* Connecting */) {
254
- const noConnectionSession = SessionStateGraph.transition.ConnectingToNoConnection(oldSession, {
255
- onSessionGracePeriodElapsed: () => {
256
- this.onSessionGracePeriodElapsed(noConnectionSession);
242
+ if (oldSession.state !== "NoConnection" /* NoConnection */) {
243
+ const noConnectionSession = ServerSessionStateGraph.transition.ConnectedToNoConnection(
244
+ oldSession,
245
+ {
246
+ onSessionGracePeriodElapsed: () => {
247
+ this.onSessionGracePeriodElapsed(noConnectionSession);
248
+ }
257
249
  }
258
- });
250
+ );
259
251
  oldSession = noConnectionSession;
260
252
  }
261
253
  this.updateSession(oldSession);
@@ -304,7 +296,7 @@ var ServerTransport = class extends Transport {
304
296
  }
305
297
  });
306
298
  session.sendHandshake(responseMsg);
307
- const connectedSession = SessionStateGraph.transition.WaitingForHandshakeToConnected(
299
+ const connectedSession = ServerSessionStateGraph.transition.WaitingForHandshakeToConnected(
308
300
  session,
309
301
  // by this point oldSession is either no connection or we dont have an old session
310
302
  oldSession,
@@ -339,43 +331,43 @@ var ServerTransport = class extends Transport {
339
331
  connectedSession.startActiveHeartbeat();
340
332
  }
341
333
  async validateHandshakeMetadata(handshakingSession, existingSession, rawMetadata, from) {
342
- let parsedMetadata = {};
343
- if (this.handshakeExtensions) {
344
- if (!Value.Check(this.handshakeExtensions.schema, rawMetadata)) {
345
- this.rejectHandshakeRequest(
346
- handshakingSession,
347
- from,
348
- "received malformed handshake metadata",
349
- "MALFORMED_HANDSHAKE_META",
350
- {
351
- ...handshakingSession.loggingMetadata,
352
- connectedTo: from,
353
- validationErrors: [
354
- ...Value.Errors(this.handshakeExtensions.schema, rawMetadata)
355
- ]
356
- }
357
- );
358
- return false;
359
- }
360
- const previousParsedMetadata = existingSession ? this.sessionHandshakeMetadata.get(existingSession.to) : void 0;
361
- parsedMetadata = await this.handshakeExtensions.validate(
362
- rawMetadata,
363
- previousParsedMetadata
334
+ if (!this.handshakeExtensions) {
335
+ return {};
336
+ }
337
+ if (!Value.Check(this.handshakeExtensions.schema, rawMetadata)) {
338
+ this.rejectHandshakeRequest(
339
+ handshakingSession,
340
+ from,
341
+ "received malformed handshake metadata",
342
+ "MALFORMED_HANDSHAKE_META",
343
+ {
344
+ ...handshakingSession.loggingMetadata,
345
+ connectedTo: from,
346
+ validationErrors: [
347
+ ...Value.Errors(this.handshakeExtensions.schema, rawMetadata)
348
+ ]
349
+ }
364
350
  );
365
- if (parsedMetadata === false) {
366
- this.rejectHandshakeRequest(
367
- handshakingSession,
368
- from,
369
- "rejected by handshake handler",
370
- "REJECTED_BY_CUSTOM_HANDLER",
371
- {
372
- ...handshakingSession.loggingMetadata,
373
- connectedTo: from,
374
- clientId: this.clientId
375
- }
376
- );
377
- return false;
378
- }
351
+ return false;
352
+ }
353
+ const previousParsedMetadata = existingSession ? this.sessionHandshakeMetadata.get(existingSession.to) : void 0;
354
+ const parsedMetadata = await this.handshakeExtensions.validate(
355
+ rawMetadata,
356
+ previousParsedMetadata
357
+ );
358
+ if (Value.Check(HandshakeErrorCustomHandlerFatalResponseCodes, parsedMetadata)) {
359
+ this.rejectHandshakeRequest(
360
+ handshakingSession,
361
+ from,
362
+ "rejected by handshake handler",
363
+ parsedMetadata,
364
+ {
365
+ ...handshakingSession.loggingMetadata,
366
+ connectedTo: from,
367
+ clientId: this.clientId
368
+ }
369
+ );
370
+ return false;
379
371
  }
380
372
  return parsedMetadata;
381
373
  }
@@ -384,4 +376,4 @@ var ServerTransport = class extends Transport {
384
376
  export {
385
377
  ServerTransport
386
378
  };
387
- //# sourceMappingURL=chunk-PCBPPTXH.js.map
379
+ //# sourceMappingURL=chunk-7ETNUCOL.js.map