mirror of
https://github.com/denoland/deno.git
synced 2025-01-06 22:35:51 -05:00
workaround for https request from make-fetch-happen (@npmcli/agent)
This commit is contained in:
parent
d777191e33
commit
9c09c1d889
2 changed files with 21 additions and 9 deletions
|
@ -85,6 +85,7 @@ export class TLSSocket extends net.Socket {
|
||||||
constructor(socket: any, opts: any = kEmptyObject) {
|
constructor(socket: any, opts: any = kEmptyObject) {
|
||||||
const tlsOptions = { ...opts };
|
const tlsOptions = { ...opts };
|
||||||
|
|
||||||
|
|
||||||
const hostname = opts.servername ?? opts.host ?? socket._host;
|
const hostname = opts.servername ?? opts.host ?? socket._host;
|
||||||
tlsOptions.hostname = hostname;
|
tlsOptions.hostname = hostname;
|
||||||
|
|
||||||
|
@ -151,6 +152,11 @@ export class TLSSocket extends net.Socket {
|
||||||
// Patches `afterConnect` hook to replace TCP conn with TLS conn
|
// Patches `afterConnect` hook to replace TCP conn with TLS conn
|
||||||
const afterConnect = handle.afterConnect;
|
const afterConnect = handle.afterConnect;
|
||||||
handle.afterConnect = async (req: any, status: number) => {
|
handle.afterConnect = async (req: any, status: number) => {
|
||||||
|
if (tlssock._isNpmAgent) {
|
||||||
|
tlssock.emit("secure");
|
||||||
|
tlssock.removeListener("end", onConnectEnd);
|
||||||
|
return afterConnect.call(handle, req, status);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const conn = await Deno.startTls(handle[kStreamBaseField], options);
|
const conn = await Deno.startTls(handle[kStreamBaseField], options);
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -781,6 +781,7 @@ export class Socket extends Duplex {
|
||||||
_host: string | null = null;
|
_host: string | null = null;
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
_parent: any = null;
|
_parent: any = null;
|
||||||
|
_isNpmAgent = false;
|
||||||
|
|
||||||
constructor(options: SocketOptions | number) {
|
constructor(options: SocketOptions | number) {
|
||||||
if (typeof options === "number") {
|
if (typeof options === "number") {
|
||||||
|
@ -800,6 +801,20 @@ export class Socket extends Duplex {
|
||||||
|
|
||||||
super(options);
|
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) {
|
if (options.handle) {
|
||||||
this._handle = options.handle;
|
this._handle = options.handle;
|
||||||
this[asyncIdSymbol] = _getNewAsyncId(this._handle);
|
this[asyncIdSymbol] = _getNewAsyncId(this._handle);
|
||||||
|
@ -1586,15 +1601,6 @@ export function connect(...args: unknown[]) {
|
||||||
debug("createConnection", normalized);
|
debug("createConnection", normalized);
|
||||||
const socket = new Socket(options);
|
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) {
|
if (netClientSocketChannel.hasSubscribers) {
|
||||||
netClientSocketChannel.publish({
|
netClientSocketChannel.publish({
|
||||||
socket,
|
socket,
|
||||||
|
|
Loading…
Reference in a new issue