Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: add HTTP proxy support #90

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
176 changes: 169 additions & 7 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@
},
"devDependencies": {
"@biomejs/biome": "^1.7.0",
"proxy": "^2.0.0",
"@tsconfig/strictest": "^2.0.5",
"@types/node": "^20.12.7",
"tsup": "^8.0.2",
"typescript": "^5.4.5",
"undici": "^6.0.0",
"vitest": "^1.5.0"
}
}
106 changes: 106 additions & 0 deletions src/snapshot.proxy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*!
* Tests are based on work by Nathan Rajlich:
* https://github.com/TooTallNate/node-http-proxy-agent/blob/65307ac8fe4e6ce1a2685d21ec4affa4c2a0a30d/test/test.js
* Copyright (c) 2013 Nathan Rajlich <[email protected]>
* Released under the MIT license
*
* and on work by Rafael Gonzaga (https://github.com/RafaelGSS)
*
* https://github.com/nodejs/undici/blob/512cdadc403874571cd5035a6c41debab1165310/test/proxy-agent.js#L370-L418
* Released under the MIT license
*/
import { Server, createServer } from "node:http";
import { type AddressInfo } from "node:net";
import { type ProxyServer, createProxy } from "proxy";
import { ProxyAgent, fetch as undiciFetch } from "undici";

Check failure on line 15 in src/snapshot.proxy.test.ts

View workflow job for this annotation

GitHub Actions / build

'undiciFetch' is declared but its value is never read.
import { Octokit } from "@octokit/core";

Check failure on line 16 in src/snapshot.proxy.test.ts

View workflow job for this annotation

GitHub Actions / build

'Octokit' is declared but its value is never read.
import { afterEach, beforeEach, describe, expect, it } from "vitest";

import { Snapshot, submitSnapshot } from './snapshot.js'
import { context } from '@actions/github'
import * as core from '@actions/core'

describe("client proxy", () => {
let server: Server;
let proxyServer: ProxyServer;
let serverUrl: string;

Check failure on line 26 in src/snapshot.proxy.test.ts

View workflow job for this annotation

GitHub Actions / build

'serverUrl' is declared but its value is never read.
let proxyUrl: string;

beforeEach(() => {
server = createServer();
server.listen(0, () => {});

proxyServer = createProxy();
proxyServer.listen(0, () => {});

serverUrl = `http://localhost:${(server.address() as AddressInfo).port}`;
proxyUrl = `http://localhost:${
(proxyServer.address() as AddressInfo).port
}`;
});

it("options.request.fetch = customFetch with dispatcher: new ProxyAgent(proxyUrl)", async () => {
let proxyConnectionEstablished = false;

// requests are not exposed to the proxy server, they are tunneled to
// Reference: https://github.com/advisories/GHSA-pgw7-wx7w-2w33
// Commit: https://github.com/nodejs/undici/commit/df4f7e0e95f5112322a96fd7a666cb28c1d48327#diff-90964a82994d6c63f28161d5410c64406e6abdee4ac0759e83b1abbbe469cda4L35-R39
proxyServer.on("connect", () => {
core.notice(`proxyServer.on("connect")`);
proxyConnectionEstablished = true;
});

server.on("request", (request, response) => {
core.notice(`request: ${request}`);
expect(request.method).toEqual("GET");
expect(request.url).toEqual("/");
expect(request.headers.accept).toBe("application/vnd.github.v3+json");

response.writeHead(200);
// return a body containing the expected JSON: {"value": "foo"}
response.write(JSON.stringify({ value: "foo" }));
response.end();
});

// const myFetch: typeof undiciFetch = (url, opts) => {
// return undiciFetch(url, {
// ...opts,
// dispatcher: new ProxyAgent({
// uri: proxyUrl,
// keepAliveTimeout: 10,
// keepAliveMaxTimeout: 10,
// }),
// });
// };

// const octokit = new Octokit({
// baseUrl: serverUrl,
// request: { fetch: myFetch },
// });

// await octokit.request("/");
const snapshot = new Snapshot(
{name: 'example-detector', url: 'http://example.com', version: '1.0.0'},
context,
{id: 'job', correlator: 'correlator'},
new Date()
)

await submitSnapshot(snapshot, context,
new ProxyAgent({
uri: proxyUrl,
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
})
);


expect(proxyConnectionEstablished).toBeTruthy();
expect.assertions(4);
});

afterEach(() => {
server.close();
proxyServer.close();
});
});
Loading
Loading