Skip to content

Commit

Permalink
module: strip byte order marker when loading *.js and *.json files
Browse files Browse the repository at this point in the history
BOMs make V8 raise a 'SyntaxError: Unexpected token ILLEGAL' exception.

Fixes nodejs#1440.
  • Loading branch information
bnoordhuis committed Aug 2, 2011
1 parent 48dcb90 commit 24d4539
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};


Expand Down
1 change: 1 addition & 0 deletions test/fixtures/utf8-bom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 42;
1 change: 1 addition & 0 deletions test/fixtures/utf8-bom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
5 changes: 5 additions & 0 deletions test/simple/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

0 comments on commit 24d4539

Please sign in to comment.