@dimah-s3/db 2.0.0-beta.0 → 2.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -10
- package/dist/adapters/drizzle.d.ts +2 -2
- package/dist/adapters/kysely.d.ts +1 -1
- package/dist/adapters/prisma.d.ts +1 -1
- package/dist/cli.d.ts +18 -14
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +69 -153
- package/dist/cli.js.map +1 -1
- package/dist/client/db-client.d.ts +1 -1
- package/dist/client/db-client.d.ts.map +1 -1
- package/dist/client/db-object-source.d.ts +15 -9
- package/dist/client/db-object-source.d.ts.map +1 -1
- package/dist/client/index.d.ts +3 -3
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +101 -21
- package/dist/client/index.js.map +1 -1
- package/dist/folder-path.d.ts +17 -4
- package/dist/folder-path.d.ts.map +1 -1
- package/dist/fuma-db.d.ts +22 -43
- package/dist/fuma-db.d.ts.map +1 -1
- package/dist/index.d.ts +9 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +606 -314
- package/dist/index.js.map +1 -1
- package/dist/plugin/create-database-endpoints.d.ts +46 -22
- package/dist/plugin/create-database-endpoints.d.ts.map +1 -1
- package/dist/plugin/create-database-lifecycle-hooks.d.ts +4 -4
- package/dist/plugin/create-database-lifecycle-hooks.d.ts.map +1 -1
- package/dist/plugin/db.d.ts +2 -2
- package/dist/plugin/db.d.ts.map +1 -1
- package/dist/plugin/object-access.d.ts +2 -2
- package/dist/plugin/object-access.d.ts.map +1 -1
- package/dist/plugin/public-stores.d.ts +5 -8
- package/dist/plugin/public-stores.d.ts.map +1 -1
- package/dist/plugin/quota.d.ts +1 -1
- package/dist/plugin/schemas.d.ts +25 -11
- package/dist/plugin/schemas.d.ts.map +1 -1
- package/dist/plugin/scope.d.ts +2 -0
- package/dist/plugin/scope.d.ts.map +1 -1
- package/dist/plugin/to-db-client-object.d.ts.map +1 -1
- package/dist/schema/schema.d.ts +72 -0
- package/dist/schema/schema.d.ts.map +1 -0
- package/dist/store/create-db-store.d.ts +4 -4
- package/dist/store/create-db-store.d.ts.map +1 -1
- package/dist/store/folder-store.d.ts +73 -15
- package/dist/store/folder-store.d.ts.map +1 -1
- package/dist/store/list-cursor.d.ts +3 -1
- package/dist/store/list-cursor.d.ts.map +1 -1
- package/dist/store/map-row.d.ts +3 -9
- package/dist/store/map-row.d.ts.map +1 -1
- package/dist/store/object-store.d.ts +24 -14
- package/dist/store/object-store.d.ts.map +1 -1
- package/dist/store/schema-version.d.ts +1 -1
- package/dist/store/search-name.d.ts +7 -0
- package/dist/store/search-name.d.ts.map +1 -0
- package/dist/types/client.d.ts +114 -23
- package/dist/types/client.d.ts.map +1 -1
- package/dist/types/plugin.d.ts +17 -6
- package/dist/types/plugin.d.ts.map +1 -1
- package/dist/types/storage-object.d.ts +8 -7
- package/dist/types/storage-object.d.ts.map +1 -1
- package/package.json +15 -30
- package/dist/drizzle/mysql.d.ts +0 -1024
- package/dist/drizzle/mysql.d.ts.map +0 -1
- package/dist/drizzle/mysql.js +0 -123
- package/dist/drizzle/mysql.js.map +0 -1
- package/dist/drizzle/pg.d.ts +0 -963
- package/dist/drizzle/pg.d.ts.map +0 -1
- package/dist/drizzle/pg.js +0 -104
- package/dist/drizzle/pg.js.map +0 -1
- package/dist/drizzle/shared.d.ts +0 -16
- package/dist/drizzle/shared.d.ts.map +0 -1
- package/dist/drizzle/sqlite.d.ts +0 -1023
- package/dist/drizzle/sqlite.d.ts.map +0 -1
- package/dist/drizzle/sqlite.js +0 -107
- package/dist/drizzle/sqlite.js.map +0 -1
- package/dist/schema/v1.d.ts +0 -24
- package/dist/schema/v1.d.ts.map +0 -1
- package/dist/schema/v2.d.ts +0 -64
- package/dist/schema/v2.d.ts.map +0 -1
- package/prisma/dimah-s3.prisma +0 -80
- package/sql/mysql.sql +0 -59
- package/sql/pg.sql +0 -53
- package/sql/sqlite.sql +0 -54
- package/sql/upgrade/1.0.0-2.0.0/mysql.sql +0 -71
- package/sql/upgrade/1.0.0-2.0.0/pg.sql +0 -47
- package/sql/upgrade/1.0.0-2.0.0/sqlite.sql +0 -119
package/dist/index.js
CHANGED
|
@@ -1,188 +1,123 @@
|
|
|
1
|
-
import { fumadb } from 'fumadb';
|
|
1
|
+
import { fumadb, UniqueConstraintError } from 'fumadb';
|
|
2
2
|
import { table, column, idColumn, schema } from 'fumadb/schema';
|
|
3
3
|
import { definePlugin, isFeatureOn, createS3Endpoint, isAwsNotFound, readUploadMarker, deleteObjectIfMatch } from '@dimah-s3/server/plugins';
|
|
4
|
-
import { s3Error, pluginPath, base64UrlDecode, base64UrlEncode } from '@dimah-s3/core';
|
|
4
|
+
import { s3Error, pluginPath, 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
|
-
var
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
size: column("size", "bigint").nullable(),
|
|
18
|
-
eTag: column("e_tag", "string").nullable(),
|
|
19
|
-
filename: column("filename", "string").nullable(),
|
|
20
|
-
status: column("status", "string"),
|
|
21
|
-
s3Metadata: column("metadata", "json").nullable(),
|
|
22
|
-
acl: column("acl", "string").nullable(),
|
|
23
|
-
uploadId: column("upload_id", "string").nullable(),
|
|
24
|
-
declaredSize: column("declared_size", "bigint").nullable(),
|
|
25
|
-
confirmedAt: column("confirmed_at", "timestamp").nullable(),
|
|
26
|
-
expiresAt: column("expires_at", "timestamp").nullable(),
|
|
27
|
-
createdAt: column("created_at", "timestamp").defaultTo$("now"),
|
|
28
|
-
updatedAt: column("updated_at", "timestamp").defaultTo$("now"),
|
|
29
|
-
deletedAt: column("deleted_at", "timestamp").nullable()
|
|
30
|
-
}).unique("storage_object_bucket_key_uk", ["bucket", "key"]);
|
|
31
|
-
var v1 = schema({
|
|
32
|
-
version: "1.0.0",
|
|
33
|
-
tables: {
|
|
34
|
-
storageObject
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
function byteCount(name) {
|
|
38
|
-
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);
|
|
39
17
|
}
|
|
40
|
-
var
|
|
41
|
-
id: idColumn("id", "varchar(255)").defaultTo$("auto"),
|
|
18
|
+
var storageObject = table("storage_object", {
|
|
19
|
+
id: idColumn("id", "varchar(255)").collate(BINARY).defaultTo$("auto"),
|
|
42
20
|
/** Owner, for example `user:123` or `org:acme`. */
|
|
43
|
-
scope:
|
|
44
|
-
bucket:
|
|
45
|
-
key:
|
|
21
|
+
scope: string("scope", SCOPE_MAX_LENGTH),
|
|
22
|
+
bucket: string("bucket"),
|
|
23
|
+
key: string("key", 1024),
|
|
46
24
|
/** Named dimah-s3 route that created the object. */
|
|
47
|
-
route:
|
|
48
|
-
/**
|
|
49
|
-
|
|
50
|
-
filename: column("filename", "
|
|
51
|
-
|
|
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(),
|
|
52
36
|
/** Verified size from HeadObject, set on confirm. */
|
|
53
|
-
size:
|
|
37
|
+
size: column("size", "bigint").nullable(),
|
|
54
38
|
/** Size the client declared before confirm. Quota counts it while pending. */
|
|
55
|
-
declaredSize:
|
|
56
|
-
eTag:
|
|
57
|
-
status:
|
|
39
|
+
declaredSize: column("declared_size", "bigint").nullable(),
|
|
40
|
+
eTag: string("e_tag").nullable(),
|
|
41
|
+
status: string("status", 16),
|
|
58
42
|
/** S3 user metadata of the verified object. */
|
|
59
43
|
s3Metadata: column("s3_metadata", "json").nullable(),
|
|
60
44
|
/** App attributes, validated by the plugin's `attributes.schema`. */
|
|
61
45
|
attributes: column("attributes", "json").nullable(),
|
|
62
|
-
acl:
|
|
46
|
+
acl: string("acl").nullable(),
|
|
63
47
|
/** Multipart upload ID while the row is pending. */
|
|
64
|
-
uploadId: column("upload_id", "string").nullable(),
|
|
48
|
+
uploadId: column("upload_id", "string").collate(BINARY).nullable(),
|
|
65
49
|
/** Token of the upload ticket that wrote the row. Confirm and purge match it. */
|
|
66
|
-
uploadToken:
|
|
50
|
+
uploadToken: string("upload_token", 64).nullable(),
|
|
67
51
|
/** Confirm deadline of a pending row (the upload ticket's expiry). */
|
|
68
52
|
expiresAt: column("expires_at", "timestamp").nullable(),
|
|
69
53
|
confirmedAt: column("confirmed_at", "timestamp").nullable(),
|
|
70
54
|
createdAt: column("created_at", "timestamp").defaultTo$("now"),
|
|
71
55
|
updatedAt: column("updated_at", "timestamp").defaultTo$("now"),
|
|
72
56
|
deletedAt: column("deleted_at", "timestamp").nullable()
|
|
73
|
-
}).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"]);
|
|
74
65
|
var storageFolder = table("storage_folder", {
|
|
75
|
-
id: idColumn("id", "varchar(255)").defaultTo$("auto"),
|
|
76
|
-
scope:
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
/** Path of the containing folder. `null` is the root. */
|
|
80
|
-
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(""),
|
|
81
70
|
/** Last segment of `path`. */
|
|
82
|
-
name:
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
columns: ["scope", "status", "updated_at"]
|
|
95
|
-
},
|
|
96
|
-
{
|
|
97
|
-
name: "storage_object_scope_filename_idx",
|
|
98
|
-
table: "storage_object",
|
|
99
|
-
columns: ["scope", "status", "filename"]
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
name: "storage_object_scope_size_idx",
|
|
103
|
-
table: "storage_object",
|
|
104
|
-
columns: ["scope", "status", "size"]
|
|
105
|
-
},
|
|
106
|
-
{
|
|
107
|
-
name: "storage_object_status_expires_idx",
|
|
108
|
-
table: "storage_object",
|
|
109
|
-
columns: ["status", "expires_at"]
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
name: "storage_folder_scope_parent_idx",
|
|
113
|
-
table: "storage_folder",
|
|
114
|
-
columns: ["scope", "parent"]
|
|
115
|
-
}
|
|
116
|
-
];
|
|
117
|
-
var v1Indexes = [
|
|
118
|
-
"storage_object_scope_status_created_idx",
|
|
119
|
-
"storage_object_scope_route_idx",
|
|
120
|
-
"storage_object_status_created_idx"
|
|
121
|
-
];
|
|
122
|
-
var custom = (sql) => ({ type: "custom", sql });
|
|
123
|
-
var keyName = `COALESCE(NULLIF(substr("key", length(rtrim("key", replace("key", '/', ''))) + 1), ''), "key")`;
|
|
124
|
-
var beforeV2 = [
|
|
125
|
-
custom(
|
|
126
|
-
`UPDATE "storage_object" SET "filename" = ${keyName} WHERE "filename" IS NULL`
|
|
127
|
-
)
|
|
128
|
-
];
|
|
129
|
-
var afterV2 = [
|
|
130
|
-
...v1Indexes.map((name) => custom(`DROP INDEX IF EXISTS "${name}"`)),
|
|
131
|
-
...v2Indexes.map((index) => {
|
|
132
|
-
const columns = index.columns.map((name) => `"${name}"`).join(", ");
|
|
133
|
-
return custom(
|
|
134
|
-
`CREATE INDEX IF NOT EXISTS "${index.name}" ON "${index.table}" (${columns})`
|
|
135
|
-
);
|
|
136
|
-
})
|
|
137
|
-
];
|
|
138
|
-
var v2 = schema({
|
|
139
|
-
version: "2.0.0",
|
|
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"]);
|
|
81
|
+
var dbSchema = schema({
|
|
82
|
+
version: "2.2.0",
|
|
140
83
|
tables: {
|
|
141
|
-
storageObject
|
|
84
|
+
storageObject,
|
|
142
85
|
storageFolder
|
|
143
|
-
},
|
|
144
|
-
async up({ auto }) {
|
|
145
|
-
return [...beforeV2, ...await auto(), ...afterV2];
|
|
146
86
|
}
|
|
147
87
|
});
|
|
148
88
|
|
|
149
89
|
// src/fuma-db.ts
|
|
150
90
|
var DimahS3DB = fumadb({
|
|
151
91
|
namespace: "dimah_s3",
|
|
152
|
-
schemas: [
|
|
92
|
+
schemas: [dbSchema]
|
|
153
93
|
});
|
|
154
|
-
var DB_SCHEMA_VERSION =
|
|
155
|
-
var
|
|
94
|
+
var DB_SCHEMA_VERSION = dbSchema.version;
|
|
95
|
+
var FOLDER_PATH_MAX_BYTES = 1024;
|
|
156
96
|
var FOLDER_NAME_MAX_LENGTH = 255;
|
|
157
97
|
var FOLDER_MAX_DEPTH = 32;
|
|
158
98
|
var INVALID_CHARACTER = /[\p{Cc}\p{Cs}]/u;
|
|
99
|
+
var utf8 = new TextEncoder();
|
|
159
100
|
function characterCount(value) {
|
|
160
101
|
return Array.from(value).length;
|
|
161
102
|
}
|
|
162
|
-
function
|
|
103
|
+
function isFolderName(name) {
|
|
163
104
|
const length = characterCount(name);
|
|
164
|
-
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);
|
|
165
106
|
}
|
|
166
107
|
function normalizeFolderPath(path) {
|
|
167
108
|
if (!path.isWellFormed()) return null;
|
|
168
109
|
const normalized = path.normalize("NFC");
|
|
169
|
-
if (
|
|
110
|
+
if (utf8.encode(normalized).byteLength > FOLDER_PATH_MAX_BYTES) return null;
|
|
170
111
|
const names = normalized.split("/");
|
|
171
112
|
if (names.length > FOLDER_MAX_DEPTH) return null;
|
|
172
|
-
return names.every(
|
|
173
|
-
}
|
|
174
|
-
function parseFolder(value) {
|
|
175
|
-
if (value === "") return null;
|
|
176
|
-
const path = normalizeFolderPath(value);
|
|
177
|
-
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
178
|
-
return path;
|
|
113
|
+
return names.every(isFolderName) ? normalized : null;
|
|
179
114
|
}
|
|
180
|
-
function
|
|
181
|
-
|
|
182
|
-
return
|
|
115
|
+
function childFolderPath(parent, name) {
|
|
116
|
+
if (!isFolderName(name)) return null;
|
|
117
|
+
return normalizeFolderPath(joinFolderPath(parent, name));
|
|
183
118
|
}
|
|
184
|
-
function
|
|
185
|
-
return
|
|
119
|
+
function joinFolderPath(parent, name) {
|
|
120
|
+
return parent === null ? name : `${parent}/${name}`;
|
|
186
121
|
}
|
|
187
122
|
function folderAncestors(path) {
|
|
188
123
|
const segments = path.split("/");
|
|
@@ -195,8 +130,9 @@ var DEFAULT_LIST_SORT = {
|
|
|
195
130
|
var decoder = new TextDecoder();
|
|
196
131
|
function fingerprint(filters) {
|
|
197
132
|
const text = JSON.stringify([
|
|
198
|
-
filters.
|
|
199
|
-
filters.
|
|
133
|
+
filters.folderId === void 0,
|
|
134
|
+
filters.folderId ?? null,
|
|
135
|
+
filters.recursive,
|
|
200
136
|
filters.search ?? null,
|
|
201
137
|
filters.status,
|
|
202
138
|
filters.route ?? null,
|
|
@@ -259,7 +195,7 @@ function mapStorageObjectRow(row) {
|
|
|
259
195
|
bucket: row.bucket,
|
|
260
196
|
key: row.key,
|
|
261
197
|
route: row.route,
|
|
262
|
-
|
|
198
|
+
folderId: row.folderId,
|
|
263
199
|
filename: row.filename,
|
|
264
200
|
contentType: row.contentType,
|
|
265
201
|
size: toNumber(row.size),
|
|
@@ -278,6 +214,19 @@ function mapStorageObjectRow(row) {
|
|
|
278
214
|
deletedAt: row.deletedAt
|
|
279
215
|
};
|
|
280
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
|
+
}
|
|
281
230
|
|
|
282
231
|
// src/store/object-store.ts
|
|
283
232
|
var DEFAULT_LIST_LIMIT = 50;
|
|
@@ -334,15 +283,17 @@ function claimDeadline() {
|
|
|
334
283
|
);
|
|
335
284
|
return new Date(lastClaimDeadline);
|
|
336
285
|
}
|
|
337
|
-
function filterConditions(b, filter, now) {
|
|
286
|
+
function filterConditions(b, filter, folders, now) {
|
|
338
287
|
const status = filter.status ?? "active";
|
|
339
288
|
const parts = [
|
|
340
289
|
b("scope", "=", filter.scope),
|
|
341
290
|
status === "pending" ? b.and(b("status", "=", "pending"), b("expiresAt", ">=", now)) : b("status", "=", status)
|
|
342
291
|
];
|
|
343
|
-
if (
|
|
344
|
-
else if (
|
|
345
|
-
parts.push(
|
|
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
|
+
);
|
|
346
297
|
}
|
|
347
298
|
if (filter.route) parts.push(b("route", "=", filter.route));
|
|
348
299
|
if (filter.contentType?.endsWith("/*")) {
|
|
@@ -351,11 +302,24 @@ function filterConditions(b, filter, now) {
|
|
|
351
302
|
} else if (filter.contentType) {
|
|
352
303
|
parts.push(b("contentType", "=", filter.contentType));
|
|
353
304
|
}
|
|
354
|
-
|
|
305
|
+
const needle = filter.search ? toSearchName(filter.search) : "";
|
|
306
|
+
if (needle) parts.push(b("searchName", "contains", needle));
|
|
355
307
|
return parts;
|
|
356
308
|
}
|
|
357
|
-
function
|
|
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) {
|
|
358
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
|
+
};
|
|
359
323
|
const find = async (ref) => {
|
|
360
324
|
const orm = await getOrm();
|
|
361
325
|
const row = await orm.findFirst("storageObject", {
|
|
@@ -381,7 +345,8 @@ function createObjectStore(getOrm) {
|
|
|
381
345
|
const after = direction === "desc" ? "<" : ">";
|
|
382
346
|
const nullable = field === "size";
|
|
383
347
|
const filters = {
|
|
384
|
-
|
|
348
|
+
folderId: input.folderId,
|
|
349
|
+
recursive: input.recursive === true && input.folderId !== void 0,
|
|
385
350
|
search: input.search || void 0,
|
|
386
351
|
status: input.status ?? "active",
|
|
387
352
|
route: input.route || void 0,
|
|
@@ -389,8 +354,9 @@ function createObjectStore(getOrm) {
|
|
|
389
354
|
sort
|
|
390
355
|
};
|
|
391
356
|
const cursor = input.cursor ? decodeListCursor(input.cursor, filters) : null;
|
|
357
|
+
const folders = await folderSet(input);
|
|
392
358
|
const where = (segment) => (b) => {
|
|
393
|
-
const parts = filterConditions(b, input, now);
|
|
359
|
+
const parts = filterConditions(b, input, folders, now);
|
|
394
360
|
const position = segment.after;
|
|
395
361
|
if (segment.nulls) {
|
|
396
362
|
parts.push(b.isNull(field));
|
|
@@ -398,13 +364,11 @@ function createObjectStore(getOrm) {
|
|
|
398
364
|
} else {
|
|
399
365
|
if (nullable) parts.push(b.isNotNull(field));
|
|
400
366
|
if (position && position.value !== null) {
|
|
367
|
+
const value = toColumnValue(position.value);
|
|
401
368
|
parts.push(
|
|
402
369
|
b.or(
|
|
403
|
-
b(field, after,
|
|
404
|
-
b.and(
|
|
405
|
-
b(field, "=", position.value),
|
|
406
|
-
b("id", after, position.id)
|
|
407
|
-
)
|
|
370
|
+
b(field, after, value),
|
|
371
|
+
b.and(b(field, "=", value), b("id", after, position.id))
|
|
408
372
|
)
|
|
409
373
|
);
|
|
410
374
|
}
|
|
@@ -445,8 +409,9 @@ function createObjectStore(getOrm) {
|
|
|
445
409
|
async count(filter) {
|
|
446
410
|
const orm = await getOrm();
|
|
447
411
|
const now = /* @__PURE__ */ new Date();
|
|
412
|
+
const folders = await folderSet(filter);
|
|
448
413
|
return orm.count("storageObject", {
|
|
449
|
-
where: (b) => b.and(...filterConditions(b, filter, now))
|
|
414
|
+
where: (b) => b.and(...filterConditions(b, filter, folders, now))
|
|
450
415
|
});
|
|
451
416
|
},
|
|
452
417
|
async getUsage(scope, options = {}) {
|
|
@@ -489,7 +454,7 @@ function createObjectStore(getOrm) {
|
|
|
489
454
|
const upload = {
|
|
490
455
|
uploadId: input.uploadId ?? null,
|
|
491
456
|
uploadToken: input.uploadToken,
|
|
492
|
-
declaredSize: input.declaredSize
|
|
457
|
+
declaredSize: toByteCount(input.declaredSize),
|
|
493
458
|
expiresAt: input.expiresAt
|
|
494
459
|
};
|
|
495
460
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -498,8 +463,9 @@ function createObjectStore(getOrm) {
|
|
|
498
463
|
...ref,
|
|
499
464
|
scope: input.scope,
|
|
500
465
|
route: input.route,
|
|
501
|
-
|
|
466
|
+
folderId: input.folderId,
|
|
502
467
|
filename: input.filename,
|
|
468
|
+
searchName: toSearchName(input.filename),
|
|
503
469
|
contentType: input.contentType ?? null,
|
|
504
470
|
size: null,
|
|
505
471
|
eTag: null,
|
|
@@ -516,7 +482,7 @@ function createObjectStore(getOrm) {
|
|
|
516
482
|
try {
|
|
517
483
|
await orm.create("storageObject", fresh);
|
|
518
484
|
} catch (error) {
|
|
519
|
-
if (
|
|
485
|
+
if (error instanceof UniqueConstraintError) throw s3Error("CONFLICT");
|
|
520
486
|
throw error;
|
|
521
487
|
}
|
|
522
488
|
};
|
|
@@ -551,13 +517,16 @@ function createObjectStore(getOrm) {
|
|
|
551
517
|
where: (b) => b.and(byUpload(b, input), uploading(b)),
|
|
552
518
|
set: {
|
|
553
519
|
status: "active",
|
|
554
|
-
size: input.size,
|
|
520
|
+
size: BigInt(input.size),
|
|
555
521
|
declaredSize: null,
|
|
556
522
|
eTag: input.eTag ?? null,
|
|
557
523
|
...input.contentType !== void 0 && {
|
|
558
524
|
contentType: input.contentType
|
|
559
525
|
},
|
|
560
|
-
...input.filename !== void 0 && {
|
|
526
|
+
...input.filename !== void 0 && {
|
|
527
|
+
filename: input.filename,
|
|
528
|
+
searchName: toSearchName(input.filename)
|
|
529
|
+
},
|
|
561
530
|
...input.s3Metadata !== void 0 && {
|
|
562
531
|
s3Metadata: input.s3Metadata
|
|
563
532
|
},
|
|
@@ -584,8 +553,11 @@ function createObjectStore(getOrm) {
|
|
|
584
553
|
ref.scope === void 0 || b("scope", "=", ref.scope)
|
|
585
554
|
),
|
|
586
555
|
set: {
|
|
587
|
-
...input.filename !== void 0 && {
|
|
588
|
-
|
|
556
|
+
...input.filename !== void 0 && {
|
|
557
|
+
filename: input.filename,
|
|
558
|
+
searchName: toSearchName(input.filename)
|
|
559
|
+
},
|
|
560
|
+
...input.folderId !== void 0 && { folderId: input.folderId },
|
|
589
561
|
...input.attributes !== void 0 && {
|
|
590
562
|
attributes: input.attributes
|
|
591
563
|
},
|
|
@@ -694,72 +666,154 @@ function createObjectStore(getOrm) {
|
|
|
694
666
|
|
|
695
667
|
// src/store/folder-store.ts
|
|
696
668
|
var DEFAULT_FOLDER_CHILDREN_LIMIT = 1e3;
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
669
|
+
var DEFAULT_FOLDER_DESCENDANTS_LIMIT = 1e3;
|
|
670
|
+
var FOLDER_SUBTREE_LIMIT = 1e4;
|
|
671
|
+
var IN_BATCH = 500;
|
|
672
|
+
function storedParent(parentId) {
|
|
673
|
+
return parentId ?? "";
|
|
674
|
+
}
|
|
675
|
+
function readParent(parentId) {
|
|
676
|
+
return parentId === "" ? null : parentId;
|
|
677
|
+
}
|
|
678
|
+
function batches(values, size) {
|
|
679
|
+
const runs = [];
|
|
680
|
+
for (let index = 0; index < values.length; index += size) {
|
|
681
|
+
runs.push(values.slice(index, index + size));
|
|
682
|
+
}
|
|
683
|
+
return runs;
|
|
684
|
+
}
|
|
685
|
+
function byNameThenPath(a, b) {
|
|
686
|
+
return a.name.localeCompare(b.name, void 0, {
|
|
687
|
+
numeric: true,
|
|
688
|
+
sensitivity: "base"
|
|
689
|
+
}) || (a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
690
|
+
}
|
|
691
|
+
function mapFolder(row) {
|
|
692
|
+
return {
|
|
693
|
+
id: row.id,
|
|
694
|
+
scope: row.scope,
|
|
695
|
+
parentId: readParent(row.parentId),
|
|
696
|
+
name: row.name,
|
|
697
|
+
path: row.path,
|
|
698
|
+
createdAt: row.createdAt,
|
|
699
|
+
updatedAt: row.updatedAt
|
|
700
|
+
};
|
|
705
701
|
}
|
|
706
702
|
function createFolderStore(getOrm) {
|
|
707
|
-
const find = async (scope,
|
|
703
|
+
const find = async (scope, id) => {
|
|
708
704
|
const orm = await getOrm();
|
|
709
|
-
|
|
710
|
-
where: (b) => b.and(b("scope", "=", scope), b("
|
|
705
|
+
const row = await orm.findFirst("storageFolder", {
|
|
706
|
+
where: (b) => b.and(b("scope", "=", scope), b("id", "=", id))
|
|
711
707
|
});
|
|
708
|
+
return row ? mapFolder(row) : null;
|
|
712
709
|
};
|
|
713
|
-
const
|
|
714
|
-
const
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
710
|
+
const insertFolder = async (orm, folder) => {
|
|
711
|
+
const now = /* @__PURE__ */ new Date();
|
|
712
|
+
const row = await orm.create("storageFolder", {
|
|
713
|
+
...folder.id !== void 0 && { id: folder.id },
|
|
714
|
+
scope: folder.scope,
|
|
715
|
+
parentId: storedParent(folder.parentId),
|
|
716
|
+
name: folder.name,
|
|
717
|
+
searchName: toSearchName(folder.name),
|
|
718
|
+
path: folder.path,
|
|
719
|
+
createdAt: folder.createdAt ?? now,
|
|
720
|
+
updatedAt: folder.updatedAt ?? now
|
|
721
|
+
});
|
|
722
|
+
return mapFolder(row);
|
|
723
723
|
};
|
|
724
|
-
const
|
|
725
|
-
if (
|
|
726
|
-
|
|
727
|
-
const
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
724
|
+
const destination = async (scope, parentId, folderId) => {
|
|
725
|
+
if (parentId === null) return null;
|
|
726
|
+
if (parentId === folderId) throw s3Error("FOLDER_INVALID");
|
|
727
|
+
const parent = await find(scope, parentId);
|
|
728
|
+
if (!parent) throw s3Error("FOLDER_NOT_FOUND");
|
|
729
|
+
if (folderId !== void 0) {
|
|
730
|
+
let cursor = parent;
|
|
731
|
+
const seen = /* @__PURE__ */ new Set();
|
|
732
|
+
while (cursor && !seen.has(cursor.id)) {
|
|
733
|
+
if (cursor.id === folderId) throw s3Error("FOLDER_INVALID");
|
|
734
|
+
seen.add(cursor.id);
|
|
735
|
+
cursor = cursor.parentId ? await find(scope, cursor.parentId) : null;
|
|
736
|
+
}
|
|
736
737
|
}
|
|
738
|
+
return parent;
|
|
737
739
|
};
|
|
738
|
-
const countContents = async (scope,
|
|
740
|
+
const countContents = async (scope, id) => {
|
|
739
741
|
const orm = await getOrm();
|
|
740
742
|
const now = /* @__PURE__ */ new Date();
|
|
741
743
|
const [folders, objects] = await Promise.all([
|
|
742
744
|
orm.count("storageFolder", {
|
|
743
|
-
where: (b) => b.and(b("scope", "=", scope), b("
|
|
745
|
+
where: (b) => b.and(b("scope", "=", scope), b("parentId", "=", id))
|
|
744
746
|
}),
|
|
745
747
|
orm.count("storageObject", {
|
|
746
748
|
where: (b) => b.and(
|
|
747
749
|
b("scope", "=", scope),
|
|
748
|
-
b("
|
|
750
|
+
b("folderId", "=", id),
|
|
749
751
|
liveObject(b, now)
|
|
750
752
|
)
|
|
751
753
|
})
|
|
752
754
|
]);
|
|
753
755
|
return folders + objects;
|
|
754
756
|
};
|
|
757
|
+
const subtree = async (scope, id, limit = Infinity) => {
|
|
758
|
+
const orm = await getOrm();
|
|
759
|
+
const found = [];
|
|
760
|
+
const seen = /* @__PURE__ */ new Set([id]);
|
|
761
|
+
let level = [id];
|
|
762
|
+
for (let depth = 0; depth < FOLDER_MAX_DEPTH && level.length > 0; depth++) {
|
|
763
|
+
const next = [];
|
|
764
|
+
for (const parents of batches(level, IN_BATCH)) {
|
|
765
|
+
const rows = await orm.findMany("storageFolder", {
|
|
766
|
+
where: (b) => b.and(b("scope", "=", scope), b("parentId", "in", parents)),
|
|
767
|
+
orderBy: [["id", "asc"]]
|
|
768
|
+
});
|
|
769
|
+
for (const row of rows) {
|
|
770
|
+
if (seen.has(row.id)) continue;
|
|
771
|
+
seen.add(row.id);
|
|
772
|
+
found.push(mapFolder(row));
|
|
773
|
+
next.push(row.id);
|
|
774
|
+
if (found.length >= limit) return found;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
level = next;
|
|
778
|
+
}
|
|
779
|
+
return found;
|
|
780
|
+
};
|
|
781
|
+
const walks = /* @__PURE__ */ new Map();
|
|
782
|
+
const sharedSubtree = (scope, id) => {
|
|
783
|
+
const key = JSON.stringify([scope, id]);
|
|
784
|
+
let walk = walks.get(key);
|
|
785
|
+
if (!walk) {
|
|
786
|
+
walk = subtree(scope, id, FOLDER_SUBTREE_LIMIT).finally(() => {
|
|
787
|
+
walks.delete(key);
|
|
788
|
+
});
|
|
789
|
+
walks.set(key, walk);
|
|
790
|
+
}
|
|
791
|
+
return walk;
|
|
792
|
+
};
|
|
793
|
+
const retain = async (folder) => {
|
|
794
|
+
if (await find(folder.scope, folder.id)) return;
|
|
795
|
+
const orm = await getOrm();
|
|
796
|
+
try {
|
|
797
|
+
await insertFolder(orm, folder);
|
|
798
|
+
} catch (error) {
|
|
799
|
+
if (error instanceof UniqueConstraintError && await find(folder.scope, folder.id)) {
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
throw error;
|
|
803
|
+
}
|
|
804
|
+
};
|
|
755
805
|
return {
|
|
756
806
|
find,
|
|
757
|
-
async
|
|
807
|
+
async subtreeIds(scope, id) {
|
|
808
|
+
const below = await sharedSubtree(scope, id);
|
|
809
|
+
return below.map((folder) => folder.id);
|
|
810
|
+
},
|
|
811
|
+
async children(scope, parentId, options = {}) {
|
|
758
812
|
const orm = await getOrm();
|
|
759
|
-
|
|
813
|
+
const rows = await orm.findMany("storageFolder", {
|
|
760
814
|
where: (b) => b.and(
|
|
761
815
|
b("scope", "=", scope),
|
|
762
|
-
|
|
816
|
+
b("parentId", "=", storedParent(parentId))
|
|
763
817
|
),
|
|
764
818
|
orderBy: [
|
|
765
819
|
["name", "asc"],
|
|
@@ -767,33 +821,160 @@ function createFolderStore(getOrm) {
|
|
|
767
821
|
],
|
|
768
822
|
limit: options.limit ?? DEFAULT_FOLDER_CHILDREN_LIMIT
|
|
769
823
|
});
|
|
824
|
+
return rows.map(mapFolder);
|
|
825
|
+
},
|
|
826
|
+
async trail(scope, id) {
|
|
827
|
+
const chain = [];
|
|
828
|
+
const seen = /* @__PURE__ */ new Set();
|
|
829
|
+
let current = await find(scope, id);
|
|
830
|
+
if (!current) throw s3Error("FOLDER_NOT_FOUND");
|
|
831
|
+
while (current && !seen.has(current.id)) {
|
|
832
|
+
seen.add(current.id);
|
|
833
|
+
chain.push(current);
|
|
834
|
+
if (!current.parentId || chain.length > FOLDER_MAX_DEPTH) break;
|
|
835
|
+
current = await find(scope, current.parentId);
|
|
836
|
+
}
|
|
837
|
+
return chain.reverse();
|
|
838
|
+
},
|
|
839
|
+
async descendants(scope, parentId, options = {}) {
|
|
840
|
+
const limit = options.limit ?? DEFAULT_FOLDER_DESCENDANTS_LIMIT;
|
|
841
|
+
const needle = options.search ? toSearchName(options.search) : "";
|
|
842
|
+
let folders;
|
|
843
|
+
if (parentId === null) {
|
|
844
|
+
const orm = await getOrm();
|
|
845
|
+
const rows = await orm.findMany("storageFolder", {
|
|
846
|
+
where: (b) => b.and(
|
|
847
|
+
b("scope", "=", scope),
|
|
848
|
+
needle === "" || b("searchName", "contains", needle)
|
|
849
|
+
),
|
|
850
|
+
orderBy: [["path", "asc"]],
|
|
851
|
+
limit: FOLDER_SUBTREE_LIMIT
|
|
852
|
+
});
|
|
853
|
+
folders = rows.map(mapFolder);
|
|
854
|
+
} else {
|
|
855
|
+
const [parent, below] = await Promise.all([
|
|
856
|
+
find(scope, parentId),
|
|
857
|
+
sharedSubtree(scope, parentId)
|
|
858
|
+
]);
|
|
859
|
+
if (!parent) throw s3Error("FOLDER_NOT_FOUND");
|
|
860
|
+
folders = below.filter(
|
|
861
|
+
(folder) => needle === "" || toSearchName(folder.name).includes(needle)
|
|
862
|
+
);
|
|
863
|
+
}
|
|
864
|
+
return folders.sort(byNameThenPath).slice(0, limit);
|
|
865
|
+
},
|
|
866
|
+
async ancestors(scope, ids) {
|
|
867
|
+
if (ids.length === 0) return [];
|
|
868
|
+
const orm = await getOrm();
|
|
869
|
+
const byPath = /* @__PURE__ */ new Map();
|
|
870
|
+
for (const run of batches([...new Set(ids)], IN_BATCH)) {
|
|
871
|
+
const rows = await orm.findMany("storageFolder", {
|
|
872
|
+
where: (b) => b.and(b("scope", "=", scope), b("id", "in", run))
|
|
873
|
+
});
|
|
874
|
+
for (const row of rows) byPath.set(row.path, mapFolder(row));
|
|
875
|
+
}
|
|
876
|
+
const missing = /* @__PURE__ */ new Set();
|
|
877
|
+
for (const path of byPath.keys()) {
|
|
878
|
+
for (const above of folderAncestors(path)) {
|
|
879
|
+
if (!byPath.has(above)) missing.add(above);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
for (const run of batches([...missing], IN_BATCH)) {
|
|
883
|
+
const rows = await orm.findMany("storageFolder", {
|
|
884
|
+
where: (b) => b.and(b("scope", "=", scope), b("path", "in", run))
|
|
885
|
+
});
|
|
886
|
+
for (const row of rows) byPath.set(row.path, mapFolder(row));
|
|
887
|
+
}
|
|
888
|
+
return [...byPath.values()].sort(
|
|
889
|
+
(a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0
|
|
890
|
+
);
|
|
770
891
|
},
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
892
|
+
async create(scope, input) {
|
|
893
|
+
const parent = await destination(scope, input.parentId);
|
|
894
|
+
const path = childFolderPath(parent?.path ?? null, input.name);
|
|
895
|
+
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
896
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
897
|
+
const orm = await getOrm();
|
|
775
898
|
try {
|
|
776
|
-
|
|
899
|
+
return await insertFolder(orm, {
|
|
900
|
+
scope,
|
|
901
|
+
parentId: input.parentId,
|
|
902
|
+
name,
|
|
903
|
+
path
|
|
904
|
+
});
|
|
777
905
|
} catch (error) {
|
|
778
|
-
if (
|
|
906
|
+
if (error instanceof UniqueConstraintError) {
|
|
907
|
+
throw s3Error("FOLDER_EXISTS");
|
|
908
|
+
}
|
|
779
909
|
throw error;
|
|
780
910
|
}
|
|
781
|
-
await ensure(scope, parentFolderPath(path));
|
|
782
|
-
return folder;
|
|
783
911
|
},
|
|
784
|
-
async
|
|
785
|
-
|
|
912
|
+
async update(scope, id, input) {
|
|
913
|
+
const folder = await find(scope, id);
|
|
914
|
+
if (!folder) throw s3Error("FOLDER_NOT_FOUND");
|
|
915
|
+
const name = input.name ?? folder.name;
|
|
916
|
+
const parentId = input.parentId === void 0 ? folder.parentId : input.parentId;
|
|
917
|
+
if (name === folder.name && parentId === folder.parentId) return folder;
|
|
918
|
+
const parent = await destination(scope, parentId, id);
|
|
919
|
+
const path = childFolderPath(parent?.path ?? null, name);
|
|
920
|
+
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
921
|
+
const storedName = path.slice(path.lastIndexOf("/") + 1);
|
|
922
|
+
const below = await subtree(scope, id);
|
|
923
|
+
const prefix = folder.path;
|
|
924
|
+
const rewritten = below.map((child) => ({
|
|
925
|
+
id: child.id,
|
|
926
|
+
path: `${path}${child.path.slice(prefix.length)}`
|
|
927
|
+
}));
|
|
928
|
+
for (const next of [path, ...rewritten.map((child) => child.path)]) {
|
|
929
|
+
if (normalizeFolderPath(next) !== next) throw s3Error("FOLDER_INVALID");
|
|
930
|
+
}
|
|
931
|
+
const orm = await getOrm();
|
|
932
|
+
const now = /* @__PURE__ */ new Date();
|
|
933
|
+
await orm.transaction(async (tx) => {
|
|
934
|
+
try {
|
|
935
|
+
await tx.updateMany("storageFolder", {
|
|
936
|
+
where: (b) => b.and(b("scope", "=", scope), b("id", "=", id)),
|
|
937
|
+
set: {
|
|
938
|
+
name: storedName,
|
|
939
|
+
searchName: toSearchName(storedName),
|
|
940
|
+
parentId: storedParent(parentId),
|
|
941
|
+
path,
|
|
942
|
+
updatedAt: now
|
|
943
|
+
}
|
|
944
|
+
});
|
|
945
|
+
} catch (error) {
|
|
946
|
+
if (error instanceof UniqueConstraintError) {
|
|
947
|
+
throw s3Error("FOLDER_EXISTS");
|
|
948
|
+
}
|
|
949
|
+
throw error;
|
|
950
|
+
}
|
|
951
|
+
for (const child of rewritten) {
|
|
952
|
+
await tx.updateMany("storageFolder", {
|
|
953
|
+
where: (b) => b.and(b("scope", "=", scope), b("id", "=", child.id)),
|
|
954
|
+
set: { path: child.path, updatedAt: now }
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
});
|
|
958
|
+
const updated = await find(scope, id);
|
|
959
|
+
if (!updated) throw s3Error("FOLDER_NOT_FOUND");
|
|
960
|
+
return updated;
|
|
961
|
+
},
|
|
962
|
+
async delete(scope, id) {
|
|
963
|
+
const folder = await find(scope, id);
|
|
964
|
+
if (!folder) return;
|
|
965
|
+
if (await countContents(scope, id) > 0) {
|
|
786
966
|
throw s3Error("FOLDER_NOT_EMPTY");
|
|
787
967
|
}
|
|
788
968
|
const orm = await getOrm();
|
|
789
969
|
await orm.deleteMany("storageFolder", {
|
|
790
|
-
where: (b) => b.and(b("scope", "=", scope), b("
|
|
970
|
+
where: (b) => b.and(b("scope", "=", scope), b("id", "=", id))
|
|
791
971
|
});
|
|
792
|
-
if (await countContents(scope,
|
|
793
|
-
await
|
|
972
|
+
if (await countContents(scope, id) > 0) {
|
|
973
|
+
await retain(folder);
|
|
794
974
|
throw s3Error("FOLDER_NOT_EMPTY");
|
|
795
975
|
}
|
|
796
|
-
}
|
|
976
|
+
},
|
|
977
|
+
retain
|
|
797
978
|
};
|
|
798
979
|
}
|
|
799
980
|
|
|
@@ -815,7 +996,7 @@ async function checkSchemaVersion(client) {
|
|
|
815
996
|
}
|
|
816
997
|
if (version !== DB_SCHEMA_VERSION) {
|
|
817
998
|
throw new Error(
|
|
818
|
-
`[dimah-s3/db] The database is at schema ${version}, but @dimah-s3/db
|
|
999
|
+
`[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.`
|
|
819
1000
|
);
|
|
820
1001
|
}
|
|
821
1002
|
}
|
|
@@ -843,16 +1024,17 @@ function createDbStore(client) {
|
|
|
843
1024
|
await checkSchema();
|
|
844
1025
|
return client.orm(DB_SCHEMA_VERSION);
|
|
845
1026
|
};
|
|
1027
|
+
const folders = createFolderStore(getOrm);
|
|
846
1028
|
return {
|
|
847
|
-
objects: createObjectStore(getOrm),
|
|
848
|
-
folders
|
|
1029
|
+
objects: createObjectStore(getOrm, folders.subtreeIds),
|
|
1030
|
+
folders,
|
|
849
1031
|
checkSchema
|
|
850
1032
|
};
|
|
851
1033
|
}
|
|
852
1034
|
|
|
853
1035
|
// src/standard-schema.ts
|
|
854
|
-
async function validateStandardSchema(
|
|
855
|
-
return
|
|
1036
|
+
async function validateStandardSchema(schema2, value) {
|
|
1037
|
+
return schema2["~standard"].validate(value);
|
|
856
1038
|
}
|
|
857
1039
|
function formatStandardIssue(issues) {
|
|
858
1040
|
const [issue] = issues;
|
|
@@ -865,16 +1047,24 @@ function formatStandardIssue(issues) {
|
|
|
865
1047
|
var DB_LIST_MAX_LIMIT = 100;
|
|
866
1048
|
var MAX_SEARCH_LENGTH = 255;
|
|
867
1049
|
var MAX_CURSOR_LENGTH = 2048;
|
|
868
|
-
function queryInt(
|
|
1050
|
+
function queryInt(schema2) {
|
|
869
1051
|
return z.codec(
|
|
870
|
-
z.union([z.string().trim().regex(z.regexes.integer),
|
|
871
|
-
|
|
1052
|
+
z.union([z.string().trim().regex(z.regexes.integer), schema2]),
|
|
1053
|
+
schema2,
|
|
872
1054
|
{
|
|
873
1055
|
decode: (value) => typeof value === "number" ? value : Number.parseInt(value, 10),
|
|
874
1056
|
encode: (value) => value
|
|
875
1057
|
}
|
|
876
1058
|
);
|
|
877
1059
|
}
|
|
1060
|
+
var queryBoolean = z.codec(
|
|
1061
|
+
z.union([z.enum(["true", "false"]), z.boolean()]),
|
|
1062
|
+
z.boolean(),
|
|
1063
|
+
{
|
|
1064
|
+
decode: (value) => value === true || value === "true",
|
|
1065
|
+
encode: (value) => value
|
|
1066
|
+
}
|
|
1067
|
+
);
|
|
878
1068
|
var dbListSortSchema = z.object({
|
|
879
1069
|
field: z.enum(["createdAt", "updatedAt", "size", "filename"]),
|
|
880
1070
|
direction: z.enum(["asc", "desc"])
|
|
@@ -901,12 +1091,13 @@ var dbListSortQuerySchema = z.codec(
|
|
|
901
1091
|
}
|
|
902
1092
|
);
|
|
903
1093
|
var dbListQuerySchema = z.strictObject({
|
|
904
|
-
|
|
1094
|
+
folderId: z.string().optional(),
|
|
1095
|
+
recursive: queryBoolean.optional(),
|
|
905
1096
|
route: routeNameSchema.optional(),
|
|
906
1097
|
// Rows that purge has claimed stay internal, as on `GET /db/object`.
|
|
907
1098
|
status: z.enum(["pending", "active", "deleted"]).optional(),
|
|
908
1099
|
contentType: optionalContentTypeSchema,
|
|
909
|
-
/** Text the filename contains. */
|
|
1100
|
+
/** Text the filename contains, ignoring case and accents. */
|
|
910
1101
|
search: optionalTrimmedString.pipe(
|
|
911
1102
|
z.string().max(MAX_SEARCH_LENGTH).optional()
|
|
912
1103
|
),
|
|
@@ -926,12 +1117,21 @@ var dbUpdateObjectBodySchema = z.strictObject({
|
|
|
926
1117
|
key: objectKeySchema,
|
|
927
1118
|
bucket: trimmedString.optional(),
|
|
928
1119
|
filename: fileNameSchema.optional(),
|
|
929
|
-
|
|
1120
|
+
folderId: z.string().nullable().optional(),
|
|
930
1121
|
/** `null` clears the attributes. */
|
|
931
1122
|
attributes: z.record(z.string(), z.unknown()).nullable().optional()
|
|
932
1123
|
});
|
|
933
|
-
var
|
|
934
|
-
|
|
1124
|
+
var dbCreateFolderBodySchema = z.strictObject({
|
|
1125
|
+
parentId: z.string().nullable().optional(),
|
|
1126
|
+
name: z.string()
|
|
1127
|
+
});
|
|
1128
|
+
var dbUpdateFolderBodySchema = z.strictObject({
|
|
1129
|
+
id: trimmedString,
|
|
1130
|
+
name: z.string().optional(),
|
|
1131
|
+
parentId: z.string().nullable().optional()
|
|
1132
|
+
});
|
|
1133
|
+
var dbDeleteFolderQuerySchema = z.strictObject({
|
|
1134
|
+
id: trimmedString
|
|
935
1135
|
});
|
|
936
1136
|
|
|
937
1137
|
// src/plugin/to-db-client-object.ts
|
|
@@ -942,7 +1142,7 @@ function toDbClientObject(object2, toClient) {
|
|
|
942
1142
|
bucket: object2.bucket,
|
|
943
1143
|
key: object2.key,
|
|
944
1144
|
route: object2.route,
|
|
945
|
-
|
|
1145
|
+
folderId: object2.folderId,
|
|
946
1146
|
filename: object2.filename,
|
|
947
1147
|
contentType: object2.contentType,
|
|
948
1148
|
size: object2.size,
|
|
@@ -960,16 +1160,18 @@ function toDbClientObject(object2, toClient) {
|
|
|
960
1160
|
}
|
|
961
1161
|
function toDbClientFolder(folder) {
|
|
962
1162
|
return {
|
|
963
|
-
|
|
1163
|
+
id: folder.id,
|
|
964
1164
|
name: folder.name,
|
|
965
|
-
|
|
966
|
-
|
|
1165
|
+
parentId: folder.parentId,
|
|
1166
|
+
path: folder.path,
|
|
1167
|
+
createdAt: folder.createdAt.toISOString(),
|
|
1168
|
+
updatedAt: folder.updatedAt.toISOString()
|
|
967
1169
|
};
|
|
968
1170
|
}
|
|
969
1171
|
|
|
970
1172
|
// src/plugin/create-database-endpoints.ts
|
|
971
1173
|
var DEFAULT_ATTRIBUTES_MAX_BYTES = 16384;
|
|
972
|
-
var
|
|
1174
|
+
var utf82 = new TextEncoder();
|
|
973
1175
|
function resolveBucket(config, bucket, field) {
|
|
974
1176
|
const buckets = /* @__PURE__ */ new Set();
|
|
975
1177
|
for (const route of Object.values(config.routes)) {
|
|
@@ -992,16 +1194,18 @@ function resolveBucket(config, bucket, field) {
|
|
|
992
1194
|
}
|
|
993
1195
|
function matchingFolders(children, search) {
|
|
994
1196
|
if (!search) return children;
|
|
995
|
-
|
|
996
|
-
return children.filter((child) => child.name.toLowerCase().includes(text));
|
|
1197
|
+
return children.filter((child) => matchesSearchText(child.name, search));
|
|
997
1198
|
}
|
|
998
|
-
function
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1199
|
+
function wireFolderId(value) {
|
|
1200
|
+
if (value === void 0) return void 0;
|
|
1201
|
+
return value === "" || value === null ? null : value;
|
|
1202
|
+
}
|
|
1203
|
+
function folderTarget(folder) {
|
|
1204
|
+
if (folder === null) return { folder: null, folderId: null };
|
|
1205
|
+
return { folder: folder.path, folderId: folder.id };
|
|
1002
1206
|
}
|
|
1003
1207
|
function createDatabaseEndpoints(options) {
|
|
1004
|
-
const { access, quota, toClientObject } = options;
|
|
1208
|
+
const { access, quota, resolveScope, toClientObject } = options;
|
|
1005
1209
|
const { objects, folders } = options.store;
|
|
1006
1210
|
const parseAttributes = async (value) => {
|
|
1007
1211
|
const { attributes } = options;
|
|
@@ -1017,7 +1221,7 @@ function createDatabaseEndpoints(options) {
|
|
|
1017
1221
|
});
|
|
1018
1222
|
}
|
|
1019
1223
|
const maxBytes = attributes.maxBytes ?? DEFAULT_ATTRIBUTES_MAX_BYTES;
|
|
1020
|
-
const size =
|
|
1224
|
+
const size = utf82.encode(JSON.stringify(result.value)).byteLength;
|
|
1021
1225
|
if (size > maxBytes) {
|
|
1022
1226
|
throw s3Error("VALIDATION_ERROR", {
|
|
1023
1227
|
issue: `[body.attributes] Exceeds ${maxBytes} bytes`
|
|
@@ -1025,43 +1229,80 @@ function createDatabaseEndpoints(options) {
|
|
|
1025
1229
|
}
|
|
1026
1230
|
return result.value;
|
|
1027
1231
|
};
|
|
1232
|
+
const openFolder = async (scope, wire) => {
|
|
1233
|
+
const folderId = wireFolderId(wire);
|
|
1234
|
+
if (folderId === void 0) return void 0;
|
|
1235
|
+
if (folderId === null) return null;
|
|
1236
|
+
const folder = await folders.find(scope, folderId);
|
|
1237
|
+
if (!folder) throw s3Error("FOLDER_NOT_FOUND");
|
|
1238
|
+
return folder;
|
|
1239
|
+
};
|
|
1028
1240
|
return {
|
|
1029
|
-
/** One page of the caller's objects. `
|
|
1241
|
+
/** One page of the caller's objects. `folderId` omitted lists every folder. */
|
|
1030
1242
|
objects: createS3Endpoint(
|
|
1031
1243
|
pluginPath("db", "objects"),
|
|
1032
1244
|
{ method: "GET", query: dbListQuerySchema },
|
|
1033
1245
|
async (ctx) => {
|
|
1034
|
-
const {
|
|
1035
|
-
|
|
1246
|
+
const {
|
|
1247
|
+
folderId: wireFolder,
|
|
1248
|
+
recursive: wantsSubtree,
|
|
1249
|
+
...filters
|
|
1250
|
+
} = ctx.query;
|
|
1251
|
+
const peeked = await resolveScope(ctx.context, {
|
|
1252
|
+
operation: "list",
|
|
1253
|
+
route: filters.route
|
|
1254
|
+
});
|
|
1255
|
+
const opened = await openFolder(peeked.scope, wireFolder);
|
|
1256
|
+
const recursive = wantsSubtree === true && opened !== void 0;
|
|
1036
1257
|
const scope = await access.scope(ctx.context, {
|
|
1037
1258
|
operation: "list",
|
|
1038
1259
|
route: filters.route,
|
|
1039
|
-
...
|
|
1040
|
-
|
|
1041
|
-
const page = await objects.list({
|
|
1042
|
-
...filters,
|
|
1043
|
-
scope: scope.scope,
|
|
1044
|
-
folder
|
|
1260
|
+
...opened !== void 0 && folderTarget(opened),
|
|
1261
|
+
...recursive && { recursive }
|
|
1045
1262
|
});
|
|
1263
|
+
const folderId = opened === void 0 ? void 0 : opened?.id ?? null;
|
|
1264
|
+
const filter = { ...filters, scope: scope.scope, folderId, recursive };
|
|
1265
|
+
const firstPage = () => Promise.all([
|
|
1266
|
+
objects.getUsage(scope.scope),
|
|
1267
|
+
quota.limits(scope),
|
|
1268
|
+
opened === void 0 ? void 0 : recursive ? folders.descendants(scope.scope, folderId ?? null, {
|
|
1269
|
+
search: filters.search
|
|
1270
|
+
}) : folders.children(scope.scope, folderId ?? null).then(
|
|
1271
|
+
(children) => matchingFolders(children, filters.search)
|
|
1272
|
+
),
|
|
1273
|
+
opened === void 0 ? void 0 : opened === null ? [] : folders.trail(scope.scope, opened.id),
|
|
1274
|
+
filters.search ? objects.count(filter) : void 0
|
|
1275
|
+
]);
|
|
1276
|
+
const early = filters.cursor ? null : firstPage();
|
|
1277
|
+
early?.catch(() => void 0);
|
|
1278
|
+
const page = await objects.list(filter);
|
|
1046
1279
|
const response = {
|
|
1047
1280
|
objects: page.objects.map((object2) => toClientObject(object2)),
|
|
1048
1281
|
nextCursor: page.nextCursor
|
|
1049
1282
|
};
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1283
|
+
const located = page.objects.flatMap(
|
|
1284
|
+
(object2) => object2.folderId === null ? [] : [object2.folderId]
|
|
1285
|
+
);
|
|
1286
|
+
if (!page.firstPage) {
|
|
1287
|
+
if (!recursive) return response;
|
|
1288
|
+
const locations2 = await folders.ancestors(scope.scope, located);
|
|
1289
|
+
return { ...response, locations: locations2.map(toDbClientFolder) };
|
|
1290
|
+
}
|
|
1291
|
+
const [usage, limits, listed, trail, total] = await (early ?? firstPage());
|
|
1292
|
+
const locations = recursive ? await folders.ancestors(scope.scope, [
|
|
1293
|
+
...located,
|
|
1294
|
+
...(listed ?? []).flatMap(
|
|
1295
|
+
(folder) => folder.parentId === null ? [] : [folder.parentId]
|
|
1296
|
+
)
|
|
1297
|
+
]) : void 0;
|
|
1056
1298
|
return {
|
|
1057
1299
|
...response,
|
|
1058
1300
|
usage,
|
|
1059
1301
|
limits,
|
|
1060
|
-
...
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
}
|
|
1302
|
+
...listed && { folders: listed.map(toDbClientFolder) },
|
|
1303
|
+
...trail && { trail: trail.map(toDbClientFolder) },
|
|
1304
|
+
...locations && { locations: locations.map(toDbClientFolder) },
|
|
1305
|
+
...total !== void 0 && { total }
|
|
1065
1306
|
};
|
|
1066
1307
|
}
|
|
1067
1308
|
),
|
|
@@ -1087,8 +1328,7 @@ function createDatabaseEndpoints(options) {
|
|
|
1087
1328
|
),
|
|
1088
1329
|
/**
|
|
1089
1330
|
* Renames, moves, or sets the attributes of an active object. A move
|
|
1090
|
-
*
|
|
1091
|
-
* created when missing.
|
|
1331
|
+
* requires the target folder to exist and authorizes that folder.
|
|
1092
1332
|
*/
|
|
1093
1333
|
updateObject: createS3Endpoint(
|
|
1094
1334
|
pluginPath("db", "object"),
|
|
@@ -1100,55 +1340,95 @@ function createDatabaseEndpoints(options) {
|
|
|
1100
1340
|
ctx.body.bucket,
|
|
1101
1341
|
"body.bucket"
|
|
1102
1342
|
);
|
|
1103
|
-
const
|
|
1343
|
+
const folderId = wireFolderId(ctx.body.folderId);
|
|
1344
|
+
const peeked = await resolveScope(ctx.context, {
|
|
1345
|
+
operation: "update"
|
|
1346
|
+
});
|
|
1347
|
+
const opened = folderId === void 0 ? void 0 : await openFolder(peeked.scope, folderId ?? "");
|
|
1104
1348
|
const { scope, object: object2 } = await access.object(
|
|
1105
1349
|
ctx.context,
|
|
1106
1350
|
{
|
|
1107
1351
|
operation: "update",
|
|
1108
1352
|
key,
|
|
1109
|
-
...
|
|
1353
|
+
...opened !== void 0 && folderTarget(opened)
|
|
1110
1354
|
},
|
|
1111
1355
|
{ bucket, key }
|
|
1112
1356
|
);
|
|
1113
1357
|
if (object2.status !== "active") throw s3Error("OBJECT_NOT_FOUND");
|
|
1114
1358
|
const attributes = ctx.body.attributes === void 0 || ctx.body.attributes === null ? ctx.body.attributes : await parseAttributes(ctx.body.attributes);
|
|
1115
|
-
if (filename === void 0 &&
|
|
1359
|
+
if (filename === void 0 && folderId === void 0 && attributes === void 0) {
|
|
1116
1360
|
return { object: toClientObject(object2) };
|
|
1117
1361
|
}
|
|
1118
1362
|
const updated = await options.objects.update(
|
|
1119
1363
|
{ bucket, key, id: object2.id, scope: scope.scope },
|
|
1120
|
-
{ filename,
|
|
1364
|
+
{ filename, folderId, attributes }
|
|
1121
1365
|
);
|
|
1122
1366
|
if (!updated) throw s3Error("OBJECT_NOT_FOUND");
|
|
1123
1367
|
return { object: toClientObject(updated) };
|
|
1124
1368
|
}
|
|
1125
1369
|
),
|
|
1126
|
-
/** Creates
|
|
1370
|
+
/** Creates one folder inside a parent that already exists. */
|
|
1127
1371
|
createFolder: createS3Endpoint(
|
|
1128
1372
|
pluginPath("db", "folder"),
|
|
1129
|
-
{ method: "POST", body:
|
|
1373
|
+
{ method: "POST", body: dbCreateFolderBodySchema },
|
|
1130
1374
|
async (ctx) => {
|
|
1131
|
-
const
|
|
1375
|
+
const parentId = wireFolderId(ctx.body.parentId) ?? null;
|
|
1376
|
+
const peeked = await resolveScope(ctx.context, { operation: "update" });
|
|
1377
|
+
const parent = parentId === null ? null : await openFolder(peeked.scope, parentId);
|
|
1378
|
+
const path = childFolderPath(parent?.path ?? null, ctx.body.name);
|
|
1379
|
+
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
1132
1380
|
const scope = await access.scope(ctx.context, {
|
|
1133
1381
|
operation: "update",
|
|
1134
1382
|
folder: path
|
|
1135
1383
|
});
|
|
1136
|
-
const folder = await folders.create(scope.scope,
|
|
1384
|
+
const folder = await folders.create(scope.scope, {
|
|
1385
|
+
parentId,
|
|
1386
|
+
name: ctx.body.name
|
|
1387
|
+
});
|
|
1388
|
+
return { folder: toDbClientFolder(folder) };
|
|
1389
|
+
}
|
|
1390
|
+
),
|
|
1391
|
+
/** Renames a folder, moves it, or both. */
|
|
1392
|
+
updateFolder: createS3Endpoint(
|
|
1393
|
+
pluginPath("db", "folder"),
|
|
1394
|
+
{ method: "PATCH", body: dbUpdateFolderBodySchema },
|
|
1395
|
+
async (ctx) => {
|
|
1396
|
+
const peeked = await resolveScope(ctx.context, { operation: "update" });
|
|
1397
|
+
const current = await folders.find(peeked.scope, ctx.body.id);
|
|
1398
|
+
if (!current) throw s3Error("FOLDER_NOT_FOUND");
|
|
1399
|
+
const parentId = ctx.body.parentId === void 0 ? current.parentId : wireFolderId(ctx.body.parentId) ?? null;
|
|
1400
|
+
const parent = parentId === null ? null : await openFolder(peeked.scope, parentId);
|
|
1401
|
+
const path = childFolderPath(
|
|
1402
|
+
parent?.path ?? null,
|
|
1403
|
+
ctx.body.name ?? current.name
|
|
1404
|
+
);
|
|
1405
|
+
if (path === null) throw s3Error("FOLDER_INVALID");
|
|
1406
|
+
await access.scope(ctx.context, {
|
|
1407
|
+
operation: "update",
|
|
1408
|
+
folder: path,
|
|
1409
|
+
folderId: current.id
|
|
1410
|
+
});
|
|
1411
|
+
const folder = await folders.update(peeked.scope, current.id, {
|
|
1412
|
+
...ctx.body.name !== void 0 && { name: ctx.body.name },
|
|
1413
|
+
...ctx.body.parentId !== void 0 && { parentId }
|
|
1414
|
+
});
|
|
1137
1415
|
return { folder: toDbClientFolder(folder) };
|
|
1138
1416
|
}
|
|
1139
1417
|
),
|
|
1140
1418
|
/** Removes an empty folder. A missing folder is not an error. */
|
|
1141
1419
|
deleteFolder: createS3Endpoint(
|
|
1142
1420
|
pluginPath("db", "folder"),
|
|
1143
|
-
{ method: "DELETE", query:
|
|
1421
|
+
{ method: "DELETE", query: dbDeleteFolderQuerySchema },
|
|
1144
1422
|
async (ctx) => {
|
|
1145
|
-
const
|
|
1146
|
-
const
|
|
1423
|
+
const peeked = await resolveScope(ctx.context, { operation: "update" });
|
|
1424
|
+
const current = await folders.find(peeked.scope, ctx.query.id);
|
|
1425
|
+
if (!current) return { id: ctx.query.id };
|
|
1426
|
+
await access.scope(ctx.context, {
|
|
1147
1427
|
operation: "update",
|
|
1148
|
-
|
|
1428
|
+
...folderTarget(current)
|
|
1149
1429
|
});
|
|
1150
|
-
await folders.delete(
|
|
1151
|
-
return {
|
|
1430
|
+
await folders.delete(peeked.scope, current.id);
|
|
1431
|
+
return { id: current.id };
|
|
1152
1432
|
}
|
|
1153
1433
|
)
|
|
1154
1434
|
};
|
|
@@ -1168,12 +1448,9 @@ function refOf(context) {
|
|
|
1168
1448
|
function objectTarget(context, operation) {
|
|
1169
1449
|
return { operation, route: context.route, key: context.key };
|
|
1170
1450
|
}
|
|
1171
|
-
function
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
folder: parseFolder(context.clientMetadata?.folder ?? ""),
|
|
1175
|
-
clientMetadata: context.clientMetadata
|
|
1176
|
-
};
|
|
1451
|
+
function requestedFolderId(metadata) {
|
|
1452
|
+
const value = metadata?.folderId ?? "";
|
|
1453
|
+
return value === "" ? null : value;
|
|
1177
1454
|
}
|
|
1178
1455
|
function writtenBy(object2, uploadToken) {
|
|
1179
1456
|
return object2 !== null && object2.uploadToken === uploadToken;
|
|
@@ -1199,16 +1476,22 @@ function createDatabaseLifecycleHooks(options) {
|
|
|
1199
1476
|
if (!live) throw s3Error("OBJECT_NOT_FOUND");
|
|
1200
1477
|
};
|
|
1201
1478
|
const uploadWrite = async (context) => {
|
|
1202
|
-
const target = uploadTarget(context);
|
|
1203
1479
|
const existing = await objects.find(refOf(context));
|
|
1204
|
-
|
|
1205
|
-
|
|
1480
|
+
const replaced = existing?.status === "active" && routes.replace(context.route) === "overwrite" ? existing : null;
|
|
1481
|
+
const folderId = replaced ? replaced.folderId : requestedFolderId(context.clientMetadata);
|
|
1482
|
+
const scope = await resolveScope(context, objectTarget(context, "upload"));
|
|
1483
|
+
let folder = null;
|
|
1484
|
+
if (folderId !== null) {
|
|
1485
|
+
folder = await folders.find(scope.scope, folderId);
|
|
1486
|
+
if (!folder && !replaced) throw s3Error("FOLDER_NOT_FOUND");
|
|
1206
1487
|
}
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1488
|
+
const target = {
|
|
1489
|
+
...objectTarget(context, "upload"),
|
|
1490
|
+
folderId,
|
|
1491
|
+
clientMetadata: context.clientMetadata,
|
|
1492
|
+
...folder ? { folder: folder.path } : folderId === null ? { folder: null } : {}
|
|
1211
1493
|
};
|
|
1494
|
+
return { target, existing, replaced, folder };
|
|
1212
1495
|
};
|
|
1213
1496
|
const abortPendingMultipart = async (context, row) => {
|
|
1214
1497
|
const client = routes.client(context.route);
|
|
@@ -1256,13 +1539,13 @@ function createDatabaseLifecycleHooks(options) {
|
|
|
1256
1539
|
});
|
|
1257
1540
|
},
|
|
1258
1541
|
async onStart(context) {
|
|
1259
|
-
const
|
|
1260
|
-
const { scope } = await resolveScope(context, target);
|
|
1261
|
-
|
|
1542
|
+
const written = await uploadWrite(context);
|
|
1543
|
+
const { scope } = await resolveScope(context, written.target);
|
|
1544
|
+
await objects.upsertPending({
|
|
1262
1545
|
...refOf(context),
|
|
1263
1546
|
scope,
|
|
1264
1547
|
route: context.route,
|
|
1265
|
-
|
|
1548
|
+
folderId: written.target.folderId ?? null,
|
|
1266
1549
|
filename: context.file.name,
|
|
1267
1550
|
contentType: context.file.type ?? null,
|
|
1268
1551
|
declaredSize: context.file.size,
|
|
@@ -1272,7 +1555,7 @@ function createDatabaseLifecycleHooks(options) {
|
|
|
1272
1555
|
expiresAt: context.expiresAt,
|
|
1273
1556
|
overwrite: routes.replace(context.route) === "overwrite"
|
|
1274
1557
|
});
|
|
1275
|
-
await folders.
|
|
1558
|
+
if (written.folder) await folders.retain(written.folder);
|
|
1276
1559
|
},
|
|
1277
1560
|
async confirmGuard(context) {
|
|
1278
1561
|
await assertUploadRow(context, "confirm", {
|
|
@@ -1377,6 +1660,8 @@ function createObjectAccess(options) {
|
|
|
1377
1660
|
...scope,
|
|
1378
1661
|
...target?.key !== void 0 && { key: target.key },
|
|
1379
1662
|
...target?.folder !== void 0 && { folder: target.folder },
|
|
1663
|
+
...target?.folderId !== void 0 && { folderId: target.folderId },
|
|
1664
|
+
...target?.recursive === true && { recursive: true },
|
|
1380
1665
|
...target?.clientMetadata !== void 0 && {
|
|
1381
1666
|
clientMetadata: target.clientMetadata
|
|
1382
1667
|
},
|
|
@@ -1406,13 +1691,20 @@ function createObjectAccess(options) {
|
|
|
1406
1691
|
assertOwner
|
|
1407
1692
|
};
|
|
1408
1693
|
}
|
|
1409
|
-
function
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1694
|
+
function rootId(folderId) {
|
|
1695
|
+
return folderId === "" ? null : folderId;
|
|
1696
|
+
}
|
|
1697
|
+
async function targetFolder(folders, scope, folderId) {
|
|
1698
|
+
if (folderId === void 0) return void 0;
|
|
1699
|
+
const id = rootId(folderId);
|
|
1700
|
+
if (id === null) return null;
|
|
1701
|
+
const folder = await folders.find(scope, id);
|
|
1702
|
+
if (!folder) throw s3Error("FOLDER_NOT_FOUND");
|
|
1703
|
+
return folder;
|
|
1413
1704
|
}
|
|
1414
1705
|
function createPublicStores(store) {
|
|
1415
|
-
const { objects
|
|
1706
|
+
const { objects } = store;
|
|
1707
|
+
const { subtreeIds: _subtreeIds, ...folders } = store.folders;
|
|
1416
1708
|
return {
|
|
1417
1709
|
objects: {
|
|
1418
1710
|
find: objects.find,
|
|
@@ -1420,26 +1712,19 @@ function createPublicStores(store) {
|
|
|
1420
1712
|
count: objects.count,
|
|
1421
1713
|
getUsage: objects.getUsage,
|
|
1422
1714
|
async update(ref, input) {
|
|
1423
|
-
const
|
|
1424
|
-
|
|
1425
|
-
if (
|
|
1426
|
-
|
|
1715
|
+
const folderId = input.folderId === void 0 ? void 0 : rootId(input.folderId);
|
|
1716
|
+
let folder;
|
|
1717
|
+
if (typeof folderId === "string") {
|
|
1718
|
+
const scope = ref.scope ?? (await objects.find(ref))?.scope;
|
|
1719
|
+
if (!scope) return null;
|
|
1720
|
+
folder = await targetFolder(folders, scope, folderId);
|
|
1427
1721
|
}
|
|
1722
|
+
const row = await objects.update(ref, { ...input, folderId });
|
|
1723
|
+
if (row && folder) await folders.retain(folder);
|
|
1428
1724
|
return row;
|
|
1429
1725
|
}
|
|
1430
1726
|
},
|
|
1431
|
-
|
|
1432
|
-
folders: {
|
|
1433
|
-
find: async (scope, path) => folders.find(scope, folderPath(path)),
|
|
1434
|
-
children: async (scope, parent, options) => folders.children(
|
|
1435
|
-
scope,
|
|
1436
|
-
parent === null ? null : folderPath(parent),
|
|
1437
|
-
options
|
|
1438
|
-
),
|
|
1439
|
-
ensure: async (scope, path) => folders.ensure(scope, path === null ? null : folderPath(path)),
|
|
1440
|
-
create: async (scope, path) => folders.create(scope, folderPath(path)),
|
|
1441
|
-
delete: async (scope, path) => folders.delete(scope, folderPath(path))
|
|
1442
|
-
}
|
|
1727
|
+
folders
|
|
1443
1728
|
};
|
|
1444
1729
|
}
|
|
1445
1730
|
async function abortUpload(client, row, uploadId) {
|
|
@@ -1588,6 +1873,12 @@ function createScopeResolver(options) {
|
|
|
1588
1873
|
}
|
|
1589
1874
|
const resolved = await scope;
|
|
1590
1875
|
if (resolved === null) throw s3Error("UNAUTHORIZED");
|
|
1876
|
+
const length = characterCount(resolved);
|
|
1877
|
+
if (length > SCOPE_MAX_LENGTH) {
|
|
1878
|
+
throw new Error(
|
|
1879
|
+
`[dimah-s3/db] resolveScope returned a scope of ${length} characters. A scope holds at most ${SCOPE_MAX_LENGTH}.`
|
|
1880
|
+
);
|
|
1881
|
+
}
|
|
1591
1882
|
return { ...context, scope: resolved };
|
|
1592
1883
|
};
|
|
1593
1884
|
}
|
|
@@ -1636,6 +1927,7 @@ function db(options) {
|
|
|
1636
1927
|
store,
|
|
1637
1928
|
objects: stores.objects,
|
|
1638
1929
|
access,
|
|
1930
|
+
resolveScope,
|
|
1639
1931
|
quota,
|
|
1640
1932
|
attributes: options.attributes,
|
|
1641
1933
|
toClientObject
|
|
@@ -1670,6 +1962,6 @@ function warnUnguardedRoutes(env) {
|
|
|
1670
1962
|
}
|
|
1671
1963
|
}
|
|
1672
1964
|
|
|
1673
|
-
export { DB_SCHEMA_VERSION, DimahS3DB, db
|
|
1965
|
+
export { DB_SCHEMA_VERSION, DimahS3DB, db };
|
|
1674
1966
|
//# sourceMappingURL=index.js.map
|
|
1675
1967
|
//# sourceMappingURL=index.js.map
|