Skip to content

Commit

Permalink
fix: got tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed May 9, 2024
1 parent 28226e1 commit e857199
Show file tree
Hide file tree
Showing 12 changed files with 3,598 additions and 7,486 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ aliases:
defaults: &defaults
working_directory: ~/server-js
docker:
- image: cimg/node:18.13.0
- image: cimg/node:20.12.1
jobs:
test:
<<: *defaults
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Use Node.js 14.15.1
uses: actions/setup-node@v1
with:
node-version: 14.15.1
- uses: actions/setup-node@v4
- name: npm install
run: npm install
- name: lint
Expand Down
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18.13.0
20.12.1
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ module.exports = {
resetMocks: true,
restoreMocks: true,
rootDir: './src',
testEnvironment: 'node',
preset: 'ts-jest',
}
10,838 changes: 3,443 additions & 7,395 deletions package-lock.json

Large diffs are not rendered by default.

43 changes: 21 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,34 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@open-rpc/schema-utils-js": "1.15.0",
"@open-rpc/schema-utils-js": "^2.0.2",
"body-parser": "^1.19.0",
"connect": "^3.7.0",
"cors": "^2.8.5",
"json-schema-faker": "^0.5.0-rcv.26",
"node-ipc": "9.1.1",
"lodash": "^4.17.19",
"node-ipc": "9.1.1",
"ws": "^8.0.0"
},
"devDependencies": {
"@open-rpc/examples": "1.6.1",
"@open-rpc/meta-schema": "1.14.2",
"@types/body-parser": "^1.19.0",
"@types/connect": "^3.4.33",
"@types/cors": "^2.8.6",
"@types/fs-extra": "^9.0.1",
"@types/jest": "^26.0.7",
"@types/json-schema": "^7.0.5",
"@types/lodash": "^4.14.158",
"@types/node": "^16.0.0",
"@types/node-fetch": "^2.5.7",
"@types/node-ipc": "9.1.3",
"@types/ws": "^7.2.6",
"@typescript-eslint/eslint-plugin": "^4.9.1",
"@typescript-eslint/parser": "^4.9.1",
"eslint": "^7.15.0",
"jest": "^26.1.0",
"node-fetch": "^2.6.0",
"ts-jest": "^26.1.4",
"typescript": "^4.0.2"
"@open-rpc/examples": "^1.7.2",
"@open-rpc/meta-schema": "^1.14.9",
"@types/body-parser": "^1.19.5",
"@types/connect": "^3.4.38",
"@types/cors": "^2.8.17",
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.12",
"@types/json-schema": "^7.0.15",
"@types/lodash": "^4.17.1",
"@types/node": "^20.12.11",
"@types/node-ipc": "^9.2.3",
"@types/ws": "^8.5.10",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.17.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^4.9.5",
"undici": "^6.16.0"
}
}
5 changes: 2 additions & 3 deletions src/transports/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import examples from "@open-rpc/examples";
import { parseOpenRPCDocument } from "@open-rpc/schema-utils-js";
import { Router } from "../router";
import HTTPTransport from "./http";
import fetch from "node-fetch";
import { JSONRPCResponse } from "./server-transport";

describe("http transport", () => {
Expand Down Expand Up @@ -35,7 +34,7 @@ describe("http transport", () => {
}),
headers: { "Content-Type": "application/json" },
method: "post",
}).then((res) => res.json());
}).then((res) => res.json() as Promise<JSONRPCResponse>);

expect(result).toBe(4);
});
Expand All @@ -56,7 +55,7 @@ describe("http transport", () => {
]),
headers: { "Content-Type": "application/json" },
method: "post",
}).then((res) => res.json());
}).then((res) => res.json() as Promise<JSONRPCResponse[]>);

const pluckedResult = result.map((r: JSONRPCResponse) => r.result);
expect(pluckedResult).toEqual([4, 8]);
Expand Down
17 changes: 10 additions & 7 deletions src/transports/https.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import examples from "@open-rpc/examples";
import { parseOpenRPCDocument } from "@open-rpc/schema-utils-js";
import { Router } from "../router";
import fetch from "node-fetch";
import * as fs from "fs";
import { promisify } from "util";
import HTTPSTransport from "./https";
Expand All @@ -11,9 +10,13 @@ import cors from "cors";
import { json as jsonParser } from "body-parser";
import { HandleFunction } from "connect";
import { JSONRPCResponse } from "./server-transport";
import { Agent } from 'undici';

const agent = new https.Agent({ rejectUnauthorized: false });

const agent = new Agent({
connect: {
rejectUnauthorized: false,
},
}) as any;
describe("https transport", () => {
let transport: HTTPSTransport;
beforeAll(async () => {
Expand Down Expand Up @@ -44,7 +47,7 @@ describe("https transport", () => {

it("can start an https server that works", async () => {
const { result } = await fetch("https://localhost:9697", {
agent,
dispatcher: agent,
body: JSON.stringify({
id: "0",
jsonrpc: "2.0",
Expand All @@ -53,14 +56,14 @@ describe("https transport", () => {
}),
headers: { "Content-Type": "application/json" },
method: "post",
}).then((res) => res.json());
}).then((res) => res.json() as Promise<JSONRPCResponse>);

expect(result).toBe(4);
});

it("works with batching", async () => {
const result = await fetch("https://localhost:9697", {
agent,
dispatcher: agent,
body: JSON.stringify([
{
id: "0",
Expand All @@ -76,7 +79,7 @@ describe("https transport", () => {
]),
headers: { "Content-Type": "application/json" },
method: "post",
}).then((res) => res.json());
}).then((res) => res.json() as Promise<JSONRPCResponse[]>);

const pluckedResult = result.map((r: JSONRPCResponse) => r.result);
expect(pluckedResult).toEqual([4, 8]);
Expand Down
34 changes: 24 additions & 10 deletions src/transports/websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { JSONRPCResponse } from "./server-transport";

describe("WebSocket transport", () => {

it("can start an https server that works", async (done) => {
it("can start an https server that works", async () => {
expect.assertions(1);
const simpleMathExample = await parseOpenRPCDocument(examples.simpleMath);

const transport = new WebSocketTransport({
Expand All @@ -25,12 +26,13 @@ describe("WebSocket transport", () => {
transport.start();

const ws = new WebSocket("wss://localhost:9698", { rejectUnauthorized: false });
let done: any;
const handleMessage = (data: string) => {
ws.off("message", handleMessage);
transport.stop();
const { result } = JSON.parse(data);
expect(result).toBe(4);
done();
setTimeout(done, 3500); // give ws 3.5 seconds to shutdown
};
const handleConnnect = () => {
ws.off("open", handleConnnect);
Expand All @@ -42,10 +44,14 @@ describe("WebSocket transport", () => {
params: [2, 2],
}));
};
ws.on("open", handleConnnect);
const prom = new Promise((resolve) => {
done = resolve;
ws.on("open", handleConnnect);
});
await prom;
});

it("can start an https server that works", async (done) => {
it("can start an https server that works", async () => {
const simpleMathExample = await parseOpenRPCDocument(examples.simpleMath);

const transport = new WebSocketTransport({
Expand All @@ -58,12 +64,13 @@ describe("WebSocket transport", () => {
transport.start();

const ws = new WebSocket("ws://localhost:9698", { rejectUnauthorized: false });
let done: any;
const handleMessage = (data: string) => {
ws.off("message", handleMessage);
transport.stop();
const { result } = JSON.parse(data);
expect(result).toBe(4);
done();
setTimeout(done, 3500); // give ws 3.5 seconds to shutdown
};
const handleConnnect = () => {
ws.off("open", handleConnnect);
Expand All @@ -75,10 +82,13 @@ describe("WebSocket transport", () => {
params: [2, 2],
}));
};
ws.on("open", handleConnnect);
await new Promise((resolve) => {
done = resolve;
ws.on("open", handleConnnect);
});
});

it("works with batching", async (done) => {
it("works with batching", async () => {
const simpleMathExample = await parseOpenRPCDocument(examples.simpleMath);

const transport = new WebSocketTransport({
Expand All @@ -93,14 +103,14 @@ describe("WebSocket transport", () => {
transport.start();

const ws = new WebSocket("wss://localhost:9698", { rejectUnauthorized: false });

let done: any;
const handleMessage = (data: string) => {
ws.off("message", handleMessage);
transport.stop();
const result = JSON.parse(data) as JSONRPCResponse[];
expect(result.map((r) => r.result)).toEqual([4, 8]);
transport.removeRouter(router);
done();
setTimeout(done, 3500); // give ws 3.5 seconds to shutdown
};

const handleConnnect = () => {
Expand All @@ -121,6 +131,10 @@ describe("WebSocket transport", () => {
},
]));
};
ws.on("open", handleConnnect);

await new Promise((resolve) => {
done = resolve;
ws.on("open", handleConnnect);
});
});
});
13 changes: 13 additions & 0 deletions src/transports/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ export default class WebSocketServerTransport extends ServerTransport {
}

public stop() {
// First sweep, soft close
this.wss.clients.forEach((socket) => {
socket.close();
});
setTimeout(() => {
// Second sweep, hard close
// for everyone who's left
this.wss.clients.forEach((socket) => {
if ([socket.OPEN, socket.CLOSING].includes((socket as any).readyState)) {
socket.terminate();

Check warning on line 71 in src/transports/websocket.ts

View check run for this annotation

Codecov / codecov/patch

src/transports/websocket.ts#L71

Added line #L71 was not covered by tests
}
});
}, 3000);
this.wss.removeAllListeners();
this.wss.close();
this.server.close();
Expand Down
48 changes: 32 additions & 16 deletions test-cert/server.cert
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
-----BEGIN CERTIFICATE-----
MIIC6jCCAdICCQCzgQuiq2QhKDANBgkqhkiG9w0BAQsFADA3MQswCQYDVQQGEwJj
YTESMBAGA1UEAwwJbG9jYWxob3N0MRQwEgYJKoZIhvcNAQkBFgViQGIuYzAeFw0x
OTA0MDYwNjQwNTdaFw0xOTA1MDYwNjQwNTdaMDcxCzAJBgNVBAYTAmNhMRIwEAYD
VQQDDAlsb2NhbGhvc3QxFDASBgkqhkiG9w0BCQEWBWJAYi5jMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxAzCG/q3tOu97Mvv1ZtNS3jxS8qW6pxNuLC+
9+dm7E/8Q36fpWs9dQgv4Q5Fgg8QcKwqkVIAuKsz8Ls2J0xpZBIVkRverPoamyi2
/xyTj8SpUYxnyr9eNdDgQvvN2W9K58cBDhQl/Tv0CbkXf1T9Tcunoov7O0Sp09n3
/9HSUK9s48lA7F3UsETHKpcwdrsRMWMKbW/2EKWx3PP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