Skip to content

Commit 702331b

Browse files
committed
lib: no need to strip BOM or shebang for scripts
PR-URL: #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>
1 parent abfc8ae commit 702331b

File tree

6 files changed

+3
-57
lines changed

6 files changed

+3
-57
lines changed

lib/internal/main/check_syntax.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ const {
1313

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

16-
const {
17-
stripShebangOrBOM,
18-
} = require('internal/modules/cjs/helpers');
19-
2016
const {
2117
Module: {
2218
_resolveFilename: resolveCJSModuleName,
@@ -68,5 +64,5 @@ function checkSyntax(source, filename) {
6864
}
6965
}
7066

71-
wrapSafe(filename, stripShebangOrBOM(source));
67+
wrapSafe(filename, source);
7268
}

lib/internal/modules/cjs/helpers.js

-33
Original file line numberDiff line numberDiff line change
@@ -52,37 +52,6 @@ function stripBOM(content) {
5252
return content;
5353
}
5454

55-
/**
56-
* Find end of shebang line and slice it off
57-
*/
58-
function stripShebang(content) {
59-
// Remove shebang
60-
if (content.charAt(0) === '#' && content.charAt(1) === '!') {
61-
// Find end of shebang line and slice it off
62-
let index = content.indexOf('\n', 2);
63-
if (index === -1)
64-
return '';
65-
if (content.charAt(index - 1) === '\r')
66-
index--;
67-
// Note that this actually includes the newline character(s) in the
68-
// new output. This duplicates the behavior of the regular expression
69-
// that was previously used to replace the shebang line.
70-
content = content.slice(index);
71-
}
72-
return content;
73-
}
74-
75-
// Strip either the shebang or UTF BOM of a file.
76-
// Note that this only processes one. If both occur in
77-
// either order, the one that comes second is not
78-
// significant.
79-
function stripShebangOrBOM(content) {
80-
if (content.charCodeAt(0) === 0xFEFF) {
81-
return content.slice(1);
82-
}
83-
return stripShebang(content);
84-
}
85-
8655
const builtinLibs = [
8756
'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'crypto',
8857
'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https', 'net',
@@ -148,6 +117,4 @@ module.exports = {
148117
makeRequireFunction,
149118
normalizeReferrerURL,
150119
stripBOM,
151-
stripShebang,
152-
stripShebangOrBOM,
153120
};

lib/internal/modules/cjs/loader.js

-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ const {
4040
makeRequireFunction,
4141
normalizeReferrerURL,
4242
stripBOM,
43-
stripShebangOrBOM,
4443
} = require('internal/modules/cjs/helpers');
4544
const { getOptionValue } = require('internal/options');
4645
const preserveSymlinks = getOptionValue('--preserve-symlinks');
@@ -745,9 +744,6 @@ Module.prototype._compile = function(content, filename) {
745744
manifest.assertIntegrity(moduleURL, content);
746745
}
747746

748-
// Strip after manifest integrity check
749-
content = stripShebangOrBOM(content);
750-
751747
const compiledWrapper = wrapSafe(filename, content);
752748

753749
var inspectorWrapper = null;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#!shebang
2+
#!shebang
23
module.exports = 42;

test/parallel/test-internal-modules-strip-shebang.js

-14
This file was deleted.

test/sequential/test-module-loading.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ assert.strictEqual(require('../fixtures/utf8-bom.json'), 42);
355355
// Loading files with BOM + shebang.
356356
// See https://github.com/nodejs/node/issues/27767
357357
assert.throws(() => {
358-
require('../fixtures/utf8-bom-shebang.js');
358+
require('../fixtures/utf8-bom-shebang-shebang.js');
359359
}, { name: 'SyntaxError' });
360360
assert.strictEqual(require('../fixtures/utf8-shebang-bom.js'), 42);
361361

0 commit comments

Comments
 (0)