@replit/river 0.23.15 → 0.200.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-5HK7ZQYH.js → chunk-3FALN7ZG.js} +14 -2
- package/dist/{chunk-5HK7ZQYH.js.map → chunk-3FALN7ZG.js.map} +1 -1
- package/dist/{chunk-RQQZUQGE.js → chunk-6GK2IIDP.js} +2 -2
- package/dist/{chunk-JMVKSGND.js → chunk-6RKO3DDG.js} +4 -4
- package/dist/chunk-E2ZXI663.js +1995 -0
- package/dist/chunk-E2ZXI663.js.map +1 -0
- package/dist/{chunk-OXVWMLID.js → chunk-LK74ZG7M.js} +25 -10
- package/dist/chunk-LK74ZG7M.js.map +1 -0
- package/dist/{chunk-XYEOXPZQ.js → chunk-NDLWNT7B.js} +2 -2
- package/dist/{chunk-6LCL2ZZF.js → chunk-QMM35C3H.js} +1 -1
- package/dist/chunk-QMM35C3H.js.map +1 -0
- package/dist/{chunk-IJTGEBLG.js → chunk-TK7QHUFP.js} +4 -4
- package/dist/{chunk-MD4S7GO2.js → chunk-YUY37VAK.js} +23 -7
- package/dist/chunk-YUY37VAK.js.map +1 -0
- package/dist/{connection-5d0978ce.d.ts → connection-0638316b.d.ts} +1 -1
- package/dist/{connection-e57e98ea.d.ts → connection-c6521735.d.ts} +1 -1
- package/dist/{index-ea74cdbb.d.ts → index-10ebd26a.d.ts} +33 -33
- package/dist/logging/index.cjs.map +1 -1
- package/dist/logging/index.d.cts +1 -1
- package/dist/logging/index.d.ts +1 -1
- package/dist/logging/index.js +1 -1
- package/dist/router/index.cjs +1053 -912
- package/dist/router/index.cjs.map +1 -1
- package/dist/router/index.d.cts +19 -23
- package/dist/router/index.d.ts +19 -23
- package/dist/router/index.js +12 -6
- package/dist/services-34d97070.d.ts +1366 -0
- package/dist/transport/impls/uds/client.cjs +33 -5
- package/dist/transport/impls/uds/client.cjs.map +1 -1
- package/dist/transport/impls/uds/client.d.cts +3 -4
- package/dist/transport/impls/uds/client.d.ts +3 -4
- package/dist/transport/impls/uds/client.js +6 -6
- package/dist/transport/impls/uds/server.cjs +33 -5
- package/dist/transport/impls/uds/server.cjs.map +1 -1
- package/dist/transport/impls/uds/server.d.cts +4 -4
- package/dist/transport/impls/uds/server.d.ts +4 -4
- package/dist/transport/impls/uds/server.js +6 -6
- package/dist/transport/impls/ws/client.cjs +33 -5
- package/dist/transport/impls/ws/client.cjs.map +1 -1
- package/dist/transport/impls/ws/client.d.cts +5 -6
- package/dist/transport/impls/ws/client.d.ts +5 -6
- package/dist/transport/impls/ws/client.js +6 -6
- package/dist/transport/impls/ws/server.cjs +33 -5
- package/dist/transport/impls/ws/server.cjs.map +1 -1
- package/dist/transport/impls/ws/server.d.cts +4 -4
- package/dist/transport/impls/ws/server.d.ts +4 -4
- package/dist/transport/impls/ws/server.js +6 -6
- package/dist/transport/index.cjs +33 -5
- package/dist/transport/index.cjs.map +1 -1
- package/dist/transport/index.d.cts +27 -5
- package/dist/transport/index.d.ts +27 -5
- package/dist/transport/index.js +6 -6
- package/dist/util/testHelpers.cjs +382 -326
- package/dist/util/testHelpers.cjs.map +1 -1
- package/dist/util/testHelpers.d.cts +32 -21
- package/dist/util/testHelpers.d.ts +32 -21
- package/dist/util/testHelpers.js +76 -42
- package/dist/util/testHelpers.js.map +1 -1
- package/package.json +15 -15
- package/dist/chunk-6LCL2ZZF.js.map +0 -1
- package/dist/chunk-AEY7BBOZ.js +0 -1865
- package/dist/chunk-AEY7BBOZ.js.map +0 -1
- package/dist/chunk-MD4S7GO2.js.map +0 -1
- package/dist/chunk-OXVWMLID.js.map +0 -1
- package/dist/client-e13979ac.d.ts +0 -52
- package/dist/handshake-5665ffd3.d.ts +0 -511
- package/dist/server-1cfc88d1.d.ts +0 -24
- package/dist/services-86c4d10d.d.ts +0 -709
- /package/dist/{chunk-RQQZUQGE.js.map → chunk-6GK2IIDP.js.map} +0 -0
- /package/dist/{chunk-JMVKSGND.js.map → chunk-6RKO3DDG.js.map} +0 -0
- /package/dist/{chunk-XYEOXPZQ.js.map → chunk-NDLWNT7B.js.map} +0 -0
- /package/dist/{chunk-IJTGEBLG.js.map → chunk-TK7QHUFP.js.map} +0 -0
|
@@ -0,0 +1,1366 @@
|
|
|
1
|
+
import * as _sinclair_typebox from '@sinclair/typebox';
|
|
2
|
+
import { Static, TSchema, TObject, TUnion, TString, TLiteral, TNever } from '@sinclair/typebox';
|
|
3
|
+
import { a as TelemetryInfo, M as MessageMetadata, T as TransportClientId, b as PropagationContext, L as Logger, P as PartialTransportMessage, c as TransportMessage, O as OpaqueTransportMessage, d as LogFn, e as LoggingLevel } from './index-10ebd26a.js';
|
|
4
|
+
import { C as Codec } from './types-3e5768ec.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A connection is the actual raw underlying transport connection.
|
|
8
|
+
* It’s responsible for dispatching to/from the actual connection itself
|
|
9
|
+
* This should be instantiated as soon as the client/server has a connection
|
|
10
|
+
* It’s tied to the lifecycle of the underlying transport connection (i.e. if the WS drops, this connection should be deleted)
|
|
11
|
+
*/
|
|
12
|
+
declare abstract class Connection {
|
|
13
|
+
id: string;
|
|
14
|
+
telemetry?: TelemetryInfo;
|
|
15
|
+
constructor();
|
|
16
|
+
get loggingMetadata(): MessageMetadata;
|
|
17
|
+
/**
|
|
18
|
+
* Handle adding a callback for when a message is received.
|
|
19
|
+
* @param msg The message that was received.
|
|
20
|
+
*/
|
|
21
|
+
abstract addDataListener(cb: (msg: Uint8Array) => void): void;
|
|
22
|
+
abstract removeDataListener(cb: (msg: Uint8Array) => void): void;
|
|
23
|
+
/**
|
|
24
|
+
* Handle adding a callback for when the connection is closed.
|
|
25
|
+
* This should also be called if an error happens.
|
|
26
|
+
* @param cb The callback to call when the connection is closed.
|
|
27
|
+
*/
|
|
28
|
+
abstract addCloseListener(cb: () => void): void;
|
|
29
|
+
/**
|
|
30
|
+
* Handle adding a callback for when an error is received.
|
|
31
|
+
* This should only be used for this.logging errors, all cleanup
|
|
32
|
+
* should be delegated to addCloseListener.
|
|
33
|
+
*
|
|
34
|
+
* The implementer should take care such that the implemented
|
|
35
|
+
* connection will call both the close and error callbacks
|
|
36
|
+
* on an error.
|
|
37
|
+
*
|
|
38
|
+
* @param cb The callback to call when an error is received.
|
|
39
|
+
*/
|
|
40
|
+
abstract addErrorListener(cb: (err: Error) => void): void;
|
|
41
|
+
/**
|
|
42
|
+
* Sends a message over the connection.
|
|
43
|
+
* @param msg The message to send.
|
|
44
|
+
* @returns true if the message was sent, false otherwise.
|
|
45
|
+
*/
|
|
46
|
+
abstract send(msg: Uint8Array): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Closes the connection.
|
|
49
|
+
*/
|
|
50
|
+
abstract close(): void;
|
|
51
|
+
}
|
|
52
|
+
interface SessionOptions {
|
|
53
|
+
/**
|
|
54
|
+
* Frequency at which to send heartbeat acknowledgements
|
|
55
|
+
*/
|
|
56
|
+
heartbeatIntervalMs: number;
|
|
57
|
+
/**
|
|
58
|
+
* Number of elapsed heartbeats without a response message before we consider
|
|
59
|
+
* the connection dead.
|
|
60
|
+
*/
|
|
61
|
+
heartbeatsUntilDead: number;
|
|
62
|
+
/**
|
|
63
|
+
* Duration to wait between connection disconnect and actual session disconnect
|
|
64
|
+
*/
|
|
65
|
+
sessionDisconnectGraceMs: number;
|
|
66
|
+
/**
|
|
67
|
+
* The codec to use for encoding/decoding messages over the wire
|
|
68
|
+
*/
|
|
69
|
+
codec: Codec;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A session is a higher-level abstraction that operates over the span of potentially multiple transport-level connections
|
|
73
|
+
* - It’s responsible for tracking any metadata for a particular client that might need to be persisted across connections (i.e. the sendBuffer, ack, seq)
|
|
74
|
+
* - This will only be considered disconnected if
|
|
75
|
+
* - the server tells the client that we’ve reconnected but it doesn’t recognize us anymore (server definitely died) or
|
|
76
|
+
* - we hit a grace period after a connection disconnect
|
|
77
|
+
*/
|
|
78
|
+
declare class Session<ConnType extends Connection> {
|
|
79
|
+
private codec;
|
|
80
|
+
private options;
|
|
81
|
+
readonly telemetry: TelemetryInfo;
|
|
82
|
+
/**
|
|
83
|
+
* The buffer of messages that have been sent but not yet acknowledged.
|
|
84
|
+
*/
|
|
85
|
+
private sendBuffer;
|
|
86
|
+
/**
|
|
87
|
+
* The active connection associated with this session
|
|
88
|
+
*/
|
|
89
|
+
connection?: ConnType;
|
|
90
|
+
/**
|
|
91
|
+
* A connection that is currently undergoing handshaking. Used to distinguish between the active
|
|
92
|
+
* connection, but still be able to close it if needed.
|
|
93
|
+
*/
|
|
94
|
+
private handshakingConnection?;
|
|
95
|
+
readonly from: TransportClientId;
|
|
96
|
+
readonly to: TransportClientId;
|
|
97
|
+
/**
|
|
98
|
+
* The unique ID of this session.
|
|
99
|
+
*/
|
|
100
|
+
readonly id: string;
|
|
101
|
+
/**
|
|
102
|
+
* What the other side advertised as their session ID
|
|
103
|
+
* for this session.
|
|
104
|
+
*/
|
|
105
|
+
advertisedSessionId?: string;
|
|
106
|
+
/**
|
|
107
|
+
* Number of messages we've sent along this session (excluding handshake and acks)
|
|
108
|
+
*/
|
|
109
|
+
private seq;
|
|
110
|
+
/**
|
|
111
|
+
* Number of unique messages we've received this session (excluding handshake and acks)
|
|
112
|
+
*/
|
|
113
|
+
private ack;
|
|
114
|
+
/**
|
|
115
|
+
* The grace period between when the inner connection is disconnected
|
|
116
|
+
* and when we should consider the entire session disconnected.
|
|
117
|
+
*/
|
|
118
|
+
private disconnectionGrace?;
|
|
119
|
+
/**
|
|
120
|
+
* Number of heartbeats we've sent without a response.
|
|
121
|
+
*/
|
|
122
|
+
private heartbeatMisses;
|
|
123
|
+
/**
|
|
124
|
+
* The interval for sending heartbeats.
|
|
125
|
+
*/
|
|
126
|
+
private heartbeat;
|
|
127
|
+
private log?;
|
|
128
|
+
constructor(conn: ConnType | undefined, from: TransportClientId, to: TransportClientId, options: SessionOptions, propagationCtx?: PropagationContext);
|
|
129
|
+
bindLogger(log: Logger): void;
|
|
130
|
+
get loggingMetadata(): MessageMetadata;
|
|
131
|
+
/**
|
|
132
|
+
* Sends a message over the session's connection.
|
|
133
|
+
* If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
|
|
134
|
+
*
|
|
135
|
+
* @param msg The partial message to be sent, which will be constructed into a full message.
|
|
136
|
+
* @param addToSendBuff Whether to add the message to the send buffer for retry.
|
|
137
|
+
* @returns The full transport ID of the message that was attempted to be sent.
|
|
138
|
+
*/
|
|
139
|
+
send(msg: PartialTransportMessage): string;
|
|
140
|
+
sendHeartbeat(): void;
|
|
141
|
+
resetBufferedMessages(): void;
|
|
142
|
+
sendBufferedMessages(conn: ConnType): void;
|
|
143
|
+
updateBookkeeping(ack: number, seq: number): void;
|
|
144
|
+
private closeStaleConnection;
|
|
145
|
+
replaceWithNewConnection(newConn: ConnType, isTransparentReconnect: boolean): void;
|
|
146
|
+
replaceWithNewHandshakingConnection(newConn: ConnType): void;
|
|
147
|
+
beginGrace(cb: () => void): void;
|
|
148
|
+
cancelGrace(): void;
|
|
149
|
+
/**
|
|
150
|
+
* Used to close the handshaking connection, if set.
|
|
151
|
+
*/
|
|
152
|
+
closeHandshakingConnection(expectedHandshakingConn?: ConnType): void;
|
|
153
|
+
close(): void;
|
|
154
|
+
get connected(): boolean;
|
|
155
|
+
get nextExpectedAck(): number;
|
|
156
|
+
get nextExpectedSeq(): number;
|
|
157
|
+
/**
|
|
158
|
+
* Check that the peer's next expected seq number matches something that is in our send buffer
|
|
159
|
+
* _or_ matches our actual next seq.
|
|
160
|
+
*/
|
|
161
|
+
nextExpectedSeqInRange(nextExpectedSeq: number): boolean;
|
|
162
|
+
advanceAckForTesting(by: number): void;
|
|
163
|
+
constructMsg<Payload>(partialMsg: PartialTransportMessage<Payload>): TransportMessage<Payload>;
|
|
164
|
+
inspectSendBuffer(): ReadonlyArray<OpaqueTransportMessage>;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
type ConnectionStatus = 'connect' | 'disconnect';
|
|
168
|
+
declare const ProtocolError: {
|
|
169
|
+
readonly RetriesExceeded: "conn_retry_exceeded";
|
|
170
|
+
readonly HandshakeFailed: "handshake_failed";
|
|
171
|
+
readonly MessageOrderingViolated: "message_ordering_violated";
|
|
172
|
+
};
|
|
173
|
+
type ProtocolErrorType = (typeof ProtocolError)[keyof typeof ProtocolError];
|
|
174
|
+
interface EventMap {
|
|
175
|
+
message: OpaqueTransportMessage;
|
|
176
|
+
connectionStatus: {
|
|
177
|
+
status: ConnectionStatus;
|
|
178
|
+
conn: Connection;
|
|
179
|
+
};
|
|
180
|
+
sessionStatus: {
|
|
181
|
+
status: ConnectionStatus;
|
|
182
|
+
session: Session<Connection>;
|
|
183
|
+
};
|
|
184
|
+
protocolError: {
|
|
185
|
+
type: ProtocolErrorType;
|
|
186
|
+
message: string;
|
|
187
|
+
};
|
|
188
|
+
transportStatus: {
|
|
189
|
+
status: TransportStatus;
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
type EventTypes = keyof EventMap;
|
|
193
|
+
type EventHandler<K extends EventTypes> = (event: EventMap[K]) => unknown;
|
|
194
|
+
declare class EventDispatcher<T extends EventTypes> {
|
|
195
|
+
private eventListeners;
|
|
196
|
+
removeAllListeners(): void;
|
|
197
|
+
numberOfListeners<K extends T>(eventType: K): number;
|
|
198
|
+
addEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
|
|
199
|
+
removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
|
|
200
|
+
dispatchEvent<K extends T>(eventType: K, event: EventMap[K]): void;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Options to control the backoff and retry behavior of the client transport's connection behaviour.
|
|
205
|
+
*
|
|
206
|
+
* River implements exponential backoff with jitter to prevent flooding the server
|
|
207
|
+
* when there's an issue with connection establishment.
|
|
208
|
+
*
|
|
209
|
+
* The backoff is calculated via the following:
|
|
210
|
+
* backOff = min(jitter + {@link baseIntervalMs} * 2 ^ budget_consumed, {@link maxBackoffMs})
|
|
211
|
+
*
|
|
212
|
+
* We use a leaky bucket rate limit with a budget of {@link attemptBudgetCapacity} reconnection attempts.
|
|
213
|
+
* Budget only starts to restore after a successful handshake at a rate of one budget per {@link budgetRestoreIntervalMs}.
|
|
214
|
+
*/
|
|
215
|
+
interface ConnectionRetryOptions {
|
|
216
|
+
/**
|
|
217
|
+
* The base interval to wait before retrying a connection.
|
|
218
|
+
*/
|
|
219
|
+
baseIntervalMs: number;
|
|
220
|
+
/**
|
|
221
|
+
* The maximum random jitter to add to the total backoff time.
|
|
222
|
+
*/
|
|
223
|
+
maxJitterMs: number;
|
|
224
|
+
/**
|
|
225
|
+
* The maximum amount of time to wait before retrying a connection.
|
|
226
|
+
* This does not include the jitter.
|
|
227
|
+
*/
|
|
228
|
+
maxBackoffMs: number;
|
|
229
|
+
/**
|
|
230
|
+
* The max number of times to attempt a connection before a successful handshake.
|
|
231
|
+
* This persists across connections but starts restoring budget after a successful handshake.
|
|
232
|
+
* The restoration interval depends on {@link budgetRestoreIntervalMs}
|
|
233
|
+
*/
|
|
234
|
+
attemptBudgetCapacity: number;
|
|
235
|
+
/**
|
|
236
|
+
* After a successful connection attempt, how long to wait before we restore a single budget.
|
|
237
|
+
*/
|
|
238
|
+
budgetRestoreIntervalMs: number;
|
|
239
|
+
}
|
|
240
|
+
declare class LeakyBucketRateLimit {
|
|
241
|
+
private budgetConsumed;
|
|
242
|
+
private intervalHandles;
|
|
243
|
+
private readonly options;
|
|
244
|
+
constructor(options: ConnectionRetryOptions);
|
|
245
|
+
getBackoffMs(user: TransportClientId): number;
|
|
246
|
+
get totalBudgetRestoreTime(): number;
|
|
247
|
+
consumeBudget(user: TransportClientId): void;
|
|
248
|
+
getBudgetConsumed(user: TransportClientId): number;
|
|
249
|
+
hasBudget(user: TransportClientId): boolean;
|
|
250
|
+
startRestoringBudget(user: TransportClientId): void;
|
|
251
|
+
private stopLeak;
|
|
252
|
+
close(): void;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
type TransportOptions = SessionOptions;
|
|
256
|
+
type ProvidedTransportOptions = Partial<TransportOptions>;
|
|
257
|
+
type ClientTransportOptions = TransportOptions & ConnectionRetryOptions;
|
|
258
|
+
type ProvidedClientTransportOptions = Partial<ClientTransportOptions>;
|
|
259
|
+
type ServerTransportOptions = TransportOptions;
|
|
260
|
+
type ProvidedServerTransportOptions = Partial<ServerTransportOptions>;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Represents the possible states of a transport.
|
|
264
|
+
* @property {'open'} open - The transport is open and operational (note that this doesn't mean it is actively connected)
|
|
265
|
+
* @property {'closed'} closed - The transport is permanently closed and cannot be reopened.
|
|
266
|
+
*/
|
|
267
|
+
type TransportStatus = 'open' | 'closed';
|
|
268
|
+
/**
|
|
269
|
+
* Transports manage the lifecycle (creation/deletion) of sessions and connections. Its responsibilities include:
|
|
270
|
+
*
|
|
271
|
+
* 1) Constructing a new {@link Session} and {@link Connection} on {@link TransportMessage}s from new clients.
|
|
272
|
+
* After constructing the {@link Connection}, {@link onConnect} is called which adds it to the connection map.
|
|
273
|
+
* 2) Delegating message listening of the connection to the newly created {@link Connection}.
|
|
274
|
+
* From this point on, the {@link Connection} is responsible for *reading* and *writing*
|
|
275
|
+
* messages from the connection.
|
|
276
|
+
* 3) When a connection is closed, the {@link Transport} calls {@link onDisconnect} which closes the
|
|
277
|
+
* connection via {@link Connection.close} and removes it from the {@link connections} map.
|
|
278
|
+
|
|
279
|
+
*
|
|
280
|
+
* ```plaintext
|
|
281
|
+
* ▲
|
|
282
|
+
* incoming │
|
|
283
|
+
* messages │
|
|
284
|
+
* ▼
|
|
285
|
+
* ┌─────────────┐ 1:N ┌───────────┐ 1:1* ┌────────────┐
|
|
286
|
+
* │ Transport │ ◄─────► │ Session │ ◄─────► │ Connection │
|
|
287
|
+
* └─────────────┘ └───────────┘ └────────────┘
|
|
288
|
+
* ▲ * (may or may not be initialized yet)
|
|
289
|
+
* │
|
|
290
|
+
* ▼
|
|
291
|
+
* ┌───────────┐
|
|
292
|
+
* │ Message │
|
|
293
|
+
* │ Listeners │
|
|
294
|
+
* └───────────┘
|
|
295
|
+
* ```
|
|
296
|
+
* @abstract
|
|
297
|
+
*/
|
|
298
|
+
declare abstract class Transport<ConnType extends Connection> {
|
|
299
|
+
/**
|
|
300
|
+
* The status of the transport.
|
|
301
|
+
*/
|
|
302
|
+
private status;
|
|
303
|
+
/**
|
|
304
|
+
* The {@link Codec} used to encode and decode messages.
|
|
305
|
+
*/
|
|
306
|
+
codec: Codec;
|
|
307
|
+
/**
|
|
308
|
+
* The client ID of this transport.
|
|
309
|
+
*/
|
|
310
|
+
clientId: TransportClientId;
|
|
311
|
+
/**
|
|
312
|
+
* The map of {@link Session}s managed by this transport.
|
|
313
|
+
*/
|
|
314
|
+
sessions: Map<TransportClientId, Session<ConnType>>;
|
|
315
|
+
/**
|
|
316
|
+
* The map of {@link Connection}s managed by this transport.
|
|
317
|
+
*/
|
|
318
|
+
get connections(): Map<string, ConnType>;
|
|
319
|
+
/**
|
|
320
|
+
* The event dispatcher for handling events of type EventTypes.
|
|
321
|
+
*/
|
|
322
|
+
eventDispatcher: EventDispatcher<EventTypes>;
|
|
323
|
+
/**
|
|
324
|
+
* The options for this transport.
|
|
325
|
+
*/
|
|
326
|
+
protected options: TransportOptions;
|
|
327
|
+
log?: Logger;
|
|
328
|
+
/**
|
|
329
|
+
* Creates a new Transport instance.
|
|
330
|
+
* This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
|
|
331
|
+
* @param codec The codec used to encode and decode messages.
|
|
332
|
+
* @param clientId The client ID of this transport.
|
|
333
|
+
*/
|
|
334
|
+
constructor(clientId: TransportClientId, providedOptions?: ProvidedTransportOptions);
|
|
335
|
+
bindLogger(fn: LogFn | Logger, level?: LoggingLevel): void;
|
|
336
|
+
/**
|
|
337
|
+
* This is called immediately after a new connection is established and we
|
|
338
|
+
* may or may not know the identity of the connected client.
|
|
339
|
+
* It should attach all the necessary listeners to the connection for lifecycle
|
|
340
|
+
* events (i.e. data, close, error)
|
|
341
|
+
*
|
|
342
|
+
* This method is implemented by {@link ClientTransport} and {@link ServerTransport}.
|
|
343
|
+
*/
|
|
344
|
+
protected abstract handleConnection(conn: ConnType, to: TransportClientId): void;
|
|
345
|
+
/**
|
|
346
|
+
* Called when a new connection is established
|
|
347
|
+
* and we know the identity of the connected client.
|
|
348
|
+
* @param conn The connection object.
|
|
349
|
+
*/
|
|
350
|
+
protected onConnect(conn: ConnType, session: Session<ConnType>, isTransparentReconnect: boolean): void;
|
|
351
|
+
protected createSession(to: TransportClientId, conn?: ConnType, propagationCtx?: PropagationContext): Session<ConnType>;
|
|
352
|
+
protected createNewSession({ to, conn, sessionId, propagationCtx, }: {
|
|
353
|
+
to: TransportClientId;
|
|
354
|
+
conn: ConnType;
|
|
355
|
+
sessionId: string;
|
|
356
|
+
propagationCtx?: PropagationContext;
|
|
357
|
+
}): Session<ConnType>;
|
|
358
|
+
protected getExistingSession({ to, sessionId, nextExpectedSeq, }: {
|
|
359
|
+
to: TransportClientId;
|
|
360
|
+
sessionId: string;
|
|
361
|
+
nextExpectedSeq: number;
|
|
362
|
+
}): false | Session<ConnType>;
|
|
363
|
+
protected getOrCreateSession({ to, conn, handshakingConn, sessionId, propagationCtx, }: {
|
|
364
|
+
to: TransportClientId;
|
|
365
|
+
conn?: ConnType;
|
|
366
|
+
handshakingConn?: ConnType;
|
|
367
|
+
sessionId?: string;
|
|
368
|
+
propagationCtx?: PropagationContext;
|
|
369
|
+
}): {
|
|
370
|
+
session: Session<ConnType>;
|
|
371
|
+
isReconnect: boolean;
|
|
372
|
+
isTransparentReconnect: boolean;
|
|
373
|
+
};
|
|
374
|
+
protected deleteSession({ session, closeHandshakingConnection, handshakingConn, }: {
|
|
375
|
+
session: Session<ConnType>;
|
|
376
|
+
closeHandshakingConnection: boolean;
|
|
377
|
+
handshakingConn?: ConnType;
|
|
378
|
+
}): void;
|
|
379
|
+
/**
|
|
380
|
+
* The downstream implementation needs to call this when a connection is closed.
|
|
381
|
+
* @param conn The connection object.
|
|
382
|
+
* @param connectedTo The peer we are connected to.
|
|
383
|
+
*/
|
|
384
|
+
protected onDisconnect(conn: ConnType, session: Session<ConnType>): void;
|
|
385
|
+
/**
|
|
386
|
+
* Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
|
|
387
|
+
* @param msg The message to parse.
|
|
388
|
+
* @returns The parsed message, or null if the message is malformed or invalid.
|
|
389
|
+
*/
|
|
390
|
+
protected parseMsg(msg: Uint8Array, conn: ConnType): OpaqueTransportMessage | null;
|
|
391
|
+
/**
|
|
392
|
+
* Called when a message is received by this transport.
|
|
393
|
+
* You generally shouldn't need to override this in downstream transport implementations.
|
|
394
|
+
* @param msg The received message.
|
|
395
|
+
*/
|
|
396
|
+
protected handleMsg(msg: OpaqueTransportMessage, conn: ConnType): void;
|
|
397
|
+
/**
|
|
398
|
+
* Adds a listener to this transport.
|
|
399
|
+
* @param the type of event to listen for
|
|
400
|
+
* @param handler The message handler to add.
|
|
401
|
+
*/
|
|
402
|
+
addEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
|
|
403
|
+
/**
|
|
404
|
+
* Removes a listener from this transport.
|
|
405
|
+
* @param the type of event to un-listen on
|
|
406
|
+
* @param handler The message handler to remove.
|
|
407
|
+
*/
|
|
408
|
+
removeEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
|
|
409
|
+
/**
|
|
410
|
+
* Sends a message over this transport, delegating to the appropriate connection to actually
|
|
411
|
+
* send the message.
|
|
412
|
+
* @param msg The message to send.
|
|
413
|
+
* @returns The ID of the sent message or undefined if it wasn't sent
|
|
414
|
+
*/
|
|
415
|
+
send(to: TransportClientId, msg: PartialTransportMessage): string;
|
|
416
|
+
sendCloseControl(to: TransportClientId, streamId: string): string;
|
|
417
|
+
sendRequestCloseControl(to: TransportClientId, streamId: string): string;
|
|
418
|
+
sendAbort(to: TransportClientId, streamId: string, payload: ErrResult<Static<typeof OutputReaderErrorSchema | typeof InputReaderErrorSchema>>): string;
|
|
419
|
+
protected protocolError(type: ProtocolErrorType, message: string): void;
|
|
420
|
+
/**
|
|
421
|
+
* Default close implementation for transports. You should override this in the downstream
|
|
422
|
+
* implementation if you need to do any additional cleanup and call super.close() at the end.
|
|
423
|
+
* Closes the transport. Any messages sent while the transport is closed will be silently discarded.
|
|
424
|
+
*/
|
|
425
|
+
close(): void;
|
|
426
|
+
getStatus(): TransportStatus;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
type ConstructHandshake<T extends TSchema> = () => Static<T> | Promise<Static<T>>;
|
|
430
|
+
type ValidateHandshake<T extends TSchema> = (metadata: Static<T>, previousParsedMetadata?: ParsedMetadata) => false | ParsedMetadata | Promise<false | ParsedMetadata>;
|
|
431
|
+
interface ClientHandshakeOptions<MetadataSchema extends TSchema = TSchema> {
|
|
432
|
+
/**
|
|
433
|
+
* Schema for the metadata that the client sends to the server
|
|
434
|
+
* during the handshake.
|
|
435
|
+
*/
|
|
436
|
+
schema: MetadataSchema;
|
|
437
|
+
/**
|
|
438
|
+
* Gets the {@link HandshakeRequestMetadata} to send to the server.
|
|
439
|
+
*/
|
|
440
|
+
construct: ConstructHandshake<MetadataSchema>;
|
|
441
|
+
}
|
|
442
|
+
interface ServerHandshakeOptions<MetadataSchema extends TSchema = TSchema> {
|
|
443
|
+
/**
|
|
444
|
+
* Schema for the metadata that the server receives from the client
|
|
445
|
+
* during the handshake.
|
|
446
|
+
*/
|
|
447
|
+
schema: MetadataSchema;
|
|
448
|
+
/**
|
|
449
|
+
* Parses the {@link HandshakeRequestMetadata} sent by the client, transforming
|
|
450
|
+
* it into {@link ParsedHandshakeMetadata}.
|
|
451
|
+
*
|
|
452
|
+
* May return `false` if the client should be rejected.
|
|
453
|
+
*
|
|
454
|
+
* @param metadata - The metadata sent by the client.
|
|
455
|
+
* @param session - The session that the client would be associated with.
|
|
456
|
+
* @param isReconnect - Whether the client is reconnecting to the session,
|
|
457
|
+
* or if this is a new session.
|
|
458
|
+
*/
|
|
459
|
+
validate: ValidateHandshake<MetadataSchema>;
|
|
460
|
+
}
|
|
461
|
+
declare function createClientHandshakeOptions<MetadataSchema extends TSchema = TSchema>(schema: MetadataSchema, construct: ConstructHandshake<MetadataSchema>): ClientHandshakeOptions;
|
|
462
|
+
declare function createServerHandshakeOptions<MetadataSchema extends TSchema = TSchema>(schema: MetadataSchema, validate: ValidateHandshake<MetadataSchema>): ServerHandshakeOptions;
|
|
463
|
+
|
|
464
|
+
declare abstract class ClientTransport<ConnType extends Connection> extends Transport<ConnType> {
|
|
465
|
+
/**
|
|
466
|
+
* The options for this transport.
|
|
467
|
+
*/
|
|
468
|
+
protected options: ClientTransportOptions;
|
|
469
|
+
/**
|
|
470
|
+
* The map of reconnect promises for each client ID.
|
|
471
|
+
*/
|
|
472
|
+
inflightConnectionPromises: Map<TransportClientId, Promise<ConnType>>;
|
|
473
|
+
retryBudget: LeakyBucketRateLimit;
|
|
474
|
+
/**
|
|
475
|
+
* A flag indicating whether the transport should automatically reconnect
|
|
476
|
+
* when a connection is dropped.
|
|
477
|
+
* Realistically, this should always be true for clients unless you are writing
|
|
478
|
+
* tests or a special case where you don't want to reconnect.
|
|
479
|
+
*/
|
|
480
|
+
reconnectOnConnectionDrop: boolean;
|
|
481
|
+
/**
|
|
482
|
+
* Optional handshake options for this client.
|
|
483
|
+
*/
|
|
484
|
+
handshakeExtensions?: ClientHandshakeOptions;
|
|
485
|
+
constructor(clientId: TransportClientId, providedOptions?: ProvidedClientTransportOptions);
|
|
486
|
+
extendHandshake(options: ClientHandshakeOptions): void;
|
|
487
|
+
protected handleConnection(conn: ConnType, to: TransportClientId): void;
|
|
488
|
+
receiveHandshakeResponseMessage(data: Uint8Array, conn: ConnType): Session<ConnType> | false;
|
|
489
|
+
/**
|
|
490
|
+
* Abstract method that creates a new {@link Connection} object.
|
|
491
|
+
* This should call {@link handleConnection} when the connection is created.
|
|
492
|
+
* The downstream client implementation needs to implement this.
|
|
493
|
+
*
|
|
494
|
+
* @param to The client ID of the node to connect to.
|
|
495
|
+
* @returns The new connection object.
|
|
496
|
+
*/
|
|
497
|
+
protected abstract createNewOutgoingConnection(to: TransportClientId): Promise<ConnType>;
|
|
498
|
+
/**
|
|
499
|
+
* Manually attempts to connect to a client.
|
|
500
|
+
* @param to The client ID of the node to connect to.
|
|
501
|
+
*/
|
|
502
|
+
connect(to: TransportClientId): Promise<void>;
|
|
503
|
+
protected deleteSession({ session, closeHandshakingConnection, handshakingConn, }: {
|
|
504
|
+
session: Session<ConnType>;
|
|
505
|
+
closeHandshakingConnection: boolean;
|
|
506
|
+
handshakingConn?: ConnType;
|
|
507
|
+
}): void;
|
|
508
|
+
protected sendHandshake(to: TransportClientId, conn: ConnType): Promise<boolean>;
|
|
509
|
+
close(): void;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* ServiceContext exist for the purpose of declaration merging
|
|
514
|
+
* to extend the context with additional properties.
|
|
515
|
+
*
|
|
516
|
+
* For example:
|
|
517
|
+
*
|
|
518
|
+
* ```ts
|
|
519
|
+
* declare module '@replit/river' {
|
|
520
|
+
* interface ServiceContext {
|
|
521
|
+
* db: Database;
|
|
522
|
+
* }
|
|
523
|
+
* }
|
|
524
|
+
*
|
|
525
|
+
* createServer(someTransport, myServices, { extendedContext: { db: myDb } });
|
|
526
|
+
* ```
|
|
527
|
+
*
|
|
528
|
+
* Once you do this, your {@link ProcedureHandlerContext} will have `db` property on it.
|
|
529
|
+
*/
|
|
530
|
+
interface ServiceContext {
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* The parsed metadata schema for a service. This is the
|
|
534
|
+
* return value of the {@link ServerHandshakeOptions.validate}
|
|
535
|
+
* if the handshake extension is used.
|
|
536
|
+
*
|
|
537
|
+
* You should use declaration merging to extend this interface
|
|
538
|
+
* with the sanitized metadata.
|
|
539
|
+
*
|
|
540
|
+
* ```ts
|
|
541
|
+
* declare module '@replit/river' {
|
|
542
|
+
* interface ParsedMetadata {
|
|
543
|
+
* userId: number;
|
|
544
|
+
* }
|
|
545
|
+
* }
|
|
546
|
+
* ```
|
|
547
|
+
*/
|
|
548
|
+
interface ParsedMetadata {
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* This is passed to every procedure handler and contains various context-level
|
|
552
|
+
* information and utilities. This may be extended, see {@link ServiceContext}
|
|
553
|
+
*/
|
|
554
|
+
type ProcedureHandlerContext<State> = ServiceContext & {
|
|
555
|
+
/**
|
|
556
|
+
* State for this service as defined by the service definition.
|
|
557
|
+
*/
|
|
558
|
+
state: State;
|
|
559
|
+
/**
|
|
560
|
+
* Metadata parsed on the server. See {@link ParsedMetadata}
|
|
561
|
+
*/
|
|
562
|
+
metadata: ParsedMetadata;
|
|
563
|
+
/**
|
|
564
|
+
* The ID of the client that sent this request.
|
|
565
|
+
*/
|
|
566
|
+
from: TransportClientId;
|
|
567
|
+
/**
|
|
568
|
+
* An AbortController for this stream. This is used to abort the stream from the
|
|
569
|
+
* handler and notify the client that the stream was aborted.
|
|
570
|
+
*
|
|
571
|
+
* Important to note that this controller is owned by the handler, if you
|
|
572
|
+
* want to listen to aborts coming from the client, you should use the
|
|
573
|
+
* {@link clientAbortSignal}.
|
|
574
|
+
*
|
|
575
|
+
* Aborts are not the same as closing streams gracefully, please refer to
|
|
576
|
+
* the river documentation to understand the difference between the two concepts.
|
|
577
|
+
*/
|
|
578
|
+
abortController: AbortController;
|
|
579
|
+
/**
|
|
580
|
+
* You can listen to clientAbortSignal this to check if the client aborted the request,
|
|
581
|
+
* or if the request was aborted due to an unexpected disconnect from the calling
|
|
582
|
+
* session.
|
|
583
|
+
*
|
|
584
|
+
* If the procedure has a read stream (e.g. upload or stream), the procedure will
|
|
585
|
+
* notified of aborts as part of the stream, but you may still want to use
|
|
586
|
+
* this signal as it is triggered immediately after an abort comes in,
|
|
587
|
+
* in readStreams some data may be buffered before the abort result shows up.
|
|
588
|
+
*
|
|
589
|
+
* Important to note that this signal is owned by the client, you have a separate
|
|
590
|
+
* signal inside {@link abortController} for aborts triggered within the handler.
|
|
591
|
+
*
|
|
592
|
+
* Aborts are not the same as closing streams gracefully, please refer to
|
|
593
|
+
* the river documentation to understand the difference between the two concepts.
|
|
594
|
+
*/
|
|
595
|
+
clientAbortSignal: AbortSignal;
|
|
596
|
+
/**
|
|
597
|
+
* Lets you add a function that will run when the request is done, this can be
|
|
598
|
+
* due to an abort (from either side), error, or success. If the callback is
|
|
599
|
+
* added after the stream ended, it will run immediately.
|
|
600
|
+
*/
|
|
601
|
+
onRequestFinished: (callback: () => void) => void;
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
declare const StreamDrainedError: {
|
|
605
|
+
readonly code: "STREAM_DRAINED";
|
|
606
|
+
readonly message: "Stream was drained";
|
|
607
|
+
};
|
|
608
|
+
type ReadStreamResult<T, E extends Static<BaseErrorSchemaType>> = Result<T, E | typeof StreamDrainedError>;
|
|
609
|
+
/**
|
|
610
|
+
* A `ReadStream` represents a stream of data.
|
|
611
|
+
*
|
|
612
|
+
* This stream is not closable by the reader, the reader must wait for
|
|
613
|
+
* the writer to close it.
|
|
614
|
+
*
|
|
615
|
+
* The stream can only be locked (aka consumed) once and will remain
|
|
616
|
+
* locked, trying to lock the stream again will throw an TypeError.
|
|
617
|
+
*/
|
|
618
|
+
interface ReadStream<T, E extends Static<BaseErrorSchemaType>> {
|
|
619
|
+
/**
|
|
620
|
+
* Stream implements AsyncIterator API and can be consumed via
|
|
621
|
+
* for-await-of loops.
|
|
622
|
+
*
|
|
623
|
+
*/
|
|
624
|
+
[Symbol.asyncIterator](): {
|
|
625
|
+
next(): Promise<{
|
|
626
|
+
done: false;
|
|
627
|
+
value: ReadStreamResult<T, E>;
|
|
628
|
+
} | {
|
|
629
|
+
done: true;
|
|
630
|
+
value: undefined;
|
|
631
|
+
}>;
|
|
632
|
+
};
|
|
633
|
+
/**
|
|
634
|
+
* `asArray` locks the stream and returns a promise that resolves
|
|
635
|
+
* with an array of the stream's content when the stream is closed.
|
|
636
|
+
*
|
|
637
|
+
*/
|
|
638
|
+
asArray(): Promise<Array<ReadStreamResult<T, E>>>;
|
|
639
|
+
/**
|
|
640
|
+
* `drain` locks the stream and discards any existing or future data.
|
|
641
|
+
*
|
|
642
|
+
* If there is an existing Promise waiting for the next value,
|
|
643
|
+
* `drain` causes it to resolve with a {@link StreamDrainedError} error.
|
|
644
|
+
*/
|
|
645
|
+
drain(): undefined;
|
|
646
|
+
/**
|
|
647
|
+
* `isLocked` returns true if the stream is locked.
|
|
648
|
+
*/
|
|
649
|
+
isLocked(): boolean;
|
|
650
|
+
/**
|
|
651
|
+
* `onClose` registers a callback that will be called when the stream
|
|
652
|
+
* is closed. Returns a function that can be used to unregister the
|
|
653
|
+
* listener.
|
|
654
|
+
*/
|
|
655
|
+
onClose(cb: () => void): () => void;
|
|
656
|
+
/**
|
|
657
|
+
* `isClosed` returns true if the stream was closed by the writer.
|
|
658
|
+
*
|
|
659
|
+
* Note that the stream can still have queued data and can still be
|
|
660
|
+
* consumed.
|
|
661
|
+
*/
|
|
662
|
+
isClosed(): boolean;
|
|
663
|
+
/**
|
|
664
|
+
* `requestClose` sends a request to the writer to close the stream,
|
|
665
|
+
* and resolves the writer closes its end of the stream..
|
|
666
|
+
*
|
|
667
|
+
* The stream can still receive more data after the request is sent. If you
|
|
668
|
+
* no longer wish to use the stream, make sure to call `drain`.
|
|
669
|
+
*/
|
|
670
|
+
requestClose(): Promise<undefined>;
|
|
671
|
+
/**
|
|
672
|
+
* `isCloseRequested` checks if the reader has requested to close the stream.
|
|
673
|
+
*/
|
|
674
|
+
isCloseRequested(): boolean;
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* A `WriteStream` is a streams that can be written to.
|
|
678
|
+
*/
|
|
679
|
+
interface WriteStream<T> {
|
|
680
|
+
/**
|
|
681
|
+
* `write` writes a value to the stream. An error is thrown if writing to a closed stream.
|
|
682
|
+
*/
|
|
683
|
+
write(value: T): undefined;
|
|
684
|
+
/**
|
|
685
|
+
* `close` signals the closure of the write stream, informing the reader that the stream is complete.
|
|
686
|
+
* Calling close multiple times has no effect.
|
|
687
|
+
*/
|
|
688
|
+
close(): undefined;
|
|
689
|
+
/**
|
|
690
|
+
* `isCloseRequested` checks if the reader has requested to close the stream.
|
|
691
|
+
*/
|
|
692
|
+
isCloseRequested(): boolean;
|
|
693
|
+
/**
|
|
694
|
+
* `onCloseRequest` registers a callback that will be called when the stream's
|
|
695
|
+
* reader requests a close. Returns a function that can be used to unregister the
|
|
696
|
+
* listener.
|
|
697
|
+
*/
|
|
698
|
+
onCloseRequest(cb: () => void): () => void;
|
|
699
|
+
/**
|
|
700
|
+
* `isClosed` returns true if the stream was closed by the writer.
|
|
701
|
+
*/
|
|
702
|
+
isClosed(): boolean;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
interface CallOptions {
|
|
706
|
+
signal?: AbortSignal;
|
|
707
|
+
}
|
|
708
|
+
type RpcFn<Router extends AnyService, ProcName extends keyof Router['procedures']> = (init: ProcInit<Router, ProcName>, options?: CallOptions) => Promise<Result<ProcOutput<Router, ProcName>, ProcErrors<Router, ProcName>>>;
|
|
709
|
+
type UploadFn<Router extends AnyService, ProcName extends keyof Router['procedures']> = (init: ProcInit<Router, ProcName>, options?: CallOptions) => [
|
|
710
|
+
WriteStream<ProcInput<Router, ProcName>>,
|
|
711
|
+
() => Promise<Result<ProcOutput<Router, ProcName>, ProcErrors<Router, ProcName>>>
|
|
712
|
+
];
|
|
713
|
+
type StreamFn<Router extends AnyService, ProcName extends keyof Router['procedures']> = (init: ProcInit<Router, ProcName>, options?: CallOptions) => [
|
|
714
|
+
WriteStream<ProcInput<Router, ProcName>>,
|
|
715
|
+
ReadStream<ProcOutput<Router, ProcName>, ProcErrors<Router, ProcName>>
|
|
716
|
+
];
|
|
717
|
+
type SubscriptionFn<Router extends AnyService, ProcName extends keyof Router['procedures']> = (init: ProcInit<Router, ProcName>, options?: CallOptions) => ReadStream<ProcOutput<Router, ProcName>, ProcErrors<Router, ProcName>>;
|
|
718
|
+
/**
|
|
719
|
+
* A helper type to transform an actual service type into a type
|
|
720
|
+
* we can case to in the proxy.
|
|
721
|
+
* @template Router - The type of the Router.
|
|
722
|
+
*/
|
|
723
|
+
type ServiceClient<Router extends AnyService> = {
|
|
724
|
+
[ProcName in keyof Router['procedures']]: ProcType<Router, ProcName> extends 'rpc' ? {
|
|
725
|
+
rpc: RpcFn<Router, ProcName>;
|
|
726
|
+
} : ProcType<Router, ProcName> extends 'upload' ? {
|
|
727
|
+
upload: UploadFn<Router, ProcName>;
|
|
728
|
+
} : ProcType<Router, ProcName> extends 'stream' ? {
|
|
729
|
+
stream: StreamFn<Router, ProcName>;
|
|
730
|
+
} : ProcType<Router, ProcName> extends 'subscription' ? {
|
|
731
|
+
subscribe: SubscriptionFn<Router, ProcName>;
|
|
732
|
+
} : never;
|
|
733
|
+
};
|
|
734
|
+
/**
|
|
735
|
+
* Defines a type that represents a client for a server with a set of services.
|
|
736
|
+
* @template Srv - The type of the server.
|
|
737
|
+
*/
|
|
738
|
+
type Client<Services extends AnyServiceSchemaMap, IS extends InstantiatedServiceSchemaMap<Services> = InstantiatedServiceSchemaMap<Services>> = {
|
|
739
|
+
[SvcName in keyof IS]: ServiceClient<IS[SvcName]>;
|
|
740
|
+
};
|
|
741
|
+
interface ClientOptions {
|
|
742
|
+
connectOnInvoke: boolean;
|
|
743
|
+
eagerlyConnect: boolean;
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Creates a client for a given server using the provided transport.
|
|
747
|
+
* Note that the client only needs the type of the server, not the actual
|
|
748
|
+
* server definition itself.
|
|
749
|
+
*
|
|
750
|
+
* This relies on a proxy to dynamically create the client, so the client
|
|
751
|
+
* will be typed as if it were the actual server with the appropriate services
|
|
752
|
+
* and procedures.
|
|
753
|
+
*
|
|
754
|
+
* @template Srv - The type of the server.
|
|
755
|
+
* @param {Transport} transport - The transport to use for communication.
|
|
756
|
+
* @param {TransportClientId} serverId - The ID of the server to connect to.
|
|
757
|
+
* @param {Partial<ClientOptions>} providedClientOptions - The options for the client.
|
|
758
|
+
* @returns The client for the server.
|
|
759
|
+
*/
|
|
760
|
+
declare function createClient<ServiceSchemaMap extends AnyServiceSchemaMap>(transport: ClientTransport<Connection>, serverId: TransportClientId, providedClientOptions?: Partial<ClientOptions & {
|
|
761
|
+
handshakeOptions: ClientHandshakeOptions;
|
|
762
|
+
}>): Client<ServiceSchemaMap>;
|
|
763
|
+
|
|
764
|
+
type TLiteralString = TLiteral<string>;
|
|
765
|
+
type BaseErrorSchemaType = TObject<{
|
|
766
|
+
code: TLiteralString | TUnion<Array<TLiteralString>>;
|
|
767
|
+
message: TLiteralString | TString;
|
|
768
|
+
}> | TObject<{
|
|
769
|
+
code: TLiteralString | TUnion<Array<TLiteralString>>;
|
|
770
|
+
message: TLiteralString | TString;
|
|
771
|
+
extras: TSchema;
|
|
772
|
+
}>;
|
|
773
|
+
interface OkResult<T> {
|
|
774
|
+
ok: true;
|
|
775
|
+
payload: T;
|
|
776
|
+
}
|
|
777
|
+
interface ErrResult<Err extends Static<BaseErrorSchemaType>> {
|
|
778
|
+
ok: false;
|
|
779
|
+
payload: Err;
|
|
780
|
+
}
|
|
781
|
+
type Result<T, Err extends Static<BaseErrorSchemaType>> = OkResult<T> | ErrResult<Err>;
|
|
782
|
+
declare function Ok<const T extends Array<unknown>>(p: T): OkResult<T>;
|
|
783
|
+
declare function Ok<const T extends ReadonlyArray<unknown>>(p: T): OkResult<T>;
|
|
784
|
+
declare function Ok<const T>(payload: T): OkResult<T>;
|
|
785
|
+
declare function Err<const Err extends Static<BaseErrorSchemaType>>(error: Err): ErrResult<Err>;
|
|
786
|
+
/**
|
|
787
|
+
* Refine a {@link Result} type to its returned payload.
|
|
788
|
+
*/
|
|
789
|
+
type ResultUnwrapOk<R> = R extends Result<infer T, infer __E> ? T : never;
|
|
790
|
+
/**
|
|
791
|
+
* Refine a {@link Result} type to its error payload.
|
|
792
|
+
*/
|
|
793
|
+
type ResultUnwrapErr<R> = R extends Result<infer __T, infer Err> ? Err : never;
|
|
794
|
+
/**
|
|
795
|
+
* Retrieve the output type for a procedure, represented as a {@link Result}
|
|
796
|
+
* type.
|
|
797
|
+
* Example:
|
|
798
|
+
* ```
|
|
799
|
+
* type Message = Output<typeof client, 'serviceName', 'procedureName'>
|
|
800
|
+
* ```
|
|
801
|
+
*/
|
|
802
|
+
type Output<RiverClient, ServiceName extends keyof RiverClient, ProcedureName extends keyof RiverClient[ServiceName], Procedure = RiverClient[ServiceName][ProcedureName], Fn extends (...args: never) => unknown = (...args: never) => unknown> = RiverClient extends Client<infer __ServiceSchemaMap> ? Procedure extends object ? Procedure extends object & {
|
|
803
|
+
rpc: infer RpcHandler extends Fn;
|
|
804
|
+
} ? Awaited<ReturnType<RpcHandler>> : Procedure extends object & {
|
|
805
|
+
upload: infer UploadHandler extends Fn;
|
|
806
|
+
} ? ReturnType<UploadHandler> extends [
|
|
807
|
+
infer __UploadInputMessage,
|
|
808
|
+
(...args: never) => Promise<infer UploadOutputMessage>
|
|
809
|
+
] ? UploadOutputMessage : never : Procedure extends object & {
|
|
810
|
+
stream: infer StreamHandler extends Fn;
|
|
811
|
+
} ? ReturnType<StreamHandler> extends [
|
|
812
|
+
infer __StreamInputMessage,
|
|
813
|
+
ReadStream<infer StreamOutputMessage, Static<BaseErrorSchemaType>>
|
|
814
|
+
] ? StreamOutputMessage : never : Procedure extends object & {
|
|
815
|
+
subscribe: infer SubscriptionHandler extends Fn;
|
|
816
|
+
} ? Awaited<ReturnType<SubscriptionHandler>> extends ReadStream<infer SubscriptionOutputMessage, Static<BaseErrorSchemaType>> ? SubscriptionOutputMessage : never : never : never : never;
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* Brands a type to prevent it from being directly constructed.
|
|
820
|
+
*/
|
|
821
|
+
type Branded<T> = T & {
|
|
822
|
+
readonly __BRAND_DO_NOT_USE: unique symbol;
|
|
823
|
+
};
|
|
824
|
+
/**
|
|
825
|
+
* Unbrands a {@link Branded} type.
|
|
826
|
+
*/
|
|
827
|
+
type Unbranded<T> = T extends Branded<infer U> ? U : never;
|
|
828
|
+
/**
|
|
829
|
+
* The valid {@link Procedure} types. The `stream` and `upload` types can optionally have a
|
|
830
|
+
* different type for the very first initialization message. The suffixless types correspond to
|
|
831
|
+
* gRPC's four combinations of stream / non-stream in each direction.
|
|
832
|
+
*/
|
|
833
|
+
type ValidProcType = 'rpc' | 'upload' | 'subscription' | 'stream';
|
|
834
|
+
/**
|
|
835
|
+
* Represents the payload type for {@link Procedure}s.
|
|
836
|
+
*/
|
|
837
|
+
type PayloadType = TSchema;
|
|
838
|
+
/**
|
|
839
|
+
* UNCAUGHT_ERROR_CODE is the code that is used when an error is thrown
|
|
840
|
+
* inside a procedure handler that's not required.
|
|
841
|
+
*/
|
|
842
|
+
declare const UNCAUGHT_ERROR_CODE: "UNCAUGHT_ERROR";
|
|
843
|
+
/**
|
|
844
|
+
* UNEXPECTED_DISCONNECT_CODE is the code used the stream's session
|
|
845
|
+
* disconnect unexpetedly.
|
|
846
|
+
*/
|
|
847
|
+
declare const UNEXPECTED_DISCONNECT_CODE: "UNEXPECTED_DISCONNECT";
|
|
848
|
+
/**
|
|
849
|
+
* INVALID_REQUEST_CODE is the code used when a client's request is invalid.
|
|
850
|
+
*/
|
|
851
|
+
declare const INVALID_REQUEST_CODE: "INVALID_REQUEST";
|
|
852
|
+
/**
|
|
853
|
+
* OutputReaderErrorSchema is the schema for all the errors that can be
|
|
854
|
+
* emitted in the Output ReadStream on the client.
|
|
855
|
+
*/
|
|
856
|
+
declare const OutputReaderErrorSchema: _sinclair_typebox.TObject<{
|
|
857
|
+
code: TUnion<[_sinclair_typebox.TLiteral<"INTERNAL_RIVER_ERROR">, _sinclair_typebox.TLiteral<"UNCAUGHT_ERROR">, _sinclair_typebox.TLiteral<"UNEXPECTED_DISCONNECT">, _sinclair_typebox.TLiteral<"INVALID_REQUEST">, _sinclair_typebox.TLiteral<"ABORT">]>;
|
|
858
|
+
message: _sinclair_typebox.TString;
|
|
859
|
+
}>;
|
|
860
|
+
/**
|
|
861
|
+
* InputReaderErrorSchema is the schema for all the errors that can be
|
|
862
|
+
* emitted in the Input ReadStream on the server.
|
|
863
|
+
*/
|
|
864
|
+
declare const InputReaderErrorSchema: _sinclair_typebox.TObject<{
|
|
865
|
+
code: TUnion<[_sinclair_typebox.TLiteral<"UNCAUGHT_ERROR">, _sinclair_typebox.TLiteral<"UNEXPECTED_DISCONNECT">, _sinclair_typebox.TLiteral<"INVALID_REQUEST">, _sinclair_typebox.TLiteral<"ABORT">]>;
|
|
866
|
+
message: _sinclair_typebox.TString;
|
|
867
|
+
}>;
|
|
868
|
+
/**
|
|
869
|
+
* Represents an acceptable schema to pass to a procedure.
|
|
870
|
+
* Just a type of a schema, not an actual schema.
|
|
871
|
+
*/
|
|
872
|
+
type ProcedureErrorSchemaType = TUnion<Array<BaseErrorSchemaType>> | BaseErrorSchemaType | TNever;
|
|
873
|
+
/**
|
|
874
|
+
* Procedure for a single message in both directions (1:1).
|
|
875
|
+
*
|
|
876
|
+
* @template State - The context state object.
|
|
877
|
+
* @template Init - The TypeBox schema of the initialization object.
|
|
878
|
+
* @template Output - The TypeBox schema of the output object.
|
|
879
|
+
* @template Err - The TypeBox schema of the error object.
|
|
880
|
+
*/
|
|
881
|
+
interface RpcProcedure<State, Init extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType> {
|
|
882
|
+
type: 'rpc';
|
|
883
|
+
init: Init;
|
|
884
|
+
output: Output;
|
|
885
|
+
errors: Err;
|
|
886
|
+
description?: string;
|
|
887
|
+
handler(context: ProcedureHandlerContext<State>, init: Static<Init>): Promise<Result<Static<Output>, Static<Err>>>;
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* Procedure for a client-stream (potentially preceded by an initialization message),
|
|
891
|
+
* single message from server (n:1).
|
|
892
|
+
*
|
|
893
|
+
* @template State - The context state object.
|
|
894
|
+
* @template Init - The TypeBox schema of the initialization object.
|
|
895
|
+
* @template Input - The TypeBox schema of the input object.
|
|
896
|
+
* @template Output - The TypeBox schema of the output object.
|
|
897
|
+
* @template Err - The TypeBox schema of the error object.
|
|
898
|
+
*/
|
|
899
|
+
interface UploadProcedure<State, Init extends PayloadType, Input extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType> {
|
|
900
|
+
type: 'upload';
|
|
901
|
+
init: Init;
|
|
902
|
+
input: Input;
|
|
903
|
+
output: Output;
|
|
904
|
+
errors: Err;
|
|
905
|
+
description?: string;
|
|
906
|
+
handler(context: ProcedureHandlerContext<State>, init: Static<Init>, input: ReadStream<Static<Input>, Static<typeof InputReaderErrorSchema>>): Promise<Result<Static<Output>, Static<Err>>>;
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* Procedure for a single message from client, stream from server (1:n).
|
|
910
|
+
*
|
|
911
|
+
* @template State - The context state object.
|
|
912
|
+
* @template Init - The TypeBox schema of the initialization object.
|
|
913
|
+
* @template Output - The TypeBox schema of the output object.
|
|
914
|
+
* @template Err - The TypeBox schema of the error object.
|
|
915
|
+
*/
|
|
916
|
+
interface SubscriptionProcedure<State, Init extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType> {
|
|
917
|
+
type: 'subscription';
|
|
918
|
+
init: Init;
|
|
919
|
+
output: Output;
|
|
920
|
+
errors: Err;
|
|
921
|
+
description?: string;
|
|
922
|
+
handler(context: ProcedureHandlerContext<State>, init: Static<Init>, output: WriteStream<Result<Static<Output>, Static<Err>>>): Promise<void | undefined>;
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* Procedure for a bidirectional stream (potentially preceded by an initialization message),
|
|
926
|
+
* (n:n).
|
|
927
|
+
*
|
|
928
|
+
* @template State - The context state object.
|
|
929
|
+
* @template Init - The TypeBox schema of the initialization object.
|
|
930
|
+
* @template Input - The TypeBox schema of the input object.
|
|
931
|
+
* @template Output - The TypeBox schema of the output object.
|
|
932
|
+
* @template Err - The TypeBox schema of the error object.
|
|
933
|
+
*/
|
|
934
|
+
interface StreamProcedure<State, Init extends PayloadType, Input extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType> {
|
|
935
|
+
type: 'stream';
|
|
936
|
+
init: Init;
|
|
937
|
+
input: Input;
|
|
938
|
+
output: Output;
|
|
939
|
+
errors: Err;
|
|
940
|
+
description?: string;
|
|
941
|
+
handler(context: ProcedureHandlerContext<State>, init: Static<Init>, input: ReadStream<Static<Input>, Static<typeof InputReaderErrorSchema>>, output: WriteStream<Result<Static<Output>, Static<Err>>>): Promise<void | undefined>;
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Represents any {@link Procedure} type.
|
|
945
|
+
*
|
|
946
|
+
* @template State - The context state object. You can provide this to constrain
|
|
947
|
+
* the type of procedures.
|
|
948
|
+
*/
|
|
949
|
+
type AnyProcedure<State = object> = Procedure<State, ValidProcType, PayloadType, PayloadType | null, PayloadType, ProcedureErrorSchemaType>;
|
|
950
|
+
/**
|
|
951
|
+
* Represents a map of {@link Procedure}s.
|
|
952
|
+
*
|
|
953
|
+
* @template State - The context state object. You can provide this to constrain
|
|
954
|
+
* the type of procedures.
|
|
955
|
+
*/
|
|
956
|
+
type ProcedureMap<State = object> = Record<string, AnyProcedure<State>>;
|
|
957
|
+
/**
|
|
958
|
+
* Creates an {@link RpcProcedure}.
|
|
959
|
+
*/
|
|
960
|
+
declare function rpc<State, Init extends PayloadType, Output extends PayloadType>(def: {
|
|
961
|
+
init: Init;
|
|
962
|
+
output: Output;
|
|
963
|
+
errors?: never;
|
|
964
|
+
description?: string;
|
|
965
|
+
handler: RpcProcedure<State, Init, Output, TNever>['handler'];
|
|
966
|
+
}): Branded<RpcProcedure<State, Init, Output, TNever>>;
|
|
967
|
+
declare function rpc<State, Init extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType>(def: {
|
|
968
|
+
init: Init;
|
|
969
|
+
output: Output;
|
|
970
|
+
errors: Err;
|
|
971
|
+
description?: string;
|
|
972
|
+
handler: RpcProcedure<State, Init, Output, Err>['handler'];
|
|
973
|
+
}): Branded<RpcProcedure<State, Init, Output, Err>>;
|
|
974
|
+
/**
|
|
975
|
+
* Creates an {@link UploadProcedure}, optionally with an initialization message.
|
|
976
|
+
*/
|
|
977
|
+
declare function upload<State, Init extends PayloadType, Input extends PayloadType, Output extends PayloadType>(def: {
|
|
978
|
+
init: Init;
|
|
979
|
+
input: Input;
|
|
980
|
+
output: Output;
|
|
981
|
+
errors?: never;
|
|
982
|
+
description?: string;
|
|
983
|
+
handler: UploadProcedure<State, Init, Input, Output, TNever>['handler'];
|
|
984
|
+
}): Branded<UploadProcedure<State, Init, Input, Output, TNever>>;
|
|
985
|
+
declare function upload<State, Init extends PayloadType, Input extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType>(def: {
|
|
986
|
+
init: Init;
|
|
987
|
+
input: Input;
|
|
988
|
+
output: Output;
|
|
989
|
+
errors: Err;
|
|
990
|
+
description?: string;
|
|
991
|
+
handler: UploadProcedure<State, Init, Input, Output, Err>['handler'];
|
|
992
|
+
}): Branded<UploadProcedure<State, Init, Input, Output, Err>>;
|
|
993
|
+
/**
|
|
994
|
+
* Creates a {@link SubscriptionProcedure}.
|
|
995
|
+
*/
|
|
996
|
+
declare function subscription<State, Init extends PayloadType, Output extends PayloadType>(def: {
|
|
997
|
+
init: Init;
|
|
998
|
+
output: Output;
|
|
999
|
+
errors?: never;
|
|
1000
|
+
description?: string;
|
|
1001
|
+
handler: SubscriptionProcedure<State, Init, Output, TNever>['handler'];
|
|
1002
|
+
}): Branded<SubscriptionProcedure<State, Init, Output, TNever>>;
|
|
1003
|
+
declare function subscription<State, Init extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType>(def: {
|
|
1004
|
+
init: Init;
|
|
1005
|
+
output: Output;
|
|
1006
|
+
errors: Err;
|
|
1007
|
+
description?: string;
|
|
1008
|
+
handler: SubscriptionProcedure<State, Init, Output, Err>['handler'];
|
|
1009
|
+
}): Branded<SubscriptionProcedure<State, Init, Output, Err>>;
|
|
1010
|
+
/**
|
|
1011
|
+
* Creates a {@link StreamProcedure}, optionally with an initialization message.
|
|
1012
|
+
*/
|
|
1013
|
+
declare function stream<State, Init extends PayloadType, Input extends PayloadType, Output extends PayloadType>(def: {
|
|
1014
|
+
init: Init;
|
|
1015
|
+
input: Input;
|
|
1016
|
+
output: Output;
|
|
1017
|
+
errors?: never;
|
|
1018
|
+
description?: string;
|
|
1019
|
+
handler: StreamProcedure<State, Init, Input, Output, TNever>['handler'];
|
|
1020
|
+
}): Branded<StreamProcedure<State, Init, Input, Output, TNever>>;
|
|
1021
|
+
declare function stream<State, Init extends PayloadType, Input extends PayloadType, Output extends PayloadType, Err extends ProcedureErrorSchemaType>(def: {
|
|
1022
|
+
init: Init;
|
|
1023
|
+
input: Input;
|
|
1024
|
+
output: Output;
|
|
1025
|
+
errors: Err;
|
|
1026
|
+
description?: string;
|
|
1027
|
+
handler: StreamProcedure<State, Init, Input, Output, Err>['handler'];
|
|
1028
|
+
}): Branded<StreamProcedure<State, Init, Input, Output, Err>>;
|
|
1029
|
+
/**
|
|
1030
|
+
* Defines a Procedure type that can be a:
|
|
1031
|
+
* - {@link RpcProcedure} for a single message in both directions (1:1)
|
|
1032
|
+
* - {@link UploadProcedure} for a client-stream (potentially preceded by an
|
|
1033
|
+
* initialization message)
|
|
1034
|
+
* - {@link SubscriptionProcedure} for a single message from client, stream from server (1:n)
|
|
1035
|
+
* - {@link StreamProcedure} for a bidirectional stream (potentially preceded by an
|
|
1036
|
+
* initialization message)
|
|
1037
|
+
*
|
|
1038
|
+
* @template State - The TypeBox schema of the state object.
|
|
1039
|
+
* @template Ty - The type of the procedure.
|
|
1040
|
+
* @template Input - The TypeBox schema of the input object.
|
|
1041
|
+
* @template Init - The TypeBox schema of the input initialization object, if any.
|
|
1042
|
+
* @template Output - The TypeBox schema of the output object.
|
|
1043
|
+
*/
|
|
1044
|
+
type Procedure<State, Ty extends ValidProcType, Init extends PayloadType, Input extends PayloadType | null, Output extends PayloadType, Err extends ProcedureErrorSchemaType> = {
|
|
1045
|
+
type: Ty;
|
|
1046
|
+
} & (Input extends PayloadType ? Ty extends 'upload' ? UploadProcedure<State, Init, Input, Output, Err> : Ty extends 'stream' ? StreamProcedure<State, Init, Input, Output, Err> : never : Ty extends 'rpc' ? RpcProcedure<State, Init, Output, Err> : Ty extends 'subscription' ? SubscriptionProcedure<State, Init, Output, Err> : never);
|
|
1047
|
+
/**
|
|
1048
|
+
* Holds the {@link Procedure} creation functions. Use these to create
|
|
1049
|
+
* procedures for services. You aren't allowed to create procedures directly.
|
|
1050
|
+
*/
|
|
1051
|
+
declare const Procedure: {
|
|
1052
|
+
rpc: typeof rpc;
|
|
1053
|
+
upload: typeof upload;
|
|
1054
|
+
subscription: typeof subscription;
|
|
1055
|
+
stream: typeof stream;
|
|
1056
|
+
};
|
|
1057
|
+
|
|
1058
|
+
/**
|
|
1059
|
+
* An instantiated service, probably from a {@link ServiceSchema}.
|
|
1060
|
+
*
|
|
1061
|
+
* You shouldn't construct these directly, use {@link ServiceSchema} instead.
|
|
1062
|
+
*/
|
|
1063
|
+
interface Service<State extends object, Procs extends ProcedureMap<State>> {
|
|
1064
|
+
readonly state: State;
|
|
1065
|
+
readonly procedures: Procs;
|
|
1066
|
+
}
|
|
1067
|
+
/**
|
|
1068
|
+
* Represents any {@link Service} object.
|
|
1069
|
+
*/
|
|
1070
|
+
type AnyService = Service<object, ProcedureMap>;
|
|
1071
|
+
/**
|
|
1072
|
+
* Represents any {@link ServiceSchema} object.
|
|
1073
|
+
*/
|
|
1074
|
+
type AnyServiceSchema = ServiceSchema<object, ProcedureMap>;
|
|
1075
|
+
/**
|
|
1076
|
+
* A dictionary of {@link ServiceSchema}s, where the key is the service name.
|
|
1077
|
+
*/
|
|
1078
|
+
type AnyServiceSchemaMap = Record<string, AnyServiceSchema>;
|
|
1079
|
+
/**
|
|
1080
|
+
* Takes a {@link AnyServiceSchemaMap} and returns a dictionary of instantiated
|
|
1081
|
+
* services.
|
|
1082
|
+
*/
|
|
1083
|
+
type InstantiatedServiceSchemaMap<T extends AnyServiceSchemaMap> = {
|
|
1084
|
+
[K in keyof T]: T[K] extends ServiceSchema<infer S, infer P> ? Service<S, P> : never;
|
|
1085
|
+
};
|
|
1086
|
+
/**
|
|
1087
|
+
* Helper to get the type definition for a specific handler of a procedure in a service.
|
|
1088
|
+
* @template S - The service.
|
|
1089
|
+
* @template ProcName - The name of the procedure.
|
|
1090
|
+
*/
|
|
1091
|
+
type ProcHandler<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['handler'];
|
|
1092
|
+
/**
|
|
1093
|
+
* Helper to get the type definition for the procedure init type of a service.
|
|
1094
|
+
* @template S - The service.
|
|
1095
|
+
* @template ProcName - The name of the procedure.
|
|
1096
|
+
*/
|
|
1097
|
+
type ProcInit<S extends AnyService, ProcName extends keyof S['procedures']> = Static<S['procedures'][ProcName]['init']>;
|
|
1098
|
+
/**
|
|
1099
|
+
* Helper to get the type definition for the procedure input of a service.
|
|
1100
|
+
* @template S - The service.
|
|
1101
|
+
* @template ProcName - The name of the procedure.
|
|
1102
|
+
*/
|
|
1103
|
+
type ProcInput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName] extends {
|
|
1104
|
+
input: PayloadType;
|
|
1105
|
+
} ? Static<S['procedures'][ProcName]['input']> : never;
|
|
1106
|
+
/**
|
|
1107
|
+
* Helper to get the type definition for the procedure output of a service.
|
|
1108
|
+
* @template S - The service.
|
|
1109
|
+
* @template ProcName - The name of the procedure.
|
|
1110
|
+
*/
|
|
1111
|
+
type ProcOutput<S extends AnyService, ProcName extends keyof S['procedures']> = Static<S['procedures'][ProcName]['output']>;
|
|
1112
|
+
/**
|
|
1113
|
+
* Helper to get the type definition for the procedure errors of a service.
|
|
1114
|
+
* @template S - The service.
|
|
1115
|
+
* @template ProcName - The name of the procedure.
|
|
1116
|
+
*/
|
|
1117
|
+
type ProcErrors<S extends AnyService, ProcName extends keyof S['procedures']> = Static<S['procedures'][ProcName]['errors']> | Static<typeof OutputReaderErrorSchema>;
|
|
1118
|
+
/**
|
|
1119
|
+
* Helper to get the type of procedure in a service.
|
|
1120
|
+
* @template S - The service.
|
|
1121
|
+
* @template ProcName - The name of the procedure.
|
|
1122
|
+
*/
|
|
1123
|
+
type ProcType<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['type'];
|
|
1124
|
+
/**
|
|
1125
|
+
* A list of procedures where every procedure is "branded", as-in the procedure
|
|
1126
|
+
* was created via the {@link Procedure} constructors.
|
|
1127
|
+
*/
|
|
1128
|
+
type BrandedProcedureMap<State> = Record<string, Branded<AnyProcedure<State>>>;
|
|
1129
|
+
/**
|
|
1130
|
+
* The configuration for a service.
|
|
1131
|
+
*/
|
|
1132
|
+
interface ServiceConfiguration<State extends object> {
|
|
1133
|
+
/**
|
|
1134
|
+
* A factory function for creating a fresh state.
|
|
1135
|
+
*/
|
|
1136
|
+
initializeState: (extendedContext: ServiceContext) => State;
|
|
1137
|
+
}
|
|
1138
|
+
interface SerializedProcedureSchema {
|
|
1139
|
+
init: PayloadType;
|
|
1140
|
+
input?: PayloadType;
|
|
1141
|
+
output: PayloadType;
|
|
1142
|
+
errors?: ProcedureErrorSchemaType;
|
|
1143
|
+
type: 'rpc' | 'subscription' | 'upload' | 'stream';
|
|
1144
|
+
}
|
|
1145
|
+
interface SerializedServiceSchema {
|
|
1146
|
+
procedures: Record<string, SerializedProcedureSchema>;
|
|
1147
|
+
}
|
|
1148
|
+
interface SerializedServerSchema {
|
|
1149
|
+
handshakeSchema?: TSchema;
|
|
1150
|
+
services: Record<string, SerializedServiceSchema>;
|
|
1151
|
+
}
|
|
1152
|
+
declare function serializeSchema(services: AnyServiceSchemaMap, handshakeSchema?: TSchema): SerializedServerSchema;
|
|
1153
|
+
/**
|
|
1154
|
+
* The schema for a {@link Service}. This is used to define a service, specifically
|
|
1155
|
+
* its initial state and procedures.
|
|
1156
|
+
*
|
|
1157
|
+
* There are two ways to define a service:
|
|
1158
|
+
* 1. the {@link ServiceSchema.define} static method, which takes a configuration and
|
|
1159
|
+
* a list of procedures directly. Use this to ergonomically define a service schema
|
|
1160
|
+
* in one go. Good for smaller services, especially if they're stateless.
|
|
1161
|
+
* 2. the {@link ServiceSchema.scaffold} static method, which creates a scaffold that
|
|
1162
|
+
* can be used to define procedures separately from the configuration. Use this to
|
|
1163
|
+
* better organize your service's definition, especially if it's a large service.
|
|
1164
|
+
* You can also use it in a builder pattern to define the service in a more
|
|
1165
|
+
* fluent way.
|
|
1166
|
+
*
|
|
1167
|
+
* See the static methods for more information and examples.
|
|
1168
|
+
*
|
|
1169
|
+
* When defining procedures, use the {@link Procedure} constructors to create them.
|
|
1170
|
+
*/
|
|
1171
|
+
declare class ServiceSchema<State extends object, Procedures extends ProcedureMap<State>> {
|
|
1172
|
+
/**
|
|
1173
|
+
* Factory function for creating a fresh state.
|
|
1174
|
+
*/
|
|
1175
|
+
protected readonly initializeState: (extendedContext: ServiceContext) => State;
|
|
1176
|
+
/**
|
|
1177
|
+
* The procedures for this service.
|
|
1178
|
+
*/
|
|
1179
|
+
readonly procedures: Procedures;
|
|
1180
|
+
/**
|
|
1181
|
+
* @param config - The configuration for this service.
|
|
1182
|
+
* @param procedures - The procedures for this service.
|
|
1183
|
+
*/
|
|
1184
|
+
protected constructor(config: ServiceConfiguration<State>, procedures: Procedures);
|
|
1185
|
+
/**
|
|
1186
|
+
* Creates a {@link ServiceScaffold}, which can be used to define procedures
|
|
1187
|
+
* that can then be merged into a {@link ServiceSchema}, via the scaffold's
|
|
1188
|
+
* `finalize` method.
|
|
1189
|
+
*
|
|
1190
|
+
* There are two patterns that work well with this method. The first is using
|
|
1191
|
+
* it to separate the definition of procedures from the definition of the
|
|
1192
|
+
* service's configuration:
|
|
1193
|
+
* ```ts
|
|
1194
|
+
* const MyServiceScaffold = ServiceSchema.scaffold({
|
|
1195
|
+
* initializeState: () => ({ count: 0 }),
|
|
1196
|
+
* });
|
|
1197
|
+
*
|
|
1198
|
+
* const incrementProcedures = MyServiceScaffold.procedures({
|
|
1199
|
+
* increment: Procedure.rpc({
|
|
1200
|
+
* init: Type.Object({ amount: Type.Number() }),
|
|
1201
|
+
* output: Type.Object({ current: Type.Number() }),
|
|
1202
|
+
* async handler(ctx, init) {
|
|
1203
|
+
* ctx.state.count += init.amount;
|
|
1204
|
+
* return Ok({ current: ctx.state.count });
|
|
1205
|
+
* }
|
|
1206
|
+
* }),
|
|
1207
|
+
* })
|
|
1208
|
+
*
|
|
1209
|
+
* const MyService = MyServiceScaffold.finalize({
|
|
1210
|
+
* ...incrementProcedures,
|
|
1211
|
+
* // you can also directly define procedures here
|
|
1212
|
+
* });
|
|
1213
|
+
* ```
|
|
1214
|
+
* This might be really handy if you have a very large service and you're
|
|
1215
|
+
* wanting to split it over multiple files. You can define the scaffold
|
|
1216
|
+
* in one file, and then import that scaffold in other files where you
|
|
1217
|
+
* define procedures - and then finally import the scaffolds and your
|
|
1218
|
+
* procedure objects in a final file where you finalize the scaffold into
|
|
1219
|
+
* a service schema.
|
|
1220
|
+
*
|
|
1221
|
+
* The other way is to use it like in a builder pattern:
|
|
1222
|
+
* ```ts
|
|
1223
|
+
* const MyService = ServiceSchema
|
|
1224
|
+
* .scaffold({ initializeState: () => ({ count: 0 }) })
|
|
1225
|
+
* .finalize({
|
|
1226
|
+
* increment: Procedure.rpc({
|
|
1227
|
+
* init: Type.Object({ amount: Type.Number() }),
|
|
1228
|
+
* output: Type.Object({ current: Type.Number() }),
|
|
1229
|
+
* async handler(ctx, init) {
|
|
1230
|
+
* ctx.state.count += init.amount;
|
|
1231
|
+
* return Ok({ current: ctx.state.count });
|
|
1232
|
+
* }
|
|
1233
|
+
* }),
|
|
1234
|
+
* })
|
|
1235
|
+
* ```
|
|
1236
|
+
* Depending on your preferences, this may be a more appealing way to define
|
|
1237
|
+
* a schema versus using the {@link ServiceSchema.define} method.
|
|
1238
|
+
*/
|
|
1239
|
+
static scaffold<State extends object>(config: ServiceConfiguration<State>): ServiceScaffold<State>;
|
|
1240
|
+
/**
|
|
1241
|
+
* Creates a new {@link ServiceSchema} with the given configuration and procedures.
|
|
1242
|
+
*
|
|
1243
|
+
* All procedures must be created with the {@link Procedure} constructors.
|
|
1244
|
+
*
|
|
1245
|
+
* NOTE: There is an overload that lets you just provide the procedures alone if your
|
|
1246
|
+
* service has no state.
|
|
1247
|
+
*
|
|
1248
|
+
* @param config - The configuration for this service.
|
|
1249
|
+
* @param procedures - The procedures for this service.
|
|
1250
|
+
*
|
|
1251
|
+
* @example
|
|
1252
|
+
* ```
|
|
1253
|
+
* const service = ServiceSchema.define(
|
|
1254
|
+
* { initializeState: () => ({ count: 0 }) },
|
|
1255
|
+
* {
|
|
1256
|
+
* increment: Procedure.rpc({
|
|
1257
|
+
* init: Type.Object({ amount: Type.Number() }),
|
|
1258
|
+
* output: Type.Object({ current: Type.Number() }),
|
|
1259
|
+
* async handler(ctx, init) {
|
|
1260
|
+
* ctx.state.count += init.amount;
|
|
1261
|
+
* return Ok({ current: ctx.state.count });
|
|
1262
|
+
* }
|
|
1263
|
+
* }),
|
|
1264
|
+
* },
|
|
1265
|
+
* );
|
|
1266
|
+
* ```
|
|
1267
|
+
*/
|
|
1268
|
+
static define<State extends object, Procedures extends BrandedProcedureMap<State>>(config: ServiceConfiguration<State>, procedures: Procedures): ServiceSchema<State, {
|
|
1269
|
+
[K in keyof Procedures]: Unbranded<Procedures[K]>;
|
|
1270
|
+
}>;
|
|
1271
|
+
/**
|
|
1272
|
+
* Creates a new {@link ServiceSchema} with the given procedures.
|
|
1273
|
+
*
|
|
1274
|
+
* All procedures must be created with the {@link Procedure} constructors.
|
|
1275
|
+
*
|
|
1276
|
+
* NOTE: There is an overload that lets you provide configuration as well,
|
|
1277
|
+
* if your service has extra configuration like a state.
|
|
1278
|
+
*
|
|
1279
|
+
* @param procedures - The procedures for this service.
|
|
1280
|
+
*
|
|
1281
|
+
* @example
|
|
1282
|
+
* ```
|
|
1283
|
+
* const service = ServiceSchema.define({
|
|
1284
|
+
* add: Procedure.rpc({
|
|
1285
|
+
* init: Type.Object({ a: Type.Number(), b: Type.Number() }),
|
|
1286
|
+
* output: Type.Object({ result: Type.Number() }),
|
|
1287
|
+
* async handler(ctx, init) {
|
|
1288
|
+
* return Ok({ result: init.a + init.b });
|
|
1289
|
+
* }
|
|
1290
|
+
* }),
|
|
1291
|
+
* });
|
|
1292
|
+
*/
|
|
1293
|
+
static define<Procedures extends BrandedProcedureMap<Record<string, never>>>(procedures: Procedures): ServiceSchema<Record<string, never>, {
|
|
1294
|
+
[K in keyof Procedures]: Unbranded<Procedures[K]>;
|
|
1295
|
+
}>;
|
|
1296
|
+
/**
|
|
1297
|
+
* Serializes this schema's procedures into a plain object that is JSON compatible.
|
|
1298
|
+
*/
|
|
1299
|
+
serialize(): SerializedServiceSchema;
|
|
1300
|
+
/**
|
|
1301
|
+
* Instantiates this schema into a {@link Service} object.
|
|
1302
|
+
*
|
|
1303
|
+
* You probably don't need this, usually the River server will handle this
|
|
1304
|
+
* for you.
|
|
1305
|
+
*/
|
|
1306
|
+
instantiate(extendedContext: ServiceContext): Service<State, Procedures>;
|
|
1307
|
+
}
|
|
1308
|
+
/**
|
|
1309
|
+
* A scaffold for defining a service's procedures.
|
|
1310
|
+
*
|
|
1311
|
+
* @see {@link ServiceSchema.scaffold}
|
|
1312
|
+
*/
|
|
1313
|
+
declare class ServiceScaffold<State extends object> {
|
|
1314
|
+
/**
|
|
1315
|
+
* The configuration for this service.
|
|
1316
|
+
*/
|
|
1317
|
+
protected readonly config: ServiceConfiguration<State>;
|
|
1318
|
+
/**
|
|
1319
|
+
* @param config - The configuration for this service.
|
|
1320
|
+
*/
|
|
1321
|
+
constructor(config: ServiceConfiguration<State>);
|
|
1322
|
+
/**
|
|
1323
|
+
* Define procedures for this service. Use the {@link Procedure} constructors
|
|
1324
|
+
* to create them. This returns the procedures object, which can then be
|
|
1325
|
+
* passed to {@link ServiceSchema.finalize} to create a {@link ServiceSchema}.
|
|
1326
|
+
*
|
|
1327
|
+
* @example
|
|
1328
|
+
* ```
|
|
1329
|
+
* const myProcedures = MyServiceScaffold.procedures({
|
|
1330
|
+
* myRPC: Procedure.rpc({
|
|
1331
|
+
* // ...
|
|
1332
|
+
* }),
|
|
1333
|
+
* });
|
|
1334
|
+
*
|
|
1335
|
+
* const MyService = MyServiceScaffold.finalize({
|
|
1336
|
+
* ...myProcedures,
|
|
1337
|
+
* });
|
|
1338
|
+
* ```
|
|
1339
|
+
*
|
|
1340
|
+
* @param procedures - The procedures for this service.
|
|
1341
|
+
*/
|
|
1342
|
+
procedures<T extends BrandedProcedureMap<State>>(procedures: T): T;
|
|
1343
|
+
/**
|
|
1344
|
+
* Finalizes the scaffold into a {@link ServiceSchema}. This is where you
|
|
1345
|
+
* provide the service's procedures and get a {@link ServiceSchema} in return.
|
|
1346
|
+
*
|
|
1347
|
+
* You can directly define procedures here, or you can define them separately
|
|
1348
|
+
* with the {@link ServiceScaffold.procedures} method, and then pass them here.
|
|
1349
|
+
*
|
|
1350
|
+
* @example
|
|
1351
|
+
* ```
|
|
1352
|
+
* const MyService = MyServiceScaffold.finalize({
|
|
1353
|
+
* myRPC: Procedure.rpc({
|
|
1354
|
+
* // ...
|
|
1355
|
+
* }),
|
|
1356
|
+
* // e.g. from the procedures method
|
|
1357
|
+
* ...myOtherProcedures,
|
|
1358
|
+
* });
|
|
1359
|
+
* ```
|
|
1360
|
+
*/
|
|
1361
|
+
finalize<T extends BrandedProcedureMap<State>>(procedures: T): ServiceSchema<State, {
|
|
1362
|
+
[K in keyof T]: Unbranded<T[K]>;
|
|
1363
|
+
}>;
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
export { createClientHandshakeOptions as $, AnyServiceSchemaMap as A, BaseErrorSchemaType as B, Connection as C, StreamProcedure as D, ErrResult as E, UNCAUGHT_ERROR_CODE as F, UNEXPECTED_DISCONNECT_CODE as G, INVALID_REQUEST_CODE as H, InstantiatedServiceSchemaMap as I, InputReaderErrorSchema as J, createClient as K, Client as L, ParsedMetadata as M, ProcedureHandlerContext as N, OkResult as O, PayloadType as P, Ok as Q, ReadStream as R, SessionOptions as S, Transport as T, UploadProcedure as U, ValidProcType as V, WriteStream as W, Err as X, ResultUnwrapOk as Y, ResultUnwrapErr as Z, Output as _, Session as a, createServerHandshakeOptions as a0, ServerTransportOptions as a1, TransportStatus as a2, ProvidedTransportOptions as a3, EventMap as a4, EventTypes as a5, EventHandler as a6, ProtocolError as a7, ProtocolErrorType as a8, ProcedureErrorSchemaType as b, Procedure as c, ServiceContext as d, Result as e, OutputReaderErrorSchema as f, ClientTransport as g, ProvidedClientTransportOptions as h, ProvidedServerTransportOptions as i, SerializedServerSchema as j, ServerHandshakeOptions as k, Service as l, ServiceConfiguration as m, ProcHandler as n, ProcInit as o, ProcInput as p, ProcOutput as q, ProcErrors as r, ProcType as s, ServiceSchema as t, serializeSchema as u, SerializedServiceSchema as v, SerializedProcedureSchema as w, ProcedureMap as x, RpcProcedure as y, SubscriptionProcedure as z };
|