Skip to content

Commit 0d35a4e

Browse files
authored
Merge pull request #14 from sapphi-red/refactor/remove-awaiting
replace functions from `awaiting` package with builtin functions
2 parents 3fc5030 + 25dffa2 commit 0d35a4e

13 files changed

+22
-54
lines changed

lib/test/balancer/simple-balancer-with-websockets.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pnpm test ./simple-balancer-with-websockets.test.ts
77
import * as http from "http";
88
import * as httpProxy from "../..";
99
import getPort from "../get-port";
10-
import { once } from "../wait";
10+
import { once } from "events";
1111
import fetch from "node-fetch";
1212

1313
describe("A simple round-robin load balancer that supports websockets", () => {

lib/test/http/error-handling.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import * as httpProxy from "../..";
66
import * as http from "http";
77
import getPort from "../get-port";
88
import log from "../log";
9-
import { callback } from "awaiting";
109
import fetch from "node-fetch";
1110

1211
const CUSTOM_ERROR = "There was an error proxying your request";
@@ -68,15 +67,14 @@ describe("Test proxying over HTTP with latency", () => {
6867
Upgrade: "websocket",
6968
},
7069
};
71-
const f = (cb: any) => {
70+
const err = await new Promise<Error>((resolve) => {
7271
const req = http.request(options);
7372
req.end();
7473
req.on("error", (err) => {
7574
log(`request error -- ${err}`);
76-
cb(undefined, err);
75+
resolve(err);
7776
});
78-
};
79-
const err = await callback(f);
77+
});
8078
expect(err.message).toContain("socket hang up");
8179
expect(customWSErrorCalled).toBe(true);
8280
});

lib/test/http/server-sent-events.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import * as http from "http";
99
import getPort from "../get-port";
1010
import { createSession } from "better-sse";
1111
import { EventSource } from "eventsource";
12-
import { callback } from "awaiting";
1312
import fetch from "node-fetch";
1413

1514
describe("proxying server sent events over HTTP", () => {
@@ -60,26 +59,24 @@ describe("proxying server sent events over HTTP", () => {
6059
// These two tests leave open handles on node v18, so we disable them ONLY
6160
// with node v18.
6261
it("test receiving an SSE WITHOUT using the proxy", async () => {
63-
const f = (cb: any) => {
62+
const resp = await new Promise(resolve => {
6463
const sse = new EventSource(`http://localhost:${ports.http}/sse`);
6564
sse.addEventListener("message", ({ data }) => {
6665
sse.close();
67-
cb(undefined, JSON.parse(data));
66+
resolve(JSON.parse(data));
6867
});
69-
};
70-
const resp = await callback(f);
68+
})
7169
expect(resp).toEqual("Hello world! - 1");
7270
});
7371

7472
it("test receiving an SSE USING the proxy", async () => {
75-
const f = (cb: any) => {
73+
const resp = await new Promise(resolve => {
7674
const sse = new EventSource(`http://localhost:${ports.proxy}/sse`);
7775
sse.addEventListener("message", ({ data }) => {
7876
sse.close();
79-
cb(undefined, JSON.parse(data));
77+
resolve(JSON.parse(data));
8078
});
81-
};
82-
const resp = await callback(f);
79+
});
8380
expect(resp).toEqual("Hello world! - 2");
8481
});
8582
}

lib/test/lib/http-proxy.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import * as net from "net";
1111
import WebSocket, { WebSocketServer } from "ws";
1212
import { Server } from "socket.io";
1313
import { io as socketioClient } from "socket.io-client";
14-
import wait, { once } from "../wait";
14+
import wait from "../wait";
15+
import { once } from "events";
1516

1617
const ports: { [port: string]: number } = {};
1718
let portIndex = -1;

lib/test/wait.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,9 @@
1-
import { callback, delay } from "awaiting";
1+
import { setTimeout } from "node:timers/promises";
22

33
export default async function wait({ until }: { until: Function }) {
44
let d = 5;
55
while (!(await until())) {
6-
await delay(d);
6+
await setTimeout(d);
77
d = Math.min(1000, d * 1.2);
88
}
99
}
10-
11-
import type { EventEmitter } from "events";
12-
13-
export async function once(obj: EventEmitter, event: string): Promise<any> {
14-
if (obj == null) {
15-
throw Error("once -- obj is undefined");
16-
}
17-
if (typeof obj.once != "function") {
18-
throw Error("once -- obj.once must be a function");
19-
}
20-
let val: any[] = [];
21-
function wait(cb: Function): void {
22-
obj.once(event, function (...args): void {
23-
val = args;
24-
cb();
25-
});
26-
}
27-
await callback(wait);
28-
return val;
29-
}

lib/test/websocket/latent-websocket-proxy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pnpm test latent-websocket-proxy.test.ts
66

77
import * as httpProxy from "../..";
88
import getPort from "../get-port";
9-
import { once } from "../wait";
9+
import { once } from "events";
1010
import http, { createServer } from "http";
1111
import { Server } from "socket.io";
1212
import { io as socketioClient } from "socket.io-client";

lib/test/websocket/simple-websocket-proxy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as http from "http";
1515
import * as httpProxy from "../..";
1616
import log from "../log";
1717
import getPort from "../get-port";
18-
import { once } from "../wait";
18+
import { once } from "events";
1919

2020
describe("Example of simple proxying of a WebSocket", () => {
2121
let ports: Record<'ws' | 'proxy', number>;

lib/test/websocket/standalone-websocket-proxy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pnpm test ./standalone-websocket-proxy.test.ts
66

77
import * as httpProxy from "../..";
88
import getPort from "../get-port";
9-
import { once } from "../wait";
9+
import { once } from "events";
1010
import http, { createServer } from "http";
1111
import { Server } from "socket.io";
1212
import { io as socketioClient } from "socket.io-client";

lib/test/websocket/websocket-load.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ pnpm test websocket-load.test.ts
77
import * as http from "http";
88
import * as httpProxy from "../..";
99
import getPort from "../get-port";
10-
import wait, { once } from "../wait";
10+
import wait from "../wait";
11+
import { once } from "events";
1112

1213
describe("Load testing proxying a WebSocket", () => {
1314
let ports: Record<'ws' | 'proxy', number>;

lib/test/websocket/websocket-proxy-websocket.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import * as httpProxy from "../..";
1717
import getPort from "../get-port";
1818
import { Server } from "socket.io";
1919
import { io as socketioClient } from "socket.io-client";
20-
import { once } from "../wait";
20+
import { once } from "events";
2121

2222
describe("Multilevel Proxying of a Websocket using Socket.io", () => {
2323
let ports: Record<'socketio' | 'inner' | 'outer', number>;

0 commit comments

Comments
 (0)