Skip to content

Commit

Permalink
Merge pull request #670 from open-rpc/feat/custom-transport
Browse files Browse the repository at this point in the history
feat: this adds support for adding custom transports
  • Loading branch information
zcstarr authored Jun 9, 2021
2 parents 71b6b46 + b3fe773 commit d21995b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Server, { ServerOptions } from "./server";
import { Router } from "./router";
import { JSONRPCError } from "./error";
export * as transports from "./transports"

export {
Server,
Expand Down
21 changes: 12 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router, MethodMapping } from "./router";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
import Transports, { TransportOptions, TransportClasses, TransportNames } from "./transports";
import Transports, {ServerTransport, TransportOptions, TransportClasses, TransportNames } from "./transports";

interface TransportConfig {
type: TransportNames;
Expand Down Expand Up @@ -31,12 +31,20 @@ export default class Server {

if (options.transportConfigs) {
options.transportConfigs.forEach((transportConfig) => {
this.addTransport(transportConfig.type, transportConfig.options);
this.addDefaultTransport(transportConfig.type, transportConfig.options);
});
}
}

public addTransport(transportType: TransportNames, transportOptions: TransportOptions) {
public addTransport(transport: ServerTransport): void{
this.routers.forEach((router) => {
transport.addRouter(router);
});

this.transports.push(transport);
}

public addDefaultTransport(transportType: TransportNames, transportOptions: TransportOptions) {
const TransportClass = Transports[transportType];

console.log(`Adding Transport of the type ${transportType} on port ${transportOptions.port}`);
Expand All @@ -46,12 +54,7 @@ export default class Server {
}

const transport = new TransportClass(transportOptions);

this.routers.forEach((router) => {
transport.addRouter(router);
});

this.transports.push(transport);
this.addTransport(transport);
}

public addRouter(openrpcDocument: OpenRPC, methodMapping: MethodMapping | MockModeOptions) {
Expand Down
4 changes: 3 additions & 1 deletion src/transports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import WebSocketTransport, { WebSocketServerTransportOptions } from "./websocket
import HTTPTransport, { HTTPServerTransportOptions } from "./http";
import HTTPSTransport, { HTTPSServerTransportOptions } from "./https";
import IPCTransport, { IPCServerTransportOptions } from "./ipc";
import { ServerTransport } from "./server-transport";
export {HTTPSTransport, HTTPTransport, WebSocketTransport, IPCTransport, ServerTransport}

export type TransportNames = "IPCTransport" | "HTTPTransport" | "HTTPSTransport" | "WebSocketTransport";

export type TransportClasses = WebSocketTransport |
HTTPTransport |
HTTPSTransport |
IPCTransport;
IPCTransport | ServerTransport;

export type TransportOptions = WebSocketServerTransportOptions |
HTTPServerTransportOptions |
Expand Down
13 changes: 13 additions & 0 deletions src/transports/server-transport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import ServerTransport from "./server-transport";

describe("Server transport test", () => {

class DummyTransport extends ServerTransport {

}

it("transport implementation throws without start", async () => {
expect(()=>new DummyTransport().start()).toThrowError()
});

});
8 changes: 7 additions & 1 deletion src/transports/server-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface JSONRPCResponse {
error?: JSONRPCErrorObject;
}

export default abstract class ServerTransport {
export abstract class ServerTransport {
public routers: Router[] = [];

public addRouter(router: Router): void {
Expand All @@ -31,6 +31,11 @@ export default abstract class ServerTransport {
this.routers = this.routers.filter((r) => r !== router);
}

public start(): void {
console.warn("Transport must implement start()"); // tslint:disable-line
throw new Error("Transport missing start implementation");
}

protected async routerHandler({ id, method, params }: JSONRPCRequest): Promise<JSONRPCResponse> {
if (this.routers.length === 0) {
console.warn("transport method called without a router configured."); // tslint:disable-line
Expand Down Expand Up @@ -60,3 +65,4 @@ export default abstract class ServerTransport {
return res;
}
}
export default ServerTransport;

0 comments on commit d21995b

Please sign in to comment.