Skip to content

Commit bcd27f7

Browse files
refackBethGriggs
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 1c50714 commit bcd27f7

File tree

7 files changed

+21
-66
lines changed

7 files changed

+21
-66
lines changed

‎lib/internal/main/check_syntax.js‎

Lines changed: 1 addition & 5 deletions
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‎

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -111,37 +111,6 @@ function stripBOM(content){
111111
returncontent;
112112
}
113113

114-
/**
115-
* Find end of shebang line and slice it off
116-
*/
117-
functionstripShebang(content){
118-
// Remove shebang
119-
if(content.charAt(0)==='#'&&content.charAt(1)==='!'){
120-
// Find end of shebang line and slice it off
121-
letindex=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-
returncontent;
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-
functionstripShebangOrBOM(content){
139-
if(content.charCodeAt(0)===0xFEFF){
140-
returncontent.slice(1);
141-
}
142-
returnstripShebang(content);
143-
}
144-
145114
constbuiltinLibs=[
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‎

Lines changed: 13 additions & 10 deletions
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
constenableSourceMaps=getOptionValue('--enable-source-maps');
@@ -961,9 +961,9 @@ function wrapSafe(filename, content){
961961
});
962962
}
963963

964-
letcompiledWrapper;
964+
letcompiled;
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
throwerr;
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
constloader=awaitasyncESM.loaderPromise;
993995
returnloader.import(specifier,normalizeReferrerURL(filename));
994996
}
995997
});
996998
}
997999

998-
returncompiledWrapper;
1000+
returncompiled.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+
letmoduleURL;
1009+
letredirects;
10061010
if(manifest){
1007-
constmoduleURL=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
constcompiledWrapper=wrapSafe(filename,content);
10151018

10161019
varinspectorWrapper=null;

‎lib/internal/modules/esm/translators.js‎

Lines changed: 3 additions & 3 deletions
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
constCJSModule=require('internal/modules/cjs/loader').Module;
1819
constinternalURLModule=require('internal/url');
@@ -78,9 +79,8 @@ translators.set('module', async function moduleStrategy(url){
7879
constsource=`${awaitgetSource(url)}`;
7980
maybeCacheSourceMap(url,source);
8081
debug(`Translating StandardModule ${url}`);
81-
const{ ModuleWrap, callbackMap }=internalBinding('module_wrap');
8282
constmodule=newModuleWrap(source,url);
83-
callbackMap.set(module,{
83+
moduleWrap.callbackMap.set(module,{
8484
initializeImportMeta,
8585
importModuleDynamically,
8686
});
Lines changed: 3 additions & 0 deletions
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‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎test/sequential/test-module-loading.js‎

Lines changed: 1 addition & 1 deletion
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)