@dimah-s3/db 2.0.0-beta.10 → 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.
- package/dist/cli.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +249 -2
- package/dist/index.js.map +1 -1
- package/dist/plugin/db.d.ts +8 -0
- package/dist/plugin/db.d.ts.map +1 -1
- package/dist/plugin/record.d.ts +58 -0
- package/dist/plugin/record.d.ts.map +1 -0
- package/dist/store/object-store.d.ts +22 -0
- package/dist/store/object-store.d.ts.map +1 -1
- package/dist/types/plugin.d.ts +2 -1
- package/dist/types/plugin.d.ts.map +1 -1
- package/package.json +8 -8
package/dist/cli.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { DB_SCHEMA_VERSION, DimahS3DB, type DimahS3DbClient } from "./fuma-db.js";
|
|
2
2
|
export { db, type DbPluginContext } from "./plugin/db.js";
|
|
3
3
|
export type { PruneDeletedOptions, PurgeStaleOptions, PurgeStaleResult, } from "./plugin/purge.js";
|
|
4
|
+
export type { RecordObjectCall, RecordObjectInput } from "./plugin/record.js";
|
|
4
5
|
export type { DbFolders, DbObjects } from "./plugin/public-stores.js";
|
|
5
6
|
export type { AuthorizeContext, DbAttributesOptions, DbPluginOptions, DbQuotaOption, DbRouteOptions, ScopeContext, ScopeOperation, } from "./types/plugin.js";
|
|
6
7
|
export type { StorageFolder, StorageObject, StorageObjectStatus, } from "./types/storage-object.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAG/E,OAAO,EAAE,EAAE,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AACvD,YAAY,EACV,mBAAmB,EACnB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnE,YAAY,EACV,gBAAgB,EAChB,mBAAmB,EACnB,eAAe,EACf,aAAa,EACb,cAAc,EACd,YAAY,EACZ,cAAc,GACf,MAAM,gBAAgB,CAAC;AAGxB,YAAY,EACV,aAAa,EACb,aAAa,EACb,mBAAmB,GACpB,MAAM,wBAAwB,CAAC;AAChC,YAAY,EACV,cAAc,EACd,cAAc,EACd,UAAU,EACV,eAAe,EACf,OAAO,EACP,OAAO,GACR,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAG1D,YAAY,EACV,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,SAAS,EACT,iBAAiB,GAClB,MAAM,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAG/E,OAAO,EAAE,EAAE,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AACvD,YAAY,EACV,mBAAmB,EACnB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAC3E,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnE,YAAY,EACV,gBAAgB,EAChB,mBAAmB,EACnB,eAAe,EACf,aAAa,EACb,cAAc,EACd,YAAY,EACZ,cAAc,GACf,MAAM,gBAAgB,CAAC;AAGxB,YAAY,EACV,aAAa,EACb,aAAa,EACb,mBAAmB,GACpB,MAAM,wBAAwB,CAAC;AAChC,YAAY,EACV,cAAc,EACd,cAAc,EACd,UAAU,EACV,eAAe,EACf,OAAO,EACP,OAAO,GACR,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAG1D,YAAY,EACV,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,SAAS,EACT,iBAAiB,GAClB,MAAM,sBAAsB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
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, normalizeSearchText, base64UrlDecode, base64UrlEncode, matchesSearchText } 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';
|
|
@@ -543,6 +543,53 @@ function createObjectStore(getOrm, subtreeIds) {
|
|
|
543
543
|
}
|
|
544
544
|
throw s3Error("OBJECT_NOT_FOUND");
|
|
545
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
|
+
},
|
|
546
593
|
async update(ref, input) {
|
|
547
594
|
const orm = await getOrm();
|
|
548
595
|
await orm.updateMany("storageObject", {
|
|
@@ -1839,6 +1886,198 @@ function createQuota(objects, option) {
|
|
|
1839
1886
|
}
|
|
1840
1887
|
};
|
|
1841
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
|
+
}
|
|
1842
2081
|
function perKey(cache, key, create) {
|
|
1843
2082
|
let value = cache.get(key);
|
|
1844
2083
|
if (value === void 0) {
|
|
@@ -1909,6 +2148,13 @@ function db(options) {
|
|
|
1909
2148
|
client: routes.client,
|
|
1910
2149
|
logger: () => env?.logger
|
|
1911
2150
|
});
|
|
2151
|
+
const record2 = createRecord({
|
|
2152
|
+
store,
|
|
2153
|
+
resolveScope,
|
|
2154
|
+
access,
|
|
2155
|
+
quota,
|
|
2156
|
+
env: () => env
|
|
2157
|
+
});
|
|
1912
2158
|
return definePlugin({
|
|
1913
2159
|
id: "db",
|
|
1914
2160
|
init(initEnv) {
|
|
@@ -1935,6 +2181,7 @@ function db(options) {
|
|
|
1935
2181
|
context: {
|
|
1936
2182
|
objects: stores.objects,
|
|
1937
2183
|
folders: stores.folders,
|
|
2184
|
+
record: record2,
|
|
1938
2185
|
checkSchema: store.checkSchema,
|
|
1939
2186
|
purgeStale: purge.purgeStale,
|
|
1940
2187
|
pruneDeleted: purge.pruneDeleted
|