@yannosay/sinth 5.3.2 → 5.3.4
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/CHANGELOG.md +83 -0
- package/README.md +16 -4
- package/dist/browser/sinth.js +630 -81
- package/dist/cli/index.js +52 -10
- package/dist/cli/skill.d.ts +10 -0
- package/dist/cli/skill.js +106 -0
- package/dist/cli/templates.js +1 -1
- package/dist/compiler/codegen.js +42 -5
- package/dist/compiler/elements.d.ts +6 -0
- package/dist/compiler/elements.js +12 -0
- package/dist/compiler/html.d.ts +2 -0
- package/dist/compiler/html.js +7 -0
- package/dist/compiler/prerender.js +5 -3
- package/dist/deploy/cloudflare.d.ts +2 -0
- package/dist/deploy/cloudflare.js +103 -18
- package/dist/deploy/platforms.d.ts +20 -0
- package/dist/deploy/platforms.js +212 -0
- package/dist/deploy/redirects.d.ts +31 -0
- package/dist/deploy/redirects.js +102 -0
- package/dist/project/build.js +104 -18
- package/dist/project/config.d.ts +19 -0
- package/dist/project/config.js +66 -8
- package/dist/project/env.d.ts +11 -0
- package/dist/project/env.js +84 -0
- package/dist/project/project.d.ts +7 -1
- package/dist/project/project.js +37 -10
- package/dist/runtime/index.js +48 -8
- package/dist/runtime/router.js +17 -1
- package/dist/server/dev.d.ts +4 -0
- package/dist/server/dev.js +35 -11
- package/dist/server/functions-router.d.ts +60 -0
- package/dist/server/functions-router.js +161 -0
- package/dist/server/functions.d.ts +8 -12
- package/dist/server/functions.js +43 -133
- package/dist/server/preview.d.ts +3 -0
- package/dist/server/preview.js +8 -1
- package/dist/server/static.d.ts +9 -0
- package/dist/server/static.js +24 -1
- package/lib/ui/index.sinth +533 -24
- package/package.json +2 -1
- package/schema/sinth.config.schema.json +30 -1
- package/skills/agents.md +62 -0
- package/skills/sinth/SKILL.md +125 -0
- package/skills/sinth/references/language.md +175 -0
- package/skills/sinth/references/project-and-deploy.md +87 -0
- package/skills/sinth/references/ui.md +95 -0
package/dist/server/functions.js
CHANGED
|
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.FunctionsHost = exports.FUNCTIONS_DIR = void 0;
|
|
37
|
+
exports.scanFunctions = scanFunctions;
|
|
37
38
|
exports.toRequest = toRequest;
|
|
38
39
|
exports.sendResponse = sendResponse;
|
|
39
40
|
const crypto = __importStar(require("crypto"));
|
|
@@ -42,6 +43,9 @@ const fs = __importStar(require("fs"));
|
|
|
42
43
|
const path = __importStar(require("path"));
|
|
43
44
|
const diagnostics_1 = require("../compiler/diagnostics");
|
|
44
45
|
const ui_1 = require("../cli/ui");
|
|
46
|
+
const env_1 = require("../project/env");
|
|
47
|
+
const static_1 = require("./static");
|
|
48
|
+
const functions_router_1 = require("./functions-router");
|
|
45
49
|
/**
|
|
46
50
|
* Server routes in `functions/`, following the Cloudflare Pages Functions conventions so the same
|
|
47
51
|
* files run in `sinth dev`, `sinth preview` and on Cloudflare Pages after `sinth deploy`:
|
|
@@ -54,81 +58,38 @@ const ui_1 = require("../cli/ui");
|
|
|
54
58
|
* A module exports `onRequest` or method handlers such as `onRequestGet` / `onRequestPost`.
|
|
55
59
|
*/
|
|
56
60
|
exports.FUNCTIONS_DIR = "functions";
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return { kind: "catchall", name: catchall[1] };
|
|
70
|
-
const param = p.match(/^\[([A-Za-z_]\w*)\]$/);
|
|
71
|
-
if (param)
|
|
72
|
-
return { kind: "param", name: param[1] };
|
|
73
|
-
return { kind: "static", value: p };
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
function rank(route) {
|
|
77
|
-
return route.segments.map(s => (s.kind === "static" ? 0 : s.kind === "param" ? 1 : 2));
|
|
78
|
-
}
|
|
79
|
-
function compareRoutes(a, b) {
|
|
80
|
-
if (a.segments.length !== b.segments.length)
|
|
81
|
-
return b.segments.length - a.segments.length;
|
|
82
|
-
const ra = rank(a);
|
|
83
|
-
const rb = rank(b);
|
|
84
|
-
for (let i = 0; i < ra.length; i++)
|
|
85
|
-
if (ra[i] !== rb[i])
|
|
86
|
-
return ra[i] - rb[i];
|
|
87
|
-
return 0;
|
|
88
|
-
}
|
|
89
|
-
function matchRoute(route, parts, prefixOnly) {
|
|
90
|
-
const params = {};
|
|
91
|
-
for (let i = 0; i < route.segments.length; i++) {
|
|
92
|
-
const seg = route.segments[i];
|
|
93
|
-
if (seg.kind === "catchall") {
|
|
94
|
-
if (i >= parts.length)
|
|
95
|
-
return null;
|
|
96
|
-
params[seg.name] = parts.slice(i);
|
|
97
|
-
return params;
|
|
61
|
+
/** Every route file in a functions/ folder, most specific first. */
|
|
62
|
+
function scanFunctions(dir) {
|
|
63
|
+
const routes = [];
|
|
64
|
+
const walk = (d) => {
|
|
65
|
+
if (!fs.existsSync(d))
|
|
66
|
+
return;
|
|
67
|
+
for (const e of fs.readdirSync(d, { withFileTypes: true })) {
|
|
68
|
+
const full = path.join(d, e.name);
|
|
69
|
+
if (e.isDirectory())
|
|
70
|
+
walk(full);
|
|
71
|
+
else if ((0, functions_router_1.isFunctionFile)(e.name))
|
|
72
|
+
routes.push({ ...(0, functions_router_1.routeDef)(path.relative(dir, full).split(path.sep).join("/")), file: full });
|
|
98
73
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return null;
|
|
103
|
-
if (seg.kind === "param")
|
|
104
|
-
params[seg.name] = parts[i];
|
|
105
|
-
}
|
|
106
|
-
return prefixOnly || route.segments.length === parts.length ? params : null;
|
|
74
|
+
};
|
|
75
|
+
walk(dir);
|
|
76
|
+
return routes.sort(functions_router_1.compareRoutes);
|
|
107
77
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const file = path.join(root, name);
|
|
112
|
-
if (!fs.existsSync(file))
|
|
113
|
-
continue;
|
|
114
|
-
for (const line of fs.readFileSync(file, "utf-8").split(/\r?\n/)) {
|
|
115
|
-
const m = line.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*$/);
|
|
116
|
-
if (!m || m[1] in vars)
|
|
117
|
-
continue;
|
|
118
|
-
vars[m[1]] = m[2].replace(/^(["'])(.*)\1$/, "$2");
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return vars;
|
|
78
|
+
/** Server-side variables: `.dev.vars` (Wrangler's file) wins over the .env files. */
|
|
79
|
+
function readDevVars(root, mode) {
|
|
80
|
+
return { ...(0, env_1.loadEnv)(root, mode), ...(0, env_1.parseEnvFile)(path.join(root, ".dev.vars")) };
|
|
122
81
|
}
|
|
123
82
|
class FunctionsHost {
|
|
124
83
|
root;
|
|
84
|
+
mode;
|
|
125
85
|
routes = null;
|
|
126
86
|
modules = new Map();
|
|
127
87
|
building = null;
|
|
128
88
|
dir;
|
|
129
89
|
cacheDir;
|
|
130
|
-
constructor(root) {
|
|
90
|
+
constructor(root, mode = "development") {
|
|
131
91
|
this.root = root;
|
|
92
|
+
this.mode = mode;
|
|
132
93
|
this.dir = path.join(root, exports.FUNCTIONS_DIR);
|
|
133
94
|
this.cacheDir = path.join(root, ".sinth", "functions");
|
|
134
95
|
if (!globalThis.crypto)
|
|
@@ -142,24 +103,8 @@ class FunctionsHost {
|
|
|
142
103
|
this.building = null;
|
|
143
104
|
}
|
|
144
105
|
scan() {
|
|
145
|
-
if (this.routes)
|
|
146
|
-
|
|
147
|
-
const routes = [];
|
|
148
|
-
const walk = (dir) => {
|
|
149
|
-
if (!fs.existsSync(dir))
|
|
150
|
-
return;
|
|
151
|
-
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
152
|
-
const full = path.join(dir, e.name);
|
|
153
|
-
if (e.isDirectory())
|
|
154
|
-
walk(full);
|
|
155
|
-
else if (EXTENSIONS.test(e.name) && !e.name.endsWith(".d.ts") && !/\.(test|spec)\./.test(e.name)) {
|
|
156
|
-
const rel = path.relative(this.dir, full).replace(/\\/g, "/");
|
|
157
|
-
routes.push({ file: full, segments: parseSegments(rel), middleware: /(^|\/)_middleware\.\w+$/.test(rel) });
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
walk(this.dir);
|
|
162
|
-
this.routes = routes.sort(compareRoutes);
|
|
106
|
+
if (!this.routes)
|
|
107
|
+
this.routes = scanFunctions(this.dir);
|
|
163
108
|
return this.routes;
|
|
164
109
|
}
|
|
165
110
|
async load() {
|
|
@@ -210,60 +155,20 @@ class FunctionsHost {
|
|
|
210
155
|
async handle(request, assets) {
|
|
211
156
|
if (!this.enabled)
|
|
212
157
|
return null;
|
|
213
|
-
const url = new URL(request.url);
|
|
214
|
-
const parts = url.pathname.split("/").filter(Boolean).map(decodeURIComponent);
|
|
215
158
|
const routes = this.scan();
|
|
216
159
|
if (!routes.length)
|
|
217
160
|
return null;
|
|
218
161
|
await this.load();
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
continue;
|
|
230
|
-
const params = matchRoute(r, parts, false);
|
|
231
|
-
if (!params)
|
|
232
|
-
continue;
|
|
233
|
-
const handler = this.handlerFor(r.file, request.method);
|
|
234
|
-
if (!handler)
|
|
235
|
-
continue;
|
|
236
|
-
for (const h of Array.isArray(handler) ? handler : [handler])
|
|
237
|
-
chain.push({ handler: h, params, file: r.file });
|
|
238
|
-
break;
|
|
239
|
-
}
|
|
240
|
-
if (!chain.length)
|
|
241
|
-
return null;
|
|
242
|
-
const env = { ...readDevVars(this.root), ASSETS: { fetch: (input, init) => assets(new Request(input, init)) } };
|
|
243
|
-
const data = {};
|
|
244
|
-
const run = async (index, req) => {
|
|
245
|
-
const step = chain[index];
|
|
246
|
-
if (!step)
|
|
247
|
-
return assets(req);
|
|
248
|
-
const result = await step.handler({
|
|
249
|
-
request: req, params: step.params, env, data,
|
|
250
|
-
functionPath: url.pathname,
|
|
251
|
-
next: (input, init) => run(index + 1, input === undefined ? req : new Request(input, init)),
|
|
252
|
-
waitUntil: p => { p.catch(err => ui_1.log.error(String(err))); },
|
|
253
|
-
passThroughOnException: () => undefined,
|
|
254
|
-
});
|
|
255
|
-
if (!(result instanceof Response)) {
|
|
256
|
-
throw new Error(`${path.relative(this.root, step.file)} must return a Response (got ${result === undefined ? "undefined" : typeof result})`);
|
|
257
|
-
}
|
|
258
|
-
return result;
|
|
259
|
-
};
|
|
260
|
-
return run(0, request);
|
|
261
|
-
}
|
|
262
|
-
handlerFor(file, method) {
|
|
263
|
-
const mod = this.modules.get(file);
|
|
264
|
-
if (!mod)
|
|
265
|
-
return undefined;
|
|
266
|
-
return mod[METHOD_EXPORTS[method] ?? ""] ?? mod.onRequest;
|
|
162
|
+
const loaded = routes.flatMap(r => {
|
|
163
|
+
const mod = this.modules.get(r.file);
|
|
164
|
+
return mod ? [{ ...r, module: mod }] : [];
|
|
165
|
+
});
|
|
166
|
+
const env = { ...readDevVars(this.root, this.mode), ASSETS: { fetch: (input, init) => assets(new Request(input, init)) } };
|
|
167
|
+
return (0, functions_router_1.dispatch)(loaded, request, {
|
|
168
|
+
env,
|
|
169
|
+
assets,
|
|
170
|
+
waitUntil: p => { p.catch(err => ui_1.log.error(String(err))); },
|
|
171
|
+
});
|
|
267
172
|
}
|
|
268
173
|
}
|
|
269
174
|
exports.FunctionsHost = FunctionsHost;
|
|
@@ -279,8 +184,13 @@ async function toRequest(req) {
|
|
|
279
184
|
let body;
|
|
280
185
|
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
281
186
|
const chunks = [];
|
|
282
|
-
|
|
187
|
+
let size = 0;
|
|
188
|
+
for await (const chunk of req) {
|
|
189
|
+
size += chunk.byteLength;
|
|
190
|
+
if (size > static_1.MAX_BODY_BYTES)
|
|
191
|
+
throw new diagnostics_1.SinthError("E0804", `Request body is larger than ${static_1.MAX_BODY_BYTES / 1024 / 1024} MB`);
|
|
283
192
|
chunks.push(chunk);
|
|
193
|
+
}
|
|
284
194
|
body = Buffer.concat(chunks);
|
|
285
195
|
}
|
|
286
196
|
return new Request(`http://${req.headers.host ?? "localhost"}${req.url ?? "/"}`, { method: req.method, headers, body });
|
package/dist/server/preview.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { Redirect } from "../deploy/redirects";
|
|
1
2
|
export interface PreviewOptions {
|
|
2
3
|
port: number;
|
|
3
4
|
host: string;
|
|
4
5
|
open: boolean;
|
|
5
6
|
base: string;
|
|
6
7
|
root: string;
|
|
8
|
+
redirects: Redirect[];
|
|
9
|
+
allowedHosts: string[] | true;
|
|
7
10
|
}
|
|
8
11
|
export declare function startPreview(outDir: string, opts: PreviewOptions): Promise<void>;
|
package/dist/server/preview.js
CHANGED
|
@@ -41,6 +41,7 @@ const diagnostics_1 = require("../compiler/diagnostics");
|
|
|
41
41
|
const ui_1 = require("../cli/ui");
|
|
42
42
|
const dev_1 = require("./dev");
|
|
43
43
|
const functions_1 = require("./functions");
|
|
44
|
+
const redirects_1 = require("../deploy/redirects");
|
|
44
45
|
const static_1 = require("./static");
|
|
45
46
|
function parseHeadersFile(file) {
|
|
46
47
|
if (!fs.existsSync(file))
|
|
@@ -91,11 +92,17 @@ async function startPreview(outDir, opts) {
|
|
|
91
92
|
const notFound = (0, static_1.fileIfExists)(path.join(outDir, "404.html"));
|
|
92
93
|
return { status: 404, body: notFound ? fs.readFileSync(notFound) : "404 Not Found", type: notFound ? "text/html; charset=utf-8" : "text/plain", headers: headersFor(urlPath) };
|
|
93
94
|
};
|
|
94
|
-
const functions = new functions_1.FunctionsHost(opts.root);
|
|
95
|
+
const functions = new functions_1.FunctionsHost(opts.root, "production");
|
|
95
96
|
const handle = async (req, res) => {
|
|
97
|
+
if (!(0, static_1.hostAllowed)(req.headers.host, opts.allowedHosts === true || [...opts.allowedHosts, opts.host]))
|
|
98
|
+
return (0, static_1.forbiddenHost)(res, req.headers.host);
|
|
96
99
|
const urlPath = (0, static_1.decodePath)(req.url);
|
|
97
100
|
if (!urlPath)
|
|
98
101
|
return (0, static_1.send)(res, 400, "Bad request", "text/plain");
|
|
102
|
+
const sitePath = (0, static_1.stripBase)(urlPath, opts.base);
|
|
103
|
+
const redirect = sitePath === null ? null : (0, redirects_1.matchRedirect)(opts.redirects, sitePath, (req.url ?? "").match(/\?[^#]*/)?.[0]);
|
|
104
|
+
if (redirect)
|
|
105
|
+
return (0, static_1.send)(res, redirect.status, "", "text/plain", { Location: redirect.location });
|
|
99
106
|
const head = req.method === "HEAD";
|
|
100
107
|
if (functions.enabled) {
|
|
101
108
|
const response = await functions.handle(await (0, functions_1.toRequest)(req), async (r) => (0, static_1.assetResponse)(resolve((0, static_1.decodePath)(new URL(r.url).pathname) ?? "/")));
|
package/dist/server/static.d.ts
CHANGED
|
@@ -17,4 +17,13 @@ export declare function sendAsset(res: http.ServerResponse, asset: Asset, head?:
|
|
|
17
17
|
export declare function assetResponse(asset: Asset): Response;
|
|
18
18
|
export declare function routeCandidates(urlPath: string): string[];
|
|
19
19
|
export declare function listen(server: http.Server, port: number, host: string, attempts?: number): Promise<number>;
|
|
20
|
+
/**
|
|
21
|
+
* Guards the local servers against DNS rebinding: a page on another site cannot reach them through a
|
|
22
|
+
* domain it controls. IP addresses, localhost and *.localhost are always allowed; other names (a
|
|
23
|
+
* tunnel, a custom dev domain) need "allowedHosts" in sinth.config.json, or `true` to allow any.
|
|
24
|
+
*/
|
|
25
|
+
export declare function hostAllowed(hostHeader: string | undefined, allowed: string[] | true): boolean;
|
|
26
|
+
export declare function forbiddenHost(res: http.ServerResponse, host: string | undefined): void;
|
|
27
|
+
/** Largest request body the local servers accept (functions/ requests). */
|
|
28
|
+
export declare const MAX_BODY_BYTES: number;
|
|
20
29
|
export declare function displayHost(host: string): string;
|
package/dist/server/static.js
CHANGED
|
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.MIME = void 0;
|
|
36
|
+
exports.MAX_BODY_BYTES = exports.MIME = void 0;
|
|
37
37
|
exports.mimeType = mimeType;
|
|
38
38
|
exports.decodePath = decodePath;
|
|
39
39
|
exports.safeJoin = safeJoin;
|
|
@@ -44,6 +44,8 @@ exports.sendAsset = sendAsset;
|
|
|
44
44
|
exports.assetResponse = assetResponse;
|
|
45
45
|
exports.routeCandidates = routeCandidates;
|
|
46
46
|
exports.listen = listen;
|
|
47
|
+
exports.hostAllowed = hostAllowed;
|
|
48
|
+
exports.forbiddenHost = forbiddenHost;
|
|
47
49
|
exports.displayHost = displayHost;
|
|
48
50
|
const fs = __importStar(require("fs"));
|
|
49
51
|
const path = __importStar(require("path"));
|
|
@@ -156,6 +158,27 @@ function listen(server, port, host, attempts = 10) {
|
|
|
156
158
|
tryListen();
|
|
157
159
|
});
|
|
158
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Guards the local servers against DNS rebinding: a page on another site cannot reach them through a
|
|
163
|
+
* domain it controls. IP addresses, localhost and *.localhost are always allowed; other names (a
|
|
164
|
+
* tunnel, a custom dev domain) need "allowedHosts" in sinth.config.json, or `true` to allow any.
|
|
165
|
+
*/
|
|
166
|
+
function hostAllowed(hostHeader, allowed) {
|
|
167
|
+
if (allowed === true || !hostHeader)
|
|
168
|
+
return true;
|
|
169
|
+
const host = hostHeader.replace(/:\d+$/, "").replace(/^\[|\]$/g, "").toLowerCase();
|
|
170
|
+
if (host === "localhost" || host.endsWith(".localhost") || /^[\d.]+$/.test(host) || host.includes(":"))
|
|
171
|
+
return true;
|
|
172
|
+
return allowed.some(a => {
|
|
173
|
+
const name = a.toLowerCase();
|
|
174
|
+
return name.startsWith(".") ? host === name.slice(1) || host.endsWith(name) : host === name;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
function forbiddenHost(res, host) {
|
|
178
|
+
send(res, 403, `Blocked request for host "${host}". Add it to "allowedHosts" in sinth.config.json to allow it.`, "text/plain; charset=utf-8");
|
|
179
|
+
}
|
|
180
|
+
/** Largest request body the local servers accept (functions/ requests). */
|
|
181
|
+
exports.MAX_BODY_BYTES = 64 * 1024 * 1024;
|
|
159
182
|
function displayHost(host) {
|
|
160
183
|
return host === "127.0.0.1" || host === "::1" || host === "0.0.0.0" || host === "::" ? "localhost" : host;
|
|
161
184
|
}
|