Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion lib/test/balancer/simple-balancer-with-websockets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pnpm test ./simple-balancer-with-websockets.test.ts
import * as http from "http";
import * as httpProxy from "../..";
import getPort from "../get-port";
import { once } from "../wait";
import { once } from "events";
import fetch from "node-fetch";

describe("A simple round-robin load balancer that supports websockets", () => {
Expand Down
8 changes: 3 additions & 5 deletions lib/test/http/error-handling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as httpProxy from "../..";
import * as http from "http";
import getPort from "../get-port";
import log from "../log";
import { callback } from "awaiting";
import fetch from "node-fetch";

const CUSTOM_ERROR = "There was an error proxying your request";
Expand Down Expand Up @@ -68,15 +67,14 @@ describe("Test proxying over HTTP with latency", () => {
Upgrade: "websocket",
},
};
const f = (cb: any) => {
const err = await new Promise<Error>((resolve) => {
const req = http.request(options);
req.end();
req.on("error", (err) => {
log(`request error -- ${err}`);
cb(undefined, err);
resolve(err);
});
};
const err = await callback(f);
});
expect(err.message).toContain("socket hang up");
expect(customWSErrorCalled).toBe(true);
});
Expand Down
15 changes: 6 additions & 9 deletions lib/test/http/server-sent-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as http from "http";
import getPort from "../get-port";
import { createSession } from "better-sse";
import { EventSource } from "eventsource";
import { callback } from "awaiting";
import fetch from "node-fetch";

describe("proxying server sent events over HTTP", () => {
Expand Down Expand Up @@ -60,26 +59,24 @@ describe("proxying server sent events over HTTP", () => {
// These two tests leave open handles on node v18, so we disable them ONLY
// with node v18.
it("test receiving an SSE WITHOUT using the proxy", async () => {
const f = (cb: any) => {
const resp = await new Promise(resolve => {
const sse = new EventSource(`http://localhost:${ports.http}/sse`);
sse.addEventListener("message", ({ data }) => {
sse.close();
cb(undefined, JSON.parse(data));
resolve(JSON.parse(data));
});
};
const resp = await callback(f);
})
expect(resp).toEqual("Hello world! - 1");
});

it("test receiving an SSE USING the proxy", async () => {
const f = (cb: any) => {
const resp = await new Promise(resolve => {
const sse = new EventSource(`http://localhost:${ports.proxy}/sse`);
sse.addEventListener("message", ({ data }) => {
sse.close();
cb(undefined, JSON.parse(data));
resolve(JSON.parse(data));
});
};
const resp = await callback(f);
});
expect(resp).toEqual("Hello world! - 2");
});
}
Expand Down
3 changes: 2 additions & 1 deletion lib/test/lib/http-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import * as net from "net";
import WebSocket, { WebSocketServer } from "ws";
import { Server } from "socket.io";
import { io as socketioClient } from "socket.io-client";
import wait, { once } from "../wait";
import wait from "../wait";
import { once } from "events";

const ports: { [port: string]: number } = {};
let portIndex = -1;
Expand Down
24 changes: 2 additions & 22 deletions lib/test/wait.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
import { callback, delay } from "awaiting";
import { setTimeout } from "node:timers/promises";

export default async function wait({ until }: { until: Function }) {
let d = 5;
while (!(await until())) {
await delay(d);
await setTimeout(d);
d = Math.min(1000, d * 1.2);
}
}

import type { EventEmitter } from "events";

export async function once(obj: EventEmitter, event: string): Promise<any> {
if (obj == null) {
throw Error("once -- obj is undefined");
}
if (typeof obj.once != "function") {
throw Error("once -- obj.once must be a function");
}
let val: any[] = [];
function wait(cb: Function): void {
obj.once(event, function (...args): void {
val = args;
cb();
});
}
await callback(wait);
return val;
}
2 changes: 1 addition & 1 deletion lib/test/websocket/latent-websocket-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pnpm test latent-websocket-proxy.test.ts

import * as httpProxy from "../..";
import getPort from "../get-port";
import { once } from "../wait";
import { once } from "events";
import http, { createServer } from "http";
import { Server } from "socket.io";
import { io as socketioClient } from "socket.io-client";
Expand Down
2 changes: 1 addition & 1 deletion lib/test/websocket/simple-websocket-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as http from "http";
import * as httpProxy from "../..";
import log from "../log";
import getPort from "../get-port";
import { once } from "../wait";
import { once } from "events";

describe("Example of simple proxying of a WebSocket", () => {
let ports: Record<'ws' | 'proxy', number>;
Expand Down
2 changes: 1 addition & 1 deletion lib/test/websocket/standalone-websocket-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pnpm test ./standalone-websocket-proxy.test.ts

import * as httpProxy from "../..";
import getPort from "../get-port";
import { once } from "../wait";
import { once } from "events";
import http, { createServer } from "http";
import { Server } from "socket.io";
import { io as socketioClient } from "socket.io-client";
Expand Down
3 changes: 2 additions & 1 deletion lib/test/websocket/websocket-load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ pnpm test websocket-load.test.ts
import * as http from "http";
import * as httpProxy from "../..";
import getPort from "../get-port";
import wait, { once } from "../wait";
import wait from "../wait";
import { once } from "events";

describe("Load testing proxying a WebSocket", () => {
let ports: Record<'ws' | 'proxy', number>;
Expand Down
2 changes: 1 addition & 1 deletion lib/test/websocket/websocket-proxy-websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as httpProxy from "../..";
import getPort from "../get-port";
import { Server } from "socket.io";
import { io as socketioClient } from "socket.io-client";
import { once } from "../wait";
import { once } from "events";

describe("Multilevel Proxying of a Websocket using Socket.io", () => {
let ports: Record<'socketio' | 'inner' | 'outer', number>;
Expand Down
3 changes: 2 additions & 1 deletion lib/test/websocket/websocket-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ that no sockets leak.
import * as httpProxy from "../..";
import log from "../log";
import getPort from "../get-port";
import wait, { once } from "../wait";
import wait from "../wait";
import { once } from "events";
import { createServer } from "http";
import { Server } from "socket.io";
import { io as socketioClient } from "socket.io-client";
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@types/ws": "^8.18.1",
"async": "^3.2.6",
"auto-changelog": "^2.5.0",
"awaiting": "^3.0.0",
"better-sse": "^0.14.1",
"body-parser": "^2.2.0",
"compression": "^1.8.0",
Expand Down
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.