Skip to content

Core: Use hpagent for proxies #6779

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 11 additions & 13 deletions .pnp.cjs

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

Binary file not shown.
Binary file not shown.
35 changes: 35 additions & 0 deletions .yarn/versions/562a0ff0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/plugin-essentials": patch
"@yarnpkg/plugin-git": patch
"@yarnpkg/plugin-github": patch
"@yarnpkg/plugin-http": patch
"@yarnpkg/plugin-npm": patch
"@yarnpkg/plugin-npm-cli": patch
"@yarnpkg/plugin-typescript": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-jsr"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
105 changes: 105 additions & 0 deletions packages/acceptance-tests/pkg-tests-core/sources/utils/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {IncomingMessage, ServerResponse} from 'http';
import http from 'http';
import invariant from 'invariant';
import {AddressInfo} from 'net';
import net from 'net';
import os from 'os';
import pem from 'pem';
import semver from 'semver';
Expand Down Expand Up @@ -808,6 +809,110 @@ export const startPackageServer = ({type}: {type: keyof typeof packageServerUrls
});
};

const proxyServerUrls: {
http: Promise<string> | null;
https: Promise<string> | null;
} = {http: null, https: null};

export const startProxyServer = ({type = `http`}: {type?: keyof typeof proxyServerUrls} = {}): Promise<string> => {
const serverUrl = proxyServerUrls[type];
if (serverUrl !== null)
return serverUrl;

const sendError = (res: ServerResponse, statusCode: number, errorMessage: string): void => {
res.writeHead(statusCode);
res.end(errorMessage);
};

return proxyServerUrls[type] = new Promise((resolve, reject) => {
const listener: http.RequestListener = (req, res) => {
void (async () => {
try {
if (!req.url) {
sendError(res, 400, `Missing URL in request`);
return;
}

const url = new URL(req.url);
const options = {
hostname: url.hostname,
port: url.port || (url.protocol === `https:` ? 443 : 80),
path: `${url.pathname}${url.search}`,
method: req.method,
headers: {...req.headers},
};

delete options.headers[`proxy-authorization`];
delete options.headers[`proxy-connection`];
options.headers.connection = `close`;

const proxyReq = (url.protocol === `https:` ? https : http).request(options, proxyRes => {
res.writeHead(proxyRes.statusCode || 500, proxyRes.headers);
proxyRes.pipe(res);
});

req.pipe(proxyReq);

proxyReq.on(`error`, err => {
sendError(res, 502, `Proxy error: ${err.message}`);
});
} catch (error) {
sendError(res, 500, `Proxy server error: ${error.message}`);
}
})();
};

(async () => {
let server: https.Server | http.Server;

if (type === `https`) {
const certs = await getHttpsCertificates();

server = https.createServer({
cert: certs.server.certificate,
key: certs.server.clientKey,
ca: certs.ca.certificate,
}, listener);
} else {
server = http.createServer(listener);
}

// Handle the CONNECT method using the 'connect' event
server.on(`connect`, (req, clientSocket, head) => {
// Parse the target and establish the connection
const [targetHost, targetPort] = (req.url || ``).split(`:`);
const port = parseInt(targetPort) || 443;

const serverSocket = net.connect(port, targetHost, () => {
clientSocket.write(
`HTTP/1.1 200 Connection Established\r\n` +
`\r\n`,
);

serverSocket.write(head);

serverSocket.pipe(clientSocket);
clientSocket.pipe(serverSocket);
});

serverSocket.on(`error`, () => {
clientSocket.end();
});
clientSocket.on(`error`, () => {
serverSocket.end();
});
});

// We don't want the server to prevent the process from exiting
server.unref();
server.listen(() => {
const {port} = server.address() as AddressInfo;
resolve(`${type}://localhost:${port}`);
});
})();
});
};

export interface PackageDriver {
(packageJson: Record<string, any>, subDefinition: Record<string, any> | RunFunction, fn?: RunFunction): any;
getPackageManagerName: () => string;
Expand Down
Loading
Loading