module: standardize strip shebang behaviour
When loading a module, Node needs to finds the end of a shebang comment by searching for a \r or \n character. This behaviour is now standardized into a dedicated internal module function Refs: https://github.com/nodejs/node/issues/12180 PR-URL: https://github.com/nodejs/node/pull/12202 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
a2843f2cf9
commit
f97156623a
6
lib/internal/bootstrap_node.js
vendored
6
lib/internal/bootstrap_node.js
vendored
@ -449,8 +449,10 @@
|
|||||||
const vm = NativeModule.require('vm');
|
const vm = NativeModule.require('vm');
|
||||||
const internalModule = NativeModule.require('internal/module');
|
const internalModule = NativeModule.require('internal/module');
|
||||||
|
|
||||||
// remove shebang and BOM
|
// remove Shebang
|
||||||
source = internalModule.stripBOM(source.replace(/^#!.*/, ''));
|
source = internalModule.stripShebang(source);
|
||||||
|
// remove BOM
|
||||||
|
source = internalModule.stripBOM(source);
|
||||||
// wrap it
|
// wrap it
|
||||||
source = Module.wrap(source);
|
source = Module.wrap(source);
|
||||||
// compile the script, this will throw if it fails
|
// compile the script, this will throw if it fails
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
exports = module.exports = {
|
exports = module.exports = {
|
||||||
makeRequireFunction,
|
makeRequireFunction,
|
||||||
stripBOM,
|
stripBOM,
|
||||||
|
stripShebang,
|
||||||
addBuiltinLibsToObject
|
addBuiltinLibsToObject
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -50,6 +51,40 @@ function stripBOM(content) {
|
|||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find end of shebang line and slice it off
|
||||||
|
*/
|
||||||
|
function stripShebang(content) {
|
||||||
|
// Remove shebang
|
||||||
|
var contLen = content.length;
|
||||||
|
if (contLen >= 2) {
|
||||||
|
if (content.charCodeAt(0) === 35/*#*/ &&
|
||||||
|
content.charCodeAt(1) === 33/*!*/) {
|
||||||
|
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 === 10/*\n*/ || code === 13/*\r*/)
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
exports.builtinLibs = [
|
exports.builtinLibs = [
|
||||||
'assert', 'buffer', 'child_process', 'cluster', 'crypto', 'dgram', 'dns',
|
'assert', 'buffer', 'child_process', 'cluster', 'crypto', 'dgram', 'dns',
|
||||||
'domain', 'events', 'fs', 'http', 'https', 'net', 'os', 'path', 'punycode',
|
'domain', 'events', 'fs', 'http', 'https', 'net', 'os', 'path', 'punycode',
|
||||||
|
@ -537,33 +537,8 @@ var resolvedArgv;
|
|||||||
// the file.
|
// the file.
|
||||||
// Returns exception, if any.
|
// Returns exception, if any.
|
||||||
Module.prototype._compile = function(content, filename) {
|
Module.prototype._compile = function(content, filename) {
|
||||||
// Remove shebang
|
|
||||||
var contLen = content.length;
|
content = internalModule.stripShebang(content);
|
||||||
if (contLen >= 2) {
|
|
||||||
if (content.charCodeAt(0) === 35/*#*/ &&
|
|
||||||
content.charCodeAt(1) === 33/*!*/) {
|
|
||||||
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 === 10/*\n*/ || code === 13/*\r*/)
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// create wrapper function
|
// create wrapper function
|
||||||
var wrapper = Module.wrap(content);
|
var wrapper = Module.wrap(content);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user