Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

module: strip byte order marker when loading *.js and *.json files

BOMs make V8 raise a 'SyntaxError: Unexpected token ILLEGAL' exception.

Fixes #1440.
  • Loading branch information...
commit 24d4539a71a7db2e996a073f9770b47471cd59b0 1 parent 48dcb90
@bnoordhuis authored
View
28 lib/module.js
@@ -416,23 +416,35 @@ Module.prototype._compile = function(content, filename) {
return compiledWrapper.apply(self.exports, args);
};
+
+function stripBOM(content) {
+ // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
+ // because the buffer-to-string conversion in `fs.readFileSync()`
+ // translates it to FEFF, the UTF-16 BOM.
+ if (content.charCodeAt(0) === 0xFEFF) {
+ content = content.slice(1);
+ }
+ return content;
+}
+
+
// Native extension for .js
Module._extensions['.js'] = function(module, filename) {
var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
- module._compile(content, filename);
-};
-
-
-// Native extension for .node
-Module._extensions['.node'] = function(module, filename) {
- process.dlopen(filename, module.exports);
+ module._compile(stripBOM(content), filename);
};
// Native extension for .json
Module._extensions['.json'] = function (module, filename) {
var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
- module.exports = JSON.parse(content);
+ module.exports = JSON.parse(stripBOM(content));
+};
+
+
+//Native extension for .node
+Module._extensions['.node'] = function(module, filename) {
+ process.dlopen(filename, module.exports);
};
View
1  test/fixtures/utf8-bom.js
@@ -0,0 +1 @@
+module.exports = 42;
View
1  test/fixtures/utf8-bom.json
@@ -0,0 +1 @@
+42
View
5 test/simple/test-module-loading.js
@@ -221,3 +221,8 @@ process.addListener('exit', function() {
console.log('exit');
});
+
+
+// #1440 Loading files with a byte order marker.
+assert.equal(42, require('../fixtures/utf8-bom.js'));
+assert.equal(42, require('../fixtures/utf8-bom.json'));
Please sign in to comment.
Something went wrong with that request. Please try again.