@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,119 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { createApp } from "./app.js";
|
|
5
|
+
import { BUILD_FORMAT } from "./build.js";
|
|
6
|
+
import { ConfigError, resolveSecret } from "./config.js";
|
|
7
|
+
import { loadConfig } from "./load-config.js";
|
|
8
|
+
import { serveBun } from "./bun.js";
|
|
9
|
+
import { serveNode } from "./node.js";
|
|
10
|
+
import { ProjectError, loadProject } from "./project.js";
|
|
11
|
+
import { loadEnvFile } from "./env.js";
|
|
12
|
+
import { ModelStorage } from "./storage.js";
|
|
13
|
+
export class ProductionError extends Error {
|
|
14
|
+
}
|
|
15
|
+
function readManifest(dist) {
|
|
16
|
+
const file = join(dist, "manifest.json");
|
|
17
|
+
if (!existsSync(file)) {
|
|
18
|
+
throw new ProductionError(`${dist}: no manifest.json; run \`weft build\``);
|
|
19
|
+
}
|
|
20
|
+
const m = JSON.parse(readFileSync(file, "utf8"));
|
|
21
|
+
if (m.weft !== BUILD_FORMAT) {
|
|
22
|
+
throw new ProductionError(`${dist}: built by an incompatible Weft (format ${String(m.weft)}); rebuild`);
|
|
23
|
+
}
|
|
24
|
+
return m;
|
|
25
|
+
}
|
|
26
|
+
function readAssets(dist, manifest) {
|
|
27
|
+
const out = {};
|
|
28
|
+
for (const path of Object.keys(manifest.assets)) {
|
|
29
|
+
const parts = path.split("/").filter((p) => p !== "").map(decodeURI);
|
|
30
|
+
out[path] = readFileSync(join(dist, "client", ...parts), "utf8");
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
function port(value, fallback) {
|
|
35
|
+
if (value === undefined || value === "") {
|
|
36
|
+
return fallback;
|
|
37
|
+
}
|
|
38
|
+
const n = Number(value);
|
|
39
|
+
if (!Number.isInteger(n) || n < 0 || n > 65535) {
|
|
40
|
+
throw new ProductionError(`PORT must be an integer from 0 to 65535, not ${JSON.stringify(value)}`);
|
|
41
|
+
}
|
|
42
|
+
return n;
|
|
43
|
+
}
|
|
44
|
+
function ms(value, name, fallback) {
|
|
45
|
+
if (value === undefined || value === "") {
|
|
46
|
+
return fallback;
|
|
47
|
+
}
|
|
48
|
+
const n = Number(value);
|
|
49
|
+
if (!Number.isInteger(n) || n < 0 || n > 600_000) {
|
|
50
|
+
throw new ProductionError(`${name} must be a whole number of milliseconds from 0 to 600000, not ${JSON.stringify(value)}`);
|
|
51
|
+
}
|
|
52
|
+
return n;
|
|
53
|
+
}
|
|
54
|
+
export async function startProduction(distUrl, opts = {}) {
|
|
55
|
+
const env = opts.env ?? process.env;
|
|
56
|
+
const dist = resolve(typeof distUrl === "string" ? distUrl : fileURLToPath(distUrl));
|
|
57
|
+
const root = dirname(dist);
|
|
58
|
+
loadEnvFile(join(root, ".env"), env);
|
|
59
|
+
const manifest = readManifest(dist);
|
|
60
|
+
const config = await loadConfig(root);
|
|
61
|
+
const secret = resolveSecret(config, env, true);
|
|
62
|
+
const project = await loadProject(root, { outDir: join(dist, "server"), browserAssets: false });
|
|
63
|
+
const storage = new ModelStorage();
|
|
64
|
+
await storage.attach(Object.values(project.models), { database: project.database });
|
|
65
|
+
const bun = globalThis.Bun;
|
|
66
|
+
const listen = opts.listen ?? true;
|
|
67
|
+
const { models: _models, database: _database, emails: _emails, ...appOptions } = project;
|
|
68
|
+
const app = createApp({
|
|
69
|
+
...appOptions,
|
|
70
|
+
secret,
|
|
71
|
+
assets: readAssets(dist, manifest),
|
|
72
|
+
clientEntry: manifest.clientEntry,
|
|
73
|
+
immutableAssets: true,
|
|
74
|
+
transports: bun && listen ? ["ws", "sse"] : ["sse"],
|
|
75
|
+
});
|
|
76
|
+
const drain = { delayMs: ms(env.WEFT_DRAIN_DELAY_MS, "WEFT_DRAIN_DELAY_MS", 0), timeoutMs: ms(env.WEFT_DRAIN_TIMEOUT_MS, "WEFT_DRAIN_TIMEOUT_MS", 10_000) };
|
|
77
|
+
const shutdown = async (stop) => {
|
|
78
|
+
await app.drain(drain);
|
|
79
|
+
await stop();
|
|
80
|
+
app.close();
|
|
81
|
+
storage.close();
|
|
82
|
+
};
|
|
83
|
+
if (!listen) {
|
|
84
|
+
return { app, manifest, close: () => shutdown(() => undefined) };
|
|
85
|
+
}
|
|
86
|
+
const p = port(env.PORT, config.port ?? 3000);
|
|
87
|
+
const host = env.HOST || config.host || "127.0.0.1";
|
|
88
|
+
let server;
|
|
89
|
+
if (bun) {
|
|
90
|
+
const s = serveBun(app, { port: p, hostname: host, trustProxy: env.WEFT_TRUST_PROXY === "1" }, bun);
|
|
91
|
+
server = { app, manifest, url: s.url, close: () => shutdown(() => s.close()) };
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const s = await serveNode(app, { port: p, host, trustProxy: env.WEFT_TRUST_PROXY === "1" });
|
|
95
|
+
server = { app, manifest, url: s.url, close: () => shutdown(() => s.close()) };
|
|
96
|
+
}
|
|
97
|
+
let closing = false;
|
|
98
|
+
for (const signal of ["SIGINT", "SIGTERM"]) {
|
|
99
|
+
process.once(signal, () => {
|
|
100
|
+
if (closing) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
closing = true;
|
|
104
|
+
void server.close().then(() => process.exit(0));
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
console.log(`[weft] build ${manifest.buildId} serving at ${server.url}`);
|
|
108
|
+
return server;
|
|
109
|
+
}
|
|
110
|
+
export async function runProduction(distUrl) {
|
|
111
|
+
try {
|
|
112
|
+
await startProduction(distUrl);
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
const known = err instanceof ConfigError || err instanceof ProjectError || err instanceof ProductionError;
|
|
116
|
+
console.error(known ? `[weft] ${err.message}` : err);
|
|
117
|
+
process.exit(1);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Model } from "@weft-framework/data";
|
|
2
|
+
import type { AppOptions } from "./app.js";
|
|
3
|
+
import { type Compiler } from "./compiler.js";
|
|
4
|
+
import { type PublicFilesOptions } from "./files.js";
|
|
5
|
+
import type { EmailTemplate } from "./mail.js";
|
|
6
|
+
export interface ProjectOptions {
|
|
7
|
+
compiler?: Compiler;
|
|
8
|
+
outDir: string;
|
|
9
|
+
publicFiles?: PublicFilesOptions;
|
|
10
|
+
browserAssets?: boolean;
|
|
11
|
+
production?: boolean;
|
|
12
|
+
compileStyles?: boolean;
|
|
13
|
+
onWarn?: (message: string) => void;
|
|
14
|
+
cacheKey?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface ProjectApp extends Pick<AppOptions, "routes" | "notFound" | "error" | "shell" | "files" | "assets" | "actions" | "meta" | "siteUrl" | "cookieName" | "htmlAttributes" | "csp" | "edge" | "middleware" | "ready" | "sharedFields" | "offline" | "pdf" | "images" | "sitemap" | "robots" | "auth" | "uploads" | "api" | "stylesheet" | "componentStyles"> {
|
|
17
|
+
models: Record<string, Model<unknown>>;
|
|
18
|
+
emails: Record<string, EmailTemplate>;
|
|
19
|
+
database?: string;
|
|
20
|
+
}
|
|
21
|
+
export declare class ProjectError extends Error {
|
|
22
|
+
}
|
|
23
|
+
export declare function loadProject(root: string, opts: ProjectOptions): Promise<ProjectApp>;
|
package/dist/project.js
ADDED
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { join, relative, resolve, sep } from "node:path";
|
|
11
|
+
import { pathToFileURL } from "node:url";
|
|
12
|
+
import { componentStyles, styleMark } from "@weft-framework/runtime";
|
|
13
|
+
import { Model } from "@weft-framework/data";
|
|
14
|
+
import { isAction } from "./action.js";
|
|
15
|
+
import { checkOg } from "./app.js";
|
|
16
|
+
import { islandAssets, runtimeAssets } from "./assets.js";
|
|
17
|
+
import { compileTree } from "./compiler.js";
|
|
18
|
+
import { projectStyles, StyleError } from "./tailwind.js";
|
|
19
|
+
import { ConfigError } from "./config.js";
|
|
20
|
+
import { publicFiles } from "./files.js";
|
|
21
|
+
import { loadConfig } from "./load-config.js";
|
|
22
|
+
import { patternForFile } from "./router.js";
|
|
23
|
+
import { checkCspSources } from "./security.js";
|
|
24
|
+
import { checkPdfRoute } from "./pdf.js";
|
|
25
|
+
export class ProjectError extends Error {
|
|
26
|
+
}
|
|
27
|
+
const API_METHODS = ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"];
|
|
28
|
+
function isComponent(v) {
|
|
29
|
+
return typeof v === "function" && "def" in v;
|
|
30
|
+
}
|
|
31
|
+
function isModel(v) {
|
|
32
|
+
return v instanceof Model;
|
|
33
|
+
}
|
|
34
|
+
function pickComponent(mod, preferred, file) {
|
|
35
|
+
if (isComponent(mod.default)) {
|
|
36
|
+
return mod.default;
|
|
37
|
+
}
|
|
38
|
+
if (isComponent(mod[preferred])) {
|
|
39
|
+
return mod[preferred];
|
|
40
|
+
}
|
|
41
|
+
const found = Object.values(mod).filter(isComponent);
|
|
42
|
+
if (found.length === 1) {
|
|
43
|
+
return found[0];
|
|
44
|
+
}
|
|
45
|
+
throw new ProjectError(found.length === 0
|
|
46
|
+
? `${file}: no component exported`
|
|
47
|
+
: `${file}: ${found.length} components; name the one to use \`${preferred}\``);
|
|
48
|
+
}
|
|
49
|
+
function checkExports(mod, file, allowLoad) {
|
|
50
|
+
if (mod.meta !== undefined && typeof mod.meta !== "function" && (typeof mod.meta !== "object" || mod.meta === null)) {
|
|
51
|
+
throw new ProjectError(`${file}: \`meta\` must be an object or a function`);
|
|
52
|
+
}
|
|
53
|
+
if (mod.csp !== undefined) {
|
|
54
|
+
if (!allowLoad)
|
|
55
|
+
throw new ProjectError(`${file}: layouts cannot export \`csp\`; export it from each page that needs it`);
|
|
56
|
+
try {
|
|
57
|
+
checkCspSources(mod.csp, file);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
throw new ProjectError(err.message);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (mod.og !== undefined) {
|
|
64
|
+
if (!allowLoad)
|
|
65
|
+
throw new ProjectError(`${file}: layouts cannot export \`og\``);
|
|
66
|
+
try {
|
|
67
|
+
checkOg(mod.og, file);
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
throw new ProjectError(err.message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (mod.edge !== undefined && (!allowLoad || typeof mod.edge !== "boolean")) {
|
|
74
|
+
throw new ProjectError(allowLoad ? `${file}: export \`edge = true\` or \`false\`` : `${file}: layouts cannot export \`edge\``);
|
|
75
|
+
}
|
|
76
|
+
if (mod.pdf !== undefined) {
|
|
77
|
+
if (!allowLoad)
|
|
78
|
+
throw new ProjectError(`${file}: layouts cannot export \`pdf\``);
|
|
79
|
+
try {
|
|
80
|
+
checkPdfRoute(mod.pdf, file);
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
throw new ProjectError(err.message);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (mod.cache !== undefined && (!allowLoad || mod.cache !== "etag")) {
|
|
87
|
+
throw new ProjectError(allowLoad ? `${file}: export \`cache = "etag"\` or nothing` : `${file}: layouts cannot export \`cache\``);
|
|
88
|
+
}
|
|
89
|
+
if (mod.load !== undefined && (!allowLoad || typeof mod.load !== "function")) {
|
|
90
|
+
throw new ProjectError(allowLoad ? `${file}: \`load\` must be a function` : `${file}: layouts cannot export \`load\``);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function listModules(dir) {
|
|
94
|
+
const out = [];
|
|
95
|
+
const walk = (d) => {
|
|
96
|
+
for (const name of readdirSync(d).sort()) {
|
|
97
|
+
const full = join(d, name);
|
|
98
|
+
if (statSync(full).isDirectory()) {
|
|
99
|
+
walk(full);
|
|
100
|
+
}
|
|
101
|
+
else if (name.endsWith(".ts")) {
|
|
102
|
+
out.push(relative(dir, full).split(sep).join("/"));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
if (existsSync(dir)) {
|
|
107
|
+
walk(dir);
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
export async function loadProject(root, opts) {
|
|
112
|
+
const src = join(root, "src");
|
|
113
|
+
if (opts.compiler) {
|
|
114
|
+
if (!existsSync(join(src, "pages"))) {
|
|
115
|
+
throw new ProjectError(`${root}: no src/pages directory`);
|
|
116
|
+
}
|
|
117
|
+
compileTree(opts.compiler, src, opts.outDir);
|
|
118
|
+
}
|
|
119
|
+
else if (!existsSync(join(opts.outDir, "pages"))) {
|
|
120
|
+
throw new ProjectError(`${opts.outDir}: not a compiled project (no pages/); run \`weft build\``);
|
|
121
|
+
}
|
|
122
|
+
const pagesOut = join(opts.outDir, "pages");
|
|
123
|
+
const isClient = (f) => f.endsWith(".client.ts");
|
|
124
|
+
const files = listModules(pagesOut).filter((f) => !isClient(f));
|
|
125
|
+
const clientModules = {};
|
|
126
|
+
for (const rel of listModules(opts.outDir).filter(isClient)) {
|
|
127
|
+
clientModules[rel] = readFileSync(join(opts.outDir, rel), "utf8");
|
|
128
|
+
}
|
|
129
|
+
let assets = {};
|
|
130
|
+
if (opts.browserAssets !== false) {
|
|
131
|
+
try {
|
|
132
|
+
assets = {
|
|
133
|
+
...runtimeAssets(),
|
|
134
|
+
...islandAssets(clientModules, {
|
|
135
|
+
root: src,
|
|
136
|
+
mode: opts.production ? "production" : "development",
|
|
137
|
+
onWarn: opts.onWarn,
|
|
138
|
+
}),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
throw new ProjectError(err.message);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const mark = styleMark();
|
|
146
|
+
const load = async (rel) => (await import(__rewriteRelativeImportExtension(pathToFileURL(join(pagesOut, rel)).href)));
|
|
147
|
+
const shown = (rel) => `src/pages/${rel.replace(/\.ts$/, "")}`;
|
|
148
|
+
const layouts = new Map();
|
|
149
|
+
for (const rel of files.filter((f) => f === "_layout.ts" || f.endsWith("/_layout.ts"))) {
|
|
150
|
+
const mod = await load(rel);
|
|
151
|
+
checkExports(mod, shown(rel), false);
|
|
152
|
+
layouts.set(rel.slice(0, -"_layout.ts".length).replace(/\/$/, ""), {
|
|
153
|
+
component: pickComponent(mod, "Layout", shown(rel)),
|
|
154
|
+
meta: mod.meta,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const chain = (rel) => {
|
|
158
|
+
const dirs = rel.split("/").slice(0, -1);
|
|
159
|
+
const out = [];
|
|
160
|
+
for (let i = 0; i <= dirs.length; i++) {
|
|
161
|
+
const l = layouts.get(dirs.slice(0, i).join("/"));
|
|
162
|
+
if (l) {
|
|
163
|
+
out.push(l);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return out;
|
|
167
|
+
};
|
|
168
|
+
const route = async (rel) => {
|
|
169
|
+
const mod = await load(rel);
|
|
170
|
+
checkExports(mod, shown(rel), true);
|
|
171
|
+
return {
|
|
172
|
+
component: pickComponent(mod, "Page", shown(rel)),
|
|
173
|
+
load: mod.load,
|
|
174
|
+
meta: mod.meta,
|
|
175
|
+
...(mod.cache === "etag" ? { cache: "etag" } : {}),
|
|
176
|
+
...(mod.csp !== undefined ? { csp: mod.csp } : {}),
|
|
177
|
+
...(mod.pdf !== undefined ? { pdf: mod.pdf } : {}),
|
|
178
|
+
...(typeof mod.edge === "boolean" ? { edge: mod.edge } : {}),
|
|
179
|
+
...(mod.og !== undefined ? { og: mod.og } : {}),
|
|
180
|
+
layouts: chain(rel),
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
const routes = {};
|
|
184
|
+
const owner = {};
|
|
185
|
+
for (const rel of files) {
|
|
186
|
+
if (rel.split("/").some((part) => part.startsWith("_")) || rel.startsWith("api/")) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const pattern = patternForFile(rel);
|
|
190
|
+
if (owner[pattern]) {
|
|
191
|
+
throw new ProjectError(`${shown(rel)} and ${shown(owner[pattern])} both serve ${pattern}`);
|
|
192
|
+
}
|
|
193
|
+
owner[pattern] = rel;
|
|
194
|
+
routes[pattern] = await route(rel);
|
|
195
|
+
}
|
|
196
|
+
const actions = {};
|
|
197
|
+
const actionFrom = {};
|
|
198
|
+
for (const rel of listModules(opts.outDir).filter((f) => !isClient(f))) {
|
|
199
|
+
const full = join(opts.outDir, rel);
|
|
200
|
+
if (!readFileSync(full, "utf8").includes("defineAction(")) {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(full).href)));
|
|
204
|
+
for (const v of Object.values(mod)) {
|
|
205
|
+
if (!isAction(v)) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const prev = actions[v.name];
|
|
209
|
+
if (prev && prev !== v) {
|
|
210
|
+
throw new ProjectError(`action ${v.name} is defined in both src/${actionFrom[v.name]} and src/${rel}`);
|
|
211
|
+
}
|
|
212
|
+
actions[v.name] = v;
|
|
213
|
+
actionFrom[v.name] ??= rel;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const api = {};
|
|
217
|
+
for (const rel of files.filter((f) => f.startsWith("api/"))) {
|
|
218
|
+
const pattern = patternForFile(rel);
|
|
219
|
+
if (owner[pattern]) {
|
|
220
|
+
throw new ProjectError(`${shown(rel)} and ${shown(owner[pattern])} both serve ${pattern}`);
|
|
221
|
+
}
|
|
222
|
+
owner[pattern] = rel;
|
|
223
|
+
const mod = await load(rel);
|
|
224
|
+
api[pattern] = apiHandlers(mod, shown(rel));
|
|
225
|
+
}
|
|
226
|
+
const models = {};
|
|
227
|
+
const modelFrom = {};
|
|
228
|
+
for (const rel of listModules(join(opts.outDir, "models")).filter((f) => !isClient(f))) {
|
|
229
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(join(opts.outDir, "models", rel)).href)));
|
|
230
|
+
let found = 0;
|
|
231
|
+
for (const value of Object.values(mod)) {
|
|
232
|
+
if (!isModel(value)) {
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
found += 1;
|
|
236
|
+
const prev = modelFrom[value.name];
|
|
237
|
+
if (prev) {
|
|
238
|
+
throw new ProjectError(`model ${value.name} is defined in both src/models/${prev} and src/models/${rel}`);
|
|
239
|
+
}
|
|
240
|
+
models[value.name] = value;
|
|
241
|
+
modelFrom[value.name] = rel;
|
|
242
|
+
}
|
|
243
|
+
if (found === 0) {
|
|
244
|
+
throw new ProjectError(`src/models/${rel.replace(/\.ts$/, "")}: no model exported`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const emails = {};
|
|
248
|
+
for (const rel of listModules(join(opts.outDir, "emails")).filter((f) => !isClient(f))) {
|
|
249
|
+
const name = rel.replace(/\.ts$/, "");
|
|
250
|
+
const file = `src/emails/${name}`;
|
|
251
|
+
if (!/^[A-Za-z0-9_/-]{1,200}$/.test(name))
|
|
252
|
+
throw new ProjectError(`${file}: email template names use letters, digits, _, - and /`);
|
|
253
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(join(opts.outDir, "emails", rel)).href)));
|
|
254
|
+
const component = pickComponent(mod, "Email", file);
|
|
255
|
+
if (mod.subject !== undefined && typeof mod.subject !== "string" && typeof mod.subject !== "function") {
|
|
256
|
+
throw new ProjectError(`${file}: \`subject\` is a string or a function of the props`);
|
|
257
|
+
}
|
|
258
|
+
const isProps = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
259
|
+
if (mod.preview !== undefined && !isProps(mod.preview))
|
|
260
|
+
throw new ProjectError(`${file}: \`preview\` is sample props (an object)`);
|
|
261
|
+
if (mod.previews !== undefined && (!isProps(mod.previews) || !Object.values(mod.previews).every(isProps))) {
|
|
262
|
+
throw new ProjectError(`${file}: \`previews\` maps sample names to props`);
|
|
263
|
+
}
|
|
264
|
+
const previews = { ...(mod.preview ? { default: mod.preview } : {}) };
|
|
265
|
+
for (const [k, v] of Object.entries((mod.previews ?? {}))) {
|
|
266
|
+
if (!/^[A-Za-z0-9_-]{1,64}$/.test(k))
|
|
267
|
+
throw new ProjectError(`${file}: sample name "${k}" uses letters, digits, _ and -`);
|
|
268
|
+
previews[k] = v;
|
|
269
|
+
}
|
|
270
|
+
emails[name] = {
|
|
271
|
+
component,
|
|
272
|
+
...(mod.subject !== undefined ? { subject: mod.subject } : {}),
|
|
273
|
+
previews,
|
|
274
|
+
file,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
let config;
|
|
278
|
+
try {
|
|
279
|
+
config = await loadConfig(root, opts.cacheKey);
|
|
280
|
+
}
|
|
281
|
+
catch (err) {
|
|
282
|
+
if (err instanceof ConfigError) {
|
|
283
|
+
throw new ProjectError(err.message);
|
|
284
|
+
}
|
|
285
|
+
throw err;
|
|
286
|
+
}
|
|
287
|
+
const middlewareFile = ["middleware.ts", "middleware.js"].map((name) => join(opts.outDir, name)).find((file) => existsSync(file));
|
|
288
|
+
let middleware;
|
|
289
|
+
if (middlewareFile) {
|
|
290
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(middlewareFile).href)));
|
|
291
|
+
const exported = mod.middleware ?? mod.default;
|
|
292
|
+
if (typeof exported === "function") {
|
|
293
|
+
middleware = exported;
|
|
294
|
+
}
|
|
295
|
+
else if (Array.isArray(exported) && exported.every((item) => typeof item === "function")) {
|
|
296
|
+
middleware = exported;
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
throw new ProjectError("src/middleware: export a function or an array of functions");
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const readyFile = ["ready.ts", "ready.js"].map((name) => join(opts.outDir, name)).find((file) => existsSync(file));
|
|
303
|
+
let ready;
|
|
304
|
+
if (readyFile) {
|
|
305
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(readyFile).href)));
|
|
306
|
+
const exported = mod.ready ?? mod.default;
|
|
307
|
+
if (!exported ||
|
|
308
|
+
typeof exported !== "object" ||
|
|
309
|
+
Array.isArray(exported) ||
|
|
310
|
+
!Object.values(exported).every((check) => typeof check === "function")) {
|
|
311
|
+
throw new ProjectError("src/ready: export `ready`, an object of check functions by name");
|
|
312
|
+
}
|
|
313
|
+
ready = exported;
|
|
314
|
+
}
|
|
315
|
+
const sharedFile = ["shared.ts", "shared.js"].map((name) => join(opts.outDir, name)).find((file) => existsSync(file));
|
|
316
|
+
let sharedFields;
|
|
317
|
+
if (sharedFile) {
|
|
318
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(sharedFile).href)));
|
|
319
|
+
const { label, saveMs, maxLength } = mod;
|
|
320
|
+
const count = (v) => v === undefined || (typeof v === "number" && Number.isInteger(v) && v > 0);
|
|
321
|
+
if ((label !== undefined && typeof label !== "function") || !count(saveMs) || !count(maxLength)) {
|
|
322
|
+
throw new ProjectError("src/shared: export `label(actor)` (a function), `saveMs` and `maxLength` (positive whole numbers)");
|
|
323
|
+
}
|
|
324
|
+
sharedFields = {
|
|
325
|
+
...(label ? { label: label } : {}),
|
|
326
|
+
...(saveMs !== undefined ? { saveMs: saveMs } : {}),
|
|
327
|
+
...(maxLength !== undefined ? { maxLength: maxLength } : {}),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
const pdfFile = ["pdf.ts", "pdf.js"].map((name) => join(opts.outDir, name)).find((file) => existsSync(file));
|
|
331
|
+
let pdf;
|
|
332
|
+
if (pdfFile) {
|
|
333
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(pdfFile).href)));
|
|
334
|
+
const p = mod.pdf;
|
|
335
|
+
if (!p || typeof p.print !== "function" || typeof p.close !== "function") {
|
|
336
|
+
throw new ProjectError("src/pdf: export `pdf = definePdf()`");
|
|
337
|
+
}
|
|
338
|
+
pdf = p;
|
|
339
|
+
}
|
|
340
|
+
const imagesFile = ["images.ts", "images.js"].map((name) => join(opts.outDir, name)).find((file) => existsSync(file));
|
|
341
|
+
let images;
|
|
342
|
+
if (imagesFile) {
|
|
343
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(imagesFile).href)));
|
|
344
|
+
const i = mod.images;
|
|
345
|
+
if (!i || typeof i.handle !== "function" || typeof i.srcset !== "function") {
|
|
346
|
+
throw new ProjectError("src/images: export `images = defineImages()`");
|
|
347
|
+
}
|
|
348
|
+
images = i;
|
|
349
|
+
}
|
|
350
|
+
const liveFile = ["live.ts", "live.js"].map((name) => join(opts.outDir, name)).find((file) => existsSync(file));
|
|
351
|
+
let offline;
|
|
352
|
+
if (liveFile) {
|
|
353
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(liveFile).href)));
|
|
354
|
+
const o = mod.offline;
|
|
355
|
+
const keys = o && typeof o === "object" ? Object.keys(o) : [];
|
|
356
|
+
if (o !== undefined && (typeof o !== "object" || o === null || keys.some((k) => k !== "stale") ||
|
|
357
|
+
(o.stale !== undefined && o.stale !== "run" && o.stale !== "refuse"))) {
|
|
358
|
+
throw new ProjectError("src/live: export `offline = { stale: \"run\" | \"refuse\" }`");
|
|
359
|
+
}
|
|
360
|
+
if (o?.stale)
|
|
361
|
+
offline = { stale: o.stale };
|
|
362
|
+
}
|
|
363
|
+
const seoFile = ["seo.ts", "seo.js"].map((name) => join(opts.outDir, name)).find((file) => existsSync(file));
|
|
364
|
+
let seo = {};
|
|
365
|
+
if (seoFile) {
|
|
366
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(seoFile).href)));
|
|
367
|
+
for (const key of ["sitemap", "robots"]) {
|
|
368
|
+
const v = mod[key];
|
|
369
|
+
if (v !== undefined && (v === null || typeof v !== "object" || Array.isArray(v))) {
|
|
370
|
+
throw new ProjectError(`src/seo: \`${key}\` is an options object`);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (mod.sitemap !== undefined && !config.siteUrl) {
|
|
374
|
+
throw new ProjectError("src/seo: a sitemap needs `siteUrl` in weft.config.ts");
|
|
375
|
+
}
|
|
376
|
+
seo = {
|
|
377
|
+
...(mod.sitemap ? { sitemap: mod.sitemap } : {}),
|
|
378
|
+
...(mod.robots ? { robots: mod.robots } : {}),
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
const uploadsFile = ["uploads.ts", "uploads.js"].map((name) => join(opts.outDir, name)).find((file) => existsSync(file));
|
|
382
|
+
let uploads;
|
|
383
|
+
if (uploadsFile) {
|
|
384
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(uploadsFile).href)));
|
|
385
|
+
const u = mod.uploads;
|
|
386
|
+
if (typeof u?.handle !== "function" || typeof u.serve !== "function") {
|
|
387
|
+
throw new ProjectError("src/uploads: export `uploads`, a `defineUploads({...})`");
|
|
388
|
+
}
|
|
389
|
+
uploads = u;
|
|
390
|
+
}
|
|
391
|
+
const authFile = ["auth.ts", "auth.js"].map((name) => join(opts.outDir, name)).find((file) => existsSync(file));
|
|
392
|
+
let auth;
|
|
393
|
+
if (authFile) {
|
|
394
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(authFile).href)));
|
|
395
|
+
const hasProviders = mod.providers !== undefined;
|
|
396
|
+
const hasEmail = mod.email !== undefined;
|
|
397
|
+
if (typeof mod.user !== "function" && !hasProviders && !hasEmail) {
|
|
398
|
+
throw new ProjectError("src/auth: export a function `user(id)` returning the actor for a user id");
|
|
399
|
+
}
|
|
400
|
+
if (mod.user !== undefined && typeof mod.user !== "function") {
|
|
401
|
+
throw new ProjectError("src/auth: `user` must be a function `user(id)`");
|
|
402
|
+
}
|
|
403
|
+
auth = mod.user ? { user: mod.user } : {};
|
|
404
|
+
if (hasProviders && !Array.isArray(mod.providers)) {
|
|
405
|
+
throw new ProjectError("src/auth: `providers` is an array of sign-in providers");
|
|
406
|
+
}
|
|
407
|
+
if (hasEmail && typeof mod.email?.consume !== "function") {
|
|
408
|
+
throw new ProjectError("src/auth: `email` is an `emailSignIn({...})`");
|
|
409
|
+
}
|
|
410
|
+
if (hasProviders || hasEmail) {
|
|
411
|
+
if (typeof mod.signIn !== "function") {
|
|
412
|
+
throw new ProjectError("src/auth: export `signIn(identity)` returning the user id an identity signs in as, or null");
|
|
413
|
+
}
|
|
414
|
+
if (hasProviders)
|
|
415
|
+
auth.providers = mod.providers;
|
|
416
|
+
if (hasEmail)
|
|
417
|
+
auth.email = mod.email;
|
|
418
|
+
auth.signIn = mod.signIn;
|
|
419
|
+
if (mod.signInError !== undefined) {
|
|
420
|
+
if (typeof mod.signInError !== "string") {
|
|
421
|
+
throw new ProjectError("src/auth: `signInError` is a path such as \"/signin\"");
|
|
422
|
+
}
|
|
423
|
+
auth.signInError = mod.signInError;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (mod.twoFactor !== undefined) {
|
|
427
|
+
if (typeof mod.twoFactor?.needed !== "function") {
|
|
428
|
+
throw new ProjectError("src/auth: `twoFactor` is a `defineTwoFactor({...})`");
|
|
429
|
+
}
|
|
430
|
+
auth.twoFactor = mod.twoFactor;
|
|
431
|
+
}
|
|
432
|
+
if (mod.sessions !== undefined) {
|
|
433
|
+
const s = mod.sessions;
|
|
434
|
+
if (typeof s?.get !== "function" || typeof s.set !== "function" || typeof s.delete !== "function") {
|
|
435
|
+
throw new ProjectError("src/auth: `sessions` is a session store such as `new SqlSessionStore(db)`");
|
|
436
|
+
}
|
|
437
|
+
auth.store = mod.sessions;
|
|
438
|
+
}
|
|
439
|
+
if (mod.viewAs !== undefined) {
|
|
440
|
+
if (typeof mod.viewAs !== "function")
|
|
441
|
+
throw new ProjectError("src/auth: `viewAs` is a function `viewAs(operator, user)`");
|
|
442
|
+
auth.viewAs = mod.viewAs;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
const themeFile = ["theme.ts", "theme.js"].map((name) => join(opts.outDir, name)).find((file) => existsSync(file));
|
|
446
|
+
let theme;
|
|
447
|
+
if (themeFile) {
|
|
448
|
+
const exported = (await import(__rewriteRelativeImportExtension(pathToFileURL(themeFile).href))).default;
|
|
449
|
+
if (!exported ||
|
|
450
|
+
typeof exported.css !== "function" ||
|
|
451
|
+
typeof exported.tailwind !== "function" ||
|
|
452
|
+
typeof exported.htmlAttributes !== "function") {
|
|
453
|
+
throw new ProjectError("src/theme: export default defineTheme({...}) from @weft-framework/theme");
|
|
454
|
+
}
|
|
455
|
+
theme = exported;
|
|
456
|
+
}
|
|
457
|
+
const notFound = files.includes("_404.ts") ? await route("_404.ts") : undefined;
|
|
458
|
+
const error = files.includes("_error.ts") ? await route("_error.ts") : undefined;
|
|
459
|
+
const stylesFile = join(opts.outDir, "styles.json");
|
|
460
|
+
let styles = { components: {} };
|
|
461
|
+
if (opts.compileStyles ?? opts.compiler !== undefined) {
|
|
462
|
+
try {
|
|
463
|
+
styles = await projectStyles(root, componentStyles(mark), { minify: opts.production, theme });
|
|
464
|
+
}
|
|
465
|
+
catch (err) {
|
|
466
|
+
if (err instanceof StyleError) {
|
|
467
|
+
throw new ProjectError(err.message);
|
|
468
|
+
}
|
|
469
|
+
throw err;
|
|
470
|
+
}
|
|
471
|
+
writeFileSync(stylesFile, JSON.stringify(styles));
|
|
472
|
+
}
|
|
473
|
+
else if (existsSync(stylesFile)) {
|
|
474
|
+
styles = JSON.parse(readFileSync(stylesFile, "utf8"));
|
|
475
|
+
}
|
|
476
|
+
const shellFile = opts.compiler ? join(src, "index.html") : join(opts.outDir, "index.html");
|
|
477
|
+
const publicDir = join(root, "public");
|
|
478
|
+
return {
|
|
479
|
+
routes,
|
|
480
|
+
notFound,
|
|
481
|
+
error,
|
|
482
|
+
shell: existsSync(shellFile) ? readFileSync(shellFile, "utf8") : undefined,
|
|
483
|
+
files: existsSync(publicDir) ? publicFiles(publicDir, opts.publicFiles) : undefined,
|
|
484
|
+
assets,
|
|
485
|
+
...(styles.global !== undefined ? { stylesheet: styles.global } : {}),
|
|
486
|
+
...(Object.keys(styles.components).length > 0 ? { componentStyles: styles.components } : {}),
|
|
487
|
+
actions,
|
|
488
|
+
api,
|
|
489
|
+
models,
|
|
490
|
+
emails,
|
|
491
|
+
database: config.database === undefined || config.database === ":memory:" ? config.database : resolve(root, config.database),
|
|
492
|
+
middleware,
|
|
493
|
+
...(ready ? { ready } : {}),
|
|
494
|
+
...(sharedFields ? { sharedFields } : {}),
|
|
495
|
+
...(offline ? { offline } : {}),
|
|
496
|
+
...(pdf ? { pdf } : {}),
|
|
497
|
+
...(images ? { images } : {}),
|
|
498
|
+
...seo,
|
|
499
|
+
...(auth ? { auth } : {}),
|
|
500
|
+
...(uploads ? { uploads } : {}),
|
|
501
|
+
meta: config.meta,
|
|
502
|
+
siteUrl: config.siteUrl,
|
|
503
|
+
cookieName: config.cookieName,
|
|
504
|
+
...(config.csp ? { csp: config.csp } : {}),
|
|
505
|
+
...(config.edge !== undefined ? { edge: config.edge } : {}),
|
|
506
|
+
...htmlAttributesOf(theme, config.htmlAttributes),
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
function htmlAttributesOf(theme, own) {
|
|
510
|
+
if (!theme)
|
|
511
|
+
return own ? { htmlAttributes: own } : {};
|
|
512
|
+
return { htmlAttributes: (req) => ({ ...theme.htmlAttributes(req), ...own?.(req) }) };
|
|
513
|
+
}
|
|
514
|
+
function apiHandlers(mod, file) {
|
|
515
|
+
const handlers = {};
|
|
516
|
+
for (const method of API_METHODS) {
|
|
517
|
+
const fn = mod[method];
|
|
518
|
+
if (fn === undefined) {
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
if (typeof fn !== "function" || isComponent(fn)) {
|
|
522
|
+
throw new ProjectError(`${file}: \`${method}\` must be a function that returns a Response`);
|
|
523
|
+
}
|
|
524
|
+
handlers[method] = fn;
|
|
525
|
+
}
|
|
526
|
+
if (mod.default !== undefined) {
|
|
527
|
+
if (typeof mod.default !== "function" || isComponent(mod.default)) {
|
|
528
|
+
throw new ProjectError(`${file}: the default export must be a function that returns a Response`);
|
|
529
|
+
}
|
|
530
|
+
handlers.default = mod.default;
|
|
531
|
+
}
|
|
532
|
+
if (Object.keys(handlers).length === 0) {
|
|
533
|
+
throw new ProjectError(`${file}: export a request method or a default function returning a Response`);
|
|
534
|
+
}
|
|
535
|
+
return handlers;
|
|
536
|
+
}
|