@lostgradient/hoard
The shared contract lists objects, reads bytes with their opaque quoted ETag, and performs conditional writes. An absent write succeeds only once; an etag write succeeds only when that ETag is current. A stale condition leaves stored bytes unchanged.
createS3ObjectOperations signs S3-compatible fetch requests and additionally exposes conditional remove: a stale ETag preserves the object and a matching ETag removes it. createR2ObjectOperations returns the shared operations plus deleteUnconditionally(key) (the R2ObjectStoreOperations type). R2's binding has no conditional delete, so this delete ignores the object's ETag, succeeds for an absent key, and is named to say so; there is no remove, and a conditional delete is never approximated with a read followed by delete. Conditional R2 writes are verified against a local Miniflare R2 binding.
Presigned uploads to R2
A server can hand a client URLs it uploads to directly, so the bytes never pass through a Worker. Both paths target R2's S3-compatible endpoint (https://<account>.r2.cloudflarestorage.com, region auto) and read no process state: the credentials and the fetch are arguments.
import {
abortMultipartUpload,
completeMultipartUpload,
createSigV4Presigner,
presignObjectUpload,
presignUploadPart,
startMultipartUpload,
} from '@lostgradient/hoard';
const presigner = createSigV4Presigner({
endpoint: `https://${accountId}.r2.cloudflarestorage.com`,
region: 'auto',
accessKeyId,
secretAccessKey,
bucket: 'screenshots',
});
Single part. presignObjectUpload returns a PUT URL. A contentType is part of the signature, so the client must send exactly that Content-Type. expiresIn is a whole number of seconds from 1 to 604800 (default 3600).
const url = presignObjectUpload(presigner, 'runs/42/home.png', {
expiresIn: 900,
contentType: 'image/png',
});
// The client: await fetch(url, { method: 'PUT', headers: { 'content-type': 'image/png' }, body });
Multipart. The server starts the upload, signs one URL per part, and completes (or aborts) it. Starting, completing and aborting are signed requests the server makes itself with fetch; only the parts are uploaded by the client. Part numbers run from 1 to 10000. The client reads each part's ETag response header and returns it to the server.
const options = { presigner }; // optionally { presigner, fetch } to inject a fetch
const uploadId = await startMultipartUpload(options, 'runs/42/video.mp4', {
contentType: 'video/mp4',
});
const partUrl = presignUploadPart(presigner, 'runs/42/video.mp4', { uploadId, partNumber: 1 });
// The client PUTs part bytes to partUrl and reports the ETag it receives.
await completeMultipartUpload(options, 'runs/42/video.mp4', {
uploadId,
parts: [{ partNumber: 1, etag: '"9b2cf535f27731c974343645a3985328"' }],
});
// To discard an upload instead: await abortMultipartUpload(options, key, { uploadId });
An invalid part number, an empty part list, or an expiry over 604800 seconds throws a TypeError before any request. A failed request raises ObjectStoreError, whose message holds no URL, signature or credential.