@voltro/plugin-storage 0.86.0 → 0.88.0

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +5484 -0
  2. package/README.md +27 -0
  3. package/THIRD-PARTY-NOTICES.md +2503 -690
  4. package/dist/access.js +26 -27
  5. package/dist/assetRef.js +1 -2
  6. package/dist/avatar.js +1 -2
  7. package/dist/byteSecurity.d.ts +3 -0
  8. package/dist/byteSecurity.js +6 -0
  9. package/dist/connection.js +98 -0
  10. package/dist/constraints.js +1 -2
  11. package/dist/custody.js +131 -0
  12. package/dist/derivatives.js +1 -2
  13. package/dist/env.js +8 -3
  14. package/dist/grant.js +90 -71
  15. package/dist/index.d.ts +453 -215
  16. package/dist/index.js +709 -537
  17. package/dist/ingest.js +7 -2
  18. package/dist/ingestPolicy.js +1 -1
  19. package/dist/metadataError.js +8 -9
  20. package/dist/objectAbandon.js +15 -0
  21. package/dist/objectLifecycle.js +135 -102
  22. package/dist/objectMetadata.js +253 -96
  23. package/dist/objectWriter.js +51 -23
  24. package/dist/pathDurability.js +1 -2
  25. package/dist/physicalReferences.js +53 -0
  26. package/dist/providers.js +451 -307
  27. package/dist/referenceCatalogue.js +1 -2
  28. package/dist/renditionProof.js +81 -0
  29. package/dist/retainedCapture.js +390 -0
  30. package/dist/retainedObjects.js +189 -0
  31. package/dist/rpc.d.ts +1 -15
  32. package/dist/rpc.js +1 -2
  33. package/dist/scanners.js +1 -2
  34. package/dist/server.d.ts +153 -0
  35. package/dist/server.js +4 -0
  36. package/dist/service.js +879 -546
  37. package/dist/sourceIdentity.js +13 -0
  38. package/dist/sourcePin.js +34 -0
  39. package/dist/storageNamespaces.js +52 -29
  40. package/dist/storageNumbers.js +1 -2
  41. package/dist/storageObjectNamespaces.js +170 -122
  42. package/dist/storageReferenceInstances.js +1 -2
  43. package/dist/storageRequest.js +9 -0
  44. package/dist/storageRouting.js +45 -0
  45. package/dist/streamBridge.js +5 -3
  46. package/dist/table.js +2 -2
  47. package/dist/transcodeVideo.js +85 -48
  48. package/dist/transform.js +30 -27
  49. package/dist/transformCache.js +1 -2
  50. package/dist/types.d.ts +115 -137
  51. package/dist/types.js +15 -16
  52. package/package.json +22 -11
package/dist/service.js CHANGED
@@ -1,25 +1,28 @@
1
- import { requireStorageReferenceScope as e, storageObjectNamespace as t, storageUploadTicketScope as n } from "./storageNamespaces.js";
2
- import { storageReferenceCatalogueId as r } from "./referenceCatalogue.js";
3
- import { STORAGE_GRANTS_TABLE as i, STORAGE_REFS_TABLE as a, STORAGE_USAGE_TABLE as o } from "./table.js";
1
+ import { requireStorageReferenceScope as e, storageObjectNamespace as t, storageReferenceScopeFor as n, storageUploadTicketScope as r } from "./storageNamespaces.js";
2
+ import { storageReferenceCatalogueId as i } from "./referenceCatalogue.js";
3
+ import { STORAGE_GRANTS_TABLE as a, STORAGE_REFS_TABLE as o, STORAGE_USAGE_TABLE as s } from "./table.js";
4
4
  import "./storageReferenceInstances.js";
5
- import { StorageAccessDenied as s, StorageError as c, StorageRejected as l, StorageScanRejected as u, StorageService as d } from "./types.js";
6
- import { storageInteger as f, storageIntegerSum as p } from "./storageNumbers.js";
7
- import { getObjectStream as m } from "./streamBridge.js";
8
- import { storageMetadataError as h } from "./metadataError.js";
9
- import { evaluatePolicy as g, grantsAllow as _, hashPassword as v, resolveSubjectGroups as y } from "./access.js";
10
- import { signGrant as b } from "./grant.js";
11
- import { checkLimits as ee } from "./constraints.js";
12
- import { extractImageMetadata as te } from "./derivatives.js";
13
- import { fetchIngestBody as x, withIngestAbort as S } from "./ingest.js";
14
- import { makeStorageObjectWriter as C } from "./objectWriter.js";
15
- import { randomUUID as w } from "node:crypto";
16
- import { and as T, authoredError as E, column as D, contains as O, eq as k, generateId as A, isNull as j, or as M } from "@voltro/database";
17
- import { Effect as N, Exit as P, Layer as F, Schedule as I, Stream as L } from "effect";
18
- //#region src/service.ts
19
- var R = async (e) => {
5
+ import { StorageAccessDenied as c, StorageError as l, StorageRejected as u, StorageScanRejected as d, StorageService as f, storageRefAddress as p } from "./types.js";
6
+ import { storageInteger as m, storageIntegerSum as h } from "./storageNumbers.js";
7
+ import { getObjectStream as g } from "./streamBridge.js";
8
+ import { storageMetadataError as _ } from "./metadataError.js";
9
+ import { evaluatePolicy as v, grantsAllow as y, hashPassword as ee, resolveSubjectGroups as b } from "./access.js";
10
+ import { declareStorageSigningLifetime as x, signGrant as S } from "./grant.js";
11
+ import { checkLimits as te } from "./constraints.js";
12
+ import { extractImageMetadata as ne } from "./derivatives.js";
13
+ import { assertRenditionOutput as re, makeRenditionProof as C, ownRenditionRef as w, readRenditionProof as T } from "./renditionProof.js";
14
+ import { fetchIngestBody as E, withIngestAbort as D } from "./ingest.js";
15
+ import { makeStorageObjectWriter as ie } from "./objectWriter.js";
16
+ import { protectStorageReferences as ae } from "./physicalReferences.js";
17
+ import { StorageAddress as O, storageKeyProblem as k } from "@voltro/kv/storage";
18
+ import { Effect as A, Exit as oe, Layer as j, Schedule as M, Schema as N, Stream as se } from "effect";
19
+ import { randomUUID as P } from "node:crypto";
20
+ import { and as F, authoredError as I, column as L, contains as R, eq as z, generateId as B, isNull as V, or as H } from "@voltro/database";
21
+ // src/service.ts
22
+ var U = async (e) => {
20
23
  let { createHash: t } = await import("node:crypto");
21
24
  return t("sha256").update(e).digest("hex");
22
- }, z = (e, t, n) => `${e?.replace(/\/+$/, "") ?? ""}${n}/${t}`, B = (e) => {
25
+ }, ce = (e, t, n) => `${e?.replace(/\/+$/, "") ?? ""}${n}/${t}`, W = (e) => {
23
26
  let t = /* @__PURE__ */ new Map(), n = {
24
27
  public: 0,
25
28
  private: 0
@@ -29,13 +32,13 @@ var R = async (e) => {
29
32
  count: 0,
30
33
  bytes: 0
31
34
  };
32
- i.count = p(i.count, r.count, "ref count"), i.bytes = p(i.bytes, r.bytes, "usage bytes"), t.set(e, i), r.visibility === "public" ? n.public = p(n.public, r.count, "public ref count") : n.private = p(n.private, r.count, "private ref count");
35
+ i.count = h(i.count, r.count, "ref count"), i.bytes = h(i.bytes, r.bytes, "usage bytes"), t.set(e, i), r.visibility === "public" ? n.public = h(n.public, r.count, "public ref count") : n.private = h(n.private, r.count, "private ref count");
33
36
  }
34
37
  return {
35
38
  byTenant: Object.fromEntries(t),
36
39
  visibility: n
37
40
  };
38
- }, V = (e, t) => {
41
+ }, G = (e, t) => {
39
42
  if (t.search && ![
40
43
  e.id,
41
44
  e.key,
@@ -52,12 +55,18 @@ var R = async (e) => {
52
55
  if (!t.tags.every((e) => n.includes(e))) return !1;
53
56
  }
54
57
  return !0;
55
- }, H = (e) => e ?? "global", U = () => {
58
+ }, K = (e) => e === null ? "global" : e === "global" ? "%67lobal" : e.startsWith("%") ? `%${e}` : e, q = () => {
56
59
  let e = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map();
57
60
  return {
58
- catalogueId: N.succeed(t(["memory-reference-catalogue", w()])),
59
- statistics: () => N.try({
60
- try: () => B((function* () {
61
+ listObjectReferences: () => A.sync(() => [...e.values()].map((e) => ({
62
+ bucket: e.bucket,
63
+ tenantId: e.tenantId,
64
+ key: e.key,
65
+ visibility: e.visibility
66
+ }))),
67
+ catalogueId: A.succeed(t(["memory-reference-catalogue", P()])),
68
+ statistics: () => A.try({
69
+ try: () => W((function* () {
61
70
  for (let t of e.values()) yield {
62
71
  tenantId: t.tenantId,
63
72
  visibility: t.visibility,
@@ -65,46 +74,49 @@ var R = async (e) => {
65
74
  bytes: t.size
66
75
  };
67
76
  })()),
68
- catch: (e) => h(e, "refstore")
77
+ catch: (e) => _(e, "refstore")
69
78
  }),
70
- insert: (t) => N.try({
79
+ insert: (t) => A.try({
71
80
  try: () => {
72
- f(t.size, "ref size"), e.set(t.id, t);
81
+ m(t.size, "ref size"), e.set(t.id, w(t));
73
82
  },
74
- catch: (e) => h(e, "refstore")
83
+ catch: (e) => _(e, "refstore")
84
+ }),
85
+ getById: (t) => A.sync(() => {
86
+ let n = e.get(t);
87
+ return n === void 0 ? null : w(n);
75
88
  }),
76
- getById: (t) => N.sync(() => e.get(t) ?? null),
77
- delete: (t) => N.sync(() => e.delete(t)),
78
- listRecent: (t) => N.sync(() => [...e.values()].sort((e, t) => t.createdAt.getTime() - e.createdAt.getTime() || t.id.localeCompare(e.id)).slice(0, t)),
79
- countByKey: (t, n) => N.sync(() => [...e.values()].filter((e) => e.bucket === t && e.key === n).length),
80
- countPublicByKey: (t, n) => N.sync(() => [...e.values()].filter((e) => e.bucket === t && e.key === n && e.visibility === "public").length),
81
- setVisibility: (t, n) => N.sync(() => {
89
+ delete: (t) => A.sync(() => e.delete(t)),
90
+ listRecent: (t) => A.sync(() => [...e.values()].sort((e, t) => t.createdAt.getTime() - e.createdAt.getTime() || t.id.localeCompare(e.id)).slice(0, t).map(w)),
91
+ countByAddress: (t) => A.sync(() => [...e.values()].filter((e) => e.bucket === t.destination && e.tenantId === t.tenantId && e.key === t.key).length),
92
+ countPublicByAddress: (t) => A.sync(() => [...e.values()].filter((e) => e.bucket === t.destination && e.tenantId === t.tenantId && e.key === t.key && e.visibility === "public").length),
93
+ setVisibility: (t, n) => A.sync(() => {
82
94
  let r = e.get(t);
83
95
  if (r === void 0) return null;
84
96
  let i = {
85
97
  ...r,
86
98
  visibility: n
87
99
  };
88
- return e.set(t, i), i;
100
+ return e.set(t, i), w(i);
89
101
  }),
90
- usageByTenant: (t) => N.try({
102
+ usageByTenant: (t) => A.try({
91
103
  try: () => {
92
104
  let n = 0, r = 0;
93
- for (let i of e.values()) (i.tenantId ?? null) === (t ?? null) && (n = p(n, i.size, "usage bytes"), r++);
105
+ for (let i of e.values()) (i.tenantId ?? null) === (t ?? null) && (n = h(n, i.size, "usage bytes"), r++);
94
106
  return {
95
107
  bytes: n,
96
108
  count: r
97
109
  };
98
110
  },
99
- catch: (e) => h(e, "refstore")
111
+ catch: (e) => _(e, "refstore")
100
112
  }),
101
- consumeUsage: (e, t, r) => N.try({
113
+ consumeUsage: (e, t, r) => A.try({
102
114
  try: () => {
103
- let i = H(e), a = n.get(i) ?? {
115
+ let i = K(e), a = n.get(i) ?? {
104
116
  bytes: 0,
105
117
  count: 0
106
118
  };
107
- f(t.bytes, "usage byte delta"), f(t.count, "usage count delta");
119
+ m(t.bytes, "usage byte delta"), m(t.count, "usage count delta");
108
120
  let o = Number.isSafeInteger(a.bytes + t.bytes) && Number.isSafeInteger(a.count + t.count) && (r?.maxBytes === void 0 || a.bytes + t.bytes <= r.maxBytes) && (r?.maxCount === void 0 || a.count + t.count <= r.maxCount);
109
121
  return o && n.set(i, {
110
122
  bytes: a.bytes + t.bytes,
@@ -115,48 +127,52 @@ var R = async (e) => {
115
127
  count: a.count
116
128
  };
117
129
  },
118
- catch: (e) => h(e, "refstore")
130
+ catch: (e) => _(e, "refstore")
119
131
  }),
120
- releaseUsage: (e, t) => N.try({
132
+ releaseUsage: (e, t) => A.try({
121
133
  try: () => {
122
- let r = H(e), i = n.get(r);
123
- f(t.bytes, "usage byte delta"), f(t.count, "usage count delta"), i && n.set(r, {
134
+ let r = K(e), i = n.get(r);
135
+ m(t.bytes, "usage byte delta"), m(t.count, "usage count delta"), i && n.set(r, {
124
136
  bytes: Math.max(0, i.bytes - t.bytes),
125
137
  count: Math.max(0, i.count - t.count)
126
138
  });
127
139
  },
128
- catch: (e) => h(e, "refstore")
140
+ catch: (e) => _(e, "refstore")
129
141
  }),
130
- listByDerivedFrom: (t) => N.sync(() => [...e.values()].filter((e) => (e.derivedFrom ?? null) === t)),
131
- listRefs: (t) => N.sync(() => [...e.values()].filter((e) => V(e, t)).sort((e, t) => t.createdAt.getTime() - e.createdAt.getTime() || t.id.localeCompare(e.id)).slice(t.offset, t.offset + t.limit))
142
+ listByDerivedFrom: (t) => A.sync(() => [...e.values()].filter((e) => (e.derivedFrom ?? null) === t).map(w)),
143
+ listRefs: (t) => A.sync(() => [...e.values()].filter((e) => G(e, t)).sort((e, t) => t.createdAt.getTime() - e.createdAt.getTime() || t.id.localeCompare(e.id)).slice(t.offset, t.offset + t.limit).map(w))
132
144
  };
133
- }, W = (e) => Array.isArray(e) && e.every((e) => typeof e == "string") ? e : null, G = (e) => ({
134
- id: String(e.id),
135
- tenantId: e.tenantId ?? null,
136
- ownerId: e.ownerId ?? null,
137
- bucket: String(e.bucket),
138
- key: String(e.key),
139
- contentType: String(e.contentType),
140
- size: f(e.size, "ref size"),
141
- checksum: String(e.checksum),
142
- visibility: e.visibility === "public" ? "public" : "private",
143
- accessPolicy: e.accessPolicy ?? null,
144
- passwordHash: e.passwordHash ?? null,
145
- createdAt: e.createdAt instanceof Date ? e.createdAt : new Date(String(e.createdAt)),
146
- width: e.width ?? null,
147
- height: e.height ?? null,
148
- duration: e.duration ?? null,
149
- placeholder: e.placeholder ?? null,
150
- folder: e.folder ?? null,
151
- tags: W(e.tags),
152
- alt: e.alt ?? null,
153
- caption: e.caption ?? null,
154
- derivedFrom: e.derivedFrom ?? null,
155
- kind: e.kind ?? null
156
- }), K = (e) => N.tryPromise({
145
+ }, le = (e) => Array.isArray(e) && e.every((e) => typeof e == "string") ? e : null, J = (e) => {
146
+ let t = {
147
+ id: String(e.id),
148
+ tenantId: e.tenantId ?? null,
149
+ ownerId: e.ownerId ?? null,
150
+ bucket: String(e.bucket),
151
+ key: String(e.key),
152
+ contentType: String(e.contentType),
153
+ size: m(e.size, "ref size"),
154
+ checksum: String(e.checksum),
155
+ visibility: e.visibility === "public" ? "public" : "private",
156
+ accessPolicy: e.accessPolicy ?? null,
157
+ passwordHash: e.passwordHash ?? null,
158
+ createdAt: e.createdAt instanceof Date ? e.createdAt : new Date(String(e.createdAt)),
159
+ width: e.width ?? null,
160
+ height: e.height ?? null,
161
+ duration: e.duration ?? null,
162
+ placeholder: e.placeholder ?? null,
163
+ folder: e.folder ?? null,
164
+ tags: le(e.tags),
165
+ alt: e.alt ?? null,
166
+ caption: e.caption ?? null,
167
+ derivedFrom: e.derivedFrom ?? null,
168
+ kind: e.kind ?? null,
169
+ ...e.renditionProof == null ? {} : { renditionProof: T(e.renditionProof) }
170
+ };
171
+ return t.renditionProof !== void 0 && t.renditionProof !== null && re(t.renditionProof, t), t;
172
+ }, Y = (e) => A.tryPromise({
157
173
  try: e,
158
- catch: (e) => h(e, "refstore", !0)
159
- }), q = async (e, t) => (await e.query({
174
+ catch: (e) => _(e, "refstore", !0)
175
+ }), X = async (e, t) => (await e.query({
160
176
  table: "_voltro_storage_usage",
161
177
  predicate: {
162
178
  column: "tenantId",
@@ -167,74 +183,204 @@ var R = async (e) => {
167
183
  take: 1,
168
184
  skip: void 0,
169
185
  projection: void 0
170
- }))[0] ?? null, J = (t, n) => {
171
- let i = Object.freeze({ ...n });
172
- return e(i), {
173
- catalogueId: K(() => r(t, i)),
174
- statistics: () => K(async () => B((await t.query({
175
- table: a,
176
- predicate: T(k("storageInstance", i.storageInstance), k("objectNamespace", i.objectNamespace)),
186
+ }))[0] ?? null, Z = (r, a) => {
187
+ let c = Object.freeze({ ...a }), l = "resolveNamespace" in c;
188
+ if (!c.storageInstance.trim()) throw I("Storage requires an explicit plugin instance");
189
+ l || e(c);
190
+ let u = l ? z("storageInstance", c.storageInstance) : F(z("storageInstance", c.storageInstance), z("objectNamespace", c.objectNamespace)), d = async (e) => {
191
+ if (e.storageInstance !== c.storageInstance) return !1;
192
+ let t = N.decodeUnknownSync(O)({
193
+ destination: e.bucket,
194
+ tenantId: e.tenantId,
195
+ key: e.key
196
+ }), r = await A.runPromise(n(c, t));
197
+ return e.objectNamespace === r.objectNamespace;
198
+ }, f = async function* (e) {
199
+ let t = e.take, n = e.skip ?? 0;
200
+ if (!l) {
201
+ yield* await r.query(e);
202
+ return;
203
+ }
204
+ let i = 0;
205
+ for (let a = 0;; a += 256) {
206
+ let o = await r.query({
207
+ ...e,
208
+ take: 256,
209
+ skip: a,
210
+ projection: void 0
211
+ });
212
+ for (let e of o) if (await d(e) && !(i++ < n)) {
213
+ if (t !== void 0 && i > n + t) return;
214
+ yield e;
215
+ }
216
+ if (o.length < 256) return;
217
+ }
218
+ }, g = async (e) => {
219
+ let t = [];
220
+ for await (let n of f(e)) t.push(n);
221
+ return t;
222
+ };
223
+ return {
224
+ catalogueId: Y(() => "resolveNamespace" in c ? i(r).then((e) => t([
225
+ "reference-view",
226
+ e,
227
+ c.storageInstance
228
+ ])) : i(r, c)),
229
+ hasObjectReservation: (e, t) => Y(async () => (await r.query({
230
+ table: "_voltro_storage_object_heads",
231
+ predicate: F(z("objectNamespace", e), z("key", t)),
232
+ order: [],
233
+ take: 1,
234
+ skip: void 0,
235
+ projection: ["id"]
236
+ })).length > 0),
237
+ listObjectReferences: () => Y(async () => (await r.query({
238
+ table: o,
239
+ predicate: void 0,
177
240
  order: [],
178
241
  take: void 0,
179
242
  skip: void 0,
180
- projection: void 0,
181
- groupBy: ["tenantId", "visibility"],
182
- aggregations: [
183
- D("tenantId"),
184
- D("visibility"),
185
- {
186
- op: "count",
187
- alias: "count"
188
- },
189
- {
190
- op: "sum",
191
- column: "size",
192
- alias: "bytes"
193
- }
243
+ projection: [
244
+ "bucket",
245
+ "tenantId",
246
+ "key",
247
+ "visibility",
248
+ "storageInstance",
249
+ "objectNamespace"
194
250
  ]
195
- })).map((e) => ({
196
- tenantId: e.tenantId == null ? null : String(e.tenantId),
197
- visibility: String(e.visibility),
198
- count: f(e.count, "ref count"),
199
- bytes: f(e.bytes ?? 0, "usage bytes")
200
- })))),
201
- insert: (e) => K(() => t.insert(a, {
202
- ...e,
203
- storageInstance: i.storageInstance,
204
- objectNamespace: i.objectNamespace,
205
- size: String(f(e.size, "ref size"))
251
+ })).map((t) => {
252
+ let n = N.decodeUnknownSync(O)({
253
+ destination: t.bucket,
254
+ tenantId: t.tenantId,
255
+ key: t.key
256
+ }), r = N.decodeUnknownSync(N.Struct({
257
+ storageInstance: N.String,
258
+ objectNamespace: N.String
259
+ }))(t);
260
+ return e(r), {
261
+ ...r,
262
+ bucket: n.destination,
263
+ tenantId: n.tenantId,
264
+ key: n.key,
265
+ visibility: N.decodeUnknownSync(N.Literals(["public", "private"]))(t.visibility)
266
+ };
206
267
  })),
207
- getById: (e) => K(async () => {
208
- let n = await t.query({
209
- table: a,
210
- predicate: T(T(k("storageInstance", i.storageInstance), k("objectNamespace", i.objectNamespace)), k("id", e)),
268
+ statistics: () => Y(async () => {
269
+ if (l) {
270
+ let e = /* @__PURE__ */ new Map();
271
+ for await (let t of f({
272
+ table: o,
273
+ predicate: u,
274
+ order: [{
275
+ column: "id",
276
+ direction: "asc"
277
+ }],
278
+ take: void 0,
279
+ skip: void 0,
280
+ projection: void 0
281
+ })) {
282
+ let n = J(t), r = JSON.stringify([n.tenantId, n.visibility]), i = e.get(r) ?? {
283
+ tenantId: n.tenantId,
284
+ visibility: n.visibility,
285
+ count: 0,
286
+ bytes: 0
287
+ };
288
+ i.count = h(i.count, 1, "ref count"), i.bytes = h(i.bytes, n.size, "usage bytes"), e.set(r, i);
289
+ }
290
+ return W(e.values());
291
+ }
292
+ return W((await r.query({
293
+ table: o,
294
+ predicate: u,
295
+ order: [],
296
+ take: void 0,
297
+ skip: void 0,
298
+ projection: void 0,
299
+ groupBy: ["tenantId", "visibility"],
300
+ aggregations: [
301
+ L("tenantId"),
302
+ L("visibility"),
303
+ {
304
+ op: "count",
305
+ alias: "count"
306
+ },
307
+ {
308
+ op: "sum",
309
+ column: "size",
310
+ alias: "bytes"
311
+ }
312
+ ]
313
+ })).map((e) => ({
314
+ tenantId: e.tenantId == null ? null : String(e.tenantId),
315
+ visibility: String(e.visibility),
316
+ count: m(e.count, "ref count"),
317
+ bytes: m(e.bytes ?? 0, "usage bytes")
318
+ })));
319
+ }),
320
+ insert: (e) => n(c, p(e)).pipe(A.flatMap((t) => Y(() => r.insert(o, {
321
+ ...w(e),
322
+ ...t,
323
+ size: String(m(e.size, "ref size"))
324
+ })))),
325
+ getById: (e) => Y(async () => {
326
+ let t = await r.query({
327
+ table: o,
328
+ predicate: F(u, z("id", e)),
211
329
  order: [],
212
330
  take: 1,
213
331
  skip: void 0,
214
332
  projection: void 0
215
333
  });
216
- return n[0] ? G(n[0]) : null;
334
+ return t[0] && await d(t[0]) ? J(t[0]) : null;
217
335
  }),
218
- delete: (e) => K(async () => await t.deleteMany(a, { where: T(T(k("storageInstance", i.storageInstance), k("objectNamespace", i.objectNamespace)), k("id", e)) }) > 0),
219
- listRecent: (e) => K(async () => (await t.query({
220
- table: a,
221
- predicate: T(k("storageInstance", i.storageInstance), k("objectNamespace", i.objectNamespace)),
336
+ delete: (e) => Y(l ? () => r.transactional(async (t) => {
337
+ let n = await t.query({
338
+ table: o,
339
+ predicate: F(u, z("id", e)),
340
+ lock: "update",
341
+ order: [],
342
+ take: 1,
343
+ skip: void 0,
344
+ projection: void 0
345
+ });
346
+ return !n[0] || !await d(n[0]) ? !1 : await t.deleteMany(o, { where: F(u, z("objectNamespace", n[0].objectNamespace), z("id", e)) }) > 0;
347
+ }) : async () => await r.deleteMany(o, { where: F(u, z("id", e)) }) > 0),
348
+ listRecent: (e) => Y(async () => (await g({
349
+ table: o,
350
+ predicate: u,
222
351
  order: [{
223
352
  column: "createdAt",
224
353
  direction: "desc"
354
+ }, {
355
+ column: "id",
356
+ direction: "desc"
225
357
  }],
226
358
  take: e,
227
359
  skip: void 0,
228
360
  projection: void 0
229
- })).map(G)),
230
- countByKey: (e, n) => K(async () => {
231
- let e = await t.query({
232
- table: a,
233
- predicate: { and: [k("objectNamespace", i.objectNamespace), {
234
- column: "key",
235
- op: "eq",
236
- value: n
237
- }] },
361
+ })).map(J)),
362
+ countByAddress: (e) => n(c, e).pipe(A.flatMap((t) => Y(async () => {
363
+ let n = await r.query({
364
+ table: o,
365
+ predicate: { and: [
366
+ u,
367
+ z("objectNamespace", t.objectNamespace),
368
+ {
369
+ column: "bucket",
370
+ op: "eq",
371
+ value: e.destination
372
+ },
373
+ {
374
+ column: "tenantId",
375
+ op: "eq",
376
+ value: e.tenantId
377
+ },
378
+ {
379
+ column: "key",
380
+ op: "eq",
381
+ value: e.key
382
+ }
383
+ ] },
238
384
  order: [],
239
385
  take: void 0,
240
386
  skip: void 0,
@@ -243,25 +389,29 @@ var R = async (e) => {
243
389
  op: "count",
244
390
  alias: "count"
245
391
  }]
246
- }), r = f(e[0]?.count ?? 0, "ref count");
247
- return r === 0 ? (await t.query({
248
- table: "_voltro_storage_object_heads",
249
- predicate: T(k("objectNamespace", i.objectNamespace), k("key", n)),
250
- order: [],
251
- take: 1,
252
- skip: void 0,
253
- projection: ["id"]
254
- })).length : r;
255
- }),
256
- countPublicByKey: (e, n) => K(async () => {
257
- let e = await t.query({
258
- table: a,
392
+ });
393
+ return m(n[0]?.count ?? 0, "ref count");
394
+ }))),
395
+ countPublicByAddress: (e) => n(c, e).pipe(A.flatMap((t) => Y(async () => {
396
+ let n = await r.query({
397
+ table: o,
259
398
  predicate: { and: [
260
- k("objectNamespace", i.objectNamespace),
399
+ u,
400
+ z("objectNamespace", t.objectNamespace),
401
+ {
402
+ column: "bucket",
403
+ op: "eq",
404
+ value: e.destination
405
+ },
406
+ {
407
+ column: "tenantId",
408
+ op: "eq",
409
+ value: e.tenantId
410
+ },
261
411
  {
262
412
  column: "key",
263
413
  op: "eq",
264
- value: n
414
+ value: e.key
265
415
  },
266
416
  {
267
417
  column: "visibility",
@@ -278,25 +428,26 @@ var R = async (e) => {
278
428
  alias: "count"
279
429
  }]
280
430
  });
281
- return f(e[0]?.count ?? 0, "ref count");
282
- }),
283
- setVisibility: (e, n) => K(() => t.transactional(async (t) => {
284
- if ((await t.query({
285
- table: "_voltro_storage_refs",
286
- predicate: T(T(k("storageInstance", i.storageInstance), k("objectNamespace", i.objectNamespace)), k("id", e)),
431
+ return m(n[0]?.count ?? 0, "ref count");
432
+ }))),
433
+ setVisibility: (e, t) => Y(() => r.transactional(async (n) => {
434
+ let r = await n.query({
435
+ table: o,
436
+ predicate: F(u, z("id", e)),
287
437
  lock: "update",
288
438
  order: [],
289
439
  take: 1,
290
440
  skip: void 0,
291
- projection: ["id"]
292
- })).length === 0) return null;
293
- let r = await t.update(a, e, { visibility: n });
294
- return r ? G(r) : null;
441
+ projection: void 0
442
+ });
443
+ if (!r[0] || !await d(r[0])) return null;
444
+ let i = await n.update(o, e, { visibility: t });
445
+ return i ? J(i) : null;
295
446
  })),
296
- usageByTenant: (e) => K(async () => {
297
- let n = await t.query({
298
- table: a,
299
- predicate: e === null ? j("tenantId") : k("tenantId", e),
447
+ usageByTenant: (e) => Y(async () => {
448
+ let t = await r.query({
449
+ table: o,
450
+ predicate: e === null ? V("tenantId") : z("tenantId", e),
300
451
  order: [],
301
452
  take: void 0,
302
453
  skip: void 0,
@@ -323,91 +474,94 @@ var R = async (e) => {
323
474
  }
324
475
  ]
325
476
  });
326
- return f(n[0]?.count ?? 0, "ref count") > 0 && (f(n[0]?.minSize, "ref size"), f(n[0]?.maxSize, "ref size")), {
327
- bytes: f(n[0]?.bytes ?? 0, "usage bytes"),
328
- count: f(n[0]?.count ?? 0, "ref count")
477
+ return m(t[0]?.count ?? 0, "ref count") > 0 && (m(t[0]?.minSize, "ref size"), m(t[0]?.maxSize, "ref size")), {
478
+ bytes: m(t[0]?.bytes ?? 0, "usage bytes"),
479
+ count: m(t[0]?.count ?? 0, "ref count")
329
480
  };
330
481
  }),
331
- consumeUsage: (e, n, r) => K(async () => {
332
- let i = H(e);
333
- f(n.bytes, "usage byte delta"), f(n.count, "usage count delta");
482
+ consumeUsage: (e, t, n) => Y(async () => {
483
+ let i = K(e);
484
+ m(t.bytes, "usage byte delta"), m(t.count, "usage count delta");
334
485
  for (let e = 0; e < 32; e++) {
335
486
  e > 0 && await new Promise((t) => setTimeout(t, e));
336
- let a = await q(t, i), s = a ? f(a.bytes, "usage bytes") : 0, c = a ? f(a.count, "ref count") : 0;
337
- if (!(Number.isSafeInteger(s + n.bytes) && Number.isSafeInteger(c + n.count) && (r?.maxBytes === void 0 || s + n.bytes <= r.maxBytes) && (r?.maxCount === void 0 || c + n.count <= r.maxCount))) return {
487
+ let a = await X(r, i), o = a ? m(a.bytes, "usage bytes") : 0, c = a ? m(a.count, "ref count") : 0;
488
+ if (!(Number.isSafeInteger(o + t.bytes) && Number.isSafeInteger(c + t.count) && (n?.maxBytes === void 0 || o + t.bytes <= n.maxBytes) && (n?.maxCount === void 0 || c + t.count <= n.maxCount))) return {
338
489
  allowed: !1,
339
- bytes: s,
490
+ bytes: o,
340
491
  count: c
341
492
  };
342
493
  if (a) {
343
- if (await t.updateMany("_voltro_storage_usage", {
344
- bytes: String(s + n.bytes),
345
- count: String(c + n.count),
494
+ if (await r.updateMany("_voltro_storage_usage", {
495
+ bytes: String(o + t.bytes),
496
+ count: String(c + t.count),
346
497
  updatedAt: /* @__PURE__ */ new Date()
347
- }, { where: T(k("id", String(a.id)), k("bytes", a.bytes), k("count", a.count)) }) === 1) return {
498
+ }, { where: F(z("id", String(a.id)), z("bytes", a.bytes), z("count", a.count)) }) === 1) return {
348
499
  allowed: !0,
349
- bytes: s,
500
+ bytes: o,
350
501
  count: c
351
502
  };
352
503
  } else {
353
- let e = A({
504
+ let e = B({
354
505
  kind: "typeid",
355
506
  prefix: "susage"
356
- }, o), r = await t.insertIgnore(o, {
507
+ }, s), n = await r.insertIgnore(s, {
357
508
  id: e,
358
509
  tenantId: i,
359
- bytes: String(n.bytes),
360
- count: String(n.count),
510
+ bytes: String(t.bytes),
511
+ count: String(t.count),
361
512
  updatedAt: /* @__PURE__ */ new Date()
362
513
  }, { conflictColumns: ["tenantId"] });
363
- if (String(r.id) === e) return {
514
+ if (String(n.id) === e) return {
364
515
  allowed: !0,
365
516
  bytes: 0,
366
517
  count: 0
367
518
  };
368
519
  }
369
520
  }
370
- throw E("Storage quota reservation contended after 32 attempts; retry");
521
+ throw I("Storage quota reservation contended after 32 attempts; retry");
371
522
  }),
372
- releaseUsage: (e, n) => K(async () => {
373
- let r = H(e);
374
- f(n.bytes, "usage byte delta"), f(n.count, "usage count delta");
523
+ releaseUsage: (e, t) => Y(async () => {
524
+ let n = K(e);
525
+ m(t.bytes, "usage byte delta"), m(t.count, "usage count delta");
375
526
  for (let e = 0; e < 32; e++) {
376
527
  e > 0 && await new Promise((t) => setTimeout(t, e));
377
- let i = await q(t, r);
528
+ let i = await X(r, n);
378
529
  if (!i) return;
379
- let a = f(i.bytes, "usage bytes"), o = f(i.count, "ref count");
380
- if (await t.updateMany("_voltro_storage_usage", {
381
- bytes: String(Math.max(0, a - n.bytes)),
382
- count: String(Math.max(0, o - n.count)),
530
+ let a = m(i.bytes, "usage bytes"), o = m(i.count, "ref count");
531
+ if (await r.updateMany("_voltro_storage_usage", {
532
+ bytes: String(Math.max(0, a - t.bytes)),
533
+ count: String(Math.max(0, o - t.count)),
383
534
  updatedAt: /* @__PURE__ */ new Date()
384
- }, { where: T(k("id", String(i.id)), k("bytes", i.bytes), k("count", i.count)) }) === 1) return;
535
+ }, { where: F(z("id", String(i.id)), z("bytes", i.bytes), z("count", i.count)) }) === 1) return;
385
536
  }
386
- throw E("Storage quota release contended after 32 attempts; retry");
537
+ throw I("Storage quota release contended after 32 attempts; retry");
387
538
  }),
388
- listByDerivedFrom: (e) => K(async () => (await t.query({
389
- table: a,
390
- predicate: T(T(k("storageInstance", i.storageInstance), k("objectNamespace", i.objectNamespace)), k("derivedFrom", e)),
539
+ listByDerivedFrom: (e) => Y(async () => (await g({
540
+ table: o,
541
+ predicate: F(u, z("derivedFrom", e)),
391
542
  order: [{
392
543
  column: "createdAt",
393
544
  direction: "asc"
545
+ }, {
546
+ column: "id",
547
+ direction: "asc"
394
548
  }],
395
549
  take: void 0,
396
550
  skip: void 0,
397
551
  projection: void 0
398
- })).map(G)),
399
- listRefs: (e) => K(async () => {
400
- let n = [T(k("storageInstance", i.storageInstance), k("objectNamespace", i.objectNamespace))];
401
- e.tenantId !== void 0 && n.push(e.tenantId === null ? j("tenantId") : k("tenantId", e.tenantId)), e.ownerId !== void 0 && n.push(k("ownerId", e.ownerId)), e.search && n.push(M(...[
552
+ })).map(J)),
553
+ listRefs: (e) => Y(async () => {
554
+ let t = [u];
555
+ e.tenantId !== void 0 && t.push(e.tenantId === null ? V("tenantId") : z("tenantId", e.tenantId)), e.ownerId !== void 0 && t.push(z("ownerId", e.ownerId)), e.search && t.push(H(...[
402
556
  "id",
403
557
  "key",
404
558
  "contentType",
405
559
  "ownerId",
406
560
  "tenantId"
407
- ].map((t) => O(t, e.search)))), e.includeDerived || n.push(j("derivedFrom"));
408
- let r = e.folder === void 0 && e.tags.length === 0, o = n.length ? T(...n) : void 0, s = await t.query({
409
- table: a,
410
- predicate: o,
561
+ ].map((t) => R(t, e.search)))), e.includeDerived || t.push(V("derivedFrom"));
562
+ let n = e.folder === void 0 && e.tags.length === 0, r = t.length ? F(...t) : void 0, i = await g({
563
+ table: o,
564
+ predicate: r,
411
565
  order: [{
412
566
  column: "createdAt",
413
567
  direction: "desc"
@@ -415,29 +569,29 @@ var R = async (e) => {
415
569
  column: "id",
416
570
  direction: "desc"
417
571
  }],
418
- take: r ? e.limit : void 0,
419
- skip: r ? e.offset : void 0,
572
+ take: n ? e.limit : void 0,
573
+ skip: n ? e.offset : void 0,
420
574
  projection: void 0
421
575
  });
422
- return r ? s.map(G) : s.map(G).filter((t) => V(t, e)).slice(e.offset, e.offset + e.limit);
576
+ return n ? i.map(J) : i.map(J).filter((t) => G(t, e)).slice(e.offset, e.offset + e.limit);
423
577
  })
424
578
  };
425
- }, Y = () => {
579
+ }, Q = () => {
426
580
  let e = /* @__PURE__ */ new Map();
427
581
  return {
428
- insert: (t) => N.sync(() => {
582
+ insert: (t) => A.sync(() => {
429
583
  e.set(t.id, t);
430
584
  }),
431
- delete: (t) => N.sync(() => {
585
+ delete: (t) => A.sync(() => {
432
586
  e.delete(t);
433
587
  }),
434
- getById: (t) => N.sync(() => e.get(t) ?? null),
435
- listByRef: (t) => N.sync(() => [...e.values()].filter((e) => e.refId === t)),
436
- deleteByRef: (t) => N.sync(() => {
588
+ getById: (t) => A.sync(() => e.get(t) ?? null),
589
+ listByRef: (t) => A.sync(() => [...e.values()].filter((e) => e.refId === t)),
590
+ deleteByRef: (t) => A.sync(() => {
437
591
  for (let [n, r] of e) r.refId === t && e.delete(n);
438
592
  })
439
593
  };
440
- }, X = (e) => ({
594
+ }, $ = (e) => ({
441
595
  id: String(e.id),
442
596
  refId: String(e.refId),
443
597
  principalType: e.principalType,
@@ -446,28 +600,28 @@ var R = async (e) => {
446
600
  createdAt: e.createdAt instanceof Date ? e.createdAt : new Date(String(e.createdAt)),
447
601
  expiresAt: e.expiresAt ? e.expiresAt instanceof Date ? e.expiresAt : new Date(String(e.expiresAt)) : null,
448
602
  createdBy: e.createdBy ?? null
449
- }), Z = (t, n) => {
603
+ }), ue = (t, n) => {
450
604
  let r = Object.freeze({ ...n });
451
605
  return e(r), {
452
- insert: (e) => K(() => t.insert(i, {
606
+ insert: (e) => Y(() => t.insert(a, {
453
607
  ...e,
454
608
  ...r
455
609
  })),
456
- delete: (e) => K(() => t.deleteMany(i, { where: T(T(k("storageInstance", r.storageInstance), k("objectNamespace", r.objectNamespace)), k("id", e)) })).pipe(N.asVoid),
457
- getById: (e) => K(async () => {
610
+ delete: (e) => Y(() => t.deleteMany(a, { where: F(F(z("storageInstance", r.storageInstance), z("objectNamespace", r.objectNamespace)), z("id", e)) })).pipe(A.asVoid),
611
+ getById: (e) => Y(async () => {
458
612
  let n = (await t.query({
459
- table: i,
460
- predicate: T(T(k("storageInstance", r.storageInstance), k("objectNamespace", r.objectNamespace)), k("id", e)),
613
+ table: a,
614
+ predicate: F(F(z("storageInstance", r.storageInstance), z("objectNamespace", r.objectNamespace)), z("id", e)),
461
615
  order: [],
462
616
  take: 1,
463
617
  skip: void 0,
464
618
  projection: void 0
465
619
  }))[0];
466
- return n ? X(n) : null;
620
+ return n ? $(n) : null;
467
621
  }),
468
- listByRef: (e) => K(async () => (await t.query({
469
- table: i,
470
- predicate: T(T(k("storageInstance", r.storageInstance), k("objectNamespace", r.objectNamespace)), k("refId", e)),
622
+ listByRef: (e) => Y(async () => (await t.query({
623
+ table: a,
624
+ predicate: F(F(z("storageInstance", r.storageInstance), z("objectNamespace", r.objectNamespace)), z("refId", e)),
471
625
  order: [{
472
626
  column: "createdAt",
473
627
  direction: "desc"
@@ -475,382 +629,523 @@ var R = async (e) => {
475
629
  take: void 0,
476
630
  skip: void 0,
477
631
  projection: void 0
478
- })).map(X)),
479
- deleteByRef: (e) => K(async () => {
632
+ })).map($)),
633
+ deleteByRef: (e) => Y(async () => {
480
634
  let n = await t.query({
481
- table: i,
482
- predicate: T(T(k("storageInstance", r.storageInstance), k("objectNamespace", r.objectNamespace)), k("refId", e)),
635
+ table: a,
636
+ predicate: F(F(z("storageInstance", r.storageInstance), z("objectNamespace", r.objectNamespace)), z("refId", e)),
483
637
  order: [],
484
638
  take: void 0,
485
639
  skip: void 0,
486
640
  projection: void 0
487
641
  });
488
- for (let e of n) await t.delete(i, String(e.id));
642
+ for (let e of n) await t.delete(a, String(e.id));
489
643
  })
490
644
  };
491
- }, Q = () => A({
645
+ }, de = (e, t) => {
646
+ let r = Object.freeze({ ...t });
647
+ if (!("resolveNamespace" in r)) return ue(e, r);
648
+ let i = Z(e, r), o = (t) => i.getById(t).pipe(A.flatMap((t) => t === null ? A.succeed(void 0) : n(r, p(t)).pipe(A.map((t) => ue(e, t))))), s = (t) => Y(() => e.query({
649
+ table: a,
650
+ predicate: F(z("storageInstance", r.storageInstance), z("id", t)),
651
+ order: [],
652
+ take: 1,
653
+ skip: void 0,
654
+ projection: ["refId"]
655
+ })).pipe(A.flatMap((e) => e[0] === void 0 ? A.succeed(void 0) : o(String(e[0].refId))));
656
+ return {
657
+ insert: (e) => o(e.refId).pipe(A.flatMap((t) => t === void 0 ? A.fail(new l({
658
+ provider: "metadata",
659
+ message: "Storage grant reference is outside the current destination view",
660
+ transient: !1,
661
+ status: 409
662
+ })) : t.insert(e))),
663
+ delete: (e) => s(e).pipe(A.flatMap((t) => t === void 0 ? A.void : t.delete(e))),
664
+ getById: (e) => s(e).pipe(A.flatMap((t) => t === void 0 ? A.succeed(null) : t.getById(e))),
665
+ listByRef: (e) => o(e).pipe(A.flatMap((t) => t === void 0 ? A.succeed([]) : t.listByRef(e))),
666
+ deleteByRef: (e) => o(e).pipe(A.flatMap((t) => t === void 0 ? A.void : t.deleteByRef(e)))
667
+ };
668
+ }, fe = () => B({
492
669
  kind: "typeid",
493
670
  prefix: "file"
494
- }, a), ne = () => A({
671
+ }, o), pe = () => B({
495
672
  kind: "typeid",
496
673
  prefix: "grant"
497
- }, i), $ = (e) => {
498
- let { provider: t, refStore: r, grantStore: i, bucket: o } = e, d = e.access ?? {}, f = e.attempts ?? 3, p = e.urlExpiresInSec ?? 3600, w = (t) => z(e.publicBaseUrl, t, e.httpPath ?? "/_voltro/storage"), T = I.max([I.recurs(Math.max(0, f - 1)), I.exponential("100 millis")]).pipe(I.while(({ input: e }) => e.transient)), E = (t, n, r) => [
674
+ }, a), me = (e) => {
675
+ let { provider: n, refStore: i, grantStore: a } = e, s = (t) => N.decodeUnknownEffect(O)(p(t)).pipe(A.mapError(() => new l({
676
+ provider: "storage",
677
+ message: "Invalid stored object address",
678
+ transient: !1
679
+ })), A.flatMap((r) => e.destinations === void 0 ? t.bucket === e.bucket ? A.succeed(n) : A.fail(new l({
680
+ provider: "storage",
681
+ message: "Stored object destination is not configured",
682
+ transient: !1
683
+ })) : e.destinations.resolve(r))), f = (t) => ae(t, i, (e) => s({
684
+ bucket: e.destination,
685
+ tenantId: e.tenantId,
686
+ key: e.key
687
+ }), e.resolveObjectReference), m = (e) => s(e).pipe(A.map(f)), h = (t, r, i) => N.decodeUnknownEffect(O)({
688
+ destination: i ?? e.bucket,
689
+ tenantId: t,
690
+ key: r
691
+ }).pipe(A.mapError(() => new l({
692
+ provider: "storage",
693
+ message: "Invalid new object address",
694
+ transient: !1,
695
+ status: 400
696
+ })), A.flatMap(() => i === void 0 ? e.destinations === void 0 ? A.succeed({
697
+ destination: e.bucket,
698
+ provider: n
699
+ }) : e.destinations.select(t) : s({
700
+ bucket: i,
701
+ tenantId: t,
702
+ key: r
703
+ }).pipe(A.map((e) => ({
704
+ destination: i,
705
+ provider: e
706
+ })))), A.map((e) => ({
707
+ ...e,
708
+ provider: f(e.provider)
709
+ }))), T = (t) => A.gen(function* () {
710
+ let n = yield* i.catalogueId, a = yield* t.objectNamespace;
711
+ return yield* A.try({
712
+ try: () => r({
713
+ catalogueId: n,
714
+ objectNamespace: a,
715
+ storageInstance: e.storageInstance ?? "@voltro/plugin-storage"
716
+ }),
717
+ catch: (e) => _(e, "storage", !1)
718
+ });
719
+ }), j = e.access ?? {}, P = e.attempts ?? 3, F = e.urlExpiresInSec ?? 3600;
720
+ x(F);
721
+ let I = (t) => ce(e.publicBaseUrl, t, e.httpPath ?? "/_voltro/storage"), L = M.max([M.recurs(Math.max(0, P - 1)), M.exponential("100 millis")]).pipe(M.while(({ input: e }) => e.transient)), R = (e) => e === null ? "global" : typeof e != "string" || e.length === 0 || /[\uD800-\uDFFF]/u.test(e) ? "\0" : e === "global" ? "%67lobal" : e === "." ? "%2E" : e === ".." ? "%2E%2E" : encodeURIComponent(e), z = (t, n, r) => [
499
722
  e.tenantPrefix,
500
- t ?? "global",
723
+ R(t),
501
724
  r ?? n
502
- ].filter(Boolean).join("/"), D = (e) => {
503
- if (e === void 0) return N.succeed(void 0);
504
- let n = e.length === 0 ? "empty" : e.includes("\0") ? "contains NUL" : e.startsWith("/") || e.startsWith("\\") ? "starts with a path separator" : e.includes("\\") ? "contains a backslash" : e.split("/").some((e) => e === "" || e === "." || e === "..") ? "contains an empty, \".\" or \"..\" segment" : void 0;
505
- return n === void 0 ? N.succeed(e) : N.fail(new c({
506
- provider: t.name,
507
- message: `storage key rejected: ${n}`,
725
+ ].filter(Boolean).join("/"), V = (e) => {
726
+ if (e === void 0) return A.succeed(void 0);
727
+ let t = k(e);
728
+ return t === void 0 ? A.succeed(e) : A.fail(new l({
729
+ provider: "storage",
730
+ message: `storage key rejected: ${t}`,
508
731
  transient: !1,
509
732
  status: 400
510
733
  }));
511
- }, O = (t, n, r) => [
734
+ }, H = (t, n, r) => [
512
735
  e.tenantPrefix,
513
- t ?? "global",
736
+ R(t),
514
737
  "_incoming",
515
738
  n,
516
739
  String(r).padStart(6, "0")
517
- ].filter(Boolean).join("/"), k = (e, t) => t === void 0 || e.tenantId === t, j = (e, n) => k(e, n) ? N.succeed(e) : N.fail(new c({
518
- provider: t.name,
740
+ ].filter(Boolean).join("/"), W = (e, t) => t === void 0 || e.tenantId === t, G = (e, t) => t !== void 0 && e.tenantId !== t ? A.fail(new l({
741
+ provider: "storage",
519
742
  message: "cross-tenant access denied",
520
743
  transient: !1,
521
744
  status: 403
522
- })), M = (n, r, i) => N.gen(function* () {
523
- let a = e.objects?.(), o = (e) => new c({
745
+ })) : A.succeed(e), K = (t, n, r) => A.gen(function* () {
746
+ let i = e.objects?.(), a = (e) => new l({
524
747
  provider: "metadata",
525
748
  message: e,
526
749
  transient: !1,
527
750
  status: 409
528
751
  });
529
- if (a === void 0) return i === void 0 ? !1 : yield* N.fail(o(`claim '${i}' cannot be released: no data store is bound, so ref '${n}' holds no claims`));
530
- let s = yield* N.tryPromise({
531
- try: () => a.intent(n),
532
- catch: (e) => new c({
752
+ if (i === void 0) return r === void 0 ? !1 : yield* A.fail(a(`claim '${r}' cannot be released: no data store is bound, so ref '${t}' holds no claims`));
753
+ let o = yield* A.tryPromise({
754
+ try: () => i.intent(t),
755
+ catch: (e) => new l({
533
756
  provider: "metadata",
534
757
  message: String(e?.message ?? e),
535
758
  transient: !1
536
759
  })
537
760
  });
538
- return s === void 0 ? i === void 0 ? !1 : yield* N.fail(o(`claim '${i}' cannot be released: ref '${n}' was not written through an operation and holds no claims`)) : (yield* j(s.ref, r), yield* C({
539
- metadata: a,
761
+ if (o === void 0) return r === void 0 ? !1 : yield* A.fail(a(`claim '${r}' cannot be released: ref '${t}' was not written through an operation and holds no claims`));
762
+ yield* G(o.ref, n);
763
+ let s = yield* m(o.ref);
764
+ return yield* ie({
765
+ metadata: i,
540
766
  provider: {
541
- ...t,
542
- delete: (e) => t.delete(e).pipe(N.retry(T))
767
+ ...s,
768
+ delete: (e) => s.delete(e).pipe(A.retry(L))
543
769
  }
544
- }).delete(n, r, i).pipe(N.catchTag("StorageRejected", (e) => N.fail(e.reason === "claim-not-held" ? o(e.detail) : new c({
770
+ }).delete(t, n, r).pipe(A.catchTag("StorageRejected", (e) => A.fail(e.reason === "claim-not-held" ? a(e.detail) : new l({
545
771
  provider: "metadata",
546
772
  message: e.detail,
547
773
  transient: !1
548
- })))), !0);
549
- }), F = (e, n) => r.getById(e).pipe(N.flatMap((r) => r ? j(r, n) : N.fail(new c({
550
- provider: t.name,
774
+ })))), !0;
775
+ }), q = (e, t) => i.getById(e).pipe(A.flatMap((n) => n ? G(n, t) : A.fail(new l({
776
+ provider: "storage",
551
777
  message: `no ref '${e}'`,
552
778
  transient: !1,
553
779
  status: 404
554
- })))), B = (t) => N.suspend(() => {
780
+ })))), le = (t) => A.suspend(() => {
555
781
  let n = e.scan(t);
556
- return N.isEffect(n) ? n : N.tryPromise({
782
+ return A.isEffect(n) ? n : A.tryPromise({
557
783
  try: () => Promise.resolve(n),
558
- catch: (e) => new c({
784
+ catch: (e) => new l({
559
785
  provider: "scan",
560
786
  message: String(e?.message ?? e),
561
787
  transient: !0
562
788
  })
563
789
  });
564
- }), V = (t) => N.suspend(() => {
790
+ }), J = (t) => A.suspend(() => {
565
791
  let n = t.contentType;
566
- if (!e.videoProbe || !(n.startsWith("video/") || n.startsWith("audio/"))) return N.succeed(null);
792
+ if (!e.videoProbe || !(n.startsWith("video/") || n.startsWith("audio/"))) return A.succeed(null);
567
793
  let r;
568
794
  try {
569
795
  r = e.videoProbe(t);
570
796
  } catch {
571
- return N.succeed(null);
797
+ return A.succeed(null);
572
798
  }
573
- return N.isEffect(r) ? r.pipe(N.catch(() => N.succeed(null))) : N.tryPromise({
799
+ return A.isEffect(r) ? r.pipe(A.catch(() => A.succeed(null))) : A.tryPromise({
574
800
  try: () => Promise.resolve(r),
575
801
  catch: () => null
576
- }).pipe(N.catch(() => N.succeed(null)));
577
- }), H = (n) => N.gen(function* () {
578
- let i = ee(n.bytes, n.contentType, e.limits);
579
- if (i) return yield* N.fail(new l(i));
580
- if (n.claim !== void 0 && n.operationId === void 0) return yield* N.fail(new l({
802
+ }).pipe(A.catch(() => A.succeed(null)));
803
+ }), Y = (t, n) => A.gen(function* () {
804
+ let r = te(t.bytes, t.contentType, e.limits);
805
+ if (r) return yield* A.fail(new u(r));
806
+ if (t.claim !== void 0 && t.operationId === void 0) return yield* A.fail(new u({
581
807
  reason: "claim-without-operation",
582
808
  detail: "a put with a claim needs an operationId; the claim names this caller's hold on that operation's object"
583
809
  }));
584
810
  if (e.scan) {
585
- let e = yield* B({
586
- bytes: n.bytes,
587
- contentType: n.contentType,
588
- ...n.key ? { key: n.key } : {}
811
+ let e = yield* le({
812
+ bytes: t.bytes,
813
+ contentType: t.contentType,
814
+ ...t.key ? { key: t.key } : {}
589
815
  });
590
- if (!e.clean) return yield* N.fail(new u({ threat: e.threat ?? "unknown" }));
816
+ if (!e.clean) return yield* A.fail(new d({ threat: e.threat ?? "unknown" }));
591
817
  }
592
- let a = yield* N.promise(() => te(n.bytes, n.contentType, { normalize: e.normalizeImages ?? !1 })), s = a.bytes, c = yield* V({
593
- bytes: s,
594
- contentType: n.contentType
595
- }), d = a.width ?? c?.width, f = a.height ?? c?.height, p = a.placeholder ?? c?.poster, m = n.duration ?? c?.duration, h = yield* N.promise(() => R(s)), g = n.tenantId ?? null, _ = n.visibility ?? "private", y = yield* D(n.key), b = E(g, h, y), x = n.operationId === void 0 ? E(g, h, `i-${yield* N.promise(() => R(new TextEncoder().encode(JSON.stringify([e.storageInstance ?? "@voltro/plugin-storage", o]))))}/plain/${y ?? h}`) : b, S = n.password ? yield* N.sync(() => v(n.password)) : null, w = {
596
- id: Q(),
818
+ let a = yield* A.promise(() => ne(t.bytes, t.contentType, { normalize: e.normalizeImages ?? !1 })), o = a.bytes, s = yield* J({
819
+ bytes: o,
820
+ contentType: t.contentType
821
+ }), c = a.width ?? s?.width, l = a.height ?? s?.height, f = a.placeholder ?? s?.poster, p = t.duration ?? s?.duration, m = yield* A.promise(() => U(o)), g = t.tenantId ?? null, _ = t.visibility ?? "private", v = yield* V(t.key), y = z(g, m, v), b = yield* h(g, y, t.destination), { provider: x } = b, S = b.destination, C = t.operationId === void 0 ? z(g, m, `i-${yield* A.promise(() => U(new TextEncoder().encode(JSON.stringify([e.storageInstance ?? "@voltro/plugin-storage", S]))))}/plain/${v ?? m}`) : y, T = t.password ? yield* A.sync(() => ee(t.password)) : null, E = {
822
+ id: fe(),
597
823
  tenantId: g,
598
- ownerId: n.ownerId ?? null,
599
- bucket: o,
600
- key: x,
601
- contentType: n.contentType,
602
- size: s.byteLength,
603
- checksum: h,
824
+ ownerId: t.ownerId ?? null,
825
+ bucket: S,
826
+ key: C,
827
+ contentType: t.contentType,
828
+ size: o.byteLength,
829
+ checksum: m,
604
830
  visibility: _,
605
- accessPolicy: n.access ?? null,
606
- passwordHash: S,
831
+ accessPolicy: t.access ?? null,
832
+ passwordHash: T,
607
833
  createdAt: /* @__PURE__ */ new Date(),
608
- ...d === void 0 ? {} : { width: d },
609
- ...f === void 0 ? {} : { height: f },
610
- ...p === void 0 ? {} : { placeholder: p },
611
- ...m === void 0 ? {} : { duration: m },
612
- ...n.folder === void 0 ? {} : { folder: n.folder },
613
- ...n.tags === void 0 ? {} : { tags: n.tags },
614
- ...n.alt === void 0 ? {} : { alt: n.alt },
615
- ...n.caption === void 0 ? {} : { caption: n.caption },
616
- ...n.derivedFrom === void 0 ? {} : { derivedFrom: n.derivedFrom },
617
- ...n.kind === void 0 ? {} : { kind: n.kind }
618
- }, O = e.transcode !== void 0 && e.autoTranscode !== !1, k = (e) => O && e.derivedFrom == null && (n.contentType.startsWith("video/") || n.contentType.startsWith("audio/")) ? N.forkDetach(W(e).pipe(N.ignore)) : N.void;
619
- if (n.operationId !== void 0) {
620
- let r = e.objects?.();
621
- if (r === void 0) return yield* N.fail(new l({
834
+ ...c === void 0 ? {} : { width: c },
835
+ ...l === void 0 ? {} : { height: l },
836
+ ...f === void 0 ? {} : { placeholder: f },
837
+ ...p === void 0 ? {} : { duration: p },
838
+ ...t.folder === void 0 ? {} : { folder: t.folder },
839
+ ...t.tags === void 0 ? {} : { tags: t.tags },
840
+ ...t.alt === void 0 ? {} : { alt: t.alt },
841
+ ...t.caption === void 0 ? {} : { caption: t.caption },
842
+ ...t.derivedFrom === void 0 ? {} : { derivedFrom: t.derivedFrom },
843
+ ...t.kind === void 0 ? {} : { kind: t.kind }
844
+ }, D = n === void 0 ? E : {
845
+ ...E,
846
+ renditionProof: yield* A.try({
847
+ try: () => re(n, E),
848
+ catch: (e) => e
849
+ })
850
+ }, ae = e.transcode !== void 0 && e.autoTranscode !== !1, O = (e) => ae && e.derivedFrom == null && (t.contentType.startsWith("video/") || t.contentType.startsWith("audio/")) ? A.forkDetach(Z(e).pipe(A.ignore)) : A.void;
851
+ if (t.operationId !== void 0) {
852
+ let n = e.objects?.();
853
+ if (n === void 0) return yield* A.fail(new u({
622
854
  reason: "operation-store-unbound",
623
855
  detail: "a put with an operationId needs a bound data store; the in-memory ref store cannot hold operation intents"
624
856
  }));
625
- let i = {
857
+ let r = {
626
858
  tenantId: g,
627
- bucket: o,
628
- operationId: n.operationId,
629
- checksum: h,
630
- logicalKey: x,
631
- fingerprint: yield* N.promise(() => R(new TextEncoder().encode(JSON.stringify([
632
- h,
633
- n.contentType,
859
+ bucket: S,
860
+ operationId: t.operationId,
861
+ checksum: m,
862
+ logicalKey: C,
863
+ fingerprint: yield* A.promise(() => U(new TextEncoder().encode(JSON.stringify([
864
+ m,
865
+ t.contentType,
634
866
  _,
635
- n.access ?? null,
636
- n.key ?? null,
637
- n.ownerId ?? null,
638
- n.folder ?? null,
639
- n.tags ?? null,
640
- n.alt ?? null,
641
- n.caption ?? null,
642
- n.derivedFrom ?? null,
643
- n.kind ?? null,
644
- m ?? null,
645
- n.password !== void 0
867
+ t.access ?? null,
868
+ t.key ?? null,
869
+ t.ownerId ?? null,
870
+ t.folder ?? null,
871
+ t.tags ?? null,
872
+ t.alt ?? null,
873
+ t.caption ?? null,
874
+ t.derivedFrom ?? null,
875
+ t.kind ?? null,
876
+ p ?? null,
877
+ t.password !== void 0
646
878
  ]))))
647
- }, a = yield* C({
648
- metadata: r,
879
+ }, i = yield* ie({
880
+ metadata: n,
649
881
  provider: {
650
- ...t,
651
- put: (e, n, r) => t.put(e, n, r).pipe(N.retry(T))
882
+ ...x,
883
+ put: (e, t, n) => x.put(e, t, n).pipe(A.retry(L))
652
884
  }
653
885
  }).put({
654
- identity: i,
655
- ref: w,
656
- bytes: s,
886
+ identity: r,
887
+ ref: D,
888
+ bytes: o,
657
889
  ...e.quota === void 0 ? {} : { quota: e.quota },
658
- ...n.claim === void 0 ? {} : { claim: n.claim }
890
+ ...t.claim === void 0 ? {} : { claim: t.claim }
659
891
  });
660
- return yield* k(a.ref), {
661
- ...a.ref,
662
- outcome: a.outcome
892
+ return yield* O(i.ref), {
893
+ ...i.ref,
894
+ outcome: i.outcome
663
895
  };
664
896
  }
665
- let A = {
666
- bytes: s.byteLength,
897
+ let k = {
898
+ bytes: o.byteLength,
667
899
  count: 1
668
- }, j = yield* r.consumeUsage(g, A, e.quota);
900
+ }, j = yield* i.consumeUsage(g, k, e.quota);
669
901
  if (!j.allowed) {
670
- let t = e.quota ?? {}, n = t.maxCount !== void 0 && j.count + 1 > t.maxCount ? `tenant object count ${j.count} + 1 exceeds max ${t.maxCount}` : `tenant usage ${j.bytes} + ${s.byteLength} bytes exceeds max ${t.maxBytes}`;
671
- return yield* N.fail(new l({
902
+ let t = e.quota ?? {}, n = t.maxCount !== void 0 && j.count + 1 > t.maxCount ? `tenant object count ${j.count} + 1 exceeds max ${t.maxCount}` : `tenant usage ${j.bytes} + ${o.byteLength} bytes exceeds max ${t.maxBytes}`;
903
+ return yield* A.fail(new u({
672
904
  reason: "quota-exceeded",
673
905
  detail: n
674
906
  }));
675
907
  }
676
- return yield* ((e) => e.pipe(N.onExit((e) => P.isSuccess(e) ? N.void : r.delete(w.id).pipe(N.andThen(r.releaseUsage(g, A)), N.ignore))))(r.insert(w).pipe(N.andThen(t.put(x, s, {
677
- contentType: n.contentType,
908
+ return yield* ((e) => e.pipe(A.onExit((e) => oe.isSuccess(e) ? A.void : i.delete(D.id).pipe(A.andThen(i.releaseUsage(g, k)), A.ignore))))(i.insert(w(D)).pipe(A.andThen(x.put(C, o, {
909
+ contentType: t.contentType,
678
910
  public: _ === "public"
679
- }).pipe(N.retry(T))))), yield* k(w), {
680
- ...w,
911
+ }).pipe(A.retry(L))))), yield* O(D), {
912
+ ...D,
681
913
  outcome: "created"
682
914
  };
683
- }), U = (t) => N.suspend(() => {
684
- if (!e.transcode) return N.succeed([]);
915
+ }), X = (t) => A.suspend(() => {
916
+ if (!e.transcode) return A.succeed([]);
685
917
  let n;
686
918
  try {
687
919
  n = e.transcode(t);
688
920
  } catch {
689
- return N.succeed([]);
921
+ return A.succeed([]);
690
922
  }
691
- return N.isEffect(n) ? n.pipe(N.catch(() => N.succeed([]))) : N.tryPromise({
923
+ return A.isEffect(n) ? n.pipe(A.catch(() => A.succeed([]))) : A.tryPromise({
692
924
  try: () => Promise.resolve(n),
693
925
  catch: () => []
694
- }).pipe(N.catch(() => N.succeed([])));
695
- }), W = (n) => N.gen(function* () {
696
- if (!e.transcode || n.derivedFrom != null) return [];
697
- let r = yield* t.get(n.key).pipe(N.retry(T)), i = yield* U({
698
- bytes: r.bytes,
699
- contentType: n.contentType,
700
- ref: n
701
- }), a = [];
702
- for (let e of i) {
703
- let t = yield* H({
704
- bytes: e.bytes,
705
- contentType: e.contentType,
706
- tenantId: n.tenantId,
707
- ownerId: n.ownerId,
708
- visibility: n.visibility,
709
- derivedFrom: n.id,
710
- kind: e.kind,
711
- ...e.label === void 0 ? {} : { caption: e.label }
712
- });
713
- a.push(t);
926
+ }).pipe(A.catch(() => A.succeed([])));
927
+ }), Z = (t) => A.gen(function* () {
928
+ if (!e.transcode || t.derivedFrom != null) return [];
929
+ let n = yield* m(t), r = { ...t }, i = r.checksum !== "" && !/^[a-f0-9]{64}$/.test(r.checksum), a = () => n.head(r.key).pipe(A.map((e) => e === null ? null : { ...e })), o = i ? yield* a() : void 0, s = yield* n.get(r.key).pipe(A.retry(L)), c = i ? yield* a() : void 0, l = yield* X({
930
+ bytes: Uint8Array.from(s.bytes),
931
+ contentType: r.contentType,
932
+ ref: { ...r }
933
+ }), u = [];
934
+ for (let e of l) {
935
+ let n = {
936
+ ...e,
937
+ bytes: Uint8Array.from(e.bytes)
938
+ }, i = yield* A.try({
939
+ try: () => C(r, s.bytes, n, {
940
+ before: o,
941
+ after: c
942
+ }),
943
+ catch: (e) => e
944
+ }), a = yield* Y({
945
+ destination: t.bucket,
946
+ bytes: n.bytes,
947
+ contentType: n.contentType,
948
+ tenantId: r.tenantId,
949
+ ownerId: r.ownerId,
950
+ visibility: r.visibility,
951
+ derivedFrom: r.id,
952
+ kind: n.kind,
953
+ ...n.label === void 0 ? {} : { caption: n.label }
954
+ }, i);
955
+ u.push(a);
714
956
  }
715
- return a;
716
- }), G = (e, t, n) => N.gen(function* () {
957
+ return u;
958
+ }), Q = (e, t, n) => A.gen(function* () {
717
959
  if (e.derivedFrom != null) {
718
- let i = yield* r.getById(e.derivedFrom);
719
- return i === null ? !1 : yield* G(i, t, n);
960
+ let r = yield* i.getById(e.derivedFrom);
961
+ return r === null ? !1 : yield* Q(r, t, n);
720
962
  }
721
963
  if (e.visibility === "public") return !0;
722
- if (e.tenantId !== null && !k(e, t.tenantId ?? null)) return !1;
723
- if (yield* g(e, t, {
724
- ...d,
964
+ if (e.tenantId !== null && !W(e, t.tenantId ?? null)) return !1;
965
+ if (yield* v(e, t, {
966
+ ...j,
725
967
  ...n?.password === void 0 ? {} : { password: n.password }
726
968
  })) return !0;
727
- let a = yield* y(t, d), o = yield* i.listByRef(e.id);
728
- return _(o, t, a, n?.permission ?? "read", Date.now());
729
- }), K = (e) => {
969
+ let r = yield* b(t, j), o = yield* a.listByRef(e.id);
970
+ return y(o, t, r, n?.permission ?? "read", Date.now());
971
+ }), $ = (e, t) => {
730
972
  if (e.visibility === "public") {
731
973
  let n = t.publicUrl?.(e.key);
732
974
  if (n != null) return n;
733
975
  }
734
- return w(e.id);
976
+ return I(e.id);
735
977
  };
736
978
  return {
737
- uploadTicketScope: () => N.gen(function* () {
738
- let i = yield* r.catalogueId, a = yield* t.objectNamespace;
739
- return yield* N.try({
740
- try: () => n({
741
- catalogueId: i,
742
- objectNamespace: a,
743
- storageInstance: e.storageInstance ?? "@voltro/plugin-storage"
979
+ operationStatus: (n) => A.gen(function* () {
980
+ if (!n || typeof n.operationId != "string" || n.operationId.length === 0 || n.tenantId !== null && typeof n.tenantId != "string") return yield* A.fail(new l({
981
+ provider: "metadata",
982
+ message: "An exact operation ID and explicit tenant are required",
983
+ transient: !1,
984
+ status: 400
985
+ }));
986
+ let a = e.objects?.();
987
+ if (!a) return yield* A.fail(new l({
988
+ provider: "metadata",
989
+ message: "Durable storage operation metadata is not bound",
990
+ transient: !1,
991
+ status: 409
992
+ }));
993
+ let o = yield* h(n.tenantId, "_namespace", n.destination), s = yield* A.tryPromise({
994
+ try: () => a.operationStatus({
995
+ operationId: n.operationId,
996
+ tenantId: n.tenantId,
997
+ bucket: o.destination
744
998
  }),
745
- catch: (e) => h(e, "storage", !1)
999
+ catch: (e) => {
1000
+ let t = _(e, "metadata");
1001
+ return new l({
1002
+ provider: "metadata",
1003
+ message: "Storage operation state could not be confirmed; retain its identity for recovery",
1004
+ transient: t.transient,
1005
+ ...t.status === void 0 ? {} : { status: t.status }
1006
+ });
1007
+ }
1008
+ });
1009
+ if ((yield* o.provider.objectNamespace) !== s.scope.objectNamespace || s.scope.storageInstance !== (e.storageInstance ?? "@voltro/plugin-storage")) return yield* A.fail(new l({
1010
+ provider: "metadata",
1011
+ message: "Storage operation metadata belongs to another configured destination",
1012
+ transient: !1,
1013
+ status: 409
1014
+ }));
1015
+ let c = yield* i.catalogueId, u = yield* A.try({
1016
+ try: () => t([
1017
+ "durable-operation-observation",
1018
+ r({
1019
+ ...s.scope,
1020
+ catalogueId: c
1021
+ }),
1022
+ o.destination
1023
+ ]),
1024
+ catch: (e) => _(e, "metadata")
746
1025
  });
1026
+ return {
1027
+ ...s.observation,
1028
+ scope: u,
1029
+ destination: o.destination
1030
+ };
747
1031
  }),
748
- put: H,
749
- get: (e, n) => F(e, n?.tenantId).pipe(N.flatMap((e) => t.get(e.key).pipe(N.retry(T), N.map((t) => ({
1032
+ uploadTicketScope: (e) => h(e?.tenantId ?? null, "_namespace", e?.destination).pipe(A.flatMap((e) => T(e.provider))),
1033
+ put: (e) => Y(e),
1034
+ get: (e, t) => q(e, t?.tenantId).pipe(A.flatMap((e) => m(e).pipe(A.flatMap((t) => t.get(e.key).pipe(A.retry(L), A.map((t) => ({
750
1035
  bytes: t.bytes,
751
1036
  ref: e
752
- }))))),
753
- getStream: (e, n) => F(e, n?.tenantId).pipe(N.flatMap((e) => m(t, e.key).pipe(N.retry(T), N.map((t) => ({
1037
+ }))))))),
1038
+ getStream: (e, t) => q(e, t?.tenantId).pipe(A.flatMap((e) => m(e).pipe(A.flatMap((t) => g(t, e.key).pipe(A.retry(L), A.map((t) => ({
754
1039
  ref: e,
755
1040
  stream: t.stream
756
- }))))),
757
- getRange: (e, n, r) => F(e, r?.tenantId).pipe(N.flatMap((e) => {
758
- let r = e.size, i = Math.max(0, Math.trunc(n.start));
759
- if (r > 0 && i >= r) return N.fail(new c({
760
- provider: t.name,
1041
+ }))))))),
1042
+ getRange: (e, t, n) => q(e, n?.tenantId).pipe(A.flatMap((e) => m(e).pipe(A.flatMap((n) => {
1043
+ let r = e.size, i = Math.max(0, Math.trunc(t.start));
1044
+ if (r > 0 && i >= r) return A.fail(new l({
1045
+ provider: n.name,
761
1046
  message: `range start ${i} past end ${r}`,
762
1047
  transient: !1,
763
1048
  status: 416
764
1049
  }));
765
- let a = Math.min(Math.trunc(n.endInclusive), Math.max(0, r - 1));
766
- return a < i ? N.fail(new c({
767
- provider: t.name,
1050
+ let a = Math.min(Math.trunc(t.endInclusive), Math.max(0, r - 1));
1051
+ return a < i ? A.fail(new l({
1052
+ provider: n.name,
768
1053
  message: `empty range ${i}-${a}`,
769
1054
  transient: !1,
770
1055
  status: 416
771
- })) : (t.getRange ? t.getRange(e.key, i, a).pipe(N.retry(T)) : t.get(e.key).pipe(N.retry(T), N.map((e) => e.bytes.subarray(i, a + 1)))).pipe(N.map((t) => ({
1056
+ })) : (n.getRange ? n.getRange(e.key, i, a).pipe(A.retry(L)) : n.get(e.key).pipe(A.retry(L), A.map((e) => e.bytes.subarray(i, a + 1)))).pipe(A.map((t) => ({
772
1057
  bytes: t,
773
1058
  ref: e,
774
1059
  totalSize: r
775
1060
  })));
776
- })),
777
- getUrl: (e, n) => F(e, n?.tenantId).pipe(N.flatMap((e) => e.visibility === "public" ? N.succeed(K(e)) : t.presigns ? t.getUrl(e.key, n?.expiresInSec ?? p) : N.succeed(w(e.id)))),
778
- head: (e, t) => r.getById(e).pipe(N.flatMap((e) => e ? j(e, t?.tenantId).pipe(N.map((e) => e)) : N.succeed(null))),
779
- delete: (e, n) => M(e, n?.tenantId, n?.claim).pipe(N.flatMap((a) => a ? N.void : F(e, n?.tenantId).pipe(N.flatMap((n) => i.deleteByRef(e).pipe(N.flatMap(() => r.delete(e)), N.flatMap((e) => e ? r.countByKey(n.bucket, n.key).pipe(N.flatMap((e) => e === 0 ? t.delete(n.key).pipe(N.retry(T)) : N.void), N.flatMap(() => r.releaseUsage(n.tenantId, {
780
- bytes: n.size,
1061
+ })))),
1062
+ getUrl: (e, t) => q(e, t?.tenantId).pipe(A.flatMap((e) => m(e).pipe(A.flatMap((n) => e.visibility === "public" ? A.succeed($(e, n)) : n.presigns ? n.getUrl(e.key, t?.expiresInSec ?? F) : A.succeed(I(e.id)))))),
1063
+ head: (e, t) => i.getById(e).pipe(A.flatMap((e) => e ? G(e, t?.tenantId).pipe(A.map((e) => e)) : A.succeed(null))),
1064
+ delete: (e, t) => K(e, t?.tenantId, t?.claim).pipe(A.flatMap((n) => n ? A.void : q(e, t?.tenantId).pipe(A.flatMap((t) => m(t).pipe(A.flatMap((n) => a.deleteByRef(e).pipe(A.flatMap(() => i.delete(e)), A.flatMap((e) => e ? n.delete(t.key).pipe(A.retry(L), A.flatMap(() => i.releaseUsage(t.tenantId, {
1065
+ bytes: t.size,
781
1066
  count: 1
782
- }))) : N.void), N.asVoid))))),
783
- signedUrl: (n, r) => F(n, r?.tenantId).pipe(N.flatMap((n) => {
784
- if (n.visibility === "public") return N.succeed(K(n));
785
- let i = r?.expiresInSec ?? p;
786
- return t.presigns ? t.getUrl(n.key, i) : N.sync(() => `${w(n.id)}?t=${b(n.id, i, void 0, e.httpPath ?? "/_voltro/storage")}`);
787
- })),
788
- setVisibility: (e, n, i) => N.gen(function* () {
789
- let a = yield* F(e, i?.tenantId);
790
- if (a.visibility === n) return a;
791
- let o = r.setVisibility;
792
- if (o === void 0) return yield* N.fail(new l({
793
- reason: "visibility-unsupported",
794
- detail: "the ref store cannot change a stored object's visibility"
795
- }));
796
- if (n === "private" && t.publicUrl?.(a.key) != null) return yield* N.fail(new l({
797
- reason: "visibility-unsupported",
798
- detail: `objects are served from a public base (${t.name} cdnBaseUrl); a public object cannot be made private there`
799
- }));
800
- if (n === "private" && t.setObjectPublic !== void 0 && r.countPublicByKey === void 0) return yield* N.fail(new l({
1067
+ }))) : A.void), A.asVoid))))))),
1068
+ signedUrl: (t, n) => q(t, n?.tenantId).pipe(A.flatMap((t) => m(t).pipe(A.flatMap((r) => {
1069
+ if (t.visibility === "public") return A.succeed($(t, r));
1070
+ let i = n?.expiresInSec ?? F;
1071
+ return r.presigns ? r.getUrl(t.key, i) : A.sync(() => `${I(t.id)}?t=${S(t.id, i, void 0, e.httpPath ?? "/_voltro/storage")}`);
1072
+ })))),
1073
+ setVisibility: (e, t, n) => A.gen(function* () {
1074
+ let r = yield* q(e, n?.tenantId);
1075
+ if (r.visibility === t) return r;
1076
+ let a = i.setVisibility;
1077
+ if (a === void 0) return yield* A.fail(new u({
801
1078
  reason: "visibility-unsupported",
802
- detail: "the ref store cannot tell whether other refs keep these bytes public"
1079
+ detail: "the ref store cannot change a stored object visibility"
803
1080
  }));
804
- let s = yield* o(a.id, n);
805
- if (s === null) return yield* N.fail(new c({
806
- provider: t.name,
1081
+ let o = r.derivedFrom == null ? yield* i.listByDerivedFrom(r.id) : [], s = [];
1082
+ for (let e of [r, ...o]) {
1083
+ let n = yield* m(e);
1084
+ if (t === "private" && n.publicUrl?.(e.key) != null) return yield* A.fail(new u({
1085
+ reason: "visibility-unsupported",
1086
+ detail: `objects are served from a public base (${n.name} cdnBaseUrl); a public object cannot be made private there`
1087
+ }));
1088
+ if (t === "private" && n.setObjectPublic !== void 0 && i.countPublicByAddress === void 0) return yield* A.fail(new u({
1089
+ reason: "visibility-unsupported",
1090
+ detail: "the ref store cannot tell whether other refs keep these bytes public"
1091
+ }));
1092
+ s.push({
1093
+ ref: e,
1094
+ provider: n
1095
+ });
1096
+ }
1097
+ let c = yield* a(r.id, t);
1098
+ if (c === null) return yield* A.fail(new l({
1099
+ provider: "storage",
807
1100
  message: `no ref '${e}'`,
808
1101
  transient: !1,
809
1102
  status: 404
810
1103
  }));
811
- let u = a.derivedFrom == null ? yield* r.listByDerivedFrom(a.id) : [];
812
- for (let e of u) e.visibility !== n && (yield* o(e.id, n));
813
- if (t.setObjectPublic !== void 0) {
814
- let e = r.countPublicByKey;
815
- for (let r of [a, ...u]) {
816
- let i = n === "public" || e !== void 0 && (yield* e(r.bucket, r.key)) > 0;
817
- yield* t.setObjectPublic(r.key, i);
818
- }
1104
+ for (let e of o) if (e.visibility !== t && (yield* a(e.id, t)) === null) return yield* A.fail(new l({
1105
+ provider: "storage",
1106
+ message: `no ref '${e.id}'`,
1107
+ transient: !1,
1108
+ status: 404
1109
+ }));
1110
+ for (let e of s) if (e.provider.setObjectPublic !== void 0) {
1111
+ let n = i.countPublicByAddress, r = t === "public" || n !== void 0 && (yield* n(p(e.ref))) > 0;
1112
+ yield* e.provider.setObjectPublic(e.ref.key, r);
819
1113
  }
820
- return s;
1114
+ return c;
821
1115
  }),
822
- mintUrl: (n, i, a) => N.gen(function* () {
823
- let o = yield* r.getById(n).pipe(N.flatMap((e) => e ? N.succeed(e) : N.fail(new c({
824
- provider: t.name,
825
- message: `no ref '${n}'`,
1116
+ mintUrl: (t, n, r) => A.gen(function* () {
1117
+ let a = yield* i.getById(t).pipe(A.flatMap((e) => e ? A.succeed(e) : A.fail(new l({
1118
+ provider: "storage",
1119
+ message: `no ref '${t}'`,
826
1120
  transient: !1,
827
1121
  status: 404
828
1122
  }))));
829
- if (o.visibility === "public" && o.derivedFrom == null) return K(o);
830
- if (!(yield* G(o, i, a?.password === void 0 ? {} : { password: a.password }))) return yield* N.fail(new s({
831
- refId: n,
1123
+ if (a.visibility === "public" && a.derivedFrom == null) return $(a, yield* m(a));
1124
+ if (!(yield* Q(a, n, r?.password === void 0 ? {} : { password: r.password }))) return yield* A.fail(new c({
1125
+ refId: t,
832
1126
  reason: "access denied"
833
1127
  }));
834
- let l = a?.expiresInSec ?? p;
835
- if (t.presigns) return yield* t.getUrl(o.key, l);
836
- let u = yield* N.sync(() => b(o.id, l, void 0, e.httpPath ?? "/_voltro/storage"));
837
- return `${w(o.id)}?t=${u}`;
1128
+ let o = yield* m(a), s = r?.expiresInSec ?? F;
1129
+ if (o.presigns) return yield* o.getUrl(a.key, s);
1130
+ let u = yield* A.sync(() => S(a.id, s, void 0, e.httpPath ?? "/_voltro/storage"));
1131
+ return `${I(a.id)}?t=${u}`;
838
1132
  }),
839
- mintUploadUrl: (e) => N.gen(function* () {
840
- if (!t.getUploadUrl) return yield* N.fail(new c({
1133
+ mintUploadUrl: (e) => A.gen(function* () {
1134
+ let { provider: t } = yield* h(e.tenantId ?? null, z(e.tenantId ?? null, "", e.key));
1135
+ if (!t.getUploadUrl) return yield* A.fail(new l({
841
1136
  provider: t.name,
842
1137
  message: `provider '${t.name}' does not support presigned uploads`,
843
1138
  transient: !1,
844
1139
  status: 400
845
1140
  }));
846
- let n = E(e.tenantId ?? null, "", yield* D(e.key));
847
- return yield* t.getUploadUrl(n, e.expiresInSec ?? p, e.contentType);
1141
+ let n = z(e.tenantId ?? null, "", yield* V(e.key));
1142
+ return yield* t.getUploadUrl(n, e.expiresInSec ?? F, e.contentType);
848
1143
  }),
849
- checkAccess: G,
850
- grant: (e) => N.gen(function* () {
851
- yield* F(e.refId);
1144
+ checkAccess: Q,
1145
+ grant: (e) => A.gen(function* () {
1146
+ yield* q(e.refId);
852
1147
  let t = {
853
- id: ne(),
1148
+ id: pe(),
854
1149
  refId: e.refId,
855
1150
  principalType: e.principalType,
856
1151
  principalId: e.principalId,
@@ -859,10 +1154,10 @@ var R = async (e) => {
859
1154
  expiresAt: e.expiresAt ?? null,
860
1155
  createdBy: e.createdBy ?? null
861
1156
  };
862
- return yield* i.insert(t), t;
1157
+ return yield* a.insert(t), t;
863
1158
  }),
864
- listRefs: (e) => N.suspend(() => {
865
- let t = Math.min(500, Math.max(1, Math.trunc(e?.limit ?? 50))), n = Math.max(0, Math.trunc(e?.offset ?? 0)), i = {
1159
+ listRefs: (e) => A.suspend(() => {
1160
+ let t = Math.min(500, Math.max(1, Math.trunc(e?.limit ?? 50))), n = Math.max(0, Math.trunc(e?.offset ?? 0)), r = {
866
1161
  tenantId: e && "tenantId" in e ? e.tenantId : void 0,
867
1162
  folder: e?.folder,
868
1163
  tags: e?.tags ?? [],
@@ -871,7 +1166,7 @@ var R = async (e) => {
871
1166
  limit: t + 1,
872
1167
  offset: n
873
1168
  };
874
- return r.listRefs(i).pipe(N.map((e) => {
1169
+ return i.listRefs(r).pipe(A.map((e) => {
875
1170
  let r = e.length > t;
876
1171
  return {
877
1172
  refs: r ? e.slice(0, t) : e,
@@ -879,30 +1174,30 @@ var R = async (e) => {
879
1174
  };
880
1175
  }));
881
1176
  }),
882
- revoke: (e) => i.delete(e),
883
- getGrant: (e) => i.getById(e),
884
- listGrants: (e) => i.listByRef(e),
885
- usage: (e) => r.usageByTenant(e),
886
- sweepOrphans: (e) => N.gen(function* () {
887
- let n = e?.limit ?? 1e3, i = e?.dryRun ?? !1, a = yield* r.listRecent(n), o = [];
888
- for (let e of a) if ((yield* t.head(e.key)) === null) {
889
- if (!i) {
890
- if (!(yield* r.delete(e.id))) continue;
891
- yield* r.releaseUsage(e.tenantId, {
1177
+ revoke: (e) => a.delete(e),
1178
+ getGrant: (e) => a.getById(e),
1179
+ listGrants: (e) => a.listByRef(e),
1180
+ usage: (e) => i.usageByTenant(e),
1181
+ sweepOrphans: (e) => A.gen(function* () {
1182
+ let t = e?.limit ?? 1e3, n = e?.dryRun ?? !1, r = yield* i.listRecent(t), a = [];
1183
+ for (let e of r) if ((yield* (yield* m(e)).head(e.key)) === null) {
1184
+ if (!n) {
1185
+ if (!(yield* i.delete(e.id))) continue;
1186
+ yield* i.releaseUsage(e.tenantId, {
892
1187
  bytes: e.size,
893
1188
  count: 1
894
1189
  });
895
1190
  }
896
- o.push(e.id);
1191
+ a.push(e.id);
897
1192
  }
898
1193
  return {
899
- scanned: a.length,
900
- removed: o
1194
+ scanned: r.length,
1195
+ removed: a
901
1196
  };
902
1197
  }),
903
- ingestUrl: (t, n) => S(N.gen(function* () {
904
- let r = yield* x(t, e.limits?.maxBytes, e.ingest);
905
- return yield* H({
1198
+ ingestUrl: (t, n) => D(A.gen(function* () {
1199
+ let r = yield* E(t, e.limits?.maxBytes, e.ingest);
1200
+ return yield* Y({
906
1201
  bytes: r.bytes,
907
1202
  contentType: n?.contentType ?? r.contentType,
908
1203
  ...n?.tenantId === void 0 ? {} : { tenantId: n.tenantId },
@@ -917,24 +1212,33 @@ var R = async (e) => {
917
1212
  ...n?.caption === void 0 ? {} : { caption: n.caption }
918
1213
  });
919
1214
  }), n?.abortSignal),
920
- mintPresignedUpload: (e) => N.gen(function* () {
921
- if (!t.getUploadUrl) return yield* N.fail(new c({
922
- provider: t.name,
923
- message: `provider '${t.name}' does not support presigned uploads`,
1215
+ mintPresignedUpload: (e) => A.gen(function* () {
1216
+ let t = yield* h(e.tenantId ?? null, H(e.tenantId ?? null, "selection", 0)), { provider: n } = t;
1217
+ if (!n.getUploadUrl) return yield* A.fail(new l({
1218
+ provider: n.name,
1219
+ message: `provider '${n.name}' does not support presigned uploads`,
924
1220
  transient: !1,
925
1221
  status: 400
926
1222
  }));
927
- let n = e.expiresInSec ?? p, r = `_incoming/${A({
1223
+ let r = e.expiresInSec ?? F, i = `_incoming/${B({
928
1224
  kind: "typeid",
929
1225
  prefix: "up"
930
- }, a)}`, i = E(e.tenantId ?? null, "", r);
1226
+ }, o)}`, a = z(e.tenantId ?? null, "", i), s = yield* T(n), c = yield* n.getUploadUrl(a, r, e.contentType);
931
1227
  return {
932
- uploadUrl: yield* t.getUploadUrl(i, n, e.contentType),
933
- key: i,
934
- expiresInSec: n
1228
+ writeScope: s,
1229
+ destination: t.destination,
1230
+ tenantId: e.tenantId ?? null,
1231
+ uploadUrl: c,
1232
+ key: a,
1233
+ expiresInSec: r
935
1234
  };
936
1235
  }),
937
- finalizeUpload: (e) => t.get(e.key).pipe(N.retry(T), N.flatMap((t) => H({
1236
+ finalizeUpload: (e) => m({
1237
+ bucket: e.destination,
1238
+ tenantId: e.tenantId ?? null,
1239
+ key: e.key
1240
+ }).pipe(A.flatMap((t) => t.get(e.key).pipe(A.retry(L), A.flatMap((t) => Y({
1241
+ destination: e.destination,
938
1242
  bytes: t.bytes,
939
1243
  contentType: e.contentType,
940
1244
  ...e.tenantId === void 0 ? {} : { tenantId: e.tenantId },
@@ -945,12 +1249,20 @@ var R = async (e) => {
945
1249
  ...e.tags === void 0 ? {} : { tags: e.tags },
946
1250
  ...e.alt === void 0 ? {} : { alt: e.alt },
947
1251
  ...e.caption === void 0 ? {} : { caption: e.caption }
948
- })), N.ensuring(t.delete(e.key).pipe(N.retry(T), N.ignore))),
949
- putResumableChunk: (e) => t.put(O(e.tenantId ?? null, e.uploadId, e.index), e.bytes, { contentType: "application/octet-stream" }).pipe(N.retry(T), N.asVoid),
950
- resumableStatus: (e) => N.gen(function* () {
951
- let n = 0, r = 0;
1252
+ })), A.ensuring(t.delete(e.key).pipe(A.retry(L), A.ignore))))),
1253
+ putResumableChunk: (e) => m({
1254
+ bucket: e.destination,
1255
+ tenantId: e.tenantId ?? null,
1256
+ key: H(e.tenantId ?? null, e.uploadId, e.index)
1257
+ }).pipe(A.flatMap((t) => t.put(H(e.tenantId ?? null, e.uploadId, e.index), e.bytes, { contentType: "application/octet-stream" }).pipe(A.retry(L), A.asVoid))),
1258
+ resumableStatus: (e) => A.gen(function* () {
1259
+ let t = yield* m({
1260
+ bucket: e.destination,
1261
+ tenantId: e.tenantId ?? null,
1262
+ key: H(e.tenantId ?? null, e.uploadId, 0)
1263
+ }), n = 0, r = 0;
952
1264
  for (; n < 1e6;) {
953
- let i = yield* t.head(O(e.tenantId ?? null, e.uploadId, n)).pipe(N.retry(T), N.catch(() => N.succeed(null)));
1265
+ let i = yield* t.head(H(e.tenantId ?? null, e.uploadId, n)).pipe(A.retry(L), A.catchIf((e) => e.status === 404, () => A.succeed(null)));
954
1266
  if (i === null) break;
955
1267
  r += i.size, n += 1;
956
1268
  }
@@ -959,42 +1271,46 @@ var R = async (e) => {
959
1271
  nextIndex: n
960
1272
  };
961
1273
  }),
962
- finalizeResumable: (n) => N.gen(function* () {
963
- let r = e.limits?.maxBytes ?? 2 ** 53 - 1;
964
- if (!Number.isSafeInteger(r) || r < 0 || !Number.isSafeInteger(n.count) || n.count < 0) return yield* N.fail(new c({
965
- provider: t.name,
1274
+ finalizeResumable: (t) => A.gen(function* () {
1275
+ let n = yield* m({
1276
+ bucket: t.destination,
1277
+ tenantId: t.tenantId ?? null,
1278
+ key: H(t.tenantId ?? null, t.uploadId, 0)
1279
+ }), r = e.limits?.maxBytes ?? 2 ** 53 - 1;
1280
+ if (!Number.isSafeInteger(r) || r < 0 || !Number.isSafeInteger(t.count) || t.count < 0) return yield* A.fail(new l({
1281
+ provider: n.name,
966
1282
  message: "resumable count and limits.maxBytes must be non-negative safe integers",
967
1283
  transient: !1,
968
1284
  status: 400
969
1285
  }));
970
- let i = (e) => O(n.tenantId ?? null, n.uploadId, e), a = N.gen(function* () {
971
- for (let e = 0; e < n.count; e++) yield* t.delete(i(e)).pipe(N.retry(T), N.ignore);
972
- }), o = () => new l({
1286
+ let i = (e) => H(t.tenantId ?? null, t.uploadId, e), a = A.gen(function* () {
1287
+ for (let e = 0; e < t.count; e++) yield* n.delete(i(e)).pipe(A.retry(L), A.ignore);
1288
+ }), o = () => new u({
973
1289
  reason: "too-large",
974
1290
  detail: `resumable upload exceeds ${r}-byte limit`
975
- }), s = yield* N.gen(function* () {
1291
+ }), s = yield* A.gen(function* () {
976
1292
  let e = 0;
977
- for (let a = 0; a < n.count; a++) {
978
- let n = yield* t.head(i(a)).pipe(N.retry(T));
979
- if (n === null) return yield* N.fail(new c({
980
- provider: t.name,
1293
+ for (let a = 0; a < t.count; a++) {
1294
+ let t = yield* n.head(i(a)).pipe(A.retry(L));
1295
+ if (t === null) return yield* A.fail(new l({
1296
+ provider: n.name,
981
1297
  message: `no resumable chunk at '${i(a)}'`,
982
1298
  transient: !1,
983
1299
  status: 404
984
1300
  }));
985
- if (!Number.isSafeInteger(n.size) || n.size < 0) return yield* N.fail(new c({
986
- provider: t.name,
1301
+ if (!Number.isSafeInteger(t.size) || t.size < 0) return yield* A.fail(new l({
1302
+ provider: n.name,
987
1303
  message: "resumable chunk metadata has an invalid size",
988
1304
  transient: !1
989
1305
  }));
990
- if (n.size > r - e) return yield* N.fail(o());
991
- e += n.size;
1306
+ if (t.size > r - e) return yield* A.fail(o());
1307
+ e += t.size;
992
1308
  }
993
1309
  let a = /* @__PURE__ */ new Uint8Array(), s = 0;
994
- for (let e = 0; e < n.count; e++) {
995
- let n = yield* m(t, i(e)).pipe(N.retry(T));
996
- yield* L.runForEach(n.stream, (e) => N.gen(function* () {
997
- if (e.byteLength > r - s) return yield* N.fail(o());
1310
+ for (let e = 0; e < t.count; e++) {
1311
+ let t = yield* g(n, i(e)).pipe(A.retry(L));
1312
+ yield* se.runForEach(t.stream, (e) => A.gen(function* () {
1313
+ if (e.byteLength > r - s) return yield* A.fail(o());
998
1314
  let t = s + e.byteLength;
999
1315
  if (t > a.byteLength) {
1000
1316
  let e = new Uint8Array(Math.min(r, Math.max(65536, a.byteLength * 2, t)));
@@ -1004,81 +1320,98 @@ var R = async (e) => {
1004
1320
  }));
1005
1321
  }
1006
1322
  return a.subarray(0, s);
1007
- }).pipe(N.tapError((e) => e._tag === "StorageRejected" ? a : N.void));
1008
- return yield* H({
1323
+ }).pipe(A.tapError((e) => e._tag === "StorageRejected" ? a : A.void));
1324
+ return yield* Y({
1325
+ destination: t.destination,
1009
1326
  bytes: s,
1010
- contentType: n.contentType,
1011
- ...n.tenantId === void 0 ? {} : { tenantId: n.tenantId },
1012
- ...n.ownerId === void 0 ? {} : { ownerId: n.ownerId },
1013
- ...n.visibility === void 0 ? {} : { visibility: n.visibility },
1014
- ...n.access === void 0 ? {} : { access: n.access },
1015
- ...n.folder === void 0 ? {} : { folder: n.folder },
1016
- ...n.tags === void 0 ? {} : { tags: n.tags },
1017
- ...n.alt === void 0 ? {} : { alt: n.alt },
1018
- ...n.caption === void 0 ? {} : { caption: n.caption }
1019
- }).pipe(N.ensuring(a));
1327
+ contentType: t.contentType,
1328
+ ...t.tenantId === void 0 ? {} : { tenantId: t.tenantId },
1329
+ ...t.ownerId === void 0 ? {} : { ownerId: t.ownerId },
1330
+ ...t.visibility === void 0 ? {} : { visibility: t.visibility },
1331
+ ...t.access === void 0 ? {} : { access: t.access },
1332
+ ...t.folder === void 0 ? {} : { folder: t.folder },
1333
+ ...t.tags === void 0 ? {} : { tags: t.tags },
1334
+ ...t.alt === void 0 ? {} : { alt: t.alt },
1335
+ ...t.caption === void 0 ? {} : { caption: t.caption }
1336
+ }).pipe(A.ensuring(a));
1020
1337
  }),
1021
- transcode: (e, t) => F(e, t?.tenantId).pipe(N.flatMap((e) => W(e))),
1022
- renditions: (e, t) => F(e, t?.tenantId).pipe(N.flatMap(() => r.listByDerivedFrom(e))),
1023
- beginMultipartUpload: (e) => N.gen(function* () {
1024
- if (!t.createMultipartUpload || !t.presignUploadPart || !t.completeMultipartUpload) return yield* N.fail(new c({
1025
- provider: t.name,
1026
- message: `provider '${t.name}' does not support multipart uploads`,
1338
+ transcode: (e, t) => q(e, t?.tenantId).pipe(A.flatMap((e) => Z(e))),
1339
+ renditions: (e, t) => q(e, t?.tenantId).pipe(A.flatMap(() => i.listByDerivedFrom(e))),
1340
+ beginMultipartUpload: (e) => A.gen(function* () {
1341
+ let t = yield* h(e.tenantId ?? null, H(e.tenantId ?? null, "selection", 0)), { provider: n } = t;
1342
+ if (!n.createMultipartUpload || !n.presignUploadPart || !n.completeMultipartUpload) return yield* A.fail(new l({
1343
+ provider: n.name,
1344
+ message: `provider '${n.name}' does not support multipart uploads`,
1027
1345
  transient: !1,
1028
1346
  status: 400
1029
1347
  }));
1030
- let n = E(e.tenantId ?? null, "", `multipart/${A({
1348
+ let r = z(e.tenantId ?? null, "", `multipart/${B({
1031
1349
  kind: "typeid",
1032
1350
  prefix: "mp"
1033
- }, a)}`);
1351
+ }, o)}`), i = yield* T(n), a = yield* n.createMultipartUpload(r, e.contentType, { public: (e.visibility ?? "private") === "public" });
1034
1352
  return {
1035
- uploadId: (yield* t.createMultipartUpload(n, e.contentType, { public: (e.visibility ?? "private") === "public" })).uploadId,
1036
- key: n
1353
+ writeScope: i,
1354
+ destination: t.destination,
1355
+ tenantId: e.tenantId ?? null,
1356
+ uploadId: a.uploadId,
1357
+ key: r
1037
1358
  };
1038
1359
  }),
1039
- signMultipartPart: (e) => N.suspend(() => t.presignUploadPart ? t.presignUploadPart(e.key, e.uploadId, e.partNumber, e.expiresInSec ?? p) : N.fail(new c({
1360
+ signMultipartPart: (e) => m({
1361
+ bucket: e.destination,
1362
+ tenantId: e.tenantId ?? null,
1363
+ key: e.key
1364
+ }).pipe(A.flatMap((t) => A.suspend(() => t.presignUploadPart ? t.presignUploadPart(e.key, e.uploadId, e.partNumber, e.expiresInSec ?? F) : A.fail(new l({
1040
1365
  provider: t.name,
1041
1366
  message: "provider does not support multipart uploads",
1042
1367
  transient: !1,
1043
1368
  status: 400
1044
- }))),
1045
- completeMultipart: (n) => N.gen(function* () {
1046
- if (!t.completeMultipartUpload) return yield* N.fail(new c({
1047
- provider: t.name,
1369
+ }))))),
1370
+ completeMultipart: (t) => A.gen(function* () {
1371
+ let n = yield* m({
1372
+ bucket: t.destination,
1373
+ tenantId: t.tenantId ?? null,
1374
+ key: t.key
1375
+ }), r = t.destination;
1376
+ if (!n.completeMultipartUpload) return yield* A.fail(new l({
1377
+ provider: n.name,
1048
1378
  message: "provider does not support multipart uploads",
1049
1379
  transient: !1,
1050
1380
  status: 400
1051
1381
  }));
1052
- let i = yield* t.completeMultipartUpload(n.key, n.uploadId, n.parts), a = yield* t.head(n.key).pipe(N.retry(T)), s = a?.size ?? 0, u = (i.etag ?? a?.etag ?? "").replace(/"/g, ""), d = n.tenantId ?? null, f = {
1382
+ let a = yield* n.completeMultipartUpload(t.key, t.uploadId, t.parts), o = yield* n.head(t.key).pipe(A.retry(L)), s = o?.size ?? 0, c = (a.etag ?? o?.etag ?? "").replace(/"/g, ""), d = t.tenantId ?? null, f = {
1053
1383
  bytes: s,
1054
1384
  count: 1
1055
1385
  };
1056
- if (!(yield* r.consumeUsage(d, f, e.quota)).allowed) return yield* t.delete(n.key).pipe(N.retry(T), N.ignore), yield* N.fail(new l({
1386
+ if (!(yield* i.consumeUsage(d, f, e.quota)).allowed) return yield* n.delete(t.key).pipe(A.retry(L), A.ignore), yield* A.fail(new u({
1057
1387
  reason: "quota-exceeded",
1058
1388
  detail: `multipart upload of ${s} bytes exceeds tenant quota`
1059
1389
  }));
1060
1390
  let p = {
1061
- id: Q(),
1062
- tenantId: n.tenantId ?? null,
1063
- ownerId: n.ownerId ?? null,
1064
- bucket: o,
1065
- key: n.key,
1066
- contentType: n.contentType,
1391
+ id: fe(),
1392
+ tenantId: t.tenantId ?? null,
1393
+ ownerId: t.ownerId ?? null,
1394
+ bucket: r,
1395
+ key: t.key,
1396
+ contentType: t.contentType,
1067
1397
  size: s,
1068
- checksum: u,
1069
- visibility: n.visibility ?? "private",
1070
- accessPolicy: n.access ?? null,
1398
+ checksum: c,
1399
+ visibility: t.visibility ?? "private",
1400
+ accessPolicy: t.access ?? null,
1071
1401
  passwordHash: null,
1072
1402
  createdAt: /* @__PURE__ */ new Date(),
1073
- ...n.folder === void 0 ? {} : { folder: n.folder },
1074
- ...n.tags === void 0 ? {} : { tags: n.tags },
1075
- ...n.alt === void 0 ? {} : { alt: n.alt },
1076
- ...n.caption === void 0 ? {} : { caption: n.caption }
1403
+ ...t.folder === void 0 ? {} : { folder: t.folder },
1404
+ ...t.tags === void 0 ? {} : { tags: t.tags },
1405
+ ...t.alt === void 0 ? {} : { alt: t.alt },
1406
+ ...t.caption === void 0 ? {} : { caption: t.caption }
1077
1407
  };
1078
- return yield* r.insert(p).pipe(N.onExit((e) => P.isSuccess(e) ? N.void : r.releaseUsage(d, f).pipe(N.ignore))), p;
1408
+ return yield* i.insert(p).pipe(A.onExit((e) => oe.isSuccess(e) ? A.void : i.releaseUsage(d, f).pipe(A.ignore))), p;
1079
1409
  }),
1080
- abortMultipart: (e) => N.suspend(() => t.abortMultipartUpload ? t.abortMultipartUpload(e.key, e.uploadId).pipe(N.retry(T)) : N.void)
1410
+ abortMultipart: (e) => m({
1411
+ bucket: e.destination,
1412
+ tenantId: e.tenantId ?? null,
1413
+ key: e.key
1414
+ }).pipe(A.flatMap((t) => A.suspend(() => t.abortMultipartUpload ? t.abortMultipartUpload(e.key, e.uploadId).pipe(A.retry(L)) : A.void)))
1081
1415
  };
1082
- }, re = (e) => F.succeed(d, $(e));
1083
- //#endregion
1084
- export { $ as buildStorageService, Z as dataStoreGrantStore, J as dataStoreRefStore, Y as memoryGrantStore, U as memoryRefStore, V as refMatchesListQuery, re as storageServiceLayer, G as toRef };
1416
+ }, he = (e) => j.succeed(f, me(e));
1417
+ export { me as buildStorageService, de as dataStoreGrantStore, Z as dataStoreRefStore, Q as memoryGrantStore, q as memoryRefStore, G as refMatchesListQuery, he as storageServiceLayer, J as toRef };