@dimah-s3/db 2.0.0-beta.1 → 2.0.0-beta.11

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 (85) hide show
  1. package/README.md +19 -10
  2. package/dist/adapters/drizzle.d.ts +2 -3
  3. package/dist/adapters/drizzle.d.ts.map +1 -1
  4. package/dist/adapters/kysely.d.ts +1 -1
  5. package/dist/adapters/prisma.d.ts +1 -1
  6. package/dist/cli.d.ts +18 -13
  7. package/dist/cli.d.ts.map +1 -1
  8. package/dist/cli.js +57 -84
  9. package/dist/cli.js.map +1 -1
  10. package/dist/client/db-client.d.ts +1 -1
  11. package/dist/client/db-client.d.ts.map +1 -1
  12. package/dist/client/db-object-source.d.ts +15 -9
  13. package/dist/client/db-object-source.d.ts.map +1 -1
  14. package/dist/client/index.d.ts +3 -3
  15. package/dist/client/index.d.ts.map +1 -1
  16. package/dist/client/index.js +101 -21
  17. package/dist/client/index.js.map +1 -1
  18. package/dist/folder-path.d.ts +17 -4
  19. package/dist/folder-path.d.ts.map +1 -1
  20. package/dist/fuma-db.d.ts +23 -23
  21. package/dist/fuma-db.d.ts.map +1 -1
  22. package/dist/index.d.ts +10 -9
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +843 -268
  25. package/dist/index.js.map +1 -1
  26. package/dist/plugin/create-database-endpoints.d.ts +46 -22
  27. package/dist/plugin/create-database-endpoints.d.ts.map +1 -1
  28. package/dist/plugin/create-database-lifecycle-hooks.d.ts +4 -4
  29. package/dist/plugin/create-database-lifecycle-hooks.d.ts.map +1 -1
  30. package/dist/plugin/db.d.ts +10 -2
  31. package/dist/plugin/db.d.ts.map +1 -1
  32. package/dist/plugin/object-access.d.ts +2 -2
  33. package/dist/plugin/object-access.d.ts.map +1 -1
  34. package/dist/plugin/public-stores.d.ts +5 -8
  35. package/dist/plugin/public-stores.d.ts.map +1 -1
  36. package/dist/plugin/quota.d.ts +1 -1
  37. package/dist/plugin/record.d.ts +58 -0
  38. package/dist/plugin/record.d.ts.map +1 -0
  39. package/dist/plugin/schemas.d.ts +25 -11
  40. package/dist/plugin/schemas.d.ts.map +1 -1
  41. package/dist/plugin/scope.d.ts +2 -0
  42. package/dist/plugin/scope.d.ts.map +1 -1
  43. package/dist/plugin/to-db-client-object.d.ts.map +1 -1
  44. package/dist/schema/schema.d.ts +48 -49
  45. package/dist/schema/schema.d.ts.map +1 -1
  46. package/dist/store/create-db-store.d.ts +2 -2
  47. package/dist/store/create-db-store.d.ts.map +1 -1
  48. package/dist/store/folder-store.d.ts +73 -15
  49. package/dist/store/folder-store.d.ts.map +1 -1
  50. package/dist/store/list-cursor.d.ts +3 -1
  51. package/dist/store/list-cursor.d.ts.map +1 -1
  52. package/dist/store/map-row.d.ts +3 -9
  53. package/dist/store/map-row.d.ts.map +1 -1
  54. package/dist/store/object-store.d.ts +42 -10
  55. package/dist/store/object-store.d.ts.map +1 -1
  56. package/dist/store/search-name.d.ts +7 -0
  57. package/dist/store/search-name.d.ts.map +1 -0
  58. package/dist/types/client.d.ts +114 -23
  59. package/dist/types/client.d.ts.map +1 -1
  60. package/dist/types/plugin.d.ts +19 -7
  61. package/dist/types/plugin.d.ts.map +1 -1
  62. package/dist/types/storage-object.d.ts +8 -7
  63. package/dist/types/storage-object.d.ts.map +1 -1
  64. package/package.json +15 -33
  65. package/dist/drizzle/mysql.d.ts +0 -1025
  66. package/dist/drizzle/mysql.d.ts.map +0 -1
  67. package/dist/drizzle/mysql.js +0 -117
  68. package/dist/drizzle/mysql.js.map +0 -1
  69. package/dist/drizzle/pg.d.ts +0 -964
  70. package/dist/drizzle/pg.d.ts.map +0 -1
  71. package/dist/drizzle/pg.js +0 -98
  72. package/dist/drizzle/pg.js.map +0 -1
  73. package/dist/drizzle/shared.d.ts +0 -7
  74. package/dist/drizzle/shared.d.ts.map +0 -1
  75. package/dist/drizzle/sqlite.d.ts +0 -1024
  76. package/dist/drizzle/sqlite.d.ts.map +0 -1
  77. package/dist/drizzle/sqlite.js +0 -101
  78. package/dist/drizzle/sqlite.js.map +0 -1
  79. package/prisma/dimah-s3.prisma +0 -80
  80. package/sql/mysql.sql +0 -59
  81. package/sql/pg.sql +0 -53
  82. package/sql/sqlite.sql +0 -54
  83. package/src/drizzle/mysql.ts +0 -172
  84. package/src/drizzle/pg.ts +0 -140
  85. package/src/drizzle/sqlite.ts +0 -144
package/dist/index.js CHANGED
@@ -1,152 +1,123 @@
1
- import { fumadb } from 'fumadb';
1
+ import { fumadb, UniqueConstraintError } from 'fumadb';
2
2
  import { table, column, idColumn, schema } from 'fumadb/schema';
3
- import { definePlugin, isFeatureOn, createS3Endpoint, isAwsNotFound, readUploadMarker, deleteObjectIfMatch } from '@dimah-s3/server/plugins';
4
- import { s3Error, pluginPath, base64UrlDecode, base64UrlEncode } from '@dimah-s3/core';
3
+ import { definePlugin, isFeatureOn, createS3Endpoint, assertFileConstraints, readObjectMetadata, isAwsNotFound, readUploadMarker, deleteObjectIfMatch } from '@dimah-s3/server/plugins';
4
+ import { s3Error, pluginPath, normalizeObjectKey, normalizeSearchText, base64UrlDecode, base64UrlEncode, matchesSearchText } from '@dimah-s3/core';
5
5
  import { optionalContentTypeSchema, optionalTrimmedString, routeNameSchema, objectKeySchema, trimmedString, fileNameSchema } from '@dimah-s3/core/schema';
6
6
  import * as z from 'zod';
7
7
  import { AbortMultipartUploadCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
8
8
 
9
9
  // src/fuma-db.ts
10
- function byteCount(name) {
11
- return column(name, "bigint").nullable();
10
+ var BINARY = { mysql: "utf8mb4_bin" };
11
+ var TEXT = { mysql: "utf8mb4_unicode_ci" };
12
+ var SEARCH_NAME_MAX_LENGTH = 1024;
13
+ var LONG_PREFIX = 512;
14
+ var SCOPE_MAX_LENGTH = 255;
15
+ function string(name, length = 255) {
16
+ return column(name, `varchar(${length})`).collate(BINARY);
12
17
  }
13
18
  var storageObject = table("storage_object", {
14
- id: idColumn("id", "varchar(255)").defaultTo$("auto"),
19
+ id: idColumn("id", "varchar(255)").collate(BINARY).defaultTo$("auto"),
15
20
  /** Owner, for example `user:123` or `org:acme`. */
16
- scope: column("scope", "string"),
17
- bucket: column("bucket", "string"),
18
- key: column("key", "string"),
21
+ scope: string("scope", SCOPE_MAX_LENGTH),
22
+ bucket: string("bucket"),
23
+ key: string("key", 1024),
19
24
  /** Named dimah-s3 route that created the object. */
20
- route: column("route", "string"),
21
- /** Logical folder path. `null` is the root. */
22
- folder: column("folder", "string").nullable(),
23
- filename: column("filename", "string"),
24
- contentType: column("content_type", "string").nullable(),
25
+ route: string("route"),
26
+ /** Folder that holds the object. `null` is the root. */
27
+ folderId: string("folder_id").nullable(),
28
+ filename: column("filename", "varchar(255)").collate(TEXT),
29
+ /**
30
+ * `filename` folded for search: lowercase, without accents, with `%`,
31
+ * `_`, and `\` kept literal. Search matches it on every database the
32
+ * same way.
33
+ */
34
+ searchName: string("search_name", SEARCH_NAME_MAX_LENGTH),
35
+ contentType: string("content_type").nullable(),
25
36
  /** Verified size from HeadObject, set on confirm. */
26
- size: byteCount("size"),
37
+ size: column("size", "bigint").nullable(),
27
38
  /** Size the client declared before confirm. Quota counts it while pending. */
28
- declaredSize: byteCount("declared_size"),
29
- eTag: column("e_tag", "string").nullable(),
30
- status: column("status", "string"),
39
+ declaredSize: column("declared_size", "bigint").nullable(),
40
+ eTag: string("e_tag").nullable(),
41
+ status: string("status", 16),
31
42
  /** S3 user metadata of the verified object. */
32
43
  s3Metadata: column("s3_metadata", "json").nullable(),
33
44
  /** App attributes, validated by the plugin's `attributes.schema`. */
34
45
  attributes: column("attributes", "json").nullable(),
35
- acl: column("acl", "string").nullable(),
46
+ acl: string("acl").nullable(),
36
47
  /** Multipart upload ID while the row is pending. */
37
- uploadId: column("upload_id", "string").nullable(),
48
+ uploadId: column("upload_id", "string").collate(BINARY).nullable(),
38
49
  /** Token of the upload ticket that wrote the row. Confirm and purge match it. */
39
- uploadToken: column("upload_token", "string").nullable(),
50
+ uploadToken: string("upload_token", 64).nullable(),
40
51
  /** Confirm deadline of a pending row (the upload ticket's expiry). */
41
52
  expiresAt: column("expires_at", "timestamp").nullable(),
42
53
  confirmedAt: column("confirmed_at", "timestamp").nullable(),
43
54
  createdAt: column("created_at", "timestamp").defaultTo$("now"),
44
55
  updatedAt: column("updated_at", "timestamp").defaultTo$("now"),
45
56
  deletedAt: column("deleted_at", "timestamp").nullable()
46
- }).unique("storage_object_bucket_key_uk", ["bucket", "key"]);
57
+ }).unique("storage_object_bucket_key_uk", ["bucket", "key"], {
58
+ length: { key: LONG_PREFIX }
59
+ }).index("storage_object_scope_folder_idx", [
60
+ "scope",
61
+ "status",
62
+ "folderId",
63
+ "createdAt"
64
+ ]).index("storage_object_scope_updated_idx", ["scope", "status", "updatedAt"]).index("storage_object_scope_filename_idx", ["scope", "status", "filename"]).index("storage_object_scope_size_idx", ["scope", "status", "size"]).index("storage_object_status_expires_idx", ["status", "expiresAt"]);
47
65
  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(),
66
+ id: idColumn("id", "varchar(255)").collate(BINARY).defaultTo$("auto"),
67
+ scope: string("scope", SCOPE_MAX_LENGTH),
68
+ /** Id of the containing folder. `""` is the root. */
69
+ parentId: string("parent_id").defaultTo(""),
54
70
  /** 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
- });
71
+ name: string("name"),
72
+ /** `name` folded for search, like `storage_object.search_name`. */
73
+ searchName: string("search_name", SEARCH_NAME_MAX_LENGTH),
74
+ /** Full folder path. */
75
+ path: string("path", 1024),
76
+ createdAt: column("created_at", "timestamp").defaultTo$("now"),
77
+ updatedAt: column("updated_at", "timestamp").defaultTo$("now")
78
+ }).unique("storage_folder_scope_path_uk", ["scope", "path"], {
79
+ length: { path: LONG_PREFIX }
80
+ }).unique("storage_folder_scope_parent_name_uk", ["scope", "parentId", "name"]);
96
81
  var dbSchema = schema({
97
- version: "2.0.0",
82
+ version: "2.2.0",
98
83
  tables: {
99
84
  storageObject,
100
85
  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];
110
86
  }
111
87
  });
112
88
 
113
89
  // src/fuma-db.ts
114
90
  var DimahS3DB = fumadb({
115
91
  namespace: "dimah_s3",
116
- schemas: [predecessor, dbSchema]
92
+ schemas: [dbSchema]
117
93
  });
118
94
  var DB_SCHEMA_VERSION = dbSchema.version;
119
- var FOLDER_PATH_MAX_LENGTH = 1024;
95
+ var FOLDER_PATH_MAX_BYTES = 1024;
120
96
  var FOLDER_NAME_MAX_LENGTH = 255;
121
97
  var FOLDER_MAX_DEPTH = 32;
122
98
  var INVALID_CHARACTER = /[\p{Cc}\p{Cs}]/u;
99
+ var utf8 = new TextEncoder();
123
100
  function characterCount(value) {
124
101
  return Array.from(value).length;
125
102
  }
126
- function isValidName(name) {
103
+ function isFolderName(name) {
127
104
  const length = characterCount(name);
128
- return length >= 1 && length <= FOLDER_NAME_MAX_LENGTH && name !== "." && name !== ".." && !INVALID_CHARACTER.test(name);
105
+ return length >= 1 && length <= FOLDER_NAME_MAX_LENGTH && name !== "." && name !== ".." && !name.includes("/") && !INVALID_CHARACTER.test(name);
129
106
  }
130
107
  function normalizeFolderPath(path) {
131
108
  if (!path.isWellFormed()) return null;
132
109
  const normalized = path.normalize("NFC");
133
- if (characterCount(normalized) > FOLDER_PATH_MAX_LENGTH) return null;
110
+ if (utf8.encode(normalized).byteLength > FOLDER_PATH_MAX_BYTES) return null;
134
111
  const names = normalized.split("/");
135
112
  if (names.length > FOLDER_MAX_DEPTH) return null;
136
- return names.every(isValidName) ? normalized : null;
137
- }
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;
113
+ return names.every(isFolderName) ? normalized : null;
143
114
  }
144
- function parentFolderPath(path) {
145
- const index = path.lastIndexOf("/");
146
- return index === -1 ? null : path.slice(0, index);
115
+ function childFolderPath(parent, name) {
116
+ if (!isFolderName(name)) return null;
117
+ return normalizeFolderPath(joinFolderPath(parent, name));
147
118
  }
148
- function folderName(path) {
149
- return path.slice(path.lastIndexOf("/") + 1);
119
+ function joinFolderPath(parent, name) {
120
+ return parent === null ? name : `${parent}/${name}`;
150
121
  }
151
122
  function folderAncestors(path) {
152
123
  const segments = path.split("/");
@@ -159,8 +130,9 @@ var DEFAULT_LIST_SORT = {
159
130
  var decoder = new TextDecoder();
160
131
  function fingerprint(filters) {
161
132
  const text = JSON.stringify([
162
- filters.folder === void 0,
163
- filters.folder ?? null,
133
+ filters.folderId === void 0,
134
+ filters.folderId ?? null,
135
+ filters.recursive,
164
136
  filters.search ?? null,
165
137
  filters.status,
166
138
  filters.route ?? null,
@@ -223,7 +195,7 @@ function mapStorageObjectRow(row) {
223
195
  bucket: row.bucket,
224
196
  key: row.key,
225
197
  route: row.route,
226
- folder: row.folder,
198
+ folderId: row.folderId,
227
199
  filename: row.filename,
228
200
  contentType: row.contentType,
229
201
  size: toNumber(row.size),
@@ -242,6 +214,19 @@ function mapStorageObjectRow(row) {
242
214
  deletedAt: row.deletedAt
243
215
  };
244
216
  }
217
+ var LIKE_LITERALS = {
218
+ "%": "\uE000",
219
+ _: "\uE001",
220
+ "\\": "\uE002"
221
+ };
222
+ function toSearchName(value) {
223
+ const folded = normalizeSearchText(value).replaceAll(
224
+ /[%_\\]/g,
225
+ (char) => LIKE_LITERALS[char] ?? char
226
+ );
227
+ const characters = Array.from(folded);
228
+ return characters.length > SEARCH_NAME_MAX_LENGTH ? characters.slice(0, SEARCH_NAME_MAX_LENGTH).join("") : folded;
229
+ }
245
230
 
246
231
  // src/store/object-store.ts
247
232
  var DEFAULT_LIST_LIMIT = 50;
@@ -298,15 +283,17 @@ function claimDeadline() {
298
283
  );
299
284
  return new Date(lastClaimDeadline);
300
285
  }
301
- function filterConditions(b, filter, now) {
286
+ function filterConditions(b, filter, folders, now) {
302
287
  const status = filter.status ?? "active";
303
288
  const parts = [
304
289
  b("scope", "=", filter.scope),
305
290
  status === "pending" ? b.and(b("status", "=", "pending"), b("expiresAt", ">=", now)) : b("status", "=", status)
306
291
  ];
307
- if (filter.folder === null) parts.push(b.isNull("folder"));
308
- else if (filter.folder !== void 0) {
309
- parts.push(b("folder", "=", filter.folder));
292
+ if (folders === null) parts.push(b.isNull("folderId"));
293
+ else if (folders !== void 0) {
294
+ parts.push(
295
+ folders.length === 1 ? b("folderId", "=", folders[0] ?? "") : b("folderId", "in", [...folders])
296
+ );
310
297
  }
311
298
  if (filter.route) parts.push(b("route", "=", filter.route));
312
299
  if (filter.contentType?.endsWith("/*")) {
@@ -315,11 +302,24 @@ function filterConditions(b, filter, now) {
315
302
  } else if (filter.contentType) {
316
303
  parts.push(b("contentType", "=", filter.contentType));
317
304
  }
318
- if (filter.search) parts.push(b("filename", "contains", filter.search));
305
+ const needle = filter.search ? toSearchName(filter.search) : "";
306
+ if (needle) parts.push(b("searchName", "contains", needle));
319
307
  return parts;
320
308
  }
321
- function createObjectStore(getOrm) {
309
+ function toByteCount(value) {
310
+ return value == null ? null : BigInt(value);
311
+ }
312
+ function toColumnValue(value) {
313
+ return typeof value === "number" ? BigInt(value) : value;
314
+ }
315
+ function createObjectStore(getOrm, subtreeIds) {
322
316
  const map = mapStorageObjectRow;
317
+ const folderSet = async (filter) => {
318
+ const { folderId } = filter;
319
+ if (folderId == null) return filter.recursive ? void 0 : folderId;
320
+ if (!filter.recursive) return [folderId];
321
+ return [folderId, ...await subtreeIds(filter.scope, folderId)];
322
+ };
323
323
  const find = async (ref) => {
324
324
  const orm = await getOrm();
325
325
  const row = await orm.findFirst("storageObject", {
@@ -345,7 +345,8 @@ function createObjectStore(getOrm) {
345
345
  const after = direction === "desc" ? "<" : ">";
346
346
  const nullable = field === "size";
347
347
  const filters = {
348
- folder: input.folder,
348
+ folderId: input.folderId,
349
+ recursive: input.recursive === true && input.folderId !== void 0,
349
350
  search: input.search || void 0,
350
351
  status: input.status ?? "active",
351
352
  route: input.route || void 0,
@@ -353,8 +354,9 @@ function createObjectStore(getOrm) {
353
354
  sort
354
355
  };
355
356
  const cursor = input.cursor ? decodeListCursor(input.cursor, filters) : null;
357
+ const folders = await folderSet(input);
356
358
  const where = (segment) => (b) => {
357
- const parts = filterConditions(b, input, now);
359
+ const parts = filterConditions(b, input, folders, now);
358
360
  const position = segment.after;
359
361
  if (segment.nulls) {
360
362
  parts.push(b.isNull(field));
@@ -362,13 +364,11 @@ function createObjectStore(getOrm) {
362
364
  } else {
363
365
  if (nullable) parts.push(b.isNotNull(field));
364
366
  if (position && position.value !== null) {
367
+ const value = toColumnValue(position.value);
365
368
  parts.push(
366
369
  b.or(
367
- b(field, after, position.value),
368
- b.and(
369
- b(field, "=", position.value),
370
- b("id", after, position.id)
371
- )
370
+ b(field, after, value),
371
+ b.and(b(field, "=", value), b("id", after, position.id))
372
372
  )
373
373
  );
374
374
  }
@@ -409,8 +409,9 @@ function createObjectStore(getOrm) {
409
409
  async count(filter) {
410
410
  const orm = await getOrm();
411
411
  const now = /* @__PURE__ */ new Date();
412
+ const folders = await folderSet(filter);
412
413
  return orm.count("storageObject", {
413
- where: (b) => b.and(...filterConditions(b, filter, now))
414
+ where: (b) => b.and(...filterConditions(b, filter, folders, now))
414
415
  });
415
416
  },
416
417
  async getUsage(scope, options = {}) {
@@ -453,7 +454,7 @@ function createObjectStore(getOrm) {
453
454
  const upload = {
454
455
  uploadId: input.uploadId ?? null,
455
456
  uploadToken: input.uploadToken,
456
- declaredSize: input.declaredSize ?? null,
457
+ declaredSize: toByteCount(input.declaredSize),
457
458
  expiresAt: input.expiresAt
458
459
  };
459
460
  const now = /* @__PURE__ */ new Date();
@@ -462,8 +463,9 @@ function createObjectStore(getOrm) {
462
463
  ...ref,
463
464
  scope: input.scope,
464
465
  route: input.route,
465
- folder: input.folder,
466
+ folderId: input.folderId,
466
467
  filename: input.filename,
468
+ searchName: toSearchName(input.filename),
467
469
  contentType: input.contentType ?? null,
468
470
  size: null,
469
471
  eTag: null,
@@ -480,7 +482,7 @@ function createObjectStore(getOrm) {
480
482
  try {
481
483
  await orm.create("storageObject", fresh);
482
484
  } catch (error) {
483
- if (await find(ref)) throw s3Error("CONFLICT");
485
+ if (error instanceof UniqueConstraintError) throw s3Error("CONFLICT");
484
486
  throw error;
485
487
  }
486
488
  };
@@ -515,13 +517,16 @@ function createObjectStore(getOrm) {
515
517
  where: (b) => b.and(byUpload(b, input), uploading(b)),
516
518
  set: {
517
519
  status: "active",
518
- size: input.size,
520
+ size: BigInt(input.size),
519
521
  declaredSize: null,
520
522
  eTag: input.eTag ?? null,
521
523
  ...input.contentType !== void 0 && {
522
524
  contentType: input.contentType
523
525
  },
524
- ...input.filename !== void 0 && { filename: input.filename },
526
+ ...input.filename !== void 0 && {
527
+ filename: input.filename,
528
+ searchName: toSearchName(input.filename)
529
+ },
525
530
  ...input.s3Metadata !== void 0 && {
526
531
  s3Metadata: input.s3Metadata
527
532
  },
@@ -538,6 +543,53 @@ function createObjectStore(getOrm) {
538
543
  }
539
544
  throw s3Error("OBJECT_NOT_FOUND");
540
545
  },
546
+ async insertActive(input) {
547
+ const ref = { bucket: input.bucket, key: input.key };
548
+ const existing = await find(ref);
549
+ if (existing?.status === "active") throw s3Error("OBJECT_EXISTS");
550
+ if (existing && existing.status !== "deleted") throw s3Error("CONFLICT");
551
+ const orm = await getOrm();
552
+ const now = /* @__PURE__ */ new Date();
553
+ const fresh = {
554
+ ...ref,
555
+ scope: input.scope,
556
+ route: input.route,
557
+ folderId: input.folderId,
558
+ filename: input.filename,
559
+ searchName: toSearchName(input.filename),
560
+ contentType: input.contentType,
561
+ size: BigInt(input.size),
562
+ declaredSize: null,
563
+ eTag: input.eTag,
564
+ status: "active",
565
+ s3Metadata: input.s3Metadata,
566
+ attributes: null,
567
+ acl: null,
568
+ uploadId: null,
569
+ uploadToken: null,
570
+ expiresAt: null,
571
+ confirmedAt: now,
572
+ createdAt: now,
573
+ updatedAt: now,
574
+ deletedAt: null
575
+ };
576
+ if (existing?.status === "deleted") {
577
+ await orm.deleteMany("storageObject", {
578
+ where: (b) => b.and(b("id", "=", existing.id), b("status", "=", "deleted"))
579
+ });
580
+ }
581
+ try {
582
+ await orm.create("storageObject", fresh);
583
+ } catch (error) {
584
+ if (error instanceof UniqueConstraintError) throw s3Error("CONFLICT");
585
+ throw error;
586
+ }
587
+ const row = await find(ref);
588
+ if (row?.status !== "active" || row.scope !== input.scope) {
589
+ throw s3Error("CONFLICT");
590
+ }
591
+ return row;
592
+ },
541
593
  async update(ref, input) {
542
594
  const orm = await getOrm();
543
595
  await orm.updateMany("storageObject", {
@@ -548,8 +600,11 @@ function createObjectStore(getOrm) {
548
600
  ref.scope === void 0 || b("scope", "=", ref.scope)
549
601
  ),
550
602
  set: {
551
- ...input.filename !== void 0 && { filename: input.filename },
552
- ...input.folder !== void 0 && { folder: input.folder },
603
+ ...input.filename !== void 0 && {
604
+ filename: input.filename,
605
+ searchName: toSearchName(input.filename)
606
+ },
607
+ ...input.folderId !== void 0 && { folderId: input.folderId },
553
608
  ...input.attributes !== void 0 && {
554
609
  attributes: input.attributes
555
610
  },
@@ -658,72 +713,154 @@ function createObjectStore(getOrm) {
658
713
 
659
714
  // src/store/folder-store.ts
660
715
  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
- });
716
+ var DEFAULT_FOLDER_DESCENDANTS_LIMIT = 1e3;
717
+ var FOLDER_SUBTREE_LIMIT = 1e4;
718
+ var IN_BATCH = 500;
719
+ function storedParent(parentId) {
720
+ return parentId ?? "";
721
+ }
722
+ function readParent(parentId) {
723
+ return parentId === "" ? null : parentId;
724
+ }
725
+ function batches(values, size) {
726
+ const runs = [];
727
+ for (let index = 0; index < values.length; index += size) {
728
+ runs.push(values.slice(index, index + size));
729
+ }
730
+ return runs;
731
+ }
732
+ function byNameThenPath(a, b) {
733
+ return a.name.localeCompare(b.name, void 0, {
734
+ numeric: true,
735
+ sensitivity: "base"
736
+ }) || (a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
737
+ }
738
+ function mapFolder(row) {
739
+ return {
740
+ id: row.id,
741
+ scope: row.scope,
742
+ parentId: readParent(row.parentId),
743
+ name: row.name,
744
+ path: row.path,
745
+ createdAt: row.createdAt,
746
+ updatedAt: row.updatedAt
747
+ };
669
748
  }
670
749
  function createFolderStore(getOrm) {
671
- const find = async (scope, path) => {
750
+ const find = async (scope, id) => {
672
751
  const orm = await getOrm();
673
- return orm.findFirst("storageFolder", {
674
- where: (b) => b.and(b("scope", "=", scope), b("path", "=", path))
752
+ const row = await orm.findFirst("storageFolder", {
753
+ where: (b) => b.and(b("scope", "=", scope), b("id", "=", id))
675
754
  });
755
+ return row ? mapFolder(row) : null;
676
756
  };
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
- }
757
+ const insertFolder = async (orm, folder) => {
758
+ const now = /* @__PURE__ */ new Date();
759
+ const row = await orm.create("storageFolder", {
760
+ ...folder.id !== void 0 && { id: folder.id },
761
+ scope: folder.scope,
762
+ parentId: storedParent(folder.parentId),
763
+ name: folder.name,
764
+ searchName: toSearchName(folder.name),
765
+ path: folder.path,
766
+ createdAt: folder.createdAt ?? now,
767
+ updatedAt: folder.updatedAt ?? now
768
+ });
769
+ return mapFolder(row);
687
770
  };
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);
771
+ const destination = async (scope, parentId, folderId) => {
772
+ if (parentId === null) return null;
773
+ if (parentId === folderId) throw s3Error("FOLDER_INVALID");
774
+ const parent = await find(scope, parentId);
775
+ if (!parent) throw s3Error("FOLDER_NOT_FOUND");
776
+ if (folderId !== void 0) {
777
+ let cursor = parent;
778
+ const seen = /* @__PURE__ */ new Set();
779
+ while (cursor && !seen.has(cursor.id)) {
780
+ if (cursor.id === folderId) throw s3Error("FOLDER_INVALID");
781
+ seen.add(cursor.id);
782
+ cursor = cursor.parentId ? await find(scope, cursor.parentId) : null;
783
+ }
700
784
  }
785
+ return parent;
701
786
  };
702
- const countContents = async (scope, path) => {
787
+ const countContents = async (scope, id) => {
703
788
  const orm = await getOrm();
704
789
  const now = /* @__PURE__ */ new Date();
705
790
  const [folders, objects] = await Promise.all([
706
791
  orm.count("storageFolder", {
707
- where: (b) => b.and(b("scope", "=", scope), b("parent", "=", path))
792
+ where: (b) => b.and(b("scope", "=", scope), b("parentId", "=", id))
708
793
  }),
709
794
  orm.count("storageObject", {
710
795
  where: (b) => b.and(
711
796
  b("scope", "=", scope),
712
- b("folder", "=", path),
797
+ b("folderId", "=", id),
713
798
  liveObject(b, now)
714
799
  )
715
800
  })
716
801
  ]);
717
802
  return folders + objects;
718
803
  };
804
+ const subtree = async (scope, id, limit = Infinity) => {
805
+ const orm = await getOrm();
806
+ const found = [];
807
+ const seen = /* @__PURE__ */ new Set([id]);
808
+ let level = [id];
809
+ for (let depth = 0; depth < FOLDER_MAX_DEPTH && level.length > 0; depth++) {
810
+ const next = [];
811
+ for (const parents of batches(level, IN_BATCH)) {
812
+ const rows = await orm.findMany("storageFolder", {
813
+ where: (b) => b.and(b("scope", "=", scope), b("parentId", "in", parents)),
814
+ orderBy: [["id", "asc"]]
815
+ });
816
+ for (const row of rows) {
817
+ if (seen.has(row.id)) continue;
818
+ seen.add(row.id);
819
+ found.push(mapFolder(row));
820
+ next.push(row.id);
821
+ if (found.length >= limit) return found;
822
+ }
823
+ }
824
+ level = next;
825
+ }
826
+ return found;
827
+ };
828
+ const walks = /* @__PURE__ */ new Map();
829
+ const sharedSubtree = (scope, id) => {
830
+ const key = JSON.stringify([scope, id]);
831
+ let walk = walks.get(key);
832
+ if (!walk) {
833
+ walk = subtree(scope, id, FOLDER_SUBTREE_LIMIT).finally(() => {
834
+ walks.delete(key);
835
+ });
836
+ walks.set(key, walk);
837
+ }
838
+ return walk;
839
+ };
840
+ const retain = async (folder) => {
841
+ if (await find(folder.scope, folder.id)) return;
842
+ const orm = await getOrm();
843
+ try {
844
+ await insertFolder(orm, folder);
845
+ } catch (error) {
846
+ if (error instanceof UniqueConstraintError && await find(folder.scope, folder.id)) {
847
+ return;
848
+ }
849
+ throw error;
850
+ }
851
+ };
719
852
  return {
720
853
  find,
721
- async children(scope, parent, options = {}) {
854
+ async subtreeIds(scope, id) {
855
+ const below = await sharedSubtree(scope, id);
856
+ return below.map((folder) => folder.id);
857
+ },
858
+ async children(scope, parentId, options = {}) {
722
859
  const orm = await getOrm();
723
- return orm.findMany("storageFolder", {
860
+ const rows = await orm.findMany("storageFolder", {
724
861
  where: (b) => b.and(
725
862
  b("scope", "=", scope),
726
- parent === null ? b.isNull("parent") : b("parent", "=", parent)
863
+ b("parentId", "=", storedParent(parentId))
727
864
  ),
728
865
  orderBy: [
729
866
  ["name", "asc"],
@@ -731,33 +868,160 @@ function createFolderStore(getOrm) {
731
868
  ],
732
869
  limit: options.limit ?? DEFAULT_FOLDER_CHILDREN_LIMIT
733
870
  });
871
+ return rows.map(mapFolder);
872
+ },
873
+ async trail(scope, id) {
874
+ const chain = [];
875
+ const seen = /* @__PURE__ */ new Set();
876
+ let current = await find(scope, id);
877
+ if (!current) throw s3Error("FOLDER_NOT_FOUND");
878
+ while (current && !seen.has(current.id)) {
879
+ seen.add(current.id);
880
+ chain.push(current);
881
+ if (!current.parentId || chain.length > FOLDER_MAX_DEPTH) break;
882
+ current = await find(scope, current.parentId);
883
+ }
884
+ return chain.reverse();
885
+ },
886
+ async descendants(scope, parentId, options = {}) {
887
+ const limit = options.limit ?? DEFAULT_FOLDER_DESCENDANTS_LIMIT;
888
+ const needle = options.search ? toSearchName(options.search) : "";
889
+ let folders;
890
+ if (parentId === null) {
891
+ const orm = await getOrm();
892
+ const rows = await orm.findMany("storageFolder", {
893
+ where: (b) => b.and(
894
+ b("scope", "=", scope),
895
+ needle === "" || b("searchName", "contains", needle)
896
+ ),
897
+ orderBy: [["path", "asc"]],
898
+ limit: FOLDER_SUBTREE_LIMIT
899
+ });
900
+ folders = rows.map(mapFolder);
901
+ } else {
902
+ const [parent, below] = await Promise.all([
903
+ find(scope, parentId),
904
+ sharedSubtree(scope, parentId)
905
+ ]);
906
+ if (!parent) throw s3Error("FOLDER_NOT_FOUND");
907
+ folders = below.filter(
908
+ (folder) => needle === "" || toSearchName(folder.name).includes(needle)
909
+ );
910
+ }
911
+ return folders.sort(byNameThenPath).slice(0, limit);
912
+ },
913
+ async ancestors(scope, ids) {
914
+ if (ids.length === 0) return [];
915
+ const orm = await getOrm();
916
+ const byPath = /* @__PURE__ */ new Map();
917
+ for (const run of batches([...new Set(ids)], IN_BATCH)) {
918
+ const rows = await orm.findMany("storageFolder", {
919
+ where: (b) => b.and(b("scope", "=", scope), b("id", "in", run))
920
+ });
921
+ for (const row of rows) byPath.set(row.path, mapFolder(row));
922
+ }
923
+ const missing = /* @__PURE__ */ new Set();
924
+ for (const path of byPath.keys()) {
925
+ for (const above of folderAncestors(path)) {
926
+ if (!byPath.has(above)) missing.add(above);
927
+ }
928
+ }
929
+ for (const run of batches([...missing], IN_BATCH)) {
930
+ const rows = await orm.findMany("storageFolder", {
931
+ where: (b) => b.and(b("scope", "=", scope), b("path", "in", run))
932
+ });
933
+ for (const row of rows) byPath.set(row.path, mapFolder(row));
934
+ }
935
+ return [...byPath.values()].sort(
936
+ (a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0
937
+ );
734
938
  },
735
- ensure,
736
- async create(scope, path) {
737
- if (await find(scope, path)) throw s3Error("FOLDER_EXISTS");
738
- let folder;
939
+ async create(scope, input) {
940
+ const parent = await destination(scope, input.parentId);
941
+ const path = childFolderPath(parent?.path ?? null, input.name);
942
+ if (path === null) throw s3Error("FOLDER_INVALID");
943
+ const name = path.slice(path.lastIndexOf("/") + 1);
944
+ const orm = await getOrm();
739
945
  try {
740
- folder = await insertFolder(await getOrm(), scope, path);
946
+ return await insertFolder(orm, {
947
+ scope,
948
+ parentId: input.parentId,
949
+ name,
950
+ path
951
+ });
741
952
  } catch (error) {
742
- if (await find(scope, path)) throw s3Error("FOLDER_EXISTS");
953
+ if (error instanceof UniqueConstraintError) {
954
+ throw s3Error("FOLDER_EXISTS");
955
+ }
743
956
  throw error;
744
957
  }
745
- await ensure(scope, parentFolderPath(path));
746
- return folder;
747
958
  },
748
- async delete(scope, path) {
749
- if (await countContents(scope, path) > 0) {
959
+ async update(scope, id, input) {
960
+ const folder = await find(scope, id);
961
+ if (!folder) throw s3Error("FOLDER_NOT_FOUND");
962
+ const name = input.name ?? folder.name;
963
+ const parentId = input.parentId === void 0 ? folder.parentId : input.parentId;
964
+ if (name === folder.name && parentId === folder.parentId) return folder;
965
+ const parent = await destination(scope, parentId, id);
966
+ const path = childFolderPath(parent?.path ?? null, name);
967
+ if (path === null) throw s3Error("FOLDER_INVALID");
968
+ const storedName = path.slice(path.lastIndexOf("/") + 1);
969
+ const below = await subtree(scope, id);
970
+ const prefix = folder.path;
971
+ const rewritten = below.map((child) => ({
972
+ id: child.id,
973
+ path: `${path}${child.path.slice(prefix.length)}`
974
+ }));
975
+ for (const next of [path, ...rewritten.map((child) => child.path)]) {
976
+ if (normalizeFolderPath(next) !== next) throw s3Error("FOLDER_INVALID");
977
+ }
978
+ const orm = await getOrm();
979
+ const now = /* @__PURE__ */ new Date();
980
+ await orm.transaction(async (tx) => {
981
+ try {
982
+ await tx.updateMany("storageFolder", {
983
+ where: (b) => b.and(b("scope", "=", scope), b("id", "=", id)),
984
+ set: {
985
+ name: storedName,
986
+ searchName: toSearchName(storedName),
987
+ parentId: storedParent(parentId),
988
+ path,
989
+ updatedAt: now
990
+ }
991
+ });
992
+ } catch (error) {
993
+ if (error instanceof UniqueConstraintError) {
994
+ throw s3Error("FOLDER_EXISTS");
995
+ }
996
+ throw error;
997
+ }
998
+ for (const child of rewritten) {
999
+ await tx.updateMany("storageFolder", {
1000
+ where: (b) => b.and(b("scope", "=", scope), b("id", "=", child.id)),
1001
+ set: { path: child.path, updatedAt: now }
1002
+ });
1003
+ }
1004
+ });
1005
+ const updated = await find(scope, id);
1006
+ if (!updated) throw s3Error("FOLDER_NOT_FOUND");
1007
+ return updated;
1008
+ },
1009
+ async delete(scope, id) {
1010
+ const folder = await find(scope, id);
1011
+ if (!folder) return;
1012
+ if (await countContents(scope, id) > 0) {
750
1013
  throw s3Error("FOLDER_NOT_EMPTY");
751
1014
  }
752
1015
  const orm = await getOrm();
753
1016
  await orm.deleteMany("storageFolder", {
754
- where: (b) => b.and(b("scope", "=", scope), b("path", "=", path))
1017
+ where: (b) => b.and(b("scope", "=", scope), b("id", "=", id))
755
1018
  });
756
- if (await countContents(scope, path) > 0) {
757
- await insertMissing(scope, path);
1019
+ if (await countContents(scope, id) > 0) {
1020
+ await retain(folder);
758
1021
  throw s3Error("FOLDER_NOT_EMPTY");
759
1022
  }
760
- }
1023
+ },
1024
+ retain
761
1025
  };
762
1026
  }
763
1027
 
@@ -807,9 +1071,10 @@ function createDbStore(client) {
807
1071
  await checkSchema();
808
1072
  return client.orm(DB_SCHEMA_VERSION);
809
1073
  };
1074
+ const folders = createFolderStore(getOrm);
810
1075
  return {
811
- objects: createObjectStore(getOrm),
812
- folders: createFolderStore(getOrm),
1076
+ objects: createObjectStore(getOrm, folders.subtreeIds),
1077
+ folders,
813
1078
  checkSchema
814
1079
  };
815
1080
  }
@@ -839,6 +1104,14 @@ function queryInt(schema2) {
839
1104
  }
840
1105
  );
841
1106
  }
1107
+ var queryBoolean = z.codec(
1108
+ z.union([z.enum(["true", "false"]), z.boolean()]),
1109
+ z.boolean(),
1110
+ {
1111
+ decode: (value) => value === true || value === "true",
1112
+ encode: (value) => value
1113
+ }
1114
+ );
842
1115
  var dbListSortSchema = z.object({
843
1116
  field: z.enum(["createdAt", "updatedAt", "size", "filename"]),
844
1117
  direction: z.enum(["asc", "desc"])
@@ -865,12 +1138,13 @@ var dbListSortQuerySchema = z.codec(
865
1138
  }
866
1139
  );
867
1140
  var dbListQuerySchema = z.strictObject({
868
- folder: z.string().optional(),
1141
+ folderId: z.string().optional(),
1142
+ recursive: queryBoolean.optional(),
869
1143
  route: routeNameSchema.optional(),
870
1144
  // Rows that purge has claimed stay internal, as on `GET /db/object`.
871
1145
  status: z.enum(["pending", "active", "deleted"]).optional(),
872
1146
  contentType: optionalContentTypeSchema,
873
- /** Text the filename contains. */
1147
+ /** Text the filename contains, ignoring case and accents. */
874
1148
  search: optionalTrimmedString.pipe(
875
1149
  z.string().max(MAX_SEARCH_LENGTH).optional()
876
1150
  ),
@@ -890,12 +1164,21 @@ var dbUpdateObjectBodySchema = z.strictObject({
890
1164
  key: objectKeySchema,
891
1165
  bucket: trimmedString.optional(),
892
1166
  filename: fileNameSchema.optional(),
893
- folder: z.string().nullable().optional(),
1167
+ folderId: z.string().nullable().optional(),
894
1168
  /** `null` clears the attributes. */
895
1169
  attributes: z.record(z.string(), z.unknown()).nullable().optional()
896
1170
  });
897
- var dbFolderInputSchema = z.strictObject({
898
- path: z.string()
1171
+ var dbCreateFolderBodySchema = z.strictObject({
1172
+ parentId: z.string().nullable().optional(),
1173
+ name: z.string()
1174
+ });
1175
+ var dbUpdateFolderBodySchema = z.strictObject({
1176
+ id: trimmedString,
1177
+ name: z.string().optional(),
1178
+ parentId: z.string().nullable().optional()
1179
+ });
1180
+ var dbDeleteFolderQuerySchema = z.strictObject({
1181
+ id: trimmedString
899
1182
  });
900
1183
 
901
1184
  // src/plugin/to-db-client-object.ts
@@ -906,7 +1189,7 @@ function toDbClientObject(object2, toClient) {
906
1189
  bucket: object2.bucket,
907
1190
  key: object2.key,
908
1191
  route: object2.route,
909
- folder: object2.folder,
1192
+ folderId: object2.folderId,
910
1193
  filename: object2.filename,
911
1194
  contentType: object2.contentType,
912
1195
  size: object2.size,
@@ -924,16 +1207,18 @@ function toDbClientObject(object2, toClient) {
924
1207
  }
925
1208
  function toDbClientFolder(folder) {
926
1209
  return {
927
- path: folder.path,
1210
+ id: folder.id,
928
1211
  name: folder.name,
929
- parent: folder.parent,
930
- createdAt: folder.createdAt.toISOString()
1212
+ parentId: folder.parentId,
1213
+ path: folder.path,
1214
+ createdAt: folder.createdAt.toISOString(),
1215
+ updatedAt: folder.updatedAt.toISOString()
931
1216
  };
932
1217
  }
933
1218
 
934
1219
  // src/plugin/create-database-endpoints.ts
935
1220
  var DEFAULT_ATTRIBUTES_MAX_BYTES = 16384;
936
- var utf8 = new TextEncoder();
1221
+ var utf82 = new TextEncoder();
937
1222
  function resolveBucket(config, bucket, field) {
938
1223
  const buckets = /* @__PURE__ */ new Set();
939
1224
  for (const route of Object.values(config.routes)) {
@@ -956,16 +1241,18 @@ function resolveBucket(config, bucket, field) {
956
1241
  }
957
1242
  function matchingFolders(children, search) {
958
1243
  if (!search) return children;
959
- const text = search.toLowerCase();
960
- return children.filter((child) => child.name.toLowerCase().includes(text));
1244
+ return children.filter((child) => matchesSearchText(child.name, search));
1245
+ }
1246
+ function wireFolderId(value) {
1247
+ if (value === void 0) return void 0;
1248
+ return value === "" || value === null ? null : value;
961
1249
  }
962
- function parseFolderPath(value) {
963
- const path = parseFolder(value);
964
- if (path === null) throw s3Error("FOLDER_INVALID");
965
- return path;
1250
+ function folderTarget(folder) {
1251
+ if (folder === null) return { folder: null, folderId: null };
1252
+ return { folder: folder.path, folderId: folder.id };
966
1253
  }
967
1254
  function createDatabaseEndpoints(options) {
968
- const { access, quota, toClientObject } = options;
1255
+ const { access, quota, resolveScope, toClientObject } = options;
969
1256
  const { objects, folders } = options.store;
970
1257
  const parseAttributes = async (value) => {
971
1258
  const { attributes } = options;
@@ -981,7 +1268,7 @@ function createDatabaseEndpoints(options) {
981
1268
  });
982
1269
  }
983
1270
  const maxBytes = attributes.maxBytes ?? DEFAULT_ATTRIBUTES_MAX_BYTES;
984
- const size = utf8.encode(JSON.stringify(result.value)).byteLength;
1271
+ const size = utf82.encode(JSON.stringify(result.value)).byteLength;
985
1272
  if (size > maxBytes) {
986
1273
  throw s3Error("VALIDATION_ERROR", {
987
1274
  issue: `[body.attributes] Exceeds ${maxBytes} bytes`
@@ -989,43 +1276,80 @@ function createDatabaseEndpoints(options) {
989
1276
  }
990
1277
  return result.value;
991
1278
  };
1279
+ const openFolder = async (scope, wire) => {
1280
+ const folderId = wireFolderId(wire);
1281
+ if (folderId === void 0) return void 0;
1282
+ if (folderId === null) return null;
1283
+ const folder = await folders.find(scope, folderId);
1284
+ if (!folder) throw s3Error("FOLDER_NOT_FOUND");
1285
+ return folder;
1286
+ };
992
1287
  return {
993
- /** One page of the caller's objects. `folder` omitted lists every folder. */
1288
+ /** One page of the caller's objects. `folderId` omitted lists every folder. */
994
1289
  objects: createS3Endpoint(
995
1290
  pluginPath("db", "objects"),
996
1291
  { method: "GET", query: dbListQuerySchema },
997
1292
  async (ctx) => {
998
- const { folder: wireFolder, ...filters } = ctx.query;
999
- const folder = wireFolder === void 0 ? void 0 : parseFolder(wireFolder);
1293
+ const {
1294
+ folderId: wireFolder,
1295
+ recursive: wantsSubtree,
1296
+ ...filters
1297
+ } = ctx.query;
1298
+ const peeked = await resolveScope(ctx.context, {
1299
+ operation: "list",
1300
+ route: filters.route
1301
+ });
1302
+ const opened = await openFolder(peeked.scope, wireFolder);
1303
+ const recursive = wantsSubtree === true && opened !== void 0;
1000
1304
  const scope = await access.scope(ctx.context, {
1001
1305
  operation: "list",
1002
1306
  route: filters.route,
1003
- ...folder !== void 0 && { folder }
1004
- });
1005
- const page = await objects.list({
1006
- ...filters,
1007
- scope: scope.scope,
1008
- folder
1307
+ ...opened !== void 0 && folderTarget(opened),
1308
+ ...recursive && { recursive }
1009
1309
  });
1310
+ const folderId = opened === void 0 ? void 0 : opened?.id ?? null;
1311
+ const filter = { ...filters, scope: scope.scope, folderId, recursive };
1312
+ const firstPage = () => Promise.all([
1313
+ objects.getUsage(scope.scope),
1314
+ quota.limits(scope),
1315
+ opened === void 0 ? void 0 : recursive ? folders.descendants(scope.scope, folderId ?? null, {
1316
+ search: filters.search
1317
+ }) : folders.children(scope.scope, folderId ?? null).then(
1318
+ (children) => matchingFolders(children, filters.search)
1319
+ ),
1320
+ opened === void 0 ? void 0 : opened === null ? [] : folders.trail(scope.scope, opened.id),
1321
+ filters.search ? objects.count(filter) : void 0
1322
+ ]);
1323
+ const early = filters.cursor ? null : firstPage();
1324
+ early?.catch(() => void 0);
1325
+ const page = await objects.list(filter);
1010
1326
  const response = {
1011
1327
  objects: page.objects.map((object2) => toClientObject(object2)),
1012
1328
  nextCursor: page.nextCursor
1013
1329
  };
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)
1019
- ]);
1330
+ const located = page.objects.flatMap(
1331
+ (object2) => object2.folderId === null ? [] : [object2.folderId]
1332
+ );
1333
+ if (!page.firstPage) {
1334
+ if (!recursive) return response;
1335
+ const locations2 = await folders.ancestors(scope.scope, located);
1336
+ return { ...response, locations: locations2.map(toDbClientFolder) };
1337
+ }
1338
+ const [usage, limits, listed, trail, total] = await (early ?? firstPage());
1339
+ const locations = recursive ? await folders.ancestors(scope.scope, [
1340
+ ...located,
1341
+ ...(listed ?? []).flatMap(
1342
+ (folder) => folder.parentId === null ? [] : [folder.parentId]
1343
+ )
1344
+ ]) : void 0;
1020
1345
  return {
1021
1346
  ...response,
1022
1347
  usage,
1023
1348
  limits,
1024
- ...children && {
1025
- folders: matchingFolders(children, filters.search).map(
1026
- toDbClientFolder
1027
- )
1028
- }
1349
+ ...listed && { folders: listed.map(toDbClientFolder) },
1350
+ ...trail && { trail: trail.map(toDbClientFolder) },
1351
+ ...locations && { locations: locations.map(toDbClientFolder) },
1352
+ ...total !== void 0 && { total }
1029
1353
  };
1030
1354
  }
1031
1355
  ),
@@ -1051,8 +1375,7 @@ function createDatabaseEndpoints(options) {
1051
1375
  ),
1052
1376
  /**
1053
1377
  * 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.
1378
+ * requires the target folder to exist and authorizes that folder.
1056
1379
  */
1057
1380
  updateObject: createS3Endpoint(
1058
1381
  pluginPath("db", "object"),
@@ -1064,55 +1387,95 @@ function createDatabaseEndpoints(options) {
1064
1387
  ctx.body.bucket,
1065
1388
  "body.bucket"
1066
1389
  );
1067
- const folder = ctx.body.folder === void 0 ? void 0 : parseFolder(ctx.body.folder ?? "");
1390
+ const folderId = wireFolderId(ctx.body.folderId);
1391
+ const peeked = await resolveScope(ctx.context, {
1392
+ operation: "update"
1393
+ });
1394
+ const opened = folderId === void 0 ? void 0 : await openFolder(peeked.scope, folderId ?? "");
1068
1395
  const { scope, object: object2 } = await access.object(
1069
1396
  ctx.context,
1070
1397
  {
1071
1398
  operation: "update",
1072
1399
  key,
1073
- ...folder !== void 0 && { folder }
1400
+ ...opened !== void 0 && folderTarget(opened)
1074
1401
  },
1075
1402
  { bucket, key }
1076
1403
  );
1077
1404
  if (object2.status !== "active") throw s3Error("OBJECT_NOT_FOUND");
1078
1405
  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) {
1406
+ if (filename === void 0 && folderId === void 0 && attributes === void 0) {
1080
1407
  return { object: toClientObject(object2) };
1081
1408
  }
1082
1409
  const updated = await options.objects.update(
1083
1410
  { bucket, key, id: object2.id, scope: scope.scope },
1084
- { filename, folder, attributes }
1411
+ { filename, folderId, attributes }
1085
1412
  );
1086
1413
  if (!updated) throw s3Error("OBJECT_NOT_FOUND");
1087
1414
  return { object: toClientObject(updated) };
1088
1415
  }
1089
1416
  ),
1090
- /** Creates a folder and its missing ancestors. */
1417
+ /** Creates one folder inside a parent that already exists. */
1091
1418
  createFolder: createS3Endpoint(
1092
1419
  pluginPath("db", "folder"),
1093
- { method: "POST", body: dbFolderInputSchema },
1420
+ { method: "POST", body: dbCreateFolderBodySchema },
1094
1421
  async (ctx) => {
1095
- const path = parseFolderPath(ctx.body.path);
1422
+ const parentId = wireFolderId(ctx.body.parentId) ?? null;
1423
+ const peeked = await resolveScope(ctx.context, { operation: "update" });
1424
+ const parent = parentId === null ? null : await openFolder(peeked.scope, parentId);
1425
+ const path = childFolderPath(parent?.path ?? null, ctx.body.name);
1426
+ if (path === null) throw s3Error("FOLDER_INVALID");
1096
1427
  const scope = await access.scope(ctx.context, {
1097
1428
  operation: "update",
1098
1429
  folder: path
1099
1430
  });
1100
- const folder = await folders.create(scope.scope, path);
1431
+ const folder = await folders.create(scope.scope, {
1432
+ parentId,
1433
+ name: ctx.body.name
1434
+ });
1435
+ return { folder: toDbClientFolder(folder) };
1436
+ }
1437
+ ),
1438
+ /** Renames a folder, moves it, or both. */
1439
+ updateFolder: createS3Endpoint(
1440
+ pluginPath("db", "folder"),
1441
+ { method: "PATCH", body: dbUpdateFolderBodySchema },
1442
+ async (ctx) => {
1443
+ const peeked = await resolveScope(ctx.context, { operation: "update" });
1444
+ const current = await folders.find(peeked.scope, ctx.body.id);
1445
+ if (!current) throw s3Error("FOLDER_NOT_FOUND");
1446
+ const parentId = ctx.body.parentId === void 0 ? current.parentId : wireFolderId(ctx.body.parentId) ?? null;
1447
+ const parent = parentId === null ? null : await openFolder(peeked.scope, parentId);
1448
+ const path = childFolderPath(
1449
+ parent?.path ?? null,
1450
+ ctx.body.name ?? current.name
1451
+ );
1452
+ if (path === null) throw s3Error("FOLDER_INVALID");
1453
+ await access.scope(ctx.context, {
1454
+ operation: "update",
1455
+ folder: path,
1456
+ folderId: current.id
1457
+ });
1458
+ const folder = await folders.update(peeked.scope, current.id, {
1459
+ ...ctx.body.name !== void 0 && { name: ctx.body.name },
1460
+ ...ctx.body.parentId !== void 0 && { parentId }
1461
+ });
1101
1462
  return { folder: toDbClientFolder(folder) };
1102
1463
  }
1103
1464
  ),
1104
1465
  /** Removes an empty folder. A missing folder is not an error. */
1105
1466
  deleteFolder: createS3Endpoint(
1106
1467
  pluginPath("db", "folder"),
1107
- { method: "DELETE", query: dbFolderInputSchema },
1468
+ { method: "DELETE", query: dbDeleteFolderQuerySchema },
1108
1469
  async (ctx) => {
1109
- const path = parseFolderPath(ctx.query.path);
1110
- const scope = await access.scope(ctx.context, {
1470
+ const peeked = await resolveScope(ctx.context, { operation: "update" });
1471
+ const current = await folders.find(peeked.scope, ctx.query.id);
1472
+ if (!current) return { id: ctx.query.id };
1473
+ await access.scope(ctx.context, {
1111
1474
  operation: "update",
1112
- folder: path
1475
+ ...folderTarget(current)
1113
1476
  });
1114
- await folders.delete(scope.scope, path);
1115
- return { path };
1477
+ await folders.delete(peeked.scope, current.id);
1478
+ return { id: current.id };
1116
1479
  }
1117
1480
  )
1118
1481
  };
@@ -1132,12 +1495,9 @@ function refOf(context) {
1132
1495
  function objectTarget(context, operation) {
1133
1496
  return { operation, route: context.route, key: context.key };
1134
1497
  }
1135
- function uploadTarget(context) {
1136
- return {
1137
- ...objectTarget(context, "upload"),
1138
- folder: parseFolder(context.clientMetadata?.folder ?? ""),
1139
- clientMetadata: context.clientMetadata
1140
- };
1498
+ function requestedFolderId(metadata) {
1499
+ const value = metadata?.folderId ?? "";
1500
+ return value === "" ? null : value;
1141
1501
  }
1142
1502
  function writtenBy(object2, uploadToken) {
1143
1503
  return object2 !== null && object2.uploadToken === uploadToken;
@@ -1163,16 +1523,22 @@ function createDatabaseLifecycleHooks(options) {
1163
1523
  if (!live) throw s3Error("OBJECT_NOT_FOUND");
1164
1524
  };
1165
1525
  const uploadWrite = async (context) => {
1166
- const target = uploadTarget(context);
1167
1526
  const existing = await objects.find(refOf(context));
1168
- if (existing?.status !== "active" || routes.replace(context.route) !== "overwrite") {
1169
- return { target, existing, replaced: null };
1527
+ const replaced = existing?.status === "active" && routes.replace(context.route) === "overwrite" ? existing : null;
1528
+ const folderId = replaced ? replaced.folderId : requestedFolderId(context.clientMetadata);
1529
+ const scope = await resolveScope(context, objectTarget(context, "upload"));
1530
+ let folder = null;
1531
+ if (folderId !== null) {
1532
+ folder = await folders.find(scope.scope, folderId);
1533
+ if (!folder && !replaced) throw s3Error("FOLDER_NOT_FOUND");
1170
1534
  }
1171
- return {
1172
- target: { ...target, folder: existing.folder },
1173
- existing,
1174
- replaced: existing
1535
+ const target = {
1536
+ ...objectTarget(context, "upload"),
1537
+ folderId,
1538
+ clientMetadata: context.clientMetadata,
1539
+ ...folder ? { folder: folder.path } : folderId === null ? { folder: null } : {}
1175
1540
  };
1541
+ return { target, existing, replaced, folder };
1176
1542
  };
1177
1543
  const abortPendingMultipart = async (context, row) => {
1178
1544
  const client = routes.client(context.route);
@@ -1220,13 +1586,13 @@ function createDatabaseLifecycleHooks(options) {
1220
1586
  });
1221
1587
  },
1222
1588
  async onStart(context) {
1223
- const { target } = await uploadWrite(context);
1224
- const { scope } = await resolveScope(context, target);
1225
- const row = await objects.upsertPending({
1589
+ const written = await uploadWrite(context);
1590
+ const { scope } = await resolveScope(context, written.target);
1591
+ await objects.upsertPending({
1226
1592
  ...refOf(context),
1227
1593
  scope,
1228
1594
  route: context.route,
1229
- folder: target.folder ?? null,
1595
+ folderId: written.target.folderId ?? null,
1230
1596
  filename: context.file.name,
1231
1597
  contentType: context.file.type ?? null,
1232
1598
  declaredSize: context.file.size,
@@ -1236,7 +1602,7 @@ function createDatabaseLifecycleHooks(options) {
1236
1602
  expiresAt: context.expiresAt,
1237
1603
  overwrite: routes.replace(context.route) === "overwrite"
1238
1604
  });
1239
- await folders.ensure(scope, row.folder);
1605
+ if (written.folder) await folders.retain(written.folder);
1240
1606
  },
1241
1607
  async confirmGuard(context) {
1242
1608
  await assertUploadRow(context, "confirm", {
@@ -1341,6 +1707,8 @@ function createObjectAccess(options) {
1341
1707
  ...scope,
1342
1708
  ...target?.key !== void 0 && { key: target.key },
1343
1709
  ...target?.folder !== void 0 && { folder: target.folder },
1710
+ ...target?.folderId !== void 0 && { folderId: target.folderId },
1711
+ ...target?.recursive === true && { recursive: true },
1344
1712
  ...target?.clientMetadata !== void 0 && {
1345
1713
  clientMetadata: target.clientMetadata
1346
1714
  },
@@ -1370,13 +1738,20 @@ function createObjectAccess(options) {
1370
1738
  assertOwner
1371
1739
  };
1372
1740
  }
1373
- function folderPath(path) {
1374
- const normalized = normalizeFolderPath(path);
1375
- if (normalized === null) throw s3Error("FOLDER_INVALID");
1376
- return normalized;
1741
+ function rootId(folderId) {
1742
+ return folderId === "" ? null : folderId;
1743
+ }
1744
+ async function targetFolder(folders, scope, folderId) {
1745
+ if (folderId === void 0) return void 0;
1746
+ const id = rootId(folderId);
1747
+ if (id === null) return null;
1748
+ const folder = await folders.find(scope, id);
1749
+ if (!folder) throw s3Error("FOLDER_NOT_FOUND");
1750
+ return folder;
1377
1751
  }
1378
1752
  function createPublicStores(store) {
1379
- const { objects, folders } = store;
1753
+ const { objects } = store;
1754
+ const { subtreeIds: _subtreeIds, ...folders } = store.folders;
1380
1755
  return {
1381
1756
  objects: {
1382
1757
  find: objects.find,
@@ -1384,26 +1759,19 @@ function createPublicStores(store) {
1384
1759
  count: objects.count,
1385
1760
  getUsage: objects.getUsage,
1386
1761
  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);
1762
+ const folderId = input.folderId === void 0 ? void 0 : rootId(input.folderId);
1763
+ let folder;
1764
+ if (typeof folderId === "string") {
1765
+ const scope = ref.scope ?? (await objects.find(ref))?.scope;
1766
+ if (!scope) return null;
1767
+ folder = await targetFolder(folders, scope, folderId);
1391
1768
  }
1769
+ const row = await objects.update(ref, { ...input, folderId });
1770
+ if (row && folder) await folders.retain(folder);
1392
1771
  return row;
1393
1772
  }
1394
1773
  },
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
- }
1774
+ folders
1407
1775
  };
1408
1776
  }
1409
1777
  async function abortUpload(client, row, uploadId) {
@@ -1518,6 +1886,198 @@ function createQuota(objects, option) {
1518
1886
  }
1519
1887
  };
1520
1888
  }
1889
+ function createRecord(options) {
1890
+ const { store, resolveScope, access, quota } = options;
1891
+ return async (input, call = {}) => {
1892
+ const env = options.env();
1893
+ if (!env) {
1894
+ throw new Error(
1895
+ "[dimah-s3/db] s3.db.record ran before the plugin was initialized."
1896
+ );
1897
+ }
1898
+ const located = locateRoute(env.config, input.route);
1899
+ if (!located) throw s3Error("UNKNOWN_ROUTE", { route: input.route });
1900
+ if (env.routeOptions(input.route) === false) {
1901
+ throw s3Error("VALIDATION_ERROR", {
1902
+ issue: `[route] "${input.route}" does not track objects`
1903
+ });
1904
+ }
1905
+ const key = routeKey(input.key, located.keyPrefix);
1906
+ const filename = fileName(
1907
+ input.name ?? key.slice(key.lastIndexOf("/") + 1)
1908
+ );
1909
+ const folderId = rootId2(input.folderId);
1910
+ const scopeCall = await scopeCallOf(env, input.route, located.route, call);
1911
+ const peeked = await resolveScope(scopeCall, {
1912
+ operation: "record",
1913
+ route: input.route
1914
+ });
1915
+ let folderPath = null;
1916
+ if (folderId !== null) {
1917
+ const folder = await store.folders.find(peeked.scope, folderId);
1918
+ if (!folder) throw s3Error("FOLDER_NOT_FOUND");
1919
+ folderPath = folder.path;
1920
+ }
1921
+ const scope = await access.scope(scopeCall, {
1922
+ operation: "record",
1923
+ route: input.route,
1924
+ key,
1925
+ folderId,
1926
+ folder: folderPath
1927
+ });
1928
+ const ref = { bucket: located.bucket, key };
1929
+ const existing = await store.objects.find(ref);
1930
+ if (existing && existing.status !== "deleted") {
1931
+ if (existing.scope !== scope.scope) throw s3Error("FORBIDDEN");
1932
+ if (existing.status !== "active") throw s3Error("CONFLICT");
1933
+ }
1934
+ const head = await headStored(
1935
+ clientOf(env, input.route),
1936
+ located.bucket,
1937
+ key,
1938
+ scopeCall.request.signal
1939
+ );
1940
+ assertFileConstraints(uploadConstraints(located.route), {
1941
+ name: filename,
1942
+ size: head.size,
1943
+ type: head.contentType ?? void 0
1944
+ });
1945
+ if (existing?.status === "active" && head.eTag !== null && existing.eTag === head.eTag && existing.size === head.size) {
1946
+ return existing;
1947
+ }
1948
+ if (existing?.status === "active") throw s3Error("OBJECT_EXISTS");
1949
+ await quota.assert(scope, { size: head.size, excluding: ref });
1950
+ const row = await store.objects.insertActive({
1951
+ ...ref,
1952
+ scope: scope.scope,
1953
+ route: input.route,
1954
+ folderId,
1955
+ filename,
1956
+ contentType: head.contentType,
1957
+ size: head.size,
1958
+ eTag: head.eTag,
1959
+ s3Metadata: head.metadata
1960
+ });
1961
+ if (folderId !== null) {
1962
+ const folder = await store.folders.find(scope.scope, folderId);
1963
+ if (folder) await store.folders.retain(folder);
1964
+ }
1965
+ return row;
1966
+ };
1967
+ }
1968
+ function locateRoute(config, name) {
1969
+ const route = Object.hasOwn(config.routes, name) ? config.routes[name] : void 0;
1970
+ if (!route) return null;
1971
+ const bucket = route.bucket ?? config.bucket;
1972
+ if (!bucket) return null;
1973
+ if (route.keyPrefix === false) return { route, bucket, keyPrefix: false };
1974
+ const keyPrefix = normalizeObjectKey(route.keyPrefix ?? name);
1975
+ if (!keyPrefix) return null;
1976
+ return { route, bucket, keyPrefix };
1977
+ }
1978
+ function uploadConstraints(route) {
1979
+ const { upload } = route;
1980
+ if (!upload || typeof upload === "boolean") return {};
1981
+ return { fileTypes: upload.fileTypes, maxFileSize: upload.maxFileSize };
1982
+ }
1983
+ function routeKey(key, keyPrefix) {
1984
+ const safe = normalizeObjectKey(key);
1985
+ if (!safe) throw s3Error("KEY_INVALID");
1986
+ if (keyPrefix === false) return safe;
1987
+ if (safe.startsWith(`${keyPrefix}/`)) return safe;
1988
+ throw s3Error("KEY_INVALID");
1989
+ }
1990
+ function rootId2(folderId) {
1991
+ return folderId === void 0 || folderId === "" ? null : folderId;
1992
+ }
1993
+ function fileName(name) {
1994
+ if (!name.isWellFormed()) {
1995
+ throw s3Error("VALIDATION_ERROR", {
1996
+ issue: "[name] File name is invalid"
1997
+ });
1998
+ }
1999
+ const normalized = name.normalize("NFC");
2000
+ if (!isFolderName(normalized) || normalized.includes("\\")) {
2001
+ throw s3Error("VALIDATION_ERROR", {
2002
+ issue: "[name] File name is invalid"
2003
+ });
2004
+ }
2005
+ return normalized;
2006
+ }
2007
+ async function scopeCallOf(env, routeName, route, call) {
2008
+ const request = call.request ?? new Request(requestUrl(env.config), {
2009
+ headers: call.headers,
2010
+ signal: call.signal
2011
+ });
2012
+ const locals = Object.assign(
2013
+ /* @__PURE__ */ Object.create(null),
2014
+ call.locals
2015
+ );
2016
+ const context = {
2017
+ request,
2018
+ operation: "record",
2019
+ locals,
2020
+ route: routeName
2021
+ };
2022
+ await runUserGuard(env.config.guard, context);
2023
+ await runUserGuard(route.guard, context);
2024
+ return { request, locals };
2025
+ }
2026
+ async function runUserGuard(guard, context) {
2027
+ if (!guard) return;
2028
+ const result = await guard(context);
2029
+ if (result === void 0) return;
2030
+ if (isLocalsResult(result)) {
2031
+ Object.assign(context.locals, result.locals);
2032
+ return;
2033
+ }
2034
+ throw new Error(
2035
+ `[dimah-s3/db] A guard for "${context.operation}" returned a value s3.db.record cannot use. Return nothing or { locals }, and throw to reject.`
2036
+ );
2037
+ }
2038
+ function isLocalsResult(value) {
2039
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
2040
+ return false;
2041
+ }
2042
+ const keys = Object.keys(value);
2043
+ if (keys.length !== 1 || keys[0] !== "locals") return false;
2044
+ const locals = value.locals;
2045
+ return locals !== null && typeof locals === "object" && !Array.isArray(locals);
2046
+ }
2047
+ function requestUrl(config) {
2048
+ if (config.baseURL) return config.baseURL;
2049
+ return new URL(config.basePath ?? "/api/s3", "http://localhost").href;
2050
+ }
2051
+ function clientOf(env, route) {
2052
+ const { routes, client } = env.config;
2053
+ const config = Object.hasOwn(routes, route) ? routes[route] : void 0;
2054
+ const resolved = config?.client ?? client;
2055
+ if (!resolved) {
2056
+ throw new Error(`[dimah-s3/db] Route "${route}" has no S3 client.`);
2057
+ }
2058
+ return resolved;
2059
+ }
2060
+ async function headStored(client, bucket, key, signal) {
2061
+ try {
2062
+ const head = await client.send(
2063
+ new HeadObjectCommand({ Bucket: bucket, Key: key }),
2064
+ { abortSignal: signal }
2065
+ );
2066
+ if (typeof head.ContentLength !== "number") {
2067
+ throw new TypeError(`HeadObject returned no ContentLength for "${key}".`);
2068
+ }
2069
+ const metadata = readObjectMetadata(head.Metadata).metadata;
2070
+ return {
2071
+ size: head.ContentLength,
2072
+ contentType: head.ContentType || null,
2073
+ eTag: head.ETag ? head.ETag.replaceAll('"', "") : null,
2074
+ metadata: Object.keys(metadata).length > 0 ? metadata : null
2075
+ };
2076
+ } catch (error) {
2077
+ if (isAwsNotFound(error)) throw s3Error("OBJECT_NOT_FOUND");
2078
+ throw error;
2079
+ }
2080
+ }
1521
2081
  function perKey(cache, key, create) {
1522
2082
  let value = cache.get(key);
1523
2083
  if (value === void 0) {
@@ -1552,6 +2112,12 @@ function createScopeResolver(options) {
1552
2112
  }
1553
2113
  const resolved = await scope;
1554
2114
  if (resolved === null) throw s3Error("UNAUTHORIZED");
2115
+ const length = characterCount(resolved);
2116
+ if (length > SCOPE_MAX_LENGTH) {
2117
+ throw new Error(
2118
+ `[dimah-s3/db] resolveScope returned a scope of ${length} characters. A scope holds at most ${SCOPE_MAX_LENGTH}.`
2119
+ );
2120
+ }
1555
2121
  return { ...context, scope: resolved };
1556
2122
  };
1557
2123
  }
@@ -1582,6 +2148,13 @@ function db(options) {
1582
2148
  client: routes.client,
1583
2149
  logger: () => env?.logger
1584
2150
  });
2151
+ const record2 = createRecord({
2152
+ store,
2153
+ resolveScope,
2154
+ access,
2155
+ quota,
2156
+ env: () => env
2157
+ });
1585
2158
  return definePlugin({
1586
2159
  id: "db",
1587
2160
  init(initEnv) {
@@ -1600,6 +2173,7 @@ function db(options) {
1600
2173
  store,
1601
2174
  objects: stores.objects,
1602
2175
  access,
2176
+ resolveScope,
1603
2177
  quota,
1604
2178
  attributes: options.attributes,
1605
2179
  toClientObject
@@ -1607,6 +2181,7 @@ function db(options) {
1607
2181
  context: {
1608
2182
  objects: stores.objects,
1609
2183
  folders: stores.folders,
2184
+ record: record2,
1610
2185
  checkSchema: store.checkSchema,
1611
2186
  purgeStale: purge.purgeStale,
1612
2187
  pruneDeleted: purge.pruneDeleted