Skip to content

Commit a9b1091

Browse files
authored
WIP: add proxy support
1 parent 329fcc7 commit a9b1091

File tree

4 files changed

+285
-9
lines changed

4 files changed

+285
-9
lines changed

package-lock.json

Lines changed: 169 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
},
4545
"devDependencies": {
4646
"@biomejs/biome": "^1.7.0",
47+
"proxy": "^2.0.0",
4748
"@tsconfig/strictest": "^2.0.5",
4849
"@types/node": "^20.12.7",
4950
"tsup": "^8.0.2",
5051
"typescript": "^5.4.5",
52+
"undici": "^6.0.0",
5153
"vitest": "^1.5.0"
5254
}
5355
}

src/snapshot.proxy.test.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*!
2+
* Tests are based on work by Nathan Rajlich:
3+
* https://github.com/TooTallNate/node-http-proxy-agent/blob/65307ac8fe4e6ce1a2685d21ec4affa4c2a0a30d/test/test.js
4+
* Copyright (c) 2013 Nathan Rajlich <[email protected]>
5+
* Released under the MIT license
6+
*
7+
* and on work by Rafael Gonzaga (https://github.com/RafaelGSS)
8+
*
9+
* https://github.com/nodejs/undici/blob/512cdadc403874571cd5035a6c41debab1165310/test/proxy-agent.js#L370-L418
10+
* Released under the MIT license
11+
*/
12+
import { Server, createServer } from "node:http";
13+
import { type AddressInfo } from "node:net";
14+
import { type ProxyServer, createProxy } from "proxy";
15+
import { ProxyAgent, fetch as undiciFetch } from "undici";
16+
import { Octokit } from "@octokit/core";
17+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
18+
19+
import { Snapshot, submitSnapshot } from './snapshot.js'
20+
import { context } from '@actions/github'
21+
import * as core from '@actions/core'
22+
23+
describe("client proxy", () => {
24+
let server: Server;
25+
let proxyServer: ProxyServer;
26+
let serverUrl: string;
27+
let proxyUrl: string;
28+
29+
beforeEach(() => {
30+
server = createServer();
31+
server.listen(0, () => {});
32+
33+
proxyServer = createProxy();
34+
proxyServer.listen(0, () => {});
35+
36+
serverUrl = `http://localhost:${(server.address() as AddressInfo).port}`;
37+
proxyUrl = `http://localhost:${
38+
(proxyServer.address() as AddressInfo).port
39+
}`;
40+
});
41+
42+
it("options.request.fetch = customFetch with dispatcher: new ProxyAgent(proxyUrl)", async () => {
43+
let proxyConnectionEstablished = false;
44+
45+
// requests are not exposed to the proxy server, they are tunneled to
46+
// Reference: https://github.com/advisories/GHSA-pgw7-wx7w-2w33
47+
// Commit: https://github.com/nodejs/undici/commit/df4f7e0e95f5112322a96fd7a666cb28c1d48327#diff-90964a82994d6c63f28161d5410c64406e6abdee4ac0759e83b1abbbe469cda4L35-R39
48+
proxyServer.on("connect", () => {
49+
core.notice(`proxyServer.on("connect")`);
50+
proxyConnectionEstablished = true;
51+
});
52+
53+
server.on("request", (request, response) => {
54+
core.notice(`request: ${request}`);
55+
expect(request.method).toEqual("GET");
56+
expect(request.url).toEqual("/");
57+
expect(request.headers.accept).toBe("application/vnd.github.v3+json");
58+
59+
response.writeHead(200);
60+
// return a body containing the expected JSON: {"value": "foo"}
61+
response.write(JSON.stringify({ value: "foo" }));
62+
response.end();
63+
});
64+
65+
// const myFetch: typeof undiciFetch = (url, opts) => {
66+
// return undiciFetch(url, {
67+
// ...opts,
68+
// dispatcher: new ProxyAgent({
69+
// uri: proxyUrl,
70+
// keepAliveTimeout: 10,
71+
// keepAliveMaxTimeout: 10,
72+
// }),
73+
// });
74+
// };
75+
76+
// const octokit = new Octokit({
77+
// baseUrl: serverUrl,
78+
// request: { fetch: myFetch },
79+
// });
80+
81+
// await octokit.request("/");
82+
const snapshot = new Snapshot(
83+
{name: 'example-detector', url: 'http://example.com', version: '1.0.0'},
84+
context,
85+
{id: 'job', correlator: 'correlator'},
86+
new Date()
87+
)
88+
89+
await submitSnapshot(snapshot, context,
90+
new ProxyAgent({
91+
uri: proxyUrl,
92+
keepAliveTimeout: 10,
93+
keepAliveMaxTimeout: 10,
94+
})
95+
);
96+
97+
98+
expect(proxyConnectionEstablished).toBeTruthy();
99+
expect.assertions(4);
100+
});
101+
102+
afterEach(() => {
103+
server.close();
104+
proxyServer.close();
105+
});
106+
});

0 commit comments

Comments
 (0)