Update checked-in dependencies
This commit is contained in:
parent
7fdbca3ba3
commit
357e0ceaa9
360 changed files with 25673 additions and 917 deletions
157
node_modules/nock/lib/recorder.js
generated
vendored
157
node_modules/nock/lib/recorder.js
generated
vendored
|
|
@ -1,16 +1,24 @@
|
|||
'use strict'
|
||||
|
||||
const debug = require('debug')('nock.recorder')
|
||||
const { recorder: debug } = require('./debug')
|
||||
const querystring = require('querystring')
|
||||
const { inspect } = require('util')
|
||||
|
||||
const common = require('./common')
|
||||
const { restoreOverriddenClientRequest } = require('./intercept')
|
||||
|
||||
const { EventEmitter } = require('stream')
|
||||
const { gzipSync, brotliCompressSync, deflateSync } = require('zlib')
|
||||
const {
|
||||
default: nodeInterceptors,
|
||||
} = require('@mswjs/interceptors/presets/node')
|
||||
const SEPARATOR = '\n<<<<<<-- cut here -->>>>>>\n'
|
||||
let recordingInProgress = false
|
||||
let outputs = []
|
||||
|
||||
// TODO: don't reuse the nodeInterceptors, create new ones.
|
||||
const clientRequestInterceptor = nodeInterceptors[0]
|
||||
const fetchRequestInterceptor = nodeInterceptors[2]
|
||||
|
||||
function getScope(options) {
|
||||
const { proto, host, port } = common.normalizeRequestOptions(options)
|
||||
return common.normalizeOrigin(proto, host, port)
|
||||
|
|
@ -210,28 +218,67 @@ function record(recOptions) {
|
|||
// we restore any requests that may have been overridden by other parts of nock (e.g. intercept)
|
||||
// NOTE: This is hacky as hell but it keeps the backward compatibility *and* allows correct
|
||||
// behavior in the face of other modules also overriding ClientRequest.
|
||||
common.restoreOverriddenRequests()
|
||||
// common.restoreOverriddenRequests()
|
||||
// We restore ClientRequest as it messes with recording of modules that also override ClientRequest (e.g. xhr2)
|
||||
restoreOverriddenClientRequest()
|
||||
|
||||
// We override the requests so that we can save information on them before executing.
|
||||
common.overrideRequests(function (proto, overriddenRequest, rawArgs) {
|
||||
const { options, callback } = common.normalizeClientRequestArgs(...rawArgs)
|
||||
const bodyChunks = []
|
||||
clientRequestInterceptor.apply()
|
||||
fetchRequestInterceptor.apply()
|
||||
clientRequestInterceptor.on('request', async function ({ request }) {
|
||||
await recordRequest(request)
|
||||
})
|
||||
fetchRequestInterceptor.on('request', async function ({ request }) {
|
||||
await recordRequest(request)
|
||||
})
|
||||
|
||||
async function recordRequest(mswRequest) {
|
||||
const request = mswRequest.clone()
|
||||
const { options } = common.normalizeClientRequestArgs(request.url)
|
||||
options.method = request.method
|
||||
const proto = options.protocol.slice(0, -1)
|
||||
|
||||
// Node 0.11 https.request calls http.request -- don't want to record things
|
||||
// twice.
|
||||
/* istanbul ignore if */
|
||||
if (options._recording) {
|
||||
return overriddenRequest(options, callback)
|
||||
return
|
||||
}
|
||||
options._recording = true
|
||||
|
||||
const req = overriddenRequest(options, function (res) {
|
||||
const req = new EventEmitter()
|
||||
req.on('response', function () {
|
||||
debug(thisRecordingId, 'intercepting', proto, 'request to record')
|
||||
|
||||
// We put our 'end' listener to the front of the listener array.
|
||||
res.once('end', function () {
|
||||
clientRequestInterceptor.once('response', async function ({ response }) {
|
||||
await recordResponse(response)
|
||||
})
|
||||
fetchRequestInterceptor.once('response', async function ({ response }) {
|
||||
// fetch decompresses the body automatically, so we need to recompress it
|
||||
const codings =
|
||||
response.headers
|
||||
.get('content-encoding')
|
||||
?.toLowerCase()
|
||||
.split(',')
|
||||
.map(c => c.trim()) || []
|
||||
|
||||
let body = await response.arrayBuffer()
|
||||
for (const coding of codings) {
|
||||
if (coding === 'gzip') {
|
||||
body = gzipSync(body)
|
||||
} else if (coding === 'deflate') {
|
||||
body = deflateSync(body)
|
||||
} else if (coding === 'br') {
|
||||
body = brotliCompressSync(body)
|
||||
}
|
||||
}
|
||||
|
||||
await recordResponse(new Response(body, response))
|
||||
})
|
||||
|
||||
// Intercept "res.once('end', ...)"-like event
|
||||
async function recordResponse(mswResponse) {
|
||||
const response = mswResponse.clone()
|
||||
debug(thisRecordingId, proto, 'intercepted request ended')
|
||||
|
||||
let reqheaders
|
||||
|
|
@ -239,19 +286,26 @@ function record(recOptions) {
|
|||
if (enableReqHeadersRecording) {
|
||||
// We never record user-agent headers as they are worse than useless -
|
||||
// they actually make testing more difficult without providing any benefit (see README)
|
||||
reqheaders = req.getHeaders()
|
||||
reqheaders = Object.fromEntries(request.headers.entries())
|
||||
common.deleteHeadersField(reqheaders, 'user-agent')
|
||||
}
|
||||
|
||||
const headers = Object.fromEntries(response.headers.entries())
|
||||
const res = {
|
||||
statusCode: response.status,
|
||||
headers,
|
||||
rawHeaders: headers,
|
||||
}
|
||||
|
||||
const generateFn = outputObjects
|
||||
? generateRequestAndResponseObject
|
||||
: generateRequestAndResponse
|
||||
let out = generateFn({
|
||||
req,
|
||||
bodyChunks,
|
||||
req: options,
|
||||
bodyChunks: [Buffer.from(await request.arrayBuffer())],
|
||||
options,
|
||||
res,
|
||||
dataChunks,
|
||||
dataChunks: [Buffer.from(await response.arrayBuffer())],
|
||||
reqheaders,
|
||||
})
|
||||
|
||||
|
|
@ -282,33 +336,6 @@ function record(recOptions) {
|
|||
logging(out)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
let encoding
|
||||
// We need to be aware of changes to the stream's encoding so that we
|
||||
// don't accidentally mangle the data.
|
||||
const { setEncoding } = res
|
||||
res.setEncoding = function (newEncoding) {
|
||||
encoding = newEncoding
|
||||
return setEncoding.apply(this, arguments)
|
||||
}
|
||||
|
||||
const dataChunks = []
|
||||
// Replace res.push with our own implementation that stores chunks
|
||||
const origResPush = res.push
|
||||
res.push = function (data) {
|
||||
if (data) {
|
||||
if (encoding) {
|
||||
data = Buffer.from(data, encoding)
|
||||
}
|
||||
dataChunks.push(data)
|
||||
}
|
||||
|
||||
return origResPush.call(res, data)
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback(res, options, callback)
|
||||
}
|
||||
|
||||
debug('finished setting up intercepting')
|
||||
|
|
@ -322,46 +349,11 @@ function record(recOptions) {
|
|||
}
|
||||
})
|
||||
|
||||
const recordChunk = (chunk, encoding) => {
|
||||
debug(thisRecordingId, 'new', proto, 'body chunk')
|
||||
if (!Buffer.isBuffer(chunk)) {
|
||||
chunk = Buffer.from(chunk, encoding)
|
||||
}
|
||||
bodyChunks.push(chunk)
|
||||
}
|
||||
|
||||
const oldWrite = req.write
|
||||
req.write = function (chunk, encoding) {
|
||||
if (typeof chunk !== 'undefined') {
|
||||
recordChunk(chunk, encoding)
|
||||
oldWrite.apply(req, arguments)
|
||||
} else {
|
||||
throw new Error('Data was undefined.')
|
||||
}
|
||||
}
|
||||
|
||||
// Starting in Node 8, `OutgoingMessage.end()` directly calls an internal
|
||||
// `write_` function instead of proxying to the public
|
||||
// `OutgoingMessage.write()` method, so we have to wrap `end` too.
|
||||
const oldEnd = req.end
|
||||
req.end = function (chunk, encoding, callback) {
|
||||
debug('req.end')
|
||||
if (typeof chunk === 'function') {
|
||||
callback = chunk
|
||||
chunk = null
|
||||
} else if (typeof encoding === 'function') {
|
||||
callback = encoding
|
||||
encoding = null
|
||||
}
|
||||
|
||||
if (chunk) {
|
||||
recordChunk(chunk, encoding)
|
||||
}
|
||||
oldEnd.call(req, chunk, encoding, callback)
|
||||
}
|
||||
|
||||
return req
|
||||
})
|
||||
// This is a massive change, we are trying to change minimum code, so we emit end event here
|
||||
// because mswjs take care for these events
|
||||
// TODO: refactor the recorder, we no longer need all the listeners and can just record the request we get from MSW
|
||||
req.emit('response')
|
||||
}
|
||||
}
|
||||
|
||||
// Restore *all* the overridden http/https modules' properties.
|
||||
|
|
@ -371,7 +363,8 @@ function restore() {
|
|||
'restoring all the overridden http/https properties',
|
||||
)
|
||||
|
||||
common.restoreOverriddenRequests()
|
||||
clientRequestInterceptor.dispose()
|
||||
fetchRequestInterceptor.dispose()
|
||||
restoreOverriddenClientRequest()
|
||||
recordingInProgress = false
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue