From 9c4451a0776a22d52b6c1a5b97e5896e24ab172e Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Sun, 13 Jul 2025 01:36:11 +0900 Subject: [PATCH 1/2] use `once` from `events` --- .../simple-balancer-with-websockets.test.ts | 2 +- lib/test/lib/http-proxy.test.ts | 3 ++- lib/test/wait.ts | 22 +------------------ .../websocket/latent-websocket-proxy.test.ts | 2 +- .../websocket/simple-websocket-proxy.test.ts | 2 +- .../standalone-websocket-proxy.test.ts | 2 +- lib/test/websocket/websocket-load.test.ts | 3 ++- .../websocket-proxy-websocket.test.ts | 2 +- lib/test/websocket/websocket-proxy.test.ts | 3 ++- 9 files changed, 12 insertions(+), 29 deletions(-) diff --git a/lib/test/balancer/simple-balancer-with-websockets.test.ts b/lib/test/balancer/simple-balancer-with-websockets.test.ts index 4dc02ec..d8d7bbc 100644 --- a/lib/test/balancer/simple-balancer-with-websockets.test.ts +++ b/lib/test/balancer/simple-balancer-with-websockets.test.ts @@ -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", () => { diff --git a/lib/test/lib/http-proxy.test.ts b/lib/test/lib/http-proxy.test.ts index 9afbb4e..4b73a35 100644 --- a/lib/test/lib/http-proxy.test.ts +++ b/lib/test/lib/http-proxy.test.ts @@ -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; diff --git a/lib/test/wait.ts b/lib/test/wait.ts index dd27886..93a63e6 100644 --- a/lib/test/wait.ts +++ b/lib/test/wait.ts @@ -1,4 +1,4 @@ -import { callback, delay } from "awaiting"; +import { delay } from "awaiting"; export default async function wait({ until }: { until: Function }) { let d = 5; @@ -7,23 +7,3 @@ export default async function wait({ until }: { until: Function }) { d = Math.min(1000, d * 1.2); } } - -import type { EventEmitter } from "events"; - -export async function once(obj: EventEmitter, event: string): Promise { - 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; -} diff --git a/lib/test/websocket/latent-websocket-proxy.test.ts b/lib/test/websocket/latent-websocket-proxy.test.ts index 3f21cbe..2b9eb6f 100644 --- a/lib/test/websocket/latent-websocket-proxy.test.ts +++ b/lib/test/websocket/latent-websocket-proxy.test.ts @@ -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"; diff --git a/lib/test/websocket/simple-websocket-proxy.test.ts b/lib/test/websocket/simple-websocket-proxy.test.ts index 446bc2e..810302d 100644 --- a/lib/test/websocket/simple-websocket-proxy.test.ts +++ b/lib/test/websocket/simple-websocket-proxy.test.ts @@ -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>; diff --git a/lib/test/websocket/standalone-websocket-proxy.test.ts b/lib/test/websocket/standalone-websocket-proxy.test.ts index 61ce04c..6ac13ce 100644 --- a/lib/test/websocket/standalone-websocket-proxy.test.ts +++ b/lib/test/websocket/standalone-websocket-proxy.test.ts @@ -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"; diff --git a/lib/test/websocket/websocket-load.test.ts b/lib/test/websocket/websocket-load.test.ts index 145a4ed..ce900d2 100644 --- a/lib/test/websocket/websocket-load.test.ts +++ b/lib/test/websocket/websocket-load.test.ts @@ -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>; diff --git a/lib/test/websocket/websocket-proxy-websocket.test.ts b/lib/test/websocket/websocket-proxy-websocket.test.ts index 8d85df5..5308e68 100644 --- a/lib/test/websocket/websocket-proxy-websocket.test.ts +++ b/lib/test/websocket/websocket-proxy-websocket.test.ts @@ -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>; diff --git a/lib/test/websocket/websocket-proxy.test.ts b/lib/test/websocket/websocket-proxy.test.ts index 97e05bc..24616ba 100644 --- a/lib/test/websocket/websocket-proxy.test.ts +++ b/lib/test/websocket/websocket-proxy.test.ts @@ -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"; From 25dffa26fece4c7c231266046d9a6da19f20f267 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Sun, 13 Jul 2025 01:39:39 +0900 Subject: [PATCH 2/2] replace other places using awaiting with builtin functions --- lib/test/http/error-handling.test.ts | 8 +++----- lib/test/http/server-sent-events.test.ts | 15 ++++++--------- lib/test/wait.ts | 4 ++-- package.json | 1 - pnpm-lock.yaml | 9 --------- 5 files changed, 11 insertions(+), 26 deletions(-) diff --git a/lib/test/http/error-handling.test.ts b/lib/test/http/error-handling.test.ts index bd253d0..4e02a8c 100644 --- a/lib/test/http/error-handling.test.ts +++ b/lib/test/http/error-handling.test.ts @@ -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"; @@ -68,15 +67,14 @@ describe("Test proxying over HTTP with latency", () => { Upgrade: "websocket", }, }; - const f = (cb: any) => { + const err = await new Promise((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); }); diff --git a/lib/test/http/server-sent-events.test.ts b/lib/test/http/server-sent-events.test.ts index 59de8eb..cab24b4 100644 --- a/lib/test/http/server-sent-events.test.ts +++ b/lib/test/http/server-sent-events.test.ts @@ -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", () => { @@ -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"); }); } diff --git a/lib/test/wait.ts b/lib/test/wait.ts index 93a63e6..ba323a5 100644 --- a/lib/test/wait.ts +++ b/lib/test/wait.ts @@ -1,9 +1,9 @@ -import { 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); } } diff --git a/package.json b/package.json index 00abd7f..dcbc669 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index abb46b5..44e7f60 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -61,9 +61,6 @@ importers: auto-changelog: specifier: ^2.5.0 version: 2.5.0 - awaiting: - specifier: ^3.0.0 - version: 3.0.0 better-sse: specifier: ^0.14.1 version: 0.14.1 @@ -532,10 +529,6 @@ packages: engines: {node: '>=8.3'} hasBin: true - awaiting@3.0.0: - resolution: {integrity: sha512-19i4G7Hjxj9idgMlAM0BTRII8HfvsOdlr4D9cf3Dm1MZhvcKjBpzY8AMNEyIKyi+L9TIK15xZatmdcPG003yww==} - engines: {node: '>=7.6.x'} - babel-jest@29.7.0: resolution: {integrity: sha512-BrvGY3xZSwEcCzKvKsCi2GgHqDqsYkOP4/by5xCgIwGXQxIEh+8ew3gmrE1y7XRR6LHZIj6yLYnUi/mm2KXKBg==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -2408,8 +2401,6 @@ snapshots: transitivePeerDependencies: - encoding - awaiting@3.0.0: {} - babel-jest@29.7.0(@babel/core@7.27.1): dependencies: '@babel/core': 7.27.1