@dimah-s3/db 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +6 -4
  2. package/dist/cli.js +1 -1
  3. package/dist/client/db-client.d.ts +29 -2
  4. package/dist/client/db-client.d.ts.map +1 -1
  5. package/dist/client/index.d.ts +1 -1
  6. package/dist/client/index.d.ts.map +1 -1
  7. package/dist/client/index.js +4 -0
  8. package/dist/client/index.js.map +1 -1
  9. package/dist/fuma-db.d.ts +1 -0
  10. package/dist/fuma-db.d.ts.map +1 -1
  11. package/dist/hooks/create-object-access-guard.d.ts +11 -4
  12. package/dist/hooks/create-object-access-guard.d.ts.map +1 -1
  13. package/dist/hooks/create-quota-guard.d.ts +22 -0
  14. package/dist/hooks/create-quota-guard.d.ts.map +1 -0
  15. package/dist/index.d.ts +3 -3
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +328 -76
  18. package/dist/index.js.map +1 -1
  19. package/dist/jobs/purge-stale-pending.d.ts +9 -6
  20. package/dist/jobs/purge-stale-pending.d.ts.map +1 -1
  21. package/dist/plugin/create-database-endpoints.d.ts +26 -6
  22. package/dist/plugin/create-database-endpoints.d.ts.map +1 -1
  23. package/dist/plugin/create-database-lifecycle-hooks.d.ts.map +1 -1
  24. package/dist/plugin/db.d.ts +41 -4
  25. package/dist/plugin/db.d.ts.map +1 -1
  26. package/dist/plugin/list-query-schema.d.ts +8 -0
  27. package/dist/plugin/list-query-schema.d.ts.map +1 -1
  28. package/dist/plugin/to-db-client-object.d.ts.map +1 -1
  29. package/dist/schema/v1.d.ts +2 -0
  30. package/dist/schema/v1.d.ts.map +1 -1
  31. package/dist/store/list-cursor.d.ts +6 -0
  32. package/dist/store/list-cursor.d.ts.map +1 -0
  33. package/dist/store/map-row.d.ts +1 -0
  34. package/dist/store/map-row.d.ts.map +1 -1
  35. package/dist/store/storage-object-store.d.ts +19 -6
  36. package/dist/store/storage-object-store.d.ts.map +1 -1
  37. package/dist/types/storage-object.d.ts +12 -0
  38. package/dist/types/storage-object.d.ts.map +1 -1
  39. package/package.json +15 -10
  40. package/src/schema/examples/drizzle.ts +2 -0
  41. package/src/schema/examples/schema.prisma +2 -0
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { fumadb } from 'fumadb';
2
2
  import { table, column, idColumn, schema } from 'fumadb/schema';
3
- import { S3_ERROR_CODES, DimahS3Error, pluginPath } from '@dimah-s3/core';
4
- import { definePlugin, createS3Endpoint } from '@dimah-s3/server';
3
+ import { routeNameSchema, S3_ERROR_CODES, DimahS3Error, pluginPath } from '@dimah-s3/core';
4
+ import { chainHooks, definePlugin, isFeatureOn, createS3Endpoint } from '@dimah-s3/server';
5
5
  import { z } from 'zod';
6
+ import { AbortMultipartUploadCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
6
7
 
7
8
  // src/fuma-db.ts
8
9
  var storageObject = table("storage_object", {
@@ -10,6 +11,8 @@ var storageObject = table("storage_object", {
10
11
  scope: column("scope", "string"),
11
12
  bucket: column("bucket", "string"),
12
13
  key: column("key", "string"),
14
+ /** Named dimah-s3 route that created the object. */
15
+ route: column("route", "string"),
13
16
  contentType: column("content_type", "string").nullable(),
14
17
  /** Verified size from HeadObject after confirm. */
15
18
  size: column("size", "bigint").nullable(),
@@ -78,6 +81,7 @@ function mapStorageObjectRow(row) {
78
81
  scope: row.scope,
79
82
  bucket: row.bucket,
80
83
  key: row.key,
84
+ route: row.route,
81
85
  contentType: row.contentType,
82
86
  size: toNumber(row.size),
83
87
  eTag: row.eTag,
@@ -95,7 +99,33 @@ function mapStorageObjectRow(row) {
95
99
  };
96
100
  }
97
101
 
102
+ // src/store/list-cursor.ts
103
+ function encodeListCursor(createdAt, id) {
104
+ return Buffer.from(`${createdAt.toISOString()} ${id}`, "utf8").toString(
105
+ "base64url"
106
+ );
107
+ }
108
+ function decodeListCursor(cursor) {
109
+ try {
110
+ const raw = Buffer.from(cursor, "base64url").toString("utf8");
111
+ const sep = raw.indexOf(" ");
112
+ if (sep < 1) return null;
113
+ const createdAt = new Date(raw.slice(0, sep));
114
+ const id = raw.slice(sep + 1);
115
+ if (!id || Number.isNaN(createdAt.getTime())) return null;
116
+ return { createdAt, id };
117
+ } catch {
118
+ return null;
119
+ }
120
+ }
121
+
98
122
  // src/store/storage-object-store.ts
123
+ function condition(value) {
124
+ if (typeof value === "boolean") {
125
+ throw new Error("[dimah-s3/db] invalid query condition");
126
+ }
127
+ return value;
128
+ }
99
129
  function toPendingMultipartResume(object) {
100
130
  if (object.status !== "pending" || object.uploadId == null || object.declaredSize == null) {
101
131
  return null;
@@ -117,6 +147,7 @@ function createStorageObjectStore(db2) {
117
147
  const now = /* @__PURE__ */ new Date();
118
148
  const pendingFields = {
119
149
  scope: input.scope,
150
+ route: input.route,
120
151
  contentType: input.contentType ?? null,
121
152
  declaredSize: toBigInt(input.declaredSize),
122
153
  metadata: input.metadata ?? null,
@@ -141,12 +172,44 @@ function createStorageObjectStore(db2) {
141
172
  });
142
173
  },
143
174
  async markActive(input) {
175
+ const existing = await store.find(input);
176
+ if (!existing || existing.status === "deleted") {
177
+ throw notFound("No pending object to confirm");
178
+ }
144
179
  const now = /* @__PURE__ */ new Date();
180
+ if (existing.status === "active") {
181
+ await orm.updateMany("storageObject", {
182
+ where: (b) => b.and(
183
+ b("bucket", "=", input.bucket),
184
+ b("key", "=", input.key),
185
+ b("status", "=", "active")
186
+ ),
187
+ set: {
188
+ size: toBigInt(input.size),
189
+ eTag: input.eTag ?? null,
190
+ ...input.contentType !== void 0 && {
191
+ contentType: input.contentType
192
+ },
193
+ ...input.acl !== void 0 && { acl: input.acl },
194
+ ...input.filename !== void 0 && { filename: input.filename },
195
+ ...input.metadata !== void 0 && { metadata: input.metadata },
196
+ uploadId: null,
197
+ declaredSize: null,
198
+ expiresAt: null,
199
+ confirmedAt: now,
200
+ updatedAt: now
201
+ }
202
+ });
203
+ return;
204
+ }
205
+ if (existing.status !== "pending") {
206
+ throw conflict("Object is not pending confirmation");
207
+ }
145
208
  await orm.updateMany("storageObject", {
146
209
  where: (b) => b.and(
147
210
  b("bucket", "=", input.bucket),
148
211
  b("key", "=", input.key),
149
- b("status", "!=", "deleted")
212
+ b("status", "=", "pending")
150
213
  ),
151
214
  set: {
152
215
  status: "active",
@@ -173,6 +236,21 @@ function createStorageObjectStore(db2) {
173
236
  });
174
237
  return row ? mapStorageObjectRow(row) : null;
175
238
  },
239
+ async findByScopeKey(input) {
240
+ const row = await orm.findFirst("storageObject", {
241
+ where: (b) => {
242
+ const parts = [
243
+ b("scope", "=", input.scope),
244
+ b("key", "=", input.key)
245
+ ];
246
+ if (input.bucket) {
247
+ parts.push(b("bucket", "=", input.bucket));
248
+ }
249
+ return b.and(...parts);
250
+ }
251
+ });
252
+ return row ? mapStorageObjectRow(row) : null;
253
+ },
176
254
  async findActive(ref) {
177
255
  const row = await orm.findFirst("storageObject", {
178
256
  where: (b) => b.and(
@@ -194,16 +272,44 @@ function createStorageObjectStore(db2) {
194
272
  return object;
195
273
  },
196
274
  async listByScope(input) {
275
+ const status = input.status ?? "active";
276
+ const parsed = input.cursor ? decodeListCursor(input.cursor) : null;
197
277
  const rows = await orm.findMany("storageObject", {
198
- where: (b) => b.and(
199
- b("scope", "=", input.scope),
200
- input.status ? b("status", "=", input.status) : b("status", "!=", "deleted")
201
- ),
278
+ where: (b) => {
279
+ const parts = [
280
+ b("scope", "=", input.scope),
281
+ b("status", "=", status)
282
+ ];
283
+ if (input.route) parts.push(b("route", "=", input.route));
284
+ if (input.contentType) {
285
+ parts.push(b("contentType", "=", input.contentType));
286
+ }
287
+ if (parsed) {
288
+ parts.push(
289
+ condition(
290
+ b.or(
291
+ condition(b("createdAt", "<", parsed.createdAt)),
292
+ condition(
293
+ b.and(
294
+ b("createdAt", "=", parsed.createdAt),
295
+ b("id", "<", parsed.id)
296
+ )
297
+ )
298
+ )
299
+ )
300
+ );
301
+ }
302
+ return b.and(...parts);
303
+ },
202
304
  orderBy: ["createdAt", "desc"],
203
305
  limit: input.limit,
204
- offset: input.offset
306
+ offset: parsed ? void 0 : input.offset
205
307
  });
206
- return rows.map(mapStorageObjectRow);
308
+ let mapped = rows.map(mapStorageObjectRow);
309
+ if (input.prefix) {
310
+ mapped = mapped.filter((row) => row.key.startsWith(input.prefix));
311
+ }
312
+ return mapped;
207
313
  },
208
314
  async getScopeUsage(scope) {
209
315
  const rows = await orm.findMany("storageObject", {
@@ -216,12 +322,9 @@ function createStorageObjectStore(db2) {
216
322
  }
217
323
  return { totalBytes, objectCount: rows.length };
218
324
  },
219
- async countByScope(scope, status) {
325
+ async countByScope(scope, status = "active") {
220
326
  return orm.count("storageObject", {
221
- where: (b) => b.and(
222
- b("scope", "=", scope),
223
- status ? b("status", "=", status) : b("status", "!=", "deleted")
224
- )
327
+ where: (b) => b.and(b("scope", "=", scope), b("status", "=", status))
225
328
  });
226
329
  },
227
330
  async softDelete(ref) {
@@ -238,11 +341,21 @@ function createStorageObjectStore(db2) {
238
341
  },
239
342
  async deletePending(ref) {
240
343
  await orm.deleteMany("storageObject", {
241
- where: (b) => b.and(
242
- b("bucket", "=", ref.bucket),
243
- b("key", "=", ref.key),
244
- b("status", "=", "pending")
245
- )
344
+ where: (b) => {
345
+ if (ref.uploadId != null) {
346
+ return b.and(
347
+ b("bucket", "=", ref.bucket),
348
+ b("key", "=", ref.key),
349
+ b("status", "=", "pending"),
350
+ b("uploadId", "=", ref.uploadId)
351
+ );
352
+ }
353
+ return b.and(
354
+ b("bucket", "=", ref.bucket),
355
+ b("key", "=", ref.key),
356
+ b("status", "=", "pending")
357
+ );
358
+ }
246
359
  });
247
360
  },
248
361
  async findStalePending({ olderThan }) {
@@ -252,7 +365,9 @@ function createStorageObjectStore(db2) {
252
365
  b("status", "=", "pending"),
253
366
  b.or(
254
367
  b("expiresAt", "<", now),
255
- b.and(b.isNull("expiresAt"), b("createdAt", "<", olderThan))
368
+ condition(
369
+ b.and(b.isNull("expiresAt"), b("createdAt", "<", olderThan))
370
+ )
256
371
  )
257
372
  ),
258
373
  orderBy: ["createdAt", "asc"]
@@ -268,12 +383,74 @@ function createStorageObjectStore(db2) {
268
383
  };
269
384
  return store;
270
385
  }
386
+
387
+ // src/hooks/create-object-access-guard.ts
388
+ function resolveStore(client) {
389
+ return "upsertPending" in client ? client : createStorageObjectStore(client);
390
+ }
391
+ function createObjectAccessGuard(options) {
392
+ const store = resolveStore(options.client);
393
+ const requireStatus = options.requireStatus ?? "active";
394
+ return async (context) => {
395
+ const scope = await options.resolveScope(context.request);
396
+ if (scope === null) throw unauthorized();
397
+ const object = await store.find({
398
+ bucket: context.bucket,
399
+ key: context.key
400
+ });
401
+ if (!object || object.status === "deleted") throw notFound();
402
+ const allowed = options.authorize ? await options.authorize(object, { ...context, scope }) : object.scope === scope;
403
+ if (!allowed) throw forbidden();
404
+ const statusOk = requireStatus === "any" ? true : object.status === requireStatus;
405
+ if (!statusOk) throw notFound();
406
+ if (options.requireUploadId) {
407
+ if (!context.uploadId || object.uploadId !== context.uploadId) {
408
+ throw forbidden("Upload session does not match");
409
+ }
410
+ }
411
+ };
412
+ }
413
+
414
+ // src/hooks/create-quota-guard.ts
415
+ function quotaExceeded(message) {
416
+ return DimahS3Error.from("PAYLOAD_TOO_LARGE", {
417
+ ...S3_ERROR_CODES.QUOTA_EXCEEDED,
418
+ message
419
+ });
420
+ }
421
+ function createQuotaGuard(options) {
422
+ const objects = resolveStore(options.client);
423
+ return async (context) => {
424
+ const scope = await options.resolveScope(context.request);
425
+ if (scope === null) throw unauthorized();
426
+ const usage = await objects.getScopeUsage(scope);
427
+ const incoming = context.file?.size ?? 0;
428
+ const replacing = context.replace === "overwrite";
429
+ if (typeof options.maxFiles === "number" && !replacing && usage.objectCount >= options.maxFiles) {
430
+ throw quotaExceeded("File count quota exceeded");
431
+ }
432
+ if (typeof options.maxBytes === "number") {
433
+ const projected = replacing ? Math.max(usage.totalBytes, incoming) : usage.totalBytes + incoming;
434
+ if (projected > options.maxBytes) {
435
+ throw quotaExceeded("Storage byte quota exceeded");
436
+ }
437
+ }
438
+ };
439
+ }
271
440
  var DB_LIST_DEFAULT_LIMIT = 50;
272
441
  var DB_LIST_MAX_LIMIT = 100;
273
442
  var dbListQuerySchema = z.object({
274
443
  status: z.enum(["pending", "active", "deleted"]).optional(),
275
444
  limit: z.coerce.number().int().min(1).max(DB_LIST_MAX_LIMIT).default(DB_LIST_DEFAULT_LIMIT),
276
- offset: z.coerce.number().int().nonnegative().optional()
445
+ offset: z.coerce.number().int().nonnegative().optional(),
446
+ cursor: z.string().min(1).optional(),
447
+ route: routeNameSchema.optional(),
448
+ contentType: z.string().min(1).optional(),
449
+ prefix: z.string().min(1).optional()
450
+ });
451
+ var dbGetQuerySchema = z.object({
452
+ key: z.string().min(1),
453
+ bucket: z.string().min(1).optional()
277
454
  });
278
455
 
279
456
  // src/plugin/to-db-client-object.ts
@@ -282,6 +459,7 @@ function toDbClientObject(object) {
282
459
  id: object.id,
283
460
  bucket: object.bucket,
284
461
  key: object.key,
462
+ route: object.route,
285
463
  filename: object.filename,
286
464
  contentType: object.contentType,
287
465
  size: object.size,
@@ -304,49 +482,56 @@ function createDatabaseEndpoints(options) {
304
482
  const {
305
483
  status,
306
484
  limit = DB_LIST_DEFAULT_LIMIT,
307
- offset
485
+ offset,
486
+ cursor,
487
+ route,
488
+ contentType,
489
+ prefix
308
490
  } = ctx.query ?? {};
491
+ const fetchLimit = limit + 1;
309
492
  const [rows, usage] = await Promise.all([
310
493
  options.objects.listByScope({
311
494
  scope,
312
- status,
313
- limit,
314
- offset
495
+ status: status ?? "active",
496
+ limit: fetchLimit,
497
+ offset,
498
+ cursor,
499
+ route,
500
+ contentType,
501
+ prefix
315
502
  }),
316
503
  options.objects.getScopeUsage(scope)
317
504
  ]);
505
+ const hasMore = rows.length > limit;
506
+ const page = hasMore ? rows.slice(0, limit) : rows;
507
+ const last = page.at(-1);
508
+ const nextCursor = hasMore && last ? encodeListCursor(last.createdAt, last.id) : null;
318
509
  return {
319
510
  scope,
320
511
  usage,
321
- objects: rows.map(toDbClientObject)
512
+ objects: page.map(toDbClientObject),
513
+ nextCursor
322
514
  };
323
515
  }
516
+ ),
517
+ object: createS3Endpoint(
518
+ pluginPath("db", "object"),
519
+ { method: "GET", query: dbGetQuerySchema },
520
+ async (ctx) => {
521
+ const scope = await options.resolveScope(ctx.context.request);
522
+ if (scope === null) throw unauthorized();
523
+ const found = await options.objects.findByScopeKey({
524
+ scope,
525
+ key: ctx.query.key,
526
+ bucket: ctx.query.bucket
527
+ });
528
+ if (!found || found.status === "deleted") throw notFound();
529
+ return { object: toDbClientObject(found) };
530
+ }
324
531
  )
325
532
  };
326
533
  }
327
534
 
328
- // src/hooks/create-object-access-guard.ts
329
- function resolveStore(db2) {
330
- return "upsertPending" in db2 ? db2 : createStorageObjectStore(db2);
331
- }
332
- function createObjectAccessGuard(options) {
333
- const store = resolveStore(options.db);
334
- const requireStatus = options.requireStatus ?? "active";
335
- return async (context) => {
336
- const scope = await options.resolveScope(context.request);
337
- if (scope === null) throw unauthorized();
338
- const object = await store.find({
339
- bucket: context.bucket,
340
- key: context.key
341
- });
342
- if (!object) throw notFound();
343
- const statusOk = requireStatus === "any" ? object.status !== "deleted" : object.status === requireStatus;
344
- if (!statusOk) throw notFound();
345
- const allowed = options.authorize ? await options.authorize(object, { ...context, scope }) : object.scope === scope;
346
- if (!allowed) throw forbidden();
347
- };
348
- }
349
-
350
535
  // src/plugin/create-database-lifecycle-hooks.ts
351
536
  var DEFAULT_PENDING_TTL_MS = 24 * 60 * 60 * 1e3;
352
537
  function createDatabaseLifecycleHooks(options) {
@@ -369,21 +554,31 @@ function createDatabaseLifecycleHooks(options) {
369
554
  bucket: context.bucket,
370
555
  key: context.key
371
556
  });
372
- if (existing && existing.scope !== scope) {
557
+ if (existing && existing.status !== "deleted" && existing.scope !== scope) {
373
558
  throw forbidden("Key belongs to another scope");
374
559
  }
375
560
  }
376
561
  async function trackPending(context) {
377
562
  const scope = await requireScope(context.request);
563
+ if (context.replace === "overwrite") {
564
+ const existing = await objects.find({
565
+ bucket: context.bucket,
566
+ key: context.key
567
+ });
568
+ if (existing?.status === "active" && existing.scope === scope) {
569
+ return;
570
+ }
571
+ }
378
572
  await objects.upsertPending({
379
573
  scope,
380
574
  bucket: context.bucket,
381
575
  key: context.key,
382
- contentType: context.contentType ?? null,
383
- declaredSize: context.fileSize ?? null,
576
+ route: context.route,
577
+ contentType: context.file.type ?? null,
578
+ declaredSize: context.file.size ?? null,
384
579
  metadata: context.metadata ?? null,
385
580
  acl: context.acl ?? null,
386
- filename: context.fileName ?? null,
581
+ filename: context.file.name,
387
582
  uploadId: context.uploadId ?? null,
388
583
  expiresAt: new Date(Date.now() + pendingTtlMs)
389
584
  });
@@ -400,38 +595,51 @@ function createDatabaseLifecycleHooks(options) {
400
595
  metadata: context.metadata
401
596
  });
402
597
  }
403
- const ownedObjectGuard = createObjectAccessGuard({
404
- db: objects,
598
+ const pendingObjectGuard = createObjectAccessGuard({
599
+ client: objects,
405
600
  resolveScope: options.resolveScope,
406
- requireStatus: "any"
601
+ requireStatus: "pending"
602
+ });
603
+ const multipartSessionGuard = createObjectAccessGuard({
604
+ client: objects,
605
+ resolveScope: options.resolveScope,
606
+ requireStatus: "pending",
607
+ requireUploadId: true
407
608
  });
408
609
  const activeObjectGuard = createObjectAccessGuard({
409
- db: objects,
610
+ client: objects,
410
611
  resolveScope: options.resolveScope,
411
612
  requireStatus: "active"
412
613
  });
614
+ const ownedObjectGuard = createObjectAccessGuard({
615
+ client: objects,
616
+ resolveScope: options.resolveScope,
617
+ requireStatus: "any"
618
+ });
413
619
  return {
414
620
  objects,
415
621
  hooks: {
416
622
  upload: {
417
623
  guard: guardKeyOwnership,
418
624
  onPresigned: trackPending,
419
- confirmGuard: ownedObjectGuard,
420
- onConfirmed: trackConfirmed
421
- },
422
- multipart: {
423
- initGuard: guardKeyOwnership,
424
- onInit: trackPending,
425
- partGuard: ownedObjectGuard,
426
- listGuard: ownedObjectGuard,
427
- completeGuard: ownedObjectGuard,
428
- abortGuard: ownedObjectGuard,
429
- onComplete: trackConfirmed,
430
- onAbort: async (context) => {
431
- await objects.deletePending({
432
- bucket: context.bucket,
433
- key: context.key
434
- });
625
+ confirmGuard: async (context) => {
626
+ if (context.replace === "overwrite") {
627
+ await ownedObjectGuard(context);
628
+ return;
629
+ }
630
+ await pendingObjectGuard(context);
631
+ },
632
+ onConfirmed: trackConfirmed,
633
+ multipart: {
634
+ onInit: trackPending,
635
+ guard: multipartSessionGuard,
636
+ onAbort: async (context) => {
637
+ await objects.deletePending({
638
+ bucket: context.bucket,
639
+ key: context.key,
640
+ uploadId: context.uploadId
641
+ });
642
+ }
435
643
  }
436
644
  },
437
645
  download: {
@@ -452,8 +660,28 @@ function createDatabaseLifecycleHooks(options) {
452
660
  // src/plugin/db.ts
453
661
  function db(options) {
454
662
  const { objects, hooks } = createDatabaseLifecycleHooks(options);
663
+ if (options.quota && hooks.upload) {
664
+ const quotaGuard = createQuotaGuard({
665
+ client: objects,
666
+ resolveScope: options.resolveScope,
667
+ maxBytes: options.quota.maxBytes,
668
+ maxFiles: options.quota.maxFiles
669
+ });
670
+ hooks.upload.guard = chainHooks(hooks.upload.guard, quotaGuard);
671
+ }
455
672
  return definePlugin({
456
673
  id: "db",
674
+ init({ config }) {
675
+ const routes = Object.values(config.routes);
676
+ const hasLifecycle = routes.some(
677
+ (r) => isFeatureOn(r.upload) || isFeatureOn(r.download) || isFeatureOn(r.delete)
678
+ );
679
+ if (!hasLifecycle) {
680
+ throw new Error(
681
+ "[dimah-s3] db plugin requires at least one route with upload, download, or delete enabled."
682
+ );
683
+ }
684
+ },
457
685
  hooks,
458
686
  endpoints: createDatabaseEndpoints({
459
687
  objects,
@@ -465,21 +693,45 @@ function db(options) {
465
693
  }
466
694
  });
467
695
  }
468
-
469
- // src/jobs/purge-stale-pending.ts
470
696
  var DEFAULT_OLDER_THAN_MS = 24 * 60 * 60 * 1e3;
697
+ async function abortAndDelete(s3, objects) {
698
+ for (const object of objects) {
699
+ if (object.uploadId) {
700
+ try {
701
+ await s3.send(
702
+ new AbortMultipartUploadCommand({
703
+ Bucket: object.bucket,
704
+ Key: object.key,
705
+ UploadId: object.uploadId
706
+ })
707
+ );
708
+ } catch {
709
+ }
710
+ }
711
+ try {
712
+ await s3.send(
713
+ new DeleteObjectCommand({
714
+ Bucket: object.bucket,
715
+ Key: object.key
716
+ })
717
+ );
718
+ } catch {
719
+ }
720
+ }
721
+ }
471
722
  async function purgeStalePendingObjects(options) {
472
- const store = resolveStore(options.db);
723
+ const store = resolveStore(options.client);
473
724
  const olderThan = new Date(
474
725
  Date.now() - (options.olderThanMs ?? DEFAULT_OLDER_THAN_MS)
475
726
  );
476
727
  const stale = await store.findStalePending({ olderThan });
477
728
  if (stale.length === 0) return { purged: [] };
729
+ if (options.s3) await abortAndDelete(options.s3, stale);
478
730
  await options.onBeforePurge?.(stale);
479
731
  await store.deleteByIds(stale.map((object) => object.id));
480
732
  return { purged: stale };
481
733
  }
482
734
 
483
- export { DimahS3DB, conflict, createObjectAccessGuard, createStorageObjectStore, db, forbidden, mapStorageObjectRow, notFound, purgeStalePendingObjects, toPendingMultipartResume, unauthorized, v1 };
735
+ export { DimahS3DB, conflict, createObjectAccessGuard, createQuotaGuard, createStorageObjectStore, db, forbidden, notFound, purgeStalePendingObjects, toPendingMultipartResume, unauthorized, v1 };
484
736
  //# sourceMappingURL=index.js.map
485
737
  //# sourceMappingURL=index.js.map