@weft-framework/server 0.0.0-stage → 0.0.3
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/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/account.d.ts +124 -0
- package/dist/account.js +344 -0
- package/dist/action.d.ts +43 -0
- package/dist/action.js +135 -0
- package/dist/admin.d.ts +99 -0
- package/dist/admin.js +431 -0
- package/dist/api.d.ts +21 -0
- package/dist/api.js +72 -0
- package/dist/app.d.ts +202 -0
- package/dist/app.js +3692 -0
- package/dist/assets.d.ts +4 -0
- package/dist/assets.js +91 -0
- package/dist/auth.d.ts +150 -0
- package/dist/auth.js +215 -0
- package/dist/backup.d.ts +46 -0
- package/dist/backup.js +345 -0
- package/dist/build.d.ts +33 -0
- package/dist/build.js +274 -0
- package/dist/bun.d.ts +30 -0
- package/dist/bun.js +53 -0
- package/dist/calendar.d.ts +161 -0
- package/dist/calendar.js +690 -0
- package/dist/compiler.d.ts +13 -0
- package/dist/compiler.js +58 -0
- package/dist/compress.d.ts +6 -0
- package/dist/compress.js +78 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.js +317 -0
- package/dist/context.d.ts +9 -0
- package/dist/context.js +1 -0
- package/dist/contract.d.ts +18 -0
- package/dist/contract.js +171 -0
- package/dist/datamap.d.ts +24 -0
- package/dist/datamap.js +365 -0
- package/dist/db-backup.d.ts +27 -0
- package/dist/db-backup.js +115 -0
- package/dist/dev-events.d.ts +14 -0
- package/dist/dev-events.js +135 -0
- package/dist/dev.d.ts +1 -0
- package/dist/dev.js +240 -0
- package/dist/devtools-client.d.ts +1 -0
- package/dist/devtools-client.js +362 -0
- package/dist/devtools.d.ts +87 -0
- package/dist/devtools.js +68 -0
- package/dist/edge.d.ts +3 -0
- package/dist/edge.js +7 -0
- package/dist/email-signin.d.ts +92 -0
- package/dist/email-signin.js +259 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.js +15 -0
- package/dist/explain.d.ts +12 -0
- package/dist/explain.js +217 -0
- package/dist/files.d.ts +6 -0
- package/dist/files.js +170 -0
- package/dist/grants.d.ts +69 -0
- package/dist/grants.js +206 -0
- package/dist/image/bitmap.d.ts +11 -0
- package/dist/image/bitmap.js +172 -0
- package/dist/image/jpeg.d.ts +10 -0
- package/dist/image/jpeg.js +697 -0
- package/dist/image/png.d.ts +8 -0
- package/dist/image/png.js +282 -0
- package/dist/image/transform.d.ts +20 -0
- package/dist/image/transform.js +52 -0
- package/dist/image/worker.d.ts +1 -0
- package/dist/image/worker.js +11 -0
- package/dist/images.d.ts +39 -0
- package/dist/images.js +283 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +31 -0
- package/dist/inspect-hub.d.ts +42 -0
- package/dist/inspect-hub.js +14 -0
- package/dist/inspector.d.ts +51 -0
- package/dist/inspector.js +193 -0
- package/dist/job-admin.d.ts +55 -0
- package/dist/job-admin.js +89 -0
- package/dist/jobs.d.ts +222 -0
- package/dist/jobs.js +789 -0
- package/dist/live.d.ts +72 -0
- package/dist/live.js +481 -0
- package/dist/load-config.d.ts +2 -0
- package/dist/load-config.js +41 -0
- package/dist/log.d.ts +20 -0
- package/dist/log.js +143 -0
- package/dist/mail.d.ts +189 -0
- package/dist/mail.js +999 -0
- package/dist/media.d.ts +129 -0
- package/dist/media.js +470 -0
- package/dist/meta.d.ts +54 -0
- package/dist/meta.js +146 -0
- package/dist/middleware.d.ts +20 -0
- package/dist/middleware.js +101 -0
- package/dist/migrate.d.ts +12 -0
- package/dist/migrate.js +88 -0
- package/dist/node.d.ts +26 -0
- package/dist/node.js +96 -0
- package/dist/notice.d.ts +21 -0
- package/dist/notice.js +48 -0
- package/dist/npm.d.ts +16 -0
- package/dist/npm.js +347 -0
- package/dist/oauth.d.ts +76 -0
- package/dist/oauth.js +408 -0
- package/dist/otel.d.ts +57 -0
- package/dist/otel.js +293 -0
- package/dist/passkey.d.ts +99 -0
- package/dist/passkey.js +572 -0
- package/dist/password.d.ts +8 -0
- package/dist/password.js +65 -0
- package/dist/pdf.d.ts +46 -0
- package/dist/pdf.js +332 -0
- package/dist/personal-data.d.ts +22 -0
- package/dist/personal-data.js +148 -0
- package/dist/pointers.d.ts +9 -0
- package/dist/pointers.js +148 -0
- package/dist/postcss.d.ts +3 -0
- package/dist/postcss.js +89 -0
- package/dist/presence.d.ts +27 -0
- package/dist/presence.js +161 -0
- package/dist/preview.d.ts +25 -0
- package/dist/preview.js +353 -0
- package/dist/production.d.ts +16 -0
- package/dist/production.js +119 -0
- package/dist/project.d.ts +23 -0
- package/dist/project.js +536 -0
- package/dist/regions.d.ts +47 -0
- package/dist/regions.js +511 -0
- package/dist/replay.d.ts +44 -0
- package/dist/replay.js +259 -0
- package/dist/router.d.ts +29 -0
- package/dist/router.js +146 -0
- package/dist/rules-diff.d.ts +18 -0
- package/dist/rules-diff.js +165 -0
- package/dist/rules-test.d.ts +17 -0
- package/dist/rules-test.js +108 -0
- package/dist/saml.d.ts +160 -0
- package/dist/saml.js +959 -0
- package/dist/seal.d.ts +35 -0
- package/dist/seal.js +107 -0
- package/dist/secret.d.ts +14 -0
- package/dist/secret.js +110 -0
- package/dist/security.d.ts +53 -0
- package/dist/security.js +250 -0
- package/dist/seed.d.ts +16 -0
- package/dist/seed.js +94 -0
- package/dist/seo.d.ts +42 -0
- package/dist/seo.js +175 -0
- package/dist/settings.d.ts +79 -0
- package/dist/settings.js +212 -0
- package/dist/shared.d.ts +15 -0
- package/dist/shared.js +283 -0
- package/dist/shell.d.ts +12 -0
- package/dist/shell.js +118 -0
- package/dist/storage.d.ts +13 -0
- package/dist/storage.js +55 -0
- package/dist/support.d.ts +83 -0
- package/dist/support.js +236 -0
- package/dist/sync.d.ts +35 -0
- package/dist/sync.js +167 -0
- package/dist/tailwind.d.ts +14 -0
- package/dist/tailwind.js +117 -0
- package/dist/two-factor.d.ts +103 -0
- package/dist/two-factor.js +435 -0
- package/dist/uploads.d.ts +185 -0
- package/dist/uploads.js +991 -0
- package/dist/webhooks.d.ts +169 -0
- package/dist/webhooks.js +758 -0
- package/dist/webpush.d.ts +97 -0
- package/dist/webpush.js +372 -0
- package/dist/wizard.d.ts +141 -0
- package/dist/wizard.js +410 -0
- package/dist/workflows.d.ts +134 -0
- package/dist/workflows.js +511 -0
- package/package.json +186 -4
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface Compiler {
|
|
2
|
+
cmd: string;
|
|
3
|
+
args: string[];
|
|
4
|
+
cwd?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function findCompiler(from: string): Compiler;
|
|
7
|
+
export declare class CompileError extends Error {
|
|
8
|
+
readonly diagnostics: string;
|
|
9
|
+
constructor(diagnostics: string);
|
|
10
|
+
}
|
|
11
|
+
export declare function compileFile(compiler: Compiler, file: string): string;
|
|
12
|
+
export declare function compileClient(compiler: Compiler, file: string): string;
|
|
13
|
+
export declare function compileTree(compiler: Compiler, src: string, out: string): void;
|
package/dist/compiler.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
export function findCompiler(from) {
|
|
6
|
+
if (process.env.WEFT_BIN) {
|
|
7
|
+
return { cmd: process.env.WEFT_BIN, args: [] };
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const cli = createRequire(join(resolve(from), "package.json"))("@weft-framework/cli");
|
|
11
|
+
return { cmd: cli.binaryPath(), args: [] };
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
execFileSync("weft", ["version"], { stdio: "ignore" });
|
|
17
|
+
return { cmd: "weft", args: [] };
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
}
|
|
21
|
+
for (let dir = resolve(from);; dir = dirname(dir)) {
|
|
22
|
+
if (existsSync(join(dir, "crates", "weft-cli", "Cargo.toml"))) {
|
|
23
|
+
return { cmd: "cargo", args: ["run", "-q", "-p", "weft-cli", "--"], cwd: dir };
|
|
24
|
+
}
|
|
25
|
+
if (dirname(dir) === dir) {
|
|
26
|
+
throw new Error("weft compiler not found: set WEFT_BIN or install `weft`");
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export class CompileError extends Error {
|
|
31
|
+
diagnostics;
|
|
32
|
+
constructor(diagnostics) {
|
|
33
|
+
super(diagnostics || "compilation failed");
|
|
34
|
+
this.diagnostics = diagnostics;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function run(compiler, args) {
|
|
38
|
+
try {
|
|
39
|
+
return execFileSync(compiler.cmd, [...compiler.args, ...args], {
|
|
40
|
+
cwd: compiler.cwd,
|
|
41
|
+
encoding: "utf8",
|
|
42
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
const e = err;
|
|
47
|
+
throw new CompileError((e.stderr ?? "").trim() || String(e.message));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function compileFile(compiler, file) {
|
|
51
|
+
return run(compiler, ["emit", resolve(file)]);
|
|
52
|
+
}
|
|
53
|
+
export function compileClient(compiler, file) {
|
|
54
|
+
return run(compiler, ["emit", "--client", resolve(file)]);
|
|
55
|
+
}
|
|
56
|
+
export function compileTree(compiler, src, out) {
|
|
57
|
+
run(compiler, ["compile", resolve(src), "--out", resolve(out)]);
|
|
58
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export interface CompressOptions {
|
|
2
|
+
minBytes?: number;
|
|
3
|
+
}
|
|
4
|
+
export declare function compressibleType(type: string): boolean;
|
|
5
|
+
export declare function acceptsGzip(header: string | null): boolean;
|
|
6
|
+
export declare function compress(req: Request, res: Response, opts?: CompressOptions): Response;
|
package/dist/compress.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const COMPRESSIBLE = new Set([
|
|
2
|
+
"application/javascript",
|
|
3
|
+
"application/json",
|
|
4
|
+
"application/ld+json",
|
|
5
|
+
"application/manifest+json",
|
|
6
|
+
"application/wasm",
|
|
7
|
+
"application/xml",
|
|
8
|
+
"application/atom+xml",
|
|
9
|
+
"application/rss+xml",
|
|
10
|
+
"image/svg+xml",
|
|
11
|
+
"text/javascript",
|
|
12
|
+
]);
|
|
13
|
+
function mediaType(res) {
|
|
14
|
+
return (res.headers.get("content-type") ?? "").split(";")[0].trim().toLowerCase();
|
|
15
|
+
}
|
|
16
|
+
export function compressibleType(type) {
|
|
17
|
+
if (type === "text/event-stream") {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return type.startsWith("text/") || COMPRESSIBLE.has(type) || type.endsWith("+json") || type.endsWith("+xml");
|
|
21
|
+
}
|
|
22
|
+
export function acceptsGzip(header) {
|
|
23
|
+
if (!header) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
let star = null;
|
|
27
|
+
for (const part of header.split(",")) {
|
|
28
|
+
const [name, ...params] = part.trim().toLowerCase().split(";");
|
|
29
|
+
let q = 1;
|
|
30
|
+
for (const p of params) {
|
|
31
|
+
const m = /^\s*q\s*=\s*([0-9.]+)\s*$/.exec(p);
|
|
32
|
+
if (m) {
|
|
33
|
+
q = Number(m[1]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (name.trim() === "gzip") {
|
|
37
|
+
return q > 0;
|
|
38
|
+
}
|
|
39
|
+
if (name.trim() === "*") {
|
|
40
|
+
star = q > 0;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return star === true;
|
|
44
|
+
}
|
|
45
|
+
export function compress(req, res, opts = {}) {
|
|
46
|
+
const type = mediaType(res);
|
|
47
|
+
if (!compressibleType(type) ||
|
|
48
|
+
res.body === null ||
|
|
49
|
+
req.method === "HEAD" ||
|
|
50
|
+
res.status < 200 ||
|
|
51
|
+
res.status === 204 ||
|
|
52
|
+
res.status === 206 ||
|
|
53
|
+
res.status === 304 ||
|
|
54
|
+
res.headers.has("content-encoding") ||
|
|
55
|
+
/\bno-transform\b/i.test(res.headers.get("cache-control") ?? "") ||
|
|
56
|
+
res.headers.get("x-accel-buffering") === "no") {
|
|
57
|
+
return res;
|
|
58
|
+
}
|
|
59
|
+
const headers = new Headers(res.headers);
|
|
60
|
+
const vary = headers.get("vary");
|
|
61
|
+
if (!vary || !/(^|,)\s*(accept-encoding|\*)\s*(,|$)/i.test(vary)) {
|
|
62
|
+
headers.set("vary", vary ? `${vary}, Accept-Encoding` : "Accept-Encoding");
|
|
63
|
+
}
|
|
64
|
+
const length = res.headers.get("content-length");
|
|
65
|
+
const small = length !== null && Number(length) < (opts.minBytes ?? 1024);
|
|
66
|
+
const Stream = globalThis.CompressionStream;
|
|
67
|
+
if (small || !Stream || !acceptsGzip(req.headers.get("accept-encoding"))) {
|
|
68
|
+
return new Response(res.body, { status: res.status, statusText: res.statusText, headers });
|
|
69
|
+
}
|
|
70
|
+
headers.set("content-encoding", "gzip");
|
|
71
|
+
headers.delete("content-length");
|
|
72
|
+
const etag = headers.get("etag");
|
|
73
|
+
if (etag && !etag.startsWith("W/")) {
|
|
74
|
+
headers.set("etag", `W/${etag}`);
|
|
75
|
+
}
|
|
76
|
+
const body = res.body.pipeThrough(new Stream("gzip"));
|
|
77
|
+
return new Response(body, { status: res.status, statusText: res.statusText, headers });
|
|
78
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { PageMeta } from "./meta.js";
|
|
2
|
+
import { type CspSources } from "./security.js";
|
|
3
|
+
export interface SecretSource {
|
|
4
|
+
env: string;
|
|
5
|
+
}
|
|
6
|
+
export interface WeftConfig {
|
|
7
|
+
port?: number;
|
|
8
|
+
host?: string;
|
|
9
|
+
secret?: SecretSource;
|
|
10
|
+
siteUrl?: string;
|
|
11
|
+
meta?: PageMeta;
|
|
12
|
+
cookieName?: string;
|
|
13
|
+
htmlAttributes?: (req: Request) => Record<string, string | boolean | null | undefined>;
|
|
14
|
+
database?: string;
|
|
15
|
+
csp?: CspSources;
|
|
16
|
+
edge?: false | {
|
|
17
|
+
maxAge?: number;
|
|
18
|
+
stale?: number;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export declare class ConfigError extends Error {
|
|
22
|
+
constructor(message: string);
|
|
23
|
+
}
|
|
24
|
+
export declare function isSiteUrl(value: string): boolean;
|
|
25
|
+
export declare function parseConfig(raw: unknown): WeftConfig;
|
|
26
|
+
export declare function resolveSecret(config: WeftConfig, env: Record<string, string | undefined>, production: boolean): string | null;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { checkCspSources } from "./security.js";
|
|
2
|
+
export class ConfigError extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "ConfigError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
9
|
+
const HOST = /^[A-Za-z0-9._:-]{1,253}$/;
|
|
10
|
+
const COOKIE = /^[A-Za-z0-9_][A-Za-z0-9_-]{0,63}$/;
|
|
11
|
+
const KEYS = new Set(["port", "host", "secret", "siteUrl", "meta", "cookieName", "database", "htmlAttributes", "csp", "edge"]);
|
|
12
|
+
function fail(message) {
|
|
13
|
+
throw new ConfigError(`weft.config.ts: ${message}`);
|
|
14
|
+
}
|
|
15
|
+
export function isSiteUrl(value) {
|
|
16
|
+
let url;
|
|
17
|
+
try {
|
|
18
|
+
url = new URL(value);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return ((url.protocol === "http:" || url.protocol === "https:") &&
|
|
24
|
+
url.username === "" &&
|
|
25
|
+
url.password === "" &&
|
|
26
|
+
url.search === "" &&
|
|
27
|
+
url.hash === "" &&
|
|
28
|
+
(url.pathname === "/" || url.pathname === ""));
|
|
29
|
+
}
|
|
30
|
+
export function parseConfig(raw) {
|
|
31
|
+
if (raw === undefined || raw === null) {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
35
|
+
fail("the config must be an object");
|
|
36
|
+
}
|
|
37
|
+
const src = raw;
|
|
38
|
+
for (const key of Object.keys(src)) {
|
|
39
|
+
if (!KEYS.has(key)) {
|
|
40
|
+
fail(`unknown option "${key}"`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const out = {};
|
|
44
|
+
if (src.port !== undefined) {
|
|
45
|
+
if (typeof src.port !== "number" || !Number.isInteger(src.port) || src.port < 0 || src.port > 65535) {
|
|
46
|
+
fail("port must be an integer from 0 to 65535");
|
|
47
|
+
}
|
|
48
|
+
out.port = src.port;
|
|
49
|
+
}
|
|
50
|
+
if (src.host !== undefined) {
|
|
51
|
+
if (typeof src.host !== "string" || !HOST.test(src.host)) {
|
|
52
|
+
fail("host must be a hostname or IP address");
|
|
53
|
+
}
|
|
54
|
+
out.host = src.host;
|
|
55
|
+
}
|
|
56
|
+
if (src.secret !== undefined) {
|
|
57
|
+
out.secret = parseSecret(src.secret);
|
|
58
|
+
}
|
|
59
|
+
if (src.siteUrl !== undefined) {
|
|
60
|
+
if (typeof src.siteUrl !== "string" || !isSiteUrl(src.siteUrl)) {
|
|
61
|
+
fail("siteUrl must be an http(s) origin with no path, query, or credentials");
|
|
62
|
+
}
|
|
63
|
+
out.siteUrl = new URL(src.siteUrl).origin;
|
|
64
|
+
}
|
|
65
|
+
if (src.cookieName !== undefined) {
|
|
66
|
+
if (typeof src.cookieName !== "string" || !COOKIE.test(src.cookieName)) {
|
|
67
|
+
fail("cookieName is not a valid cookie name");
|
|
68
|
+
}
|
|
69
|
+
out.cookieName = src.cookieName;
|
|
70
|
+
}
|
|
71
|
+
if (src.htmlAttributes !== undefined) {
|
|
72
|
+
if (typeof src.htmlAttributes !== "function") {
|
|
73
|
+
fail("htmlAttributes must be a function of the request");
|
|
74
|
+
}
|
|
75
|
+
out.htmlAttributes = src.htmlAttributes;
|
|
76
|
+
}
|
|
77
|
+
if (src.meta !== undefined) {
|
|
78
|
+
out.meta = parseMeta(src.meta);
|
|
79
|
+
}
|
|
80
|
+
if (src.database !== undefined) {
|
|
81
|
+
if (typeof src.database !== "string" ||
|
|
82
|
+
src.database.trim() === "" ||
|
|
83
|
+
src.database.length > 1024 ||
|
|
84
|
+
src.database.includes("\0") ||
|
|
85
|
+
/^[a-z][a-z0-9+.-]*:\/\//i.test(src.database)) {
|
|
86
|
+
fail('database must be a file path (relative to the project) or ":memory:"');
|
|
87
|
+
}
|
|
88
|
+
out.database = src.database;
|
|
89
|
+
}
|
|
90
|
+
if (src.edge !== undefined) {
|
|
91
|
+
const e = src.edge;
|
|
92
|
+
const secs = (v) => v === undefined || (Number.isInteger(v) && v >= 0 && v <= 31_536_000);
|
|
93
|
+
if (e !== false && (typeof e !== "object" || e === null || Array.isArray(e) || Object.keys(e).some((k) => k !== "maxAge" && k !== "stale") || !secs(e.maxAge) || !secs(e.stale))) {
|
|
94
|
+
fail("edge must be false or { maxAge, stale } in whole seconds (0 to 31536000)");
|
|
95
|
+
}
|
|
96
|
+
out.edge = e;
|
|
97
|
+
}
|
|
98
|
+
if (src.csp !== undefined) {
|
|
99
|
+
try {
|
|
100
|
+
out.csp = checkCspSources(src.csp, "weft.config.ts");
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
throw new ConfigError(err.message);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
function parseSecret(raw) {
|
|
109
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
110
|
+
fail('secret must be { env: "NAME" }, naming an environment variable');
|
|
111
|
+
}
|
|
112
|
+
const keys = Object.keys(raw);
|
|
113
|
+
const env = raw.env;
|
|
114
|
+
if (keys.length !== 1 || keys[0] !== "env" || typeof env !== "string" || !ENV_NAME.test(env)) {
|
|
115
|
+
fail('secret must be { env: "NAME" }, naming an environment variable');
|
|
116
|
+
}
|
|
117
|
+
return { env };
|
|
118
|
+
}
|
|
119
|
+
export function resolveSecret(config, env, production) {
|
|
120
|
+
if (!config.secret && production && env.WEFT_SECRET) {
|
|
121
|
+
return resolveSecret({ ...config, secret: { env: "WEFT_SECRET" } }, env, production);
|
|
122
|
+
}
|
|
123
|
+
if (config.secret) {
|
|
124
|
+
const value = env[config.secret.env];
|
|
125
|
+
if (typeof value !== "string" || value.length < 32) {
|
|
126
|
+
fail(value === undefined || value === ""
|
|
127
|
+
? `environment variable ${config.secret.env} is not set`
|
|
128
|
+
: `environment variable ${config.secret.env} must be at least 32 characters`);
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
if (production) {
|
|
133
|
+
fail('production requires a session secret: set WEFT_SECRET (32+ characters) or secret: { env: "NAME" }');
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
function parseMeta(raw) {
|
|
138
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
139
|
+
fail("meta must be an object");
|
|
140
|
+
}
|
|
141
|
+
const src = raw;
|
|
142
|
+
const allowed = new Set([
|
|
143
|
+
"title",
|
|
144
|
+
"titleTemplate",
|
|
145
|
+
"description",
|
|
146
|
+
"canonical",
|
|
147
|
+
"robots",
|
|
148
|
+
"lang",
|
|
149
|
+
"themeColor",
|
|
150
|
+
"openGraph",
|
|
151
|
+
"twitter",
|
|
152
|
+
"jsonLd",
|
|
153
|
+
"tags",
|
|
154
|
+
"links",
|
|
155
|
+
]);
|
|
156
|
+
for (const key of Object.keys(src)) {
|
|
157
|
+
if (!allowed.has(key)) {
|
|
158
|
+
fail(`unknown meta field "${key}"`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const meta = {};
|
|
162
|
+
const text = (key, max) => {
|
|
163
|
+
if (src[key] === undefined) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
meta[key] = line(src[key], max, `meta.${key}`);
|
|
167
|
+
};
|
|
168
|
+
text("title", 300);
|
|
169
|
+
text("titleTemplate", 300);
|
|
170
|
+
text("description", 2000);
|
|
171
|
+
text("canonical", 2000);
|
|
172
|
+
text("robots", 200);
|
|
173
|
+
text("lang", 40);
|
|
174
|
+
text("themeColor", 40);
|
|
175
|
+
if (src.openGraph !== undefined) {
|
|
176
|
+
meta.openGraph = parseOpenGraph(src.openGraph);
|
|
177
|
+
}
|
|
178
|
+
if (src.twitter !== undefined) {
|
|
179
|
+
meta.twitter = parseTwitter(src.twitter);
|
|
180
|
+
}
|
|
181
|
+
if (src.jsonLd !== undefined) {
|
|
182
|
+
const json = JSON.stringify(plainJson(src.jsonLd, "meta.jsonLd", 0));
|
|
183
|
+
if (json.length > 16_000) {
|
|
184
|
+
fail("meta.jsonLd is too large");
|
|
185
|
+
}
|
|
186
|
+
meta.jsonLd = JSON.parse(json);
|
|
187
|
+
}
|
|
188
|
+
if (src.tags !== undefined) {
|
|
189
|
+
meta.tags = parseTags(src.tags);
|
|
190
|
+
}
|
|
191
|
+
if (src.links !== undefined) {
|
|
192
|
+
meta.links = parseLinks(src.links);
|
|
193
|
+
}
|
|
194
|
+
return meta;
|
|
195
|
+
}
|
|
196
|
+
function line(value, max, field) {
|
|
197
|
+
if (typeof value !== "string") {
|
|
198
|
+
fail(`${field} must be a string`);
|
|
199
|
+
}
|
|
200
|
+
if (value.length > max || /[\u0000-\u001f\u007f]/.test(value)) {
|
|
201
|
+
fail(`${field} contains control characters or is too long`);
|
|
202
|
+
}
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
function parseOpenGraph(raw) {
|
|
206
|
+
return pickStrings(raw, "meta.openGraph", ["title", "description", "type", "url", "image", "imageAlt", "siteName", "locale"], 2000);
|
|
207
|
+
}
|
|
208
|
+
function parseTwitter(raw) {
|
|
209
|
+
const card = pickStrings(raw, "meta.twitter", ["card", "site", "creator", "title", "description", "image"], 2000);
|
|
210
|
+
if (card.card !== undefined && !["summary", "summary_large_image", "app", "player"].includes(card.card)) {
|
|
211
|
+
fail('meta.twitter.card must be "summary", "summary_large_image", "app", or "player"');
|
|
212
|
+
}
|
|
213
|
+
return card;
|
|
214
|
+
}
|
|
215
|
+
function pickStrings(raw, field, keys, max) {
|
|
216
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
217
|
+
fail(`${field} must be an object`);
|
|
218
|
+
}
|
|
219
|
+
const src = raw;
|
|
220
|
+
const allowed = new Set(keys);
|
|
221
|
+
for (const key of Object.keys(src)) {
|
|
222
|
+
if (!allowed.has(key)) {
|
|
223
|
+
fail(`unknown ${field} field "${key}"`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
const out = {};
|
|
227
|
+
for (const key of keys) {
|
|
228
|
+
if (src[key] !== undefined) {
|
|
229
|
+
out[key] = line(src[key], max, `${field}.${key}`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return out;
|
|
233
|
+
}
|
|
234
|
+
function parseTags(raw) {
|
|
235
|
+
if (!Array.isArray(raw) || raw.length > 20) {
|
|
236
|
+
fail("meta.tags must be an array of at most 20 tags");
|
|
237
|
+
}
|
|
238
|
+
return raw.map((item, i) => {
|
|
239
|
+
if (typeof item !== "object" || item === null || Array.isArray(item)) {
|
|
240
|
+
fail(`meta.tags[${i}] must be an object`);
|
|
241
|
+
}
|
|
242
|
+
const src = item;
|
|
243
|
+
const content = line(src.content, 2000, `meta.tags[${i}].content`);
|
|
244
|
+
const name = src.name === undefined ? undefined : line(src.name, 200, `meta.tags[${i}].name`);
|
|
245
|
+
const property = src.property === undefined ? undefined : line(src.property, 200, `meta.tags[${i}].property`);
|
|
246
|
+
const httpEquiv = src.httpEquiv === undefined ? undefined : line(src.httpEquiv, 200, `meta.tags[${i}].httpEquiv`);
|
|
247
|
+
const which = [name, property, httpEquiv].filter((v) => v !== undefined);
|
|
248
|
+
if (which.length !== 1) {
|
|
249
|
+
fail(`meta.tags[${i}] needs exactly one of name, property, httpEquiv`);
|
|
250
|
+
}
|
|
251
|
+
return { content, name, property, httpEquiv };
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
function parseLinks(raw) {
|
|
255
|
+
if (!Array.isArray(raw) || raw.length > 20) {
|
|
256
|
+
fail("meta.links must be an array of at most 20 links");
|
|
257
|
+
}
|
|
258
|
+
return raw.map((item, i) => {
|
|
259
|
+
if (typeof item !== "object" || item === null || Array.isArray(item)) {
|
|
260
|
+
fail(`meta.links[${i}] must be an object`);
|
|
261
|
+
}
|
|
262
|
+
const src = item;
|
|
263
|
+
const link = {
|
|
264
|
+
rel: line(src.rel, 200, `meta.links[${i}].rel`),
|
|
265
|
+
href: line(src.href, 2000, `meta.links[${i}].href`),
|
|
266
|
+
};
|
|
267
|
+
for (const key of ["type", "sizes", "hreflang", "media"]) {
|
|
268
|
+
if (src[key] !== undefined) {
|
|
269
|
+
link[key] = line(src[key], 200, `meta.links[${i}].${key}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return link;
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
function plainJson(value, field, depth) {
|
|
276
|
+
if (depth > 8) {
|
|
277
|
+
fail(`${field} is too deep`);
|
|
278
|
+
}
|
|
279
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
280
|
+
if (typeof value === "string" && value.length > 4000) {
|
|
281
|
+
fail(`${field} is too large`);
|
|
282
|
+
}
|
|
283
|
+
return value;
|
|
284
|
+
}
|
|
285
|
+
if (typeof value === "number") {
|
|
286
|
+
if (!Number.isFinite(value)) {
|
|
287
|
+
fail(`${field} must be finite numbers, strings, booleans, arrays, and plain objects`);
|
|
288
|
+
}
|
|
289
|
+
return value;
|
|
290
|
+
}
|
|
291
|
+
if (Array.isArray(value)) {
|
|
292
|
+
if (value.length > 40) {
|
|
293
|
+
fail(`${field} is too large`);
|
|
294
|
+
}
|
|
295
|
+
return value.map((item) => plainJson(item, field, depth + 1));
|
|
296
|
+
}
|
|
297
|
+
if (typeof value === "object") {
|
|
298
|
+
const proto = Object.getPrototypeOf(value);
|
|
299
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
300
|
+
fail(`${field} must be plain data`);
|
|
301
|
+
}
|
|
302
|
+
const src = value;
|
|
303
|
+
const keys = Object.keys(src);
|
|
304
|
+
if (keys.length > 40) {
|
|
305
|
+
fail(`${field} is too large`);
|
|
306
|
+
}
|
|
307
|
+
const out = {};
|
|
308
|
+
for (const key of keys) {
|
|
309
|
+
if (key === "__proto__" || key === "constructor" || key === "prototype") {
|
|
310
|
+
fail(`${field} has an invalid key`);
|
|
311
|
+
}
|
|
312
|
+
out[key] = plainJson(src[key], field, depth + 1);
|
|
313
|
+
}
|
|
314
|
+
return out;
|
|
315
|
+
}
|
|
316
|
+
fail(`${field} must be plain data`);
|
|
317
|
+
}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Model } from "@weft-framework/data";
|
|
2
|
+
import type { Action, ParamSpec } from "./action.js";
|
|
3
|
+
import type { ApiHandlers } from "./api.js";
|
|
4
|
+
export interface ContractSource {
|
|
5
|
+
actions?: Record<string, Action>;
|
|
6
|
+
api?: Record<string, ApiHandlers>;
|
|
7
|
+
models?: Record<string, Model<unknown>>;
|
|
8
|
+
}
|
|
9
|
+
export interface ContractInfo {
|
|
10
|
+
title: string;
|
|
11
|
+
version: string;
|
|
12
|
+
description?: string;
|
|
13
|
+
}
|
|
14
|
+
type Schema = Record<string, unknown>;
|
|
15
|
+
export declare function actionSchema(params: readonly ParamSpec[]): Schema;
|
|
16
|
+
export declare function modelSchema(model: Model<unknown>): Schema;
|
|
17
|
+
export declare function openApi(src: ContractSource, info: ContractInfo): Record<string, unknown>;
|
|
18
|
+
export {};
|