@replit/river 0.1.7 → 0.1.9
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__/integration.test.d.ts +4 -4
- package/dist/__tests__/integration.test.js +4 -4
- package/dist/index.d.ts +1 -0
- package/dist/router/builder.d.ts +3 -2
- package/dist/router/client.d.ts +1 -1
- package/dist/router/context.d.ts +28 -0
- package/dist/router/context.js +1 -0
- package/dist/router/server.d.ts +2 -1
- package/dist/router/server.js +14 -5
- package/dist/router/server.util.d.ts +3 -2
- package/dist/router/server.util.js +4 -4
- package/package.json +1 -1
|
@@ -18,9 +18,9 @@ export declare const TestServiceConstructor: () => {
|
|
|
18
18
|
output: import("@sinclair/typebox").TObject<{
|
|
19
19
|
result: import("@sinclair/typebox").TNumber;
|
|
20
20
|
}>;
|
|
21
|
-
handler: (
|
|
21
|
+
handler: (context: import("..").ServiceContextWithState<{
|
|
22
22
|
count: number;
|
|
23
|
-
}
|
|
23
|
+
}>, input: import("../transport/message").TransportMessage<{
|
|
24
24
|
n: number;
|
|
25
25
|
}>) => Promise<import("../transport/message").TransportMessage<{
|
|
26
26
|
result: number;
|
|
@@ -36,9 +36,9 @@ export declare const TestServiceConstructor: () => {
|
|
|
36
36
|
output: import("@sinclair/typebox").TObject<{
|
|
37
37
|
response: import("@sinclair/typebox").TString;
|
|
38
38
|
}>;
|
|
39
|
-
handler: (
|
|
39
|
+
handler: (context: import("..").ServiceContextWithState<{
|
|
40
40
|
count: number;
|
|
41
|
-
}
|
|
41
|
+
}>, input: AsyncIterable<import("../transport/message").TransportMessage<{
|
|
42
42
|
msg: string;
|
|
43
43
|
ignore: boolean;
|
|
44
44
|
}>>, output: import("it-pushable").Pushable<import("../transport/message").TransportMessage<{
|
|
@@ -20,17 +20,17 @@ export const TestServiceConstructor = () => ServiceBuilder.create('test')
|
|
|
20
20
|
type: 'rpc',
|
|
21
21
|
input: Type.Object({ n: Type.Number() }),
|
|
22
22
|
output: Type.Object({ result: Type.Number() }),
|
|
23
|
-
async handler(
|
|
23
|
+
async handler(ctx, msg) {
|
|
24
24
|
const { n } = msg.payload;
|
|
25
|
-
state.count += n;
|
|
26
|
-
return reply(msg, { result: state.count });
|
|
25
|
+
ctx.state.count += n;
|
|
26
|
+
return reply(msg, { result: ctx.state.count });
|
|
27
27
|
},
|
|
28
28
|
})
|
|
29
29
|
.defineProcedure('echo', {
|
|
30
30
|
type: 'stream',
|
|
31
31
|
input: EchoRequest,
|
|
32
32
|
output: EchoResponse,
|
|
33
|
-
async handler(
|
|
33
|
+
async handler(_ctx, msgStream, returnStream) {
|
|
34
34
|
for await (const msg of msgStream) {
|
|
35
35
|
const req = msg.payload;
|
|
36
36
|
if (!req.ignore) {
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export type { ServerClient } from './router/client';
|
|
|
5
5
|
export { createServer } from './router/server';
|
|
6
6
|
export { asClientRpc, asClientStream } from './router/server.util';
|
|
7
7
|
export type { Server } from './router/server';
|
|
8
|
+
export type { ServiceContext, ServiceContextWithState } from './router/context';
|
|
8
9
|
export { Transport } from './transport/types';
|
|
9
10
|
export { TransportMessageSchema, OpaqueTransportMessageSchema, TransportAckSchema, msg, payloadToTransportMessage, ack, reply, } from './transport/message';
|
|
10
11
|
export type { TransportMessage, MessageId, OpaqueTransportMessage, TransportClientId, TransportMessageAck, } from './transport/message';
|
package/dist/router/builder.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TObject, Static } from '@sinclair/typebox';
|
|
2
2
|
import type { Pushable } from 'it-pushable';
|
|
3
3
|
import { TransportMessage } from '../transport/message';
|
|
4
|
+
import { ServiceContextWithState } from './context';
|
|
4
5
|
export type ValidProcType = 'stream' | 'rpc';
|
|
5
6
|
export type ProcListing = Record<string, Procedure<object, ValidProcType, TObject, TObject>>;
|
|
6
7
|
export interface Service<Name extends string, State extends object, Procs extends ProcListing> {
|
|
@@ -17,12 +18,12 @@ export type ProcType<S extends AnyService, ProcName extends keyof S['procedures'
|
|
|
17
18
|
export type Procedure<State extends object | unknown, Ty extends ValidProcType, I extends TObject, O extends TObject> = Ty extends 'rpc' ? {
|
|
18
19
|
input: I;
|
|
19
20
|
output: O;
|
|
20
|
-
handler: (
|
|
21
|
+
handler: (context: ServiceContextWithState<State>, input: TransportMessage<Static<I>>) => Promise<TransportMessage<Static<O>>>;
|
|
21
22
|
type: Ty;
|
|
22
23
|
} : {
|
|
23
24
|
input: I;
|
|
24
25
|
output: O;
|
|
25
|
-
handler: (
|
|
26
|
+
handler: (context: ServiceContextWithState<State>, input: AsyncIterable<TransportMessage<Static<I>>>, output: Pushable<TransportMessage<Static<O>>>) => Promise<void>;
|
|
26
27
|
type: Ty;
|
|
27
28
|
};
|
|
28
29
|
export declare class ServiceBuilder<T extends Service<string, object, ProcListing>> {
|
package/dist/router/client.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { Static } from '@sinclair/typebox';
|
|
|
6
6
|
type ServiceClient<Router extends AnyService> = {
|
|
7
7
|
[ProcName in keyof Router['procedures']]: ProcType<Router, ProcName> extends 'rpc' ? (input: Static<ProcInput<Router, ProcName>>) => Promise<Static<ProcOutput<Router, ProcName>>> : () => Promise<[
|
|
8
8
|
Pushable<Static<ProcInput<Router, ProcName>>>,
|
|
9
|
-
|
|
9
|
+
AsyncIterableIterator<Static<ProcOutput<Router, ProcName>>>,
|
|
10
10
|
() => void
|
|
11
11
|
]>;
|
|
12
12
|
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The context for services/procedures. This is used only on
|
|
3
|
+
* the server.
|
|
4
|
+
*
|
|
5
|
+
* An important detail is that the state prop is always on
|
|
6
|
+
* this interface and it shouldn't be changed, removed, or
|
|
7
|
+
* extended. This prop is for the state of a service.
|
|
8
|
+
*
|
|
9
|
+
* You should use declaration merging to extend this interface
|
|
10
|
+
* with whatever you need. For example, if you need to access
|
|
11
|
+
* a database, you could do:
|
|
12
|
+
* ```ts
|
|
13
|
+
* declare module '@replit/river' {
|
|
14
|
+
* interface ServiceContext {
|
|
15
|
+
* db: Database;
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export interface ServiceContext {
|
|
21
|
+
state: object | unknown;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The {@link ServiceContext} with state. This is what is passed to procedures.
|
|
25
|
+
*/
|
|
26
|
+
export type ServiceContextWithState<State extends object | unknown> = ServiceContext & {
|
|
27
|
+
state: State;
|
|
28
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/router/server.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Transport } from '../transport/types';
|
|
2
2
|
import { AnyService } from './builder';
|
|
3
|
+
import { ServiceContext } from './context';
|
|
3
4
|
export interface Server<Services> {
|
|
4
5
|
services: Services;
|
|
5
6
|
close(): Promise<void>;
|
|
6
7
|
}
|
|
7
|
-
export declare function createServer<Services extends Record<string, AnyService>>(transport: Transport, services: Services): Promise<Server<Services>>;
|
|
8
|
+
export declare function createServer<Services extends Record<string, AnyService>>(transport: Transport, services: Services, extendedContext?: Omit<ServiceContext, 'state'>): Promise<Server<Services>>;
|
package/dist/router/server.js
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
import { Value } from '@sinclair/typebox/value';
|
|
2
2
|
import { pushable } from 'it-pushable';
|
|
3
|
-
export async function createServer(transport, services) {
|
|
4
|
-
|
|
3
|
+
export async function createServer(transport, services, extendedContext) {
|
|
4
|
+
const contextMap = new Map();
|
|
5
5
|
const streamMap = new Map();
|
|
6
|
+
function getContext(service) {
|
|
7
|
+
const context = contextMap.get(service);
|
|
8
|
+
if (!context) {
|
|
9
|
+
throw new Error(`No context found for ${service.name}`);
|
|
10
|
+
}
|
|
11
|
+
return context;
|
|
12
|
+
}
|
|
6
13
|
for (const [serviceName, service] of Object.entries(services)) {
|
|
14
|
+
// populate the context map
|
|
15
|
+
contextMap.set(service, { ...extendedContext, state: service.state });
|
|
16
|
+
// create streams for every stream procedure
|
|
7
17
|
for (const [procedureName, proc] of Object.entries(service.procedures)) {
|
|
8
18
|
const procedure = proc;
|
|
9
19
|
if (procedure.type === 'stream') {
|
|
@@ -14,7 +24,7 @@ export async function createServer(transport, services) {
|
|
|
14
24
|
outgoing,
|
|
15
25
|
doneCtx: Promise.all([
|
|
16
26
|
// processing the actual procedure
|
|
17
|
-
procedure.handler(service
|
|
27
|
+
procedure.handler(getContext(service), incoming, outgoing),
|
|
18
28
|
// sending outgoing messages back to client
|
|
19
29
|
(async () => {
|
|
20
30
|
for await (const response of outgoing) {
|
|
@@ -39,8 +49,7 @@ export async function createServer(transport, services) {
|
|
|
39
49
|
const inputMessage = msg;
|
|
40
50
|
if (procedure.type === 'rpc' &&
|
|
41
51
|
Value.Check(procedure.input, inputMessage.payload)) {
|
|
42
|
-
|
|
43
|
-
const response = await procedure.handler(service.state, inputMessage);
|
|
52
|
+
const response = await procedure.handler(getContext(service), inputMessage);
|
|
44
53
|
transport.send(response);
|
|
45
54
|
return;
|
|
46
55
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Static, TObject } from '@sinclair/typebox';
|
|
2
2
|
import { Procedure } from './builder';
|
|
3
3
|
import type { Pushable } from 'it-pushable';
|
|
4
|
-
|
|
5
|
-
export declare function
|
|
4
|
+
import { ServiceContext } from './context';
|
|
5
|
+
export declare function asClientRpc<State extends object | unknown, I extends TObject, O extends TObject>(state: State, proc: Procedure<State, 'rpc', I, O>, extendedContext?: Omit<ServiceContext, 'state'>): (msg: Static<I>) => Promise<Static<O>>;
|
|
6
|
+
export declare function asClientStream<State extends object | unknown, I extends TObject, O extends TObject>(state: State, proc: Procedure<State, 'stream', I, O>, extendedContext?: Omit<ServiceContext, 'state'>): [Pushable<Static<I>>, Pushable<Static<O>>];
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { payloadToTransportMessage, } from '../transport/message';
|
|
2
2
|
import { pushable } from 'it-pushable';
|
|
3
|
-
export function asClientRpc(state, proc) {
|
|
3
|
+
export function asClientRpc(state, proc, extendedContext) {
|
|
4
4
|
return (msg) => proc
|
|
5
|
-
.handler(state, payloadToTransportMessage(msg))
|
|
5
|
+
.handler({ ...extendedContext, state }, payloadToTransportMessage(msg))
|
|
6
6
|
.then((res) => res.payload);
|
|
7
7
|
}
|
|
8
|
-
export function asClientStream(state, proc) {
|
|
8
|
+
export function asClientStream(state, proc, extendedContext) {
|
|
9
9
|
const i = pushable({ objectMode: true });
|
|
10
10
|
const o = pushable({ objectMode: true });
|
|
11
11
|
const ri = pushable({ objectMode: true });
|
|
@@ -25,7 +25,7 @@ export function asClientStream(state, proc) {
|
|
|
25
25
|
})();
|
|
26
26
|
// handle
|
|
27
27
|
(async () => {
|
|
28
|
-
await proc.handler(state, ri, ro);
|
|
28
|
+
await proc.handler({ ...extendedContext, state }, ri, ro);
|
|
29
29
|
ro.end();
|
|
30
30
|
})();
|
|
31
31
|
return [i, o];
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@replit/river",
|
|
3
3
|
"description": "It's like tRPC but... with JSON Schema Support, duplex streaming and support for service multiplexing. Transport agnostic!",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.9",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|