@weft-framework/server 0.0.0-stage → 0.0.1
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
package/dist/contract.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { parsePattern } from "./router.js";
|
|
2
|
+
const PARAM_TYPE = { string: "string", number: "number", boolean: "boolean" };
|
|
3
|
+
function paramSchema(p) {
|
|
4
|
+
const item = { type: PARAM_TYPE[p.type] };
|
|
5
|
+
return p.list ? { type: "array", items: item } : item;
|
|
6
|
+
}
|
|
7
|
+
export function actionSchema(params) {
|
|
8
|
+
const properties = {};
|
|
9
|
+
const required = [];
|
|
10
|
+
for (const p of params) {
|
|
11
|
+
properties[p.name] = p.optional ? { anyOf: [paramSchema(p), { type: "null" }] } : paramSchema(p);
|
|
12
|
+
if (!p.optional) {
|
|
13
|
+
required.push(p.name);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return { type: "object", properties, ...(required.length ? { required } : {}), additionalProperties: false };
|
|
17
|
+
}
|
|
18
|
+
const KIND = {
|
|
19
|
+
text: { type: "string" },
|
|
20
|
+
real: { type: "number" },
|
|
21
|
+
integer: { type: "integer" },
|
|
22
|
+
bool: { type: "boolean" },
|
|
23
|
+
date: { type: "string", format: "date-time" },
|
|
24
|
+
json: {},
|
|
25
|
+
};
|
|
26
|
+
export function modelSchema(model) {
|
|
27
|
+
const properties = {};
|
|
28
|
+
const required = [];
|
|
29
|
+
for (const f of model.fields) {
|
|
30
|
+
let s = { ...KIND[f.kind] };
|
|
31
|
+
if (f.nullable && f.kind !== "json") {
|
|
32
|
+
s = { anyOf: [s, { type: "null" }] };
|
|
33
|
+
}
|
|
34
|
+
if (f.ref !== undefined) {
|
|
35
|
+
s["x-weft-ref"] = f.ref;
|
|
36
|
+
}
|
|
37
|
+
if (f.unique) {
|
|
38
|
+
s["x-weft-unique"] = true;
|
|
39
|
+
}
|
|
40
|
+
s["x-weft-type"] = f.type;
|
|
41
|
+
properties[f.name] = s;
|
|
42
|
+
if (!f.optional && !f.nullable) {
|
|
43
|
+
required.push(f.name);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
type: "object",
|
|
48
|
+
properties,
|
|
49
|
+
...(required.length ? { required } : {}),
|
|
50
|
+
"x-weft-key": model.key,
|
|
51
|
+
"x-weft-authorization": "rows are read and written through the model's @read/@write rules",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function openApiPaths(pattern) {
|
|
55
|
+
const segs = parsePattern(pattern);
|
|
56
|
+
const out = [];
|
|
57
|
+
const params = [];
|
|
58
|
+
let optionalRest = false;
|
|
59
|
+
for (const s of segs) {
|
|
60
|
+
if (s.kind === "static") {
|
|
61
|
+
out.push(s.value);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
out.push(`{${s.name}}`);
|
|
65
|
+
params.push(s.name);
|
|
66
|
+
optionalRest = s.kind === "rest" && s.optional;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const full = { path: "/" + out.join("/"), params };
|
|
70
|
+
if (!optionalRest) {
|
|
71
|
+
return [full];
|
|
72
|
+
}
|
|
73
|
+
return [{ path: "/" + out.slice(0, -1).join("/"), params: params.slice(0, -1) }, full];
|
|
74
|
+
}
|
|
75
|
+
const SECURED = [{ session: [], csrf: [] }];
|
|
76
|
+
const METHODS = ["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"];
|
|
77
|
+
export function openApi(src, info) {
|
|
78
|
+
const paths = {};
|
|
79
|
+
paths["/_weft/session"] = {
|
|
80
|
+
get: {
|
|
81
|
+
operationId: "weftSession",
|
|
82
|
+
summary: "Session cookie and CSRF token for code without a rendered page",
|
|
83
|
+
responses: {
|
|
84
|
+
"200": {
|
|
85
|
+
description: "The session's CSRF token (a session cookie is set if there was none)",
|
|
86
|
+
content: {
|
|
87
|
+
"application/json": {
|
|
88
|
+
schema: { type: "object", properties: { csrf: { type: "string" } }, required: ["csrf"] },
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
"403": { description: "Cross-origin request" },
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
for (const name of Object.keys(src.actions ?? {}).sort()) {
|
|
97
|
+
const action = src.actions[name];
|
|
98
|
+
if (action.params.some((p) => p.held))
|
|
99
|
+
continue;
|
|
100
|
+
paths[`/_weft/action/${name}`] = {
|
|
101
|
+
post: {
|
|
102
|
+
operationId: name,
|
|
103
|
+
summary: `Action ${name}`,
|
|
104
|
+
security: SECURED,
|
|
105
|
+
requestBody: {
|
|
106
|
+
required: true,
|
|
107
|
+
content: { "application/json": { schema: actionSchema(action.params) } },
|
|
108
|
+
},
|
|
109
|
+
responses: {
|
|
110
|
+
"200": { description: "Done", content: { "application/json": { schema: { $ref: "#/components/schemas/ActionResult" } } } },
|
|
111
|
+
default: {
|
|
112
|
+
description: "Refused (400 bad arguments or the action's own failure, 403 session/CSRF, 404, 413, 429, 500)",
|
|
113
|
+
content: { "application/json": { schema: { $ref: "#/components/schemas/ActionFailure" } } },
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
for (const pattern of Object.keys(src.api ?? {}).sort()) {
|
|
120
|
+
const handlers = src.api[pattern];
|
|
121
|
+
for (const { path, params } of openApiPaths(pattern)) {
|
|
122
|
+
const item = {};
|
|
123
|
+
if (params.length) {
|
|
124
|
+
item.parameters = params.map((p) => ({ name: p, in: "path", required: true, schema: { type: "string" } }));
|
|
125
|
+
}
|
|
126
|
+
for (const m of METHODS) {
|
|
127
|
+
if (typeof handlers[m] !== "function") {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const unsafe = m === "POST" || m === "PUT" || m === "PATCH" || m === "DELETE";
|
|
131
|
+
item[m.toLowerCase()] = {
|
|
132
|
+
summary: `${m} ${pattern}`,
|
|
133
|
+
...(unsafe ? { security: SECURED } : {}),
|
|
134
|
+
responses: { default: { description: "The handler's response" } },
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
paths[path] = item;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const schemas = {
|
|
141
|
+
ActionResult: {
|
|
142
|
+
type: "object",
|
|
143
|
+
properties: {
|
|
144
|
+
ok: { const: true },
|
|
145
|
+
value: { description: "The action's return value" },
|
|
146
|
+
redirect: { type: "string", description: "A same-origin path to navigate to" },
|
|
147
|
+
},
|
|
148
|
+
required: ["ok"],
|
|
149
|
+
},
|
|
150
|
+
ActionFailure: {
|
|
151
|
+
type: "object",
|
|
152
|
+
properties: { ok: { const: false }, error: { type: "string" } },
|
|
153
|
+
required: ["ok", "error"],
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
for (const name of Object.keys(src.models ?? {}).sort()) {
|
|
157
|
+
schemas[name] = modelSchema(src.models[name]);
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
openapi: "3.1.0",
|
|
161
|
+
info,
|
|
162
|
+
paths,
|
|
163
|
+
components: {
|
|
164
|
+
schemas,
|
|
165
|
+
securitySchemes: {
|
|
166
|
+
session: { type: "apiKey", in: "cookie", name: "weft_sid", description: "Signed session id (HttpOnly)" },
|
|
167
|
+
csrf: { type: "apiKey", in: "header", name: "x-weft-csrf", description: "The session's CSRF token" },
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface DataAccess {
|
|
2
|
+
modules: string[];
|
|
3
|
+
reads: string[];
|
|
4
|
+
writes: string[];
|
|
5
|
+
unscoped: string[];
|
|
6
|
+
uses: string[];
|
|
7
|
+
grants: string[];
|
|
8
|
+
packages: string[];
|
|
9
|
+
}
|
|
10
|
+
export interface DataManifest {
|
|
11
|
+
weft: 1;
|
|
12
|
+
routes: Record<string, DataAccess>;
|
|
13
|
+
api: Record<string, DataAccess>;
|
|
14
|
+
middleware: DataAccess | null;
|
|
15
|
+
models: Record<string, {
|
|
16
|
+
readBy: string[];
|
|
17
|
+
writtenBy: string[];
|
|
18
|
+
}>;
|
|
19
|
+
unused: {
|
|
20
|
+
models: string[];
|
|
21
|
+
fields: Record<string, string[]>;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export declare function dataManifest(serverDir: string): Promise<DataManifest>;
|
package/dist/datamap.js
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
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, readdirSync, readFileSync, statSync } from "node:fs";
|
|
10
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
11
|
+
import { pathToFileURL } from "node:url";
|
|
12
|
+
import { Model } from "@weft-framework/data";
|
|
13
|
+
import { patternForFile } from "./router.js";
|
|
14
|
+
const READS = new Set(["get", "all", "where", "find", "query", "count", "exists", "first", "page", "search", "feed", "writable"]);
|
|
15
|
+
const WRITES = new Set(["create", "update", "updateLocked", "updateRows", "delete", "deleteRows", "restore", "upsert", "insert"]);
|
|
16
|
+
const UNSCOPED_WRITES = new Set(["removeUnscoped", "unlinkUnscoped", "purgeTrash", "purgeExpired"]);
|
|
17
|
+
function regexIdent(name) {
|
|
18
|
+
let out = "";
|
|
19
|
+
for (const ch of name) {
|
|
20
|
+
out += (ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z") || (ch >= "0" && ch <= "9") || ch === "_" ? ch : `\\${ch}`;
|
|
21
|
+
}
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
function listModules(dir) {
|
|
25
|
+
const out = [];
|
|
26
|
+
const walk = (d) => {
|
|
27
|
+
for (const name of readdirSync(d).sort()) {
|
|
28
|
+
const full = join(d, name);
|
|
29
|
+
if (statSync(full).isDirectory())
|
|
30
|
+
walk(full);
|
|
31
|
+
else if (name.endsWith(".ts") && !name.endsWith(".d.ts") && !name.endsWith(".client.ts"))
|
|
32
|
+
out.push(relative(dir, full).split(sep).join("/"));
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
if (existsSync(dir))
|
|
36
|
+
walk(dir);
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
const IMPORT = /\b(?:import|export)\s+(type\s+)?([\w$*{}\s,]*?)\s*from\s*["']([^"']+)["']|\bimport\s*\(\s*["']([^"']+)["']\s*\)/g;
|
|
40
|
+
function resolveImport(root, from, spec) {
|
|
41
|
+
const base = resolve(dirname(join(root, from)), spec);
|
|
42
|
+
for (const cand of [base, base.replace(/\.(wx|js|tsx?)$/, ".ts"), base + ".ts", join(base, "index.ts")]) {
|
|
43
|
+
if (cand.endsWith(".ts") && existsSync(cand) && statSync(cand).isFile()) {
|
|
44
|
+
const rel = relative(root, cand).split(sep).join("/");
|
|
45
|
+
return rel.startsWith("..") ? null : rel;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
function importedNames(clause) {
|
|
51
|
+
const out = [];
|
|
52
|
+
const braces = /\{([^}]*)\}/.exec(clause);
|
|
53
|
+
if (braces) {
|
|
54
|
+
for (const part of braces[1].split(",")) {
|
|
55
|
+
const p = part.trim().replace(/^type\s+/, "");
|
|
56
|
+
if (p === "")
|
|
57
|
+
continue;
|
|
58
|
+
const m = /^([\w$]+)(?:\s+as\s+([\w$]+))?$/.exec(p);
|
|
59
|
+
if (m)
|
|
60
|
+
out.push([m[1], m[2] ?? m[1]]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const rest = clause.replace(/\{[^}]*\}/, "").replace(/,/g, " ").trim();
|
|
64
|
+
const star = /\*\s+as\s+([\w$]+)/.exec(rest);
|
|
65
|
+
if (star)
|
|
66
|
+
out.push(["*", star[1]]);
|
|
67
|
+
else if (/^[\w$]+$/.test(rest))
|
|
68
|
+
out.push(["default", rest]);
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
function modelsOf(v) {
|
|
72
|
+
if (v instanceof Model)
|
|
73
|
+
return [v];
|
|
74
|
+
if (typeof v === "object" && v !== null && "model" in v && v.model instanceof Model) {
|
|
75
|
+
return [v.model];
|
|
76
|
+
}
|
|
77
|
+
if (typeof v === "object" && v !== null && "models" in v) {
|
|
78
|
+
const m = v.models;
|
|
79
|
+
if (typeof m === "object" && m !== null) {
|
|
80
|
+
return Object.values(m)
|
|
81
|
+
.filter((x) => x instanceof Model);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
function skipParens(s, i) {
|
|
87
|
+
let depth = 0;
|
|
88
|
+
for (let j = i; j < s.length; j++) {
|
|
89
|
+
const c = s[j];
|
|
90
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
91
|
+
for (j++; j < s.length && s[j] !== c; j++)
|
|
92
|
+
if (s[j] === "\\")
|
|
93
|
+
j++;
|
|
94
|
+
}
|
|
95
|
+
else if (c === "(")
|
|
96
|
+
depth++;
|
|
97
|
+
else if (c === ")" && --depth === 0)
|
|
98
|
+
return j + 1;
|
|
99
|
+
}
|
|
100
|
+
return s.length;
|
|
101
|
+
}
|
|
102
|
+
function chain(s, i) {
|
|
103
|
+
const out = [];
|
|
104
|
+
const re = /^\s*\??\.\s*([\w$]+)\s*(\()?/;
|
|
105
|
+
for (;;) {
|
|
106
|
+
const m = re.exec(s.slice(i, i + 200));
|
|
107
|
+
if (!m)
|
|
108
|
+
return out;
|
|
109
|
+
out.push(m[1]);
|
|
110
|
+
i += m[0].length;
|
|
111
|
+
if (m[2])
|
|
112
|
+
i = skipParens(s, i - 1);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function code(s) {
|
|
116
|
+
const out = s.split("");
|
|
117
|
+
const blank = (from, to) => {
|
|
118
|
+
for (let k = from; k < to; k++)
|
|
119
|
+
if (out[k] !== "\n")
|
|
120
|
+
out[k] = " ";
|
|
121
|
+
};
|
|
122
|
+
const tpl = [];
|
|
123
|
+
for (let i = 0; i < s.length; i++) {
|
|
124
|
+
const c = s[i];
|
|
125
|
+
if (c === "/" && s[i + 1] === "/") {
|
|
126
|
+
const end = s.indexOf("\n", i);
|
|
127
|
+
const e = end < 0 ? s.length : end;
|
|
128
|
+
blank(i, e);
|
|
129
|
+
i = e;
|
|
130
|
+
}
|
|
131
|
+
else if (c === "/" && s[i + 1] === "*") {
|
|
132
|
+
const end = s.indexOf("*/", i + 2);
|
|
133
|
+
const e = end < 0 ? s.length : end + 2;
|
|
134
|
+
blank(i, e);
|
|
135
|
+
i = e - 1;
|
|
136
|
+
}
|
|
137
|
+
else if (c === '"' || c === "'" || (c === "`") || (c === "}" && tpl.length > 0 && tpl[tpl.length - 1] === 0)) {
|
|
138
|
+
if (c === "}")
|
|
139
|
+
tpl.pop();
|
|
140
|
+
const q = c === "}" ? "`" : c;
|
|
141
|
+
let j = i + 1;
|
|
142
|
+
for (; j < s.length && s[j] !== q; j++) {
|
|
143
|
+
if (s[j] === "\\")
|
|
144
|
+
j++;
|
|
145
|
+
else if (q === "`" && s[j] === "$" && s[j + 1] === "{") {
|
|
146
|
+
tpl.push(0);
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
blank(i + 1, Math.min(j, s.length));
|
|
151
|
+
i = q === "`" && s[j] === "$" ? j + 1 : j;
|
|
152
|
+
}
|
|
153
|
+
else if (tpl.length > 0 && c === "{")
|
|
154
|
+
tpl[tpl.length - 1]++;
|
|
155
|
+
else if (tpl.length > 0 && c === "}")
|
|
156
|
+
tpl[tpl.length - 1]--;
|
|
157
|
+
}
|
|
158
|
+
return out.join("");
|
|
159
|
+
}
|
|
160
|
+
function emptyAccess() {
|
|
161
|
+
return { modules: new Set(), packages: new Set(), reads: [], writes: [], unscoped: [], uses: [], grants: [] };
|
|
162
|
+
}
|
|
163
|
+
function unusedData(modules, own, objects) {
|
|
164
|
+
const read = new Set();
|
|
165
|
+
for (const a of own.values())
|
|
166
|
+
for (const m of [...a.reads, ...a.unscoped, ...a.uses, ...a.grants])
|
|
167
|
+
read.add(m);
|
|
168
|
+
const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
169
|
+
const fields = {};
|
|
170
|
+
for (const [name, model] of objects) {
|
|
171
|
+
const decl = new RegExp(`defineModel\\s*(?:<[^>]*>)?\\s*\\(\\s*["'\`]${esc(name)}["'\`]`);
|
|
172
|
+
if (![...modules.values()].some((m) => decl.test(m.source)))
|
|
173
|
+
continue;
|
|
174
|
+
const key = model.key;
|
|
175
|
+
const unread = Object.keys(model.config.fields ?? {}).filter((f) => {
|
|
176
|
+
if (f === key || f === "id")
|
|
177
|
+
return false;
|
|
178
|
+
const n = esc(f);
|
|
179
|
+
const inCode = new RegExp(`(?:\\?\\.|\\.)\\s*${n}(?![\\w$])|[{,]\\s*${n}\\s*[,}=]`);
|
|
180
|
+
const inStrings = new RegExp(`\\[\\s*["'\`]${n}["'\`]\\s*\\]|["'\`]${n}["'\`](?!\\s*:)`);
|
|
181
|
+
return ![...modules.values()].some((m) => inCode.test(m.code) || inStrings.test(m.source));
|
|
182
|
+
});
|
|
183
|
+
if (unread.length > 0)
|
|
184
|
+
fields[name] = unread.sort();
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
models: [...objects.keys()].filter((m) => !read.has(m)).sort(),
|
|
188
|
+
fields: Object.fromEntries(Object.entries(fields).sort(([a], [b]) => (a < b ? -1 : 1))),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
export async function dataManifest(serverDir) {
|
|
192
|
+
const root = resolve(serverDir);
|
|
193
|
+
const files = listModules(root);
|
|
194
|
+
const modules = new Map();
|
|
195
|
+
const exportsOf = new Map();
|
|
196
|
+
const objects = new Map();
|
|
197
|
+
for (const rel of files) {
|
|
198
|
+
const source = readFileSync(join(root, rel), "utf8");
|
|
199
|
+
const own = new Map();
|
|
200
|
+
if (/\bexport\b/.test(source)) {
|
|
201
|
+
try {
|
|
202
|
+
const mod = (await import(__rewriteRelativeImportExtension(pathToFileURL(join(root, rel)).href)));
|
|
203
|
+
for (const [name, v] of Object.entries(mod)) {
|
|
204
|
+
const ms = modelsOf(v);
|
|
205
|
+
for (const x of ms)
|
|
206
|
+
objects.set(x.name, x);
|
|
207
|
+
if (ms.length > 0)
|
|
208
|
+
own.set(name, ms.map((x) => x.name));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
exportsOf.set(rel, own);
|
|
215
|
+
modules.set(rel, { imports: [], packages: [], bindings: new Map(own), source, code: code(source) });
|
|
216
|
+
}
|
|
217
|
+
for (const [rel, info] of modules) {
|
|
218
|
+
for (const m of info.source.matchAll(IMPORT)) {
|
|
219
|
+
const spec = m[3] ?? m[4];
|
|
220
|
+
if (!spec.startsWith(".")) {
|
|
221
|
+
const pkg = spec.startsWith("@") ? spec.split("/").slice(0, 2).join("/") : spec.split("/")[0];
|
|
222
|
+
if (!pkg.startsWith("node:"))
|
|
223
|
+
info.packages.push(pkg);
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
const target = resolveImport(root, rel, spec);
|
|
227
|
+
if (target === null)
|
|
228
|
+
continue;
|
|
229
|
+
info.imports.push(target);
|
|
230
|
+
if (m[1] || m[2] === undefined)
|
|
231
|
+
continue;
|
|
232
|
+
const theirs = exportsOf.get(target);
|
|
233
|
+
if (!theirs)
|
|
234
|
+
continue;
|
|
235
|
+
for (const [imported, local] of importedNames(m[2])) {
|
|
236
|
+
if (imported === "*") {
|
|
237
|
+
for (const [name, ms] of theirs)
|
|
238
|
+
info.bindings.set(`${local}.${name}`, ms);
|
|
239
|
+
}
|
|
240
|
+
else if (theirs.has(imported))
|
|
241
|
+
info.bindings.set(local, theirs.get(imported));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const own = new Map();
|
|
246
|
+
for (const [rel, info] of modules) {
|
|
247
|
+
const a = emptyAccess();
|
|
248
|
+
const imports = [...info.source.matchAll(IMPORT)].map((m) => [m.index, m.index + m[0].length]);
|
|
249
|
+
for (const [name, ms] of info.bindings) {
|
|
250
|
+
const re = new RegExp(`(?<![\\w$.])${regexIdent(name)}(?![\\w$])`, "g");
|
|
251
|
+
for (const m of info.code.matchAll(re)) {
|
|
252
|
+
if (imports.some(([s, e]) => m.index >= s && m.index < e))
|
|
253
|
+
continue;
|
|
254
|
+
const calls = chain(info.code, m.index + m[0].length);
|
|
255
|
+
const before = info.code.slice(Math.max(0, m.index - 80), m.index);
|
|
256
|
+
if (calls.length === 0 && /(?:\b(?:const|let|var)\s*(?:\{[^}=]*)?|\b(?:type|interface|typeof|keyof)\s+)$/.test(before))
|
|
257
|
+
continue;
|
|
258
|
+
if (calls.length === 0 && /^\s*[<\]]/.test(info.code.slice(m.index + m[0].length, m.index + m[0].length + 4)))
|
|
259
|
+
continue;
|
|
260
|
+
const [first, ...rest] = calls;
|
|
261
|
+
const add = (list) => list.push(...ms);
|
|
262
|
+
if (first === "publicUnscoped")
|
|
263
|
+
add(a.unscoped);
|
|
264
|
+
else if (first !== undefined && UNSCOPED_WRITES.has(first))
|
|
265
|
+
add(a.writes);
|
|
266
|
+
else if (first === "as") {
|
|
267
|
+
const ops = rest.filter((c) => c !== "using" && c !== "trash");
|
|
268
|
+
if (rest.includes("using"))
|
|
269
|
+
add(a.grants);
|
|
270
|
+
const op = ops[0];
|
|
271
|
+
if (op !== undefined && READS.has(op))
|
|
272
|
+
add(a.reads);
|
|
273
|
+
else if (op !== undefined && WRITES.has(op))
|
|
274
|
+
add(a.writes);
|
|
275
|
+
else
|
|
276
|
+
add(a.uses);
|
|
277
|
+
}
|
|
278
|
+
else if (first === "name" || first === "feed" || first === "lockedFields") {
|
|
279
|
+
}
|
|
280
|
+
else
|
|
281
|
+
add(a.uses);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
for (const p of info.packages)
|
|
285
|
+
a.packages.add(p);
|
|
286
|
+
own.set(rel, a);
|
|
287
|
+
}
|
|
288
|
+
const collect = (entry) => {
|
|
289
|
+
const seen = new Set();
|
|
290
|
+
const stack = [...entry];
|
|
291
|
+
while (stack.length > 0) {
|
|
292
|
+
const f = stack.pop();
|
|
293
|
+
if (seen.has(f) || !modules.has(f))
|
|
294
|
+
continue;
|
|
295
|
+
seen.add(f);
|
|
296
|
+
stack.push(...modules.get(f).imports);
|
|
297
|
+
}
|
|
298
|
+
const sorted = (pick) => [...new Set([...seen].flatMap((f) => [...pick(own.get(f))]))].sort();
|
|
299
|
+
return {
|
|
300
|
+
modules: [...seen].sort(),
|
|
301
|
+
reads: sorted((a) => a.reads),
|
|
302
|
+
writes: sorted((a) => a.writes),
|
|
303
|
+
unscoped: sorted((a) => a.unscoped),
|
|
304
|
+
uses: sorted((a) => a.uses),
|
|
305
|
+
grants: sorted((a) => a.grants),
|
|
306
|
+
packages: sorted((a) => a.packages),
|
|
307
|
+
};
|
|
308
|
+
};
|
|
309
|
+
const layoutsFor = (rel) => {
|
|
310
|
+
const dirs = rel.split("/").slice(0, -1);
|
|
311
|
+
const out = [];
|
|
312
|
+
for (let i = 0; i <= dirs.length; i++) {
|
|
313
|
+
const f = ["pages", ...dirs.slice(0, i), "_layout.ts"].join("/");
|
|
314
|
+
if (modules.has(f))
|
|
315
|
+
out.push(f);
|
|
316
|
+
}
|
|
317
|
+
return out;
|
|
318
|
+
};
|
|
319
|
+
const routes = {};
|
|
320
|
+
const api = {};
|
|
321
|
+
for (const rel of files.filter((f) => f.startsWith("pages/")).map((f) => f.slice("pages/".length))) {
|
|
322
|
+
const parts = rel.split("/");
|
|
323
|
+
if (rel.startsWith("api/")) {
|
|
324
|
+
if (!parts.some((p) => p.startsWith("_")))
|
|
325
|
+
api[patternForFile(rel)] = collect([`pages/${rel}`]);
|
|
326
|
+
}
|
|
327
|
+
else if (!parts.some((p) => p.startsWith("_"))) {
|
|
328
|
+
routes[patternForFile(rel)] = collect([`pages/${rel}`, ...layoutsFor(rel)]);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const middleware = modules.has("middleware.ts") ? collect(["middleware.ts"]) : null;
|
|
332
|
+
const models = {};
|
|
333
|
+
const note = (model, kind, who) => {
|
|
334
|
+
models[model] ??= { readBy: [], writtenBy: [] };
|
|
335
|
+
if (!models[model][kind].includes(who))
|
|
336
|
+
models[model][kind].push(who);
|
|
337
|
+
};
|
|
338
|
+
for (const ms of exportsOf.values())
|
|
339
|
+
for (const list of ms.values())
|
|
340
|
+
for (const m of list)
|
|
341
|
+
models[m] ??= { readBy: [], writtenBy: [] };
|
|
342
|
+
const entries = [
|
|
343
|
+
...Object.entries(routes),
|
|
344
|
+
...Object.entries(api).map(([p, a]) => [`api ${p}`, a]),
|
|
345
|
+
...(middleware ? [["middleware", middleware]] : []),
|
|
346
|
+
];
|
|
347
|
+
for (const [who, a] of entries) {
|
|
348
|
+
for (const m of [...a.reads, ...a.unscoped, ...a.uses, ...a.grants])
|
|
349
|
+
note(m, "readBy", who);
|
|
350
|
+
for (const m of [...a.writes, ...a.uses])
|
|
351
|
+
note(m, "writtenBy", who);
|
|
352
|
+
}
|
|
353
|
+
for (const v of Object.values(models)) {
|
|
354
|
+
v.readBy.sort();
|
|
355
|
+
v.writtenBy.sort();
|
|
356
|
+
}
|
|
357
|
+
return {
|
|
358
|
+
weft: 1,
|
|
359
|
+
routes,
|
|
360
|
+
api,
|
|
361
|
+
middleware,
|
|
362
|
+
models: Object.fromEntries(Object.entries(models).sort(([a], [b]) => (a < b ? -1 : 1))),
|
|
363
|
+
unused: unusedData(modules, own, objects),
|
|
364
|
+
};
|
|
365
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type BackupFile } from "./backup.js";
|
|
2
|
+
import { type Compiler } from "./compiler.js";
|
|
3
|
+
export declare class BackupCommandError extends Error {
|
|
4
|
+
}
|
|
5
|
+
export type BackupCommand = {
|
|
6
|
+
command: "backup";
|
|
7
|
+
} | {
|
|
8
|
+
command: "list";
|
|
9
|
+
} | {
|
|
10
|
+
command: "restore";
|
|
11
|
+
name: string;
|
|
12
|
+
env?: Record<string, string | undefined>;
|
|
13
|
+
};
|
|
14
|
+
export type BackupCommandResult = {
|
|
15
|
+
command: "backup";
|
|
16
|
+
database: string;
|
|
17
|
+
file: BackupFile;
|
|
18
|
+
} | {
|
|
19
|
+
command: "list";
|
|
20
|
+
database: string;
|
|
21
|
+
files: BackupFile[];
|
|
22
|
+
} | {
|
|
23
|
+
command: "restore";
|
|
24
|
+
database: string;
|
|
25
|
+
previous: string | null;
|
|
26
|
+
};
|
|
27
|
+
export declare function backupProject(root: string, compiler: Compiler, cmd: BackupCommand): Promise<BackupCommandResult>;
|