@replit/river 0.200.0-rc.2 → 0.200.0-rc.4

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 (84) hide show
  1. package/README.md +21 -20
  2. package/dist/chunk-2BF4VMUZ.js +50 -0
  3. package/dist/chunk-2BF4VMUZ.js.map +1 -0
  4. package/dist/chunk-BZQQXMVF.js +401 -0
  5. package/dist/chunk-BZQQXMVF.js.map +1 -0
  6. package/dist/{chunk-4VNY34QG.js → chunk-F7Z2QQRL.js} +24 -18
  7. package/dist/chunk-F7Z2QQRL.js.map +1 -0
  8. package/dist/chunk-IMFMNIEO.js +384 -0
  9. package/dist/chunk-IMFMNIEO.js.map +1 -0
  10. package/dist/chunk-RDGHFHXN.js +658 -0
  11. package/dist/chunk-RDGHFHXN.js.map +1 -0
  12. package/dist/chunk-SI4YHBTI.js +277 -0
  13. package/dist/chunk-SI4YHBTI.js.map +1 -0
  14. package/dist/{chunk-7CKIN3JT.js → chunk-SIRRYRLQ.js} +73 -490
  15. package/dist/chunk-SIRRYRLQ.js.map +1 -0
  16. package/dist/{chunk-S5RL45KH.js → chunk-V57VWV5S.js} +80 -44
  17. package/dist/chunk-V57VWV5S.js.map +1 -0
  18. package/dist/{chunk-QMM35C3H.js → chunk-VXYHC666.js} +1 -1
  19. package/dist/chunk-VXYHC666.js.map +1 -0
  20. package/dist/client-f56a6da3.d.ts +49 -0
  21. package/dist/{connection-f900e390.d.ts → connection-11991b13.d.ts} +1 -5
  22. package/dist/connection-6031a354.d.ts +11 -0
  23. package/dist/context-73df8978.d.ts +528 -0
  24. package/dist/logging/index.cjs.map +1 -1
  25. package/dist/logging/index.d.cts +1 -1
  26. package/dist/logging/index.d.ts +1 -1
  27. package/dist/logging/index.js +1 -1
  28. package/dist/{index-10ebd26a.d.ts → message-fd349b27.d.ts} +31 -31
  29. package/dist/router/index.cjs +125 -502
  30. package/dist/router/index.cjs.map +1 -1
  31. package/dist/router/index.d.cts +11 -46
  32. package/dist/router/index.d.ts +11 -46
  33. package/dist/router/index.js +2 -4
  34. package/dist/server-9f31d98f.d.ts +42 -0
  35. package/dist/{services-970f97bb.d.ts → services-c67758fc.d.ts} +5 -602
  36. package/dist/transport/impls/uds/client.cjs +1247 -1238
  37. package/dist/transport/impls/uds/client.cjs.map +1 -1
  38. package/dist/transport/impls/uds/client.d.cts +4 -3
  39. package/dist/transport/impls/uds/client.d.ts +4 -3
  40. package/dist/transport/impls/uds/client.js +7 -13
  41. package/dist/transport/impls/uds/client.js.map +1 -1
  42. package/dist/transport/impls/uds/server.cjs +1311 -1170
  43. package/dist/transport/impls/uds/server.cjs.map +1 -1
  44. package/dist/transport/impls/uds/server.d.cts +4 -4
  45. package/dist/transport/impls/uds/server.d.ts +4 -4
  46. package/dist/transport/impls/uds/server.js +6 -6
  47. package/dist/transport/impls/ws/client.cjs +988 -987
  48. package/dist/transport/impls/ws/client.cjs.map +1 -1
  49. package/dist/transport/impls/ws/client.d.cts +6 -5
  50. package/dist/transport/impls/ws/client.d.ts +6 -5
  51. package/dist/transport/impls/ws/client.js +6 -7
  52. package/dist/transport/impls/ws/client.js.map +1 -1
  53. package/dist/transport/impls/ws/server.cjs +1192 -1066
  54. package/dist/transport/impls/ws/server.cjs.map +1 -1
  55. package/dist/transport/impls/ws/server.d.cts +4 -4
  56. package/dist/transport/impls/ws/server.d.ts +4 -4
  57. package/dist/transport/impls/ws/server.js +6 -6
  58. package/dist/transport/index.cjs +1446 -1380
  59. package/dist/transport/index.cjs.map +1 -1
  60. package/dist/transport/index.d.cts +4 -26
  61. package/dist/transport/index.d.ts +4 -26
  62. package/dist/transport/index.js +9 -9
  63. package/dist/util/testHelpers.cjs +746 -303
  64. package/dist/util/testHelpers.cjs.map +1 -1
  65. package/dist/util/testHelpers.d.cts +9 -4
  66. package/dist/util/testHelpers.d.ts +9 -4
  67. package/dist/util/testHelpers.js +36 -10
  68. package/dist/util/testHelpers.js.map +1 -1
  69. package/package.json +1 -1
  70. package/dist/chunk-47TFNAY2.js +0 -476
  71. package/dist/chunk-47TFNAY2.js.map +0 -1
  72. package/dist/chunk-4VNY34QG.js.map +0 -1
  73. package/dist/chunk-7CKIN3JT.js.map +0 -1
  74. package/dist/chunk-CZP4LK3F.js +0 -335
  75. package/dist/chunk-CZP4LK3F.js.map +0 -1
  76. package/dist/chunk-DJCW3SKT.js +0 -59
  77. package/dist/chunk-DJCW3SKT.js.map +0 -1
  78. package/dist/chunk-NQWDT6GS.js +0 -347
  79. package/dist/chunk-NQWDT6GS.js.map +0 -1
  80. package/dist/chunk-ONUXWVRC.js +0 -492
  81. package/dist/chunk-ONUXWVRC.js.map +0 -1
  82. package/dist/chunk-QMM35C3H.js.map +0 -1
  83. package/dist/chunk-S5RL45KH.js.map +0 -1
  84. package/dist/connection-3f117047.d.ts +0 -17
@@ -0,0 +1,49 @@
1
+ import { C as Connection, T as Transport, o as ClientTransportOptions, L as LeakyBucketRateLimit, p as ClientHandshakeOptions, b as ProvidedClientTransportOptions, f as SessionConnecting, e as SessionNoConnection, g as SessionHandshaking, h as SessionConnected } from './context-73df8978.js';
2
+ import { c as TransportClientId, P as PartialTransportMessage, b as OpaqueTransportMessage } from './message-fd349b27.js';
3
+
4
+ declare abstract class ClientTransport<ConnType extends Connection> extends Transport<ConnType> {
5
+ /**
6
+ * The options for this transport.
7
+ */
8
+ protected options: ClientTransportOptions;
9
+ retryBudget: LeakyBucketRateLimit;
10
+ /**
11
+ * A flag indicating whether the transport should automatically reconnect
12
+ * when a connection is dropped.
13
+ * Realistically, this should always be true for clients unless you are writing
14
+ * tests or a special case where you don't want to reconnect.
15
+ */
16
+ reconnectOnConnectionDrop: boolean;
17
+ /**
18
+ * Optional handshake options for this client.
19
+ */
20
+ handshakeExtensions?: ClientHandshakeOptions;
21
+ constructor(clientId: TransportClientId, providedOptions?: ProvidedClientTransportOptions);
22
+ extendHandshake(options: ClientHandshakeOptions): void;
23
+ /**
24
+ * Abstract method that creates a new {@link Connection} object.
25
+ * This should call {@link handleConnection} when the connection is created.
26
+ * The downstream client implementation needs to implement this.
27
+ *
28
+ * @param to The client ID of the node to connect to.
29
+ * @returns The new connection object.
30
+ */
31
+ protected abstract createNewOutgoingConnection(to: TransportClientId): Promise<ConnType>;
32
+ private tryReconnecting;
33
+ send(to: string, msg: PartialTransportMessage): string;
34
+ private createUnconnectedSession;
35
+ protected onConnectingFailed(session: SessionConnecting<ConnType>): SessionNoConnection;
36
+ protected onConnClosed(session: SessionHandshaking<ConnType> | SessionConnected<ConnType>): SessionNoConnection;
37
+ protected onConnectionEstablished(session: SessionConnecting<ConnType>, conn: ConnType): SessionHandshaking<ConnType>;
38
+ private rejectHandshakeResponse;
39
+ protected onHandshakeResponse(session: SessionHandshaking<ConnType>, msg: OpaqueTransportMessage): void;
40
+ /**
41
+ * Manually attempts to connect to a client.
42
+ * @param to The client ID of the node to connect to.
43
+ */
44
+ connect(to: TransportClientId): void;
45
+ private sendHandshake;
46
+ close(): void;
47
+ }
48
+
49
+ export { ClientTransport as C };
@@ -1,6 +1,6 @@
1
- import { C as Connection } from './services-970f97bb.js';
2
1
  import { Socket } from 'node:net';
3
2
  import stream, { Transform, TransformCallback, TransformOptions } from 'node:stream';
3
+ import { C as Connection } from './context-73df8978.js';
4
4
 
5
5
  interface LengthEncodedOptions extends TransformOptions {
6
6
  /** Maximum in-memory buffer size before we throw */
@@ -24,10 +24,6 @@ declare class UdsConnection extends Connection {
24
24
  input: stream.Readable;
25
25
  framer: Uint32LengthPrefixFraming;
26
26
  constructor(sock: Socket);
27
- addDataListener(cb: (msg: Uint8Array) => void): void;
28
- removeDataListener(cb: (msg: Uint8Array) => void): void;
29
- addCloseListener(cb: () => void): void;
30
- addErrorListener(cb: (err: Error) => void): void;
31
27
  send(payload: Uint8Array): boolean;
32
28
  close(): void;
33
29
  }
@@ -0,0 +1,11 @@
1
+ import { C as Connection } from './context-73df8978.js';
2
+ import { W as WsLike } from './wslike-e0b32dd5.js';
3
+
4
+ declare class WebSocketConnection extends Connection {
5
+ ws: WsLike;
6
+ constructor(ws: WsLike);
7
+ send(payload: Uint8Array): boolean;
8
+ close(): void;
9
+ }
10
+
11
+ export { WebSocketConnection as W };
@@ -0,0 +1,528 @@
1
+ import { e as TelemetryInfo, M as MessageMetadata, a as TransportMessage, b as OpaqueTransportMessage, P as PartialTransportMessage, c as TransportClientId, L as Logger, f as LogFn, g as LoggingLevel } from './message-fd349b27.js';
2
+ import { TSchema, Static } from '@sinclair/typebox';
3
+ import { C as Codec } from './types-3e5768ec.js';
4
+
5
+ /**
6
+ * A connection is the actual raw underlying transport connection.
7
+ * It’s responsible for dispatching to/from the actual connection itself
8
+ * This should be instantiated as soon as the client/server has a connection
9
+ * It’s tied to the lifecycle of the underlying transport connection (i.e. if the WS drops, this connection should be deleted)
10
+ */
11
+ declare abstract class Connection {
12
+ id: string;
13
+ telemetry?: TelemetryInfo;
14
+ constructor();
15
+ get loggingMetadata(): MessageMetadata;
16
+ private _dataListeners;
17
+ private _closeListeners;
18
+ private _errorListeners;
19
+ get dataListeners(): ((msg: Uint8Array) => void)[];
20
+ get closeListeners(): (() => void)[];
21
+ get errorListeners(): ((err: Error) => void)[];
22
+ /**
23
+ * Handle adding a callback for when a message is received.
24
+ * @param msg The message that was received.
25
+ */
26
+ addDataListener(cb: (msg: Uint8Array) => void): void;
27
+ removeDataListener(cb: (msg: Uint8Array) => void): void;
28
+ /**
29
+ * Handle adding a callback for when the connection is closed.
30
+ * This should also be called if an error happens and after notifying all the error listeners.
31
+ * @param cb The callback to call when the connection is closed.
32
+ */
33
+ addCloseListener(cb: () => void): void;
34
+ removeCloseListener(cb: () => void): void;
35
+ /**
36
+ * Handle adding a callback for when an error is received.
37
+ * This should only be used for this.logging errors, all cleanup
38
+ * should be delegated to addCloseListener.
39
+ *
40
+ * The implementer should take care such that the implemented
41
+ * connection will call both the close and error callbacks
42
+ * on an error.
43
+ *
44
+ * @param cb The callback to call when an error is received.
45
+ */
46
+ addErrorListener(cb: (err: Error) => void): void;
47
+ removeErrorListener(cb: (err: Error) => void): void;
48
+ /**
49
+ * Sends a message over the connection.
50
+ * @param msg The message to send.
51
+ * @returns true if the message was sent, false otherwise.
52
+ */
53
+ abstract send(msg: Uint8Array): boolean;
54
+ /**
55
+ * Closes the connection.
56
+ */
57
+ abstract close(): void;
58
+ }
59
+
60
+ interface SessionNoConnectionListeners {
61
+ onSessionGracePeriodElapsed: () => void;
62
+ }
63
+ declare class SessionNoConnection extends IdentifiedSession {
64
+ readonly state: SessionState.NoConnection;
65
+ listeners: SessionNoConnectionListeners;
66
+ gracePeriodTimeout?: ReturnType<typeof setTimeout>;
67
+ constructor(listeners: SessionNoConnectionListeners, ...args: ConstructorParameters<typeof IdentifiedSession>);
68
+ _handleClose(): void;
69
+ _handleStateExit(): void;
70
+ }
71
+
72
+ interface SessionConnectingListeners {
73
+ onConnectionEstablished: (conn: Connection) => void;
74
+ onConnectionFailed: (err: unknown) => void;
75
+ onConnectionTimeout: () => void;
76
+ }
77
+ declare class SessionConnecting<ConnType extends Connection> extends IdentifiedSession {
78
+ readonly state: SessionState.Connecting;
79
+ connPromise: Promise<ConnType>;
80
+ listeners: SessionConnectingListeners;
81
+ connectionTimeout?: ReturnType<typeof setTimeout>;
82
+ constructor(connPromise: Promise<ConnType>, listeners: SessionConnectingListeners, ...args: ConstructorParameters<typeof IdentifiedSession>);
83
+ bestEffortClose(): void;
84
+ _handleStateExit(): void;
85
+ _handleClose(): void;
86
+ }
87
+
88
+ interface SessionHandshakingListeners {
89
+ onConnectionErrored: (err: unknown) => void;
90
+ onConnectionClosed: () => void;
91
+ onHandshake: (msg: OpaqueTransportMessage) => void;
92
+ onInvalidHandshake: (reason: string) => void;
93
+ onHandshakeTimeout: () => void;
94
+ }
95
+ declare class SessionHandshaking<ConnType extends Connection> extends IdentifiedSession {
96
+ readonly state: SessionState.Handshaking;
97
+ conn: ConnType;
98
+ listeners: SessionHandshakingListeners;
99
+ handshakeTimeout: ReturnType<typeof setTimeout>;
100
+ constructor(conn: ConnType, listeners: SessionHandshakingListeners, ...args: ConstructorParameters<typeof IdentifiedSession>);
101
+ onHandshakeData: (msg: Uint8Array) => void;
102
+ sendHandshake(msg: TransportMessage): boolean;
103
+ _handleStateExit(): void;
104
+ _handleClose(): void;
105
+ }
106
+
107
+ interface SessionConnectedListeners {
108
+ onConnectionErrored: (err: unknown) => void;
109
+ onConnectionClosed: () => void;
110
+ onMessage: (msg: OpaqueTransportMessage) => void;
111
+ onInvalidMessage: (reason: string) => void;
112
+ }
113
+ declare class SessionConnected<ConnType extends Connection> extends IdentifiedSession {
114
+ readonly state: SessionState.Connected;
115
+ conn: ConnType;
116
+ listeners: SessionConnectedListeners;
117
+ heartbeatHandle?: ReturnType<typeof setInterval> | undefined;
118
+ heartbeatMisses: number;
119
+ get isActivelyHeartbeating(): boolean;
120
+ updateBookkeeping(ack: number, seq: number): void;
121
+ send(msg: PartialTransportMessage): string;
122
+ constructor(conn: ConnType, listeners: SessionConnectedListeners, ...args: ConstructorParameters<typeof IdentifiedSession>);
123
+ startActiveHeartbeat(): void;
124
+ private sendHeartbeat;
125
+ onMessageData: (msg: Uint8Array) => void;
126
+ _handleStateExit(): void;
127
+ _handleClose(): void;
128
+ }
129
+
130
+ declare const enum SessionState {
131
+ NoConnection = "NoConnection",
132
+ Connecting = "Connecting",
133
+ Handshaking = "Handshaking",
134
+ Connected = "Connected",
135
+ WaitingForHandshake = "WaitingForHandshake"
136
+ }
137
+ type Session<ConnType extends Connection> = SessionNoConnection | SessionConnecting<ConnType> | SessionHandshaking<ConnType> | SessionConnected<ConnType>;
138
+ declare abstract class StateMachineState {
139
+ abstract readonly state: SessionState;
140
+ _isConsumed: boolean;
141
+ abstract _handleStateExit(): void;
142
+ abstract _handleClose(): void;
143
+ close(): void;
144
+ constructor();
145
+ }
146
+ interface SessionOptions {
147
+ /**
148
+ * Frequency at which to send heartbeat acknowledgements
149
+ */
150
+ heartbeatIntervalMs: number;
151
+ /**
152
+ * Number of elapsed heartbeats without a response message before we consider
153
+ * the connection dead.
154
+ */
155
+ heartbeatsUntilDead: number;
156
+ /**
157
+ * Duration to wait between connection disconnect and actual session disconnect
158
+ */
159
+ sessionDisconnectGraceMs: number;
160
+ /**
161
+ * Connection timeout in milliseconds
162
+ */
163
+ connectionTimeoutMs: number;
164
+ /**
165
+ * Handshake timeout in milliseconds
166
+ */
167
+ handshakeTimeoutMs: number;
168
+ /**
169
+ * The codec to use for encoding/decoding messages over the wire
170
+ */
171
+ codec: Codec;
172
+ }
173
+ declare abstract class CommonSession extends StateMachineState {
174
+ readonly from: TransportClientId;
175
+ readonly options: SessionOptions;
176
+ log?: Logger;
177
+ abstract get loggingMetadata(): MessageMetadata;
178
+ constructor(from: TransportClientId, options: SessionOptions, log: Logger | undefined);
179
+ parseMsg(msg: Uint8Array): OpaqueTransportMessage | null;
180
+ }
181
+ type SessionId = string;
182
+ declare abstract class IdentifiedSession extends CommonSession {
183
+ readonly id: SessionId;
184
+ readonly telemetry: TelemetryInfo;
185
+ readonly to: TransportClientId;
186
+ readonly protocolVersion: string;
187
+ /**
188
+ * Index of the message we will send next (excluding handshake)
189
+ */
190
+ seq: number;
191
+ /**
192
+ * Number of unique messages we've received this session (excluding handshake)
193
+ */
194
+ ack: number;
195
+ sendBuffer: Array<OpaqueTransportMessage>;
196
+ constructor(id: SessionId, from: TransportClientId, to: TransportClientId, seq: number, ack: number, sendBuffer: Array<OpaqueTransportMessage>, telemetry: TelemetryInfo, options: SessionOptions, protocolVersion: string, log: Logger | undefined);
197
+ get loggingMetadata(): MessageMetadata;
198
+ constructMsg<Payload>(partialMsg: PartialTransportMessage<Payload>): TransportMessage<Payload>;
199
+ nextSeq(): number;
200
+ send(msg: PartialTransportMessage): string;
201
+ _handleStateExit(): void;
202
+ _handleClose(): void;
203
+ }
204
+
205
+ declare const ProtocolError: {
206
+ readonly RetriesExceeded: "conn_retry_exceeded";
207
+ readonly HandshakeFailed: "handshake_failed";
208
+ readonly MessageOrderingViolated: "message_ordering_violated";
209
+ };
210
+ type ProtocolErrorType = (typeof ProtocolError)[keyof typeof ProtocolError];
211
+ interface EventMap {
212
+ message: OpaqueTransportMessage;
213
+ sessionStatus: {
214
+ status: 'connect' | 'disconnect';
215
+ session: Session<Connection>;
216
+ };
217
+ sessionTransition: {
218
+ state: SessionState.Connected;
219
+ } | {
220
+ state: SessionState.Handshaking;
221
+ } | {
222
+ state: SessionState.Connecting;
223
+ } | {
224
+ state: SessionState.NoConnection;
225
+ };
226
+ protocolError: {
227
+ type: ProtocolErrorType;
228
+ message: string;
229
+ };
230
+ transportStatus: {
231
+ status: TransportStatus;
232
+ };
233
+ }
234
+ type EventTypes = keyof EventMap;
235
+ type EventHandler<K extends EventTypes> = (event: EventMap[K]) => unknown;
236
+ declare class EventDispatcher<T extends EventTypes> {
237
+ private eventListeners;
238
+ removeAllListeners(): void;
239
+ numberOfListeners<K extends T>(eventType: K): number;
240
+ addEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
241
+ removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
242
+ dispatchEvent<K extends T>(eventType: K, event: EventMap[K]): void;
243
+ }
244
+
245
+ /**
246
+ * Options to control the backoff and retry behavior of the client transport's connection behaviour.
247
+ *
248
+ * River implements exponential backoff with jitter to prevent flooding the server
249
+ * when there's an issue with connection establishment.
250
+ *
251
+ * The backoff is calculated via the following:
252
+ * backOff = min(jitter + {@link baseIntervalMs} * 2 ^ budget_consumed, {@link maxBackoffMs})
253
+ *
254
+ * We use a leaky bucket rate limit with a budget of {@link attemptBudgetCapacity} reconnection attempts.
255
+ * Budget only starts to restore after a successful handshake at a rate of one budget per {@link budgetRestoreIntervalMs}.
256
+ */
257
+ interface ConnectionRetryOptions {
258
+ /**
259
+ * The base interval to wait before retrying a connection.
260
+ */
261
+ baseIntervalMs: number;
262
+ /**
263
+ * The maximum random jitter to add to the total backoff time.
264
+ */
265
+ maxJitterMs: number;
266
+ /**
267
+ * The maximum amount of time to wait before retrying a connection.
268
+ * This does not include the jitter.
269
+ */
270
+ maxBackoffMs: number;
271
+ /**
272
+ * The max number of times to attempt a connection before a successful handshake.
273
+ * This persists across connections but starts restoring budget after a successful handshake.
274
+ * The restoration interval depends on {@link budgetRestoreIntervalMs}
275
+ */
276
+ attemptBudgetCapacity: number;
277
+ /**
278
+ * After a successful connection attempt, how long to wait before we restore a single budget.
279
+ */
280
+ budgetRestoreIntervalMs: number;
281
+ }
282
+ declare class LeakyBucketRateLimit {
283
+ private budgetConsumed;
284
+ private intervalHandles;
285
+ private readonly options;
286
+ constructor(options: ConnectionRetryOptions);
287
+ getBackoffMs(user: TransportClientId): number;
288
+ get totalBudgetRestoreTime(): number;
289
+ consumeBudget(user: TransportClientId): void;
290
+ getBudgetConsumed(user: TransportClientId): number;
291
+ hasBudget(user: TransportClientId): boolean;
292
+ startRestoringBudget(user: TransportClientId): void;
293
+ private stopLeak;
294
+ close(): void;
295
+ }
296
+
297
+ type TransportOptions = SessionOptions;
298
+ type ProvidedTransportOptions = Partial<TransportOptions>;
299
+ type ClientTransportOptions = TransportOptions & ConnectionRetryOptions;
300
+ type ProvidedClientTransportOptions = Partial<ClientTransportOptions>;
301
+ type ServerTransportOptions = TransportOptions;
302
+ type ProvidedServerTransportOptions = Partial<ServerTransportOptions>;
303
+
304
+ /**
305
+ * Represents the possible states of a transport.
306
+ * @property {'open'} open - The transport is open and operational (note that this doesn't mean it is actively connected)
307
+ * @property {'closed'} closed - The transport is permanently closed and cannot be reopened.
308
+ */
309
+ type TransportStatus = 'open' | 'closed';
310
+ /**
311
+ * Transports manage the lifecycle (creation/deletion) of sessions
312
+ *
313
+ * ```plaintext
314
+ * ▲
315
+ * incoming │
316
+ * messages │
317
+ * ▼
318
+ * ┌─────────────┐ 1:N ┌───────────┐ 1:1* ┌────────────┐
319
+ * │ Transport │ ◄─────► │ Session │ ◄─────► │ Connection │
320
+ * └─────────────┘ └───────────┘ └────────────┘
321
+ * ▲ * (may or may not be initialized yet)
322
+ * │
323
+ * ▼
324
+ * ┌───────────┐
325
+ * │ Message │
326
+ * │ Listeners │
327
+ * └───────────┘
328
+ * ```
329
+ * @abstract
330
+ */
331
+ declare abstract class Transport<ConnType extends Connection> {
332
+ /**
333
+ * The status of the transport.
334
+ */
335
+ private status;
336
+ /**
337
+ * The client ID of this transport.
338
+ */
339
+ clientId: TransportClientId;
340
+ /**
341
+ * The event dispatcher for handling events of type EventTypes.
342
+ */
343
+ eventDispatcher: EventDispatcher<EventTypes>;
344
+ /**
345
+ * The options for this transport.
346
+ */
347
+ protected options: TransportOptions;
348
+ log?: Logger;
349
+ sessions: Map<TransportClientId, Session<ConnType>>;
350
+ /**
351
+ * Creates a new Transport instance.
352
+ * @param codec The codec used to encode and decode messages.
353
+ * @param clientId The client ID of this transport.
354
+ */
355
+ constructor(clientId: TransportClientId, providedOptions?: ProvidedTransportOptions);
356
+ bindLogger(fn: LogFn | Logger, level?: LoggingLevel): void;
357
+ /**
358
+ * Called when a message is received by this transport.
359
+ * You generally shouldn't need to override this in downstream transport implementations.
360
+ * @param msg The received message.
361
+ */
362
+ protected handleMsg(msg: OpaqueTransportMessage): void;
363
+ /**
364
+ * Adds a listener to this transport.
365
+ * @param the type of event to listen for
366
+ * @param handler The message handler to add.
367
+ */
368
+ addEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
369
+ /**
370
+ * Removes a listener from this transport.
371
+ * @param the type of event to un-listen on
372
+ * @param handler The message handler to remove.
373
+ */
374
+ removeEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
375
+ /**
376
+ * Sends a message over this transport, delegating to the appropriate connection to actually
377
+ * send the message.
378
+ * @param msg The message to send.
379
+ * @returns The ID of the sent message or undefined if it wasn't sent
380
+ */
381
+ abstract send(to: TransportClientId, msg: PartialTransportMessage): string;
382
+ protected protocolError(type: ProtocolErrorType, message: string): void;
383
+ /**
384
+ * Default close implementation for transports. You should override this in the downstream
385
+ * implementation if you need to do any additional cleanup and call super.close() at the end.
386
+ * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
387
+ */
388
+ close(): void;
389
+ getStatus(): TransportStatus;
390
+ protected updateSession<S extends Session<ConnType>>(session: S): S;
391
+ protected deleteSession(session: Session<ConnType>): void;
392
+ protected onSessionGracePeriodElapsed(session: SessionNoConnection): void;
393
+ protected onConnectingFailed(session: SessionConnecting<ConnType>): SessionNoConnection;
394
+ protected onConnClosed(session: SessionHandshaking<ConnType> | SessionConnected<ConnType>): SessionNoConnection;
395
+ }
396
+
397
+ type ConstructHandshake<T extends TSchema> = () => Static<T> | Promise<Static<T>>;
398
+ type ValidateHandshake<T extends TSchema> = (metadata: Static<T>, previousParsedMetadata?: ParsedMetadata) => false | ParsedMetadata | Promise<false | ParsedMetadata>;
399
+ interface ClientHandshakeOptions<MetadataSchema extends TSchema = TSchema> {
400
+ /**
401
+ * Schema for the metadata that the client sends to the server
402
+ * during the handshake.
403
+ */
404
+ schema: MetadataSchema;
405
+ /**
406
+ * Gets the {@link HandshakeRequestMetadata} to send to the server.
407
+ */
408
+ construct: ConstructHandshake<MetadataSchema>;
409
+ }
410
+ interface ServerHandshakeOptions<MetadataSchema extends TSchema = TSchema> {
411
+ /**
412
+ * Schema for the metadata that the server receives from the client
413
+ * during the handshake.
414
+ */
415
+ schema: MetadataSchema;
416
+ /**
417
+ * Parses the {@link HandshakeRequestMetadata} sent by the client, transforming
418
+ * it into {@link ParsedHandshakeMetadata}.
419
+ *
420
+ * May return `false` if the client should be rejected.
421
+ *
422
+ * @param metadata - The metadata sent by the client.
423
+ * @param session - The session that the client would be associated with.
424
+ * @param isReconnect - Whether the client is reconnecting to the session,
425
+ * or if this is a new session.
426
+ */
427
+ validate: ValidateHandshake<MetadataSchema>;
428
+ }
429
+ declare function createClientHandshakeOptions<MetadataSchema extends TSchema = TSchema>(schema: MetadataSchema, construct: ConstructHandshake<MetadataSchema>): ClientHandshakeOptions;
430
+ declare function createServerHandshakeOptions<MetadataSchema extends TSchema = TSchema>(schema: MetadataSchema, validate: ValidateHandshake<MetadataSchema>): ServerHandshakeOptions;
431
+
432
+ /**
433
+ * ServiceContext exist for the purpose of declaration merging
434
+ * to extend the context with additional properties.
435
+ *
436
+ * For example:
437
+ *
438
+ * ```ts
439
+ * declare module '@replit/river' {
440
+ * interface ServiceContext {
441
+ * db: Database;
442
+ * }
443
+ * }
444
+ *
445
+ * createServer(someTransport, myServices, { extendedContext: { db: myDb } });
446
+ * ```
447
+ *
448
+ * Once you do this, your {@link ProcedureHandlerContext} will have `db` property on it.
449
+ */
450
+ interface ServiceContext {
451
+ }
452
+ /**
453
+ * The parsed metadata schema for a service. This is the
454
+ * return value of the {@link ServerHandshakeOptions.validate}
455
+ * if the handshake extension is used.
456
+ *
457
+ * You should use declaration merging to extend this interface
458
+ * with the sanitized metadata.
459
+ *
460
+ * ```ts
461
+ * declare module '@replit/river' {
462
+ * interface ParsedMetadata {
463
+ * userId: number;
464
+ * }
465
+ * }
466
+ * ```
467
+ */
468
+ interface ParsedMetadata {
469
+ }
470
+ /**
471
+ * This is passed to every procedure handler and contains various context-level
472
+ * information and utilities. This may be extended, see {@link ServiceContext}
473
+ */
474
+ type ProcedureHandlerContext<State> = ServiceContext & {
475
+ /**
476
+ * State for this service as defined by the service definition.
477
+ */
478
+ state: State;
479
+ /**
480
+ * Metadata parsed on the server. See {@link ParsedMetadata}
481
+ */
482
+ metadata: ParsedMetadata;
483
+ /**
484
+ * The ID of the session that sent this request.
485
+ */
486
+ sessionId: SessionId;
487
+ /**
488
+ * The ID of the client that sent this request. There may be multiple sessions per client.
489
+ */
490
+ from: TransportClientId;
491
+ /**
492
+ * An AbortController for this stream. This is used to abort the stream from the
493
+ * handler and notify the client that the stream was aborted.
494
+ *
495
+ * Important to note that this controller is owned by the handler, if you
496
+ * want to listen to aborts coming from the client, you should use the
497
+ * {@link clientAbortSignal}.
498
+ *
499
+ * Aborts are not the same as closing streams gracefully, please refer to
500
+ * the river documentation to understand the difference between the two concepts.
501
+ */
502
+ abortController: AbortController;
503
+ /**
504
+ * You can listen to clientAbortSignal this to check if the client aborted the request,
505
+ * or if the request was aborted due to an unexpected disconnect from the calling
506
+ * session.
507
+ *
508
+ * If the procedure has a read stream (e.g. upload or stream), the procedure will
509
+ * notified of aborts as part of the stream, but you may still want to use
510
+ * this signal as it is triggered immediately after an abort comes in,
511
+ * in readStreams some data may be buffered before the abort result shows up.
512
+ *
513
+ * Important to note that this signal is owned by the client, you have a separate
514
+ * signal inside {@link abortController} for aborts triggered within the handler.
515
+ *
516
+ * Aborts are not the same as closing streams gracefully, please refer to
517
+ * the river documentation to understand the difference between the two concepts.
518
+ */
519
+ clientAbortSignal: AbortSignal;
520
+ /**
521
+ * Lets you add a function that will run when the request is done, this can be
522
+ * due to an abort (from either side), error, or success. If the callback is
523
+ * added after the stream ended, it will run immediately.
524
+ */
525
+ onRequestFinished: (callback: () => void) => void;
526
+ };
527
+
528
+ export { Connection as C, EventMap as E, LeakyBucketRateLimit as L, ProvidedTransportOptions as P, Session as S, Transport as T, TransportStatus as a, ProvidedClientTransportOptions as b, ProvidedServerTransportOptions as c, SessionState as d, SessionNoConnection as e, SessionConnecting as f, SessionHandshaking as g, SessionConnected as h, EventTypes as i, EventHandler as j, ProtocolError as k, ProtocolErrorType as l, SessionOptions as m, ServiceContext as n, ClientTransportOptions as o, ClientHandshakeOptions as p, ServerHandshakeOptions as q, ParsedMetadata as r, ProcedureHandlerContext as s, createClientHandshakeOptions as t, createServerHandshakeOptions as u, CommonSession as v, SessionHandshakingListeners as w, ServerTransportOptions as x };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../logging/index.ts","../../logging/log.ts"],"sourcesContent":["export { stringLogger, coloredStringLogger, jsonLogger } from './log';\nexport type { Logger, LogFn, MessageMetadata } from './log';\n","import { ValueError } from '@sinclair/typebox/value';\nimport { OpaqueTransportMessage } from '../transport/message';\n\nconst LoggingLevels = {\n debug: -1,\n info: 0,\n warn: 1,\n error: 2,\n} as const;\nexport type LoggingLevel = keyof typeof LoggingLevels;\n\nexport type LogFn = (\n msg: string,\n ctx?: MessageMetadata,\n level?: LoggingLevel,\n) => void;\nexport type Logger = {\n [key in LoggingLevel]: (msg: string, metadata?: MessageMetadata) => void;\n};\n\nexport type Tags = 'invariant-violation' | 'invalid-request';\n\nconst cleanedLogFn = (log: LogFn) => {\n return (msg: string, metadata?: MessageMetadata) => {\n // skip cloning object if metadata has no transportMessage\n if (!metadata?.transportMessage) {\n log(msg, metadata);\n return;\n }\n\n // clone metadata and clean transportMessage\n const { payload, ...rest } = metadata.transportMessage;\n metadata.transportMessage = rest;\n log(msg, metadata);\n };\n};\n\nexport type MessageMetadata = Partial<{\n protocolVersion: string;\n clientId: string;\n connectedTo: string;\n sessionId: string;\n connId: string;\n transportMessage: Partial<OpaqueTransportMessage>;\n validationErrors: Array<ValueError>;\n tags: Array<Tags>;\n telemetry: {\n traceId: string;\n spanId: string;\n };\n}>;\n\nexport class BaseLogger implements Logger {\n minLevel: LoggingLevel;\n private output: LogFn;\n\n constructor(output: LogFn, minLevel: LoggingLevel = 'info') {\n this.minLevel = minLevel;\n this.output = output;\n }\n\n debug(msg: string, metadata?: MessageMetadata) {\n if (LoggingLevels[this.minLevel] <= LoggingLevels.debug) {\n this.output(msg, metadata ?? {}, 'debug');\n }\n }\n\n info(msg: string, metadata?: MessageMetadata) {\n if (LoggingLevels[this.minLevel] <= LoggingLevels.info) {\n this.output(msg, metadata ?? {}, 'info');\n }\n }\n\n warn(msg: string, metadata?: MessageMetadata) {\n if (LoggingLevels[this.minLevel] <= LoggingLevels.warn) {\n this.output(msg, metadata ?? {}, 'warn');\n }\n }\n\n error(msg: string, metadata?: MessageMetadata) {\n if (LoggingLevels[this.minLevel] <= LoggingLevels.error) {\n this.output(msg, metadata ?? {}, 'error');\n }\n }\n}\n\nexport const stringLogger: LogFn = (msg, ctx, level = 'info') => {\n const from = ctx?.clientId ? `${ctx.clientId} -- ` : '';\n console.log(`[river:${level}] ${from}${msg}`);\n};\n\nconst colorMap = {\n debug: '\\u001b[34m',\n info: '\\u001b[32m',\n warn: '\\u001b[33m',\n error: '\\u001b[31m',\n};\n\nexport const coloredStringLogger: LogFn = (msg, ctx, level = 'info') => {\n const color = colorMap[level];\n const from = ctx?.clientId ? `${ctx.clientId} -- ` : '';\n console.log(`[river:${color}${level}\\u001b[0m] ${from}${msg}`);\n};\n\nexport const jsonLogger: LogFn = (msg, ctx, level) => {\n console.log(JSON.stringify({ msg, ctx, level }));\n};\n\nexport const createLogProxy = (log: Logger) => ({\n debug: cleanedLogFn(log.debug.bind(log)),\n info: cleanedLogFn(log.info.bind(log)),\n warn: cleanedLogFn(log.warn.bind(log)),\n error: cleanedLogFn(log.error.bind(log)),\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACsFO,IAAM,eAAsB,CAAC,KAAK,KAAK,QAAQ,WAAW;AAC/D,QAAM,OAAO,KAAK,WAAW,GAAG,IAAI,QAAQ,SAAS;AACrD,UAAQ,IAAI,UAAU,KAAK,KAAK,IAAI,GAAG,GAAG,EAAE;AAC9C;AAEA,IAAM,WAAW;AAAA,EACf,OAAO;AAAA,EACP,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AACT;AAEO,IAAM,sBAA6B,CAAC,KAAK,KAAK,QAAQ,WAAW;AACtE,QAAM,QAAQ,SAAS,KAAK;AAC5B,QAAM,OAAO,KAAK,WAAW,GAAG,IAAI,QAAQ,SAAS;AACrD,UAAQ,IAAI,UAAU,KAAK,GAAG,KAAK,YAAc,IAAI,GAAG,GAAG,EAAE;AAC/D;AAEO,IAAM,aAAoB,CAAC,KAAK,KAAK,UAAU;AACpD,UAAQ,IAAI,KAAK,UAAU,EAAE,KAAK,KAAK,MAAM,CAAC,CAAC;AACjD;","names":[]}
1
+ {"version":3,"sources":["../../logging/index.ts","../../logging/log.ts"],"sourcesContent":["export { stringLogger, coloredStringLogger, jsonLogger } from './log';\nexport type { Logger, LogFn, MessageMetadata } from './log';\n","import { ValueError } from '@sinclair/typebox/value';\nimport { OpaqueTransportMessage } from '../transport/message';\n\nconst LoggingLevels = {\n debug: -1,\n info: 0,\n warn: 1,\n error: 2,\n} as const;\nexport type LoggingLevel = keyof typeof LoggingLevels;\n\nexport type LogFn = (\n msg: string,\n ctx?: MessageMetadata,\n level?: LoggingLevel,\n) => void;\nexport type Logger = {\n [key in LoggingLevel]: (msg: string, metadata?: MessageMetadata) => void;\n};\n\nexport type Tags =\n | 'invariant-violation'\n | 'state-transition'\n | 'invalid-request';\n\nconst cleanedLogFn = (log: LogFn) => {\n return (msg: string, metadata?: MessageMetadata) => {\n // skip cloning object if metadata has no transportMessage\n if (!metadata?.transportMessage) {\n log(msg, metadata);\n return;\n }\n\n // clone metadata and clean transportMessage\n const { payload, ...rest } = metadata.transportMessage;\n metadata.transportMessage = rest;\n log(msg, metadata);\n };\n};\n\nexport type MessageMetadata = Partial<{\n protocolVersion: string;\n clientId: string;\n connectedTo: string;\n sessionId: string;\n connId: string;\n transportMessage: Partial<OpaqueTransportMessage>;\n validationErrors: Array<ValueError>;\n tags: Array<Tags>;\n telemetry: {\n traceId: string;\n spanId: string;\n };\n}>;\n\nexport class BaseLogger implements Logger {\n minLevel: LoggingLevel;\n private output: LogFn;\n\n constructor(output: LogFn, minLevel: LoggingLevel = 'info') {\n this.minLevel = minLevel;\n this.output = output;\n }\n\n debug(msg: string, metadata?: MessageMetadata) {\n if (LoggingLevels[this.minLevel] <= LoggingLevels.debug) {\n this.output(msg, metadata ?? {}, 'debug');\n }\n }\n\n info(msg: string, metadata?: MessageMetadata) {\n if (LoggingLevels[this.minLevel] <= LoggingLevels.info) {\n this.output(msg, metadata ?? {}, 'info');\n }\n }\n\n warn(msg: string, metadata?: MessageMetadata) {\n if (LoggingLevels[this.minLevel] <= LoggingLevels.warn) {\n this.output(msg, metadata ?? {}, 'warn');\n }\n }\n\n error(msg: string, metadata?: MessageMetadata) {\n if (LoggingLevels[this.minLevel] <= LoggingLevels.error) {\n this.output(msg, metadata ?? {}, 'error');\n }\n }\n}\n\nexport const stringLogger: LogFn = (msg, ctx, level = 'info') => {\n const from = ctx?.clientId ? `${ctx.clientId} -- ` : '';\n console.log(`[river:${level}] ${from}${msg}`);\n};\n\nconst colorMap = {\n debug: '\\u001b[34m',\n info: '\\u001b[32m',\n warn: '\\u001b[33m',\n error: '\\u001b[31m',\n};\n\nexport const coloredStringLogger: LogFn = (msg, ctx, level = 'info') => {\n const color = colorMap[level];\n const from = ctx?.clientId ? `${ctx.clientId} -- ` : '';\n console.log(`[river:${color}${level}\\u001b[0m] ${from}${msg}`);\n};\n\nexport const jsonLogger: LogFn = (msg, ctx, level) => {\n console.log(JSON.stringify({ msg, ctx, level }));\n};\n\nexport const createLogProxy = (log: Logger) => ({\n debug: cleanedLogFn(log.debug.bind(log)),\n info: cleanedLogFn(log.info.bind(log)),\n warn: cleanedLogFn(log.warn.bind(log)),\n error: cleanedLogFn(log.error.bind(log)),\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACyFO,IAAM,eAAsB,CAAC,KAAK,KAAK,QAAQ,WAAW;AAC/D,QAAM,OAAO,KAAK,WAAW,GAAG,IAAI,QAAQ,SAAS;AACrD,UAAQ,IAAI,UAAU,KAAK,KAAK,IAAI,GAAG,GAAG,EAAE;AAC9C;AAEA,IAAM,WAAW;AAAA,EACf,OAAO;AAAA,EACP,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AACT;AAEO,IAAM,sBAA6B,CAAC,KAAK,KAAK,QAAQ,WAAW;AACtE,QAAM,QAAQ,SAAS,KAAK;AAC5B,QAAM,OAAO,KAAK,WAAW,GAAG,IAAI,QAAQ,SAAS;AACrD,UAAQ,IAAI,UAAU,KAAK,GAAG,KAAK,YAAc,IAAI,GAAG,GAAG,EAAE;AAC/D;AAEO,IAAM,aAAoB,CAAC,KAAK,KAAK,UAAU;AACpD,UAAQ,IAAI,KAAK,UAAU,EAAE,KAAK,KAAK,MAAM,CAAC,CAAC;AACjD;","names":[]}
@@ -1,4 +1,4 @@
1
- export { d as LogFn, L as Logger, M as MessageMetadata, j as coloredStringLogger, k as jsonLogger, s as stringLogger } from '../index-10ebd26a.js';
1
+ export { f as LogFn, L as Logger, M as MessageMetadata, h as coloredStringLogger, j as jsonLogger, s as stringLogger } from '../message-fd349b27.js';
2
2
  import '@sinclair/typebox/value';
3
3
  import '@sinclair/typebox';
4
4
  import '@opentelemetry/api';
@@ -1,4 +1,4 @@
1
- export { d as LogFn, L as Logger, M as MessageMetadata, j as coloredStringLogger, k as jsonLogger, s as stringLogger } from '../index-10ebd26a.js';
1
+ export { f as LogFn, L as Logger, M as MessageMetadata, h as coloredStringLogger, j as jsonLogger, s as stringLogger } from '../message-fd349b27.js';
2
2
  import '@sinclair/typebox/value';
3
3
  import '@sinclair/typebox';
4
4
  import '@opentelemetry/api';
@@ -2,7 +2,7 @@ import {
2
2
  coloredStringLogger,
3
3
  jsonLogger,
4
4
  stringLogger
5
- } from "../chunk-QMM35C3H.js";
5
+ } from "../chunk-VXYHC666.js";
6
6
  export {
7
7
  coloredStringLogger,
8
8
  jsonLogger,