Update checked-in dependencies
This commit is contained in:
parent
7fdbca3ba3
commit
357e0ceaa9
360 changed files with 25673 additions and 917 deletions
147
node_modules/nock/lib/intercept.js
generated
vendored
147
node_modules/nock/lib/intercept.js
generated
vendored
|
|
@ -8,8 +8,20 @@ const { InterceptedRequestRouter } = require('./intercepted_request_router')
|
|||
const common = require('./common')
|
||||
const { inherits } = require('util')
|
||||
const http = require('http')
|
||||
const debug = require('debug')('nock.intercept')
|
||||
const { intercept: debug } = require('./debug')
|
||||
const globalEmitter = require('./global_emitter')
|
||||
const { BatchInterceptor } = require('@mswjs/interceptors')
|
||||
const {
|
||||
default: nodeInterceptors,
|
||||
} = require('@mswjs/interceptors/presets/node')
|
||||
const { createResponse } = require('./create_response')
|
||||
const { once } = require('events')
|
||||
|
||||
const interceptor = new BatchInterceptor({
|
||||
name: 'nock-interceptor',
|
||||
interceptors: nodeInterceptors,
|
||||
})
|
||||
let isNockActive = false
|
||||
|
||||
/**
|
||||
* @name NetConnectNotAllowedError
|
||||
|
|
@ -238,17 +250,6 @@ function removeInterceptor(options) {
|
|||
// (which might or might not be node's original http.ClientRequest)
|
||||
let originalClientRequest
|
||||
|
||||
function ErroringClientRequest(error) {
|
||||
http.OutgoingMessage.call(this)
|
||||
process.nextTick(
|
||||
function () {
|
||||
this.emit('error', error)
|
||||
}.bind(this),
|
||||
)
|
||||
}
|
||||
|
||||
inherits(ErroringClientRequest, http.ClientRequest)
|
||||
|
||||
function overrideClientRequest() {
|
||||
// Here's some background discussion about overriding ClientRequest:
|
||||
// - https://github.com/nodejitsu/mock-request/issues/4
|
||||
|
|
@ -333,6 +334,8 @@ function restoreOverriddenClientRequest() {
|
|||
if (!originalClientRequest) {
|
||||
debug('- ClientRequest was not overridden')
|
||||
} else {
|
||||
isNockActive = false
|
||||
interceptor.dispose()
|
||||
http.ClientRequest = originalClientRequest
|
||||
originalClientRequest = undefined
|
||||
|
||||
|
|
@ -341,9 +344,7 @@ function restoreOverriddenClientRequest() {
|
|||
}
|
||||
|
||||
function isActive() {
|
||||
// If ClientRequest has been overwritten by Nock then originalClientRequest is not undefined.
|
||||
// This means that Nock has been activated.
|
||||
return originalClientRequest !== undefined
|
||||
return isNockActive
|
||||
}
|
||||
|
||||
function interceptorScopes() {
|
||||
|
|
@ -367,75 +368,61 @@ function activeMocks() {
|
|||
}
|
||||
|
||||
function activate() {
|
||||
if (originalClientRequest) {
|
||||
if (isNockActive) {
|
||||
throw new Error('Nock already active')
|
||||
}
|
||||
|
||||
// ----- Overriding http.request and https.request:
|
||||
|
||||
common.overrideRequests(function (proto, overriddenRequest, args) {
|
||||
// NOTE: overriddenRequest is already bound to its module.
|
||||
|
||||
const { options, callback } = common.normalizeClientRequestArgs(...args)
|
||||
|
||||
if (Object.keys(options).length === 0) {
|
||||
// As weird as it is, it's possible to call `http.request` without
|
||||
// options, and it makes a request to localhost or somesuch. We should
|
||||
// support it too, for parity. However it doesn't work today, and fixing
|
||||
// it seems low priority. Giving an explicit error is nicer than
|
||||
// crashing with a weird stack trace. `new ClientRequest()`, nock's
|
||||
// other client-facing entry point, makes a similar check.
|
||||
// https://github.com/nock/nock/pull/1386
|
||||
// https://github.com/nock/nock/pull/1440
|
||||
throw Error(
|
||||
'Making a request with empty `options` is not supported in Nock',
|
||||
)
|
||||
}
|
||||
|
||||
// The option per the docs is `protocol`. Its unclear if this line is meant to override that and is misspelled or if
|
||||
// the intend is to explicitly keep track of which module was called using a separate name.
|
||||
// Either way, `proto` is used as the source of truth from here on out.
|
||||
options.proto = proto
|
||||
|
||||
const interceptors = interceptorsFor(options)
|
||||
|
||||
if (isOn() && interceptors) {
|
||||
const matches = interceptors.some(interceptor =>
|
||||
interceptor.matchOrigin(options),
|
||||
)
|
||||
const allowUnmocked = interceptors.some(
|
||||
interceptor => interceptor.options.allowUnmocked,
|
||||
)
|
||||
|
||||
if (!matches && allowUnmocked) {
|
||||
let req
|
||||
if (proto === 'https') {
|
||||
const { ClientRequest } = http
|
||||
http.ClientRequest = originalClientRequest
|
||||
req = overriddenRequest(options, callback)
|
||||
http.ClientRequest = ClientRequest
|
||||
} else {
|
||||
req = overriddenRequest(options, callback)
|
||||
}
|
||||
globalEmitter.emit('no match', req)
|
||||
return req
|
||||
}
|
||||
|
||||
// NOTE: Since we already overrode the http.ClientRequest we are in fact constructing
|
||||
// our own OverriddenClientRequest.
|
||||
return new http.ClientRequest(options, callback)
|
||||
} else {
|
||||
globalEmitter.emit('no match', options)
|
||||
if (isOff() || isEnabledForNetConnect(options)) {
|
||||
return overriddenRequest(options, callback)
|
||||
} else {
|
||||
const error = new NetConnectNotAllowedError(options.host, options.path)
|
||||
return new ErroringClientRequest(error)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
overrideClientRequest()
|
||||
interceptor.apply()
|
||||
// Force msw to forward Nock's error instead of coerce it into 500 error
|
||||
interceptor.on('unhandledException', ({ controller, error }) => {
|
||||
controller.errorWith(error)
|
||||
})
|
||||
interceptor.on(
|
||||
'request',
|
||||
async function ({ request: mswRequest, controller }) {
|
||||
const request = mswRequest.clone()
|
||||
const { options } = common.normalizeClientRequestArgs(request.url)
|
||||
options.proto = options.protocol.slice(0, -1)
|
||||
options.method = request.method
|
||||
const interceptors = interceptorsFor(options)
|
||||
if (isOn() && interceptors) {
|
||||
const matches = interceptors.some(interceptor =>
|
||||
interceptor.matchOrigin(options),
|
||||
)
|
||||
const allowUnmocked = interceptors.some(
|
||||
interceptor => interceptor.options.allowUnmocked,
|
||||
)
|
||||
|
||||
const nockRequest = common.convertFetchRequestToClientRequest(request)
|
||||
if (!matches && allowUnmocked) {
|
||||
globalEmitter.emit('no match', nockRequest)
|
||||
} else {
|
||||
nockRequest.on('response', nockResponse => {
|
||||
const response = createResponse(nockResponse, mswRequest.signal)
|
||||
controller.respondWith(response)
|
||||
})
|
||||
|
||||
const promise = Promise.race([
|
||||
// TODO: temp hacky way to handle allowUnmocked in startPlayback
|
||||
once(nockRequest, 'real-request'),
|
||||
once(nockRequest, 'error'),
|
||||
once(nockRequest, 'response'),
|
||||
])
|
||||
const buffer = await request.arrayBuffer()
|
||||
nockRequest.write(buffer)
|
||||
nockRequest.end()
|
||||
await promise
|
||||
}
|
||||
} else {
|
||||
globalEmitter.emit('no match', options)
|
||||
if (!(isOff() || isEnabledForNetConnect(options))) {
|
||||
throw new NetConnectNotAllowedError(options.host, options.path)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
isNockActive = true
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue