Skip to content

Commit

Permalink
Build v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tdsultratom committed Jan 19, 2023
1 parent fafa0bb commit b6b9658
Showing 1 changed file with 90 additions and 2 deletions.
92 changes: 90 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6022,6 +6022,20 @@ const isDomainOrSubdomain = function isDomainOrSubdomain(destination, original)
return orig === dest || orig[orig.length - dest.length - 1] === '.' && orig.endsWith(dest);
};

/**
* isSameProtocol reports whether the two provided URLs use the same protocol.
*
* Both domains must already be in canonical form.
* @param {string|URL} original
* @param {string|URL} destination
*/
const isSameProtocol = function isSameProtocol(destination, original) {
const orig = new URL$1(original).protocol;
const dest = new URL$1(destination).protocol;

return orig === dest;
};

/**
* Fetch function
*
Expand Down Expand Up @@ -6053,7 +6067,7 @@ function fetch(url, opts) {
let error = new AbortError('The user aborted a request.');
reject(error);
if (request.body && request.body instanceof Stream.Readable) {
request.body.destroy(error);
destroyStream(request.body, error);
}
if (!response || !response.body) return;
response.body.emit('error', error);
Expand Down Expand Up @@ -6094,9 +6108,41 @@ function fetch(url, opts) {

req.on('error', function (err) {
reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));

if (response && response.body) {
destroyStream(response.body, err);
}

finalize();
});

fixResponseChunkedTransferBadEnding(req, function (err) {
if (signal && signal.aborted) {
return;
}

destroyStream(response.body, err);
});

/* c8 ignore next 18 */
if (parseInt(process.version.substring(1)) < 14) {
// Before Node.js 14, pipeline() does not fully support async iterators and does not always
// properly handle when the socket close/end events are out of order.
req.on('socket', function (s) {
s.addListener('close', function (hadError) {
// if a data listener is still present we didn't end cleanly
const hasDataListener = s.listenerCount('data') > 0;

// if end happened before close but the socket didn't emit an error, do it now
if (response && hasDataListener && !hadError && !(signal && signal.aborted)) {
const err = new Error('Premature close');
err.code = 'ERR_STREAM_PREMATURE_CLOSE';
response.body.emit('error', err);
}
});
});
}

req.on('response', function (res) {
clearTimeout(reqTimeout);

Expand Down Expand Up @@ -6168,7 +6214,7 @@ function fetch(url, opts) {
size: request.size
};

if (!isDomainOrSubdomain(request.url, locationURL)) {
if (!isDomainOrSubdomain(request.url, locationURL) || !isSameProtocol(request.url, locationURL)) {
for (const name of ['authorization', 'www-authenticate', 'cookie', 'cookie2']) {
requestOpts.headers.delete(name);
}
Expand Down Expand Up @@ -6261,6 +6307,13 @@ function fetch(url, opts) {
response = new Response(body, response_options);
resolve(response);
});
raw.on('end', function () {
// some old IIS servers return zero-length OK deflate responses, so 'data' is never emitted.
if (!response) {
response = new Response(body, response_options);
resolve(response);
}
});
return;
}

Expand All @@ -6280,6 +6333,41 @@ function fetch(url, opts) {
writeToStream(req, request);
});
}
function fixResponseChunkedTransferBadEnding(request, errorCallback) {
let socket;

request.on('socket', function (s) {
socket = s;
});

request.on('response', function (response) {
const headers = response.headers;

if (headers['transfer-encoding'] === 'chunked' && !headers['content-length']) {
response.once('close', function (hadError) {
// if a data listener is still present we didn't end cleanly
const hasDataListener = socket.listenerCount('data') > 0;

if (hasDataListener && !hadError) {
const err = new Error('Premature close');
err.code = 'ERR_STREAM_PREMATURE_CLOSE';
errorCallback(err);
}
});
}
});
}

function destroyStream(stream, err) {
if (stream.destroy) {
stream.destroy(err);
} else {
// node < 8
stream.emit('error', err);
stream.end();
}
}

/**
* Redirect code matching
*
Expand Down

0 comments on commit b6b9658

Please sign in to comment.