diff --git a/lib/module.js b/lib/module.js index 5a80c0fb2eb7..2f8f02ff387c 100644 --- a/lib/module.js +++ b/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); }; diff --git a/test/fixtures/utf8-bom.js b/test/fixtures/utf8-bom.js new file mode 100644 index 000000000000..0a6f80d74f7a --- /dev/null +++ b/test/fixtures/utf8-bom.js @@ -0,0 +1 @@ +module.exports = 42; diff --git a/test/fixtures/utf8-bom.json b/test/fixtures/utf8-bom.json new file mode 100644 index 000000000000..68264e1ab71f --- /dev/null +++ b/test/fixtures/utf8-bom.json @@ -0,0 +1 @@ +42 diff --git a/test/simple/test-module-loading.js b/test/simple/test-module-loading.js index 823488556f82..7800cfba2faa 100644 --- a/test/simple/test-module-loading.js +++ b/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'));