lib: no need to strip BOM or shebang for scripts

PR-URL: https://github.com/nodejs/node/pull/27375
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
pull/27375/head
Refael Ackermann 2019-05-27 16:42:03 -04:00
parent abfc8ae894
commit 702331be90
7 changed files with 5 additions and 59 deletions

View File

@ -13,10 +13,6 @@ const {
const { pathToFileURL } = require('url');
const {
stripShebangOrBOM,
} = require('internal/modules/cjs/helpers');
const {
Module: {
_resolveFilename: resolveCJSModuleName,
@ -68,5 +64,5 @@ function checkSyntax(source, filename) {
}
}
wrapSafe(filename, stripShebangOrBOM(source));
wrapSafe(filename, source);
}

View File

@ -52,37 +52,6 @@ function stripBOM(content) {
return content;
}
/**
* Find end of shebang line and slice it off
*/
function stripShebang(content) {
// Remove shebang
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.
content = content.slice(index);
}
return content;
}
// Strip either the shebang or UTF BOM of a file.
// Note that this only processes one. If both occur in
// either order, the one that comes second is not
// significant.
function stripShebangOrBOM(content) {
if (content.charCodeAt(0) === 0xFEFF) {
return content.slice(1);
}
return stripShebang(content);
}
const builtinLibs = [
'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'crypto',
'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https', 'net',
@ -148,6 +117,4 @@ module.exports = {
makeRequireFunction,
normalizeReferrerURL,
stripBOM,
stripShebang,
stripShebangOrBOM,
};

View File

@ -40,7 +40,6 @@ const {
makeRequireFunction,
normalizeReferrerURL,
stripBOM,
stripShebangOrBOM,
} = require('internal/modules/cjs/helpers');
const { getOptionValue } = require('internal/options');
const preserveSymlinks = getOptionValue('--preserve-symlinks');
@ -745,9 +744,6 @@ Module.prototype._compile = function(content, filename) {
manifest.assertIntegrity(moduleURL, content);
}
// Strip after manifest integrity check
content = stripShebangOrBOM(content);
const compiledWrapper = wrapSafe(filename, content);
var inspectorWrapper = null;

View File

@ -0,0 +1,3 @@
#!shebang
#!shebang
module.exports = 42;

View File

@ -1,2 +0,0 @@
#!shebang
module.exports = 42;

View File

@ -1,14 +0,0 @@
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const stripShebang = require('internal/modules/cjs/helpers').stripShebang;
[
['', ''],
['aa', 'aa'],
['#!', ''],
['#!bin/bash', ''],
['#!bin/bash\naa', '\naa'],
].forEach((i) => assert.strictEqual(stripShebang(i[0]), i[1]));

View File

@ -355,7 +355,7 @@ assert.strictEqual(require('../fixtures/utf8-bom.json'), 42);
// Loading files with BOM + shebang.
// See https://github.com/nodejs/node/issues/27767
assert.throws(() => {
require('../fixtures/utf8-bom-shebang.js');
require('../fixtures/utf8-bom-shebang-shebang.js');
}, { name: 'SyntaxError' });
assert.strictEqual(require('../fixtures/utf8-shebang-bom.js'), 42);