@replit/river 0.200.0-rc.9 → 0.200.2

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 (72) hide show
  1. package/README.md +8 -8
  2. package/dist/{chunk-42Z2FQIU.js → chunk-6BH2CXVE.js} +21 -13
  3. package/dist/chunk-6BH2CXVE.js.map +1 -0
  4. package/dist/{chunk-4HT6P2ZG.js → chunk-A4JKES5A.js} +22 -30
  5. package/dist/chunk-A4JKES5A.js.map +1 -0
  6. package/dist/{chunk-4PVU7J25.js → chunk-AJGIY2UB.js} +1 -1
  7. package/dist/chunk-AJGIY2UB.js.map +1 -0
  8. package/dist/{chunk-EETL2L77.js → chunk-GJUUVID2.js} +14 -32
  9. package/dist/chunk-GJUUVID2.js.map +1 -0
  10. package/dist/{chunk-GR3AQKHL.js → chunk-HRKM7BIE.js} +14 -4
  11. package/dist/chunk-HRKM7BIE.js.map +1 -0
  12. package/dist/{chunk-ZXZE253M.js → chunk-PJB2Y2AV.js} +24 -37
  13. package/dist/chunk-PJB2Y2AV.js.map +1 -0
  14. package/dist/{chunk-I75XYO5W.js → chunk-QIDEN5PP.js} +82 -20
  15. package/dist/chunk-QIDEN5PP.js.map +1 -0
  16. package/dist/{chunk-VXYHC666.js → chunk-YTMS7OP6.js} +1 -1
  17. package/dist/chunk-YTMS7OP6.js.map +1 -0
  18. package/dist/chunk-Z4PX66JO.js +307 -0
  19. package/dist/chunk-Z4PX66JO.js.map +1 -0
  20. package/dist/{client-22a47343.d.ts → client-9292552a.d.ts} +3 -4
  21. package/dist/codec/index.cjs.map +1 -1
  22. package/dist/codec/index.js +1 -1
  23. package/dist/connection-94dea547.d.ts +32 -0
  24. package/dist/{context-b4aff18f.d.ts → context-69f37ac1.d.ts} +48 -43
  25. package/dist/logging/index.cjs.map +1 -1
  26. package/dist/logging/index.d.cts +1 -1
  27. package/dist/logging/index.d.ts +1 -1
  28. package/dist/logging/index.js +1 -1
  29. package/dist/{message-7d135e38.d.ts → message-57bb8187.d.ts} +5 -3
  30. package/dist/router/index.cjs +649 -709
  31. package/dist/router/index.cjs.map +1 -1
  32. package/dist/router/index.d.cts +22 -12
  33. package/dist/router/index.d.ts +22 -12
  34. package/dist/router/index.js +502 -404
  35. package/dist/router/index.js.map +1 -1
  36. package/dist/{server-dd6a9853.d.ts → server-8fdd7fb2.d.ts} +5 -5
  37. package/dist/{services-1b5ac5bc.d.ts → services-259f39a3.d.ts} +191 -194
  38. package/dist/transport/impls/ws/client.cjs +129 -62
  39. package/dist/transport/impls/ws/client.cjs.map +1 -1
  40. package/dist/transport/impls/ws/client.d.cts +4 -4
  41. package/dist/transport/impls/ws/client.d.ts +4 -4
  42. package/dist/transport/impls/ws/client.js +7 -7
  43. package/dist/transport/impls/ws/client.js.map +1 -1
  44. package/dist/transport/impls/ws/server.cjs +146 -70
  45. package/dist/transport/impls/ws/server.cjs.map +1 -1
  46. package/dist/transport/impls/ws/server.d.cts +6 -5
  47. package/dist/transport/impls/ws/server.d.ts +6 -5
  48. package/dist/transport/impls/ws/server.js +21 -9
  49. package/dist/transport/impls/ws/server.js.map +1 -1
  50. package/dist/transport/index.cjs +138 -92
  51. package/dist/transport/index.cjs.map +1 -1
  52. package/dist/transport/index.d.cts +4 -4
  53. package/dist/transport/index.d.ts +4 -4
  54. package/dist/transport/index.js +7 -7
  55. package/dist/util/testHelpers.cjs +265 -327
  56. package/dist/util/testHelpers.cjs.map +1 -1
  57. package/dist/util/testHelpers.d.cts +36 -31
  58. package/dist/util/testHelpers.d.ts +36 -31
  59. package/dist/util/testHelpers.js +82 -52
  60. package/dist/util/testHelpers.js.map +1 -1
  61. package/package.json +4 -3
  62. package/dist/chunk-42Z2FQIU.js.map +0 -1
  63. package/dist/chunk-4HT6P2ZG.js.map +0 -1
  64. package/dist/chunk-4PVU7J25.js.map +0 -1
  65. package/dist/chunk-EETL2L77.js.map +0 -1
  66. package/dist/chunk-GR3AQKHL.js.map +0 -1
  67. package/dist/chunk-I75XYO5W.js.map +0 -1
  68. package/dist/chunk-MQ6ANR3H.js +0 -451
  69. package/dist/chunk-MQ6ANR3H.js.map +0 -1
  70. package/dist/chunk-VXYHC666.js.map +0 -1
  71. package/dist/chunk-ZXZE253M.js.map +0 -1
  72. package/dist/connection-260e45a8.d.ts +0 -11
@@ -1,159 +1,193 @@
1
- import * as _sinclair_typebox from '@sinclair/typebox';
2
- import { Static, TObject, TUnion, TString, TSchema, TLiteral, TNever } from '@sinclair/typebox';
3
- import { C as Connection, p as ClientHandshakeOptions, u as ProcedureHandlerContext, o as ServiceContext } from './context-b4aff18f.js';
4
- import { c as TransportClientId } from './message-7d135e38.js';
5
- import { C as ClientTransport } from './client-22a47343.js';
1
+ import { TObject, TString, TSchema, TUnion, TLiteral, TNever, Static } from '@sinclair/typebox';
2
+ import { C as Connection, q as ClientHandshakeOptions, v as ProcedureHandlerContext, p as ServiceContext } from './context-69f37ac1.js';
3
+ import { c as TransportClientId } from './message-57bb8187.js';
4
+ import { C as ClientTransport } from './client-9292552a.js';
6
5
 
7
- declare const StreamDrainedError: {
8
- readonly code: "STREAM_DRAINED";
9
- readonly message: "Stream was drained";
10
- };
11
- type ReadStreamResult<T, E extends Static<BaseErrorSchemaType>> = Result<T, E | typeof StreamDrainedError>;
12
6
  /**
13
- * Using simple iterator here to lock down the iteration and disallow
14
- * `return` and `throw` to be called from the outside.
7
+ * {@link UNCAUGHT_ERROR_CODE} is the code that is used when an error is thrown
8
+ * inside a procedure handler that's not required.
9
+ */
10
+ declare const UNCAUGHT_ERROR_CODE = "UNCAUGHT_ERROR";
11
+ /**
12
+ * {@link UNEXPECTED_DISCONNECT_CODE} is the code used the stream's session
13
+ * disconnect unexpetedly.
14
+ */
15
+ declare const UNEXPECTED_DISCONNECT_CODE = "UNEXPECTED_DISCONNECT";
16
+ /**
17
+ * {@link INVALID_REQUEST_CODE} is the code used when a client's request is invalid.
18
+ */
19
+ declare const INVALID_REQUEST_CODE = "INVALID_REQUEST";
20
+ /**
21
+ * {@link CANCEL_CODE} is the code used when either server or client cancels the stream.
15
22
  */
16
- interface SimpleIterator<T> {
23
+ declare const CANCEL_CODE = "CANCEL";
24
+ type TLiteralString = TLiteral<string>;
25
+ type BaseErrorSchemaType = TObject<{
26
+ code: TLiteralString;
27
+ message: TLiteralString | TString;
28
+ }> | TObject<{
29
+ code: TLiteralString;
30
+ message: TLiteralString | TString;
31
+ extras: TSchema;
32
+ }>;
33
+ /**
34
+ * {@link ReaderErrorSchema} is the schema for all the built-in river errors that
35
+ * can be emitted to a reader (request reader on the server, and response reader
36
+ * on the client).
37
+ */
38
+ declare const ReaderErrorSchema: TUnion<[TObject<{
39
+ code: TLiteral<"UNCAUGHT_ERROR">;
40
+ message: TString;
41
+ }>, TObject<{
42
+ code: TLiteral<"UNEXPECTED_DISCONNECT">;
43
+ message: TString;
44
+ }>, TObject<{
45
+ code: TLiteral<"INVALID_REQUEST">;
46
+ message: TString;
47
+ }>, TObject<{
48
+ code: TLiteral<"CANCEL">;
49
+ message: TString;
50
+ }>]>;
51
+ /**
52
+ * Represents an acceptable schema to pass to a procedure.
53
+ * Just a type of a schema, not an actual schema.
54
+ *
55
+ */
56
+ type ProcedureErrorSchemaType = TNever | BaseErrorSchemaType | TUnion<Array<BaseErrorSchemaType>>;
57
+ type NestableProcedureErrorSchemaType = BaseErrorSchemaType | TUnion<NestableProcedureErrorSchemaTypeArray>;
58
+ interface NestableProcedureErrorSchemaTypeArray extends Array<NestableProcedureErrorSchemaType> {
59
+ }
60
+ type Flatten<T> = T extends BaseErrorSchemaType ? T : T extends TUnion<Array<infer U extends TSchema>> ? Flatten<U> : unknown;
61
+ /**
62
+ * In the case where API consumers for some god-forsaken reason want to use
63
+ * arbitrarily nested unions, this helper flattens them to a single level.
64
+ *
65
+ * Note that loses some metadata information on the nested unions like
66
+ * nested description fields, etc.
67
+ *
68
+ * @param errType - An arbitrarily union-nested error schema.
69
+ * @returns The flattened error schema.
70
+ */
71
+ declare function flattenErrorType<T extends NestableProcedureErrorSchemaType>(errType: T): Flatten<T>;
72
+
73
+ declare const ReadableBrokenError: Static<BaseErrorSchemaType>;
74
+ /**
75
+ * Similar to {@link Result} but with an extra error to handle cases where {@link Readable.break} is called
76
+ */
77
+ type ReadableResult<T, E extends Static<BaseErrorSchemaType>> = Result<T, E | typeof ReadableBrokenError>;
78
+ /**
79
+ * A simple {@link AsyncIterator} used in {@link Readable}
80
+ * that doesn't have a the extra "return" and "throw" methods, and
81
+ * the doesn't have a "done value" (TReturn).
82
+ */
83
+ interface ReadableIterator<T, E extends Static<BaseErrorSchemaType>> {
17
84
  next(): Promise<{
18
85
  done: false;
19
- value: T;
86
+ value: ReadableResult<T, E>;
20
87
  } | {
21
88
  done: true;
22
89
  value: undefined;
23
90
  }>;
24
91
  }
25
92
  /**
26
- * A `ReadStream` represents a stream of data.
93
+ * A {@link Readable} is an abstraction from which data is consumed from {@link Writable} source.
27
94
  *
28
- * This stream is not closable by the reader, the reader must wait for
29
- * the writer to close it.
95
+ * - On the server the argument passed the procedure handler for `upload` and `stream` implements a {@link Readable} interface
96
+ * so you can read client's request data.
97
+ * - On the client the returned value of `subscription` or `stream` invocation implements a {@link Readable} interface
98
+ * so you can read server's response data.
30
99
  *
31
- * The stream can only be locked (aka consumed) once and will remain
32
- * locked, trying to lock the stream again will throw an TypeError.
100
+ * A {@link Readable} can only have one consumer (iterator or {@link collect}) for the {@link Readable}'s
101
+ * lifetime, in essense, reading from a {@link Readable} locks it forever.
33
102
  */
34
- interface ReadStream<T, E extends Static<BaseErrorSchemaType>> {
35
- /**
36
- * Stream implements AsyncIterator API and can be consumed via
37
- * for-await-of loops. Iteration locks the stream
38
- *
39
- */
40
- [Symbol.asyncIterator](): SimpleIterator<ReadStreamResult<T, E>>;
41
- /**
42
- * `unwrappedIter` returns an AsyncIterator that will unwrap the results coming
43
- * into the stream, yielding the payload if successful, otherwise throwing.
44
- * We generally recommend using the normal iterator instead of this method,
45
- * and handling errors explicitly.
46
- */
47
- unwrappedIter(): {
48
- [Symbol.asyncIterator](): SimpleIterator<T>;
49
- };
50
- /**
51
- * `asArray` locks the stream and returns a promise that resolves
52
- * with an array of the stream's content when the stream is closed.
53
- *
54
- */
55
- asArray(): Promise<Array<ReadStreamResult<T, E>>>;
56
- /**
57
- * `drain` locks the stream and discards any existing or future data.
58
- *
59
- * If there is an existing Promise waiting for the next value,
60
- * `drain` causes it to resolve with a {@link StreamDrainedError} error.
61
- */
62
- drain(): undefined;
63
- /**
64
- * `isLocked` returns true if the stream is locked.
65
- */
66
- isLocked(): boolean;
103
+ interface Readable<T, E extends Static<BaseErrorSchemaType>> {
67
104
  /**
68
- * `onClose` registers a callback that will be called when the stream
69
- * is closed. Returns a function that can be used to unregister the
70
- * listener.
105
+ * {@link Readable} implements AsyncIterator API and can be consumed via
106
+ * for-await-of loops. Iteration locks the Readable. Exiting the loop
107
+ * will **not** release the lock and it'll be equivalent of calling
108
+ * {@link break}.
71
109
  */
72
- onClose(cb: () => void): () => void;
110
+ [Symbol.asyncIterator](): ReadableIterator<T, E>;
73
111
  /**
74
- * `isClosed` returns true if the stream was closed by the writer.
75
- *
76
- * Note that the stream can still have queued data and can still be
77
- * consumed.
112
+ * {@link collect} locks the {@link Readable} and returns a promise that resolves
113
+ * with an array of the content when the {@link Readable} is fully done. This could
114
+ * be due to the {@link Writable} end of the pipe closing cleanly, the procedure invocation
115
+ * is cancelled, or {@link break} is called.
78
116
  */
79
- isClosed(): boolean;
117
+ collect(): Promise<Array<ReadableResult<T, E>>>;
80
118
  /**
81
- * `requestClose` sends a request to the writer to close the stream,
82
- * and resolves the writer closes its end of the stream..
119
+ * {@link break} locks the {@link Readable} and discards any existing or future incoming data.
83
120
  *
84
- * The stream can still receive more data after the request is sent. If you
85
- * no longer wish to use the stream, make sure to call `drain`.
121
+ * If there is an existing reader waiting for the next value, {@link break} causes it to
122
+ * resolve with a {@link ReadableBrokenError} error.
86
123
  */
87
- requestClose(): Promise<undefined>;
124
+ break(): undefined;
88
125
  /**
89
- * `isCloseRequested` checks if the reader has requested to close the stream.
126
+ * {@link isReadable} returns true if it's safe to read from the {@link Readable}, either
127
+ * via iteration or {@link collect}. It returns false if the {@link Readable} is locked
128
+ * by a consumer (iterator or {@link collect}) or readable was broken via {@link break}.
90
129
  */
91
- isCloseRequested(): boolean;
130
+ isReadable(): boolean;
92
131
  }
93
132
  /**
94
- * A `WriteStream` is a streams that can be written to.
133
+ * A {@link Writeable} is a an abstraction for a {@link Readable} destination to which data is written to.
134
+ *
135
+ * - On the server the argument passed the procedure handler for `subscription` and `stream` implements a {@link Writeable}
136
+ * so you can write server's response data.
137
+ * - On the client the returned value of `upload` or `stream` invocation implements a {@link Writeable}
138
+ * so you can write client's request data.
139
+ *
140
+ * Once closed, a {@link Writeable} can't be re-opened.` `
95
141
  */
96
- interface WriteStream<T> {
142
+ interface Writable<T> {
97
143
  /**
98
- * `write` writes a value to the stream. An error is thrown if writing to a closed stream.
144
+ * {@link write} writes a value to the pipe. An error is thrown if writing to a closed {@link Writable}.
99
145
  */
100
146
  write(value: T): undefined;
101
147
  /**
102
- * `close` signals the closure of the write stream, informing the reader that the stream is complete.
103
- * Calling close multiple times has no effect.
148
+ * {@link close} signals the closure of the {@link Writeable}, informing the {@link Readable} end that
149
+ * all data has been transmitted and we've cleanly closed.
150
+ *
151
+ * Calling {@link close} multiple times is a no-op.
104
152
  */
105
153
  close(): undefined;
106
154
  /**
107
- * `isCloseRequested` checks if the reader has requested to close the stream.
108
- */
109
- isCloseRequested(): boolean;
110
- /**
111
- * `onCloseRequest` registers a callback that will be called when the stream's
112
- * reader requests a close. Returns a function that can be used to unregister the
113
- * listener.
155
+ * {@link isWritable} returns true if it's safe to call {@link write}, which
156
+ * means that the {@link Writable} hasn't been closed due to {@link close} being called
157
+ * on this {@link Writable} or the procedure invocation ending for any reason.
114
158
  */
115
- onCloseRequest(cb: () => void): () => void;
116
- /**
117
- * `isClosed` returns true if the stream was closed by the writer.
118
- */
119
- isClosed(): boolean;
120
- /**
121
- * `onClose` registers a callback that will be called when the stream
122
- * is closed. Returns a function that can be used to unregister the
123
- * listener.
124
- */
125
- onClose(cb: () => void): () => void;
159
+ isWritable(): boolean;
126
160
  }
127
161
 
128
162
  interface CallOptions {
129
163
  signal?: AbortSignal;
130
164
  }
131
- type RpcFn<Router extends AnyService, ProcName extends keyof Router['procedures']> = (reqInit: ProcInit<Router, ProcName>, options?: CallOptions) => Promise<Result<ProcOutput<Router, ProcName>, ProcErrors<Router, ProcName>>>;
132
- type UploadFn<Router extends AnyService, ProcName extends keyof Router['procedures']> = (reqInit: ProcInit<Router, ProcName>, options?: CallOptions) => {
133
- reqWriter: WriteStream<ProcInput<Router, ProcName>>;
134
- finalize: () => Promise<Result<ProcOutput<Router, ProcName>, ProcErrors<Router, ProcName>>>;
165
+ type RpcFn<Service extends AnyService, ProcName extends keyof Service['procedures']> = (reqInit: ProcInit<Service, ProcName>, options?: CallOptions) => Promise<Result<ProcResponse<Service, ProcName>, ProcErrors<Service, ProcName>>>;
166
+ type UploadFn<Service extends AnyService, ProcName extends keyof Service['procedures']> = (reqInit: ProcInit<Service, ProcName>, options?: CallOptions) => {
167
+ reqWritable: Writable<ProcRequest<Service, ProcName>>;
168
+ finalize: () => Promise<Result<ProcResponse<Service, ProcName>, ProcErrors<Service, ProcName>>>;
135
169
  };
136
- type StreamFn<Router extends AnyService, ProcName extends keyof Router['procedures']> = (reqInit: ProcInit<Router, ProcName>, options?: CallOptions) => {
137
- reqWriter: WriteStream<ProcInput<Router, ProcName>>;
138
- resReader: ReadStream<ProcOutput<Router, ProcName>, ProcErrors<Router, ProcName>>;
170
+ type StreamFn<Service extends AnyService, ProcName extends keyof Service['procedures']> = (reqInit: ProcInit<Service, ProcName>, options?: CallOptions) => {
171
+ reqWritable: Writable<ProcRequest<Service, ProcName>>;
172
+ resReadable: Readable<ProcResponse<Service, ProcName>, ProcErrors<Service, ProcName>>;
139
173
  };
140
- type SubscriptionFn<Router extends AnyService, ProcName extends keyof Router['procedures']> = (reqInit: ProcInit<Router, ProcName>, options?: CallOptions) => {
141
- resReader: ReadStream<ProcOutput<Router, ProcName>, ProcErrors<Router, ProcName>>;
174
+ type SubscriptionFn<Service extends AnyService, ProcName extends keyof Service['procedures']> = (reqInit: ProcInit<Service, ProcName>, options?: CallOptions) => {
175
+ resReadable: Readable<ProcResponse<Service, ProcName>, ProcErrors<Service, ProcName>>;
142
176
  };
143
177
  /**
144
178
  * A helper type to transform an actual service type into a type
145
179
  * we can case to in the proxy.
146
- * @template Router - The type of the Router.
147
- */
148
- type ServiceClient<Router extends AnyService> = {
149
- [ProcName in keyof Router['procedures']]: ProcType<Router, ProcName> extends 'rpc' ? {
150
- rpc: RpcFn<Router, ProcName>;
151
- } : ProcType<Router, ProcName> extends 'upload' ? {
152
- upload: UploadFn<Router, ProcName>;
153
- } : ProcType<Router, ProcName> extends 'stream' ? {
154
- stream: StreamFn<Router, ProcName>;
155
- } : ProcType<Router, ProcName> extends 'subscription' ? {
156
- subscribe: SubscriptionFn<Router, ProcName>;
180
+ * @template Service - The type of the Service.
181
+ */
182
+ type ServiceClient<Service extends AnyService> = {
183
+ [ProcName in keyof Service['procedures']]: ProcType<Service, ProcName> extends 'rpc' ? {
184
+ rpc: RpcFn<Service, ProcName>;
185
+ } : ProcType<Service, ProcName> extends 'upload' ? {
186
+ upload: UploadFn<Service, ProcName>;
187
+ } : ProcType<Service, ProcName> extends 'stream' ? {
188
+ stream: StreamFn<Service, ProcName>;
189
+ } : ProcType<Service, ProcName> extends 'subscription' ? {
190
+ subscribe: SubscriptionFn<Service, ProcName>;
157
191
  } : never;
158
192
  };
159
193
  /**
@@ -186,15 +220,6 @@ declare function createClient<ServiceSchemaMap extends AnyServiceSchemaMap>(tran
186
220
  handshakeOptions: ClientHandshakeOptions;
187
221
  }>): Client<ServiceSchemaMap>;
188
222
 
189
- type TLiteralString = TLiteral<string>;
190
- type BaseErrorSchemaType = TObject<{
191
- code: TLiteralString | TUnion<Array<TLiteralString>>;
192
- message: TLiteralString | TString;
193
- }> | TObject<{
194
- code: TLiteralString | TUnion<Array<TLiteralString>>;
195
- message: TLiteralString | TString;
196
- extras: TSchema;
197
- }>;
198
223
  interface OkResult<T> {
199
224
  ok: true;
200
225
  payload: T;
@@ -212,19 +237,26 @@ declare function Err<const Err extends Static<BaseErrorSchemaType>>(error: Err):
212
237
  * Refine a {@link Result} type to its returned payload.
213
238
  */
214
239
  type ResultUnwrapOk<R> = R extends Result<infer T, infer __E> ? T : never;
240
+ /**
241
+ * Unwrap a {@link Result} type and return the payload if successful,
242
+ * otherwise throws an error.
243
+ * @param result - The result to unwrap.
244
+ * @throws Will throw an error if the result is not ok.
245
+ */
246
+ declare function unwrapOrThrow<T, Err extends Static<BaseErrorSchemaType>>(result: Result<T, Err>): T;
215
247
  /**
216
248
  * Refine a {@link Result} type to its error payload.
217
249
  */
218
250
  type ResultUnwrapErr<R> = R extends Result<infer __T, infer Err> ? Err : never;
219
251
  /**
220
- * Retrieve the output type for a procedure, represented as a {@link Result}
252
+ * Retrieve the response type for a procedure, represented as a {@link Result}
221
253
  * type.
222
254
  * Example:
223
255
  * ```
224
- * type Message = Output<typeof client, 'serviceName', 'procedureName'>
256
+ * type Message = ResponseData<typeof client, 'serviceName', 'procedureName'>
225
257
  * ```
226
258
  */
227
- 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 & {
259
+ type ResponseData<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 & {
228
260
  rpc: infer RpcFn extends Fn;
229
261
  } ? Awaited<ReturnType<RpcFn>> : Procedure extends object & {
230
262
  upload: infer UploadFn extends Fn;
@@ -233,11 +265,11 @@ type Output<RiverClient, ServiceName extends keyof RiverClient, ProcedureName ex
233
265
  } ? UploadOutputMessage : never : Procedure extends object & {
234
266
  stream: infer StreamFn extends Fn;
235
267
  } ? ReturnType<StreamFn> extends {
236
- resReader: ReadStream<infer StreamOutputMessage, Static<BaseErrorSchemaType>>;
268
+ resReadable: Readable<infer StreamOutputMessage, Static<BaseErrorSchemaType>>;
237
269
  } ? StreamOutputMessage : never : Procedure extends object & {
238
270
  subscribe: infer SubscriptionFn extends Fn;
239
271
  } ? Awaited<ReturnType<SubscriptionFn>> extends {
240
- resReader: ReadStream<infer SubscriptionOutputMessage, Static<BaseErrorSchemaType>>;
272
+ resReadable: Readable<infer SubscriptionOutputMessage, Static<BaseErrorSchemaType>>;
241
273
  } ? SubscriptionOutputMessage : never : never : never : never;
242
274
 
243
275
  /**
@@ -260,57 +292,12 @@ type ValidProcType = 'rpc' | 'upload' | 'subscription' | 'stream';
260
292
  * Represents the payload type for {@link Procedure}s.
261
293
  */
262
294
  type PayloadType = TSchema;
263
- /**
264
- * INTERNAL_RIVER_ERROR_CODE is the code that is used when an internal error occurs,
265
- * this means that some invariants expected by the river server implementation have
266
- * been violated. When encountering this error please report this to river maintainers.
267
- */
268
- declare const INTERNAL_RIVER_ERROR_CODE: "INTERNAL_RIVER_ERROR";
269
- /**
270
- * UNCAUGHT_ERROR_CODE is the code that is used when an error is thrown
271
- * inside a procedure handler that's not required.
272
- */
273
- declare const UNCAUGHT_ERROR_CODE: "UNCAUGHT_ERROR";
274
- /**
275
- * UNEXPECTED_DISCONNECT_CODE is the code used the stream's session
276
- * disconnect unexpetedly.
277
- */
278
- declare const UNEXPECTED_DISCONNECT_CODE: "UNEXPECTED_DISCONNECT";
279
- /**
280
- * INVALID_REQUEST_CODE is the code used when a client's request is invalid.
281
- */
282
- declare const INVALID_REQUEST_CODE: "INVALID_REQUEST";
283
- /**
284
- * ABORT_CODE is the code used when either server or client aborts the stream.
285
- */
286
- declare const ABORT_CODE: "ABORT";
287
- /**
288
- * ResponseReaderErrorSchema is the schema for all the errors that can be
289
- * emitted in the ResponseData ReadStream on the client.
290
- */
291
- declare const ResponseReaderErrorSchema: _sinclair_typebox.TObject<{
292
- 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">]>;
293
- message: _sinclair_typebox.TString;
294
- }>;
295
- /**
296
- * RequestReaderErrorSchema is the schema for all the errors that can be
297
- * emitted in the RequestData ReadStream on the server.
298
- */
299
- declare const RequestReaderErrorSchema: _sinclair_typebox.TObject<{
300
- code: TUnion<[_sinclair_typebox.TLiteral<"UNCAUGHT_ERROR">, _sinclair_typebox.TLiteral<"UNEXPECTED_DISCONNECT">, _sinclair_typebox.TLiteral<"INVALID_REQUEST">, _sinclair_typebox.TLiteral<"ABORT">]>;
301
- message: _sinclair_typebox.TString;
302
- }>;
303
- /**
304
- * Represents an acceptable schema to pass to a procedure.
305
- * Just a type of a schema, not an actual schema.
306
- */
307
- type ProcedureErrorSchemaType = TUnion<Array<BaseErrorSchemaType>> | BaseErrorSchemaType | TNever;
308
295
  /**
309
296
  * Procedure for a single message in both directions (1:1).
310
297
  *
311
298
  * @template State - The context state object.
312
299
  * @template RequestInit - The TypeBox schema of the initialization object.
313
- * @template ResponseData - The TypeBox schema of the output object.
300
+ * @template ResponseData - The TypeBox schema of the response object.
314
301
  * @template ResponseErr - The TypeBox schema of the error object.
315
302
  */
316
303
  interface RpcProcedure<State, RequestInit extends PayloadType, ResponseData extends PayloadType, ResponseErr extends ProcedureErrorSchemaType> {
@@ -319,7 +306,7 @@ interface RpcProcedure<State, RequestInit extends PayloadType, ResponseData exte
319
306
  responseData: ResponseData;
320
307
  responseError: ResponseErr;
321
308
  description?: string;
322
- handler({ ctx, reqInit, }: {
309
+ handler(param: {
323
310
  ctx: ProcedureHandlerContext<State>;
324
311
  reqInit: Static<RequestInit>;
325
312
  }): Promise<Result<Static<ResponseData>, Static<ResponseErr>>>;
@@ -330,8 +317,8 @@ interface RpcProcedure<State, RequestInit extends PayloadType, ResponseData exte
330
317
  *
331
318
  * @template State - The context state object.
332
319
  * @template RequestInit - The TypeBox schema of the initialization object.
333
- * @template RequestData - The TypeBox schema of the input object.
334
- * @template ResponseData - The TypeBox schema of the output object.
320
+ * @template RequestData - The TypeBox schema of the request object.
321
+ * @template ResponseData - The TypeBox schema of the response object.
335
322
  * @template ResponseErr - The TypeBox schema of the error object.
336
323
  */
337
324
  interface UploadProcedure<State, RequestInit extends PayloadType, RequestData extends PayloadType, ResponseData extends PayloadType, ResponseErr extends ProcedureErrorSchemaType> {
@@ -341,10 +328,10 @@ interface UploadProcedure<State, RequestInit extends PayloadType, RequestData ex
341
328
  responseData: ResponseData;
342
329
  responseError: ResponseErr;
343
330
  description?: string;
344
- handler({ ctx, reqInit, reqReader, }: {
331
+ handler(param: {
345
332
  ctx: ProcedureHandlerContext<State>;
346
333
  reqInit: Static<RequestInit>;
347
- reqReader: ReadStream<Static<RequestData>, Static<typeof RequestReaderErrorSchema>>;
334
+ reqReadable: Readable<Static<RequestData>, Static<typeof ReaderErrorSchema>>;
348
335
  }): Promise<Result<Static<ResponseData>, Static<ResponseErr>>>;
349
336
  }
350
337
  /**
@@ -352,7 +339,7 @@ interface UploadProcedure<State, RequestInit extends PayloadType, RequestData ex
352
339
  *
353
340
  * @template State - The context state object.
354
341
  * @template RequestInit - The TypeBox schema of the initialization object.
355
- * @template ResponseData - The TypeBox schema of the output object.
342
+ * @template ResponseData - The TypeBox schema of the response object.
356
343
  * @template ResponseErr - The TypeBox schema of the error object.
357
344
  */
358
345
  interface SubscriptionProcedure<State, RequestInit extends PayloadType, ResponseData extends PayloadType, ResponseErr extends ProcedureErrorSchemaType> {
@@ -361,10 +348,10 @@ interface SubscriptionProcedure<State, RequestInit extends PayloadType, Response
361
348
  responseData: ResponseData;
362
349
  responseError: ResponseErr;
363
350
  description?: string;
364
- handler({ ctx, reqInit, resWriter, }: {
351
+ handler(param: {
365
352
  ctx: ProcedureHandlerContext<State>;
366
353
  reqInit: Static<RequestInit>;
367
- resWriter: WriteStream<Result<Static<ResponseData>, Static<ResponseErr>>>;
354
+ resWritable: Writable<Result<Static<ResponseData>, Static<ResponseErr>>>;
368
355
  }): Promise<void | undefined>;
369
356
  }
370
357
  /**
@@ -373,8 +360,8 @@ interface SubscriptionProcedure<State, RequestInit extends PayloadType, Response
373
360
  *
374
361
  * @template State - The context state object.
375
362
  * @template RequestInit - The TypeBox schema of the initialization object.
376
- * @template RequestData - The TypeBox schema of the input object.
377
- * @template ResponseData - The TypeBox schema of the output object.
363
+ * @template RequestData - The TypeBox schema of the request object.
364
+ * @template ResponseData - The TypeBox schema of the response object.
378
365
  * @template ResponseErr - The TypeBox schema of the error object.
379
366
  */
380
367
  interface StreamProcedure<State, RequestInit extends PayloadType, RequestData extends PayloadType, ResponseData extends PayloadType, ResponseErr extends ProcedureErrorSchemaType> {
@@ -384,11 +371,11 @@ interface StreamProcedure<State, RequestInit extends PayloadType, RequestData ex
384
371
  responseData: ResponseData;
385
372
  responseError: ResponseErr;
386
373
  description?: string;
387
- handler({ ctx, reqInit, reqReader, resWriter, }: {
374
+ handler(param: {
388
375
  ctx: ProcedureHandlerContext<State>;
389
376
  reqInit: Static<RequestInit>;
390
- reqReader: ReadStream<Static<RequestData>, Static<typeof RequestReaderErrorSchema>>;
391
- resWriter: WriteStream<Result<Static<ResponseData>, Static<ResponseErr>>>;
377
+ reqReadable: Readable<Static<RequestData>, Static<typeof ReaderErrorSchema>>;
378
+ resWritable: Writable<Result<Static<ResponseData>, Static<ResponseErr>>>;
392
379
  }): Promise<void | undefined>;
393
380
  }
394
381
  /**
@@ -488,9 +475,9 @@ declare function stream<State, RequestInit extends PayloadType, RequestData exte
488
475
  *
489
476
  * @template State - The TypeBox schema of the state object.
490
477
  * @template Ty - The type of the procedure.
491
- * @template RequestData - The TypeBox schema of the input object.
492
- * @template RequestInit - The TypeBox schema of the input initialization object, if any.
493
- * @template ResponseData - The TypeBox schema of the output object.
478
+ * @template RequestData - The TypeBox schema of the request object.
479
+ * @template RequestInit - The TypeBox schema of the request initialization object, if any.
480
+ * @template ResponseData - The TypeBox schema of the response object.
494
481
  */
495
482
  type Procedure<State, Ty extends ValidProcType, RequestInit extends PayloadType, RequestData extends PayloadType | null, ResponseData extends PayloadType, ResponseErr extends ProcedureErrorSchemaType> = {
496
483
  type: Ty;
@@ -547,25 +534,25 @@ type ProcHandler<S extends AnyService, ProcName extends keyof S['procedures']> =
547
534
  */
548
535
  type ProcInit<S extends AnyService, ProcName extends keyof S['procedures']> = Static<S['procedures'][ProcName]['requestInit']>;
549
536
  /**
550
- * Helper to get the type definition for the procedure input of a service.
537
+ * Helper to get the type definition for the procedure request of a service.
551
538
  * @template S - The service.
552
539
  * @template ProcName - The name of the procedure.
553
540
  */
554
- type ProcInput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName] extends {
541
+ type ProcRequest<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName] extends {
555
542
  requestData: PayloadType;
556
543
  } ? Static<S['procedures'][ProcName]['requestData']> : never;
557
544
  /**
558
- * Helper to get the type definition for the procedure output of a service.
545
+ * Helper to get the type definition for the procedure response of a service.
559
546
  * @template S - The service.
560
547
  * @template ProcName - The name of the procedure.
561
548
  */
562
- type ProcOutput<S extends AnyService, ProcName extends keyof S['procedures']> = Static<S['procedures'][ProcName]['responseData']>;
549
+ type ProcResponse<S extends AnyService, ProcName extends keyof S['procedures']> = Static<S['procedures'][ProcName]['responseData']>;
563
550
  /**
564
551
  * Helper to get the type definition for the procedure errors of a service.
565
552
  * @template S - The service.
566
553
  * @template ProcName - The name of the procedure.
567
554
  */
568
- type ProcErrors<S extends AnyService, ProcName extends keyof S['procedures']> = Static<S['procedures'][ProcName]['responseError']> | Static<typeof ResponseReaderErrorSchema>;
555
+ type ProcErrors<S extends AnyService, ProcName extends keyof S['procedures']> = Static<S['procedures'][ProcName]['responseError']> | Static<typeof ReaderErrorSchema>;
569
556
  /**
570
557
  * Helper to get the type of procedure in a service.
571
558
  * @template S - The service.
@@ -596,6 +583,16 @@ interface SerializedProcedureSchemaProtocolv1 {
596
583
  interface SerializedServiceSchemaProtocolv1 {
597
584
  procedures: Record<string, SerializedProcedureSchemaProtocolv1>;
598
585
  }
586
+ interface SerializedServerSchemaProtocolv1 {
587
+ handshakeSchema?: TSchema;
588
+ services: Record<string, SerializedServiceSchemaProtocolv1>;
589
+ }
590
+ /**
591
+ * Same as {@link serializeSchema} but with a format that is compatible with
592
+ * protocolv1. This is useful to be able to continue to generate schemas for older
593
+ * clients as they are still supported.
594
+ */
595
+ declare function serializeSchemaV1Compat(services: AnyServiceSchemaMap, handshakeSchema?: TSchema): SerializedServerSchemaProtocolv1;
599
596
  interface SerializedProcedureSchema {
600
597
  init: PayloadType;
601
598
  input?: PayloadType;
@@ -766,7 +763,7 @@ declare class ServiceSchema<State extends object, Procedures extends ProcedureMa
766
763
  * protocol v1. This is useful to be able to continue to generate schemas for older
767
764
  * clients as they are still supported.
768
765
  */
769
- serializeBackwardsCompatible(): SerializedServiceSchemaProtocolv1;
766
+ serializeV1Compat(): SerializedServiceSchemaProtocolv1;
770
767
  /**
771
768
  * Instantiates this schema into a {@link Service} object.
772
769
  *
@@ -833,4 +830,4 @@ declare class ServiceScaffold<State extends object> {
833
830
  }>;
834
831
  }
835
832
 
836
- export { AnyServiceSchemaMap as A, BaseErrorSchemaType as B, RequestReaderErrorSchema as C, createClient as D, ErrResult as E, Client as F, Ok as G, Err as H, InstantiatedServiceSchemaMap as I, ResultUnwrapOk as J, ResultUnwrapErr as K, Output as L, OkResult as O, PayloadType as P, ReadStream as R, SimpleIterator as S, UploadProcedure as U, ValidProcType as V, WriteStream as W, ProcedureErrorSchemaType as a, Procedure as b, Result as c, ResponseReaderErrorSchema as d, Service as e, ServiceConfiguration as f, ProcHandler as g, ProcInit as h, ProcInput as i, ProcOutput as j, ProcErrors as k, ProcType as l, ServiceSchema as m, SerializedServerSchema as n, SerializedServiceSchema as o, SerializedProcedureSchema as p, ProcedureMap as q, RpcProcedure as r, serializeSchema as s, SubscriptionProcedure as t, StreamProcedure as u, INTERNAL_RIVER_ERROR_CODE as v, UNCAUGHT_ERROR_CODE as w, UNEXPECTED_DISCONNECT_CODE as x, INVALID_REQUEST_CODE as y, ABORT_CODE as z };
833
+ export { AnyServiceSchemaMap as A, BaseErrorSchemaType as B, StreamProcedure as C, flattenErrorType as D, UNCAUGHT_ERROR_CODE as E, UNEXPECTED_DISCONNECT_CODE as F, INVALID_REQUEST_CODE as G, CANCEL_CODE as H, InstantiatedServiceSchemaMap as I, createClient as J, Client as K, Err as L, unwrapOrThrow as M, ErrResult as N, Ok as O, PayloadType as P, OkResult as Q, Readable as R, Service as S, ResultUnwrapOk as T, UploadProcedure as U, ValidProcType as V, Writable as W, ResultUnwrapErr as X, ResponseData as Y, ReadableIterator as a, ReadableResult as b, ProcedureErrorSchemaType as c, Procedure as d, Result as e, ReaderErrorSchema as f, AnyProcedure as g, ServiceConfiguration as h, ProcHandler as i, ProcInit as j, ProcRequest as k, ProcResponse as l, ProcErrors as m, ProcType as n, ServiceSchema as o, SerializedServerSchema as p, SerializedServiceSchema as q, SerializedProcedureSchema as r, serializeSchema as s, serializeSchemaV1Compat as t, SerializedServerSchemaProtocolv1 as u, SerializedServiceSchemaProtocolv1 as v, SerializedProcedureSchemaProtocolv1 as w, ProcedureMap as x, RpcProcedure as y, SubscriptionProcedure as z };