1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-05 13:59:01 -05:00

workaround for https request from make-fetch-happen (@npmcli/agent)

This commit is contained in:
Yoshiya Hinosawa 2024-10-25 14:43:54 +09:00
parent d777191e33
commit 9c09c1d889
No known key found for this signature in database
GPG key ID: 9017DB4559488785
2 changed files with 21 additions and 9 deletions

View file

@ -85,6 +85,7 @@ export class TLSSocket extends net.Socket {
constructor(socket: any, opts: any = kEmptyObject) {
const tlsOptions = { ...opts };
const hostname = opts.servername ?? opts.host ?? socket._host;
tlsOptions.hostname = hostname;
@ -151,6 +152,11 @@ export class TLSSocket extends net.Socket {
// Patches `afterConnect` hook to replace TCP conn with TLS conn
const afterConnect = handle.afterConnect;
handle.afterConnect = async (req: any, status: number) => {
if (tlssock._isNpmAgent) {
tlssock.emit("secure");
tlssock.removeListener("end", onConnectEnd);
return afterConnect.call(handle, req, status);
}
try {
const conn = await Deno.startTls(handle[kStreamBaseField], options);
try {

View file

@ -781,6 +781,7 @@ export class Socket extends Duplex {
_host: string | null = null;
// deno-lint-ignore no-explicit-any
_parent: any = null;
_isNpmAgent = false;
constructor(options: SocketOptions | number) {
if (typeof options === "number") {
@ -800,6 +801,20 @@ export class Socket extends Duplex {
super(options);
// Note: If the socket is created from @npmcli/agent, the 'socket' event
// on ClientRequest object happens after 'connect' event on Socket object.
// That swaps the sequence of op_node_http_request_with_conn() call and
// initial socket read. That causes op_node_http_request_with_conn() not
// working.
// To avoid the above situation, we detect the socket created from
// @npmcli/agent and pause the socket (and also skips the startTls call
// if it's TLSSocket)
this._isNpmAgent = new Error().stack?.includes("@npmcli/agent") || false;
if (this._isNpmAgent) {
this.pause();
}
if (options.handle) {
this._handle = options.handle;
this[asyncIdSymbol] = _getNewAsyncId(this._handle);
@ -1586,15 +1601,6 @@ export function connect(...args: unknown[]) {
debug("createConnection", normalized);
const socket = new Socket(options);
// If it's called from @npmcli/agent, 'connect' event on Socket happens
// before 'socket' event on ClientRequst, and that causes initial read
// happening before op_node_http_request_with_conn(), and http reqeust
// doesn't work. The below pause() call prevent that initial read for
// @npmcli/agent.
if (new Error().stack?.includes("@npmcli/agent")) {
socket.pause();
}
if (netClientSocketChannel.hasSubscribers) {
netClientSocketChannel.publish({
socket,