@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.
- package/dist/assistant/conversations.d.ts +1 -1
- package/dist/assistant/tools.d.ts +1 -1
- package/dist/assistant/turn.d.ts +2 -2
- package/dist/audit/origin.js +12 -0
- package/dist/auth/jwt.d.ts +2 -0
- package/dist/auth/jwt.js +5 -3
- package/dist/batch/reorder.js +1 -1
- package/dist/better-auth/auth-schema.d.ts +4 -2
- package/dist/better-auth/auth-schema.js +18 -8
- package/dist/better-auth/authenticator.d.ts +7 -3
- package/dist/better-auth/authenticator.js +24 -3
- package/dist/better-auth/create-auth.d.ts +683 -10
- package/dist/better-auth/create-auth.js +72 -27
- package/dist/better-auth/organizations/accept-invitation.js +80 -0
- package/dist/better-auth/organizations/active-organization.js +12 -0
- package/dist/better-auth/organizations/admin-routes.js +83 -0
- package/dist/better-auth/organizations/app-roles.js +43 -0
- package/dist/better-auth/organizations/host.d.ts +53 -0
- package/dist/better-auth/organizations/host.js +251 -0
- package/dist/better-auth/organizations/invitation-endpoints.js +133 -0
- package/dist/better-auth/organizations/invitation-mail.js +17 -0
- package/dist/better-auth/organizations/invitation-token.js +27 -0
- package/dist/better-auth/organizations/member-endpoints.js +182 -0
- package/dist/better-auth/organizations/options.d.ts +48 -0
- package/dist/better-auth/organizations/options.js +23 -0
- package/dist/better-auth/organizations/organization-plugin.js +128 -0
- package/dist/better-auth/organizations/protobase-plugin.js +21 -0
- package/dist/better-auth/organizations/role-definitions.d.ts +15 -0
- package/dist/better-auth/organizations/role-definitions.js +56 -0
- package/dist/better-auth/organizations/session-organization.js +68 -0
- package/dist/better-auth/organizations/store.d.ts +19 -0
- package/dist/better-auth/organizations/store.js +73 -0
- package/dist/better-auth/policy-store.js +44 -35
- package/dist/better-auth/snake-case-migration.js +122 -0
- package/dist/better-auth/snake-case-names.js +72 -0
- package/dist/better-auth/staff-checks.js +5 -1
- package/dist/better-auth/staff-log.js +56 -45
- package/dist/better-auth/staff-sign-in-endpoints.js +1 -1
- package/dist/better-auth/status-route.js +14 -3
- package/dist/better-auth/users.d.ts +2 -1
- package/dist/better-auth/users.js +4 -1
- package/dist/create-admin.d.ts +8 -2
- package/dist/create-admin.js +31 -2
- package/dist/deps.d.ts +1 -0
- package/dist/files/attach.js +80 -0
- package/dist/files/cleanup-runner.d.ts +8 -0
- package/dist/files/cleanup-runner.js +58 -0
- package/dist/files/cleanup-schedule.d.ts +28 -0
- package/dist/files/cleanup-schedule.js +48 -0
- package/dist/files/detect.js +51 -0
- package/dist/files/download-url.js +43 -0
- package/dist/files/download.js +90 -0
- package/dist/files/duration.js +35 -0
- package/dist/files/file-cleanup.d.ts +18 -0
- package/dist/files/file-cleanup.js +17 -0
- package/dist/files/local-files.d.ts +17 -0
- package/dist/files/local-files.js +101 -0
- package/dist/files/media-types.js +99 -0
- package/dist/files/options.d.ts +15 -0
- package/dist/files/present.d.ts +15 -0
- package/dist/files/present.js +59 -0
- package/dist/files/processors.d.ts +15 -0
- package/dist/files/processors.js +39 -0
- package/dist/files/providers.js +70 -0
- package/dist/files/routes.js +26 -0
- package/dist/files/runtime.d.ts +3 -0
- package/dist/files/runtime.js +37 -0
- package/dist/files/signer.js +32 -0
- package/dist/files/store.d.ts +37 -0
- package/dist/files/ticket.js +87 -0
- package/dist/files/upload-body.js +85 -0
- package/dist/files/upload.js +124 -0
- package/dist/files/uri.js +47 -0
- package/dist/functions/define-function.d.ts +71 -0
- package/dist/functions/define-function.js +36 -0
- package/dist/functions/function-app.d.ts +17 -0
- package/dist/functions/function-app.js +19 -0
- package/dist/functions/read-json.d.ts +35 -0
- package/dist/functions/read-json.js +24 -0
- package/dist/functions/records.d.ts +43 -0
- package/dist/functions/records.js +31 -0
- package/dist/functions/require-env.d.ts +7 -0
- package/dist/functions/require-env.js +12 -0
- package/dist/functions/routes.js +60 -0
- package/dist/functions/verify-signature.d.ts +32 -0
- package/dist/functions/verify-signature.js +49 -0
- package/dist/index.d.ts +21 -3
- package/dist/index.js +13 -3
- package/dist/list-service.js +4 -2
- package/dist/openapi/resource-paths.js +78 -1
- package/dist/openapi/schemas.js +28 -3
- package/dist/project-config.d.ts +7 -1
- package/dist/project-config.js +37 -1
- package/dist/read-service.js +5 -1
- package/dist/request-access.d.ts +5 -0
- package/dist/request-access.js +9 -4
- package/dist/resource-source.d.ts +4 -1
- package/dist/routes/batch.js +1 -1
- package/dist/routes/collection.js +3 -1
- package/dist/routes/record.js +2 -5
- package/dist/types.d.ts +25 -5
- package/dist/write-pipeline.js +2 -1
- package/dist/write-shared.js +6 -3
- package/dist/write-update.js +2 -1
- package/package.json +6 -4
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { fileColumns, runCleanup } from "./cleanup-runner.js";
|
|
2
|
+
import { storageCleanupSchedule } from "./cleanup-schedule.js";
|
|
3
|
+
import { resolveProviders, retentionOf } from "./providers.js";
|
|
4
|
+
import { createSigner } from "./signer.js";
|
|
5
|
+
//#region src/files/runtime.ts
|
|
6
|
+
/** How long an upload ticket stays usable, so a form left open still saves. */
|
|
7
|
+
const ticketSeconds = 86400;
|
|
8
|
+
/**
|
|
9
|
+
* The runtime, when a resource has a file field or the config names providers. It fails at startup when a field's
|
|
10
|
+
* provider is not configured or nothing signs uploads.
|
|
11
|
+
*/
|
|
12
|
+
const createFilesRuntime = ({ options = {}, env, models, systemPath }) => {
|
|
13
|
+
const used = [...new Set(models.flatMap((model) => Object.values(model.fields).flatMap((field) => field.file ? [field.file.provider] : [])))];
|
|
14
|
+
if (used.length === 0 && !options.providers) return void 0;
|
|
15
|
+
const secret = options.secret ?? env.PROTOBASE_FILES_SECRET ?? env.BETTER_AUTH_SECRET;
|
|
16
|
+
if (!secret) throw new Error("File fields need a secret to sign uploads: set BETTER_AUTH_SECRET, PROTOBASE_FILES_SECRET or files.secret");
|
|
17
|
+
const providers = resolveProviders(options, env, used);
|
|
18
|
+
const retention = retentionOf(options, env);
|
|
19
|
+
return {
|
|
20
|
+
providers,
|
|
21
|
+
signer: createSigner(secret),
|
|
22
|
+
schedule: storageCleanupSchedule(providers),
|
|
23
|
+
retention: (provider) => retention(providers[provider]),
|
|
24
|
+
path: `${systemPath}/files`,
|
|
25
|
+
columns: fileColumns(models)
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/** Runs the scheduled deletes that are due (see `runCleanup`). */
|
|
29
|
+
const cleanupFiles = (runtime, db, now) => runCleanup({
|
|
30
|
+
db,
|
|
31
|
+
columns: runtime.columns,
|
|
32
|
+
providers: runtime.providers,
|
|
33
|
+
schedule: runtime.schedule,
|
|
34
|
+
...now && { now }
|
|
35
|
+
});
|
|
36
|
+
//#endregion
|
|
37
|
+
export { cleanupFiles, createFilesRuntime, ticketSeconds };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/files/signer.ts
|
|
2
|
+
const encoder = new TextEncoder();
|
|
3
|
+
const toBase64Url = (bytes) => btoa(String.fromCharCode(...new Uint8Array(bytes))).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
4
|
+
const fromBase64Url = (text) => {
|
|
5
|
+
if (!/^[A-Za-z0-9_-]{43}$/.test(text)) return void 0;
|
|
6
|
+
return Uint8Array.from(atob(text.replaceAll("-", "+").replaceAll("_", "/")), (char) => char.charCodeAt(0));
|
|
7
|
+
};
|
|
8
|
+
const createSigner = (secret) => {
|
|
9
|
+
const key = (async () => {
|
|
10
|
+
const material = await crypto.subtle.importKey("raw", encoder.encode(secret), "HKDF", false, ["deriveKey"]);
|
|
11
|
+
return crypto.subtle.deriveKey({
|
|
12
|
+
name: "HKDF",
|
|
13
|
+
hash: "SHA-256",
|
|
14
|
+
salt: /* @__PURE__ */ new Uint8Array(0),
|
|
15
|
+
info: encoder.encode("protobase files")
|
|
16
|
+
}, material, {
|
|
17
|
+
name: "HMAC",
|
|
18
|
+
hash: "SHA-256",
|
|
19
|
+
length: 256
|
|
20
|
+
}, false, ["sign", "verify"]);
|
|
21
|
+
})();
|
|
22
|
+
const content = (purpose, parts) => encoder.encode(JSON.stringify([purpose, ...parts]));
|
|
23
|
+
return {
|
|
24
|
+
sign: async (purpose, parts) => toBase64Url(await crypto.subtle.sign("HMAC", await key, content(purpose, parts))),
|
|
25
|
+
verify: async (purpose, parts, signature) => {
|
|
26
|
+
const bytes = fromBase64Url(signature);
|
|
27
|
+
return bytes !== void 0 && crypto.subtle.verify("HMAC", await key, bytes, content(purpose, parts));
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
//#endregion
|
|
32
|
+
export { createSigner };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/files/store.d.ts
|
|
2
|
+
/** One stored object. */
|
|
3
|
+
export type StoredObject = {
|
|
4
|
+
path: string;
|
|
5
|
+
size: number;
|
|
6
|
+
modified: Date;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Where a provider keeps bytes: local disk now, an S3-compatible bucket later. Paths are relative and use `/`. Every
|
|
10
|
+
* operation on a path that does not exist is harmless: `head` and `read` give `undefined`, `delete` does nothing.
|
|
11
|
+
*/
|
|
12
|
+
export type FileStore = {
|
|
13
|
+
/** Writes the whole stream, or nothing when it fails: a reader never sees part of an object. */
|
|
14
|
+
put(path: string, body: ReadableStream<Uint8Array>): Promise<{
|
|
15
|
+
size: number;
|
|
16
|
+
}>;
|
|
17
|
+
head(path: string): Promise<StoredObject | undefined>;
|
|
18
|
+
/** The bytes, or the inclusive byte range `start`–`end`. */
|
|
19
|
+
read(path: string, range?: {
|
|
20
|
+
start: number;
|
|
21
|
+
end: number;
|
|
22
|
+
}): Promise<ReadableStream<Uint8Array> | undefined>;
|
|
23
|
+
delete(path: string): Promise<void>;
|
|
24
|
+
/** Every object under `prefix` (a folder such as `.cleanup/`), sorted by path. */
|
|
25
|
+
list(prefix: string): Promise<StoredObject[]>;
|
|
26
|
+
};
|
|
27
|
+
/** A configured provider: its store, whether its files are public, and how long bytes outlive their last reference. */
|
|
28
|
+
export type FileProvider = {
|
|
29
|
+
store: FileStore;
|
|
30
|
+
/** Served to anyone at a permanent URL. */
|
|
31
|
+
public: boolean;
|
|
32
|
+
/** Where public files are linked from, such as a CDN; default the app's own `/api/files/<provider>`. */
|
|
33
|
+
publicUrl?: string;
|
|
34
|
+
/** Milliseconds between a file leaving its row and its bytes being deleted; default `files.retention` (0 when public). */
|
|
35
|
+
retention?: number;
|
|
36
|
+
};
|
|
37
|
+
//#endregion
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { formatUri, parseUri } from "./uri.js";
|
|
2
|
+
//#region src/files/ticket.ts
|
|
3
|
+
const sortedEntries = (derived) => Object.entries(derived).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
4
|
+
const parts = (uri, derived, scope, expires) => [
|
|
5
|
+
uri,
|
|
6
|
+
sortedEntries(derived),
|
|
7
|
+
scope.tenant,
|
|
8
|
+
scope.resource,
|
|
9
|
+
scope.field,
|
|
10
|
+
scope.user,
|
|
11
|
+
expires
|
|
12
|
+
];
|
|
13
|
+
/**
|
|
14
|
+
* What `:upload` answers with, and what a write sends back: the file's URI with its derived values, an expiry (Unix
|
|
15
|
+
* seconds) and a signature over them and the scope, so it works only for that uploader, tenant, resource and field.
|
|
16
|
+
*/
|
|
17
|
+
const issueTicket = async (signer, ref, derived, scope, expires) => {
|
|
18
|
+
const uri = formatUri(ref);
|
|
19
|
+
return `${uri}&${new URLSearchParams([
|
|
20
|
+
...sortedEntries(derived).map(([column, value]) => [`d.${column}`, JSON.stringify(value)]),
|
|
21
|
+
["exp", String(expires)],
|
|
22
|
+
["sig", await signer.sign("ticket", parts(uri, derived, scope, expires))]
|
|
23
|
+
])}`;
|
|
24
|
+
};
|
|
25
|
+
const parseJson = (text) => {
|
|
26
|
+
try {
|
|
27
|
+
return { value: JSON.parse(text) };
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (error instanceof SyntaxError) return void 0;
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
/** The clean URI and derived values of a ticket that verifies for `scope` and has not expired at `now` (Unix seconds). */
|
|
34
|
+
const readTicket = async (signer, value, scope, now) => {
|
|
35
|
+
const query = new URLSearchParams(value.slice(value.indexOf("?") + 1));
|
|
36
|
+
const signature = query.get("sig");
|
|
37
|
+
if (!value.includes("?") || signature === null) return {
|
|
38
|
+
ok: false,
|
|
39
|
+
reason: "not-a-ticket"
|
|
40
|
+
};
|
|
41
|
+
const derived = {};
|
|
42
|
+
for (const [name, text] of query) {
|
|
43
|
+
if (!name.startsWith("d.")) continue;
|
|
44
|
+
const parsed = parseJson(text);
|
|
45
|
+
if (!parsed) return {
|
|
46
|
+
ok: false,
|
|
47
|
+
reason: "invalid"
|
|
48
|
+
};
|
|
49
|
+
derived[name.slice(2)] = parsed.value;
|
|
50
|
+
}
|
|
51
|
+
const known = /* @__PURE__ */ new Set([
|
|
52
|
+
"name",
|
|
53
|
+
"size",
|
|
54
|
+
"exp",
|
|
55
|
+
"sig"
|
|
56
|
+
]);
|
|
57
|
+
if ([...query.keys()].some((name) => !known.has(name) && !name.startsWith("d."))) return {
|
|
58
|
+
ok: false,
|
|
59
|
+
reason: "invalid"
|
|
60
|
+
};
|
|
61
|
+
const parsed = parseUri(value.slice(0, value.indexOf("?")) + `?${new URLSearchParams({
|
|
62
|
+
name: query.get("name") ?? "",
|
|
63
|
+
size: query.get("size") ?? ""
|
|
64
|
+
})}`);
|
|
65
|
+
const expires = Number(query.get("exp"));
|
|
66
|
+
if (!parsed || parsed.size === void 0 || !Number.isSafeInteger(expires)) return {
|
|
67
|
+
ok: false,
|
|
68
|
+
reason: "invalid"
|
|
69
|
+
};
|
|
70
|
+
const uri = formatUri(parsed);
|
|
71
|
+
if (!await signer.verify("ticket", parts(uri, derived, scope, expires), signature)) return {
|
|
72
|
+
ok: false,
|
|
73
|
+
reason: "invalid"
|
|
74
|
+
};
|
|
75
|
+
if (expires < now) return {
|
|
76
|
+
ok: false,
|
|
77
|
+
reason: "expired"
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
ok: true,
|
|
81
|
+
uri,
|
|
82
|
+
ref: parsed,
|
|
83
|
+
derived
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
//#endregion
|
|
87
|
+
export { issueTicket, readTicket };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { HttpProblem, badRequest } from "../problem.js";
|
|
2
|
+
//#region src/files/upload-body.ts
|
|
3
|
+
const tooLarge = (maxSize) => new HttpProblem(413, "file-too-large", "Payload Too Large", `The file is larger than this field allows (${maxSize} bytes)`);
|
|
4
|
+
const multipartSlack = 65536;
|
|
5
|
+
/**
|
|
6
|
+
* The file of an upload request: the raw body with `?name=` and its Content-Type, or the one file part of a
|
|
7
|
+
* `multipart/form-data` body with that part's file name and type. A multipart body is read whole, after its size is
|
|
8
|
+
* checked; a raw body streams.
|
|
9
|
+
*/
|
|
10
|
+
const incomingFile = async (request, maxSize) => {
|
|
11
|
+
const header = request.headers.get("content-length");
|
|
12
|
+
const length = header === null ? void 0 : Number(header);
|
|
13
|
+
const contentType = request.headers.get("content-type") ?? void 0;
|
|
14
|
+
const name = new URL(request.url).searchParams.get("name") ?? void 0;
|
|
15
|
+
if (!contentType?.toLowerCase().startsWith("multipart/form-data")) {
|
|
16
|
+
if (length !== void 0 && length > maxSize) throw tooLarge(maxSize);
|
|
17
|
+
return {
|
|
18
|
+
body: request.body ?? new Blob([]).stream(),
|
|
19
|
+
...name !== void 0 && { name },
|
|
20
|
+
...contentType && { declared: contentType },
|
|
21
|
+
...length !== void 0 && { length }
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
if (length !== void 0 && length > maxSize + multipartSlack) throw tooLarge(maxSize);
|
|
25
|
+
const files = [...(await request.formData()).values()].filter((value) => typeof value !== "string");
|
|
26
|
+
if (files.length !== 1) throw badRequest("invalid-upload", `A multipart upload carries exactly one file part, not ${files.length}`);
|
|
27
|
+
const file = files[0];
|
|
28
|
+
if (file.size > maxSize) throw tooLarge(maxSize);
|
|
29
|
+
return {
|
|
30
|
+
body: file.stream(),
|
|
31
|
+
name: file.name || name,
|
|
32
|
+
...file.type && { declared: file.type },
|
|
33
|
+
length: file.size
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const concat = (chunks, length) => {
|
|
37
|
+
const joined = new Uint8Array(length);
|
|
38
|
+
let offset = 0;
|
|
39
|
+
for (const chunk of chunks) {
|
|
40
|
+
joined.set(chunk, offset);
|
|
41
|
+
offset += chunk.length;
|
|
42
|
+
}
|
|
43
|
+
return joined;
|
|
44
|
+
};
|
|
45
|
+
/** The first `want` bytes (or all, when there are fewer), and whether that was the whole stream. */
|
|
46
|
+
const readHead = async (reader, want) => {
|
|
47
|
+
const chunks = [];
|
|
48
|
+
let length = 0;
|
|
49
|
+
while (length < want) {
|
|
50
|
+
const next = await reader.read();
|
|
51
|
+
if (next.done) return {
|
|
52
|
+
head: concat(chunks, length),
|
|
53
|
+
complete: true
|
|
54
|
+
};
|
|
55
|
+
chunks.push(next.value);
|
|
56
|
+
length += next.value.length;
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
head: concat(chunks, length),
|
|
60
|
+
complete: false
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
var FileTooLarge = class extends Error {};
|
|
64
|
+
/** The head followed by the rest of the reader, failing with `FileTooLarge` past `maxSize` bytes. */
|
|
65
|
+
const limitedStream = (head, reader, maxSize) => {
|
|
66
|
+
let size = head.length;
|
|
67
|
+
return new ReadableStream({
|
|
68
|
+
start: (controller) => {
|
|
69
|
+
if (head.length > 0) controller.enqueue(head);
|
|
70
|
+
},
|
|
71
|
+
pull: async (controller) => {
|
|
72
|
+
const next = await reader.read();
|
|
73
|
+
if (next.done) return controller.close();
|
|
74
|
+
size += next.value.length;
|
|
75
|
+
if (size > maxSize) {
|
|
76
|
+
await reader.cancel();
|
|
77
|
+
return controller.error(new FileTooLarge());
|
|
78
|
+
}
|
|
79
|
+
controller.enqueue(next.value);
|
|
80
|
+
},
|
|
81
|
+
cancel: (reason) => reader.cancel(reason)
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
//#endregion
|
|
85
|
+
export { FileTooLarge, incomingFile, limitedStream, readHead, tooLarge };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { HttpProblem, badRequest } from "../problem.js";
|
|
2
|
+
import { accepts } from "./media-types.js";
|
|
3
|
+
import { cleanName, tenantSegment } from "./uri.js";
|
|
4
|
+
import { tenantScope } from "../tenant.js";
|
|
5
|
+
import { requestAccess, requireOperation, writableFields } from "../request-access.js";
|
|
6
|
+
import { issueTicket } from "./ticket.js";
|
|
7
|
+
import { ticketScope } from "./attach.js";
|
|
8
|
+
import { ticketSeconds } from "./runtime.js";
|
|
9
|
+
import { decideType } from "./detect.js";
|
|
10
|
+
import { FileTooLarge, incomingFile, limitedStream, readHead, tooLarge } from "./upload-body.js";
|
|
11
|
+
//#region src/files/upload.ts
|
|
12
|
+
/** How many first bytes processors get as `head`: enough for image headers, EXIF included. */
|
|
13
|
+
const headBytes = 262144;
|
|
14
|
+
const uploadField = (access, name) => {
|
|
15
|
+
if (!access.resolved.operations.create && !access.resolved.operations.update) requireOperation(access, "update");
|
|
16
|
+
const field = name !== void 0 && Object.hasOwn(access.readable.fields, name) ? access.full.fields[name] : void 0;
|
|
17
|
+
if (!field?.file) throw badRequest("invalid-parameter", `"${name ?? ""}" is not a file field of ${access.entry.name}`, { errors: [{
|
|
18
|
+
parameter: "field",
|
|
19
|
+
message: "Not a file field"
|
|
20
|
+
}] });
|
|
21
|
+
if (!writableFields(access, "create").includes(field.name) && !writableFields(access, "update").includes(field.name)) throw badRequest("invalid-parameter", `"${field.name}" is read-only`, { errors: [{
|
|
22
|
+
parameter: "field",
|
|
23
|
+
message: "Read-only"
|
|
24
|
+
}] });
|
|
25
|
+
return field;
|
|
26
|
+
};
|
|
27
|
+
const deriveValues = async (entry, field, input) => {
|
|
28
|
+
const source = entry.source.state.fields?.[field.name];
|
|
29
|
+
const derived = {};
|
|
30
|
+
for (const [target, processor] of Object.entries(source?.meta.file?.derive ?? {})) {
|
|
31
|
+
const value = !processor.accepts || accepts(processor.accepts, input.type) ? await processor.run({
|
|
32
|
+
name: input.name,
|
|
33
|
+
type: input.type,
|
|
34
|
+
size: input.size,
|
|
35
|
+
head: input.head,
|
|
36
|
+
read: async () => {
|
|
37
|
+
const body = await input.store.read(input.path);
|
|
38
|
+
if (!body) throw new Error(`The upload ${input.path} is gone from its store`);
|
|
39
|
+
return body;
|
|
40
|
+
}
|
|
41
|
+
}) : null;
|
|
42
|
+
const schema = entry.source.state.fields?.[target]?.schema;
|
|
43
|
+
const normalized = value === void 0 ? null : value;
|
|
44
|
+
if (schema && !schema.safeParse(normalized).success) throw new Error(`The processor for "${target}" of ${entry.name} returned ${JSON.stringify(normalized)}, which that field refuses`);
|
|
45
|
+
derived[target] = normalized;
|
|
46
|
+
}
|
|
47
|
+
return derived;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* `POST /{resource}:upload?field=<field>`: streams one file to the field's provider and answers with a ticket for a
|
|
51
|
+
* write. The type is decided from the first bytes before anything is stored, a refused type gets 415 and a file over
|
|
52
|
+
* `maxSize` 413. The field's processors then compute its derived values, which the ticket carries signed. An upload no
|
|
53
|
+
* write ever uses is deleted after the ticket expires.
|
|
54
|
+
*/
|
|
55
|
+
const uploadFile = async (deps, entry, session, request) => {
|
|
56
|
+
const files = deps.files;
|
|
57
|
+
const access = await requestAccess(deps, entry, session);
|
|
58
|
+
const field = uploadField(access, new URL(request.url).searchParams.get("field") ?? void 0);
|
|
59
|
+
if (!files) throw new Error(`Resource "${entry.name}" has file fields but no file runtime`);
|
|
60
|
+
const { accept, maxSize, provider: providerName } = field.file;
|
|
61
|
+
const provider = files.providers[providerName];
|
|
62
|
+
const incoming = await incomingFile(request, maxSize);
|
|
63
|
+
const reader = incoming.body.getReader();
|
|
64
|
+
const { head, complete } = await readHead(reader, headBytes);
|
|
65
|
+
if (head.length > maxSize) {
|
|
66
|
+
await reader.cancel();
|
|
67
|
+
throw tooLarge(maxSize);
|
|
68
|
+
}
|
|
69
|
+
const name = cleanName(incoming.name);
|
|
70
|
+
const decision = await decideType({
|
|
71
|
+
head,
|
|
72
|
+
complete,
|
|
73
|
+
name,
|
|
74
|
+
accept,
|
|
75
|
+
...incoming.declared && { declared: incoming.declared }
|
|
76
|
+
});
|
|
77
|
+
if (!accepts(accept, decision.type)) {
|
|
78
|
+
await reader.cancel();
|
|
79
|
+
throw new HttpProblem(415, "unsupported-type", "Unsupported Media Type", `This field takes ${accept.join(", ")}; the file is ${decision.type}`, {
|
|
80
|
+
detected: decision.type,
|
|
81
|
+
accept
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const expires = Math.floor(Date.now() / 1e3) + ticketSeconds;
|
|
85
|
+
const path = `${await tenantSegment(tenantScope(entry, session)?.tenantValue)}/${crypto.randomUUID()}.${decision.extension}`;
|
|
86
|
+
await files.schedule.add([{
|
|
87
|
+
provider: providerName,
|
|
88
|
+
path,
|
|
89
|
+
due: /* @__PURE__ */ new Date((expires + 3600) * 1e3)
|
|
90
|
+
}]);
|
|
91
|
+
let size;
|
|
92
|
+
try {
|
|
93
|
+
size = (await provider.store.put(path, limitedStream(head, reader, maxSize))).size;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof FileTooLarge) throw tooLarge(maxSize);
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
const derived = await deriveValues(entry, field, {
|
|
99
|
+
name,
|
|
100
|
+
type: decision.type,
|
|
101
|
+
size,
|
|
102
|
+
head,
|
|
103
|
+
store: provider.store,
|
|
104
|
+
path
|
|
105
|
+
});
|
|
106
|
+
const ref = {
|
|
107
|
+
provider: providerName,
|
|
108
|
+
path,
|
|
109
|
+
name,
|
|
110
|
+
size
|
|
111
|
+
};
|
|
112
|
+
return {
|
|
113
|
+
value: await issueTicket(files.signer, ref, derived, ticketScope(access, field.name), expires),
|
|
114
|
+
file: {
|
|
115
|
+
name,
|
|
116
|
+
type: decision.type,
|
|
117
|
+
size
|
|
118
|
+
},
|
|
119
|
+
derived,
|
|
120
|
+
...decision.corrected && { corrected: decision.corrected }
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
//#endregion
|
|
124
|
+
export { headBytes, uploadFile };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { sha256Hex } from "../hash.js";
|
|
2
|
+
//#region src/files/uri.ts
|
|
3
|
+
const provider = /^[a-z][a-z0-9-]{0,31}$/;
|
|
4
|
+
const segment = /^[A-Za-z0-9_-][A-Za-z0-9._-]{0,127}$/;
|
|
5
|
+
const maxPath = 512;
|
|
6
|
+
const isFilePath = (path) => path.length <= maxPath && path.split("/").every((part) => segment.test(part));
|
|
7
|
+
/** The identity of a value or ticket: `{provider}:{path}`. */
|
|
8
|
+
const baseOf = (value) => value.split("?", 1)[0];
|
|
9
|
+
const formatUri = (ref) => {
|
|
10
|
+
const query = new URLSearchParams({
|
|
11
|
+
name: ref.name,
|
|
12
|
+
...ref.size !== void 0 && { size: String(ref.size) }
|
|
13
|
+
});
|
|
14
|
+
return `${ref.provider}:${ref.path}?${query}`;
|
|
15
|
+
};
|
|
16
|
+
const lastSegment = (path) => path.slice(path.lastIndexOf("/") + 1);
|
|
17
|
+
/** The file a value names, or `undefined` when it does not parse. */
|
|
18
|
+
const parseUri = (value) => {
|
|
19
|
+
const match = /^([a-z][a-z0-9-]*):([^?#]+)(?:\?([^#]*))?$/.exec(value);
|
|
20
|
+
if (!match || !provider.test(match[1]) || !isFilePath(match[2])) return void 0;
|
|
21
|
+
const query = new URLSearchParams(match[3] ?? "");
|
|
22
|
+
const size = query.get("size");
|
|
23
|
+
if (size !== null && !/^\d{1,15}$/.test(size)) return void 0;
|
|
24
|
+
return {
|
|
25
|
+
provider: match[1],
|
|
26
|
+
path: match[2],
|
|
27
|
+
name: query.get("name") || lastSegment(match[2]),
|
|
28
|
+
...size !== null && { size: Number(size) }
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
const encoder = new TextEncoder();
|
|
32
|
+
/** The uploader's file name without a path or control characters, at most 255 bytes; `file` when nothing is left. */
|
|
33
|
+
const cleanName = (name) => {
|
|
34
|
+
const text = name ?? "";
|
|
35
|
+
let clean = text.slice(Math.max(text.lastIndexOf("/"), text.lastIndexOf("\\")) + 1).replace(/[\u0000-\u001f\u007f]/g, "").trim();
|
|
36
|
+
while (encoder.encode(clean).length > 255) clean = clean.slice(0, -1);
|
|
37
|
+
return clean || "file";
|
|
38
|
+
};
|
|
39
|
+
const tenantText = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
40
|
+
/** The first path segment of an upload: the row's tenant as it is when it fits, a hash of it when not, `_` without one. */
|
|
41
|
+
const tenantSegment = async (tenant) => {
|
|
42
|
+
if (tenant === void 0) return "_";
|
|
43
|
+
const text = String(tenant);
|
|
44
|
+
return tenantText.test(text) ? text : `t${(await sha256Hex(text)).slice(0, 32)}`;
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
export { baseOf, cleanName, formatUri, isFilePath, parseUri, tenantSegment };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Session } from "../types.js";
|
|
2
|
+
import { FunctionRecords } from "./records.js";
|
|
3
|
+
import { Db } from "@protobase/query";
|
|
4
|
+
//#region src/functions/define-function.d.ts
|
|
5
|
+
/** What every function gets: its name and the raw database. */
|
|
6
|
+
export type PublicFunctionContext = {
|
|
7
|
+
/** The function's name, as in `/api/functions/<name>`. */
|
|
8
|
+
name: string;
|
|
9
|
+
/** The database itself, with no access rules, row filters or tenant scope: privileged work only. */
|
|
10
|
+
db: Db;
|
|
11
|
+
/** The caller, through the app's own `authenticate`: rejects with a 401 problem when the request carries no valid token. */
|
|
12
|
+
caller: () => Promise<FunctionCaller>;
|
|
13
|
+
};
|
|
14
|
+
/** Who called a function, and the app's resources as them. */
|
|
15
|
+
export type FunctionCaller = {
|
|
16
|
+
session: Session;
|
|
17
|
+
/** The app's resources as this caller, under the same access rules, row filters and tenant scope as the REST API. */
|
|
18
|
+
records: FunctionRecords;
|
|
19
|
+
};
|
|
20
|
+
/** What a function that needs a signed-in caller gets: the caller resolved before it runs. */
|
|
21
|
+
export type FunctionContext = PublicFunctionContext & FunctionCaller;
|
|
22
|
+
/** A Web-standard handler. Its request's path is the part after `/api/functions/<name>`: `/` or `/more/parts`. */
|
|
23
|
+
export type FunctionHandler = (request: Request, context: FunctionContext) => Response | Promise<Response>;
|
|
24
|
+
export type PublicFunctionHandler = (request: Request, context: PublicFunctionContext) => Response | Promise<Response>;
|
|
25
|
+
/** Any app with a Hono-style `fetch`, such as `new Hono()`: its `c.env` is the function's context. */
|
|
26
|
+
export type FunctionApp<Context = FunctionContext> = {
|
|
27
|
+
fetch: (request: Request, env: Context) => Response | Promise<Response>;
|
|
28
|
+
};
|
|
29
|
+
/** `hono/cors` options: an origin, a list of them, or a function of the request's origin. */
|
|
30
|
+
export type CorsOptions = {
|
|
31
|
+
origin: string | string[] | ((origin: string) => string | undefined | null);
|
|
32
|
+
allowMethods?: string[];
|
|
33
|
+
allowHeaders?: string[];
|
|
34
|
+
exposeHeaders?: string[];
|
|
35
|
+
maxAge?: number;
|
|
36
|
+
credentials?: boolean;
|
|
37
|
+
};
|
|
38
|
+
type SharedOptions = {
|
|
39
|
+
/** Answers CORS preflights before authentication and adds the CORS headers to every response, problems included. */
|
|
40
|
+
cors?: CorsOptions;
|
|
41
|
+
};
|
|
42
|
+
/** A function anyone may call, such as a webhook; it can still ask for a caller with `context.caller()`. */
|
|
43
|
+
export type PublicFunctionOptions = SharedOptions & {
|
|
44
|
+
public: true;
|
|
45
|
+
};
|
|
46
|
+
export type ProtectedFunctionOptions = SharedOptions & {
|
|
47
|
+
public?: false;
|
|
48
|
+
/** The caller needs one of these roles, otherwise the call is a 403 problem. */
|
|
49
|
+
roles?: string[];
|
|
50
|
+
};
|
|
51
|
+
export type FunctionOptions = PublicFunctionOptions | ProtectedFunctionOptions;
|
|
52
|
+
/** A function as `createAdmin` serves it: its options and what answers its requests. */
|
|
53
|
+
export type ApiFunction = {
|
|
54
|
+
kind: 'function';
|
|
55
|
+
options: PublicFunctionOptions;
|
|
56
|
+
handler: PublicFunctionHandler | FunctionApp<PublicFunctionContext>;
|
|
57
|
+
} | {
|
|
58
|
+
kind: 'function';
|
|
59
|
+
options: ProtectedFunctionOptions;
|
|
60
|
+
handler: FunctionHandler | FunctionApp;
|
|
61
|
+
};
|
|
62
|
+
/** What a function file may default-export: `defineFunction(...)`, a bare handler or a Hono app (both protected). */
|
|
63
|
+
export type FunctionSource = ApiFunction | FunctionHandler | FunctionApp;
|
|
64
|
+
/**
|
|
65
|
+
* A function of your own, served at `/api/functions/<name>`. Without options it needs a signed-in caller, through the
|
|
66
|
+
* app's `authenticate`; `{ public: true }` opens it to anyone, as for a webhook.
|
|
67
|
+
*/
|
|
68
|
+
export declare function defineFunction(handler: FunctionHandler | FunctionApp): ApiFunction;
|
|
69
|
+
export declare function defineFunction(options: PublicFunctionOptions, handler: PublicFunctionHandler | FunctionApp<PublicFunctionContext>): ApiFunction;
|
|
70
|
+
export declare function defineFunction(options: ProtectedFunctionOptions, handler: FunctionHandler | FunctionApp): ApiFunction;
|
|
71
|
+
//#endregion
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/functions/define-function.ts
|
|
2
|
+
function defineFunction(first, handler) {
|
|
3
|
+
if (handler === void 0) return {
|
|
4
|
+
kind: "function",
|
|
5
|
+
options: {},
|
|
6
|
+
handler: first
|
|
7
|
+
};
|
|
8
|
+
return {
|
|
9
|
+
kind: "function",
|
|
10
|
+
options: first,
|
|
11
|
+
handler
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const isApp = (value) => typeof value === "object" && value !== null && typeof value.fetch === "function";
|
|
15
|
+
const isApiFunction = (value) => typeof value === "object" && value !== null && value.kind === "function";
|
|
16
|
+
/** A function's name is its URL segment: letters, digits, `-` and `_`, starting with a letter or digit. */
|
|
17
|
+
const functionNamePattern = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
|
|
18
|
+
/** The function a source stands for; anything else is an error naming it. */
|
|
19
|
+
const toApiFunction = (name, source) => {
|
|
20
|
+
if (!functionNamePattern.test(name)) throw new Error(`The function name "${name}" is not a URL segment: use letters, digits, - and _`);
|
|
21
|
+
if (isApiFunction(source)) {
|
|
22
|
+
if (typeof source.handler !== "function" && !isApp(source.handler)) throw new Error(`The function "${name}" has no handler: pass a (request, context) function or a Hono app`);
|
|
23
|
+
return source;
|
|
24
|
+
}
|
|
25
|
+
if (typeof source === "function" || isApp(source)) return defineFunction(source);
|
|
26
|
+
throw new Error(`The function "${name}" must default-export defineFunction(...), a (request, context) handler or a Hono app`);
|
|
27
|
+
};
|
|
28
|
+
/** Calls a function's handler or app with its request and context; anything but a Response is an error. */
|
|
29
|
+
const invokeFunction = async (name, fn, request, context) => {
|
|
30
|
+
const { handler } = fn;
|
|
31
|
+
const response = isApp(handler) ? await handler.fetch(request, context) : await handler(request, context);
|
|
32
|
+
if (!(response instanceof Response)) throw new Error(`The function "${name}" returned no Response`);
|
|
33
|
+
return response;
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
export { defineFunction, functionNamePattern, invokeFunction, toApiFunction };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FunctionContext, PublicFunctionContext } from "./define-function.js";
|
|
2
|
+
import { Env, Hono } from "hono";
|
|
3
|
+
//#region src/functions/function-app.d.ts
|
|
4
|
+
/** A Hono app's env for a protected function: `c.env` is its context, `c.env.records` the caller's records. */
|
|
5
|
+
export type FunctionEnv = {
|
|
6
|
+
Bindings: FunctionContext;
|
|
7
|
+
};
|
|
8
|
+
/** A Hono app's env for a public function. */
|
|
9
|
+
export type PublicFunctionEnv = {
|
|
10
|
+
Bindings: PublicFunctionContext;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* `new Hono()` for a function, whose errors and unknown paths reach the app's own error handling: problems as
|
|
14
|
+
* problem+json, anything else a 500 the app reports. A plain Hono app answers them itself.
|
|
15
|
+
*/
|
|
16
|
+
export declare const functionApp: <E extends Env = FunctionEnv>() => Hono<E, import("hono/types").BlankSchema, "/">;
|
|
17
|
+
//#endregion
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { notFound } from "../problem.js";
|
|
2
|
+
import { Hono } from "hono";
|
|
3
|
+
//#region src/functions/function-app.ts
|
|
4
|
+
/**
|
|
5
|
+
* `new Hono()` for a function, whose errors and unknown paths reach the app's own error handling: problems as
|
|
6
|
+
* problem+json, anything else a 500 the app reports. A plain Hono app answers them itself.
|
|
7
|
+
*/
|
|
8
|
+
const functionApp = () => {
|
|
9
|
+
const app = new Hono();
|
|
10
|
+
app.onError((error) => {
|
|
11
|
+
throw error;
|
|
12
|
+
});
|
|
13
|
+
app.notFound((c) => {
|
|
14
|
+
throw notFound(`There is no route at ${c.req.path}`);
|
|
15
|
+
});
|
|
16
|
+
return app;
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { functionApp };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//#region src/functions/read-json.d.ts
|
|
2
|
+
type Issue = {
|
|
3
|
+
message: string;
|
|
4
|
+
path?: ReadonlyArray<PropertyKey | {
|
|
5
|
+
key: PropertyKey;
|
|
6
|
+
}>;
|
|
7
|
+
};
|
|
8
|
+
/** A Standard Schema (https://standardschema.dev), such as a zod 4, Valibot or ArkType schema. */
|
|
9
|
+
export type StandardSchema<Output = unknown> = {
|
|
10
|
+
readonly '~standard': {
|
|
11
|
+
readonly version: 1;
|
|
12
|
+
readonly validate: (value: unknown) => {
|
|
13
|
+
value: Output;
|
|
14
|
+
issues?: undefined;
|
|
15
|
+
} | {
|
|
16
|
+
issues: ReadonlyArray<Issue>;
|
|
17
|
+
} | Promise<{
|
|
18
|
+
value: Output;
|
|
19
|
+
issues?: undefined;
|
|
20
|
+
} | {
|
|
21
|
+
issues: ReadonlyArray<Issue>;
|
|
22
|
+
}>;
|
|
23
|
+
readonly types?: {
|
|
24
|
+
readonly input: unknown;
|
|
25
|
+
readonly output: Output;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* The request's JSON body, checked against `schema` when one is given. A body that is no JSON is a 400 `malformed-json`
|
|
31
|
+
* problem, one the schema refuses a 400 `invalid-body` problem listing each issue by field.
|
|
32
|
+
*/
|
|
33
|
+
export declare function readJson(request: Request): Promise<unknown>;
|
|
34
|
+
export declare function readJson<Output>(request: Request, schema: StandardSchema<Output>): Promise<Output>;
|
|
35
|
+
//#endregion
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { badRequest } from "../problem.js";
|
|
2
|
+
//#region src/functions/read-json.ts
|
|
3
|
+
const fieldOf = (issue) => (issue.path ?? []).map((part) => String(typeof part === "object" ? part.key : part)).join(".");
|
|
4
|
+
async function readJson(request, schema) {
|
|
5
|
+
const text = await request.text();
|
|
6
|
+
let body;
|
|
7
|
+
try {
|
|
8
|
+
body = JSON.parse(text);
|
|
9
|
+
} catch (error) {
|
|
10
|
+
if (error instanceof SyntaxError) throw badRequest("malformed-json", "The request body is not valid JSON");
|
|
11
|
+
throw error;
|
|
12
|
+
}
|
|
13
|
+
if (!schema) return body;
|
|
14
|
+
const result = await schema["~standard"].validate(body);
|
|
15
|
+
if (!result.issues) return result.value;
|
|
16
|
+
const errors = result.issues.map((issue) => ({
|
|
17
|
+
field: fieldOf(issue),
|
|
18
|
+
message: issue.message
|
|
19
|
+
}));
|
|
20
|
+
const first = errors[0];
|
|
21
|
+
throw badRequest("invalid-body", `The request body is invalid: ${first.field ? `${first.field}: ` : ""}${first.message}`, { errors });
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { readJson };
|