zitejs 0.10.0-start.2 → 0.10.0-start.20
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/auth/index.d.ts +2 -2
- package/dist/cjs/auth/index.js +6 -0
- package/dist/cjs/auth/index.test.js +30 -0
- package/dist/cjs/auth/previewClaim.d.ts +7 -0
- package/dist/cjs/auth/previewClaim.js +16 -0
- package/dist/cjs/backend/index.d.ts +121 -3
- package/dist/cjs/backend/index.test.js +58 -0
- package/dist/cjs/bundle/index.d.ts +8 -0
- package/dist/cjs/bundle/index.js +9 -8
- package/dist/cjs/check/index.d.ts +45 -1
- package/dist/cjs/check/index.js +198 -109
- package/dist/cjs/check/start/problem.d.ts +11 -0
- package/dist/cjs/check/start/problem.js +5 -0
- package/dist/cjs/check/start/routeTree.d.ts +9 -0
- package/dist/cjs/check/start/routeTree.js +111 -0
- package/dist/cjs/check/start/sourceRules.d.ts +8 -0
- package/dist/cjs/check/start/sourceRules.js +1110 -0
- 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/client/index.d.ts +24 -0
- package/dist/cjs/dev/index.js +14 -4
- package/dist/cjs/integrations/rules.d.ts +13 -0
- package/dist/cjs/integrations/rules.js +29 -0
- package/dist/cjs/integrations/rules.test.js +1 -0
- package/dist/cjs/internal/appMode.d.ts +3 -0
- package/dist/cjs/internal/appMode.js +15 -0
- package/dist/cjs/runtime/index.d.ts +13 -13
- package/dist/cjs/start/entries.d.ts +10 -4
- package/dist/cjs/start/entries.js +23 -6
- package/dist/cjs/steps/index.d.ts +78 -0
- package/dist/cjs/steps/index.js +507 -0
- package/dist/cjs/steps/index.test.d.ts +1 -0
- package/dist/cjs/steps/index.test.js +635 -0
- package/dist/cjs/sync/index.js +2 -1
- package/dist/cjs/sync/lib.d.ts +10 -3
- package/dist/cjs/sync/lib.js +35 -21
- package/dist/cjs/sync/lib.test.js +106 -36
- package/dist/cjs/types/fields.d.ts +8 -1
- package/dist/cjs/types/fields.js +7 -1
- package/dist/cjs/types/tables.d.ts +16 -0
- package/dist/cjs/upload/index.js +12 -2
- package/dist/cjs/upload/index.test.js +39 -0
- package/dist/cjs/vite/index.js +8 -3
- package/dist/cjs/vite/index.test.js +10 -2
- package/dist/esm/auth/index.d.ts +2 -2
- package/dist/esm/auth/index.js +6 -0
- package/dist/esm/auth/index.test.js +30 -0
- package/dist/esm/auth/previewClaim.d.ts +7 -0
- package/dist/esm/auth/previewClaim.js +12 -0
- package/dist/esm/backend/index.d.ts +121 -3
- package/dist/esm/backend/index.test.js +58 -0
- package/dist/esm/bundle/index.d.ts +8 -0
- package/dist/esm/bundle/index.js +8 -8
- package/dist/esm/check/index.d.ts +45 -1
- package/dist/esm/check/index.js +198 -113
- package/dist/esm/check/start/problem.d.ts +11 -0
- package/dist/esm/check/start/problem.js +1 -0
- package/dist/esm/check/start/routeTree.d.ts +9 -0
- package/dist/esm/check/start/routeTree.js +70 -0
- package/dist/esm/check/start/sourceRules.d.ts +8 -0
- package/dist/esm/check/start/sourceRules.js +1107 -0
- 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/client/index.d.ts +24 -0
- package/dist/esm/dev/index.js +16 -6
- package/dist/esm/integrations/rules.d.ts +13 -0
- package/dist/esm/integrations/rules.js +29 -0
- package/dist/esm/integrations/rules.test.js +1 -0
- package/dist/esm/internal/appMode.d.ts +3 -0
- package/dist/esm/internal/appMode.js +10 -0
- package/dist/esm/runtime/index.d.ts +13 -13
- package/dist/esm/start/aiUsage.d.ts +23 -0
- package/dist/esm/start/aiUsage.js +146 -0
- package/dist/esm/start/airtable/arrayFields.d.ts +10 -0
- package/dist/esm/start/airtable/arrayFields.js +30 -0
- package/dist/esm/start/airtable/dispatch.d.ts +54 -0
- package/dist/esm/start/airtable/dispatch.js +483 -0
- package/dist/esm/start/airtable/formulas.d.ts +70 -0
- package/dist/esm/start/airtable/formulas.js +258 -0
- package/dist/esm/start/airtable/index.d.ts +8 -0
- package/dist/esm/start/airtable/index.js +54 -0
- package/dist/esm/start/authHooks.d.ts +7 -0
- package/dist/esm/start/authHooks.js +10 -0
- package/dist/esm/start/authHooksModule.d.ts +1 -0
- package/dist/esm/start/authHooksModule.js +3 -0
- package/dist/esm/start/clientErrorMessage.test.d.ts +1 -0
- package/dist/esm/start/clientErrorMessage.test.js +105 -0
- package/dist/esm/start/credentials.d.ts +2 -0
- package/dist/esm/start/credentials.js +2 -0
- package/dist/esm/start/data/baseRunner.d.ts +92 -0
- package/dist/esm/start/data/baseRunner.js +83 -0
- package/dist/esm/start/data/dispatch.d.ts +16 -0
- package/dist/esm/start/data/dispatch.js +198 -0
- package/dist/esm/start/data/index.d.ts +15 -0
- package/dist/esm/start/data/index.js +58 -0
- package/dist/esm/start/data/sqlNameMappings.d.ts +58 -0
- package/dist/esm/start/data/sqlNameMappings.js +92 -0
- package/dist/esm/start/data/transformations.d.ts +97 -0
- package/dist/esm/start/data/transformations.js +262 -0
- package/dist/esm/start/data/types.d.ts +91 -0
- package/dist/esm/start/data/types.js +14 -0
- package/dist/esm/start/describe.d.ts +35 -5
- package/dist/esm/start/describe.js +74 -22
- package/dist/esm/start/entries.d.ts +10 -4
- package/dist/esm/start/entries.js +21 -5
- package/dist/esm/start/errors.d.ts +63 -0
- package/dist/esm/start/errors.js +146 -0
- package/dist/esm/start/errors.test.d.ts +1 -0
- package/dist/esm/start/errors.test.js +23 -0
- package/dist/esm/start/factories.test-d.d.ts +1 -0
- package/dist/esm/start/factories.test-d.js +20 -0
- package/dist/esm/start/index.d.ts +32 -27
- package/dist/esm/start/index.js +77 -47
- package/dist/esm/start/instance.d.ts +4 -4
- package/dist/esm/start/instance.js +54 -11
- package/dist/esm/start/outbound.d.ts +11 -0
- package/dist/esm/start/outbound.js +398 -0
- package/dist/esm/start/previewClaim.d.ts +7 -0
- package/dist/esm/start/previewClaim.js +23 -0
- package/dist/esm/start/problems.js +1 -1
- package/dist/esm/start/quiet.d.ts +2 -0
- package/dist/esm/start/quiet.js +27 -0
- package/dist/esm/start/redactSecrets.d.ts +15 -0
- package/dist/esm/start/redactSecrets.js +154 -0
- package/dist/esm/start/redactSecrets.test.d.ts +1 -0
- package/dist/esm/start/redactSecrets.test.js +151 -0
- package/dist/esm/start/registration.d.ts +13 -0
- package/dist/esm/start/registration.js +21 -0
- package/dist/esm/start/registrationHash.d.ts +22 -0
- package/dist/esm/start/registrationHash.js +470 -0
- package/dist/esm/start/router.d.ts +18 -4
- package/dist/esm/start/router.js +134 -11
- package/dist/esm/start/runLog.d.ts +7 -4
- package/dist/esm/start/runLog.js +64 -6
- package/dist/esm/start/scope.d.ts +2 -2
- package/dist/esm/start/server.d.ts +40 -36
- package/dist/esm/start/server.js +320 -48
- package/dist/esm/start/steps.d.ts +28 -0
- package/dist/esm/start/steps.js +97 -0
- package/dist/esm/start/types.d.ts +178 -5
- package/dist/esm/start/version.d.ts +1 -0
- package/dist/esm/start/version.js +2 -0
- package/dist/esm/start/vite.js +88 -11
- package/dist/esm/start/worker.d.ts +6 -0
- package/dist/esm/start/worker.js +12 -0
- package/dist/esm/start/workflow.d.ts +42 -12
- package/dist/esm/start/workflow.js +3 -3
- package/dist/esm/start/workflow.test-d.js +41 -8
- package/dist/esm/start/workflowModules.js +1 -1
- package/dist/esm/start/workflowRun.d.ts +21 -8
- package/dist/esm/start/workflowRun.js +29 -8
- package/dist/esm/steps/index.d.ts +78 -0
- package/dist/esm/steps/index.js +502 -0
- package/dist/esm/steps/index.test.d.ts +1 -0
- package/dist/esm/steps/index.test.js +633 -0
- package/dist/esm/sync/index.js +2 -1
- package/dist/esm/sync/lib.d.ts +10 -3
- package/dist/esm/sync/lib.js +35 -21
- package/dist/esm/sync/lib.test.js +106 -36
- package/dist/esm/types/fields.d.ts +8 -1
- package/dist/esm/types/fields.js +6 -0
- package/dist/esm/types/tables.d.ts +16 -0
- package/dist/esm/upload/index.js +12 -2
- package/dist/esm/upload/index.test.js +39 -0
- package/dist/esm/vite/index.js +8 -3
- package/dist/esm/vite/index.test.js +10 -2
- package/package.json +37 -16
- package/dist/cjs/start/problems.d.ts +0 -19
- package/dist/cjs/start/problems.js +0 -11
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
// cyrb53: enough to tell when the code changed, which is all publish asks.
|
|
2
|
+
const sourceHash = (text) => {
|
|
3
|
+
let h1 = 0xdeadbeef;
|
|
4
|
+
let h2 = 0x41c6ce57;
|
|
5
|
+
for (let i = 0; i < text.length; i++) {
|
|
6
|
+
const ch = text.charCodeAt(i);
|
|
7
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
8
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
9
|
+
}
|
|
10
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
11
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
12
|
+
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(16).padStart(14, "0");
|
|
13
|
+
};
|
|
14
|
+
export const registrationHash = (subscribe, unsubscribe) => sourceHash(`${subscribe}\n${unsubscribe ?? ""}`);
|
|
15
|
+
const KEYWORDS = new Set(("async await break case catch class const continue debugger default delete do else export extends false finally for " +
|
|
16
|
+
"function if import in instanceof let new null of return static super switch this throw true try typeof undefined var " +
|
|
17
|
+
"void while with yield").split(" "));
|
|
18
|
+
const IDENTIFIER = /[A-Za-z_$][\w$]*/y;
|
|
19
|
+
const NUMBER = /[0-9][\w.]*/y;
|
|
20
|
+
const QUOTED = /"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/y;
|
|
21
|
+
// A template literal's text up to its end or its next `${`.
|
|
22
|
+
const TEMPLATE_TEXT = /(?:[^`\\$]|\\.|\$(?!\{))*(?:`|\$\{)?/y;
|
|
23
|
+
/**
|
|
24
|
+
* A built function's text with its identifiers numbered in order of first appearance, so the minifier's renaming
|
|
25
|
+
* alone can't change it. Literals, property names and object keys, which it doesn't rename, stay.
|
|
26
|
+
*/
|
|
27
|
+
export const canonicalNames = (text) => {
|
|
28
|
+
const names = new Map();
|
|
29
|
+
// The brace depth each open template expression closes at.
|
|
30
|
+
const templates = [];
|
|
31
|
+
let depth = 0;
|
|
32
|
+
let out = "";
|
|
33
|
+
let i = 0;
|
|
34
|
+
const take = (pattern) => {
|
|
35
|
+
pattern.lastIndex = i;
|
|
36
|
+
const token = pattern.exec(text)?.[0] ?? text[i];
|
|
37
|
+
i += token.length;
|
|
38
|
+
return token;
|
|
39
|
+
};
|
|
40
|
+
const templateText = () => {
|
|
41
|
+
const token = take(TEMPLATE_TEXT);
|
|
42
|
+
out += token;
|
|
43
|
+
if (token.endsWith("${"))
|
|
44
|
+
templates.push(depth++);
|
|
45
|
+
};
|
|
46
|
+
while (i < text.length) {
|
|
47
|
+
const ch = text[i];
|
|
48
|
+
if (ch === "`") {
|
|
49
|
+
out += take(/`/y);
|
|
50
|
+
templateText();
|
|
51
|
+
}
|
|
52
|
+
else if (ch === "}" && templates.at(-1) === depth - 1) {
|
|
53
|
+
templates.pop();
|
|
54
|
+
depth--;
|
|
55
|
+
out += take(/\}/y);
|
|
56
|
+
templateText();
|
|
57
|
+
}
|
|
58
|
+
else if (ch === '"' || ch === "'") {
|
|
59
|
+
out += take(QUOTED);
|
|
60
|
+
}
|
|
61
|
+
else if (/[0-9]/.test(ch)) {
|
|
62
|
+
out += take(NUMBER);
|
|
63
|
+
}
|
|
64
|
+
else if (/[A-Za-z_$]/.test(ch)) {
|
|
65
|
+
const start = i;
|
|
66
|
+
const token = take(IDENTIFIER);
|
|
67
|
+
let before = start - 1;
|
|
68
|
+
while (/\s/.test(text[before] ?? ""))
|
|
69
|
+
before--;
|
|
70
|
+
let after = i;
|
|
71
|
+
while (/\s/.test(text[after] ?? ""))
|
|
72
|
+
after++;
|
|
73
|
+
const property = text[before] === "." && text[before - 1] !== ".";
|
|
74
|
+
const key = (text[before] === "{" || text[before] === ",") && text[after] === ":";
|
|
75
|
+
if (KEYWORDS.has(token) || property || key) {
|
|
76
|
+
out += token;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
if (!names.has(token))
|
|
80
|
+
names.set(token, `$${names.size}`);
|
|
81
|
+
out += names.get(token);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
if (ch === "{")
|
|
86
|
+
depth++;
|
|
87
|
+
if (ch === "}")
|
|
88
|
+
depth--;
|
|
89
|
+
out += take(/[^]/y);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
};
|
|
94
|
+
const WRAPPERS = new Set(["ParenthesizedExpression", "TSAsExpression", "TSSatisfiesExpression", "TSNonNullExpression"]);
|
|
95
|
+
const DECLARATIONS = new Set(["FunctionDeclaration", "ClassDeclaration", "TSEnumDeclaration"]);
|
|
96
|
+
// Types run nothing, so they're neither followed nor hashed.
|
|
97
|
+
const TYPE_KEYS = new Set(["typeAnnotation", "returnType", "typeParameters", "typeArguments", "superTypeArguments", "implements"]);
|
|
98
|
+
const TYPE_NODES = new Set(["TSInterfaceDeclaration", "TSTypeAliasDeclaration", "TSDeclareFunction"]);
|
|
99
|
+
const KEYED = new Set(["Property", "MethodDefinition", "PropertyDefinition", "AccessorProperty"]);
|
|
100
|
+
const LABELED = new Set(["LabeledStatement", "BreakStatement", "ContinueStatement"]);
|
|
101
|
+
const FUNCTIONS = new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]);
|
|
102
|
+
const isNode = (value) => typeof value?.type === "string";
|
|
103
|
+
const nameOf = (node) => {
|
|
104
|
+
const n = node;
|
|
105
|
+
return n?.type === "Identifier" ? n.name : n?.type === "Literal" && typeof n.value === "string" ? n.value : undefined;
|
|
106
|
+
};
|
|
107
|
+
const patternNames = (pattern) => {
|
|
108
|
+
if (!isNode(pattern))
|
|
109
|
+
return [];
|
|
110
|
+
switch (pattern.type) {
|
|
111
|
+
case "Identifier":
|
|
112
|
+
return [pattern.name];
|
|
113
|
+
case "ObjectPattern":
|
|
114
|
+
return pattern.properties.flatMap((p) => patternNames(p.type === "RestElement" ? p.argument : p.value));
|
|
115
|
+
case "ArrayPattern":
|
|
116
|
+
return pattern.elements.flatMap(patternNames);
|
|
117
|
+
case "AssignmentPattern":
|
|
118
|
+
return patternNames(pattern.left);
|
|
119
|
+
case "RestElement":
|
|
120
|
+
return patternNames(pattern.argument);
|
|
121
|
+
default:
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const moduleOf = (file, code, program) => {
|
|
126
|
+
const bindings = new Map();
|
|
127
|
+
const exports = new Map();
|
|
128
|
+
const stars = [];
|
|
129
|
+
const declare = (declaration) => {
|
|
130
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
131
|
+
return declaration.declarations.flatMap((declarator) => {
|
|
132
|
+
const names = patternNames(declarator.id);
|
|
133
|
+
const value = declarator.id.type === "Identifier" ? declarator.init : undefined;
|
|
134
|
+
for (const name of names)
|
|
135
|
+
bindings.set(name, { node: declarator, value });
|
|
136
|
+
return names;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const name = nameOf(declaration?.id);
|
|
140
|
+
if (!declaration || !name || !DECLARATIONS.has(declaration.type))
|
|
141
|
+
return [];
|
|
142
|
+
bindings.set(name, { node: declaration, value: declaration });
|
|
143
|
+
return [name];
|
|
144
|
+
};
|
|
145
|
+
for (const statement of (program?.body ?? [])) {
|
|
146
|
+
const source = statement.source?.value;
|
|
147
|
+
if (statement.type === "ImportDeclaration") {
|
|
148
|
+
if (statement.importKind === "type")
|
|
149
|
+
continue;
|
|
150
|
+
for (const specifier of statement.specifiers) {
|
|
151
|
+
if (specifier.importKind === "type")
|
|
152
|
+
continue;
|
|
153
|
+
const name = specifier.type === "ImportNamespaceSpecifier" ? "*" : specifier.type === "ImportDefaultSpecifier" ? "default" : nameOf(specifier.imported);
|
|
154
|
+
if (name)
|
|
155
|
+
bindings.set(nameOf(specifier.local), { source: source, name });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else if (statement.type === "ExportAllDeclaration") {
|
|
159
|
+
if (statement.exportKind === "type")
|
|
160
|
+
continue;
|
|
161
|
+
const as = nameOf(statement.exported);
|
|
162
|
+
if (as)
|
|
163
|
+
exports.set(as, { source: source, name: "*" });
|
|
164
|
+
else
|
|
165
|
+
stars.push(source);
|
|
166
|
+
}
|
|
167
|
+
else if (statement.type === "ExportNamedDeclaration") {
|
|
168
|
+
if (statement.exportKind === "type")
|
|
169
|
+
continue;
|
|
170
|
+
for (const name of declare(statement.declaration))
|
|
171
|
+
exports.set(name, { local: name });
|
|
172
|
+
for (const specifier of statement.specifiers ?? []) {
|
|
173
|
+
const local = nameOf(specifier.local);
|
|
174
|
+
const as = nameOf(specifier.exported);
|
|
175
|
+
if (local && as && specifier.exportKind !== "type")
|
|
176
|
+
exports.set(as, source ? { source, name: local } : { local });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
else if (statement.type === "ExportDefaultDeclaration") {
|
|
180
|
+
const declaration = statement.declaration;
|
|
181
|
+
const [name] = declare(declaration);
|
|
182
|
+
if (!name)
|
|
183
|
+
bindings.set("*default*", { node: declaration, value: declaration });
|
|
184
|
+
exports.set("default", { local: name ?? "*default*" });
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
declare(statement);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { file, code, bindings, exports, stars };
|
|
191
|
+
};
|
|
192
|
+
// `object.member` of a const object literal written whole: only that property, so a generated client's other
|
|
193
|
+
// tables, say, stay out of the hash.
|
|
194
|
+
const propertyOf = (object, member) => {
|
|
195
|
+
while (object && WRAPPERS.has(object.type))
|
|
196
|
+
object = object.expression;
|
|
197
|
+
const properties = object?.type === "ObjectExpression" ? object.properties : [];
|
|
198
|
+
if (properties.some((p) => p.type !== "Property" || p.computed))
|
|
199
|
+
return undefined;
|
|
200
|
+
return properties.find((p) => nameOf(p.key) === member);
|
|
201
|
+
};
|
|
202
|
+
const isPropertyName = (parent, key) => !!parent &&
|
|
203
|
+
((parent.type === "MemberExpression" && key === "property" && !parent.computed) ||
|
|
204
|
+
(KEYED.has(parent.type) && key === "key" && !parent.computed) ||
|
|
205
|
+
(LABELED.has(parent.type) && key === "label") ||
|
|
206
|
+
parent.type === "MetaProperty");
|
|
207
|
+
// The names a function binds for its whole body: its parameters, its `var`s, and what its body declares directly.
|
|
208
|
+
// A name an inner block declares is left out, so a module-level name it shadows there still counts: hashing too much
|
|
209
|
+
// registers once more, hashing too little never registers again.
|
|
210
|
+
const boundIn = (fn) => {
|
|
211
|
+
const names = [...fn.params.flatMap(patternNames), ...patternNames(fn.type === "FunctionExpression" ? fn.id : undefined)];
|
|
212
|
+
const body = fn.body;
|
|
213
|
+
for (const statement of body.type === "BlockStatement" ? body.body : []) {
|
|
214
|
+
if (statement.type === "VariableDeclaration")
|
|
215
|
+
for (const declarator of statement.declarations)
|
|
216
|
+
names.push(...patternNames(declarator.id));
|
|
217
|
+
if (DECLARATIONS.has(statement.type))
|
|
218
|
+
names.push(...patternNames(statement.id));
|
|
219
|
+
}
|
|
220
|
+
const vars = (value) => {
|
|
221
|
+
if (Array.isArray(value))
|
|
222
|
+
return value.forEach(vars);
|
|
223
|
+
if (!isNode(value) || FUNCTIONS.has(value.type))
|
|
224
|
+
return;
|
|
225
|
+
if (value.type === "VariableDeclaration" && value.kind === "var") {
|
|
226
|
+
for (const declarator of value.declarations)
|
|
227
|
+
names.push(...patternNames(declarator.id));
|
|
228
|
+
}
|
|
229
|
+
for (const child of Object.values(value))
|
|
230
|
+
if (child && typeof child === "object")
|
|
231
|
+
vars(child);
|
|
232
|
+
};
|
|
233
|
+
vars(body);
|
|
234
|
+
return names;
|
|
235
|
+
};
|
|
236
|
+
// The names a node's code reads from outside itself, and its text without types.
|
|
237
|
+
const scan = (code, root) => {
|
|
238
|
+
const references = [];
|
|
239
|
+
const types = [];
|
|
240
|
+
const visit = (value, scope, parent, key) => {
|
|
241
|
+
if (Array.isArray(value)) {
|
|
242
|
+
for (const item of value)
|
|
243
|
+
visit(item, scope, parent, key);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (!isNode(value))
|
|
247
|
+
return;
|
|
248
|
+
if (FUNCTIONS.has(value.type))
|
|
249
|
+
scope = new Set([...scope, ...boundIn(value)]);
|
|
250
|
+
if ((key && TYPE_KEYS.has(key)) || TYPE_NODES.has(value.type)) {
|
|
251
|
+
types.push(value);
|
|
252
|
+
}
|
|
253
|
+
else if (value.type === "Identifier") {
|
|
254
|
+
if (!isPropertyName(parent, key) && !scope.has(value.name))
|
|
255
|
+
references.push({ name: value.name });
|
|
256
|
+
if (isNode(value.typeAnnotation))
|
|
257
|
+
types.push(value.typeAnnotation);
|
|
258
|
+
}
|
|
259
|
+
else if (value.type === "MemberExpression" && !value.computed && value.object.type === "Identifier") {
|
|
260
|
+
const name = value.object.name;
|
|
261
|
+
if (!scope.has(name))
|
|
262
|
+
references.push({ name, member: nameOf(value.property) });
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
for (const [childKey, child] of Object.entries(value))
|
|
266
|
+
if (child && typeof child === "object")
|
|
267
|
+
visit(child, scope, value, childKey);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
visit(root, new Set());
|
|
271
|
+
let text = "";
|
|
272
|
+
let at = root.start;
|
|
273
|
+
for (const type of types.sort((a, b) => a.start - b.start)) {
|
|
274
|
+
if (type.start < at)
|
|
275
|
+
continue;
|
|
276
|
+
text += code.slice(at, type.start);
|
|
277
|
+
at = type.end;
|
|
278
|
+
}
|
|
279
|
+
return { references, text: text + code.slice(at, root.end) };
|
|
280
|
+
};
|
|
281
|
+
/** `automation id -> registration hash` for each provider trigger the module at `file` exports. */
|
|
282
|
+
export async function registrationHashes(file, source) {
|
|
283
|
+
const modules = new Map();
|
|
284
|
+
const open = (target) => {
|
|
285
|
+
if (!modules.has(target)) {
|
|
286
|
+
const loaded = source.load(target);
|
|
287
|
+
modules.set(target, loaded ? moduleOf(target, loaded.code, loaded.program) : null);
|
|
288
|
+
}
|
|
289
|
+
return modules.get(target);
|
|
290
|
+
};
|
|
291
|
+
const resolved = new Map();
|
|
292
|
+
const imported = (from, specifier) => {
|
|
293
|
+
const key = `${from.file}\0${specifier}`;
|
|
294
|
+
if (!resolved.has(key))
|
|
295
|
+
resolved.set(key, source.resolve(specifier, from.file).then((target) => (target ? open(target) : null)));
|
|
296
|
+
return resolved.get(key);
|
|
297
|
+
};
|
|
298
|
+
// Everything a module exports, by name: its own, then each `export *` module's but `default`.
|
|
299
|
+
const exportsOf = async (module, visited = new Set()) => {
|
|
300
|
+
const all = new Map();
|
|
301
|
+
for (const [name, exported] of module.exports)
|
|
302
|
+
all.set(name, { module, ...exported });
|
|
303
|
+
visited.add(module);
|
|
304
|
+
for (const star of module.stars) {
|
|
305
|
+
const target = await imported(module, star);
|
|
306
|
+
if (!target || visited.has(target))
|
|
307
|
+
continue;
|
|
308
|
+
for (const [name, exported] of await exportsOf(target, visited))
|
|
309
|
+
if (name !== "default" && !all.has(name))
|
|
310
|
+
all.set(name, exported);
|
|
311
|
+
}
|
|
312
|
+
return all;
|
|
313
|
+
};
|
|
314
|
+
const isNamespace = (module, node) => {
|
|
315
|
+
const binding = module.bindings.get(nameOf(node) ?? "");
|
|
316
|
+
return !!binding && "name" in binding && binding.name === "*";
|
|
317
|
+
};
|
|
318
|
+
// The one walk through bindings, imports and re-exports. `member` picks a namespace's export, or a const object's
|
|
319
|
+
// property; `visited` ends a cycle.
|
|
320
|
+
const declarationOf = async (module, name, member, visited) => {
|
|
321
|
+
const binding = module.bindings.get(name);
|
|
322
|
+
const key = `${module.file}#${name}`;
|
|
323
|
+
if (!binding || visited.has(key))
|
|
324
|
+
return undefined;
|
|
325
|
+
visited.add(key);
|
|
326
|
+
if ("source" in binding)
|
|
327
|
+
return importOf(module, binding, member, visited);
|
|
328
|
+
const property = member ? propertyOf(binding.value, member) : undefined;
|
|
329
|
+
return { kind: "declared", module, node: property ?? binding.node, value: property ? property.value : binding.value };
|
|
330
|
+
};
|
|
331
|
+
const importOf = async (module, { source: specifier, name }, member, visited) => {
|
|
332
|
+
const target = await imported(module, specifier);
|
|
333
|
+
if (!target)
|
|
334
|
+
return { kind: "text", text: `${specifier}#${name}` };
|
|
335
|
+
if (name !== "*")
|
|
336
|
+
return exportOf(target, name, member, visited);
|
|
337
|
+
return member ? exportOf(target, member, undefined, visited) : { kind: "namespace", module: target };
|
|
338
|
+
};
|
|
339
|
+
const exportOf = async (module, name, member, visited) => {
|
|
340
|
+
const key = `${module.file}>${name}`;
|
|
341
|
+
if (visited.has(key))
|
|
342
|
+
return undefined;
|
|
343
|
+
visited.add(key);
|
|
344
|
+
const found = (await exportsOf(module)).get(name);
|
|
345
|
+
// One this can't place, or a module that doesn't parse: its whole source stands in.
|
|
346
|
+
if (!found)
|
|
347
|
+
return { kind: "text", text: module.code };
|
|
348
|
+
if ("local" in found)
|
|
349
|
+
return declarationOf(found.module, found.local, member, visited);
|
|
350
|
+
return importOf(found.module, found, member, visited);
|
|
351
|
+
};
|
|
352
|
+
const valueAt = (target) => target?.kind === "declared" && target.value ? { module: target.module, node: target.value } : undefined;
|
|
353
|
+
// A value as written, through parentheses, type assertions, consts, imports, re-exports and namespaces.
|
|
354
|
+
const valueOf = async (at, visited = new Set()) => {
|
|
355
|
+
let node = at?.node;
|
|
356
|
+
while (node && WRAPPERS.has(node.type))
|
|
357
|
+
node = node.expression;
|
|
358
|
+
if (!at || !node)
|
|
359
|
+
return undefined;
|
|
360
|
+
const { module } = at;
|
|
361
|
+
if (node.type === "Identifier")
|
|
362
|
+
return valueOf(valueAt(await declarationOf(module, node.name, undefined, visited)), visited);
|
|
363
|
+
if (node.type !== "MemberExpression" || node.computed || !isNamespace(module, node.object))
|
|
364
|
+
return { module, node };
|
|
365
|
+
return valueOf(valueAt(await declarationOf(module, nameOf(node.object), nameOf(node.property), visited)), visited);
|
|
366
|
+
};
|
|
367
|
+
const isFactory = (module, callee) => {
|
|
368
|
+
if (callee.type === "MemberExpression") {
|
|
369
|
+
return !callee.computed && nameOf(callee.property) === "createAutomation" && isNamespace(module, callee.object);
|
|
370
|
+
}
|
|
371
|
+
const binding = module.bindings.get(nameOf(callee) ?? "");
|
|
372
|
+
return !!binding && "name" in binding && binding.name === "createAutomation";
|
|
373
|
+
};
|
|
374
|
+
// `createAutomation(config).handler(fn)`, down to its config.
|
|
375
|
+
const configOf = async (at, visited = new Set()) => {
|
|
376
|
+
if (!at || visited.has(at.node))
|
|
377
|
+
return undefined;
|
|
378
|
+
visited.add(at.node);
|
|
379
|
+
const { module, node } = at;
|
|
380
|
+
if (node.type === "CallExpression") {
|
|
381
|
+
const callee = node.callee;
|
|
382
|
+
if (isFactory(module, callee))
|
|
383
|
+
return valueOf({ module, node: node.arguments[0] });
|
|
384
|
+
return configOf(await valueOf({ module, node: callee }), visited);
|
|
385
|
+
}
|
|
386
|
+
return node.type === "MemberExpression" ? configOf(await valueOf({ module, node: node.object }), visited) : undefined;
|
|
387
|
+
};
|
|
388
|
+
// A property as written, through spreads; "unknown" when the object or a spread after it can't be read.
|
|
389
|
+
const property = async (object, key, visited = new Set()) => {
|
|
390
|
+
if (object?.node.type !== "ObjectExpression" || visited.has(object.node))
|
|
391
|
+
return "unknown";
|
|
392
|
+
// The objects on this path only: one two spreads share is read twice, and only a cycle is unknown.
|
|
393
|
+
visited.add(object.node);
|
|
394
|
+
try {
|
|
395
|
+
for (const p of [...object.node.properties].reverse()) {
|
|
396
|
+
if (p.type === "SpreadElement") {
|
|
397
|
+
const found = await property(await valueOf({ module: object.module, node: p.argument }), key, visited);
|
|
398
|
+
if (found !== "absent")
|
|
399
|
+
return found;
|
|
400
|
+
}
|
|
401
|
+
else if (p.computed) {
|
|
402
|
+
return "unknown";
|
|
403
|
+
}
|
|
404
|
+
else if (nameOf(p.key) === key) {
|
|
405
|
+
return { module: object.module, node: p.value };
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return "absent";
|
|
409
|
+
}
|
|
410
|
+
finally {
|
|
411
|
+
visited.delete(object.node);
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
// The code and everything it reaches: the module-level declarations it names, followed into the modules it
|
|
415
|
+
// imports them from. A package stands in by its name.
|
|
416
|
+
const reached = async (start) => {
|
|
417
|
+
const pieces = [];
|
|
418
|
+
const seen = new Set();
|
|
419
|
+
const once = (key) => !seen.has(key) && !!seen.add(key);
|
|
420
|
+
const follow = async (target) => {
|
|
421
|
+
if (target?.kind === "declared") {
|
|
422
|
+
const { module, node } = target;
|
|
423
|
+
if (!once(`${module.file}:${node.start}:${node.end}`))
|
|
424
|
+
return;
|
|
425
|
+
const { references, text } = scan(module.code, node);
|
|
426
|
+
pieces.push(text);
|
|
427
|
+
for (const { name, member } of references)
|
|
428
|
+
await follow(await declarationOf(module, name, member, new Set()));
|
|
429
|
+
}
|
|
430
|
+
else if (target?.kind === "namespace") {
|
|
431
|
+
if (!once(`${target.module.file}#*`))
|
|
432
|
+
return;
|
|
433
|
+
for (const name of (await exportsOf(target.module)).keys())
|
|
434
|
+
await follow(await exportOf(target.module, name, undefined, new Set()));
|
|
435
|
+
}
|
|
436
|
+
else if (target && once(target.text)) {
|
|
437
|
+
pieces.push(target.text);
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
await follow({ kind: "declared", ...start });
|
|
441
|
+
return pieces.join("\n");
|
|
442
|
+
};
|
|
443
|
+
const hashOf = async (root, name) => {
|
|
444
|
+
const config = await configOf(await valueOf(valueAt(await exportOf(root, name, undefined, new Set()))));
|
|
445
|
+
// Not a createAutomation this can read: describe hashes the built functions.
|
|
446
|
+
if (!config)
|
|
447
|
+
return undefined;
|
|
448
|
+
const trigger = await property(config, "trigger");
|
|
449
|
+
if (trigger === "absent")
|
|
450
|
+
return undefined;
|
|
451
|
+
if (trigger === "unknown")
|
|
452
|
+
return registrationHash(await reached(config), undefined);
|
|
453
|
+
const declared = await valueOf(trigger);
|
|
454
|
+
const subscribe = await property(declared, "subscribe");
|
|
455
|
+
const unsubscribe = await property(declared, "unsubscribe");
|
|
456
|
+
if (subscribe === "absent")
|
|
457
|
+
return undefined;
|
|
458
|
+
if (subscribe === "unknown" || unsubscribe === "unknown")
|
|
459
|
+
return registrationHash(await reached(trigger), undefined);
|
|
460
|
+
return registrationHash(await reached(subscribe), unsubscribe === "absent" ? undefined : await reached(unsubscribe));
|
|
461
|
+
};
|
|
462
|
+
const found = new Map();
|
|
463
|
+
const root = open(file);
|
|
464
|
+
for (const name of root ? (await exportsOf(root)).keys() : []) {
|
|
465
|
+
const hash = await hashOf(root, name);
|
|
466
|
+
if (hash)
|
|
467
|
+
found.set(name, hash);
|
|
468
|
+
}
|
|
469
|
+
return found;
|
|
470
|
+
}
|
|
@@ -1,17 +1,31 @@
|
|
|
1
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
1
2
|
import { type AnyRoute, type RouterConstructorOptions, type RouterHistory } from "@tanstack/react-router";
|
|
2
|
-
import type { User } from "./types.js";
|
|
3
|
+
import type { HostMode, User } from "./types.js";
|
|
4
|
+
export declare const QUERY_STALE_TIME_MS = 30000;
|
|
3
5
|
/** The router context every Zite app's root route declares: `createRootRouteWithContext<ZiteRouterContext>()`. */
|
|
4
6
|
export type ZiteRouterContext = {
|
|
5
7
|
user: User | null;
|
|
8
|
+
queryClient: QueryClient;
|
|
6
9
|
};
|
|
7
10
|
type Dehydrated = {
|
|
8
11
|
user: User | null;
|
|
12
|
+
mode?: HostMode;
|
|
9
13
|
};
|
|
14
|
+
/** @internal What a page carries to the browser beside its queries: the signed-in user, and the mode it was served in. */
|
|
15
|
+
export declare const dehydrateZite: (context: ZiteRouterContext) => Dehydrated;
|
|
16
|
+
/** @internal Takes in what the page carried. */
|
|
17
|
+
export declare const hydrateZite: (context: ZiteRouterContext, dehydrated: Dehydrated) => void;
|
|
10
18
|
type ZiteRouterOptions<TRouteTree extends AnyRoute> = Omit<RouterConstructorOptions<TRouteTree, "never", false, RouterHistory, Dehydrated>, "context" | "dehydrate" | "hydrate">;
|
|
11
19
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
|
|
20
|
+
* @internal Tells the editor's preview the page crashed, as classic's runtime reports an emptied #root: a Start page
|
|
21
|
+
* has none, and an error a route's boundary catches is the crash. Being signed out isn't one.
|
|
22
|
+
*/
|
|
23
|
+
export declare const reportCrash: (error: unknown) => void;
|
|
24
|
+
/**
|
|
25
|
+
* The router for `src/router.tsx`. The signed-in user is in `context.user`: on the server from the platform, in the
|
|
26
|
+
* browser from the page it rendered, so no request asks for it again. `context.queryClient` is new for each request
|
|
27
|
+
* on the server, so a page never renders with another visitor's cached queries, and its queries reach the browser
|
|
28
|
+
* with the page.
|
|
15
29
|
*/
|
|
16
30
|
export declare function createZiteRouter<TRouteTree extends AnyRoute>(options: ZiteRouterOptions<TRouteTree>): import("@tanstack/router-core").RouterCore<TRouteTree, "never", false, RouterHistory, Dehydrated>;
|
|
17
31
|
export {};
|
package/dist/esm/start/router.js
CHANGED
|
@@ -1,34 +1,157 @@
|
|
|
1
|
-
import { createElement as h } from "react";
|
|
1
|
+
import { createElement as h, useEffect, useState } from "react";
|
|
2
|
+
import { QueryClient } from "@tanstack/react-query";
|
|
2
3
|
import { createRouter, Link, useRouter, } from "@tanstack/react-router";
|
|
4
|
+
import { setupRouterSsrQueryIntegration } from "@tanstack/react-router-ssr-query";
|
|
5
|
+
import { asCrossed, errorFromThrown, isSignInError, withThrownRejections } from "./errors.js";
|
|
6
|
+
import { carryPreviewClaim, PREVIEW_CLAIM_PARAM, previewClaim } from "./previewClaim.js";
|
|
3
7
|
import { currentHost } from "./host.js";
|
|
8
|
+
import { keepRenderedMode } from "../internal/appMode.js";
|
|
9
|
+
// Long enough that a page's queries don't refetch as the browser picks it up, short enough to stay fresh.
|
|
10
|
+
export const QUERY_STALE_TIME_MS = 30_000;
|
|
11
|
+
/** @internal What a page carries to the browser beside its queries: the signed-in user, and the mode it was served in. */
|
|
12
|
+
export const dehydrateZite = (context) => ({ user: context.user, mode: currentHost()?.mode });
|
|
13
|
+
/** @internal Takes in what the page carried. */
|
|
14
|
+
export const hydrateZite = (context, dehydrated) => {
|
|
15
|
+
context.user = dehydrated.user;
|
|
16
|
+
keepRenderedMode(dehydrated.mode);
|
|
17
|
+
};
|
|
18
|
+
const ziteWindow = () => (typeof window === "undefined" ? undefined : window);
|
|
4
19
|
const box = { maxWidth: 480, margin: "15vh auto", padding: 24, fontFamily: "system-ui, sans-serif", textAlign: "center" };
|
|
20
|
+
/**
|
|
21
|
+
* @internal Tells the editor's preview the page crashed, as classic's runtime reports an emptied #root: a Start page
|
|
22
|
+
* has none, and an error a route's boundary catches is the crash. Being signed out isn't one.
|
|
23
|
+
*/
|
|
24
|
+
export const reportCrash = (error) => {
|
|
25
|
+
if (!isSignInError(error))
|
|
26
|
+
ziteWindow()?._ziteOnCrash?.(error);
|
|
27
|
+
};
|
|
28
|
+
// As classic's crash screen: the message, "Fix it for me" in the editor, and a reload. The server renders from the
|
|
29
|
+
// error the router already mapped and the browser from the one that crossed, so both show the same text.
|
|
5
30
|
function ZiteErrorComponent({ error }) {
|
|
6
31
|
const router = useRouter();
|
|
7
|
-
const
|
|
8
|
-
|
|
32
|
+
const [allowAiFix, setAllowAiFix] = useState(false);
|
|
33
|
+
useEffect(() => setAllowAiFix(ziteWindow()?.__ziteAllowAiFix === true), []);
|
|
34
|
+
useEffect(() => markReady("error"), []);
|
|
35
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
36
|
+
const reload = () => {
|
|
37
|
+
const onReload = ziteWindow()?._ziteOnReload;
|
|
38
|
+
if (onReload)
|
|
39
|
+
onReload();
|
|
40
|
+
else
|
|
41
|
+
router.invalidate();
|
|
42
|
+
};
|
|
43
|
+
return h("div", { role: "alert", style: box }, h("p", { style: { fontWeight: 600 } }, "Something went wrong"), h("pre", { style: { whiteSpace: "pre-wrap", textAlign: "left" } }, message), allowAiFix &&
|
|
44
|
+
!isSignInError(error) &&
|
|
45
|
+
h("button", { type: "button", onClick: () => ziteWindow()?._ziteOnFixError?.(error) }, "Fix it for me"), h("button", { type: "button", onClick: reload }, "Try again"));
|
|
9
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* `<html data-zite-ready>`: what the platform's screenshots wait for. `1` once
|
|
49
|
+
* the page has rendered with nothing loading, `error` when the error screen did.
|
|
50
|
+
*/
|
|
51
|
+
const markReady = (state) => {
|
|
52
|
+
if (typeof document !== "undefined")
|
|
53
|
+
document.documentElement.dataset.ziteReady = state;
|
|
54
|
+
};
|
|
55
|
+
const watchReady = (router) => () => {
|
|
56
|
+
const { status, isLoading, matches } = router.state;
|
|
57
|
+
if (status !== "idle" || isLoading || matches.some((match) => match.status === "pending"))
|
|
58
|
+
return;
|
|
59
|
+
markReady(matches.some((match) => match.status === "error") ? "error" : "1");
|
|
60
|
+
};
|
|
10
61
|
function ZiteNotFound() {
|
|
11
62
|
return h("div", { style: box }, h("p", { style: { fontWeight: 600 } }, "Page not found"), h(Link, { to: "/" }, "Go home"));
|
|
12
63
|
}
|
|
64
|
+
function ZitePending() {
|
|
65
|
+
return h("div", { "aria-busy": true, style: { ...box, color: "#888" } }, "Loading…");
|
|
66
|
+
}
|
|
67
|
+
const rejectingWithErrors = (load) => (...args) => {
|
|
68
|
+
const out = load(...args);
|
|
69
|
+
return out instanceof Promise ? out.then(withThrownRejections) : withThrownRejections(out);
|
|
70
|
+
};
|
|
71
|
+
// A loader's error crosses to the browser as thrown, and so does a deferred value's rejection; only an Error goes
|
|
72
|
+
// through the zite-error adapter. A route's onError may replace its error; routes outlive a request's router, so each
|
|
73
|
+
// is wrapped once.
|
|
74
|
+
const wrapped = new WeakSet();
|
|
75
|
+
const failWithErrors = (routes) => {
|
|
76
|
+
for (const route of routes) {
|
|
77
|
+
if (wrapped.has(route))
|
|
78
|
+
continue;
|
|
79
|
+
wrapped.add(route);
|
|
80
|
+
const own = route.options.onError;
|
|
81
|
+
const loader = route.options.loader;
|
|
82
|
+
route.update({
|
|
83
|
+
onError: (error) => {
|
|
84
|
+
let failure = error;
|
|
85
|
+
try {
|
|
86
|
+
own?.(error);
|
|
87
|
+
}
|
|
88
|
+
catch (replaced) {
|
|
89
|
+
failure = replaced;
|
|
90
|
+
}
|
|
91
|
+
const normalized = errorFromThrown(failure);
|
|
92
|
+
// The server renders a failed route's error component in-process.
|
|
93
|
+
if (typeof window === "undefined" && normalized instanceof Error)
|
|
94
|
+
throw asCrossed(normalized);
|
|
95
|
+
if (normalized !== error)
|
|
96
|
+
throw normalized;
|
|
97
|
+
},
|
|
98
|
+
...(loader && {
|
|
99
|
+
loader: typeof loader === "function" ? rejectingWithErrors(loader) : { ...loader, handler: rejectingWithErrors(loader.handler) },
|
|
100
|
+
}),
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
// The claim stays on every URL the router writes, and the app's own routes never see it.
|
|
105
|
+
const keepPreviewClaim = (claim, own) => ({
|
|
106
|
+
input: ({ url }) => {
|
|
107
|
+
url.searchParams.delete(PREVIEW_CLAIM_PARAM);
|
|
108
|
+
return own?.input?.({ url }) ?? url;
|
|
109
|
+
},
|
|
110
|
+
output: ({ url }) => {
|
|
111
|
+
const rewritten = own?.output?.({ url }) ?? url;
|
|
112
|
+
const out = new URL(rewritten.toString(), url);
|
|
113
|
+
out.searchParams.set(PREVIEW_CLAIM_PARAM, claim);
|
|
114
|
+
return out;
|
|
115
|
+
},
|
|
116
|
+
});
|
|
13
117
|
/**
|
|
14
|
-
* The router for `src/router.tsx`. The signed-in user is in `context.user`: on
|
|
15
|
-
*
|
|
16
|
-
*
|
|
118
|
+
* The router for `src/router.tsx`. The signed-in user is in `context.user`: on the server from the platform, in the
|
|
119
|
+
* browser from the page it rendered, so no request asks for it again. `context.queryClient` is new for each request
|
|
120
|
+
* on the server, so a page never renders with another visitor's cached queries, and its queries reach the browser
|
|
121
|
+
* with the page.
|
|
17
122
|
*/
|
|
18
123
|
export function createZiteRouter(options) {
|
|
19
|
-
const
|
|
124
|
+
const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime: QUERY_STALE_TIME_MS } } });
|
|
125
|
+
const claim = previewClaim();
|
|
126
|
+
if (claim)
|
|
127
|
+
carryPreviewClaim(claim);
|
|
128
|
+
const context = { user: currentHost()?.user ?? null, queryClient };
|
|
20
129
|
const router = createRouter({
|
|
21
130
|
defaultPreload: "intent",
|
|
22
131
|
defaultPreloadStaleTime: 0,
|
|
23
132
|
scrollRestoration: true,
|
|
24
133
|
defaultErrorComponent: ZiteErrorComponent,
|
|
25
134
|
defaultNotFoundComponent: ZiteNotFound,
|
|
135
|
+
defaultPendingComponent: ZitePending,
|
|
26
136
|
...options,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
137
|
+
...(claim && { rewrite: keepPreviewClaim(claim, options.rewrite) }),
|
|
138
|
+
// Whichever screen shows it: a route may have its own.
|
|
139
|
+
defaultOnCatch: (error, info) => {
|
|
140
|
+
markReady("error");
|
|
141
|
+
reportCrash(error);
|
|
142
|
+
options.defaultOnCatch?.(error, info);
|
|
31
143
|
},
|
|
144
|
+
context,
|
|
145
|
+
dehydrate: () => dehydrateZite(context),
|
|
146
|
+
hydrate: (dehydrated) => hydrateZite(context, dehydrated),
|
|
32
147
|
});
|
|
148
|
+
failWithErrors(Object.values(router.routesById));
|
|
149
|
+
setupRouterSsrQueryIntegration({ router, queryClient });
|
|
150
|
+
const mark = watchReady(router);
|
|
151
|
+
router.subscribe("onResolved", mark);
|
|
152
|
+
router.subscribe("onRendered", mark);
|
|
153
|
+
const host = currentHost();
|
|
154
|
+
if (host)
|
|
155
|
+
host.router = router;
|
|
33
156
|
return router;
|
|
34
157
|
}
|