-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
341 lines (301 loc) · 12.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
'use strict'
const path = require('path')
const Module = require('module')
const resolve = require('resolve')
const debug = require('debug')('require-in-the-middle')
const moduleDetailsFromPath = require('module-details-from-path')
// Using the default export is discouraged, but kept for backward compatibility.
// Use this instead:
// const { Hook } = require('require-in-the-middle')
module.exports = Hook
module.exports.Hook = Hook
/**
* Is the given module a "core" module?
* https://nodejs.org/api/modules.html#core-modules
*
* @type {(moduleName: string) => boolean}
*/
let isCore
if (Module.isBuiltin) { // Added in node v18.6.0, v16.17.0
isCore = Module.isBuiltin
} else {
const [major, minor] = process.versions.node.split('.').map(Number)
if (major === 8 && minor < 8) {
// For node versions `[8.0, 8.8)` the "http2" module was built-in but
// behind the `--expose-http2` flag. `resolve` only considers unflagged
// modules to be core: https://github.com/browserify/resolve/issues/139
// However, for `ExportsCache` to work for "http2" we need it to be
// considered core.
isCore = moduleName => {
if (moduleName === 'http2') {
return true
}
// Prefer `resolve.core` lookup to `resolve.isCore(moduleName)` because
// the latter is doing version range matches for every call.
return !!resolve.core[moduleName]
}
} else {
isCore = moduleName => {
// Prefer `resolve.core` lookup to `resolve.isCore(moduleName)` because
// the latter is doing version range matches for every call.
return !!resolve.core[moduleName]
}
}
}
// 'foo/bar.js' or 'foo/bar/index.js' => 'foo/bar'
const normalize = /([/\\]index)?(\.js)?$/
// Cache `onrequire`-patched exports for modules.
//
// Exports for built-in (a.k.a. "core") modules are stored in an internal Map.
//
// Exports for non-core modules are stored on a private field on the `Module`
// object in `require.cache`. This allows users to delete from `require.cache`
// to trigger a re-load (and re-run of the hook's `onrequire`) of a module the
// next time it is required.
// https://nodejs.org/docs/latest/api/all.html#all_modules_requirecache
//
// In some special cases -- e.g. some other `require()` hook swapping out
// `Module._cache` like `@babel/register` -- a non-core module won't be in
// `require.cache`. In that case this falls back to caching on the internal Map.
class ExportsCache {
constructor () {
this._localCache = new Map() // <module filename or id> -> <exports>
this._kRitmExports = Symbol('RitmExports')
}
has (filename, isBuiltin) {
if (this._localCache.has(filename)) {
return true
} else if (!isBuiltin) {
const mod = require.cache[filename]
return !!(mod && this._kRitmExports in mod)
} else {
return false
}
}
get (filename, isBuiltin) {
const cachedExports = this._localCache.get(filename)
if (cachedExports !== undefined) {
return cachedExports
} else if (!isBuiltin) {
const mod = require.cache[filename]
return (mod && mod[this._kRitmExports])
}
}
set (filename, exports, isBuiltin) {
if (isBuiltin) {
this._localCache.set(filename, exports)
} else if (filename in require.cache) {
require.cache[filename][this._kRitmExports] = exports
} else {
debug('non-core module is unexpectedly not in require.cache: "%s"', filename)
this._localCache.set(filename, exports)
}
}
}
function Hook (modules, options, onrequire) {
if ((this instanceof Hook) === false) return new Hook(modules, options, onrequire)
if (typeof modules === 'function') {
onrequire = modules
modules = null
options = null
} else if (typeof options === 'function') {
onrequire = options
options = null
}
if (typeof Module._resolveFilename !== 'function') {
console.error('Error: Expected Module._resolveFilename to be a function (was: %s) - aborting!', typeof Module._resolveFilename)
console.error('Please report this error as an issue related to Node.js %s at %s', process.version, require('./package.json').bugs.url)
return
}
this._cache = new ExportsCache()
this._unhooked = false
this._origRequire = Module.prototype.require
const self = this
const patching = new Set()
const internals = options ? options.internals === true : false
const hasWhitelist = Array.isArray(modules)
debug('registering require hook')
this._require = Module.prototype.require = function (id) {
if (self._unhooked === true) {
// if the patched require function could not be removed because
// someone else patched it after it was patched here, we just
// abort and pass the request onwards to the original require
debug('ignoring require call - module is soft-unhooked')
return self._origRequire.apply(this, arguments)
}
return patchedRequire.call(this, arguments, false)
}
if (typeof process.getBuiltinModule === 'function') {
this._origGetBuiltinModule = process.getBuiltinModule
this._getBuiltinModule = process.getBuiltinModule = function (id) {
if (self._unhooked === true) {
// if the patched process.getBuiltinModule function could not be removed because
// someone else patched it after it was patched here, we just abort and pass the
// request onwards to the original process.getBuiltinModule
debug('ignoring process.getBuiltinModule call - module is soft-unhooked')
return self._origGetBuiltinModule.apply(this, arguments)
}
return patchedRequire.call(this, arguments, true)
}
}
// Preserve the original require/process.getBuiltinModule arguments in `args`
function patchedRequire (args, coreOnly) {
const id = args[0]
const core = isCore(id)
let filename // the string used for caching
if (core) {
filename = id
// If this is a builtin module that can be identified both as 'foo' and
// 'node:foo', then prefer 'foo' as the caching key.
if (id.startsWith('node:')) {
const idWithoutPrefix = id.slice(5)
if (isCore(idWithoutPrefix)) {
filename = idWithoutPrefix
}
}
} else if (coreOnly) {
// `coreOnly` is `true` if this was a call to `process.getBuiltinModule`, in which case
// we don't want to return anything if the requested `id` isn't a core module. Falling
// back to default behaviour, which at the time of this wrting is simply returning `undefined`
debug('call to process.getBuiltinModule with unknown built-in id')
return self._origGetBuiltinModule.apply(this, args)
} else {
try {
filename = Module._resolveFilename(id, this)
} catch (resolveErr) {
// If someone *else* monkey-patches before this monkey-patch, then that
// code might expect `require(someId)` to get through so it can be
// handled, even if `someId` cannot be resolved to a filename. In this
// case, instead of throwing we defer to the underlying `require`.
//
// For example the Azure Functions Node.js worker module does this,
// where `@azure/functions-core` resolves to an internal object.
// https://github.com/Azure/azure-functions-nodejs-worker/blob/v3.5.2/src/setupCoreModule.ts#L46-L54
debug('Module._resolveFilename("%s") threw %j, calling original Module.require', id, resolveErr.message)
return self._origRequire.apply(this, args)
}
}
let moduleName, basedir
debug('processing %s module require(\'%s\'): %s', core === true ? 'core' : 'non-core', id, filename)
// return known patched modules immediately
if (self._cache.has(filename, core) === true) {
debug('returning already patched cached module: %s', filename)
return self._cache.get(filename, core)
}
// Check if this module has a patcher in-progress already.
// Otherwise, mark this module as patching in-progress.
const isPatching = patching.has(filename)
if (isPatching === false) {
patching.add(filename)
}
const exports = coreOnly
? self._origGetBuiltinModule.apply(this, args)
: self._origRequire.apply(this, args)
// If it's already patched, just return it as-is.
if (isPatching === true) {
debug('module is in the process of being patched already - ignoring: %s', filename)
return exports
}
// The module has already been loaded,
// so the patching mark can be cleaned up.
patching.delete(filename)
if (core === true) {
if (hasWhitelist === true && modules.includes(filename) === false) {
debug('ignoring core module not on whitelist: %s', filename)
return exports // abort if module name isn't on whitelist
}
moduleName = filename
} else if (hasWhitelist === true && modules.includes(filename)) {
// whitelist includes the absolute path to the file including extension
const parsedPath = path.parse(filename)
moduleName = parsedPath.name
basedir = parsedPath.dir
} else {
const stat = moduleDetailsFromPath(filename)
if (stat === undefined) {
debug('could not parse filename: %s', filename)
return exports // abort if filename could not be parsed
}
moduleName = stat.name
basedir = stat.basedir
// Ex: require('foo/lib/../bar.js')
// moduleName = 'foo'
// fullModuleName = 'foo/bar'
const fullModuleName = resolveModuleName(stat)
debug('resolved filename to module: %s (id: %s, resolved: %s, basedir: %s)', moduleName, id, fullModuleName, basedir)
let matchFound = false
if (hasWhitelist) {
if (!id.startsWith('.') && modules.includes(id)) {
// Not starting with '.' means `id` is identifying a module path,
// as opposed to a local file path. (Note: I'm not sure about
// absolute paths, but those are handled above.)
// If this `id` is in `modules`, then this could be a match to an
// package "exports" entry point that wouldn't otherwise match below.
moduleName = id
matchFound = true
}
// abort if module name isn't on whitelist
if (!modules.includes(moduleName) && !modules.includes(fullModuleName)) {
return exports
}
if (modules.includes(fullModuleName) && fullModuleName !== moduleName) {
// if we get to this point, it means that we're requiring a whitelisted sub-module
moduleName = fullModuleName
matchFound = true
}
}
if (!matchFound) {
// figure out if this is the main module file, or a file inside the module
let res
try {
res = resolve.sync(moduleName, { basedir })
} catch (e) {
debug('could not resolve module: %s', moduleName)
self._cache.set(filename, exports, core)
return exports // abort if module could not be resolved (e.g. no main in package.json and no index.js file)
}
if (res !== filename) {
// this is a module-internal file
if (internals === true) {
// use the module-relative path to the file, prefixed by original module name
moduleName = moduleName + path.sep + path.relative(basedir, filename)
debug('preparing to process require of internal file: %s', moduleName)
} else {
debug('ignoring require of non-main module file: %s', res)
self._cache.set(filename, exports, core)
return exports // abort if not main module file
}
}
}
}
// ensure that the cache entry is assigned a value before calling
// onrequire, in case calling onrequire requires the same module.
self._cache.set(filename, exports, core)
debug('calling require hook: %s', moduleName)
const patchedExports = onrequire(exports, moduleName, basedir)
self._cache.set(filename, patchedExports, core)
debug('returning module: %s', moduleName)
return patchedExports
}
}
Hook.prototype.unhook = function () {
this._unhooked = true
if (this._require === Module.prototype.require) {
Module.prototype.require = this._origRequire
debug('require unhook successful')
} else {
debug('require unhook unsuccessful')
}
if (process.getBuiltinModule !== undefined) {
if (this._getBuiltinModule === process.getBuiltinModule) {
process.getBuiltinModule = this._origGetBuiltinModule
debug('process.getBuiltinModule unhook successful')
} else {
debug('process.getBuiltinModule unhook unsuccessful')
}
}
}
function resolveModuleName (stat) {
const normalizedPath = path.sep !== '/' ? stat.path.split(path.sep).join('/') : stat.path
return path.posix.join(stat.name, normalizedPath).replace(normalize, '')
}