新增调试信息

This commit is contained in:
2025-10-27 22:15:25 +08:00
parent ae62457d8c
commit 04642cb2f0
5479 changed files with 683397 additions and 3450 deletions
+162
View File
@@ -0,0 +1,162 @@
/*
Copyright 2014 Google LLC
Copyright 2012-2013 Johannes Ewald
Use of this source code is governed by the MIT License, available in this package's LICENSE file
or at http://opensource.org/licenses/MIT.
*/
const _ = require('lodash');
const fs = require('fs');
const Module = require('module');
const originalWrapper = Module.wrapper.slice(0);
const requizzleWrappers = {
extras: require('./wrappers/extras'),
requirePaths: require('./wrappers/requirepaths'),
strict: require('./wrappers/strict'),
};
function wrap(wrappers, script) {
return wrappers[0] + script + wrappers[1];
}
function replaceWrapper(wrapperObj) {
const joiner = '\n';
const before = wrapperObj.before.join(joiner);
const after = wrapperObj.after.join(joiner);
const wrappers = [originalWrapper[0] + before, after + originalWrapper[1]];
Module.wrap = wrap.bind(null, wrappers);
}
function restoreWrapper() {
Module.wrap = wrap.bind(null, originalWrapper);
}
function createModule(targetPath, parentModule, moduleCache) {
moduleCache[targetPath] = moduleCache[targetPath] || new Module(targetPath, parentModule);
return moduleCache[targetPath];
}
/**
* Wrapper for `require()` to prevent the target module's dependencies from being swizzled.
*
* @param {!Module} targetModule - The module that is being swizzled.
* @param {!function} nodeRequire - The original `require()` method for the target module.
* @param {!string} filepath - The value passed to `require()`.
* @return {!Module} The requested module dependency.
*/
function requireProxy(targetModule, nodeRequire, filepath) {
restoreWrapper();
targetModule.require = nodeRequire;
return nodeRequire.call(targetModule, filepath);
}
/**
* Wrapper for `require()` to swizzle the target module's dependencies, using the same settings as
* the target module.
*
* @param {!Module} targetModule - The module that is being swizzled.
* @param {!Object} opts - The Requizzle options object.
* @param {!string} filepath - The value passed to `require()`.
* @return {!Module} The requested module dependency.
*/
function infectProxy(targetModule, cache, opts, filepath) {
let moduleExports;
// loaded here to avoid circular dependencies
const Requizzle = require('./requizzle');
let requizzle;
opts = _.clone(opts);
opts.parent = targetModule;
requizzle = new Requizzle(opts, cache);
moduleExports = requizzle.requizzle(filepath);
return moduleExports;
}
exports.load = function load(targetPath, parentModule, wrapper, cache, options) {
let nodeRequire;
let targetModule;
// Handle circular requires, and avoid reloading modules unnecessarily
if (cache.module[targetPath]) {
return cache.module[targetPath];
}
targetModule = createModule(targetPath, parentModule, cache.module);
nodeRequire = targetModule.require;
if (options.infect) {
targetModule.require = (filepath) => infectProxy(targetModule, cache, options, filepath);
} else {
targetModule.require = (filepath) => requireProxy(targetModule, nodeRequire, filepath);
}
// update the wrapper before we load the target module
replaceWrapper(wrapper);
targetModule.load(targetModule.id);
// make sure the wrapper is restored even if the target module doesn't load any dependencies
restoreWrapper();
return targetModule;
};
/**
* Check whether the entire module includes a `'use strict'` declaration.
*
* @param {string} src - The source file to check.
* @return {boolean} Set to `true` if the module includes a `use strict` declaration.
*/
function detectStrictMode(src) {
return /^\s*(?:["']use strict["'])[ \t]*(?:[\r\n]|;)/g.test(src);
}
function loadSource(targetPath, sourceCache) {
if (sourceCache[targetPath] === undefined) {
sourceCache[targetPath] = fs.readFileSync(targetPath, 'utf8');
}
return sourceCache[targetPath];
}
exports.createWrapper = function createWrapper(targetPath, parentModule, cache, options) {
let src;
const wrapperObject = {
before: [],
after: [],
};
function add(wrapperFunctions, opts) {
const params = [targetPath, parentModule, opts];
['before', 'after'].forEach((item) => {
const result = wrapperFunctions[item].apply(null, params);
if (result) {
wrapperObject[item].push(result);
}
});
}
// Preserve the module's `use strict` declaration if present
src = loadSource(targetPath, cache.source);
if (detectStrictMode(src) === true) {
add(requizzleWrappers.strict);
}
if (options.requirePaths) {
add(requizzleWrappers.requirePaths, options.requirePaths);
}
if (options.extras) {
add(requizzleWrappers.extras, options.extras);
}
return wrapperObject;
};
+102
View File
@@ -0,0 +1,102 @@
/*
Copyright 2014 Google LLC
Copyright 2012-2013 Johannes Ewald
Use of this source code is governed by the MIT License, available in this package's LICENSE file
or at http://opensource.org/licenses/MIT.
*/
/** @module lib/requizzle */
const loader = require('./loader');
const Module = require('module');
const NATIVE_MODULE_PREFIX = 'node:';
/**
* Function that returns text to swizzle into the module.
*
* @typedef module:lib/requizzle~wrapperFunction
* @type {function}
* @param {string} targetPath - The path to the target module.
* @param {string} parentModulePath - The path to the module that is requiring the target module.
* @return {string} The text to insert before or after the module's source code.
*/
/**
* Options for the wrappers that will be swizzled into the target module.
*
* @typedef module:lib/requizzle~options
* @type {Object}
* @property {Object=} options.extras - Functions that generate text to swizzle into the target
* module.
* @property {module:lib/requizzle~wrapperFunction} options.extras.after - Function that returns
* text to insert after the module's source code.
* @property {module:lib/requizzle~wrapperFunction} options.extras.before - Function that returns
* text to insert before the module's source code.
* @property {(Array.<string>|string)} options.requirePaths - Additional paths to search when
* resolving module paths in the target module.
*/
function isNativeModule(targetPath, parentModule) {
let lookupPaths;
let isNative = false;
if (targetPath.startsWith(NATIVE_MODULE_PREFIX)) {
isNative = true;
} else {
lookupPaths = Module._resolveLookupPaths(targetPath, parentModule, true);
/* istanbul ignore next */
isNative =
lookupPaths === null ||
(lookupPaths.length === 2 && lookupPaths[1].length === 0 && lookupPaths[0] === targetPath);
}
return isNative;
}
/**
* Create a `Requizzle` instance. If you provide options, Requizzle will default to those options
* when you call {@link Requizzle#requizzle}.
*
* @class
* @param {!module:lib/requizzle~options} options - Options for the wrappers that will be swizzled
* into the target module.
* @param {Object=} cache - For internal use.
*/
class Requizzle {
constructor(options, cache) {
this._options = options;
this._cache = cache || {
module: {},
source: {},
};
}
/**
* Load the module, swizzling in the requested changes.
*
* @param {!string} targetPath - The path to the module that will be loaded.
* @return {Module} The swizzled module.
*/
requizzle(targetPath) {
const options = this._options;
const parentModule = options.parent;
let targetModule;
let wrapper;
// Don't interfere with native modules
if (isNativeModule(targetPath, parentModule)) {
return require(targetPath);
}
// Resolve the filename relative to the parent module
targetPath = Module._resolveFilename(targetPath, parentModule);
wrapper = loader.createWrapper(targetPath, parentModule, this._cache, this._options);
targetModule = loader.load(targetPath, parentModule, wrapper, this._cache, this._options);
return targetModule.exports;
}
}
module.exports = Requizzle;
+21
View File
@@ -0,0 +1,21 @@
/*
Copyright 2014 Google LLC
Use of this source code is governed by the MIT License, available in this package's LICENSE file
or at http://opensource.org/licenses/MIT.
*/
function callFunction(targetPath, parentModule, func) {
if (!func) {
return '';
}
return func(targetPath, parentModule);
}
exports.before = function before(targetPath, parentModule, options) {
return callFunction(targetPath, parentModule, options.before);
};
exports.after = function after(targetPath, parentModule, options) {
return callFunction(targetPath, parentModule, options.after);
};
+48
View File
@@ -0,0 +1,48 @@
/*
Copyright 2014 Google LLC
Use of this source code is governed by the MIT License, available in this package's LICENSE file
or at http://opensource.org/licenses/MIT.
*/
const path = require('path');
function resolvePaths({ filepath }, paths) {
if (!paths) {
return [];
}
return paths.slice(0).map((p) => path.resolve(filepath, p));
}
function requirePaths(parentModule, opts) {
const result = {
before: [],
after: [],
};
if (!parentModule) {
return result;
}
if (Array.isArray(opts)) {
result.before = resolvePaths(parentModule, opts);
} else {
result.before = resolvePaths(parentModule, opts.before);
result.after = resolvePaths(parentModule, opts.after);
}
return result;
}
exports.before = function before(targetPath, parentModule, opts) {
const resolvedPaths = requirePaths(parentModule, opts);
return (
`module.paths = ${JSON.stringify(resolvedPaths.before)}.concat(module.paths)` +
`.concat(${JSON.stringify(resolvedPaths.after)}); `
);
};
exports.after = function after() {
return '';
};
+13
View File
@@ -0,0 +1,13 @@
/*
Copyright 2014 Google LLC
Use of this source code is governed by the MIT License, available in this package's LICENSE file
or at http://opensource.org/licenses/MIT.
*/
exports.before = function before() {
return '"use strict";';
};
exports.after = function after() {
return '';
};