Skip to content

Commit 19aba46

Browse files
committed
fix #4 -- add typescript typings
1 parent bfa226f commit 19aba46

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+70
-38
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tes.js
88
npm-debug.log
99
.nyc_output
1010
coverage
11-
tsconfig.tsbuildinfo
1211
dist
1312
*.sage-chat
1413
*.term
14+
*.tsbuildinfo

.tsconfig-bootstrap.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
preset: "ts-jest",
33
testEnvironment: "node",
4-
setupFiles: ["./test/setup.js"],
4+
setupFiles: ["./lib/test/setup.js"],
55
testMatch: ["**/?(*.)+(spec|test).ts?(x)"],
66
};

lib/http-proxy/common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export interface Outgoing extends Outgoing0 {
1515
method?: any;
1616
rejectUnauthorized?: boolean;
1717
path?: string;
18-
headers: { [header: string]: string | string[] | undefined };
18+
headers: { [header: string]: string | string[] | undefined } & {
19+
overwritten?: boolean;
20+
};
1921
}
2022

2123
// If we allow this header and a user sends it with a request,

lib/http-proxy/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface ProxyTargetDetailed {
2525
}
2626
export type ProxyType = "ws" | "web";
2727
export type ProxyTarget = ProxyTargetUrl | ProxyTargetDetailed;
28-
export type ProxyTargetUrl = URL | string;
28+
export type ProxyTargetUrl = URL | string | { port: number; host: string };
2929

3030
export interface ServerOptions {
3131
// NOTE: `options.target and `options.forward` cannot be both missing when the
@@ -145,10 +145,7 @@ export class ProxyServer extends EventEmitter {
145145
}
146146

147147
if (!requestOptions.target && !requestOptions.forward) {
148-
this.emit(
149-
"error",
150-
new Error("Must set target or forward"),
151-
);
148+
this.emit("error", new Error("Must set target or forward"));
152149
return;
153150
}
154151

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export { numOpenSockets } from "./http-proxy/passes/ws-incoming";
1717
* @api public
1818
*/
1919

20-
function createProxyServer(options: ServerOptions): ProxyServer {
20+
function createProxyServer(options: ServerOptions = {}): ProxyServer {
2121
return new ProxyServer(options);
2222
}
2323

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe("A simple round-robin load balancer that supports websockets", () => {
6666
);
6767

6868
proxyPort = await getPort();
69-
servers.proxy = httpProxy.createProxyServer();
69+
servers.proxy = httpProxy.createProxyServer({});
7070
let i = 0;
7171
function nextProxy() {
7272
i = (i + 1) % addresses.length;

test/balancer/simple-balancer.test.ts renamed to lib/test/balancer/simple-balancer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe("A simple round-robin load balancing strategy.", () => {
4646
let proxyPort;
4747
it("creates the round robin proxy server", async () => {
4848
proxyPort = await getPort();
49-
const proxy = httpProxy.createServer();
49+
const proxy = httpProxy.createServer({});
5050
let i = 0;
5151
servers.proxy = http
5252
.createServer((req, res) => {
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/http/basic-proxy.test.ts renamed to lib/test/http/basic-proxy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe("Load test against the basic proxy", () => {
6666
});
6767

6868
const COUNT = 200;
69-
const MAX_TIME = 1000;
69+
const MAX_TIME = 3000;
7070
it(`Does a serial load test of HTTP server with ${COUNT} requests`, async () => {
7171
const t = Date.now();
7272
for (let i = 0; i < COUNT; i++) {
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/lib/http-proxy-common.test.ts renamed to lib/test/lib/http-proxy-common.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
pnpm test ./http-proxy-common.test.ts
33
*/
44

5-
import { setupOutgoing, setupSocket } from "../../dist/lib/http-proxy/common";
5+
import {
6+
setupOutgoing as setupOutgoing0,
7+
setupSocket,
8+
} from "../../http-proxy/common";
9+
10+
// wrap setupOutgoing so types aren't checked, since a lot of the tests here
11+
// involve partial objects that typescript doesn't view as correct, e.g., {url:'foo...'}
12+
// for a Request.
13+
function setupOutgoing(...args) {
14+
setupOutgoing0(args[0], args[1], args[2], args[3]);
15+
}
616

717
describe("#setupOutgoing", () => {
818
it("should setup the correct headers", () => {
@@ -471,7 +481,7 @@ describe("#setupSocket", () => {
471481
socketConfig.keepalive = bol;
472482
},
473483
};
474-
setupSocket(stubSocket);
484+
setupSocket(stubSocket as any);
475485
expect(socketConfig.timeout).toEqual(0);
476486
expect(socketConfig.nodelay).toEqual(true);
477487
expect(socketConfig.keepalive).toEqual(true);

test/lib/http-proxy-passes-web-incoming.test.ts renamed to lib/test/lib/http-proxy-passes-web-incoming.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
deleteLength,
77
timeout,
88
XHeaders,
9-
} from "../../dist/lib/http-proxy/passes/web-incoming";
9+
} from "../../http-proxy/passes/web-incoming";
1010
import * as httpProxy from "../..";
1111
import * as http from "http";
1212
import concat from "concat-stream";
@@ -18,17 +18,17 @@ describe("#deleteLength", () => {
1818
const stubRequest = {
1919
method: "DELETE",
2020
headers: {},
21-
};
22-
deleteLength(stubRequest, {}, {});
21+
} as any;
22+
deleteLength(stubRequest);
2323
expect(stubRequest.headers["content-length"]).toEqual("0");
2424
});
2525

2626
it("should change `content-length` for OPTIONS requests", () => {
2727
const stubRequest = {
2828
method: "OPTIONS",
2929
headers: {},
30-
};
31-
deleteLength(stubRequest, {}, {});
30+
} as any;
31+
deleteLength(stubRequest);
3232
expect(stubRequest.headers["content-length"]).toEqual("0");
3333
});
3434

@@ -38,8 +38,8 @@ describe("#deleteLength", () => {
3838
headers: {
3939
"transfer-encoding": "chunked",
4040
},
41-
};
42-
deleteLength(stubRequest, {}, {});
41+
} as any;
42+
deleteLength(stubRequest);
4343
expect(stubRequest.headers["content-length"]).toEqual("0");
4444
expect(stubRequest.headers).not.toHaveProperty("transfer-encoding");
4545
});
@@ -55,7 +55,7 @@ describe("#timeout", () => {
5555
},
5656
},
5757
};
58-
58+
// @ts-ignore
5959
timeout(stubRequest, {}, { timeout: 5000 });
6060
expect(done).toEqual(5000);
6161
});
@@ -73,6 +73,7 @@ describe("#XHeaders", () => {
7373
};
7474

7575
it("set the correct x-forwarded-* headers", () => {
76+
// @ts-ignore
7677
XHeaders(stubRequest, {}, { xfwd: true });
7778
expect(stubRequest.headers["x-forwarded-for"]).toEqual("192.168.1.2");
7879
expect(stubRequest.headers["x-forwarded-port"]).toEqual("8080");

test/lib/http-proxy-passes-web-outgoing.test.ts renamed to lib/test/lib/http-proxy-passes-web-outgoing.test.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import {
22
removeChunked,
3-
setRedirectHostRewrite,
4-
setConnection,
3+
setRedirectHostRewrite as setRedirectHostRewrite0,
4+
setConnection as setConnection0,
55
writeStatusCode,
6-
writeHeaders,
7-
} from "../../dist/lib/http-proxy/passes/web-outgoing";
6+
writeHeaders as writeHeaders0,
7+
} from "../../http-proxy/passes/web-outgoing";
88
import * as url from "url";
99

1010
const state: any = { headers: {} };
1111

12+
// disable typescript
13+
function writeHeaders(...args) {
14+
return writeHeaders0(args[0], args[1], args[2], args[3]);
15+
}
16+
function setConnection(...args) {
17+
return setConnection0(args[0], args[1], args[2]);
18+
}
19+
function setRedirectHostRewrite(...args) {
20+
return setRedirectHostRewrite0(args[0], args[1], args[2], args[3]);
21+
}
22+
1223
// NOTE: here url.parse("http://backend.com") uses the deprecated url.parse
1324
// function, and we're testing that we still support it.
1425
for (const target of ["http://backend.com", url.parse("http://backend.com")]) {
@@ -273,6 +284,7 @@ describe("#writeStatusCode", () => {
273284
},
274285
};
275286

287+
// @ts-ignore
276288
writeStatusCode({}, res, { statusCode: 200 });
277289
});
278290
});
@@ -477,6 +489,7 @@ describe("#removeChunked", () => {
477489
},
478490
};
479491

492+
// @ts-ignore
480493
removeChunked({ httpVersion: "1.0" }, {}, proxyRes);
481494

482495
expect(proxyRes.headers["transfer-encoding"]).toEqual(undefined);

test/lib/http-proxy-passes-ws-incoming.test.ts renamed to lib/test/lib/http-proxy-passes-ws-incoming.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
pnpm test ./http-proxy-passes-ws-incoming.test.ts
33
*/
44

5-
import { checkMethodAndHeader, XHeaders } from "../../dist/lib/http-proxy/passes/ws-incoming";
5+
import {
6+
checkMethodAndHeader as checkMethodAndHeader0,
7+
XHeaders,
8+
} from "../../http-proxy/passes/ws-incoming";
9+
10+
// disable typescript for this function
11+
function checkMethodAndHeader(...args) {
12+
return checkMethodAndHeader0(args[0], args[1]);
13+
}
614

715
describe("#checkMethodAndHeader", () => {
816
it("should drop non-GET connections", () => {
@@ -80,6 +88,7 @@ describe("#checkMethodAndHeader", () => {
8088

8189
describe("#XHeaders", () => {
8290
it("return if no forward request", () => {
91+
// @ts-ignore
8392
let returnValue = XHeaders({}, {}, {});
8493
expect(returnValue).toBe(undefined);
8594
});
@@ -94,6 +103,7 @@ describe("#XHeaders", () => {
94103
host: "192.168.1.2:8080",
95104
},
96105
};
106+
// @ts-ignore
97107
XHeaders(stubRequest, {}, { xfwd: true });
98108
expect(stubRequest.headers["x-forwarded-for"]).toBe("192.168.1.2");
99109
expect(stubRequest.headers["x-forwarded-port"]).toBe("8080");
@@ -113,6 +123,7 @@ describe("#XHeaders", () => {
113123
host: "192.168.1.3:8181",
114124
},
115125
};
126+
// @ts-ignore
116127
XHeaders(stubRequest, {}, { xfwd: true });
117128
expect(stubRequest.headers["x-forwarded-for"]).toBe("192.168.1.3");
118129
expect(stubRequest.headers["x-forwarded-port"]).toBe("8181");
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe("Load testing proxying a WebSocket", () => {
4141
.listen(ports.proxy);
4242
});
4343

44-
const LATENCY = 250;
44+
const LATENCY = 1000;
4545
const COUNT = 250;
4646

4747
it(`Serial test ${COUNT} times plain websocket server`, async () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe("Example of proxying over HTTP and WebSockets", () => {
7070
client.close();
7171
});
7272

73-
const LATENCY = 250;
73+
const LATENCY = 1000;
7474
const COUNT = 250;
7575
it(`Serial test ${COUNT} times proxy server`, async () => {
7676
const t = Date.now();

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-proxy-3",
3-
"version": "1.20.4",
3+
"version": "1.20.5",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/sagemathinc/http-proxy-3.git"
@@ -13,7 +13,7 @@
1313
"jcrugzz <[email protected]>"
1414
],
1515
"main": "dist/lib/index.js",
16-
"files": ["dist"],
16+
"files": ["dist/lib/http-proxy", "dist/lib/index.js", "dist/lib/index.d.ts"],
1717
"dependencies": {
1818
"debug": "^4.4.0",
1919
"follow-redirects": "^1.15.9"
@@ -48,7 +48,8 @@
4848
"test-all": "pnpm audit && TEST_EXTERNAL_REVERSE_PROXY=yes pnpm test --runInBand",
4949
"test-versions": ". \"$NVM_DIR/nvm.sh\" && nvm use 18 && pnpm test && nvm use 20 && pnpm test && nvm use 22 && pnpm test && nvm use 24 && pnpm test && nvm use 20",
5050
"clean": "rm -rf dist node_modules",
51-
"build": "pnpm exec tsc --build .tsconfig-bootstrap.json && pnpm exec tsc --build",
51+
"build": "pnpm exec tsc --build",
52+
"make": "pnpm clean && pnpm install && pnpm build && pnpm test",
5253
"tsc": "pnpm exec tsc --watch --pretty --preserveWatchOutput",
5354
"prepublishOnly": "pnpm test-versions && pnpm test-all && rm -f dist/*.tsbuildinfo dist/.tsconfig-bootstrap* && rm -rf dist/test"
5455
},

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"noUnusedLocals": true,
55
"noUnusedParameters": true,
66
"strictNullChecks": true,
7+
"declaration": true,
78
"downlevelIteration": true,
89
"esModuleInterop": true,
910
"forceConsistentCasingInFileNames": true,
@@ -16,13 +17,13 @@
1617
"target": "es2020",
1718
"module": "commonjs",
1819
"rootDir": "./",
19-
"outDir": "dist"
20+
"outDir": "./dist"
2021
},
2122
"watchOptions": {
2223
"fallbackPolling": "dynamicPriority",
2324
"synchronousWatchDirectory": true,
2425
"watchDirectory": "useFsEvents",
2526
"watchFile": "useFsEvents"
2627
},
27-
"exclude": ["node_modules", "dist", "test-todo", "examples"]
28+
"exclude": ["node_modules", "dist", "benchmark"]
2829
}

0 commit comments

Comments
 (0)