@replit/river 0.23.3 → 0.23.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 (59) hide show
  1. package/README.md +48 -0
  2. package/dist/{chunk-IKT6A32W.js → chunk-BHNINW7F.js} +2 -2
  3. package/dist/chunk-BHNINW7F.js.map +1 -0
  4. package/dist/{chunk-XZV3HOQW.js → chunk-CHOAXTQF.js} +2 -2
  5. package/dist/chunk-CHOAXTQF.js.map +1 -0
  6. package/dist/{chunk-3U43VMW2.js → chunk-PTX6DT5G.js} +2 -2
  7. package/dist/{chunk-R3EG3FOP.js → chunk-RWYW2BES.js} +2 -2
  8. package/dist/{chunk-JBRQNEBV.js → chunk-WTJY7S3D.js} +2 -2
  9. package/dist/chunk-WTJY7S3D.js.map +1 -0
  10. package/dist/{connection-89a66019.d.ts → connection-36e8bb12.d.ts} +1 -1
  11. package/dist/{connection-4abad643.d.ts → connection-a5bc5e9f.d.ts} +1 -1
  12. package/dist/index-926aea33.d.ts +133 -0
  13. package/dist/logging/index.d.cts +1 -2
  14. package/dist/logging/index.d.ts +1 -2
  15. package/dist/router/index.cjs +1 -1
  16. package/dist/router/index.cjs.map +1 -1
  17. package/dist/router/index.d.cts +7 -7
  18. package/dist/router/index.d.ts +7 -7
  19. package/dist/router/index.js +2 -2
  20. package/dist/{services-ef89f5b0.d.ts → services-da250ed2.d.ts} +2 -2
  21. package/dist/transport/impls/uds/client.cjs +1 -1
  22. package/dist/transport/impls/uds/client.cjs.map +1 -1
  23. package/dist/transport/impls/uds/client.d.cts +4 -4
  24. package/dist/transport/impls/uds/client.d.ts +4 -4
  25. package/dist/transport/impls/uds/client.js +3 -3
  26. package/dist/transport/impls/uds/server.cjs +1 -1
  27. package/dist/transport/impls/uds/server.cjs.map +1 -1
  28. package/dist/transport/impls/uds/server.d.cts +4 -4
  29. package/dist/transport/impls/uds/server.d.ts +4 -4
  30. package/dist/transport/impls/uds/server.js +3 -3
  31. package/dist/transport/impls/ws/client.cjs +1 -1
  32. package/dist/transport/impls/ws/client.cjs.map +1 -1
  33. package/dist/transport/impls/ws/client.d.cts +4 -4
  34. package/dist/transport/impls/ws/client.d.ts +4 -4
  35. package/dist/transport/impls/ws/client.js +3 -3
  36. package/dist/transport/impls/ws/server.cjs +1 -1
  37. package/dist/transport/impls/ws/server.cjs.map +1 -1
  38. package/dist/transport/impls/ws/server.d.cts +3 -3
  39. package/dist/transport/impls/ws/server.d.ts +3 -3
  40. package/dist/transport/impls/ws/server.js +3 -3
  41. package/dist/transport/index.cjs +1 -1
  42. package/dist/transport/index.cjs.map +1 -1
  43. package/dist/transport/index.d.cts +4 -307
  44. package/dist/transport/index.d.ts +4 -307
  45. package/dist/transport/index.js +2 -2
  46. package/dist/transport-4a5e288a.d.ts +535 -0
  47. package/dist/util/testHelpers.cjs +1 -1
  48. package/dist/util/testHelpers.cjs.map +1 -1
  49. package/dist/util/testHelpers.d.cts +3 -3
  50. package/dist/util/testHelpers.d.ts +3 -3
  51. package/dist/util/testHelpers.js +3 -3
  52. package/package.json +1 -1
  53. package/dist/chunk-IKT6A32W.js.map +0 -1
  54. package/dist/chunk-JBRQNEBV.js.map +0 -1
  55. package/dist/chunk-XZV3HOQW.js.map +0 -1
  56. package/dist/index-a009d7f6.d.ts +0 -360
  57. package/dist/services-edc3b16d.d.ts +0 -707
  58. /package/dist/{chunk-3U43VMW2.js.map → chunk-PTX6DT5G.js.map} +0 -0
  59. /package/dist/{chunk-R3EG3FOP.js.map → chunk-RWYW2BES.js.map} +0 -0
@@ -1,309 +1,6 @@
1
- import { C as Codec } from '../types-3e5768ec.js';
2
- import { O as OpaqueTransportMessage, C as Connection, b as Session, T as TransportClientId, m as PropagationContext, P as PartialTransportMessage, g as ClientHandshakeOptions, c as ServerHandshakeOptions, d as ParsedMetadata, S as SessionOptions } from '../index-a009d7f6.js';
3
- export { i as OpaqueTransportMessageSchema, j as TransportMessage, h as TransportMessageSchema, l as isStreamClose, k as isStreamOpen } from '../index-a009d7f6.js';
4
- import '@sinclair/typebox/value';
1
+ export { c as ClientTransport, P as ClientTransportOptions, C as Connection, n as EventHandler, E as EventMap, m as EventTypes, o as ProtocolError, p as ProtocolErrorType, d as ServerTransport, e as ServerTransportOptions, b as Session, T as Transport, k as TransportOptions, l as TransportStatus } from '../transport-4a5e288a.js';
2
+ export { O as OpaqueTransportMessage, e as OpaqueTransportMessageSchema, T as TransportClientId, c as TransportMessage, d as TransportMessageSchema, f as isStreamClose, i as isStreamOpen } from '../index-926aea33.js';
3
+ import '../types-3e5768ec.js';
5
4
  import '@sinclair/typebox';
5
+ import '@sinclair/typebox/value';
6
6
  import '@opentelemetry/api';
7
-
8
- type ConnectionStatus = 'connect' | 'disconnect';
9
- declare const ProtocolError: {
10
- readonly RetriesExceeded: "conn_retry_exceeded";
11
- readonly HandshakeFailed: "handshake_failed";
12
- readonly UseAfterDestroy: "use_after_destroy";
13
- readonly MessageOrderingViolated: "message_ordering_violated";
14
- };
15
- type ProtocolErrorType = (typeof ProtocolError)[keyof typeof ProtocolError];
16
- interface EventMap {
17
- message: OpaqueTransportMessage;
18
- connectionStatus: {
19
- status: ConnectionStatus;
20
- conn: Connection;
21
- };
22
- sessionStatus: {
23
- status: ConnectionStatus;
24
- session: Session<Connection>;
25
- };
26
- protocolError: {
27
- type: ProtocolErrorType;
28
- message: string;
29
- };
30
- }
31
- type EventTypes = keyof EventMap;
32
- type EventHandler<K extends EventTypes> = (event: EventMap[K]) => unknown;
33
- declare class EventDispatcher<T extends EventTypes> {
34
- private eventListeners;
35
- numberOfListeners<K extends T>(eventType: K): number;
36
- addEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
37
- removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
38
- dispatchEvent<K extends T>(eventType: K, event: EventMap[K]): void;
39
- }
40
-
41
- /**
42
- * Options to control the backoff and retry behavior of the client transport's connection behaviour.
43
- *
44
- * River implements exponential backoff with jitter to prevent flooding the server
45
- * when there's an issue with connection establishment.
46
- *
47
- * The backoff is calculated via the following:
48
- * backOff = min(jitter + {@link baseIntervalMs} * 2 ^ budget_consumed, {@link maxBackoffMs})
49
- *
50
- * We use a leaky bucket rate limit with a budget of {@link attemptBudgetCapacity} reconnection attempts.
51
- * Budget only starts to restore after a successful handshake at a rate of one budget per {@link budgetRestoreIntervalMs}.
52
- */
53
- interface ConnectionRetryOptions {
54
- /**
55
- * The base interval to wait before retrying a connection.
56
- */
57
- baseIntervalMs: number;
58
- /**
59
- * The maximum random jitter to add to the total backoff time.
60
- */
61
- maxJitterMs: number;
62
- /**
63
- * The maximum amount of time to wait before retrying a connection.
64
- * This does not include the jitter.
65
- */
66
- maxBackoffMs: number;
67
- /**
68
- * The max number of times to attempt a connection before a successful handshake.
69
- * This persists across connections but starts restoring budget after a successful handshake.
70
- * The restoration interval depends on {@link budgetRestoreIntervalMs}
71
- */
72
- attemptBudgetCapacity: number;
73
- /**
74
- * After a successful connection attempt, how long to wait before we restore a single budget.
75
- */
76
- budgetRestoreIntervalMs: number;
77
- }
78
- declare class LeakyBucketRateLimit {
79
- private budgetConsumed;
80
- private intervalHandles;
81
- private readonly options;
82
- constructor(options: ConnectionRetryOptions);
83
- getBackoffMs(user: TransportClientId): number;
84
- get totalBudgetRestoreTime(): number;
85
- consumeBudget(user: TransportClientId): void;
86
- getBudgetConsumed(user: TransportClientId): number;
87
- hasBudget(user: TransportClientId): boolean;
88
- startRestoringBudget(user: TransportClientId): void;
89
- private stopLeak;
90
- close(): void;
91
- }
92
-
93
- /**
94
- * Represents the possible states of a transport.
95
- * @property {'open'} open - The transport is open and operational (note that this doesn't mean it is actively connected)
96
- * @property {'closed'} closed - The transport is closed and not operational, but can be reopened.
97
- * @property {'destroyed'} destroyed - The transport is permanently destroyed and cannot be reopened.
98
- */
99
- type TransportStatus = 'open' | 'closed' | 'destroyed';
100
- type TransportOptions = SessionOptions;
101
- type ProvidedTransportOptions = Partial<TransportOptions>;
102
- type ClientTransportOptions = TransportOptions & ConnectionRetryOptions;
103
- type ProvidedClientTransportOptions = Partial<ClientTransportOptions>;
104
- type ServerTransportOptions = TransportOptions;
105
- type ProvidedServerTransportOptions = Partial<ServerTransportOptions>;
106
- /**
107
- * Transports manage the lifecycle (creation/deletion) of sessions and connections. Its responsibilities include:
108
- *
109
- * 1) Constructing a new {@link Session} and {@link Connection} on {@link TransportMessage}s from new clients.
110
- * After constructing the {@link Connection}, {@link onConnect} is called which adds it to the connection map.
111
- * 2) Delegating message listening of the connection to the newly created {@link Connection}.
112
- * From this point on, the {@link Connection} is responsible for *reading* and *writing*
113
- * messages from the connection.
114
- * 3) When a connection is closed, the {@link Transport} calls {@link onDisconnect} which closes the
115
- * connection via {@link Connection.close} and removes it from the {@link connections} map.
116
-
117
- *
118
- * ```plaintext
119
- * ▲
120
- * incoming │
121
- * messages │
122
- * ▼
123
- * ┌─────────────┐ 1:N ┌───────────┐ 1:1* ┌────────────┐
124
- * │ Transport │ ◄─────► │ Session │ ◄─────► │ Connection │
125
- * └─────────────┘ └───────────┘ └────────────┘
126
- * ▲ * (may or may not be initialized yet)
127
- * │
128
- * ▼
129
- * ┌───────────┐
130
- * │ Message │
131
- * │ Listeners │
132
- * └───────────┘
133
- * ```
134
- * @abstract
135
- */
136
- declare abstract class Transport<ConnType extends Connection> {
137
- /**
138
- * A flag indicating whether the transport has been destroyed.
139
- * A destroyed transport will not attempt to reconnect and cannot be used again.
140
- */
141
- state: TransportStatus;
142
- /**
143
- * The {@link Codec} used to encode and decode messages.
144
- */
145
- codec: Codec;
146
- /**
147
- * The client ID of this transport.
148
- */
149
- clientId: TransportClientId;
150
- /**
151
- * The map of {@link Session}s managed by this transport.
152
- */
153
- sessions: Map<TransportClientId, Session<ConnType>>;
154
- /**
155
- * The map of {@link Connection}s managed by this transport.
156
- */
157
- get connections(): Map<string, ConnType>;
158
- /**
159
- * The event dispatcher for handling events of type EventTypes.
160
- */
161
- eventDispatcher: EventDispatcher<EventTypes>;
162
- /**
163
- * The options for this transport.
164
- */
165
- protected options: TransportOptions;
166
- /**
167
- * Creates a new Transport instance.
168
- * This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
169
- * @param codec The codec used to encode and decode messages.
170
- * @param clientId The client ID of this transport.
171
- */
172
- constructor(clientId: TransportClientId, providedOptions?: ProvidedTransportOptions);
173
- /**
174
- * This is called immediately after a new connection is established and we
175
- * may or may not know the identity of the connected client.
176
- * It should attach all the necessary listeners to the connection for lifecycle
177
- * events (i.e. data, close, error)
178
- *
179
- * This method is implemented by {@link ClientTransport} and {@link ServerTransport}.
180
- */
181
- protected abstract handleConnection(conn: ConnType, to: TransportClientId): void;
182
- /**
183
- * Called when a new connection is established
184
- * and we know the identity of the connected client.
185
- * @param conn The connection object.
186
- */
187
- protected onConnect(conn: ConnType, connectedTo: TransportClientId, session: Session<ConnType>, isReconnect: boolean): void;
188
- protected createSession(to: TransportClientId, conn?: ConnType, propagationCtx?: PropagationContext): Session<ConnType>;
189
- protected getOrCreateSession(to: TransportClientId, conn?: ConnType, sessionId?: string, propagationCtx?: PropagationContext): {
190
- session: Session<ConnType>;
191
- isReconnect: boolean;
192
- };
193
- protected deleteSession(session: Session<ConnType>): void;
194
- /**
195
- * The downstream implementation needs to call this when a connection is closed.
196
- * @param conn The connection object.
197
- * @param connectedTo The peer we are connected to.
198
- */
199
- protected onDisconnect(conn: ConnType, session: Session<ConnType>): void;
200
- /**
201
- * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
202
- * @param msg The message to parse.
203
- * @returns The parsed message, or null if the message is malformed or invalid.
204
- */
205
- protected parseMsg(msg: Uint8Array): OpaqueTransportMessage | null;
206
- /**
207
- * Called when a message is received by this transport.
208
- * You generally shouldn't need to override this in downstream transport implementations.
209
- * @param msg The received message.
210
- */
211
- protected handleMsg(msg: OpaqueTransportMessage): void;
212
- /**
213
- * Adds a listener to this transport.
214
- * @param the type of event to listen for
215
- * @param handler The message handler to add.
216
- */
217
- addEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
218
- /**
219
- * Removes a listener from this transport.
220
- * @param the type of event to un-listen on
221
- * @param handler The message handler to remove.
222
- */
223
- removeEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
224
- /**
225
- * Sends a message over this transport, delegating to the appropriate connection to actually
226
- * send the message.
227
- * @param msg The message to send.
228
- * @returns The ID of the sent message or undefined if it wasn't sent
229
- */
230
- send(to: TransportClientId, msg: PartialTransportMessage): string | undefined;
231
- sendCloseStream(to: TransportClientId, streamId: string): string | undefined;
232
- protected protocolError(type: ProtocolErrorType, message: string): void;
233
- /**
234
- * Default close implementation for transports. You should override this in the downstream
235
- * implementation if you need to do any additional cleanup and call super.close() at the end.
236
- * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
237
- */
238
- close(): void;
239
- /**
240
- * Default destroy implementation for transports. You should override this in the downstream
241
- * implementation if you need to do any additional cleanup and call super.destroy() at the end.
242
- * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
243
- */
244
- destroy(): void;
245
- }
246
- declare abstract class ClientTransport<ConnType extends Connection> extends Transport<ConnType> {
247
- /**
248
- * The options for this transport.
249
- */
250
- protected options: ClientTransportOptions;
251
- /**
252
- * The map of reconnect promises for each client ID.
253
- */
254
- inflightConnectionPromises: Map<TransportClientId, Promise<ConnType>>;
255
- retryBudget: LeakyBucketRateLimit;
256
- /**
257
- * A flag indicating whether the transport should automatically reconnect
258
- * when a connection is dropped.
259
- * Realistically, this should always be true for clients unless you are writing
260
- * tests or a special case where you don't want to reconnect.
261
- */
262
- reconnectOnConnectionDrop: boolean;
263
- /**
264
- * Optional handshake options for this client.
265
- */
266
- handshakeExtensions?: ClientHandshakeOptions;
267
- constructor(clientId: TransportClientId, providedOptions?: ProvidedClientTransportOptions);
268
- extendHandshake(options: ClientHandshakeOptions): void;
269
- protected handleConnection(conn: ConnType, to: TransportClientId): void;
270
- receiveHandshakeResponseMessage(data: Uint8Array, conn: ConnType): Session<ConnType> | false;
271
- /**
272
- * Abstract method that creates a new {@link Connection} object.
273
- * This should call {@link handleConnection} when the connection is created.
274
- * The downstream client implementation needs to implement this.
275
- *
276
- * @param to The client ID of the node to connect to.
277
- * @returns The new connection object.
278
- */
279
- protected abstract createNewOutgoingConnection(to: TransportClientId): Promise<ConnType>;
280
- /**
281
- * Manually attempts to connect to a client.
282
- * @param to The client ID of the node to connect to.
283
- */
284
- connect(to: TransportClientId): Promise<void>;
285
- protected deleteSession(session: Session<ConnType>): void;
286
- protected sendHandshake(to: TransportClientId, conn: ConnType): Promise<boolean>;
287
- close(): void;
288
- }
289
- declare abstract class ServerTransport<ConnType extends Connection> extends Transport<ConnType> {
290
- /**
291
- * The options for this transport.
292
- */
293
- protected options: ServerTransportOptions;
294
- /**
295
- * Optional handshake options for the server.
296
- */
297
- handshakeExtensions?: ServerHandshakeOptions;
298
- /**
299
- * A map of session handshake data for each session.
300
- */
301
- sessionHandshakeMetadata: WeakMap<Session<ConnType>, ParsedMetadata>;
302
- constructor(clientId: TransportClientId, providedOptions?: ProvidedServerTransportOptions);
303
- extendHandshake(options: ServerHandshakeOptions): void;
304
- protected handleConnection(conn: ConnType): void;
305
- private validateHandshakeMetadata;
306
- receiveHandshakeRequestMessage(data: Uint8Array, conn: ConnType): Promise<Session<ConnType> | false>;
307
- }
308
-
309
- export { ClientHandshakeOptions, ClientTransport, ProvidedClientTransportOptions as ClientTransportOptions, Connection, EventHandler, EventMap, EventTypes, OpaqueTransportMessage, ProtocolError, ProtocolErrorType, ServerHandshakeOptions, ServerTransport, ProvidedServerTransportOptions as ServerTransportOptions, Session, Transport, TransportClientId, ProvidedTransportOptions as TransportOptions, TransportStatus };
@@ -1,309 +1,6 @@
1
- import { C as Codec } from '../types-3e5768ec.js';
2
- import { O as OpaqueTransportMessage, C as Connection, b as Session, T as TransportClientId, m as PropagationContext, P as PartialTransportMessage, g as ClientHandshakeOptions, c as ServerHandshakeOptions, d as ParsedMetadata, S as SessionOptions } from '../index-a009d7f6.js';
3
- export { i as OpaqueTransportMessageSchema, j as TransportMessage, h as TransportMessageSchema, l as isStreamClose, k as isStreamOpen } from '../index-a009d7f6.js';
4
- import '@sinclair/typebox/value';
1
+ export { c as ClientTransport, P as ClientTransportOptions, C as Connection, n as EventHandler, E as EventMap, m as EventTypes, o as ProtocolError, p as ProtocolErrorType, d as ServerTransport, e as ServerTransportOptions, b as Session, T as Transport, k as TransportOptions, l as TransportStatus } from '../transport-4a5e288a.js';
2
+ export { O as OpaqueTransportMessage, e as OpaqueTransportMessageSchema, T as TransportClientId, c as TransportMessage, d as TransportMessageSchema, f as isStreamClose, i as isStreamOpen } from '../index-926aea33.js';
3
+ import '../types-3e5768ec.js';
5
4
  import '@sinclair/typebox';
5
+ import '@sinclair/typebox/value';
6
6
  import '@opentelemetry/api';
7
-
8
- type ConnectionStatus = 'connect' | 'disconnect';
9
- declare const ProtocolError: {
10
- readonly RetriesExceeded: "conn_retry_exceeded";
11
- readonly HandshakeFailed: "handshake_failed";
12
- readonly UseAfterDestroy: "use_after_destroy";
13
- readonly MessageOrderingViolated: "message_ordering_violated";
14
- };
15
- type ProtocolErrorType = (typeof ProtocolError)[keyof typeof ProtocolError];
16
- interface EventMap {
17
- message: OpaqueTransportMessage;
18
- connectionStatus: {
19
- status: ConnectionStatus;
20
- conn: Connection;
21
- };
22
- sessionStatus: {
23
- status: ConnectionStatus;
24
- session: Session<Connection>;
25
- };
26
- protocolError: {
27
- type: ProtocolErrorType;
28
- message: string;
29
- };
30
- }
31
- type EventTypes = keyof EventMap;
32
- type EventHandler<K extends EventTypes> = (event: EventMap[K]) => unknown;
33
- declare class EventDispatcher<T extends EventTypes> {
34
- private eventListeners;
35
- numberOfListeners<K extends T>(eventType: K): number;
36
- addEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
37
- removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
38
- dispatchEvent<K extends T>(eventType: K, event: EventMap[K]): void;
39
- }
40
-
41
- /**
42
- * Options to control the backoff and retry behavior of the client transport's connection behaviour.
43
- *
44
- * River implements exponential backoff with jitter to prevent flooding the server
45
- * when there's an issue with connection establishment.
46
- *
47
- * The backoff is calculated via the following:
48
- * backOff = min(jitter + {@link baseIntervalMs} * 2 ^ budget_consumed, {@link maxBackoffMs})
49
- *
50
- * We use a leaky bucket rate limit with a budget of {@link attemptBudgetCapacity} reconnection attempts.
51
- * Budget only starts to restore after a successful handshake at a rate of one budget per {@link budgetRestoreIntervalMs}.
52
- */
53
- interface ConnectionRetryOptions {
54
- /**
55
- * The base interval to wait before retrying a connection.
56
- */
57
- baseIntervalMs: number;
58
- /**
59
- * The maximum random jitter to add to the total backoff time.
60
- */
61
- maxJitterMs: number;
62
- /**
63
- * The maximum amount of time to wait before retrying a connection.
64
- * This does not include the jitter.
65
- */
66
- maxBackoffMs: number;
67
- /**
68
- * The max number of times to attempt a connection before a successful handshake.
69
- * This persists across connections but starts restoring budget after a successful handshake.
70
- * The restoration interval depends on {@link budgetRestoreIntervalMs}
71
- */
72
- attemptBudgetCapacity: number;
73
- /**
74
- * After a successful connection attempt, how long to wait before we restore a single budget.
75
- */
76
- budgetRestoreIntervalMs: number;
77
- }
78
- declare class LeakyBucketRateLimit {
79
- private budgetConsumed;
80
- private intervalHandles;
81
- private readonly options;
82
- constructor(options: ConnectionRetryOptions);
83
- getBackoffMs(user: TransportClientId): number;
84
- get totalBudgetRestoreTime(): number;
85
- consumeBudget(user: TransportClientId): void;
86
- getBudgetConsumed(user: TransportClientId): number;
87
- hasBudget(user: TransportClientId): boolean;
88
- startRestoringBudget(user: TransportClientId): void;
89
- private stopLeak;
90
- close(): void;
91
- }
92
-
93
- /**
94
- * Represents the possible states of a transport.
95
- * @property {'open'} open - The transport is open and operational (note that this doesn't mean it is actively connected)
96
- * @property {'closed'} closed - The transport is closed and not operational, but can be reopened.
97
- * @property {'destroyed'} destroyed - The transport is permanently destroyed and cannot be reopened.
98
- */
99
- type TransportStatus = 'open' | 'closed' | 'destroyed';
100
- type TransportOptions = SessionOptions;
101
- type ProvidedTransportOptions = Partial<TransportOptions>;
102
- type ClientTransportOptions = TransportOptions & ConnectionRetryOptions;
103
- type ProvidedClientTransportOptions = Partial<ClientTransportOptions>;
104
- type ServerTransportOptions = TransportOptions;
105
- type ProvidedServerTransportOptions = Partial<ServerTransportOptions>;
106
- /**
107
- * Transports manage the lifecycle (creation/deletion) of sessions and connections. Its responsibilities include:
108
- *
109
- * 1) Constructing a new {@link Session} and {@link Connection} on {@link TransportMessage}s from new clients.
110
- * After constructing the {@link Connection}, {@link onConnect} is called which adds it to the connection map.
111
- * 2) Delegating message listening of the connection to the newly created {@link Connection}.
112
- * From this point on, the {@link Connection} is responsible for *reading* and *writing*
113
- * messages from the connection.
114
- * 3) When a connection is closed, the {@link Transport} calls {@link onDisconnect} which closes the
115
- * connection via {@link Connection.close} and removes it from the {@link connections} map.
116
-
117
- *
118
- * ```plaintext
119
- * ▲
120
- * incoming │
121
- * messages │
122
- * ▼
123
- * ┌─────────────┐ 1:N ┌───────────┐ 1:1* ┌────────────┐
124
- * │ Transport │ ◄─────► │ Session │ ◄─────► │ Connection │
125
- * └─────────────┘ └───────────┘ └────────────┘
126
- * ▲ * (may or may not be initialized yet)
127
- * │
128
- * ▼
129
- * ┌───────────┐
130
- * │ Message │
131
- * │ Listeners │
132
- * └───────────┘
133
- * ```
134
- * @abstract
135
- */
136
- declare abstract class Transport<ConnType extends Connection> {
137
- /**
138
- * A flag indicating whether the transport has been destroyed.
139
- * A destroyed transport will not attempt to reconnect and cannot be used again.
140
- */
141
- state: TransportStatus;
142
- /**
143
- * The {@link Codec} used to encode and decode messages.
144
- */
145
- codec: Codec;
146
- /**
147
- * The client ID of this transport.
148
- */
149
- clientId: TransportClientId;
150
- /**
151
- * The map of {@link Session}s managed by this transport.
152
- */
153
- sessions: Map<TransportClientId, Session<ConnType>>;
154
- /**
155
- * The map of {@link Connection}s managed by this transport.
156
- */
157
- get connections(): Map<string, ConnType>;
158
- /**
159
- * The event dispatcher for handling events of type EventTypes.
160
- */
161
- eventDispatcher: EventDispatcher<EventTypes>;
162
- /**
163
- * The options for this transport.
164
- */
165
- protected options: TransportOptions;
166
- /**
167
- * Creates a new Transport instance.
168
- * This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
169
- * @param codec The codec used to encode and decode messages.
170
- * @param clientId The client ID of this transport.
171
- */
172
- constructor(clientId: TransportClientId, providedOptions?: ProvidedTransportOptions);
173
- /**
174
- * This is called immediately after a new connection is established and we
175
- * may or may not know the identity of the connected client.
176
- * It should attach all the necessary listeners to the connection for lifecycle
177
- * events (i.e. data, close, error)
178
- *
179
- * This method is implemented by {@link ClientTransport} and {@link ServerTransport}.
180
- */
181
- protected abstract handleConnection(conn: ConnType, to: TransportClientId): void;
182
- /**
183
- * Called when a new connection is established
184
- * and we know the identity of the connected client.
185
- * @param conn The connection object.
186
- */
187
- protected onConnect(conn: ConnType, connectedTo: TransportClientId, session: Session<ConnType>, isReconnect: boolean): void;
188
- protected createSession(to: TransportClientId, conn?: ConnType, propagationCtx?: PropagationContext): Session<ConnType>;
189
- protected getOrCreateSession(to: TransportClientId, conn?: ConnType, sessionId?: string, propagationCtx?: PropagationContext): {
190
- session: Session<ConnType>;
191
- isReconnect: boolean;
192
- };
193
- protected deleteSession(session: Session<ConnType>): void;
194
- /**
195
- * The downstream implementation needs to call this when a connection is closed.
196
- * @param conn The connection object.
197
- * @param connectedTo The peer we are connected to.
198
- */
199
- protected onDisconnect(conn: ConnType, session: Session<ConnType>): void;
200
- /**
201
- * Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
202
- * @param msg The message to parse.
203
- * @returns The parsed message, or null if the message is malformed or invalid.
204
- */
205
- protected parseMsg(msg: Uint8Array): OpaqueTransportMessage | null;
206
- /**
207
- * Called when a message is received by this transport.
208
- * You generally shouldn't need to override this in downstream transport implementations.
209
- * @param msg The received message.
210
- */
211
- protected handleMsg(msg: OpaqueTransportMessage): void;
212
- /**
213
- * Adds a listener to this transport.
214
- * @param the type of event to listen for
215
- * @param handler The message handler to add.
216
- */
217
- addEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
218
- /**
219
- * Removes a listener from this transport.
220
- * @param the type of event to un-listen on
221
- * @param handler The message handler to remove.
222
- */
223
- removeEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
224
- /**
225
- * Sends a message over this transport, delegating to the appropriate connection to actually
226
- * send the message.
227
- * @param msg The message to send.
228
- * @returns The ID of the sent message or undefined if it wasn't sent
229
- */
230
- send(to: TransportClientId, msg: PartialTransportMessage): string | undefined;
231
- sendCloseStream(to: TransportClientId, streamId: string): string | undefined;
232
- protected protocolError(type: ProtocolErrorType, message: string): void;
233
- /**
234
- * Default close implementation for transports. You should override this in the downstream
235
- * implementation if you need to do any additional cleanup and call super.close() at the end.
236
- * Closes the transport. Any messages sent while the transport is closed will be silently discarded.
237
- */
238
- close(): void;
239
- /**
240
- * Default destroy implementation for transports. You should override this in the downstream
241
- * implementation if you need to do any additional cleanup and call super.destroy() at the end.
242
- * Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
243
- */
244
- destroy(): void;
245
- }
246
- declare abstract class ClientTransport<ConnType extends Connection> extends Transport<ConnType> {
247
- /**
248
- * The options for this transport.
249
- */
250
- protected options: ClientTransportOptions;
251
- /**
252
- * The map of reconnect promises for each client ID.
253
- */
254
- inflightConnectionPromises: Map<TransportClientId, Promise<ConnType>>;
255
- retryBudget: LeakyBucketRateLimit;
256
- /**
257
- * A flag indicating whether the transport should automatically reconnect
258
- * when a connection is dropped.
259
- * Realistically, this should always be true for clients unless you are writing
260
- * tests or a special case where you don't want to reconnect.
261
- */
262
- reconnectOnConnectionDrop: boolean;
263
- /**
264
- * Optional handshake options for this client.
265
- */
266
- handshakeExtensions?: ClientHandshakeOptions;
267
- constructor(clientId: TransportClientId, providedOptions?: ProvidedClientTransportOptions);
268
- extendHandshake(options: ClientHandshakeOptions): void;
269
- protected handleConnection(conn: ConnType, to: TransportClientId): void;
270
- receiveHandshakeResponseMessage(data: Uint8Array, conn: ConnType): Session<ConnType> | false;
271
- /**
272
- * Abstract method that creates a new {@link Connection} object.
273
- * This should call {@link handleConnection} when the connection is created.
274
- * The downstream client implementation needs to implement this.
275
- *
276
- * @param to The client ID of the node to connect to.
277
- * @returns The new connection object.
278
- */
279
- protected abstract createNewOutgoingConnection(to: TransportClientId): Promise<ConnType>;
280
- /**
281
- * Manually attempts to connect to a client.
282
- * @param to The client ID of the node to connect to.
283
- */
284
- connect(to: TransportClientId): Promise<void>;
285
- protected deleteSession(session: Session<ConnType>): void;
286
- protected sendHandshake(to: TransportClientId, conn: ConnType): Promise<boolean>;
287
- close(): void;
288
- }
289
- declare abstract class ServerTransport<ConnType extends Connection> extends Transport<ConnType> {
290
- /**
291
- * The options for this transport.
292
- */
293
- protected options: ServerTransportOptions;
294
- /**
295
- * Optional handshake options for the server.
296
- */
297
- handshakeExtensions?: ServerHandshakeOptions;
298
- /**
299
- * A map of session handshake data for each session.
300
- */
301
- sessionHandshakeMetadata: WeakMap<Session<ConnType>, ParsedMetadata>;
302
- constructor(clientId: TransportClientId, providedOptions?: ProvidedServerTransportOptions);
303
- extendHandshake(options: ServerHandshakeOptions): void;
304
- protected handleConnection(conn: ConnType): void;
305
- private validateHandshakeMetadata;
306
- receiveHandshakeRequestMessage(data: Uint8Array, conn: ConnType): Promise<Session<ConnType> | false>;
307
- }
308
-
309
- export { ClientHandshakeOptions, ClientTransport, ProvidedClientTransportOptions as ClientTransportOptions, Connection, EventHandler, EventMap, EventTypes, OpaqueTransportMessage, ProtocolError, ProtocolErrorType, ServerHandshakeOptions, ServerTransport, ProvidedServerTransportOptions as ServerTransportOptions, Session, Transport, TransportClientId, ProvidedTransportOptions as TransportOptions, TransportStatus };
@@ -5,11 +5,11 @@ import {
5
5
  ServerTransport,
6
6
  Session,
7
7
  Transport
8
- } from "../chunk-IKT6A32W.js";
8
+ } from "../chunk-BHNINW7F.js";
9
9
  import {
10
10
  OpaqueTransportMessageSchema,
11
11
  TransportMessageSchema
12
- } from "../chunk-XZV3HOQW.js";
12
+ } from "../chunk-CHOAXTQF.js";
13
13
  import "../chunk-SPP7ACQN.js";
14
14
  import "../chunk-3AW3IXVD.js";
15
15
  export {