zitejs 0.10.0-start.27 → 0.10.0-start.29

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.
Files changed (40) hide show
  1. package/dist/cjs/check/start/sourceRules.js +7 -0
  2. package/dist/cjs/start/airtable/cache.d.ts +17 -0
  3. package/dist/cjs/start/airtable/cache.js +76 -0
  4. package/dist/cjs/start/types.d.ts +37 -7
  5. package/dist/esm/check/start/sourceRules.js +7 -0
  6. package/dist/esm/signin/index.d.ts +1 -1
  7. package/dist/esm/signin/index.js +1 -1
  8. package/dist/esm/signin/keys.d.ts +26 -19
  9. package/dist/esm/signin/keys.js +167 -45
  10. package/dist/esm/signin/signin.js +1 -3
  11. package/dist/esm/signin/verify.d.ts +4 -3
  12. package/dist/esm/signin/verify.js +30 -34
  13. package/dist/esm/start/airtable/cache.d.ts +17 -0
  14. package/dist/esm/start/airtable/cache.js +72 -0
  15. package/dist/esm/start/airtable/dispatch.d.ts +3 -0
  16. package/dist/esm/start/airtable/dispatch.js +14 -12
  17. package/dist/esm/start/airtable/index.js +8 -0
  18. package/dist/esm/start/data/baseRunner.d.ts +5 -0
  19. package/dist/esm/start/data/baseRunner.js +10 -0
  20. package/dist/esm/start/data/dispatch.d.ts +1 -0
  21. package/dist/esm/start/data/dispatch.js +13 -1
  22. package/dist/esm/start/demo.d.ts +11 -0
  23. package/dist/esm/start/demo.js +21 -0
  24. package/dist/esm/start/direct.js +7 -3
  25. package/dist/esm/start/fetch.js +3 -1
  26. package/dist/esm/start/localSignIn.d.ts +2 -4
  27. package/dist/esm/start/localSignIn.js +4 -5
  28. package/dist/esm/start/outbound.js +1 -0
  29. package/dist/esm/start/requestEnv.d.ts +8 -0
  30. package/dist/esm/start/requestEnv.js +115 -0
  31. package/dist/esm/start/server.d.ts +2 -1
  32. package/dist/esm/start/server.js +24 -28
  33. package/dist/esm/start/steps.d.ts +15 -15
  34. package/dist/esm/start/steps.js +56 -81
  35. package/dist/esm/start/types.d.ts +37 -7
  36. package/dist/esm/start/version.d.ts +1 -1
  37. package/dist/esm/start/version.js +1 -1
  38. package/dist/esm/start/workflowRun.d.ts +30 -5
  39. package/dist/esm/start/workflowRun.js +28 -20
  40. package/package.json +2 -1
@@ -1,6 +1,8 @@
1
1
  // The identity token's verifier (T5 design §6.1), the same rules as restly's `identityToken.ts` on the front and the
2
- // dispatcher. Web Crypto only.
3
- import { fromBase64Url, isRecord, jsonOf, utf8 } from "./encoding.js";
2
+ // dispatcher. jose checks the signature, on Web Crypto; the rules around it are ours.
3
+ import { compactVerify, errors } from "jose";
4
+ import { fromBase64Url, isRecord, jsonOf } from "./encoding.js";
5
+ import { ED25519_ALGS, isEd25519Alg } from "./keys.js";
4
6
  /** The `typ` every identity token carries in its header, and nothing else does. */
5
7
  export const IDENTITY_TOKEN_TYPE = "zite-id+jwt";
6
8
  export const DEFAULT_MAX_LIFETIME_S = 300;
@@ -56,24 +58,13 @@ const previewOk = (preview, mode, pinned) => {
56
58
  preview.editor.length > 0 &&
57
59
  (pinned === undefined || preview.build === pinned));
58
60
  };
59
- // Imported once per process; a CryptoKey holds no request's I/O.
60
- const imported = new Map();
61
- const publicKey = async (raw) => {
62
- const held = imported.get(raw);
63
- if (held)
64
- return held;
65
- const bytes = fromBase64Url(raw);
66
- if (!bytes)
67
- return undefined;
68
- try {
69
- const key = await crypto.subtle.importKey("raw", bytes, { name: "Ed25519" }, false, ["verify"]);
70
- imported.set(raw, key);
71
- return key;
72
- }
73
- catch {
74
- return undefined;
75
- }
76
- };
61
+ // What jose's refusal says of the token: a signature its key didn't make, a header jose won't take (an unknown
62
+ // `crit`), or no key to check it with.
63
+ const refusalOf = (error) => error instanceof errors.JWSSignatureVerificationFailed
64
+ ? "signature"
65
+ : error instanceof errors.JWSInvalid || error instanceof errors.JOSENotSupported
66
+ ? "malformed"
67
+ : "kid";
77
68
  // Reported once per reason and key id, and only so many: both come from the caller.
78
69
  const MAX_REPORTED = 64;
79
70
  const reported = new Set();
@@ -85,8 +76,9 @@ const reportOnce = (report) => {
85
76
  console.warn(`[zite] refused an identity token: ${report.reason}${report.kid ? ` (kid ${report.kid})` : ""}`);
86
77
  };
87
78
  /**
88
- * Verifies identity tokens against `keys`, in §6.1's order. A `kid` the keys don't know triggers one refetch. Each
89
- * refusal is reported to `onRefusal`, by default logged once per reason and key id.
79
+ * Verifies identity tokens against `keys`, in §6.1's order, the header's `alg` `EdDSA` or RFC 9864's `Ed25519`. A
80
+ * `kid` fetched keys don't know triggers a refetch, at most once a minute. Each refusal is reported to `onRefusal`, by
81
+ * default logged once per reason and key id.
90
82
  */
91
83
  export const createIdentityVerifier = (keys, options = {}) => async (token, expect) => {
92
84
  const refuse = (reason, kid) => {
@@ -99,25 +91,29 @@ export const createIdentityVerifier = (keys, options = {}) => async (token, expe
99
91
  return refuse("malformed");
100
92
  const [headerPart, payloadPart, signaturePart] = parts;
101
93
  const header = jsonOf(fromBase64Url(headerPart));
102
- const payload = jsonOf(fromBase64Url(payloadPart));
103
- const signature = fromBase64Url(signaturePart);
104
- if (!isRecord(header) || !isRecord(payload) || !signature)
94
+ const claimed = jsonOf(fromBase64Url(payloadPart));
95
+ // One spelling of each segment: jose decodes base64url as loosely as atob, and would take a respelled or padded
96
+ // signature.
97
+ if (!isRecord(header) || !isRecord(claimed) || !fromBase64Url(signaturePart))
105
98
  return refuse("malformed");
106
- if (header.alg !== "EdDSA")
99
+ if (!isEd25519Alg(header.alg))
107
100
  return refuse("alg");
108
101
  if (header.typ !== IDENTITY_TOKEN_TYPE)
109
102
  return refuse("typ");
110
103
  const kid = typeof header.kid === "string" ? header.kid : undefined;
111
104
  if (kid === undefined)
112
105
  return refuse("kid");
113
- const own = (map) => (map && Object.hasOwn(map, kid) ? map[kid] : undefined);
114
- const raw = own(keys.current()) ?? own(await keys.refresh());
115
- if (!raw)
116
- return refuse("kid", kid);
117
- const key = await publicKey(raw);
118
- const genuine = !!key && (await crypto.subtle.verify({ name: "Ed25519" }, key, signature, utf8(`${headerPart}.${payloadPart}`)));
119
- if (!genuine)
120
- return refuse("signature", kid);
106
+ let signed;
107
+ try {
108
+ ({ payload: signed } = await compactVerify(token, keys, { algorithms: ED25519_ALGS }));
109
+ }
110
+ catch (error) {
111
+ return refuse(refusalOf(error), kid);
112
+ }
113
+ // Read from what jose verified, which under a `b64: false` header isn't the segment's decoding.
114
+ const payload = jsonOf(signed);
115
+ if (!isRecord(payload))
116
+ return refuse("malformed", kid);
121
117
  if (payload.iss !== expect.issuer)
122
118
  return refuse("iss", kid);
123
119
  if (typeof payload.aud !== "string" || payload.aud !== expect.clientId)
@@ -0,0 +1,17 @@
1
+ /** How long a read is served from the cache, as classic's. */
2
+ export declare const AIRTABLE_READ_TTL_MS = 10000;
3
+ export type AirtableCache = {
4
+ /** A read kept across requests; a write to `table` makes it stale. */
5
+ read<T>(table: string, key: string, load: () => Promise<T>): Promise<T>;
6
+ /** A write to `table`, which makes its reads stale in every isolate the cache reaches, this request's at once. */
7
+ wrote(table: string): Promise<void>;
8
+ };
9
+ /**
10
+ * One request's view of the cache for one connection. Entries are keyed by its token's hash, so only a caller holding
11
+ * the same token reads them, whoever shares the cache.
12
+ */
13
+ export declare const createAirtableCache: ({ token, waitUntil, now, }: {
14
+ token: string;
15
+ waitUntil?: (promise: Promise<unknown>) => void;
16
+ now?: () => number;
17
+ }) => Promise<AirtableCache | undefined>;
@@ -0,0 +1,72 @@
1
+ // Classic keeps an Airtable read for 10 s across runs, in KV, and a write makes the table's reads stale wherever the
2
+ // version it bumps reaches (restly's apps/cloudflare-lambda/src/integrationCache.ts). A Start app keeps them in its
3
+ // Worker's own Cache API cache, per colo, on the same terms. Off Workers there is none, and each read goes to Airtable.
4
+ const CACHE_NAME = "zite-airtable";
5
+ const ORIGIN = "https://airtable-cache.zite.invalid";
6
+ /** How long a read is served from the cache, as classic's. */
7
+ export const AIRTABLE_READ_TTL_MS = 10_000;
8
+ // What the cache may keep an entry for, past which it's gone whether read or not; classic's KV minimum.
9
+ const HARD_TTL_S = 60;
10
+ const VERSION_TTL_S = 24 * 60 * 60;
11
+ // Classic's cap, KV's largest value.
12
+ const MAX_ENTRY_CHARS = 25 * 1024 * 1024;
13
+ const openCache = async () => {
14
+ const storage = globalThis.caches;
15
+ if (!storage?.open)
16
+ return undefined;
17
+ try {
18
+ return await storage.open(CACHE_NAME);
19
+ }
20
+ catch {
21
+ return undefined;
22
+ }
23
+ };
24
+ const sha256 = async (text) => [...new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text)))].map((b) => b.toString(16).padStart(2, "0")).join("");
25
+ /**
26
+ * One request's view of the cache for one connection. Entries are keyed by its token's hash, so only a caller holding
27
+ * the same token reads them, whoever shares the cache.
28
+ */
29
+ export const createAirtableCache = async ({ token, waitUntil, now = () => Date.now(), }) => {
30
+ const cache = await openCache();
31
+ if (!cache)
32
+ return undefined;
33
+ const scope = (await sha256(token)).slice(0, 32);
34
+ // This request's writes, added to what the cache says, so a read after a write misses even before the bump lands.
35
+ const bumps = new Map();
36
+ const at = (...parts) => new Request(`${ORIGIN}/${parts.map(encodeURIComponent).join("/")}`);
37
+ const stored = async (table) => {
38
+ const held = await cache.match(at("v", scope, table)).catch(() => undefined);
39
+ return Number.parseInt((await held?.text().catch(() => "")) ?? "", 10) || 0;
40
+ };
41
+ const kept = (put) => {
42
+ const quiet = put.catch((error) => console.warn("[zite] Airtable cache write failed", error));
43
+ waitUntil?.(quiet);
44
+ return quiet;
45
+ };
46
+ return {
47
+ async read(table, key, load) {
48
+ let entry;
49
+ try {
50
+ entry = at("r", scope, table, String((await stored(table)) + (bumps.get(table) ?? 0)), await sha256(key));
51
+ const held = await cache.match(entry);
52
+ const found = held ? (await held.json()) : undefined;
53
+ if (found && typeof found.at === "number" && now() - found.at < AIRTABLE_READ_TTL_MS)
54
+ return found.value;
55
+ }
56
+ catch (error) {
57
+ console.warn("[zite] Airtable cache read failed", error);
58
+ }
59
+ const value = await load();
60
+ const body = entry ? JSON.stringify({ at: now(), value }) : undefined;
61
+ if (entry && body && body.length <= MAX_ENTRY_CHARS) {
62
+ void kept(cache.put(entry, new Response(body, { headers: { "content-type": "application/json", "cache-control": `max-age=${HARD_TTL_S}` } })));
63
+ }
64
+ return value;
65
+ },
66
+ async wrote(table) {
67
+ bumps.set(table, (bumps.get(table) ?? 0) + 1);
68
+ const next = String((await stored(table)) + 1);
69
+ await kept(cache.put(at("v", scope, table), new Response(next, { headers: { "cache-control": `max-age=${VERSION_TTL_S}` } })));
70
+ },
71
+ };
72
+ };
@@ -1,3 +1,4 @@
1
+ import type { AirtableCache } from "./cache.js";
1
2
  /** As `@fillout/zite`'s `IntegrationFieldMappingType`, from zite.lock. */
2
3
  export type AirtableFieldMapping = {
3
4
  mapping: {
@@ -37,6 +38,8 @@ export type AirtableContext = {
37
38
  fetch: AirtableFetch;
38
39
  /** One request's reads, so the same read twice in a request goes to Airtable once. */
39
40
  reads?: Map<string, Promise<unknown>>;
41
+ /** Reads kept across requests, as classic keeps them. */
42
+ cache?: AirtableCache;
40
43
  sleep?: (ms: number) => Promise<void>;
41
44
  };
42
45
  /** The npm SDK's error, whose shape apps (and the goldens) see: not an `Error`. */
@@ -1,7 +1,3 @@
1
- // Ported from restly's apps/cloudflare-lambda/src/integrations/airtable.ts (`executeAirtableCall`), classic's
2
- // dispatch, with a fetch client in place of the npm SDK. Keep the two in step. Differences: fields come back by id
3
- // from every request, so a rename since the lock never unmaps one; the schema is the lock's (`nameMappings`), as the
4
- // build bakes it; the per-base limiter is gone.
5
1
  import { filtersToFormula as buildFiltersFormula, isRecordId } from "./formulas.js";
6
2
  const SDK_ID_FIELD = "id";
7
3
  const UNIQUE_RECORD_ID_KEY = "recordID";
@@ -319,24 +315,30 @@ export async function executeAirtableCall(context, methodName, params) {
319
315
  const tableSchema = lockSchema(tableId, tables?.[tableId]);
320
316
  const sleep = context.sleep ?? sleepMs;
321
317
  const tablePath = `/v0/${segment(baseId)}/${segment(tableId)}`;
322
- // A write makes any read this request already made stale.
318
+ // A write makes any read this request already made stale, and the table's reads stale across requests.
323
319
  const send = async (path, init) => {
324
- if (init.method !== "GET" && !path.endsWith("/listRecords"))
320
+ const writes = init.method !== "GET" && !path.endsWith("/listRecords");
321
+ if (writes)
325
322
  context.reads?.clear();
326
- return context.fetch(path, init);
323
+ const response = await context.fetch(path, init);
324
+ if (writes && response.ok)
325
+ await context.cache?.wrote(`${baseId}:${tableId}`);
326
+ return response;
327
327
  };
328
- const read = (key, load) => {
328
+ const read = (readTableId, key, load) => {
329
+ const cache = context.cache;
330
+ const loadKept = cache ? () => cache.read(`${baseId}:${readTableId}`, key, load) : load;
329
331
  if (!context.reads)
330
- return load();
332
+ return loadKept();
331
333
  const held = context.reads.get(key);
332
334
  if (held)
333
335
  return held;
334
- const loading = load();
336
+ const loading = loadKept();
335
337
  context.reads.set(key, loading);
336
338
  loading.catch(() => context.reads?.delete(key));
337
339
  return loading;
338
340
  };
339
- const getRecord = (recordTableId, recordId) => read(`get:${baseId}:${recordTableId}:${recordId}`, async () => {
341
+ const getRecord = (recordTableId, recordId) => read(recordTableId, `get:${baseId}:${recordTableId}:${recordId}`, async () => {
340
342
  const response = await send(`/v0/${segment(baseId)}/${segment(recordTableId)}/${segment(recordId)}?returnFieldsByFieldId=true`, { method: "GET" });
341
343
  if (!response.ok)
342
344
  throw await sdkError(response);
@@ -464,7 +466,7 @@ export async function executeAirtableCall(context, methodName, params) {
464
466
  }
465
467
  async function listRecords(body) {
466
468
  const request = { returnFieldsByFieldId: true, ...body };
467
- return read(`list:${baseId}:${tableId}:${JSON.stringify(request)}`, async () => {
469
+ return read(tableId, `list:${baseId}:${tableId}:${JSON.stringify(request)}`, async () => {
468
470
  const response = await send(`${tablePath}/listRecords`, { method: "POST", body: request });
469
471
  if (!response.ok) {
470
472
  const error = new Error(`Airtable API error: ${await response.text()}`);
@@ -1,4 +1,5 @@
1
1
  import { builtAirtableSettings } from "./built.js";
2
+ import { createAirtableCache } from "./cache.js";
2
3
  import { executeAirtableCall } from "./dispatch.js";
3
4
  import { ZiteError, isZiteErrorCode } from "../errors.js";
4
5
  import { rememberConnection } from "../outbound.js";
@@ -21,9 +22,16 @@ export async function runAirtableCall(host, connectionOf, integrationId, setting
21
22
  }
22
23
  rememberConnection(host, integrationId, connection.value);
23
24
  host.airtableReads ??= new Map();
25
+ const caches = (host.airtableCaches ??= new Map());
26
+ let cache = caches.get(token);
27
+ if (!cache) {
28
+ cache = createAirtableCache({ token, waitUntil: host.waitUntil });
29
+ caches.set(token, cache);
30
+ }
24
31
  return executeAirtableCall({
25
32
  settings,
26
33
  reads: host.airtableReads,
34
+ cache: await cache,
27
35
  fetch: (path, init) => fetch(`${AIRTABLE_API}${path}`, {
28
36
  method: init.method,
29
37
  headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
@@ -66,6 +66,11 @@ export declare const createBaseRunner: (transport: DataTransport) => {
66
66
  statementTimeoutMs?: number;
67
67
  nameMappings?: NameMappings;
68
68
  }) => Promise<unknown>;
69
+ getSchema: ({ baseId, tables, includeFields }: {
70
+ baseId: string;
71
+ tables?: string[];
72
+ includeFields?: boolean;
73
+ }) => Promise<unknown>;
69
74
  };
70
75
  auth: {
71
76
  findAll: ({ baseId, appIds, filters, filter, offset, sort, limit, }: {
@@ -67,6 +67,16 @@ export const createBaseRunner = (transport) => {
67
67
  },
68
68
  sql: {
69
69
  execute: ({ baseId, sql, params, statementTimeoutMs, nameMappings, }) => json("POST", `/bases/${segment(baseId)}/sql`, { sql, params, statementTimeoutMs, nameMappings }),
70
+ // Repeated `tables=`, as classic sends them, so a name with a comma stays one table.
71
+ getSchema: ({ baseId, tables, includeFields }) => {
72
+ const query = new URLSearchParams();
73
+ for (const table of tables ?? [])
74
+ query.append("tables", table);
75
+ if (includeFields === false)
76
+ query.set("includeFields", "false");
77
+ const qs = query.toString();
78
+ return json("GET", `/bases/${segment(baseId)}/sql/schema${qs ? `?${qs}` : ""}`);
79
+ },
70
80
  },
71
81
  auth: {
72
82
  findAll: ({ baseId, appIds, filters, filter, offset, sort, limit = 500, }) => json("POST", `/bases/${segment(baseId)}/auth/users/list`, {
@@ -11,6 +11,7 @@ export type DataContext = {
11
11
  readOnly: boolean;
12
12
  };
13
13
  export declare const DEMO_UNAVAILABLE_MESSAGE = "This action is not available in demo mode";
14
+ export declare const DEMO_READ_DATABASE_METHODS: ReadonlySet<string>;
14
15
  export declare function asZiteClientError(error: unknown): unknown;
15
16
  /** One `zite.*` data call, as classic's dispatch runs it. */
16
17
  export declare function executeDatabasesCall(context: DataContext, methodName: string, params: any): Promise<any>;
@@ -3,7 +3,7 @@ import { transformFieldNamesToIds, transformFieldsToFieldIds, transformFiltersTo
3
3
  import { ZITE_PLATFORM_FAULT_FLAG, } from "./types.js";
4
4
  export const DEMO_UNAVAILABLE_MESSAGE = "This action is not available in demo mode";
5
5
  // An allowlist, so a database method added later is refused until it's listed.
6
- const DEMO_READ_DATABASE_METHODS = new Set([
6
+ export const DEMO_READ_DATABASE_METHODS = new Set([
7
7
  "findOne",
8
8
  "findAll",
9
9
  "executeSql",
@@ -92,6 +92,18 @@ async function runDatabasesCall(context, methodName, params) {
92
92
  : undefined,
93
93
  });
94
94
  }
95
+ // Classic's 1.0 SDK, which workflow-runner serves there (sdk/databases.ts).
96
+ if (methodName === "getDatabaseSchema") {
97
+ const tablesParam = params.tables;
98
+ const includeFields = params.includeFields;
99
+ if (tablesParam !== undefined && (!Array.isArray(tablesParam) || !tablesParam.every((t) => typeof t === "string"))) {
100
+ throw Object.assign(new Error("getDatabaseSchema: tables must be a list of table names"), { statusCode: 400 });
101
+ }
102
+ if (includeFields !== undefined && typeof includeFields !== "boolean") {
103
+ throw Object.assign(new Error("getDatabaseSchema: includeFields must be true or false"), { statusCode: 400 });
104
+ }
105
+ return sdk.sql.getSchema({ baseId, tables: tablesParam, includeFields });
106
+ }
95
107
  const tableId = params.tableId;
96
108
  if (!tableId) {
97
109
  throw new Error("Databases calls require tableId in params");
@@ -0,0 +1,11 @@
1
+ import type { HostRequestState } from "./types.js";
2
+ export declare const DEMO_READ_ONLY_CODE = "DEMO_READ_ONLY";
3
+ export declare const ERROR_CODE_HEADER = "x-zite-error-code";
4
+ /** A platform call a demo refused, which then decides how the request's failure answers. */
5
+ export declare const noteDemoRefusal: (host: HostRequestState | undefined, error: unknown) => void;
6
+ /**
7
+ * As classic's executor outcome: a demo's failure after a refusal is the refusal, and so is a 5xx after the app called
8
+ * out, since a demo holds no account to call with.
9
+ */
10
+ export declare const failedOnDemo: (host: HostRequestState, response: Response) => boolean;
11
+ export declare const asDemoRefusal: (response: Response) => Response;
@@ -0,0 +1,21 @@
1
+ // Classic's demo answers a call the demo refused with 403 `DEMO_READ_ONLY`, on which the platform's runtime shows the
2
+ // visitor its "not available in the demo" notice (restly's app-runtime demo.ts). A refused call reaches a Start app as
3
+ // an error it may catch or rethrow, so the answer keeps its body and says so in a header.
4
+ import { DEMO_UNAVAILABLE_MESSAGE } from "./data/dispatch.js";
5
+ export const DEMO_READ_ONLY_CODE = "DEMO_READ_ONLY";
6
+ export const ERROR_CODE_HEADER = "x-zite-error-code";
7
+ /** A platform call a demo refused, which then decides how the request's failure answers. */
8
+ export const noteDemoRefusal = (host, error) => {
9
+ if (host?.mode === "demo" && error instanceof Error && error.message.includes(DEMO_UNAVAILABLE_MESSAGE))
10
+ host.demoRefused = true;
11
+ };
12
+ /**
13
+ * As classic's executor outcome: a demo's failure after a refusal is the refusal, and so is a 5xx after the app called
14
+ * out, since a demo holds no account to call with.
15
+ */
16
+ export const failedOnDemo = (host, response) => host.mode === "demo" && response.status >= 400 && (!!host.demoRefused || (response.status >= 500 && !!host.fetches?.length));
17
+ export const asDemoRefusal = (response) => {
18
+ const headers = new Headers(response.headers);
19
+ headers.set(ERROR_CODE_HEADER, DEMO_READ_ONLY_CODE);
20
+ return new Response(response.body, { status: 403, headers });
21
+ };
@@ -98,11 +98,14 @@ export const directDataFetch = (host) => async ({ method, path, body }) => {
98
98
  release();
99
99
  }
100
100
  };
101
- /** As the host answered a demo: email pretends to succeed; schedules and connections act as the author, so are refused. */
101
+ /**
102
+ * As classic answers a demo: email pretends to succeed and schedules, which would queue runs on the author's live app,
103
+ * are refused. `integrations.get` answers each connection as classic's demo does, from the backend.
104
+ */
102
105
  const demoAnswer = (input) => {
103
106
  if (input.className === "Email" && input.methodName === "send")
104
107
  return { value: { success: true } };
105
- if (input.integrationId === SCHEDULES_INTEGRATION_ID || input.integrationId === INTEGRATIONS_PLATFORM_ID)
108
+ if (input.integrationId === SCHEDULES_INTEGRATION_ID)
106
109
  return DEMO_REFUSED;
107
110
  return undefined;
108
111
  };
@@ -169,7 +172,8 @@ let tokenHeld;
169
172
  * when the host didn't hand them over. Stale if the backend didn't answer.
170
173
  */
171
174
  export const connectionsOnToken = async (access, context) => {
172
- const key = `${context?.appId ?? ""}:${context?.buildId ?? ""}:${context?.orgSettingsVersion ?? ""}`;
175
+ // A demo's answers are classic's demo set, never a live request's.
176
+ const key = `${context?.mode ?? ""}:${context?.appId ?? ""}:${context?.buildId ?? ""}:${context?.orgSettingsVersion ?? ""}`;
173
177
  const reuseMs = typeof context?.orgSettingsVersion === "number" ? CONNECTIONS_MAX_AGE_MS : CONNECTIONS_REUSE_MS;
174
178
  if (tokenHeld?.key === key && Date.now() - tokenHeld.at < reuseMs)
175
179
  return tokenHeld.answer;
@@ -1,4 +1,6 @@
1
1
  // The generated server entry's first import: an SDK client an app builds at load (`new OpenAI()` at a module's top
2
- // level) keeps the fetch it found then, so the wrapper must already be in place.
2
+ // level) keeps the fetch it found then, so the wrapper must already be in place, and so must process.env's view.
3
3
  import { watchOutboundFetches } from "./outbound.js";
4
+ import { installRequestEnv } from "./requestEnv.js";
4
5
  watchOutboundFetches();
6
+ installRequestEnv();
@@ -1,18 +1,16 @@
1
1
  import type { IdentityClaims, SignIn } from "../signin/index.js";
2
- import type { PlatformUrls, User } from "./types.js";
2
+ import type { User } from "./types.js";
3
3
  /** What the module reads, from env or from the `.zite/dev.env` that `zitejs dev` writes. */
4
4
  export declare const LOCAL_SIGNIN_ENV: {
5
5
  readonly config: "ZITE_SIGNIN";
6
6
  readonly secret: "ZITE_AUTH_SECRET";
7
7
  };
8
8
  type Env = Record<string, string | undefined>;
9
- /** Where the backend serves the identity issuer's JWK Set, for an unknown `kid`. */
10
- export declare const SIGNING_KEYS_PATH = "/v1/zite/public/start/signing-keys";
11
9
  /**
12
10
  * The module for this request, when the env names an issuer; undefined otherwise, and whenever `ZITE_DEV_USER` is
13
11
  * set: the stand-in wins, with no network, as agent sandboxes need.
14
12
  */
15
- export declare const localSignIn: (env: Env, urls: PlatformUrls, request: Request) => Promise<SignIn | undefined>;
13
+ export declare const localSignIn: (env: Env, request: Request) => Promise<SignIn | undefined>;
16
14
  /** The identity as app code reads it. Off Zite no role names are known, so each role is named by its id. */
17
15
  export declare const userOfIdentity: (claims: IdentityClaims | null) => User | null;
18
16
  export {};
@@ -20,14 +20,12 @@ const configOf = (raw) => {
20
20
  }
21
21
  return config;
22
22
  };
23
- /** Where the backend serves the identity issuer's JWK Set, for an unknown `kid`. */
24
- export const SIGNING_KEYS_PATH = "/v1/zite/public/start/signing-keys";
25
23
  const send = (request) => platformFetch(request);
26
24
  /**
27
25
  * The module for this request, when the env names an issuer; undefined otherwise, and whenever `ZITE_DEV_USER` is
28
26
  * set: the stand-in wins, with no network, as agent sandboxes need.
29
27
  */
30
- export const localSignIn = async (env, urls, request) => {
28
+ export const localSignIn = async (env, request) => {
31
29
  if (env[LOCAL_ENV.user])
32
30
  return undefined;
33
31
  const raw = env[LOCAL_SIGNIN_ENV.config] ?? devSignIn?.[LOCAL_SIGNIN_ENV.config];
@@ -41,7 +39,7 @@ export const localSignIn = async (env, urls, request) => {
41
39
  // Only the developer's client for now; a self-hosted one comes with private key JWT.
42
40
  if (mode !== "dev")
43
41
  throw new Error(`${LOCAL_SIGNIN_ENV.config} names ${config.clientId}; off Zite the client is <app>:dev`);
44
- const { aesGcmSealer, createSignIn, httpTransport, remoteIdentityKeys } = await import("../signin/index.js");
42
+ const { aesGcmSealer, createSignIn, discoveredIdentityKeys, httpTransport, remoteIdentityKeys } = await import("../signin/index.js");
45
43
  const sealer = aesGcmSealer(secret);
46
44
  return createSignIn({
47
45
  issuer: config.issuer,
@@ -50,7 +48,8 @@ export const localSignIn = async (env, urls, request) => {
50
48
  mode,
51
49
  ver: "any",
52
50
  redirectUri: config.redirectUri,
53
- keys: remoteIdentityKeys(config.keysUrl ?? `${urls.backend}${SIGNING_KEYS_PATH}`, { fetch: send }),
51
+ // The issuer's discovery document names its JWK Set (OpenID Connect Discovery 1.0); `keysUrl` overrides it.
52
+ keys: config.keysUrl ? remoteIdentityKeys(config.keysUrl, { fetch: send }) : discoveredIdentityKeys(config.issuer, { fetch: send }),
54
53
  cookies: { secure: new URL(request.url).protocol === "https:", framed: false },
55
54
  transport: httpTransport({
56
55
  issuer: config.issuer,
@@ -414,6 +414,7 @@ export const createOutboundFetch = (nativeFetch) => async (input, init) => {
414
414
  try {
415
415
  // As the gateway and the host refuse it: a demo has no AI on Zite's key.
416
416
  if (host.mode === "demo") {
417
+ host.demoRefused = true;
417
418
  const refused = refusal(403, "permission_denied", DEMO_UNAVAILABLE_MESSAGE);
418
419
  record({ status: refused.status });
419
420
  return refused;
@@ -0,0 +1,8 @@
1
+ import type { HostRequestState } from "./types.js";
2
+ /** Puts the view in place of `process.env`, once per isolate, before the app's modules read it. */
3
+ export declare const installRequestEnv: () => void;
4
+ /**
5
+ * The vars classic sets per run, from what the host sent this request. A demo gets them in its own view; any other
6
+ * request writes them into the isolate's env, which all the build's requests share.
7
+ */
8
+ export declare const applyRequestEnv: (host: HostRequestState) => void;
@@ -0,0 +1,115 @@
1
+ // Classic runs a demo in an isolate of its own, whose process.env holds no token or secret. A Start build's isolates
2
+ // serve live and demo requests alike, so a demo reads process.env through a view of its own: the isolate's vars less
3
+ // every one a request set, and what its own dispatch carries. Live and preview requests read and write the isolate's
4
+ // vars, as they always have.
5
+ import { getHost } from "./scope.js";
6
+ const processOf = () => globalThis.process;
7
+ /** The isolate's own env, under the view, and the view in its place. */
8
+ let isolateEnv;
9
+ let installed;
10
+ /** Every var a request has set in the isolate's env, the host's and the app's: a demo reads none of them. */
11
+ const setByRequests = new Set();
12
+ // The connection vars the last request set, so one gone since (an integration removed) doesn't linger.
13
+ let connectionEnvKeys = new Set();
14
+ const demoView = () => {
15
+ const host = getHost();
16
+ return host?.mode === "demo" ? (host.demoEnv ??= new Map()) : undefined;
17
+ };
18
+ /** A demo's own answer for `key`: set, deleted, or hidden as a request's; undefined when the isolate's stands. */
19
+ const demoAnswer = (key) => {
20
+ const view = demoView();
21
+ if (!view || typeof key !== "string")
22
+ return undefined;
23
+ if (view.has(key))
24
+ return { value: view.get(key) };
25
+ return setByRequests.has(key) ? { value: undefined } : undefined;
26
+ };
27
+ const noteSetByRequest = (key) => {
28
+ if (typeof key === "string" && getHost())
29
+ setByRequests.add(key);
30
+ };
31
+ /** Puts the view in place of `process.env`, once per isolate, before the app's modules read it. */
32
+ export const installRequestEnv = () => {
33
+ const proc = processOf();
34
+ if (!proc?.env || proc.env === installed)
35
+ return;
36
+ isolateEnv = proc.env;
37
+ installed = proc.env = new Proxy(isolateEnv, {
38
+ get: (target, key) => {
39
+ const own = demoAnswer(key);
40
+ return own ? own.value : Reflect.get(target, key);
41
+ },
42
+ has: (target, key) => {
43
+ const own = demoAnswer(key);
44
+ return own ? own.value !== undefined : Reflect.has(target, key);
45
+ },
46
+ set: (target, key, value) => {
47
+ const view = demoView();
48
+ if (view && typeof key === "string") {
49
+ view.set(key, String(value));
50
+ return true;
51
+ }
52
+ noteSetByRequest(key);
53
+ return Reflect.set(target, key, value);
54
+ },
55
+ defineProperty: (target, key, attributes) => {
56
+ const view = demoView();
57
+ if (view && typeof key === "string") {
58
+ view.set(key, String(attributes.value));
59
+ return true;
60
+ }
61
+ noteSetByRequest(key);
62
+ return Reflect.defineProperty(target, key, attributes);
63
+ },
64
+ deleteProperty: (target, key) => {
65
+ const view = demoView();
66
+ if (view && typeof key === "string") {
67
+ view.set(key, undefined);
68
+ return true;
69
+ }
70
+ return Reflect.deleteProperty(target, key);
71
+ },
72
+ ownKeys: (target) => {
73
+ const view = demoView();
74
+ if (!view)
75
+ return Reflect.ownKeys(target);
76
+ const kept = Reflect.ownKeys(target).filter((key) => !demoAnswer(key));
77
+ return [...kept, ...[...view].flatMap(([key, value]) => (value === undefined ? [] : [key]))];
78
+ },
79
+ getOwnPropertyDescriptor: (target, key) => {
80
+ const own = demoAnswer(key);
81
+ if (!own)
82
+ return Reflect.getOwnPropertyDescriptor(target, key);
83
+ return own.value === undefined ? undefined : { value: own.value, writable: true, enumerable: true, configurable: true };
84
+ },
85
+ });
86
+ };
87
+ /**
88
+ * The vars classic sets per run, from what the host sent this request. A demo gets them in its own view; any other
89
+ * request writes them into the isolate's env, which all the build's requests share.
90
+ */
91
+ export const applyRequestEnv = (host) => {
92
+ installRequestEnv();
93
+ const env = isolateEnv;
94
+ if (!env)
95
+ return;
96
+ const own = {
97
+ ...(host.appUrl && { ZITE_APP_URL: host.appUrl }),
98
+ ...(host.baseId && { ZITE_BASE_ID: host.baseId }),
99
+ ...Object.fromEntries(Object.entries(host.env ?? {}).filter(([key]) => key.startsWith("ZITE_"))),
100
+ };
101
+ if (host.mode === "demo") {
102
+ host.demoEnv = new Map(Object.entries(own));
103
+ return;
104
+ }
105
+ if (host.env) {
106
+ for (const key of connectionEnvKeys)
107
+ if (!Object.hasOwn(host.env, key))
108
+ delete env[key];
109
+ connectionEnvKeys = new Set(Object.keys(host.env));
110
+ }
111
+ for (const [key, value] of Object.entries(own)) {
112
+ setByRequests.add(key);
113
+ env[key] = value;
114
+ }
115
+ };
@@ -1,7 +1,8 @@
1
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 { AppContext, HostProps, PlatformBinding, PlatformCallInput, PlatformCallResult, PlatformProvider, PlatformUrls, RunRecord, } from "./types.js";
4
+ export type { WorkflowRunResult, WorkflowStop } from "./workflowRun.js";
5
+ export type { AppContext, DurableCall, HostProps, PlatformBinding, PlatformCallInput, PlatformCallResult, PlatformProvider, PlatformUrls, RunRecord, } from "./types.js";
5
6
  /**
6
7
  * Routes every zitejs data, integration and email call to this request's
7
8
  * platform on its app token, and only inside an access scope. The access kind