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

Commit 5ec2972

Browse files
committed
1 parent 3bd4b92 commit 5ec2972

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

packages/tre/src/plugins/r2/gateway.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from "zod";
22
import { Log } from "../../shared";
33
import { RangeStoredValueMeta, Storage } from "../../storage";
4+
import { _parseRanges } from "../shared";
45
import { InvalidRange, NoSuchKey } from "./errors";
56
import {
67
R2Object,
@@ -59,12 +60,26 @@ export class R2Gateway {
5960
key: string,
6061
options: R2GetOptions = {}
6162
): Promise<R2ObjectBody | R2Object> {
62-
const { range = {}, onlyIf } = options;
63-
validate.key(key).condition(await this.head(key), onlyIf);
63+
const meta = await this.head(key);
64+
validate.key(key).condition(meta, options.onlyIf);
65+
66+
let range = options.range ?? {};
67+
if (options.rangeHeader !== undefined) {
68+
const ranges = _parseRanges(options.rangeHeader, meta.size);
69+
if (ranges?.length === 1) {
70+
// If the header contained a single range, convert it to an R2Range.
71+
// Note `start` and `end` are inclusive.
72+
const [start, end] = ranges[0];
73+
range = { offset: start, length: end - start + 1 };
74+
} else {
75+
// If the header was invalid, or contained multiple ranges, just return
76+
// the full response
77+
range = {};
78+
}
79+
}
6480

6581
let stored: RangeStoredValueMeta<R2ObjectMetadata> | undefined;
6682

67-
// get data dependent upon whether suffix or range exists
6883
try {
6984
stored = await this.storage.getRange<R2ObjectMetadata>(key, range);
7085
} catch {

0 commit comments

Comments
 (0)