@powerhousedao/reactor-attachments 6.2.3-dev.1 → 6.2.3-dev.100

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/dist/index.js CHANGED
@@ -1,14 +1,15 @@
1
- import { C as UploadTooLarge, S as SizeMismatch, _ as AttachmentPending, a as RemoteAttachmentUpload, b as InvalidAttachmentRef, c as createFetchUploadTransport, d as createRef, f as parseRef, g as AttachmentNotFound, h as AttachmentAlreadyExists, i as RemoteAttachmentUploadFactory, l as SwitchboardAttachmentTransport, m as parseAttachmentUploadTarget, n as createRemoteAttachmentService, o as RemoteReservationStore, p as parseAttachmentDownloadTarget, r as RemoteAttachmentStore, s as createXhrUploadTransport, t as NullAttachmentTransport, u as AttachmentService, v as AttachmentTransferError, x as ReservationNotFound, y as HashMismatch } from "./null-attachment-transport-CrsUafCi.js";
1
+ import { a as parseRef, c as AttachmentPending, d as InvalidAttachmentMetadata, f as InvalidAttachmentRef, h as UploadTooLarge, i as createRef, l as AttachmentTransferError, m as SizeMismatch, o as AttachmentAlreadyExists, p as ReservationNotFound, s as AttachmentNotFound, t as SwitchboardAttachmentTransport, u as HashMismatch } from "./switchboard-attachment-transport-BH3ZAmxE.js";
2
+ import { a as RemoteAttachmentUploadFactory, c as createXhrUploadTransport, d as isBlob, f as AttachmentService, i as RemoteAttachmentStore, l as createFetchUploadTransport, m as parseAttachmentUploadTarget, n as NullAttachmentTransport, o as RemoteAttachmentUpload, p as parseAttachmentDownloadTarget, r as createRemoteAttachmentService, s as RemoteReservationStore, t as RoutingAttachmentService, u as blobStream } from "./routing-attachment-service-C88ryRgj.js";
3
+ import { t as AttachmentSchemaCompiler } from "./attachment-schema-compiler-BytE6Hev.js";
2
4
  import { dirname, join } from "node:path";
3
5
  import { Migrator, sql } from "kysely";
4
6
  import { mkdir, rename, rm } from "node:fs/promises";
5
7
  import { createReadStream, createWriteStream } from "node:fs";
6
8
  import { createHash, randomUUID } from "node:crypto";
7
9
  import { Readable } from "node:stream";
8
- import { generatorTypeDefs } from "@powerhousedao/document-engineering/graphql";
9
- import { constantCase, pascalCase } from "change-case";
10
- import { Kind, buildASTSchema, getNamedType, isInputObjectType, isListType, isNonNullType, parse, print } from "graphql";
11
- import { BaseReadModel } from "@powerhousedao/reactor";
10
+ import { finished } from "node:stream/promises";
11
+ import { BaseReadModel, defaultReadModelIndexingConfig } from "@powerhousedao/reactor";
12
+ import { isPurgeMarker } from "@powerhousedao/shared/document-model";
12
13
  import { Buffer as Buffer$1 } from "node:buffer";
13
14
  import { GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
14
15
  import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
@@ -24,6 +25,27 @@ var __exportAll = (all, no_symbols) => {
24
25
  return target;
25
26
  };
26
27
  //#endregion
28
+ //#region src/reserve-metadata.ts
29
+ const CONTROL_CHARS = /[\x00-\x1f\x7f]/;
30
+ const NON_HEADER_CHARS = /[^\t\x20-\x7e\x80-\xff]/;
31
+ const MIME_TYPE_PATTERN = /^[!#$%&'*+\-.^_`|~\w]+\/[!#$%&'*+\-.^_`|~\w]+(?:[ \t]*;[ \t]*[!#$%&'*+\-.^_`|~\w]+=(?:[!#$%&'*+\-.^_`|~\w]+|"(?:[^"\\\r\n]|\\[^\r\n])*"))*$/;
32
+ const MAX_FILENAME_LEN = 255;
33
+ const MAX_MIMETYPE_LEN = 255;
34
+ /**
35
+ * Validates reserve metadata with the rules the switchboard reserve route
36
+ * applies.
37
+ *
38
+ * @throws InvalidAttachmentMetadata naming the first field that fails.
39
+ */
40
+ function validateReserveMetadata(meta) {
41
+ const { mimeType, fileName, extension } = meta;
42
+ if (typeof mimeType !== "string" || mimeType.length === 0 || mimeType.length > MAX_MIMETYPE_LEN || NON_HEADER_CHARS.test(mimeType) || !MIME_TYPE_PATTERN.test(mimeType)) throw new InvalidAttachmentMetadata("mimeType");
43
+ if (typeof fileName !== "string" || fileName.length === 0 || fileName.length > MAX_FILENAME_LEN || CONTROL_CHARS.test(fileName)) throw new InvalidAttachmentMetadata("fileName");
44
+ if (typeof extension === "string") {
45
+ if (extension.length === 0 || /[\\/]/.test(extension)) throw new InvalidAttachmentMetadata("extension");
46
+ } else if (extension !== void 0 && extension !== null) throw new InvalidAttachmentMetadata("extension");
47
+ }
48
+ //#endregion
27
49
  //#region src/storage/fs/attachment-fs.ts
28
50
  /**
29
51
  * Compute the relative storage path for an attachment hash.
@@ -34,17 +56,41 @@ function storageRelativePath(hash) {
34
56
  }
35
57
  /**
36
58
  * Write a ReadableStream to disk. Creates parent directories as needed.
59
+ *
60
+ * Bytes are streamed to a uniquely-named temp file in the destination
61
+ * directory and only atomically renamed onto the final path once the whole
62
+ * stream has been written and flushed. The final path therefore never holds a
63
+ * partial or torn file: a concurrent writer of the same content-addressed
64
+ * path (e.g. two clients re-fetching the same evicted attachment) streams to
65
+ * its own temp file, and the rename publishes the new content all at once. On
66
+ * any failure the temp file is removed and the previous file (if any) is left
67
+ * untouched.
68
+ *
37
69
  * Returns the number of bytes written.
38
70
  */
71
+ async function discardTempFile(writer, tempPath) {
72
+ writer.destroy();
73
+ try {
74
+ await finished(writer, { error: false });
75
+ } catch {}
76
+ await rm(tempPath, { force: true });
77
+ }
39
78
  async function writeAttachmentBytes(path, data) {
40
79
  await mkdir(dirname(path), { recursive: true });
41
- const writer = createWriteStream(path);
80
+ const tempPath = join(dirname(path), `${randomUUID()}.tmp`);
81
+ const writer = createWriteStream(tempPath);
42
82
  const reader = data.getReader();
43
83
  let bytesWritten = 0;
84
+ let caughtError;
85
+ const recordError = (err) => {
86
+ caughtError ??= err instanceof Error ? err : new Error(String(err));
87
+ };
88
+ writer.on("error", recordError);
44
89
  try {
45
90
  for (;;) {
46
91
  const { done, value } = await reader.read();
47
92
  if (done) break;
93
+ if (caughtError) break;
48
94
  bytesWritten += value.byteLength;
49
95
  if (!writer.write(value)) await new Promise((resolve, reject) => {
50
96
  const onDrain = () => {
@@ -59,12 +105,27 @@ async function writeAttachmentBytes(path, data) {
59
105
  writer.once("error", onError);
60
106
  });
61
107
  }
108
+ } catch (err) {
109
+ recordError(err);
62
110
  } finally {
63
111
  reader.releaseLock();
112
+ }
113
+ if (caughtError) {
114
+ await discardTempFile(writer, tempPath);
115
+ throw caughtError;
116
+ }
117
+ try {
64
118
  await new Promise((resolve, reject) => {
65
- writer.end(() => resolve());
119
+ writer.end((err) => {
120
+ if (err) reject(err);
121
+ else resolve();
122
+ });
66
123
  writer.once("error", reject);
67
124
  });
125
+ await rename(tempPath, path);
126
+ } catch (err) {
127
+ await discardTempFile(writer, tempPath);
128
+ throw err instanceof Error ? err : new Error(String(err));
68
129
  }
69
130
  return bytesWritten;
70
131
  }
@@ -222,6 +283,9 @@ function wrapStreamWithCleanup(source, cleanup) {
222
283
  });
223
284
  }
224
285
  var KyselyAttachmentStore = class {
286
+ db;
287
+ transport;
288
+ basePath;
225
289
  activeReaders = /* @__PURE__ */ new Map();
226
290
  constructor(db, transport, basePath) {
227
291
  this.db = db;
@@ -250,18 +314,10 @@ var KyselyAttachmentStore = class {
250
314
  async has(hash) {
251
315
  return (await this.db.selectFrom("attachment").select("status").where("hash", "=", hash).executeTakeFirst())?.status === "available";
252
316
  }
253
- async get(hash, signal) {
317
+ async get(hash, signal, documentId) {
254
318
  const row = await this.db.selectFrom("attachment").selectAll().where("hash", "=", hash).executeTakeFirst();
255
319
  if (row) {
256
- if (row.status === "evicted") {
257
- const remote = await this.transport.fetch(hash, signal);
258
- if (remote.kind === "data") {
259
- await this.put(hash, remote.response.metadata, remote.response.body);
260
- return this.get(hash, signal);
261
- }
262
- if (remote.kind === "pending") throw new AttachmentPending(hash, remote.expiresAtUtc);
263
- throw new AttachmentNotFound(hash);
264
- }
320
+ if (row.status === "evicted") return this.fetchRemote(hash, signal, documentId);
265
321
  const now = (/* @__PURE__ */ new Date()).toISOString();
266
322
  await this.db.updateTable("attachment").set({ last_accessed_at_utc: now }).where("hash", "=", hash).execute();
267
323
  const header = rowToHeader$1(row);
@@ -279,13 +335,7 @@ var KyselyAttachmentStore = class {
279
335
  fileName: pending.file_name,
280
336
  sizeBytes: pending.size_bytes
281
337
  });
282
- const remote = await this.transport.fetch(hash, signal);
283
- if (remote.kind === "data") {
284
- await this.put(hash, remote.response.metadata, remote.response.body);
285
- return this.get(hash, signal);
286
- }
287
- if (remote.kind === "pending") throw new AttachmentPending(hash, remote.expiresAtUtc);
288
- throw new AttachmentNotFound(hash);
338
+ return this.fetchRemote(hash, signal, documentId);
289
339
  }
290
340
  async put(hash, metadata, data) {
291
341
  const existing = await this.db.selectFrom("attachment").select(["hash", "status"]).where("hash", "=", hash).executeTakeFirst();
@@ -325,6 +375,16 @@ var KyselyAttachmentStore = class {
325
375
  const result = await this.db.selectFrom("attachment").select(sql`COALESCE(SUM(size_bytes), 0)`.as("total")).where("status", "=", "available").executeTakeFirst();
326
376
  return Number(result?.total ?? 0);
327
377
  }
378
+ async fetchRemote(hash, signal, documentId) {
379
+ if (documentId === void 0) throw new AttachmentNotFound(hash);
380
+ const remote = await this.transport.fetch(hash, documentId, signal);
381
+ if (remote.kind === "data") {
382
+ await this.put(hash, remote.response.metadata, remote.response.body);
383
+ return this.get(hash, signal);
384
+ }
385
+ if (remote.kind === "pending") throw new AttachmentPending(hash, remote.expiresAtUtc);
386
+ throw new AttachmentNotFound(hash);
387
+ }
328
388
  async findPendingReservation(hash, now) {
329
389
  const row = await this.db.selectFrom("attachment_reservation as r").leftJoin("attachment as a", "a.hash", "r.client_hash").select([
330
390
  "r.mime_type",
@@ -358,7 +418,7 @@ var KyselyAttachmentStore = class {
358
418
  };
359
419
  //#endregion
360
420
  //#region src/storage/kysely/reservation-store.ts
361
- const DEFAULT_RESERVATION_TTL_MS = 1440 * 60 * 1e3;
421
+ const DEFAULT_RESERVATION_TTL_MS = 864e5;
362
422
  function rowToReservation(row) {
363
423
  return {
364
424
  reservationId: row.reservation_id,
@@ -372,6 +432,7 @@ function rowToReservation(row) {
372
432
  };
373
433
  }
374
434
  var KyselyReservationStore = class {
435
+ db;
375
436
  ttlMs;
376
437
  constructor(db, ttlMs = DEFAULT_RESERVATION_TTL_MS) {
377
438
  this.db = db;
@@ -561,6 +622,11 @@ function rowToHeader(row) {
561
622
  };
562
623
  }
563
624
  var DirectAttachmentUpload = class {
625
+ reservation;
626
+ db;
627
+ basePath;
628
+ reservations;
629
+ maxBytes;
564
630
  reservationId;
565
631
  ref;
566
632
  expiresAtUtc;
@@ -577,7 +643,7 @@ var DirectAttachmentUpload = class {
577
643
  async send(data, options) {
578
644
  if (this.reservation.clientHash != null && this.reservation.sizeBytes == null) throw new Error("hash-first reservation missing sizeBytes");
579
645
  const declaredSizeBytes = this.reservation.clientHash != null ? this.reservation.sizeBytes ?? void 0 : void 0;
580
- const { tempPath, hash, sizeBytes } = await streamHashAndWrite(this.basePath, data, {
646
+ const { tempPath, hash, sizeBytes } = await streamHashAndWrite(this.basePath, isBlob(data) ? blobStream(data) : data, {
581
647
  maxBytes: this.maxBytes,
582
648
  declaredSizeBytes,
583
649
  ...options?.signal ? { signal: options.signal } : {}
@@ -630,6 +696,10 @@ var DirectAttachmentUpload = class {
630
696
  //#endregion
631
697
  //#region src/direct/direct-attachment-upload-factory.ts
632
698
  var DirectAttachmentUploadFactory = class {
699
+ db;
700
+ basePath;
701
+ reservations;
702
+ maxBytes;
633
703
  constructor(db, basePath, reservations, maxBytes) {
634
704
  this.db = db;
635
705
  this.basePath = basePath;
@@ -648,6 +718,8 @@ var DirectAttachmentUploadFactory = class {
648
718
  * can never accidentally return a direct-provider target.
649
719
  */
650
720
  var FilesystemAttachmentBackend = class {
721
+ store;
722
+ config;
651
723
  kind = "filesystem";
652
724
  constructor(store, config) {
653
725
  this.store = store;
@@ -687,7 +759,7 @@ var S3AttachmentUpload = class {
687
759
  this.uploadTarget = reservation.uploadTarget;
688
760
  }
689
761
  async send(data) {
690
- await data.cancel().catch(() => {});
762
+ if (!isBlob(data)) await data.cancel().catch(() => {});
691
763
  throw new Error("S3 attachment upload must use the presigned target");
692
764
  }
693
765
  };
@@ -699,6 +771,8 @@ var S3AttachmentUploadFactory = class {
699
771
  //#endregion
700
772
  //#region src/attachment-builder.ts
701
773
  var AttachmentBuilder = class {
774
+ db;
775
+ storagePath;
702
776
  transport = new NullAttachmentTransport();
703
777
  customUploadFactory;
704
778
  maxUploadBytes;
@@ -769,266 +843,59 @@ var AttachmentBuilder = class {
769
843
  }
770
844
  };
771
845
  //#endregion
772
- //#region src/reference-index/attachment-schema-compiler.ts
773
- const ATTACHMENT_REF_TYPE = "AttachmentRef";
774
- const CODEGEN_SCALAR_NAMES = new Set([
775
- "Unknown",
776
- "DateTime",
777
- "Address",
778
- ATTACHMENT_REF_TYPE,
779
- ...Object.keys(generatorTypeDefs)
780
- ]);
781
- function describeContext(context) {
782
- return `document type "${context.documentType}", version ${context.version}, action "${context.actionType}"`;
783
- }
784
- function compilationError(context, reason) {
785
- return /* @__PURE__ */ new Error(`Attachment schema compilation failed for ${describeContext(context)}: ${reason}`);
786
- }
787
- function extractionError(context, path, reason) {
788
- return /* @__PURE__ */ new Error(`Attachment extraction failed for ${describeContext(context)} at ${path}: ${reason}`);
789
- }
790
- function applicableSpecification(module, context) {
791
- const matches = module.documentModel.global.specifications.filter((specification) => specification.version === context.version);
792
- if (matches.length !== 1) throw compilationError(context, matches.length === 0 ? "the module has no matching specification" : "the module has multiple matching specifications");
793
- return matches[0];
794
- }
795
- function parseOperations(specification, context) {
796
- return specification.modules.flatMap((moduleSpecification) => moduleSpecification.operations.map((operation) => {
797
- if (operation.schema === null) return {
798
- document: null,
799
- operation
800
- };
801
- try {
802
- return {
803
- document: parse(operation.schema),
804
- operation
805
- };
806
- } catch {
807
- throw compilationError(context, "an operation has invalid GraphQL SDL");
808
- }
809
- }));
810
- }
811
- function selectOperation(operations, context) {
812
- const matches = operations.filter(({ operation }) => operation.name !== null && constantCase(operation.name) === context.actionType);
813
- if (matches.length > 1) throw compilationError(context, "multiple operations map to the action");
814
- if (matches.length === 0) return null;
815
- return matches[0];
846
+ //#region src/read-models/attachment-reference/kysely-attachment-reference-store.ts
847
+ function encodeCursor(cursor) {
848
+ return JSON.stringify([cursor.documentId, cursor.ref]);
816
849
  }
817
- function buildEffectiveSchema(specification, context) {
818
- const scalarSchemas = Array.from(CODEGEN_SCALAR_NAMES, (name) => `scalar ${name}`);
819
- const stateSchemas = Object.values(specification.state).map((state) => state.schema);
820
- const operationSchemas = specification.modules.flatMap((moduleSpecification) => moduleSpecification.operations.flatMap((operation) => operation.schema === null ? [] : [operation.schema]));
850
+ function decodeCursor(cursor) {
851
+ let parsed;
821
852
  try {
822
- return buildASTSchema(dedupeTypeDefinitions(parse([
823
- ...scalarSchemas,
824
- ...stateSchemas,
825
- ...operationSchemas
826
- ].filter(Boolean).join("\n\n"))));
853
+ parsed = JSON.parse(cursor);
827
854
  } catch {
828
- throw compilationError(context, "the effective GraphQL schema is invalid");
829
- }
830
- }
831
- function dedupeTypeDefinitions(document) {
832
- const seen = /* @__PURE__ */ new Map();
833
- const definitions = document.definitions.filter((definition) => {
834
- if (!("name" in definition) || definition.name?.value === void 0 || definition.kind === Kind.SCALAR_TYPE_DEFINITION) return true;
835
- const name = definition.name.value;
836
- const printed = print(definition);
837
- const existing = seen.get(name);
838
- if (existing === void 0) {
839
- seen.set(name, printed);
840
- return true;
841
- }
842
- return existing !== printed;
843
- });
844
- return {
845
- ...document,
846
- definitions
847
- };
848
- }
849
- function attachmentReachableTypes(definitions) {
850
- const reachable = /* @__PURE__ */ new Set();
851
- let changed = true;
852
- while (changed) {
853
- changed = false;
854
- for (const [name, definition] of definitions) {
855
- if (reachable.has(name)) continue;
856
- if (Object.values(definition.getFields()).some((field) => {
857
- const typeName = getNamedType(field.type).name;
858
- return typeName === ATTACHMENT_REF_TYPE || definitions.has(typeName) && reachable.has(typeName);
859
- })) {
860
- reachable.add(name);
861
- changed = true;
862
- }
863
- }
855
+ throw new Error(`Invalid attachment reference scan cursor: ${JSON.stringify(cursor)}`);
864
856
  }
865
- return reachable;
866
- }
867
- function compileValuePlan(type, objectPlans) {
868
- if (isNonNullType(type)) return {
869
- ...compileValuePlan(type.ofType, objectPlans),
870
- required: true
871
- };
872
- if (isListType(type)) return {
873
- item: compileValuePlan(type.ofType, objectPlans),
874
- kind: "list",
875
- required: false
876
- };
877
- const typeName = type.name;
878
- if (typeName === ATTACHMENT_REF_TYPE) return {
879
- kind: "attachment",
880
- required: false
881
- };
882
- const body = objectPlans.get(typeName);
883
- if (!body) throw new Error(`Internal attachment schema plan error for ${typeName}`);
857
+ if (!Array.isArray(parsed) || parsed.length !== 2 || typeof parsed[0] !== "string" || parsed[0] === "" || typeof parsed[1] !== "string" || parsed[1] === "") throw new Error(`Invalid attachment reference scan cursor: ${JSON.stringify(cursor)}`);
884
858
  return {
885
- body,
886
- kind: "object",
887
- required: false
859
+ documentId: parsed[0],
860
+ ref: parsed[1]
888
861
  };
889
862
  }
890
- function compileRootPlan(rootName, definitions) {
891
- const reachable = attachmentReachableTypes(definitions);
892
- if (!reachable.has(rootName)) return null;
893
- const objectPlans = /* @__PURE__ */ new Map();
894
- for (const name of reachable) objectPlans.set(name, { fields: [] });
895
- for (const name of reachable) {
896
- const definition = definitions.get(name);
897
- const body = objectPlans.get(name);
898
- if (!definition || !body) continue;
899
- for (const field of Object.values(definition.getFields())) {
900
- const typeName = getNamedType(field.type).name;
901
- if (typeName !== ATTACHMENT_REF_TYPE && !reachable.has(typeName)) continue;
902
- body.fields.push({
903
- hasDefault: field.defaultValue !== void 0,
904
- name: field.name,
905
- value: compileValuePlan(field.type, objectPlans)
906
- });
907
- }
908
- }
909
- return objectPlans.get(rootName) ?? null;
910
- }
911
- function readOwnField(value, fieldName, context, path) {
912
- try {
913
- if (!Object.prototype.hasOwnProperty.call(value, fieldName)) return {
914
- present: false,
915
- value: void 0
916
- };
917
- return {
918
- present: true,
919
- value: value[fieldName]
920
- };
921
- } catch {
922
- throw extractionError(context, path, "the declared field cannot be read");
923
- }
924
- }
925
- function extractValue(plan, value, path, context, refs, seenRefs, activeObjects) {
926
- if (value === null || value === void 0) {
927
- if (plan.required) throw extractionError(context, path, "a required value is missing or null");
928
- return;
929
- }
930
- if (plan.kind === "attachment") {
931
- if (typeof value !== "string") throw extractionError(context, path, "expected an AttachmentRef string");
932
- try {
933
- parseRef(value);
934
- } catch {
935
- throw extractionError(context, path, "the AttachmentRef is malformed");
936
- }
937
- if (!seenRefs.has(value)) {
938
- seenRefs.add(value);
939
- refs.push(value);
940
- }
941
- return;
942
- }
943
- if (plan.kind === "list") {
944
- if (!Array.isArray(value)) throw extractionError(context, path, "expected a list");
945
- for (let index = 0; index < value.length; index += 1) extractValue(plan.item, value[index], `${path}[${index}]`, context, refs, seenRefs, activeObjects);
946
- return;
947
- }
948
- if (typeof value !== "object" || Array.isArray(value)) throw extractionError(context, path, "expected an input object");
949
- if (activeObjects.has(value)) throw extractionError(context, path, "the input value contains a cycle");
950
- activeObjects.add(value);
951
- try {
952
- const record = value;
953
- for (const field of plan.body.fields) {
954
- const fieldPath = `${path}.${field.name}`;
955
- const result = readOwnField(record, field.name, context, fieldPath);
956
- if ((!result.present || result.value === void 0) && field.hasDefault) continue;
957
- extractValue(field.value, result.value, fieldPath, context, refs, seenRefs, activeObjects);
958
- }
959
- } finally {
960
- activeObjects.delete(value);
961
- }
962
- }
963
- var SchemaCompiledAttachmentExtractor = class {
964
- constructor(context, rootPlan) {
965
- this.context = context;
966
- this.rootPlan = rootPlan;
967
- }
968
- extract(action) {
969
- if (action.type !== this.context.actionType) throw extractionError(this.context, "input", "the action type does not match the compiled schema");
970
- if (!this.rootPlan) return [];
971
- if (action.input === null || typeof action.input !== "object" || Array.isArray(action.input)) throw extractionError(this.context, "input", "expected an input object");
972
- const refs = [];
973
- const seenRefs = /* @__PURE__ */ new Set();
974
- const activeObjects = /* @__PURE__ */ new WeakSet();
975
- activeObjects.add(action.input);
976
- try {
977
- const input = action.input;
978
- for (const field of this.rootPlan.fields) {
979
- const path = `input.${field.name}`;
980
- const result = readOwnField(input, field.name, this.context, path);
981
- if ((!result.present || result.value === void 0) && field.hasDefault) continue;
982
- extractValue(field.value, result.value, path, this.context, refs, seenRefs, activeObjects);
983
- }
984
- } finally {
985
- activeObjects.delete(action.input);
986
- }
987
- return refs;
988
- }
989
- };
990
- function compileExtractor(module, actionType) {
991
- const context = {
992
- actionType,
993
- documentType: module.documentModel.global.id,
994
- version: module.version ?? 1
995
- };
996
- const specification = applicableSpecification(module, context);
997
- const selected = selectOperation(parseOperations(specification, context), context);
998
- if (selected === null || selected.operation.schema === null) return new SchemaCompiledAttachmentExtractor(context, null);
999
- const effectiveSchema = buildEffectiveSchema(specification, context);
1000
- const operationName = selected.operation.name;
1001
- if (operationName === null) throw compilationError(context, "the operation has no name");
1002
- const rootName = `${pascalCase(operationName)}Input`;
1003
- const definitions = /* @__PURE__ */ new Map();
1004
- for (const type of Object.values(effectiveSchema.getTypeMap())) if (isInputObjectType(type) && !type.name.startsWith("__")) definitions.set(type.name, type);
1005
- if (!(selected.document?.definitions.filter((definition) => (definition.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION || definition.kind === Kind.INPUT_OBJECT_TYPE_EXTENSION) && definition.name.value === rootName))?.length || !definitions.has(rootName)) throw compilationError(context, `the operation does not declare its expected root input "${rootName}"`);
1006
- return new SchemaCompiledAttachmentExtractor(context, compileRootPlan(rootName, definitions));
1007
- }
1008
- var AttachmentSchemaCompiler = class {
1009
- cache = /* @__PURE__ */ new WeakMap();
1010
- forModuleAction(module, actionType) {
1011
- let moduleCache = this.cache.get(module);
1012
- if (!moduleCache) {
1013
- moduleCache = /* @__PURE__ */ new Map();
1014
- this.cache.set(module, moduleCache);
1015
- }
1016
- const cached = moduleCache.get(actionType);
1017
- if (cached) return cached;
1018
- const compiled = compileExtractor(module, actionType);
1019
- moduleCache.set(actionType, compiled);
1020
- return compiled;
1021
- }
1022
- };
1023
- //#endregion
1024
- //#region src/read-models/attachment-reference/kysely-attachment-reference-store.ts
1025
863
  var KyselyAttachmentReferenceStore = class {
864
+ db;
1026
865
  constructor(db) {
1027
866
  this.db = db;
1028
867
  }
1029
868
  async hasReference(documentId, ref) {
1030
869
  return await this.db.selectFrom("attachment_reference").select("document_id").where("document_id", "=", documentId).where("attachment_ref", "=", ref).executeTakeFirst() !== void 0;
1031
870
  }
871
+ async referencingScopes(documentId, ref) {
872
+ return (await this.db.selectFrom("attachment_reference").select("scope").distinct().where("document_id", "=", documentId).where("attachment_ref", "=", ref).execute()).map((row) => row.scope);
873
+ }
874
+ /**
875
+ * Keyset paging over the unique `(document_id, attachment_ref)` constraint,
876
+ * rather than OFFSET: a scan that runs while the read model is still
877
+ * indexing must not skip or repeat a row because rows appeared behind the
878
+ * cursor. That is exactly the boot case the replicator's re-scan runs in.
879
+ */
880
+ async listReferences(cursor, limit) {
881
+ let query = this.db.selectFrom("attachment_reference").select(["document_id", "attachment_ref"]).orderBy("document_id", "asc").orderBy("attachment_ref", "asc").limit(limit);
882
+ if (cursor !== void 0) {
883
+ const after = decodeCursor(cursor);
884
+ query = query.where((eb) => eb.or([eb("document_id", ">", after.documentId), eb.and([eb("document_id", "=", after.documentId), eb("attachment_ref", ">", after.ref)])]));
885
+ }
886
+ const rows = await query.execute();
887
+ const last = rows.at(-1);
888
+ return {
889
+ references: rows.map((row) => ({
890
+ documentId: row.document_id,
891
+ ref: row.attachment_ref
892
+ })),
893
+ nextCursor: rows.length === limit && last ? encodeCursor({
894
+ documentId: last.document_id,
895
+ ref: last.attachment_ref
896
+ }) : void 0
897
+ };
898
+ }
1032
899
  async addReferences(references) {
1033
900
  if (references.length === 0) return;
1034
901
  await this.db.insertInto("attachment_reference").values(references.map((reference) => ({
@@ -1042,6 +909,10 @@ var KyselyAttachmentReferenceStore = class {
1042
909
  created_at_utc: (/* @__PURE__ */ new Date()).toISOString()
1043
910
  }))).onConflict((oc) => oc.columns(["document_id", "attachment_ref"]).doNothing()).execute();
1044
911
  }
912
+ async removeDocuments(documentIds) {
913
+ if (documentIds.length === 0) return;
914
+ await this.db.deleteFrom("attachment_reference").where("document_id", "in", [...documentIds]).execute();
915
+ }
1045
916
  };
1046
917
  //#endregion
1047
918
  //#region src/read-models/attachment-reference/storage/migrations/001_create_attachment_reference_table.ts
@@ -1120,6 +991,7 @@ async function getAttachmentReferenceMigrationStatus(db, schema = ATTACHMENT_REF
1120
991
  //#endregion
1121
992
  //#region src/read-models/attachment-reference/index-builder.ts
1122
993
  var AttachmentReferenceIndexBuilder = class {
994
+ db;
1123
995
  constructor(db) {
1124
996
  this.db = db;
1125
997
  }
@@ -1132,41 +1004,40 @@ var AttachmentReferenceIndexBuilder = class {
1132
1004
  //#endregion
1133
1005
  //#region src/read-models/attachment-reference/attachment-reference-read-model.ts
1134
1006
  const ATTACHMENT_REFERENCE_READ_MODEL_ID = "attachment-reference-read-model";
1007
+ /**
1008
+ * Indexes attachment references in chunks.
1009
+ *
1010
+ * Each reference row stands on its own and is written insert-or-do-nothing, so
1011
+ * a batch that commits in pieces exposes no partial structure, and re-indexing
1012
+ * the same range writes nothing new: the catch-up sweep needs no stream suffix.
1013
+ *
1014
+ * The rows live on another handle, so the purge fence is "skip": a tombstoned
1015
+ * id's operations are dropped at commit and its marker deletes its rows.
1016
+ */
1135
1017
  var AttachmentReferenceReadModel = class extends BaseReadModel {
1136
- indexingQueue = Promise.resolve();
1018
+ documentModelRegistry;
1019
+ schemaCompiler;
1020
+ referenceWriter;
1137
1021
  constructor(db, operationIndex, writeCache, consistencyTracker, documentModelRegistry, schemaCompiler, referenceWriter) {
1138
1022
  super(db, operationIndex, writeCache, consistencyTracker, {
1139
1023
  readModelId: ATTACHMENT_REFERENCE_READ_MODEL_ID,
1140
- rebuildStateOnInit: false
1024
+ rebuildStateOnInit: false,
1025
+ indexing: defaultReadModelIndexingConfig,
1026
+ replayStreamSuffix: false,
1027
+ purgeFence: "skip"
1141
1028
  });
1142
1029
  this.documentModelRegistry = documentModelRegistry;
1143
1030
  this.schemaCompiler = schemaCompiler;
1144
1031
  this.referenceWriter = referenceWriter;
1145
1032
  }
1146
- indexOperations(items) {
1147
- return this.enqueue(() => this.indexOperationsInOrdinalOrder(items));
1148
- }
1149
- init() {
1150
- return this.enqueue(async () => {
1151
- const viewState = await this.loadState();
1152
- if (viewState !== void 0) this.lastOrdinal = viewState;
1153
- else await this.initializeState();
1154
- let page = await this.operationIndex.getSinceOrdinal(this.lastOrdinal);
1155
- while (page.results.length > 0) {
1156
- await this.indexOperationsInOrdinalOrder(page.results);
1157
- if (!page.next) break;
1158
- page = await page.next();
1159
- }
1160
- });
1161
- }
1162
- enqueue(work) {
1163
- const result = this.indexingQueue.then(work);
1164
- this.indexingQueue = result.catch(() => void 0);
1165
- return result;
1166
- }
1167
1033
  async commitOperations(items) {
1168
1034
  const references = [];
1035
+ const purged = /* @__PURE__ */ new Set();
1169
1036
  for (const { operation, context } of items) {
1037
+ if (isPurgeMarker(operation)) {
1038
+ purged.add(context.documentId);
1039
+ continue;
1040
+ }
1170
1041
  if (operation.error !== void 0) continue;
1171
1042
  const module = this.documentModelRegistry.getModule(context.documentType);
1172
1043
  const refs = this.schemaCompiler.forModuleAction(module, operation.action.type).extract(operation.action);
@@ -1180,56 +1051,7 @@ var AttachmentReferenceReadModel = class extends BaseReadModel {
1180
1051
  });
1181
1052
  }
1182
1053
  if (references.length > 0) await this.referenceWriter.addReferences(references);
1183
- }
1184
- async indexOperationsInOrdinalOrder(incoming) {
1185
- const pending = this.sortAndDedupe(incoming.filter(({ context }) => context.ordinal > this.lastOrdinal));
1186
- if (pending.length === 0) return;
1187
- const incomingMax = pending[pending.length - 1].context.ordinal;
1188
- let candidates = pending;
1189
- if (!this.isContiguousThrough(candidates, incomingMax)) {
1190
- const replayed = await this.loadThroughOrdinal(incomingMax);
1191
- candidates = this.sortAndDedupe([...replayed, ...pending]);
1192
- }
1193
- const contiguous = [];
1194
- let expectedOrdinal = this.lastOrdinal + 1;
1195
- for (const item of candidates) {
1196
- const ordinal = item.context.ordinal;
1197
- if (ordinal < expectedOrdinal) continue;
1198
- if (ordinal > expectedOrdinal || ordinal > incomingMax) break;
1199
- contiguous.push(item);
1200
- expectedOrdinal++;
1201
- }
1202
- if (expectedOrdinal <= incomingMax) throw new Error(`Attachment reference read model cannot advance past missing ordinal ${expectedOrdinal}`);
1203
- const previousOrdinal = this.lastOrdinal;
1204
- try {
1205
- await super.indexOperations(contiguous);
1206
- } catch (error) {
1207
- this.lastOrdinal = previousOrdinal;
1208
- throw error;
1209
- }
1210
- }
1211
- isContiguousThrough(items, maxOrdinal) {
1212
- let expectedOrdinal = this.lastOrdinal + 1;
1213
- for (const item of items) {
1214
- if (item.context.ordinal !== expectedOrdinal) return false;
1215
- expectedOrdinal++;
1216
- }
1217
- return expectedOrdinal > maxOrdinal;
1218
- }
1219
- async loadThroughOrdinal(maxOrdinal) {
1220
- const operations = [];
1221
- let page = await this.operationIndex.getSinceOrdinal(this.lastOrdinal);
1222
- for (;;) {
1223
- for (const item of page.results) if (item.context.ordinal <= maxOrdinal) operations.push(item);
1224
- if (page.results.some(({ context }) => context.ordinal >= maxOrdinal) || !page.next) break;
1225
- page = await page.next();
1226
- }
1227
- return operations;
1228
- }
1229
- sortAndDedupe(items) {
1230
- const byOrdinal = /* @__PURE__ */ new Map();
1231
- for (const item of items) byOrdinal.set(item.context.ordinal, item);
1232
- return [...byOrdinal.values()].sort((left, right) => left.context.ordinal - right.context.ordinal);
1054
+ if (purged.size > 0) await this.referenceWriter.removeDocuments([...purged]);
1233
1055
  }
1234
1056
  };
1235
1057
  //#endregion
@@ -1244,7 +1066,7 @@ function required(env, name) {
1244
1066
  if (value.trim() !== value) throw new Error(`${name} must not have leading or trailing whitespace`);
1245
1067
  return value;
1246
1068
  }
1247
- const LOOPBACK_HOSTNAMES = new Set([
1069
+ const LOOPBACK_HOSTNAMES = /* @__PURE__ */ new Set([
1248
1070
  "127.0.0.1",
1249
1071
  "localhost",
1250
1072
  "[::1]"
@@ -1326,9 +1148,10 @@ function sha256HexToBase64(hash) {
1326
1148
  //#region src/storage/s3/primitives.ts
1327
1149
  const defaultPresigner = (client, command, expiresInSeconds) => getSignedUrl(client, command, {
1328
1150
  expiresIn: expiresInSeconds,
1329
- unhoistableHeaders: new Set(["x-amz-checksum-sha256"])
1151
+ unhoistableHeaders: /* @__PURE__ */ new Set(["x-amz-checksum-sha256"])
1330
1152
  });
1331
1153
  var S3AttachmentPrimitives = class {
1154
+ config;
1332
1155
  client;
1333
1156
  presign;
1334
1157
  now;
@@ -1399,7 +1222,7 @@ function createS3AttachmentPrimitives(config, dependencies = {}) {
1399
1222
  //#endregion
1400
1223
  //#region src/storage/s3/backend.ts
1401
1224
  const READINESS_PROBE_HASH = "0".repeat(64);
1402
- const OBJECT_NOT_FOUND_NAMES = new Set([
1225
+ const OBJECT_NOT_FOUND_NAMES = /* @__PURE__ */ new Set([
1403
1226
  "NotFound",
1404
1227
  "NoSuchKey",
1405
1228
  "NoSuchObject"
@@ -1413,10 +1236,12 @@ function isObjectNotFound(error) {
1413
1236
  function requireHashFirstReservation(reservation) {
1414
1237
  if (reservation.clientHash === null) throw new Error("S3 attachment reservations require a client hash");
1415
1238
  deriveS3AttachmentKey(reservation.clientHash, "validation");
1416
- if (reservation.sizeBytes === null || !Number.isSafeInteger(reservation.sizeBytes) || reservation.sizeBytes <= 0) throw new Error("S3 attachment reservation sizeBytes must be a positive safe integer");
1239
+ if (reservation.sizeBytes === null || !Number.isSafeInteger(reservation.sizeBytes) || reservation.sizeBytes < 0) throw new Error("S3 attachment reservation sizeBytes must be a non-negative safe integer");
1417
1240
  }
1418
1241
  /** Server-only S3 capability. Callers complete authorization before download. */
1419
1242
  var S3AttachmentBackend = class {
1243
+ db;
1244
+ config;
1420
1245
  kind = "s3";
1421
1246
  primitives;
1422
1247
  now;
@@ -1503,6 +1328,6 @@ function createS3AttachmentBackend(db, config, dependencies = {}) {
1503
1328
  return new S3AttachmentBackend(db, config, dependencies);
1504
1329
  }
1505
1330
  //#endregion
1506
- export { ATTACHMENT_REFERENCE_MIGRATION_LOCK_TABLE, ATTACHMENT_REFERENCE_MIGRATION_TABLE, ATTACHMENT_REFERENCE_READ_MODEL_ID, ATTACHMENT_REFERENCE_SCHEMA, ATTACHMENT_SCHEMA, AttachmentAlreadyExists, AttachmentBuilder, AttachmentNotFound, AttachmentPending, AttachmentReferenceIndexBuilder, AttachmentReferenceReadModel, AttachmentSchemaCompiler, AttachmentService, AttachmentTransferError, DEFAULT_RESERVATION_TTL_MS, DEFAULT_S3_ATTACHMENT_PREFIX, DEFAULT_S3_DOWNLOAD_TTL_SECONDS, DEFAULT_S3_UPLOAD_TTL_SECONDS, DirectAttachmentUpload, DirectAttachmentUploadFactory, FilesystemAttachmentBackend, HashMismatch, InvalidAttachmentRef, KyselyAttachmentReferenceStore, KyselyAttachmentStore, KyselyReservationStore, MAX_S3_PRESIGN_TTL_SECONDS, NullAttachmentTransport, RemoteAttachmentStore, RemoteAttachmentUpload, RemoteAttachmentUploadFactory, RemoteReservationStore, ReservationNotFound, S3AttachmentBackend, S3AttachmentPrimitives, S3AttachmentUploadFactory, SizeMismatch, SwitchboardAttachmentTransport, UploadTooLarge, createFetchUploadTransport, createRef, createRemoteAttachmentService, createS3AttachmentBackend, createS3AttachmentPrimitives, createXhrUploadTransport, deriveS3AttachmentKey, getAttachmentReferenceMigrationStatus, normalizeS3AttachmentPrefix, parseAttachmentDownloadTarget, parseAttachmentStorageConfig, parseAttachmentUploadTarget, parseRef, rollbackAttachmentReferenceMigration, runAttachmentMigrations, runAttachmentReferenceMigrations, sha256HexToBase64 };
1331
+ export { ATTACHMENT_REFERENCE_MIGRATION_LOCK_TABLE, ATTACHMENT_REFERENCE_MIGRATION_TABLE, ATTACHMENT_REFERENCE_READ_MODEL_ID, ATTACHMENT_REFERENCE_SCHEMA, ATTACHMENT_SCHEMA, AttachmentAlreadyExists, AttachmentBuilder, AttachmentNotFound, AttachmentPending, AttachmentReferenceIndexBuilder, AttachmentReferenceReadModel, AttachmentSchemaCompiler, AttachmentService, AttachmentTransferError, DEFAULT_RESERVATION_TTL_MS, DEFAULT_S3_ATTACHMENT_PREFIX, DEFAULT_S3_DOWNLOAD_TTL_SECONDS, DEFAULT_S3_UPLOAD_TTL_SECONDS, DirectAttachmentUpload, DirectAttachmentUploadFactory, FilesystemAttachmentBackend, HashMismatch, InvalidAttachmentMetadata, InvalidAttachmentRef, KyselyAttachmentReferenceStore, KyselyAttachmentStore, KyselyReservationStore, MAX_S3_PRESIGN_TTL_SECONDS, NullAttachmentTransport, RemoteAttachmentStore, RemoteAttachmentUpload, RemoteAttachmentUploadFactory, RemoteReservationStore, ReservationNotFound, RoutingAttachmentService, S3AttachmentBackend, S3AttachmentPrimitives, S3AttachmentUploadFactory, SizeMismatch, SwitchboardAttachmentTransport, UploadTooLarge, createFetchUploadTransport, createRef, createRemoteAttachmentService, createS3AttachmentBackend, createS3AttachmentPrimitives, createXhrUploadTransport, deriveS3AttachmentKey, getAttachmentReferenceMigrationStatus, normalizeS3AttachmentPrefix, parseAttachmentDownloadTarget, parseAttachmentStorageConfig, parseAttachmentUploadTarget, parseRef, rollbackAttachmentReferenceMigration, runAttachmentMigrations, runAttachmentReferenceMigrations, sha256HexToBase64, validateReserveMetadata };
1507
1332
 
1508
1333
  //# sourceMappingURL=index.js.map