@replit/river 0.15.7 → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,241 +0,0 @@
1
- import { TObject, TUnion, TString, TSchema, TNever, TLiteral, Static } from '@sinclair/typebox';
2
- import { Pushable } from 'it-pushable';
3
- import { b as TransportClientId, e as Session, C as Connection } from './index-2e402bb8.js';
4
-
5
- /**
6
- * The context for services/procedures. This is used only on
7
- * the server.
8
- *
9
- * An important detail is that the state prop is always on
10
- * this interface and it shouldn't be changed, removed, or
11
- * extended. This prop is for the state of a service.
12
- *
13
- * You should use declaration merging to extend this interface
14
- * with whatever you need. For example, if you need to access
15
- * a database, you could do:
16
- *
17
- * ```ts
18
- * declare module '@replit/river' {
19
- * interface ServiceContext {
20
- * db: Database;
21
- * }
22
- * }
23
- * ```
24
- */
25
-
26
- interface ServiceContext {
27
- }
28
- /**
29
- * The {@link ServiceContext} with state. This is what is passed to procedures.
30
- */
31
- type ServiceContextWithState<State> = ServiceContext & {
32
- state: State;
33
- };
34
- type ServiceContextWithTransportInfo<State> = ServiceContext & {
35
- state: State;
36
- to: TransportClientId;
37
- from: TransportClientId;
38
- streamId: string;
39
- session: Session<Connection>;
40
- };
41
-
42
- type TLiteralString = TLiteral<string>;
43
- type RiverErrorSchema = TObject<{
44
- code: TLiteralString | TUnion<Array<TLiteralString>>;
45
- message: TLiteralString | TString;
46
- }> | TObject<{
47
- code: TLiteralString | TUnion<Array<TLiteralString>>;
48
- message: TLiteralString | TString;
49
- extras: TSchema;
50
- }>;
51
- type RiverError = TUnion<Array<RiverErrorSchema>> | RiverErrorSchema | TNever;
52
- declare const UNCAUGHT_ERROR = "UNCAUGHT_ERROR";
53
- declare const RiverUncaughtSchema: TObject<{
54
- code: TUnion<[TLiteral<"UNCAUGHT_ERROR">, TLiteral<"UNEXPECTED_DISCONNECT">]>;
55
- message: TString;
56
- }>;
57
- type Result<T, E> = {
58
- ok: true;
59
- payload: T;
60
- } | {
61
- ok: false;
62
- payload: E;
63
- };
64
- declare function Ok<T, E>(payload: T): Result<T, E>;
65
- declare function Err<T, E>(error: E): Result<T, E>;
66
-
67
- /**
68
- * The valid {@link Procedure} types. The `stream` and `upload` types can optionally have a
69
- * different type for the very first initialization message. The suffixless types correspond to
70
- * gRPC's four combinations of stream / non-stream in each direction.
71
- */
72
- type ValidProcType = 'rpc' | 'upload' | 'subscription' | 'stream';
73
- /**
74
- * A generic procedure listing where the keys are the names of the procedures
75
- * and the values are the {@link Procedure} definitions. This is not meant to
76
- * be constructed directly, use the {@link ServiceBuilder} class instead.
77
- */
78
- type ProcListing = Record<string, AnyProcedure>;
79
- /**
80
- * Represents a service with a name, state, and procedures.
81
- * This is not meant to be constructed directly, use the {@link ServiceBuilder} class instead.
82
- * @template Name The type of the service name.
83
- * @template State The type of the service state.
84
- * @template Procs The type of the service procedures.
85
- */
86
- interface Service<Name extends string, State extends object, Procs extends ProcListing> {
87
- name: Name;
88
- state: State;
89
- procedures: Procs;
90
- }
91
- type AnyService = Service<string, object, ProcListing>;
92
- /**
93
- * Serializes a service object into its corresponding JSON Schema Draft 7 type.
94
- * @param {AnyService} s - The service object to serialize.
95
- * @returns A plain object representing the serialized service.
96
- */
97
- declare function serializeService(s: AnyService): object;
98
- /**
99
- * Helper to get the type definition for a specific handler of a procedure in a service.
100
- * @template S - The service.
101
- * @template ProcName - The name of the procedure.
102
- */
103
- type ProcHandler<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['handler'];
104
- /**
105
- * Helper to get whether the type definition for the procedure contains an init type.
106
- * @template S - The service.
107
- * @template ProcName - The name of the procedure.
108
- */
109
- type ProcHasInit<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName] extends {
110
- init: TObject;
111
- } ? true : false;
112
- /**
113
- * Helper to get the type definition for the procedure init type of a service.
114
- * @template S - The service.
115
- * @template ProcName - The name of the procedure.
116
- */
117
- type ProcInit<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName] extends {
118
- init: TObject;
119
- } ? S['procedures'][ProcName]['init'] : never;
120
- /**
121
- * Helper to get the type definition for the procedure input of a service.
122
- * @template S - The service.
123
- * @template ProcName - The name of the procedure.
124
- */
125
- type ProcInput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['input'];
126
- /**
127
- * Helper to get the type definition for the procedure output of a service.
128
- * @template S - The service.
129
- * @template ProcName - The name of the procedure.
130
- */
131
- type ProcOutput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['output'];
132
- /**
133
- * Helper to get the type definition for the procedure errors of a service.
134
- * @template S - The service.
135
- * @template ProcName - The name of the procedure.
136
- */
137
- type ProcErrors<S extends AnyService, ProcName extends keyof S['procedures']> = TUnion<[S['procedures'][ProcName]['errors'], typeof RiverUncaughtSchema]>;
138
- /**
139
- * Helper to get the type of procedure in a service.
140
- * @template S - The service.
141
- * @template ProcName - The name of the procedure.
142
- */
143
- type ProcType<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['type'];
144
- type PayloadType = TObject | TUnion<Array<TObject>>;
145
- /**
146
- * Defines a Procedure type that can be either an RPC or a stream procedure.
147
- * @template State - The TypeBox schema of the state object.
148
- * @template Ty - The type of the procedure.
149
- * @template I - The TypeBox schema of the input object.
150
- * @template O - The TypeBox schema of the output object.
151
- * @template Init - The TypeBox schema of the input initialization object.
152
- */
153
- type Procedure<State, Ty extends ValidProcType, I extends PayloadType, O extends PayloadType, E extends RiverError, Init extends PayloadType | null = null> = Ty extends 'rpc' ? Init extends null ? {
154
- input: I;
155
- output: O;
156
- errors: E;
157
- handler: (context: ServiceContextWithTransportInfo<State>, input: Static<I>) => Promise<Result<Static<O>, Static<E>>>;
158
- type: Ty;
159
- } : never : Ty extends 'upload' ? Init extends PayloadType ? {
160
- init: Init;
161
- input: I;
162
- output: O;
163
- errors: E;
164
- handler: (context: ServiceContextWithTransportInfo<State>, init: Static<Init>, input: AsyncIterableIterator<Static<I>>) => Promise<Result<Static<O>, Static<E>>>;
165
- type: Ty;
166
- } : {
167
- input: I;
168
- output: O;
169
- errors: E;
170
- handler: (context: ServiceContextWithTransportInfo<State>, input: AsyncIterableIterator<Static<I>>) => Promise<Result<Static<O>, Static<E>>>;
171
- type: Ty;
172
- } : Ty extends 'subscription' ? Init extends null ? {
173
- input: I;
174
- output: O;
175
- errors: E;
176
- handler: (context: ServiceContextWithTransportInfo<State>, input: Static<I>, output: Pushable<Result<Static<O>, Static<E>>>) => Promise<(() => void) | void>;
177
- type: Ty;
178
- } : never : Ty extends 'stream' ? Init extends PayloadType ? {
179
- init: Init;
180
- input: I;
181
- output: O;
182
- errors: E;
183
- handler: (context: ServiceContextWithTransportInfo<State>, init: Static<Init>, input: AsyncIterableIterator<Static<I>>, output: Pushable<Result<Static<O>, Static<E>>>) => Promise<void>;
184
- type: Ty;
185
- } : {
186
- input: I;
187
- output: O;
188
- errors: E;
189
- handler: (context: ServiceContextWithTransportInfo<State>, input: AsyncIterableIterator<Static<I>>, output: Pushable<Result<Static<O>, Static<E>>>) => Promise<void>;
190
- type: Ty;
191
- } : never;
192
- type AnyProcedure = Procedure<object, ValidProcType, PayloadType, PayloadType, RiverError, PayloadType | null>;
193
- /**
194
- * A builder class for creating River Services.
195
- * You must call the finalize method to get the finalized schema for use in a service router.
196
- * @template T The type of the service.
197
- */
198
- declare class ServiceBuilder<T extends Service<string, object, ProcListing>> {
199
- private readonly schema;
200
- private constructor();
201
- /**
202
- * Finalizes the schema for the service.
203
- */
204
- finalize(): Readonly<T>;
205
- /**
206
- * Sets the initial state for the service.
207
- * @template InitState The type of the initial state.
208
- * @param {InitState} state The initial state for the service.
209
- * @returns {ServiceBuilder<{ name: T['name']; state: InitState; procedures: T['procedures']; }>} A new ServiceBuilder instance with the updated schema.
210
- */
211
- initialState<InitState extends T['state']>(state: InitState): ServiceBuilder<{
212
- name: T['name'];
213
- state: InitState;
214
- procedures: T['procedures'];
215
- }>;
216
- /**
217
- * Defines a new procedure for the service.
218
- * @param {ProcName} procName The name of the procedure.
219
- * @param {Procedure<T['state'], Ty, I, O, E, Init>} procDef The definition of the procedure.
220
- * @returns {ServiceBuilder<{ name: T['name']; state: T['state']; procedures: T['procedures'] & { [k in ProcName]: Procedure<T['state'], Ty, I, O, E, Init>; }; }>} A new ServiceBuilder instance with the updated schema.
221
- */
222
- defineProcedure<ProcName extends string, Ty extends ValidProcType, I extends PayloadType, O extends PayloadType, E extends RiverError, Init extends PayloadType | null = null>(procName: ProcName, procDef: Procedure<T['state'], Ty, I, O, E, Init>): ServiceBuilder<{
223
- name: T['name'];
224
- state: T['state'];
225
- procedures: T['procedures'] & {
226
- [k in ProcName]: Procedure<T['state'], Ty, I, O, E, Init>;
227
- };
228
- }>;
229
- /**
230
- * Creates a new instance of ServiceBuilder.
231
- * @param {Name} name The name of the service.
232
- * @returns {ServiceBuilder<{ name: Name; state: {}; procedures: {}; }>} A new instance of ServiceBuilder.
233
- */
234
- static create<Name extends string>(name: Name): ServiceBuilder<{
235
- name: Name;
236
- state: object;
237
- procedures: ProcListing;
238
- }>;
239
- }
240
-
241
- export { AnyService as A, Err as E, Ok as O, PayloadType as P, RiverError as R, ServiceContext as S, UNCAUGHT_ERROR as U, ValidProcType as V, Procedure as a, Result as b, RiverUncaughtSchema as c, ProcType as d, ProcInput as e, ProcOutput as f, ProcErrors as g, ProcHasInit as h, ProcInit as i, ServiceBuilder as j, ProcListing as k, Service as l, ProcHandler as m, ServiceContextWithState as n, ServiceContextWithTransportInfo as o, RiverErrorSchema as p, serializeService as s };