From c74eeb9e6b11c5740ca7b714579c5870334b3149 Mon Sep 17 00:00:00 2001 From: Hao Xiang Date: Fri, 19 Oct 2018 18:39:37 +0800 Subject: [PATCH] fix(config loader): deal with config file charset (#525) make sure that config file charset is utf-8, or throw detail to notify. for #465 [Question] SyntaxError: Parsing JSON at for commitizen config failed --- package.json | 1 + src/configLoader/getContent.js | 21 ++++++++++++++++++++- test/fixtures/invalid-charset.json | Bin 0 -> 98 bytes test/tests/configLoader.js | 2 ++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/invalid-charset.json diff --git a/package.json b/package.json index 3d2f0990..1dc58858 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "lodash": "4.17.11", "minimist": "1.2.0", "shelljs": "0.7.6", + "strip-bom": "3.0.0", "strip-json-comments": "2.0.1" }, "babel": { diff --git a/src/configLoader/getContent.js b/src/configLoader/getContent.js index a24cabfe..08918031 100644 --- a/src/configLoader/getContent.js +++ b/src/configLoader/getContent.js @@ -2,6 +2,8 @@ import fs from 'fs'; import path from 'path'; import stripJSONComments from 'strip-json-comments'; +import isUTF8 from 'is-utf8'; +import stripBom from 'strip-bom'; import { getNormalizedConfig } from '../configLoader'; @@ -17,7 +19,7 @@ export default getConfigContent; function readConfigContent (configPath) { const parsedPath = path.parse(configPath) const isRcFile = parsedPath.ext !== '.js' && parsedPath.ext !== '.json'; - const jsonString = fs.readFileSync(configPath, 'utf-8'); + const jsonString = readConfigFileContent(configPath); const parse = isRcFile ? (contents) => JSON.parse(stripJSONComments(contents)) : (contents) => JSON.parse(contents); @@ -61,3 +63,20 @@ function getConfigContent (configPath, baseDirectory) { const content = readConfigContent(resolvedPath); return getNormalizedConfig(configBasename, content); }; + +/** + * Read proper content from config file. + * If the chartset of the config file is not utf-8, one error will be thrown. + * @param {String} configPath + * @return {String} + */ +function readConfigFileContent (configPath) { + + let rawBufContent = fs.readFileSync(configPath); + + if (!isUTF8(rawBufContent)) { + throw new Error(`The config file at "${configPath}" contains invalid charset, expect utf8`); + } + + return stripBom(rawBufContent.toString("utf8")); +} diff --git a/test/fixtures/invalid-charset.json b/test/fixtures/invalid-charset.json new file mode 100644 index 0000000000000000000000000000000000000000..9c8d9573398c73b575867e84e6e1e30b97dff84e GIT binary patch literal 98 zcmezWubP32K>-St7>XJ48FCp?8I%~TfMQAvSzwVoAWH{DT?s=5Ln1>7TwNwmodQE9 ULmopJP&^07N&(7oG1M{u02NCQ1^@s6 literal 0 HcmV?d00001 diff --git a/test/tests/configLoader.js b/test/tests/configLoader.js index 0a33e684..df8892b2 100644 --- a/test/tests/configLoader.js +++ b/test/tests/configLoader.js @@ -13,6 +13,8 @@ describe('configLoader', function () { .to.throw(/parsing json at/i); expect(() => getContent('invalid-json-rc', fixturesPath)) .to.throw(/parsing json at/i); + expect(() => getContent('invalid-charset.json', fixturesPath)) + .to.throw(/contains invalid charset/i); }); it('parses json files with comments', function () {