zitejs 0.10.0-start.28 → 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 (36) 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/keys.d.ts +10 -13
  7. package/dist/esm/signin/keys.js +87 -52
  8. package/dist/esm/signin/signin.js +1 -3
  9. package/dist/esm/signin/verify.d.ts +2 -2
  10. package/dist/esm/signin/verify.js +29 -35
  11. package/dist/esm/start/airtable/cache.d.ts +17 -0
  12. package/dist/esm/start/airtable/cache.js +72 -0
  13. package/dist/esm/start/airtable/dispatch.d.ts +3 -0
  14. package/dist/esm/start/airtable/dispatch.js +14 -12
  15. package/dist/esm/start/airtable/index.js +8 -0
  16. package/dist/esm/start/data/baseRunner.d.ts +5 -0
  17. package/dist/esm/start/data/baseRunner.js +10 -0
  18. package/dist/esm/start/data/dispatch.d.ts +1 -0
  19. package/dist/esm/start/data/dispatch.js +13 -1
  20. package/dist/esm/start/demo.d.ts +11 -0
  21. package/dist/esm/start/demo.js +21 -0
  22. package/dist/esm/start/direct.js +7 -3
  23. package/dist/esm/start/fetch.js +3 -1
  24. package/dist/esm/start/outbound.js +1 -0
  25. package/dist/esm/start/requestEnv.d.ts +8 -0
  26. package/dist/esm/start/requestEnv.js +115 -0
  27. package/dist/esm/start/server.d.ts +2 -1
  28. package/dist/esm/start/server.js +23 -27
  29. package/dist/esm/start/steps.d.ts +15 -15
  30. package/dist/esm/start/steps.js +56 -81
  31. package/dist/esm/start/types.d.ts +37 -7
  32. package/dist/esm/start/version.d.ts +1 -1
  33. package/dist/esm/start/version.js +1 -1
  34. package/dist/esm/start/workflowRun.d.ts +30 -5
  35. package/dist/esm/start/workflowRun.js +28 -20
  36. package/package.json +2 -1
@@ -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();
@@ -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
@@ -8,6 +8,7 @@ import { runRegistration } from "./registration.js";
8
8
  import { isAuthHookName, loadAuthHooks } from "./authHooks.js";
9
9
  import { errorOf, preview } from "./runLog.js";
10
10
  import { DATA_INTEGRATION_ID, runDataCall } from "./data/index.js";
11
+ import { asDemoRefusal, failedOnDemo, noteDemoRefusal } from "./demo.js";
11
12
  import { airtableSettingsOf, runAirtableCall } from "./airtable/index.js";
12
13
  import { withoutConsole } from "./quiet.js";
13
14
  import { rememberConnection, watchOutboundFetches, withGatewayUrl, withRefreshedTokens } from "./outbound.js";
@@ -23,6 +24,7 @@ import { localSignIn, userOfIdentity } from "./localSignIn.js";
23
24
  import { isSignInPath } from "../signin/paths.js";
24
25
  import { getHost, requireScope, runInScope, runWithHost } from "./scope.js";
25
26
  import { workspaceSchemaNames } from "./workspaceSchema.js";
27
+ import { applyRequestEnv, installRequestEnv } from "./requestEnv.js";
26
28
  import { ZITEJS_VERSION } from "./version.js";
27
29
  import { loadUnfinishedWorkflows, loadWorkflows, loggedInput, runWorkflow } from "./workflowRun.js";
28
30
  export { runWithHost } from "./scope.js";
@@ -36,7 +38,7 @@ export { describeApp } from "./describe.js";
36
38
  * props already answer, integrations.get and ZiteMeta.getWorkspace, needs none.
37
39
  */
38
40
  export function installPlatformTransport() {
39
- const sdkCall = async (integrationId, className, methodName, params) => {
41
+ const platformCall = async (integrationId, className, methodName, params) => {
40
42
  const scope = requireScope(`${className}.${methodName}`);
41
43
  const host = getHost();
42
44
  const getsConnection = integrationId === INTEGRATIONS_PLATFORM_ID && methodName === "get";
@@ -69,6 +71,15 @@ export function installPlatformTransport() {
69
71
  const { code, message, userFacingMessage } = result.error;
70
72
  throw new ZiteError({ code: isZiteErrorCode(code) ? code : "INTERNAL_ERROR", message, userFacingMessage });
71
73
  };
74
+ const sdkCall = async (...call) => {
75
+ try {
76
+ return await platformCall(...call);
77
+ }
78
+ catch (error) {
79
+ noteDemoRefusal(getHost(), error);
80
+ throw error;
81
+ }
82
+ };
72
83
  globalThis.__wrapSdkCall = sdkCall;
73
84
  watchOutboundFetches();
74
85
  }
@@ -122,6 +133,7 @@ export function hostContextFromProps(request, props) {
122
133
  envConnections: props.envConnections,
123
134
  connectionServices: props.connectionServices,
124
135
  runRoot: props.runRoot,
136
+ durable: props.durable,
125
137
  runs: [],
126
138
  workspace: props.workspace,
127
139
  connections: props.connections,
@@ -131,29 +143,6 @@ export function hostContextFromProps(request, props) {
131
143
  host.head = platformHeadOf(props.page, { url: request.url, mode: props.mode, context: props });
132
144
  return host;
133
145
  }
134
- // Classic sets these per run. Node and workerd both refuse getters on process.env, so each request writes what the
135
- // host sent, which is the same for every request a build serves: the live URL even on a demo.
136
- // The connection vars the last request set, so one gone since (an integration removed) doesn't linger.
137
- let connectionEnvKeys = new Set();
138
- const setRequestEnv = ({ appUrl, baseId, env: connectionEnv }) => {
139
- const env = globalThis.process?.env;
140
- if (!env)
141
- return;
142
- if (appUrl)
143
- env.ZITE_APP_URL = appUrl;
144
- if (baseId)
145
- env.ZITE_BASE_ID = baseId;
146
- if (!connectionEnv)
147
- return;
148
- for (const key of connectionEnvKeys)
149
- if (!Object.hasOwn(connectionEnv, key))
150
- delete env[key];
151
- for (const [key, value] of Object.entries(connectionEnv)) {
152
- if (key.startsWith("ZITE_"))
153
- env[key] = value;
154
- }
155
- connectionEnvKeys = new Set(Object.keys(connectionEnv));
156
- };
157
146
  /**
158
147
  * A request the host didn't dispatch: the host's warm-up, `zitejs describe` under `vite preview`, and tests. Only the
159
148
  * host can set props, so without them a build answers warm and describe, nobody is signed in and nothing reaches the
@@ -304,7 +293,7 @@ async function invokeWorkflow(request, host, workflows) {
304
293
  return Response.json({ ok: false, error: { code: "NOT_FOUND", message: `No workflow named ${name}` } }, { status: 404 });
305
294
  const input = (await request.json().catch(() => ({})))?.input;
306
295
  const startedAt = Date.now();
307
- const result = await runWorkflow(found.workflow, input, { resuming: host.runRoot !== undefined });
296
+ const result = await runWorkflow(found.workflow, input, { resuming: host.runRoot !== undefined, durable: host.durable });
308
297
  host.runs.push({
309
298
  fn: `workflow:${found.id}`,
310
299
  name: found.id,
@@ -312,7 +301,11 @@ async function invokeWorkflow(request, host, workflows) {
312
301
  startedAt,
313
302
  durationMs: Date.now() - startedAt,
314
303
  input: preview(loggedInput(input)),
315
- ...(result.ok ? { output: preview(result.output) } : { error: result.error }),
304
+ ...(result.stop === "finished"
305
+ ? { output: preview(result.output) }
306
+ : result.stop === "suspended"
307
+ ? { suspended: result.suspension }
308
+ : { error: result.error }),
316
309
  ...(result.steps ? { steps: result.steps } : {}),
317
310
  ...(!result.ok && result.failedStep !== undefined ? { failedStep: result.failedStep } : {}),
318
311
  });
@@ -404,6 +397,7 @@ const pageStatus = (host) => {
404
397
  */
405
398
  export function createZiteHandlers(options) {
406
399
  installPlatformTransport();
400
+ installRequestEnv();
407
401
  // Loaded on first use, so a page request never pays for importing workflow modules.
408
402
  let workflows;
409
403
  const loadedWorkflows = () => (workflows ??= loadWorkflows());
@@ -433,7 +427,7 @@ export function createZiteHandlers(options) {
433
427
  return takeUploadSession(request, host);
434
428
  if (local && host.invoke === null)
435
429
  await loadLocal(host, request);
436
- setRequestEnv(host);
430
+ applyRequestEnv(host);
437
431
  // Starting this isolate was the point: the platform warms a build before its
438
432
  // first visitors, and no app code runs.
439
433
  if (host.invoke?.kind === "warm")
@@ -462,6 +456,8 @@ export function createZiteHandlers(options) {
462
456
  const status = response.status === 500 ? pageStatus(host) : undefined;
463
457
  if (status)
464
458
  response = new Response(response.body, { status, headers: response.headers });
459
+ if (host.invoke === null && !opensPage(request) && failedOnDemo(host, response))
460
+ response = asDemoRefusal(response);
465
461
  if (mounted)
466
462
  response = withSetCookies(response, mounted.signedIn.setCookies);
467
463
  return onBodyEnd(response, () => {
@@ -1,28 +1,28 @@
1
1
  import type { ZiteStep } from "../backend/index.js";
2
- /** What a workflow's handler gets as `step`. */
2
+ import type { DurableCall, StepRecord } from "./types.js";
3
+ export { STEP_RESULT_MAX_BYTES } from "../steps/index.js";
4
+ /** What a workflow's handler gets as `step`. Waits join it once the host can wait between calls. */
3
5
  export type StartStep = Pick<ZiteStep, "run">;
4
- export type StepLog = {
5
- name: string;
6
- status: "ran" | "memoized" | "failed";
7
- durationMs?: number;
8
- at: number;
9
- };
6
+ export type StepLog = StepRecord;
10
7
  type SdkCall = (integrationId: string, className: string, methodName: string, params: unknown) => Promise<unknown>;
11
- export declare const STEP_RESULT_MAX_BYTES: number;
12
8
  /**
13
- * Each completed step's result is saved before the code after it runs, so a
14
- * resumed run gets it back instead of running the step's side effects again.
15
- * Only a resumed run reads saved results, once, at its first step.
9
+ * Each completed step's result is saved before the code after it runs, so a resumed run gets it back instead of
10
+ * running the step's side effects again. Only a resumed run reads saved results, once, at its first step. Without
11
+ * `durable` a step's error reaches the code at once, as in classic's one-call runs.
16
12
  */
17
- export declare function createSteps({ call, resuming, now }: {
13
+ export declare function createSteps({ call, resuming, durable, now, }: {
18
14
  call: SdkCall;
19
15
  resuming: boolean;
16
+ durable?: DurableCall;
20
17
  now?: () => number;
21
18
  }): {
22
19
  step: StartStep;
23
- log: StepLog[];
20
+ log: StepRecord[];
24
21
  failedStep: () => string | undefined;
25
- /** A step that throws inside `Promise.all` ends the run while its siblings still save. */
22
+ stopped: Promise<void>;
23
+ stop: () => import("../steps/index.js").StepStop | undefined;
24
+ retry: () => import("../steps/index.js").StepRetry | undefined;
25
+ suspension: () => import("../steps/index.js").StepSuspension | undefined;
26
+ saved: () => number;
26
27
  settle: () => Promise<void>;
27
28
  };
28
- export {};