Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripped BOM before parsing package.json #11

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion read-json.js
Expand Up @@ -80,14 +80,26 @@ function readJson_ (file, cb) {
})
}


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;
}


function parseJson (file, er, d, cb) {
if (er && er.code === "ENOENT") {
indexjs(file, er, cb)
return
}
if (er) return cb(er);
try {
d = JSON.parse(d)
d = JSON.parse(stripBOM(d))
} catch (er) {
d = parseIndex(d)
if (!d) return cb(parseError(er, file));
Expand Down
19 changes: 19 additions & 0 deletions test/bom.js
@@ -0,0 +1,19 @@
// vim: set softtabstop=16 shiftwidth=16:
var tap = require("tap")
var readJson = require("../")
var path = require("path")
var fs = require("fs")

console.error("BOM test")
tap.test("BOM test", function (t) {
var p = path.resolve(__dirname, "fixtures/bom.json")
readJson(p, function (er, data) {
if (er) throw er;
p = path.resolve(__dirname, "fixtures/nobom.json")
readJson(p, function (er, data2) {
if (er) throw er;
t.deepEqual(data, data2)
t.end()
})
})
})
6 changes: 6 additions & 0 deletions test/fixtures/bom.json
@@ -0,0 +1,6 @@
{
"name": "this",
"description": "file",
"author": "has <filename>",
"version" : "0.0.1"
}
6 changes: 6 additions & 0 deletions test/fixtures/nobom.json
@@ -0,0 +1,6 @@
{
"name": "this",
"description": "file",
"author": "has <filename>",
"version" : "0.0.1"
}