@powerhousedao/reactor-attachments 6.2.3-staging.0 → 6.3.0-dev.1
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/attachment-schema-compiler-BytE6Hev.js +255 -0
- package/dist/attachment-schema-compiler-BytE6Hev.js.map +1 -0
- package/dist/client.d.ts +16 -6
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +20 -21
- package/dist/client.js.map +1 -1
- package/dist/hash-BobbR4r4.js +63 -0
- package/dist/hash-BobbR4r4.js.map +1 -0
- package/dist/index.d.ts +33 -31
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +79 -258
- package/dist/index.js.map +1 -1
- package/dist/{null-attachment-transport-BkM6rq4R.d.ts → interfaces-cFMtcMqR.d.ts} +15 -316
- package/dist/interfaces-cFMtcMqR.d.ts.map +1 -0
- package/dist/replication.d.ts +955 -0
- package/dist/replication.d.ts.map +1 -0
- package/dist/replication.js +1975 -0
- package/dist/replication.js.map +1 -0
- package/dist/{null-attachment-transport-D53Rz9lE.js → routing-attachment-service-C88ryRgj.js} +76 -272
- package/dist/routing-attachment-service-C88ryRgj.js.map +1 -0
- package/dist/routing-attachment-service-Dri6e2Vz.d.ts +343 -0
- package/dist/routing-attachment-service-Dri6e2Vz.d.ts.map +1 -0
- package/dist/switchboard-attachment-transport-BH3ZAmxE.js +284 -0
- package/dist/switchboard-attachment-transport-BH3ZAmxE.js.map +1 -0
- package/dist/types-AagIcjTD.d.ts +61 -0
- package/dist/types-AagIcjTD.d.ts.map +1 -0
- package/package.json +14 -5
- package/dist/null-attachment-transport-BkM6rq4R.d.ts.map +0 -1
- package/dist/null-attachment-transport-D53Rz9lE.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
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";
|
|
@@ -6,10 +8,8 @@ import { createReadStream, createWriteStream } from "node:fs";
|
|
|
6
8
|
import { createHash, randomUUID } from "node:crypto";
|
|
7
9
|
import { Readable } from "node:stream";
|
|
8
10
|
import { finished } from "node:stream/promises";
|
|
9
|
-
import { generatorTypeDefs } from "@powerhousedao/document-engineering/graphql";
|
|
10
|
-
import { constantCase, pascalCase } from "change-case";
|
|
11
|
-
import { Kind, buildASTSchema, getNamedType, isInputObjectType, isListType, isNonNullType, parse, print } from "graphql";
|
|
12
11
|
import { BaseReadModel, defaultReadModelIndexingConfig } from "@powerhousedao/reactor";
|
|
12
|
+
import { isPurgeMarker } from "@powerhousedao/shared/document-model";
|
|
13
13
|
import { Buffer as Buffer$1 } from "node:buffer";
|
|
14
14
|
import { GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
15
15
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
@@ -25,6 +25,27 @@ var __exportAll = (all, no_symbols) => {
|
|
|
25
25
|
return target;
|
|
26
26
|
};
|
|
27
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
|
|
28
49
|
//#region src/storage/fs/attachment-fs.ts
|
|
29
50
|
/**
|
|
30
51
|
* Compute the relative storage path for an attachment hash.
|
|
@@ -622,7 +643,7 @@ var DirectAttachmentUpload = class {
|
|
|
622
643
|
async send(data, options) {
|
|
623
644
|
if (this.reservation.clientHash != null && this.reservation.sizeBytes == null) throw new Error("hash-first reservation missing sizeBytes");
|
|
624
645
|
const declaredSizeBytes = this.reservation.clientHash != null ? this.reservation.sizeBytes ?? void 0 : void 0;
|
|
625
|
-
const { tempPath, hash, sizeBytes } = await streamHashAndWrite(this.basePath, data, {
|
|
646
|
+
const { tempPath, hash, sizeBytes } = await streamHashAndWrite(this.basePath, isBlob(data) ? blobStream(data) : data, {
|
|
626
647
|
maxBytes: this.maxBytes,
|
|
627
648
|
declaredSizeBytes,
|
|
628
649
|
...options?.signal ? { signal: options.signal } : {}
|
|
@@ -738,7 +759,7 @@ var S3AttachmentUpload = class {
|
|
|
738
759
|
this.uploadTarget = reservation.uploadTarget;
|
|
739
760
|
}
|
|
740
761
|
async send(data) {
|
|
741
|
-
await data.cancel().catch(() => {});
|
|
762
|
+
if (!isBlob(data)) await data.cancel().catch(() => {});
|
|
742
763
|
throw new Error("S3 attachment upload must use the presigned target");
|
|
743
764
|
}
|
|
744
765
|
};
|
|
@@ -822,262 +843,23 @@ var AttachmentBuilder = class {
|
|
|
822
843
|
}
|
|
823
844
|
};
|
|
824
845
|
//#endregion
|
|
825
|
-
//#region src/
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
"Unknown",
|
|
829
|
-
"DateTime",
|
|
830
|
-
"Address",
|
|
831
|
-
ATTACHMENT_REF_TYPE,
|
|
832
|
-
...Object.keys(generatorTypeDefs)
|
|
833
|
-
]);
|
|
834
|
-
function describeContext(context) {
|
|
835
|
-
return `document type "${context.documentType}", version ${context.version}, action "${context.actionType}"`;
|
|
836
|
-
}
|
|
837
|
-
function compilationError(context, reason) {
|
|
838
|
-
return /* @__PURE__ */ new Error(`Attachment schema compilation failed for ${describeContext(context)}: ${reason}`);
|
|
839
|
-
}
|
|
840
|
-
function extractionError(context, path, reason) {
|
|
841
|
-
return /* @__PURE__ */ new Error(`Attachment extraction failed for ${describeContext(context)} at ${path}: ${reason}`);
|
|
842
|
-
}
|
|
843
|
-
function applicableSpecification(module, context) {
|
|
844
|
-
const matches = module.documentModel.global.specifications.filter((specification) => specification.version === context.version);
|
|
845
|
-
if (matches.length !== 1) throw compilationError(context, matches.length === 0 ? "the module has no matching specification" : "the module has multiple matching specifications");
|
|
846
|
-
return matches[0];
|
|
847
|
-
}
|
|
848
|
-
function parseOperations(specification, context) {
|
|
849
|
-
return specification.modules.flatMap((moduleSpecification) => moduleSpecification.operations.map((operation) => {
|
|
850
|
-
if (operation.schema === null) return {
|
|
851
|
-
document: null,
|
|
852
|
-
operation
|
|
853
|
-
};
|
|
854
|
-
try {
|
|
855
|
-
return {
|
|
856
|
-
document: parse(operation.schema),
|
|
857
|
-
operation
|
|
858
|
-
};
|
|
859
|
-
} catch {
|
|
860
|
-
throw compilationError(context, "an operation has invalid GraphQL SDL");
|
|
861
|
-
}
|
|
862
|
-
}));
|
|
863
|
-
}
|
|
864
|
-
function selectOperation(operations, context) {
|
|
865
|
-
const matches = operations.filter(({ operation }) => operation.name !== null && constantCase(operation.name) === context.actionType);
|
|
866
|
-
if (matches.length > 1) throw compilationError(context, "multiple operations map to the action");
|
|
867
|
-
if (matches.length === 0) return null;
|
|
868
|
-
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]);
|
|
869
849
|
}
|
|
870
|
-
function
|
|
871
|
-
|
|
872
|
-
const stateSchemas = Object.values(specification.state).map((state) => state.schema);
|
|
873
|
-
const operationSchemas = specification.modules.flatMap((moduleSpecification) => moduleSpecification.operations.flatMap((operation) => operation.schema === null ? [] : [operation.schema]));
|
|
850
|
+
function decodeCursor(cursor) {
|
|
851
|
+
let parsed;
|
|
874
852
|
try {
|
|
875
|
-
|
|
876
|
-
...scalarSchemas,
|
|
877
|
-
...stateSchemas,
|
|
878
|
-
...operationSchemas
|
|
879
|
-
].filter(Boolean).join("\n\n"));
|
|
880
|
-
return buildASTSchema(dedupeTypeDefinitions(document));
|
|
853
|
+
parsed = JSON.parse(cursor);
|
|
881
854
|
} catch {
|
|
882
|
-
throw
|
|
883
|
-
}
|
|
884
|
-
}
|
|
885
|
-
function dedupeTypeDefinitions(document) {
|
|
886
|
-
const seen = /* @__PURE__ */ new Map();
|
|
887
|
-
const definitions = document.definitions.filter((definition) => {
|
|
888
|
-
if (!("name" in definition) || definition.name?.value === void 0 || definition.kind === Kind.SCALAR_TYPE_DEFINITION) return true;
|
|
889
|
-
const name = definition.name.value;
|
|
890
|
-
const printed = print(definition);
|
|
891
|
-
const existing = seen.get(name);
|
|
892
|
-
if (existing === void 0) {
|
|
893
|
-
seen.set(name, printed);
|
|
894
|
-
return true;
|
|
895
|
-
}
|
|
896
|
-
return existing !== printed;
|
|
897
|
-
});
|
|
898
|
-
return {
|
|
899
|
-
...document,
|
|
900
|
-
definitions
|
|
901
|
-
};
|
|
902
|
-
}
|
|
903
|
-
function attachmentReachableTypes(definitions) {
|
|
904
|
-
const reachable = /* @__PURE__ */ new Set();
|
|
905
|
-
let changed = true;
|
|
906
|
-
while (changed) {
|
|
907
|
-
changed = false;
|
|
908
|
-
for (const [name, definition] of definitions) {
|
|
909
|
-
if (reachable.has(name)) continue;
|
|
910
|
-
if (Object.values(definition.getFields()).some((field) => {
|
|
911
|
-
const typeName = getNamedType(field.type).name;
|
|
912
|
-
return typeName === ATTACHMENT_REF_TYPE || definitions.has(typeName) && reachable.has(typeName);
|
|
913
|
-
})) {
|
|
914
|
-
reachable.add(name);
|
|
915
|
-
changed = true;
|
|
916
|
-
}
|
|
917
|
-
}
|
|
855
|
+
throw new Error(`Invalid attachment reference scan cursor: ${JSON.stringify(cursor)}`);
|
|
918
856
|
}
|
|
919
|
-
|
|
920
|
-
}
|
|
921
|
-
function compileValuePlan(type, objectPlans) {
|
|
922
|
-
if (isNonNullType(type)) return {
|
|
923
|
-
...compileValuePlan(type.ofType, objectPlans),
|
|
924
|
-
required: true
|
|
925
|
-
};
|
|
926
|
-
if (isListType(type)) return {
|
|
927
|
-
item: compileValuePlan(type.ofType, objectPlans),
|
|
928
|
-
kind: "list",
|
|
929
|
-
required: false
|
|
930
|
-
};
|
|
931
|
-
const typeName = type.name;
|
|
932
|
-
if (typeName === ATTACHMENT_REF_TYPE) return {
|
|
933
|
-
kind: "attachment",
|
|
934
|
-
required: false
|
|
935
|
-
};
|
|
936
|
-
const body = objectPlans.get(typeName);
|
|
937
|
-
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)}`);
|
|
938
858
|
return {
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
required: false
|
|
942
|
-
};
|
|
943
|
-
}
|
|
944
|
-
function compileRootPlan(rootName, definitions) {
|
|
945
|
-
const reachable = attachmentReachableTypes(definitions);
|
|
946
|
-
if (!reachable.has(rootName)) return null;
|
|
947
|
-
const objectPlans = /* @__PURE__ */ new Map();
|
|
948
|
-
for (const name of reachable) objectPlans.set(name, { fields: [] });
|
|
949
|
-
for (const name of reachable) {
|
|
950
|
-
const definition = definitions.get(name);
|
|
951
|
-
const body = objectPlans.get(name);
|
|
952
|
-
if (!definition || !body) continue;
|
|
953
|
-
for (const field of Object.values(definition.getFields())) {
|
|
954
|
-
const typeName = getNamedType(field.type).name;
|
|
955
|
-
if (typeName !== ATTACHMENT_REF_TYPE && !reachable.has(typeName)) continue;
|
|
956
|
-
body.fields.push({
|
|
957
|
-
hasDefault: field.defaultValue !== void 0,
|
|
958
|
-
name: field.name,
|
|
959
|
-
value: compileValuePlan(field.type, objectPlans)
|
|
960
|
-
});
|
|
961
|
-
}
|
|
962
|
-
}
|
|
963
|
-
return objectPlans.get(rootName) ?? null;
|
|
964
|
-
}
|
|
965
|
-
function readOwnField(value, fieldName, context, path) {
|
|
966
|
-
try {
|
|
967
|
-
if (!Object.prototype.hasOwnProperty.call(value, fieldName)) return {
|
|
968
|
-
present: false,
|
|
969
|
-
value: void 0
|
|
970
|
-
};
|
|
971
|
-
return {
|
|
972
|
-
present: true,
|
|
973
|
-
value: value[fieldName]
|
|
974
|
-
};
|
|
975
|
-
} catch {
|
|
976
|
-
throw extractionError(context, path, "the declared field cannot be read");
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
function extractValue(plan, value, path, context, refs, seenRefs, activeObjects) {
|
|
980
|
-
if (value === null || value === void 0) {
|
|
981
|
-
if (plan.required) throw extractionError(context, path, "a required value is missing or null");
|
|
982
|
-
return;
|
|
983
|
-
}
|
|
984
|
-
if (plan.kind === "attachment") {
|
|
985
|
-
if (typeof value !== "string") throw extractionError(context, path, "expected an AttachmentRef string");
|
|
986
|
-
try {
|
|
987
|
-
parseRef(value);
|
|
988
|
-
} catch {
|
|
989
|
-
throw extractionError(context, path, "the AttachmentRef is malformed");
|
|
990
|
-
}
|
|
991
|
-
if (!seenRefs.has(value)) {
|
|
992
|
-
seenRefs.add(value);
|
|
993
|
-
refs.push(value);
|
|
994
|
-
}
|
|
995
|
-
return;
|
|
996
|
-
}
|
|
997
|
-
if (plan.kind === "list") {
|
|
998
|
-
if (!Array.isArray(value)) throw extractionError(context, path, "expected a list");
|
|
999
|
-
for (let index = 0; index < value.length; index += 1) extractValue(plan.item, value[index], `${path}[${index}]`, context, refs, seenRefs, activeObjects);
|
|
1000
|
-
return;
|
|
1001
|
-
}
|
|
1002
|
-
if (typeof value !== "object" || Array.isArray(value)) throw extractionError(context, path, "expected an input object");
|
|
1003
|
-
if (activeObjects.has(value)) throw extractionError(context, path, "the input value contains a cycle");
|
|
1004
|
-
activeObjects.add(value);
|
|
1005
|
-
try {
|
|
1006
|
-
const record = value;
|
|
1007
|
-
for (const field of plan.body.fields) {
|
|
1008
|
-
const fieldPath = `${path}.${field.name}`;
|
|
1009
|
-
const result = readOwnField(record, field.name, context, fieldPath);
|
|
1010
|
-
if ((!result.present || result.value === void 0) && field.hasDefault) continue;
|
|
1011
|
-
extractValue(field.value, result.value, fieldPath, context, refs, seenRefs, activeObjects);
|
|
1012
|
-
}
|
|
1013
|
-
} finally {
|
|
1014
|
-
activeObjects.delete(value);
|
|
1015
|
-
}
|
|
1016
|
-
}
|
|
1017
|
-
var SchemaCompiledAttachmentExtractor = class {
|
|
1018
|
-
context;
|
|
1019
|
-
rootPlan;
|
|
1020
|
-
constructor(context, rootPlan) {
|
|
1021
|
-
this.context = context;
|
|
1022
|
-
this.rootPlan = rootPlan;
|
|
1023
|
-
}
|
|
1024
|
-
extract(action) {
|
|
1025
|
-
if (action.type !== this.context.actionType) throw extractionError(this.context, "input", "the action type does not match the compiled schema");
|
|
1026
|
-
if (!this.rootPlan) return [];
|
|
1027
|
-
if (action.input === null || typeof action.input !== "object" || Array.isArray(action.input)) throw extractionError(this.context, "input", "expected an input object");
|
|
1028
|
-
const refs = [];
|
|
1029
|
-
const seenRefs = /* @__PURE__ */ new Set();
|
|
1030
|
-
const activeObjects = /* @__PURE__ */ new WeakSet();
|
|
1031
|
-
activeObjects.add(action.input);
|
|
1032
|
-
try {
|
|
1033
|
-
const input = action.input;
|
|
1034
|
-
for (const field of this.rootPlan.fields) {
|
|
1035
|
-
const path = `input.${field.name}`;
|
|
1036
|
-
const result = readOwnField(input, field.name, this.context, path);
|
|
1037
|
-
if ((!result.present || result.value === void 0) && field.hasDefault) continue;
|
|
1038
|
-
extractValue(field.value, result.value, path, this.context, refs, seenRefs, activeObjects);
|
|
1039
|
-
}
|
|
1040
|
-
} finally {
|
|
1041
|
-
activeObjects.delete(action.input);
|
|
1042
|
-
}
|
|
1043
|
-
return refs;
|
|
1044
|
-
}
|
|
1045
|
-
};
|
|
1046
|
-
function compileExtractor(module, actionType) {
|
|
1047
|
-
const context = {
|
|
1048
|
-
actionType,
|
|
1049
|
-
documentType: module.documentModel.global.id,
|
|
1050
|
-
version: module.version ?? 1
|
|
859
|
+
documentId: parsed[0],
|
|
860
|
+
ref: parsed[1]
|
|
1051
861
|
};
|
|
1052
|
-
const specification = applicableSpecification(module, context);
|
|
1053
|
-
const selected = selectOperation(parseOperations(specification, context), context);
|
|
1054
|
-
if (selected === null || selected.operation.schema === null) return new SchemaCompiledAttachmentExtractor(context, null);
|
|
1055
|
-
const effectiveSchema = buildEffectiveSchema(specification, context);
|
|
1056
|
-
const operationName = selected.operation.name;
|
|
1057
|
-
if (operationName === null) throw compilationError(context, "the operation has no name");
|
|
1058
|
-
const rootName = `${pascalCase(operationName)}Input`;
|
|
1059
|
-
const definitions = /* @__PURE__ */ new Map();
|
|
1060
|
-
for (const type of Object.values(effectiveSchema.getTypeMap())) if (isInputObjectType(type) && !type.name.startsWith("__")) definitions.set(type.name, type);
|
|
1061
|
-
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}"`);
|
|
1062
|
-
return new SchemaCompiledAttachmentExtractor(context, compileRootPlan(rootName, definitions));
|
|
1063
862
|
}
|
|
1064
|
-
var AttachmentSchemaCompiler = class {
|
|
1065
|
-
cache = /* @__PURE__ */ new WeakMap();
|
|
1066
|
-
forModuleAction(module, actionType) {
|
|
1067
|
-
let moduleCache = this.cache.get(module);
|
|
1068
|
-
if (!moduleCache) {
|
|
1069
|
-
moduleCache = /* @__PURE__ */ new Map();
|
|
1070
|
-
this.cache.set(module, moduleCache);
|
|
1071
|
-
}
|
|
1072
|
-
const cached = moduleCache.get(actionType);
|
|
1073
|
-
if (cached) return cached;
|
|
1074
|
-
const compiled = compileExtractor(module, actionType);
|
|
1075
|
-
moduleCache.set(actionType, compiled);
|
|
1076
|
-
return compiled;
|
|
1077
|
-
}
|
|
1078
|
-
};
|
|
1079
|
-
//#endregion
|
|
1080
|
-
//#region src/read-models/attachment-reference/kysely-attachment-reference-store.ts
|
|
1081
863
|
var KyselyAttachmentReferenceStore = class {
|
|
1082
864
|
db;
|
|
1083
865
|
constructor(db) {
|
|
@@ -1089,6 +871,31 @@ var KyselyAttachmentReferenceStore = class {
|
|
|
1089
871
|
async referencingScopes(documentId, ref) {
|
|
1090
872
|
return (await this.db.selectFrom("attachment_reference").select("scope").distinct().where("document_id", "=", documentId).where("attachment_ref", "=", ref).execute()).map((row) => row.scope);
|
|
1091
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
|
+
}
|
|
1092
899
|
async addReferences(references) {
|
|
1093
900
|
if (references.length === 0) return;
|
|
1094
901
|
await this.db.insertInto("attachment_reference").values(references.map((reference) => ({
|
|
@@ -1102,6 +909,10 @@ var KyselyAttachmentReferenceStore = class {
|
|
|
1102
909
|
created_at_utc: (/* @__PURE__ */ new Date()).toISOString()
|
|
1103
910
|
}))).onConflict((oc) => oc.columns(["document_id", "attachment_ref"]).doNothing()).execute();
|
|
1104
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
|
+
}
|
|
1105
916
|
};
|
|
1106
917
|
//#endregion
|
|
1107
918
|
//#region src/read-models/attachment-reference/storage/migrations/001_create_attachment_reference_table.ts
|
|
@@ -1199,6 +1010,9 @@ const ATTACHMENT_REFERENCE_READ_MODEL_ID = "attachment-reference-read-model";
|
|
|
1199
1010
|
* Each reference row stands on its own and is written insert-or-do-nothing, so
|
|
1200
1011
|
* a batch that commits in pieces exposes no partial structure, and re-indexing
|
|
1201
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.
|
|
1202
1016
|
*/
|
|
1203
1017
|
var AttachmentReferenceReadModel = class extends BaseReadModel {
|
|
1204
1018
|
documentModelRegistry;
|
|
@@ -1209,7 +1023,8 @@ var AttachmentReferenceReadModel = class extends BaseReadModel {
|
|
|
1209
1023
|
readModelId: ATTACHMENT_REFERENCE_READ_MODEL_ID,
|
|
1210
1024
|
rebuildStateOnInit: false,
|
|
1211
1025
|
indexing: defaultReadModelIndexingConfig,
|
|
1212
|
-
replayStreamSuffix: false
|
|
1026
|
+
replayStreamSuffix: false,
|
|
1027
|
+
purgeFence: "skip"
|
|
1213
1028
|
});
|
|
1214
1029
|
this.documentModelRegistry = documentModelRegistry;
|
|
1215
1030
|
this.schemaCompiler = schemaCompiler;
|
|
@@ -1217,7 +1032,12 @@ var AttachmentReferenceReadModel = class extends BaseReadModel {
|
|
|
1217
1032
|
}
|
|
1218
1033
|
async commitOperations(items) {
|
|
1219
1034
|
const references = [];
|
|
1035
|
+
const purged = /* @__PURE__ */ new Set();
|
|
1220
1036
|
for (const { operation, context } of items) {
|
|
1037
|
+
if (isPurgeMarker(operation)) {
|
|
1038
|
+
purged.add(context.documentId);
|
|
1039
|
+
continue;
|
|
1040
|
+
}
|
|
1221
1041
|
if (operation.error !== void 0) continue;
|
|
1222
1042
|
const module = this.documentModelRegistry.getModule(context.documentType);
|
|
1223
1043
|
const refs = this.schemaCompiler.forModuleAction(module, operation.action.type).extract(operation.action);
|
|
@@ -1231,6 +1051,7 @@ var AttachmentReferenceReadModel = class extends BaseReadModel {
|
|
|
1231
1051
|
});
|
|
1232
1052
|
}
|
|
1233
1053
|
if (references.length > 0) await this.referenceWriter.addReferences(references);
|
|
1054
|
+
if (purged.size > 0) await this.referenceWriter.removeDocuments([...purged]);
|
|
1234
1055
|
}
|
|
1235
1056
|
};
|
|
1236
1057
|
//#endregion
|
|
@@ -1415,7 +1236,7 @@ function isObjectNotFound(error) {
|
|
|
1415
1236
|
function requireHashFirstReservation(reservation) {
|
|
1416
1237
|
if (reservation.clientHash === null) throw new Error("S3 attachment reservations require a client hash");
|
|
1417
1238
|
deriveS3AttachmentKey(reservation.clientHash, "validation");
|
|
1418
|
-
if (reservation.sizeBytes === null || !Number.isSafeInteger(reservation.sizeBytes) || reservation.sizeBytes
|
|
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");
|
|
1419
1240
|
}
|
|
1420
1241
|
/** Server-only S3 capability. Callers complete authorization before download. */
|
|
1421
1242
|
var S3AttachmentBackend = class {
|
|
@@ -1507,6 +1328,6 @@ function createS3AttachmentBackend(db, config, dependencies = {}) {
|
|
|
1507
1328
|
return new S3AttachmentBackend(db, config, dependencies);
|
|
1508
1329
|
}
|
|
1509
1330
|
//#endregion
|
|
1510
|
-
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 };
|
|
1511
1332
|
|
|
1512
1333
|
//# sourceMappingURL=index.js.map
|