@midsummer-ai/sdk 0.1.0-staging.4954 → 0.1.0-staging.4963

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/client.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { m as StorageEngine } from "./index-CXiY5knc.mjs";
1
+ import { m as StorageEngine } from "./index-_phnWnst.mjs";
2
2
  import { S3Credential } from "./s3.mjs";
3
3
  import { Transport } from "./wire.mjs";
4
4
  import { socket } from "./socket.mjs";
package/client.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { a as FRAMES_PATH, c as serveSigned, l as signedHttp, o as SOCKET_PATH, s as client } from "./src-GjQuOm-D.mjs";
1
+ import { a as FRAMES_PATH, c as serveSigned, l as signedHttp, o as SOCKET_PATH, s as client } from "./src-DM6HKVuc.mjs";
2
2
  export { FRAMES_PATH, SOCKET_PATH, client, serveSigned, signedHttp };
package/contract.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { p as Storage } from "./index-CXiY5knc.mjs";
1
+ import { p as Storage } from "./index-_phnWnst.mjs";
2
2
  //#region src/contract.d.ts
3
3
  interface ContractCase {
4
4
  /** A line of the specification. */
package/contract.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as StorageError } from "./errors-B1tO_wdm.mjs";
2
- import "./src-GjQuOm-D.mjs";
2
+ import "./src-DM6HKVuc.mjs";
3
3
  //#region src/contract.ts
4
4
  /**
5
5
  * Storage's contract: what every engine promises, as cases any test runner can run. The Stand-in
@@ -419,6 +419,72 @@ const storageContract = [
419
419
  await bucket.put("film.mp4", "someone else's");
420
420
  await fails(bucket.completeUpload("film.mp4", uploadId, [part]), "conflict", "completing over a key that now holds something");
421
421
  }
422
+ },
423
+ {
424
+ name: "a put named with a SHA-256 is kept only when its bytes have it, in either case of hex; another stores nothing and the key keeps what it held",
425
+ async run(storage) {
426
+ const bucket = storage.bucket("blobs");
427
+ const put = await bucket.put(`sha256/${HELLO}`, "hello", {
428
+ sha256: HELLO,
429
+ ifNoneMatch: "*"
430
+ });
431
+ equal([put.key, put.etag], [`sha256/${HELLO}`, HELLO], "the put whose bytes have the SHA-256 named");
432
+ equal((await bucket.put("upper.txt", "hello", { sha256: HELLO.toUpperCase() })).etag, HELLO, "the SHA-256 named in capitals");
433
+ await fails(bucket.put(`sha256/${HELLO}`, "hello world", { sha256: HELLO }), "invalid", "bytes that do not have the SHA-256 named");
434
+ await fails(bucket.put("new.txt", "hello world", { sha256: HELLO }), "invalid", "a new key whose bytes do not have the SHA-256 named");
435
+ await fails(bucket.put("bad.txt", "hello", { sha256: "not-a-hash" }), "invalid", "a SHA-256 that is not 64 hexadecimal characters");
436
+ equal(text((await bucket.get(`sha256/${HELLO}`)).body), "hello", "the key that refused other bytes");
437
+ equal(await bucket.head("new.txt"), null, "the new key that refused its bytes");
438
+ }
439
+ },
440
+ {
441
+ name: "an upload in parts named with a SHA-256 completes only when the whole Object has it; one whose bytes lack it is ended, storing nothing",
442
+ async run(storage) {
443
+ const bucket = storage.bucket("blobs");
444
+ await bucket.put("whole.txt", "before");
445
+ const wrong = await bucket.createUpload("whole.txt", { sha256: HELLO_WORLD });
446
+ const sent = await bucket.uploadPart("whole.txt", wrong.uploadId, 1, "hello");
447
+ await fails(bucket.completeUpload("whole.txt", wrong.uploadId, [sent]), "invalid", "completing with bytes that do not have the SHA-256 named");
448
+ equal(text((await bucket.get("whole.txt")).body), "before", "the key after the refused completion");
449
+ await fails(bucket.uploadPart("whole.txt", wrong.uploadId, 1, "hello world"), "not_found", "a part sent to the ended upload", "upload");
450
+ const right = await bucket.createUpload("whole.txt", { sha256: HELLO_WORLD.toUpperCase() });
451
+ const part = await bucket.uploadPart("whole.txt", right.uploadId, 1, "hello world");
452
+ equal((await bucket.completeUpload("whole.txt", right.uploadId, [part])).etag, HELLO_WORLD, "the completion whose bytes have it");
453
+ const large = pattern(LARGE);
454
+ await bucket.put("large.bin", "hello");
455
+ await fails(bucket.put("large.bin", large, { sha256: HELLO }), "invalid", "a large body that does not have the SHA-256 named");
456
+ equal(text((await bucket.get("large.bin")).body), "hello", "the large key after the refused put");
457
+ }
458
+ },
459
+ {
460
+ name: "missing answers the keys that hold nothing, each once in the order first asked, from 1 to 10,000 keys a call",
461
+ async run(storage) {
462
+ const bucket = storage.bucket("blobs");
463
+ await bucket.put("a", "hello");
464
+ await bucket.put("c", "hello");
465
+ await bucket.put("folder/e", "hello");
466
+ equal(await bucket.missing([
467
+ "d",
468
+ "a",
469
+ "b",
470
+ "c",
471
+ "d",
472
+ "folder/",
473
+ "folder/e"
474
+ ]), [
475
+ "d",
476
+ "b",
477
+ "folder/"
478
+ ], "the keys holding nothing");
479
+ equal(await bucket.missing(["a", "c"]), [], "keys that all hold something");
480
+ await bucket.delete("a");
481
+ equal(await bucket.missing(["a", "c"]), ["a"], "a key after its delete");
482
+ const many = Array.from({ length: 1e4 }, (_, i) => `k${i}`);
483
+ equal((await bucket.missing(many)).length, 1e4, "10,000 keys that hold nothing");
484
+ await fails(bucket.missing([...many, "one-more"]), "invalid", "10,001 keys");
485
+ await fails(bucket.missing([]), "invalid", "no keys");
486
+ await fails(bucket.missing(["a", ""]), "invalid", "an empty key");
487
+ }
422
488
  }
423
489
  ];
424
490
  function pick(o) {
@@ -1,5 +1,5 @@
1
1
  import { t as DatabaseError } from "../errors-DcEc-3eL.mjs";
2
- import { p as Storage } from "../index-CXiY5knc.mjs";
2
+ import { p as Storage } from "../index-_phnWnst.mjs";
3
3
  import { t as AuthConfig } from "../auth-B7rCtXFQ.mjs";
4
4
  import { a as FileMetadata, c as FunctionReference, d as HttpRouter, n as CronJob, s as FunctionKind, v as RegisteredFunction, y as Row } from "../server-B5HK2Ou9.mjs";
5
5
  import { r as Built } from "../build-DX7RUtzJ.mjs";
@@ -1,4 +1,4 @@
1
- import { r as storage } from "../src-GjQuOm-D.mjs";
1
+ import { r as storage } from "../src-DM6HKVuc.mjs";
2
2
  import { standIn } from "../stand-in.mjs";
3
3
  import { _ as shapeError, a as tagBytes, b as valueAt, f as indexFields, g as shapeAt, i as keyBytes, o as untagBytes, v as tableCode } from "../bytes-C2pcyl9u.mjs";
4
4
  import { A as comparePrefix, M as indexTuple, O as valueKey, S as nextCron, T as readLimited, _ as isFunction, a as Refusal, b as limits, i as NotFoundError, j as documentBytes, k as compareKey, l as fetchOver, o as TooLarge, r as InsufficientError, y as isRouter } from "../server-Drjni3-F.mjs";
package/here-C2jhTheQ.mjs CHANGED
@@ -12,7 +12,7 @@ function processEnv() {
12
12
  * This package's version: `0.0.0-dev` here, and in a published package the version it was published at, which the
13
13
  * release writes in its place as it stages the package (`stampSdk` in `scripts/release/packages.ts`).
14
14
  */
15
- const SDK_VERSION = "0.1.0-staging.4954";
15
+ const SDK_VERSION = "0.1.0-staging.4963";
16
16
  /**
17
17
  * The header that tells Midsummer which client made each of this package's calls: `Midsummer-Client: sdk/<version>
18
18
  * <part>`, `part` the product whose client it is (`database`, `storage`, `queues`, ...), so the platform counts which
@@ -61,6 +61,12 @@ interface PutOptions {
61
61
  readonly ifNoneMatch?: "*";
62
62
  /** Put only if the key holds content with this etag, else `conflict`. */
63
63
  readonly ifMatch?: string;
64
+ /**
65
+ * The SHA-256 the bytes must have, in hex. Storage hashes what arrives and stores nothing, failing with `invalid`, when
66
+ * it differs, so a key named by its content's hash is proven to hold exactly that content. The etag returned is
67
+ * this hash.
68
+ */
69
+ readonly sha256?: string;
64
70
  }
65
71
  interface ListOptions {
66
72
  /** Only keys that start with this. */
@@ -89,6 +95,12 @@ interface Bucket {
89
95
  }): Promise<ObjectRead | null>;
90
96
  /** The Object's description without its bytes; `null` if nothing is stored under the key. */
91
97
  head(key: string): Promise<StoredObject | null>;
98
+ /**
99
+ * Of `keys` (1 to 10,000 in one call), the ones nothing is stored under, each once, in the order first asked. No
100
+ * bytes are read. It costs one read for each range of the Bucket's keys it touches, not one a key: a Bucket is one
101
+ * range until it holds 50,000 keys or takes hundreds of calls a second, so 10,000 keys of most Buckets cost one read.
102
+ */
103
+ missing(keys: ReadonlyArray<string>): Promise<ReadonlyArray<string>>;
92
104
  /** Removes what `key` holds. Removing nothing is not an error. */
93
105
  delete(key: string): Promise<void>;
94
106
  list(options?: ListOptions): Promise<Listing>;
@@ -98,7 +110,8 @@ interface Bucket {
98
110
  }): Promise<StoredObject>;
99
111
  /**
100
112
  * Starts putting `key` in parts, for an Object too big for one call or sent in parallel. The key
101
- * keeps what it holds until the upload completes. `ifNoneMatch` is checked when it completes.
113
+ * keeps what it holds until the upload completes. `ifNoneMatch` is checked when it completes, and so is `sha256`,
114
+ * against the whole Object's bytes: an upload whose bytes lack it is ended, storing nothing.
102
115
  */
103
116
  createUpload(key: string, options?: Omit<PutOptions, "ifMatch">): Promise<{
104
117
  readonly uploadId: string;
@@ -154,6 +167,11 @@ interface StorageEngine {
154
167
  */
155
168
  get(bucket: string, key: string, range?: Range, limit?: number): Promise<ObjectRead | null>;
156
169
  head(bucket: string, key: string): Promise<StoredObject | null>;
170
+ /**
171
+ * Of `keys`, those nothing is stored under, each once, in the order first asked; one read for each range of the
172
+ * Bucket's keys it touches. An engine without it is asked `head` for each key instead.
173
+ */
174
+ missing?(bucket: string, keys: ReadonlyArray<string>): Promise<ReadonlyArray<string>>;
157
175
  delete(bucket: string, key: string): Promise<void>;
158
176
  list(bucket: string, options: EngineList): Promise<Listing>;
159
177
  copy(bucket: string, from: string, to: string, options: {
@@ -199,6 +217,8 @@ interface EnginePut {
199
217
  readonly metadata: Readonly<Record<string, string>>;
200
218
  readonly ifNoneMatch?: "*";
201
219
  readonly ifMatch?: string;
220
+ /** The SHA-256 the body must have, in lowercase hex: one that differs is `invalid` and stored nowhere. */
221
+ readonly sha256?: string;
202
222
  }
203
223
  interface EngineList {
204
224
  readonly prefix: string;
package/index.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import { C as StorageErrorReason, S as StorageErrorCode, _ as WORKLOAD_PATH, a as ListOptions, b as streamsOf, c as ObjectStream, d as PutOptions, f as Range, g as Usage, h as StoredObject, i as EnginePut, l as Operations, m as StorageEngine, n as Bucket, o as Listing, p as Storage, r as EngineList, s as ObjectRead, t as Body, u as Part, v as bucket, x as StorageError, y as storage } from "./index-CXiY5knc.mjs";
1
+ import { C as StorageErrorReason, S as StorageErrorCode, _ as WORKLOAD_PATH, a as ListOptions, b as streamsOf, c as ObjectStream, d as PutOptions, f as Range, g as Usage, h as StoredObject, i as EnginePut, l as Operations, m as StorageEngine, n as Bucket, o as Listing, p as Storage, r as EngineList, s as ObjectRead, t as Body, u as Part, v as bucket, x as StorageError, y as storage } from "./index-_phnWnst.mjs";
2
2
  export { Body, Bucket, EngineList, EnginePut, ListOptions, Listing, ObjectRead, ObjectStream, Operations, Part, PutOptions, Range, Storage, StorageEngine, StorageError, type StorageErrorCode, type StorageErrorReason, StoredObject, Usage, WORKLOAD_PATH, bucket, storage, streamsOf };
package/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
1
  import { t as StorageError } from "./errors-B1tO_wdm.mjs";
2
- import { i as streamsOf, n as bucket, r as storage, t as WORKLOAD_PATH } from "./src-GjQuOm-D.mjs";
2
+ import { i as streamsOf, n as bucket, r as storage, t as WORKLOAD_PATH } from "./src-DM6HKVuc.mjs";
3
3
  export { StorageError, WORKLOAD_PATH, bucket, storage, streamsOf };
@@ -64,6 +64,33 @@ function checkParts(parts) {
64
64
  if (i > 0 && part.partNumber <= parts[i - 1].partNumber) throw new StorageError("invalid", "the parts are not in ascending part number", "part-order");
65
65
  });
66
66
  }
67
+ /** A SHA-256 the caller names for a body, in hex: answered in lowercase, as etags are. */
68
+ function checkSha256(sha256) {
69
+ if (!/^[0-9a-fA-F]{64}$/.test(sha256)) invalid("sha256 is not a SHA-256: 64 hexadecimal characters");
70
+ return sha256.toLowerCase();
71
+ }
72
+ /** The keys of one `missing` call: 1 to 10,000 of them, each a key. */
73
+ function checkMissingKeys(keys) {
74
+ if (!Array.isArray(keys) || keys.length < 1 || keys.length > 1e4) invalid(`missing asks about 1 to 10,000 keys a call; this asked about ${Array.isArray(keys) ? keys.length.toLocaleString("en-US") : "none"}`);
75
+ for (const key of keys) {
76
+ if (typeof key !== "string") invalid("every key is a string");
77
+ checkKey(key);
78
+ }
79
+ }
80
+ /** `missing` over an engine that has only `head`: every key asked at once, so a batching engine sends them together. */
81
+ async function missingByHead(engine, bucket, keys) {
82
+ const unique = [...new Set(keys)];
83
+ const found = await Promise.all(unique.map((key) => engine.head(bucket, key)));
84
+ return unique.filter((_, i) => found[i] === null);
85
+ }
86
+ /** Why a body named with the SHA-256 `expected` is refused, its own being `actual`. */
87
+ const sha256Refusal = (expected, actual) => `the body's SHA-256 is ${actual}, not the ${expected} it was sent with; nothing was stored`;
88
+ /** Why an upload named with the SHA-256 `expected` is refused on completing, its whole bytes' being `actual`: it is ended. */
89
+ const uploadSha256Refusal = (expected, actual) => `the upload's SHA-256 is ${actual}, not the ${expected} it was begun with; nothing was stored and the upload is ended, so begin another`;
90
+ /** The SHA-256 a body was named with, against the one it has: `invalid`, storing nothing, when they differ. */
91
+ function checkBodySha256(expected, actual) {
92
+ if (expected !== void 0 && expected !== actual) invalid(sha256Refusal(expected, actual));
93
+ }
67
94
  function checkListLimit(limit) {
68
95
  if (!count(limit) || limit < 1 || limit > 1e3) invalid("a listing's limit is 1 to 1,000");
69
96
  }
@@ -86,4 +113,4 @@ function sameSizes(sizes, named) {
86
113
  for (let i = 1; i < sizes.length; i += 1) if (i < sizes.length - 1 ? sizes[i] !== sizes[0] : sizes[i] > sizes[0]) throw new StorageError("invalid", `part ${named[i].partNumber} is ${sizes[i]} bytes: every part but the last must be ${sizes[0]} bytes, as the first is, and the last no larger`, "part");
87
114
  }
88
115
  //#endregion
89
- export { checkMetadata as a, checkRange as c, invalid as d, resolveRange as f, checkListLimit as i, checkUploadId as l, sha256 as m, checkBucketName as n, checkPartNumber as o, sameSizes as p, checkKey as r, checkParts as s, MIN_PART as t, compareKeys as u };
116
+ export { sameSizes as _, checkListLimit as a, checkPartNumber as c, checkSha256 as d, checkUploadId as f, resolveRange as g, missingByHead as h, checkKey as i, checkParts as l, invalid as m, checkBodySha256 as n, checkMetadata as o, compareKeys as p, checkBucketName as r, checkMissingKeys as s, MIN_PART as t, checkRange as u, sha256 as v, uploadSha256Refusal as y };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midsummer-ai/sdk",
3
- "version": "0.1.0-staging.4954",
3
+ "version": "0.1.0-staging.4963",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "description": "Midsummer's handles for an app's code: its Database, Storage, Queues, Vector DB, AI, Email and Agents, with stand-ins for tests.",
package/s3.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { m as StorageEngine } from "./index-CXiY5knc.mjs";
1
+ import { m as StorageEngine } from "./index-_phnWnst.mjs";
2
2
  //#region src/s3.d.ts
3
3
  /** What one access key may do. */
4
4
  interface S3Credential {
package/s3.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { t as StorageError } from "./errors-B1tO_wdm.mjs";
2
- import { a as checkMetadata, l as checkUploadId, m as sha256, n as checkBucketName, o as checkPartNumber, r as checkKey, s as checkParts } from "./names-Do0ot5Ha.mjs";
3
- import { i as streamsOf } from "./src-GjQuOm-D.mjs";
2
+ import { c as checkPartNumber, f as checkUploadId, i as checkKey, l as checkParts, o as checkMetadata, r as checkBucketName, v as sha256 } from "./names-44OMPNTX.mjs";
3
+ import { i as streamsOf } from "./src-DM6HKVuc.mjs";
4
4
  import { inertHeaders } from "./inert.mjs";
5
5
  //#region src/s3.ts
6
6
  /**
@@ -651,21 +651,55 @@ function conditionsOf(headers) {
651
651
  ...match !== null ? { ifMatch: match.replace(/^"|"$/g, "") } : {}
652
652
  };
653
653
  }
654
+ /** The SHA-256 an `x-amz-checksum-sha256` header names, in hex; undefined when none is sent. */
655
+ function checksumSha256(headers) {
656
+ const given = headers.get("x-amz-checksum-sha256");
657
+ if (given === null) return void 0;
658
+ let bytes;
659
+ try {
660
+ bytes = atob(given.trim());
661
+ } catch {
662
+ bytes = "";
663
+ }
664
+ if (bytes.length !== 32) throw new S3Error(400, "InvalidRequest", "x-amz-checksum-sha256 is not a SHA-256: 32 bytes in base64");
665
+ return [...bytes].map((c) => c.charCodeAt(0).toString(16).padStart(2, "0")).join("");
666
+ }
667
+ const BAD_SHA256 = "The body's SHA-256 is not its x-amz-checksum-sha256, so nothing was stored";
654
668
  async function putObject(call, bucket, key) {
655
669
  noTags(call.request.headers);
670
+ const checksum = checksumSha256(call.request.headers);
656
671
  const options = {
657
672
  contentType: call.request.headers.get("content-type") ?? "application/octet-stream",
658
673
  metadata: metadataOf(call.request.headers),
659
674
  ...conditionsOf(call.request.headers)
660
675
  };
661
676
  const { streamed, engine } = call;
662
- const object = streamed && engine.putStream ? await engine.putStream(bucket, key, streamed.body, streamed.size, {
663
- ...options,
664
- ...streamed.sha256 ? { sha256: streamed.sha256 } : {}
665
- }) : await engine.put(bucket, key, call.body, options);
677
+ if (checksum !== void 0 && streamed?.sha256 !== void 0 && streamed.sha256 !== checksum) throw new S3Error(400, "BadDigest", BAD_SHA256);
678
+ let object;
679
+ if (streamed && engine.putStream) {
680
+ const sha256 = checksum ?? streamed.sha256;
681
+ try {
682
+ object = await engine.putStream(bucket, key, streamed.body, streamed.size, {
683
+ ...options,
684
+ ...sha256 ? { sha256 } : {}
685
+ });
686
+ } catch (error) {
687
+ if (checksum !== void 0 && error instanceof StorageError && error.code === "invalid" && error.message.includes("SHA-256")) throw new S3Error(400, "BadDigest", BAD_SHA256);
688
+ throw error;
689
+ }
690
+ } else {
691
+ if (checksum !== void 0 && checksum !== await sha256(call.body)) throw new S3Error(400, "BadDigest", BAD_SHA256);
692
+ object = await engine.put(bucket, key, call.body, {
693
+ ...options,
694
+ ...checksum !== void 0 ? { sha256: checksum } : {}
695
+ });
696
+ }
666
697
  return new Response(null, {
667
698
  status: 200,
668
- headers: { etag: `"${object.etag}"` }
699
+ headers: {
700
+ etag: `"${object.etag}"`,
701
+ ...checksum !== void 0 ? { "x-amz-checksum-sha256": call.request.headers.get("x-amz-checksum-sha256").trim() } : {}
702
+ }
669
703
  });
670
704
  }
671
705
  /** The Object `x-amz-copy-source` names, which the key must be able to read. */
package/socket.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { m as StorageEngine } from "./index-CXiY5knc.mjs";
1
+ import { m as StorageEngine } from "./index-_phnWnst.mjs";
2
2
  import { S3Credential } from "./s3.mjs";
3
3
  import { Transport } from "./wire.mjs";
4
4
  //#region src/socket.d.ts
package/socket.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { d as sameHex, f as sign, m as socketSession, p as socket, u as keyed } from "./src-GjQuOm-D.mjs";
1
+ import { d as sameHex, f as sign, m as socketSession, p as socket, u as keyed } from "./src-DM6HKVuc.mjs";
2
2
  export { keyed, sameHex, sign, socket, socketSession };
@@ -1,5 +1,5 @@
1
1
  import { t as StorageError } from "./errors-B1tO_wdm.mjs";
2
- import { a as checkMetadata, c as checkRange, d as invalid, i as checkListLimit, l as checkUploadId, n as checkBucketName, o as checkPartNumber, r as checkKey, s as checkParts } from "./names-Do0ot5Ha.mjs";
2
+ import { a as checkListLimit, c as checkPartNumber, d as checkSha256, f as checkUploadId, h as missingByHead, i as checkKey, l as checkParts, m as invalid, o as checkMetadata, r as checkBucketName, s as checkMissingKeys, u as checkRange } from "./names-44OMPNTX.mjs";
3
3
  import { a as processEnv, n as here, o as workloadTransport, r as late, t as clientHeaders } from "./here-C2jhTheQ.mjs";
4
4
  import { bucketsOf, remote, repeatable, serve } from "./wire.mjs";
5
5
  //#region src/socket.ts
@@ -196,6 +196,7 @@ function keyed(engine, credential) {
196
196
  put: (bucket, ...rest) => engine.put(writable(bucket), ...rest),
197
197
  get: (bucket, ...rest) => engine.get(id(bucket), ...rest),
198
198
  head: (bucket, ...rest) => engine.head(id(bucket), ...rest),
199
+ ...engine.missing ? { missing: (bucket, keys) => engine.missing(id(bucket), keys) } : {},
199
200
  delete: (bucket, ...rest) => engine.delete(writable(bucket), ...rest),
200
201
  list: (bucket, ...rest) => engine.list(id(bucket), ...rest),
201
202
  copy: (bucket, ...rest) => engine.copy(writable(bucket), ...rest),
@@ -445,11 +446,13 @@ function storage(engine) {
445
446
  const metadata = options.metadata ?? {};
446
447
  checkMetadata(metadata);
447
448
  if (options.ifMatch !== void 0 && !/^[0-9a-f]{64}$/.test(options.ifMatch)) invalid("ifMatch is not an etag");
449
+ const sha256 = options.sha256 === void 0 ? void 0 : checkSha256(options.sha256);
448
450
  return engine.put(name, key, body instanceof Blob ? await bytesOfBlob(body) : bytesOf(body), {
449
451
  contentType: options.contentType ?? "application/octet-stream",
450
452
  metadata,
451
453
  ...options.ifNoneMatch ? { ifNoneMatch: options.ifNoneMatch } : {},
452
- ...options.ifMatch !== void 0 ? { ifMatch: options.ifMatch } : {}
454
+ ...options.ifMatch !== void 0 ? { ifMatch: options.ifMatch } : {},
455
+ ...sha256 !== void 0 ? { sha256 } : {}
453
456
  });
454
457
  },
455
458
  async get(key, options = {}) {
@@ -461,6 +464,11 @@ function storage(engine) {
461
464
  checkKey(key);
462
465
  return engine.head(name, key);
463
466
  },
467
+ async missing(keys) {
468
+ checkMissingKeys(keys);
469
+ if (engine.missing) return engine.missing(name, keys);
470
+ return missingByHead(engine, name, keys);
471
+ },
464
472
  async delete(key) {
465
473
  checkKey(key);
466
474
  return engine.delete(name, key);
@@ -485,10 +493,12 @@ function storage(engine) {
485
493
  checkKey(key);
486
494
  const metadata = options.metadata ?? {};
487
495
  checkMetadata(metadata);
496
+ const sha256 = options.sha256 === void 0 ? void 0 : checkSha256(options.sha256);
488
497
  return engine.createUpload(name, key, {
489
498
  contentType: options.contentType ?? "application/octet-stream",
490
499
  metadata,
491
- ...options.ifNoneMatch ? { ifNoneMatch: options.ifNoneMatch } : {}
500
+ ...options.ifNoneMatch ? { ifNoneMatch: options.ifNoneMatch } : {},
501
+ ...sha256 !== void 0 ? { sha256 } : {}
492
502
  });
493
503
  },
494
504
  async uploadPart(key, uploadId, partNumber, body) {
package/stand-in.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { m as StorageEngine } from "./index-CXiY5knc.mjs";
1
+ import { m as StorageEngine } from "./index-_phnWnst.mjs";
2
2
  //#region src/stand-in.d.ts
3
3
  declare function standIn(options: {
4
4
  readonly clock: () => number;
package/stand-in.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as StorageError } from "./errors-B1tO_wdm.mjs";
2
- import { f as resolveRange, m as sha256, p as sameSizes, u as compareKeys } from "./names-Do0ot5Ha.mjs";
2
+ import { _ as sameSizes, g as resolveRange, n as checkBodySha256, p as compareKeys, v as sha256, y as uploadSha256Refusal } from "./names-44OMPNTX.mjs";
3
3
  //#region src/stand-in.ts
4
4
  /**
5
5
  * Storage's Stand-in: the Capability's behaviour in memory, for `midsummer dev` and tests. It holds
@@ -49,10 +49,12 @@ function standIn(options) {
49
49
  const current = buckets.get(bucket)?.get(key);
50
50
  if (put.ifNoneMatch && current) throw new StorageError("conflict", `"${key}" already holds an Object`);
51
51
  if (put.ifMatch !== void 0 && current?.object.etag !== put.ifMatch) throw new StorageError("conflict", `"${key}" does not hold the etag ${put.ifMatch}`);
52
+ const etag = await sha256(body);
53
+ checkBodySha256(put.sha256, etag);
52
54
  const object = {
53
55
  key,
54
56
  size: body.length,
55
- etag: await sha256(body),
57
+ etag,
56
58
  contentType: put.contentType,
57
59
  metadata: { ...put.metadata },
58
60
  uploadedAt: options.clock()
@@ -76,6 +78,11 @@ function standIn(options) {
76
78
  count(bucket, false);
77
79
  return buckets.get(bucket)?.get(key)?.object ?? null;
78
80
  },
81
+ async missing(bucket, keys) {
82
+ count(bucket, false);
83
+ const held = buckets.get(bucket);
84
+ return [...new Set(keys)].filter((key) => !held?.has(key));
85
+ },
79
86
  async delete(bucket, key) {
80
87
  count(bucket, true);
81
88
  buckets.get(bucket)?.delete(key);
@@ -156,6 +163,11 @@ function standIn(options) {
156
163
  const whole = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
157
164
  let at = 0;
158
165
  for (const p of parts) whole.set(p, at), at += p.length;
166
+ const etag = await sha256(whole);
167
+ if (upload.options.sha256 !== void 0 && upload.options.sha256 !== etag) {
168
+ uploads.delete(uploadId);
169
+ throw new StorageError("invalid", uploadSha256Refusal(upload.options.sha256, etag));
170
+ }
159
171
  const object = await engine.put(bucket, key, whole, upload.options);
160
172
  uploads.delete(uploadId);
161
173
  return object;
@@ -1,4 +1,4 @@
1
- import { m as StorageEngine, t as Body } from "../index-CXiY5knc.mjs";
1
+ import { m as StorageEngine, t as Body } from "../index-_phnWnst.mjs";
2
2
  import { n as AuthProvider, r as VerifierOptions } from "../auth-B7rCtXFQ.mjs";
3
3
  //#region src/storage/files.d.ts
4
4
  /** Whether a rule only reads its folder, or reads, puts and deletes in it. */
package/storage/files.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as StorageError } from "../errors-B1tO_wdm.mjs";
2
- import { n as checkBucketName } from "../names-Do0ot5Ha.mjs";
2
+ import { r as checkBucketName } from "../names-44OMPNTX.mjs";
3
3
  import { t as clientHeaders } from "../here-C2jhTheQ.mjs";
4
4
  import { inertHeaders } from "../inert.mjs";
5
5
  import { DatabaseError } from "../database/errors.mjs";
package/wire.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { m as StorageEngine } from "./index-CXiY5knc.mjs";
1
+ import { m as StorageEngine } from "./index-_phnWnst.mjs";
2
2
  //#region src/wire.d.ts
3
3
  /** Carries one request frame to a server and brings back its answer frame. */
4
4
  interface Transport {
package/wire.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { n as storageErrorCodes, r as storageErrorReasons, t as StorageError } from "./errors-B1tO_wdm.mjs";
2
- import { a as checkMetadata, c as checkRange, i as checkListLimit, l as checkUploadId, n as checkBucketName, o as checkPartNumber, r as checkKey, s as checkParts } from "./names-Do0ot5Ha.mjs";
2
+ import { a as checkListLimit, c as checkPartNumber, d as checkSha256, f as checkUploadId, h as missingByHead, i as checkKey, l as checkParts, o as checkMetadata, r as checkBucketName, s as checkMissingKeys, u as checkRange } from "./names-44OMPNTX.mjs";
3
3
  import { t as clientHeaders } from "./here-C2jhTheQ.mjs";
4
4
  import { z } from "zod";
5
5
  //#region src/wire.ts
@@ -36,7 +36,8 @@ const request = z.discriminatedUnion("op", [
36
36
  contentType: z.string().max(256),
37
37
  metadata: z.record(z.string(), z.string()),
38
38
  ifNoneMatch: z.literal("*").optional(),
39
- ifMatch: z.string().optional()
39
+ ifMatch: z.string().optional(),
40
+ sha256: z.string().optional()
40
41
  }).strict(),
41
42
  z.object({
42
43
  op: z.literal("get"),
@@ -48,6 +49,11 @@ const request = z.discriminatedUnion("op", [
48
49
  op: z.literal("head"),
49
50
  ...at
50
51
  }).strict(),
52
+ z.object({
53
+ op: z.literal("missing"),
54
+ bucket: z.string(),
55
+ keys: z.array(z.string())
56
+ }).strict(),
51
57
  z.object({
52
58
  op: z.literal("delete"),
53
59
  ...at
@@ -72,7 +78,8 @@ const request = z.discriminatedUnion("op", [
72
78
  ...at,
73
79
  contentType: z.string().max(256),
74
80
  metadata: z.record(z.string(), z.string()),
75
- ifNoneMatch: z.literal("*").optional()
81
+ ifNoneMatch: z.literal("*").optional(),
82
+ sha256: z.string().optional()
76
83
  }).strict(),
77
84
  z.object({
78
85
  op: z.literal("uploadPart"),
@@ -138,6 +145,7 @@ const answer = z.union([
138
145
  next: z.string().optional()
139
146
  }).strict() }).strict(),
140
147
  z.object({ done: z.literal(true) }).strict(),
148
+ z.object({ missing: z.array(z.string()) }).strict(),
141
149
  z.object({ usage: z.object({
142
150
  objects: z.number(),
143
151
  bytes: z.number()
@@ -266,7 +274,8 @@ function remote(transport, options = {}) {
266
274
  const { uploadId } = await engine.createUpload(bucket, key, {
267
275
  contentType: put.contentType,
268
276
  metadata: put.metadata,
269
- ...put.ifNoneMatch ? { ifNoneMatch: put.ifNoneMatch } : {}
277
+ ...put.ifNoneMatch ? { ifNoneMatch: put.ifNoneMatch } : {},
278
+ ...put.sha256 !== void 0 ? { sha256: put.sha256 } : {}
270
279
  });
271
280
  try {
272
281
  const count = Math.ceil(body.length / PART_BYTES), parts = [];
@@ -328,6 +337,14 @@ function remote(transport, options = {}) {
328
337
  key
329
338
  })).reply);
330
339
  },
340
+ async missing(bucket, keys) {
341
+ const { reply } = await call({
342
+ op: "missing",
343
+ bucket,
344
+ keys: [...keys]
345
+ });
346
+ return "missing" in reply ? reply.missing : [];
347
+ },
331
348
  async delete(bucket, key) {
332
349
  await call({
333
350
  op: "delete",
@@ -476,6 +493,7 @@ function repeatableCall(call) {
476
493
  switch (call.op) {
477
494
  case "get":
478
495
  case "head":
496
+ case "missing":
479
497
  case "list":
480
498
  case "delete":
481
499
  case "uploadPart":
@@ -591,7 +609,8 @@ async function answerTo(engine, call, body) {
591
609
  contentType: call.contentType,
592
610
  metadata: call.metadata,
593
611
  ...call.ifNoneMatch ? { ifNoneMatch: call.ifNoneMatch } : {},
594
- ...call.ifMatch !== void 0 ? { ifMatch: call.ifMatch } : {}
612
+ ...call.ifMatch !== void 0 ? { ifMatch: call.ifMatch } : {},
613
+ ...call.sha256 !== void 0 ? { sha256: checkSha256(call.sha256) } : {}
595
614
  };
596
615
  return encodeFrame({ object: await engine.put(call.bucket, call.key, body, put) });
597
616
  }
@@ -610,6 +629,9 @@ async function answerTo(engine, call, body) {
610
629
  case "head":
611
630
  checkKey(call.key);
612
631
  return encodeFrame({ object: await engine.head(call.bucket, call.key) });
632
+ case "missing":
633
+ checkMissingKeys(call.keys);
634
+ return encodeFrame({ missing: await (engine.missing ? engine.missing(call.bucket, call.keys) : missingByHead(engine, call.bucket, call.keys)) });
613
635
  case "delete":
614
636
  checkKey(call.key);
615
637
  await engine.delete(call.bucket, call.key);
@@ -634,7 +656,8 @@ async function answerTo(engine, call, body) {
634
656
  const upload = {
635
657
  contentType: call.contentType,
636
658
  metadata: call.metadata,
637
- ...call.ifNoneMatch ? { ifNoneMatch: call.ifNoneMatch } : {}
659
+ ...call.ifNoneMatch ? { ifNoneMatch: call.ifNoneMatch } : {},
660
+ ...call.sha256 !== void 0 ? { sha256: checkSha256(call.sha256) } : {}
638
661
  };
639
662
  return encodeFrame(await engine.createUpload(call.bucket, call.key, upload));
640
663
  }