Skip to content

Commit

Permalink
lib: no need to strip BOM or shebang for scripts
Browse files Browse the repository at this point in the history
PR-URL: #27375
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Ujjwal Sharma <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
refack committed Jun 1, 2019
1 parent abfc8ae commit 702331b
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 57 deletions.
6 changes: 1 addition & 5 deletions lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ const {

const { pathToFileURL } = require('url');

const {
stripShebangOrBOM,
} = require('internal/modules/cjs/helpers');

const {
Module: {
_resolveFilename: resolveCJSModuleName,
Expand Down Expand Up @@ -68,5 +64,5 @@ function checkSyntax(source, filename) {
}
}

wrapSafe(filename, stripShebangOrBOM(source));
wrapSafe(filename, source);
}
33 changes: 0 additions & 33 deletions lib/internal/modules/cjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -148,6 +117,4 @@ module.exports = {
makeRequireFunction,
normalizeReferrerURL,
stripBOM,
stripShebang,
stripShebangOrBOM,
};
4 changes: 0 additions & 4 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const {
makeRequireFunction,
normalizeReferrerURL,
stripBOM,
stripShebangOrBOM,
} = require('internal/modules/cjs/helpers');
const { getOptionValue } = require('internal/options');
const preserveSymlinks = getOptionValue('--preserve-symlinks');
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!shebang
#!shebang
module.exports = 42;
14 changes: 0 additions & 14 deletions test/parallel/test-internal-modules-strip-shebang.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/sequential/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 702331b

Please sign in to comment.