zitejs 0.10.0-start.14 → 0.10.0-start.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/check/index.d.ts +18 -6
- package/dist/cjs/check/index.js +57 -20
- package/dist/cjs/check/start/routeTree.d.ts +8 -0
- package/dist/cjs/check/start/routeTree.js +111 -0
- package/dist/cjs/check/start/sourceRules.d.ts +3 -1
- package/dist/cjs/check/start/sourceRules.js +401 -12
- package/dist/cjs/check/start/tsgo.d.ts +2 -0
- package/dist/cjs/check/start/tsgo.js +35 -0
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/dev/index.js +14 -4
- package/dist/cjs/runtime/index.d.ts +6 -0
- package/dist/cjs/start/entries.d.ts +2 -1
- package/dist/cjs/start/entries.js +4 -1
- package/dist/cjs/sync/index.js +2 -1
- package/dist/cjs/sync/lib.d.ts +10 -3
- package/dist/cjs/sync/lib.js +24 -7
- package/dist/cjs/sync/lib.test.js +16 -0
- package/dist/esm/check/index.d.ts +18 -6
- package/dist/esm/check/index.js +57 -21
- package/dist/esm/check/start/routeTree.d.ts +8 -0
- package/dist/esm/check/start/routeTree.js +70 -0
- package/dist/esm/check/start/sourceRules.d.ts +3 -1
- package/dist/esm/check/start/sourceRules.js +401 -12
- package/dist/esm/check/start/tsgo.d.ts +2 -0
- package/dist/esm/check/start/tsgo.js +29 -0
- package/dist/esm/cli.js +1 -1
- package/dist/esm/dev/index.js +16 -6
- package/dist/esm/runtime/index.d.ts +6 -0
- package/dist/esm/start/entries.d.ts +2 -1
- package/dist/esm/start/entries.js +2 -0
- package/dist/esm/start/version.d.ts +1 -1
- package/dist/esm/start/version.js +1 -1
- package/dist/esm/sync/index.js +2 -1
- package/dist/esm/sync/lib.d.ts +10 -3
- package/dist/esm/sync/lib.js +24 -7
- package/dist/esm/sync/lib.test.js +16 -0
- package/package.json +1 -1
- package/dist/cjs/check/start/describeRules.d.ts +0 -35
- package/dist/cjs/check/start/describeRules.js +0 -89
- package/dist/cjs/start/problems.d.ts +0 -19
- package/dist/cjs/start/problems.js +0 -11
- package/dist/esm/check/start/describeRules.d.ts +0 -35
- package/dist/esm/check/start/describeRules.js +0 -86
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.sourceProblems = sourceProblems;
|
|
4
|
-
// Source hints for a Start app: line-level findings read from the code
|
|
5
|
-
//
|
|
6
|
-
//
|
|
4
|
+
// Source hints for a Start app: line-level findings read from the code. The
|
|
5
|
+
// platform's build describes the app and enforces access; these only say early,
|
|
6
|
+
// and only where the code shows it for sure, what that build would refuse. A
|
|
7
|
+
// file that doesn't parse is skipped; the typecheck reports it.
|
|
7
8
|
const parser_1 = require("@babel/parser");
|
|
8
9
|
const isNode = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
|
|
9
10
|
const SKIP_KEYS = new Set(["loc", "start", "end", "extra", "leadingComments", "trailingComments", "innerComments", "range"]);
|
|
@@ -122,22 +123,23 @@ const parseFile = (path, code) => {
|
|
|
122
123
|
};
|
|
123
124
|
const FACTORIES = ["signedInFn", "publicFn", "createServerFn"];
|
|
124
125
|
/**
|
|
125
|
-
* The names a server function can start from, app-wide
|
|
126
|
-
* TanStack's raw one, and the app's own
|
|
126
|
+
* The names a server function can start from, app-wide, each with the factory
|
|
127
|
+
* it comes from: zitejs's, TanStack's raw one, and the app's own built from them
|
|
127
128
|
* (`export const managerFn = signedInFn.middleware([role("manager")])`).
|
|
128
129
|
*/
|
|
129
130
|
const factoryNames = (files) => {
|
|
130
|
-
const names = new
|
|
131
|
+
const names = new Map(FACTORIES.map((factory) => [factory, factory]));
|
|
131
132
|
for (let changed = true; changed;) {
|
|
132
133
|
changed = false;
|
|
133
134
|
for (const file of files) {
|
|
134
|
-
const
|
|
135
|
+
const factoryOf = (local) => names.get(file.imports.get(local)?.imported ?? local);
|
|
135
136
|
for (const statement of file.program.body) {
|
|
136
137
|
if (statement.type === "ExportNamedDeclaration" && statement.source) {
|
|
137
138
|
for (const specifier of statement.specifiers) {
|
|
138
139
|
const exported = nameOf(specifier.exported);
|
|
139
|
-
|
|
140
|
-
|
|
140
|
+
const factory = names.get(nameOf(specifier.local) ?? "");
|
|
141
|
+
if (exported && factory && !names.has(exported)) {
|
|
142
|
+
names.set(exported, factory);
|
|
141
143
|
changed = true;
|
|
142
144
|
}
|
|
143
145
|
}
|
|
@@ -151,8 +153,9 @@ const factoryNames = (files) => {
|
|
|
151
153
|
if (!name || !init || names.has(name) || isHandlerCall(unwrap(init)))
|
|
152
154
|
continue;
|
|
153
155
|
const { root } = chainOf(init);
|
|
154
|
-
|
|
155
|
-
|
|
156
|
+
const factory = root && factoryOf(root);
|
|
157
|
+
if (factory) {
|
|
158
|
+
names.set(name, factory);
|
|
156
159
|
changed = true;
|
|
157
160
|
}
|
|
158
161
|
}
|
|
@@ -161,6 +164,23 @@ const factoryNames = (files) => {
|
|
|
161
164
|
}
|
|
162
165
|
return names;
|
|
163
166
|
};
|
|
167
|
+
const OWNER_INPUTS = ["userId", "ownerId"];
|
|
168
|
+
/** An owner id a function's validator takes as input: `.validator(z.object({ userId: … }))`. */
|
|
169
|
+
const ownerInput = (calls) => {
|
|
170
|
+
for (const call of calls) {
|
|
171
|
+
const callee = call.callee;
|
|
172
|
+
if (callee.type !== "MemberExpression" || !["validator", "inputValidator"].includes(nameOf(callee.property) ?? ""))
|
|
173
|
+
continue;
|
|
174
|
+
const schema = call.arguments[0];
|
|
175
|
+
if (schema?.type !== "CallExpression" || nameOf(schema.callee.property) !== "object")
|
|
176
|
+
continue;
|
|
177
|
+
const shape = schema.arguments[0];
|
|
178
|
+
const owner = shape?.type === "ObjectExpression" ? OWNER_INPUTS.find((key) => propertyNamed(shape, key)) : undefined;
|
|
179
|
+
if (owner)
|
|
180
|
+
return owner;
|
|
181
|
+
}
|
|
182
|
+
return undefined;
|
|
183
|
+
};
|
|
164
184
|
const callsCreateWorkflow = (program) => {
|
|
165
185
|
let found = false;
|
|
166
186
|
walk(program, (node) => {
|
|
@@ -216,8 +236,362 @@ const GUARD_TYPES = new Set(["IfStatement", "ConditionalExpression", "LogicalExp
|
|
|
216
236
|
const guardedByTypeofWindow = (ancestors) => ancestors.some((a) => GUARD_TYPES.has(a.type) && containsTypeofWindow((a.test ?? a.left)));
|
|
217
237
|
const isRelativePath = (node) => (node?.type === "StringLiteral" && node.value.startsWith("/")) ||
|
|
218
238
|
(node?.type === "TemplateLiteral" && (node.quasis[0]?.value?.cooked ?? "").startsWith("/"));
|
|
239
|
+
/** A top-level `const` in the file, so `{ server: serverOptions }` reads as written. */
|
|
240
|
+
const resolveConst = (file, node) => {
|
|
241
|
+
if (!node)
|
|
242
|
+
return undefined;
|
|
243
|
+
const value = unwrap(node);
|
|
244
|
+
if (value.type !== "Identifier")
|
|
245
|
+
return value;
|
|
246
|
+
for (const statement of file.program.body) {
|
|
247
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
248
|
+
if (declaration?.type !== "VariableDeclaration" || declaration.kind !== "const")
|
|
249
|
+
continue;
|
|
250
|
+
const declarator = declaration.declarations.find((d) => nameOf(d.id) === value.name);
|
|
251
|
+
if (declarator?.init)
|
|
252
|
+
return unwrap(declarator.init);
|
|
253
|
+
}
|
|
254
|
+
return undefined;
|
|
255
|
+
};
|
|
256
|
+
/** An object's property, read through a top-level const; undefined when absent, null when there but unreadable. */
|
|
257
|
+
const readProperty = (file, object, name) => {
|
|
258
|
+
if (object.properties.some((p) => p.type === "SpreadElement"))
|
|
259
|
+
return null;
|
|
260
|
+
const property = propertyNamed(object, name);
|
|
261
|
+
if (!property)
|
|
262
|
+
return undefined;
|
|
263
|
+
return (property.type === "ObjectMethod" ? property : resolveConst(file, property.value)) ?? null;
|
|
264
|
+
};
|
|
265
|
+
// The platform's, as describe's rule had them: the host, TanStack and Cloudflare answer these before the app does.
|
|
266
|
+
const RESERVED_PATHS = [
|
|
267
|
+
/^\/_zite/,
|
|
268
|
+
/^\/__zite/,
|
|
269
|
+
/^\/_serverFn(\/|$)/,
|
|
270
|
+
/^\/cdn-cgi(\/|$)/,
|
|
271
|
+
/^\/auth(\/|$)/,
|
|
272
|
+
/^\/api\/auth(\/|$)/,
|
|
273
|
+
/^\/manifest\.webmanifest$/,
|
|
274
|
+
];
|
|
275
|
+
/** What one middleware grants, as zitejs/start names it: `role()` signs in. Undefined when it isn't one of those. */
|
|
276
|
+
const accessOf = (file, node) => {
|
|
277
|
+
const value = unwrap(node);
|
|
278
|
+
const fromZitejs = (local) => {
|
|
279
|
+
const imported = local ? file.imports.get(local) : undefined;
|
|
280
|
+
return imported?.source === "zitejs/start" ? imported.imported : undefined;
|
|
281
|
+
};
|
|
282
|
+
const name = value.type === "Identifier"
|
|
283
|
+
? fromZitejs(value.name)
|
|
284
|
+
: value.type === "MemberExpression"
|
|
285
|
+
? fromZitejs(nameOf(value.object))
|
|
286
|
+
: value.type === "CallExpression"
|
|
287
|
+
? fromZitejs(nameOf(value.callee))
|
|
288
|
+
: undefined;
|
|
289
|
+
if (name === "role")
|
|
290
|
+
return value.type === "CallExpression" ? "signedIn" : undefined;
|
|
291
|
+
if (value.type === "MemberExpression")
|
|
292
|
+
return name === "webhook" ? "webhook" : undefined;
|
|
293
|
+
return name === "signedIn" || name === "anyone" || name === "webhook" ? name : undefined;
|
|
294
|
+
};
|
|
295
|
+
/** A middleware list's access, and whether anything in it is the app's own, which only the build can read. */
|
|
296
|
+
const middlewareAccess = (file, node) => {
|
|
297
|
+
if (node === undefined)
|
|
298
|
+
return { access: [], unread: false };
|
|
299
|
+
const list = node && resolveConst(file, node);
|
|
300
|
+
if (list?.type !== "ArrayExpression")
|
|
301
|
+
return { access: [], unread: true };
|
|
302
|
+
const access = [];
|
|
303
|
+
let unread = false;
|
|
304
|
+
for (const element of list.elements) {
|
|
305
|
+
const granted = element && element.type !== "SpreadElement" ? accessOf(file, element) : undefined;
|
|
306
|
+
if (granted)
|
|
307
|
+
access.push(granted);
|
|
308
|
+
else
|
|
309
|
+
unread = true;
|
|
310
|
+
}
|
|
311
|
+
return { access, unread };
|
|
312
|
+
};
|
|
313
|
+
/** `createFileRoute('/id')({ … })` in each route file; the root route as id "". */
|
|
314
|
+
const fileRoutes = (files) => files.flatMap((file) => {
|
|
315
|
+
if (!file.path.startsWith("src/routes/"))
|
|
316
|
+
return [];
|
|
317
|
+
const found = [];
|
|
318
|
+
walk(file.program, (node) => {
|
|
319
|
+
if (node.type !== "CallExpression" || !isNode(node.callee) || node.callee.type !== "CallExpression")
|
|
320
|
+
return;
|
|
321
|
+
const factory = nameOf(node.callee.callee);
|
|
322
|
+
const literal = node.callee.arguments[0];
|
|
323
|
+
const id = factory === "createFileRoute" && literal?.type === "StringLiteral"
|
|
324
|
+
? literal.value
|
|
325
|
+
: factory === "createRootRoute" || factory === "createRootRouteWithContext"
|
|
326
|
+
? ""
|
|
327
|
+
: undefined;
|
|
328
|
+
if (id === undefined)
|
|
329
|
+
return;
|
|
330
|
+
const options = resolveConst(file, node.arguments[0]);
|
|
331
|
+
found.push({ file, id, node, ...(options?.type === "ObjectExpression" && { options }) });
|
|
332
|
+
});
|
|
333
|
+
return found;
|
|
334
|
+
});
|
|
335
|
+
const segments = (id) => id.split("/").filter(Boolean);
|
|
336
|
+
const isAncestor = (ancestor, id) => {
|
|
337
|
+
const outer = segments(ancestor);
|
|
338
|
+
const inner = segments(id);
|
|
339
|
+
return outer.length < inner.length && outer.every((segment, i) => segment === inner[i]);
|
|
340
|
+
};
|
|
341
|
+
/** As TanStack shows a route's path: no pathless layouts or groups. */
|
|
342
|
+
const fullPathOf = (id) => {
|
|
343
|
+
const path = `/${segments(id).filter((s) => !s.startsWith("_") && !/^\(.*\)$/.test(s)).join("/")}`;
|
|
344
|
+
return id.endsWith("/") && path !== "/" ? `${path}/` : path;
|
|
345
|
+
};
|
|
346
|
+
/** A server route's methods, each with its own middleware; null when the handlers can't be read from source. */
|
|
347
|
+
const routeMethods = (file, handlers) => {
|
|
348
|
+
let object = handlers;
|
|
349
|
+
if (FUNCTION_TYPES.has(handlers.type)) {
|
|
350
|
+
// `handlers: ({ createHandlers }) => createHandlers({ … })`
|
|
351
|
+
const body = handlers.body;
|
|
352
|
+
const call = body.type === "CallExpression" ? body : undefined;
|
|
353
|
+
object = call && nameOf(call.callee) === "createHandlers" ? resolveConst(file, call.arguments[0]) : undefined;
|
|
354
|
+
}
|
|
355
|
+
if (object?.type !== "ObjectExpression")
|
|
356
|
+
return null;
|
|
357
|
+
const methods = [];
|
|
358
|
+
for (const property of object.properties) {
|
|
359
|
+
const method = property.type === "SpreadElement" ? undefined : nameOf(property.key);
|
|
360
|
+
if (!method)
|
|
361
|
+
return null;
|
|
362
|
+
const value = property.type === "ObjectMethod" ? property : resolveConst(file, property.value);
|
|
363
|
+
if (!value)
|
|
364
|
+
return null;
|
|
365
|
+
methods.push({ method, middleware: value.type === "ObjectExpression" ? readProperty(file, value, "middleware") : undefined });
|
|
366
|
+
}
|
|
367
|
+
return methods;
|
|
368
|
+
};
|
|
369
|
+
/** What the build's describe refuses or warns about a route, where the source shows it for sure. */
|
|
370
|
+
const routeProblems = (files, internal) => {
|
|
371
|
+
const problems = [];
|
|
372
|
+
const routes = fileRoutes(files);
|
|
373
|
+
const report = (rule, severity, route, message) => problems.push({ rule, severity, file: route.file.path, ...(lineOf(route.node) && { line: lineOf(route.node) }), message });
|
|
374
|
+
for (const route of routes) {
|
|
375
|
+
const path = fullPathOf(route.id);
|
|
376
|
+
if (route.id && RESERVED_PATHS.some((reserved) => reserved.test(path))) {
|
|
377
|
+
report("reserved-path", "error", route, `${path} is the platform's; give the route another path`);
|
|
378
|
+
}
|
|
379
|
+
const server = route.options && readProperty(route.file, route.options, "server");
|
|
380
|
+
if (!server || server.type !== "ObjectExpression")
|
|
381
|
+
continue;
|
|
382
|
+
const handlers = readProperty(route.file, server, "handlers");
|
|
383
|
+
if (!handlers)
|
|
384
|
+
continue;
|
|
385
|
+
const methods = routeMethods(route.file, handlers);
|
|
386
|
+
if (!methods)
|
|
387
|
+
continue;
|
|
388
|
+
const subject = `route ${path}`;
|
|
389
|
+
const staticData = readProperty(route.file, route.options, "staticData");
|
|
390
|
+
const description = staticData?.type === "ObjectExpression" ? readProperty(route.file, staticData, "description") : staticData;
|
|
391
|
+
if (description === undefined) {
|
|
392
|
+
report("description-missing", "warning", route, `${subject} has no description; add staticData: { description } for the editor`);
|
|
393
|
+
}
|
|
394
|
+
// Middleware applies from the root down: every route this one sits under, then its own, then the method's.
|
|
395
|
+
const inherited = routes
|
|
396
|
+
.filter((other) => other !== route && (other.id === "" || isAncestor(other.id, route.id)))
|
|
397
|
+
.map((other) => {
|
|
398
|
+
const otherServer = other.options ? readProperty(other.file, other.options, "server") : undefined;
|
|
399
|
+
if (otherServer === undefined)
|
|
400
|
+
return { access: [], unread: !other.options };
|
|
401
|
+
if (otherServer?.type !== "ObjectExpression")
|
|
402
|
+
return { access: [], unread: true };
|
|
403
|
+
return middlewareAccess(other.file, readProperty(other.file, otherServer, "middleware"));
|
|
404
|
+
});
|
|
405
|
+
const own = middlewareAccess(route.file, readProperty(route.file, server, "middleware"));
|
|
406
|
+
const publicMethods = [];
|
|
407
|
+
for (const { method, middleware } of methods) {
|
|
408
|
+
const chain = [...inherited, own, middlewareAccess(route.file, middleware)];
|
|
409
|
+
const unread = chain.some((link) => link.unread);
|
|
410
|
+
const access = [...new Set(chain.flatMap((link) => link.access))];
|
|
411
|
+
if (access.length === 0) {
|
|
412
|
+
if (!unread)
|
|
413
|
+
report("access-missing", "error", route, `${subject} ${method}: no signedIn, anyone or webhook middleware in its chain`);
|
|
414
|
+
}
|
|
415
|
+
else if (access.length > 1) {
|
|
416
|
+
report("access-conflict", "error", route, `${subject} ${method}: ${access.join(" + ")}`);
|
|
417
|
+
}
|
|
418
|
+
else if (access[0] === "anyone" && !unread) {
|
|
419
|
+
publicMethods.push(method);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
if (internal && publicMethods.length > 0) {
|
|
423
|
+
report("public-in-internal", "error", route, `${subject} lets anyone call ${publicMethods.join(", ")}, and this internal app signs everyone in; use signedIn`);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return problems;
|
|
427
|
+
};
|
|
428
|
+
/** The workflows an app exports, by id, as the build loads them: every module that calls createWorkflow. */
|
|
429
|
+
const workflowProblems = (files) => {
|
|
430
|
+
const problems = [];
|
|
431
|
+
const exported = [];
|
|
432
|
+
for (const file of files) {
|
|
433
|
+
if (!callsCreateWorkflow(file.program))
|
|
434
|
+
continue;
|
|
435
|
+
const createCall = (init) => {
|
|
436
|
+
if (!init)
|
|
437
|
+
return undefined;
|
|
438
|
+
const { root, calls } = chainOf(init);
|
|
439
|
+
const innermost = calls.at(-1);
|
|
440
|
+
return root && file.imports.get(root)?.imported === "createWorkflow" && innermost && nameOf(innermost.callee) === root
|
|
441
|
+
? innermost
|
|
442
|
+
: undefined;
|
|
443
|
+
};
|
|
444
|
+
const workflow = (id, node, call) => {
|
|
445
|
+
exported.push({ id, file: file.path, node });
|
|
446
|
+
const config = resolveConst(file, call.arguments[0]);
|
|
447
|
+
const trigger = config?.type === "ObjectExpression" ? readProperty(file, config, "trigger") : undefined;
|
|
448
|
+
if (trigger?.type !== "ObjectExpression")
|
|
449
|
+
return;
|
|
450
|
+
if (["every", "events", "subscribe"].some((key) => readProperty(file, trigger, key) !== undefined))
|
|
451
|
+
return;
|
|
452
|
+
problems.push({
|
|
453
|
+
rule: "workflow-trigger-unregistered",
|
|
454
|
+
severity: "error",
|
|
455
|
+
file: file.path,
|
|
456
|
+
...(lineOf(node) && { line: lineOf(node) }),
|
|
457
|
+
message: `workflow ${id}: a provider trigger needs \`subscribe\` to register it with the provider`,
|
|
458
|
+
});
|
|
459
|
+
};
|
|
460
|
+
const locals = new Map();
|
|
461
|
+
for (const statement of file.program.body) {
|
|
462
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
463
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
464
|
+
for (const declarator of declaration.declarations) {
|
|
465
|
+
const name = nameOf(declarator.id);
|
|
466
|
+
const call = createCall(declarator.init);
|
|
467
|
+
if (!name || !call)
|
|
468
|
+
continue;
|
|
469
|
+
if (statement.type === "ExportNamedDeclaration")
|
|
470
|
+
workflow(name, declarator, call);
|
|
471
|
+
else
|
|
472
|
+
locals.set(name, { node: declarator, call });
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (statement.type === "ExportNamedDeclaration" && !statement.source) {
|
|
476
|
+
for (const specifier of statement.specifiers) {
|
|
477
|
+
const local = locals.get(nameOf(specifier.local) ?? "");
|
|
478
|
+
const id = nameOf(specifier.exported);
|
|
479
|
+
if (local && id)
|
|
480
|
+
workflow(id, specifier, local.call);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (statement.type === "ExportDefaultDeclaration") {
|
|
484
|
+
const declared = statement.declaration;
|
|
485
|
+
const call = createCall(declared) ?? (declared.type === "Identifier" ? locals.get(declared.name)?.call : undefined);
|
|
486
|
+
if (call) {
|
|
487
|
+
problems.push({
|
|
488
|
+
rule: "workflow-default-export",
|
|
489
|
+
severity: "error",
|
|
490
|
+
file: file.path,
|
|
491
|
+
...(lineOf(statement) && { line: lineOf(statement) }),
|
|
492
|
+
message: "workflow default: export it by name, which becomes its id",
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
for (const { id, file, node } of exported) {
|
|
499
|
+
const elsewhere = exported.find((other) => other.id === id && other.file !== file);
|
|
500
|
+
if (elsewhere) {
|
|
501
|
+
problems.push({
|
|
502
|
+
rule: "duplicate-workflow",
|
|
503
|
+
severity: "error",
|
|
504
|
+
file,
|
|
505
|
+
...(lineOf(node) && { line: lineOf(node) }),
|
|
506
|
+
message: `workflow ${id}: ${id} is also exported from ${elsewhere.file}`,
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return problems;
|
|
511
|
+
};
|
|
512
|
+
// What the build keeps out of the browser (zite()'s importProtection), by the specifier an app writes.
|
|
513
|
+
const SERVER_ONLY_IMPORTS = [/^zitejs\/db$/, /^zitejs\/integrations(\/|$)/, /^zitejs\/email$/, /(^|\/)\.zite\/db(\.ts)?$/, /(^|\/)\.zite\/integrations(\/|$)/];
|
|
514
|
+
const isServerOnlyFile = (path) => /\.server(\.[^/]*)?$/.test(path.split("/").pop() ?? "");
|
|
515
|
+
const SOURCE_EXTENSIONS = ["", ".ts", ".tsx", ".js", ".jsx", ".mts", ".mjs", "/index.ts", "/index.tsx", "/index.js"];
|
|
516
|
+
/** The app file an import names, by relative path or the `~/` and `@/` aliases for src/. */
|
|
517
|
+
const resolveImport = (from, source, paths) => {
|
|
518
|
+
const base = /^[~@]\//.test(source)
|
|
519
|
+
? `src/${source.slice(2)}`
|
|
520
|
+
: source.startsWith(".")
|
|
521
|
+
? posixJoin(from.split("/").slice(0, -1).join("/"), source)
|
|
522
|
+
: undefined;
|
|
523
|
+
return base === undefined ? undefined : SOURCE_EXTENSIONS.map((extension) => base + extension).find((path) => paths.has(path));
|
|
524
|
+
};
|
|
525
|
+
const posixJoin = (dir, relative) => {
|
|
526
|
+
const parts = dir ? dir.split("/") : [];
|
|
527
|
+
for (const part of relative.split("/")) {
|
|
528
|
+
if (part === "..")
|
|
529
|
+
parts.pop();
|
|
530
|
+
else if (part !== "." && part !== "")
|
|
531
|
+
parts.push(part);
|
|
532
|
+
}
|
|
533
|
+
return parts.join("/");
|
|
534
|
+
};
|
|
535
|
+
/** Whether a file defines server code the build compiles out of the browser's copy, which only the build can follow. */
|
|
536
|
+
const definesServerCode = (file, factories) => {
|
|
537
|
+
let found = false;
|
|
538
|
+
walk(file.program, (node) => {
|
|
539
|
+
if (found)
|
|
540
|
+
return;
|
|
541
|
+
if (node.type === "CallExpression" && isHandlerCall(node)) {
|
|
542
|
+
const { root } = chainOf(node);
|
|
543
|
+
found = !!root && factories.has(file.imports.get(root)?.imported ?? root);
|
|
544
|
+
}
|
|
545
|
+
if ((node.type === "ObjectProperty" || node.type === "ObjectMethod") && nameOf(node.key) === "server")
|
|
546
|
+
found = true;
|
|
547
|
+
});
|
|
548
|
+
return found;
|
|
549
|
+
};
|
|
550
|
+
/**
|
|
551
|
+
* Page code that imports server-only modules, which the build refuses: followed
|
|
552
|
+
* from the routes through plain modules, never into a *.functions.* file, whose
|
|
553
|
+
* handlers the browser never gets, nor into or out of a file that defines
|
|
554
|
+
* server code itself.
|
|
555
|
+
*/
|
|
556
|
+
const serverImportProblems = (files, factories) => {
|
|
557
|
+
const byPath = new Map(files.map((file) => [file.path, file]));
|
|
558
|
+
const paths = new Set(byPath.keys());
|
|
559
|
+
const problems = [];
|
|
560
|
+
const pageRoots = files.filter((file) => file.path === "src/router.tsx" || (file.path.startsWith("src/routes/") && !file.path.startsWith("src/routes/api/")));
|
|
561
|
+
const seen = new Set();
|
|
562
|
+
const visit = (file) => {
|
|
563
|
+
if (seen.has(file.path))
|
|
564
|
+
return;
|
|
565
|
+
seen.add(file.path);
|
|
566
|
+
if (definesServerCode(file, factories))
|
|
567
|
+
return;
|
|
568
|
+
for (const statement of file.program.body) {
|
|
569
|
+
if (statement.type !== "ImportDeclaration")
|
|
570
|
+
continue;
|
|
571
|
+
const values = valueImports(statement);
|
|
572
|
+
if (values !== "all" && values.length === 0)
|
|
573
|
+
continue;
|
|
574
|
+
const source = statement.source.value;
|
|
575
|
+
const target = resolveImport(file.path, source, paths);
|
|
576
|
+
if (SERVER_ONLY_IMPORTS.some((pattern) => pattern.test(source)) || isServerOnlyFile(target ?? source)) {
|
|
577
|
+
problems.push({
|
|
578
|
+
rule: "server-import",
|
|
579
|
+
severity: "error",
|
|
580
|
+
file: file.path,
|
|
581
|
+
...(lineOf(statement) && { line: lineOf(statement) }),
|
|
582
|
+
message: `${source} runs only on the server, so page code can't import it; call a server function that uses it instead`,
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
else if (target && !/\.functions(\.[^/]*)?$/.test(target)) {
|
|
586
|
+
visit(byPath.get(target));
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
pageRoots.forEach(visit);
|
|
591
|
+
return problems;
|
|
592
|
+
};
|
|
219
593
|
/** Every source hint for one Start app, given its files by app-relative path. */
|
|
220
|
-
function sourceProblems(sources) {
|
|
594
|
+
function sourceProblems(sources, { internal = false } = {}) {
|
|
221
595
|
const files = Object.entries(sources)
|
|
222
596
|
.filter(([path]) => /\.[cm]?[jt]sx?$/.test(path) && !path.endsWith("routeTree.gen.ts") && !path.endsWith(".d.ts"))
|
|
223
597
|
.flatMap(([path, code]) => parseFile(path, code) ?? []);
|
|
@@ -308,6 +682,20 @@ function sourceProblems(sources) {
|
|
|
308
682
|
if (!hasMethodOption(calls)) {
|
|
309
683
|
report("method-missing", "error", path, node, `call ${root} with { method: 'GET' | 'POST', description } before .handler()`);
|
|
310
684
|
}
|
|
685
|
+
const factory = factories.get(file.imports.get(root)?.imported ?? root);
|
|
686
|
+
const subject = `function ${name ?? "this function"}`;
|
|
687
|
+
if (factory === "createServerFn") {
|
|
688
|
+
report("raw-server-fn", "error", path, node, `${subject}: define it with signedInFn or publicFn from zitejs/start`);
|
|
689
|
+
}
|
|
690
|
+
else if (factory === "publicFn" && internal) {
|
|
691
|
+
report("public-in-internal", "error", path, node, `${subject} is a publicFn, and this internal app signs everyone in; make it a signedInFn`);
|
|
692
|
+
}
|
|
693
|
+
else if (factory === "signedInFn") {
|
|
694
|
+
const owner = ownerInput(calls);
|
|
695
|
+
if (owner) {
|
|
696
|
+
report("owner-from-input", "warning", path, node, `${subject} takes ${owner} as input, which any caller can set; use context.user.id`);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
311
699
|
if (file.imports.get(root)?.imported === "publicFn" || root === "publicFn") {
|
|
312
700
|
const handler = node.arguments[0];
|
|
313
701
|
if (handler && FUNCTION_TYPES.has(handler.type)) {
|
|
@@ -426,6 +814,7 @@ function sourceProblems(sources) {
|
|
|
426
814
|
}
|
|
427
815
|
});
|
|
428
816
|
}
|
|
817
|
+
problems.push(...routeProblems(files, internal), ...workflowProblems(files), ...serverImportProblems(files, factories));
|
|
429
818
|
for (const [name, paths] of serverFnFiles) {
|
|
430
819
|
const distinct = [...new Set(paths)];
|
|
431
820
|
if (distinct.length > 1) {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.findTsgo = findTsgo;
|
|
7
|
+
// TypeScript's native compiler, as the platform's checks run it, found the same
|
|
8
|
+
// way every time: never from PATH, so a check here and one in the sandbox use
|
|
9
|
+
// the same compiler.
|
|
10
|
+
const node_fs_1 = require("node:fs");
|
|
11
|
+
const node_module_1 = require("node:module");
|
|
12
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
13
|
+
const PACKAGE = "@typescript/native-preview";
|
|
14
|
+
// Where `npm install -g` put it for the node running this: the sandbox image's.
|
|
15
|
+
const globalRoot = () => process.env.npm_config_prefix
|
|
16
|
+
? node_path_1.default.join(process.env.npm_config_prefix, process.platform === "win32" ? "node_modules" : "lib/node_modules")
|
|
17
|
+
: process.platform === "win32"
|
|
18
|
+
? node_path_1.default.join(node_path_1.default.dirname(process.execPath), "node_modules")
|
|
19
|
+
: node_path_1.default.join(node_path_1.default.dirname(node_path_1.default.dirname(process.execPath)), "lib", "node_modules");
|
|
20
|
+
/** tsgo's launcher: the app's own, walking up from it as Node does, else the global install beside this node. */
|
|
21
|
+
function findTsgo(appDir) {
|
|
22
|
+
let manifest;
|
|
23
|
+
try {
|
|
24
|
+
manifest = (0, node_module_1.createRequire)(node_path_1.default.join(node_path_1.default.resolve(appDir), "package.json")).resolve(`${PACKAGE}/package.json`);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
const global = node_path_1.default.join(globalRoot(), PACKAGE, "package.json");
|
|
28
|
+
manifest = (0, node_fs_1.existsSync)(global) ? global : undefined;
|
|
29
|
+
}
|
|
30
|
+
if (!manifest)
|
|
31
|
+
return undefined;
|
|
32
|
+
const { bin } = JSON.parse((0, node_fs_1.readFileSync)(manifest, "utf-8"));
|
|
33
|
+
const launcher = typeof bin === "string" ? bin : bin?.tsgo;
|
|
34
|
+
return launcher ? node_path_1.default.join(node_path_1.default.dirname(manifest), launcher) : undefined;
|
|
35
|
+
}
|
package/dist/cjs/cli.js
CHANGED
|
@@ -80,7 +80,7 @@ async function main() {
|
|
|
80
80
|
console.error(' sync Generate .zite/db.ts from database schema (single-app)');
|
|
81
81
|
console.error(' dev Run sync then watch for changes (like npx convex dev)');
|
|
82
82
|
console.error(' generate Regenerate every app and automation .zite/ (monorepo)');
|
|
83
|
-
console.error(' check Typecheck and build every app and automation; a Start app is
|
|
83
|
+
console.error(' check Typecheck and build every app and automation; a Start app is typechecked with tsgo against its regenerated route tree and its source checked by rule (--json for a report)');
|
|
84
84
|
console.error(' bundle Bundle endpoints (src/api/*.ts, or an automation\'s src/index.ts) for cloudflare-lambda');
|
|
85
85
|
console.error(' describe Print what a built TanStack Start app declares: functions, routes, access');
|
|
86
86
|
process.exit(1);
|
package/dist/cjs/dev/index.js
CHANGED
|
@@ -97,6 +97,7 @@ function getDeclaredEnvVarNames(appDir) {
|
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
function regenerateAppTypedWrappers(appDir) {
|
|
100
|
+
const runtime = (0, entries_js_1.isStartAppDir)(appDir.path) ? "start" : "classic";
|
|
100
101
|
const outDir = (0, path_1.join)(appDir.path, ".zite");
|
|
101
102
|
(0, fs_2.mkdirSync)(outDir, { recursive: true });
|
|
102
103
|
// user.ts and auth.ts no longer generated — User type is fixed in zitejs/auth
|
|
@@ -107,9 +108,16 @@ function regenerateAppTypedWrappers(appDir) {
|
|
|
107
108
|
if (emailIntegrationId) {
|
|
108
109
|
const intDir = (0, path_1.join)(outDir, "integrations");
|
|
109
110
|
(0, fs_2.mkdirSync)(intDir, { recursive: true });
|
|
110
|
-
(0, fs_2.writeFileSync)((0, path_1.join)(intDir, "email.ts"), (0, lib_js_1.generateEmailSdk)(emailIntegrationId));
|
|
111
|
+
(0, fs_2.writeFileSync)((0, path_1.join)(intDir, "email.ts"), (0, lib_js_1.generateEmailSdk)(emailIntegrationId, runtime));
|
|
111
112
|
}
|
|
112
|
-
|
|
113
|
+
// A Start app's `zitejs/backend` is the package itself. This classic wrapper
|
|
114
|
+
// would only sit in its program with a second, id-only `hasRole`.
|
|
115
|
+
const backendPath = (0, path_1.join)(outDir, "backend.ts");
|
|
116
|
+
if (runtime === "start") {
|
|
117
|
+
(0, fs_2.rmSync)(backendPath, { force: true });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
(0, fs_2.writeFileSync)(backendPath, (0, lib_js_1.generateBackendWrapperTs)(getDeclaredEnvVarNames(appDir)));
|
|
113
121
|
}
|
|
114
122
|
function regenerateAppApiTs(appDir) {
|
|
115
123
|
const apiDir = (0, path_1.join)(appDir.path, "src", "api");
|
|
@@ -158,7 +166,7 @@ function regenerateAppAirtableSdk(appDir) {
|
|
|
158
166
|
integrationId,
|
|
159
167
|
tables: int.idMappings.tables,
|
|
160
168
|
};
|
|
161
|
-
const content = (0, lib_js_1.generateAirtableTs)(airtableLock);
|
|
169
|
+
const content = (0, lib_js_1.generateAirtableTs)(airtableLock, (0, entries_js_1.isStartAppDir)(appDir.path) ? "start" : "classic");
|
|
162
170
|
if (content) {
|
|
163
171
|
const outDir = (0, path_1.join)(appDir.path, ".zite", "integrations");
|
|
164
172
|
(0, fs_2.mkdirSync)(outDir, { recursive: true });
|
|
@@ -190,7 +198,9 @@ async function runGenerate() {
|
|
|
190
198
|
try {
|
|
191
199
|
if ((0, fs_2.existsSync)("zite.schema.json")) {
|
|
192
200
|
const schema = JSON.parse((0, fs_2.readFileSync)("zite.schema.json", "utf-8"));
|
|
193
|
-
const dbTs = (0, lib_js_1.generateDbTs)(schema
|
|
201
|
+
const dbTs = (0, lib_js_1.generateDbTs)(schema, {
|
|
202
|
+
hasStartApps: (0, entries_js_1.workspaceHasStartApps)(),
|
|
203
|
+
});
|
|
194
204
|
(0, fs_2.mkdirSync)(".zite", { recursive: true });
|
|
195
205
|
(0, fs_2.writeFileSync)((0, path_1.join)(".zite", "db.ts"), dbTs);
|
|
196
206
|
console.log("Wrote .zite/db.ts");
|
|
@@ -272,6 +272,12 @@ export type EmailBlock = {
|
|
|
272
272
|
label: string;
|
|
273
273
|
href: string;
|
|
274
274
|
alignment?: "left" | "center" | "right";
|
|
275
|
+
/** A hex or `rgb()` color; defaults to the platform's button color. */
|
|
276
|
+
backgroundColor?: string;
|
|
277
|
+
/** A hex or `rgb()` color; defaults to white. */
|
|
278
|
+
textColor?: string;
|
|
279
|
+
/** Corner radius in pixels; defaults to 6. */
|
|
280
|
+
borderRadius?: number;
|
|
275
281
|
} | {
|
|
276
282
|
type: "image";
|
|
277
283
|
src: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type SourceDir } from "../sourceRoots.js";
|
|
2
2
|
export declare const START_SERVER_ENTRY = ".zite/server.ts";
|
|
3
3
|
export declare const START_INSTANCE_ENTRY = ".zite/start.ts";
|
|
4
4
|
export type StartTarget = "cloudflare" | "node";
|
|
@@ -13,6 +13,7 @@ export declare const hasAppStart: (appDir: string) => boolean;
|
|
|
13
13
|
/** Writes `file` under `appDir` when its content differs. Returns whether it wrote. */
|
|
14
14
|
export declare const writeIfChanged: (appDir: string, file: string, content: string) => boolean;
|
|
15
15
|
export declare const isStartAppDir: (appPath: string) => boolean;
|
|
16
|
+
export declare const workspaceHasStartApps: () => boolean;
|
|
16
17
|
export declare function regenerateStartEntries(app: SourceDir): void;
|
|
17
18
|
/** App modules that mention createWorkflow: what the workflow registry imports. Describe decides what each one really exports. */
|
|
18
19
|
export declare function workflowFiles(appDir: string): string[];
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.renderWorkflowModules = exports.isStartAppDir = exports.writeIfChanged = exports.hasAppStart = exports.renderStartInstance = exports.renderStartServerEntry = exports.START_INSTANCE_ENTRY = exports.START_SERVER_ENTRY = void 0;
|
|
3
|
+
exports.renderWorkflowModules = exports.workspaceHasStartApps = exports.isStartAppDir = exports.writeIfChanged = exports.hasAppStart = exports.renderStartInstance = exports.renderStartServerEntry = exports.START_INSTANCE_ENTRY = exports.START_SERVER_ENTRY = void 0;
|
|
4
4
|
exports.regenerateStartEntries = regenerateStartEntries;
|
|
5
5
|
exports.workflowFiles = workflowFiles;
|
|
6
6
|
const node_fs_1 = require("node:fs");
|
|
7
7
|
const node_path_1 = require("node:path");
|
|
8
|
+
const sourceRoots_js_1 = require("../sourceRoots.js");
|
|
8
9
|
exports.START_SERVER_ENTRY = ".zite/server.ts";
|
|
9
10
|
exports.START_INSTANCE_ENTRY = ".zite/start.ts";
|
|
10
11
|
const APP_START_FILES = ["src/start.ts", "src/start.tsx"];
|
|
@@ -66,6 +67,8 @@ const isStartAppDir = (appPath) => {
|
|
|
66
67
|
};
|
|
67
68
|
exports.isStartAppDir = isStartAppDir;
|
|
68
69
|
const isStartApp = (app) => (0, exports.isStartAppDir)(app.path);
|
|
70
|
+
const workspaceHasStartApps = () => (0, sourceRoots_js_1.listSourceDirs)().some(isStartApp);
|
|
71
|
+
exports.workspaceHasStartApps = workspaceHasStartApps;
|
|
69
72
|
function regenerateStartEntries(app) {
|
|
70
73
|
if (!isStartApp(app))
|
|
71
74
|
return;
|
package/dist/cjs/sync/index.js
CHANGED
|
@@ -21,6 +21,7 @@ __exportStar(require("./lib.js"), exports);
|
|
|
21
21
|
const fs_1 = require("fs");
|
|
22
22
|
const path_1 = require("path");
|
|
23
23
|
const lib_js_1 = require("./lib.js");
|
|
24
|
+
const entries_js_1 = require("../start/entries.js");
|
|
24
25
|
const DB_ENVIRONMENTS = {
|
|
25
26
|
production: "https://tables.fillout.com/api/v1",
|
|
26
27
|
staging: "https://tables.filloutstaging.com/api/v1",
|
|
@@ -74,7 +75,7 @@ async function runSync() {
|
|
|
74
75
|
const schema = (0, lib_js_1.generateSchema)(database, existingSchema);
|
|
75
76
|
(0, fs_1.writeFileSync)("zite.schema.json", JSON.stringify(schema, null, 2));
|
|
76
77
|
console.log("Wrote zite.schema.json");
|
|
77
|
-
const dbTs = (0, lib_js_1.generateDbTs)(schema);
|
|
78
|
+
const dbTs = (0, lib_js_1.generateDbTs)(schema, { hasStartApps: (0, entries_js_1.workspaceHasStartApps)() });
|
|
78
79
|
const dotZite = ".zite";
|
|
79
80
|
(0, fs_1.mkdirSync)(dotZite, { recursive: true });
|
|
80
81
|
(0, fs_1.writeFileSync)((0, path_1.join)(dotZite, "db.ts"), dbTs);
|