From 9f29c14937d874c3aaaaf27c7fc9725ddeb7aaeb Mon Sep 17 00:00:00 2001 From: Tatyana Kostromskaya <32135588+takost@users.noreply.github.com> Date: Tue, 26 Sep 2023 11:01:17 +0000 Subject: [PATCH] . --- .../github/__tests__/github.proxy.test.ts | 40 ++++++++++++-- packages/github/package-lock.json | 54 ++++++++++++++++++- packages/github/package.json | 3 +- packages/github/src/internal/utils.ts | 16 ++++++ packages/github/src/utils.ts | 2 +- 5 files changed, 108 insertions(+), 7 deletions(-) diff --git a/packages/github/__tests__/github.proxy.test.ts b/packages/github/__tests__/github.proxy.test.ts index 86c51fc2..7cd1fc4a 100644 --- a/packages/github/__tests__/github.proxy.test.ts +++ b/packages/github/__tests__/github.proxy.test.ts @@ -1,6 +1,7 @@ import * as http from 'http' import * as https from 'https' -import { createProxy } from 'proxy' +import { ProxyServer, createProxy } from "proxy"; +import { ProxyAgent, fetch as undiciFetch } from "undici"; // Default values are set when the module is imported, so we need to set proxy first. const proxyUrl = 'http://127.0.0.1:8081' @@ -22,7 +23,7 @@ describe('@actions/github', () => { proxyServer.listen(port, () => resolve()) }) proxyServer.on('connect', req => { - console.log('connect', req.url) + console.log("Connect to proxy server") proxyConnects.push(req.url ?? '') }) }) @@ -49,7 +50,23 @@ describe('@actions/github', () => { return } - const octokit = getOctokit(token) + const myFetch: typeof undiciFetch = (url, opts) => { + return undiciFetch(url, { + ...opts, + dispatcher: new ProxyAgent({ + uri: proxyUrl, + keepAliveTimeout: 10, + keepAliveMaxTimeout: 10, + }), + }); + }; + + const octokit = getOctokit(token, { + request: { + fetch: myFetch + } + }) + const branch = await octokit.rest.repos.getBranch({ owner: 'actions', repo: 'toolkit', @@ -65,7 +82,22 @@ describe('@actions/github', () => { return } process.env['https_proxy'] = proxyUrl - const octokit = getOctokit(token) + const myFetch: typeof undiciFetch = (url, opts) => { + return undiciFetch(url, { + ...opts, + dispatcher: new ProxyAgent({ + uri: proxyUrl, + keepAliveTimeout: 10, + keepAliveMaxTimeout: 10, + }), + }); + }; + + const octokit = getOctokit(token, { + request: { + fetch: myFetch + } + }) const repository = await octokit.graphql( '{repository(owner:"actions", name:"toolkit"){name}}' diff --git a/packages/github/package-lock.json b/packages/github/package-lock.json index 5a1824b5..f162c592 100644 --- a/packages/github/package-lock.json +++ b/packages/github/package-lock.json @@ -12,7 +12,8 @@ "@actions/http-client": "^2.1.1", "@octokit/core": "^4.2.4", "@octokit/plugin-paginate-rest": "^6.1.2", - "@octokit/plugin-rest-endpoint-methods": "^7.2.3" + "@octokit/plugin-rest-endpoint-methods": "^7.2.3", + "undici": "^5.25.1" }, "devDependencies": { "@types/proxy": "^1.0.1", @@ -215,6 +216,17 @@ "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.1.tgz", "integrity": "sha512-/6FKxSTWoJdbsLDF8tdIjaRiFXiE6UHsEHE3OPI/cwPURCVi1ukP0gmLn7XWEiFk5TcwQjjY5PWsU+j+tgXgmw==" }, + "node_modules/busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "dependencies": { + "streamsearch": "^1.1.0" + }, + "engines": { + "node": ">=10.16.0" + } + }, "node_modules/camelcase": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.0.0.tgz", @@ -366,6 +378,14 @@ "node": ">= 14" } }, + "node_modules/streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/supports-color": { "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", @@ -391,6 +411,17 @@ "node": ">=0.6.11 <=0.7.0 || >=0.7.3" } }, + "node_modules/undici": { + "version": "5.25.1", + "resolved": "https://registry.npmjs.org/undici/-/undici-5.25.1.tgz", + "integrity": "sha512-nTw6b2G2OqP6btYPyghCgV4hSwjJlL/78FMJatVLCa3otj6PCOQSt6dVtYt82OtNqFz8XsnJ+vsXLADPXjPhqw==", + "dependencies": { + "busboy": "^1.6.0" + }, + "engines": { + "node": ">=14.0" + } + }, "node_modules/universal-user-agent": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-6.0.0.tgz", @@ -579,6 +610,14 @@ "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.1.tgz", "integrity": "sha512-/6FKxSTWoJdbsLDF8tdIjaRiFXiE6UHsEHE3OPI/cwPURCVi1ukP0gmLn7XWEiFk5TcwQjjY5PWsU+j+tgXgmw==" }, + "busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "requires": { + "streamsearch": "^1.1.0" + } + }, "camelcase": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.0.0.tgz", @@ -687,6 +726,11 @@ "debug": "^4.3.4" } }, + "streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==" + }, "supports-color": { "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", @@ -706,6 +750,14 @@ "resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz", "integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg==" }, + "undici": { + "version": "5.25.1", + "resolved": "https://registry.npmjs.org/undici/-/undici-5.25.1.tgz", + "integrity": "sha512-nTw6b2G2OqP6btYPyghCgV4hSwjJlL/78FMJatVLCa3otj6PCOQSt6dVtYt82OtNqFz8XsnJ+vsXLADPXjPhqw==", + "requires": { + "busboy": "^1.6.0" + } + }, "universal-user-agent": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-6.0.0.tgz", diff --git a/packages/github/package.json b/packages/github/package.json index 9543b1b4..5e8e3701 100644 --- a/packages/github/package.json +++ b/packages/github/package.json @@ -41,7 +41,8 @@ "@actions/http-client": "^2.1.1", "@octokit/core": "^4.2.4", "@octokit/plugin-paginate-rest": "^6.1.2", - "@octokit/plugin-rest-endpoint-methods": "^7.2.3" + "@octokit/plugin-rest-endpoint-methods": "^7.2.3", + "undici": "^5.25.1" }, "devDependencies": { "@types/proxy": "^1.0.1", diff --git a/packages/github/src/internal/utils.ts b/packages/github/src/internal/utils.ts index ea5ec382..8efbd6ee 100644 --- a/packages/github/src/internal/utils.ts +++ b/packages/github/src/internal/utils.ts @@ -1,6 +1,8 @@ import * as http from 'http' import * as httpClient from '@actions/http-client' import {OctokitOptions} from '@octokit/core/dist-types/types' +import { ProxyServer, createProxy } from "proxy"; +import { ProxyAgent, fetch as undiciFetch } from "undici"; export function getAuthString( token: string, @@ -20,6 +22,20 @@ export function getProxyAgent(destinationUrl: string): http.Agent { return hc.getAgent(destinationUrl) } +export function getProxyFetchAgent(destinationUrl): any { + const myFetch: typeof undiciFetch = (url, opts) => { + return undiciFetch(url, { + ...opts, + dispatcher: new ProxyAgent({ + uri: destinationUrl, + keepAliveTimeout: 10, + keepAliveMaxTimeout: 10, + }), + }); + }; + return myFetch; +} + export function getApiBaseUrl(): string { return process.env['GITHUB_API_URL'] || 'https://api.github.com' } diff --git a/packages/github/src/utils.ts b/packages/github/src/utils.ts index 5238f308..9aa80a54 100644 --- a/packages/github/src/utils.ts +++ b/packages/github/src/utils.ts @@ -13,7 +13,7 @@ const baseUrl = Utils.getApiBaseUrl() export const defaults: OctokitOptions = { baseUrl, request: { - agent: Utils.getProxyAgent(baseUrl) + fetch: Utils.getProxyFetchAgent(baseUrl) } }