@protobase/server 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/dist/assistant/conversations.d.ts +1 -1
  2. package/dist/assistant/tools.d.ts +1 -1
  3. package/dist/assistant/turn.d.ts +2 -2
  4. package/dist/audit/origin.js +12 -0
  5. package/dist/auth/jwt.d.ts +2 -0
  6. package/dist/auth/jwt.js +5 -3
  7. package/dist/batch/reorder.js +1 -1
  8. package/dist/better-auth/auth-schema.d.ts +4 -2
  9. package/dist/better-auth/auth-schema.js +18 -8
  10. package/dist/better-auth/authenticator.d.ts +7 -3
  11. package/dist/better-auth/authenticator.js +24 -3
  12. package/dist/better-auth/create-auth.d.ts +683 -10
  13. package/dist/better-auth/create-auth.js +72 -27
  14. package/dist/better-auth/organizations/accept-invitation.js +80 -0
  15. package/dist/better-auth/organizations/active-organization.js +12 -0
  16. package/dist/better-auth/organizations/admin-routes.js +83 -0
  17. package/dist/better-auth/organizations/app-roles.js +43 -0
  18. package/dist/better-auth/organizations/host.d.ts +53 -0
  19. package/dist/better-auth/organizations/host.js +251 -0
  20. package/dist/better-auth/organizations/invitation-endpoints.js +133 -0
  21. package/dist/better-auth/organizations/invitation-mail.js +17 -0
  22. package/dist/better-auth/organizations/invitation-token.js +27 -0
  23. package/dist/better-auth/organizations/member-endpoints.js +182 -0
  24. package/dist/better-auth/organizations/options.d.ts +48 -0
  25. package/dist/better-auth/organizations/options.js +23 -0
  26. package/dist/better-auth/organizations/organization-plugin.js +128 -0
  27. package/dist/better-auth/organizations/protobase-plugin.js +21 -0
  28. package/dist/better-auth/organizations/role-definitions.d.ts +15 -0
  29. package/dist/better-auth/organizations/role-definitions.js +56 -0
  30. package/dist/better-auth/organizations/session-organization.js +68 -0
  31. package/dist/better-auth/organizations/store.d.ts +19 -0
  32. package/dist/better-auth/organizations/store.js +73 -0
  33. package/dist/better-auth/policy-store.js +44 -35
  34. package/dist/better-auth/snake-case-migration.js +122 -0
  35. package/dist/better-auth/snake-case-names.js +72 -0
  36. package/dist/better-auth/staff-checks.js +5 -1
  37. package/dist/better-auth/staff-log.js +56 -45
  38. package/dist/better-auth/staff-sign-in-endpoints.js +1 -1
  39. package/dist/better-auth/status-route.js +14 -3
  40. package/dist/better-auth/users.d.ts +2 -1
  41. package/dist/better-auth/users.js +4 -1
  42. package/dist/create-admin.d.ts +8 -2
  43. package/dist/create-admin.js +31 -2
  44. package/dist/deps.d.ts +1 -0
  45. package/dist/files/attach.js +80 -0
  46. package/dist/files/cleanup-runner.d.ts +8 -0
  47. package/dist/files/cleanup-runner.js +58 -0
  48. package/dist/files/cleanup-schedule.d.ts +28 -0
  49. package/dist/files/cleanup-schedule.js +48 -0
  50. package/dist/files/detect.js +51 -0
  51. package/dist/files/download-url.js +43 -0
  52. package/dist/files/download.js +90 -0
  53. package/dist/files/duration.js +35 -0
  54. package/dist/files/file-cleanup.d.ts +18 -0
  55. package/dist/files/file-cleanup.js +17 -0
  56. package/dist/files/local-files.d.ts +17 -0
  57. package/dist/files/local-files.js +101 -0
  58. package/dist/files/media-types.js +99 -0
  59. package/dist/files/options.d.ts +15 -0
  60. package/dist/files/present.d.ts +15 -0
  61. package/dist/files/present.js +59 -0
  62. package/dist/files/processors.d.ts +15 -0
  63. package/dist/files/processors.js +39 -0
  64. package/dist/files/providers.js +70 -0
  65. package/dist/files/routes.js +26 -0
  66. package/dist/files/runtime.d.ts +3 -0
  67. package/dist/files/runtime.js +37 -0
  68. package/dist/files/signer.js +32 -0
  69. package/dist/files/store.d.ts +37 -0
  70. package/dist/files/ticket.js +87 -0
  71. package/dist/files/upload-body.js +85 -0
  72. package/dist/files/upload.js +124 -0
  73. package/dist/files/uri.js +47 -0
  74. package/dist/functions/define-function.d.ts +71 -0
  75. package/dist/functions/define-function.js +36 -0
  76. package/dist/functions/function-app.d.ts +17 -0
  77. package/dist/functions/function-app.js +19 -0
  78. package/dist/functions/read-json.d.ts +35 -0
  79. package/dist/functions/read-json.js +24 -0
  80. package/dist/functions/records.d.ts +43 -0
  81. package/dist/functions/records.js +31 -0
  82. package/dist/functions/require-env.d.ts +7 -0
  83. package/dist/functions/require-env.js +12 -0
  84. package/dist/functions/routes.js +60 -0
  85. package/dist/functions/verify-signature.d.ts +32 -0
  86. package/dist/functions/verify-signature.js +49 -0
  87. package/dist/index.d.ts +21 -3
  88. package/dist/index.js +13 -3
  89. package/dist/list-service.js +4 -2
  90. package/dist/openapi/resource-paths.js +78 -1
  91. package/dist/openapi/schemas.js +28 -3
  92. package/dist/project-config.d.ts +7 -1
  93. package/dist/project-config.js +37 -1
  94. package/dist/read-service.js +5 -1
  95. package/dist/request-access.d.ts +5 -0
  96. package/dist/request-access.js +9 -4
  97. package/dist/resource-source.d.ts +4 -1
  98. package/dist/routes/batch.js +1 -1
  99. package/dist/routes/collection.js +3 -1
  100. package/dist/routes/record.js +2 -5
  101. package/dist/types.d.ts +25 -5
  102. package/dist/write-pipeline.js +2 -1
  103. package/dist/write-shared.js +6 -3
  104. package/dist/write-update.js +2 -1
  105. package/package.json +6 -4
@@ -0,0 +1,90 @@
1
+ import { HttpProblem, forbidden, notFound } from "../problem.js";
2
+ import { decodeKeyText } from "../key.js";
3
+ import { readDownloadUrl } from "./download-url.js";
4
+ import { downloadName, isInline, servedType } from "./media-types.js";
5
+ import { baseOf, isFilePath, parseUri, tenantSegment } from "./uri.js";
6
+ import { fetchRecord } from "../records.js";
7
+ import { readTransaction } from "../transactions.js";
8
+ //#region src/files/download.ts
9
+ const gone = () => notFound("There is no such file");
10
+ const rangeOf = (header, size) => {
11
+ if (!header) return void 0;
12
+ const match = /^bytes=(\d*)-(\d*)$/.exec(header.trim());
13
+ if (!match || match[1] === "" && match[2] === "") return false;
14
+ const start = match[1] === "" ? Math.max(size - Number(match[2]), 0) : Number(match[1]);
15
+ const end = match[1] === "" || match[2] === "" ? size - 1 : Math.min(Number(match[2]), size - 1);
16
+ return start <= end && start < size ? {
17
+ start,
18
+ end
19
+ } : false;
20
+ };
21
+ const disposition = (name, type) => {
22
+ const ascii = name.replace(/[^\x20-\x7e]|["\\]/g, "_");
23
+ return `${isInline(type) ? "inline" : "attachment"}; filename="${ascii}"; filename*=UTF-8''${encodeURIComponent(name)}`;
24
+ };
25
+ const grantedName = async (deps, provider, path, query) => {
26
+ const read = await readDownloadUrl(deps.files.signer, provider, path, query);
27
+ if (read.status === "invalid") throw forbidden("invalid-signature", "This file link is not valid");
28
+ if (read.status === "expired") throw forbidden("link-expired", "This file link expired; load the record again for a new one");
29
+ const { grant } = read;
30
+ const entry = deps.registry.find(grant.resource);
31
+ const field = entry?.model.fields[grant.field];
32
+ if (!entry || field?.type !== "file") throw gone();
33
+ const tenantField = entry.model.tenant ? entry.model.fields[entry.model.tenant] : void 0;
34
+ const fields = tenantField ? [field, tenantField] : [field];
35
+ const row = await readTransaction(deps.db, deps.statementTimeoutMs, (trx) => fetchRecord(trx, entry, decodeKeyText(entry, grant.key), void 0, {
36
+ deleted: "show",
37
+ fields
38
+ }));
39
+ const value = row?.record[field.name];
40
+ if (typeof value !== "string" || baseOf(value) !== `${provider}:${path}`) throw gone();
41
+ if (tenantField && path.split("/")[0] !== await tenantSegment(row.record[tenantField.name])) throw gone();
42
+ return {
43
+ name: parseUri(value)?.name ?? path,
44
+ expires: grant.expires
45
+ };
46
+ };
47
+ /**
48
+ * `GET <systemPath>/files/<provider>/<path>`: a public provider's file for anyone; a private one's only through a signed
49
+ * URL whose row still holds it. The Content-Type comes from the path's extension, never from what was uploaded, and
50
+ * nothing renders: `nosniff`, a sandboxing CSP, and `inline` only for images, PDF, audio and video.
51
+ */
52
+ const serveFile = async (deps, request, provider, path) => {
53
+ const configured = deps.files?.providers[provider];
54
+ if (!configured || !isFilePath(path)) throw gone();
55
+ const granted = configured.public ? void 0 : await grantedName(deps, provider, path, new URL(request.url).searchParams);
56
+ const object = await configured.store.head(path);
57
+ if (!object) throw gone();
58
+ const type = servedType(path);
59
+ const name = downloadName(granted?.name ?? path.slice(path.lastIndexOf("/") + 1), type);
60
+ const caching = granted ? `private, max-age=${Math.max(granted.expires - Math.floor(Date.now() / 1e3), 0)}` : "public, max-age=31536000, immutable";
61
+ const headers = {
62
+ "content-type": type,
63
+ "content-disposition": disposition(name, type),
64
+ "x-content-type-options": "nosniff",
65
+ "content-security-policy": "sandbox; default-src 'none'",
66
+ "cache-control": caching,
67
+ "accept-ranges": "bytes"
68
+ };
69
+ const range = rangeOf(request.headers.get("range"), object.size);
70
+ if (range === false) throw new HttpProblem(416, "range-not-satisfiable", "Range Not Satisfiable", "The requested range is outside the file", {}, { "content-range": `bytes */${object.size}` });
71
+ const body = await configured.store.read(path, range);
72
+ if (!body) throw gone();
73
+ if (!range) return new Response(body, {
74
+ status: 200,
75
+ headers: {
76
+ ...headers,
77
+ "content-length": String(object.size)
78
+ }
79
+ });
80
+ return new Response(body, {
81
+ status: 206,
82
+ headers: {
83
+ ...headers,
84
+ "content-length": String(range.end - range.start + 1),
85
+ "content-range": `bytes ${range.start}-${range.end}/${object.size}`
86
+ }
87
+ });
88
+ };
89
+ //#endregion
90
+ export { serveFile };
@@ -0,0 +1,35 @@
1
+ //#region src/files/duration.ts
2
+ const units = {
3
+ s: 1e3,
4
+ second: 1e3,
5
+ m: 6e4,
6
+ min: 6e4,
7
+ minute: 6e4,
8
+ h: 36e5,
9
+ hour: 36e5,
10
+ d: 864e5,
11
+ day: 864e5,
12
+ w: 6048e5,
13
+ week: 6048e5
14
+ };
15
+ /** Milliseconds from a number of them or text such as `1 day`, `14d`, `12 hours` or `0`. */
16
+ const parseDuration = (value, label) => {
17
+ if (typeof value === "number") {
18
+ if (!Number.isSafeInteger(value) || value < 0) throw new Error(`${label} must be a whole number of milliseconds, not ${value}`);
19
+ return value;
20
+ }
21
+ const invalid = /* @__PURE__ */ new Error(`${label} is "${value}"; write it like "1 day", "14d" or "12 hours"`);
22
+ const match = /^\s*(\d+)\s*([a-z]*)\s*$/i.exec(value);
23
+ if (!match) throw invalid;
24
+ const amount = Number(match[1]);
25
+ const word = match[2].toLowerCase();
26
+ if (word === "") {
27
+ if (amount === 0) return 0;
28
+ throw invalid;
29
+ }
30
+ const unit = units[word] ?? units[word.replace(/s$/, "")];
31
+ if (unit === void 0) throw invalid;
32
+ return amount * unit;
33
+ };
34
+ //#endregion
35
+ export { parseDuration };
@@ -0,0 +1,18 @@
1
+ import { FilesOptions } from "./options.js";
2
+ import { CleanupReport } from "./cleanup-runner.js";
3
+ import { ResourceSource } from "../resource-source.js";
4
+ import { Db } from "@protobase/query";
5
+ //#region src/files/file-cleanup.d.ts
6
+ export type FileCleanupInput = {
7
+ resources: ResourceSource[];
8
+ db: Db;
9
+ files?: FilesOptions;
10
+ };
11
+ /**
12
+ * The scheduled deletes of a project's files, for a runner outside a request: `protobase files cleanup`, or an interval
13
+ * in `protobase serve`. `undefined` when the project has no file fields.
14
+ */
15
+ export declare const createFileCleanup: ({ resources, db, files }: FileCleanupInput) => {
16
+ run: (now?: Date) => Promise<CleanupReport>;
17
+ } | undefined;
18
+ //#endregion
@@ -0,0 +1,17 @@
1
+ import { cleanupFiles, createFilesRuntime } from "./runtime.js";
2
+ //#region src/files/file-cleanup.ts
3
+ /**
4
+ * The scheduled deletes of a project's files, for a runner outside a request: `protobase files cleanup`, or an interval
5
+ * in `protobase serve`. `undefined` when the project has no file fields.
6
+ */
7
+ const createFileCleanup = ({ resources, db, files }) => {
8
+ const runtime = createFilesRuntime({
9
+ ...files && { options: files },
10
+ env: globalThis.process?.env ?? {},
11
+ models: resources.map((source) => source.toModel()),
12
+ systemPath: ""
13
+ });
14
+ return runtime && { run: (now) => cleanupFiles(runtime, db, now) };
15
+ };
16
+ //#endregion
17
+ export { createFileCleanup };
@@ -0,0 +1,17 @@
1
+ import { FileProvider, FileStore } from "./store.js";
2
+ //#region src/files/local-files.d.ts
3
+ export type LocalFilesOptions = {
4
+ /** The folder the files live in, created when missing; relative to the working directory. */
5
+ dir: string;
6
+ /** Files anyone with the link may open, served without a signature. */
7
+ public?: boolean;
8
+ /** Where public files are linked from instead of the app's own `/api/files/<provider>`, such as a CDN in front of it. */
9
+ publicUrl?: string;
10
+ /** How long bytes outlive their last reference; default `files.retention`, or 0 for a public provider. */
11
+ retention?: number | string;
12
+ };
13
+ /** A store in a folder on local disk. Writes go to `.tmp/` first and are renamed into place, so they are atomic. */
14
+ export declare const localFileStore: (dir: string) => FileStore;
15
+ /** A provider on local disk: `localFiles({ dir: './data/files/private' })`. */
16
+ export declare const localFiles: ({ dir, public: isPublic, publicUrl, retention }: LocalFilesOptions) => FileProvider;
17
+ //#endregion
@@ -0,0 +1,101 @@
1
+ import { HttpProblem } from "../problem.js";
2
+ import { parseDuration } from "./duration.js";
3
+ //#region src/files/local-files.ts
4
+ const node = async () => ({
5
+ fs: await import("node:fs/promises"),
6
+ streams: await import("node:fs"),
7
+ path: await import("node:path"),
8
+ stream: await import("node:stream"),
9
+ pipeline: (await import("node:stream/promises")).pipeline,
10
+ crypto: await import("node:crypto")
11
+ });
12
+ const codeOf = (error) => error instanceof Error ? error.code : void 0;
13
+ const storageFull = () => new HttpProblem(507, "storage-full", "Insufficient Storage", "There is no room left to store this file");
14
+ /** A store in a folder on local disk. Writes go to `.tmp/` first and are renamed into place, so they are atomic. */
15
+ const localFileStore = (dir) => {
16
+ const resolve = async (relative) => {
17
+ const { path } = await node();
18
+ const root = path.resolve(dir);
19
+ const full = path.resolve(root, relative);
20
+ if (!full.startsWith(`${root}${path.sep}`)) throw new Error(`"${relative}" is outside the file store`);
21
+ return full;
22
+ };
23
+ const head = async (relative) => {
24
+ const { fs } = await node();
25
+ try {
26
+ const stat = await fs.stat(await resolve(relative));
27
+ return stat.isFile() ? {
28
+ path: relative,
29
+ size: stat.size,
30
+ modified: stat.mtime
31
+ } : void 0;
32
+ } catch (error) {
33
+ if (codeOf(error) === "ENOENT" || codeOf(error) === "ENOTDIR") return void 0;
34
+ throw error;
35
+ }
36
+ };
37
+ const put = async (relative, body) => {
38
+ const { fs, streams, path, stream, pipeline, crypto } = await node();
39
+ const full = await resolve(relative);
40
+ const temporary = await resolve(`.tmp/${crypto.randomUUID()}`);
41
+ await fs.mkdir(path.dirname(temporary), { recursive: true });
42
+ await fs.mkdir(path.dirname(full), { recursive: true });
43
+ try {
44
+ await pipeline(stream.Readable.fromWeb(body), streams.createWriteStream(temporary, { flags: "wx" }));
45
+ await fs.rename(temporary, full);
46
+ } catch (error) {
47
+ await fs.rm(temporary, { force: true });
48
+ if (codeOf(error) === "ENOSPC" || codeOf(error) === "EDQUOT") throw storageFull();
49
+ throw error;
50
+ }
51
+ return { size: (await fs.stat(full)).size };
52
+ };
53
+ const read = async (relative, range) => {
54
+ const { streams, stream } = await node();
55
+ if (!await head(relative)) return void 0;
56
+ const reader = streams.createReadStream(await resolve(relative), range ? {
57
+ start: range.start,
58
+ end: range.end
59
+ } : {});
60
+ return stream.Readable.toWeb(reader);
61
+ };
62
+ const remove = async (relative) => {
63
+ const { fs } = await node();
64
+ await fs.rm(await resolve(relative), { force: true });
65
+ };
66
+ const list = async (prefix) => {
67
+ const { fs, path } = await node();
68
+ const root = path.resolve(dir);
69
+ const folder = await resolve(prefix.replace(/\/$/, ""));
70
+ let names;
71
+ try {
72
+ names = await fs.readdir(folder, { recursive: true });
73
+ } catch (error) {
74
+ if (codeOf(error) === "ENOENT") return [];
75
+ throw error;
76
+ }
77
+ const found = [];
78
+ for (const name of names) {
79
+ const relative = path.relative(root, path.join(folder, name)).split(path.sep).join("/");
80
+ const object = await head(relative);
81
+ if (object) found.push(object);
82
+ }
83
+ return found.sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
84
+ };
85
+ return {
86
+ put,
87
+ head,
88
+ read,
89
+ delete: remove,
90
+ list
91
+ };
92
+ };
93
+ /** A provider on local disk: `localFiles({ dir: './data/files/private' })`. */
94
+ const localFiles = ({ dir, public: isPublic = false, publicUrl, retention }) => ({
95
+ store: localFileStore(dir),
96
+ public: isPublic || publicUrl !== void 0,
97
+ ...publicUrl !== void 0 && { publicUrl: publicUrl.replace(/\/$/, "") },
98
+ ...retention !== void 0 && { retention: parseDuration(retention, "retention") }
99
+ });
100
+ //#endregion
101
+ export { localFileStore, localFiles };
@@ -0,0 +1,99 @@
1
+ //#region src/files/media-types.ts
2
+ /**
3
+ * The one table between types and extensions. An upload's decided type picks its path's extension, and every download's
4
+ * Content-Type is looked up from that extension here, so what is served never comes from a client's header.
5
+ */
6
+ const table = [
7
+ ["image/png", ["png"]],
8
+ ["image/jpeg", ["jpg", "jpeg"]],
9
+ ["image/gif", ["gif"]],
10
+ ["image/webp", ["webp"]],
11
+ ["image/avif", ["avif"]],
12
+ ["image/heic", ["heic"]],
13
+ ["image/bmp", ["bmp"]],
14
+ ["image/tiff", ["tif", "tiff"]],
15
+ ["image/x-icon", ["ico"]],
16
+ ["image/svg+xml", ["svg"]],
17
+ ["application/pdf", ["pdf"]],
18
+ ["application/zip", ["zip"]],
19
+ ["application/gzip", ["gz"]],
20
+ ["application/x-tar", ["tar"]],
21
+ ["application/x-7z-compressed", ["7z"]],
22
+ ["application/vnd.rar", ["rar"]],
23
+ ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", ["docx"]],
24
+ ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ["xlsx"]],
25
+ ["application/vnd.openxmlformats-officedocument.presentationml.presentation", ["pptx"]],
26
+ ["application/vnd.oasis.opendocument.text", ["odt"]],
27
+ ["application/vnd.oasis.opendocument.spreadsheet", ["ods"]],
28
+ ["application/vnd.oasis.opendocument.presentation", ["odp"]],
29
+ ["application/msword", ["doc"]],
30
+ ["application/vnd.ms-excel", ["xls"]],
31
+ ["application/vnd.ms-powerpoint", ["ppt"]],
32
+ ["application/rtf", ["rtf"]],
33
+ ["application/epub+zip", ["epub"]],
34
+ ["application/x-msdownload", ["exe"]],
35
+ ["application/x-sqlite3", ["sqlite"]],
36
+ ["audio/mpeg", ["mp3"]],
37
+ ["audio/wav", ["wav"]],
38
+ ["audio/ogg", ["ogg"]],
39
+ ["audio/mp4", ["m4a"]],
40
+ ["audio/flac", ["flac"]],
41
+ ["video/mp4", ["mp4"]],
42
+ ["video/webm", ["webm"]],
43
+ ["video/quicktime", ["mov"]],
44
+ ["video/x-matroska", ["mkv"]],
45
+ ["text/csv", ["csv"]],
46
+ ["text/tab-separated-values", ["tsv"]],
47
+ ["text/plain", ["txt"]],
48
+ ["text/markdown", ["md"]],
49
+ ["application/json", ["json"]],
50
+ ["application/octet-stream", ["bin"]]
51
+ ];
52
+ const octetStream = "application/octet-stream";
53
+ const byExtension = new Map(table.flatMap(([type, extensions]) => extensions.map((extension) => [extension, type])));
54
+ const byType = new Map(table.map(([type, extensions]) => [type, extensions[0]]));
55
+ /** Text formats a name's extension may give text content, which magic bytes cannot tell apart. */
56
+ const textTypes = /* @__PURE__ */ new Set([
57
+ "text/csv",
58
+ "text/tab-separated-values",
59
+ "text/plain",
60
+ "text/markdown",
61
+ "application/json"
62
+ ]);
63
+ const inlineTypes = /* @__PURE__ */ new Set([
64
+ "image/png",
65
+ "image/jpeg",
66
+ "image/gif",
67
+ "image/webp",
68
+ "image/avif",
69
+ "image/bmp",
70
+ "application/pdf"
71
+ ]);
72
+ const extensionOf = (name) => {
73
+ const dot = name.lastIndexOf(".");
74
+ return dot <= 0 || dot === name.length - 1 ? void 0 : name.slice(dot + 1).toLowerCase();
75
+ };
76
+ /** The type of a name or path, from its extension; `undefined` for an extension the table does not know. */
77
+ const typeOfExtension = (name) => {
78
+ const extension = extensionOf(name);
79
+ return extension === void 0 ? void 0 : byExtension.get(extension);
80
+ };
81
+ /** The type a stored path is served as. */
82
+ const servedType = (path) => typeOfExtension(path) ?? "application/octet-stream";
83
+ /** The canonical extension of a type the table knows. */
84
+ const canonicalExtension = (type) => byType.get(type);
85
+ const isKnownType = (type) => byType.has(type);
86
+ const isInline = (type) => inlineTypes.has(type) || type.startsWith("audio/") || type.startsWith("video/");
87
+ /** Whether `type` passes a field's allowed list: exact types, `type/*` wildcards, `*\/*`; an empty list allows any. */
88
+ const accepts = (allowed, type) => allowed.length === 0 || allowed.some((entry) => entry === "*/*" || entry === type || entry.endsWith("/*") && type.startsWith(entry.slice(0, -1)));
89
+ /** The uploader's name with its extension made to match the stored type: `photo.png` holding a JPEG downloads as `photo.jpg`. */
90
+ const downloadName = (name, type) => {
91
+ const canonical = canonicalExtension(type);
92
+ if (!canonical || type === "application/octet-stream") return name;
93
+ const known = typeOfExtension(name);
94
+ if (known === type) return name;
95
+ const extension = extensionOf(name);
96
+ return `${extension !== void 0 && known !== void 0 ? name.slice(0, -extension.length - 1) : name}.${canonical}`;
97
+ };
98
+ //#endregion
99
+ export { accepts, canonicalExtension, downloadName, extensionOf, isInline, isKnownType, octetStream, servedType, textTypes, typeOfExtension };
@@ -0,0 +1,15 @@
1
+ import { FileProvider } from "./store.js";
2
+ //#region src/files/options.d.ts
3
+ /** `files` in `protobase.config.ts`. Environment variables override it (see `resolveProviders`). */
4
+ export type FilesOptions = {
5
+ /**
6
+ * Providers by name, such as `private: localFiles({ dir: './data/files' })`. A name is part of every stored value, so
7
+ * name the role (`archive`), not the vendor. Without config, `private` and `public` are folders under `data/files`.
8
+ */
9
+ providers?: Record<string, FileProvider>;
10
+ /** How long bytes outlive their last reference, for restores; default `1 day`. Public providers default to 0. */
11
+ retention?: number | string;
12
+ /** Signs upload tickets and download URLs; default `PROTOBASE_FILES_SECRET`, then `BETTER_AUTH_SECRET`. */
13
+ secret?: string;
14
+ };
15
+ //#endregion
@@ -0,0 +1,15 @@
1
+ import "../types.js";
2
+ import "../request-access.js";
3
+ //#region src/files/present.d.ts
4
+ /** A file field as responses carry it; `missing` when the value does not parse or names a provider the config lacks. */
5
+ export type FileObject = {
6
+ uri: string;
7
+ name: string;
8
+ type: string;
9
+ size?: number;
10
+ url?: string;
11
+ } | {
12
+ uri: string;
13
+ missing: true;
14
+ };
15
+ //#endregion
@@ -0,0 +1,59 @@
1
+ import { downloadExpiry, signedDownloadUrl } from "./download-url.js";
2
+ import { servedType } from "./media-types.js";
3
+ import { parseUri } from "./uri.js";
4
+ import { encodeKey } from "@protobase/schema";
5
+ //#region src/files/present.ts
6
+ const fileObject = async (files, access, row, field, value, expires) => {
7
+ const ref = parseUri(value);
8
+ const provider = ref && files.providers[ref.provider];
9
+ if (!ref || !provider) return {
10
+ uri: value,
11
+ missing: true
12
+ };
13
+ const described = {
14
+ uri: value,
15
+ name: ref.name,
16
+ type: servedType(ref.path),
17
+ ...ref.size !== void 0 && { size: ref.size }
18
+ };
19
+ if (provider.public) return {
20
+ ...described,
21
+ url: `${provider.publicUrl ?? `${files.path}/${ref.provider}`}/${ref.path}`
22
+ };
23
+ const key = access.full.primaryKey.map((name) => row[name]);
24
+ if (key.some((part) => typeof part !== "string" && typeof part !== "number")) return described;
25
+ const keyText = encodeKey(key);
26
+ const url = await signedDownloadUrl(files.signer, files.path, {
27
+ provider: ref.provider,
28
+ path: ref.path,
29
+ resource: access.entry.name,
30
+ key: keyText,
31
+ field,
32
+ expires
33
+ });
34
+ return {
35
+ ...described,
36
+ url
37
+ };
38
+ };
39
+ /**
40
+ * Records as responses carry them: each readable file field as `{ uri, name, type, size, url }`. The URL is minted here,
41
+ * after the ETag, for a caller who may already see the row: private files get a signed link to this row's field, public
42
+ * ones their permanent address. Stored values, hooks and ETags only ever see the URI.
43
+ */
44
+ const presentFiles = async (access, rows) => {
45
+ const files = access.deps.files;
46
+ const fields = Object.values(access.model.fields).filter((field) => field.type === "file");
47
+ if (!files || fields.length === 0) return rows;
48
+ const expires = downloadExpiry();
49
+ return Promise.all(rows.map(async (row) => {
50
+ const shown = { ...row };
51
+ for (const field of fields) {
52
+ const value = row[field.name];
53
+ if (typeof value === "string") shown[field.name] = await fileObject(files, access, row, field.name, value, expires);
54
+ }
55
+ return shown;
56
+ }));
57
+ };
58
+ //#endregion
59
+ export { presentFiles };
@@ -0,0 +1,15 @@
1
+ import { FileProcessor } from "@protobase/schema";
2
+ //#region src/files/processors.d.ts
3
+ /** A processor of the project's own: `processor({ accepts: ['image/*'], run: async (file) => … })`. */
4
+ export declare const processor: (definition: FileProcessor) => FileProcessor;
5
+ /** An image's width or height in pixels, as shown (EXIF rotation applied), for an integer field. Null for other files. */
6
+ export declare const imageSize: (dimension: "width" | "height") => FileProcessor;
7
+ /** An image's width divided by its height, as decimal text with `decimals` places (default 4), for a decimal field. */
8
+ export declare const aspectRatio: ({ decimals }?: {
9
+ decimals?: number;
10
+ }) => FileProcessor;
11
+ /** The file's size in bytes, for an integer field to filter or sort on. */
12
+ export declare const fileSize: () => FileProcessor;
13
+ /** The file's detected type, for a text field to filter or sort on. */
14
+ export declare const fileType: () => FileProcessor;
15
+ //#endregion
@@ -0,0 +1,39 @@
1
+ import { imageSize } from "image-size";
2
+ //#region src/files/processors.ts
3
+ const shownSize = (head) => {
4
+ let size;
5
+ try {
6
+ size = imageSize(head);
7
+ } catch (error) {
8
+ if (error instanceof TypeError || error instanceof RangeError) return void 0;
9
+ throw error;
10
+ }
11
+ return (size.orientation ?? 1) >= 5 ? {
12
+ width: size.height,
13
+ height: size.width
14
+ } : {
15
+ width: size.width,
16
+ height: size.height
17
+ };
18
+ };
19
+ /** A processor of the project's own: `processor({ accepts: ['image/*'], run: async (file) => … })`. */
20
+ const processor = (definition) => definition;
21
+ /** An image's width or height in pixels, as shown (EXIF rotation applied), for an integer field. Null for other files. */
22
+ const imageSize$1 = (dimension) => processor({
23
+ accepts: ["image/*"],
24
+ run: (file) => shownSize(file.head)?.[dimension] ?? null
25
+ });
26
+ /** An image's width divided by its height, as decimal text with `decimals` places (default 4), for a decimal field. */
27
+ const aspectRatio = ({ decimals = 4 } = {}) => processor({
28
+ accepts: ["image/*"],
29
+ run: (file) => {
30
+ const size = shownSize(file.head);
31
+ return size && size.height > 0 ? (size.width / size.height).toFixed(decimals) : null;
32
+ }
33
+ });
34
+ /** The file's size in bytes, for an integer field to filter or sort on. */
35
+ const fileSize = () => processor({ run: (file) => file.size });
36
+ /** The file's detected type, for a text field to filter or sort on. */
37
+ const fileType = () => processor({ run: (file) => file.type });
38
+ //#endregion
39
+ export { aspectRatio, fileSize, fileType, imageSize$1 as imageSize, processor };
@@ -0,0 +1,70 @@
1
+ import { parseDuration } from "./duration.js";
2
+ import { localFiles } from "./local-files.js";
3
+ import { checkedProvider } from "@protobase/schema";
4
+ //#region src/files/providers.ts
5
+ const prefix = "PROTOBASE_FILES_";
6
+ /** `PROTOBASE_FILES_<NAME>` configures a provider, except for these settings. */
7
+ const settingVariables = [
8
+ "RETENTION",
9
+ "SECRET",
10
+ "CLEANUP_MINUTES"
11
+ ];
12
+ const reservedNames = new Set(settingVariables.map((name) => name.toLowerCase().replaceAll("_", "-")));
13
+ const checkedName = (name) => {
14
+ checkedProvider(name);
15
+ if (reservedNames.has(name)) throw new Error(`"${name}" cannot name a file provider: PROTOBASE_FILES_${name.toUpperCase().replaceAll("-", "_")} is a setting`);
16
+ return name;
17
+ };
18
+ const variableOf = (name) => `${prefix}${name.toUpperCase().replaceAll("-", "_")}`;
19
+ /** A provider from `file:///data/files/private?public=1&public_url=…&retention=14d` (a relative `file:data/files` works too). */
20
+ const providerFromUrl = (text, variable) => {
21
+ let url;
22
+ try {
23
+ url = new URL(text);
24
+ } catch (error) {
25
+ if (error instanceof TypeError) throw new Error(`${variable} is not a URL; write it like file:///data/files/private`);
26
+ throw error;
27
+ }
28
+ if (url.protocol !== "file:") throw new Error(`${variable} uses ${url.protocol}; not implemented: only file: providers (local disk) are supported so far`);
29
+ const flag = url.searchParams.get("public");
30
+ const publicUrl = url.searchParams.get("public_url") ?? void 0;
31
+ const retention = url.searchParams.get("retention") ?? void 0;
32
+ return localFiles({
33
+ dir: decodeURIComponent(url.pathname),
34
+ public: flag === "1" || flag === "true",
35
+ ...publicUrl && { publicUrl },
36
+ ...retention && { retention }
37
+ });
38
+ };
39
+ /**
40
+ * The providers in effect: the config's, each replaced by `PROTOBASE_FILES_<NAME>` when that is set, plus any the
41
+ * environment adds. `private` and `public` default to `data/files/private` and `data/files/public` when `used` needs them.
42
+ */
43
+ const resolveProviders = (options, env, used) => {
44
+ const providers = {};
45
+ for (const [name, provider] of Object.entries(options.providers ?? {})) providers[checkedName(name)] = provider;
46
+ for (const [variable, value] of Object.entries(env)) {
47
+ if (!variable.startsWith(prefix) || !value) continue;
48
+ const suffix = variable.slice(16);
49
+ if (settingVariables.includes(suffix)) continue;
50
+ const name = checkedName(suffix.toLowerCase().replaceAll("_", "-"));
51
+ providers[name] = providerFromUrl(value, variable);
52
+ }
53
+ for (const name of used) {
54
+ if (providers[name]) continue;
55
+ if (name !== "private" && name !== "public") throw new Error(`No file provider "${name}": add it to files.providers in protobase.config.ts or set ${variableOf(name)}`);
56
+ providers[name] = localFiles({
57
+ dir: `data/files/${name}`,
58
+ public: name === "public"
59
+ });
60
+ }
61
+ return providers;
62
+ };
63
+ /** How long a provider keeps bytes after their last reference. */
64
+ const retentionOf = (options, env) => {
65
+ const fallback = env[`${prefix}RETENTION`] ?? options.retention ?? "1 day";
66
+ const global = parseDuration(fallback, env[`${prefix}RETENTION`] ? `${prefix}RETENTION` : "files.retention");
67
+ return (provider) => provider.retention ?? (provider.public ? 0 : global);
68
+ };
69
+ //#endregion
70
+ export { providerFromUrl, resolveProviders, retentionOf, settingVariables };
@@ -0,0 +1,26 @@
1
+ import { forbidden } from "../problem.js";
2
+ import { serveFile } from "./download.js";
3
+ import { cleanupFiles } from "./runtime.js";
4
+ import { Hono } from "hono";
5
+ //#region src/files/routes.ts
6
+ /**
7
+ * Beside the API, on the app's own host: `GET <systemPath>/files/<provider>/<path>` serves files, and
8
+ * `POST <systemPath>/files:cleanup` runs the scheduled deletes that are due, for an admin (a platform calls it on a schedule).
9
+ */
10
+ const fileRoutes = (deps, authenticate) => {
11
+ const app = new Hono();
12
+ const files = deps.files;
13
+ if (!files) return app;
14
+ app.get(`${files.path}/:provider/*`, (c) => {
15
+ const provider = c.req.param("provider");
16
+ const path = decodeURIComponent(new URL(c.req.url).pathname.slice(`${files.path}/${provider}/`.length));
17
+ return serveFile(deps, c.req.raw, provider, path);
18
+ });
19
+ app.post(`${files.path}:cleanup`, async (c) => {
20
+ if (!(await authenticate(c.req.raw)).user.roles.includes("admin")) throw forbidden("access-denied", "Only an admin may run the file cleanup");
21
+ return c.json(await cleanupFiles(files, deps.db));
22
+ });
23
+ return app;
24
+ };
25
+ //#endregion
26
+ export { fileRoutes };
@@ -0,0 +1,3 @@
1
+ import "./cleanup-runner.js";
2
+ import { ResourceModel } from "@protobase/schema";
3
+ import { Db } from "@protobase/query";