@replit/river 0.11.0 → 0.12.1
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.
- package/README.md +28 -8
- package/dist/{builder-1f26296b.d.ts → builder-c593de11.d.ts} +14 -13
- package/dist/{chunk-3JGVFWKQ.js → chunk-55XUAPC6.js} +205 -180
- package/dist/{chunk-R6H2BIMC.js → chunk-GZ7HCLLM.js} +31 -7
- package/dist/{chunk-T7M7OKPE.js → chunk-H4BYJELI.js} +5 -1
- package/dist/{chunk-GKPT5YQE.js → chunk-IIBVKYDB.js} +6 -34
- package/dist/chunk-M6LY25P2.js +47 -0
- package/dist/chunk-QEYN2Z6O.js +726 -0
- package/dist/chunk-RDTTKCGV.js +40 -0
- package/dist/chunk-TKINU53F.js +44 -0
- package/dist/chunk-XFFS4UOD.js +127 -0
- package/dist/codec/index.cjs +13 -7
- package/dist/codec/index.js +2 -4
- package/dist/connection-bf7811aa.d.ts +17 -0
- package/dist/connection-d880aa4a.d.ts +18 -0
- package/dist/connection-eb10d250.d.ts +15 -0
- package/dist/index-0c0a69f6.d.ts +440 -0
- package/dist/logging/index.cjs +8 -3
- package/dist/logging/index.d.cts +6 -1
- package/dist/logging/index.d.ts +6 -1
- package/dist/logging/index.js +5 -3
- package/dist/messageFraming-b200ef25.d.ts +20 -0
- package/dist/router/index.cjs +250 -211
- package/dist/router/index.d.cts +6 -7
- package/dist/router/index.d.ts +6 -7
- package/dist/router/index.js +3 -3
- package/dist/transport/impls/stdio/client.cjs +909 -0
- package/dist/transport/impls/stdio/client.d.cts +27 -0
- package/dist/transport/impls/stdio/client.d.ts +27 -0
- package/dist/transport/impls/stdio/client.js +42 -0
- package/dist/transport/impls/stdio/server.cjs +883 -0
- package/dist/transport/impls/stdio/server.d.cts +25 -0
- package/dist/transport/impls/stdio/server.d.ts +25 -0
- package/dist/transport/impls/stdio/server.js +33 -0
- package/dist/transport/impls/uds/client.cjs +911 -0
- package/dist/transport/impls/uds/client.d.cts +16 -0
- package/dist/transport/impls/uds/client.d.ts +16 -0
- package/dist/transport/impls/uds/client.js +44 -0
- package/dist/transport/impls/uds/server.cjs +885 -0
- package/dist/transport/impls/uds/server.d.cts +16 -0
- package/dist/transport/impls/uds/server.d.ts +16 -0
- package/dist/transport/impls/uds/server.js +39 -0
- package/dist/transport/impls/ws/client.cjs +612 -249
- package/dist/transport/impls/ws/client.d.cts +6 -21
- package/dist/transport/impls/ws/client.d.ts +6 -21
- package/dist/transport/impls/ws/client.js +83 -7
- package/dist/transport/impls/ws/server.cjs +565 -196
- package/dist/transport/impls/ws/server.d.cts +6 -10
- package/dist/transport/impls/ws/server.d.ts +6 -10
- package/dist/transport/impls/ws/server.js +31 -8
- package/dist/transport/index.cjs +673 -130
- package/dist/transport/index.d.cts +3 -276
- package/dist/transport/index.d.ts +3 -276
- package/dist/transport/index.js +13 -10
- package/dist/util/testHelpers.cjs +40 -602
- package/dist/util/testHelpers.d.cts +18 -37
- package/dist/util/testHelpers.d.ts +18 -37
- package/dist/util/testHelpers.js +27 -47
- package/package.json +29 -14
- package/dist/chunk-5IC5XMWK.js +0 -140
- package/dist/chunk-L7D75G4K.js +0 -29
- package/dist/chunk-LQXPKF3A.js +0 -282
- package/dist/chunk-PJ2EUO7O.js +0 -63
- package/dist/chunk-WVT5QXMZ.js +0 -20
- package/dist/chunk-ZE4MX7DF.js +0 -75
- package/dist/connection-2529fc14.d.ts +0 -10
- package/dist/connection-316d6e3a.d.ts +0 -10
- package/dist/connection-8e19874c.d.ts +0 -11
- package/dist/connection-f7688cc1.d.ts +0 -11
- package/dist/transport/impls/stdio/stdio.cjs +0 -518
- package/dist/transport/impls/stdio/stdio.d.cts +0 -26
- package/dist/transport/impls/stdio/stdio.d.ts +0 -26
- package/dist/transport/impls/stdio/stdio.js +0 -70
- package/dist/transport/impls/unixsocket/client.cjs +0 -516
- package/dist/transport/impls/unixsocket/client.d.cts +0 -16
- package/dist/transport/impls/unixsocket/client.d.ts +0 -16
- package/dist/transport/impls/unixsocket/client.js +0 -67
- package/dist/transport/impls/unixsocket/server.cjs +0 -520
- package/dist/transport/impls/unixsocket/server.d.cts +0 -18
- package/dist/transport/impls/unixsocket/server.d.ts +0 -18
- package/dist/transport/impls/unixsocket/server.js +0 -73
- /package/dist/{chunk-ORAG7IAU.js → chunk-5IZ2UHWV.js} +0 -0
|
@@ -1,276 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Generic Typebox schema for a transport message.
|
|
7
|
-
* @template T The type of the payload.
|
|
8
|
-
* @param {T} t The payload schema.
|
|
9
|
-
* @returns The transport message schema.
|
|
10
|
-
*/
|
|
11
|
-
declare const TransportMessageSchema: <T extends TSchema>(t: T) => _sinclair_typebox.TObject<{
|
|
12
|
-
id: _sinclair_typebox.TString;
|
|
13
|
-
from: _sinclair_typebox.TString;
|
|
14
|
-
to: _sinclair_typebox.TString;
|
|
15
|
-
serviceName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
16
|
-
procedureName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
17
|
-
streamId: _sinclair_typebox.TString;
|
|
18
|
-
controlFlags: _sinclair_typebox.TInteger;
|
|
19
|
-
payload: T;
|
|
20
|
-
}>;
|
|
21
|
-
/**
|
|
22
|
-
* Defines the schema for an opaque transport message that is agnostic to any
|
|
23
|
-
* procedure/service.
|
|
24
|
-
* @returns The transport message schema.
|
|
25
|
-
*/
|
|
26
|
-
declare const OpaqueTransportMessageSchema: _sinclair_typebox.TObject<{
|
|
27
|
-
id: _sinclair_typebox.TString;
|
|
28
|
-
from: _sinclair_typebox.TString;
|
|
29
|
-
to: _sinclair_typebox.TString;
|
|
30
|
-
serviceName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
31
|
-
procedureName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
32
|
-
streamId: _sinclair_typebox.TString;
|
|
33
|
-
controlFlags: _sinclair_typebox.TInteger;
|
|
34
|
-
payload: _sinclair_typebox.TUnknown;
|
|
35
|
-
}>;
|
|
36
|
-
/**
|
|
37
|
-
* Represents a transport message. This is the same type as {@link TransportMessageSchema} but
|
|
38
|
-
* we can't statically infer generics from generic Typebox schemas so we have to define it again here.
|
|
39
|
-
*
|
|
40
|
-
* TypeScript can't enforce types when a bitmask is involved, so these are the semantics of
|
|
41
|
-
* `controlFlags`:
|
|
42
|
-
* * If `controlFlags & StreamOpenBit == StreamOpenBit`, `streamId` must be set to a unique value
|
|
43
|
-
* (suggestion: use `nanoid`).
|
|
44
|
-
* * If `controlFlags & StreamOpenBit == StreamOpenBit`, `serviceName` and `procedureName` must be set.
|
|
45
|
-
* * If `controlFlags & StreamClosedBit == StreamClosedBit` and the kind is `stream` or `subscription`,
|
|
46
|
-
* `payload` should be discarded (usually contains a control message).
|
|
47
|
-
* @template Payload The type of the payload.
|
|
48
|
-
*/
|
|
49
|
-
type TransportMessage<Payload extends Record<string, unknown> | unknown = Record<string, unknown>> = {
|
|
50
|
-
id: string;
|
|
51
|
-
from: string;
|
|
52
|
-
to: string;
|
|
53
|
-
serviceName?: string;
|
|
54
|
-
procedureName?: string;
|
|
55
|
-
streamId: string;
|
|
56
|
-
controlFlags: number;
|
|
57
|
-
payload: Payload;
|
|
58
|
-
};
|
|
59
|
-
type MessageId = string;
|
|
60
|
-
/**
|
|
61
|
-
* A type alias for a transport message with an opaque payload.
|
|
62
|
-
* @template T - The type of the opaque payload.
|
|
63
|
-
*/
|
|
64
|
-
type OpaqueTransportMessage = TransportMessage<unknown>;
|
|
65
|
-
type TransportClientId = string;
|
|
66
|
-
/**
|
|
67
|
-
* Creates a transport message with the given parameters. You shouldn't need to call this manually unless
|
|
68
|
-
* you're writing a test.
|
|
69
|
-
* @param from The sender of the message.
|
|
70
|
-
* @param to The intended recipient of the message.
|
|
71
|
-
* @param service The name of the service the message is intended for.
|
|
72
|
-
* @param proc The name of the procedure the message is intended for.
|
|
73
|
-
* @param stream The ID of the stream the message is intended for.
|
|
74
|
-
* @param payload The payload of the message.
|
|
75
|
-
* @returns A TransportMessage object with the given parameters.
|
|
76
|
-
*/
|
|
77
|
-
declare function msg<Payload extends object>(from: string, to: string, streamId: string, payload: Payload, serviceName?: string, procedureName?: string): TransportMessage<Payload>;
|
|
78
|
-
/**
|
|
79
|
-
* Creates a new transport message as a response to the given message.
|
|
80
|
-
* @param msg The original message to respond to.
|
|
81
|
-
* @param response The payload of the response message.
|
|
82
|
-
* @returns A new transport message with appropriate to, from, and payload fields
|
|
83
|
-
*/
|
|
84
|
-
declare function reply<Payload extends object>(msg: OpaqueTransportMessage, response: Payload): TransportMessage<Payload>;
|
|
85
|
-
/**
|
|
86
|
-
* Checks if the given control flag (usually found in msg.controlFlag) is a stream open message.
|
|
87
|
-
* @param controlFlag - The control flag to check.
|
|
88
|
-
* @returns True if the control flag contains the StreamOpenBit, false otherwise.
|
|
89
|
-
*/
|
|
90
|
-
declare function isStreamOpen(controlFlag: number): boolean;
|
|
91
|
-
/**
|
|
92
|
-
* Checks if the given control flag (usually found in msg.controlFlag) is a stream close message.
|
|
93
|
-
* @param controlFlag - The control flag to check.
|
|
94
|
-
* @returns True if the control flag contains the StreamCloseBit, false otherwise.
|
|
95
|
-
*/
|
|
96
|
-
declare function isStreamClose(controlFlag: number): boolean;
|
|
97
|
-
|
|
98
|
-
interface EventMap {
|
|
99
|
-
message: OpaqueTransportMessage;
|
|
100
|
-
connectionStatus: {
|
|
101
|
-
status: 'connect' | 'disconnect';
|
|
102
|
-
conn: Connection;
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
type EventTypes = keyof EventMap;
|
|
106
|
-
type EventHandler<K extends EventTypes> = (event: EventMap[K]) => void;
|
|
107
|
-
declare class EventDispatcher<T extends EventTypes> {
|
|
108
|
-
private eventListeners;
|
|
109
|
-
numberOfListeners<K extends T>(eventType: K): number;
|
|
110
|
-
addEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
|
|
111
|
-
removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
|
|
112
|
-
dispatchEvent<K extends T>(eventType: K, event: EventMap[K]): void;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* A 1:1 connection between two transports. Once this is created,
|
|
117
|
-
* the {@link Connection} is expected to take over responsibility for
|
|
118
|
-
* reading and writing messages from the underlying connection.
|
|
119
|
-
*
|
|
120
|
-
* 1) Messages received on the {@link Connection} are dispatched back to the {@link Transport}
|
|
121
|
-
* via {@link Transport.onMessage}. The {@link Transport} then notifies any registered message listeners.
|
|
122
|
-
* 2) When {@link Transport.send}(msg) is called, the transport looks up the appropriate
|
|
123
|
-
* connection in the {@link connections} map via `msg.to` and calls {@link send}(bytes)
|
|
124
|
-
* so the connection can send it.
|
|
125
|
-
*/
|
|
126
|
-
declare abstract class Connection {
|
|
127
|
-
connectedTo: TransportClientId;
|
|
128
|
-
transport: Transport<Connection>;
|
|
129
|
-
constructor(transport: Transport<Connection>, connectedTo: TransportClientId);
|
|
130
|
-
abstract send(msg: Uint8Array): boolean;
|
|
131
|
-
abstract close(): void;
|
|
132
|
-
}
|
|
133
|
-
type TransportStatus = 'open' | 'closed' | 'destroyed';
|
|
134
|
-
/**
|
|
135
|
-
* Transports manage the lifecycle (creation/deletion) of connections. Its responsibilities include:
|
|
136
|
-
*
|
|
137
|
-
* 1) Constructing a new {@link Connection} on {@link TransportMessage}s from new clients.
|
|
138
|
-
* After constructing the {@link Connection}, {@link onConnect} is called which adds it to the connection map.
|
|
139
|
-
* 2) Delegating message listening of the connection to the newly created {@link Connection}.
|
|
140
|
-
* From this point on, the {@link Connection} is responsible for *reading* and *writing*
|
|
141
|
-
* messages from the connection.
|
|
142
|
-
* 3) When a connection is closed, the {@link Transport} calls {@link onDisconnect} which closes the
|
|
143
|
-
* connection via {@link Connection.close} and removes it from the {@link connections} map.
|
|
144
|
-
|
|
145
|
-
*
|
|
146
|
-
* ```plaintext
|
|
147
|
-
* ▲
|
|
148
|
-
* incoming │
|
|
149
|
-
* messages │
|
|
150
|
-
* ▼
|
|
151
|
-
* ┌─────────────┐ 1:N ┌────────────┐
|
|
152
|
-
* │ Transport │ ◄─────► │ Connection │
|
|
153
|
-
* └─────────────┘ └────────────┘
|
|
154
|
-
* ▲
|
|
155
|
-
* │
|
|
156
|
-
* ▼
|
|
157
|
-
* ┌───────────┐
|
|
158
|
-
* │ Message │
|
|
159
|
-
* │ Listeners │
|
|
160
|
-
* └───────────┘
|
|
161
|
-
* ```
|
|
162
|
-
* @abstract
|
|
163
|
-
*/
|
|
164
|
-
declare abstract class Transport<ConnType extends Connection> {
|
|
165
|
-
/**
|
|
166
|
-
* A flag indicating whether the transport has been destroyed.
|
|
167
|
-
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
168
|
-
*/
|
|
169
|
-
state: TransportStatus;
|
|
170
|
-
/**
|
|
171
|
-
* The {@link Codec} used to encode and decode messages.
|
|
172
|
-
*/
|
|
173
|
-
codec: Codec;
|
|
174
|
-
/**
|
|
175
|
-
* The client ID of this transport.
|
|
176
|
-
*/
|
|
177
|
-
clientId: TransportClientId;
|
|
178
|
-
/**
|
|
179
|
-
* An array of message IDs that are waiting to be sent over the WebSocket connection.
|
|
180
|
-
* This builds up if the WebSocket is down for a period of time.
|
|
181
|
-
*/
|
|
182
|
-
sendQueue: Map<TransportClientId, Array<MessageId>>;
|
|
183
|
-
/**
|
|
184
|
-
* The buffer of messages that have been sent but not yet acknowledged.
|
|
185
|
-
*/
|
|
186
|
-
sendBuffer: Map<MessageId, OpaqueTransportMessage>;
|
|
187
|
-
/**
|
|
188
|
-
* The map of {@link Connection}s managed by this transport.
|
|
189
|
-
*/
|
|
190
|
-
connections: Map<TransportClientId, ConnType>;
|
|
191
|
-
/**
|
|
192
|
-
* The event dispatcher for handling events of type EventTypes.
|
|
193
|
-
*/
|
|
194
|
-
eventDispatcher: EventDispatcher<EventTypes>;
|
|
195
|
-
/**
|
|
196
|
-
* Creates a new Transport instance.
|
|
197
|
-
* This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
|
|
198
|
-
* @param codec The codec used to encode and decode messages.
|
|
199
|
-
* @param clientId The client ID of this transport.
|
|
200
|
-
*/
|
|
201
|
-
constructor(codec: Codec, clientId: TransportClientId);
|
|
202
|
-
/**
|
|
203
|
-
* Abstract method that creates a new {@link Connection} object.
|
|
204
|
-
* This should call {@link onConnect} when the connection is established.
|
|
205
|
-
* The downstream implementation needs to implement this. If the downstream
|
|
206
|
-
* transport cannot make new outgoing connections (e.g. a server transport),
|
|
207
|
-
* it is ok to log an error and return.
|
|
208
|
-
*
|
|
209
|
-
* Consumers of river should never need to call this directly.
|
|
210
|
-
* Instead, look for a `reopen` method on the transport.
|
|
211
|
-
*
|
|
212
|
-
* @param to The client ID of the node to connect to.
|
|
213
|
-
* @returns The new connection object.
|
|
214
|
-
*/
|
|
215
|
-
abstract createNewConnection(to: TransportClientId): Promise<void>;
|
|
216
|
-
/**
|
|
217
|
-
* The downstream implementation needs to call this when a new connection is established.
|
|
218
|
-
* @param conn The connection object.
|
|
219
|
-
*/
|
|
220
|
-
onConnect(conn: ConnType): void;
|
|
221
|
-
/**
|
|
222
|
-
* The downstream implementation needs to call this when a connection is closed.
|
|
223
|
-
* @param conn The connection object.
|
|
224
|
-
*/
|
|
225
|
-
onDisconnect(conn: ConnType): void;
|
|
226
|
-
/**
|
|
227
|
-
* Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
|
|
228
|
-
* @param msg The message to handle.
|
|
229
|
-
*/
|
|
230
|
-
onMessage(msg: Uint8Array): void;
|
|
231
|
-
/**
|
|
232
|
-
* Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
|
|
233
|
-
* @param msg The message to parse.
|
|
234
|
-
* @returns The parsed message, or null if the message is malformed or invalid.
|
|
235
|
-
*/
|
|
236
|
-
protected parseMsg(msg: Uint8Array): OpaqueTransportMessage | null;
|
|
237
|
-
/**
|
|
238
|
-
* Called when a message is received by this transport.
|
|
239
|
-
* You generally shouldn't need to override this in downstream transport implementations.
|
|
240
|
-
* @param msg The received message.
|
|
241
|
-
*/
|
|
242
|
-
protected handleMsg(msg: OpaqueTransportMessage | null): void;
|
|
243
|
-
/**
|
|
244
|
-
* Adds a listener to this transport.
|
|
245
|
-
* @param the type of event to listen for
|
|
246
|
-
* @param handler The message handler to add.
|
|
247
|
-
*/
|
|
248
|
-
addEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
|
|
249
|
-
/**
|
|
250
|
-
* Removes a listener from this transport.
|
|
251
|
-
* @param the type of event to unlisten on
|
|
252
|
-
* @param handler The message handler to remove.
|
|
253
|
-
*/
|
|
254
|
-
removeEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
|
|
255
|
-
/**
|
|
256
|
-
* Sends a message over this transport, delegating to the appropriate connection to actually
|
|
257
|
-
* send the message.
|
|
258
|
-
* @param msg The message to send.
|
|
259
|
-
* @returns The ID of the sent message.
|
|
260
|
-
*/
|
|
261
|
-
send(msg: OpaqueTransportMessage): MessageId;
|
|
262
|
-
/**
|
|
263
|
-
* Default close implementation for transports. You should override this in the downstream
|
|
264
|
-
* implementation if you need to do any additional cleanup and call super.close() at the end.
|
|
265
|
-
* Closes the transport. Any messages sent while the transport is closed will be silently discarded.
|
|
266
|
-
*/
|
|
267
|
-
close(): Promise<void>;
|
|
268
|
-
/**
|
|
269
|
-
* Default destroy implementation for transports. You should override this in the downstream
|
|
270
|
-
* implementation if you need to do any additional cleanup and call super.destroy() at the end.
|
|
271
|
-
* Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
|
|
272
|
-
*/
|
|
273
|
-
destroy(): Promise<void>;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
export { Connection, MessageId, OpaqueTransportMessage, OpaqueTransportMessageSchema, Transport, TransportClientId, TransportMessage, TransportMessageSchema, isStreamClose, isStreamOpen, msg, reply };
|
|
1
|
+
export { a as ClientTransport, C as Connection, k as EventHandler, E as EventMap, j as EventTypes, O as OpaqueTransportMessage, f as OpaqueTransportMessageSchema, S as ServerTransport, d as Session, T as Transport, b as TransportClientId, g as TransportMessage, e as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-0c0a69f6.js';
|
|
2
|
+
import '../types-3e5768ec.js';
|
|
3
|
+
import '@sinclair/typebox';
|
|
@@ -1,276 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Generic Typebox schema for a transport message.
|
|
7
|
-
* @template T The type of the payload.
|
|
8
|
-
* @param {T} t The payload schema.
|
|
9
|
-
* @returns The transport message schema.
|
|
10
|
-
*/
|
|
11
|
-
declare const TransportMessageSchema: <T extends TSchema>(t: T) => _sinclair_typebox.TObject<{
|
|
12
|
-
id: _sinclair_typebox.TString;
|
|
13
|
-
from: _sinclair_typebox.TString;
|
|
14
|
-
to: _sinclair_typebox.TString;
|
|
15
|
-
serviceName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
16
|
-
procedureName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
17
|
-
streamId: _sinclair_typebox.TString;
|
|
18
|
-
controlFlags: _sinclair_typebox.TInteger;
|
|
19
|
-
payload: T;
|
|
20
|
-
}>;
|
|
21
|
-
/**
|
|
22
|
-
* Defines the schema for an opaque transport message that is agnostic to any
|
|
23
|
-
* procedure/service.
|
|
24
|
-
* @returns The transport message schema.
|
|
25
|
-
*/
|
|
26
|
-
declare const OpaqueTransportMessageSchema: _sinclair_typebox.TObject<{
|
|
27
|
-
id: _sinclair_typebox.TString;
|
|
28
|
-
from: _sinclair_typebox.TString;
|
|
29
|
-
to: _sinclair_typebox.TString;
|
|
30
|
-
serviceName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
31
|
-
procedureName: _sinclair_typebox.TOptional<_sinclair_typebox.TUnion<[_sinclair_typebox.TString, _sinclair_typebox.TNull]>>;
|
|
32
|
-
streamId: _sinclair_typebox.TString;
|
|
33
|
-
controlFlags: _sinclair_typebox.TInteger;
|
|
34
|
-
payload: _sinclair_typebox.TUnknown;
|
|
35
|
-
}>;
|
|
36
|
-
/**
|
|
37
|
-
* Represents a transport message. This is the same type as {@link TransportMessageSchema} but
|
|
38
|
-
* we can't statically infer generics from generic Typebox schemas so we have to define it again here.
|
|
39
|
-
*
|
|
40
|
-
* TypeScript can't enforce types when a bitmask is involved, so these are the semantics of
|
|
41
|
-
* `controlFlags`:
|
|
42
|
-
* * If `controlFlags & StreamOpenBit == StreamOpenBit`, `streamId` must be set to a unique value
|
|
43
|
-
* (suggestion: use `nanoid`).
|
|
44
|
-
* * If `controlFlags & StreamOpenBit == StreamOpenBit`, `serviceName` and `procedureName` must be set.
|
|
45
|
-
* * If `controlFlags & StreamClosedBit == StreamClosedBit` and the kind is `stream` or `subscription`,
|
|
46
|
-
* `payload` should be discarded (usually contains a control message).
|
|
47
|
-
* @template Payload The type of the payload.
|
|
48
|
-
*/
|
|
49
|
-
type TransportMessage<Payload extends Record<string, unknown> | unknown = Record<string, unknown>> = {
|
|
50
|
-
id: string;
|
|
51
|
-
from: string;
|
|
52
|
-
to: string;
|
|
53
|
-
serviceName?: string;
|
|
54
|
-
procedureName?: string;
|
|
55
|
-
streamId: string;
|
|
56
|
-
controlFlags: number;
|
|
57
|
-
payload: Payload;
|
|
58
|
-
};
|
|
59
|
-
type MessageId = string;
|
|
60
|
-
/**
|
|
61
|
-
* A type alias for a transport message with an opaque payload.
|
|
62
|
-
* @template T - The type of the opaque payload.
|
|
63
|
-
*/
|
|
64
|
-
type OpaqueTransportMessage = TransportMessage<unknown>;
|
|
65
|
-
type TransportClientId = string;
|
|
66
|
-
/**
|
|
67
|
-
* Creates a transport message with the given parameters. You shouldn't need to call this manually unless
|
|
68
|
-
* you're writing a test.
|
|
69
|
-
* @param from The sender of the message.
|
|
70
|
-
* @param to The intended recipient of the message.
|
|
71
|
-
* @param service The name of the service the message is intended for.
|
|
72
|
-
* @param proc The name of the procedure the message is intended for.
|
|
73
|
-
* @param stream The ID of the stream the message is intended for.
|
|
74
|
-
* @param payload The payload of the message.
|
|
75
|
-
* @returns A TransportMessage object with the given parameters.
|
|
76
|
-
*/
|
|
77
|
-
declare function msg<Payload extends object>(from: string, to: string, streamId: string, payload: Payload, serviceName?: string, procedureName?: string): TransportMessage<Payload>;
|
|
78
|
-
/**
|
|
79
|
-
* Creates a new transport message as a response to the given message.
|
|
80
|
-
* @param msg The original message to respond to.
|
|
81
|
-
* @param response The payload of the response message.
|
|
82
|
-
* @returns A new transport message with appropriate to, from, and payload fields
|
|
83
|
-
*/
|
|
84
|
-
declare function reply<Payload extends object>(msg: OpaqueTransportMessage, response: Payload): TransportMessage<Payload>;
|
|
85
|
-
/**
|
|
86
|
-
* Checks if the given control flag (usually found in msg.controlFlag) is a stream open message.
|
|
87
|
-
* @param controlFlag - The control flag to check.
|
|
88
|
-
* @returns True if the control flag contains the StreamOpenBit, false otherwise.
|
|
89
|
-
*/
|
|
90
|
-
declare function isStreamOpen(controlFlag: number): boolean;
|
|
91
|
-
/**
|
|
92
|
-
* Checks if the given control flag (usually found in msg.controlFlag) is a stream close message.
|
|
93
|
-
* @param controlFlag - The control flag to check.
|
|
94
|
-
* @returns True if the control flag contains the StreamCloseBit, false otherwise.
|
|
95
|
-
*/
|
|
96
|
-
declare function isStreamClose(controlFlag: number): boolean;
|
|
97
|
-
|
|
98
|
-
interface EventMap {
|
|
99
|
-
message: OpaqueTransportMessage;
|
|
100
|
-
connectionStatus: {
|
|
101
|
-
status: 'connect' | 'disconnect';
|
|
102
|
-
conn: Connection;
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
type EventTypes = keyof EventMap;
|
|
106
|
-
type EventHandler<K extends EventTypes> = (event: EventMap[K]) => void;
|
|
107
|
-
declare class EventDispatcher<T extends EventTypes> {
|
|
108
|
-
private eventListeners;
|
|
109
|
-
numberOfListeners<K extends T>(eventType: K): number;
|
|
110
|
-
addEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
|
|
111
|
-
removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
|
|
112
|
-
dispatchEvent<K extends T>(eventType: K, event: EventMap[K]): void;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* A 1:1 connection between two transports. Once this is created,
|
|
117
|
-
* the {@link Connection} is expected to take over responsibility for
|
|
118
|
-
* reading and writing messages from the underlying connection.
|
|
119
|
-
*
|
|
120
|
-
* 1) Messages received on the {@link Connection} are dispatched back to the {@link Transport}
|
|
121
|
-
* via {@link Transport.onMessage}. The {@link Transport} then notifies any registered message listeners.
|
|
122
|
-
* 2) When {@link Transport.send}(msg) is called, the transport looks up the appropriate
|
|
123
|
-
* connection in the {@link connections} map via `msg.to` and calls {@link send}(bytes)
|
|
124
|
-
* so the connection can send it.
|
|
125
|
-
*/
|
|
126
|
-
declare abstract class Connection {
|
|
127
|
-
connectedTo: TransportClientId;
|
|
128
|
-
transport: Transport<Connection>;
|
|
129
|
-
constructor(transport: Transport<Connection>, connectedTo: TransportClientId);
|
|
130
|
-
abstract send(msg: Uint8Array): boolean;
|
|
131
|
-
abstract close(): void;
|
|
132
|
-
}
|
|
133
|
-
type TransportStatus = 'open' | 'closed' | 'destroyed';
|
|
134
|
-
/**
|
|
135
|
-
* Transports manage the lifecycle (creation/deletion) of connections. Its responsibilities include:
|
|
136
|
-
*
|
|
137
|
-
* 1) Constructing a new {@link Connection} on {@link TransportMessage}s from new clients.
|
|
138
|
-
* After constructing the {@link Connection}, {@link onConnect} is called which adds it to the connection map.
|
|
139
|
-
* 2) Delegating message listening of the connection to the newly created {@link Connection}.
|
|
140
|
-
* From this point on, the {@link Connection} is responsible for *reading* and *writing*
|
|
141
|
-
* messages from the connection.
|
|
142
|
-
* 3) When a connection is closed, the {@link Transport} calls {@link onDisconnect} which closes the
|
|
143
|
-
* connection via {@link Connection.close} and removes it from the {@link connections} map.
|
|
144
|
-
|
|
145
|
-
*
|
|
146
|
-
* ```plaintext
|
|
147
|
-
* ▲
|
|
148
|
-
* incoming │
|
|
149
|
-
* messages │
|
|
150
|
-
* ▼
|
|
151
|
-
* ┌─────────────┐ 1:N ┌────────────┐
|
|
152
|
-
* │ Transport │ ◄─────► │ Connection │
|
|
153
|
-
* └─────────────┘ └────────────┘
|
|
154
|
-
* ▲
|
|
155
|
-
* │
|
|
156
|
-
* ▼
|
|
157
|
-
* ┌───────────┐
|
|
158
|
-
* │ Message │
|
|
159
|
-
* │ Listeners │
|
|
160
|
-
* └───────────┘
|
|
161
|
-
* ```
|
|
162
|
-
* @abstract
|
|
163
|
-
*/
|
|
164
|
-
declare abstract class Transport<ConnType extends Connection> {
|
|
165
|
-
/**
|
|
166
|
-
* A flag indicating whether the transport has been destroyed.
|
|
167
|
-
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
168
|
-
*/
|
|
169
|
-
state: TransportStatus;
|
|
170
|
-
/**
|
|
171
|
-
* The {@link Codec} used to encode and decode messages.
|
|
172
|
-
*/
|
|
173
|
-
codec: Codec;
|
|
174
|
-
/**
|
|
175
|
-
* The client ID of this transport.
|
|
176
|
-
*/
|
|
177
|
-
clientId: TransportClientId;
|
|
178
|
-
/**
|
|
179
|
-
* An array of message IDs that are waiting to be sent over the WebSocket connection.
|
|
180
|
-
* This builds up if the WebSocket is down for a period of time.
|
|
181
|
-
*/
|
|
182
|
-
sendQueue: Map<TransportClientId, Array<MessageId>>;
|
|
183
|
-
/**
|
|
184
|
-
* The buffer of messages that have been sent but not yet acknowledged.
|
|
185
|
-
*/
|
|
186
|
-
sendBuffer: Map<MessageId, OpaqueTransportMessage>;
|
|
187
|
-
/**
|
|
188
|
-
* The map of {@link Connection}s managed by this transport.
|
|
189
|
-
*/
|
|
190
|
-
connections: Map<TransportClientId, ConnType>;
|
|
191
|
-
/**
|
|
192
|
-
* The event dispatcher for handling events of type EventTypes.
|
|
193
|
-
*/
|
|
194
|
-
eventDispatcher: EventDispatcher<EventTypes>;
|
|
195
|
-
/**
|
|
196
|
-
* Creates a new Transport instance.
|
|
197
|
-
* This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
|
|
198
|
-
* @param codec The codec used to encode and decode messages.
|
|
199
|
-
* @param clientId The client ID of this transport.
|
|
200
|
-
*/
|
|
201
|
-
constructor(codec: Codec, clientId: TransportClientId);
|
|
202
|
-
/**
|
|
203
|
-
* Abstract method that creates a new {@link Connection} object.
|
|
204
|
-
* This should call {@link onConnect} when the connection is established.
|
|
205
|
-
* The downstream implementation needs to implement this. If the downstream
|
|
206
|
-
* transport cannot make new outgoing connections (e.g. a server transport),
|
|
207
|
-
* it is ok to log an error and return.
|
|
208
|
-
*
|
|
209
|
-
* Consumers of river should never need to call this directly.
|
|
210
|
-
* Instead, look for a `reopen` method on the transport.
|
|
211
|
-
*
|
|
212
|
-
* @param to The client ID of the node to connect to.
|
|
213
|
-
* @returns The new connection object.
|
|
214
|
-
*/
|
|
215
|
-
abstract createNewConnection(to: TransportClientId): Promise<void>;
|
|
216
|
-
/**
|
|
217
|
-
* The downstream implementation needs to call this when a new connection is established.
|
|
218
|
-
* @param conn The connection object.
|
|
219
|
-
*/
|
|
220
|
-
onConnect(conn: ConnType): void;
|
|
221
|
-
/**
|
|
222
|
-
* The downstream implementation needs to call this when a connection is closed.
|
|
223
|
-
* @param conn The connection object.
|
|
224
|
-
*/
|
|
225
|
-
onDisconnect(conn: ConnType): void;
|
|
226
|
-
/**
|
|
227
|
-
* Handles a message received by this transport. Thin wrapper around {@link handleMsg} and {@link parseMsg}.
|
|
228
|
-
* @param msg The message to handle.
|
|
229
|
-
*/
|
|
230
|
-
onMessage(msg: Uint8Array): void;
|
|
231
|
-
/**
|
|
232
|
-
* Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
|
|
233
|
-
* @param msg The message to parse.
|
|
234
|
-
* @returns The parsed message, or null if the message is malformed or invalid.
|
|
235
|
-
*/
|
|
236
|
-
protected parseMsg(msg: Uint8Array): OpaqueTransportMessage | null;
|
|
237
|
-
/**
|
|
238
|
-
* Called when a message is received by this transport.
|
|
239
|
-
* You generally shouldn't need to override this in downstream transport implementations.
|
|
240
|
-
* @param msg The received message.
|
|
241
|
-
*/
|
|
242
|
-
protected handleMsg(msg: OpaqueTransportMessage | null): void;
|
|
243
|
-
/**
|
|
244
|
-
* Adds a listener to this transport.
|
|
245
|
-
* @param the type of event to listen for
|
|
246
|
-
* @param handler The message handler to add.
|
|
247
|
-
*/
|
|
248
|
-
addEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
|
|
249
|
-
/**
|
|
250
|
-
* Removes a listener from this transport.
|
|
251
|
-
* @param the type of event to unlisten on
|
|
252
|
-
* @param handler The message handler to remove.
|
|
253
|
-
*/
|
|
254
|
-
removeEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
|
|
255
|
-
/**
|
|
256
|
-
* Sends a message over this transport, delegating to the appropriate connection to actually
|
|
257
|
-
* send the message.
|
|
258
|
-
* @param msg The message to send.
|
|
259
|
-
* @returns The ID of the sent message.
|
|
260
|
-
*/
|
|
261
|
-
send(msg: OpaqueTransportMessage): MessageId;
|
|
262
|
-
/**
|
|
263
|
-
* Default close implementation for transports. You should override this in the downstream
|
|
264
|
-
* implementation if you need to do any additional cleanup and call super.close() at the end.
|
|
265
|
-
* Closes the transport. Any messages sent while the transport is closed will be silently discarded.
|
|
266
|
-
*/
|
|
267
|
-
close(): Promise<void>;
|
|
268
|
-
/**
|
|
269
|
-
* Default destroy implementation for transports. You should override this in the downstream
|
|
270
|
-
* implementation if you need to do any additional cleanup and call super.destroy() at the end.
|
|
271
|
-
* Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
|
|
272
|
-
*/
|
|
273
|
-
destroy(): Promise<void>;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
export { Connection, MessageId, OpaqueTransportMessage, OpaqueTransportMessageSchema, Transport, TransportClientId, TransportMessage, TransportMessageSchema, isStreamClose, isStreamOpen, msg, reply };
|
|
1
|
+
export { a as ClientTransport, C as Connection, k as EventHandler, E as EventMap, j as EventTypes, O as OpaqueTransportMessage, f as OpaqueTransportMessageSchema, S as ServerTransport, d as Session, T as Transport, b as TransportClientId, g as TransportMessage, e as TransportMessageSchema, h as isStreamClose, i as isStreamOpen } from '../index-0c0a69f6.js';
|
|
2
|
+
import '../types-3e5768ec.js';
|
|
3
|
+
import '@sinclair/typebox';
|
package/dist/transport/index.js
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
import "../chunk-
|
|
1
|
+
import "../chunk-5IZ2UHWV.js";
|
|
2
2
|
import {
|
|
3
|
+
ClientTransport,
|
|
3
4
|
Connection,
|
|
5
|
+
ServerTransport,
|
|
6
|
+
Session,
|
|
4
7
|
Transport
|
|
5
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-QEYN2Z6O.js";
|
|
9
|
+
import "../chunk-GZ7HCLLM.js";
|
|
6
10
|
import {
|
|
7
11
|
OpaqueTransportMessageSchema,
|
|
8
|
-
TransportMessageSchema
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from "../chunk-ZE4MX7DF.js";
|
|
12
|
-
import "../chunk-T7M7OKPE.js";
|
|
12
|
+
TransportMessageSchema
|
|
13
|
+
} from "../chunk-XFFS4UOD.js";
|
|
14
|
+
import "../chunk-H4BYJELI.js";
|
|
13
15
|
export {
|
|
16
|
+
ClientTransport,
|
|
14
17
|
Connection,
|
|
15
18
|
OpaqueTransportMessageSchema,
|
|
19
|
+
ServerTransport,
|
|
20
|
+
Session,
|
|
16
21
|
Transport,
|
|
17
|
-
TransportMessageSchema
|
|
18
|
-
msg,
|
|
19
|
-
reply
|
|
22
|
+
TransportMessageSchema
|
|
20
23
|
};
|