Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 102409c

Browse files
authored
R2 bug fix: remove quotes in onlyIf headers (#403)
* remove quotes in onlyIf headers * fix ava * typings fix * improve strip function; improve tests; move trim into strip function
1 parent 8634653 commit 102409c

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

packages/r2/src/r2Object.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,17 @@ function matchStrings(a: string | string[], b: string): boolean {
164164
}
165165

166166
// headers can be a list: e.g. ["if-match", "a, b, c"] -> "if-match: [a, b, c]"
167-
function parseHeaderArray(input?: string): undefined | string | string[] {
168-
if (typeof input !== "string") return;
169-
if (!input.includes(",")) return input;
170-
return input.split(",").map((x) => x.trim());
167+
function parseHeaderArray(input: string): string | string[] {
168+
// split if comma found, otherwise return input
169+
if (!input.includes(",")) return stripQuotes(input);
170+
return input.split(",").map((x) => stripQuotes(x));
171+
}
172+
173+
function stripQuotes(input: string): string {
174+
input = input.trim();
175+
if (input[0] === '"') input = input.slice(1);
176+
if (input[input.length - 1] === '"') input = input.slice(0, -1);
177+
return input;
171178
}
172179

173180
export function parseOnlyIf(
@@ -185,18 +192,26 @@ export function parseOnlyIf(
185192
// if string list, convert to array. e.g. 'etagMatches': 'a, b, c' -> ['a', 'b', 'c']
186193
if (typeof onlyIf.etagMatches === "string") {
187194
onlyIf.etagMatches = parseHeaderArray(onlyIf.etagMatches);
195+
} else if (Array.isArray(onlyIf.etagMatches)) {
196+
// otherwise if an array, strip the quotes
197+
onlyIf.etagMatches = onlyIf.etagMatches.map((x) => stripQuotes(x));
188198
}
189199
// if string list, convert to array. e.g. 'etagMatches': 'a, b, c' -> ['a', 'b', 'c']
190200
if (typeof onlyIf.etagDoesNotMatch === "string") {
191201
onlyIf.etagDoesNotMatch = parseHeaderArray(onlyIf.etagDoesNotMatch);
202+
} else if (Array.isArray(onlyIf.etagDoesNotMatch)) {
203+
// otherwise if an array, strip the quotes
204+
onlyIf.etagDoesNotMatch = onlyIf.etagDoesNotMatch.map((x) =>
205+
stripQuotes(x)
206+
);
192207
}
193208
// if string, convert to date
194209
if (typeof onlyIf.uploadedBefore === "string") {
195-
onlyIf.uploadedBefore = new Date(onlyIf.uploadedBefore);
210+
onlyIf.uploadedBefore = new Date(stripQuotes(onlyIf.uploadedBefore));
196211
}
197212
// if string, convert to date
198213
if (typeof onlyIf.uploadedAfter === "string") {
199-
onlyIf.uploadedAfter = new Date(onlyIf.uploadedAfter);
214+
onlyIf.uploadedAfter = new Date(stripQuotes(onlyIf.uploadedAfter));
200215
}
201216

202217
return onlyIf as R2Conditional;

packages/r2/test/r2Object.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,20 @@ test("R2Object: parseOnlyIf: each parameter is parsed correctly as an R2Conditio
427427
t.deepEqual(parsed.uploadedAfter, new Date(0));
428428
});
429429

430+
test("R2Object: parseOnlyIf with quotes: each parameter is parsed correctly as an R2Conditional object", (t) => {
431+
const r2conditional = {
432+
etagMatches: '"*"',
433+
etagDoesNotMatch: ['"123"', '"456"'],
434+
uploadedBefore: '"1970-01-01T00:00:00.000Z"',
435+
uploadedAfter: '"1970-01-01T00:00:00.000Z"',
436+
};
437+
const parsed = parseOnlyIf(r2conditional as any);
438+
t.is(parsed.etagMatches, "*");
439+
t.deepEqual(parsed.etagDoesNotMatch, ["123", "456"]);
440+
t.deepEqual(parsed.uploadedBefore, new Date(0));
441+
t.deepEqual(parsed.uploadedAfter, new Date(0));
442+
});
443+
430444
test("R2Object: parseOnlyIf: parsing instanceof Headers", (t) => {
431445
const r2ConditionalHeaders = new Headers();
432446
// test capitalization

0 commit comments

Comments
 (0)