Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add injected isomorphic environment #4

Merged
merged 9 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions environment/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IsomorphicEnvironment {
log: (...args: unknown[]) => void;
error: (...args: unknown[]) => void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment here about declaration merging and then add a test that adds something to the environment?

}
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ export {
waitForSocketReady,
createWebSocketClient,
} from './transport/util';

export type { IsomorphicEnvironment } from './environment/types';
10 changes: 8 additions & 2 deletions router/builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TObject, Static, Type } from '@sinclair/typebox';
import type { Pushable } from 'it-pushable';
import { TransportMessage } from '../transport/message';
import { IsomorphicEnvironment } from '../environment/types';

export type ValidProcType = 'stream' | 'rpc';
export type ProcListing = Record<
Expand Down Expand Up @@ -58,6 +59,11 @@ export type ProcType<
ProcName extends keyof S['procedures'],
> = S['procedures'][ProcName]['type'];

export interface ProcedureContext<State extends object | unknown> {
environment: IsomorphicEnvironment;
state: State;
}

export type Procedure<
State extends object | unknown,
Ty extends ValidProcType,
Expand All @@ -68,7 +74,7 @@ export type Procedure<
input: I;
output: O;
handler: (
state: State,
context: ProcedureContext<State>,
input: TransportMessage<Static<I>>,
) => Promise<TransportMessage<Static<O>>>;
type: Ty;
Expand All @@ -77,7 +83,7 @@ export type Procedure<
input: I;
output: O;
handler: (
state: State,
context: ProcedureContext<State>,
input: AsyncIterable<TransportMessage<Static<I>>>,
output: Pushable<TransportMessage<Static<O>>>,
) => Promise<void>;
Expand Down
22 changes: 19 additions & 3 deletions router/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { TObject } from '@sinclair/typebox';
import { Transport } from '../transport/types';
import { AnyService, Procedure, ValidProcType } from './builder';
import {
AnyService,
Procedure,
ProcedureContext,
ValidProcType,
} from './builder';
import { Value } from '@sinclair/typebox/value';
import { pushable } from 'it-pushable';
import type { Pushable } from 'it-pushable';
import { OpaqueTransportMessage, TransportMessage } from '../transport/message';
import { IsomorphicEnvironment } from '../environment/types';

export interface Server<Services> {
services: Services;
Expand All @@ -18,12 +24,18 @@ interface ProcStream {
}

export async function createServer<Services extends Record<string, AnyService>>(
environment: IsomorphicEnvironment,
transport: Transport,
services: Services,
): Promise<Server<Services>> {
// create streams for every stream procedure
const streamMap: Map<string, ProcStream> = new Map();
for (const [serviceName, service] of Object.entries(services)) {
const context: ProcedureContext<object> = {
environment,
state: service.state,
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this a ServiceContext? would also be good to have a mapping of service -> ServiceContext so that we avoid constructing it on each request

for (const [procedureName, proc] of Object.entries(service.procedures)) {
const procedure = proc as Procedure<
object,
Expand All @@ -39,7 +51,7 @@ export async function createServer<Services extends Record<string, AnyService>>(
outgoing,
doneCtx: Promise.all([
// processing the actual procedure
procedure.handler(service.state, incoming, outgoing),
procedure.handler(context, incoming, outgoing),
// sending outgoing messages back to client
(async () => {
for await (const response of outgoing) {
Expand Down Expand Up @@ -78,7 +90,11 @@ export async function createServer<Services extends Record<string, AnyService>>(
Value.Check(procedure.input, inputMessage.payload)
) {
// synchronous rpc
const response = await procedure.handler(service.state, inputMessage);
const context: ProcedureContext<object> = {
environment,
state: service.state,
};
const response = await procedure.handler(context, inputMessage);
transport.send(response);
return;
} else if (
Expand Down
Loading