@voltro/plugin-storage 0.86.0 → 0.87.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.
package/dist/providers.js CHANGED
@@ -1,69 +1,95 @@
1
1
  import { canonicalStorageRoot as e, filesystemObjectNamespace as t, storageEndpointIdentity as n, storageObjectNamespace as r } from "./storageNamespaces.js";
2
- import { STORAGE_BLOBS_TABLE as i } from "./table.js";
3
- import { StorageError as a } from "./types.js";
4
- import { storageInteger as o } from "./storageNumbers.js";
5
- import { concatBytes as s, fromNodeReadable as c, toNodeReadable as l } from "./streamBridge.js";
6
- import { storageEnv as u } from "./env.js";
7
- import { storageMetadataError as d } from "./metadataError.js";
8
- import { randomUUID as f } from "node:crypto";
9
- import { authoredError as p, generateId as m } from "@voltro/database";
10
- import { Effect as h } from "effect";
2
+ import { storageReferenceCatalogueId as i } from "./referenceCatalogue.js";
3
+ import { STORAGE_BLOBS_TABLE as a } from "./table.js";
4
+ import { StorageError as o } from "./types.js";
5
+ import { storageInteger as s } from "./storageNumbers.js";
6
+ import { concatBytes as c, fromNodeReadable as l, toNodeReadable as u } from "./streamBridge.js";
7
+ import { storageEnv as d } from "./env.js";
8
+ import { storageMetadataError as f } from "./metadataError.js";
9
+ import { storageRequest as p } from "./storageRequest.js";
10
+ import { Effect as m } from "effect";
11
+ import { randomUUID as h } from "node:crypto";
12
+ import { authoredError as g, generateId as _ } from "@voltro/database";
11
13
  //#region src/providers.ts
12
- var g = (e, t, n, r) => new a({
14
+ var v = (e, t, n, r) => new o({
13
15
  provider: e,
14
16
  message: t,
15
17
  transient: n,
16
18
  ...r === void 0 ? {} : { status: r }
17
- }), _ = (e) => e === void 0 || e === 408 || e === 429 || e >= 500, v = f(), y = /* @__PURE__ */ new Map(), b = (e, t) => y.get(e)?.get(t)?.bytes, x = (e) => {
18
- y.get(e)?.clear();
19
- }, S = (e = "default") => {
20
- let t = y.get(e) ?? /* @__PURE__ */ new Map();
21
- return y.set(e, t), {
19
+ }), y = (e) => e === void 0 || e === 408 || e === 429 || e >= 500, b = h(), x = /* @__PURE__ */ new WeakMap(), S = /* @__PURE__ */ new Map(), C = (e, t) => S.get(e)?.get(t)?.bytes, w = (e) => {
20
+ S.get(e)?.clear();
21
+ }, T = (e = "default") => {
22
+ let t = S.get(e) ?? /* @__PURE__ */ new Map();
23
+ return S.set(e, t), E(t);
24
+ }, E = (e) => {
25
+ let t = x.get(e);
26
+ return t === void 0 && (t = r([
27
+ "memory",
28
+ b,
29
+ h()
30
+ ]), x.set(e, t)), {
31
+ objectLocation: (t) => m.succeed({
32
+ namespace: e,
33
+ key: t
34
+ }),
22
35
  name: "memory",
23
- objectNamespace: h.succeed(r([
24
- "memory",
25
- v,
26
- e
27
- ])),
36
+ objectNamespace: m.succeed(t),
28
37
  presigns: !1,
29
- put: (e, n, r) => h.sync(() => (t.set(e, {
38
+ put: (t, n, r) => m.sync(() => (e.set(t, {
30
39
  bytes: n,
31
40
  contentType: r.contentType
32
41
  }), { size: n.byteLength })),
33
- get: (e) => h.suspend(() => {
34
- let n = t.get(e);
35
- return n ? h.succeed({
42
+ get: (t) => m.suspend(() => {
43
+ let n = e.get(t);
44
+ return n ? m.succeed({
36
45
  bytes: n.bytes,
37
46
  contentType: n.contentType
38
- }) : h.fail(g("memory", `no object at key '${e}'`, !1, 404));
47
+ }) : m.fail(v("memory", `no object at key '${t}'`, !1, 404));
39
48
  }),
40
- delete: (e) => h.sync(() => {
41
- t.delete(e);
49
+ delete: (t) => m.sync(() => {
50
+ e.delete(t);
42
51
  }),
43
52
  publicUrl: () => null,
44
- getUrl: () => h.succeed(""),
45
- head: (e) => h.sync(() => {
46
- let n = t.get(e);
47
- return n ? { size: n.bytes.byteLength } : null;
53
+ getUrl: () => m.succeed(""),
54
+ head: (t) => m.sync(() => {
55
+ let n = e.get(t);
56
+ return n ? {
57
+ size: n.bytes.byteLength,
58
+ contentType: n.contentType
59
+ } : null;
48
60
  })
49
61
  };
50
- }, C = (n) => {
62
+ }, D = (n) => {
51
63
  let r = e(n.root), i = async (e) => {
52
64
  if (e.split("/")[0]?.startsWith(".voltro-object-namespace")) throw Error("Storage namespace metadata is not an object key");
53
65
  let { join: t, resolve: n, sep: i } = await import("node:path"), a = n(r), o = n(a, e);
54
66
  if (o.startsWith(n(a, ".voltro-object-namespace"))) throw Error("Storage namespace metadata is not an object key");
55
67
  if (o !== a && !o.startsWith(a + i)) throw Error(`key '${e}' escapes the storage root`);
56
68
  return t(o);
57
- }, a = (e, t, n = !1) => h.tryPromise({
69
+ }, a = (e, t, n = !1) => m.tryPromise({
58
70
  try: e,
59
- catch: (e) => e?.code === "ENOENT" ? g("filesystem", `no object at key '${t}'`, !1, 404) : g("filesystem", String(e?.message ?? e), n)
71
+ catch: (e) => e?.code === "ENOENT" ? v("filesystem", `no object at key '${t}'`, !1, 404) : v("filesystem", String(e?.message ?? e), n)
60
72
  });
61
73
  return {
62
74
  root: r,
75
+ objectLocation: (e) => a(async () => {
76
+ let t = await import("node:fs/promises"), { dirname: n, basename: r, join: a } = await import("node:path"), o = await i(e), s = [];
77
+ for (;;) try {
78
+ return {
79
+ namespace: "filesystem",
80
+ key: a(await t.realpath(o), ...s)
81
+ };
82
+ } catch (e) {
83
+ if (e.code !== "ENOENT") throw e;
84
+ let t = n(o);
85
+ if (t === o) throw e;
86
+ s.unshift(r(o)), o = t;
87
+ }
88
+ }, e),
63
89
  name: "filesystem",
64
- objectNamespace: h.try({
90
+ objectNamespace: m.try({
65
91
  try: () => t(r),
66
- catch: (e) => g("filesystem", String(e instanceof Error ? e.message : e), !1)
92
+ catch: (e) => v("filesystem", String(e instanceof Error ? e.message : e), !1)
67
93
  }),
68
94
  presigns: !1,
69
95
  put: (e, t, n) => a(async () => {
@@ -88,8 +114,8 @@ var g = (e, t, n, r) => new a({
88
114
  await (await import("node:fs/promises")).rm(await i(e), { force: !0 });
89
115
  }, e),
90
116
  publicUrl: () => null,
91
- getUrl: () => h.succeed(""),
92
- head: (e) => h.tryPromise({
117
+ getUrl: () => m.succeed(""),
118
+ head: (e) => m.tryPromise({
93
119
  try: async () => {
94
120
  let t = await import("node:fs/promises");
95
121
  try {
@@ -99,26 +125,26 @@ var g = (e, t, n, r) => new a({
99
125
  throw e;
100
126
  }
101
127
  },
102
- catch: (e) => g("filesystem", String(e?.message ?? e), !1)
128
+ catch: (e) => v("filesystem", String(e?.message ?? e), !1)
103
129
  }),
104
130
  getStream: (e) => a(async () => {
105
131
  let t = await import("node:fs/promises"), { createReadStream: n } = await import("node:fs"), r = await i(e), a = await t.stat(r);
106
132
  return {
107
- stream: c("filesystem", () => n(r)),
133
+ stream: l("filesystem", () => n(r)),
108
134
  contentType: "application/octet-stream",
109
135
  size: a.size
110
136
  };
111
137
  }, e),
112
- putStream: (e, t, n) => a(async () => {
113
- let n = await import("node:fs/promises"), { dirname: r } = await import("node:path"), { randomUUID: a } = await import("node:crypto"), { createWriteStream: o } = await import("node:fs"), { pipeline: s } = await import("node:stream/promises"), c = await i(e);
114
- await n.mkdir(r(c), { recursive: !0 });
115
- let u = `${c}.tmp-${a()}`;
138
+ putStream: (e, t, n) => a(async (n) => {
139
+ let r = await import("node:fs/promises"), { dirname: a } = await import("node:path"), { randomUUID: o } = await import("node:crypto"), { createWriteStream: s } = await import("node:fs"), { pipeline: c } = await import("node:stream/promises"), l = await i(e);
140
+ n.throwIfAborted(), await r.mkdir(a(l), { recursive: !0 });
141
+ let d = `${l}.tmp-${o()}`;
116
142
  try {
117
- await s(await l(t), o(u));
118
- let e = await n.stat(u);
119
- return await n.rename(u, c), { size: e.size };
143
+ await c(await u(t), s(d), { signal: n });
144
+ let e = await r.stat(d);
145
+ return n.throwIfAborted(), await r.rename(d, l), { size: e.size };
120
146
  } catch (e) {
121
- throw await n.rm(u, { force: !0 }).catch(() => {}), e;
147
+ throw await r.rm(d, { force: !0 }).catch(() => {}), e;
122
148
  }
123
149
  }, e, !0),
124
150
  getRange: (e, t, n) => a(async () => {
@@ -129,11 +155,11 @@ var g = (e, t, n, r) => new a({
129
155
  end: n
130
156
  });
131
157
  s.on("data", (e) => o.push(e)), s.on("end", () => e()), s.on("error", i);
132
- }), s(o);
158
+ }), c(o);
133
159
  }, e)
134
160
  };
135
- }, w = (e) => {
136
- let t = e.name ?? "s3", i = e.publicAcl ?? !0, a = null, o = () => a || (a = (async () => {
161
+ }, O = (e) => k(e).provider, k = (e) => {
162
+ let t = e.name ?? "s3", i = e.publicAcl ?? !0, a = null, s = () => a || (a = (async () => {
137
163
  let t, n;
138
164
  try {
139
165
  t = await import("@aws-sdk/client-s3"), n = await import("@aws-sdk/s3-request-presigner");
@@ -142,6 +168,7 @@ var g = (e, t, n, r) => new a({
142
168
  }
143
169
  return {
144
170
  client: new t.S3Client({
171
+ maxAttempts: 1,
145
172
  ...e.region ? { region: e.region } : { region: "us-east-1" },
146
173
  ...e.endpoint ? { endpoint: e.endpoint } : {},
147
174
  ...e.forcePathStyle ? { forcePathStyle: !0 } : {},
@@ -153,192 +180,251 @@ var g = (e, t, n, r) => new a({
153
180
  cmds: t,
154
181
  presign: n.getSignedUrl
155
182
  };
156
- })(), a), s = (e) => h.tryPromise({
157
- try: e,
158
- catch: (e) => {
159
- let n = e?.$metadata?.httpStatusCode;
160
- return g(t, String(e?.message ?? e), _(n), n);
183
+ })(), a), c = (e, n) => p(e, (e) => {
184
+ if (e instanceof o) return e;
185
+ let n = e?.$metadata?.httpStatusCode;
186
+ return v(t, String(e?.message ?? e), y(n), n);
187
+ }, n), d = async (e, t) => {
188
+ let n = e, r = () => {
189
+ n.destroy?.();
190
+ };
191
+ t.addEventListener("abort", r, { once: !0 });
192
+ try {
193
+ if (t.aborted) throw r(), Error("Storage read interrupted");
194
+ if (typeof n.transformToByteArray == "function") return await n.transformToByteArray();
195
+ let e = [];
196
+ for await (let t of n) e.push(t);
197
+ let i = e.reduce((e, t) => e + t.byteLength, 0), a = new Uint8Array(i), o = 0;
198
+ for (let t of e) a.set(t, o), o += t.byteLength;
199
+ return a;
200
+ } finally {
201
+ t.removeEventListener("abort", r);
161
202
  }
162
- }), u = async (e) => {
163
- let t = e;
164
- if (typeof t.transformToByteArray == "function") return t.transformToByteArray();
165
- let n = [];
166
- for await (let e of t) n.push(e);
167
- let r = n.reduce((e, t) => e + t.byteLength, 0), i = new Uint8Array(r), a = 0;
168
- for (let e of n) i.set(e, a), a += e.byteLength;
169
- return i;
170
203
  };
171
204
  return {
172
- name: t,
173
- objectNamespace: h.succeed(r([
174
- "s3",
175
- e.endpoint ? n(e.endpoint) : "aws-s3",
176
- e.bucket
177
- ])),
178
- presigns: !0,
179
- put: (t, n, r) => s(async () => {
180
- let { client: a, cmds: s } = await o();
181
- return await a.send(new s.PutObjectCommand({
182
- Bucket: e.bucket,
183
- Key: t,
184
- Body: n,
185
- ContentType: r.contentType,
186
- ...r.public && i ? { ACL: "public-read" } : {}
187
- })), { size: n.byteLength };
188
- }),
189
- get: (t) => s(async () => {
190
- let { client: n, cmds: r } = await o(), i = await n.send(new r.GetObjectCommand({
191
- Bucket: e.bucket,
192
- Key: t
193
- }));
194
- return {
195
- bytes: await u(i.Body),
196
- contentType: i.ContentType ?? "application/octet-stream"
197
- };
198
- }),
199
- delete: (t) => s(async () => {
200
- let { client: n, cmds: r } = await o();
201
- await n.send(new r.DeleteObjectCommand({
202
- Bucket: e.bucket,
203
- Key: t
204
- }));
205
- }),
206
- publicUrl: (t) => e.cdnBaseUrl ? `${e.cdnBaseUrl.replace(/\/$/, "")}/${t}` : null,
207
- ...i ? { setObjectPublic: (t, n) => s(async () => {
208
- let { client: r, cmds: i } = await o();
209
- await r.send(new i.PutObjectAclCommand({
210
- Bucket: e.bucket,
211
- Key: t,
212
- ACL: n ? "public-read" : "private"
213
- }));
214
- }) } : {},
215
- getUrl: (t, n) => s(async () => {
216
- let { client: r, cmds: i, presign: a } = await o();
217
- return a(r, new i.GetObjectCommand({
218
- Bucket: e.bucket,
219
- Key: t
220
- }), { expiresIn: n });
221
- }),
222
- getUploadUrl: (t, n, r) => s(async () => {
223
- let { client: i, cmds: a, presign: s } = await o();
224
- return s(i, new a.PutObjectCommand({
225
- Bucket: e.bucket,
226
- Key: t,
227
- ContentType: r
228
- }), { expiresIn: n });
229
- }),
230
- createMultipartUpload: (t, n, r) => s(async () => {
231
- let { client: a, cmds: s } = await o(), c = await a.send(new s.CreateMultipartUploadCommand({
232
- Bucket: e.bucket,
233
- Key: t,
234
- ContentType: n,
235
- ...r?.public && i ? { ACL: "public-read" } : {}
236
- }));
237
- if (!c.UploadId) throw Error("s3 createMultipartUpload returned no UploadId");
238
- return { uploadId: c.UploadId };
239
- }),
240
- presignUploadPart: (t, n, r, i) => s(async () => {
241
- let { client: a, cmds: s, presign: c } = await o();
242
- return c(a, new s.UploadPartCommand({
243
- Bucket: e.bucket,
244
- Key: t,
245
- UploadId: n,
246
- PartNumber: r
247
- }), { expiresIn: i });
248
- }),
249
- completeMultipartUpload: (t, n, r) => s(async () => {
250
- let { client: i, cmds: a } = await o(), s = await i.send(new a.CompleteMultipartUploadCommand({
251
- Bucket: e.bucket,
252
- Key: t,
253
- UploadId: n,
254
- MultipartUpload: { Parts: r.map((e) => ({
255
- PartNumber: e.partNumber,
256
- ETag: e.etag
257
- })) }
258
- }));
259
- return s.ETag === void 0 ? {} : { etag: s.ETag };
260
- }),
261
- abortMultipartUpload: (t, n) => s(async () => {
262
- let { client: r, cmds: i } = await o();
263
- await r.send(new i.AbortMultipartUploadCommand({
264
- Bucket: e.bucket,
265
- Key: t,
266
- UploadId: n
267
- }));
268
- }),
269
- head: (n) => h.tryPromise({
270
- try: async () => {
271
- let { client: t, cmds: r } = await o();
205
+ provider: {
206
+ name: t,
207
+ objectLocation: (n) => m.try({
208
+ try: () => ({
209
+ namespace: JSON.stringify([
210
+ "s3",
211
+ e.endpoint === void 0 ? "aws" : new URL(e.endpoint).href.replace(/\/+$/, ""),
212
+ e.bucket
213
+ ]),
214
+ key: n
215
+ }),
216
+ catch: () => v(t, "Storage endpoint cannot identify its physical namespace", !1)
217
+ }),
218
+ objectNamespace: m.succeed(r([
219
+ "s3",
220
+ e.endpoint ? n(e.endpoint) : "aws-s3",
221
+ e.bucket
222
+ ])),
223
+ presigns: !0,
224
+ put: (t, n, r) => c(async (a) => {
225
+ let { client: o, cmds: c } = await s();
226
+ return await o.send(new c.PutObjectCommand({
227
+ Bucket: e.bucket,
228
+ Key: t,
229
+ Body: n,
230
+ ContentType: r.contentType,
231
+ ...r.public && i ? { ACL: "public-read" } : {}
232
+ }), { abortSignal: a }), { size: n.byteLength };
233
+ }),
234
+ get: (t) => c(async (n) => {
235
+ let { client: r, cmds: i } = await s(), a = await r.send(new i.GetObjectCommand({
236
+ Bucket: e.bucket,
237
+ Key: t
238
+ }), { abortSignal: n });
239
+ return {
240
+ bytes: await d(a.Body, n),
241
+ contentType: a.ContentType ?? "application/octet-stream"
242
+ };
243
+ }),
244
+ delete: (t) => c(async (n) => {
245
+ let { client: r, cmds: i } = await s();
246
+ await r.send(new i.DeleteObjectCommand({
247
+ Bucket: e.bucket,
248
+ Key: t
249
+ }), { abortSignal: n });
250
+ }),
251
+ publicUrl: (t) => e.cdnBaseUrl ? `${e.cdnBaseUrl.replace(/\/$/, "")}/${t}` : null,
252
+ ...i ? { setObjectPublic: (t, n) => c(async (r) => {
253
+ let { client: i, cmds: a } = await s();
254
+ await i.send(new a.PutObjectAclCommand({
255
+ Bucket: e.bucket,
256
+ Key: t,
257
+ ACL: n ? "public-read" : "private"
258
+ }), { abortSignal: r });
259
+ }) } : {},
260
+ getUrl: (t, n) => c(async (r) => {
261
+ let { client: i, cmds: a, presign: o } = await s();
262
+ return o(i, new a.GetObjectCommand({
263
+ Bucket: e.bucket,
264
+ Key: t
265
+ }), { expiresIn: n });
266
+ }),
267
+ getUploadUrl: (t, n, r) => c(async (i) => {
268
+ let { client: a, cmds: o, presign: c } = await s();
269
+ return c(a, new o.PutObjectCommand({
270
+ Bucket: e.bucket,
271
+ Key: t,
272
+ ContentType: r
273
+ }), { expiresIn: n });
274
+ }),
275
+ createMultipartUpload: (t, n, r) => c(async (a) => {
276
+ let { client: o, cmds: c } = await s(), l = await o.send(new c.CreateMultipartUploadCommand({
277
+ Bucket: e.bucket,
278
+ Key: t,
279
+ ContentType: n,
280
+ ...r?.public && i ? { ACL: "public-read" } : {}
281
+ }), { abortSignal: a });
282
+ if (!l.UploadId) throw Error("s3 createMultipartUpload returned no UploadId");
283
+ return { uploadId: l.UploadId };
284
+ }),
285
+ presignUploadPart: (t, n, r, i) => c(async (a) => {
286
+ let { client: o, cmds: c, presign: l } = await s();
287
+ return l(o, new c.UploadPartCommand({
288
+ Bucket: e.bucket,
289
+ Key: t,
290
+ UploadId: n,
291
+ PartNumber: r
292
+ }), { expiresIn: i });
293
+ }),
294
+ completeMultipartUpload: (t, n, r) => c(async (i) => {
295
+ let { client: a, cmds: o } = await s(), c = await a.send(new o.CompleteMultipartUploadCommand({
296
+ Bucket: e.bucket,
297
+ Key: t,
298
+ UploadId: n,
299
+ MultipartUpload: { Parts: r.map((e) => ({
300
+ PartNumber: e.partNumber,
301
+ ETag: e.etag
302
+ })) }
303
+ }), { abortSignal: i });
304
+ return c.ETag === void 0 ? {} : { etag: c.ETag };
305
+ }),
306
+ abortMultipartUpload: (t, n) => c(async (r) => {
307
+ let { client: i, cmds: a } = await s();
308
+ await i.send(new a.AbortMultipartUploadCommand({
309
+ Bucket: e.bucket,
310
+ Key: t,
311
+ UploadId: n
312
+ }), { abortSignal: r });
313
+ }),
314
+ head: (t) => c(async (n) => {
315
+ let { client: r, cmds: i } = await s();
272
316
  try {
273
- let i = await t.send(new r.HeadObjectCommand({
317
+ let a = await r.send(new i.HeadObjectCommand({
274
318
  Bucket: e.bucket,
275
- Key: n
276
- }));
319
+ Key: t
320
+ }), { abortSignal: n });
277
321
  return {
278
- size: i.ContentLength ?? 0,
279
- ...i.ETag === void 0 ? {} : { etag: i.ETag }
322
+ size: a.ContentLength ?? 0,
323
+ contentType: a.ContentType ?? "application/octet-stream",
324
+ ...a.ETag === void 0 ? {} : { etag: a.ETag }
280
325
  };
281
326
  } catch (e) {
282
327
  if (e?.$metadata?.httpStatusCode === 404) return null;
283
328
  throw e;
284
329
  }
285
- },
286
- catch: (e) => {
287
- let n = e?.$metadata?.httpStatusCode;
288
- return g(t, String(e?.message ?? e), _(n), n);
289
- }
290
- }),
291
- getStream: (n) => s(async () => {
292
- let { client: r, cmds: i } = await o(), a = await r.send(new i.GetObjectCommand({
293
- Bucket: e.bucket,
294
- Key: n
295
- })), s = a.Body;
296
- return {
297
- stream: c(t, () => s),
298
- contentType: a.ContentType ?? "application/octet-stream",
299
- ...a.ContentLength === void 0 ? {} : { size: a.ContentLength }
300
- };
301
- }),
302
- putStream: (t, n, r) => s(async () => {
303
- let { client: a, cmds: s } = await o(), c;
304
- try {
305
- c = await import("@aws-sdk/lib-storage");
306
- } catch (e) {
307
- throw Error("s3/minio `putStream` requires the '@aws-sdk/lib-storage' optional dependency (multipart upload). Install it to enable streamed uploads.", { cause: e });
308
- }
309
- let u = await l(n);
310
- return await new c.Upload({
311
- client: a,
312
- params: {
330
+ }),
331
+ getStream: (n) => c(async (r) => {
332
+ let { client: i, cmds: a } = await s(), o = await i.send(new a.GetObjectCommand({
313
333
  Bucket: e.bucket,
314
- Key: t,
315
- Body: u,
316
- ContentType: r.contentType,
317
- ...r.public && i ? { ACL: "public-read" } : {}
334
+ Key: n
335
+ }), { abortSignal: r }), c = o.Body;
336
+ return {
337
+ stream: l(t, () => c),
338
+ close: m.sync(() => {
339
+ c.destroy();
340
+ }),
341
+ contentType: o.ContentType ?? "application/octet-stream",
342
+ ...o.ContentLength === void 0 ? {} : { size: o.ContentLength }
343
+ };
344
+ }, (e) => e.close ?? m.void),
345
+ putStream: (n, r, a) => c(async (c) => {
346
+ let { client: l, cmds: d } = await s(), f;
347
+ try {
348
+ f = await import("@aws-sdk/lib-storage");
349
+ } catch (e) {
350
+ throw Error("s3/minio `putStream` requires the '@aws-sdk/lib-storage' optional dependency (multipart upload). Install it to enable streamed uploads.", { cause: e });
318
351
  }
319
- }).done(), { size: r.totalSize ?? (await a.send(new s.HeadObjectCommand({
320
- Bucket: e.bucket,
321
- Key: t
322
- }))).ContentLength ?? 0 };
352
+ c.throwIfAborted();
353
+ let p = await u(r), m = new AbortController(), h = AbortSignal.any([c, m.signal]), g = () => {
354
+ p.destroy(/* @__PURE__ */ Error("Storage upload interrupted"));
355
+ }, _ = (e) => {
356
+ m.abort(e);
357
+ }, v = new Proxy(l, { get(e, t, n) {
358
+ return t === "send" ? (e) => l.send(e, { abortSignal: h }).then((t) => {
359
+ if (e instanceof d.UploadPartCommand && (!("ETag" in t) || !t.ETag)) throw Error("Storage upload part acknowledgement omitted its ETag");
360
+ return t;
361
+ }).catch((e) => {
362
+ throw m.abort(e), e;
363
+ }) : Reflect.get(e, t, n);
364
+ } }), y = new f.Upload({
365
+ client: v,
366
+ leavePartsOnError: !0,
367
+ params: {
368
+ Bucket: e.bucket,
369
+ Key: n,
370
+ Body: p,
371
+ ContentType: a.contentType,
372
+ ...a.public && i ? { ACL: "public-read" } : {}
373
+ }
374
+ });
375
+ p.once("error", _), h.addEventListener("abort", g, { once: !0 });
376
+ try {
377
+ h.aborted && g(), await y.done();
378
+ } catch (r) {
379
+ if (y.uploadId !== void 0) try {
380
+ await l.send(new d.AbortMultipartUploadCommand({
381
+ Bucket: e.bucket,
382
+ Key: n,
383
+ UploadId: y.uploadId
384
+ }), { abortSignal: AbortSignal.timeout(5e3) });
385
+ } catch {
386
+ throw new o({
387
+ provider: t,
388
+ message: "Multipart cleanup was not confirmed; object outcome is unknown",
389
+ transient: !1,
390
+ outcome: "unknown"
391
+ });
392
+ }
393
+ throw r;
394
+ } finally {
395
+ h.removeEventListener("abort", g), p.removeListener("error", _), p.destroy();
396
+ }
397
+ return { size: a.totalSize ?? (await l.send(new d.HeadObjectCommand({
398
+ Bucket: e.bucket,
399
+ Key: n
400
+ }), { abortSignal: c })).ContentLength ?? 0 };
401
+ }),
402
+ getRange: (t, n, r) => c(async (i) => {
403
+ let { client: a, cmds: o } = await s(), c = await a.send(new o.GetObjectCommand({
404
+ Bucket: e.bucket,
405
+ Key: t,
406
+ Range: `bytes=${n}-${r}`
407
+ }), { abortSignal: i });
408
+ return d(c.Body, i);
409
+ })
410
+ },
411
+ probe: c(async (t) => {
412
+ let { client: n, cmds: r } = await s();
413
+ await n.send(new r.HeadBucketCommand({ Bucket: e.bucket }), { abortSignal: t });
323
414
  }),
324
- getRange: (t, n, r) => s(async () => {
325
- let { client: i, cmds: a } = await o(), s = await i.send(new a.GetObjectCommand({
326
- Bucket: e.bucket,
327
- Key: t,
328
- Range: `bytes=${n}-${r}`
329
- }));
330
- return u(s.Body);
415
+ close: m.promise(async () => {
416
+ a !== null && (await a.catch(() => null))?.client.destroy();
331
417
  })
332
418
  };
333
- }, T = (e) => e instanceof Uint8Array ? e : typeof e == "string" ? new Uint8Array(Buffer.from(e, "base64")) : new Uint8Array(e), E = () => {
419
+ }, A = (e) => e instanceof Uint8Array ? e : typeof e == "string" ? new Uint8Array(Buffer.from(e, "base64")) : new Uint8Array(e), j = () => {
334
420
  let e = null, t = () => {
335
- if (!e) throw p("the 'database' storage provider isn't bound to a DataStore yet");
421
+ if (!e) throw g("the 'database' storage provider isn't bound to a DataStore yet");
336
422
  return e;
337
- }, n = (e) => h.tryPromise({
423
+ }, n = (e) => m.tryPromise({
338
424
  try: e,
339
- catch: (e) => d(e, "database", !0)
340
- }), a = async (e, n) => (await t().query({
341
- table: i,
425
+ catch: (e) => f(e, "database", !0)
426
+ }), o = async (e, n) => (await t().query({
427
+ table: a,
342
428
  predicate: {
343
429
  column: "key",
344
430
  op: "eq",
@@ -351,74 +437,103 @@ var g = (e, t, n, r) => new a({
351
437
  }))[0];
352
438
  return {
353
439
  name: "database",
354
- objectNamespace: h.succeed(r(["database", i])),
440
+ objectLocation: (e) => n(async () => ({
441
+ namespace: r([
442
+ "database",
443
+ await i(t()),
444
+ a
445
+ ]),
446
+ key: e
447
+ })),
448
+ objectNamespace: n(async () => r([
449
+ "database",
450
+ await i(t()),
451
+ a
452
+ ])),
355
453
  presigns: !1,
356
454
  bindDataStore: (t) => {
357
455
  e = t;
358
456
  },
359
- put: (e, r, o) => n(async () => {
360
- let n = await a(e);
361
- return n ? await t().update(i, String(n.id), {
457
+ put: (e, r, i) => n(async () => {
458
+ let n = await o(e);
459
+ return n ? await t().update(a, String(n.id), {
362
460
  data: r,
363
- contentType: o.contentType,
461
+ contentType: i.contentType,
364
462
  size: String(r.byteLength)
365
- }) : await t().insert(i, {
366
- id: m({
463
+ }) : await t().insert(a, {
464
+ id: _({
367
465
  kind: "typeid",
368
466
  prefix: "blob"
369
- }, i),
467
+ }, a),
370
468
  key: e,
371
469
  data: r,
372
- contentType: o.contentType,
470
+ contentType: i.contentType,
373
471
  size: String(r.byteLength)
374
472
  }), { size: r.byteLength };
375
473
  }),
376
- get: (e) => h.suspend(() => n(() => a(e)).pipe(h.flatMap((t) => t ? h.succeed({
377
- bytes: T(t.data),
474
+ get: (e) => m.suspend(() => n(() => o(e)).pipe(m.flatMap((t) => t ? m.succeed({
475
+ bytes: A(t.data),
378
476
  contentType: String(t.contentType)
379
- }) : h.fail(g("database", `no object at key '${e}'`, !1, 404))))),
477
+ }) : m.fail(v("database", `no object at key '${e}'`, !1, 404))))),
380
478
  delete: (e) => n(async () => {
381
- let n = await a(e, ["id"]);
382
- n && await t().delete(i, String(n.id));
479
+ let n = await o(e, ["id"]);
480
+ n && await t().delete(a, String(n.id));
383
481
  }),
384
482
  publicUrl: () => null,
385
- getUrl: () => h.succeed(""),
483
+ getUrl: () => m.succeed(""),
386
484
  head: (e) => n(async () => {
387
- let t = await a(e, ["size"]);
388
- return t ? { size: o(t.size, "blob size") } : null;
485
+ let t = await o(e, ["size", "contentType"]);
486
+ return t ? {
487
+ size: s(t.size, "blob size"),
488
+ contentType: String(t.contentType)
489
+ } : null;
389
490
  })
390
491
  };
391
- }, D = (e) => {
392
- let t = null, i = () => t || (t = (async () => {
492
+ }, M = (e) => {
493
+ let t = d.read("AZURE_STORAGE_ACCOUNT", e.accountName), n = d.read("AZURE_STORAGE_KEY", e.accountKey), r = d.read("AZURE_STORAGE_CONNECTION_STRING", e.connectionString);
494
+ return N({
495
+ ...e,
496
+ ...t === void 0 ? {} : { accountName: t },
497
+ ...n === void 0 ? {} : { accountKey: n },
498
+ ...r === void 0 ? {} : { connectionString: r }
499
+ }).provider;
500
+ }, N = (e) => {
501
+ let t = new AbortController(), i = null, a = () => i || (i = (async () => {
393
502
  let t;
394
503
  try {
395
504
  t = await import("@azure/storage-blob");
396
505
  } catch (e) {
397
506
  throw Error("the 'azure' storage provider requires the '@azure/storage-blob' optional dependency. Install it to enable it.", { cause: e });
398
507
  }
399
- let n = u.read("AZURE_STORAGE_ACCOUNT", e.accountName), r = u.read("AZURE_STORAGE_KEY", e.accountKey), i = n && r ? new t.StorageSharedKeyCredential(n, r) : null, a = u.read("AZURE_STORAGE_CONNECTION_STRING", e.connectionString);
508
+ let n = e.accountName, r = e.accountKey, i = n && r ? new t.StorageSharedKeyCredential(n, r) : null, a = e.connectionString;
400
509
  return {
401
- container: (a ? t.BlobServiceClient.fromConnectionString(a) : new t.BlobServiceClient(e.endpoint ?? `https://${n}.blob.core.windows.net`, i ?? void 0)).getContainerClient(e.container),
510
+ container: (a ? t.BlobServiceClient.fromConnectionString(a, { retryOptions: { maxTries: 1 } }) : new t.BlobServiceClient(e.endpoint ?? `https://${n}.blob.core.windows.net`, i ?? void 0, { retryOptions: { maxTries: 1 } })).getContainerClient(e.container),
402
511
  sdk: t,
403
512
  cred: i
404
513
  };
405
- })(), t), a = (e) => h.tryPromise({
406
- try: e,
407
- catch: (e) => {
408
- let t = e?.statusCode;
409
- return g("azure", String(e?.message ?? e), _(t), t);
410
- }
411
- }), o = async (e) => {
514
+ })(), i), o = (e, n) => p((n) => e(AbortSignal.any([n, t.signal])), (e) => {
515
+ let t = e?.statusCode;
516
+ return v("azure", String(e?.message ?? e), y(t), t);
517
+ }, n), s = async (e, t) => {
412
518
  if (!e) return /* @__PURE__ */ new Uint8Array();
413
- let t = [];
414
- for await (let n of e) t.push(typeof n == "string" ? new TextEncoder().encode(n) : new Uint8Array(n));
415
- let n = t.reduce((e, t) => e + t.byteLength, 0), r = new Uint8Array(n), i = 0;
416
- for (let e of t) r.set(e, i), i += e.byteLength;
417
- return r;
418
- }, s = async (t, n, r) => {
419
- let { container: a, sdk: o, cred: s } = await i();
519
+ let n = () => {
520
+ e.destroy();
521
+ };
522
+ t.addEventListener("abort", n, { once: !0 });
523
+ try {
524
+ if (t.aborted) throw n(), Error("Storage read interrupted");
525
+ let r = [];
526
+ for await (let t of e) r.push(typeof t == "string" ? new TextEncoder().encode(t) : new Uint8Array(t));
527
+ let i = r.reduce((e, t) => e + t.byteLength, 0), a = new Uint8Array(i), o = 0;
528
+ for (let e of r) a.set(e, o), o += e.byteLength;
529
+ return a;
530
+ } finally {
531
+ t.removeEventListener("abort", n);
532
+ }
533
+ }, c = async (t, n, r) => {
534
+ let { container: i, sdk: o, cred: s } = await a();
420
535
  if (!s) throw Error("azure SAS requires accountName + accountKey (set them or AZURE_STORAGE_ACCOUNT/AZURE_STORAGE_KEY)");
421
- let c = a.getBlockBlobClient(t), l = o.generateBlobSASQueryParameters({
536
+ let c = i.getBlockBlobClient(t), l = o.generateBlobSASQueryParameters({
422
537
  containerName: e.container,
423
538
  blobName: t,
424
539
  permissions: o.BlobSASPermissions.parse(n),
@@ -428,66 +543,96 @@ var g = (e, t, n, r) => new a({
428
543
  return `${c.url}?${l}`;
429
544
  };
430
545
  return {
431
- name: "azure",
432
- objectNamespace: a(async () => {
433
- let { container: e } = await i();
434
- return r(["azure", n(e.url)]);
435
- }),
436
- presigns: !0,
437
- put: (e, t, n) => a(async () => {
438
- let { container: r } = await i();
439
- return await r.getBlockBlobClient(e).uploadData(Buffer.from(t), { blobHTTPHeaders: { blobContentType: n.contentType } }), { size: t.byteLength };
440
- }),
441
- get: (e) => a(async () => {
442
- let { container: t } = await i(), n = await t.getBlockBlobClient(e).download();
443
- return {
444
- bytes: await o(n.readableStreamBody),
445
- contentType: n.contentType ?? "application/octet-stream"
446
- };
447
- }),
448
- delete: (e) => a(async () => {
449
- let { container: t } = await i();
450
- await t.getBlockBlobClient(e).deleteIfExists();
451
- }),
452
- publicUrl: (t) => e.cdnBaseUrl ? `${e.cdnBaseUrl.replace(/\/$/, "")}/${t}` : null,
453
- getUrl: (e, t) => a(() => s(e, "r", t)),
454
- getUploadUrl: (e, t, n) => a(() => s(e, "cw", t)),
455
- head: (e) => h.tryPromise({
456
- try: async () => {
457
- let { container: t } = await i();
546
+ provider: {
547
+ name: "azure",
548
+ objectLocation: (e) => o(async () => {
549
+ let t = (await a()).container, n = new URL(t.url);
550
+ return n.search = "", n.hash = "", {
551
+ namespace: JSON.stringify(["azure", n.href.replace(/\/+$/, "")]),
552
+ key: e
553
+ };
554
+ }),
555
+ objectNamespace: o(async () => {
556
+ let { container: e } = await a();
557
+ return r(["azure", n(e.url)]);
558
+ }),
559
+ presigns: !0,
560
+ put: (e, t, n) => o(async (r) => {
561
+ let { container: i } = await a();
562
+ return await i.getBlockBlobClient(e).uploadData(Buffer.from(t), {
563
+ blobHTTPHeaders: { blobContentType: n.contentType },
564
+ abortSignal: r
565
+ }), { size: t.byteLength };
566
+ }),
567
+ get: (e) => o(async (t) => {
568
+ let { container: n } = await a(), r = await n.getBlockBlobClient(e).download(0, void 0, { abortSignal: t });
569
+ return {
570
+ bytes: await s(r.readableStreamBody, t),
571
+ contentType: r.contentType ?? "application/octet-stream"
572
+ };
573
+ }),
574
+ delete: (e) => o(async (t) => {
575
+ let { container: n } = await a();
576
+ await n.getBlockBlobClient(e).deleteIfExists({ abortSignal: t });
577
+ }),
578
+ publicUrl: (t) => e.cdnBaseUrl ? `${e.cdnBaseUrl.replace(/\/$/, "")}/${t}` : null,
579
+ getUrl: (e, t) => o(() => c(e, "r", t)),
580
+ getUploadUrl: (e, t, n) => o(() => c(e, "cw", t)),
581
+ head: (e) => o(async (t) => {
582
+ let { container: n } = await a();
458
583
  try {
459
- let n = await t.getBlockBlobClient(e).getProperties();
584
+ let r = await n.getBlockBlobClient(e).getProperties({ abortSignal: t });
460
585
  return {
461
- size: n.contentLength ?? 0,
462
- ...n.etag === void 0 ? {} : { etag: n.etag }
586
+ size: r.contentLength ?? 0,
587
+ contentType: r.contentType ?? "application/octet-stream",
588
+ ...r.etag === void 0 ? {} : { etag: r.etag }
463
589
  };
464
590
  } catch (e) {
465
591
  if (e?.statusCode === 404) return null;
466
592
  throw e;
467
593
  }
468
- },
469
- catch: (e) => {
470
- let t = e?.statusCode;
471
- return g("azure", String(e?.message ?? e), _(t), t);
472
- }
473
- }),
474
- getStream: (e) => a(async () => {
475
- let { container: t } = await i(), n = await t.getBlockBlobClient(e).download();
476
- return {
477
- stream: c("azure", () => n.readableStreamBody),
478
- contentType: n.contentType ?? "application/octet-stream",
479
- ...n.contentLength === void 0 ? {} : { size: n.contentLength }
480
- };
481
- }),
482
- putStream: (e, t, n) => a(async () => {
483
- let { container: r } = await i(), a = await l(t);
484
- return await r.getBlockBlobClient(e).uploadStream(a, 4194304, 5, { blobHTTPHeaders: { blobContentType: n.contentType } }), { size: n.totalSize ?? (await r.getBlockBlobClient(e).getProperties()).contentLength ?? 0 };
594
+ }),
595
+ getStream: (e) => o(async (t) => {
596
+ let { container: n } = await a(), r = await n.getBlockBlobClient(e).download(0, void 0, { abortSignal: t });
597
+ return {
598
+ stream: l("azure", () => r.readableStreamBody),
599
+ close: m.sync(() => {
600
+ r.readableStreamBody?.destroy();
601
+ }),
602
+ contentType: r.contentType ?? "application/octet-stream",
603
+ ...r.contentLength === void 0 ? {} : { size: r.contentLength }
604
+ };
605
+ }, (e) => e.close ?? m.void),
606
+ putStream: (e, n, r) => o(async (i) => {
607
+ let { container: o } = await a();
608
+ i.throwIfAborted();
609
+ let s = await u(n), c = () => {
610
+ s.destroy();
611
+ };
612
+ i.addEventListener("abort", c, { once: !0 });
613
+ try {
614
+ if (i.aborted) throw c(), Error("Storage upload interrupted");
615
+ await o.getBlockBlobClient(e).uploadStream(s, 4194304, 5, {
616
+ blobHTTPHeaders: { blobContentType: r.contentType },
617
+ abortSignal: i
618
+ });
619
+ } finally {
620
+ i.removeEventListener("abort", c), s.destroy();
621
+ }
622
+ return { size: r.totalSize ?? (await o.getBlockBlobClient(e).getProperties({ abortSignal: AbortSignal.any([i, t.signal]) })).contentLength ?? 0 };
623
+ }),
624
+ getRange: (e, t, n) => o(async (r) => {
625
+ let { container: i } = await a(), o = await i.getBlockBlobClient(e).download(t, n - t + 1, { abortSignal: r });
626
+ return s(o.readableStreamBody, r);
627
+ })
628
+ },
629
+ probe: o(async (e) => {
630
+ await (await a()).container.getProperties({ abortSignal: e });
485
631
  }),
486
- getRange: (e, t, n) => a(async () => {
487
- let { container: r } = await i(), a = await r.getBlockBlobClient(e).download(t, n - t + 1);
488
- return o(a.readableStreamBody);
632
+ close: m.sync(() => {
633
+ t.abort();
489
634
  })
490
635
  };
491
636
  };
492
637
  //#endregion
493
- export { D as azureProvider, x as clearStorageBuffer, E as databaseProvider, C as filesystemProvider, S as memoryProvider, b as readStorageBuffer, w as s3Provider };
638
+ export { N as azureConnectionTransport, M as azureProvider, w as clearStorageBuffer, j as databaseProvider, D as filesystemProvider, T as memoryProvider, E as memoryProviderWithState, C as readStorageBuffer, k as s3ConnectionTransport, O as s3Provider };