Comparing version 13.5.1 to 14.0.0-beta.1
@@ -738,2 +738,20 @@ 'use strict' | ||
/** | ||
* @param {Request} request | ||
*/ | ||
function convertFetchRequestToOptions(request) { | ||
const url = new URL(request.url) | ||
const options = { | ||
...urlToOptions(url), | ||
method: request.method, | ||
host: url.hostname, | ||
port: url.port || (url.protocol === 'https:' ? 443 : 80), | ||
path: url.pathname + url.search, | ||
proto: url.protocol.slice(0, -1), | ||
headers: Object.fromEntries(request.headers.entries()), | ||
} | ||
return options | ||
} | ||
module.exports = { | ||
@@ -769,2 +787,3 @@ contentEncoding, | ||
stringifyRequest, | ||
convertFetchRequestToClientRequest: convertFetchRequestToOptions, | ||
} |
@@ -13,2 +13,3 @@ 'use strict' | ||
const globalEmitter = require('./global_emitter') | ||
const { createResponse } = require('./create_response') | ||
@@ -306,3 +307,5 @@ /** | ||
if (isOff() || isEnabledForNetConnect(options)) { | ||
originalClientRequest.apply(this, arguments) | ||
if (options.isFetchRequest === undefined) { | ||
originalClientRequest.apply(this, arguments) | ||
} | ||
} else { | ||
@@ -439,2 +442,24 @@ common.setImmediate( | ||
const originalFetch = global.fetch | ||
global.fetch = async (input, init = undefined) => { | ||
const request = new Request(input, init) | ||
const options = common.convertFetchRequestToClientRequest(request) | ||
options.isFetchRequest = true | ||
const body = await request.arrayBuffer() | ||
const clientRequest = new http.ClientRequest(options) | ||
// If mock found | ||
if (clientRequest.interceptors) { | ||
return new Promise((resolve, reject) => { | ||
clientRequest.on('response', response => { | ||
resolve(createResponse(response)) | ||
}) | ||
clientRequest.on('error', reject) | ||
clientRequest.end(body) | ||
}) | ||
} else { | ||
return originalFetch(input, init) | ||
} | ||
} | ||
overrideClientRequest() | ||
@@ -441,0 +466,0 @@ } |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "13.5.1", | ||
"version": "14.0.0-beta.1", | ||
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>", | ||
@@ -13,0 +13,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
188985
17
3691
2