zitejs 0.10.0-start.13 → 0.10.0-start.15
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/internal/appMode.d.ts +3 -0
- package/dist/cjs/internal/appMode.js +15 -0
- package/dist/cjs/upload/index.js +12 -2
- package/dist/cjs/upload/index.test.js +39 -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/internal/appMode.d.ts +3 -0
- package/dist/esm/internal/appMode.js +10 -0
- package/dist/esm/start/airtable/index.d.ts +1 -1
- package/dist/esm/start/airtable/index.js +14 -30
- package/dist/esm/start/credentials.d.ts +1 -18
- package/dist/esm/start/credentials.js +1 -29
- package/dist/esm/start/data/index.d.ts +2 -5
- package/dist/esm/start/data/index.js +4 -22
- package/dist/esm/start/outbound.js +3 -10
- package/dist/esm/start/router.d.ts +6 -1
- package/dist/esm/start/router.js +10 -4
- package/dist/esm/start/server.d.ts +2 -5
- package/dist/esm/start/server.js +5 -10
- package/dist/esm/start/types.d.ts +12 -15
- package/dist/esm/start/version.d.ts +1 -1
- package/dist/esm/start/version.js +1 -1
- package/dist/esm/upload/index.js +12 -2
- package/dist/esm/upload/index.test.js +39 -0
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
// Source hints for a Start app: line-level findings read from the code
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// Source hints for a Start app: line-level findings read from the code. The
|
|
2
|
+
// platform's build describes the app and enforces access; these only say early,
|
|
3
|
+
// and only where the code shows it for sure, what that build would refuse. A
|
|
4
|
+
// file that doesn't parse is skipped; the typecheck reports it.
|
|
4
5
|
import { parse } from "@babel/parser";
|
|
5
6
|
const isNode = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
|
|
6
7
|
const SKIP_KEYS = new Set(["loc", "start", "end", "extra", "leadingComments", "trailingComments", "innerComments", "range"]);
|
|
@@ -119,22 +120,23 @@ const parseFile = (path, code) => {
|
|
|
119
120
|
};
|
|
120
121
|
const FACTORIES = ["signedInFn", "publicFn", "createServerFn"];
|
|
121
122
|
/**
|
|
122
|
-
* The names a server function can start from, app-wide
|
|
123
|
-
* TanStack's raw one, and the app's own
|
|
123
|
+
* The names a server function can start from, app-wide, each with the factory
|
|
124
|
+
* it comes from: zitejs's, TanStack's raw one, and the app's own built from them
|
|
124
125
|
* (`export const managerFn = signedInFn.middleware([role("manager")])`).
|
|
125
126
|
*/
|
|
126
127
|
const factoryNames = (files) => {
|
|
127
|
-
const names = new
|
|
128
|
+
const names = new Map(FACTORIES.map((factory) => [factory, factory]));
|
|
128
129
|
for (let changed = true; changed;) {
|
|
129
130
|
changed = false;
|
|
130
131
|
for (const file of files) {
|
|
131
|
-
const
|
|
132
|
+
const factoryOf = (local) => names.get(file.imports.get(local)?.imported ?? local);
|
|
132
133
|
for (const statement of file.program.body) {
|
|
133
134
|
if (statement.type === "ExportNamedDeclaration" && statement.source) {
|
|
134
135
|
for (const specifier of statement.specifiers) {
|
|
135
136
|
const exported = nameOf(specifier.exported);
|
|
136
|
-
|
|
137
|
-
|
|
137
|
+
const factory = names.get(nameOf(specifier.local) ?? "");
|
|
138
|
+
if (exported && factory && !names.has(exported)) {
|
|
139
|
+
names.set(exported, factory);
|
|
138
140
|
changed = true;
|
|
139
141
|
}
|
|
140
142
|
}
|
|
@@ -148,8 +150,9 @@ const factoryNames = (files) => {
|
|
|
148
150
|
if (!name || !init || names.has(name) || isHandlerCall(unwrap(init)))
|
|
149
151
|
continue;
|
|
150
152
|
const { root } = chainOf(init);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
+
const factory = root && factoryOf(root);
|
|
154
|
+
if (factory) {
|
|
155
|
+
names.set(name, factory);
|
|
153
156
|
changed = true;
|
|
154
157
|
}
|
|
155
158
|
}
|
|
@@ -158,6 +161,23 @@ const factoryNames = (files) => {
|
|
|
158
161
|
}
|
|
159
162
|
return names;
|
|
160
163
|
};
|
|
164
|
+
const OWNER_INPUTS = ["userId", "ownerId"];
|
|
165
|
+
/** An owner id a function's validator takes as input: `.validator(z.object({ userId: … }))`. */
|
|
166
|
+
const ownerInput = (calls) => {
|
|
167
|
+
for (const call of calls) {
|
|
168
|
+
const callee = call.callee;
|
|
169
|
+
if (callee.type !== "MemberExpression" || !["validator", "inputValidator"].includes(nameOf(callee.property) ?? ""))
|
|
170
|
+
continue;
|
|
171
|
+
const schema = call.arguments[0];
|
|
172
|
+
if (schema?.type !== "CallExpression" || nameOf(schema.callee.property) !== "object")
|
|
173
|
+
continue;
|
|
174
|
+
const shape = schema.arguments[0];
|
|
175
|
+
const owner = shape?.type === "ObjectExpression" ? OWNER_INPUTS.find((key) => propertyNamed(shape, key)) : undefined;
|
|
176
|
+
if (owner)
|
|
177
|
+
return owner;
|
|
178
|
+
}
|
|
179
|
+
return undefined;
|
|
180
|
+
};
|
|
161
181
|
const callsCreateWorkflow = (program) => {
|
|
162
182
|
let found = false;
|
|
163
183
|
walk(program, (node) => {
|
|
@@ -213,8 +233,362 @@ const GUARD_TYPES = new Set(["IfStatement", "ConditionalExpression", "LogicalExp
|
|
|
213
233
|
const guardedByTypeofWindow = (ancestors) => ancestors.some((a) => GUARD_TYPES.has(a.type) && containsTypeofWindow((a.test ?? a.left)));
|
|
214
234
|
const isRelativePath = (node) => (node?.type === "StringLiteral" && node.value.startsWith("/")) ||
|
|
215
235
|
(node?.type === "TemplateLiteral" && (node.quasis[0]?.value?.cooked ?? "").startsWith("/"));
|
|
236
|
+
/** A top-level `const` in the file, so `{ server: serverOptions }` reads as written. */
|
|
237
|
+
const resolveConst = (file, node) => {
|
|
238
|
+
if (!node)
|
|
239
|
+
return undefined;
|
|
240
|
+
const value = unwrap(node);
|
|
241
|
+
if (value.type !== "Identifier")
|
|
242
|
+
return value;
|
|
243
|
+
for (const statement of file.program.body) {
|
|
244
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
245
|
+
if (declaration?.type !== "VariableDeclaration" || declaration.kind !== "const")
|
|
246
|
+
continue;
|
|
247
|
+
const declarator = declaration.declarations.find((d) => nameOf(d.id) === value.name);
|
|
248
|
+
if (declarator?.init)
|
|
249
|
+
return unwrap(declarator.init);
|
|
250
|
+
}
|
|
251
|
+
return undefined;
|
|
252
|
+
};
|
|
253
|
+
/** An object's property, read through a top-level const; undefined when absent, null when there but unreadable. */
|
|
254
|
+
const readProperty = (file, object, name) => {
|
|
255
|
+
if (object.properties.some((p) => p.type === "SpreadElement"))
|
|
256
|
+
return null;
|
|
257
|
+
const property = propertyNamed(object, name);
|
|
258
|
+
if (!property)
|
|
259
|
+
return undefined;
|
|
260
|
+
return (property.type === "ObjectMethod" ? property : resolveConst(file, property.value)) ?? null;
|
|
261
|
+
};
|
|
262
|
+
// The platform's, as describe's rule had them: the host, TanStack and Cloudflare answer these before the app does.
|
|
263
|
+
const RESERVED_PATHS = [
|
|
264
|
+
/^\/_zite/,
|
|
265
|
+
/^\/__zite/,
|
|
266
|
+
/^\/_serverFn(\/|$)/,
|
|
267
|
+
/^\/cdn-cgi(\/|$)/,
|
|
268
|
+
/^\/auth(\/|$)/,
|
|
269
|
+
/^\/api\/auth(\/|$)/,
|
|
270
|
+
/^\/manifest\.webmanifest$/,
|
|
271
|
+
];
|
|
272
|
+
/** What one middleware grants, as zitejs/start names it: `role()` signs in. Undefined when it isn't one of those. */
|
|
273
|
+
const accessOf = (file, node) => {
|
|
274
|
+
const value = unwrap(node);
|
|
275
|
+
const fromZitejs = (local) => {
|
|
276
|
+
const imported = local ? file.imports.get(local) : undefined;
|
|
277
|
+
return imported?.source === "zitejs/start" ? imported.imported : undefined;
|
|
278
|
+
};
|
|
279
|
+
const name = value.type === "Identifier"
|
|
280
|
+
? fromZitejs(value.name)
|
|
281
|
+
: value.type === "MemberExpression"
|
|
282
|
+
? fromZitejs(nameOf(value.object))
|
|
283
|
+
: value.type === "CallExpression"
|
|
284
|
+
? fromZitejs(nameOf(value.callee))
|
|
285
|
+
: undefined;
|
|
286
|
+
if (name === "role")
|
|
287
|
+
return value.type === "CallExpression" ? "signedIn" : undefined;
|
|
288
|
+
if (value.type === "MemberExpression")
|
|
289
|
+
return name === "webhook" ? "webhook" : undefined;
|
|
290
|
+
return name === "signedIn" || name === "anyone" || name === "webhook" ? name : undefined;
|
|
291
|
+
};
|
|
292
|
+
/** A middleware list's access, and whether anything in it is the app's own, which only the build can read. */
|
|
293
|
+
const middlewareAccess = (file, node) => {
|
|
294
|
+
if (node === undefined)
|
|
295
|
+
return { access: [], unread: false };
|
|
296
|
+
const list = node && resolveConst(file, node);
|
|
297
|
+
if (list?.type !== "ArrayExpression")
|
|
298
|
+
return { access: [], unread: true };
|
|
299
|
+
const access = [];
|
|
300
|
+
let unread = false;
|
|
301
|
+
for (const element of list.elements) {
|
|
302
|
+
const granted = element && element.type !== "SpreadElement" ? accessOf(file, element) : undefined;
|
|
303
|
+
if (granted)
|
|
304
|
+
access.push(granted);
|
|
305
|
+
else
|
|
306
|
+
unread = true;
|
|
307
|
+
}
|
|
308
|
+
return { access, unread };
|
|
309
|
+
};
|
|
310
|
+
/** `createFileRoute('/id')({ … })` in each route file; the root route as id "". */
|
|
311
|
+
const fileRoutes = (files) => files.flatMap((file) => {
|
|
312
|
+
if (!file.path.startsWith("src/routes/"))
|
|
313
|
+
return [];
|
|
314
|
+
const found = [];
|
|
315
|
+
walk(file.program, (node) => {
|
|
316
|
+
if (node.type !== "CallExpression" || !isNode(node.callee) || node.callee.type !== "CallExpression")
|
|
317
|
+
return;
|
|
318
|
+
const factory = nameOf(node.callee.callee);
|
|
319
|
+
const literal = node.callee.arguments[0];
|
|
320
|
+
const id = factory === "createFileRoute" && literal?.type === "StringLiteral"
|
|
321
|
+
? literal.value
|
|
322
|
+
: factory === "createRootRoute" || factory === "createRootRouteWithContext"
|
|
323
|
+
? ""
|
|
324
|
+
: undefined;
|
|
325
|
+
if (id === undefined)
|
|
326
|
+
return;
|
|
327
|
+
const options = resolveConst(file, node.arguments[0]);
|
|
328
|
+
found.push({ file, id, node, ...(options?.type === "ObjectExpression" && { options }) });
|
|
329
|
+
});
|
|
330
|
+
return found;
|
|
331
|
+
});
|
|
332
|
+
const segments = (id) => id.split("/").filter(Boolean);
|
|
333
|
+
const isAncestor = (ancestor, id) => {
|
|
334
|
+
const outer = segments(ancestor);
|
|
335
|
+
const inner = segments(id);
|
|
336
|
+
return outer.length < inner.length && outer.every((segment, i) => segment === inner[i]);
|
|
337
|
+
};
|
|
338
|
+
/** As TanStack shows a route's path: no pathless layouts or groups. */
|
|
339
|
+
const fullPathOf = (id) => {
|
|
340
|
+
const path = `/${segments(id).filter((s) => !s.startsWith("_") && !/^\(.*\)$/.test(s)).join("/")}`;
|
|
341
|
+
return id.endsWith("/") && path !== "/" ? `${path}/` : path;
|
|
342
|
+
};
|
|
343
|
+
/** A server route's methods, each with its own middleware; null when the handlers can't be read from source. */
|
|
344
|
+
const routeMethods = (file, handlers) => {
|
|
345
|
+
let object = handlers;
|
|
346
|
+
if (FUNCTION_TYPES.has(handlers.type)) {
|
|
347
|
+
// `handlers: ({ createHandlers }) => createHandlers({ … })`
|
|
348
|
+
const body = handlers.body;
|
|
349
|
+
const call = body.type === "CallExpression" ? body : undefined;
|
|
350
|
+
object = call && nameOf(call.callee) === "createHandlers" ? resolveConst(file, call.arguments[0]) : undefined;
|
|
351
|
+
}
|
|
352
|
+
if (object?.type !== "ObjectExpression")
|
|
353
|
+
return null;
|
|
354
|
+
const methods = [];
|
|
355
|
+
for (const property of object.properties) {
|
|
356
|
+
const method = property.type === "SpreadElement" ? undefined : nameOf(property.key);
|
|
357
|
+
if (!method)
|
|
358
|
+
return null;
|
|
359
|
+
const value = property.type === "ObjectMethod" ? property : resolveConst(file, property.value);
|
|
360
|
+
if (!value)
|
|
361
|
+
return null;
|
|
362
|
+
methods.push({ method, middleware: value.type === "ObjectExpression" ? readProperty(file, value, "middleware") : undefined });
|
|
363
|
+
}
|
|
364
|
+
return methods;
|
|
365
|
+
};
|
|
366
|
+
/** What the build's describe refuses or warns about a route, where the source shows it for sure. */
|
|
367
|
+
const routeProblems = (files, internal) => {
|
|
368
|
+
const problems = [];
|
|
369
|
+
const routes = fileRoutes(files);
|
|
370
|
+
const report = (rule, severity, route, message) => problems.push({ rule, severity, file: route.file.path, ...(lineOf(route.node) && { line: lineOf(route.node) }), message });
|
|
371
|
+
for (const route of routes) {
|
|
372
|
+
const path = fullPathOf(route.id);
|
|
373
|
+
if (route.id && RESERVED_PATHS.some((reserved) => reserved.test(path))) {
|
|
374
|
+
report("reserved-path", "error", route, `${path} is the platform's; give the route another path`);
|
|
375
|
+
}
|
|
376
|
+
const server = route.options && readProperty(route.file, route.options, "server");
|
|
377
|
+
if (!server || server.type !== "ObjectExpression")
|
|
378
|
+
continue;
|
|
379
|
+
const handlers = readProperty(route.file, server, "handlers");
|
|
380
|
+
if (!handlers)
|
|
381
|
+
continue;
|
|
382
|
+
const methods = routeMethods(route.file, handlers);
|
|
383
|
+
if (!methods)
|
|
384
|
+
continue;
|
|
385
|
+
const subject = `route ${path}`;
|
|
386
|
+
const staticData = readProperty(route.file, route.options, "staticData");
|
|
387
|
+
const description = staticData?.type === "ObjectExpression" ? readProperty(route.file, staticData, "description") : staticData;
|
|
388
|
+
if (description === undefined) {
|
|
389
|
+
report("description-missing", "warning", route, `${subject} has no description; add staticData: { description } for the editor`);
|
|
390
|
+
}
|
|
391
|
+
// Middleware applies from the root down: every route this one sits under, then its own, then the method's.
|
|
392
|
+
const inherited = routes
|
|
393
|
+
.filter((other) => other !== route && (other.id === "" || isAncestor(other.id, route.id)))
|
|
394
|
+
.map((other) => {
|
|
395
|
+
const otherServer = other.options ? readProperty(other.file, other.options, "server") : undefined;
|
|
396
|
+
if (otherServer === undefined)
|
|
397
|
+
return { access: [], unread: !other.options };
|
|
398
|
+
if (otherServer?.type !== "ObjectExpression")
|
|
399
|
+
return { access: [], unread: true };
|
|
400
|
+
return middlewareAccess(other.file, readProperty(other.file, otherServer, "middleware"));
|
|
401
|
+
});
|
|
402
|
+
const own = middlewareAccess(route.file, readProperty(route.file, server, "middleware"));
|
|
403
|
+
const publicMethods = [];
|
|
404
|
+
for (const { method, middleware } of methods) {
|
|
405
|
+
const chain = [...inherited, own, middlewareAccess(route.file, middleware)];
|
|
406
|
+
const unread = chain.some((link) => link.unread);
|
|
407
|
+
const access = [...new Set(chain.flatMap((link) => link.access))];
|
|
408
|
+
if (access.length === 0) {
|
|
409
|
+
if (!unread)
|
|
410
|
+
report("access-missing", "error", route, `${subject} ${method}: no signedIn, anyone or webhook middleware in its chain`);
|
|
411
|
+
}
|
|
412
|
+
else if (access.length > 1) {
|
|
413
|
+
report("access-conflict", "error", route, `${subject} ${method}: ${access.join(" + ")}`);
|
|
414
|
+
}
|
|
415
|
+
else if (access[0] === "anyone" && !unread) {
|
|
416
|
+
publicMethods.push(method);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (internal && publicMethods.length > 0) {
|
|
420
|
+
report("public-in-internal", "error", route, `${subject} lets anyone call ${publicMethods.join(", ")}, and this internal app signs everyone in; use signedIn`);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return problems;
|
|
424
|
+
};
|
|
425
|
+
/** The workflows an app exports, by id, as the build loads them: every module that calls createWorkflow. */
|
|
426
|
+
const workflowProblems = (files) => {
|
|
427
|
+
const problems = [];
|
|
428
|
+
const exported = [];
|
|
429
|
+
for (const file of files) {
|
|
430
|
+
if (!callsCreateWorkflow(file.program))
|
|
431
|
+
continue;
|
|
432
|
+
const createCall = (init) => {
|
|
433
|
+
if (!init)
|
|
434
|
+
return undefined;
|
|
435
|
+
const { root, calls } = chainOf(init);
|
|
436
|
+
const innermost = calls.at(-1);
|
|
437
|
+
return root && file.imports.get(root)?.imported === "createWorkflow" && innermost && nameOf(innermost.callee) === root
|
|
438
|
+
? innermost
|
|
439
|
+
: undefined;
|
|
440
|
+
};
|
|
441
|
+
const workflow = (id, node, call) => {
|
|
442
|
+
exported.push({ id, file: file.path, node });
|
|
443
|
+
const config = resolveConst(file, call.arguments[0]);
|
|
444
|
+
const trigger = config?.type === "ObjectExpression" ? readProperty(file, config, "trigger") : undefined;
|
|
445
|
+
if (trigger?.type !== "ObjectExpression")
|
|
446
|
+
return;
|
|
447
|
+
if (["every", "events", "subscribe"].some((key) => readProperty(file, trigger, key) !== undefined))
|
|
448
|
+
return;
|
|
449
|
+
problems.push({
|
|
450
|
+
rule: "workflow-trigger-unregistered",
|
|
451
|
+
severity: "error",
|
|
452
|
+
file: file.path,
|
|
453
|
+
...(lineOf(node) && { line: lineOf(node) }),
|
|
454
|
+
message: `workflow ${id}: a provider trigger needs \`subscribe\` to register it with the provider`,
|
|
455
|
+
});
|
|
456
|
+
};
|
|
457
|
+
const locals = new Map();
|
|
458
|
+
for (const statement of file.program.body) {
|
|
459
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
460
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
461
|
+
for (const declarator of declaration.declarations) {
|
|
462
|
+
const name = nameOf(declarator.id);
|
|
463
|
+
const call = createCall(declarator.init);
|
|
464
|
+
if (!name || !call)
|
|
465
|
+
continue;
|
|
466
|
+
if (statement.type === "ExportNamedDeclaration")
|
|
467
|
+
workflow(name, declarator, call);
|
|
468
|
+
else
|
|
469
|
+
locals.set(name, { node: declarator, call });
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (statement.type === "ExportNamedDeclaration" && !statement.source) {
|
|
473
|
+
for (const specifier of statement.specifiers) {
|
|
474
|
+
const local = locals.get(nameOf(specifier.local) ?? "");
|
|
475
|
+
const id = nameOf(specifier.exported);
|
|
476
|
+
if (local && id)
|
|
477
|
+
workflow(id, specifier, local.call);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (statement.type === "ExportDefaultDeclaration") {
|
|
481
|
+
const declared = statement.declaration;
|
|
482
|
+
const call = createCall(declared) ?? (declared.type === "Identifier" ? locals.get(declared.name)?.call : undefined);
|
|
483
|
+
if (call) {
|
|
484
|
+
problems.push({
|
|
485
|
+
rule: "workflow-default-export",
|
|
486
|
+
severity: "error",
|
|
487
|
+
file: file.path,
|
|
488
|
+
...(lineOf(statement) && { line: lineOf(statement) }),
|
|
489
|
+
message: "workflow default: export it by name, which becomes its id",
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
for (const { id, file, node } of exported) {
|
|
496
|
+
const elsewhere = exported.find((other) => other.id === id && other.file !== file);
|
|
497
|
+
if (elsewhere) {
|
|
498
|
+
problems.push({
|
|
499
|
+
rule: "duplicate-workflow",
|
|
500
|
+
severity: "error",
|
|
501
|
+
file,
|
|
502
|
+
...(lineOf(node) && { line: lineOf(node) }),
|
|
503
|
+
message: `workflow ${id}: ${id} is also exported from ${elsewhere.file}`,
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return problems;
|
|
508
|
+
};
|
|
509
|
+
// What the build keeps out of the browser (zite()'s importProtection), by the specifier an app writes.
|
|
510
|
+
const SERVER_ONLY_IMPORTS = [/^zitejs\/db$/, /^zitejs\/integrations(\/|$)/, /^zitejs\/email$/, /(^|\/)\.zite\/db(\.ts)?$/, /(^|\/)\.zite\/integrations(\/|$)/];
|
|
511
|
+
const isServerOnlyFile = (path) => /\.server(\.[^/]*)?$/.test(path.split("/").pop() ?? "");
|
|
512
|
+
const SOURCE_EXTENSIONS = ["", ".ts", ".tsx", ".js", ".jsx", ".mts", ".mjs", "/index.ts", "/index.tsx", "/index.js"];
|
|
513
|
+
/** The app file an import names, by relative path or the `~/` and `@/` aliases for src/. */
|
|
514
|
+
const resolveImport = (from, source, paths) => {
|
|
515
|
+
const base = /^[~@]\//.test(source)
|
|
516
|
+
? `src/${source.slice(2)}`
|
|
517
|
+
: source.startsWith(".")
|
|
518
|
+
? posixJoin(from.split("/").slice(0, -1).join("/"), source)
|
|
519
|
+
: undefined;
|
|
520
|
+
return base === undefined ? undefined : SOURCE_EXTENSIONS.map((extension) => base + extension).find((path) => paths.has(path));
|
|
521
|
+
};
|
|
522
|
+
const posixJoin = (dir, relative) => {
|
|
523
|
+
const parts = dir ? dir.split("/") : [];
|
|
524
|
+
for (const part of relative.split("/")) {
|
|
525
|
+
if (part === "..")
|
|
526
|
+
parts.pop();
|
|
527
|
+
else if (part !== "." && part !== "")
|
|
528
|
+
parts.push(part);
|
|
529
|
+
}
|
|
530
|
+
return parts.join("/");
|
|
531
|
+
};
|
|
532
|
+
/** Whether a file defines server code the build compiles out of the browser's copy, which only the build can follow. */
|
|
533
|
+
const definesServerCode = (file, factories) => {
|
|
534
|
+
let found = false;
|
|
535
|
+
walk(file.program, (node) => {
|
|
536
|
+
if (found)
|
|
537
|
+
return;
|
|
538
|
+
if (node.type === "CallExpression" && isHandlerCall(node)) {
|
|
539
|
+
const { root } = chainOf(node);
|
|
540
|
+
found = !!root && factories.has(file.imports.get(root)?.imported ?? root);
|
|
541
|
+
}
|
|
542
|
+
if ((node.type === "ObjectProperty" || node.type === "ObjectMethod") && nameOf(node.key) === "server")
|
|
543
|
+
found = true;
|
|
544
|
+
});
|
|
545
|
+
return found;
|
|
546
|
+
};
|
|
547
|
+
/**
|
|
548
|
+
* Page code that imports server-only modules, which the build refuses: followed
|
|
549
|
+
* from the routes through plain modules, never into a *.functions.* file, whose
|
|
550
|
+
* handlers the browser never gets, nor into or out of a file that defines
|
|
551
|
+
* server code itself.
|
|
552
|
+
*/
|
|
553
|
+
const serverImportProblems = (files, factories) => {
|
|
554
|
+
const byPath = new Map(files.map((file) => [file.path, file]));
|
|
555
|
+
const paths = new Set(byPath.keys());
|
|
556
|
+
const problems = [];
|
|
557
|
+
const pageRoots = files.filter((file) => file.path === "src/router.tsx" || (file.path.startsWith("src/routes/") && !file.path.startsWith("src/routes/api/")));
|
|
558
|
+
const seen = new Set();
|
|
559
|
+
const visit = (file) => {
|
|
560
|
+
if (seen.has(file.path))
|
|
561
|
+
return;
|
|
562
|
+
seen.add(file.path);
|
|
563
|
+
if (definesServerCode(file, factories))
|
|
564
|
+
return;
|
|
565
|
+
for (const statement of file.program.body) {
|
|
566
|
+
if (statement.type !== "ImportDeclaration")
|
|
567
|
+
continue;
|
|
568
|
+
const values = valueImports(statement);
|
|
569
|
+
if (values !== "all" && values.length === 0)
|
|
570
|
+
continue;
|
|
571
|
+
const source = statement.source.value;
|
|
572
|
+
const target = resolveImport(file.path, source, paths);
|
|
573
|
+
if (SERVER_ONLY_IMPORTS.some((pattern) => pattern.test(source)) || isServerOnlyFile(target ?? source)) {
|
|
574
|
+
problems.push({
|
|
575
|
+
rule: "server-import",
|
|
576
|
+
severity: "error",
|
|
577
|
+
file: file.path,
|
|
578
|
+
...(lineOf(statement) && { line: lineOf(statement) }),
|
|
579
|
+
message: `${source} runs only on the server, so page code can't import it; call a server function that uses it instead`,
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
else if (target && !/\.functions(\.[^/]*)?$/.test(target)) {
|
|
583
|
+
visit(byPath.get(target));
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
pageRoots.forEach(visit);
|
|
588
|
+
return problems;
|
|
589
|
+
};
|
|
216
590
|
/** Every source hint for one Start app, given its files by app-relative path. */
|
|
217
|
-
export function sourceProblems(sources) {
|
|
591
|
+
export function sourceProblems(sources, { internal = false } = {}) {
|
|
218
592
|
const files = Object.entries(sources)
|
|
219
593
|
.filter(([path]) => /\.[cm]?[jt]sx?$/.test(path) && !path.endsWith("routeTree.gen.ts") && !path.endsWith(".d.ts"))
|
|
220
594
|
.flatMap(([path, code]) => parseFile(path, code) ?? []);
|
|
@@ -305,6 +679,20 @@ export function sourceProblems(sources) {
|
|
|
305
679
|
if (!hasMethodOption(calls)) {
|
|
306
680
|
report("method-missing", "error", path, node, `call ${root} with { method: 'GET' | 'POST', description } before .handler()`);
|
|
307
681
|
}
|
|
682
|
+
const factory = factories.get(file.imports.get(root)?.imported ?? root);
|
|
683
|
+
const subject = `function ${name ?? "this function"}`;
|
|
684
|
+
if (factory === "createServerFn") {
|
|
685
|
+
report("raw-server-fn", "error", path, node, `${subject}: define it with signedInFn or publicFn from zitejs/start`);
|
|
686
|
+
}
|
|
687
|
+
else if (factory === "publicFn" && internal) {
|
|
688
|
+
report("public-in-internal", "error", path, node, `${subject} is a publicFn, and this internal app signs everyone in; make it a signedInFn`);
|
|
689
|
+
}
|
|
690
|
+
else if (factory === "signedInFn") {
|
|
691
|
+
const owner = ownerInput(calls);
|
|
692
|
+
if (owner) {
|
|
693
|
+
report("owner-from-input", "warning", path, node, `${subject} takes ${owner} as input, which any caller can set; use context.user.id`);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
308
696
|
if (file.imports.get(root)?.imported === "publicFn" || root === "publicFn") {
|
|
309
697
|
const handler = node.arguments[0];
|
|
310
698
|
if (handler && FUNCTION_TYPES.has(handler.type)) {
|
|
@@ -423,6 +811,7 @@ export function sourceProblems(sources) {
|
|
|
423
811
|
}
|
|
424
812
|
});
|
|
425
813
|
}
|
|
814
|
+
problems.push(...routeProblems(files, internal), ...workflowProblems(files), ...serverImportProblems(files, factories));
|
|
426
815
|
for (const [name, paths] of serverFnFiles) {
|
|
427
816
|
const distinct = [...new Set(paths)];
|
|
428
817
|
if (distinct.length > 1) {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// TypeScript's native compiler, as the platform's checks run it, found the same
|
|
2
|
+
// way every time: never from PATH, so a check here and one in the sandbox use
|
|
3
|
+
// the same compiler.
|
|
4
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
const PACKAGE = "@typescript/native-preview";
|
|
8
|
+
// Where `npm install -g` put it for the node running this: the sandbox image's.
|
|
9
|
+
const globalRoot = () => process.env.npm_config_prefix
|
|
10
|
+
? path.join(process.env.npm_config_prefix, process.platform === "win32" ? "node_modules" : "lib/node_modules")
|
|
11
|
+
: process.platform === "win32"
|
|
12
|
+
? path.join(path.dirname(process.execPath), "node_modules")
|
|
13
|
+
: path.join(path.dirname(path.dirname(process.execPath)), "lib", "node_modules");
|
|
14
|
+
/** tsgo's launcher: the app's own, walking up from it as Node does, else the global install beside this node. */
|
|
15
|
+
export function findTsgo(appDir) {
|
|
16
|
+
let manifest;
|
|
17
|
+
try {
|
|
18
|
+
manifest = createRequire(path.join(path.resolve(appDir), "package.json")).resolve(`${PACKAGE}/package.json`);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
const global = path.join(globalRoot(), PACKAGE, "package.json");
|
|
22
|
+
manifest = existsSync(global) ? global : undefined;
|
|
23
|
+
}
|
|
24
|
+
if (!manifest)
|
|
25
|
+
return undefined;
|
|
26
|
+
const { bin } = JSON.parse(readFileSync(manifest, "utf-8"));
|
|
27
|
+
const launcher = typeof bin === "string" ? bin : bin?.tsgo;
|
|
28
|
+
return launcher ? path.join(path.dirname(manifest), launcher) : undefined;
|
|
29
|
+
}
|
package/dist/esm/cli.js
CHANGED
|
@@ -45,7 +45,7 @@ async function main() {
|
|
|
45
45
|
console.error(' sync Generate .zite/db.ts from database schema (single-app)');
|
|
46
46
|
console.error(' dev Run sync then watch for changes (like npx convex dev)');
|
|
47
47
|
console.error(' generate Regenerate every app and automation .zite/ (monorepo)');
|
|
48
|
-
console.error(' check Typecheck and build every app and automation; a Start app is
|
|
48
|
+
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)');
|
|
49
49
|
console.error(' bundle Bundle endpoints (src/api/*.ts, or an automation\'s src/index.ts) for cloudflare-lambda');
|
|
50
50
|
console.error(' describe Print what a built TanStack Start app declares: functions, routes, access');
|
|
51
51
|
process.exit(1);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Classic's upload client reads this too, so it imports nothing of Start: the
|
|
2
|
+
// request's host comes through the key src/start/host.ts reads it under.
|
|
3
|
+
const HOST_STORE_KEY = Symbol.for("zitejs:host-context");
|
|
4
|
+
// What a Start page was rendered with, kept in the browser when it hydrates.
|
|
5
|
+
const RENDERED_MODE = Symbol.for("zitejs:rendered-mode");
|
|
6
|
+
export const keepRenderedMode = (mode) => {
|
|
7
|
+
globalThis[RENDERED_MODE] = mode;
|
|
8
|
+
};
|
|
9
|
+
/** The mode the platform served this request or page in; undefined outside a Start app. */
|
|
10
|
+
export const platformMode = () => globalThis[HOST_STORE_KEY]?.getStore()?.mode ?? globalThis[RENDERED_MODE];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AirtableSettings } from "./dispatch.js";
|
|
2
2
|
import type { PlatformSession, ZiteRequestContext } from "../types.js";
|
|
3
|
-
/** The build's Airtable integration `integrationId`, or undefined when it isn't one
|
|
3
|
+
/** The build's Airtable integration `integrationId`, or undefined when it isn't one. */
|
|
4
4
|
export declare const airtableSettingsOf: (session: PlatformSession, integrationId: string) => Promise<AirtableSettings | undefined>;
|
|
5
5
|
/** One generated Airtable table call, run here with the connection's token, which the outbound fetch refreshes on a 401. */
|
|
6
6
|
export declare function runAirtableCall(host: ZiteRequestContext, session: PlatformSession, integrationId: string, settings: AirtableSettings, methodName: string, params: unknown, access: "signedIn" | "anyone" | "webhook" | "workflow"): Promise<unknown>;
|
|
@@ -5,44 +5,29 @@ import { rememberConnection } from "../outbound.js";
|
|
|
5
5
|
const AIRTABLE_API = "https://api.airtable.com";
|
|
6
6
|
// A build's integrations don't change, so per isolate: only answers, never a pending call.
|
|
7
7
|
const settingsHeld = new Map();
|
|
8
|
-
|
|
9
|
-
const NOT_IMPLEMENTED = /does not implement (?:the )?method/i;
|
|
10
|
-
/** The build's Airtable integration `integrationId`, or undefined when it isn't one, or the host can't say. */
|
|
8
|
+
/** The build's Airtable integration `integrationId`, or undefined when it isn't one. */
|
|
11
9
|
export const airtableSettingsOf = async (session, integrationId) => {
|
|
12
|
-
if (unsupported || !session.integrationSettings)
|
|
13
|
-
return undefined;
|
|
14
10
|
if (settingsHeld.has(integrationId))
|
|
15
11
|
return settingsHeld.get(integrationId) ?? undefined;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return settings ?? undefined;
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
if (error instanceof Error && NOT_IMPLEMENTED.test(error.message)) {
|
|
26
|
-
unsupported = true;
|
|
27
|
-
return undefined;
|
|
28
|
-
}
|
|
29
|
-
throw error;
|
|
30
|
-
}
|
|
12
|
+
const answer = await session.integrationSettings(integrationId);
|
|
13
|
+
const settings = answer?.serviceType === "airtable" && typeof answer.baseId === "string"
|
|
14
|
+
? { baseId: answer.baseId, nameMappings: answer.nameMappings ?? null }
|
|
15
|
+
: null;
|
|
16
|
+
settingsHeld.set(integrationId, settings);
|
|
17
|
+
return settings ?? undefined;
|
|
31
18
|
};
|
|
32
19
|
/** One generated Airtable table call, run here with the connection's token, which the outbound fetch refreshes on a 401. */
|
|
33
20
|
export async function runAirtableCall(host, session, integrationId, settings, methodName, params, access) {
|
|
34
21
|
// As classic: a demo reaches no connected account.
|
|
35
22
|
if (host.mode === "demo")
|
|
36
23
|
throw new ZiteError("FORBIDDEN", "This action is not available in demo mode");
|
|
37
|
-
const connection =
|
|
38
|
-
|
|
39
|
-
:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
access,
|
|
45
|
-
});
|
|
24
|
+
const connection = await session.call({
|
|
25
|
+
integrationId: INTEGRATIONS_PLATFORM_ID,
|
|
26
|
+
className: "Connections",
|
|
27
|
+
methodName: "get",
|
|
28
|
+
params: { name: integrationId },
|
|
29
|
+
access,
|
|
30
|
+
});
|
|
46
31
|
if ("error" in connection) {
|
|
47
32
|
const { code, message, userFacingMessage } = connection.error;
|
|
48
33
|
throw new ZiteError({ code: isZiteErrorCode(code) ? code : "INTERNAL_ERROR", message, userFacingMessage });
|
|
@@ -66,5 +51,4 @@ export async function runAirtableCall(host, session, integrationId, settings, me
|
|
|
66
51
|
/** For tests: forget what this isolate learned. */
|
|
67
52
|
export const resetAirtableSettings = () => {
|
|
68
53
|
settingsHeld.clear();
|
|
69
|
-
unsupported = false;
|
|
70
54
|
};
|
|
@@ -1,19 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Where `integrations.get` reads a connection. In Zite's cloud the host
|
|
3
|
-
* answers it on the request's session; an app run elsewhere names its own
|
|
4
|
-
* source in its server entry: memory or the environment here, a shared cache
|
|
5
|
-
* once one exists.
|
|
6
|
-
*/
|
|
7
|
-
import type { PlatformCallInput, PlatformCallResult, PlatformSession } from "./types.js";
|
|
8
|
-
export type CredentialSource = {
|
|
9
|
-
get(name: string): Promise<PlatformCallResult>;
|
|
10
|
-
/** After a provider answered 401: the connection refreshed, where the source can. */
|
|
11
|
-
refresh?(name: string): Promise<PlatformCallResult>;
|
|
12
|
-
};
|
|
1
|
+
/** The platform id `integrations.get` is answered under. */
|
|
13
2
|
export declare const INTEGRATIONS_PLATFORM_ID = "__connections__";
|
|
14
|
-
/** The host's, on the request's session. */
|
|
15
|
-
export declare const sessionCredentials: (session: PlatformSession, access: PlatformCallInput["access"]) => CredentialSource;
|
|
16
|
-
/** Connections held in memory, by name, as `integrations.get` answers them; `refresh` fetches one anew. */
|
|
17
|
-
export declare const memoryCredentials: (connections: Record<string, unknown>, refresh?: (name: string) => Promise<unknown>) => CredentialSource;
|
|
18
|
-
/** Connections from `ZITE_CONNECTIONS`, JSON by name, for an app run outside Zite. */
|
|
19
|
-
export declare const envCredentials: (env?: Record<string, string | undefined>) => CredentialSource;
|