Skip to content

Commit b743f2c

Browse files
refacktargos
authored andcommitted
lib: no need to strip BOM or shebang for scripts
Backport-PR-URL: #31228 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]>
1 parent 6c34ad6 commit b743f2c

File tree

7 files changed

+21
-66
lines changed

7 files changed

+21
-66
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
@@ -111,37 +111,6 @@ function stripBOM(content) {
111111
return content;
112112
}
113113

114-
/**
115-
* Find end of shebang line and slice it off
116-
*/
117-
function stripShebang(content) {
118-
// Remove shebang
119-
if (content.charAt(0) === '#' && content.charAt(1) === '!') {
120-
// Find end of shebang line and slice it off
121-
let index = content.indexOf('\n', 2);
122-
if (index === -1)
123-
return '';
124-
if (content.charAt(index - 1) === '\r')
125-
index--;
126-
// Note that this actually includes the newline character(s) in the
127-
// new output. This duplicates the behavior of the regular expression
128-
// that was previously used to replace the shebang line.
129-
content = content.slice(index);
130-
}
131-
return content;
132-
}
133-
134-
// Strip either the shebang or UTF BOM of a file.
135-
// Note that this only processes one. If both occur in
136-
// either order, the one that comes second is not
137-
// significant.
138-
function stripShebangOrBOM(content) {
139-
if (content.charCodeAt(0) === 0xFEFF) {
140-
return content.slice(1);
141-
}
142-
return stripShebang(content);
143-
}
144-
145114
const builtinLibs = [
146115
'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'crypto',
147116
'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https', 'net',
@@ -208,6 +177,4 @@ module.exports = {
208177
makeRequireFunction,
209178
normalizeReferrerURL,
210179
stripBOM,
211-
stripShebang,
212-
stripShebangOrBOM,
213180
};

lib/internal/modules/cjs/loader.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const {
5151
makeRequireFunction,
5252
normalizeReferrerURL,
5353
stripBOM,
54-
stripShebangOrBOM,
54+
loadNativeModule
5555
} = require('internal/modules/cjs/helpers');
5656
const { getOptionValue } = require('internal/options');
5757
const enableSourceMaps = getOptionValue('--enable-source-maps');
@@ -961,9 +961,9 @@ function wrapSafe(filename, content) {
961961
});
962962
}
963963

964-
let compiledWrapper;
964+
let compiled;
965965
try {
966-
compiledWrapper = compileFunction(
966+
compiled = compileFunction(
967967
content,
968968
filename,
969969
0,
@@ -981,36 +981,39 @@ function wrapSafe(filename, content) {
981981
]
982982
);
983983
} catch (err) {
984-
enrichCJSError(err);
984+
if (experimentalModules) {
985+
enrichCJSError(err);
986+
}
985987
throw err;
986988
}
987989

988990
if (experimentalModules) {
989991
const { callbackMap } = internalBinding('module_wrap');
990-
callbackMap.set(compiledWrapper, {
992+
callbackMap.set(compiled.cacheKey, {
991993
importModuleDynamically: async (specifier) => {
992994
const loader = await asyncESM.loaderPromise;
993995
return loader.import(specifier, normalizeReferrerURL(filename));
994996
}
995997
});
996998
}
997999

998-
return compiledWrapper;
1000+
return compiled.function;
9991001
}
10001002

10011003
// Run the file contents in the correct scope or sandbox. Expose
10021004
// the correct helper variables (require, module, exports) to
10031005
// the file.
10041006
// Returns exception, if any.
10051007
Module.prototype._compile = function(content, filename) {
1008+
let moduleURL;
1009+
let redirects;
10061010
if (manifest) {
1007-
const moduleURL = pathToFileURL(filename);
1011+
moduleURL = pathToFileURL(filename);
1012+
redirects = manifest.getRedirector(moduleURL);
10081013
manifest.assertIntegrity(moduleURL, content);
10091014
}
10101015

1011-
// Strip after manifest integrity check
1012-
content = stripShebangOrBOM(content);
1013-
1016+
maybeCacheSourceMap(filename, content, this);
10141017
const compiledWrapper = wrapSafe(filename, content);
10151018

10161019
var inspectorWrapper = null;

lib/internal/modules/esm/translators.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const {
1212
const { Buffer } = require('buffer');
1313

1414
const {
15-
stripBOM
15+
stripBOM,
16+
loadNativeModule
1617
} = require('internal/modules/cjs/helpers');
1718
const CJSModule = require('internal/modules/cjs/loader').Module;
1819
const internalURLModule = require('internal/url');
@@ -78,9 +79,8 @@ translators.set('module', async function moduleStrategy(url) {
7879
const source = `${await getSource(url)}`;
7980
maybeCacheSourceMap(url, source);
8081
debug(`Translating StandardModule ${url}`);
81-
const { ModuleWrap, callbackMap } = internalBinding('module_wrap');
8282
const module = new ModuleWrap(source, url);
83-
callbackMap.set(module, {
83+
moduleWrap.callbackMap.set(module, {
8484
initializeImportMeta,
8585
importModuleDynamically,
8686
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!shebang
2+
#!shebang
3+
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)