Skip to content
Merged
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Object (blob) storage for [Pegma](https://pegma.dev) components — the byte
counterpart to structured records in `@pegma/storage-core`.

> [!IMPORTANT]
> Storage Blobs is early `0.x`. Packages are published at `0.1.0`; pin exact
> Storage Blobs is early `0.x`. Packages are published at `0.2.0`; pin exact
> versions. The public API is not frozen.

## Why it exists
Expand All @@ -27,10 +27,10 @@ application path.

| Package | Role | Status |
| ------------------------------ | ------------------------------------- | ----------- |
| `@pegma/storage-blobs` | Port, memory store, conformance suite | `0.1.0` npm |
| `@pegma/storage-azure-blob` | Azure Blob Storage adapter | `0.1.0` npm |
| `@pegma/storage-cloudflare-r2` | Cloudflare R2 adapter | `0.1.0` npm |
| `@pegma/storage-s3` | AWS S3 / S3-compatible adapter | `0.1.0` npm |
| `@pegma/storage-blobs` | Port, memory store, conformance suite | `0.2.0` npm |
| `@pegma/storage-azure-blob` | Azure Blob Storage adapter | `0.2.0` npm |
| `@pegma/storage-cloudflare-r2` | Cloudflare R2 adapter | `0.2.0` npm |
| `@pegma/storage-s3` | AWS S3 / S3-compatible adapter | `0.2.0` npm |

## Constraint that shapes everything

Expand Down
8 changes: 5 additions & 3 deletions docs/PROJECT_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## Status

**Stage:** Phase 6 complete. `@pegma/storage-blobs@0.1.2` and the Azure Blob,
Cloudflare R2, and S3 adapters are on npm at the same version with
trusted-publisher provenance from signed annotated release tags.
**Stage:** Phase 6 complete. `@pegma/storage-blobs@0.2.0` and the Azure Blob,
Cloudflare R2, and S3 adapters publish at the same version with
trusted-publisher provenance from signed annotated release tags. `0.2.0` adds
an optional `cacheControl` on put, stored as native object state by every
first-class backend and returned by `head`/`get`.

**Package (first release):** `@pegma/storage-blobs@0.1.0` (with adapters
`@pegma/storage-azure-blob`, `@pegma/storage-cloudflare-r2`,
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/storage-azure-blob/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Azure Blob Storage adapter for [`@pegma/storage-blobs`](../storage-blobs).

> [!IMPORTANT]
> Early `0.x` (`0.1.0` on npm). Pin exact versions. Verified against Azurite in
> Early `0.x` (`0.2.0` on npm). Pin exact versions. Verified against Azurite in
> this monorepo's CI.

## Usage
Expand Down
4 changes: 2 additions & 2 deletions packages/storage-azure-blob/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pegma/storage-azure-blob",
"version": "0.1.2",
"version": "0.2.0",
"description": "Azure Blob Storage adapter for @pegma/storage-blobs.",
"license": "MIT",
"type": "module",
Expand Down Expand Up @@ -30,7 +30,7 @@
},
"dependencies": {
"@azure/storage-blob": "^12.29.1",
"@pegma/storage-blobs": "0.1.2"
"@pegma/storage-blobs": "0.2.0"
},
"scripts": {
"build": "tsc -p tsconfig.json",
Expand Down
21 changes: 20 additions & 1 deletion packages/storage-azure-blob/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
DEFAULT_MAX_OBJECT_BYTES,
assertValidBlobKey,
assertValidBlobPrefix,
assertValidCacheControl,
assertValidContentType,
normalizeUserMetadata,
type BlobGetResult,
Expand Down Expand Up @@ -228,6 +229,7 @@ function toObjectInfo(
props: {
contentLength: number | undefined;
contentType: string | undefined;
cacheControl: string | undefined;
etag: string | undefined;
metadata: Record<string, string> | undefined;
},
Expand All @@ -241,6 +243,11 @@ function toObjectInfo(
key,
size: props.contentLength ?? 0,
contentType: props.contentType ?? DEFAULT_CONTENT_TYPE,
// Azurite reports a cleared cache-control as "" rather than absent.
cacheControl:
props.cacheControl === undefined || props.cacheControl.length === 0
? undefined
: props.cacheControl,
etag: props.etag,
userMetadata: metadataFromAzure(props.metadata),
};
Expand Down Expand Up @@ -316,6 +323,10 @@ export function createAzureBlobStore(
const contentType = assertValidContentType(
putOptions?.contentType ?? DEFAULT_CONTENT_TYPE,
);
const cacheControl =
putOptions?.cacheControl === undefined
? undefined
: assertValidCacheControl(putOptions.cacheControl);
const userMetadata = normalizeUserMetadata(putOptions?.userMetadata);
const limitBytes = assertMaxBytes(putOptions?.maxBytes, maxObjectBytes);
const bytes = await readBody(body, limitBytes);
Expand All @@ -332,12 +343,18 @@ export function createAzureBlobStore(

try {
const uploadOptions: {
blobHTTPHeaders: { blobContentType: string };
blobHTTPHeaders: {
blobContentType: string;
blobCacheControl?: string;
};
metadata?: Record<string, string>;
conditions?: { ifNoneMatch: "*" } | { ifMatch: string };
} = {
blobHTTPHeaders: { blobContentType: contentType },
};
if (cacheControl !== undefined) {
uploadOptions.blobHTTPHeaders.blobCacheControl = cacheControl;
}
if (Object.keys(userMetadata).length > 0) {
uploadOptions.metadata = { ...userMetadata };
}
Expand Down Expand Up @@ -409,6 +426,7 @@ export function createAzureBlobStore(
const info = toObjectInfo(key, {
contentLength: download.contentLength,
contentType: download.contentType,
cacheControl: download.cacheControl,
etag: download.etag,
metadata: download.metadata,
});
Expand Down Expand Up @@ -440,6 +458,7 @@ export function createAzureBlobStore(
return toObjectInfo(key, {
contentLength: props.contentLength,
contentType: props.contentType,
cacheControl: props.cacheControl,
etag: props.etag,
metadata: props.metadata,
});
Expand Down
15 changes: 13 additions & 2 deletions packages/storage-blobs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ components: opaque keys, streaming put/get, conditional writes, bounded prefix
listing, and a conformance suite that every adapter must pass.

> [!IMPORTANT]
> Early `0.x` (`0.1.0` on npm). Pin exact versions; the public API is not frozen.
> Early `0.x` (`0.2.0` on npm). Pin exact versions; the public API is not frozen.
> See [PROJECT_PLAN.md](../../docs/PROJECT_PLAN.md).

## Install

```sh
npm install @pegma/storage-blobs@0.1.0
npm install @pegma/storage-blobs@0.2.0
```

## Server-side composition
Expand All @@ -34,6 +34,7 @@ const blobs: BlobStore = createMemoryBlobStore({
const key = "support-desk/attachments/01JEXAMPLE";
const put = await blobs.put(key, requestBodyStream, {
contentType: "application/pdf",
cacheControl: "public, max-age=31536000, immutable", // optional
ifNoneMatch: "*", // create-only
userMetadata: { ticket_id: "t_123" },
});
Expand Down Expand Up @@ -82,6 +83,16 @@ do {

Listing is not a snapshot. Always delete with the etag you observed.

### Cache-control

`put` accepts an optional `cacheControl` string stored with the object and
returned by `head`/`get` (`undefined` when the put omitted it; a replacing put
without one clears it). It is in the port because every first-class backend
keeps Cache-Control as native object state served with the bytes — unlike
signed URLs or lifecycle rules, which stay out. The store never interprets the
value; whether a host response honours or overrides it is host policy, like
`Content-Disposition`.

## Conformance (v1 specification)

The exported suite **is** the v1 contract. A behaviour not asserted there is
Expand Down
2 changes: 1 addition & 1 deletion packages/storage-blobs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pegma/storage-blobs",
"version": "0.1.2",
"version": "0.2.0",
"description": "Provider-neutral object (blob) storage: opaque keys, streaming put/get, conditionals, and a conformance suite.",
"license": "MIT",
"type": "module",
Expand Down
68 changes: 68 additions & 0 deletions packages/storage-blobs/src/conformance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DEFAULT_CONTENT_TYPE,
MAX_BLOB_KEY_BYTES,
MAX_BLOB_KEY_SEGMENTS,
MAX_CACHE_CONTROL_BYTES,
MAX_LIST_PAGE_SIZE,
MAX_USER_METADATA_TOTAL_BYTES,
type BlobStore,
Expand Down Expand Up @@ -745,6 +746,73 @@ export const conformanceCases: readonly ConformanceCase[] = [
assert.equal(await store.head("gone/obj"), null);
assert.equal(await store.get("gone/obj"), null);
}),

// --- Cache-control (0.2.0 port extension) ---

testCase(
"put stores cache-control and head/get return it verbatim",
async (store) => {
const cacheControl = "public, max-age=31536000, immutable";
const put = await store.put("cache/asset.png", textBytes("png-bytes"), {
contentType: "image/png",
cacheControl,
});
assert.equal(put.ok, true);

const head = await store.head("cache/asset.png");
assert.ok(head);
assert.equal(head.cacheControl, cacheControl);

const got = await store.get("cache/asset.png");
assert.ok(got);
assert.equal(got.cacheControl, cacheControl);
},
),

testCase(
"head and get report undefined cache-control when a put omits one",
async (store) => {
await store.put("cache/plain", textBytes("x"));
const head = await store.head("cache/plain");
assert.ok(head);
assert.equal(head.cacheControl, undefined);
const got = await store.get("cache/plain");
assert.ok(got);
assert.equal(got.cacheControl, undefined);
},
),

testCase(
"put replace clears cache-control when the replacing put omits it",
async (store) => {
await store.put("cache/replace", textBytes("v1"), {
cacheControl: "no-store",
});
await store.put("cache/replace", textBytes("v2"));
const head = await store.head("cache/replace");
assert.ok(head);
assert.equal(head.cacheControl, undefined);
Comment thread
FlyOverCoderKY marked this conversation as resolved.
Resolved
const got = await store.get("cache/replace");
assert.ok(got);
assert.equal(got.cacheControl, undefined);
},
),

testCase("rejects illegal cache-control values", async (store) => {
const illegal = [
"",
" no-store ",
"max-age=\u20ac",
"v".repeat(MAX_CACHE_CONTROL_BYTES + 1),
];
for (const cacheControl of illegal) {
await assert.rejects(
() => store.put("cache/bad", textBytes("x"), { cacheControl }),
(error: unknown) => error instanceof BlobValidationError,
);
}
assert.equal(await store.head("cache/bad"), null);
}),
];

/**
Expand Down
Loading