@replit/river 0.3.0 → 0.5.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/__tests__/bandwidth.bench.d.ts +1 -0
- package/dist/__tests__/bandwidth.bench.d.ts.map +1 -0
- package/dist/__tests__/bandwidth.bench.js +12 -5
- package/dist/__tests__/e2e.test.d.ts +2 -0
- package/dist/__tests__/e2e.test.d.ts.map +1 -0
- package/dist/__tests__/e2e.test.js +153 -0
- package/dist/__tests__/fixtures.d.ts +155 -0
- package/dist/__tests__/fixtures.d.ts.map +1 -0
- package/dist/__tests__/fixtures.js +129 -0
- package/dist/__tests__/handler.test.d.ts +2 -0
- package/dist/__tests__/handler.test.d.ts.map +1 -0
- package/dist/__tests__/handler.test.js +71 -0
- package/dist/__tests__/serialize.test.d.ts +2 -0
- package/dist/__tests__/serialize.test.d.ts.map +1 -0
- package/dist/__tests__/serialize.test.js +135 -0
- package/dist/__tests__/typescript-stress.test.d.ts +736 -98
- package/dist/__tests__/typescript-stress.test.d.ts.map +1 -0
- package/dist/__tests__/typescript-stress.test.js +13 -1
- package/dist/codec/codec.test.d.ts +1 -0
- package/dist/codec/codec.test.d.ts.map +1 -0
- package/dist/codec/index.d.ts +1 -0
- package/dist/codec/index.d.ts.map +1 -0
- package/dist/codec/json.d.ts +5 -0
- package/dist/codec/json.d.ts.map +1 -0
- package/dist/codec/json.js +4 -0
- package/dist/codec/types.d.ts +15 -0
- package/dist/codec/types.d.ts.map +1 -0
- package/dist/logging/index.d.ts +13 -0
- package/dist/logging/index.d.ts.map +1 -0
- package/dist/logging/index.js +12 -0
- package/dist/router/builder.d.ts +91 -7
- package/dist/router/builder.d.ts.map +1 -0
- package/dist/router/builder.js +32 -0
- package/dist/router/client.d.ts +28 -3
- package/dist/router/client.d.ts.map +1 -0
- package/dist/router/client.js +37 -6
- package/dist/router/context.d.ts +2 -0
- package/dist/router/context.d.ts.map +1 -0
- package/dist/router/index.d.ts +1 -0
- package/dist/router/index.d.ts.map +1 -0
- package/dist/router/result.d.ts +25 -0
- package/dist/router/result.d.ts.map +1 -0
- package/dist/router/result.js +18 -0
- package/dist/router/server.d.ts +13 -0
- package/dist/router/server.d.ts.map +1 -0
- package/dist/router/server.js +85 -56
- package/dist/testUtils.d.ts +69 -2
- package/dist/testUtils.d.ts.map +1 -0
- package/dist/testUtils.js +91 -4
- package/dist/transport/impls/stdio.d.ts +25 -0
- package/dist/transport/impls/stdio.d.ts.map +1 -0
- package/dist/transport/impls/stdio.js +24 -0
- package/dist/transport/impls/stdio.test.d.ts +1 -0
- package/dist/transport/impls/stdio.test.d.ts.map +1 -0
- package/dist/transport/impls/stdio.test.js +2 -8
- package/dist/transport/impls/ws.d.ts +40 -1
- package/dist/transport/impls/ws.d.ts.map +1 -0
- package/dist/transport/impls/ws.js +39 -2
- package/dist/transport/impls/ws.test.d.ts +1 -0
- package/dist/transport/impls/ws.test.d.ts.map +1 -0
- package/dist/transport/impls/ws.test.js +8 -20
- package/dist/transport/index.d.ts +9 -2
- package/dist/transport/index.d.ts.map +1 -0
- package/dist/transport/index.js +7 -1
- package/dist/transport/message.d.ts +94 -36
- package/dist/transport/message.d.ts.map +1 -0
- package/dist/transport/message.js +66 -19
- package/dist/transport/message.test.d.ts +1 -0
- package/dist/transport/message.test.d.ts.map +1 -0
- package/dist/transport/message.test.js +39 -6
- package/dist/transport/types.d.ts +38 -2
- package/dist/transport/types.d.ts.map +1 -0
- package/dist/transport/types.js +44 -5
- package/package.json +1 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typescript-stress.test.d.ts","sourceRoot":"","sources":["../../__tests__/typescript-stress.test.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAS,MAAM,sBAAsB,CAAC;AAEhF,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AA8B/C,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmDlB,CAAC;AAGhB,qBAAa,aAAc,SAAQ,SAAS;gBAC9B,QAAQ,EAAE,MAAM;IAI5B,IAAI,CAAC,GAAG,EAAE,sBAAsB,GAAG,SAAS;IAKtC,KAAK;CACZ"}
|
|
@@ -6,14 +6,26 @@ import { createServer } from '../router/server';
|
|
|
6
6
|
import { Transport } from '../transport/types';
|
|
7
7
|
import { NaiveJsonCodec } from '../codec/json';
|
|
8
8
|
import { createClient } from '../router/client';
|
|
9
|
+
import { Ok } from '../router/result';
|
|
9
10
|
const input = Type.Object({ a: Type.Number() });
|
|
10
11
|
const output = Type.Object({ b: Type.Number() });
|
|
12
|
+
const errors = Type.Union([
|
|
13
|
+
Type.Object({
|
|
14
|
+
code: Type.Literal('ERROR1'),
|
|
15
|
+
message: Type.String(),
|
|
16
|
+
}),
|
|
17
|
+
Type.Object({
|
|
18
|
+
code: Type.Literal('ERROR2'),
|
|
19
|
+
message: Type.String(),
|
|
20
|
+
}),
|
|
21
|
+
]);
|
|
11
22
|
const fnBody = {
|
|
12
23
|
type: 'rpc',
|
|
13
24
|
input,
|
|
14
25
|
output,
|
|
26
|
+
errors,
|
|
15
27
|
async handler(_state, msg) {
|
|
16
|
-
return reply(msg, { b: msg.payload.a });
|
|
28
|
+
return reply(msg, Ok({ b: msg.payload.a }));
|
|
17
29
|
},
|
|
18
30
|
};
|
|
19
31
|
// typescript is limited to max 50 constraints
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec.test.d.ts","sourceRoot":"","sources":["../../codec/codec.test.ts"],"names":[],"mappings":""}
|
package/dist/codec/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../codec/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AACxC,YAAY,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/codec/json.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../codec/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC;;;GAGG;AACH,eAAO,MAAM,cAAc,EAAE,KAS5B,CAAC"}
|
package/dist/codec/json.js
CHANGED
package/dist/codec/types.d.ts
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codec interface for encoding and decoding objects to and from string buffers.
|
|
3
|
+
* Used to prepare messages for use by the transport layer.
|
|
4
|
+
*/
|
|
1
5
|
export interface Codec {
|
|
6
|
+
/**
|
|
7
|
+
* Encodes an object to a string buffer.
|
|
8
|
+
* @param obj - The object to encode.
|
|
9
|
+
* @returns The encoded string buffer.
|
|
10
|
+
*/
|
|
2
11
|
toStringBuf(obj: object): string;
|
|
12
|
+
/**
|
|
13
|
+
* Decodes an object from a string buffer.
|
|
14
|
+
* @param buf - The string buffer to decode.
|
|
15
|
+
* @returns The decoded object, or null if decoding failed.
|
|
16
|
+
*/
|
|
3
17
|
fromStringBuf(buf: string): object | null;
|
|
4
18
|
}
|
|
19
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../codec/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,KAAK;IACpB;;;;OAIG;IACH,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IACjC;;;;OAIG;IACH,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CAC3C"}
|
package/dist/logging/index.d.ts
CHANGED
|
@@ -9,7 +9,20 @@ export type Logger = {
|
|
|
9
9
|
} & {
|
|
10
10
|
[key in LoggingLevel]: (msg: string) => void;
|
|
11
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* The global River logger instance.
|
|
14
|
+
*/
|
|
12
15
|
export declare let log: Logger | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Binds the given write function to River's logger {@link log}.
|
|
18
|
+
* @param write - The function to write log messages.
|
|
19
|
+
* @param color - Whether to use colored log levels.
|
|
20
|
+
*/
|
|
13
21
|
export declare function bindLogger(write: (msg: string) => void, color?: boolean): void;
|
|
22
|
+
/**
|
|
23
|
+
* Sets the minimum logging level for the logger.
|
|
24
|
+
* @param level - The minimum logging level to set.
|
|
25
|
+
*/
|
|
14
26
|
export declare function setLevel(level: LoggingLevel): void;
|
|
15
27
|
export {};
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../logging/index.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,aAAa;;;;CAIT,CAAC;AAEX,KAAK,YAAY,GAAG,MAAM,OAAO,aAAa,CAAC;AAC/C,MAAM,MAAM,MAAM,GAAG;IACnB,QAAQ,EAAE,YAAY,CAAC;CACxB,GAAG;KACD,GAAG,IAAI,YAAY,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI;CAC7C,CAAC;AAEF;;GAEG;AACH,eAAO,IAAI,GAAG,EAAE,MAAM,GAAG,SAAS,CAAC;AAGnC;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,EAAE,KAAK,CAAC,EAAE,OAAO,QAoBvE;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,YAAY,QAI3C"}
|
package/dist/logging/index.js
CHANGED
|
@@ -3,8 +3,16 @@ const LoggingLevels = {
|
|
|
3
3
|
warn: 1,
|
|
4
4
|
error: 2,
|
|
5
5
|
};
|
|
6
|
+
/**
|
|
7
|
+
* The global River logger instance.
|
|
8
|
+
*/
|
|
6
9
|
export let log;
|
|
7
10
|
const defaultLoggingLevel = 'warn';
|
|
11
|
+
/**
|
|
12
|
+
* Binds the given write function to River's logger {@link log}.
|
|
13
|
+
* @param write - The function to write log messages.
|
|
14
|
+
* @param color - Whether to use colored log levels.
|
|
15
|
+
*/
|
|
8
16
|
export function bindLogger(write, color) {
|
|
9
17
|
const info = color ? '\u001b[37minfo\u001b[0m' : 'info';
|
|
10
18
|
const warn = color ? '\u001b[33mwarn\u001b[0m' : 'warn';
|
|
@@ -22,6 +30,10 @@ export function bindLogger(write, color) {
|
|
|
22
30
|
minLevel: log?.minLevel ?? defaultLoggingLevel,
|
|
23
31
|
};
|
|
24
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Sets the minimum logging level for the logger.
|
|
35
|
+
* @param level - The minimum logging level to set.
|
|
36
|
+
*/
|
|
25
37
|
export function setLevel(level) {
|
|
26
38
|
if (log) {
|
|
27
39
|
log.minLevel = level;
|
package/dist/router/builder.d.ts
CHANGED
|
@@ -1,50 +1,134 @@
|
|
|
1
|
-
import { TObject, Static } from '@sinclair/typebox';
|
|
1
|
+
import { TObject, Static, TUnion } from '@sinclair/typebox';
|
|
2
2
|
import type { Pushable } from 'it-pushable';
|
|
3
3
|
import { TransportMessage } from '../transport/message';
|
|
4
4
|
import { ServiceContextWithState } from './context';
|
|
5
|
+
import { Result, RiverError, RiverUncaughtSchema } from './result';
|
|
6
|
+
/**
|
|
7
|
+
* The valid {@link Procedure} types.
|
|
8
|
+
*/
|
|
5
9
|
export type ValidProcType = 'stream' | 'rpc';
|
|
6
|
-
|
|
10
|
+
/**
|
|
11
|
+
* A generic procedure listing where the keys are the names of the procedures
|
|
12
|
+
* and the values are the {@link Procedure} definitions. This is not meant to
|
|
13
|
+
* be constructed directly, use the {@link ServiceBuilder} class instead.
|
|
14
|
+
*/
|
|
15
|
+
export type ProcListing = Record<string, AnyProcedure>;
|
|
16
|
+
/**
|
|
17
|
+
* Represents a service with a name, state, and procedures.
|
|
18
|
+
* This is not meant to be constructed directly, use the {@link ServiceBuilder} class instead.
|
|
19
|
+
* @template Name The type of the service name.
|
|
20
|
+
* @template State The type of the service state.
|
|
21
|
+
* @template Procs The type of the service procedures.
|
|
22
|
+
*/
|
|
7
23
|
export interface Service<Name extends string, State extends object, Procs extends ProcListing> {
|
|
8
24
|
name: Name;
|
|
9
25
|
state: State;
|
|
10
26
|
procedures: Procs;
|
|
11
27
|
}
|
|
12
28
|
export type AnyService = Service<string, object, any>;
|
|
29
|
+
/**
|
|
30
|
+
* Serializes a service object into its corresponding JSON Schema Draft 7 type.
|
|
31
|
+
* @param {AnyService} s - The service object to serialize.
|
|
32
|
+
* @returns A plain object representing the serialized service.
|
|
33
|
+
*/
|
|
13
34
|
export declare function serializeService(s: AnyService): object;
|
|
35
|
+
/**
|
|
36
|
+
* Helper to get the type definition for a specific handler of a procedure in a service.
|
|
37
|
+
* @template S - The service.
|
|
38
|
+
* @template ProcName - The name of the procedure.
|
|
39
|
+
*/
|
|
14
40
|
export type ProcHandler<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['handler'];
|
|
41
|
+
/**
|
|
42
|
+
* Helper to get the type definition for the procedure input of a service.
|
|
43
|
+
* @template S - The service.
|
|
44
|
+
* @template ProcName - The name of the procedure.
|
|
45
|
+
*/
|
|
15
46
|
export type ProcInput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['input'];
|
|
47
|
+
/**
|
|
48
|
+
* Helper to get the type definition for the procedure output of a service.
|
|
49
|
+
* @template S - The service.
|
|
50
|
+
* @template ProcName - The name of the procedure.
|
|
51
|
+
*/
|
|
16
52
|
export type ProcOutput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['output'];
|
|
53
|
+
/**
|
|
54
|
+
* Helper to get the type definition for the procedure errors of a service.
|
|
55
|
+
* @template S - The service.
|
|
56
|
+
* @template ProcName - The name of the procedure.
|
|
57
|
+
*/
|
|
58
|
+
export type ProcErrors<S extends AnyService, ProcName extends keyof S['procedures']> = TUnion<[S['procedures'][ProcName]['errors'], typeof RiverUncaughtSchema]>;
|
|
59
|
+
/**
|
|
60
|
+
* Helper to get the type of procedure in a service.
|
|
61
|
+
* @template S - The service.
|
|
62
|
+
* @template ProcName - The name of the procedure.
|
|
63
|
+
*/
|
|
17
64
|
export type ProcType<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['type'];
|
|
18
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Defines a Procedure type that can be either an RPC or a stream procedure.
|
|
67
|
+
* @template State - The TypeBox schema of the state object.
|
|
68
|
+
* @template Ty - The type of the procedure, either 'rpc' or 'stream'.
|
|
69
|
+
* @template I - The TypeBox schema of the input object.
|
|
70
|
+
* @template O - The TypeBox schema of the output object.
|
|
71
|
+
*/
|
|
72
|
+
export type Procedure<State extends object | unknown, Ty extends ValidProcType, I extends TObject, O extends TObject, E extends RiverError> = Ty extends 'rpc' ? {
|
|
19
73
|
input: I;
|
|
20
74
|
output: O;
|
|
21
|
-
|
|
75
|
+
errors: E;
|
|
76
|
+
handler: (context: ServiceContextWithState<State>, input: TransportMessage<Static<I>>) => Promise<TransportMessage<Result<Static<O>, Static<E>>>>;
|
|
22
77
|
type: Ty;
|
|
23
78
|
} : {
|
|
24
79
|
input: I;
|
|
25
80
|
output: O;
|
|
26
|
-
|
|
81
|
+
errors: E;
|
|
82
|
+
handler: (context: ServiceContextWithState<State>, input: AsyncIterable<TransportMessage<Static<I>>>, output: Pushable<TransportMessage<Result<Static<O>, Static<E>>>>) => Promise<void>;
|
|
27
83
|
type: Ty;
|
|
28
84
|
};
|
|
85
|
+
export type AnyProcedure = Procedure<object, ValidProcType, TObject, TObject, RiverError>;
|
|
86
|
+
/**
|
|
87
|
+
* A builder class for creating River Services.
|
|
88
|
+
* You must call the finalize method to get the finalized schema for use in a service router.
|
|
89
|
+
* @template T The type of the service.
|
|
90
|
+
*/
|
|
29
91
|
export declare class ServiceBuilder<T extends Service<string, object, ProcListing>> {
|
|
30
92
|
private readonly schema;
|
|
31
93
|
private constructor();
|
|
94
|
+
/**
|
|
95
|
+
* Finalizes the schema for the service.
|
|
96
|
+
* @returns {T} The finalized schema for the service.
|
|
97
|
+
*/
|
|
32
98
|
finalize(): T;
|
|
99
|
+
/**
|
|
100
|
+
* Sets the initial state for the service.
|
|
101
|
+
* @template InitState The type of the initial state.
|
|
102
|
+
* @param {InitState} state The initial state for the service.
|
|
103
|
+
* @returns {ServiceBuilder<{ name: T['name']; state: InitState; procedures: T['procedures']; }>} A new ServiceBuilder instance with the updated schema.
|
|
104
|
+
*/
|
|
33
105
|
initialState<InitState extends T['state']>(state: InitState): ServiceBuilder<{
|
|
34
106
|
name: T['name'];
|
|
35
107
|
state: InitState;
|
|
36
108
|
procedures: T['procedures'];
|
|
37
109
|
}>;
|
|
38
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Defines a new procedure for the service.
|
|
112
|
+
* @param {ProcName} procName The name of the procedure.
|
|
113
|
+
* @param {Procedure<T['state'], Ty, I, O>} procDef The definition of the procedure.
|
|
114
|
+
* @returns {ServiceBuilder<{ name: T['name']; state: T['state']; procedures: T['procedures'] & { [k in ProcName]: Procedure<T['state'], Ty, I, O>; }; }>} A new ServiceBuilder instance with the updated schema.
|
|
115
|
+
*/
|
|
116
|
+
defineProcedure<ProcName extends string, Ty extends ValidProcType, I extends TObject, O extends TObject, E extends RiverError>(procName: ProcName, procDef: Procedure<T['state'], Ty, I, O, E>): ServiceBuilder<{
|
|
39
117
|
name: T['name'];
|
|
40
118
|
state: T['state'];
|
|
41
119
|
procedures: T['procedures'] & {
|
|
42
|
-
[k in ProcName]: Procedure<T['state'], Ty, I, O>;
|
|
120
|
+
[k in ProcName]: Procedure<T['state'], Ty, I, O, E>;
|
|
43
121
|
};
|
|
44
122
|
}>;
|
|
123
|
+
/**
|
|
124
|
+
* Creates a new instance of ServiceBuilder.
|
|
125
|
+
* @param {Name} name The name of the service.
|
|
126
|
+
* @returns {ServiceBuilder<{ name: Name; state: {}; procedures: {}; }>} A new instance of ServiceBuilder.
|
|
127
|
+
*/
|
|
45
128
|
static create<Name extends string>(name: Name): ServiceBuilder<{
|
|
46
129
|
name: Name;
|
|
47
130
|
state: {};
|
|
48
131
|
procedures: {};
|
|
49
132
|
}>;
|
|
50
133
|
}
|
|
134
|
+
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../router/builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAQ,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAClE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAEnE;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,KAAK,CAAC;AAE7C;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AAEvD;;;;;;GAMG;AACH,MAAM,WAAW,OAAO,CACtB,IAAI,SAAS,MAAM,EACnB,KAAK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
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
|
+
/**
|
|
3
|
+
* Serializes a service object into its corresponding JSON Schema Draft 7 type.
|
|
4
|
+
* @param {AnyService} s - The service object to serialize.
|
|
5
|
+
* @returns A plain object representing the serialized service.
|
|
6
|
+
*/
|
|
2
7
|
export function serializeService(s) {
|
|
3
8
|
return {
|
|
4
9
|
name: s.name,
|
|
@@ -8,25 +13,47 @@ export function serializeService(s) {
|
|
|
8
13
|
{
|
|
9
14
|
input: Type.Strict(procDef.input),
|
|
10
15
|
output: Type.Strict(procDef.output),
|
|
16
|
+
errors: Type.Strict(procDef.errors),
|
|
11
17
|
type: procDef.type,
|
|
12
18
|
},
|
|
13
19
|
])),
|
|
14
20
|
};
|
|
15
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* A builder class for creating River Services.
|
|
24
|
+
* You must call the finalize method to get the finalized schema for use in a service router.
|
|
25
|
+
* @template T The type of the service.
|
|
26
|
+
*/
|
|
16
27
|
export class ServiceBuilder {
|
|
17
28
|
schema;
|
|
18
29
|
constructor(schema) {
|
|
19
30
|
this.schema = schema;
|
|
20
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Finalizes the schema for the service.
|
|
34
|
+
* @returns {T} The finalized schema for the service.
|
|
35
|
+
*/
|
|
21
36
|
finalize() {
|
|
22
37
|
return this.schema;
|
|
23
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Sets the initial state for the service.
|
|
41
|
+
* @template InitState The type of the initial state.
|
|
42
|
+
* @param {InitState} state The initial state for the service.
|
|
43
|
+
* @returns {ServiceBuilder<{ name: T['name']; state: InitState; procedures: T['procedures']; }>} A new ServiceBuilder instance with the updated schema.
|
|
44
|
+
*/
|
|
24
45
|
initialState(state) {
|
|
25
46
|
return new ServiceBuilder({
|
|
26
47
|
...this.schema,
|
|
27
48
|
state,
|
|
28
49
|
});
|
|
29
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Defines a new procedure for the service.
|
|
53
|
+
* @param {ProcName} procName The name of the procedure.
|
|
54
|
+
* @param {Procedure<T['state'], Ty, I, O>} procDef The definition of the procedure.
|
|
55
|
+
* @returns {ServiceBuilder<{ name: T['name']; state: T['state']; procedures: T['procedures'] & { [k in ProcName]: Procedure<T['state'], Ty, I, O>; }; }>} A new ServiceBuilder instance with the updated schema.
|
|
56
|
+
*/
|
|
30
57
|
defineProcedure(procName, procDef) {
|
|
31
58
|
const newProcedure = { [procName]: procDef };
|
|
32
59
|
const procedures = {
|
|
@@ -38,6 +65,11 @@ export class ServiceBuilder {
|
|
|
38
65
|
procedures,
|
|
39
66
|
});
|
|
40
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Creates a new instance of ServiceBuilder.
|
|
70
|
+
* @param {Name} name The name of the service.
|
|
71
|
+
* @returns {ServiceBuilder<{ name: Name; state: {}; procedures: {}; }>} A new instance of ServiceBuilder.
|
|
72
|
+
*/
|
|
41
73
|
static create(name) {
|
|
42
74
|
return new ServiceBuilder({
|
|
43
75
|
name,
|
package/dist/router/client.d.ts
CHANGED
|
@@ -1,17 +1,42 @@
|
|
|
1
1
|
import { Transport } from '../transport/types';
|
|
2
|
-
import { AnyService, ProcInput, ProcOutput, ProcType } from './builder';
|
|
2
|
+
import { AnyService, ProcErrors, ProcInput, ProcOutput, ProcType } from './builder';
|
|
3
3
|
import type { Pushable } from 'it-pushable';
|
|
4
4
|
import { Server } from './server';
|
|
5
5
|
import { Static } from '@sinclair/typebox';
|
|
6
|
+
import { Result } from './result';
|
|
7
|
+
type AsyncIter<T> = AsyncGenerator<T, T, T>;
|
|
8
|
+
/**
|
|
9
|
+
* A helper type to transform an actual service type into a type
|
|
10
|
+
* we can case to in the proxy.
|
|
11
|
+
* @template Router - The type of the Router.
|
|
12
|
+
*/
|
|
6
13
|
type ServiceClient<Router extends AnyService> = {
|
|
7
|
-
[ProcName in keyof Router['procedures']]: ProcType<Router, ProcName> extends 'rpc' ? (input: Static<ProcInput<Router, ProcName>>) => Promise<Static<ProcOutput<Router, ProcName
|
|
14
|
+
[ProcName in keyof Router['procedures']]: ProcType<Router, ProcName> extends 'rpc' ? (input: Static<ProcInput<Router, ProcName>>) => Promise<Result<Static<ProcOutput<Router, ProcName>>, Static<ProcErrors<Router, ProcName>>>> : () => Promise<[
|
|
8
15
|
Pushable<Static<ProcInput<Router, ProcName>>>,
|
|
9
|
-
|
|
16
|
+
AsyncIter<Result<Static<ProcOutput<Router, ProcName>>, Static<ProcErrors<Router, ProcName>>>>,
|
|
10
17
|
() => void
|
|
11
18
|
]>;
|
|
12
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Defines a type that represents a client for a server with a set of services.
|
|
22
|
+
* @template Srv - The type of the server.
|
|
23
|
+
*/
|
|
13
24
|
export type ServerClient<Srv extends Server<Record<string, AnyService>>> = {
|
|
14
25
|
[SvcName in keyof Srv['services']]: ServiceClient<Srv['services'][SvcName]>;
|
|
15
26
|
};
|
|
27
|
+
/**
|
|
28
|
+
* Creates a client for a given server using the provided transport.
|
|
29
|
+
* Note that the client only needs the type of the server, not the actual
|
|
30
|
+
* server definition itself.
|
|
31
|
+
*
|
|
32
|
+
* This relies on a proxy to dynamically create the client, so the client
|
|
33
|
+
* will be typed as if it were the actual server with the appropriate services
|
|
34
|
+
* and procedures.
|
|
35
|
+
*
|
|
36
|
+
* @template Srv - The type of the server.
|
|
37
|
+
* @param {Transport} transport - The transport to use for communication.
|
|
38
|
+
* @returns The client for the server.
|
|
39
|
+
*/
|
|
16
40
|
export declare const createClient: <Srv extends Server<Record<string, AnyService>>>(transport: Transport) => ServerClient<Srv>;
|
|
17
41
|
export {};
|
|
42
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../router/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EACL,UAAU,EACV,UAAU,EACV,SAA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,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC;CAC5E,CAAC;AAgCF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,YAAY,8DACZ,SAAS,sBAgFO,CAAC"}
|
package/dist/router/client.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { pushable } from 'it-pushable';
|
|
2
|
-
import { msg } from '../transport/message';
|
|
2
|
+
import { msg, } from '../transport/message';
|
|
3
3
|
import { waitForMessage } from '../transport';
|
|
4
|
+
import { nanoid } from 'nanoid';
|
|
4
5
|
const noop = () => { };
|
|
5
6
|
function _createRecursiveProxy(callback, path) {
|
|
6
7
|
const proxy = new Proxy(noop, {
|
|
8
|
+
// property access, recurse and add field to path
|
|
7
9
|
get(_obj, key) {
|
|
8
10
|
if (typeof key !== 'string')
|
|
9
11
|
return undefined;
|
|
10
12
|
return _createRecursiveProxy(callback, [...path, key]);
|
|
11
13
|
},
|
|
14
|
+
// hit the end, let's invoke the handler
|
|
12
15
|
apply(_target, _this, args) {
|
|
13
16
|
return callback({
|
|
14
17
|
path,
|
|
@@ -18,23 +21,44 @@ function _createRecursiveProxy(callback, path) {
|
|
|
18
21
|
});
|
|
19
22
|
return proxy;
|
|
20
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Creates a client for a given server using the provided transport.
|
|
26
|
+
* Note that the client only needs the type of the server, not the actual
|
|
27
|
+
* server definition itself.
|
|
28
|
+
*
|
|
29
|
+
* This relies on a proxy to dynamically create the client, so the client
|
|
30
|
+
* will be typed as if it were the actual server with the appropriate services
|
|
31
|
+
* and procedures.
|
|
32
|
+
*
|
|
33
|
+
* @template Srv - The type of the server.
|
|
34
|
+
* @param {Transport} transport - The transport to use for communication.
|
|
35
|
+
* @returns The client for the server.
|
|
36
|
+
*/
|
|
21
37
|
export const createClient = (transport) => _createRecursiveProxy(async (opts) => {
|
|
22
38
|
const [serviceName, procName] = [...opts.path];
|
|
23
39
|
const [input] = opts.args;
|
|
40
|
+
const streamId = nanoid();
|
|
41
|
+
function belongsToSameStream(msg) {
|
|
42
|
+
return (msg.streamId === streamId &&
|
|
43
|
+
msg.serviceName === serviceName &&
|
|
44
|
+
msg.procedureName === procName);
|
|
45
|
+
}
|
|
24
46
|
if (input === undefined) {
|
|
25
|
-
// stream case
|
|
47
|
+
// stream case (stream methods are called with zero arguments)
|
|
26
48
|
const inputStream = pushable({ objectMode: true });
|
|
27
49
|
const outputStream = pushable({ objectMode: true });
|
|
28
50
|
// input -> transport
|
|
29
51
|
// this gets cleaned up on i.end() which is called by closeHandler
|
|
30
52
|
(async () => {
|
|
31
53
|
for await (const rawIn of inputStream) {
|
|
32
|
-
|
|
54
|
+
const m = msg(transport.clientId, 'SERVER', serviceName, procName, streamId, rawIn);
|
|
55
|
+
m.controlFlags |= 2 /* ControlFlags.StreamOpenBit */;
|
|
56
|
+
transport.send(m);
|
|
33
57
|
}
|
|
34
58
|
})();
|
|
35
59
|
// transport -> output
|
|
36
60
|
const listener = (msg) => {
|
|
37
|
-
if (msg
|
|
61
|
+
if (belongsToSameStream(msg)) {
|
|
38
62
|
outputStream.push(msg.payload);
|
|
39
63
|
}
|
|
40
64
|
};
|
|
@@ -42,13 +66,20 @@ export const createClient = (transport) => _createRecursiveProxy(async (opts) =>
|
|
|
42
66
|
const closeHandler = () => {
|
|
43
67
|
inputStream.end();
|
|
44
68
|
outputStream.end();
|
|
69
|
+
const closeMessage = msg(transport.clientId, 'SERVER', serviceName, procName, streamId, {});
|
|
70
|
+
closeMessage.controlFlags |= 4 /* ControlFlags.StreamClosedBit */;
|
|
71
|
+
transport.send(closeMessage);
|
|
45
72
|
transport.removeMessageListener(listener);
|
|
46
73
|
};
|
|
47
74
|
return [inputStream, outputStream, closeHandler];
|
|
48
75
|
}
|
|
49
76
|
else {
|
|
50
77
|
// rpc case
|
|
51
|
-
const
|
|
52
|
-
|
|
78
|
+
const m = msg(transport.clientId, 'SERVER', serviceName, procName, streamId, input);
|
|
79
|
+
// rpc is a stream open + close
|
|
80
|
+
m.controlFlags |=
|
|
81
|
+
2 /* ControlFlags.StreamOpenBit */ | 4 /* ControlFlags.StreamClosedBit */;
|
|
82
|
+
transport.send(m);
|
|
83
|
+
return waitForMessage(transport, belongsToSameStream);
|
|
53
84
|
}
|
|
54
85
|
}, []);
|
package/dist/router/context.d.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* You should use declaration merging to extend this interface
|
|
10
10
|
* with whatever you need. For example, if you need to access
|
|
11
11
|
* a database, you could do:
|
|
12
|
+
*
|
|
12
13
|
* ```ts
|
|
13
14
|
* declare module '@replit/river' {
|
|
14
15
|
* interface ServiceContext {
|
|
@@ -26,3 +27,4 @@ export interface ServiceContext {
|
|
|
26
27
|
export type ServiceContextWithState<State extends object | unknown> = ServiceContext & {
|
|
27
28
|
state: State;
|
|
28
29
|
};
|
|
30
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../router/context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,MAAM,uBAAuB,CAAC,KAAK,SAAS,MAAM,GAAG,OAAO,IAChE,cAAc,GAAG;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC"}
|
package/dist/router/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../router/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC7D,YAAY,EACV,aAAa,EACb,WAAW,EACX,OAAO,EACP,WAAW,EACX,SAAS,EACT,UAAU,EACV,QAAQ,EACR,SAAS,GACV,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,YAAY,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AACvC,YAAY,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { TLiteralString, TNever, TObject, TSchema, TString, TUnion } from '@sinclair/typebox';
|
|
2
|
+
export type RiverErrorSchema = TObject<{
|
|
3
|
+
code: TLiteralString;
|
|
4
|
+
message: TLiteralString | TString;
|
|
5
|
+
}> | TObject<{
|
|
6
|
+
code: TLiteralString;
|
|
7
|
+
message: TLiteralString | TString;
|
|
8
|
+
extras: TSchema;
|
|
9
|
+
}>;
|
|
10
|
+
export type RiverError = TUnion<RiverErrorSchema[]> | RiverErrorSchema | TNever;
|
|
11
|
+
export declare const UNCAUGHT_ERROR = "UNCAUGHT_ERROR";
|
|
12
|
+
export declare const RiverUncaughtSchema: TObject<{
|
|
13
|
+
code: import("@sinclair/typebox").TLiteral<"UNCAUGHT_ERROR">;
|
|
14
|
+
message: TString;
|
|
15
|
+
}>;
|
|
16
|
+
export type Result<T, E> = {
|
|
17
|
+
ok: true;
|
|
18
|
+
payload: T;
|
|
19
|
+
} | {
|
|
20
|
+
ok: false;
|
|
21
|
+
payload: E;
|
|
22
|
+
};
|
|
23
|
+
export declare function Ok<T, E>(payload: T): Result<T, E>;
|
|
24
|
+
export declare function Err<T, E>(error: E): Result<T, E>;
|
|
25
|
+
//# sourceMappingURL=result.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../router/result.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,MAAM,EACN,OAAO,EACP,OAAO,EACP,OAAO,EACP,MAAM,EAEP,MAAM,mBAAmB,CAAC;AAE3B,MAAM,MAAM,gBAAgB,GACxB,OAAO,CAAC;IACN,IAAI,EAAE,cAAc,CAAC;IACrB,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC;CACnC,CAAC,GACF,OAAO,CAAC;IACN,IAAI,EAAE,cAAc,CAAC;IACrB,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC;IAClC,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC,CAAC;AACP,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,gBAAgB,EAAE,CAAC,GAAG,gBAAgB,GAAG,MAAM,CAAC;AAEhF,eAAO,MAAM,cAAc,mBAAmB,CAAC;AAC/C,eAAO,MAAM,mBAAmB;;;EAG9B,CAAC;AAEH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IACnB;IACE,EAAE,EAAE,IAAI,CAAC;IACT,OAAO,EAAE,CAAC,CAAC;CACZ,GACD;IACE,EAAE,EAAE,KAAK,CAAC;IACV,OAAO,EAAE,CAAC,CAAC;CACZ,CAAC;AAEN,wBAAgB,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAKjD;AAED,wBAAgB,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAKhD"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Type, } from '@sinclair/typebox';
|
|
2
|
+
export const UNCAUGHT_ERROR = 'UNCAUGHT_ERROR';
|
|
3
|
+
export const RiverUncaughtSchema = Type.Object({
|
|
4
|
+
code: Type.Literal(UNCAUGHT_ERROR),
|
|
5
|
+
message: Type.String(),
|
|
6
|
+
});
|
|
7
|
+
export function Ok(payload) {
|
|
8
|
+
return {
|
|
9
|
+
ok: true,
|
|
10
|
+
payload,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export function Err(error) {
|
|
14
|
+
return {
|
|
15
|
+
ok: false,
|
|
16
|
+
payload: error,
|
|
17
|
+
};
|
|
18
|
+
}
|
package/dist/router/server.d.ts
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import { Transport } from '../transport/types';
|
|
2
2
|
import { AnyService } from './builder';
|
|
3
3
|
import { ServiceContext } from './context';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a server with a set of services. Use {@link createServer} to create it.
|
|
6
|
+
* @template Services - The type of services provided by the server.
|
|
7
|
+
*/
|
|
4
8
|
export interface Server<Services> {
|
|
5
9
|
services: Services;
|
|
6
10
|
close(): Promise<void>;
|
|
7
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Creates a server instance that listens for incoming messages from a transport and routes them to the appropriate service and procedure.
|
|
14
|
+
* The server tracks the state of each service along with open streams and the extended context object.
|
|
15
|
+
* @param transport - The transport to listen to.
|
|
16
|
+
* @param services - An object containing all the services to be registered on the server.
|
|
17
|
+
* @param extendedContext - An optional object containing additional context to be passed to all services.
|
|
18
|
+
* @returns A promise that resolves to a server instance with the registered services.
|
|
19
|
+
*/
|
|
8
20
|
export declare function createServer<Services extends Record<string, AnyService>>(transport: Transport, services: Services, extendedContext?: Omit<ServiceContext, 'state'>): Promise<Server<Services>>;
|
|
21
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../router/server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAgB,UAAU,EAAE,MAAM,WAAW,CAAC;AAUrD,OAAO,EAAE,cAAc,EAA2B,MAAM,WAAW,CAAC;AAWpE;;;GAGG;AACH,MAAM,WAAW,MAAM,CAAC,QAAQ;IAC9B,QAAQ,EAAE,QAAQ,CAAC;IACnB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAWD;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAAC,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,EAC5E,SAAS,EAAE,SAAS,EACpB,QAAQ,EAAE,QAAQ,EAClB,eAAe,CAAC,EAAE,IAAI,CAAC,cAAc,EAAE,OAAO,CAAC,GAC9C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAoJ3B"}
|