initial commit of actions

This commit is contained in:
Dominik Polakovics Polakovics 2026-01-31 18:56:04 +01:00
commit 949ece5785
44660 changed files with 12034344 additions and 0 deletions

21
github/codeql-action-v2/node_modules/nock/LICENSE generated vendored Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2011-2019 Pedro Teixeira and other contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

1737
github/codeql-action-v2/node_modules/nock/README.md generated vendored Normal file

File diff suppressed because it is too large Load diff

53
github/codeql-action-v2/node_modules/nock/index.js generated vendored Normal file
View file

@ -0,0 +1,53 @@
'use strict'
const back = require('./lib/back')
const emitter = require('./lib/global_emitter')
const {
activate,
isActive,
isDone,
isOn,
pendingMocks,
activeMocks,
removeInterceptor,
disableNetConnect,
enableNetConnect,
removeAll,
abortPendingRequests,
} = require('./lib/intercept')
const recorder = require('./lib/recorder')
const { Scope, load, loadDefs, define } = require('./lib/scope')
module.exports = (basePath, options) => new Scope(basePath, options)
Object.assign(module.exports, {
activate,
isActive,
isDone,
pendingMocks,
activeMocks,
removeInterceptor,
disableNetConnect,
enableNetConnect,
cleanAll: removeAll,
abortPendingRequests,
load,
loadDefs,
define,
emitter,
recorder: {
rec: recorder.record,
clear: recorder.clear,
play: recorder.outputs,
},
restore: recorder.restore,
back,
})
// We always activate Nock on import, overriding the globals.
// Setting the Back mode "activates" Nock by overriding the global entries in the `http/s` modules.
// If Nock Back is configured, we need to honor that setting for backward compatibility,
// otherwise we rely on Nock Back's default initializing side effect.
if (isOn()) {
back.setMode(process.env.NOCK_BACK_MODE || 'dryrun')
}

353
github/codeql-action-v2/node_modules/nock/lib/back.js generated vendored Normal file
View file

@ -0,0 +1,353 @@
'use strict'
const assert = require('assert')
const recorder = require('./recorder')
const {
activate,
disableNetConnect,
enableNetConnect,
removeAll: cleanAll,
} = require('./intercept')
const { loadDefs, define } = require('./scope')
const { format } = require('util')
const path = require('path')
const debug = require('debug')('nock.back')
let _mode = null
let fs
try {
fs = require('fs')
} catch (err) {
// do nothing, probably in browser
}
/**
* nock the current function with the fixture given
*
* @param {string} fixtureName - the name of the fixture, e.x. 'foo.json'
* @param {object} options - [optional] extra options for nock with, e.x. `{ assert: true }`
* @param {function} nockedFn - [optional] callback function to be executed with the given fixture being loaded;
* if defined the function will be called with context `{ scopes: loaded_nocks || [] }`
* set as `this` and `nockDone` callback function as first and only parameter;
* if not defined a promise resolving to `{nockDone, context}` where `context` is
* aforementioned `{ scopes: loaded_nocks || [] }`
*
* List of options:
*
* @param {function} before - a preprocessing function, gets called before nock.define
* @param {function} after - a postprocessing function, gets called after nock.define
* @param {function} afterRecord - a postprocessing function, gets called after recording. Is passed the array
* of scopes recorded and should return the array scopes to save to the fixture
* @param {function} recorder - custom options to pass to the recorder
*
*/
function Back(fixtureName, options, nockedFn) {
if (!Back.fixtures) {
throw new Error(
'Back requires nock.back.fixtures to be set\n' +
'Ex:\n' +
"\trequire(nock).back.fixtures = '/path/to/fixtures/'",
)
}
if (typeof fixtureName !== 'string') {
throw new Error('Parameter fixtureName must be a string')
}
if (arguments.length === 1) {
options = {}
} else if (arguments.length === 2) {
// If 2nd parameter is a function then `options` has been omitted
// otherwise `options` haven't been omitted but `nockedFn` was.
if (typeof options === 'function') {
nockedFn = options
options = {}
}
}
_mode.setup()
const fixture = path.join(Back.fixtures, fixtureName)
const context = _mode.start(fixture, options)
const nockDone = function () {
_mode.finish(fixture, options, context)
}
debug('context:', context)
// If nockedFn is a function then invoke it, otherwise return a promise resolving to nockDone.
if (typeof nockedFn === 'function') {
nockedFn.call(context, nockDone)
} else {
return Promise.resolve({ nockDone, context })
}
}
/*******************************************************************************
* Modes *
*******************************************************************************/
const wild = {
setup: function () {
cleanAll()
recorder.restore()
activate()
enableNetConnect()
},
start: function () {
return load() // don't load anything but get correct context
},
finish: function () {
// nothing to do
},
}
const dryrun = {
setup: function () {
recorder.restore()
cleanAll()
activate()
// We have to explicitly enable net connectivity as by default it's off.
enableNetConnect()
},
start: function (fixture, options) {
const contexts = load(fixture, options)
enableNetConnect()
return contexts
},
finish: function () {
// nothing to do
},
}
const record = {
setup: function () {
recorder.restore()
recorder.clear()
cleanAll()
activate()
disableNetConnect()
},
start: function (fixture, options) {
if (!fs) {
throw new Error('no fs')
}
const context = load(fixture, options)
if (!context.isLoaded) {
recorder.record({
dont_print: true,
output_objects: true,
...options.recorder,
})
context.isRecording = true
}
return context
},
finish: function (fixture, options, context) {
if (context.isRecording) {
let outputs = recorder.outputs()
if (typeof options.afterRecord === 'function') {
outputs = options.afterRecord(outputs)
}
outputs =
typeof outputs === 'string' ? outputs : JSON.stringify(outputs, null, 4)
debug('recorder outputs:', outputs)
fs.mkdirSync(path.dirname(fixture), { recursive: true })
fs.writeFileSync(fixture, outputs)
}
},
}
const update = {
setup: function () {
recorder.restore()
recorder.clear()
cleanAll()
activate()
disableNetConnect()
},
start: function (fixture, options) {
if (!fs) {
throw new Error('no fs')
}
const context = removeFixture(fixture)
recorder.record({
dont_print: true,
output_objects: true,
...options.recorder,
})
context.isRecording = true
return context
},
finish: function (fixture, options, context) {
let outputs = recorder.outputs()
if (typeof options.afterRecord === 'function') {
outputs = options.afterRecord(outputs)
}
outputs =
typeof outputs === 'string' ? outputs : JSON.stringify(outputs, null, 4)
debug('recorder outputs:', outputs)
fs.mkdirSync(path.dirname(fixture), { recursive: true })
fs.writeFileSync(fixture, outputs)
},
}
const lockdown = {
setup: function () {
recorder.restore()
recorder.clear()
cleanAll()
activate()
disableNetConnect()
},
start: function (fixture, options) {
return load(fixture, options)
},
finish: function () {
// nothing to do
},
}
function load(fixture, options) {
const context = {
scopes: [],
assertScopesFinished: function () {
assertScopes(this.scopes, fixture)
},
query: function () {
const nested = this.scopes.map(scope =>
scope.interceptors.map(interceptor => ({
method: interceptor.method,
uri: interceptor.uri,
basePath: interceptor.basePath,
path: interceptor.path,
queries: interceptor.queries,
counter: interceptor.counter,
body: interceptor.body,
statusCode: interceptor.statusCode,
optional: interceptor.optional,
})),
)
return [].concat.apply([], nested)
},
}
if (fixture && fixtureExists(fixture)) {
let scopes = loadDefs(fixture)
applyHook(scopes, options.before)
scopes = define(scopes)
applyHook(scopes, options.after)
context.scopes = scopes
context.isLoaded = true
}
return context
}
function removeFixture(fixture, options) {
const context = {
scopes: [],
assertScopesFinished: function () {},
}
if (fixture && fixtureExists(fixture)) {
/* istanbul ignore next - fs.unlinkSync is for node 10 support */
fs.rmSync ? fs.rmSync(fixture) : fs.unlinkSync(fixture)
}
context.isLoaded = false
return context
}
function applyHook(scopes, fn) {
if (!fn) {
return
}
if (typeof fn !== 'function') {
throw new Error('processing hooks must be a function')
}
scopes.forEach(fn)
}
function fixtureExists(fixture) {
if (!fs) {
throw new Error('no fs')
}
return fs.existsSync(fixture)
}
function assertScopes(scopes, fixture) {
const pending = scopes
.filter(scope => !scope.isDone())
.map(scope => scope.pendingMocks())
if (pending.length) {
assert.fail(
format(
'%j was not used, consider removing %s to rerecord fixture',
[].concat(...pending),
fixture,
),
)
}
}
const Modes = {
wild, // all requests go out to the internet, dont replay anything, doesnt record anything
dryrun, // use recorded nocks, allow http calls, doesnt record anything, useful for writing new tests (default)
record, // use recorded nocks, record new nocks
update, // allow http calls, record all nocks, don't use recorded nocks
lockdown, // use recorded nocks, disables all http calls even when not nocked, doesnt record
}
Back.setMode = function (mode) {
if (!(mode in Modes)) {
throw new Error(`Unknown mode: ${mode}`)
}
Back.currentMode = mode
debug('New nock back mode:', Back.currentMode)
_mode = Modes[mode]
_mode.setup()
}
Back.fixtures = null
Back.currentMode = null
module.exports = Back

771
github/codeql-action-v2/node_modules/nock/lib/common.js generated vendored Normal file
View file

@ -0,0 +1,771 @@
'use strict'
const debug = require('debug')('nock.common')
const timers = require('timers')
const url = require('url')
const util = require('util')
/**
* Normalizes the request options so that it always has `host` property.
*
* @param {Object} options - a parsed options object of the request
*/
function normalizeRequestOptions(options) {
options.proto = options.proto || 'http'
options.port = options.port || (options.proto === 'http' ? 80 : 443)
if (options.host) {
debug('options.host:', options.host)
if (!options.hostname) {
if (options.host.split(':').length === 2) {
options.hostname = options.host.split(':')[0]
} else {
options.hostname = options.host
}
}
}
debug('options.hostname in the end: %j', options.hostname)
options.host = `${options.hostname || 'localhost'}:${options.port}`
debug('options.host in the end: %j', options.host)
/// lowercase host names
;['hostname', 'host'].forEach(function (attr) {
if (options[attr]) {
options[attr] = options[attr].toLowerCase()
}
})
return options
}
/**
* Returns true if the data contained in buffer can be reconstructed
* from its utf8 representation.
*
* @param {Object} buffer - a Buffer object
* @returns {boolean}
*/
function isUtf8Representable(buffer) {
const utfEncodedBuffer = buffer.toString('utf8')
const reconstructedBuffer = Buffer.from(utfEncodedBuffer, 'utf8')
return reconstructedBuffer.equals(buffer)
}
// Array where all information about all the overridden requests are held.
let requestOverrides = {}
/**
* Overrides the current `request` function of `http` and `https` modules with
* our own version which intercepts issues HTTP/HTTPS requests and forwards them
* to the given `newRequest` function.
*
* @param {Function} newRequest - a function handling requests; it accepts four arguments:
* - proto - a string with the overridden module's protocol name (either `http` or `https`)
* - overriddenRequest - the overridden module's request function already bound to module's object
* - options - the options of the issued request
* - callback - the callback of the issued request
*/
function overrideRequests(newRequest) {
debug('overriding requests')
;['http', 'https'].forEach(function (proto) {
debug('- overriding request for', proto)
const moduleName = proto // 1 to 1 match of protocol and module is fortunate :)
const module = require(proto)
const overriddenRequest = module.request
const overriddenGet = module.get
if (requestOverrides[moduleName]) {
throw new Error(
`Module's request already overridden for ${moduleName} protocol.`,
)
}
// Store the properties of the overridden request so that it can be restored later on.
requestOverrides[moduleName] = {
module,
request: overriddenRequest,
get: overriddenGet,
}
// https://nodejs.org/api/http.html#http_http_request_url_options_callback
module.request = function (input, options, callback) {
return newRequest(proto, overriddenRequest.bind(module), [
input,
options,
callback,
])
}
// https://nodejs.org/api/http.html#http_http_get_options_callback
module.get = function (input, options, callback) {
const req = newRequest(proto, overriddenGet.bind(module), [
input,
options,
callback,
])
req.end()
return req
}
debug('- overridden request for', proto)
})
}
/**
* Restores `request` function of `http` and `https` modules to values they
* held before they were overridden by us.
*/
function restoreOverriddenRequests() {
debug('restoring requests')
Object.entries(requestOverrides).forEach(
([proto, { module, request, get }]) => {
debug('- restoring request for', proto)
module.request = request
module.get = get
debug('- restored request for', proto)
},
)
requestOverrides = {}
}
/**
* In WHATWG URL vernacular, this returns the origin portion of a URL.
* However, the port is not included if it's standard and not already present on the host.
*/
function normalizeOrigin(proto, host, port) {
const hostHasPort = host.includes(':')
const portIsStandard =
(proto === 'http' && (port === 80 || port === '80')) ||
(proto === 'https' && (port === 443 || port === '443'))
const portStr = hostHasPort || portIsStandard ? '' : `:${port}`
return `${proto}://${host}${portStr}`
}
/**
* Get high level information about request as string
* @param {Object} options
* @param {string} options.method
* @param {number|string} options.port
* @param {string} options.proto Set internally. always http or https
* @param {string} options.hostname
* @param {string} options.path
* @param {Object} options.headers
* @param {string} body
* @return {string}
*/
function stringifyRequest(options, body) {
const { method = 'GET', path = '', port } = options
const origin = normalizeOrigin(options.proto, options.hostname, port)
const log = {
method,
url: `${origin}${path}`,
headers: options.headers,
}
if (body) {
log.body = body
}
return JSON.stringify(log, null, 2)
}
function isContentEncoded(headers) {
const contentEncoding = headers['content-encoding']
return typeof contentEncoding === 'string' && contentEncoding !== ''
}
function contentEncoding(headers, encoder) {
const contentEncoding = headers['content-encoding']
return contentEncoding !== undefined && contentEncoding.toString() === encoder
}
function isJSONContent(headers) {
// https://tools.ietf.org/html/rfc8259
const contentType = String(headers['content-type'] || '').toLowerCase()
return contentType.startsWith('application/json')
}
/**
* Return a new object with all field names of the headers lower-cased.
*
* Duplicates throw an error.
*/
function headersFieldNamesToLowerCase(headers, throwOnDuplicate) {
if (!isPlainObject(headers)) {
throw Error('Headers must be provided as an object')
}
const lowerCaseHeaders = {}
Object.entries(headers).forEach(([fieldName, fieldValue]) => {
const key = fieldName.toLowerCase()
if (lowerCaseHeaders[key] !== undefined) {
if (throwOnDuplicate) {
throw Error(
`Failed to convert header keys to lower case due to field name conflict: ${key}`,
)
} else {
debug(
`Duplicate header provided in request: ${key}. Only the last value can be matched.`,
)
}
}
lowerCaseHeaders[key] = fieldValue
})
return lowerCaseHeaders
}
const headersFieldsArrayToLowerCase = headers => [
...new Set(headers.map(fieldName => fieldName.toLowerCase())),
]
/**
* Converts the various accepted formats of headers into a flat array representing "raw headers".
*
* Nock allows headers to be provided as a raw array, a plain object, or a Map.
*
* While all the header names are expected to be strings, the values are left intact as they can
* be functions, strings, or arrays of strings.
*
* https://nodejs.org/api/http.html#http_message_rawheaders
*/
function headersInputToRawArray(headers) {
if (headers === undefined) {
return []
}
if (Array.isArray(headers)) {
// If the input is an array, assume it's already in the raw format and simply return a copy
// but throw an error if there aren't an even number of items in the array
if (headers.length % 2) {
throw new Error(
`Raw headers must be provided as an array with an even number of items. [fieldName, value, ...]`,
)
}
return [...headers]
}
// [].concat(...) is used instead of Array.flat until v11 is the minimum Node version
if (util.types.isMap(headers)) {
return [].concat(...Array.from(headers, ([k, v]) => [k.toString(), v]))
}
if (isPlainObject(headers)) {
return [].concat(...Object.entries(headers))
}
throw new Error(
`Headers must be provided as an array of raw values, a Map, or a plain Object. ${headers}`,
)
}
/**
* Converts an array of raw headers to an object, using the same rules as Nodes `http.IncomingMessage.headers`.
*
* Header names/keys are lower-cased.
*/
function headersArrayToObject(rawHeaders) {
if (!Array.isArray(rawHeaders)) {
throw Error('Expected a header array')
}
const accumulator = {}
forEachHeader(rawHeaders, (value, fieldName) => {
addHeaderLine(accumulator, fieldName, value)
})
return accumulator
}
const noDuplicatesHeaders = new Set([
'age',
'authorization',
'content-length',
'content-type',
'etag',
'expires',
'from',
'host',
'if-modified-since',
'if-unmodified-since',
'last-modified',
'location',
'max-forwards',
'proxy-authorization',
'referer',
'retry-after',
'user-agent',
])
/**
* Set key/value data in accordance with Node's logic for folding duplicate headers.
*
* The `value` param should be a function, string, or array of strings.
*
* Node's docs and source:
* https://nodejs.org/api/http.html#http_message_headers
* https://github.com/nodejs/node/blob/908292cf1f551c614a733d858528ffb13fb3a524/lib/_http_incoming.js#L245
*
* Header names are lower-cased.
* Duplicates in raw headers are handled in the following ways, depending on the header name:
* - Duplicates of field names listed in `noDuplicatesHeaders` (above) are discarded.
* - `set-cookie` is always an array. Duplicates are added to the array.
* - For duplicate `cookie` headers, the values are joined together with '; '.
* - For all other headers, the values are joined together with ', '.
*
* Node's implementation is larger because it highly optimizes for not having to call `toLowerCase()`.
* We've opted to always call `toLowerCase` in exchange for a more concise function.
*
* While Node has the luxury of knowing `value` is always a string, we do an extra step of coercion at the top.
*/
function addHeaderLine(headers, name, value) {
let values // code below expects `values` to be an array of strings
if (typeof value === 'function') {
// Function values are evaluated towards the end of the response, before that we use a placeholder
// string just to designate that the header exists. Useful when `Content-Type` is set with a function.
values = [value.name]
} else if (Array.isArray(value)) {
values = value.map(String)
} else {
values = [String(value)]
}
const key = name.toLowerCase()
if (key === 'set-cookie') {
// Array header -- only Set-Cookie at the moment
if (headers['set-cookie'] === undefined) {
headers['set-cookie'] = values
} else {
headers['set-cookie'].push(...values)
}
} else if (noDuplicatesHeaders.has(key)) {
if (headers[key] === undefined) {
// Drop duplicates
headers[key] = values[0]
}
} else {
if (headers[key] !== undefined) {
values = [headers[key], ...values]
}
const separator = key === 'cookie' ? '; ' : ', '
headers[key] = values.join(separator)
}
}
/**
* Deletes the given `fieldName` property from `headers` object by performing
* case-insensitive search through keys.
*
* @headers {Object} headers - object of header field names and values
* @fieldName {String} field name - string with the case-insensitive field name
*/
function deleteHeadersField(headers, fieldNameToDelete) {
if (!isPlainObject(headers)) {
throw Error('headers must be an object')
}
if (typeof fieldNameToDelete !== 'string') {
throw Error('field name must be a string')
}
const lowerCaseFieldNameToDelete = fieldNameToDelete.toLowerCase()
// Search through the headers and delete all values whose field name matches the given field name.
Object.keys(headers)
.filter(fieldName => fieldName.toLowerCase() === lowerCaseFieldNameToDelete)
.forEach(fieldName => delete headers[fieldName])
}
/**
* Utility for iterating over a raw headers array.
*
* The callback is called with:
* - The header value. string, array of strings, or a function
* - The header field name. string
* - Index of the header field in the raw header array.
*/
function forEachHeader(rawHeaders, callback) {
for (let i = 0; i < rawHeaders.length; i += 2) {
callback(rawHeaders[i + 1], rawHeaders[i], i)
}
}
function percentDecode(str) {
try {
return decodeURIComponent(str.replace(/\+/g, ' '))
} catch (e) {
return str
}
}
/**
* URI encode the provided string, stringently adhering to RFC 3986.
*
* RFC 3986 reserves !, ', (, ), and * but encodeURIComponent does not encode them so we do it manually.
*
* https://tools.ietf.org/html/rfc3986
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
*/
function percentEncode(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, function (c) {
return `%${c.charCodeAt(0).toString(16).toUpperCase()}`
})
}
function matchStringOrRegexp(target, pattern) {
const targetStr =
target === undefined || target === null ? '' : String(target)
if (pattern instanceof RegExp) {
// if the regexp happens to have a global flag, we want to ensure we test the entire target
pattern.lastIndex = 0
return pattern.test(targetStr)
}
return targetStr === String(pattern)
}
/**
* Formats a query parameter.
*
* @param key The key of the query parameter to format.
* @param value The value of the query parameter to format.
* @param stringFormattingFn The function used to format string values. Can
* be used to encode or decode the query value.
*
* @returns *[] the formatted [key, value] pair.
*/
function formatQueryValue(key, value, stringFormattingFn) {
// TODO: Probably refactor code to replace `switch(true)` with `if`/`else`.
switch (true) {
case typeof value === 'number': // fall-through
case typeof value === 'boolean':
value = value.toString()
break
case value === null:
case value === undefined:
value = ''
break
case typeof value === 'string':
if (stringFormattingFn) {
value = stringFormattingFn(value)
}
break
case value instanceof RegExp:
break
case Array.isArray(value): {
value = value.map(function (val, idx) {
return formatQueryValue(idx, val, stringFormattingFn)[1]
})
break
}
case typeof value === 'object': {
value = Object.entries(value).reduce(function (acc, [subKey, subVal]) {
const subPair = formatQueryValue(subKey, subVal, stringFormattingFn)
acc[subPair[0]] = subPair[1]
return acc
}, {})
break
}
}
if (stringFormattingFn) key = stringFormattingFn(key)
return [key, value]
}
function isStream(obj) {
return (
obj &&
typeof obj !== 'string' &&
!Buffer.isBuffer(obj) &&
typeof obj.setEncoding === 'function'
)
}
/**
* Converts the arguments from the various signatures of http[s].request into a standard
* options object and an optional callback function.
*
* https://nodejs.org/api/http.html#http_http_request_url_options_callback
*
* Taken from the beginning of the native `ClientRequest`.
* https://github.com/nodejs/node/blob/908292cf1f551c614a733d858528ffb13fb3a524/lib/_http_client.js#L68
*/
function normalizeClientRequestArgs(input, options, cb) {
if (typeof input === 'string') {
input = urlToOptions(new url.URL(input))
} else if (input instanceof url.URL) {
input = urlToOptions(input)
} else {
cb = options
options = input
input = null
}
if (typeof options === 'function') {
cb = options
options = input || {}
} else {
options = Object.assign(input || {}, options)
}
return { options, callback: cb }
}
/**
* Utility function that converts a URL object into an ordinary
* options object as expected by the http.request and https.request APIs.
*
* This was copied from Node's source
* https://github.com/nodejs/node/blob/908292cf1f551c614a733d858528ffb13fb3a524/lib/internal/url.js#L1257
*/
function urlToOptions(url) {
const options = {
protocol: url.protocol,
hostname:
typeof url.hostname === 'string' && url.hostname.startsWith('[')
? url.hostname.slice(1, -1)
: url.hostname,
hash: url.hash,
search: url.search,
pathname: url.pathname,
path: `${url.pathname}${url.search || ''}`,
href: url.href,
}
if (url.port !== '') {
options.port = Number(url.port)
}
if (url.username || url.password) {
options.auth = `${url.username}:${url.password}`
}
return options
}
/**
* Determines if request data matches the expected schema.
*
* Used for comparing decoded search parameters, request body JSON objects,
* and URL decoded request form bodies.
*
* Performs a general recursive strict comparison with two caveats:
* - The expected data can use regexp to compare values
* - JSON path notation and nested objects are considered equal
*/
const dataEqual = (expected, actual) => {
if (isPlainObject(expected)) {
expected = expand(expected)
}
if (isPlainObject(actual)) {
actual = expand(actual)
}
return deepEqual(expected, actual)
}
/**
* Performs a recursive strict comparison between two values.
*
* Expected values or leaf nodes of expected object values that are RegExp use test() for comparison.
*/
function deepEqual(expected, actual) {
debug('deepEqual comparing', typeof expected, expected, typeof actual, actual)
if (expected instanceof RegExp) {
return expected.test(actual)
}
if (Array.isArray(expected) && Array.isArray(actual)) {
if (expected.length !== actual.length) {
return false
}
return expected.every((expVal, idx) => deepEqual(expVal, actual[idx]))
}
if (isPlainObject(expected) && isPlainObject(actual)) {
const allKeys = Array.from(
new Set(Object.keys(expected).concat(Object.keys(actual))),
)
return allKeys.every(key => deepEqual(expected[key], actual[key]))
}
return expected === actual
}
const timeouts = new Set()
const immediates = new Set()
const wrapTimer =
(timer, ids) =>
(callback, ...timerArgs) => {
const cb = (...callbackArgs) => {
try {
// eslint-disable-next-line n/no-callback-literal
callback(...callbackArgs)
} finally {
ids.delete(id)
}
}
const id = timer(cb, ...timerArgs)
ids.add(id)
return id
}
const setTimeout = wrapTimer(timers.setTimeout, timeouts)
const setImmediate = wrapTimer(timers.setImmediate, immediates)
function clearTimer(clear, ids) {
ids.forEach(clear)
ids.clear()
}
function removeAllTimers() {
clearTimer(clearTimeout, timeouts)
clearTimer(clearImmediate, immediates)
}
/**
* Check if the Client Request has been cancelled.
*
* Until Node 14 is the minimum, we need to look at both flags to see if the request has been cancelled.
* The two flags have the same purpose, but the Node maintainers are migrating from `abort(ed)` to
* `destroy(ed)` terminology, to be more consistent with `stream.Writable`.
* In Node 14.x+, Calling `abort()` will set both `aborted` and `destroyed` to true, however,
* calling `destroy()` will only set `destroyed` to true.
* Falling back on checking if the socket is destroyed to cover the case of Node <14.x where
* `destroy()` is called, but `destroyed` is undefined.
*
* Node Client Request history:
* - `request.abort()`: Added in: v0.3.8, Deprecated since: v14.1.0, v13.14.0
* - `request.aborted`: Added in: v0.11.14, Became a boolean instead of a timestamp: v11.0.0, Not deprecated (yet)
* - `request.destroy()`: Added in: v0.3.0
* - `request.destroyed`: Added in: v14.1.0, v13.14.0
*
* @param {ClientRequest} req
* @returns {boolean}
*/
function isRequestDestroyed(req) {
return !!(
req.destroyed === true ||
req.aborted ||
(req.socket && req.socket.destroyed)
)
}
/**
* Returns true if the given value is a plain object and not an Array.
* @param {*} value
* @returns {boolean}
*/
function isPlainObject(value) {
if (typeof value !== 'object' || value === null) return false
if (Object.prototype.toString.call(value) !== '[object Object]') return false
const proto = Object.getPrototypeOf(value)
if (proto === null) return true
const Ctor =
Object.prototype.hasOwnProperty.call(proto, 'constructor') &&
proto.constructor
return (
typeof Ctor === 'function' &&
Ctor instanceof Ctor &&
Function.prototype.call(Ctor) === Function.prototype.call(value)
)
}
const prototypePollutionBlockList = ['__proto__', 'prototype', 'constructor']
const blocklistFilter = function (part) {
return prototypePollutionBlockList.indexOf(part) === -1
}
/**
* Converts flat objects whose keys use JSON path notation to nested objects.
*
* The input object is not mutated.
*
* @example
* { 'foo[bar][0]': 'baz' } -> { foo: { bar: [ 'baz' ] } }
*/
const expand = input => {
if (input === undefined || input === null) {
return input
}
const keys = Object.keys(input)
const result = {}
let resultPtr = result
for (let path of keys) {
const originalPath = path
if (path.indexOf('[') >= 0) {
path = path.replace(/\[/g, '.').replace(/]/g, '')
}
const parts = path.split('.')
const check = parts.filter(blocklistFilter)
if (check.length !== parts.length) {
return undefined
}
resultPtr = result
const lastIndex = parts.length - 1
for (let i = 0; i < parts.length; ++i) {
const part = parts[i]
if (i === lastIndex) {
if (Array.isArray(resultPtr)) {
resultPtr[+part] = input[originalPath]
} else {
resultPtr[part] = input[originalPath]
}
} else {
if (resultPtr[part] === undefined || resultPtr[part] === null) {
const nextPart = parts[i + 1]
if (/^\d+$/.test(nextPart)) {
resultPtr[part] = []
} else {
resultPtr[part] = {}
}
}
resultPtr = resultPtr[part]
}
}
}
return result
}
module.exports = {
contentEncoding,
dataEqual,
deleteHeadersField,
expand,
forEachHeader,
formatQueryValue,
headersArrayToObject,
headersFieldNamesToLowerCase,
headersFieldsArrayToLowerCase,
headersInputToRawArray,
isContentEncoded,
isJSONContent,
isPlainObject,
isRequestDestroyed,
isStream,
isUtf8Representable,
matchStringOrRegexp,
normalizeClientRequestArgs,
normalizeOrigin,
normalizeRequestOptions,
overrideRequests,
percentDecode,
percentEncode,
removeAllTimers,
restoreOverriddenRequests,
setImmediate,
setTimeout,
stringifyRequest,
}

View file

@ -0,0 +1,5 @@
'use strict'
const { EventEmitter } = require('events')
module.exports = new EventEmitter()

View file

@ -0,0 +1,456 @@
'use strict'
/**
* @module nock/intercept
*/
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 globalEmitter = require('./global_emitter')
/**
* @name NetConnectNotAllowedError
* @private
* @desc Error trying to make a connection when disabled external access.
* @class
* @example
* nock.disableNetConnect();
* http.get('http://zombo.com');
* // throw NetConnectNotAllowedError
*/
function NetConnectNotAllowedError(host, path) {
Error.call(this)
this.name = 'NetConnectNotAllowedError'
this.code = 'ENETUNREACH'
this.message = `Nock: Disallowed net connect for "${host}${path}"`
Error.captureStackTrace(this, this.constructor)
}
inherits(NetConnectNotAllowedError, Error)
let allInterceptors = {}
let allowNetConnect
/**
* Enabled real request.
* @public
* @param {String|RegExp} matcher=RegExp.new('.*') Expression to match
* @example
* // Enables all real requests
* nock.enableNetConnect();
* @example
* // Enables real requests for url that matches google
* nock.enableNetConnect('google');
* @example
* // Enables real requests for url that matches google and amazon
* nock.enableNetConnect(/(google|amazon)/);
* @example
* // Enables real requests for url that includes google
* nock.enableNetConnect(host => host.includes('google'));
*/
function enableNetConnect(matcher) {
if (typeof matcher === 'string') {
allowNetConnect = new RegExp(matcher)
} else if (matcher instanceof RegExp) {
allowNetConnect = matcher
} else if (typeof matcher === 'function') {
allowNetConnect = { test: matcher }
} else {
allowNetConnect = /.*/
}
}
function isEnabledForNetConnect(options) {
common.normalizeRequestOptions(options)
const enabled = allowNetConnect && allowNetConnect.test(options.host)
debug('Net connect', enabled ? '' : 'not', 'enabled for', options.host)
return enabled
}
/**
* Disable all real requests.
* @public
* @example
* nock.disableNetConnect();
*/
function disableNetConnect() {
allowNetConnect = undefined
}
function isOn() {
return !isOff()
}
function isOff() {
return process.env.NOCK_OFF === 'true'
}
function addInterceptor(key, interceptor, scope, scopeOptions, host) {
if (!(key in allInterceptors)) {
allInterceptors[key] = { key, interceptors: [] }
}
interceptor.__nock_scope = scope
// We need scope's key and scope options for scope filtering function (if defined)
interceptor.__nock_scopeKey = key
interceptor.__nock_scopeOptions = scopeOptions
// We need scope's host for setting correct request headers for filtered scopes.
interceptor.__nock_scopeHost = host
interceptor.interceptionCounter = 0
if (scopeOptions.allowUnmocked) allInterceptors[key].allowUnmocked = true
allInterceptors[key].interceptors.push(interceptor)
}
function remove(interceptor) {
if (interceptor.__nock_scope.shouldPersist() || --interceptor.counter > 0) {
return
}
const { basePath } = interceptor
const interceptors =
(allInterceptors[basePath] && allInterceptors[basePath].interceptors) || []
// TODO: There is a clearer way to write that we want to delete the first
// matching instance. I'm also not sure why we couldn't delete _all_
// matching instances.
interceptors.some(function (thisInterceptor, i) {
return thisInterceptor === interceptor ? interceptors.splice(i, 1) : false
})
}
function removeAll() {
Object.keys(allInterceptors).forEach(function (key) {
allInterceptors[key].interceptors.forEach(function (interceptor) {
interceptor.scope.keyedInterceptors = {}
})
})
allInterceptors = {}
}
/**
* Return all the Interceptors whose Scopes match against the base path of the provided options.
*
* @returns {Interceptor[]}
*/
function interceptorsFor(options) {
common.normalizeRequestOptions(options)
debug('interceptors for %j', options.host)
const basePath = `${options.proto}://${options.host}`
debug('filtering interceptors for basepath', basePath)
// First try to use filteringScope if any of the interceptors has it defined.
for (const { key, interceptors, allowUnmocked } of Object.values(
allInterceptors,
)) {
for (const interceptor of interceptors) {
const { filteringScope } = interceptor.__nock_scopeOptions
// If scope filtering function is defined and returns a truthy value then
// we have to treat this as a match.
if (filteringScope && filteringScope(basePath)) {
interceptor.scope.logger('found matching scope interceptor')
// Keep the filtered scope (its key) to signal the rest of the module
// that this wasn't an exact but filtered match.
interceptors.forEach(ic => {
ic.__nock_filteredScope = ic.__nock_scopeKey
})
return interceptors
}
}
if (common.matchStringOrRegexp(basePath, key)) {
if (allowUnmocked && interceptors.length === 0) {
debug('matched base path with allowUnmocked (no matching interceptors)')
return [
{
options: { allowUnmocked: true },
matchOrigin() {
return false
},
},
]
} else {
debug(
`matched base path (${interceptors.length} interceptor${
interceptors.length > 1 ? 's' : ''
})`,
)
return interceptors
}
}
}
return undefined
}
function removeInterceptor(options) {
// Lazily import to avoid circular imports.
const Interceptor = require('./interceptor')
let baseUrl, key, method, proto
if (options instanceof Interceptor) {
baseUrl = options.basePath
key = options._key
} else {
proto = options.proto ? options.proto : 'http'
common.normalizeRequestOptions(options)
baseUrl = `${proto}://${options.host}`
method = (options.method && options.method.toUpperCase()) || 'GET'
key = `${method} ${baseUrl}${options.path || '/'}`
}
if (
allInterceptors[baseUrl] &&
allInterceptors[baseUrl].interceptors.length > 0
) {
for (let i = 0; i < allInterceptors[baseUrl].interceptors.length; i++) {
const interceptor = allInterceptors[baseUrl].interceptors[i]
if (
options instanceof Interceptor
? interceptor === options
: interceptor._key === key
) {
allInterceptors[baseUrl].interceptors.splice(i, 1)
interceptor.scope.remove(key, interceptor)
break
}
}
return true
}
return false
}
// Variable where we keep the ClientRequest we have overridden
// (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
// - https://github.com/nock/nock/issues/26
// It would be good to add a comment that explains this more clearly.
debug('Overriding ClientRequest')
// ----- Extending http.ClientRequest
// Define the overriding client request that nock uses internally.
function OverriddenClientRequest(...args) {
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. `http[s].request()`, 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(
'Creating a ClientRequest with empty `options` is not supported in Nock',
)
}
http.OutgoingMessage.call(this)
// Filter the interceptors per request options.
const interceptors = interceptorsFor(options)
if (isOn() && interceptors) {
debug('using', interceptors.length, 'interceptors')
// Use filtered interceptors to intercept requests.
// TODO: this shouldn't be a class anymore
// the overrider explicitly overrides methods and attrs on the request so the `assign` below should be removed.
const overrider = new InterceptedRequestRouter({
req: this,
options,
interceptors,
})
Object.assign(this, overrider)
if (callback) {
this.once('response', callback)
}
} else {
debug('falling back to original ClientRequest')
// Fallback to original ClientRequest if nock is off or the net connection is enabled.
if (isOff() || isEnabledForNetConnect(options)) {
originalClientRequest.apply(this, arguments)
} else {
common.setImmediate(
function () {
const error = new NetConnectNotAllowedError(
options.host,
options.path,
)
this.emit('error', error)
}.bind(this),
)
}
}
}
inherits(OverriddenClientRequest, http.ClientRequest)
// Override the http module's request but keep the original so that we can use it and later restore it.
// NOTE: We only override http.ClientRequest as https module also uses it.
originalClientRequest = http.ClientRequest
http.ClientRequest = OverriddenClientRequest
debug('ClientRequest overridden')
}
function restoreOverriddenClientRequest() {
debug('restoring overridden ClientRequest')
// Restore the ClientRequest we have overridden.
if (!originalClientRequest) {
debug('- ClientRequest was not overridden')
} else {
http.ClientRequest = originalClientRequest
originalClientRequest = undefined
debug('- ClientRequest restored')
}
}
function isActive() {
// If ClientRequest has been overwritten by Nock then originalClientRequest is not undefined.
// This means that Nock has been activated.
return originalClientRequest !== undefined
}
function interceptorScopes() {
const nestedInterceptors = Object.values(allInterceptors).map(
i => i.interceptors,
)
const scopes = new Set([].concat(...nestedInterceptors).map(i => i.scope))
return [...scopes]
}
function isDone() {
return interceptorScopes().every(scope => scope.isDone())
}
function pendingMocks() {
return [].concat(...interceptorScopes().map(scope => scope.pendingMocks()))
}
function activeMocks() {
return [].concat(...interceptorScopes().map(scope => scope.activeMocks()))
}
function activate() {
if (originalClientRequest) {
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()
}
module.exports = {
addInterceptor,
remove,
removeAll,
removeInterceptor,
isOn,
activate,
isActive,
isDone,
pendingMocks,
activeMocks,
enableNetConnect,
disableNetConnect,
restoreOverriddenClientRequest,
abortPendingRequests: common.removeAllTimers,
}

View file

@ -0,0 +1,355 @@
'use strict'
const debug = require('debug')('nock.request_overrider')
const {
IncomingMessage,
ClientRequest,
request: originalHttpRequest,
} = require('http')
const { request: originalHttpsRequest } = require('https')
const propagate = require('propagate')
const common = require('./common')
const globalEmitter = require('./global_emitter')
const Socket = require('./socket')
const { playbackInterceptor } = require('./playback_interceptor')
function socketOnClose(req) {
debug('socket close')
if (!req.res && !req.socket._hadError) {
// If we don't have a response then we know that the socket
// ended prematurely and we need to emit an error on the request.
req.socket._hadError = true
const err = new Error('socket hang up')
err.code = 'ECONNRESET'
req.emit('error', err)
}
req.emit('close')
}
/**
* Given a group of interceptors, appropriately route an outgoing request.
* Identify which interceptor ought to respond, if any, then delegate to
* `playbackInterceptor()` to consume the request itself.
*/
class InterceptedRequestRouter {
constructor({ req, options, interceptors }) {
this.req = req
this.options = {
// We may be changing the options object and we don't want those changes
// affecting the user so we use a clone of the object.
...options,
// We use lower-case header field names throughout Nock.
headers: common.headersFieldNamesToLowerCase(
options.headers || {},
false,
),
}
this.interceptors = interceptors
this.socket = new Socket(options)
// support setting `timeout` using request `options`
// https://nodejs.org/docs/latest-v12.x/api/http.html#http_http_request_url_options_callback
// any timeout in the request options override any timeout in the agent options.
// per https://github.com/nodejs/node/pull/21204
const timeout =
options.timeout ||
(options.agent && options.agent.options && options.agent.options.timeout)
if (timeout) {
this.socket.setTimeout(timeout)
}
this.response = new IncomingMessage(this.socket)
this.requestBodyBuffers = []
this.playbackStarted = false
// For parity with Node, it's important the socket event is emitted before we begin playback.
// This flag is set when playback is triggered if we haven't yet gotten the
// socket event to indicate that playback should start as soon as it comes in.
this.readyToStartPlaybackOnSocketEvent = false
this.attachToReq()
// Emit a fake socket event on the next tick to mimic what would happen on a real request.
// Some clients listen for a 'socket' event to be emitted before calling end(),
// which causes Nock to hang.
process.nextTick(() => this.connectSocket())
}
attachToReq() {
const { req, options } = this
for (const [name, val] of Object.entries(options.headers)) {
req.setHeader(name.toLowerCase(), val)
}
if (options.auth && !options.headers.authorization) {
req.setHeader(
// We use lower-case header field names throughout Nock.
'authorization',
`Basic ${Buffer.from(options.auth).toString('base64')}`,
)
}
req.path = options.path
req.method = options.method
req.write = (...args) => this.handleWrite(...args)
req.end = (...args) => this.handleEnd(...args)
req.flushHeaders = (...args) => this.handleFlushHeaders(...args)
// https://github.com/nock/nock/issues/256
if (options.headers.expect === '100-continue') {
common.setImmediate(() => {
debug('continue')
req.emit('continue')
})
}
}
connectSocket() {
const { req, socket } = this
if (common.isRequestDestroyed(req)) {
return
}
// ClientRequest.connection is an alias for ClientRequest.socket
// https://nodejs.org/api/http.html#http_request_socket
// https://github.com/nodejs/node/blob/b0f75818f39ed4e6bd80eb7c4010c1daf5823ef7/lib/_http_client.js#L640-L641
// The same Socket is shared between the request and response to mimic native behavior.
req.socket = req.connection = socket
propagate(['error', 'timeout'], socket, req)
socket.on('close', () => socketOnClose(req))
socket.connecting = false
req.emit('socket', socket)
// https://nodejs.org/api/net.html#net_event_connect
socket.emit('connect')
// https://nodejs.org/api/tls.html#tls_event_secureconnect
if (socket.authorized) {
socket.emit('secureConnect')
}
if (this.readyToStartPlaybackOnSocketEvent) {
this.maybeStartPlayback()
}
}
// from docs: When write function is called with empty string or buffer, it does nothing and waits for more input.
// However, actually implementation checks the state of finished and aborted before checking if the first arg is empty.
handleWrite(...args) {
debug('request write')
let [buffer, encoding] = args
const { req } = this
if (req.finished) {
const err = new Error('write after end')
err.code = 'ERR_STREAM_WRITE_AFTER_END'
process.nextTick(() => req.emit('error', err))
// It seems odd to return `true` here, not sure why you'd want to have
// the stream potentially written to more, but it's what Node does.
// https://github.com/nodejs/node/blob/a9270dcbeba4316b1e179b77ecb6c46af5aa8c20/lib/_http_outgoing.js#L662-L665
return true
}
if (req.socket && req.socket.destroyed) {
return false
}
if (!buffer) {
return true
}
if (!Buffer.isBuffer(buffer)) {
buffer = Buffer.from(buffer, encoding)
}
this.requestBodyBuffers.push(buffer)
// writable.write encoding param is optional
// so if callback is present it's the last argument
const callback = args.length > 1 ? args[args.length - 1] : undefined
// can't use instanceof Function because some test runners
// run tests in vm.runInNewContext where Function is not same
// as that in the current context
// https://github.com/nock/nock/pull/1754#issuecomment-571531407
if (typeof callback === 'function') {
callback()
}
common.setImmediate(function () {
req.emit('drain')
})
return false
}
handleEnd(chunk, encoding, callback) {
debug('request end')
const { req } = this
// handle the different overloaded arg signatures
if (typeof chunk === 'function') {
callback = chunk
chunk = null
} else if (typeof encoding === 'function') {
callback = encoding
encoding = null
}
if (typeof callback === 'function') {
req.once('finish', callback)
}
if (chunk) {
req.write(chunk, encoding)
}
req.finished = true
this.maybeStartPlayback()
return req
}
handleFlushHeaders() {
debug('request flushHeaders')
this.maybeStartPlayback()
}
/**
* Set request headers of the given request. This is needed both during the
* routing phase, in case header filters were specified, and during the
* interceptor-playback phase, to correctly pass mocked request headers.
* TODO There are some problems with this; see https://github.com/nock/nock/issues/1718
*/
setHostHeaderUsingInterceptor(interceptor) {
const { req, options } = this
// If a filtered scope is being used we have to use scope's host in the
// header, otherwise 'host' header won't match.
// NOTE: We use lower-case header field names throughout Nock.
const HOST_HEADER = 'host'
if (interceptor.__nock_filteredScope && interceptor.__nock_scopeHost) {
options.headers[HOST_HEADER] = interceptor.__nock_scopeHost
req.setHeader(HOST_HEADER, interceptor.__nock_scopeHost)
} else {
// For all other cases, we always add host header equal to the requested
// host unless it was already defined.
if (options.host && !req.getHeader(HOST_HEADER)) {
let hostHeader = options.host
if (options.port === 80 || options.port === 443) {
hostHeader = hostHeader.split(':')[0]
}
req.setHeader(HOST_HEADER, hostHeader)
}
}
}
maybeStartPlayback() {
const { req, socket, playbackStarted } = this
// In order to get the events in the right order we need to delay playback
// if we get here before the `socket` event is emitted.
if (socket.connecting) {
this.readyToStartPlaybackOnSocketEvent = true
return
}
if (!common.isRequestDestroyed(req) && !playbackStarted) {
this.startPlayback()
}
}
startPlayback() {
debug('ending')
this.playbackStarted = true
const { req, response, socket, options, interceptors } = this
Object.assign(options, {
// Re-update `options` with the current value of `req.path` because badly
// behaving agents like superagent like to change `req.path` mid-flight.
path: req.path,
// Similarly, node-http-proxy will modify headers in flight, so we have
// to put the headers back into options.
// https://github.com/nock/nock/pull/1484
headers: req.getHeaders(),
// Fixes https://github.com/nock/nock/issues/976
protocol: `${options.proto}:`,
})
interceptors.forEach(interceptor => {
this.setHostHeaderUsingInterceptor(interceptor)
})
const requestBodyBuffer = Buffer.concat(this.requestBodyBuffers)
// When request body is a binary buffer we internally use in its hexadecimal
// representation.
const requestBodyIsUtf8Representable =
common.isUtf8Representable(requestBodyBuffer)
const requestBodyString = requestBodyBuffer.toString(
requestBodyIsUtf8Representable ? 'utf8' : 'hex',
)
const matchedInterceptor = interceptors.find(i =>
i.match(req, options, requestBodyString),
)
if (matchedInterceptor) {
matchedInterceptor.scope.logger(
'interceptor identified, starting mocking',
)
matchedInterceptor.markConsumed()
// wait to emit the finish event until we know for sure an Interceptor is going to playback.
// otherwise an unmocked request might emit finish twice.
req.emit('finish')
playbackInterceptor({
req,
socket,
options,
requestBodyString,
requestBodyIsUtf8Representable,
response,
interceptor: matchedInterceptor,
})
} else {
globalEmitter.emit('no match', req, options, requestBodyString)
// Try to find a hostname match that allows unmocked.
const allowUnmocked = interceptors.some(
i => i.matchHostName(options) && i.options.allowUnmocked,
)
if (allowUnmocked && req instanceof ClientRequest) {
const newReq =
options.proto === 'https'
? originalHttpsRequest(options)
: originalHttpRequest(options)
propagate(newReq, req)
// We send the raw buffer as we received it, not as we interpreted it.
newReq.end(requestBodyBuffer)
} else {
const reqStr = common.stringifyRequest(options, requestBodyString)
const err = new Error(`Nock: No match for request ${reqStr}`)
err.code = 'ERR_NOCK_NO_MATCH'
err.statusCode = err.status = 404
req.destroy(err)
}
}
}
}
module.exports = { InterceptedRequestRouter }

View file

@ -0,0 +1,628 @@
'use strict'
const stringify = require('json-stringify-safe')
const querystring = require('querystring')
const { URL, URLSearchParams } = require('url')
const common = require('./common')
const { remove } = require('./intercept')
const matchBody = require('./match_body')
let fs
try {
fs = require('fs')
} catch (err) {
// do nothing, we're in the browser
}
module.exports = class Interceptor {
/**
*
* Valid argument types for `uri`:
* - A string used for strict comparisons with pathname.
* The search portion of the URI may also be postfixed, in which case the search params
* are striped and added via the `query` method.
* - A RegExp instance that tests against only the pathname of requests.
* - A synchronous function bound to this Interceptor instance. It's provided the pathname
* of requests and must return a boolean denoting if the request is considered a match.
*/
constructor(scope, uri, method, requestBody, interceptorOptions) {
const uriIsStr = typeof uri === 'string'
// Check for leading slash. Uri can be either a string or a regexp, but
// When enabled filteringScope ignores the passed URL entirely so we skip validation.
if (
uriIsStr &&
!scope.scopeOptions.filteringScope &&
!scope.basePathname &&
!uri.startsWith('/') &&
!uri.startsWith('*')
) {
throw Error(
`Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything) (got: ${uri})`,
)
}
if (!method) {
throw new Error(
'The "method" parameter is required for an intercept call.',
)
}
this.scope = scope
this.interceptorMatchHeaders = []
this.method = method.toUpperCase()
this.uri = uri
this._key = `${this.method} ${scope.basePath}${scope.basePathname}${
uriIsStr ? '' : '/'
}${uri}`
this.basePath = this.scope.basePath
this.path = uriIsStr ? scope.basePathname + uri : uri
this.queries = null
this.options = interceptorOptions || {}
this.counter = 1
this._requestBody = requestBody
// We use lower-case header field names throughout Nock.
this.reqheaders = common.headersFieldNamesToLowerCase(
scope.scopeOptions.reqheaders || {},
true,
)
this.badheaders = common.headersFieldsArrayToLowerCase(
scope.scopeOptions.badheaders || [],
)
this.delayBodyInMs = 0
this.delayConnectionInMs = 0
this.optional = false
// strip off literal query parameters if they were provided as part of the URI
if (uriIsStr && uri.includes('?')) {
// localhost is a dummy value because the URL constructor errors for only relative inputs
const parsedURL = new URL(this.path, 'http://localhost')
this.path = parsedURL.pathname
this.query(parsedURL.searchParams)
this._key = `${this.method} ${scope.basePath}${this.path}`
}
}
optionally(flag = true) {
// The default behaviour of optionally() with no arguments is to make the mock optional.
if (typeof flag !== 'boolean') {
throw new Error('Invalid arguments: argument should be a boolean')
}
this.optional = flag
return this
}
replyWithError(errorMessage) {
this.errorMessage = errorMessage
this.options = {
...this.scope.scopeOptions,
...this.options,
}
this.scope.add(this._key, this)
return this.scope
}
reply(statusCode, body, rawHeaders) {
// support the format of only passing in a callback
if (typeof statusCode === 'function') {
if (arguments.length > 1) {
// It's not very Javascript-y to throw an error for extra args to a function, but because
// of legacy behavior, this error was added to reduce confusion for those migrating.
throw Error(
'Invalid arguments. When providing a function for the first argument, .reply does not accept other arguments.',
)
}
this.statusCode = null
this.fullReplyFunction = statusCode
} else {
if (statusCode !== undefined && !Number.isInteger(statusCode)) {
throw new Error(`Invalid ${typeof statusCode} value for status code`)
}
this.statusCode = statusCode || 200
if (typeof body === 'function') {
this.replyFunction = body
body = null
}
}
this.options = {
...this.scope.scopeOptions,
...this.options,
}
this.rawHeaders = common.headersInputToRawArray(rawHeaders)
if (this.scope.date) {
// https://tools.ietf.org/html/rfc7231#section-7.1.1.2
this.rawHeaders.push('Date', this.scope.date.toUTCString())
}
// Prepare the headers temporarily so we can make best guesses about content-encoding and content-type
// below as well as while the response is being processed in RequestOverrider.end().
// Including all the default headers is safe for our purposes because of the specific headers we introspect.
// A more thoughtful process is used to merge the default headers when the response headers are finally computed.
this.headers = common.headersArrayToObject(
this.rawHeaders.concat(this.scope._defaultReplyHeaders),
)
// If the content is not encoded we may need to transform the response body.
// Otherwise, we leave it as it is.
if (
body &&
typeof body !== 'string' &&
!Buffer.isBuffer(body) &&
!common.isStream(body) &&
!common.isContentEncoded(this.headers)
) {
try {
body = stringify(body)
} catch (err) {
throw new Error('Error encoding response body into JSON')
}
if (!this.headers['content-type']) {
// https://tools.ietf.org/html/rfc7231#section-3.1.1.5
this.rawHeaders.push('Content-Type', 'application/json')
}
}
if (this.scope.contentLen) {
// https://tools.ietf.org/html/rfc7230#section-3.3.2
if (typeof body === 'string') {
this.rawHeaders.push('Content-Length', body.length)
} else if (Buffer.isBuffer(body)) {
this.rawHeaders.push('Content-Length', body.byteLength)
}
}
this.scope.logger('reply.headers:', this.headers)
this.scope.logger('reply.rawHeaders:', this.rawHeaders)
this.body = body
this.scope.add(this._key, this)
return this.scope
}
replyWithFile(statusCode, filePath, headers) {
if (!fs) {
throw new Error('No fs')
}
this.filePath = filePath
return this.reply(
statusCode,
() => {
const readStream = fs.createReadStream(filePath)
readStream.pause()
return readStream
},
headers,
)
}
// Also match request headers
// https://github.com/nock/nock/issues/163
reqheaderMatches(options, key) {
const reqHeader = this.reqheaders[key]
let header = options.headers[key]
// https://github.com/nock/nock/issues/399
// https://github.com/nock/nock/issues/822
if (header && typeof header !== 'string' && header.toString) {
header = header.toString()
}
// We skip 'host' header comparison unless it's available in both mock and
// actual request. This because 'host' may get inserted by Nock itself and
// then get recorded. NOTE: We use lower-case header field names throughout
// Nock. See https://github.com/nock/nock/pull/196.
if (key === 'host' && (header === undefined || reqHeader === undefined)) {
return true
}
if (reqHeader !== undefined && header !== undefined) {
if (typeof reqHeader === 'function') {
return reqHeader(header)
} else if (common.matchStringOrRegexp(header, reqHeader)) {
return true
}
}
this.scope.logger(
"request header field doesn't match:",
key,
header,
reqHeader,
)
return false
}
match(req, options, body) {
// check if the logger is enabled because the stringifies can be expensive.
if (this.scope.logger.enabled) {
this.scope.logger(
'attempting match %s, body = %s',
stringify(options),
stringify(body),
)
}
const method = (options.method || 'GET').toUpperCase()
let { path = '/' } = options
let matches
let matchKey
const { proto } = options
if (this.method !== method) {
this.scope.logger(
`Method did not match. Request ${method} Interceptor ${this.method}`,
)
return false
}
if (this.scope.transformPathFunction) {
path = this.scope.transformPathFunction(path)
}
const requestMatchesFilter = ({ name, value: predicate }) => {
const headerValue = req.getHeader(name)
if (typeof predicate === 'function') {
return predicate(headerValue)
} else {
return common.matchStringOrRegexp(headerValue, predicate)
}
}
if (
!this.scope.matchHeaders.every(requestMatchesFilter) ||
!this.interceptorMatchHeaders.every(requestMatchesFilter)
) {
this.scope.logger("headers don't match")
return false
}
const reqHeadersMatch = Object.keys(this.reqheaders).every(key =>
this.reqheaderMatches(options, key),
)
if (!reqHeadersMatch) {
this.scope.logger("headers don't match")
return false
}
if (
this.scope.scopeOptions.conditionally &&
!this.scope.scopeOptions.conditionally()
) {
this.scope.logger(
'matching failed because Scope.conditionally() did not validate',
)
return false
}
const badHeaders = this.badheaders.filter(
header => header in options.headers,
)
if (badHeaders.length) {
this.scope.logger('request contains bad headers', ...badHeaders)
return false
}
// Match query strings when using query()
if (this.queries === null) {
this.scope.logger('query matching skipped')
} else {
// can't rely on pathname or search being in the options, but path has a default
const [pathname, search] = path.split('?')
const matchQueries = this.matchQuery({ search })
this.scope.logger(
matchQueries ? 'query matching succeeded' : 'query matching failed',
)
if (!matchQueries) {
return false
}
// If the query string was explicitly checked then subsequent checks against
// the path using a callback or regexp only validate the pathname.
path = pathname
}
// If we have a filtered scope then we use it instead reconstructing the
// scope from the request options (proto, host and port) as these two won't
// necessarily match and we have to remove the scope that was matched (vs.
// that was defined).
if (this.__nock_filteredScope) {
matchKey = this.__nock_filteredScope
} else {
matchKey = common.normalizeOrigin(proto, options.host, options.port)
}
if (typeof this.uri === 'function') {
matches =
common.matchStringOrRegexp(matchKey, this.basePath) &&
// This is a false positive, as `uri` is not bound to `this`.
// eslint-disable-next-line no-useless-call
this.uri.call(this, path)
} else {
matches =
common.matchStringOrRegexp(matchKey, this.basePath) &&
common.matchStringOrRegexp(path, this.path)
}
this.scope.logger(`matching ${matchKey}${path} to ${this._key}: ${matches}`)
if (matches && this._requestBody !== undefined) {
if (this.scope.transformRequestBodyFunction) {
body = this.scope.transformRequestBodyFunction(body, this._requestBody)
}
matches = matchBody(options, this._requestBody, body)
if (!matches) {
this.scope.logger(
"bodies don't match: \n",
this._requestBody,
'\n',
body,
)
}
}
return matches
}
/**
* Return true when the interceptor's method, protocol, host, port, and path
* match the provided options.
*/
matchOrigin(options) {
const isPathFn = typeof this.path === 'function'
const isRegex = this.path instanceof RegExp
const isRegexBasePath = this.scope.basePath instanceof RegExp
const method = (options.method || 'GET').toUpperCase()
let { path } = options
const { proto } = options
// NOTE: Do not split off the query params as the regex could use them
if (!isRegex) {
path = path ? path.split('?')[0] : ''
}
if (this.scope.transformPathFunction) {
path = this.scope.transformPathFunction(path)
}
const comparisonKey = isPathFn || isRegex ? this.__nock_scopeKey : this._key
const matchKey = `${method} ${proto}://${options.host}${path}`
if (isPathFn) {
return !!(matchKey.match(comparisonKey) && this.path(path))
}
if (isRegex && !isRegexBasePath) {
return !!matchKey.match(comparisonKey) && this.path.test(path)
}
if (isRegexBasePath) {
return this.scope.basePath.test(matchKey) && !!path.match(this.path)
}
return comparisonKey === matchKey
}
matchHostName(options) {
const { basePath } = this.scope
if (basePath instanceof RegExp) {
return basePath.test(options.hostname)
}
return options.hostname === this.scope.urlParts.hostname
}
matchQuery(options) {
if (this.queries === true) {
return true
}
const reqQueries = querystring.parse(options.search)
this.scope.logger('Interceptor queries: %j', this.queries)
this.scope.logger(' Request queries: %j', reqQueries)
if (typeof this.queries === 'function') {
return this.queries(reqQueries)
}
return common.dataEqual(this.queries, reqQueries)
}
filteringPath(...args) {
this.scope.filteringPath(...args)
return this
}
// TODO filtering by path is valid on the intercept level, but not filtering
// by request body?
markConsumed() {
this.interceptionCounter++
remove(this)
if (!this.scope.shouldPersist() && this.counter < 1) {
this.scope.remove(this._key, this)
}
}
matchHeader(name, value) {
this.interceptorMatchHeaders.push({ name, value })
return this
}
basicAuth({ user, pass = '' }) {
const encoded = Buffer.from(`${user}:${pass}`).toString('base64')
this.matchHeader('authorization', `Basic ${encoded}`)
return this
}
/**
* Set query strings for the interceptor
* @name query
* @param queries Object of query string name,values (accepts regexp values)
* @public
* @example
* // Will match 'http://zombo.com/?q=t'
* nock('http://zombo.com').get('/').query({q: 't'});
*/
query(queries) {
if (this.queries !== null) {
throw Error(`Query parameters have already been defined`)
}
// Allow all query strings to match this route
if (queries === true) {
this.queries = queries
return this
}
if (typeof queries === 'function') {
this.queries = queries
return this
}
let strFormattingFn
if (this.scope.scopeOptions.encodedQueryParams) {
strFormattingFn = common.percentDecode
}
if (queries instanceof URLSearchParams || typeof queries === 'string') {
// Normalize the data into the shape that is matched against.
// Duplicate keys are handled by combining the values into an array.
queries = querystring.parse(queries.toString())
} else if (!common.isPlainObject(queries)) {
throw Error(`Argument Error: ${queries}`)
}
this.queries = {}
for (const [key, value] of Object.entries(queries)) {
const formatted = common.formatQueryValue(key, value, strFormattingFn)
const [formattedKey, formattedValue] = formatted
this.queries[formattedKey] = formattedValue
}
return this
}
/**
* Set number of times will repeat the interceptor
* @name times
* @param newCounter Number of times to repeat (should be > 0)
* @public
* @example
* // Will repeat mock 5 times for same king of request
* nock('http://zombo.com).get('/').times(5).reply(200, 'Ok');
*/
times(newCounter) {
if (newCounter < 1) {
return this
}
this.counter = newCounter
return this
}
/**
* An sugar syntax for times(1)
* @name once
* @see {@link times}
* @public
* @example
* nock('http://zombo.com).get('/').once().reply(200, 'Ok');
*/
once() {
return this.times(1)
}
/**
* An sugar syntax for times(2)
* @name twice
* @see {@link times}
* @public
* @example
* nock('http://zombo.com).get('/').twice().reply(200, 'Ok');
*/
twice() {
return this.times(2)
}
/**
* An sugar syntax for times(3).
* @name thrice
* @see {@link times}
* @public
* @example
* nock('http://zombo.com).get('/').thrice().reply(200, 'Ok');
*/
thrice() {
return this.times(3)
}
/**
* Delay the response by a certain number of ms.
*
* @param {(integer|object)} opts - Number of milliseconds to wait, or an object
* @param {integer} [opts.head] - Number of milliseconds to wait before response is sent
* @param {integer} [opts.body] - Number of milliseconds to wait before response body is sent
* @return {Interceptor} - the current interceptor for chaining
*/
delay(opts) {
let headDelay
let bodyDelay
if (typeof opts === 'number') {
headDelay = opts
bodyDelay = 0
} else if (typeof opts === 'object') {
headDelay = opts.head || 0
bodyDelay = opts.body || 0
} else {
throw new Error(`Unexpected input opts ${opts}`)
}
return this.delayConnection(headDelay).delayBody(bodyDelay)
}
/**
* Delay the response body by a certain number of ms.
*
* @param {integer} ms - Number of milliseconds to wait before response is sent
* @return {Interceptor} - the current interceptor for chaining
*/
delayBody(ms) {
this.delayBodyInMs = ms
return this
}
/**
* Delay the connection by a certain number of ms.
*
* @param {integer} ms - Number of milliseconds to wait
* @return {Interceptor} - the current interceptor for chaining
*/
delayConnection(ms) {
this.delayConnectionInMs = ms
return this
}
}

View file

@ -0,0 +1,84 @@
'use strict'
const querystring = require('querystring')
const common = require('./common')
module.exports = function matchBody(options, spec, body) {
if (spec instanceof RegExp) {
return spec.test(body)
}
if (Buffer.isBuffer(spec)) {
const encoding = common.isUtf8Representable(spec) ? 'utf8' : 'hex'
spec = spec.toString(encoding)
}
const contentType = (
(options.headers &&
(options.headers['Content-Type'] || options.headers['content-type'])) ||
''
).toString()
const isMultipart = contentType.includes('multipart')
const isUrlencoded = contentType.includes('application/x-www-form-urlencoded')
// try to transform body to json
let json
if (typeof spec === 'object' || typeof spec === 'function') {
try {
json = JSON.parse(body)
} catch (err) {
// not a valid JSON string
}
if (json !== undefined) {
body = json
} else if (isUrlencoded) {
body = querystring.parse(body)
}
}
if (typeof spec === 'function') {
return spec.call(options, body)
}
// strip line endings from both so that we get a match no matter what OS we are running on
// if Content-Type does not contain 'multipart'
if (!isMultipart && typeof body === 'string') {
body = body.replace(/\r?\n|\r/g, '')
}
if (!isMultipart && typeof spec === 'string') {
spec = spec.replace(/\r?\n|\r/g, '')
}
// Because the nature of URL encoding, all the values in the body must be cast to strings.
// dataEqual does strict checking, so we have to cast the non-regexp values in the spec too.
if (isUrlencoded) {
spec = mapValuesDeep(spec, val => (val instanceof RegExp ? val : `${val}`))
}
return common.dataEqual(spec, body)
}
function mapValues(object, cb) {
const keys = Object.keys(object)
for (const key of keys) {
object[key] = cb(object[key], key, object)
}
return object
}
/**
* Based on lodash issue discussion
* https://github.com/lodash/lodash/issues/1244
*/
function mapValuesDeep(obj, cb) {
if (Array.isArray(obj)) {
return obj.map(v => mapValuesDeep(v, cb))
}
if (common.isPlainObject(obj)) {
return mapValues(obj, v => mapValuesDeep(v, cb))
}
return cb(obj)
}

View file

@ -0,0 +1,327 @@
'use strict'
const stream = require('stream')
const util = require('util')
const zlib = require('zlib')
const debug = require('debug')('nock.playback_interceptor')
const common = require('./common')
function parseJSONRequestBody(req, requestBody) {
if (!requestBody || !common.isJSONContent(req.headers)) {
return requestBody
}
if (common.contentEncoding(req.headers, 'gzip')) {
requestBody = String(zlib.gunzipSync(Buffer.from(requestBody, 'hex')))
} else if (common.contentEncoding(req.headers, 'deflate')) {
requestBody = String(zlib.inflateSync(Buffer.from(requestBody, 'hex')))
}
return JSON.parse(requestBody)
}
function parseFullReplyResult(response, fullReplyResult) {
debug('full response from callback result: %j', fullReplyResult)
if (!Array.isArray(fullReplyResult)) {
throw Error('A single function provided to .reply MUST return an array')
}
if (fullReplyResult.length > 3) {
throw Error(
'The array returned from the .reply callback contains too many values',
)
}
const [status, body = '', headers] = fullReplyResult
if (!Number.isInteger(status)) {
throw new Error(`Invalid ${typeof status} value for status code`)
}
response.statusCode = status
response.rawHeaders.push(...common.headersInputToRawArray(headers))
debug('response.rawHeaders after reply: %j', response.rawHeaders)
return body
}
/**
* Determine which of the default headers should be added to the response.
*
* Don't include any defaults whose case-insensitive keys are already on the response.
*/
function selectDefaultHeaders(existingHeaders, defaultHeaders) {
if (!defaultHeaders.length) {
return [] // return early if we don't need to bother
}
const definedHeaders = new Set()
const result = []
common.forEachHeader(existingHeaders, (_, fieldName) => {
definedHeaders.add(fieldName.toLowerCase())
})
common.forEachHeader(defaultHeaders, (value, fieldName) => {
if (!definedHeaders.has(fieldName.toLowerCase())) {
result.push(fieldName, value)
}
})
return result
}
// Presents a list of Buffers as a Readable
class ReadableBuffers extends stream.Readable {
constructor(buffers, opts = {}) {
super(opts)
this.buffers = buffers
}
_read(_size) {
while (this.buffers.length) {
if (!this.push(this.buffers.shift())) {
return
}
}
this.push(null)
}
}
function convertBodyToStream(body) {
if (common.isStream(body)) {
return body
}
if (body === undefined) {
return new ReadableBuffers([])
}
if (Buffer.isBuffer(body)) {
return new ReadableBuffers([body])
}
if (typeof body !== 'string') {
body = JSON.stringify(body)
}
return new ReadableBuffers([Buffer.from(body)])
}
/**
* Play back an interceptor using the given request and mock response.
*/
function playbackInterceptor({
req,
socket,
options,
requestBodyString,
requestBodyIsUtf8Representable,
response,
interceptor,
}) {
const { logger } = interceptor.scope
function start() {
req.headers = req.getHeaders()
interceptor.scope.emit('request', req, interceptor, requestBodyString)
if (typeof interceptor.errorMessage !== 'undefined') {
let error
if (typeof interceptor.errorMessage === 'object') {
error = interceptor.errorMessage
} else {
error = new Error(interceptor.errorMessage)
}
const delay = interceptor.delayBodyInMs + interceptor.delayConnectionInMs
common.setTimeout(() => req.destroy(error), delay)
return
}
// This will be null if we have a fullReplyFunction,
// in that case status code will be set in `parseFullReplyResult`
response.statusCode = interceptor.statusCode
// Clone headers/rawHeaders to not override them when evaluating later
response.rawHeaders = [...interceptor.rawHeaders]
logger('response.rawHeaders:', response.rawHeaders)
// TODO: MAJOR: Don't tack the request onto the interceptor.
// The only reason we do this is so that it's available inside reply functions.
// It would be better to pass the request as an argument to the functions instead.
// Not adding the req as a third arg now because it should first be decided if (path, body, req)
// is the signature we want to go with going forward.
interceptor.req = req
if (interceptor.replyFunction) {
const parsedRequestBody = parseJSONRequestBody(req, requestBodyString)
let fn = interceptor.replyFunction
if (fn.length === 3) {
// Handle the case of an async reply function, the third parameter being the callback.
fn = util.promisify(fn)
}
// At this point `fn` is either a synchronous function or a promise-returning function;
// wrapping in `Promise.resolve` makes it into a promise either way.
Promise.resolve(fn.call(interceptor, options.path, parsedRequestBody))
.then(continueWithResponseBody)
.catch(err => req.destroy(err))
return
}
if (interceptor.fullReplyFunction) {
const parsedRequestBody = parseJSONRequestBody(req, requestBodyString)
let fn = interceptor.fullReplyFunction
if (fn.length === 3) {
fn = util.promisify(fn)
}
Promise.resolve(fn.call(interceptor, options.path, parsedRequestBody))
.then(continueWithFullResponse)
.catch(err => req.destroy(err))
return
}
if (
common.isContentEncoded(interceptor.headers) &&
!common.isStream(interceptor.body)
) {
// If the content is encoded we know that the response body *must* be an array
// of response buffers which should be mocked one by one.
// (otherwise decompressions after the first one fails as unzip expects to receive
// buffer by buffer and not one single merged buffer)
const bufferData = Array.isArray(interceptor.body)
? interceptor.body
: [interceptor.body]
const responseBuffers = bufferData.map(data => Buffer.from(data, 'hex'))
const responseBody = new ReadableBuffers(responseBuffers)
continueWithResponseBody(responseBody)
return
}
// If we get to this point, the body is either a string or an object that
// will eventually be JSON stringified.
let responseBody = interceptor.body
// If the request was not UTF8-representable then we assume that the
// response won't be either. In that case we send the response as a Buffer
// object as that's what the client will expect.
if (!requestBodyIsUtf8Representable && typeof responseBody === 'string') {
// Try to create the buffer from the interceptor's body response as hex.
responseBody = Buffer.from(responseBody, 'hex')
// Creating buffers does not necessarily throw errors; check for difference in size.
if (
!responseBody ||
(interceptor.body.length > 0 && responseBody.length === 0)
) {
// We fallback on constructing buffer from utf8 representation of the body.
responseBody = Buffer.from(interceptor.body, 'utf8')
}
}
return continueWithResponseBody(responseBody)
}
function continueWithFullResponse(fullReplyResult) {
let responseBody
try {
responseBody = parseFullReplyResult(response, fullReplyResult)
} catch (err) {
req.destroy(err)
return
}
continueWithResponseBody(responseBody)
}
function prepareResponseHeaders(body) {
const defaultHeaders = [...interceptor.scope._defaultReplyHeaders]
// Include a JSON content type when JSON.stringify is called on the body.
// This is a convenience added by Nock that has no analog in Node. It's added to the
// defaults, so it will be ignored if the caller explicitly provided the header already.
const isJSON =
body !== undefined &&
typeof body !== 'string' &&
!Buffer.isBuffer(body) &&
!common.isStream(body)
if (isJSON) {
defaultHeaders.push('Content-Type', 'application/json')
}
response.rawHeaders.push(
...selectDefaultHeaders(response.rawHeaders, defaultHeaders),
)
// Evaluate functional headers.
common.forEachHeader(response.rawHeaders, (value, fieldName, i) => {
if (typeof value === 'function') {
response.rawHeaders[i + 1] = value(req, response, body)
}
})
response.headers = common.headersArrayToObject(response.rawHeaders)
}
function continueWithResponseBody(rawBody) {
prepareResponseHeaders(rawBody)
const bodyAsStream = convertBodyToStream(rawBody)
bodyAsStream.pause()
// IncomingMessage extends Readable so we can't simply pipe.
bodyAsStream.on('data', function (chunk) {
response.push(chunk)
})
bodyAsStream.on('end', function () {
// https://nodejs.org/dist/latest-v10.x/docs/api/http.html#http_message_complete
response.complete = true
response.push(null)
interceptor.scope.emit('replied', req, interceptor)
})
bodyAsStream.on('error', function (err) {
response.emit('error', err)
})
const { delayBodyInMs, delayConnectionInMs } = interceptor
function respond() {
if (common.isRequestDestroyed(req)) {
return
}
// Even though we've had the response object for awhile at this point,
// we only attach it to the request immediately before the `response`
// event because, as in Node, it alters the error handling around aborts.
req.res = response
response.req = req
logger('emitting response')
req.emit('response', response)
common.setTimeout(() => bodyAsStream.resume(), delayBodyInMs)
}
socket.applyDelay(delayConnectionInMs)
common.setTimeout(respond, delayConnectionInMs)
}
// Calling `start` immediately could take the request all the way to the connection delay
// during a single microtask execution. This setImmediate stalls the playback to ensure the
// correct events are emitted first ('socket', 'finish') and any aborts in the queue or
// called during a 'finish' listener can be called.
common.setImmediate(() => {
if (!common.isRequestDestroyed(req)) {
start()
}
})
}
module.exports = { playbackInterceptor }

View file

@ -0,0 +1,388 @@
'use strict'
const debug = require('debug')('nock.recorder')
const querystring = require('querystring')
const { inspect } = require('util')
const common = require('./common')
const { restoreOverriddenClientRequest } = require('./intercept')
const SEPARATOR = '\n<<<<<<-- cut here -->>>>>>\n'
let recordingInProgress = false
let outputs = []
function getScope(options) {
const { proto, host, port } = common.normalizeRequestOptions(options)
return common.normalizeOrigin(proto, host, port)
}
function getMethod(options) {
return options.method || 'GET'
}
function getBodyFromChunks(chunks, headers) {
// If we have headers and there is content-encoding it means that the body
// shouldn't be merged but instead persisted as an array of hex strings so
// that the response chunks can be mocked one by one.
if (headers && common.isContentEncoded(headers)) {
return {
body: chunks.map(chunk => chunk.toString('hex')),
}
}
const mergedBuffer = Buffer.concat(chunks)
// The merged buffer can be one of three things:
// 1. A UTF-8-representable string buffer which represents a JSON object.
// 2. A UTF-8-representable buffer which doesn't represent a JSON object.
// 3. A non-UTF-8-representable buffer which then has to be recorded as a hex string.
const isUtf8Representable = common.isUtf8Representable(mergedBuffer)
if (isUtf8Representable) {
const maybeStringifiedJson = mergedBuffer.toString('utf8')
try {
return {
isUtf8Representable,
body: JSON.parse(maybeStringifiedJson),
}
} catch (err) {
return {
isUtf8Representable,
body: maybeStringifiedJson,
}
}
} else {
return {
isUtf8Representable,
body: mergedBuffer.toString('hex'),
}
}
}
function generateRequestAndResponseObject({
req,
bodyChunks,
options,
res,
dataChunks,
reqheaders,
}) {
const { body, isUtf8Representable } = getBodyFromChunks(
dataChunks,
res.headers,
)
options.path = req.path
return {
scope: getScope(options),
method: getMethod(options),
path: options.path,
// Is it deliberate that `getBodyFromChunks()` is called a second time?
body: getBodyFromChunks(bodyChunks).body,
status: res.statusCode,
response: body,
rawHeaders: res.rawHeaders,
reqheaders: reqheaders || undefined,
// When content-encoding is enabled, isUtf8Representable is `undefined`,
// so we explicitly check for `false`.
responseIsBinary: isUtf8Representable === false,
}
}
function generateRequestAndResponse({
req,
bodyChunks,
options,
res,
dataChunks,
reqheaders,
}) {
const requestBody = getBodyFromChunks(bodyChunks).body
const responseBody = getBodyFromChunks(dataChunks, res.headers).body
// Remove any query params from options.path so they can be added in the query() function
let { path } = options
const queryIndex = req.path.indexOf('?')
let queryObj = {}
if (queryIndex !== -1) {
// Remove the query from the path
path = path.substring(0, queryIndex)
const queryStr = req.path.slice(queryIndex + 1)
queryObj = querystring.parse(queryStr)
}
// Escape any single quotes in the path as the output uses them
path = path.replace(/'/g, `\\'`)
// Always encode the query parameters when recording.
const encodedQueryObj = {}
for (const key in queryObj) {
const formattedPair = common.formatQueryValue(
key,
queryObj[key],
common.percentEncode,
)
encodedQueryObj[formattedPair[0]] = formattedPair[1]
}
const lines = []
// We want a leading newline.
lines.push('')
const scope = getScope(options)
lines.push(`nock('${scope}', {"encodedQueryParams":true})`)
const methodName = getMethod(options).toLowerCase()
if (requestBody) {
lines.push(` .${methodName}('${path}', ${JSON.stringify(requestBody)})`)
} else {
lines.push(` .${methodName}('${path}')`)
}
Object.entries(reqheaders || {}).forEach(([fieldName, fieldValue]) => {
const safeName = JSON.stringify(fieldName)
const safeValue = JSON.stringify(fieldValue)
lines.push(` .matchHeader(${safeName}, ${safeValue})`)
})
if (queryIndex !== -1) {
lines.push(` .query(${JSON.stringify(encodedQueryObj)})`)
}
const statusCode = res.statusCode.toString()
const stringifiedResponseBody = JSON.stringify(responseBody)
const headers = inspect(res.rawHeaders)
lines.push(` .reply(${statusCode}, ${stringifiedResponseBody}, ${headers});`)
return lines.join('\n')
}
// This module variable is used to identify a unique recording ID in order to skip
// spurious requests that sometimes happen. This problem has been, so far,
// exclusively detected in nock's unit testing where 'checks if callback is specified'
// interferes with other tests as its t.end() is invoked without waiting for request
// to finish (which is the point of the test).
let currentRecordingId = 0
const defaultRecordOptions = {
dont_print: false,
enable_reqheaders_recording: false,
logging: console.log, // eslint-disable-line no-console
output_objects: false,
use_separator: true,
}
function record(recOptions) {
// Trying to start recording with recording already in progress implies an error
// in the recording configuration (double recording makes no sense and used to lead
// to duplicates in output)
if (recordingInProgress) {
throw new Error('Nock recording already in progress')
}
recordingInProgress = true
// Set the new current recording ID and capture its value in this instance of record().
currentRecordingId = currentRecordingId + 1
const thisRecordingId = currentRecordingId
// Originally the parameter was a dont_print boolean flag.
// To keep the existing code compatible we take that case into account.
if (typeof recOptions === 'boolean') {
recOptions = { dont_print: recOptions }
}
recOptions = { ...defaultRecordOptions, ...recOptions }
debug('start recording', thisRecordingId, recOptions)
const {
dont_print: dontPrint,
enable_reqheaders_recording: enableReqHeadersRecording,
logging,
output_objects: outputObjects,
use_separator: useSeparator,
} = recOptions
debug(thisRecordingId, 'restoring overridden requests before new overrides')
// To preserve backward compatibility (starting recording wasn't throwing if nock was already active)
// 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()
// 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 = []
// 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)
}
options._recording = true
const req = overriddenRequest(options, function (res) {
debug(thisRecordingId, 'intercepting', proto, 'request to record')
// We put our 'end' listener to the front of the listener array.
res.once('end', function () {
debug(thisRecordingId, proto, 'intercepted request ended')
let reqheaders
// Ignore request headers completely unless it was explicitly enabled by the user (see README)
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()
common.deleteHeadersField(reqheaders, 'user-agent')
}
const generateFn = outputObjects
? generateRequestAndResponseObject
: generateRequestAndResponse
let out = generateFn({
req,
bodyChunks,
options,
res,
dataChunks,
reqheaders,
})
debug('out:', out)
// Check that the request was made during the current recording.
// If it hasn't then skip it. There is no other simple way to handle
// this as it depends on the timing of requests and responses. Throwing
// will make some recordings/unit tests fail randomly depending on how
// fast/slow the response arrived.
// If you are seeing this error then you need to make sure that all
// the requests made during a single recording session finish before
// ending the same recording session.
if (thisRecordingId !== currentRecordingId) {
debug('skipping recording of an out-of-order request', out)
return
}
outputs.push(out)
if (!dontPrint) {
if (useSeparator) {
if (typeof out !== 'string') {
out = JSON.stringify(out, null, 2)
}
logging(SEPARATOR + out + SEPARATOR)
} else {
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')
// We override both the http and the https modules; when we are
// serializing the request, we need to know which was called.
// By stuffing the state, we can make sure that nock records
// the intended protocol.
if (proto === 'https') {
options.proto = 'https'
}
})
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
})
}
// Restore *all* the overridden http/https modules' properties.
function restore() {
debug(
currentRecordingId,
'restoring all the overridden http/https properties',
)
common.restoreOverriddenRequests()
restoreOverriddenClientRequest()
recordingInProgress = false
}
function clear() {
outputs = []
}
module.exports = {
record,
outputs: () => outputs,
restore,
clear,
}

416
github/codeql-action-v2/node_modules/nock/lib/scope.js generated vendored Normal file
View file

@ -0,0 +1,416 @@
'use strict'
/**
* @module nock/scope
*/
const { addInterceptor, isOn } = require('./intercept')
const common = require('./common')
const assert = require('assert')
const url = require('url')
const debug = require('debug')('nock.scope')
const { EventEmitter } = require('events')
const Interceptor = require('./interceptor')
const { URL, Url: LegacyUrl } = url
let fs
try {
fs = require('fs')
} catch (err) {
// do nothing, we're in the browser
}
/**
* Normalizes the passed url for consistent internal processing
* @param {string|LegacyUrl|URL} u
*/
function normalizeUrl(u) {
if (!(u instanceof URL)) {
if (u instanceof LegacyUrl) {
return normalizeUrl(new URL(url.format(u)))
}
// If the url is invalid, let the URL library report it
return normalizeUrl(new URL(u))
}
if (!/https?:/.test(u.protocol)) {
throw new TypeError(
`Protocol '${u.protocol}' not recognized. This commonly occurs when a hostname and port are included without a protocol, producing a URL that is valid but confusing, and probably not what you want.`,
)
}
return {
href: u.href,
origin: u.origin,
protocol: u.protocol,
username: u.username,
password: u.password,
host: u.host,
hostname:
// strip brackets from IPv6
typeof u.hostname === 'string' && u.hostname.startsWith('[')
? u.hostname.slice(1, -1)
: u.hostname,
port: u.port || (u.protocol === 'http:' ? 80 : 443),
pathname: u.pathname,
search: u.search,
searchParams: u.searchParams,
hash: u.hash,
}
}
/**
* @param {string|RegExp|LegacyUrl|URL} basePath
* @param {Object} options
* @param {boolean} options.allowUnmocked
* @param {string[]} options.badheaders
* @param {function} options.conditionally
* @param {boolean} options.encodedQueryParams
* @param {function} options.filteringScope
* @param {Object} options.reqheaders
* @constructor
*/
class Scope extends EventEmitter {
constructor(basePath, options) {
super()
this.keyedInterceptors = {}
this.interceptors = []
this.transformPathFunction = null
this.transformRequestBodyFunction = null
this.matchHeaders = []
this.scopeOptions = options || {}
this.urlParts = {}
this._persist = false
this.contentLen = false
this.date = null
this.basePath = basePath
this.basePathname = ''
this.port = null
this._defaultReplyHeaders = []
let logNamespace = String(basePath)
if (!(basePath instanceof RegExp)) {
this.urlParts = normalizeUrl(basePath)
this.port = this.urlParts.port
this.basePathname = this.urlParts.pathname.replace(/\/$/, '')
this.basePath = `${this.urlParts.protocol}//${this.urlParts.hostname}:${this.port}`
logNamespace = this.urlParts.host
}
this.logger = debug.extend(logNamespace)
}
add(key, interceptor) {
if (!(key in this.keyedInterceptors)) {
this.keyedInterceptors[key] = []
}
this.keyedInterceptors[key].push(interceptor)
addInterceptor(
this.basePath,
interceptor,
this,
this.scopeOptions,
this.urlParts.hostname,
)
}
remove(key, interceptor) {
if (this._persist) {
return
}
const arr = this.keyedInterceptors[key]
if (arr) {
arr.splice(arr.indexOf(interceptor), 1)
if (arr.length === 0) {
delete this.keyedInterceptors[key]
}
}
}
intercept(uri, method, requestBody, interceptorOptions) {
const ic = new Interceptor(
this,
uri,
method,
requestBody,
interceptorOptions,
)
this.interceptors.push(ic)
return ic
}
get(uri, requestBody, options) {
return this.intercept(uri, 'GET', requestBody, options)
}
post(uri, requestBody, options) {
return this.intercept(uri, 'POST', requestBody, options)
}
put(uri, requestBody, options) {
return this.intercept(uri, 'PUT', requestBody, options)
}
head(uri, requestBody, options) {
return this.intercept(uri, 'HEAD', requestBody, options)
}
patch(uri, requestBody, options) {
return this.intercept(uri, 'PATCH', requestBody, options)
}
merge(uri, requestBody, options) {
return this.intercept(uri, 'MERGE', requestBody, options)
}
delete(uri, requestBody, options) {
return this.intercept(uri, 'DELETE', requestBody, options)
}
options(uri, requestBody, options) {
return this.intercept(uri, 'OPTIONS', requestBody, options)
}
// Returns the list of keys for non-optional Interceptors that haven't been completed yet.
// TODO: This assumes that completed mocks are removed from the keyedInterceptors list
// (when persistence is off). We should change that (and this) in future.
pendingMocks() {
return this.activeMocks().filter(key =>
this.keyedInterceptors[key].some(({ interceptionCounter, optional }) => {
const persistedAndUsed = this._persist && interceptionCounter > 0
return !persistedAndUsed && !optional
}),
)
}
// Returns all keyedInterceptors that are active.
// This includes incomplete interceptors, persisted but complete interceptors, and
// optional interceptors, but not non-persisted and completed interceptors.
activeMocks() {
return Object.keys(this.keyedInterceptors)
}
isDone() {
if (!isOn()) {
return true
}
return this.pendingMocks().length === 0
}
done() {
assert.ok(
this.isDone(),
`Mocks not yet satisfied:\n${this.pendingMocks().join('\n')}`,
)
}
buildFilter() {
const filteringArguments = arguments
if (arguments[0] instanceof RegExp) {
return function (candidate) {
/* istanbul ignore if */
if (typeof candidate !== 'string') {
// Given the way nock is written, it seems like `candidate` will always
// be a string, regardless of what options might be passed to it.
// However the code used to contain a truthiness test of `candidate`.
// The check is being preserved for now.
throw Error(
`Nock internal assertion failed: typeof candidate is ${typeof candidate}. If you encounter this error, please report it as a bug.`,
)
}
return candidate.replace(filteringArguments[0], filteringArguments[1])
}
} else if (typeof arguments[0] === 'function') {
return arguments[0]
}
}
filteringPath() {
this.transformPathFunction = this.buildFilter.apply(this, arguments)
if (!this.transformPathFunction) {
throw new Error(
'Invalid arguments: filtering path should be a function or a regular expression',
)
}
return this
}
filteringRequestBody() {
this.transformRequestBodyFunction = this.buildFilter.apply(this, arguments)
if (!this.transformRequestBodyFunction) {
throw new Error(
'Invalid arguments: filtering request body should be a function or a regular expression',
)
}
return this
}
matchHeader(name, value) {
// We use lower-case header field names throughout Nock.
this.matchHeaders.push({ name: name.toLowerCase(), value })
return this
}
defaultReplyHeaders(headers) {
this._defaultReplyHeaders = common.headersInputToRawArray(headers)
return this
}
persist(flag = true) {
if (typeof flag !== 'boolean') {
throw new Error('Invalid arguments: argument should be a boolean')
}
this._persist = flag
return this
}
/**
* @private
* @returns {boolean}
*/
shouldPersist() {
return this._persist
}
replyContentLength() {
this.contentLen = true
return this
}
replyDate(d) {
this.date = d || new Date()
return this
}
clone() {
return new Scope(this.basePath, this.scopeOptions)
}
}
function loadDefs(path) {
if (!fs) {
throw new Error('No fs')
}
const contents = fs.readFileSync(path)
return JSON.parse(contents)
}
function load(path) {
return define(loadDefs(path))
}
function getStatusFromDefinition(nockDef) {
// Backward compatibility for when `status` was encoded as string in `reply`.
if (nockDef.reply !== undefined) {
const parsedReply = parseInt(nockDef.reply, 10)
if (isNaN(parsedReply)) {
throw Error('`reply`, when present, must be a numeric string')
}
return parsedReply
}
const DEFAULT_STATUS_OK = 200
return nockDef.status || DEFAULT_STATUS_OK
}
function getScopeFromDefinition(nockDef) {
// Backward compatibility for when `port` was part of definition.
if (nockDef.port !== undefined) {
// Include `port` into scope if it doesn't exist.
const options = url.parse(nockDef.scope)
if (options.port === null) {
return `${nockDef.scope}:${nockDef.port}`
} else {
if (parseInt(options.port) !== parseInt(nockDef.port)) {
throw new Error(
'Mismatched port numbers in scope and port properties of nock definition.',
)
}
}
}
return nockDef.scope
}
function tryJsonParse(string) {
try {
return JSON.parse(string)
} catch (err) {
return string
}
}
function define(nockDefs) {
const scopes = []
nockDefs.forEach(function (nockDef) {
const nscope = getScopeFromDefinition(nockDef)
const npath = nockDef.path
if (!nockDef.method) {
throw Error('Method is required')
}
const method = nockDef.method.toLowerCase()
const status = getStatusFromDefinition(nockDef)
const rawHeaders = nockDef.rawHeaders || []
const reqheaders = nockDef.reqheaders || {}
const badheaders = nockDef.badheaders || []
const options = { ...nockDef.options }
// We use request headers for both filtering (see below) and mocking.
// Here we are setting up mocked request headers but we don't want to
// be changing the user's options object so we clone it first.
options.reqheaders = reqheaders
options.badheaders = badheaders
// Response is not always JSON as it could be a string or binary data or
// even an array of binary buffers (e.g. when content is encoded).
let response
if (!nockDef.response) {
response = ''
// TODO: Rename `responseIsBinary` to `responseIsUtf8Representable`.
} else if (nockDef.responseIsBinary) {
response = Buffer.from(nockDef.response, 'hex')
} else {
response =
typeof nockDef.response === 'string'
? tryJsonParse(nockDef.response)
: nockDef.response
}
const scope = new Scope(nscope, options)
// If request headers were specified filter by them.
Object.entries(reqheaders).forEach(([fieldName, value]) => {
scope.matchHeader(fieldName, value)
})
const acceptableFilters = ['filteringRequestBody', 'filteringPath']
acceptableFilters.forEach(filter => {
if (nockDef[filter]) {
scope[filter](nockDef[filter])
}
})
scope
.intercept(npath, method, nockDef.body)
.reply(status, response, rawHeaders)
scopes.push(scope)
})
return scopes
}
module.exports = {
Scope,
load,
loadDefs,
define,
}

108
github/codeql-action-v2/node_modules/nock/lib/socket.js generated vendored Normal file
View file

@ -0,0 +1,108 @@
'use strict'
const { EventEmitter } = require('events')
const debug = require('debug')('nock.socket')
module.exports = class Socket extends EventEmitter {
constructor(options) {
super()
// Pretend this is a TLSSocket
if (options.proto === 'https') {
// https://github.com/nock/nock/issues/158
this.authorized = true
// https://github.com/nock/nock/issues/2147
this.encrypted = true
}
this.bufferSize = 0
this.writableLength = 0
this.writable = true
this.readable = true
this.pending = false
this.destroyed = false
this.connecting = true
// Undocumented flag used by ClientRequest to ensure errors aren't double-fired
this._hadError = false
// Maximum allowed delay. 0 means unlimited.
this.timeout = 0
const ipv6 = options.family === 6
this.remoteFamily = ipv6 ? 'IPv6' : 'IPv4'
this.localAddress = this.remoteAddress = ipv6 ? '::1' : '127.0.0.1'
this.localPort = this.remotePort = parseInt(options.port)
}
setNoDelay() {}
setKeepAlive() {}
resume() {}
ref() {}
unref() {}
write() {}
address() {
return {
port: this.remotePort,
family: this.remoteFamily,
address: this.remoteAddress,
}
}
setTimeout(timeoutMs, fn) {
this.timeout = timeoutMs
if (fn) {
this.once('timeout', fn)
}
return this
}
/**
* Artificial delay that will trip socket timeouts when appropriate.
*
* Doesn't actually wait for time to pass.
* Timeout events don't necessarily end the request.
* While many clients choose to abort the request upon a timeout, Node itself does not.
*/
applyDelay(delayMs) {
if (this.timeout && delayMs > this.timeout) {
debug('socket timeout')
this.emit('timeout')
}
}
getPeerCertificate() {
return Buffer.from(
(Math.random() * 10000 + Date.now()).toString(),
).toString('base64')
}
/**
* Denotes that no more I/O activity should happen on this socket.
*
* The implementation in Node if far more complex as it juggles underlying async streams.
* For the purposes of Nock, we just need it to set some flags and on the first call
* emit a 'close' and optional 'error' event. Both events propagate through the request object.
*/
destroy(err) {
if (this.destroyed) {
return this
}
debug('socket destroy')
this.destroyed = true
this.readable = this.writable = false
this.readableEnded = this.writableFinished = true
process.nextTick(() => {
if (err) {
this._hadError = true
this.emit('error', err)
}
this.emit('close')
})
return this
}
}

89
github/codeql-action-v2/node_modules/nock/package.json generated vendored Normal file
View file

@ -0,0 +1,89 @@
{
"name": "nock",
"description": "HTTP server mocking and expectations library for Node.js",
"tags": [
"Mock",
"HTTP",
"testing",
"isolation"
],
"version": "13.5.6",
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
"repository": {
"type": "git",
"url": "https://github.com/nock/nock.git"
},
"bugs": {
"url": "https://github.com/nock/nock/issues"
},
"engines": {
"node": ">= 10.13"
},
"main": "./index.js",
"types": "types",
"dependencies": {
"debug": "^4.1.0",
"json-stringify-safe": "^5.0.1",
"propagate": "^2.0.0"
},
"devDependencies": {
"@definitelytyped/dtslint": "^0.0.163",
"@sinonjs/fake-timers": "^11.2.2",
"assert-rejects": "^1.0.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"eslint": "^8.8.0",
"eslint-config-prettier": "^9.0.0",
"eslint-config-standard": "^17.0.0-0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-mocha": "^10.0.3",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^6.0.0",
"form-data": "^4.0.0",
"got": "^11.3.0",
"jest": "^29.7.0",
"mocha": "^9.1.3",
"npm-run-all": "^4.1.5",
"nyc": "^15.0.0",
"prettier": "3.2.5",
"proxyquire": "^2.1.0",
"rimraf": "^3.0.0",
"semantic-release": "^24.1.0",
"sinon": "^15.0.1",
"sinon-chai": "^3.7.0",
"typescript": "^5.0.4"
},
"scripts": {
"format:fix": "prettier --write '**/*.{js,json,md,ts,yml,yaml}'",
"format": "prettier --check '**/*.{js,json,md,ts,yml,yaml}'",
"lint": "run-p lint:js lint:ts",
"lint:js": "eslint --cache --cache-location './.cache/eslint' '**/*.js'",
"lint:js:fix": "eslint --cache --cache-location './.cache/eslint' --fix '**/*.js'",
"lint:ts": "dtslint --expectOnly types",
"test": "nyc --reporter=lcov --reporter=text mocha --recursive tests",
"test:coverage": "open coverage/lcov-report/index.html",
"test:jest": "jest tests_jest --detectLeaks"
},
"license": "MIT",
"files": [
"index.js",
"lib",
"types/index.d.ts"
],
"release": {
"branches": [
"+([0-9])?(.{+([0-9]),x}).x",
"main",
"next",
"next-major",
{
"name": "beta",
"prerelease": true
},
{
"name": "alpha",
"prerelease": true
}
]
}
}

View file

@ -0,0 +1,297 @@
// TypeScript Version: 3.5
import { ReadStream } from 'fs'
import { ClientRequest, IncomingMessage, RequestOptions } from 'http'
import { ParsedUrlQuery } from 'querystring'
import { Url, URLSearchParams } from 'url'
export = nock
declare function nock(
basePath: string | RegExp | Url | URL,
options?: nock.Options,
): nock.Scope
declare namespace nock {
function cleanAll(): void
function activate(): void
function isActive(): boolean
function isDone(): boolean
function pendingMocks(): string[]
function activeMocks(): string[]
function removeInterceptor(interceptor: Interceptor | ReqOptions): boolean
function disableNetConnect(): void
function enableNetConnect(
matcher?: string | RegExp | ((host: string) => boolean),
): void
function load(path: string): Scope[]
function loadDefs(path: string): Definition[]
function define(defs: Definition[]): Scope[]
function restore(): void
function abortPendingRequests(): void
let back: Back
let emitter: NodeJS.EventEmitter
let recorder: Recorder
type InterceptFunction = (
uri: string | RegExp | { (uri: string): boolean },
requestBody?: RequestBodyMatcher,
interceptorOptions?: Options,
) => Interceptor
// Essentially valid, decoded JSON with the addition of possible RegExp. TS doesn't currently have
// a great way to represent JSON type data, this data matcher design is based off this comment.
// https://github.com/microsoft/TypeScript/issues/1897#issuecomment-338650717
type DataMatcher =
| boolean
| number
| string
| null
| undefined
| RegExp
| DataMatcherArray
| DataMatcherMap
interface DataMatcherArray extends ReadonlyArray<DataMatcher> {}
interface DataMatcherMap {
[key: string]: DataMatcher
}
type RequestBodyMatcher =
| string
| Buffer
| RegExp
| DataMatcherArray
| DataMatcherMap
| { (body: any): boolean }
type RequestHeaderMatcher =
| string
| RegExp
| { (fieldValue: string): boolean }
type Body = string | Record<string, any> // a string or decoded JSON
type ReplyBody = Body | Buffer | ReadStream
type ReplyHeaderFunction = (
req: ClientRequest,
res: IncomingMessage,
body: string | Buffer,
) => string | string[]
type ReplyHeaderValue = string | string[] | ReplyHeaderFunction
type ReplyHeaders =
| Record<string, ReplyHeaderValue>
| Map<string, ReplyHeaderValue>
| ReplyHeaderValue[]
type StatusCode = number
type ReplyFnResult =
| readonly [StatusCode]
| readonly [StatusCode, ReplyBody]
| readonly [StatusCode, ReplyBody, ReplyHeaders]
interface ReplyFnContext extends Interceptor {
req: ClientRequest & {
headers: Record<string, string>
}
}
interface Scope extends NodeJS.EventEmitter {
get: InterceptFunction
post: InterceptFunction
put: InterceptFunction
head: InterceptFunction
patch: InterceptFunction
merge: InterceptFunction
delete: InterceptFunction
options: InterceptFunction
intercept: (
uri: string | RegExp | { (uri: string): boolean },
method: string,
requestBody?: RequestBodyMatcher,
options?: Options,
) => Interceptor
defaultReplyHeaders(headers: ReplyHeaders): this
matchHeader(name: string, value: RequestHeaderMatcher): this
filteringPath(regex: RegExp, replace: string): this
filteringPath(fn: (path: string) => string): this
filteringRequestBody(regex: RegExp, replace: string): this
filteringRequestBody(
fn: (body: string, recordedBody: string) => string,
): this
persist(flag?: boolean): this
replyContentLength(): this
replyDate(d?: Date): this
done(): void
isDone(): boolean
pendingMocks(): string[]
activeMocks(): string[]
}
interface Interceptor {
query(
matcher:
| boolean
| string
| DataMatcherMap
| URLSearchParams
| { (parsedObj: ParsedUrlQuery): boolean },
): this
// tslint (as of 5.16) is under the impression that the callback types can be unified,
// however, doing so causes the params to lose their inherited types during use.
// the order of the overrides is important for determining the param types in the replay fns.
/* tslint:disable:unified-signatures */
reply(
replyFnWithCallback: (
this: ReplyFnContext,
uri: string,
body: Body,
callback: (
err: NodeJS.ErrnoException | null,
result: ReplyFnResult,
) => void,
) => void,
): Scope
reply(
replyFn: (
this: ReplyFnContext,
uri: string,
body: Body,
) => ReplyFnResult | Promise<ReplyFnResult>,
): Scope
reply(
statusCode: StatusCode,
replyBodyFnWithCallback: (
this: ReplyFnContext,
uri: string,
body: Body,
callback: (
err: NodeJS.ErrnoException | null,
result: ReplyBody,
) => void,
) => void,
headers?: ReplyHeaders,
): Scope
reply(
statusCode: StatusCode,
replyBodyFn: (
this: ReplyFnContext,
uri: string,
body: Body,
) => ReplyBody | Promise<ReplyBody>,
headers?: ReplyHeaders,
): Scope
reply(responseCode?: StatusCode, body?: Body, headers?: ReplyHeaders): Scope
/* tslint:enable:unified-signatures */
replyWithError(errorMessage: string | object): Scope
replyWithFile(
statusCode: StatusCode,
fileName: string,
headers?: ReplyHeaders,
): Scope
matchHeader(name: string, value: RequestHeaderMatcher): this
basicAuth(options: { user: string; pass?: string }): this
times(newCounter: number): this
once(): this
twice(): this
thrice(): this
optionally(flag?: boolean): this
delay(opts: number | { head?: number; body?: number }): this
delayBody(timeMs: number): this
delayConnection(timeMs: number): this
}
interface Options {
allowUnmocked?: boolean
reqheaders?: Record<string, RequestHeaderMatcher>
badheaders?: string[]
filteringScope?: { (scope: string): boolean }
encodedQueryParams?: boolean
}
interface Recorder {
rec(options?: boolean | RecorderOptions): void
clear(): void
play(): string[] | Definition[]
}
interface RecorderOptions {
dont_print?: boolean
output_objects?: boolean
enable_reqheaders_recording?: boolean
logging?: (content: string) => void
use_separator?: boolean
}
interface Definition {
scope: string | RegExp
path: string | RegExp
port?: number | string
method?: string
status?: number
body?: RequestBodyMatcher
reqheaders?: Record<string, RequestHeaderMatcher>
response?: ReplyBody
headers?: ReplyHeaders
options?: Options
}
type BackMode = 'wild' | 'dryrun' | 'record' | 'update' | 'lockdown'
interface Back {
currentMode: BackMode
fixtures: string
setMode(mode: BackMode): void
(fixtureName: string, nockedFn: (nockDone: () => void) => void): void
(
fixtureName: string,
options: BackOptions,
nockedFn: (nockDone: () => void) => void,
): void
(
fixtureName: string,
options?: BackOptions,
): Promise<{
nockDone: () => void
context: BackContext
}>
}
interface InterceptorSurface {
method: string
uri: string
basePath: string
path: string
queries?: string
counter: number
body: string
statusCode: number
optional: boolean
}
interface BackContext {
isLoaded: boolean
scopes: Scope[]
assertScopesFinished(): void
query: InterceptorSurface[]
}
interface BackOptions {
before?: (def: Definition) => void
after?: (scope: Scope) => void
afterRecord?: (defs: Definition[]) => Definition[] | string
recorder?: RecorderOptions
}
}
type ReqOptions = RequestOptions & { proto?: string }