From 7340c0712b2bd44c145d0f2a5a9f308f39082a3d Mon Sep 17 00:00:00 2001 From: Mr Martian Date: Mon, 3 Oct 2022 17:40:14 -0400 Subject: [PATCH 1/4] remove quotes in onlyIf headers --- ava.config.mjs | 2 +- packages/r2/src/r2Object.ts | 28 ++++++++++++++++++++++------ packages/r2/test/r2Object.spec.ts | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/ava.config.mjs b/ava.config.mjs index 31f165a21..afb9fcc27 100644 --- a/ava.config.mjs +++ b/ava.config.mjs @@ -11,7 +11,7 @@ export default { nonSemVerExperiments: { nextGenConfig: true, }, - files: ["packages/*/test/**/*.spec.ts"], + files: ["packages/r2/test/**/*.spec.ts"], timeout: "5m", nodeArguments: ["--no-warnings", "--experimental-vm-modules"], typescript: { diff --git a/packages/r2/src/r2Object.ts b/packages/r2/src/r2Object.ts index 62d85ef32..25ffcdcf0 100644 --- a/packages/r2/src/r2Object.ts +++ b/packages/r2/src/r2Object.ts @@ -164,10 +164,14 @@ function matchStrings(a: string | string[], b: string): boolean { } // headers can be a list: e.g. ["if-match", "a, b, c"] -> "if-match: [a, b, c]" -function parseHeaderArray(input?: string): undefined | string | string[] { - if (typeof input !== "string") return; - if (!input.includes(",")) return input; - return input.split(",").map((x) => x.trim()); +function parseHeaderArray(input: string): undefined | string | string[] { + // split if comma found, otherwise return input + if (!input.includes(",")) return _stripQuotes(input); + return input.split(",").map((x) => _stripQuotes(x.trim())); +} + +function _stripQuotes(input: string): string { + return input.replaceAll('"', ""); } export function parseOnlyIf( @@ -185,18 +189,30 @@ export function parseOnlyIf( // if string list, convert to array. e.g. 'etagMatches': 'a, b, c' -> ['a', 'b', 'c'] if (typeof onlyIf.etagMatches === "string") { onlyIf.etagMatches = parseHeaderArray(onlyIf.etagMatches); + } else if (Array.isArray(onlyIf.etagMatches)) { + // otherwise if an array, strip the quotes + onlyIf.etagMatches = onlyIf.etagMatches.map((x) => _stripQuotes(x.trim())); } // if string list, convert to array. e.g. 'etagMatches': 'a, b, c' -> ['a', 'b', 'c'] if (typeof onlyIf.etagDoesNotMatch === "string") { onlyIf.etagDoesNotMatch = parseHeaderArray(onlyIf.etagDoesNotMatch); + } else if (Array.isArray(onlyIf.etagDoesNotMatch)) { + // otherwise if an array, strip the quotes + onlyIf.etagDoesNotMatch = onlyIf.etagDoesNotMatch.map((x) => + _stripQuotes(x.trim()) + ); } // if string, convert to date if (typeof onlyIf.uploadedBefore === "string") { - onlyIf.uploadedBefore = new Date(onlyIf.uploadedBefore); + onlyIf.uploadedBefore = new Date( + _stripQuotes(onlyIf.uploadedBefore) as string + ); } // if string, convert to date if (typeof onlyIf.uploadedAfter === "string") { - onlyIf.uploadedAfter = new Date(onlyIf.uploadedAfter); + onlyIf.uploadedAfter = new Date( + _stripQuotes(onlyIf.uploadedAfter) as string + ); } return onlyIf as R2Conditional; diff --git a/packages/r2/test/r2Object.spec.ts b/packages/r2/test/r2Object.spec.ts index 8d37a0231..5575d1e18 100644 --- a/packages/r2/test/r2Object.spec.ts +++ b/packages/r2/test/r2Object.spec.ts @@ -427,6 +427,20 @@ test("R2Object: parseOnlyIf: each parameter is parsed correctly as an R2Conditio t.deepEqual(parsed.uploadedAfter, new Date(0)); }); +test("R2Object: parseOnlyIf with quotes: each parameter is parsed correctly as an R2Conditional object", (t) => { + const r2conditional = { + etagMatches: '"*"', + etagDoesNotMatch: ['"123"', '"456"'], + uploadedBefore: new Date(0), + uploadedAfter: '"1970-01-01T00:00:00.000Z"', + }; + const parsed = parseOnlyIf(r2conditional as any); + t.is(parsed.etagMatches, "*"); + t.deepEqual(parsed.etagDoesNotMatch, ["123", "456"]); + t.deepEqual(parsed.uploadedBefore, new Date(0)); + t.deepEqual(parsed.uploadedAfter, new Date(0)); +}); + test("R2Object: parseOnlyIf: parsing instanceof Headers", (t) => { const r2ConditionalHeaders = new Headers(); // test capitalization From 42036bc42afee2af9f98ab784892d13b18cc1f34 Mon Sep 17 00:00:00 2001 From: Mr Martian Date: Mon, 3 Oct 2022 17:41:20 -0400 Subject: [PATCH 2/4] fix ava --- ava.config.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ava.config.mjs b/ava.config.mjs index afb9fcc27..31f165a21 100644 --- a/ava.config.mjs +++ b/ava.config.mjs @@ -11,7 +11,7 @@ export default { nonSemVerExperiments: { nextGenConfig: true, }, - files: ["packages/r2/test/**/*.spec.ts"], + files: ["packages/*/test/**/*.spec.ts"], timeout: "5m", nodeArguments: ["--no-warnings", "--experimental-vm-modules"], typescript: { From fef0549c81e2366dee499e240a8e92400c5cf6a1 Mon Sep 17 00:00:00 2001 From: Mr Martian Date: Mon, 3 Oct 2022 17:42:34 -0400 Subject: [PATCH 3/4] typings fix --- packages/r2/src/r2Object.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/packages/r2/src/r2Object.ts b/packages/r2/src/r2Object.ts index 25ffcdcf0..3e82ec1d9 100644 --- a/packages/r2/src/r2Object.ts +++ b/packages/r2/src/r2Object.ts @@ -164,7 +164,7 @@ function matchStrings(a: string | string[], b: string): boolean { } // headers can be a list: e.g. ["if-match", "a, b, c"] -> "if-match: [a, b, c]" -function parseHeaderArray(input: string): undefined | string | string[] { +function parseHeaderArray(input: string): string | string[] { // split if comma found, otherwise return input if (!input.includes(",")) return _stripQuotes(input); return input.split(",").map((x) => _stripQuotes(x.trim())); @@ -204,15 +204,11 @@ export function parseOnlyIf( } // if string, convert to date if (typeof onlyIf.uploadedBefore === "string") { - onlyIf.uploadedBefore = new Date( - _stripQuotes(onlyIf.uploadedBefore) as string - ); + onlyIf.uploadedBefore = new Date(_stripQuotes(onlyIf.uploadedBefore)); } // if string, convert to date if (typeof onlyIf.uploadedAfter === "string") { - onlyIf.uploadedAfter = new Date( - _stripQuotes(onlyIf.uploadedAfter) as string - ); + onlyIf.uploadedAfter = new Date(_stripQuotes(onlyIf.uploadedAfter)); } return onlyIf as R2Conditional; From 3904e0cc7c8d82c9d7e78b3e3506c63978e9c9c5 Mon Sep 17 00:00:00 2001 From: Mr Martian Date: Mon, 10 Oct 2022 23:54:53 -0400 Subject: [PATCH 4/4] improve strip function; improve tests; move trim into strip function --- packages/r2/src/r2Object.ts | 19 +++++++++++-------- packages/r2/test/r2Object.spec.ts | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/r2/src/r2Object.ts b/packages/r2/src/r2Object.ts index 3e82ec1d9..e3f6ebdfc 100644 --- a/packages/r2/src/r2Object.ts +++ b/packages/r2/src/r2Object.ts @@ -166,12 +166,15 @@ function matchStrings(a: string | string[], b: string): boolean { // headers can be a list: e.g. ["if-match", "a, b, c"] -> "if-match: [a, b, c]" function parseHeaderArray(input: string): string | string[] { // split if comma found, otherwise return input - if (!input.includes(",")) return _stripQuotes(input); - return input.split(",").map((x) => _stripQuotes(x.trim())); + if (!input.includes(",")) return stripQuotes(input); + return input.split(",").map((x) => stripQuotes(x)); } -function _stripQuotes(input: string): string { - return input.replaceAll('"', ""); +function stripQuotes(input: string): string { + input = input.trim(); + if (input[0] === '"') input = input.slice(1); + if (input[input.length - 1] === '"') input = input.slice(0, -1); + return input; } export function parseOnlyIf( @@ -191,7 +194,7 @@ export function parseOnlyIf( onlyIf.etagMatches = parseHeaderArray(onlyIf.etagMatches); } else if (Array.isArray(onlyIf.etagMatches)) { // otherwise if an array, strip the quotes - onlyIf.etagMatches = onlyIf.etagMatches.map((x) => _stripQuotes(x.trim())); + onlyIf.etagMatches = onlyIf.etagMatches.map((x) => stripQuotes(x)); } // if string list, convert to array. e.g. 'etagMatches': 'a, b, c' -> ['a', 'b', 'c'] if (typeof onlyIf.etagDoesNotMatch === "string") { @@ -199,16 +202,16 @@ export function parseOnlyIf( } else if (Array.isArray(onlyIf.etagDoesNotMatch)) { // otherwise if an array, strip the quotes onlyIf.etagDoesNotMatch = onlyIf.etagDoesNotMatch.map((x) => - _stripQuotes(x.trim()) + stripQuotes(x) ); } // if string, convert to date if (typeof onlyIf.uploadedBefore === "string") { - onlyIf.uploadedBefore = new Date(_stripQuotes(onlyIf.uploadedBefore)); + onlyIf.uploadedBefore = new Date(stripQuotes(onlyIf.uploadedBefore)); } // if string, convert to date if (typeof onlyIf.uploadedAfter === "string") { - onlyIf.uploadedAfter = new Date(_stripQuotes(onlyIf.uploadedAfter)); + onlyIf.uploadedAfter = new Date(stripQuotes(onlyIf.uploadedAfter)); } return onlyIf as R2Conditional; diff --git a/packages/r2/test/r2Object.spec.ts b/packages/r2/test/r2Object.spec.ts index 5575d1e18..dd9481206 100644 --- a/packages/r2/test/r2Object.spec.ts +++ b/packages/r2/test/r2Object.spec.ts @@ -431,7 +431,7 @@ test("R2Object: parseOnlyIf with quotes: each parameter is parsed correctly as a const r2conditional = { etagMatches: '"*"', etagDoesNotMatch: ['"123"', '"456"'], - uploadedBefore: new Date(0), + uploadedBefore: '"1970-01-01T00:00:00.000Z"', uploadedAfter: '"1970-01-01T00:00:00.000Z"', }; const parsed = parseOnlyIf(r2conditional as any);