@replit/river 0.23.3 → 0.23.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +48 -0
  2. package/dist/{chunk-3U43VMW2.js → chunk-42ON4GTR.js} +2 -2
  3. package/dist/{chunk-XZV3HOQW.js → chunk-6GPJFN7T.js} +2 -2
  4. package/dist/chunk-6GPJFN7T.js.map +1 -0
  5. package/dist/{chunk-R3EG3FOP.js → chunk-DZLXTSL5.js} +2 -2
  6. package/dist/{chunk-JBRQNEBV.js → chunk-JJWZ5EHY.js} +13 -3
  7. package/dist/chunk-JJWZ5EHY.js.map +1 -0
  8. package/dist/{chunk-IKT6A32W.js → chunk-N7OICPYR.js} +2 -2
  9. package/dist/chunk-N7OICPYR.js.map +1 -0
  10. package/dist/{connection-89a66019.d.ts → connection-5331d77f.d.ts} +1 -1
  11. package/dist/{connection-4abad643.d.ts → connection-e257f6b7.d.ts} +1 -1
  12. package/dist/index-926aea33.d.ts +133 -0
  13. package/dist/logging/index.d.cts +1 -2
  14. package/dist/logging/index.d.ts +1 -2
  15. package/dist/router/index.cjs +13 -1
  16. package/dist/router/index.cjs.map +1 -1
  17. package/dist/router/index.d.cts +7 -7
  18. package/dist/router/index.d.ts +7 -7
  19. package/dist/router/index.js +6 -2
  20. package/dist/{services-ef89f5b0.d.ts → services-3f99f5d2.d.ts} +2 -2
  21. package/dist/transport/impls/uds/client.cjs +1 -1
  22. package/dist/transport/impls/uds/client.cjs.map +1 -1
  23. package/dist/transport/impls/uds/client.d.cts +4 -4
  24. package/dist/transport/impls/uds/client.d.ts +4 -4
  25. package/dist/transport/impls/uds/client.js +3 -3
  26. package/dist/transport/impls/uds/server.cjs +1 -1
  27. package/dist/transport/impls/uds/server.cjs.map +1 -1
  28. package/dist/transport/impls/uds/server.d.cts +4 -4
  29. package/dist/transport/impls/uds/server.d.ts +4 -4
  30. package/dist/transport/impls/uds/server.js +3 -3
  31. package/dist/transport/impls/ws/client.cjs +1 -1
  32. package/dist/transport/impls/ws/client.cjs.map +1 -1
  33. package/dist/transport/impls/ws/client.d.cts +4 -4
  34. package/dist/transport/impls/ws/client.d.ts +4 -4
  35. package/dist/transport/impls/ws/client.js +3 -3
  36. package/dist/transport/impls/ws/server.cjs +1 -1
  37. package/dist/transport/impls/ws/server.cjs.map +1 -1
  38. package/dist/transport/impls/ws/server.d.cts +3 -3
  39. package/dist/transport/impls/ws/server.d.ts +3 -3
  40. package/dist/transport/impls/ws/server.js +3 -3
  41. package/dist/transport/index.cjs +1 -1
  42. package/dist/transport/index.cjs.map +1 -1
  43. package/dist/transport/index.d.cts +4 -307
  44. package/dist/transport/index.d.ts +4 -307
  45. package/dist/transport/index.js +2 -2
  46. package/dist/transport-cbe70659.d.ts +537 -0
  47. package/dist/util/testHelpers.cjs +1 -1
  48. package/dist/util/testHelpers.cjs.map +1 -1
  49. package/dist/util/testHelpers.d.cts +3 -3
  50. package/dist/util/testHelpers.d.ts +3 -3
  51. package/dist/util/testHelpers.js +3 -3
  52. package/package.json +1 -1
  53. package/dist/chunk-IKT6A32W.js.map +0 -1
  54. package/dist/chunk-JBRQNEBV.js.map +0 -1
  55. package/dist/chunk-XZV3HOQW.js.map +0 -1
  56. package/dist/index-a009d7f6.d.ts +0 -360
  57. package/dist/services-edc3b16d.d.ts +0 -707
  58. /package/dist/{chunk-3U43VMW2.js.map → chunk-42ON4GTR.js.map} +0 -0
  59. /package/dist/{chunk-R3EG3FOP.js.map → chunk-DZLXTSL5.js.map} +0 -0
@@ -1,360 +0,0 @@
1
- import { ValueError } from '@sinclair/typebox/value';
2
- import * as _sinclair_typebox from '@sinclair/typebox';
3
- import { TSchema, Static } from '@sinclair/typebox';
4
- import { Span, Context } from '@opentelemetry/api';
5
- import { C as Codec } from './types-3e5768ec.js';
6
-
7
- declare const LoggingLevels: {
8
- readonly debug: -1;
9
- readonly info: 0;
10
- readonly warn: 1;
11
- readonly error: 2;
12
- };
13
- type LoggingLevel = keyof typeof LoggingLevels;
14
- type LogFn = (msg: string, ctx?: MessageMetadata, level?: LoggingLevel) => void;
15
- type Logger = {
16
- [key in LoggingLevel]: (msg: string, metadata?: MessageMetadata) => void;
17
- };
18
- type Tags = 'invariant-violation';
19
- type MessageMetadata = Partial<{
20
- protocolVersion: string;
21
- clientId: string;
22
- connectedTo: string;
23
- sessionId: string;
24
- connId: string;
25
- transportMessage: Partial<OpaqueTransportMessage>;
26
- validationErrors: Array<ValueError>;
27
- tags: Array<Tags>;
28
- }>;
29
- declare const stringLogger: LogFn;
30
- declare const coloredStringLogger: LogFn;
31
- declare const jsonLogger: LogFn;
32
- declare function bindLogger(fn: LogFn | Logger | undefined, level?: LoggingLevel): void;
33
-
34
- /**
35
- * A connection is the actual raw underlying transport connection.
36
- * It’s responsible for dispatching to/from the actual connection itself
37
- * This should be instantiated as soon as the client/server has a connection
38
- * It’s tied to the lifecycle of the underlying transport connection (i.e. if the WS drops, this connection should be deleted)
39
- */
40
- declare abstract class Connection {
41
- id: string;
42
- telemetry?: TelemetryInfo;
43
- constructor();
44
- /**
45
- * Handle adding a callback for when a message is received.
46
- * @param msg The message that was received.
47
- */
48
- abstract addDataListener(cb: (msg: Uint8Array) => void): void;
49
- abstract removeDataListener(cb: (msg: Uint8Array) => void): void;
50
- /**
51
- * Handle adding a callback for when the connection is closed.
52
- * This should also be called if an error happens.
53
- * @param cb The callback to call when the connection is closed.
54
- */
55
- abstract addCloseListener(cb: () => void): void;
56
- /**
57
- * Handle adding a callback for when an error is received.
58
- * This should only be used for logging errors, all cleanup
59
- * should be delegated to addCloseListener.
60
- *
61
- * The implementer should take care such that the implemented
62
- * connection will call both the close and error callbacks
63
- * on an error.
64
- *
65
- * @param cb The callback to call when an error is received.
66
- */
67
- abstract addErrorListener(cb: (err: Error) => void): void;
68
- /**
69
- * Sends a message over the connection.
70
- * @param msg The message to send.
71
- * @returns true if the message was sent, false otherwise.
72
- */
73
- abstract send(msg: Uint8Array): boolean;
74
- /**
75
- * Closes the connection.
76
- */
77
- abstract close(): void;
78
- }
79
- interface SessionOptions {
80
- /**
81
- * Frequency at which to send heartbeat acknowledgements
82
- */
83
- heartbeatIntervalMs: number;
84
- /**
85
- * Number of elapsed heartbeats without a response message before we consider
86
- * the connection dead.
87
- */
88
- heartbeatsUntilDead: number;
89
- /**
90
- * Duration to wait between connection disconnect and actual session disconnect
91
- */
92
- sessionDisconnectGraceMs: number;
93
- /**
94
- * The codec to use for encoding/decoding messages over the wire
95
- */
96
- codec: Codec;
97
- }
98
- /**
99
- * A session is a higher-level abstraction that operates over the span of potentially multiple transport-level connections
100
- * - 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)
101
- * - This will only be considered disconnected if
102
- * - the server tells the client that we’ve reconnected but it doesn’t recognize us anymore (server definitely died) or
103
- * - we hit a grace period after a connection disconnect
104
- */
105
- declare class Session<ConnType extends Connection> {
106
- private codec;
107
- private options;
108
- telemetry: TelemetryInfo;
109
- /**
110
- * The buffer of messages that have been sent but not yet acknowledged.
111
- */
112
- private sendBuffer;
113
- /**
114
- * The active connection associated with this session
115
- */
116
- connection?: ConnType;
117
- readonly from: TransportClientId;
118
- readonly to: TransportClientId;
119
- /**
120
- * The unique ID of this session.
121
- */
122
- id: string;
123
- /**
124
- * What the other side advertised as their session ID
125
- * for this session.
126
- */
127
- advertisedSessionId?: string;
128
- /**
129
- * Number of messages we've sent along this session (excluding handshake and acks)
130
- */
131
- private seq;
132
- /**
133
- * Number of unique messages we've received this session (excluding handshake and acks)
134
- */
135
- private ack;
136
- /**
137
- * The grace period between when the inner connection is disconnected
138
- * and when we should consider the entire session disconnected.
139
- */
140
- private disconnectionGrace?;
141
- /**
142
- * Number of heartbeats we've sent without a response.
143
- */
144
- private heartbeatMisses;
145
- /**
146
- * The interval for sending heartbeats.
147
- */
148
- private heartbeat;
149
- constructor(conn: ConnType | undefined, from: TransportClientId, to: TransportClientId, options: SessionOptions, propagationCtx?: PropagationContext);
150
- get loggingMetadata(): Omit<MessageMetadata, 'parsedMsg'>;
151
- /**
152
- * Sends a message over the session's connection.
153
- * If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
154
- *
155
- * @param msg The partial message to be sent, which will be constructed into a full message.
156
- * @param addToSendBuff Whether to add the message to the send buffer for retry.
157
- * @returns The full transport ID of the message that was attempted to be sent.
158
- */
159
- send(msg: PartialTransportMessage): string;
160
- sendHeartbeat(): void;
161
- resetBufferedMessages(): void;
162
- sendBufferedMessages(conn: ConnType): void;
163
- updateBookkeeping(ack: number, seq: number): void;
164
- closeStaleConnection(conn?: ConnType): void;
165
- replaceWithNewConnection(newConn: ConnType): void;
166
- beginGrace(cb: () => void): void;
167
- cancelGrace(): void;
168
- close(): void;
169
- get connected(): boolean;
170
- get nextExpectedSeq(): number;
171
- constructMsg<Payload>(partialMsg: PartialTransportMessage<Payload>): TransportMessage<Payload>;
172
- inspectSendBuffer(): ReadonlyArray<OpaqueTransportMessage>;
173
- }
174
-
175
- interface PropagationContext {
176
- traceparent: string;
177
- tracestate: string;
178
- }
179
- interface TelemetryInfo {
180
- span: Span;
181
- ctx: Context;
182
- }
183
-
184
- /**
185
- * The context for services/procedures. This is used only on
186
- * the server.
187
- *
188
- * An important detail is that the state prop is always on
189
- * this interface and it shouldn't be changed, removed, or
190
- * extended. This prop is for the state of a service.
191
- *
192
- * You should use declaration merging to extend this interface
193
- * with whatever you need. For example, if you need to access
194
- * a database, you could do:
195
- *
196
- * ```ts
197
- * declare module '@replit/river' {
198
- * interface ServiceContext {
199
- * db: Database;
200
- * }
201
- * }
202
- * ```
203
- */
204
- interface ServiceContext {
205
- }
206
- /**
207
- * The parsed metadata schema for a service. This is the
208
- * return value of the {@link ServerHandshakeOptions.validate}
209
- * if the handshake extension is used.
210
-
211
- * You should use declaration merging to extend this interface
212
- * with the sanitized metadata.
213
- *
214
- * ```ts
215
- * declare module '@replit/river' {
216
- * interface ParsedMetadata {
217
- * userId: number;
218
- * }
219
- * }
220
- * ```
221
- */
222
- interface ParsedMetadata {
223
- }
224
- /**
225
- * The {@link ServiceContext} with state. This is what is passed to procedures.
226
- */
227
- type ServiceContextWithState<State> = ServiceContext & {
228
- state: State;
229
- };
230
- type ServiceContextWithTransportInfo<State> = ServiceContext & {
231
- state: Omit<State, typeof Symbol.dispose>;
232
- to: TransportClientId;
233
- from: TransportClientId;
234
- streamId: string;
235
- session: Session<Connection>;
236
- metadata: ParsedMetadata;
237
- };
238
-
239
- interface ClientHandshakeOptions<MetadataSchema extends TSchema = TSchema> {
240
- /**
241
- * Schema for the metadata that the client sends to the server
242
- * during the handshake.
243
- */
244
- schema: MetadataSchema;
245
- /**
246
- * Gets the {@link HandshakeRequestMetadata} to send to the server.
247
- */
248
- construct: () => Static<MetadataSchema> | Promise<Static<MetadataSchema>>;
249
- }
250
- interface ServerHandshakeOptions<MetadataSchema extends TSchema = TSchema> {
251
- /**
252
- * Schema for the metadata that the server receives from the client
253
- * during the handshake.
254
- */
255
- schema: MetadataSchema;
256
- /**
257
- * Parses the {@link HandshakeRequestMetadata} sent by the client, transforming
258
- * it into {@link ParsedHandshakeMetadata}.
259
- *
260
- * May return `false` if the client should be rejected.
261
- *
262
- * @param metadata - The metadata sent by the client.
263
- * @param session - The session that the client would be associated with.
264
- * @param isReconnect - Whether the client is reconnecting to the session,
265
- * or if this is a new session.
266
- */
267
- validate: (metadata: Static<MetadataSchema>, previousParsedMetadata?: ParsedMetadata) => false | ParsedMetadata | Promise<false | ParsedMetadata>;
268
- }
269
- /**
270
- * Generic Typebox schema for a transport message.
271
- * @template T The type of the payload.
272
- * @param {T} t The payload schema.
273
- * @returns The transport message schema.
274
- */
275
- declare const TransportMessageSchema: <T extends TSchema>(t: T) => _sinclair_typebox.TObject<{
276
- id: _sinclair_typebox.TString;
277
- from: _sinclair_typebox.TString;
278
- to: _sinclair_typebox.TString;
279
- seq: _sinclair_typebox.TInteger;
280
- ack: _sinclair_typebox.TInteger;
281
- serviceName: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
282
- procedureName: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
283
- streamId: _sinclair_typebox.TString;
284
- controlFlags: _sinclair_typebox.TInteger;
285
- tracing: _sinclair_typebox.TOptional<_sinclair_typebox.TObject<{
286
- traceparent: _sinclair_typebox.TString;
287
- tracestate: _sinclair_typebox.TString;
288
- }>>;
289
- payload: T;
290
- }>;
291
- /**
292
- * Defines the schema for an opaque transport message that is agnostic to any
293
- * procedure/service.
294
- * @returns The transport message schema.
295
- */
296
- declare const OpaqueTransportMessageSchema: _sinclair_typebox.TObject<{
297
- id: _sinclair_typebox.TString;
298
- from: _sinclair_typebox.TString;
299
- to: _sinclair_typebox.TString;
300
- seq: _sinclair_typebox.TInteger;
301
- ack: _sinclair_typebox.TInteger;
302
- serviceName: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
303
- procedureName: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
304
- streamId: _sinclair_typebox.TString;
305
- controlFlags: _sinclair_typebox.TInteger;
306
- tracing: _sinclair_typebox.TOptional<_sinclair_typebox.TObject<{
307
- traceparent: _sinclair_typebox.TString;
308
- tracestate: _sinclair_typebox.TString;
309
- }>>;
310
- payload: _sinclair_typebox.TUnknown;
311
- }>;
312
- /**
313
- * Represents a transport message. This is the same type as {@link TransportMessageSchema} but
314
- * we can't statically infer generics from generic Typebox schemas so we have to define it again here.
315
- *
316
- * TypeScript can't enforce types when a bitmask is involved, so these are the semantics of
317
- * `controlFlags`:
318
- * * If `controlFlags & StreamOpenBit == StreamOpenBit`, `streamId` must be set to a unique value
319
- * (suggestion: use `nanoid`).
320
- * * If `controlFlags & StreamOpenBit == StreamOpenBit`, `serviceName` and `procedureName` must be set.
321
- * * If `controlFlags & StreamClosedBit == StreamClosedBit` and the kind is `stream` or `subscription`,
322
- * `payload` should be discarded (usually contains a control message).
323
- * * If `controlFlags & AckBit == AckBit`, the message is an explicit acknowledgement message and doesn't
324
- * contain any payload that is relevant to the application so should not be delivered.
325
- * @template Payload The type of the payload.
326
- */
327
- interface TransportMessage<Payload = unknown> {
328
- id: string;
329
- from: string;
330
- to: string;
331
- seq: number;
332
- ack: number;
333
- serviceName?: string;
334
- procedureName?: string;
335
- streamId: string;
336
- controlFlags: number;
337
- tracing?: PropagationContext;
338
- payload: Payload;
339
- }
340
- type PartialTransportMessage<Payload = unknown> = Omit<TransportMessage<Payload>, 'id' | 'from' | 'to' | 'seq' | 'ack'>;
341
- /**
342
- * A type alias for a transport message with an opaque payload.
343
- * @template T - The type of the opaque payload.
344
- */
345
- type OpaqueTransportMessage = TransportMessage;
346
- type TransportClientId = string;
347
- /**
348
- * Checks if the given control flag (usually found in msg.controlFlag) is a stream open message.
349
- * @param controlFlag - The control flag to check.
350
- * @returns True if the control flag contains the StreamOpenBit, false otherwise.
351
- */
352
- declare function isStreamOpen(controlFlag: number): boolean;
353
- /**
354
- * Checks if the given control flag (usually found in msg.controlFlag) is a stream close message.
355
- * @param controlFlag - The control flag to check.
356
- * @returns True if the control flag contains the StreamCloseBit, false otherwise.
357
- */
358
- declare function isStreamClose(controlFlag: number): boolean;
359
-
360
- export { Connection as C, Logger as L, MessageMetadata as M, OpaqueTransportMessage as O, PartialTransportMessage as P, SessionOptions as S, TransportClientId as T, ServiceContext as a, Session as b, ServerHandshakeOptions as c, ParsedMetadata as d, ServiceContextWithState as e, ServiceContextWithTransportInfo as f, ClientHandshakeOptions as g, TransportMessageSchema as h, OpaqueTransportMessageSchema as i, TransportMessage as j, isStreamOpen as k, isStreamClose as l, PropagationContext as m, coloredStringLogger as n, jsonLogger as o, bindLogger as p, LogFn as q, stringLogger as s };