From a0e0b2696f498e0d7913e8ffd3db5abd025e7085 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Mon, 9 May 2022 12:38:22 +0100 Subject: [PATCH] fix: support Windows line-endings in TOML files (#936) The TOML parser that Wrangler uses crashes if there is a Windows line-ending in a comment. See https://github.com/iarna/iarna-toml/issues/33. According to the TOML spec, we should be able to normalize line-endings as we see fit. See https://toml.io/en/v1.0.0#:~:text=normalize%20newline%20to%20whatever%20makes%20sense. This change normalizes line-endings of TOML strings before parsing to avoid hitting this bug. Fixes https://github.com/cloudflare/wrangler2/issues/915 --- .changeset/wise-lies-visit.md | 15 +++++++++++++++ packages/wrangler/src/__tests__/parse.test.ts | 4 ++++ packages/wrangler/src/parse.ts | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .changeset/wise-lies-visit.md diff --git a/.changeset/wise-lies-visit.md b/.changeset/wise-lies-visit.md new file mode 100644 index 000000000000..6351bcddfe7e --- /dev/null +++ b/.changeset/wise-lies-visit.md @@ -0,0 +1,15 @@ +--- +"wrangler": patch +--- + +fix: support Windows line-endings in TOML files + +The TOML parser that Wrangler uses crashes if there is a Windows line-ending in a comment. +See https://github.com/iarna/iarna-toml/issues/33. + +According to the TOML spec, we should be able to normalize line-endings as we see fit. +See https://toml.io/en/v1.0.0#:~:text=normalize%20newline%20to%20whatever%20makes%20sense. + +This change normalizes line-endings of TOML strings before parsing to avoid hitting this bug. + +Fixes https://github.com/cloudflare/wrangler2/issues/915 diff --git a/packages/wrangler/src/__tests__/parse.test.ts b/packages/wrangler/src/__tests__/parse.test.ts index 2398587baed1..d7175b811888 100644 --- a/packages/wrangler/src/__tests__/parse.test.ts +++ b/packages/wrangler/src/__tests__/parse.test.ts @@ -165,6 +165,10 @@ describe("parseTOML", () => { }); } }); + + it("should cope with Windows line-endings", () => { + expect(parseTOML("# A comment with a Windows line-ending\r\n")).toEqual({}); + }); }); describe("parseJSON", () => { diff --git a/packages/wrangler/src/parse.ts b/packages/wrangler/src/parse.ts index d22c69a97fc2..ec145f8eebee 100644 --- a/packages/wrangler/src/parse.ts +++ b/packages/wrangler/src/parse.ts @@ -77,7 +77,9 @@ type TomlError = Error & { */ export function parseTOML(input: string, file?: string): TOML.JsonMap | never { try { - return TOML.parse(input); + // Normalize CRLF to LF to avoid hitting https://github.com/iarna/iarna-toml/issues/33. + const normalizedInput = input.replace(/\r\n$/g, "\n"); + return TOML.parse(normalizedInput); } catch (err) { const { name, message, line, col } = err as TomlError; if (name !== TOML_ERROR_NAME) {