1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Strip BOM character from JSON files if present

Fixes #1239
This commit is contained in:
Nick O'Leary 2017-05-15 14:21:12 +01:00
parent 9d4139085b
commit f97f92c297
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -122,6 +122,12 @@ function writeFile(path,content) {
}); });
} }
function parseJSON(data) {
if (data.charCodeAt(0) === 0xFEFF) {
data = data.slice(1)
}
return JSON.parse(data);
}
function readFile(path,backupPath,emptyResponse,type) { function readFile(path,backupPath,emptyResponse,type) {
return when.promise(function(resolve) { return when.promise(function(resolve) {
@ -154,7 +160,7 @@ function readFile(path,backupPath,emptyResponse,type) {
} }
} }
try { try {
return resolve(JSON.parse(data)); return resolve(parseJSON(data));
} catch(parseErr) { } catch(parseErr) {
log.warn(log._("storage.localfilesystem.invalid",{type:type})); log.warn(log._("storage.localfilesystem.invalid",{type:type}));
return resolve(emptyResponse); return resolve(emptyResponse);
@ -307,7 +313,7 @@ var localfilesystem = {
fs.readFile(globalSettingsFile,'utf8',function(err,data) { fs.readFile(globalSettingsFile,'utf8',function(err,data) {
if (!err) { if (!err) {
try { try {
return resolve(JSON.parse(data)); return resolve(parseJSON(data));
} catch(err2) { } catch(err2) {
log.trace("Corrupted config detected - resetting"); log.trace("Corrupted config detected - resetting");
} }
@ -327,7 +333,7 @@ var localfilesystem = {
fs.readFile(sessionsFile,'utf8',function(err,data){ fs.readFile(sessionsFile,'utf8',function(err,data){
if (!err) { if (!err) {
try { try {
return resolve(JSON.parse(data)); return resolve(parseJSON(data));
} catch(err2) { } catch(err2) {
log.trace("Corrupted sessions file - resetting"); log.trace("Corrupted sessions file - resetting");
} }