zitejs 0.10.0-start.22 → 0.10.0-start.23
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/start/provider.d.ts +2 -2
- package/dist/cjs/start/provider.js +3 -3
- package/dist/cjs/start/types.d.ts +29 -47
- package/dist/cjs/sync/lib.js +23 -50
- package/dist/cjs/sync/sdkNames.d.ts +65 -7
- package/dist/cjs/sync/sdkNames.js +76 -12
- package/dist/cjs/sync/sdkNames.test.js +38 -0
- package/dist/esm/start/airtable/built.d.ts +2 -0
- package/dist/esm/start/airtable/built.js +3 -0
- package/dist/esm/start/airtable/index.d.ts +3 -5
- package/dist/esm/start/airtable/index.js +4 -25
- package/dist/esm/start/airtable/settings.d.ts +31 -0
- package/dist/esm/start/airtable/settings.js +110 -0
- package/dist/esm/start/data/index.d.ts +21 -9
- package/dist/esm/start/data/index.js +26 -20
- package/dist/esm/start/data/names.d.ts +7 -0
- package/dist/esm/start/data/names.js +15 -0
- package/dist/esm/start/data/types.d.ts +4 -1
- package/dist/esm/start/direct.d.ts +16 -12
- package/dist/esm/start/direct.js +185 -56
- package/dist/esm/start/outbound.d.ts +1 -2
- package/dist/esm/start/outbound.js +8 -19
- package/dist/esm/start/provider.d.ts +2 -2
- package/dist/esm/start/provider.js +3 -3
- package/dist/esm/start/server.d.ts +17 -13
- package/dist/esm/start/server.js +41 -43
- package/dist/esm/start/telemetry.d.ts +4 -1
- package/dist/esm/start/telemetry.js +13 -16
- package/dist/esm/start/types.d.ts +29 -47
- package/dist/esm/start/version.d.ts +1 -1
- package/dist/esm/start/version.js +1 -1
- package/dist/esm/start/vite.d.ts +7 -1
- package/dist/esm/start/vite.js +68 -21
- package/dist/esm/start/workspaceSchema.d.ts +2 -1
- package/dist/esm/start/workspaceSchema.js +3 -3
- package/dist/esm/sync/lib.js +23 -50
- package/dist/esm/sync/sdkNames.d.ts +65 -7
- package/dist/esm/sync/sdkNames.js +73 -11
- package/dist/esm/sync/sdkNames.test.js +38 -0
- package/package.json +2 -2
package/dist/esm/start/direct.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
// Data and services on the request's app token, sent straight to the platform.
|
|
2
|
-
//
|
|
1
|
+
// Data and services on the request's app token, sent straight to the platform. Each answer is shaped as the host's relay
|
|
2
|
+
// shaped it, so what an app sees didn't change when the relay went. A request with no token or platform URLs reaches
|
|
3
|
+
// nothing: the host dispatches every request with both, and `local` mints its own.
|
|
3
4
|
import { appTokenClaims } from "../internal/appToken.js";
|
|
4
5
|
import { platformFetch } from "../internal/platformFetch.js";
|
|
5
6
|
import { serviceUrl } from "../internal/serviceUrls.js";
|
|
@@ -10,12 +11,15 @@ import { errorOf, preview } from "./runLog.js";
|
|
|
10
11
|
import { STEP_RESULT_MAX_BYTES } from "./steps.js";
|
|
11
12
|
import { sendRunReport } from "./telemetry.js";
|
|
12
13
|
const STEPS_INTEGRATION_ID = "__steps__";
|
|
14
|
+
const META_INTEGRATION_ID = "__meta__";
|
|
15
|
+
// Unpublished from zitejs; classic answers it as an integration it can't find.
|
|
16
|
+
const NOTIFICATIONS_INTEGRATION_ID = "__notifications__";
|
|
13
17
|
const PDF_INTEGRATION_ID = "__pdf__";
|
|
14
18
|
const SCHEDULES_INTEGRATION_ID = "__schedules__";
|
|
15
19
|
const BACKEND_TIMEOUT_MS = 5_000;
|
|
16
20
|
// Attachments take up to 30 s each, and a render 30 s, as on classic.
|
|
17
21
|
const SDK_TIMEOUT_MS = 90_000;
|
|
18
|
-
// One request's fan-out, as the host capped it; base-runner's per-app bucket is what
|
|
22
|
+
// One request's fan-out, as the host once capped it; base-runner's per-app bucket is what bounds an app.
|
|
19
23
|
const DATA_CONCURRENCY = 16;
|
|
20
24
|
const SQL_CONCURRENCY = 4;
|
|
21
25
|
const SQL_PATH = /\/sql(?:\?|$)/;
|
|
@@ -29,13 +33,14 @@ class PlatformError extends Error {
|
|
|
29
33
|
}
|
|
30
34
|
}
|
|
31
35
|
const DEMO_REFUSED = { error: { code: "FORBIDDEN", message: DEMO_UNAVAILABLE_MESSAGE } };
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
36
|
+
/** Thrown for a platform call on a request that has no token or no platform URLs. */
|
|
37
|
+
export class NoPlatformError extends Error {
|
|
38
|
+
constructor() {
|
|
39
|
+
super("This request has no app token or platform URLs: Zite didn't dispatch it, or its host predates this zitejs");
|
|
40
|
+
this.name = "NoPlatformError";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** The request's token and where to send it, renewed first if it's about to expire; undefined without either. */
|
|
39
44
|
export const directAccess = async (host) => {
|
|
40
45
|
const urls = host.provider?.urls;
|
|
41
46
|
const token = urls ? await host.provider?.token() : undefined;
|
|
@@ -87,7 +92,7 @@ export const directDataFetch = (host) => async ({ method, path, body }) => {
|
|
|
87
92
|
try {
|
|
88
93
|
const access = await directAccess(host);
|
|
89
94
|
if (!access)
|
|
90
|
-
throw new
|
|
95
|
+
throw new NoPlatformError();
|
|
91
96
|
return await send(access, `${access.urls.baseRunner}${path}`, { method, body });
|
|
92
97
|
}
|
|
93
98
|
finally {
|
|
@@ -159,28 +164,31 @@ const steps = async (host, access, { methodName, params }) => {
|
|
|
159
164
|
const CONNECTIONS_REUSE_MS = 60_000;
|
|
160
165
|
const CONNECTIONS_MAX_AGE_MS = 10 * 60_000;
|
|
161
166
|
// Only answers, never a pending call: Workers ties a request's I/O to that request.
|
|
162
|
-
let
|
|
163
|
-
/**
|
|
164
|
-
|
|
167
|
+
let tokenHeld;
|
|
168
|
+
/**
|
|
169
|
+
* The backend's `GET connections` on the token, once per reuse window: all `local` has, and what an app on Zite asks
|
|
170
|
+
* when the host didn't hand them over. Stale if the backend didn't answer.
|
|
171
|
+
*/
|
|
172
|
+
export const connectionsOnToken = async (access) => {
|
|
165
173
|
const claims = appTokenClaims(access.token);
|
|
166
|
-
const key = `${claims?.app ?? ""}:${claims?.ver ?? ""}`;
|
|
174
|
+
const key = `${claims?.app ?? ""}:${claims?.build ?? ""}:${claims?.ver ?? ""}`;
|
|
167
175
|
const reuseMs = typeof claims?.ver === "number" ? CONNECTIONS_MAX_AGE_MS : CONNECTIONS_REUSE_MS;
|
|
168
|
-
if (
|
|
169
|
-
return
|
|
176
|
+
if (tokenHeld?.key === key && Date.now() - tokenHeld.at < reuseMs)
|
|
177
|
+
return tokenHeld.answer;
|
|
170
178
|
try {
|
|
171
179
|
const answer = await backendJson(access, "GET", "/connections");
|
|
172
|
-
|
|
180
|
+
tokenHeld = {
|
|
173
181
|
key,
|
|
174
182
|
at: Date.now(),
|
|
175
183
|
answer: { connections: answer.connections ?? {}, env: answer.env ?? {}, envConnections: answer.envConnections ?? {} },
|
|
176
184
|
};
|
|
177
185
|
}
|
|
178
186
|
catch (error) {
|
|
179
|
-
if (!
|
|
187
|
+
if (!tokenHeld)
|
|
180
188
|
throw error;
|
|
181
189
|
console.error("[zite] couldn't refresh connections; using the last ones", error);
|
|
182
190
|
}
|
|
183
|
-
return
|
|
191
|
+
return tokenHeld.answer;
|
|
184
192
|
};
|
|
185
193
|
// A dev token names the newest build, whose page settings change only with another.
|
|
186
194
|
const PAGE_REUSE_MS = 60_000;
|
|
@@ -201,32 +209,133 @@ export const localPagePlan = async (access) => {
|
|
|
201
209
|
localPage = { key, at: Date.now(), plan };
|
|
202
210
|
return plan;
|
|
203
211
|
};
|
|
212
|
+
// The app's place in Zite, which moves only with the app, and with it the dev token's base.
|
|
213
|
+
let localWorkspace;
|
|
214
|
+
/** `ZiteMeta.getWorkspace()` for `local`, which has no build config in props: the backend's, for the dev token's app. */
|
|
215
|
+
const localWorkspaceOf = async (access) => {
|
|
216
|
+
const claims = appTokenClaims(access.token);
|
|
217
|
+
const key = `${claims?.app ?? ""}:${claims?.base ?? ""}`;
|
|
218
|
+
if (localWorkspace?.key === key)
|
|
219
|
+
return localWorkspace.workspace;
|
|
220
|
+
const { workspace } = await backendJson(access, "GET", "/workspace");
|
|
221
|
+
if (workspace)
|
|
222
|
+
localWorkspace = { key, workspace };
|
|
223
|
+
return workspace;
|
|
224
|
+
};
|
|
225
|
+
/** `ZiteMeta`'s one method, as the host answered it from the build's config. */
|
|
226
|
+
const metaAnswer = (methodName, workspace) => {
|
|
227
|
+
if (methodName !== "getWorkspace")
|
|
228
|
+
return { error: { code: "BAD_REQUEST", message: `Unknown ZiteMeta method: ${methodName}` } };
|
|
229
|
+
return workspace
|
|
230
|
+
? { value: workspace }
|
|
231
|
+
: { error: { code: "INTERNAL_ERROR", message: "Publish the app again to use ZiteMeta.getWorkspace" } };
|
|
232
|
+
};
|
|
204
233
|
/** For tests: forget what this isolate learned. */
|
|
205
234
|
export const resetLocalConnections = () => {
|
|
206
|
-
|
|
235
|
+
refreshed.clear();
|
|
236
|
+
tokenHeld = undefined;
|
|
207
237
|
localPage = undefined;
|
|
238
|
+
localWorkspace = undefined;
|
|
208
239
|
};
|
|
209
240
|
const notFound = (name) => ({ error: { code: "BAD_REQUEST", message: `Integration ${name} not found` } });
|
|
210
|
-
|
|
211
|
-
const localIntegration = async (access, { methodName, params }) => {
|
|
241
|
+
const nameOf = ({ methodName, params }) => {
|
|
212
242
|
const name = params?.name;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
243
|
+
return methodName === "get" && typeof name === "string" && name ? name : undefined;
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* `integrations.get` as the host answered it, from the connections the request was handed. A connection that needs
|
|
247
|
+
* reconnecting fails with classic's error, which visitors can see; the link to reconnect is a note for the run log,
|
|
248
|
+
* which only the owner reads.
|
|
249
|
+
*/
|
|
250
|
+
const connectionAnswer = (connections, name) => {
|
|
217
251
|
const answer = Object.hasOwn(connections, name) ? connections[name] : undefined;
|
|
218
252
|
if (!answer)
|
|
219
|
-
return notFound(name);
|
|
220
|
-
if (answer.status
|
|
221
|
-
return {
|
|
253
|
+
return { result: notFound(name) };
|
|
254
|
+
if (answer.status !== "unavailable")
|
|
255
|
+
return { result: { value: answer } };
|
|
256
|
+
const message = String(answer.message);
|
|
257
|
+
return {
|
|
258
|
+
result: {
|
|
222
259
|
error: {
|
|
223
260
|
code: "INTERNAL_ERROR",
|
|
224
|
-
message
|
|
261
|
+
message,
|
|
225
262
|
...(typeof answer.userFacingMessage === "string" && { userFacingMessage: answer.userFacingMessage }),
|
|
226
263
|
},
|
|
227
|
-
}
|
|
264
|
+
},
|
|
265
|
+
...(typeof answer.connectUrl === "string" && { note: `${message}; reconnect at ${answer.connectUrl}` }),
|
|
266
|
+
};
|
|
267
|
+
};
|
|
268
|
+
const MAX_REFRESHED = 50;
|
|
269
|
+
// A connection refreshed after a 401, by name, with the copy the host handed over when it was: later requests in the
|
|
270
|
+
// isolate use it rather than that copy, which failed, until the host hands over a different one.
|
|
271
|
+
const refreshed = new Map();
|
|
272
|
+
const keepRefreshed = (name, from, connection) => {
|
|
273
|
+
refreshed.delete(name);
|
|
274
|
+
refreshed.set(name, { from: JSON.stringify(from), connection });
|
|
275
|
+
if (refreshed.size > MAX_REFRESHED)
|
|
276
|
+
refreshed.delete(refreshed.keys().next().value);
|
|
277
|
+
};
|
|
278
|
+
const withRefreshed = (given, name) => {
|
|
279
|
+
const held = refreshed.get(name);
|
|
280
|
+
if (!held || !Object.hasOwn(given, name))
|
|
281
|
+
return given;
|
|
282
|
+
if (JSON.stringify(given[name]) !== held.from) {
|
|
283
|
+
refreshed.delete(name);
|
|
284
|
+
return given;
|
|
228
285
|
}
|
|
229
|
-
return {
|
|
286
|
+
return { ...given, [name]: held.connection };
|
|
287
|
+
};
|
|
288
|
+
// As the host's run log kept it: a connection's values are its secrets, so only its status is shown.
|
|
289
|
+
const redactedConnection = (value) => value && typeof value === "object" && value.status === "connected"
|
|
290
|
+
? Object.fromEntries(Object.entries(value).map(([key, field]) => [key, key !== "status" && typeof field === "string" ? "[redacted]" : field]))
|
|
291
|
+
: value;
|
|
292
|
+
/** A call answered in the app, kept for the run's detail, which goes with the response's. */
|
|
293
|
+
const noted = (host, input, result, { outputs = preview, note } = {}) => {
|
|
294
|
+
(host.calls ??= []).push({
|
|
295
|
+
integrationId: input.integrationId,
|
|
296
|
+
className: input.className,
|
|
297
|
+
methodName: input.methodName,
|
|
298
|
+
at: Date.now(),
|
|
299
|
+
durationMs: 0,
|
|
300
|
+
inputs: preview(input.params),
|
|
301
|
+
...("value" in result ? { outputs: outputs(result.value) } : { error: { code: result.error.code, message: note ?? result.error.message } }),
|
|
302
|
+
});
|
|
303
|
+
return result;
|
|
304
|
+
};
|
|
305
|
+
const UNLOADED = {
|
|
306
|
+
error: { code: "INTERNAL_ERROR", message: "The app's connections couldn't be loaded; try again in a moment" },
|
|
307
|
+
};
|
|
308
|
+
/**
|
|
309
|
+
* `integrations.get`'s set: the host's, with any refreshed since, when it handed them over; else the backend's on the
|
|
310
|
+
* token, asked on the first call. Undefined when they couldn't be loaded. A registration is handed its one connection
|
|
311
|
+
* or nothing, since the backend's answer would name the rest.
|
|
312
|
+
*/
|
|
313
|
+
const connectionsOf = async (host, name) => {
|
|
314
|
+
if (host.connections)
|
|
315
|
+
return withRefreshed(host.connections, name);
|
|
316
|
+
if (host.invoke?.kind === "register")
|
|
317
|
+
return undefined;
|
|
318
|
+
const access = await directAccess(host);
|
|
319
|
+
if (!access)
|
|
320
|
+
throw new NoPlatformError();
|
|
321
|
+
try {
|
|
322
|
+
return (await (host.connectionsLoad ??= connectionsOnToken(access))).connections;
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
console.error("[zite] couldn't load the app's connections", error);
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
/** `integrations.get`: one answer from whichever set the request has, recorded with the connection's values redacted. */
|
|
330
|
+
const integrationsGet = async (host, input) => {
|
|
331
|
+
const name = nameOf(input);
|
|
332
|
+
const connections = name ? await connectionsOf(host, name) : undefined;
|
|
333
|
+
const answered = !name
|
|
334
|
+
? { result: { error: { code: "BAD_REQUEST", message: "integrations.get(name): name must be a non-empty string" } } }
|
|
335
|
+
: connections
|
|
336
|
+
? connectionAnswer(connections, name)
|
|
337
|
+
: { result: UNLOADED };
|
|
338
|
+
return noted(host, input, answered.result, { outputs: (value) => preview(redactedConnection(value)), note: answered.note });
|
|
230
339
|
};
|
|
231
340
|
const somethingWentWrong = (host, input, error) => {
|
|
232
341
|
console.error("[zite] platform call failed", {
|
|
@@ -246,7 +355,7 @@ const report = (host, record) => {
|
|
|
246
355
|
const sent = sendRunReport(host, { runs: [], calls: [record] }).catch((error) => console.error("[zite] couldn't record a platform call", error));
|
|
247
356
|
host.waitUntil?.(sent);
|
|
248
357
|
};
|
|
249
|
-
// The
|
|
358
|
+
// The platform calls a request made, failures included, for its run, as the host once recorded the ones it relayed.
|
|
250
359
|
const recorded = async (host, input, run) => {
|
|
251
360
|
const at = Date.now();
|
|
252
361
|
const result = await run().catch((error) => somethingWentWrong(host, input, error));
|
|
@@ -261,53 +370,73 @@ const recorded = async (host, input, run) => {
|
|
|
261
370
|
});
|
|
262
371
|
return result;
|
|
263
372
|
};
|
|
264
|
-
/**
|
|
265
|
-
* A platform call on the token: undefined without one, or for a call only the host's session answers (`ZiteMeta`,
|
|
266
|
-
* and `integrations.get` on Zite). Errors come back as values, as the relay's do.
|
|
267
|
-
*/
|
|
373
|
+
/** A platform call on the token; a throw without one. Errors come back as values, as the relay's did. */
|
|
268
374
|
export const directCall = async (host, input) => {
|
|
269
|
-
const access = await directAccess(host);
|
|
270
|
-
if (!access)
|
|
271
|
-
return undefined;
|
|
272
|
-
const local = host.provider?.kind === "local";
|
|
273
375
|
const demo = host.mode === "demo" ? demoAnswer(input) : undefined;
|
|
274
376
|
if (demo)
|
|
275
377
|
return demo;
|
|
378
|
+
// Only what leaves the app: an answer from props needs no token, so it outlives the dispatch's.
|
|
379
|
+
const access = async () => {
|
|
380
|
+
const granted = await directAccess(host);
|
|
381
|
+
if (!granted)
|
|
382
|
+
throw new NoPlatformError();
|
|
383
|
+
return granted;
|
|
384
|
+
};
|
|
276
385
|
try {
|
|
277
386
|
if (input.integrationId === STEPS_INTEGRATION_ID)
|
|
278
|
-
return await steps(host, access, input);
|
|
387
|
+
return await steps(host, await access(), input);
|
|
388
|
+
if (input.integrationId === META_INTEGRATION_ID) {
|
|
389
|
+
// Without props' answer, a request Zite didn't dispatch still fails loudly, and `local` asks the backend.
|
|
390
|
+
let workspace = host.workspace;
|
|
391
|
+
if (!workspace) {
|
|
392
|
+
const granted = await access();
|
|
393
|
+
if (host.provider?.kind === "local" && input.methodName === "getWorkspace")
|
|
394
|
+
workspace = await localWorkspaceOf(granted);
|
|
395
|
+
}
|
|
396
|
+
return noted(host, input, metaAnswer(input.methodName, workspace));
|
|
397
|
+
}
|
|
279
398
|
if (input.integrationId === INTEGRATIONS_PLATFORM_ID)
|
|
280
|
-
return
|
|
399
|
+
return await integrationsGet(host, input);
|
|
281
400
|
const route = sdkRoute(host, input);
|
|
282
|
-
if (route)
|
|
283
|
-
|
|
284
|
-
|
|
401
|
+
if (route) {
|
|
402
|
+
const granted = await access();
|
|
403
|
+
return await recorded(host, input, () => sdk(granted, route));
|
|
404
|
+
}
|
|
405
|
+
if (input.integrationId === NOTIFICATIONS_INTEGRATION_ID)
|
|
406
|
+
return notFound(NOTIFICATIONS_INTEGRATION_ID);
|
|
407
|
+
return { error: { code: "NOT_FOUND", message: `${input.className}.${input.methodName} isn't served yet` } };
|
|
285
408
|
}
|
|
286
409
|
catch (error) {
|
|
410
|
+
if (error instanceof NoPlatformError)
|
|
411
|
+
throw error;
|
|
287
412
|
return somethingWentWrong(host, input, error);
|
|
288
413
|
}
|
|
289
414
|
};
|
|
290
|
-
/** After a provider answered 401: the connection refreshed by the backend
|
|
415
|
+
/** After a provider answered 401: the connection refreshed by the backend. */
|
|
291
416
|
export const refreshConnection = async (host, name) => {
|
|
292
417
|
const access = await directAccess(host);
|
|
293
418
|
if (!access)
|
|
294
|
-
return
|
|
419
|
+
return { error: { code: "INTERNAL_ERROR", message: new NoPlatformError().message } };
|
|
295
420
|
if (host.mode === "demo")
|
|
296
421
|
return DEMO_REFUSED;
|
|
297
|
-
let
|
|
422
|
+
let fresh;
|
|
298
423
|
try {
|
|
299
|
-
|
|
300
|
-
.connection;
|
|
424
|
+
fresh = (await backendJson(access, "POST", `/connections/${encodeURIComponent(name)}/refresh`)).connection;
|
|
301
425
|
}
|
|
302
426
|
catch (error) {
|
|
303
427
|
return error instanceof PlatformError && error.status === 404
|
|
304
428
|
? notFound(name)
|
|
305
429
|
: { error: { code: "INTERNAL_ERROR", message: `Integration ${name} could not be refreshed` } };
|
|
306
430
|
}
|
|
307
|
-
|
|
308
|
-
|
|
431
|
+
// Over whichever set the request read: the host's copy is replaced until the host hands over another, the backend's
|
|
432
|
+
// answer in place.
|
|
433
|
+
const given = host.connections;
|
|
434
|
+
if (fresh && given && Object.hasOwn(given, name))
|
|
435
|
+
keepRefreshed(name, given[name], fresh);
|
|
436
|
+
else if (fresh && !given && tokenHeld) {
|
|
437
|
+
tokenHeld.answer = { ...tokenHeld.answer, connections: { ...tokenHeld.answer.connections, [name]: fresh } };
|
|
309
438
|
}
|
|
310
|
-
if (
|
|
439
|
+
if (fresh?.status !== "connected")
|
|
311
440
|
return { error: { code: "INTERNAL_ERROR", message: `Integration ${name} is not connected` } };
|
|
312
|
-
return { value:
|
|
441
|
+
return { value: fresh };
|
|
313
442
|
};
|
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
* it: logged, every secret it knows redacted, with the connection whose token
|
|
4
4
|
* it carries and what an org's own AI key used; a 401 on a connection's token
|
|
5
5
|
* refreshed and sent once more; and a call on Zite's own AI key sent to the
|
|
6
|
-
* AI gateway on the request's app token
|
|
7
|
-
* platform binding when there's no token or gateway.
|
|
6
|
+
* AI gateway on the request's app token.
|
|
8
7
|
*/
|
|
9
8
|
import type { HostRequestState } from "./types.js";
|
|
10
9
|
/** At the gateway, the connection a call on Zite's AI key bills to: the marker's, which the token takes the place of. */
|
|
@@ -3,13 +3,12 @@
|
|
|
3
3
|
* it: logged, every secret it knows redacted, with the connection whose token
|
|
4
4
|
* it carries and what an org's own AI key used; a 401 on a connection's token
|
|
5
5
|
* refreshed and sent once more; and a call on Zite's own AI key sent to the
|
|
6
|
-
* AI gateway on the request's app token
|
|
7
|
-
* platform binding when there's no token or gateway.
|
|
6
|
+
* AI gateway on the request's app token.
|
|
8
7
|
*/
|
|
9
8
|
import { isPlatformRequest } from "../internal/platformFetch.js";
|
|
10
9
|
import { AI_USAGE_HOSTS, meterAiResponse, withStreamUsage } from "./aiUsage.js";
|
|
11
10
|
import { DEMO_UNAVAILABLE_MESSAGE } from "./data/dispatch.js";
|
|
12
|
-
import { directAccess, limiter, refreshConnection
|
|
11
|
+
import { directAccess, limiter, refreshConnection } from "./direct.js";
|
|
13
12
|
import { REDACTED, redactKnown, secretForms } from "./redactSecrets.js";
|
|
14
13
|
import { getHost } from "./scope.js";
|
|
15
14
|
const MAX_FETCHES = 500;
|
|
@@ -36,7 +35,7 @@ const MAX_ERROR_CHARS = 1_000;
|
|
|
36
35
|
const SECRET_HEADER = /auth|key|token|secret|signature|password|cookie|session/i;
|
|
37
36
|
// Usage still being read when the request's log is flushed isn't waited on for longer.
|
|
38
37
|
const USAGE_WAIT_MS = 10_000;
|
|
39
|
-
// A request's calls on Zite's AI key at once, until each is answered, as the host
|
|
38
|
+
// A request's calls on Zite's AI key at once, until each is answered, as the host once had them.
|
|
40
39
|
const AI_CONCURRENCY = 4;
|
|
41
40
|
// Tokens refreshed in this isolate, which a later request's props may not name yet.
|
|
42
41
|
const MAX_REFRESHED = 50;
|
|
@@ -296,7 +295,7 @@ const freshToken = async (host, token, connection) => {
|
|
|
296
295
|
if (!pending) {
|
|
297
296
|
pending = (async () => {
|
|
298
297
|
const result = await refreshConnection(host, connection);
|
|
299
|
-
if (!
|
|
298
|
+
if (!("value" in result))
|
|
300
299
|
return undefined;
|
|
301
300
|
const fresh = result.value;
|
|
302
301
|
const previous = (host.connectionAnswers?.get(connection) ?? {});
|
|
@@ -324,7 +323,7 @@ const freshToken = async (host, token, connection) => {
|
|
|
324
323
|
const aiRefusal = (status, type, message) => Response.json({ error: { type, code: type, message } }, { status });
|
|
325
324
|
const aiUnavailable = (host) => aiRefusal(503, "unavailable", host.provider?.kind === "local"
|
|
326
325
|
? "Zite's AI key needs the AI gateway: name aiGateway in ZITE_URLS"
|
|
327
|
-
: "Zite's AI key
|
|
326
|
+
: "Zite's AI key needs the platform's AI gateway, which this request wasn't given");
|
|
328
327
|
/**
|
|
329
328
|
* A call on Zite's AI key at the gateway, the marker swapped for the request's app token. The token goes to the
|
|
330
329
|
* gateway's origin alone: the target is built from it, and a redirect is handed back rather than followed.
|
|
@@ -431,19 +430,9 @@ export const createOutboundFetch = (nativeFetch) => async (input, init) => {
|
|
|
431
430
|
release();
|
|
432
431
|
}
|
|
433
432
|
}
|
|
434
|
-
const
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
record({ status: refused.status });
|
|
438
|
-
return refused;
|
|
439
|
-
}
|
|
440
|
-
// Whole, so it crosses RPC as bytes whatever stream the app built it from, and sized, so the host
|
|
441
|
-
// reserves only what it is rather than the most it allows.
|
|
442
|
-
const body = base.body ? await base.arrayBuffer() : undefined;
|
|
443
|
-
headers.set("content-length", String(body?.byteLength ?? 0));
|
|
444
|
-
const response = await session.aiFetch(new Request(`https://${AI_HOST}/${ai.provider}${ai.path}`, { method: base.method, headers, body, redirect: "manual" }));
|
|
445
|
-
record({ status: response.status });
|
|
446
|
-
return response;
|
|
433
|
+
const refused = aiUnavailable(host);
|
|
434
|
+
record({ status: refused.status });
|
|
435
|
+
return refused;
|
|
447
436
|
}
|
|
448
437
|
catch (error) {
|
|
449
438
|
record({ error });
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AppTokenClaims } from "../internal/appToken.js";
|
|
2
|
-
import type { HostMode, HostProps, PlatformProvider,
|
|
3
|
-
export declare const ziteProvider: (props: HostProps
|
|
2
|
+
import type { HostMode, HostProps, PlatformProvider, PlatformUrls } from "./types.js";
|
|
3
|
+
export declare const ziteProvider: (props: HostProps) => PlatformProvider;
|
|
4
4
|
/** What `local` reads, documented in `zitejs`'s help. */
|
|
5
5
|
export declare const LOCAL_ENV: {
|
|
6
6
|
readonly token: "ZITE_DEV_TOKEN";
|
|
@@ -14,16 +14,16 @@ const URL_NAMES = ["baseRunner", "backend", "aiGateway", "runs", "email", "pdf",
|
|
|
14
14
|
// and `local` keeps no run detail) and each service's own URL (the backend then answers it).
|
|
15
15
|
const OPTIONAL_URLS = new Set(["aiGateway", "runs", "email", "pdf", "uploads", "steps"]);
|
|
16
16
|
const trimmed = (urls) => Object.fromEntries(URL_NAMES.flatMap((name) => (urls[name] ? [[name, urls[name].replace(/\/+$/, "")]] : [])));
|
|
17
|
-
export const ziteProvider = (props
|
|
17
|
+
export const ziteProvider = (props) => {
|
|
18
18
|
let current = props.appToken;
|
|
19
19
|
let renewing;
|
|
20
20
|
const renew = async () => {
|
|
21
21
|
try {
|
|
22
|
-
const fresh = await
|
|
22
|
+
const fresh = await props.platform.appToken();
|
|
23
23
|
return typeof fresh === "string" && fresh ? fresh : undefined;
|
|
24
24
|
}
|
|
25
25
|
catch (error) {
|
|
26
|
-
//
|
|
26
|
+
// The token held still serves until it expires.
|
|
27
27
|
console.error("[zite] couldn't renew the app token", error);
|
|
28
28
|
return undefined;
|
|
29
29
|
}
|
|
@@ -1,22 +1,25 @@
|
|
|
1
|
-
import type { HostProps,
|
|
1
|
+
import type { HostProps, PlatformProvider, HostRequestState } from "./types.js";
|
|
2
2
|
export { runWithHost } from "./scope.js";
|
|
3
3
|
export { describeApp } from "./describe.js";
|
|
4
|
-
export type { HostProps, PlatformBinding, PlatformCallInput, PlatformCallResult,
|
|
4
|
+
export type { HostProps, PlatformBinding, PlatformCallInput, PlatformCallResult, PlatformProvider, PlatformUrls, RunRecord, } from "./types.js";
|
|
5
5
|
/**
|
|
6
6
|
* Routes every zitejs data, integration and email call to this request's
|
|
7
|
-
* platform
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* platform on its app token, and only inside an access scope. The access kind
|
|
8
|
+
* travels with the call so the platform can record what a page read and
|
|
9
|
+
* narrow anonymous reads. A request with no token or URLs fails each call
|
|
10
|
+
* that would leave the app loudly: there is nowhere else for it to go. What
|
|
11
|
+
* props already answer, integrations.get and ZiteMeta.getWorkspace, needs none.
|
|
10
12
|
*/
|
|
11
13
|
export declare function installPlatformTransport(): void;
|
|
12
14
|
/** What the host dispatched this request with. Headers are ignored: props are the only thing it alone can set. */
|
|
13
15
|
export declare function hostContextFromProps(request: Request, props: HostProps): HostRequestState;
|
|
14
16
|
/**
|
|
15
17
|
* A request the host didn't dispatch: the host's warm-up, `zitejs describe` under `vite preview`, and tests. Only the
|
|
16
|
-
* host can set props, so without them a build answers warm and describe
|
|
17
|
-
* its own
|
|
18
|
+
* host can set props, so without them a build answers warm and describe, nobody is signed in and nothing reaches the
|
|
19
|
+
* platform. An entry that passes its own provider (tests) owns its requests: it may say who is calling in `x-zite-*`
|
|
20
|
+
* headers, and its provider's token and URLs reach the platform.
|
|
18
21
|
*/
|
|
19
|
-
export declare function hostContextFromRequest(request: Request,
|
|
22
|
+
export declare function hostContextFromRequest(request: Request, provider?: PlatformProvider): HostRequestState;
|
|
20
23
|
/**
|
|
21
24
|
* A request under `vite dev` with the `local` provider: the developer's dev token reaches the platform, and the
|
|
22
25
|
* stand-in user is signed in. Headers still invoke only warm and describe. Its token's mode, base and app URL come
|
|
@@ -39,7 +42,8 @@ export type ZiteServerOptions = {
|
|
|
39
42
|
fetch(request: Request, ...rest: unknown[]): Promise<Response> | Response;
|
|
40
43
|
};
|
|
41
44
|
getRouter: () => unknown;
|
|
42
|
-
platform
|
|
45
|
+
/** A test entry's own platform, for the requests no host dispatched; see `hostContextFromRequest`. */
|
|
46
|
+
provider?: PlatformProvider;
|
|
43
47
|
/** The app's start instance, which a test run of a server function runs under. */
|
|
44
48
|
startInstance: {
|
|
45
49
|
getOptions: () => unknown;
|
|
@@ -50,10 +54,10 @@ export type ZiteHandlers = {
|
|
|
50
54
|
};
|
|
51
55
|
/**
|
|
52
56
|
* The handlers of a Start app's server entry. A request the host dispatched
|
|
53
|
-
* carries its identity and platform in props. Without props, a
|
|
54
|
-
* dev token or API key in env selects the `local` provider;
|
|
55
|
-
* `vite preview`, tests) reads `x-zite-*`
|
|
56
|
-
* is given.
|
|
57
|
+
* carries its identity, app token and platform URLs in props. Without props, a
|
|
58
|
+
* developer's dev token or API key in env selects the `local` provider;
|
|
59
|
+
* anything else (describe under `vite preview`, tests) reads `x-zite-*`
|
|
60
|
+
* headers and uses `provider` if one is given.
|
|
57
61
|
*/
|
|
58
62
|
export declare function createZiteHandlers(options: ZiteServerOptions): ZiteHandlers;
|
|
59
63
|
/** The fetch handler alone, for the Node entry. */
|