@dimah-s3/db 1.7.0 → 2.0.0-beta.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 (120) hide show
  1. package/README.md +62 -17
  2. package/dist/adapters/drizzle.d.ts +7 -0
  3. package/dist/adapters/drizzle.d.ts.map +1 -0
  4. package/dist/adapters/drizzle.js +3 -0
  5. package/dist/adapters/drizzle.js.map +1 -0
  6. package/dist/adapters/kysely.d.ts +7 -0
  7. package/dist/adapters/kysely.d.ts.map +1 -0
  8. package/dist/adapters/kysely.js +3 -0
  9. package/dist/adapters/kysely.js.map +1 -0
  10. package/dist/adapters/prisma.d.ts +7 -0
  11. package/dist/adapters/prisma.d.ts.map +1 -0
  12. package/dist/adapters/prisma.js +3 -0
  13. package/dist/adapters/prisma.js.map +1 -0
  14. package/dist/cli.d.ts +33 -4
  15. package/dist/cli.d.ts.map +1 -1
  16. package/dist/cli.js +192 -4
  17. package/dist/cli.js.map +1 -1
  18. package/dist/client/db-client.d.ts +9 -85
  19. package/dist/client/db-client.d.ts.map +1 -1
  20. package/dist/client/db-object-source.d.ts +25 -0
  21. package/dist/client/db-object-source.d.ts.map +1 -0
  22. package/dist/client/index.d.ts +3 -3
  23. package/dist/client/index.d.ts.map +1 -1
  24. package/dist/client/index.js +96 -42
  25. package/dist/client/index.js.map +1 -1
  26. package/dist/drizzle/mysql.d.ts +1024 -0
  27. package/dist/drizzle/mysql.d.ts.map +1 -0
  28. package/dist/drizzle/mysql.js +123 -0
  29. package/dist/drizzle/mysql.js.map +1 -0
  30. package/dist/drizzle/pg.d.ts +963 -0
  31. package/dist/drizzle/pg.d.ts.map +1 -0
  32. package/dist/drizzle/pg.js +104 -0
  33. package/dist/drizzle/pg.js.map +1 -0
  34. package/dist/drizzle/shared.d.ts +16 -0
  35. package/dist/drizzle/shared.d.ts.map +1 -0
  36. package/dist/drizzle/sqlite.d.ts +1023 -0
  37. package/dist/drizzle/sqlite.d.ts.map +1 -0
  38. package/dist/drizzle/sqlite.js +107 -0
  39. package/dist/drizzle/sqlite.js.map +1 -0
  40. package/dist/folder-path.d.ts +32 -0
  41. package/dist/folder-path.d.ts.map +1 -0
  42. package/dist/fuma-db.d.ts +47 -5
  43. package/dist/fuma-db.d.ts.map +1 -1
  44. package/dist/index.d.ts +9 -13
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +1452 -620
  47. package/dist/index.js.map +1 -1
  48. package/dist/plugin/create-database-endpoints.d.ts +105 -21
  49. package/dist/plugin/create-database-endpoints.d.ts.map +1 -1
  50. package/dist/plugin/create-database-lifecycle-hooks.d.ts +37 -19
  51. package/dist/plugin/create-database-lifecycle-hooks.d.ts.map +1 -1
  52. package/dist/plugin/db.d.ts +29 -118
  53. package/dist/plugin/db.d.ts.map +1 -1
  54. package/dist/plugin/object-access.d.ts +46 -0
  55. package/dist/plugin/object-access.d.ts.map +1 -0
  56. package/dist/plugin/public-stores.d.ts +33 -0
  57. package/dist/plugin/public-stores.d.ts.map +1 -0
  58. package/dist/plugin/purge.d.ts +49 -0
  59. package/dist/plugin/purge.d.ts.map +1 -0
  60. package/dist/plugin/quota.d.ts +25 -0
  61. package/dist/plugin/quota.d.ts.map +1 -0
  62. package/dist/plugin/schemas.d.ts +69 -0
  63. package/dist/plugin/schemas.d.ts.map +1 -0
  64. package/dist/plugin/scope.d.ts +31 -0
  65. package/dist/plugin/scope.d.ts.map +1 -0
  66. package/dist/plugin/to-db-client-object.d.ts +8 -4
  67. package/dist/plugin/to-db-client-object.d.ts.map +1 -1
  68. package/dist/schema/v1.d.ts +1 -8
  69. package/dist/schema/v1.d.ts.map +1 -1
  70. package/dist/schema/v2.d.ts +64 -0
  71. package/dist/schema/v2.d.ts.map +1 -0
  72. package/dist/standard-schema.d.ts +42 -0
  73. package/dist/standard-schema.d.ts.map +1 -0
  74. package/dist/store/create-db-store.d.ts +17 -0
  75. package/dist/store/create-db-store.d.ts.map +1 -0
  76. package/dist/store/folder-store.d.ts +36 -0
  77. package/dist/store/folder-store.d.ts.map +1 -0
  78. package/dist/store/list-cursor.d.ts +26 -4
  79. package/dist/store/list-cursor.d.ts.map +1 -1
  80. package/dist/store/map-row.d.ts +17 -8
  81. package/dist/store/map-row.d.ts.map +1 -1
  82. package/dist/store/object-store.d.ts +208 -0
  83. package/dist/store/object-store.d.ts.map +1 -0
  84. package/dist/store/schema-version.d.ts +7 -0
  85. package/dist/store/schema-version.d.ts.map +1 -0
  86. package/dist/types/client.d.ts +209 -0
  87. package/dist/types/client.d.ts.map +1 -0
  88. package/dist/types/plugin.d.ts +112 -0
  89. package/dist/types/plugin.d.ts.map +1 -0
  90. package/dist/types/storage-object.d.ts +40 -85
  91. package/dist/types/storage-object.d.ts.map +1 -1
  92. package/package.json +51 -12
  93. package/prisma/dimah-s3.prisma +80 -0
  94. package/sql/mysql.sql +59 -0
  95. package/sql/pg.sql +53 -0
  96. package/sql/sqlite.sql +54 -0
  97. package/sql/upgrade/1.0.0-2.0.0/mysql.sql +71 -0
  98. package/sql/upgrade/1.0.0-2.0.0/pg.sql +47 -0
  99. package/sql/upgrade/1.0.0-2.0.0/sqlite.sql +119 -0
  100. package/dist/client/db-object-list-accessors.d.ts +0 -36
  101. package/dist/client/db-object-list-accessors.d.ts.map +0 -1
  102. package/dist/client/db-object-query.d.ts +0 -40
  103. package/dist/client/db-object-query.d.ts.map +0 -1
  104. package/dist/errors.d.ts +0 -6
  105. package/dist/errors.d.ts.map +0 -1
  106. package/dist/hooks/create-object-access-guard.d.ts +0 -51
  107. package/dist/hooks/create-object-access-guard.d.ts.map +0 -1
  108. package/dist/hooks/create-quota-guard.d.ts +0 -28
  109. package/dist/hooks/create-quota-guard.d.ts.map +0 -1
  110. package/dist/jobs/purge-stale-pending.d.ts +0 -31
  111. package/dist/jobs/purge-stale-pending.d.ts.map +0 -1
  112. package/dist/plugin/list-query-schema.d.ts +0 -21
  113. package/dist/plugin/list-query-schema.d.ts.map +0 -1
  114. package/dist/plugin/memoize-scope-resolver.d.ts +0 -8
  115. package/dist/plugin/memoize-scope-resolver.d.ts.map +0 -1
  116. package/dist/store/storage-object-store.d.ts +0 -137
  117. package/dist/store/storage-object-store.d.ts.map +0 -1
  118. package/src/schema/examples/README.md +0 -10
  119. package/src/schema/examples/drizzle.ts +0 -92
  120. package/src/schema/examples/schema.prisma +0 -55
package/dist/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { fumadb } from 'fumadb';
2
2
  import { table, column, idColumn, schema } from 'fumadb/schema';
3
- import { trimmedString, routeNameSchema, S3_ERROR_CODES, APIError, pluginPath } from '@dimah-s3/core';
4
- import { chainHooks, definePlugin, isFeatureOn, createS3Endpoint } from '@dimah-s3/server';
3
+ import { definePlugin, isFeatureOn, createS3Endpoint, isAwsNotFound, readUploadMarker, deleteObjectIfMatch } from '@dimah-s3/server/plugins';
4
+ import { s3Error, pluginPath, base64UrlDecode, base64UrlEncode } from '@dimah-s3/core';
5
+ import { optionalContentTypeSchema, optionalTrimmedString, routeNameSchema, objectKeySchema, trimmedString, fileNameSchema } from '@dimah-s3/core/schema';
5
6
  import * as z from 'zod';
6
- import { AbortMultipartUploadCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
7
+ import { AbortMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
7
8
 
8
9
  // src/fuma-db.ts
9
10
  var storageObject = table("storage_object", {
@@ -11,23 +12,17 @@ var storageObject = table("storage_object", {
11
12
  scope: column("scope", "string"),
12
13
  bucket: column("bucket", "string"),
13
14
  key: column("key", "string"),
14
- /** Named dimah-s3 route that created the object. */
15
15
  route: column("route", "string"),
16
16
  contentType: column("content_type", "string").nullable(),
17
- /** Verified size from HeadObject after confirm. */
18
17
  size: column("size", "bigint").nullable(),
19
18
  eTag: column("e_tag", "string").nullable(),
20
19
  filename: column("filename", "string").nullable(),
21
20
  status: column("status", "string"),
22
- /** App-specific labels, visibility, publicUrl, etc. */
23
- metadata: column("metadata", "json").nullable(),
21
+ s3Metadata: column("metadata", "json").nullable(),
24
22
  acl: column("acl", "string").nullable(),
25
- /** Multipart upload ID while status = pending. */
26
23
  uploadId: column("upload_id", "string").nullable(),
27
- /** Client-declared size before confirm (quota reservation). */
28
24
  declaredSize: column("declared_size", "bigint").nullable(),
29
25
  confirmedAt: column("confirmed_at", "timestamp").nullable(),
30
- /** Pending TTL or temporary object expiry. */
31
26
  expiresAt: column("expires_at", "timestamp").nullable(),
32
27
  createdAt: column("created_at", "timestamp").defaultTo$("now"),
33
28
  updatedAt: column("updated_at", "timestamp").defaultTo$("now"),
@@ -39,41 +34,223 @@ var v1 = schema({
39
34
  storageObject
40
35
  }
41
36
  });
37
+ function byteCount(name) {
38
+ return column(name, "bigint").nullable();
39
+ }
40
+ var storageObject2 = table("storage_object", {
41
+ id: idColumn("id", "varchar(255)").defaultTo$("auto"),
42
+ /** Owner, for example `user:123` or `org:acme`. */
43
+ scope: column("scope", "string"),
44
+ bucket: column("bucket", "string"),
45
+ key: column("key", "string"),
46
+ /** Named dimah-s3 route that created the object. */
47
+ route: column("route", "string"),
48
+ /** Logical folder path. `null` is the root. */
49
+ folder: column("folder", "string").nullable(),
50
+ filename: column("filename", "string"),
51
+ contentType: column("content_type", "string").nullable(),
52
+ /** Verified size from HeadObject, set on confirm. */
53
+ size: byteCount("size"),
54
+ /** Size the client declared before confirm. Quota counts it while pending. */
55
+ declaredSize: byteCount("declared_size"),
56
+ eTag: column("e_tag", "string").nullable(),
57
+ status: column("status", "string"),
58
+ /** S3 user metadata of the verified object. */
59
+ s3Metadata: column("s3_metadata", "json").nullable(),
60
+ /** App attributes, validated by the plugin's `attributes.schema`. */
61
+ attributes: column("attributes", "json").nullable(),
62
+ acl: column("acl", "string").nullable(),
63
+ /** Multipart upload ID while the row is pending. */
64
+ uploadId: column("upload_id", "string").nullable(),
65
+ /** Token of the upload ticket that wrote the row. Confirm and purge match it. */
66
+ uploadToken: column("upload_token", "string").nullable(),
67
+ /** Confirm deadline of a pending row (the upload ticket's expiry). */
68
+ expiresAt: column("expires_at", "timestamp").nullable(),
69
+ confirmedAt: column("confirmed_at", "timestamp").nullable(),
70
+ createdAt: column("created_at", "timestamp").defaultTo$("now"),
71
+ updatedAt: column("updated_at", "timestamp").defaultTo$("now"),
72
+ deletedAt: column("deleted_at", "timestamp").nullable()
73
+ }).unique("storage_object_bucket_key_uk", ["bucket", "key"]);
74
+ var storageFolder = table("storage_folder", {
75
+ id: idColumn("id", "varchar(255)").defaultTo$("auto"),
76
+ scope: column("scope", "string"),
77
+ /** Full folder path. */
78
+ path: column("path", "string"),
79
+ /** Path of the containing folder. `null` is the root. */
80
+ parent: column("parent", "string").nullable(),
81
+ /** Last segment of `path`. */
82
+ name: column("name", "string"),
83
+ createdAt: column("created_at", "timestamp").defaultTo$("now")
84
+ }).unique("storage_folder_scope_path_uk", ["scope", "path"]);
85
+ var v2Indexes = [
86
+ {
87
+ name: "storage_object_scope_folder_idx",
88
+ table: "storage_object",
89
+ columns: ["scope", "status", "folder", "created_at"]
90
+ },
91
+ {
92
+ name: "storage_object_scope_updated_idx",
93
+ table: "storage_object",
94
+ columns: ["scope", "status", "updated_at"]
95
+ },
96
+ {
97
+ name: "storage_object_scope_filename_idx",
98
+ table: "storage_object",
99
+ columns: ["scope", "status", "filename"]
100
+ },
101
+ {
102
+ name: "storage_object_scope_size_idx",
103
+ table: "storage_object",
104
+ columns: ["scope", "status", "size"]
105
+ },
106
+ {
107
+ name: "storage_object_status_expires_idx",
108
+ table: "storage_object",
109
+ columns: ["status", "expires_at"]
110
+ },
111
+ {
112
+ name: "storage_folder_scope_parent_idx",
113
+ table: "storage_folder",
114
+ columns: ["scope", "parent"]
115
+ }
116
+ ];
117
+ var v1Indexes = [
118
+ "storage_object_scope_status_created_idx",
119
+ "storage_object_scope_route_idx",
120
+ "storage_object_status_created_idx"
121
+ ];
122
+ var custom = (sql) => ({ type: "custom", sql });
123
+ var keyName = `COALESCE(NULLIF(substr("key", length(rtrim("key", replace("key", '/', ''))) + 1), ''), "key")`;
124
+ var beforeV2 = [
125
+ custom(
126
+ `UPDATE "storage_object" SET "filename" = ${keyName} WHERE "filename" IS NULL`
127
+ )
128
+ ];
129
+ var afterV2 = [
130
+ ...v1Indexes.map((name) => custom(`DROP INDEX IF EXISTS "${name}"`)),
131
+ ...v2Indexes.map((index) => {
132
+ const columns = index.columns.map((name) => `"${name}"`).join(", ");
133
+ return custom(
134
+ `CREATE INDEX IF NOT EXISTS "${index.name}" ON "${index.table}" (${columns})`
135
+ );
136
+ })
137
+ ];
138
+ var v2 = schema({
139
+ version: "2.0.0",
140
+ tables: {
141
+ storageObject: storageObject2,
142
+ storageFolder
143
+ },
144
+ async up({ auto }) {
145
+ return [...beforeV2, ...await auto(), ...afterV2];
146
+ }
147
+ });
42
148
 
43
149
  // src/fuma-db.ts
44
150
  var DimahS3DB = fumadb({
45
151
  namespace: "dimah_s3",
46
- schemas: [v1]
152
+ schemas: [v1, v2]
47
153
  });
48
- function unauthorized(message = S3_ERROR_CODES.UNAUTHORIZED.message) {
49
- return APIError.from("UNAUTHORIZED", {
50
- ...S3_ERROR_CODES.UNAUTHORIZED,
51
- message
52
- });
154
+ var DB_SCHEMA_VERSION = v2.version;
155
+ var FOLDER_PATH_MAX_LENGTH = 1024;
156
+ var FOLDER_NAME_MAX_LENGTH = 255;
157
+ var FOLDER_MAX_DEPTH = 32;
158
+ var INVALID_CHARACTER = /[\p{Cc}\p{Cs}]/u;
159
+ function characterCount(value) {
160
+ return Array.from(value).length;
53
161
  }
54
- function forbidden(message = S3_ERROR_CODES.FORBIDDEN.message) {
55
- return APIError.from("FORBIDDEN", {
56
- ...S3_ERROR_CODES.FORBIDDEN,
57
- message
58
- });
162
+ function isValidName(name) {
163
+ const length = characterCount(name);
164
+ return length >= 1 && length <= FOLDER_NAME_MAX_LENGTH && name !== "." && name !== ".." && !INVALID_CHARACTER.test(name);
59
165
  }
60
- function notFound(message = S3_ERROR_CODES.OBJECT_NOT_FOUND.message) {
61
- return APIError.from("NOT_FOUND", {
62
- ...S3_ERROR_CODES.OBJECT_NOT_FOUND,
63
- message
64
- });
166
+ function normalizeFolderPath(path) {
167
+ if (!path.isWellFormed()) return null;
168
+ const normalized = path.normalize("NFC");
169
+ if (characterCount(normalized) > FOLDER_PATH_MAX_LENGTH) return null;
170
+ const names = normalized.split("/");
171
+ if (names.length > FOLDER_MAX_DEPTH) return null;
172
+ return names.every(isValidName) ? normalized : null;
65
173
  }
66
- function conflict(message = S3_ERROR_CODES.CONFLICT.message) {
67
- return APIError.from("CONFLICT", {
68
- ...S3_ERROR_CODES.CONFLICT,
69
- message
70
- });
174
+ function parseFolder(value) {
175
+ if (value === "") return null;
176
+ const path = normalizeFolderPath(value);
177
+ if (path === null) throw s3Error("FOLDER_INVALID");
178
+ return path;
179
+ }
180
+ function parentFolderPath(path) {
181
+ const index = path.lastIndexOf("/");
182
+ return index === -1 ? null : path.slice(0, index);
183
+ }
184
+ function folderName(path) {
185
+ return path.slice(path.lastIndexOf("/") + 1);
186
+ }
187
+ function folderAncestors(path) {
188
+ const segments = path.split("/");
189
+ return segments.map((_, index) => segments.slice(0, index + 1).join("/"));
190
+ }
191
+ var DEFAULT_LIST_SORT = {
192
+ field: "createdAt",
193
+ direction: "desc"
194
+ };
195
+ var decoder = new TextDecoder();
196
+ function fingerprint(filters) {
197
+ const text = JSON.stringify([
198
+ filters.folder === void 0,
199
+ filters.folder ?? null,
200
+ filters.search ?? null,
201
+ filters.status,
202
+ filters.route ?? null,
203
+ filters.contentType ?? null,
204
+ filters.sort.field,
205
+ filters.sort.direction
206
+ ]);
207
+ let hash = 2166136261;
208
+ for (let index = 0; index < text.length; index++) {
209
+ hash ^= text.charCodeAt(index);
210
+ hash = Math.imul(hash, 16777619);
211
+ }
212
+ return (hash >>> 0).toString(36);
213
+ }
214
+ function encodeListCursor(filters, object2) {
215
+ const value = object2[filters.sort.field];
216
+ const payload = {
217
+ f: fingerprint(filters),
218
+ v: value instanceof Date ? value.toISOString() : value,
219
+ i: object2.id
220
+ };
221
+ return base64UrlEncode(JSON.stringify(payload));
222
+ }
223
+ function decodeListCursor(cursor, filters) {
224
+ let payload;
225
+ try {
226
+ payload = JSON.parse(decoder.decode(base64UrlDecode(cursor)));
227
+ } catch {
228
+ return null;
229
+ }
230
+ if (typeof payload !== "object" || payload === null) return null;
231
+ const { f, v, i } = payload;
232
+ if (f !== fingerprint(filters) || typeof i !== "string" || !i) return null;
233
+ const value = decodeValue(filters.sort.field, v);
234
+ return value === void 0 ? null : { value, id: i };
235
+ }
236
+ function decodeValue(field, value) {
237
+ switch (field) {
238
+ case "createdAt":
239
+ case "updatedAt": {
240
+ if (typeof value !== "string") return void 0;
241
+ const date = new Date(value);
242
+ return Number.isNaN(date.getTime()) ? void 0 : date;
243
+ }
244
+ case "size":
245
+ return value === null || typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? value : void 0;
246
+ case "filename":
247
+ return typeof value === "string" ? value : void 0;
248
+ }
71
249
  }
72
250
 
73
251
  // src/store/map-row.ts
74
252
  function toNumber(value) {
75
- if (value === null) return null;
76
- return typeof value === "bigint" ? Number(value) : value;
253
+ return value === null ? null : Number(value);
77
254
  }
78
255
  function mapStorageObjectRow(row) {
79
256
  return {
@@ -82,605 +259,962 @@ function mapStorageObjectRow(row) {
82
259
  bucket: row.bucket,
83
260
  key: row.key,
84
261
  route: row.route,
262
+ folder: row.folder,
263
+ filename: row.filename,
85
264
  contentType: row.contentType,
86
265
  size: toNumber(row.size),
266
+ declaredSize: toNumber(row.declaredSize),
87
267
  eTag: row.eTag,
88
- filename: row.filename,
89
268
  status: row.status,
90
- metadata: row.metadata ?? null,
269
+ s3Metadata: row.s3Metadata ?? null,
270
+ attributes: row.attributes ?? null,
91
271
  acl: row.acl,
92
272
  uploadId: row.uploadId,
93
- declaredSize: toNumber(row.declaredSize),
94
- confirmedAt: row.confirmedAt,
273
+ uploadToken: row.uploadToken,
95
274
  expiresAt: row.expiresAt,
275
+ confirmedAt: row.confirmedAt,
96
276
  createdAt: row.createdAt,
97
277
  updatedAt: row.updatedAt,
98
278
  deletedAt: row.deletedAt
99
279
  };
100
280
  }
101
281
 
102
- // src/store/list-cursor.ts
103
- function encodeListCursor(createdAt, id) {
104
- return Buffer.from(`${createdAt.toISOString()} ${id}`, "utf8").toString(
105
- "base64url"
282
+ // src/store/object-store.ts
283
+ var DEFAULT_LIST_LIMIT = 50;
284
+ var DEFAULT_STALE_LIMIT = 100;
285
+ var PURGE_CLAIM_MS = 15 * 6e4;
286
+ var CLAIMED_TOKEN_PREFIX = "purge:";
287
+ function claimedToken(token) {
288
+ return token.startsWith(CLAIMED_TOKEN_PREFIX) ? token : CLAIMED_TOKEN_PREFIX + token;
289
+ }
290
+ function uploadTokenOf(object2) {
291
+ const token = object2.uploadToken;
292
+ return token?.startsWith(CLAIMED_TOKEN_PREFIX) ? token.slice(CLAIMED_TOKEN_PREFIX.length) : token;
293
+ }
294
+ function isUploading(object2) {
295
+ return object2.uploadToken !== null && (object2.status === "pending" || object2.status === "active" && object2.expiresAt !== null);
296
+ }
297
+ function liveObject(b, now) {
298
+ return b.or(
299
+ b("status", "=", "active"),
300
+ b.and(b("status", "=", "pending"), b("expiresAt", ">=", now))
106
301
  );
107
302
  }
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
- }
303
+ function uploading(b) {
304
+ return b.or(
305
+ b("status", "=", "pending"),
306
+ b.and(b("status", "=", "active"), b.isNotNull("expiresAt"))
307
+ );
120
308
  }
121
-
122
- // src/store/storage-object-store.ts
123
- function condition(value) {
124
- if (typeof value === "boolean") {
125
- throw new TypeError("[dimah-s3/db] invalid query condition");
126
- }
127
- return value;
309
+ function staleObject(b, now) {
310
+ return b.or(
311
+ b.and(
312
+ b("status", "=", "pending"),
313
+ b.or(b.isNull("expiresAt"), b("expiresAt", "<", now))
314
+ ),
315
+ b.and(b("status", "=", "purging"), b("expiresAt", "<", now)),
316
+ b.and(
317
+ b("status", "=", "active"),
318
+ b.isNotNull("uploadToken"),
319
+ b("expiresAt", "<", now)
320
+ )
321
+ );
128
322
  }
129
- function toPendingMultipartResume(object2) {
130
- if (object2.status !== "pending" || object2.uploadId == null || object2.declaredSize == null) {
131
- return null;
132
- }
133
- return {
134
- uploadId: object2.uploadId,
135
- key: object2.key,
136
- fileSize: object2.declaredSize,
137
- bucket: object2.bucket
138
- };
323
+ function byRef(b, ref) {
324
+ return b.and(b("bucket", "=", ref.bucket), b("key", "=", ref.key));
139
325
  }
140
- function toBigInt(value) {
141
- return value === null || value === void 0 ? null : BigInt(Math.trunc(value));
326
+ function byUpload(b, ref) {
327
+ return b.and(byRef(b, ref), b("uploadToken", "=", ref.uploadToken));
142
328
  }
143
- function confirmedFields(input, now) {
144
- return {
145
- size: toBigInt(input.size),
146
- eTag: input.eTag ?? null,
147
- ...input.contentType !== void 0 && {
148
- contentType: input.contentType
149
- },
150
- ...input.acl !== void 0 && { acl: input.acl },
151
- ...input.filename !== void 0 && { filename: input.filename },
152
- ...input.metadata !== void 0 && { metadata: input.metadata },
153
- uploadId: null,
154
- declaredSize: null,
155
- expiresAt: null,
156
- confirmedAt: now,
157
- updatedAt: now
158
- };
329
+ var lastClaimDeadline = 0;
330
+ function claimDeadline() {
331
+ lastClaimDeadline = Math.max(
332
+ Date.now() + PURGE_CLAIM_MS,
333
+ lastClaimDeadline + 1
334
+ );
335
+ return new Date(lastClaimDeadline);
159
336
  }
160
- function toActiveObject(existing, input, now) {
337
+ function filterConditions(b, filter, now) {
338
+ const status = filter.status ?? "active";
339
+ const parts = [
340
+ b("scope", "=", filter.scope),
341
+ status === "pending" ? b.and(b("status", "=", "pending"), b("expiresAt", ">=", now)) : b("status", "=", status)
342
+ ];
343
+ if (filter.folder === null) parts.push(b.isNull("folder"));
344
+ else if (filter.folder !== void 0) {
345
+ parts.push(b("folder", "=", filter.folder));
346
+ }
347
+ if (filter.route) parts.push(b("route", "=", filter.route));
348
+ if (filter.contentType?.endsWith("/*")) {
349
+ const prefix = filter.contentType.slice(0, -1);
350
+ parts.push(b("contentType", "starts with", prefix));
351
+ } else if (filter.contentType) {
352
+ parts.push(b("contentType", "=", filter.contentType));
353
+ }
354
+ if (filter.search) parts.push(b("filename", "contains", filter.search));
355
+ return parts;
356
+ }
357
+ function createObjectStore(getOrm) {
358
+ const map = mapStorageObjectRow;
359
+ const find = async (ref) => {
360
+ const orm = await getOrm();
361
+ const row = await orm.findFirst("storageObject", {
362
+ where: (b) => byRef(b, ref)
363
+ });
364
+ return row ? map(row) : null;
365
+ };
366
+ const findById = async (id) => {
367
+ const orm = await getOrm();
368
+ const row = await orm.findFirst("storageObject", {
369
+ where: (b) => b("id", "=", id)
370
+ });
371
+ return row ? map(row) : null;
372
+ };
161
373
  return {
162
- ...existing,
163
- status: "active",
164
- size: input.size,
165
- eTag: input.eTag ?? null,
166
- contentType: input.contentType !== void 0 ? input.contentType : existing.contentType,
167
- acl: input.acl !== void 0 ? input.acl : existing.acl,
168
- filename: input.filename !== void 0 ? input.filename : existing.filename,
169
- metadata: input.metadata !== void 0 ? input.metadata : existing.metadata,
170
- uploadId: null,
171
- declaredSize: null,
172
- expiresAt: null,
173
- confirmedAt: now,
174
- updatedAt: now,
175
- deletedAt: null
176
- };
177
- }
178
- function createStorageObjectStore(db2) {
179
- const orm = db2.orm("1.0.0");
180
- const store = {
374
+ find,
375
+ async list(input) {
376
+ const orm = await getOrm();
377
+ const now = /* @__PURE__ */ new Date();
378
+ const sort = input.sort ?? DEFAULT_LIST_SORT;
379
+ const limit = input.limit ?? DEFAULT_LIST_LIMIT;
380
+ const { field, direction } = sort;
381
+ const after = direction === "desc" ? "<" : ">";
382
+ const nullable = field === "size";
383
+ const filters = {
384
+ folder: input.folder,
385
+ search: input.search || void 0,
386
+ status: input.status ?? "active",
387
+ route: input.route || void 0,
388
+ contentType: input.contentType || void 0,
389
+ sort
390
+ };
391
+ const cursor = input.cursor ? decodeListCursor(input.cursor, filters) : null;
392
+ const where = (segment) => (b) => {
393
+ const parts = filterConditions(b, input, now);
394
+ const position = segment.after;
395
+ if (segment.nulls) {
396
+ parts.push(b.isNull(field));
397
+ if (position) parts.push(b("id", after, position.id));
398
+ } else {
399
+ if (nullable) parts.push(b.isNotNull(field));
400
+ if (position && position.value !== null) {
401
+ parts.push(
402
+ b.or(
403
+ b(field, after, position.value),
404
+ b.and(
405
+ b(field, "=", position.value),
406
+ b("id", after, position.id)
407
+ )
408
+ )
409
+ );
410
+ }
411
+ }
412
+ return b.and(...parts);
413
+ };
414
+ const findSegment = (segment, take2) => orm.findMany("storageObject", {
415
+ where: where(segment),
416
+ orderBy: segment.nulls ? [["id", direction]] : [
417
+ [field, direction],
418
+ ["id", direction]
419
+ ],
420
+ limit: take2
421
+ });
422
+ const take = limit + 1;
423
+ let rows;
424
+ if (cursor?.value === null) {
425
+ rows = await findSegment({ nulls: true, after: cursor }, take);
426
+ } else {
427
+ rows = await findSegment(
428
+ { nulls: false, after: cursor ?? void 0 },
429
+ take
430
+ );
431
+ if (nullable && rows.length < take) {
432
+ rows.push(
433
+ ...await findSegment({ nulls: true }, take - rows.length)
434
+ );
435
+ }
436
+ }
437
+ const objects = rows.slice(0, limit).map(map);
438
+ const last = objects.at(-1);
439
+ return {
440
+ objects,
441
+ nextCursor: rows.length > limit && last ? encodeListCursor(filters, last) : null,
442
+ firstPage: cursor === null
443
+ };
444
+ },
445
+ async count(filter) {
446
+ const orm = await getOrm();
447
+ const now = /* @__PURE__ */ new Date();
448
+ return orm.count("storageObject", {
449
+ where: (b) => b.and(...filterConditions(b, filter, now))
450
+ });
451
+ },
452
+ async getUsage(scope, options = {}) {
453
+ const orm = await getOrm();
454
+ const now = /* @__PURE__ */ new Date();
455
+ const rows = await orm.findMany("storageObject", {
456
+ select: [
457
+ "bucket",
458
+ "key",
459
+ "status",
460
+ "size",
461
+ "declaredSize",
462
+ "expiresAt"
463
+ ],
464
+ where: (b) => b.and(b("scope", "=", scope), liveObject(b, now))
465
+ });
466
+ const { excluding } = options;
467
+ let totalBytes = 0;
468
+ let objectCount = 0;
469
+ for (const row of rows) {
470
+ if (excluding?.bucket === row.bucket && excluding.key === row.key) {
471
+ continue;
472
+ }
473
+ const size = Number(row.size ?? 0);
474
+ const declared = Number(row.declaredSize ?? 0);
475
+ const overwriting = row.status === "active" && row.expiresAt !== null && row.expiresAt >= now;
476
+ totalBytes += row.size === null ? declared : overwriting ? Math.max(size, declared) : size;
477
+ objectCount += 1;
478
+ }
479
+ return { totalBytes, objectCount };
480
+ },
181
481
  async upsertPending(input) {
482
+ const ref = { bucket: input.bucket, key: input.key };
483
+ const existing = await find(ref);
484
+ if (existing?.status === "purging") throw s3Error("CONFLICT");
485
+ if (existing?.status === "active" && !input.overwrite) {
486
+ throw s3Error("OBJECT_EXISTS");
487
+ }
488
+ const orm = await getOrm();
489
+ const upload = {
490
+ uploadId: input.uploadId ?? null,
491
+ uploadToken: input.uploadToken,
492
+ declaredSize: input.declaredSize ?? null,
493
+ expiresAt: input.expiresAt
494
+ };
182
495
  const now = /* @__PURE__ */ new Date();
183
- const pendingFields = {
496
+ const fresh = {
497
+ ...upload,
498
+ ...ref,
184
499
  scope: input.scope,
185
500
  route: input.route,
501
+ folder: input.folder,
502
+ filename: input.filename,
186
503
  contentType: input.contentType ?? null,
187
- declaredSize: toBigInt(input.declaredSize),
188
- metadata: input.metadata ?? null,
189
- acl: input.acl ?? null,
190
- filename: input.filename ?? null,
191
- uploadId: input.uploadId ?? null,
192
- expiresAt: input.expiresAt ?? null,
193
- status: "pending",
194
504
  size: null,
195
505
  eTag: null,
506
+ status: "pending",
507
+ s3Metadata: null,
508
+ attributes: null,
509
+ acl: input.acl ?? null,
196
510
  confirmedAt: null,
511
+ createdAt: now,
512
+ updatedAt: now,
197
513
  deletedAt: null
198
514
  };
199
- const row = await orm.upsert("storageObject", {
200
- where: (b) => b.and(b("bucket", "=", input.bucket), b("key", "=", input.key)),
201
- update: { ...pendingFields, updatedAt: now },
202
- create: {
203
- ...pendingFields,
204
- bucket: input.bucket,
205
- key: input.key,
206
- updatedAt: now
515
+ const create = async () => {
516
+ try {
517
+ await orm.create("storageObject", fresh);
518
+ } catch (error) {
519
+ if (await find(ref)) throw s3Error("CONFLICT");
520
+ throw error;
207
521
  }
208
- }).forceReturning();
209
- return mapStorageObjectRow(row);
210
- },
211
- async markActive(input) {
212
- const existing = await store.find(input);
213
- if (!existing || existing.status === "deleted") {
214
- throw notFound("No pending object to confirm");
215
- }
216
- const now = /* @__PURE__ */ new Date();
217
- const fields = confirmedFields(input, now);
218
- if (existing.status === "active") {
522
+ };
523
+ if (existing === null) {
524
+ await create();
525
+ } else if (existing.status === "deleted") {
526
+ await orm.deleteMany("storageObject", {
527
+ where: (b) => b.and(b("id", "=", existing.id), b("status", "=", "deleted"))
528
+ });
529
+ await create();
530
+ } else {
531
+ const unchanged = (b) => b.and(
532
+ byRef(b, ref),
533
+ b("status", "=", existing.status),
534
+ b("scope", "=", input.scope),
535
+ existing.uploadToken === null ? b.isNull("uploadToken") : b("uploadToken", "=", existing.uploadToken)
536
+ );
219
537
  await orm.updateMany("storageObject", {
220
- where: (b) => b.and(
221
- b("bucket", "=", input.bucket),
222
- b("key", "=", input.key),
223
- b("status", "=", "active")
224
- ),
225
- set: fields
538
+ where: unchanged,
539
+ // An active row stays active until the overwrite is confirmed.
540
+ set: existing.status === "active" ? upload : fresh
226
541
  });
227
- return toActiveObject(existing, input, now);
228
- }
229
- if (existing.status !== "pending") {
230
- throw conflict("Object is not pending confirmation");
231
542
  }
543
+ const row = await find(ref);
544
+ if (row?.uploadToken !== input.uploadToken) throw s3Error("CONFLICT");
545
+ return row;
546
+ },
547
+ async markActive(input) {
548
+ const orm = await getOrm();
549
+ const now = /* @__PURE__ */ new Date();
232
550
  await orm.updateMany("storageObject", {
233
- where: (b) => b.and(
234
- b("bucket", "=", input.bucket),
235
- b("key", "=", input.key),
236
- b("status", "=", "pending")
237
- ),
551
+ where: (b) => b.and(byUpload(b, input), uploading(b)),
238
552
  set: {
239
553
  status: "active",
240
- ...fields,
241
- deletedAt: null
242
- }
243
- });
244
- return toActiveObject(existing, input, now);
245
- },
246
- async find(ref) {
247
- const row = await orm.findFirst("storageObject", {
248
- where: (b) => b.and(b("bucket", "=", ref.bucket), b("key", "=", ref.key))
249
- });
250
- return row ? mapStorageObjectRow(row) : null;
251
- },
252
- async findByScopeKey(input) {
253
- const row = await orm.findFirst("storageObject", {
254
- where: (b) => {
255
- const parts = [
256
- b("scope", "=", input.scope),
257
- b("key", "=", input.key)
258
- ];
259
- if (input.bucket) {
260
- parts.push(b("bucket", "=", input.bucket));
261
- }
262
- return b.and(...parts);
554
+ size: input.size,
555
+ declaredSize: null,
556
+ eTag: input.eTag ?? null,
557
+ ...input.contentType !== void 0 && {
558
+ contentType: input.contentType
559
+ },
560
+ ...input.filename !== void 0 && { filename: input.filename },
561
+ ...input.s3Metadata !== void 0 && {
562
+ s3Metadata: input.s3Metadata
563
+ },
564
+ ...input.acl !== void 0 && { acl: input.acl },
565
+ uploadId: null,
566
+ expiresAt: null,
567
+ confirmedAt: now,
568
+ updatedAt: now
263
569
  }
264
570
  });
265
- return row ? mapStorageObjectRow(row) : null;
266
- },
267
- async findActive(ref) {
268
- const row = await orm.findFirst("storageObject", {
269
- where: (b) => b.and(
270
- b("bucket", "=", ref.bucket),
271
- b("key", "=", ref.key),
272
- b("status", "=", "active")
273
- )
274
- });
275
- return row ? mapStorageObjectRow(row) : null;
276
- },
277
- async findPendingMultipart(input) {
278
- const row = await orm.findFirst("storageObject", {
279
- where: (b) => b.and(
280
- b("bucket", "=", input.bucket),
281
- b("key", "=", input.key),
282
- b("status", "=", "pending"),
283
- b.isNotNull("uploadId"),
284
- b("declaredSize", "=", BigInt(Math.trunc(input.fileSize)))
285
- )
286
- });
287
- return row ? mapStorageObjectRow(row) : null;
288
- },
289
- async listByScope(input) {
290
- const status = input.status ?? "active";
291
- const parsed = input.cursor ? decodeListCursor(input.cursor) : null;
292
- const rows = await orm.findMany("storageObject", {
293
- where: (b) => {
294
- const parts = [
295
- b("scope", "=", input.scope),
296
- b("status", "=", status)
297
- ];
298
- if (input.route) parts.push(b("route", "=", input.route));
299
- if (input.contentType) {
300
- parts.push(b("contentType", "=", input.contentType));
301
- }
302
- if (input.prefix) {
303
- parts.push(b("key", "starts with", input.prefix));
304
- }
305
- if (parsed) {
306
- parts.push(
307
- condition(
308
- b.or(
309
- condition(b("createdAt", "<", parsed.createdAt)),
310
- condition(
311
- b.and(
312
- b("createdAt", "=", parsed.createdAt),
313
- b("id", "<", parsed.id)
314
- )
315
- )
316
- )
317
- )
318
- );
319
- }
320
- return b.and(...parts);
321
- },
322
- orderBy: ["createdAt", "desc"],
323
- limit: input.limit,
324
- offset: parsed ? void 0 : input.offset
325
- });
326
- return rows.map(mapStorageObjectRow);
327
- },
328
- async getScopeUsage(scope) {
329
- const rows = await orm.findMany("storageObject", {
330
- select: ["size", "declaredSize"],
331
- where: (b) => b.and(b("scope", "=", scope), b("status", "!=", "deleted"))
332
- });
333
- let totalBytes = 0;
334
- for (const row of rows) {
335
- totalBytes += Number(row.size ?? row.declaredSize ?? 0n);
571
+ const row = await find(input);
572
+ if (row?.status === "active" && row.uploadToken === input.uploadToken && !isUploading(row)) {
573
+ return row;
336
574
  }
337
- return { totalBytes, objectCount: rows.length };
575
+ throw s3Error("OBJECT_NOT_FOUND");
338
576
  },
339
- async countByScope(scope, status = "active") {
340
- return orm.count("storageObject", {
341
- where: (b) => b.and(b("scope", "=", scope), b("status", "=", status))
577
+ async update(ref, input) {
578
+ const orm = await getOrm();
579
+ await orm.updateMany("storageObject", {
580
+ where: (b) => b.and(
581
+ byRef(b, ref),
582
+ b("status", "=", "active"),
583
+ ref.id === void 0 || b("id", "=", ref.id),
584
+ ref.scope === void 0 || b("scope", "=", ref.scope)
585
+ ),
586
+ set: {
587
+ ...input.filename !== void 0 && { filename: input.filename },
588
+ ...input.folder !== void 0 && { folder: input.folder },
589
+ ...input.attributes !== void 0 && {
590
+ attributes: input.attributes
591
+ },
592
+ updatedAt: /* @__PURE__ */ new Date()
593
+ }
342
594
  });
595
+ const row = await find(ref);
596
+ if (row?.status !== "active") return null;
597
+ if (ref.id !== void 0 && row.id !== ref.id) return null;
598
+ return ref.scope === void 0 || row.scope === ref.scope ? row : null;
343
599
  },
344
600
  async softDelete(ref) {
601
+ const orm = await getOrm();
345
602
  const now = /* @__PURE__ */ new Date();
346
603
  await orm.updateMany("storageObject", {
347
- where: (b) => b.and(b("bucket", "=", ref.bucket), b("key", "=", ref.key)),
604
+ where: (b) => b.and(byRef(b, ref), b("status", "!=", "deleted")),
348
605
  set: { status: "deleted", deletedAt: now, updatedAt: now }
349
606
  });
350
607
  },
351
608
  async hardDelete(ref) {
609
+ const orm = await getOrm();
610
+ await orm.deleteMany("storageObject", { where: (b) => byRef(b, ref) });
611
+ },
612
+ async cancelUpload(ref) {
613
+ const orm = await getOrm();
352
614
  await orm.deleteMany("storageObject", {
353
- where: (b) => b.and(b("bucket", "=", ref.bucket), b("key", "=", ref.key))
615
+ where: (b) => b.and(byUpload(b, ref), b("status", "=", "pending"))
616
+ });
617
+ await orm.updateMany("storageObject", {
618
+ where: (b) => b.and(
619
+ byUpload(b, ref),
620
+ b("status", "=", "active"),
621
+ b.isNotNull("expiresAt")
622
+ ),
623
+ set: {
624
+ uploadId: null,
625
+ uploadToken: null,
626
+ declaredSize: null,
627
+ expiresAt: null
628
+ }
354
629
  });
355
630
  },
356
- async deletePending(ref) {
631
+ async discardUpload(ref) {
632
+ const orm = await getOrm();
357
633
  await orm.deleteMany("storageObject", {
358
- where: (b) => {
359
- if (ref.uploadId != null) {
360
- return b.and(
361
- b("bucket", "=", ref.bucket),
362
- b("key", "=", ref.key),
363
- b("status", "=", "pending"),
364
- b("uploadId", "=", ref.uploadId)
365
- );
366
- }
367
- return b.and(
368
- b("bucket", "=", ref.bucket),
369
- b("key", "=", ref.key),
370
- b("status", "=", "pending")
371
- );
372
- }
634
+ where: (b) => b.and(byUpload(b, ref), uploading(b))
373
635
  });
374
636
  },
375
- async findStalePending({ olderThan }) {
637
+ async findStale(options = {}) {
638
+ const orm = await getOrm();
376
639
  const now = /* @__PURE__ */ new Date();
377
640
  const rows = await orm.findMany("storageObject", {
378
- where: (b) => b.and(
379
- b("status", "=", "pending"),
380
- b.or(
381
- b("expiresAt", "<", now),
382
- condition(
383
- b.and(b.isNull("expiresAt"), b("createdAt", "<", olderThan))
384
- )
385
- )
386
- ),
387
- orderBy: ["createdAt", "asc"]
641
+ where: (b) => staleObject(b, now),
642
+ // A lapsed claim's deadline is its claim time plus the lease, so a
643
+ // row that keeps failing queues behind the rows that expired before.
644
+ orderBy: [
645
+ ["expiresAt", "asc"],
646
+ ["id", "asc"]
647
+ ],
648
+ limit: options.limit ?? DEFAULT_STALE_LIMIT
388
649
  });
389
- return rows.map(mapStorageObjectRow);
650
+ return rows.map(map);
651
+ },
652
+ async claimStale(id) {
653
+ const orm = await getOrm();
654
+ const now = /* @__PURE__ */ new Date();
655
+ const deadline = claimDeadline();
656
+ const stale = (b) => b.and(b("id", "=", id), staleObject(b, now));
657
+ const current = await findById(id);
658
+ if (!current) return null;
659
+ if (current.status === "active") {
660
+ const token = current.uploadToken;
661
+ if (token === null) return null;
662
+ await orm.updateMany("storageObject", {
663
+ where: (b) => b.and(
664
+ stale(b),
665
+ b("status", "=", "active"),
666
+ b("uploadToken", "=", token)
667
+ ),
668
+ set: { expiresAt: deadline, uploadToken: claimedToken(token) }
669
+ });
670
+ } else {
671
+ await orm.updateMany("storageObject", {
672
+ where: (b) => b.and(stale(b), b("status", "in", ["pending", "purging"])),
673
+ set: { status: "purging", expiresAt: deadline, updatedAt: now }
674
+ });
675
+ }
676
+ const row = await findById(id);
677
+ return row?.expiresAt?.getTime() === deadline.getTime() ? row : null;
390
678
  },
391
- async deleteByIds(ids) {
392
- if (ids.length === 0) return;
679
+ async deletePurged(id) {
680
+ const orm = await getOrm();
393
681
  await orm.deleteMany("storageObject", {
394
- where: (b) => b("id", "in", ids)
682
+ where: (b) => b.and(b("id", "=", id), b("status", "=", "purging"))
395
683
  });
684
+ },
685
+ async pruneDeleted(olderThan) {
686
+ const orm = await getOrm();
687
+ const where = (b) => b.and(b("status", "=", "deleted"), b("deletedAt", "<", olderThan));
688
+ const pruned = await orm.count("storageObject", { where });
689
+ if (pruned > 0) await orm.deleteMany("storageObject", { where });
690
+ return pruned;
396
691
  }
397
692
  };
398
- return store;
399
693
  }
400
694
 
401
- // src/hooks/create-object-access-guard.ts
402
- function resolveStore(client) {
403
- return "upsertPending" in client ? client : createStorageObjectStore(client);
404
- }
405
- function createObjectAccessGuard(options) {
406
- const store = resolveStore(options.client);
407
- const requireStatus = options.requireStatus ?? "active";
408
- return async (context) => {
409
- const scope = await options.resolveScope(context.request, {
410
- operation: options.operation,
411
- route: context.route,
412
- key: context.key
413
- });
414
- if (scope === null) throw unauthorized();
415
- const object2 = await store.find({
416
- bucket: context.bucket,
417
- key: context.key
695
+ // src/store/folder-store.ts
696
+ var DEFAULT_FOLDER_CHILDREN_LIMIT = 1e3;
697
+ function insertFolder(orm, scope, path) {
698
+ return orm.create("storageFolder", {
699
+ scope,
700
+ path,
701
+ parent: parentFolderPath(path),
702
+ name: folderName(path),
703
+ createdAt: /* @__PURE__ */ new Date()
704
+ });
705
+ }
706
+ function createFolderStore(getOrm) {
707
+ const find = async (scope, path) => {
708
+ const orm = await getOrm();
709
+ return orm.findFirst("storageFolder", {
710
+ where: (b) => b.and(b("scope", "=", scope), b("path", "=", path))
418
711
  });
419
- if (!object2 || object2.status === "deleted") throw notFound();
420
- const allowed = options.authorize ? await options.authorize(object2, { ...context, scope }) : object2.scope === scope;
421
- if (!allowed) throw forbidden();
422
- const statusOk = requireStatus === "any" ? true : object2.status === requireStatus;
423
- if (!statusOk) throw notFound();
424
- if (options.requireUploadId) {
425
- if (!context.uploadId || object2.uploadId !== context.uploadId) {
426
- throw forbidden("Upload session does not match");
712
+ };
713
+ const insertMissing = async (scope, path) => {
714
+ const existing = await find(scope, path);
715
+ if (existing) return existing;
716
+ try {
717
+ return await insertFolder(await getOrm(), scope, path);
718
+ } catch (error) {
719
+ const raced = await find(scope, path);
720
+ if (raced) return raced;
721
+ throw error;
722
+ }
723
+ };
724
+ const ensure = async (scope, path) => {
725
+ if (path === null) return;
726
+ const ancestors = folderAncestors(path);
727
+ const orm = await getOrm();
728
+ const present = new Set(
729
+ (await orm.findMany("storageFolder", {
730
+ select: ["path"],
731
+ where: (b) => b.and(b("scope", "=", scope), b("path", "in", ancestors))
732
+ })).map((folder) => folder.path)
733
+ );
734
+ for (const ancestor of ancestors) {
735
+ if (!present.has(ancestor)) await insertMissing(scope, ancestor);
736
+ }
737
+ };
738
+ const countContents = async (scope, path) => {
739
+ const orm = await getOrm();
740
+ const now = /* @__PURE__ */ new Date();
741
+ const [folders, objects] = await Promise.all([
742
+ orm.count("storageFolder", {
743
+ where: (b) => b.and(b("scope", "=", scope), b("parent", "=", path))
744
+ }),
745
+ orm.count("storageObject", {
746
+ where: (b) => b.and(
747
+ b("scope", "=", scope),
748
+ b("folder", "=", path),
749
+ liveObject(b, now)
750
+ )
751
+ })
752
+ ]);
753
+ return folders + objects;
754
+ };
755
+ return {
756
+ find,
757
+ async children(scope, parent, options = {}) {
758
+ const orm = await getOrm();
759
+ return orm.findMany("storageFolder", {
760
+ where: (b) => b.and(
761
+ b("scope", "=", scope),
762
+ parent === null ? b.isNull("parent") : b("parent", "=", parent)
763
+ ),
764
+ orderBy: [
765
+ ["name", "asc"],
766
+ ["id", "asc"]
767
+ ],
768
+ limit: options.limit ?? DEFAULT_FOLDER_CHILDREN_LIMIT
769
+ });
770
+ },
771
+ ensure,
772
+ async create(scope, path) {
773
+ if (await find(scope, path)) throw s3Error("FOLDER_EXISTS");
774
+ let folder;
775
+ try {
776
+ folder = await insertFolder(await getOrm(), scope, path);
777
+ } catch (error) {
778
+ if (await find(scope, path)) throw s3Error("FOLDER_EXISTS");
779
+ throw error;
780
+ }
781
+ await ensure(scope, parentFolderPath(path));
782
+ return folder;
783
+ },
784
+ async delete(scope, path) {
785
+ if (await countContents(scope, path) > 0) {
786
+ throw s3Error("FOLDER_NOT_EMPTY");
787
+ }
788
+ const orm = await getOrm();
789
+ await orm.deleteMany("storageFolder", {
790
+ where: (b) => b.and(b("scope", "=", scope), b("path", "=", path))
791
+ });
792
+ if (await countContents(scope, path) > 0) {
793
+ await insertMissing(scope, path);
794
+ throw s3Error("FOLDER_NOT_EMPTY");
427
795
  }
428
796
  }
429
797
  };
430
798
  }
431
799
 
432
- // src/hooks/create-quota-guard.ts
433
- function quotaExceeded(message) {
434
- return APIError.from("PAYLOAD_TOO_LARGE", {
435
- ...S3_ERROR_CODES.QUOTA_EXCEEDED,
436
- message
437
- });
800
+ // src/store/schema-version.ts
801
+ async function checkSchemaVersion(client) {
802
+ let version;
803
+ try {
804
+ version = await client.version();
805
+ } catch (error) {
806
+ throw new Error(
807
+ `[dimah-s3/db] The database has no dimah-s3 schema. Create it with sql/<dialect>.sql, your ORM's migrations, or migrate(client) from @dimah-s3/db/cli.`,
808
+ { cause: error }
809
+ );
810
+ }
811
+ if (isNewer(version, DB_SCHEMA_VERSION)) {
812
+ throw new Error(
813
+ `[dimah-s3/db] The database is at schema ${version}, newer than the ${DB_SCHEMA_VERSION} this @dimah-s3/db reads. Upgrade @dimah-s3/db on every instance.`
814
+ );
815
+ }
816
+ if (version !== DB_SCHEMA_VERSION) {
817
+ throw new Error(
818
+ `[dimah-s3/db] The database is at schema ${version}, but @dimah-s3/db needs ${DB_SCHEMA_VERSION}. Run sql/upgrade/${version}-${DB_SCHEMA_VERSION}/<dialect>.sql or migrate(client) from @dimah-s3/db/cli.`
819
+ );
820
+ }
821
+ }
822
+ function isNewer(a, b) {
823
+ const left = a.split(".").map(Number);
824
+ const right = b.split(".").map(Number);
825
+ for (let index = 0; index < Math.max(left.length, right.length); index++) {
826
+ const difference = (left[index] ?? 0) - (right[index] ?? 0);
827
+ if (difference !== 0) return difference > 0;
828
+ }
829
+ return false;
438
830
  }
439
- function createQuotaGuard(options) {
440
- const objects = resolveStore(options.client);
441
- return async (context) => {
442
- const scope = await options.resolveScope(context.request, {
443
- operation: "upload",
444
- route: context.route,
445
- key: context.key,
446
- clientMetadata: context.clientMetadata
831
+
832
+ // src/store/create-db-store.ts
833
+ function createDbStore(client) {
834
+ let checked;
835
+ const checkSchema = () => {
836
+ checked ??= checkSchemaVersion(client).catch((error) => {
837
+ checked = void 0;
838
+ throw error;
447
839
  });
448
- if (scope === null) throw unauthorized();
449
- const usage = await objects.getScopeUsage(scope);
450
- const incoming = context.file?.size ?? 0;
451
- const replacing = context.replace === "overwrite";
452
- if (typeof options.maxFiles === "number" && !replacing && usage.objectCount >= options.maxFiles) {
453
- throw quotaExceeded("File count quota exceeded");
454
- }
455
- if (typeof options.maxBytes === "number") {
456
- let projected = usage.totalBytes + incoming;
457
- if (replacing && context.key && context.bucket) {
458
- const existing = await objects.find({
459
- bucket: context.bucket,
460
- key: context.key
461
- });
462
- const oldSize = existing && existing.status !== "deleted" ? existing.size ?? existing.declaredSize ?? 0 : 0;
463
- projected = usage.totalBytes - oldSize + incoming;
464
- }
465
- if (projected > options.maxBytes) {
466
- throw quotaExceeded("Storage byte quota exceeded");
467
- }
468
- }
840
+ return checked;
841
+ };
842
+ const getOrm = async () => {
843
+ await checkSchema();
844
+ return client.orm(DB_SCHEMA_VERSION);
469
845
  };
846
+ return {
847
+ objects: createObjectStore(getOrm),
848
+ folders: createFolderStore(getOrm),
849
+ checkSchema
850
+ };
851
+ }
852
+
853
+ // src/standard-schema.ts
854
+ async function validateStandardSchema(schema3, value) {
855
+ return schema3["~standard"].validate(value);
856
+ }
857
+ function formatStandardIssue(issues) {
858
+ const [issue] = issues;
859
+ if (!issue) return "Invalid value";
860
+ const path = (issue.path ?? []).map(
861
+ (segment) => String(typeof segment === "object" ? segment.key : segment)
862
+ ).join(".");
863
+ return path ? `${path}: ${issue.message}` : issue.message;
470
864
  }
471
- var DB_LIST_DEFAULT_LIMIT = 50;
472
865
  var DB_LIST_MAX_LIMIT = 100;
473
- function queryInt(schema2) {
866
+ var MAX_SEARCH_LENGTH = 255;
867
+ var MAX_CURSOR_LENGTH = 2048;
868
+ function queryInt(schema3) {
474
869
  return z.codec(
475
- z.union([z.string().trim().regex(z.regexes.integer), schema2]),
476
- schema2,
870
+ z.union([z.string().trim().regex(z.regexes.integer), schema3]),
871
+ schema3,
477
872
  {
478
873
  decode: (value) => typeof value === "number" ? value : Number.parseInt(value, 10),
479
874
  encode: (value) => value
480
875
  }
481
876
  );
482
877
  }
483
- var dbListQuerySchema = z.object({
878
+ var dbListSortSchema = z.object({
879
+ field: z.enum(["createdAt", "updatedAt", "size", "filename"]),
880
+ direction: z.enum(["asc", "desc"])
881
+ });
882
+ var dbListSortQuerySchema = z.codec(
883
+ z.union([z.string(), dbListSortSchema]),
884
+ dbListSortSchema,
885
+ {
886
+ decode: (value, ctx) => {
887
+ if (typeof value !== "string") return value;
888
+ try {
889
+ return JSON.parse(value);
890
+ } catch {
891
+ ctx.issues.push({
892
+ code: "invalid_format",
893
+ format: "json",
894
+ input: value,
895
+ message: "Invalid sort"
896
+ });
897
+ return z.NEVER;
898
+ }
899
+ },
900
+ encode: (value) => value
901
+ }
902
+ );
903
+ var dbListQuerySchema = z.strictObject({
904
+ folder: z.string().optional(),
905
+ route: routeNameSchema.optional(),
906
+ // Rows that purge has claimed stay internal, as on `GET /db/object`.
484
907
  status: z.enum(["pending", "active", "deleted"]).optional(),
908
+ contentType: optionalContentTypeSchema,
909
+ /** Text the filename contains. */
910
+ search: optionalTrimmedString.pipe(
911
+ z.string().max(MAX_SEARCH_LENGTH).optional()
912
+ ),
913
+ sort: dbListSortQuerySchema.optional(),
485
914
  limit: queryInt(z.int().min(1).max(DB_LIST_MAX_LIMIT)).default(
486
- DB_LIST_DEFAULT_LIMIT
915
+ DEFAULT_LIST_LIMIT
487
916
  ),
488
- offset: queryInt(z.int().nonnegative()).optional(),
489
- cursor: trimmedString.optional(),
490
- route: routeNameSchema.optional(),
491
- contentType: trimmedString.optional(),
492
- prefix: trimmedString.optional()
917
+ cursor: optionalTrimmedString.pipe(
918
+ z.string().max(MAX_CURSOR_LENGTH).optional()
919
+ )
493
920
  });
494
- var dbGetQuerySchema = z.object({
495
- key: trimmedString,
921
+ var dbObjectQuerySchema = z.strictObject({
922
+ key: objectKeySchema,
496
923
  bucket: trimmedString.optional()
497
924
  });
925
+ var dbUpdateObjectBodySchema = z.strictObject({
926
+ key: objectKeySchema,
927
+ bucket: trimmedString.optional(),
928
+ filename: fileNameSchema.optional(),
929
+ folder: z.string().nullable().optional(),
930
+ /** `null` clears the attributes. */
931
+ attributes: z.record(z.string(), z.unknown()).nullable().optional()
932
+ });
933
+ var dbFolderInputSchema = z.strictObject({
934
+ path: z.string()
935
+ });
498
936
 
499
937
  // src/plugin/to-db-client-object.ts
500
- function toDbClientObject(object2) {
938
+ function toDbClientObject(object2, toClient) {
939
+ const { attributes } = object2;
501
940
  return {
502
941
  id: object2.id,
503
942
  bucket: object2.bucket,
504
943
  key: object2.key,
505
944
  route: object2.route,
945
+ folder: object2.folder,
506
946
  filename: object2.filename,
507
947
  contentType: object2.contentType,
508
948
  size: object2.size,
509
949
  declaredSize: object2.declaredSize,
950
+ // Purging rows never reach the browser: listings and GET /db/object
951
+ // leave them out.
510
952
  status: object2.status,
511
- createdAt: object2.createdAt.toISOString()
953
+ attributes: attributes === null ? null : toClient ? toClient(attributes) : (
954
+ // Without `toClient`, the client attributes are the stored ones.
955
+ attributes
956
+ ),
957
+ createdAt: object2.createdAt.toISOString(),
958
+ updatedAt: object2.updatedAt.toISOString()
959
+ };
960
+ }
961
+ function toDbClientFolder(folder) {
962
+ return {
963
+ path: folder.path,
964
+ name: folder.name,
965
+ parent: folder.parent,
966
+ createdAt: folder.createdAt.toISOString()
512
967
  };
513
968
  }
514
969
 
515
970
  // src/plugin/create-database-endpoints.ts
971
+ var DEFAULT_ATTRIBUTES_MAX_BYTES = 16384;
972
+ var utf8 = new TextEncoder();
973
+ function resolveBucket(config, bucket, field) {
974
+ const buckets = /* @__PURE__ */ new Set();
975
+ for (const route of Object.values(config.routes)) {
976
+ const routeBucket = route.bucket ?? config.bucket;
977
+ if (routeBucket) buckets.add(routeBucket);
978
+ }
979
+ if (bucket === void 0) {
980
+ const [only] = buckets;
981
+ if (only !== void 0 && buckets.size === 1) return only;
982
+ throw s3Error("VALIDATION_ERROR", {
983
+ issue: `[${field}] Required when the routes use several buckets`
984
+ });
985
+ }
986
+ if (!buckets.has(bucket)) {
987
+ throw s3Error("VALIDATION_ERROR", {
988
+ issue: `[${field}] No route uses this bucket`
989
+ });
990
+ }
991
+ return bucket;
992
+ }
993
+ function matchingFolders(children, search) {
994
+ if (!search) return children;
995
+ const text = search.toLowerCase();
996
+ return children.filter((child) => child.name.toLowerCase().includes(text));
997
+ }
998
+ function parseFolderPath(value) {
999
+ const path = parseFolder(value);
1000
+ if (path === null) throw s3Error("FOLDER_INVALID");
1001
+ return path;
1002
+ }
516
1003
  function createDatabaseEndpoints(options) {
1004
+ const { access, quota, toClientObject } = options;
1005
+ const { objects, folders } = options.store;
1006
+ const parseAttributes = async (value) => {
1007
+ const { attributes } = options;
1008
+ if (!attributes) {
1009
+ throw s3Error("VALIDATION_ERROR", {
1010
+ issue: "[body.attributes] Attributes are not enabled"
1011
+ });
1012
+ }
1013
+ const result = await validateStandardSchema(attributes.schema, value);
1014
+ if (result.issues) {
1015
+ throw s3Error("VALIDATION_ERROR", {
1016
+ issue: `[body.attributes] ${formatStandardIssue(result.issues)}`
1017
+ });
1018
+ }
1019
+ const maxBytes = attributes.maxBytes ?? DEFAULT_ATTRIBUTES_MAX_BYTES;
1020
+ const size = utf8.encode(JSON.stringify(result.value)).byteLength;
1021
+ if (size > maxBytes) {
1022
+ throw s3Error("VALIDATION_ERROR", {
1023
+ issue: `[body.attributes] Exceeds ${maxBytes} bytes`
1024
+ });
1025
+ }
1026
+ return result.value;
1027
+ };
517
1028
  return {
518
- /** List objects + usage for the request scope (`resolveScope`). */
1029
+ /** One page of the caller's objects. `folder` omitted lists every folder. */
519
1030
  objects: createS3Endpoint(
520
1031
  pluginPath("db", "objects"),
521
1032
  { method: "GET", query: dbListQuerySchema },
522
1033
  async (ctx) => {
523
- const {
524
- status,
525
- limit = DB_LIST_DEFAULT_LIMIT,
526
- offset,
527
- cursor,
528
- route,
529
- contentType,
530
- prefix
531
- } = ctx.query ?? {};
532
- const scope = await options.resolveScope(ctx.context.request, {
1034
+ const { folder: wireFolder, ...filters } = ctx.query;
1035
+ const folder = wireFolder === void 0 ? void 0 : parseFolder(wireFolder);
1036
+ const scope = await access.scope(ctx.context, {
533
1037
  operation: "list",
534
- route
1038
+ route: filters.route,
1039
+ ...folder !== void 0 && { folder }
1040
+ });
1041
+ const page = await objects.list({
1042
+ ...filters,
1043
+ scope: scope.scope,
1044
+ folder
535
1045
  });
536
- if (scope === null) throw unauthorized();
537
- const fetchLimit = limit + 1;
538
- const [rows, usage] = await Promise.all([
539
- options.objects.listByScope({
540
- scope,
541
- status: status ?? "active",
542
- limit: fetchLimit,
543
- offset,
544
- cursor,
545
- route,
546
- contentType,
547
- prefix
548
- }),
549
- options.objects.getScopeUsage(scope)
1046
+ const response = {
1047
+ objects: page.objects.map((object2) => toClientObject(object2)),
1048
+ nextCursor: page.nextCursor
1049
+ };
1050
+ if (!page.firstPage) return response;
1051
+ const [usage, limits, children] = await Promise.all([
1052
+ objects.getUsage(scope.scope),
1053
+ quota.limits(scope),
1054
+ folder === void 0 ? void 0 : folders.children(scope.scope, folder)
550
1055
  ]);
551
- const hasMore = rows.length > limit;
552
- const page = hasMore ? rows.slice(0, limit) : rows;
553
- const last = page.at(-1);
554
- const nextCursor = hasMore && last ? encodeListCursor(last.createdAt, last.id) : null;
555
1056
  return {
556
- scope,
1057
+ ...response,
557
1058
  usage,
558
- objects: page.map(toDbClientObject),
559
- nextCursor
1059
+ limits,
1060
+ ...children && {
1061
+ folders: matchingFolders(children, filters.search).map(
1062
+ toDbClientFolder
1063
+ )
1064
+ }
560
1065
  };
561
1066
  }
562
1067
  ),
1068
+ /** One object of the caller's scope. */
563
1069
  object: createS3Endpoint(
564
1070
  pluginPath("db", "object"),
565
- { method: "GET", query: dbGetQuerySchema },
1071
+ { method: "GET", query: dbObjectQuerySchema },
566
1072
  async (ctx) => {
567
- const scope = await options.resolveScope(ctx.context.request, {
568
- operation: "list",
569
- key: ctx.query.key
1073
+ const { key } = ctx.query;
1074
+ const bucket = resolveBucket(
1075
+ ctx.context.config,
1076
+ ctx.query.bucket,
1077
+ "query.bucket"
1078
+ );
1079
+ const { object: object2 } = await access.object(
1080
+ ctx.context,
1081
+ { operation: "read", key },
1082
+ { bucket, key }
1083
+ );
1084
+ if (object2.status === "purging") throw s3Error("OBJECT_NOT_FOUND");
1085
+ return { object: toClientObject(object2) };
1086
+ }
1087
+ ),
1088
+ /**
1089
+ * Renames, moves, or sets the attributes of an active object. A move
1090
+ * checks the target folder like an upload: validated, authorized, and
1091
+ * created when missing.
1092
+ */
1093
+ updateObject: createS3Endpoint(
1094
+ pluginPath("db", "object"),
1095
+ { method: "PATCH", body: dbUpdateObjectBodySchema },
1096
+ async (ctx) => {
1097
+ const { key, filename } = ctx.body;
1098
+ const bucket = resolveBucket(
1099
+ ctx.context.config,
1100
+ ctx.body.bucket,
1101
+ "body.bucket"
1102
+ );
1103
+ const folder = ctx.body.folder === void 0 ? void 0 : parseFolder(ctx.body.folder ?? "");
1104
+ const { scope, object: object2 } = await access.object(
1105
+ ctx.context,
1106
+ {
1107
+ operation: "update",
1108
+ key,
1109
+ ...folder !== void 0 && { folder }
1110
+ },
1111
+ { bucket, key }
1112
+ );
1113
+ if (object2.status !== "active") throw s3Error("OBJECT_NOT_FOUND");
1114
+ const attributes = ctx.body.attributes === void 0 || ctx.body.attributes === null ? ctx.body.attributes : await parseAttributes(ctx.body.attributes);
1115
+ if (filename === void 0 && folder === void 0 && attributes === void 0) {
1116
+ return { object: toClientObject(object2) };
1117
+ }
1118
+ const updated = await options.objects.update(
1119
+ { bucket, key, id: object2.id, scope: scope.scope },
1120
+ { filename, folder, attributes }
1121
+ );
1122
+ if (!updated) throw s3Error("OBJECT_NOT_FOUND");
1123
+ return { object: toClientObject(updated) };
1124
+ }
1125
+ ),
1126
+ /** Creates a folder and its missing ancestors. */
1127
+ createFolder: createS3Endpoint(
1128
+ pluginPath("db", "folder"),
1129
+ { method: "POST", body: dbFolderInputSchema },
1130
+ async (ctx) => {
1131
+ const path = parseFolderPath(ctx.body.path);
1132
+ const scope = await access.scope(ctx.context, {
1133
+ operation: "update",
1134
+ folder: path
570
1135
  });
571
- if (scope === null) throw unauthorized();
572
- const found = await options.objects.findByScopeKey({
573
- scope,
574
- key: ctx.query.key,
575
- bucket: ctx.query.bucket
1136
+ const folder = await folders.create(scope.scope, path);
1137
+ return { folder: toDbClientFolder(folder) };
1138
+ }
1139
+ ),
1140
+ /** Removes an empty folder. A missing folder is not an error. */
1141
+ deleteFolder: createS3Endpoint(
1142
+ pluginPath("db", "folder"),
1143
+ { method: "DELETE", query: dbFolderInputSchema },
1144
+ async (ctx) => {
1145
+ const path = parseFolderPath(ctx.query.path);
1146
+ const scope = await access.scope(ctx.context, {
1147
+ operation: "update",
1148
+ folder: path
576
1149
  });
577
- if (!found || found.status === "deleted") throw notFound();
578
- return { object: toDbClientObject(found) };
1150
+ await folders.delete(scope.scope, path);
1151
+ return { path };
579
1152
  }
580
1153
  )
581
1154
  };
582
1155
  }
583
-
584
- // src/plugin/memoize-scope-resolver.ts
585
- function memoizeScopeResolver(resolve) {
586
- const cache = /* @__PURE__ */ new WeakMap();
587
- return (request, context) => {
588
- let byContext = cache.get(request);
589
- if (!byContext) {
590
- byContext = /* @__PURE__ */ new Map();
591
- cache.set(request, byContext);
592
- }
593
- const id = JSON.stringify([
594
- context.operation,
595
- context.route ?? null,
596
- context.key ?? null
597
- ]);
598
- let scope = byContext.get(id);
599
- if (scope === void 0) {
600
- scope = resolve(request, context);
601
- byContext.set(id, scope);
602
- }
603
- return scope;
1156
+ var TICKET_OPERATIONS = {
1157
+ upload: "upload",
1158
+ "multipart.init": "upload",
1159
+ confirm: "confirm",
1160
+ "multipart.signPart": "multipart",
1161
+ "multipart.listParts": "multipart",
1162
+ "multipart.complete": "multipart",
1163
+ "multipart.abort": "multipart"
1164
+ };
1165
+ function refOf(context) {
1166
+ return { bucket: context.bucket, key: context.key };
1167
+ }
1168
+ function objectTarget(context, operation) {
1169
+ return { operation, route: context.route, key: context.key };
1170
+ }
1171
+ function uploadTarget(context) {
1172
+ return {
1173
+ ...objectTarget(context, "upload"),
1174
+ folder: parseFolder(context.clientMetadata?.folder ?? ""),
1175
+ clientMetadata: context.clientMetadata
604
1176
  };
605
1177
  }
606
-
607
- // src/plugin/create-database-lifecycle-hooks.ts
608
- var DEFAULT_PENDING_TTL_MS = 24 * 60 * 60 * 1e3;
1178
+ function writtenBy(object2, uploadToken) {
1179
+ return object2 !== null && object2.uploadToken === uploadToken;
1180
+ }
1181
+ function confirmedBy(object2, uploadToken, eTag) {
1182
+ if (object2?.status !== "active") return false;
1183
+ if (object2.uploadToken === uploadToken && !isUploading(object2)) return true;
1184
+ return eTag !== void 0 && object2.confirmedAt !== null && object2.eTag === eTag;
1185
+ }
609
1186
  function createDatabaseLifecycleHooks(options) {
610
- const objects = resolveStore(options.client);
611
- const pendingTtlMs = options.pendingTtlMs ?? DEFAULT_PENDING_TTL_MS;
1187
+ const { objects, folders } = options.store;
1188
+ const { access, quota, resolveScope, routes } = options;
612
1189
  const deleteMode = options.deleteMode ?? "soft";
613
- const resolveScope = memoizeScopeResolver(options.resolveScope);
614
- async function requireScope(request, context) {
615
- const scope = await resolveScope(request, context);
616
- if (scope === null) throw unauthorized();
617
- return scope;
618
- }
619
- function uploadScope(context) {
620
- return requireScope(context.request, {
621
- operation: "upload",
622
- route: context.route,
623
- key: context.key,
624
- clientMetadata: context.clientMetadata
625
- });
626
- }
627
- async function guardKeyOwnership(context) {
628
- const scope = await uploadScope(context);
629
- const existing = await objects.find({
630
- bucket: context.bucket,
631
- key: context.key
632
- });
633
- if (existing && existing.status !== "deleted" && existing.scope !== scope) {
634
- throw forbidden("Key belongs to another scope");
635
- }
636
- }
637
- async function trackPending(context) {
638
- const scope = await uploadScope(context);
639
- if (context.replace === "overwrite") {
640
- const existing = await objects.find({
641
- bucket: context.bucket,
642
- key: context.key
643
- });
644
- if (existing?.status === "active" && existing.scope === scope) {
645
- return;
646
- }
1190
+ const removeRow = (ref) => deleteMode === "hard" ? objects.hardDelete(ref) : objects.softDelete(ref);
1191
+ const assertUploadRow = async (context, operation, replay) => {
1192
+ const { object: object2 } = await access.object(
1193
+ context,
1194
+ objectTarget(context, operation),
1195
+ refOf(context)
1196
+ );
1197
+ const { uploadToken } = context;
1198
+ const live = writtenBy(object2, uploadToken) && isUploading(object2) || replay !== void 0 && confirmedBy(object2, uploadToken, replay.eTag);
1199
+ if (!live) throw s3Error("OBJECT_NOT_FOUND");
1200
+ };
1201
+ const uploadWrite = async (context) => {
1202
+ const target = uploadTarget(context);
1203
+ const existing = await objects.find(refOf(context));
1204
+ if (existing?.status !== "active" || routes.replace(context.route) !== "overwrite") {
1205
+ return { target, existing, replaced: null };
647
1206
  }
648
- await objects.upsertPending({
649
- scope,
650
- bucket: context.bucket,
651
- key: context.key,
652
- route: context.route,
653
- contentType: context.file.type ?? null,
654
- declaredSize: context.file.size ?? null,
655
- metadata: context.metadata ?? null,
656
- acl: context.acl ?? null,
657
- filename: context.file.name,
658
- uploadId: context.uploadId ?? null,
659
- expiresAt: new Date(Date.now() + pendingTtlMs)
660
- });
661
- }
662
- async function trackConfirmed(context) {
663
- await objects.markActive({
664
- bucket: context.bucket,
665
- key: context.key,
666
- size: context.contentLength,
667
- eTag: context.eTag ?? null,
668
- contentType: context.contentType,
669
- acl: context.acl,
670
- filename: context.fileName,
671
- metadata: context.metadata
672
- });
673
- }
674
- async function abortPendingMultipart(context) {
675
- const row = await objects.find({
676
- bucket: context.bucket,
677
- key: context.key
678
- });
679
- if (row?.status !== "pending" || !row.uploadId) return;
680
- const s3 = options.s3Client?.(context.route);
681
- if (!s3) return;
1207
+ return {
1208
+ target: { ...target, folder: existing.folder },
1209
+ existing,
1210
+ replaced: existing
1211
+ };
1212
+ };
1213
+ const abortPendingMultipart = async (context, row) => {
1214
+ const client = routes.client(context.route);
1215
+ if (!row || !isUploading(row) || !row.uploadId || !client) return;
682
1216
  try {
683
- await s3.send(
1217
+ await client.send(
684
1218
  new AbortMultipartUploadCommand({
685
1219
  Bucket: context.bucket,
686
1220
  Key: context.key,
@@ -689,155 +1223,453 @@ function createDatabaseLifecycleHooks(options) {
689
1223
  );
690
1224
  } catch {
691
1225
  }
692
- }
693
- const guardOptions = { client: objects, resolveScope };
694
- const pendingObjectGuard = createObjectAccessGuard({
695
- ...guardOptions,
696
- operation: "confirm",
697
- requireStatus: "pending"
698
- });
699
- const ownedObjectConfirmGuard = createObjectAccessGuard({
700
- ...guardOptions,
701
- operation: "confirm",
702
- requireStatus: "any"
703
- });
704
- const multipartSessionGuard = createObjectAccessGuard({
705
- ...guardOptions,
706
- operation: "multipart",
707
- requireStatus: "pending",
708
- requireUploadId: true
709
- });
710
- const activeObjectGuard = createObjectAccessGuard({
711
- ...guardOptions,
712
- operation: "download",
713
- requireStatus: "active"
714
- });
715
- const ownedObjectDeleteGuard = createObjectAccessGuard({
716
- ...guardOptions,
717
- operation: "delete",
718
- requireStatus: "any"
719
- });
1226
+ };
720
1227
  return {
721
- objects,
722
- resolveScope,
723
- hooks: {
724
- upload: {
725
- guard: guardKeyOwnership,
726
- onPresigned: trackPending,
727
- confirmGuard: async (context) => {
728
- if (context.replace === "overwrite") {
729
- await ownedObjectConfirmGuard(context);
1228
+ async ticketSubject(context) {
1229
+ const operation = TICKET_OPERATIONS[context.operation];
1230
+ if (!operation || !routes.tracked(context.route)) return void 0;
1231
+ const { scope } = await resolveScope(context, {
1232
+ operation,
1233
+ route: context.route
1234
+ });
1235
+ return scope;
1236
+ },
1237
+ upload: {
1238
+ async guard(context) {
1239
+ const { target, existing, replaced } = await uploadWrite(context);
1240
+ let scope;
1241
+ if (replaced) {
1242
+ scope = await resolveScope(context, target);
1243
+ if (replaced.scope !== scope.scope) throw s3Error("FORBIDDEN");
1244
+ await access.assertOwner(scope, replaced, target);
1245
+ } else {
1246
+ scope = await access.scope(context, target);
1247
+ if (existing && existing.status !== "deleted") {
1248
+ if (existing.scope !== scope.scope) throw s3Error("FORBIDDEN");
1249
+ if (existing.status === "purging") throw s3Error("CONFLICT");
1250
+ if (existing.status === "active") throw s3Error("OBJECT_EXISTS");
1251
+ }
1252
+ }
1253
+ await quota.assert(scope, {
1254
+ size: context.file.size,
1255
+ excluding: refOf(context)
1256
+ });
1257
+ },
1258
+ async onStart(context) {
1259
+ const { target } = await uploadWrite(context);
1260
+ const { scope } = await resolveScope(context, target);
1261
+ const row = await objects.upsertPending({
1262
+ ...refOf(context),
1263
+ scope,
1264
+ route: context.route,
1265
+ folder: target.folder ?? null,
1266
+ filename: context.file.name,
1267
+ contentType: context.file.type ?? null,
1268
+ declaredSize: context.file.size,
1269
+ acl: context.acl,
1270
+ uploadId: context.uploadId ?? null,
1271
+ uploadToken: context.uploadToken,
1272
+ expiresAt: context.expiresAt,
1273
+ overwrite: routes.replace(context.route) === "overwrite"
1274
+ });
1275
+ await folders.ensure(scope, row.folder);
1276
+ },
1277
+ async confirmGuard(context) {
1278
+ await assertUploadRow(context, "confirm", {
1279
+ eTag: context.object.eTag
1280
+ });
1281
+ },
1282
+ async onConfirmed(context) {
1283
+ const ref = refOf(context);
1284
+ const row = await objects.find(ref);
1285
+ if (confirmedBy(row, context.uploadToken, context.object.eTag)) return;
1286
+ const scope = await resolveScope(
1287
+ context,
1288
+ objectTarget(context, "confirm")
1289
+ );
1290
+ try {
1291
+ await quota.assert(scope, {
1292
+ size: context.object.size,
1293
+ excluding: ref
1294
+ });
1295
+ } catch (error) {
1296
+ await objects.discardUpload({
1297
+ ...ref,
1298
+ uploadToken: context.uploadToken
1299
+ });
1300
+ throw error;
1301
+ }
1302
+ await objects.markActive({
1303
+ ...ref,
1304
+ uploadToken: context.uploadToken,
1305
+ size: context.object.size,
1306
+ eTag: context.object.eTag ?? null,
1307
+ contentType: context.object.type,
1308
+ filename: context.object.name,
1309
+ s3Metadata: context.object.metadata,
1310
+ acl: context.acl
1311
+ });
1312
+ if (context.previousKey !== void 0) {
1313
+ const previous = { bucket: context.bucket, key: context.previousKey };
1314
+ const old = await objects.find(previous);
1315
+ if (old?.status === "active" && old.scope === scope.scope) {
1316
+ await removeRow(previous);
1317
+ }
1318
+ }
1319
+ },
1320
+ multipart: {
1321
+ async guard(context) {
1322
+ if (context.action !== "abort") {
1323
+ await assertUploadRow(
1324
+ context,
1325
+ "multipart",
1326
+ // Complete is retried after it finished; the confirm step then
1327
+ // checks the ETag of what it finished.
1328
+ context.action === "complete" ? {} : void 0
1329
+ );
730
1330
  return;
731
1331
  }
732
- await pendingObjectGuard(context);
1332
+ const row = await objects.find(refOf(context));
1333
+ if (!writtenBy(row, context.uploadToken)) return;
1334
+ const target = objectTarget(context, "multipart");
1335
+ const scope = await resolveScope(context, target);
1336
+ await access.assertOwner(scope, row, target);
733
1337
  },
734
- onConfirmed: trackConfirmed,
735
- multipart: {
736
- onInit: trackPending,
737
- guard: multipartSessionGuard,
738
- onAbort: async (context) => {
739
- await objects.deletePending({
740
- bucket: context.bucket,
741
- key: context.key,
742
- uploadId: context.uploadId
743
- });
744
- }
1338
+ async onAbort(context) {
1339
+ await objects.cancelUpload({
1340
+ ...refOf(context),
1341
+ uploadToken: context.uploadToken
1342
+ });
745
1343
  }
1344
+ }
1345
+ },
1346
+ download: {
1347
+ async guard(context) {
1348
+ const { object: object2 } = await access.object(
1349
+ context,
1350
+ objectTarget(context, "download"),
1351
+ refOf(context)
1352
+ );
1353
+ if (object2.status !== "active") throw s3Error("OBJECT_NOT_FOUND");
1354
+ }
1355
+ },
1356
+ delete: {
1357
+ async guard(context) {
1358
+ await access.object(
1359
+ context,
1360
+ objectTarget(context, "delete"),
1361
+ refOf(context),
1362
+ { allowDeleted: true }
1363
+ );
746
1364
  },
747
- download: {
748
- guard: activeObjectGuard
1365
+ async onDeleted(context) {
1366
+ const ref = refOf(context);
1367
+ await abortPendingMultipart(context, await objects.find(ref));
1368
+ await removeRow(ref);
1369
+ }
1370
+ }
1371
+ };
1372
+ }
1373
+ function createObjectAccess(options) {
1374
+ const authorize = async (scope, target, object2) => {
1375
+ if (!options.authorize) return;
1376
+ const allowed = await options.authorize({
1377
+ ...scope,
1378
+ ...target?.key !== void 0 && { key: target.key },
1379
+ ...target?.folder !== void 0 && { folder: target.folder },
1380
+ ...target?.clientMetadata !== void 0 && {
1381
+ clientMetadata: target.clientMetadata
749
1382
  },
750
- delete: {
751
- guard: ownedObjectDeleteGuard,
752
- onDeleted: async (context) => {
753
- if (!context.existed) await abortPendingMultipart(context);
754
- const ref = { bucket: context.bucket, key: context.key };
755
- if (deleteMode === "hard") await objects.hardDelete(ref);
756
- else await objects.softDelete(ref);
1383
+ ...object2 !== void 0 && { object: object2 }
1384
+ });
1385
+ if (!allowed) throw s3Error("FORBIDDEN");
1386
+ };
1387
+ const assertOwner = async (scope, object2, target) => {
1388
+ if (object2.scope !== scope.scope) throw s3Error("OBJECT_NOT_FOUND");
1389
+ await authorize(scope, target ?? { operation: scope.operation }, object2);
1390
+ };
1391
+ return {
1392
+ async scope(call, target) {
1393
+ const scope = await options.resolveScope(call, target);
1394
+ await authorize(scope, target);
1395
+ return scope;
1396
+ },
1397
+ async object(call, target, ref, { allowDeleted = false } = {}) {
1398
+ const scope = await options.resolveScope(call, target);
1399
+ const object2 = await options.objects.find(ref);
1400
+ if (!object2 || object2.status === "deleted" && !allowDeleted) {
1401
+ throw s3Error("OBJECT_NOT_FOUND");
1402
+ }
1403
+ await assertOwner(scope, object2, target);
1404
+ return { scope, object: object2 };
1405
+ },
1406
+ assertOwner
1407
+ };
1408
+ }
1409
+ function folderPath(path) {
1410
+ const normalized = normalizeFolderPath(path);
1411
+ if (normalized === null) throw s3Error("FOLDER_INVALID");
1412
+ return normalized;
1413
+ }
1414
+ function createPublicStores(store) {
1415
+ const { objects, folders } = store;
1416
+ return {
1417
+ objects: {
1418
+ find: objects.find,
1419
+ list: objects.list,
1420
+ count: objects.count,
1421
+ getUsage: objects.getUsage,
1422
+ async update(ref, input) {
1423
+ const folder = input.folder === void 0 || input.folder === null ? input.folder : folderPath(input.folder);
1424
+ const row = await objects.update(ref, { ...input, folder });
1425
+ if (row && folder !== void 0) {
1426
+ await folders.ensure(row.scope, row.folder);
1427
+ }
1428
+ return row;
1429
+ }
1430
+ },
1431
+ // Async, so an invalid path is a rejection like any other failure.
1432
+ folders: {
1433
+ find: async (scope, path) => folders.find(scope, folderPath(path)),
1434
+ children: async (scope, parent, options) => folders.children(
1435
+ scope,
1436
+ parent === null ? null : folderPath(parent),
1437
+ options
1438
+ ),
1439
+ ensure: async (scope, path) => folders.ensure(scope, path === null ? null : folderPath(path)),
1440
+ create: async (scope, path) => folders.create(scope, folderPath(path)),
1441
+ delete: async (scope, path) => folders.delete(scope, folderPath(path))
1442
+ }
1443
+ };
1444
+ }
1445
+ async function abortUpload(client, row, uploadId) {
1446
+ try {
1447
+ await client.send(
1448
+ new AbortMultipartUploadCommand({
1449
+ Bucket: row.bucket,
1450
+ Key: row.key,
1451
+ UploadId: uploadId
1452
+ })
1453
+ );
1454
+ } catch (error) {
1455
+ if (!isAwsNotFound(error)) throw error;
1456
+ }
1457
+ }
1458
+ async function headObject(client, row) {
1459
+ try {
1460
+ return await client.send(
1461
+ new HeadObjectCommand({ Bucket: row.bucket, Key: row.key })
1462
+ );
1463
+ } catch (error) {
1464
+ if (isAwsNotFound(error)) return null;
1465
+ throw error;
1466
+ }
1467
+ }
1468
+ async function deleteUploadedObject(client, row) {
1469
+ const head = await headObject(client, row);
1470
+ if (!head) return "empty";
1471
+ if (readUploadMarker(head.Metadata) !== uploadTokenOf(row)) return "kept";
1472
+ const deleted = await deleteObjectIfMatch(client, {
1473
+ bucket: row.bucket,
1474
+ key: row.key,
1475
+ eTag: head.ETag
1476
+ });
1477
+ return deleted ? "empty" : "kept";
1478
+ }
1479
+ function createPurge(options) {
1480
+ const { objects } = options;
1481
+ const purgeRow = async (row) => {
1482
+ const client = options.client(row.route);
1483
+ if (!client) {
1484
+ throw new Error(
1485
+ `No S3 client for route "${row.route}". Keep the route in dimahS3({ routes }) until its rows are purged.`
1486
+ );
1487
+ }
1488
+ if (row.uploadId) await abortUpload(client, row, row.uploadId);
1489
+ const key = await deleteUploadedObject(client, row);
1490
+ if (row.status === "active" && row.uploadToken !== null) {
1491
+ const upload = {
1492
+ bucket: row.bucket,
1493
+ key: row.key,
1494
+ uploadToken: row.uploadToken
1495
+ };
1496
+ if (key === "empty") await objects.discardUpload(upload);
1497
+ else await objects.cancelUpload(upload);
1498
+ return;
1499
+ }
1500
+ await objects.deletePurged(row.id);
1501
+ };
1502
+ return {
1503
+ async purgeStale({ limit = DEFAULT_STALE_LIMIT } = {}) {
1504
+ const result = {
1505
+ purged: 0,
1506
+ failed: 0,
1507
+ skipped: 0,
1508
+ more: false
1509
+ };
1510
+ const stale = await objects.findStale({ limit });
1511
+ result.more = stale.length >= limit;
1512
+ for (const candidate of stale) {
1513
+ const row = await objects.claimStale(candidate.id);
1514
+ if (!row) {
1515
+ result.skipped += 1;
1516
+ continue;
1517
+ }
1518
+ try {
1519
+ await purgeRow(row);
1520
+ result.purged += 1;
1521
+ } catch (error) {
1522
+ result.failed += 1;
1523
+ options.logger()?.warn?.(
1524
+ `[dimah-s3/db] Could not purge ${row.bucket}/${row.key}; a run after its claim lapses retries it.`,
1525
+ error
1526
+ );
757
1527
  }
758
1528
  }
1529
+ return result;
1530
+ },
1531
+ async pruneDeleted({ olderThan }) {
1532
+ return { pruned: await objects.pruneDeleted(new Date(olderThan)) };
1533
+ }
1534
+ };
1535
+ }
1536
+ function createQuota(objects, option) {
1537
+ const limits = async (scope) => (typeof option === "function" ? await option(scope) : option) ?? null;
1538
+ return {
1539
+ limits,
1540
+ async assert(scope, input) {
1541
+ const caps = await limits(scope);
1542
+ if (!caps || caps.maxFiles === void 0 && caps.maxBytes === void 0) {
1543
+ return;
1544
+ }
1545
+ const usage = await objects.getUsage(scope.scope, {
1546
+ excluding: input.excluding
1547
+ });
1548
+ if (caps.maxFiles !== void 0 && usage.objectCount >= caps.maxFiles) {
1549
+ throw s3Error("QUOTA_EXCEEDED", { limit: "files" });
1550
+ }
1551
+ if (caps.maxBytes !== void 0 && usage.totalBytes + input.size > caps.maxBytes) {
1552
+ throw s3Error("QUOTA_EXCEEDED", { limit: "bytes" });
1553
+ }
1554
+ }
1555
+ };
1556
+ }
1557
+ function perKey(cache, key, create) {
1558
+ let value = cache.get(key);
1559
+ if (value === void 0) {
1560
+ value = create();
1561
+ cache.set(key, value);
1562
+ }
1563
+ return value;
1564
+ }
1565
+ function createScopeResolver(options) {
1566
+ const sessions = /* @__PURE__ */ new WeakMap();
1567
+ const scopes = /* @__PURE__ */ new WeakMap();
1568
+ const getSession = (request) => {
1569
+ const { getSession: load } = options;
1570
+ if (!load) return Promise.resolve(void 0);
1571
+ return perKey(sessions, request, async () => load(request));
1572
+ };
1573
+ return async (call, target) => {
1574
+ const session = await getSession(call.request);
1575
+ const context = {
1576
+ request: call.request,
1577
+ locals: call.locals,
1578
+ session,
1579
+ operation: target.operation,
1580
+ ...target.route !== void 0 && { route: target.route }
1581
+ };
1582
+ const id = `${target.operation}:${target.route ?? ""}`;
1583
+ const byTarget = perKey(scopes, call.locals, () => /* @__PURE__ */ new Map());
1584
+ let scope = byTarget.get(id);
1585
+ if (scope === void 0) {
1586
+ scope = Promise.resolve(options.resolveScope(context));
1587
+ byTarget.set(id, scope);
759
1588
  }
1589
+ const resolved = await scope;
1590
+ if (resolved === null) throw s3Error("UNAUTHORIZED");
1591
+ return { ...context, scope: resolved };
760
1592
  };
761
1593
  }
762
1594
 
763
1595
  // src/plugin/db.ts
764
1596
  function db(options) {
765
- let config;
766
- const s3Client = (route) => config?.routes[route]?.client ?? config?.client;
767
- const { objects, hooks, resolveScope } = createDatabaseLifecycleHooks({
768
- ...options,
769
- s3Client
1597
+ let env;
1598
+ const routes = {
1599
+ tracked: (route) => env?.routeOptions(route) !== false,
1600
+ replace: (route) => {
1601
+ const routeOptions = env?.routeOptions(route);
1602
+ return routeOptions ? routeOptions.replace : void 0;
1603
+ },
1604
+ client: (route) => routeClient(env, route)
1605
+ };
1606
+ const store = createDbStore(options.client);
1607
+ const stores = createPublicStores(store);
1608
+ const resolveScope = createScopeResolver(options);
1609
+ const toClientObject = (object2) => toDbClientObject(object2, options.attributes?.toClient);
1610
+ const access = createObjectAccess({
1611
+ objects: store.objects,
1612
+ resolveScope,
1613
+ authorize: options.authorize
1614
+ });
1615
+ const quota = createQuota(store.objects, options.quota);
1616
+ const purge = createPurge({
1617
+ objects: store.objects,
1618
+ client: routes.client,
1619
+ logger: () => env?.logger
770
1620
  });
771
- if (options.quota && hooks.upload) {
772
- const quotaGuard = createQuotaGuard({
773
- client: objects,
774
- resolveScope,
775
- maxBytes: options.quota.maxBytes,
776
- maxFiles: options.quota.maxFiles
777
- });
778
- hooks.upload.guard = chainHooks(hooks.upload.guard, quotaGuard);
779
- }
780
1621
  return definePlugin({
781
1622
  id: "db",
782
- init(env) {
783
- config = env.config;
784
- const routes = Object.values(env.config.routes);
785
- const hasLifecycle = routes.some(
786
- (r) => isFeatureOn(r.upload) || isFeatureOn(r.download) || isFeatureOn(r.delete)
787
- );
788
- if (!hasLifecycle) {
789
- throw new Error(
790
- "[dimah-s3] db plugin requires at least one route with upload, download, or delete enabled."
791
- );
792
- }
1623
+ init(initEnv) {
1624
+ env = initEnv;
1625
+ warnUnguardedRoutes(initEnv);
793
1626
  },
794
- hooks,
795
- endpoints: createDatabaseEndpoints({ objects, resolveScope }),
1627
+ hooks: createDatabaseLifecycleHooks({
1628
+ store,
1629
+ resolveScope,
1630
+ access,
1631
+ quota,
1632
+ deleteMode: options.deleteMode,
1633
+ routes
1634
+ }),
1635
+ endpoints: createDatabaseEndpoints({
1636
+ store,
1637
+ objects: stores.objects,
1638
+ access,
1639
+ quota,
1640
+ attributes: options.attributes,
1641
+ toClientObject
1642
+ }),
796
1643
  context: {
797
- objects,
798
- client: options.client
1644
+ objects: stores.objects,
1645
+ folders: stores.folders,
1646
+ checkSchema: store.checkSchema,
1647
+ purgeStale: purge.purgeStale,
1648
+ pruneDeleted: purge.pruneDeleted
799
1649
  }
800
1650
  });
801
1651
  }
802
- var DEFAULT_OLDER_THAN_MS = 24 * 60 * 60 * 1e3;
803
- async function abortAndDelete(s3, objects) {
804
- for (const object2 of objects) {
805
- if (object2.uploadId) {
806
- try {
807
- await s3.send(
808
- new AbortMultipartUploadCommand({
809
- Bucket: object2.bucket,
810
- Key: object2.key,
811
- UploadId: object2.uploadId
812
- })
813
- );
814
- } catch {
815
- }
816
- }
817
- try {
818
- await s3.send(
819
- new DeleteObjectCommand({
820
- Bucket: object2.bucket,
821
- Key: object2.key
822
- })
823
- );
824
- } catch {
825
- }
826
- }
1652
+ function routeClient(env, route) {
1653
+ if (!env) return void 0;
1654
+ const { routes, client } = env.config;
1655
+ const config = Object.hasOwn(routes, route) ? routes[route] : void 0;
1656
+ return config?.client ?? client;
827
1657
  }
828
- async function purgeStalePendingObjects(options) {
829
- const store = resolveStore(options.client);
830
- const olderThan = new Date(
831
- Date.now() - (options.olderThanMs ?? DEFAULT_OLDER_THAN_MS)
832
- );
833
- const stale = await store.findStalePending({ olderThan });
834
- if (stale.length === 0) return { purged: [] };
835
- if (options.s3) await abortAndDelete(options.s3, stale);
836
- await options.onBeforePurge?.(stale);
837
- await store.deleteByIds(stale.map((object2) => object2.id));
838
- return { purged: stale };
1658
+ function warnUnguardedRoutes(env) {
1659
+ for (const [name, route] of Object.entries(env.config.routes)) {
1660
+ if (env.routeOptions(name) !== false || route.guard) continue;
1661
+ const unguarded = ["download", "delete"].filter((feature) => {
1662
+ const toggle = route[feature];
1663
+ if (!isFeatureOn(toggle)) return false;
1664
+ return typeof toggle !== "object" || !toggle.guard;
1665
+ });
1666
+ if (unguarded.length === 0) continue;
1667
+ env.logger.warn?.(
1668
+ `[dimah-s3/db] Route "${name}" sets plugins: { db: false } and has no guard on ${unguarded.join(" or ")}, so any caller can reach every object on it. Add a route or feature guard.`
1669
+ );
1670
+ }
839
1671
  }
840
1672
 
841
- export { DimahS3DB, conflict, createObjectAccessGuard, createQuotaGuard, createStorageObjectStore, db, forbidden, notFound, purgeStalePendingObjects, toPendingMultipartResume, unauthorized, v1 };
1673
+ export { DB_SCHEMA_VERSION, DimahS3DB, db, v2 };
842
1674
  //# sourceMappingURL=index.js.map
843
1675
  //# sourceMappingURL=index.js.map