Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: fix stat cache & simpler shebang #26266

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 12 additions & 39 deletions lib/internal/modules/cjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,13 @@ const path = require('path');
const { pathToFileURL } = require('internal/url');
const { URL } = require('url');

const {
CHAR_LINE_FEED,
CHAR_CARRIAGE_RETURN,
CHAR_EXCLAMATION_MARK,
CHAR_HASH,
} = require('internal/constants');

// Invoke with makeRequireFunction(module) where |module| is the Module object
// to use as the context for the require() function.
function makeRequireFunction(mod) {
const Module = mod.constructor;

function require(path) {
try {
exports.requireDepth += 1;
return mod.require(path);
} finally {
exports.requireDepth -= 1;
}
return mod.require(path);
}

function resolve(request, options) {
Expand Down Expand Up @@ -67,31 +55,17 @@ function stripBOM(content) {
*/
function stripShebang(content) {
// Remove shebang
var contLen = content.length;
if (contLen >= 2) {
if (content.charCodeAt(0) === CHAR_HASH &&
content.charCodeAt(1) === CHAR_EXCLAMATION_MARK) {
if (contLen === 2) {
// Exact match
content = '';
} else {
// Find end of shebang line and slice it off
var i = 2;
for (; i < contLen; ++i) {
var code = content.charCodeAt(i);
if (code === CHAR_LINE_FEED || code === CHAR_CARRIAGE_RETURN)
break;
}
if (i === contLen)
content = '';
else {
// Note that this actually includes the newline character(s) in the
// new output. This duplicates the behavior of the regular expression
// that was previously used to replace the shebang line
content = content.slice(i);
}
}
}
if (content.charAt(0) === '#' && content.charAt(1) === '!') {
// Find end of shebang line and slice it off
let index = content.indexOf('\n', 2);
if (index === -1)
return '';
if (content.charAt(index - 1) === '\r')
index--;
// Note that this actually includes the newline character(s) in the
// new output. This duplicates the behavior of the regular expression
// that was previously used to replace the shebang line
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
content = content.slice(index);
}
return content;
}
Expand Down Expand Up @@ -160,7 +134,6 @@ module.exports = exports = {
builtinLibs,
makeRequireFunction,
normalizeReferrerURL,
requireDepth: 0,
stripBOM,
stripShebang
};
27 changes: 14 additions & 13 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const { safeGetenv } = internalBinding('credentials');
const {
makeRequireFunction,
normalizeReferrerURL,
requireDepth,
stripBOM,
stripShebang
} = require('internal/modules/cjs/helpers');
Expand Down Expand Up @@ -85,18 +84,17 @@ const {

const isWindows = process.platform === 'win32';

let requireDepth = 0;
let statCache = new Map();
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
function stat(filename) {
filename = path.toNamespacedPath(filename);
const cache = stat.cache;
if (cache !== null) {
const result = cache.get(filename);
if (result !== undefined) return result;
}
const result = internalModuleStat(filename);
if (cache !== null) cache.set(filename, result);
if (statCache === null) statCache = new Map();
let result = statCache.get(filename);
if (result !== undefined) return result;
result = internalModuleStat(filename);
statCache.set(filename, result);
return result;
}
stat.cache = null;

function updateChildren(parent, child, scan) {
var children = parent && parent.children;
Expand Down Expand Up @@ -710,7 +708,12 @@ Module.prototype.require = function(id) {
throw new ERR_INVALID_ARG_VALUE('id', id,
'must be a non-empty string');
}
return Module._load(id, this, /* isMain */ false);
requireDepth++;
try {
return Module._load(id, this, /* isMain */ false);
} finally {
requireDepth--;
}
};


Expand Down Expand Up @@ -793,8 +796,6 @@ Module.prototype._compile = function(content, filename) {
}
var dirname = path.dirname(filename);
var require = makeRequireFunction(this);
var depth = requireDepth;
if (depth === 0) stat.cache = new Map();
var result;
var exports = this.exports;
var thisValue = exports;
Expand All @@ -806,7 +807,7 @@ Module.prototype._compile = function(content, filename) {
result = compiledWrapper.call(thisValue, exports, require, module,
filename, dirname);
}
if (depth === 0) stat.cache = null;
if (requireDepth === 0) statCache = null;
return result;
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
};

BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
11 changes: 0 additions & 11 deletions test/fixtures/module-require-depth/one.js

This file was deleted.

11 changes: 0 additions & 11 deletions test/fixtures/module-require-depth/two.js

This file was deleted.

16 changes: 0 additions & 16 deletions test/parallel/test-module-require-depth.js

This file was deleted.