@replit/river 0.200.4 → 0.201.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 (60) hide show
  1. package/dist/{chunk-BBTXTDJ5.js → chunk-52DVJUVZ.js} +2 -2
  2. package/dist/{chunk-Q7DD2FLX.js → chunk-7IUEEQP7.js} +10 -10
  3. package/dist/chunk-7IUEEQP7.js.map +1 -0
  4. package/dist/{chunk-GKBNAMHM.js → chunk-OZ3ITXW5.js} +5 -7
  5. package/dist/chunk-OZ3ITXW5.js.map +1 -0
  6. package/dist/{chunk-LL4CB5UD.js → chunk-PJ4GJ5CU.js} +332 -3
  7. package/dist/chunk-PJ4GJ5CU.js.map +1 -0
  8. package/dist/{chunk-6EORIAZJ.js → chunk-UBUD2LMZ.js} +4 -6
  9. package/dist/chunk-UBUD2LMZ.js.map +1 -0
  10. package/dist/{client-ba6815ae.d.ts → client-a32692b0.d.ts} +2 -2
  11. package/dist/{connection-d217c989.d.ts → connection-48d021ca.d.ts} +2 -2
  12. package/dist/{context-8d1ed9a1.d.ts → context-8d263a7f.d.ts} +2 -2
  13. package/dist/logging/index.d.cts +1 -1
  14. package/dist/logging/index.d.ts +1 -1
  15. package/dist/{message-45658364.d.ts → message-3def9ded.d.ts} +1 -1
  16. package/dist/router/index.cjs +7 -8
  17. package/dist/router/index.cjs.map +1 -1
  18. package/dist/router/index.d.cts +8 -8
  19. package/dist/router/index.d.ts +8 -8
  20. package/dist/router/index.js +323 -46
  21. package/dist/router/index.js.map +1 -1
  22. package/dist/{server-eb7d2cfd.d.ts → server-48d90a77.d.ts} +2 -2
  23. package/dist/{services-94e0afc3.d.ts → services-e71ea921.d.ts} +4 -4
  24. package/dist/testUtil/index.cjs +2616 -0
  25. package/dist/testUtil/index.cjs.map +1 -0
  26. package/dist/{util/testHelpers.d.cts → testUtil/index.d.cts} +27 -18
  27. package/dist/{util/testHelpers.d.ts → testUtil/index.d.ts} +27 -18
  28. package/dist/testUtil/index.js +370 -0
  29. package/dist/testUtil/index.js.map +1 -0
  30. package/dist/transport/impls/ws/client.cjs +2 -2
  31. package/dist/transport/impls/ws/client.cjs.map +1 -1
  32. package/dist/transport/impls/ws/client.d.cts +4 -4
  33. package/dist/transport/impls/ws/client.d.ts +4 -4
  34. package/dist/transport/impls/ws/client.js +4 -5
  35. package/dist/transport/impls/ws/client.js.map +1 -1
  36. package/dist/transport/impls/ws/server.cjs +2 -2
  37. package/dist/transport/impls/ws/server.cjs.map +1 -1
  38. package/dist/transport/impls/ws/server.d.cts +4 -4
  39. package/dist/transport/impls/ws/server.d.ts +4 -4
  40. package/dist/transport/impls/ws/server.js +4 -5
  41. package/dist/transport/impls/ws/server.js.map +1 -1
  42. package/dist/transport/index.cjs +2 -2
  43. package/dist/transport/index.cjs.map +1 -1
  44. package/dist/transport/index.d.cts +4 -4
  45. package/dist/transport/index.d.ts +4 -4
  46. package/dist/transport/index.js +6 -8
  47. package/package.json +3 -3
  48. package/dist/chunk-6EORIAZJ.js.map +0 -1
  49. package/dist/chunk-GKBNAMHM.js.map +0 -1
  50. package/dist/chunk-LL4CB5UD.js.map +0 -1
  51. package/dist/chunk-Q7DD2FLX.js.map +0 -1
  52. package/dist/chunk-RWVZCH26.js +0 -340
  53. package/dist/chunk-RWVZCH26.js.map +0 -1
  54. package/dist/chunk-ZVWJN6V2.js +0 -307
  55. package/dist/chunk-ZVWJN6V2.js.map +0 -1
  56. package/dist/util/testHelpers.cjs +0 -1563
  57. package/dist/util/testHelpers.cjs.map +0 -1
  58. package/dist/util/testHelpers.js +0 -250
  59. package/dist/util/testHelpers.js.map +0 -1
  60. /package/dist/{chunk-BBTXTDJ5.js.map → chunk-52DVJUVZ.js.map} +0 -0
@@ -1,340 +0,0 @@
1
- import {
2
- BaseLogger,
3
- createLogProxy
4
- } from "./chunk-LHH5LQ7C.js";
5
- import {
6
- SessionStateGraph,
7
- defaultTransportOptions
8
- } from "./chunk-LL4CB5UD.js";
9
- import {
10
- generateId
11
- } from "./chunk-Q7DD2FLX.js";
12
-
13
- // transport/events.ts
14
- var ProtocolError = {
15
- RetriesExceeded: "conn_retry_exceeded",
16
- HandshakeFailed: "handshake_failed",
17
- MessageOrderingViolated: "message_ordering_violated",
18
- InvalidMessage: "invalid_message"
19
- };
20
- var EventDispatcher = class {
21
- eventListeners = {};
22
- removeAllListeners() {
23
- this.eventListeners = {};
24
- }
25
- numberOfListeners(eventType) {
26
- return this.eventListeners[eventType]?.size ?? 0;
27
- }
28
- addEventListener(eventType, handler) {
29
- if (!this.eventListeners[eventType]) {
30
- this.eventListeners[eventType] = /* @__PURE__ */ new Set();
31
- }
32
- this.eventListeners[eventType]?.add(handler);
33
- }
34
- removeEventListener(eventType, handler) {
35
- const handlers = this.eventListeners[eventType];
36
- if (handlers) {
37
- this.eventListeners[eventType]?.delete(handler);
38
- }
39
- }
40
- dispatchEvent(eventType, event) {
41
- const handlers = this.eventListeners[eventType];
42
- if (handlers) {
43
- const copy = [...handlers];
44
- for (const handler of copy) {
45
- handler(event);
46
- }
47
- }
48
- }
49
- };
50
-
51
- // transport/transport.ts
52
- var Transport = class {
53
- /**
54
- * The status of the transport.
55
- */
56
- status;
57
- /**
58
- * The client ID of this transport.
59
- */
60
- clientId;
61
- /**
62
- * The event dispatcher for handling events of type EventTypes.
63
- */
64
- eventDispatcher;
65
- /**
66
- * The options for this transport.
67
- */
68
- options;
69
- log;
70
- sessions;
71
- /**
72
- * Creates a new Transport instance.
73
- * @param codec The codec used to encode and decode messages.
74
- * @param clientId The client ID of this transport.
75
- */
76
- constructor(clientId, providedOptions) {
77
- this.options = { ...defaultTransportOptions, ...providedOptions };
78
- this.eventDispatcher = new EventDispatcher();
79
- this.clientId = clientId;
80
- this.status = "open";
81
- this.sessions = /* @__PURE__ */ new Map();
82
- }
83
- bindLogger(fn, level) {
84
- if (typeof fn === "function") {
85
- this.log = createLogProxy(new BaseLogger(fn, level));
86
- return;
87
- }
88
- this.log = createLogProxy(fn);
89
- }
90
- /**
91
- * Called when a message is received by this transport.
92
- * You generally shouldn't need to override this in downstream transport implementations.
93
- * @param message The received message.
94
- */
95
- handleMsg(message) {
96
- if (this.getStatus() !== "open")
97
- return;
98
- this.eventDispatcher.dispatchEvent("message", message);
99
- }
100
- /**
101
- * Adds a listener to this transport.
102
- * @param the type of event to listen for
103
- * @param handler The message handler to add.
104
- */
105
- addEventListener(type, handler) {
106
- this.eventDispatcher.addEventListener(type, handler);
107
- }
108
- /**
109
- * Removes a listener from this transport.
110
- * @param the type of event to un-listen on
111
- * @param handler The message handler to remove.
112
- */
113
- removeEventListener(type, handler) {
114
- this.eventDispatcher.removeEventListener(type, handler);
115
- }
116
- protocolError(message) {
117
- this.eventDispatcher.dispatchEvent("protocolError", message);
118
- }
119
- /**
120
- * Default close implementation for transports. You should override this in the downstream
121
- * implementation if you need to do any additional cleanup and call super.close() at the end.
122
- * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
123
- */
124
- close() {
125
- this.status = "closed";
126
- for (const session of this.sessions.values()) {
127
- this.deleteSession(session);
128
- }
129
- this.eventDispatcher.dispatchEvent("transportStatus", {
130
- status: this.status
131
- });
132
- this.eventDispatcher.removeAllListeners();
133
- this.log?.info(`manually closed transport`, { clientId: this.clientId });
134
- }
135
- getStatus() {
136
- return this.status;
137
- }
138
- // state transitions
139
- createSession(session) {
140
- const activeSession = this.sessions.get(session.to);
141
- if (activeSession) {
142
- const msg = `attempt to create session for ${session.to} but active session (${activeSession.id}) already exists`;
143
- this.log?.error(msg, {
144
- ...session.loggingMetadata,
145
- tags: ["invariant-violation"]
146
- });
147
- throw new Error(msg);
148
- }
149
- this.sessions.set(session.to, session);
150
- this.eventDispatcher.dispatchEvent("sessionStatus", {
151
- status: "connect",
152
- session
153
- });
154
- this.eventDispatcher.dispatchEvent("sessionTransition", {
155
- state: session.state,
156
- session
157
- });
158
- }
159
- updateSession(session) {
160
- const activeSession = this.sessions.get(session.to);
161
- if (!activeSession) {
162
- const msg = `attempt to transition session for ${session.to} but no active session exists`;
163
- this.log?.error(msg, {
164
- ...session.loggingMetadata,
165
- tags: ["invariant-violation"]
166
- });
167
- throw new Error(msg);
168
- }
169
- if (activeSession.id !== session.id) {
170
- const msg = `attempt to transition active session for ${session.to} but active session (${activeSession.id}) is different from handle (${session.id})`;
171
- this.log?.error(msg, {
172
- ...session.loggingMetadata,
173
- tags: ["invariant-violation"]
174
- });
175
- throw new Error(msg);
176
- }
177
- this.sessions.set(session.to, session);
178
- this.eventDispatcher.dispatchEvent("sessionTransition", {
179
- state: session.state,
180
- session
181
- });
182
- }
183
- deleteSession(session, options) {
184
- if (session._isConsumed)
185
- return;
186
- const loggingMetadata = session.loggingMetadata;
187
- if (loggingMetadata.tags && options?.unhealthy) {
188
- loggingMetadata.tags.push("unhealthy-session");
189
- }
190
- session.log?.info(`closing session ${session.id}`, loggingMetadata);
191
- this.eventDispatcher.dispatchEvent("sessionStatus", {
192
- status: "disconnect",
193
- session
194
- });
195
- const to = session.to;
196
- session.close();
197
- this.sessions.delete(to);
198
- }
199
- // common listeners
200
- onSessionGracePeriodElapsed(session) {
201
- this.log?.warn(
202
- `session to ${session.to} grace period elapsed, closing`,
203
- session.loggingMetadata
204
- );
205
- this.deleteSession(session);
206
- }
207
- onConnectingFailed(session) {
208
- const noConnectionSession = SessionStateGraph.transition.ConnectingToNoConnection(session, {
209
- onSessionGracePeriodElapsed: () => {
210
- this.onSessionGracePeriodElapsed(noConnectionSession);
211
- }
212
- });
213
- this.updateSession(noConnectionSession);
214
- return noConnectionSession;
215
- }
216
- onConnClosed(session) {
217
- let noConnectionSession;
218
- if (session.state === "Handshaking" /* Handshaking */) {
219
- noConnectionSession = SessionStateGraph.transition.HandshakingToNoConnection(session, {
220
- onSessionGracePeriodElapsed: () => {
221
- this.onSessionGracePeriodElapsed(noConnectionSession);
222
- }
223
- });
224
- } else {
225
- noConnectionSession = SessionStateGraph.transition.ConnectedToNoConnection(session, {
226
- onSessionGracePeriodElapsed: () => {
227
- this.onSessionGracePeriodElapsed(noConnectionSession);
228
- }
229
- });
230
- }
231
- this.updateSession(noConnectionSession);
232
- return noConnectionSession;
233
- }
234
- /**
235
- * Gets a send closure scoped to a specific session. Sending using the returned
236
- * closure after the session has transitioned to a different state will be a noop.
237
- *
238
- * Session objects themselves can become stale as they transition between
239
- * states. As stale sessions cannot be used again (and will throw), holding
240
- * onto a session object is not recommended.
241
- */
242
- getSessionBoundSendFn(to, sessionId) {
243
- if (this.getStatus() !== "open") {
244
- throw new Error("cannot get a bound send function on a closed transport");
245
- }
246
- return (msg) => {
247
- const session = this.sessions.get(to);
248
- if (!session) {
249
- throw new Error(
250
- `session scope for ${sessionId} has ended (close), can't send`
251
- );
252
- }
253
- const sameSession = session.id === sessionId;
254
- if (!sameSession) {
255
- throw new Error(
256
- `session scope for ${sessionId} has ended (transition), can't send`
257
- );
258
- }
259
- return session.send(msg);
260
- };
261
- }
262
- };
263
-
264
- // transport/connection.ts
265
- var Connection = class {
266
- id;
267
- telemetry;
268
- constructor() {
269
- this.id = `conn-${generateId()}`;
270
- }
271
- get loggingMetadata() {
272
- const metadata = { connId: this.id };
273
- const spanContext = this.telemetry?.span.spanContext();
274
- if (this.telemetry?.span.isRecording() && spanContext) {
275
- metadata.telemetry = {
276
- traceId: spanContext.traceId,
277
- spanId: spanContext.spanId
278
- };
279
- }
280
- return metadata;
281
- }
282
- // can't use event emitter because we need this to work in both node + browser
283
- _dataListeners = /* @__PURE__ */ new Set();
284
- _closeListeners = /* @__PURE__ */ new Set();
285
- _errorListeners = /* @__PURE__ */ new Set();
286
- get dataListeners() {
287
- return [...this._dataListeners];
288
- }
289
- get closeListeners() {
290
- return [...this._closeListeners];
291
- }
292
- get errorListeners() {
293
- return [...this._errorListeners];
294
- }
295
- /**
296
- * Handle adding a callback for when a message is received.
297
- * @param msg The message that was received.
298
- */
299
- addDataListener(cb) {
300
- this._dataListeners.add(cb);
301
- }
302
- removeDataListener(cb) {
303
- this._dataListeners.delete(cb);
304
- }
305
- /**
306
- * Handle adding a callback for when the connection is closed.
307
- * This should also be called if an error happens and after notifying all the error listeners.
308
- * @param cb The callback to call when the connection is closed.
309
- */
310
- addCloseListener(cb) {
311
- this._closeListeners.add(cb);
312
- }
313
- removeCloseListener(cb) {
314
- this._closeListeners.delete(cb);
315
- }
316
- /**
317
- * Handle adding a callback for when an error is received.
318
- * This should only be used for this.logging errors, all cleanup
319
- * should be delegated to addCloseListener.
320
- *
321
- * The implementer should take care such that the implemented
322
- * connection will call both the close and error callbacks
323
- * on an error.
324
- *
325
- * @param cb The callback to call when an error is received.
326
- */
327
- addErrorListener(cb) {
328
- this._errorListeners.add(cb);
329
- }
330
- removeErrorListener(cb) {
331
- this._errorListeners.delete(cb);
332
- }
333
- };
334
-
335
- export {
336
- ProtocolError,
337
- Transport,
338
- Connection
339
- };
340
- //# sourceMappingURL=chunk-RWVZCH26.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../transport/events.ts","../transport/transport.ts","../transport/connection.ts"],"sourcesContent":["import { type Static } from '@sinclair/typebox';\nimport { Connection } from './connection';\nimport { OpaqueTransportMessage, HandshakeErrorResponseCodes } 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 InvalidMessage: 'invalid_message',\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 | {\n type: (typeof ProtocolError)['HandshakeFailed'];\n code: Static<typeof HandshakeErrorResponseCodes>;\n message: string;\n }\n | {\n type: Omit<\n ProtocolErrorType,\n (typeof ProtocolError)['HandshakeFailed']\n >;\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 PartialTransportMessage,\n TransportClientId,\n} from './message';\nimport {\n BaseLogger,\n LogFn,\n Logger,\n LoggingLevel,\n createLogProxy,\n} from '../logging/log';\nimport { EventDispatcher, EventHandler, EventMap, EventTypes } 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';\nimport { SessionId } from './sessionStateMachine/common';\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\nexport interface DeleteSessionOptions {\n unhealthy: boolean;\n}\n\nexport type SessionBoundSendFn = (\n msg: PartialTransportMessage,\n) => string | undefined;\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\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 message The received message.\n */\n protected handleMsg(message: OpaqueTransportMessage) {\n if (this.getStatus() !== 'open') return;\n this.eventDispatcher.dispatchEvent('message', message);\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 protected protocolError(message: EventMap['protocolError']) {\n this.eventDispatcher.dispatchEvent('protocolError', 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 this.log?.info(`manually closed transport`, { clientId: this.clientId });\n }\n\n getStatus(): TransportStatus {\n return this.status;\n }\n\n // state transitions\n protected createSession<S extends Session<ConnType>>(session: S): void {\n const activeSession = this.sessions.get(session.to);\n if (activeSession) {\n const msg = `attempt to create session for ${session.to} but active session (${activeSession.id}) already exists`;\n this.log?.error(msg, {\n ...session.loggingMetadata,\n tags: ['invariant-violation'],\n });\n throw new Error(msg);\n }\n\n this.sessions.set(session.to, session);\n this.eventDispatcher.dispatchEvent('sessionStatus', {\n status: 'connect',\n session: session,\n });\n\n this.eventDispatcher.dispatchEvent('sessionTransition', {\n state: session.state,\n session: session,\n } as EventMap['sessionTransition']);\n }\n\n protected updateSession<S extends Session<ConnType>>(session: S): void {\n const activeSession = this.sessions.get(session.to);\n if (!activeSession) {\n const msg = `attempt to transition session for ${session.to} but no active session exists`;\n this.log?.error(msg, {\n ...session.loggingMetadata,\n tags: ['invariant-violation'],\n });\n throw new Error(msg);\n }\n\n if (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 this.log?.error(msg, {\n ...session.loggingMetadata,\n tags: ['invariant-violation'],\n });\n throw new Error(msg);\n }\n\n this.sessions.set(session.to, session);\n this.eventDispatcher.dispatchEvent('sessionTransition', {\n state: session.state,\n session: session,\n } as EventMap['sessionTransition']);\n }\n\n protected deleteSession(\n session: Session<ConnType>,\n options?: DeleteSessionOptions,\n ) {\n // ensure idempotency esp re: dispatching events\n if (session._isConsumed) return;\n\n const loggingMetadata = session.loggingMetadata;\n if (loggingMetadata.tags && options?.unhealthy) {\n loggingMetadata.tags.push('unhealthy-session');\n }\n\n session.log?.info(`closing session ${session.id}`, loggingMetadata);\n this.eventDispatcher.dispatchEvent('sessionStatus', {\n status: 'disconnect',\n session: session,\n });\n\n const to = session.to;\n session.close();\n this.sessions.delete(to);\n }\n\n // common listeners\n protected onSessionGracePeriodElapsed(session: Session<ConnType>) {\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 this.updateSession(noConnectionSession);\n\n return 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 this.updateSession(noConnectionSession);\n\n return noConnectionSession;\n }\n\n /**\n * Gets a send closure scoped to a specific session. Sending using the returned\n * closure after the session has transitioned to a different state will be a noop.\n *\n * Session objects themselves can become stale as they transition between\n * states. As stale sessions cannot be used again (and will throw), holding\n * onto a session object is not recommended.\n */\n getSessionBoundSendFn(\n to: TransportClientId,\n sessionId: SessionId,\n ): SessionBoundSendFn {\n if (this.getStatus() !== 'open') {\n throw new Error('cannot get a bound send function on a closed transport');\n }\n\n return (msg: PartialTransportMessage) => {\n const session = this.sessions.get(to);\n if (!session) {\n throw new Error(\n `session scope for ${sessionId} has ended (close), can't send`,\n );\n }\n\n const sameSession = session.id === sessionId;\n if (!sameSession) {\n throw new Error(\n `session scope for ${sessionId} has ended (transition), can't send`,\n );\n }\n\n return session.send(msg);\n };\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\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":";;;;;;;;;;;;;AAMO,IAAM,gBAAgB;AAAA,EAC3B,iBAAiB;AAAA,EACjB,iBAAiB;AAAA,EACjB,yBAAyB;AAAA,EACzB,gBAAgB;AAClB;AAwCO,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,EAC7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ACrD,eAAS,YAAY;AAAA,QACnB,SAAS,YAAY;AAAA,QACrB,QAAQ,YAAY;AAAA,MACtB;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA;AAAA,EAGQ,iBAAiB,oBAAI,IAA+B;AAAA,EACpD,kBAAkB,oBAAI,IAAgB;AAAA,EACtC,kBAAkB,oBAAI,IAA0B;AAAA,EAExD,IAAI,gBAAgB;AAClB,WAAO,CAAC,GAAG,KAAK,cAAc;AAAA,EAChC;AAAA,EAEA,IAAI,iBAAiB;AACnB,WAAO,CAAC,GAAG,KAAK,eAAe;AAAA,EACjC;AAAA,EAEA,IAAI,iBAAiB;AACnB,WAAO,CAAC,GAAG,KAAK,eAAe;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,gBAAgB,IAA+B;AAC7C,SAAK,eAAe,IAAI,EAAE;AAAA,EAC5B;AAAA,EAEA,mBAAmB,IAAqC;AACtD,SAAK,eAAe,OAAO,EAAE;AAAA,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,307 +0,0 @@
1
- // router/errors.ts
2
- import {
3
- Kind,
4
- Type
5
- } from "@sinclair/typebox";
6
- var UNCAUGHT_ERROR_CODE = "UNCAUGHT_ERROR";
7
- var UNEXPECTED_DISCONNECT_CODE = "UNEXPECTED_DISCONNECT";
8
- var INVALID_REQUEST_CODE = "INVALID_REQUEST";
9
- var CANCEL_CODE = "CANCEL";
10
- var ErrResultSchema = (t) => Type.Object({
11
- ok: Type.Literal(false),
12
- payload: t
13
- });
14
- var ReaderErrorSchema = Type.Union([
15
- Type.Object({
16
- code: Type.Literal(UNCAUGHT_ERROR_CODE),
17
- message: Type.String()
18
- }),
19
- Type.Object({
20
- code: Type.Literal(UNEXPECTED_DISCONNECT_CODE),
21
- message: Type.String()
22
- }),
23
- Type.Object({
24
- code: Type.Literal(INVALID_REQUEST_CODE),
25
- message: Type.String()
26
- }),
27
- Type.Object({
28
- code: Type.Literal(CANCEL_CODE),
29
- message: Type.String()
30
- })
31
- ]);
32
- function isUnion(schema) {
33
- return schema[Kind] === "Union";
34
- }
35
- function flattenErrorType(errType) {
36
- if (!isUnion(errType)) {
37
- return errType;
38
- }
39
- const flattenedTypes = [];
40
- function flatten(type) {
41
- if (isUnion(type)) {
42
- for (const t of type.anyOf) {
43
- flatten(t);
44
- }
45
- } else {
46
- flattenedTypes.push(type);
47
- }
48
- }
49
- flatten(errType);
50
- return Type.Union(flattenedTypes);
51
- }
52
-
53
- // router/result.ts
54
- import { Type as Type2 } from "@sinclair/typebox";
55
- var AnyResultSchema = Type2.Union([
56
- Type2.Object({
57
- ok: Type2.Literal(false),
58
- payload: Type2.Object({
59
- code: Type2.String(),
60
- message: Type2.String(),
61
- extras: Type2.Optional(Type2.Unknown())
62
- })
63
- }),
64
- Type2.Object({
65
- ok: Type2.Literal(true),
66
- payload: Type2.Unknown()
67
- })
68
- ]);
69
- function Ok(payload) {
70
- return {
71
- ok: true,
72
- payload
73
- };
74
- }
75
- function Err(error) {
76
- return {
77
- ok: false,
78
- payload: error
79
- };
80
- }
81
- function unwrapOrThrow(result) {
82
- if (result.ok) {
83
- return result.payload;
84
- }
85
- throw new Error(
86
- `Cannot non-ok result, got: ${result.payload.code} - ${result.payload.message}`
87
- );
88
- }
89
-
90
- // router/streams.ts
91
- var ReadableBrokenError = {
92
- code: "READABLE_BROKEN",
93
- message: "Readable was broken before it is fully consumed"
94
- };
95
- function createPromiseWithResolvers() {
96
- let resolve;
97
- let reject;
98
- const promise = new Promise((res, rej) => {
99
- resolve = res;
100
- reject = rej;
101
- });
102
- return {
103
- promise,
104
- // @ts-expect-error promise callbacks are sync
105
- resolve,
106
- // @ts-expect-error promise callbacks are sync
107
- reject
108
- };
109
- }
110
- var ReadableImpl = class {
111
- /**
112
- * Whether the {@link Readable} is closed.
113
- *
114
- * Closed {@link Readable}s are done receiving values, but that doesn't affect
115
- * any other aspect of the {@link Readable} such as it's consumability.
116
- */
117
- closed = false;
118
- /**
119
- * Whether the {@link Readable} is locked.
120
- *
121
- * @see {@link Readable}'s typedoc to understand locking
122
- */
123
- locked = false;
124
- /**
125
- * Whether {@link break} was called.
126
- *
127
- * @see {@link break} for more information
128
- */
129
- broken = false;
130
- /**
131
- * This flag allows us to avoid emitting a {@link ReadableBrokenError} after {@link break} was called
132
- * in cases where the {@link queue} is fully consumed and {@link ReadableImpl} is {@link closed}. This is just an
133
- * ergonomic feature to avoid emitting an error in our iteration when we don't have to.
134
- */
135
- brokenWithValuesLeftToRead = false;
136
- /**
137
- * A list of values that have been pushed to the {@link ReadableImpl} but not yet emitted to the user.
138
- */
139
- queue = [];
140
- /**
141
- * Used by methods in the class to signal to the iterator that it
142
- * should check for the next value.
143
- */
144
- next = null;
145
- [Symbol.asyncIterator]() {
146
- if (this.locked) {
147
- throw new TypeError("Readable is already locked");
148
- }
149
- this.locked = true;
150
- let didSignalBreak = false;
151
- return {
152
- next: async () => {
153
- if (didSignalBreak) {
154
- return {
155
- done: true,
156
- value: void 0
157
- };
158
- }
159
- while (this.queue.length === 0) {
160
- if (this.closed && !this.brokenWithValuesLeftToRead) {
161
- return {
162
- done: true,
163
- value: void 0
164
- };
165
- }
166
- if (this.broken) {
167
- didSignalBreak = true;
168
- return {
169
- done: false,
170
- value: Err(ReadableBrokenError)
171
- };
172
- }
173
- if (!this.next) {
174
- this.next = createPromiseWithResolvers();
175
- }
176
- await this.next.promise;
177
- this.next = null;
178
- }
179
- const value = this.queue.shift();
180
- return { done: false, value };
181
- },
182
- return: () => {
183
- this.break();
184
- return { done: true, value: void 0 };
185
- }
186
- };
187
- }
188
- async collect() {
189
- const array = [];
190
- for await (const value of this) {
191
- array.push(value);
192
- }
193
- return array;
194
- }
195
- break() {
196
- if (this.broken) {
197
- return;
198
- }
199
- this.locked = true;
200
- this.broken = true;
201
- this.brokenWithValuesLeftToRead = this.queue.length > 0;
202
- this.queue.length = 0;
203
- this.next?.resolve();
204
- }
205
- isReadable() {
206
- return !this.locked && !this.broken;
207
- }
208
- /**
209
- * @internal meant for use within river, not exposed as a public API
210
- *
211
- * Pushes a value to be read.
212
- */
213
- _pushValue(value) {
214
- if (this.broken) {
215
- return;
216
- }
217
- if (this.closed) {
218
- throw new Error("Cannot push to closed Readable");
219
- }
220
- this.queue.push(value);
221
- this.next?.resolve();
222
- }
223
- /**
224
- * @internal meant for use within river, not exposed as a public API
225
- *
226
- * Triggers the close of the {@link Readable}. Make sure to push all remaining
227
- * values before calling this method.
228
- */
229
- _triggerClose() {
230
- if (this.closed) {
231
- throw new Error("Unexpected closing multiple times");
232
- }
233
- this.closed = true;
234
- this.next?.resolve();
235
- }
236
- /**
237
- * @internal meant for use within river, not exposed as a public API
238
- */
239
- _hasValuesInQueue() {
240
- return this.queue.length > 0;
241
- }
242
- /**
243
- * @internal meant for use within river, not exposed as a public API
244
- */
245
- isClosed() {
246
- return this.closed;
247
- }
248
- };
249
- var WritableImpl = class {
250
- /**
251
- * Passed via constructor to pass on calls to {@link write}
252
- */
253
- writeCb;
254
- /**
255
- * Passed via constructor to pass on calls to {@link close}
256
- */
257
- closeCb;
258
- /**
259
- * Whether {@link close} was called, and {@link Writable} is not writable anymore.
260
- */
261
- closed = false;
262
- constructor(callbacks) {
263
- this.writeCb = callbacks.writeCb;
264
- this.closeCb = callbacks.closeCb;
265
- }
266
- write(value) {
267
- if (this.closed) {
268
- throw new Error("Cannot write to closed Writable");
269
- }
270
- this.writeCb(value);
271
- }
272
- isWritable() {
273
- return !this.closed;
274
- }
275
- close() {
276
- if (this.closed) {
277
- return;
278
- }
279
- this.closed = true;
280
- this.writeCb = () => void 0;
281
- this.closeCb();
282
- this.closeCb = () => void 0;
283
- }
284
- /**
285
- * @internal meant for use within river, not exposed as a public API
286
- */
287
- isClosed() {
288
- return this.closed;
289
- }
290
- };
291
-
292
- export {
293
- UNCAUGHT_ERROR_CODE,
294
- UNEXPECTED_DISCONNECT_CODE,
295
- INVALID_REQUEST_CODE,
296
- CANCEL_CODE,
297
- ErrResultSchema,
298
- ReaderErrorSchema,
299
- flattenErrorType,
300
- AnyResultSchema,
301
- Ok,
302
- Err,
303
- unwrapOrThrow,
304
- ReadableImpl,
305
- WritableImpl
306
- };
307
- //# sourceMappingURL=chunk-ZVWJN6V2.js.map