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