@powerhousedao/reactor-attachments 6.2.3-dev.9 → 6.2.3-dev.91
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 +65 -40
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +38 -23
- 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 +81 -111
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +134 -406
- package/dist/index.js.map +1 -1
- package/dist/{null-attachment-transport-CMrO_ZKA.d.ts → interfaces-cFMtcMqR.d.ts} +38 -327
- 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-CrsUafCi.js → routing-attachment-service-C88ryRgj.js} +93 -282
- 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 +17 -8
- package/dist/null-attachment-transport-CMrO_ZKA.d.ts.map +0 -1
- package/dist/null-attachment-transport-CrsUafCi.js.map +0 -1
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { a as parseRef } from "./switchboard-attachment-transport-BH3ZAmxE.js";
|
|
2
|
+
import { documentModelScalars } from "document-model/scalars";
|
|
3
|
+
import { constantCase, pascalCase } from "change-case";
|
|
4
|
+
import { Kind, buildASTSchema, getNamedType, isInputObjectType, isListType, isNonNullType, parse, print } from "graphql";
|
|
5
|
+
//#region src/reference-index/attachment-schema-compiler.ts
|
|
6
|
+
const ATTACHMENT_REF_TYPE = "AttachmentRef";
|
|
7
|
+
const MODEL_SCALAR_SCHEMAS = documentModelScalars.map(({ name }) => `scalar ${name}`);
|
|
8
|
+
function describeContext(context) {
|
|
9
|
+
return `document type "${context.documentType}", version ${context.version}, action "${context.actionType}"`;
|
|
10
|
+
}
|
|
11
|
+
function compilationError(context, reason) {
|
|
12
|
+
return /* @__PURE__ */ new Error(`Attachment schema compilation failed for ${describeContext(context)}: ${reason}`);
|
|
13
|
+
}
|
|
14
|
+
function extractionError(context, path, reason) {
|
|
15
|
+
return /* @__PURE__ */ new Error(`Attachment extraction failed for ${describeContext(context)} at ${path}: ${reason}`);
|
|
16
|
+
}
|
|
17
|
+
function applicableSpecification(module, context) {
|
|
18
|
+
const matches = module.documentModel.global.specifications.filter((specification) => specification.version === context.version);
|
|
19
|
+
if (matches.length !== 1) throw compilationError(context, matches.length === 0 ? "the module has no matching specification" : "the module has multiple matching specifications");
|
|
20
|
+
return matches[0];
|
|
21
|
+
}
|
|
22
|
+
function parseOperations(specification, context) {
|
|
23
|
+
return specification.modules.flatMap((moduleSpecification) => moduleSpecification.operations.map((operation) => {
|
|
24
|
+
if (operation.schema === null) return {
|
|
25
|
+
document: null,
|
|
26
|
+
operation
|
|
27
|
+
};
|
|
28
|
+
try {
|
|
29
|
+
return {
|
|
30
|
+
document: parse(operation.schema),
|
|
31
|
+
operation
|
|
32
|
+
};
|
|
33
|
+
} catch {
|
|
34
|
+
throw compilationError(context, "an operation has invalid GraphQL SDL");
|
|
35
|
+
}
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
function selectOperation(operations, context) {
|
|
39
|
+
const matches = operations.filter(({ operation }) => operation.name !== null && constantCase(operation.name) === context.actionType);
|
|
40
|
+
if (matches.length > 1) throw compilationError(context, "multiple operations map to the action");
|
|
41
|
+
if (matches.length === 0) return null;
|
|
42
|
+
return matches[0];
|
|
43
|
+
}
|
|
44
|
+
function buildEffectiveSchema(specification, context) {
|
|
45
|
+
const stateSchemas = Object.values(specification.state).map((state) => state.schema);
|
|
46
|
+
const operationSchemas = specification.modules.flatMap((moduleSpecification) => moduleSpecification.operations.flatMap((operation) => operation.schema === null ? [] : [operation.schema]));
|
|
47
|
+
try {
|
|
48
|
+
const document = parse([
|
|
49
|
+
...MODEL_SCALAR_SCHEMAS,
|
|
50
|
+
...stateSchemas,
|
|
51
|
+
...operationSchemas
|
|
52
|
+
].filter(Boolean).join("\n\n"));
|
|
53
|
+
return buildASTSchema(dedupeTypeDefinitions(document));
|
|
54
|
+
} catch {
|
|
55
|
+
throw compilationError(context, "the effective GraphQL schema is invalid");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function dedupeTypeDefinitions(document) {
|
|
59
|
+
const seen = /* @__PURE__ */ new Map();
|
|
60
|
+
const definitions = document.definitions.filter((definition) => {
|
|
61
|
+
if (!("name" in definition) || definition.name?.value === void 0 || definition.kind === Kind.SCALAR_TYPE_DEFINITION) return true;
|
|
62
|
+
const name = definition.name.value;
|
|
63
|
+
const printed = print(definition);
|
|
64
|
+
const existing = seen.get(name);
|
|
65
|
+
if (existing === void 0) {
|
|
66
|
+
seen.set(name, printed);
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
return existing !== printed;
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
...document,
|
|
73
|
+
definitions
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function attachmentReachableTypes(definitions) {
|
|
77
|
+
const reachable = /* @__PURE__ */ new Set();
|
|
78
|
+
let changed = true;
|
|
79
|
+
while (changed) {
|
|
80
|
+
changed = false;
|
|
81
|
+
for (const [name, definition] of definitions) {
|
|
82
|
+
if (reachable.has(name)) continue;
|
|
83
|
+
if (Object.values(definition.getFields()).some((field) => {
|
|
84
|
+
const typeName = getNamedType(field.type).name;
|
|
85
|
+
return typeName === ATTACHMENT_REF_TYPE || definitions.has(typeName) && reachable.has(typeName);
|
|
86
|
+
})) {
|
|
87
|
+
reachable.add(name);
|
|
88
|
+
changed = true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return reachable;
|
|
93
|
+
}
|
|
94
|
+
function compileValuePlan(type, objectPlans) {
|
|
95
|
+
if (isNonNullType(type)) return {
|
|
96
|
+
...compileValuePlan(type.ofType, objectPlans),
|
|
97
|
+
required: true
|
|
98
|
+
};
|
|
99
|
+
if (isListType(type)) return {
|
|
100
|
+
item: compileValuePlan(type.ofType, objectPlans),
|
|
101
|
+
kind: "list",
|
|
102
|
+
required: false
|
|
103
|
+
};
|
|
104
|
+
const typeName = type.name;
|
|
105
|
+
if (typeName === ATTACHMENT_REF_TYPE) return {
|
|
106
|
+
kind: "attachment",
|
|
107
|
+
required: false
|
|
108
|
+
};
|
|
109
|
+
const body = objectPlans.get(typeName);
|
|
110
|
+
if (!body) throw new Error(`Internal attachment schema plan error for ${typeName}`);
|
|
111
|
+
return {
|
|
112
|
+
body,
|
|
113
|
+
kind: "object",
|
|
114
|
+
required: false
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function compileRootPlan(rootName, definitions) {
|
|
118
|
+
const reachable = attachmentReachableTypes(definitions);
|
|
119
|
+
if (!reachable.has(rootName)) return null;
|
|
120
|
+
const objectPlans = /* @__PURE__ */ new Map();
|
|
121
|
+
for (const name of reachable) objectPlans.set(name, { fields: [] });
|
|
122
|
+
for (const name of reachable) {
|
|
123
|
+
const definition = definitions.get(name);
|
|
124
|
+
const body = objectPlans.get(name);
|
|
125
|
+
if (!definition || !body) continue;
|
|
126
|
+
for (const field of Object.values(definition.getFields())) {
|
|
127
|
+
const typeName = getNamedType(field.type).name;
|
|
128
|
+
if (typeName !== ATTACHMENT_REF_TYPE && !reachable.has(typeName)) continue;
|
|
129
|
+
body.fields.push({
|
|
130
|
+
hasDefault: field.defaultValue !== void 0,
|
|
131
|
+
name: field.name,
|
|
132
|
+
value: compileValuePlan(field.type, objectPlans)
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return objectPlans.get(rootName) ?? null;
|
|
137
|
+
}
|
|
138
|
+
function readOwnField(value, fieldName, context, path) {
|
|
139
|
+
try {
|
|
140
|
+
if (!Object.prototype.hasOwnProperty.call(value, fieldName)) return {
|
|
141
|
+
present: false,
|
|
142
|
+
value: void 0
|
|
143
|
+
};
|
|
144
|
+
return {
|
|
145
|
+
present: true,
|
|
146
|
+
value: value[fieldName]
|
|
147
|
+
};
|
|
148
|
+
} catch {
|
|
149
|
+
throw extractionError(context, path, "the declared field cannot be read");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function extractValue(plan, value, path, context, refs, seenRefs, activeObjects) {
|
|
153
|
+
if (value === null || value === void 0) {
|
|
154
|
+
if (plan.required) throw extractionError(context, path, "a required value is missing or null");
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (plan.kind === "attachment") {
|
|
158
|
+
if (typeof value !== "string") throw extractionError(context, path, "expected an AttachmentRef string");
|
|
159
|
+
try {
|
|
160
|
+
parseRef(value);
|
|
161
|
+
} catch {
|
|
162
|
+
throw extractionError(context, path, "the AttachmentRef is malformed");
|
|
163
|
+
}
|
|
164
|
+
if (!seenRefs.has(value)) {
|
|
165
|
+
seenRefs.add(value);
|
|
166
|
+
refs.push(value);
|
|
167
|
+
}
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (plan.kind === "list") {
|
|
171
|
+
if (!Array.isArray(value)) throw extractionError(context, path, "expected a list");
|
|
172
|
+
for (let index = 0; index < value.length; index += 1) extractValue(plan.item, value[index], `${path}[${index}]`, context, refs, seenRefs, activeObjects);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (typeof value !== "object" || Array.isArray(value)) throw extractionError(context, path, "expected an input object");
|
|
176
|
+
if (activeObjects.has(value)) throw extractionError(context, path, "the input value contains a cycle");
|
|
177
|
+
activeObjects.add(value);
|
|
178
|
+
try {
|
|
179
|
+
const record = value;
|
|
180
|
+
for (const field of plan.body.fields) {
|
|
181
|
+
const fieldPath = `${path}.${field.name}`;
|
|
182
|
+
const result = readOwnField(record, field.name, context, fieldPath);
|
|
183
|
+
if ((!result.present || result.value === void 0) && field.hasDefault) continue;
|
|
184
|
+
extractValue(field.value, result.value, fieldPath, context, refs, seenRefs, activeObjects);
|
|
185
|
+
}
|
|
186
|
+
} finally {
|
|
187
|
+
activeObjects.delete(value);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
var SchemaCompiledAttachmentExtractor = class {
|
|
191
|
+
context;
|
|
192
|
+
rootPlan;
|
|
193
|
+
constructor(context, rootPlan) {
|
|
194
|
+
this.context = context;
|
|
195
|
+
this.rootPlan = rootPlan;
|
|
196
|
+
}
|
|
197
|
+
extract(action) {
|
|
198
|
+
if (action.type !== this.context.actionType) throw extractionError(this.context, "input", "the action type does not match the compiled schema");
|
|
199
|
+
if (!this.rootPlan) return [];
|
|
200
|
+
if (action.input === null || typeof action.input !== "object" || Array.isArray(action.input)) throw extractionError(this.context, "input", "expected an input object");
|
|
201
|
+
const refs = [];
|
|
202
|
+
const seenRefs = /* @__PURE__ */ new Set();
|
|
203
|
+
const activeObjects = /* @__PURE__ */ new WeakSet();
|
|
204
|
+
activeObjects.add(action.input);
|
|
205
|
+
try {
|
|
206
|
+
const input = action.input;
|
|
207
|
+
for (const field of this.rootPlan.fields) {
|
|
208
|
+
const path = `input.${field.name}`;
|
|
209
|
+
const result = readOwnField(input, field.name, this.context, path);
|
|
210
|
+
if ((!result.present || result.value === void 0) && field.hasDefault) continue;
|
|
211
|
+
extractValue(field.value, result.value, path, this.context, refs, seenRefs, activeObjects);
|
|
212
|
+
}
|
|
213
|
+
} finally {
|
|
214
|
+
activeObjects.delete(action.input);
|
|
215
|
+
}
|
|
216
|
+
return refs;
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
function compileExtractor(module, actionType) {
|
|
220
|
+
const context = {
|
|
221
|
+
actionType,
|
|
222
|
+
documentType: module.documentModel.global.id,
|
|
223
|
+
version: module.version ?? 1
|
|
224
|
+
};
|
|
225
|
+
const specification = applicableSpecification(module, context);
|
|
226
|
+
const selected = selectOperation(parseOperations(specification, context), context);
|
|
227
|
+
if (selected === null || selected.operation.schema === null) return new SchemaCompiledAttachmentExtractor(context, null);
|
|
228
|
+
const effectiveSchema = buildEffectiveSchema(specification, context);
|
|
229
|
+
const operationName = selected.operation.name;
|
|
230
|
+
if (operationName === null) throw compilationError(context, "the operation has no name");
|
|
231
|
+
const rootName = `${pascalCase(operationName)}Input`;
|
|
232
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
233
|
+
for (const type of Object.values(effectiveSchema.getTypeMap())) if (isInputObjectType(type) && !type.name.startsWith("__")) definitions.set(type.name, type);
|
|
234
|
+
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}"`);
|
|
235
|
+
return new SchemaCompiledAttachmentExtractor(context, compileRootPlan(rootName, definitions));
|
|
236
|
+
}
|
|
237
|
+
var AttachmentSchemaCompiler = class {
|
|
238
|
+
cache = /* @__PURE__ */ new WeakMap();
|
|
239
|
+
forModuleAction(module, actionType) {
|
|
240
|
+
let moduleCache = this.cache.get(module);
|
|
241
|
+
if (!moduleCache) {
|
|
242
|
+
moduleCache = /* @__PURE__ */ new Map();
|
|
243
|
+
this.cache.set(module, moduleCache);
|
|
244
|
+
}
|
|
245
|
+
const cached = moduleCache.get(actionType);
|
|
246
|
+
if (cached) return cached;
|
|
247
|
+
const compiled = compileExtractor(module, actionType);
|
|
248
|
+
moduleCache.set(actionType, compiled);
|
|
249
|
+
return compiled;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
//#endregion
|
|
253
|
+
export { AttachmentSchemaCompiler as t };
|
|
254
|
+
|
|
255
|
+
//# sourceMappingURL=attachment-schema-compiler-BytE6Hev.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachment-schema-compiler-BytE6Hev.js","names":[],"sources":["../src/reference-index/attachment-schema-compiler.ts"],"sourcesContent":["import type { AttachmentRef } from \"@powerhousedao/reactor\";\nimport { documentModelScalars } from \"document-model/scalars\";\nimport type {\n Action,\n DocumentModelModule,\n DocumentSpecification,\n OperationSpecification,\n} from \"@powerhousedao/shared/document-model\";\nimport { constantCase, pascalCase } from \"change-case\";\nimport {\n buildASTSchema,\n getNamedType,\n isInputObjectType,\n isListType,\n isNonNullType,\n Kind,\n parse,\n print,\n type DocumentNode,\n type GraphQLInputObjectType,\n type GraphQLInputType,\n type GraphQLSchema,\n} from \"graphql\";\nimport { parseRef } from \"../ref.js\";\nimport type {\n CompiledAttachmentExtractor,\n IAttachmentSchemaCompiler,\n} from \"./types.js\";\n\nconst ATTACHMENT_REF_TYPE = \"AttachmentRef\";\nconst MODEL_SCALAR_SCHEMAS = documentModelScalars.map(\n ({ name }) => `scalar ${name}`,\n);\n\ntype CompilerContext = {\n actionType: string;\n documentType: string;\n version: number;\n};\n\ntype ParsedOperation = {\n document: DocumentNode | null;\n operation: OperationSpecification;\n};\n\ntype ObjectPlan = {\n fields: FieldPlan[];\n};\n\ntype FieldPlan = {\n hasDefault: boolean;\n name: string;\n value: ValuePlan;\n};\n\ntype ReadFieldResult =\n | { present: false; value: undefined }\n | { present: true; value: unknown };\n\ntype ValuePlan =\n | {\n kind: \"attachment\";\n required: boolean;\n }\n | {\n item: ValuePlan;\n kind: \"list\";\n required: boolean;\n }\n | {\n body: ObjectPlan;\n kind: \"object\";\n required: boolean;\n };\n\nfunction describeContext(context: CompilerContext): string {\n return `document type \"${context.documentType}\", version ${context.version}, action \"${context.actionType}\"`;\n}\n\nfunction compilationError(context: CompilerContext, reason: string): Error {\n return new Error(\n `Attachment schema compilation failed for ${describeContext(context)}: ${reason}`,\n );\n}\n\nfunction extractionError(\n context: CompilerContext,\n path: string,\n reason: string,\n): Error {\n return new Error(\n `Attachment extraction failed for ${describeContext(context)} at ${path}: ${reason}`,\n );\n}\n\nfunction applicableSpecification(\n module: DocumentModelModule,\n context: CompilerContext,\n): DocumentSpecification {\n const matches = module.documentModel.global.specifications.filter(\n (specification) => specification.version === context.version,\n );\n if (matches.length !== 1) {\n throw compilationError(\n context,\n matches.length === 0\n ? \"the module has no matching specification\"\n : \"the module has multiple matching specifications\",\n );\n }\n return matches[0];\n}\n\nfunction parseOperations(\n specification: DocumentSpecification,\n context: CompilerContext,\n): ParsedOperation[] {\n return specification.modules.flatMap((moduleSpecification) =>\n moduleSpecification.operations.map((operation) => {\n if (operation.schema === null) return { document: null, operation };\n try {\n return { document: parse(operation.schema), operation };\n } catch {\n throw compilationError(context, \"an operation has invalid GraphQL SDL\");\n }\n }),\n );\n}\n\nfunction selectOperation(\n operations: ParsedOperation[],\n context: CompilerContext,\n): ParsedOperation | null {\n const matches = operations.filter(\n ({ operation }) =>\n operation.name !== null &&\n constantCase(operation.name) === context.actionType,\n );\n if (matches.length > 1) {\n throw compilationError(context, \"multiple operations map to the action\");\n }\n // Base/system actions (document creation, renames, undo) are not part of a\n // model's specification, so they cannot declare AttachmentRef fields.\n // They compile to the no-reference fast path instead of failing the stream.\n if (matches.length === 0) return null;\n return matches[0];\n}\n\nfunction buildEffectiveSchema(\n specification: DocumentSpecification,\n context: CompilerContext,\n): GraphQLSchema {\n const stateSchemas = Object.values(specification.state).map(\n (state) => state.schema,\n );\n const operationSchemas = specification.modules.flatMap(\n (moduleSpecification) =>\n moduleSpecification.operations.flatMap((operation) =>\n operation.schema === null ? [] : [operation.schema],\n ),\n );\n\n try {\n const document = parse(\n [...MODEL_SCALAR_SCHEMAS, ...stateSchemas, ...operationSchemas]\n .filter(Boolean)\n .join(\"\\n\\n\"),\n );\n return buildASTSchema(dedupeTypeDefinitions(document));\n } catch {\n throw compilationError(context, \"the effective GraphQL schema is invalid\");\n }\n}\n\n// Collapse identical repeats of a type (state schema vs operation schemas).\n// Conflicting or scalar duplicates stay in so the build still rejects them.\nfunction dedupeTypeDefinitions(document: DocumentNode): DocumentNode {\n const seen = new Map<string, string>();\n const definitions = document.definitions.filter((definition) => {\n if (\n !(\"name\" in definition) ||\n definition.name?.value === undefined ||\n definition.kind === Kind.SCALAR_TYPE_DEFINITION\n ) {\n return true;\n }\n const name = definition.name.value;\n const printed = print(definition);\n const existing = seen.get(name);\n if (existing === undefined) {\n seen.set(name, printed);\n return true;\n }\n return existing !== printed;\n });\n return { ...document, definitions };\n}\n\nfunction attachmentReachableTypes(\n definitions: Map<string, GraphQLInputObjectType>,\n): Set<string> {\n const reachable = new Set<string>();\n let changed = true;\n\n while (changed) {\n changed = false;\n for (const [name, definition] of definitions) {\n if (reachable.has(name)) continue;\n const reachesAttachment = Object.values(definition.getFields()).some(\n (field) => {\n const typeName = getNamedType(field.type).name;\n return (\n typeName === ATTACHMENT_REF_TYPE ||\n (definitions.has(typeName) && reachable.has(typeName))\n );\n },\n );\n if (reachesAttachment) {\n reachable.add(name);\n changed = true;\n }\n }\n }\n\n return reachable;\n}\n\nfunction compileValuePlan(\n type: GraphQLInputType,\n objectPlans: Map<string, ObjectPlan>,\n): ValuePlan {\n if (isNonNullType(type)) {\n return { ...compileValuePlan(type.ofType, objectPlans), required: true };\n }\n if (isListType(type)) {\n return {\n item: compileValuePlan(type.ofType, objectPlans),\n kind: \"list\",\n required: false,\n };\n }\n\n const typeName = type.name;\n if (typeName === ATTACHMENT_REF_TYPE) {\n return { kind: \"attachment\", required: false };\n }\n const body = objectPlans.get(typeName);\n if (!body) {\n throw new Error(`Internal attachment schema plan error for ${typeName}`);\n }\n return { body, kind: \"object\", required: false };\n}\n\nfunction compileRootPlan(\n rootName: string,\n definitions: Map<string, GraphQLInputObjectType>,\n): ObjectPlan | null {\n const reachable = attachmentReachableTypes(definitions);\n if (!reachable.has(rootName)) return null;\n\n const objectPlans = new Map<string, ObjectPlan>();\n for (const name of reachable) objectPlans.set(name, { fields: [] });\n\n for (const name of reachable) {\n const definition = definitions.get(name);\n const body = objectPlans.get(name);\n if (!definition || !body) continue;\n for (const field of Object.values(definition.getFields())) {\n const typeName = getNamedType(field.type).name;\n if (typeName !== ATTACHMENT_REF_TYPE && !reachable.has(typeName)) {\n continue;\n }\n body.fields.push({\n hasDefault: field.defaultValue !== undefined,\n name: field.name,\n value: compileValuePlan(field.type, objectPlans),\n });\n }\n }\n\n return objectPlans.get(rootName) ?? null;\n}\n\nfunction readOwnField(\n value: Record<string, unknown>,\n fieldName: string,\n context: CompilerContext,\n path: string,\n): ReadFieldResult {\n try {\n if (!Object.prototype.hasOwnProperty.call(value, fieldName)) {\n return { present: false, value: undefined };\n }\n return { present: true, value: value[fieldName] };\n } catch {\n throw extractionError(context, path, \"the declared field cannot be read\");\n }\n}\n\nfunction extractValue(\n plan: ValuePlan,\n value: unknown,\n path: string,\n context: CompilerContext,\n refs: AttachmentRef[],\n seenRefs: Set<string>,\n activeObjects: WeakSet<object>,\n): void {\n if (value === null || value === undefined) {\n if (plan.required) {\n throw extractionError(\n context,\n path,\n \"a required value is missing or null\",\n );\n }\n return;\n }\n\n if (plan.kind === \"attachment\") {\n if (typeof value !== \"string\") {\n throw extractionError(context, path, \"expected an AttachmentRef string\");\n }\n try {\n parseRef(value as AttachmentRef);\n } catch {\n throw extractionError(context, path, \"the AttachmentRef is malformed\");\n }\n if (!seenRefs.has(value)) {\n seenRefs.add(value);\n refs.push(value as AttachmentRef);\n }\n return;\n }\n\n if (plan.kind === \"list\") {\n if (!Array.isArray(value)) {\n throw extractionError(context, path, \"expected a list\");\n }\n for (let index = 0; index < value.length; index += 1) {\n extractValue(\n plan.item,\n value[index],\n `${path}[${index}]`,\n context,\n refs,\n seenRefs,\n activeObjects,\n );\n }\n return;\n }\n\n if (typeof value !== \"object\" || Array.isArray(value)) {\n throw extractionError(context, path, \"expected an input object\");\n }\n if (activeObjects.has(value)) {\n throw extractionError(context, path, \"the input value contains a cycle\");\n }\n\n activeObjects.add(value);\n try {\n const record = value as Record<string, unknown>;\n for (const field of plan.body.fields) {\n const fieldPath = `${path}.${field.name}`;\n const result = readOwnField(record, field.name, context, fieldPath);\n if ((!result.present || result.value === undefined) && field.hasDefault) {\n continue;\n }\n extractValue(\n field.value,\n result.value,\n fieldPath,\n context,\n refs,\n seenRefs,\n activeObjects,\n );\n }\n } finally {\n activeObjects.delete(value);\n }\n}\n\nclass SchemaCompiledAttachmentExtractor implements CompiledAttachmentExtractor {\n constructor(\n private readonly context: CompilerContext,\n private readonly rootPlan: ObjectPlan | null,\n ) {}\n\n extract(action: Action): AttachmentRef[] {\n if (action.type !== this.context.actionType) {\n throw extractionError(\n this.context,\n \"input\",\n \"the action type does not match the compiled schema\",\n );\n }\n if (!this.rootPlan) return [];\n if (\n action.input === null ||\n typeof action.input !== \"object\" ||\n Array.isArray(action.input)\n ) {\n throw extractionError(this.context, \"input\", \"expected an input object\");\n }\n\n const refs: AttachmentRef[] = [];\n const seenRefs = new Set<string>();\n const activeObjects = new WeakSet<object>();\n activeObjects.add(action.input);\n try {\n const input = action.input as Record<string, unknown>;\n for (const field of this.rootPlan.fields) {\n const path = `input.${field.name}`;\n const result = readOwnField(input, field.name, this.context, path);\n if (\n (!result.present || result.value === undefined) &&\n field.hasDefault\n ) {\n continue;\n }\n extractValue(\n field.value,\n result.value,\n path,\n this.context,\n refs,\n seenRefs,\n activeObjects,\n );\n }\n } finally {\n activeObjects.delete(action.input);\n }\n return refs;\n }\n}\n\nfunction compileExtractor(\n module: DocumentModelModule,\n actionType: string,\n): CompiledAttachmentExtractor {\n const context: CompilerContext = {\n actionType,\n documentType: module.documentModel.global.id,\n version: module.version ?? 1,\n };\n const specification = applicableSpecification(module, context);\n const operations = parseOperations(specification, context);\n const selected = selectOperation(operations, context);\n if (selected === null || selected.operation.schema === null) {\n return new SchemaCompiledAttachmentExtractor(context, null);\n }\n const effectiveSchema = buildEffectiveSchema(specification, context);\n\n const operationName = selected.operation.name;\n if (operationName === null) {\n throw compilationError(context, \"the operation has no name\");\n }\n const rootName = `${pascalCase(operationName)}Input`;\n const definitions = new Map<string, GraphQLInputObjectType>();\n for (const type of Object.values(effectiveSchema.getTypeMap())) {\n if (isInputObjectType(type) && !type.name.startsWith(\"__\")) {\n definitions.set(type.name, type);\n }\n }\n const rootDefinitions = selected.document?.definitions.filter(\n (definition) =>\n (definition.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ||\n definition.kind === Kind.INPUT_OBJECT_TYPE_EXTENSION) &&\n definition.name.value === rootName,\n );\n if (!rootDefinitions?.length || !definitions.has(rootName)) {\n throw compilationError(\n context,\n `the operation does not declare its expected root input \"${rootName}\"`,\n );\n }\n\n return new SchemaCompiledAttachmentExtractor(\n context,\n compileRootPlan(rootName, definitions),\n );\n}\n\nexport class AttachmentSchemaCompiler implements IAttachmentSchemaCompiler {\n private readonly cache = new WeakMap<\n DocumentModelModule,\n Map<string, CompiledAttachmentExtractor>\n >();\n\n forModuleAction(\n module: DocumentModelModule,\n actionType: string,\n ): CompiledAttachmentExtractor {\n let moduleCache = this.cache.get(module);\n if (!moduleCache) {\n moduleCache = new Map();\n this.cache.set(module, moduleCache);\n }\n\n const cached = moduleCache.get(actionType);\n if (cached) return cached;\n\n const compiled = compileExtractor(module, actionType);\n moduleCache.set(actionType, compiled);\n return compiled;\n }\n}\n"],"mappings":";;;;;AA6BA,MAAM,sBAAsB;AAC5B,MAAM,uBAAuB,qBAAqB,KAC/C,EAAE,WAAW,UAAU,MAC1B;AA2CA,SAAS,gBAAgB,SAAkC;CACzD,OAAO,kBAAkB,QAAQ,aAAa,aAAa,QAAQ,QAAQ,YAAY,QAAQ,WAAW;AAC5G;AAEA,SAAS,iBAAiB,SAA0B,QAAuB;CACzE,uBAAO,IAA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,OAAO;CAEnE,MAAM,gBAAgB,SAAS,UAAU;CACzC,IAAI,kBAAkB,MACpB,MAAM,iBAAiB,SAAS,2BAA2B;CAE7D,MAAM,WAAW,GAAG,WAAW,aAAa,EAAE;CAC9C,MAAM,8BAAc,IAAI,IAAoC;CAC5D,KAAK,MAAM,QAAQ,OAAO,OAAO,gBAAgB,WAAW,CAAC,GAC3D,IAAI,kBAAkB,IAAI,KAAK,CAAC,KAAK,KAAK,WAAW,IAAI,GACvD,YAAY,IAAI,KAAK,MAAM,IAAI;CASnC,IAAI,EANoB,SAAS,UAAU,YAAY,QACpD,gBACE,WAAW,SAAS,KAAK,gCACxB,WAAW,SAAS,KAAK,gCAC3B,WAAW,KAAK,UAAU,QAC9B,EAAA,EACsB,UAAU,CAAC,YAAY,IAAI,QAAQ,GACvD,MAAM,iBACJ,SACA,2DAA2D,SAAS,EACtE;CAGF,OAAO,IAAI,kCACT,SACA,gBAAgB,UAAU,WAAW,CACvC;AACF;AAEA,IAAa,2BAAb,MAA2E;CACzE,wBAAyB,IAAI,QAG3B;CAEF,gBACE,QACA,YAC6B;EAC7B,IAAI,cAAc,KAAK,MAAM,IAAI,MAAM;EACvC,IAAI,CAAC,aAAa;GAChB,8BAAc,IAAI,IAAI;GACtB,KAAK,MAAM,IAAI,QAAQ,WAAW;EACpC;EAEA,MAAM,SAAS,YAAY,IAAI,UAAU;EACzC,IAAI,QAAQ,OAAO;EAEnB,MAAM,WAAW,iBAAiB,QAAQ,UAAU;EACpD,YAAY,IAAI,YAAY,QAAQ;EACpC,OAAO;CACT;AACF"}
|
package/dist/client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { A as HashMismatch, C as parseAttachmentUploadTarget, D as AttachmentPending, E as AttachmentNotFound, F as UploadTooLarge, M as InvalidAttachmentRef, N as ReservationNotFound, O as AttachmentTransferError, P as SizeMismatch, S as parseAttachmentDownloadTarget, _ as SwitchboardTransportConfig, a as RemoteAttachmentStore, b as parseRef, c as RemoteReservationStore, d as createXhrUploadTransport, f as AttachmentUploadRequest, g as SwitchboardAttachmentTransport, h as createFetchUploadTransport, i as createRemoteAttachmentService, k as AttachmentTransferStage, l as SwitchboardClientConfig, m as AttachmentUploadTransport, n as RoutingAttachmentService, o as RemoteAttachmentUploadFactory, p as AttachmentUploadResponse, r as NullAttachmentTransport, s as RemoteAttachmentUpload, t as AttachmentRouteResolver, u as XhrUploadTransportOptions, v as ParsedRef, w as AttachmentAlreadyExists, x as AttachmentService, y as createRef } from "./routing-attachment-service-Dri6e2Vz.js";
|
|
2
|
+
import { A as UploadFirstReserveAttachmentOptions, C as AttachmentUploadResult, D as ReserveAttachmentOptions, E as Reservation, O as TransportFetchResult, S as AttachmentTransportConfig, T as HashFirstReserveAttachmentOptions, _ as AttachmentResponse, a as IAttachmentTransport, b as AttachmentStatus, c as IAttachmentUploadFactory, f as AttachmentDownloadOptions, g as AttachmentMetadata, h as AttachmentHeader, i as IAttachmentStore, k as TransportResponse, l as IReservationStore, m as AttachmentDownloadTargetOptions, n as IAttachmentReader, o as IAttachmentTransportFactory, p as AttachmentDownloadTarget, r as IAttachmentService, s as IAttachmentUpload, v as AttachmentSendOptions, w as AttachmentUploadTarget, x as AttachmentTargetHeaders, y as AttachmentStatOptions } from "./interfaces-cFMtcMqR.js";
|
|
2
3
|
import { AttachmentHash, AttachmentRef } from "@powerhousedao/reactor";
|
|
3
|
-
|
|
4
4
|
//#region src/concurrency.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* Ordered result of one batch item. `index` always mirrors the input
|
|
@@ -17,7 +17,8 @@ type BatchItemResult<R> = {
|
|
|
17
17
|
error: unknown;
|
|
18
18
|
};
|
|
19
19
|
type RunWithConcurrencyOptions<R = unknown> = {
|
|
20
|
-
/** Maximum simultaneously running workers. Must be a positive integer. */
|
|
20
|
+
/** Maximum simultaneously running workers. Must be a positive integer. */
|
|
21
|
+
concurrency: number;
|
|
21
22
|
/**
|
|
22
23
|
* Whole-batch cancellation: unstarted items are rejected with the signal's
|
|
23
24
|
* reason without ever starting, while already-started items keep running —
|
|
@@ -37,7 +38,7 @@ type RunWithConcurrencyOptions<R = unknown> = {
|
|
|
37
38
|
* executions. Bounding starts (not just transfers) is what keeps memory flat:
|
|
38
39
|
* an item's preprocessing (hashing/buffering) only begins when a slot frees.
|
|
39
40
|
*/
|
|
40
|
-
declare function runWithConcurrency<T, R>(items: readonly T[], worker: (item: T, index: number) => Promise<R>, options: RunWithConcurrencyOptions<R>): Promise<BatchItemResult<R>[]>;
|
|
41
|
+
export declare function runWithConcurrency<T, R>(items: readonly T[], worker: (item: T, index: number) => Promise<R>, options: RunWithConcurrencyOptions<R>): Promise<BatchItemResult<R>[]>;
|
|
41
42
|
//#endregion
|
|
42
43
|
//#region src/progress.d.ts
|
|
43
44
|
/**
|
|
@@ -50,26 +51,32 @@ declare function runWithConcurrency<T, R>(items: readonly T[], worker: (item: T,
|
|
|
50
51
|
*/
|
|
51
52
|
type AttachmentStage = "hashing" | "reserving" | "uploading" | "requesting-download-target" | "downloading" | "done" | "error";
|
|
52
53
|
type AttachmentProgress = {
|
|
53
|
-
stage: AttachmentStage;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
stage: AttachmentStage;
|
|
55
|
+
/** Bytes moved so far within `stage`. */
|
|
56
|
+
loaded: number;
|
|
57
|
+
/** Denominator for `stage`: file.size for uploads, header.sizeBytes for downloads. */
|
|
58
|
+
total: number | undefined;
|
|
59
|
+
/** True when the stage is running but bytes cannot (yet) be observed. */
|
|
60
|
+
indeterminate: boolean;
|
|
61
|
+
/** Only on a terminal `done` where dedup skipped the transfer entirely. */
|
|
57
62
|
deduped?: true;
|
|
58
63
|
};
|
|
59
64
|
type AttachmentProgressListener = (progress: AttachmentProgress) => void;
|
|
60
65
|
type AttachmentProgressOptions = {
|
|
61
|
-
onProgress?: AttachmentProgressListener;
|
|
66
|
+
onProgress?: AttachmentProgressListener;
|
|
67
|
+
/** Min ms between byte events within a stage. Default 100. 0 emits every observation. */
|
|
62
68
|
throttleMs?: number;
|
|
63
69
|
};
|
|
64
|
-
declare const DEFAULT_PROGRESS_THROTTLE_MS = 100;
|
|
70
|
+
export declare const DEFAULT_PROGRESS_THROTTLE_MS = 100;
|
|
65
71
|
/**
|
|
66
72
|
* Fraction in 0..1 for a progress event. Indeterminate and unknown-total
|
|
67
73
|
* events read as 0 — a bar cannot honestly show a position it does not know.
|
|
68
74
|
* A zero total is complete by definition (the dedup case moves no bytes).
|
|
69
75
|
*/
|
|
70
|
-
declare function progressFraction(progress: AttachmentProgress): number;
|
|
76
|
+
export declare function progressFraction(progress: AttachmentProgress): number;
|
|
71
77
|
type ProgressEmitterOptions = AttachmentProgressOptions & {
|
|
72
|
-
/** Injected clock so throttle tests stay synchronous. */
|
|
78
|
+
/** Injected clock so throttle tests stay synchronous. */
|
|
79
|
+
now?: () => number;
|
|
73
80
|
};
|
|
74
81
|
/**
|
|
75
82
|
* Emits `AttachmentProgress` for one single-item operation, upholding:
|
|
@@ -86,7 +93,7 @@ type ProgressEmitterOptions = AttachmentProgressOptions & {
|
|
|
86
93
|
* ordering, and is unnecessary because `finish()` always emits
|
|
87
94
|
* `loaded === total`, making a dropped last intra-stage tick invisible.
|
|
88
95
|
*/
|
|
89
|
-
declare class ProgressEmitter {
|
|
96
|
+
export declare class ProgressEmitter {
|
|
90
97
|
private readonly listener;
|
|
91
98
|
private readonly throttleMs;
|
|
92
99
|
private readonly now;
|
|
@@ -128,7 +135,9 @@ declare class ProgressEmitter {
|
|
|
128
135
|
private emit;
|
|
129
136
|
}
|
|
130
137
|
type ByteProgressHooks = {
|
|
131
|
-
/** Cumulative bytes handed to the consumer. */
|
|
138
|
+
/** Cumulative bytes handed to the consumer. */
|
|
139
|
+
onBytes?: (loaded: number) => void;
|
|
140
|
+
/** The source ended and every byte reached the consumer. */
|
|
132
141
|
onDone?: () => void;
|
|
133
142
|
onError?: (error: unknown) => void;
|
|
134
143
|
};
|
|
@@ -150,21 +159,34 @@ type ByteProgressHooks = {
|
|
|
150
159
|
* Returns `source` unchanged when no hook is supplied, so a caller with no
|
|
151
160
|
* listener pays nothing.
|
|
152
161
|
*/
|
|
153
|
-
declare function withByteProgress(source: ReadableStream<Uint8Array>, hooks: ByteProgressHooks): ReadableStream<Uint8Array>;
|
|
162
|
+
export declare function withByteProgress(source: ReadableStream<Uint8Array>, hooks: ByteProgressHooks): ReadableStream<Uint8Array>;
|
|
154
163
|
//#endregion
|
|
155
164
|
//#region src/client.d.ts
|
|
156
|
-
type
|
|
165
|
+
export type PreprocessOptions = {
|
|
166
|
+
fileName?: string;
|
|
167
|
+
mimeType?: string;
|
|
168
|
+
/** The document the attachment is for; see `ReserveAttachmentOptions`. */
|
|
169
|
+
documentId?: string;
|
|
170
|
+
};
|
|
171
|
+
export type PreprocessResult = {
|
|
157
172
|
ref: AttachmentRef;
|
|
158
173
|
hash: AttachmentHash;
|
|
159
174
|
sizeBytes: number;
|
|
160
175
|
options: HashFirstReserveAttachmentOptions;
|
|
176
|
+
/** @deprecated Read `blob`; this is a stream of its bytes. */
|
|
161
177
|
data: ReadableStream<Uint8Array>;
|
|
178
|
+
/** @deprecated Read `blob`; each call is a new stream of its bytes. */
|
|
162
179
|
stream: () => ReadableStream<Uint8Array>;
|
|
180
|
+
/** The file itself; uploads send it as-is, so a browser streams it from disk. */
|
|
181
|
+
blob: Blob;
|
|
163
182
|
};
|
|
164
|
-
type AttachmentUploadInput = {
|
|
183
|
+
export type AttachmentUploadInput = {
|
|
165
184
|
file: Blob;
|
|
166
185
|
fileName?: string;
|
|
167
|
-
mimeType?: string;
|
|
186
|
+
mimeType?: string;
|
|
187
|
+
/** The document the attachment is for; see `ReserveAttachmentOptions`. */
|
|
188
|
+
documentId?: string;
|
|
189
|
+
/** Per-item cancellation, checked between stages. */
|
|
168
190
|
signal?: AbortSignal;
|
|
169
191
|
};
|
|
170
192
|
/**
|
|
@@ -175,7 +197,7 @@ type AttachmentUploadInput = {
|
|
|
175
197
|
* upload. Passing the result back in skips the hashing stage rather than
|
|
176
198
|
* faking a zero-millisecond one.
|
|
177
199
|
*/
|
|
178
|
-
type AttachmentPreprocessedUploadInput = {
|
|
200
|
+
export type AttachmentPreprocessedUploadInput = {
|
|
179
201
|
preprocessed: PreprocessResult;
|
|
180
202
|
signal?: AbortSignal;
|
|
181
203
|
};
|
|
@@ -183,7 +205,7 @@ type AttachmentPreprocessedUploadInput = {
|
|
|
183
205
|
* Every remote download names the document that authorizes its ref; batches
|
|
184
206
|
* may freely mix documents because the anchor travels with each item.
|
|
185
207
|
*/
|
|
186
|
-
type AttachmentDownloadInput = {
|
|
208
|
+
export type AttachmentDownloadInput = {
|
|
187
209
|
documentId: string;
|
|
188
210
|
ref: AttachmentRef;
|
|
189
211
|
signal?: AbortSignal;
|
|
@@ -194,25 +216,27 @@ type AttachmentDownloadInput = {
|
|
|
194
216
|
* blob-producing conveniences let callers override what the server header
|
|
195
217
|
* reports from upload time.
|
|
196
218
|
*/
|
|
197
|
-
type AttachmentBlobOptions = {
|
|
219
|
+
export type AttachmentBlobOptions = {
|
|
198
220
|
mimeType?: string;
|
|
199
221
|
};
|
|
200
|
-
type AttachmentSaveOptions = {
|
|
222
|
+
export type AttachmentSaveOptions = {
|
|
201
223
|
fileName?: string;
|
|
202
224
|
mimeType?: string;
|
|
203
225
|
};
|
|
204
|
-
type AttachmentBlobResult = {
|
|
226
|
+
export type AttachmentBlobResult = {
|
|
205
227
|
blob: Blob;
|
|
206
228
|
header: AttachmentHeader;
|
|
207
229
|
};
|
|
208
|
-
type AttachmentObjectUrl = {
|
|
209
|
-
/** Ready for img/iframe/video src. Pins memory until revoke() is called. */
|
|
230
|
+
export type AttachmentObjectUrl = {
|
|
231
|
+
/** Ready for img/iframe/video src. Pins memory until revoke() is called. */
|
|
232
|
+
url: string;
|
|
210
233
|
header: AttachmentHeader;
|
|
211
234
|
revoke: () => void;
|
|
212
235
|
};
|
|
213
|
-
type AttachmentShareLinkInput = {
|
|
236
|
+
export type AttachmentShareLinkInput = {
|
|
214
237
|
documentId: string;
|
|
215
|
-
ref: AttachmentRef;
|
|
238
|
+
ref: AttachmentRef;
|
|
239
|
+
/** Requested link lifetime in seconds; the server clamps to its maximum. */
|
|
216
240
|
expiresIn?: number;
|
|
217
241
|
signal?: AbortSignal;
|
|
218
242
|
};
|
|
@@ -221,7 +245,7 @@ type AttachmentShareLinkInput = {
|
|
|
221
245
|
* expiresAtUtc, with no login and no document access. Minting one requires
|
|
222
246
|
* document read access; once minted it cannot be revoked before expiry.
|
|
223
247
|
*/
|
|
224
|
-
type AttachmentShareLink = {
|
|
248
|
+
export type AttachmentShareLink = {
|
|
225
249
|
url: string;
|
|
226
250
|
expiresAtUtc: string;
|
|
227
251
|
};
|
|
@@ -231,8 +255,10 @@ type AttachmentShareLink = {
|
|
|
231
255
|
* a presentation decision the client cannot make, and a confirmed dedup would
|
|
232
256
|
* shrink the denominator mid-flight.
|
|
233
257
|
*/
|
|
234
|
-
type AttachmentBatchOptions = {
|
|
235
|
-
/** Bounds preprocessing and transfer together. Defaults to 4. */
|
|
258
|
+
export type AttachmentBatchOptions = {
|
|
259
|
+
/** Bounds preprocessing and transfer together. Defaults to 4. */
|
|
260
|
+
concurrency?: number;
|
|
261
|
+
/** Whole-batch cancellation: stops unstarted items. */
|
|
236
262
|
signal?: AbortSignal;
|
|
237
263
|
onProgress?: (progress: AttachmentProgress & {
|
|
238
264
|
index: number;
|
|
@@ -241,21 +267,19 @@ type AttachmentBatchOptions = {
|
|
|
241
267
|
* Fired as each item settles, including items the signal skipped before they
|
|
242
268
|
* started, so `settled` always reaches `total`.
|
|
243
269
|
*/
|
|
244
|
-
onItemSettled?: (counts: AttachmentBatchCounts) => void;
|
|
270
|
+
onItemSettled?: (counts: AttachmentBatchCounts) => void;
|
|
271
|
+
/** Min ms between byte events, applied per item rather than batch-wide. */
|
|
245
272
|
throttleMs?: number;
|
|
246
273
|
};
|
|
247
|
-
type AttachmentBatchCounts = {
|
|
274
|
+
export type AttachmentBatchCounts = {
|
|
248
275
|
settled: number;
|
|
249
276
|
completed: number;
|
|
250
277
|
failed: number;
|
|
251
278
|
total: number;
|
|
252
279
|
};
|
|
253
|
-
declare const DEFAULT_ATTACHMENT_BATCH_CONCURRENCY = 4;
|
|
254
|
-
interface IAttachmentClient {
|
|
255
|
-
preprocess(file: Blob, opts?:
|
|
256
|
-
fileName?: string;
|
|
257
|
-
mimeType?: string;
|
|
258
|
-
}): Promise<PreprocessResult>;
|
|
280
|
+
export declare const DEFAULT_ATTACHMENT_BATCH_CONCURRENCY = 4;
|
|
281
|
+
export interface IAttachmentClient {
|
|
282
|
+
preprocess(file: Blob, opts?: PreprocessOptions): Promise<PreprocessResult>;
|
|
259
283
|
reserve(options: HashFirstReserveAttachmentOptions, send: (handle: IAttachmentUpload) => Promise<AttachmentUploadResult>): Promise<AttachmentUploadResult>;
|
|
260
284
|
/**
|
|
261
285
|
* Hash, reserve, and transfer one file; a confirmed dedup skips the transfer
|
|
@@ -303,7 +327,8 @@ interface IAttachmentClient {
|
|
|
303
327
|
uploadMany(inputs: readonly AttachmentUploadInput[], options?: AttachmentBatchOptions): Promise<BatchItemResult<AttachmentUploadResult>[]>;
|
|
304
328
|
downloadMany(inputs: readonly AttachmentDownloadInput[], options?: AttachmentBatchOptions): Promise<BatchItemResult<AttachmentResponse>[]>;
|
|
305
329
|
}
|
|
306
|
-
declare function createAttachmentClient(service: IAttachmentService): IAttachmentClient;
|
|
330
|
+
export declare function createAttachmentClient(service: IAttachmentService): IAttachmentClient;
|
|
307
331
|
//#endregion
|
|
308
|
-
export { AttachmentAlreadyExists,
|
|
332
|
+
export { AttachmentAlreadyExists, type AttachmentDownloadOptions, type AttachmentDownloadTarget, type AttachmentDownloadTargetOptions, type AttachmentHeader, type AttachmentMetadata, AttachmentNotFound, AttachmentPending, type AttachmentProgress, type AttachmentProgressListener, type AttachmentProgressOptions, type AttachmentResponse, type AttachmentRouteResolver, type AttachmentSendOptions, AttachmentService, type AttachmentStage, type AttachmentStatOptions, type AttachmentStatus, type AttachmentTargetHeaders, AttachmentTransferError, type AttachmentTransferStage, type AttachmentTransportConfig, type AttachmentUploadRequest, type AttachmentUploadResponse, type AttachmentUploadResult, type AttachmentUploadTarget, type AttachmentUploadTransport, type BatchItemResult, type ByteProgressHooks, type HashFirstReserveAttachmentOptions, HashMismatch, type IAttachmentReader, type IAttachmentService, type IAttachmentStore, type IAttachmentTransport, type IAttachmentTransportFactory, type IAttachmentUpload, type IAttachmentUploadFactory, type IReservationStore, InvalidAttachmentRef, NullAttachmentTransport, type ParsedRef, type ProgressEmitterOptions, RemoteAttachmentStore, RemoteAttachmentUpload, RemoteAttachmentUploadFactory, RemoteReservationStore, type Reservation, ReservationNotFound, type ReserveAttachmentOptions, RoutingAttachmentService, type RunWithConcurrencyOptions, SizeMismatch, SwitchboardAttachmentTransport, type SwitchboardClientConfig, type SwitchboardTransportConfig, type TransportFetchResult, type TransportResponse, type UploadFirstReserveAttachmentOptions, UploadTooLarge, type XhrUploadTransportOptions, createFetchUploadTransport, createRef, createRemoteAttachmentService, createXhrUploadTransport, parseAttachmentDownloadTarget, parseAttachmentUploadTarget, parseRef };
|
|
333
|
+
export {};
|
|
309
334
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","names":[],"sources":["../src/concurrency.ts","../src/progress.ts","../src/client.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.d.ts","names":[],"sources":["../src/concurrency.ts","../src/progress.ts","../src/client.ts"],"mappings":";;;;;;;;;KAKY,gBAAgB;EACtB;EAAe;EAAqB,OAAO;;EAC3C;EAAe;EAAoB;;KAE7B,0BAA0B;;EAEpC;;;;;;EAMA,SAAS;;;;;;;EAOT,aAAa,QAAQ,gBAAgB;;;;;;;wBAQjB,mBAAmB,GAAG,GAC1C,gBAAgB,KAChB,SAAS,MAAM,GAAG,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