Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/lib/storage/s3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,80 @@ describe("S3StorageClient", () => {
});
});

test("getObjectInfo falls back to a range GET when HEAD omits Content-Length", async () => {
mockSend
.mockResolvedValueOnce({
// HEAD: no ContentLength (proxy stripped it)
ContentType: "text/html",
ETag: '"abc"',
LastModified: new Date("2026-04-10"),
Metadata: {},
})
.mockResolvedValueOnce({
// Range GET: Content-Range tells us the real size
ContentRange: "bytes 0-0/5032",
Body: Readable.from([Buffer.from("x")]),
});
const client = new S3StorageClient({ endpoint: "https://data.source.coop" });
const result = await client.getObjectInfo({
account_id: "acct",
product_id: "prod",
object_path: "dir/index.html",
});

const calls = mockSend.mock.calls.map((c) => c[0]);
expect(calls[0]).toBeInstanceOf(HeadObjectCommand);
expect(calls[1]).toBeInstanceOf(GetObjectCommand);
expect(calls[1].input).toMatchObject({
Bucket: "acct",
Key: "prod/dir/index.html",
Range: "bytes=0-0",
});
expect(result?.size).toBe(5032);
});

test("getObjectInfo falls back to a range GET when ContentLength is 0", async () => {
mockSend
.mockResolvedValueOnce({
ContentLength: 0,
ContentType: "text/html",
ETag: '"abc"',
LastModified: new Date("2026-04-10"),
Metadata: {},
})
.mockResolvedValueOnce({
ContentRange: "bytes 0-0/5032",
Body: Readable.from([Buffer.from("x")]),
});
const client = new S3StorageClient({ endpoint: "https://data.source.coop" });
const result = await client.getObjectInfo({
account_id: "acct",
product_id: "prod",
object_path: "dir/index.html",
});

expect(result?.size).toBe(5032);
});

test("getObjectInfo keeps size as 0 when range GET also fails", async () => {
mockSend
.mockResolvedValueOnce({
ContentType: "text/html",
ETag: '"abc"',
LastModified: new Date("2026-04-10"),
Metadata: {},
})
.mockRejectedValueOnce(new Error("range not supported"));
const client = new S3StorageClient({ endpoint: "https://data.source.coop" });
const result = await client.getObjectInfo({
account_id: "acct",
product_id: "prod",
object_path: "dir/index.html",
});

expect(result?.size).toBe(0);
});

test("getObjectInfo returns null on a NotFound S3ServiceException", async () => {
const actual = jest.requireActual("@aws-sdk/client-s3");
const notFound = new actual.S3ServiceException({
Expand Down
31 changes: 30 additions & 1 deletion src/lib/storage/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,40 @@ export class S3StorageClient {
VersionId: params.versionId,
}),
);

let size = response.ContentLength ?? 0;

// Some proxies omit Content-Length from HEAD responses. Fall back to a
// range GET to read the actual size from Content-Range.
if (!size) {
try {
const rangeResponse = await this.client.send(
new GetObjectCommand({
Bucket: params.account_id,
Key: keyFor(params),
VersionId: params.versionId,
Range: "bytes=0-0",
}),
);
// Content-Range format: "bytes 0-0/TOTAL_SIZE". That header is all
// we need, so destroy the body instead of reading it — a proxy that
// ignores Range and returns the full object shouldn't be downloaded
// just to size it.
const match = rangeResponse.ContentRange?.match(/\/(\d+)$/);
if (match) {
size = parseInt(match[1], 10);
}
(rangeResponse.Body as Readable | undefined)?.destroy();
} catch {
// range request failed; keep size as 0
}
}

return {
id: params.object_path,
product_id: params.product_id,
path: params.object_path,
size: response.ContentLength ?? 0,
size,
mime_type: response.ContentType ?? "",
type: "file",
created_at:
Expand Down