Skip to content

Commit

Permalink
allow injecting a server impl into wsJsonServer
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecsl committed Dec 10, 2023
1 parent 8db8e03 commit 65be09d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tda-wsjson-client",
"version": "0.8.2",
"version": "0.9.0",
"description": "WebSocket client for the TD Ameritrade wsjson API",
"main": "dist/web.bundle.js",
"types": "dist/web.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/example/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import WsJsonServer from "../server/wsJsonServer";
import RealWsJsonClient from "../client/realWsJsonClient";
import { createServer } from "http";

const proxy = new WsJsonServer(() => new RealWsJsonClient());
const proxy = new WsJsonServer(() => new RealWsJsonClient(), createServer());
proxy.start();
6 changes: 1 addition & 5 deletions src/example/testApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,7 @@ async function run() {
}
const token = { accessToken, refreshToken, expiresAt: +expiresAt };
const authClient = new WsJsonClientAuth(
() =>
new WsJsonClientProxy("wss://localhost:8080", {
rejectUnauthorized: false,
}),
// new RealWsJsonClient(),
() => new WsJsonClientProxy("ws://localhost:8080"), // or new RealWsJsonClient(),
clientId,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
30 changes: 19 additions & 11 deletions src/server/wsJsonServer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ws from "ws";
import { createServer, Server } from "https";
import { IncomingMessage, ServerResponse } from "http";
import { createServer, Server as HttpsServer } from "https";
import { IncomingMessage, Server as HttpServer, ServerResponse } from "http";
import { readFileSync } from "fs";
import { WsJsonClient } from "../client/wsJsonClient";
import debug from "debug";
Expand All @@ -9,28 +9,36 @@ import { isEmpty } from "lodash";
import { Disposable } from "./disposable";

const logger = debug("wsJsonServer");
const DEFAULT_HTTPS_SERVER = createServer({
cert: readFileSync("./cert.pem"),
key: readFileSync("./key.pem"),
});
const DEFAULT_PORT = 8080;
type DefaultHttpsServer = HttpsServer<
typeof IncomingMessage,
typeof ServerResponse
>;
type DefaultHttpServer = HttpServer<
typeof IncomingMessage,
typeof ServerResponse
>;

/**
* A WebSocket server that proxies requests to a WsJsonClient client. Incoming messages must be in JSON format and have
* a "request" property that matches a method on the WsJsonClient interface and an "args" property that is an array of
* arguments to pass to the method. The response is then forwarded back to the client as a JSON string.
*/
export default class WsJsonServer implements Disposable {
private readonly server: Server<
typeof IncomingMessage,
typeof ServerResponse
>;
private readonly wss: ws.Server<typeof ws, typeof IncomingMessage>;
private proxies: WsJsonServerProxy[] = [];

constructor(
private readonly wsJsonClientFactory: () => WsJsonClient,
private readonly port = 8080
private readonly server:
| DefaultHttpsServer
| DefaultHttpServer = DEFAULT_HTTPS_SERVER,
private readonly port = DEFAULT_PORT
) {
this.server = createServer({
cert: readFileSync("./cert.pem"),
key: readFileSync("./key.pem"),
});
this.wss = new ws.WebSocketServer({ server: this.server });
}

Expand Down

0 comments on commit 65be09d

Please sign in to comment.