diff --git a/packages/lib/src/ini.ts b/packages/lib/src/ini.ts index 592d7fb1..ecf11052 100644 --- a/packages/lib/src/ini.ts +++ b/packages/lib/src/ini.ts @@ -11,6 +11,11 @@ * @returns sections parsed from the ini file. */ export function parse(input: string) { + // Ignore BOM at the start of the file + if (input[0] === "\uFEFF") { + input = input.slice(1); + } + const sections: Record> = {}; let currentSection: Record = sections as Record; const lines = input.split(/\r\n|\n/); diff --git a/test/file/bom.cfg b/test/file/bom.cfg new file mode 100644 index 00000000..e559eb73 --- /dev/null +++ b/test/file/bom.cfg @@ -0,0 +1,2 @@ +[spam] +foo=bar diff --git a/test/lib/ini.js b/test/lib/ini.js index c7cd39f5..6b0d9697 100644 --- a/test/lib/ini.js +++ b/test/lib/ini.js @@ -1,5 +1,6 @@ "use strict"; const assert = require("assert").strict; +const fs = require("fs-extra"); const lib = require("@clusterio/lib"); describe("lib/ini", function() { @@ -58,6 +59,12 @@ describe("lib/ini", function() { new Error("Duplicated key key on line 2") ); }); + it("should parse a file with BOM", async function() { + assert.deepEqual( + lib.parse(await fs.readFile("test/file/bom.cfg", "utf8")), + { spam: { foo: "bar" }}, + ); + }); }); describe("stringify()", function() { it("should serialize to reference output", function() {