zitejs 0.9.131 → 0.9.132

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 (47) hide show
  1. package/dist/cjs/start/describeBuilt.d.ts +3 -0
  2. package/dist/cjs/start/describeBuilt.js +106 -0
  3. package/dist/cjs/start/entries.d.ts +20 -0
  4. package/dist/cjs/start/entries.js +104 -0
  5. package/dist/cjs/start/problems.d.ts +19 -0
  6. package/dist/cjs/start/problems.js +11 -0
  7. package/dist/esm/start/describe.d.ts +64 -0
  8. package/dist/esm/start/describe.js +176 -0
  9. package/dist/esm/start/describeBuilt.d.ts +3 -0
  10. package/dist/esm/start/describeBuilt.js +66 -0
  11. package/dist/esm/start/descriptions.d.ts +4 -0
  12. package/dist/esm/start/descriptions.js +47 -0
  13. package/dist/esm/start/entries.d.ts +20 -0
  14. package/dist/esm/start/entries.js +93 -0
  15. package/dist/esm/start/host.d.ts +3 -0
  16. package/dist/esm/start/host.js +4 -0
  17. package/dist/esm/start/index.d.ts +79 -0
  18. package/dist/esm/start/index.js +157 -0
  19. package/dist/esm/start/instance.d.ts +12 -0
  20. package/dist/esm/start/instance.js +73 -0
  21. package/dist/esm/start/problems.d.ts +19 -0
  22. package/dist/esm/start/problems.js +8 -0
  23. package/dist/esm/start/registration.d.ts +13 -0
  24. package/dist/esm/start/registration.js +21 -0
  25. package/dist/esm/start/router.d.ts +17 -0
  26. package/dist/esm/start/router.js +37 -0
  27. package/dist/esm/start/runLog.d.ts +5 -0
  28. package/dist/esm/start/runLog.js +18 -0
  29. package/dist/esm/start/scope.d.ts +15 -0
  30. package/dist/esm/start/scope.js +58 -0
  31. package/dist/esm/start/server.d.ts +51 -0
  32. package/dist/esm/start/server.js +215 -0
  33. package/dist/esm/start/types.d.ts +94 -0
  34. package/dist/esm/start/types.js +1 -0
  35. package/dist/esm/start/vite.d.ts +9 -0
  36. package/dist/esm/start/vite.js +144 -0
  37. package/dist/esm/start/worker.d.ts +11 -0
  38. package/dist/esm/start/worker.js +20 -0
  39. package/dist/esm/start/workflow.d.ts +50 -0
  40. package/dist/esm/start/workflow.js +13 -0
  41. package/dist/esm/start/workflow.test-d.d.ts +1 -0
  42. package/dist/esm/start/workflow.test-d.js +39 -0
  43. package/dist/esm/start/workflowModules.d.ts +1 -0
  44. package/dist/esm/start/workflowModules.js +3 -0
  45. package/dist/esm/start/workflowRun.d.ts +80 -0
  46. package/dist/esm/start/workflowRun.js +107 -0
  47. package/package.json +1 -1
@@ -0,0 +1,3 @@
1
+ // Replaced at build by zite() with a lazy import of every app module that
2
+ // mentions createWorkflow. Empty wherever the plugin didn't build the app.
3
+ export const workflowModules = {};
@@ -0,0 +1,80 @@
1
+ import { type Workflow } from "./workflow.js";
2
+ export type TriggerReport = {
3
+ handled: string[];
4
+ cursor?: string;
5
+ };
6
+ export type WorkflowRunResult = {
7
+ ok: true;
8
+ output: unknown;
9
+ trigger?: TriggerReport;
10
+ } | {
11
+ ok: false;
12
+ error: {
13
+ code: string;
14
+ message: string;
15
+ };
16
+ trigger?: TriggerReport;
17
+ };
18
+ /**
19
+ * Splits the platform's state out of the input. A poll or a provider delivery
20
+ * with a cursor gets `seen`, `handled` and `advance`. Handled ids are reported
21
+ * whether or not the run throws, so an item acted on is never acted on again;
22
+ * the staged cursor only on success. Only ids the incoming `seen` lacked are
23
+ * reported: billing reads an empty list as a run that found nothing new.
24
+ */
25
+ export declare function triggerContext(workflow: Workflow, rawInput: unknown): {
26
+ input: unknown;
27
+ trigger?: undefined;
28
+ report?: undefined;
29
+ } | {
30
+ input: {
31
+ [x: string]: unknown;
32
+ };
33
+ trigger: {
34
+ provider: string;
35
+ deliveryId?: string | undefined;
36
+ headers: Record<string, unknown>;
37
+ } | {
38
+ headers?: undefined;
39
+ } | {
40
+ headers: {};
41
+ };
42
+ report?: undefined;
43
+ } | {
44
+ input: {
45
+ [x: string]: unknown;
46
+ };
47
+ trigger: {
48
+ provider: string;
49
+ deliveryId?: string | undefined;
50
+ headers: Record<string, unknown>;
51
+ cursor: string | null;
52
+ seen: (id: string) => boolean;
53
+ handled: (id: string) => void;
54
+ advance: (cursor: string) => void;
55
+ } | {
56
+ headers?: undefined;
57
+ cursor: string | null;
58
+ seen: (id: string) => boolean;
59
+ handled: (id: string) => void;
60
+ advance: (cursor: string) => void;
61
+ } | {
62
+ headers: {};
63
+ cursor: string | null;
64
+ seen: (id: string) => boolean;
65
+ handled: (id: string) => void;
66
+ advance: (cursor: string) => void;
67
+ };
68
+ report: (succeeded: boolean) => TriggerReport;
69
+ };
70
+ /** Runs the handler in a workflow access scope: no user, and data calls allowed. */
71
+ export declare function runWorkflow(workflow: Workflow, rawInput: unknown): Promise<WorkflowRunResult>;
72
+ export type LoadedWorkflow = {
73
+ id: string;
74
+ file: string;
75
+ workflow: Workflow;
76
+ };
77
+ /** Every exported workflow in the app, by export name. */
78
+ export declare function loadWorkflows(modules?: Record<string, () => Promise<Record<string, unknown>>>): Promise<LoadedWorkflow[]>;
79
+ /** The input as the run log keeps it: a trigger's `seen` list can hold thousands of ids. */
80
+ export declare const loggedInput: (rawInput: unknown) => unknown;
@@ -0,0 +1,107 @@
1
+ // Server-only: runs a workflow the host invoked. Takes the same platform input
2
+ // as classic triggered endpoints (`__cron`, `__poll`, `__webhook`), so the
3
+ // runner's schedules, polls and provider deliveries work unchanged.
4
+ import { errorOf } from "./runLog.js";
5
+ import { runInScope } from "./scope.js";
6
+ import { isWorkflow } from "./workflow.js";
7
+ import { workflowModules } from "./workflowModules.js";
8
+ const asRecord = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : null;
9
+ const idOf = (id) => {
10
+ if (typeof id !== "string" && typeof id !== "number")
11
+ throw new TypeError(`trigger ids must be strings, got ${typeof id}`);
12
+ const value = String(id);
13
+ if (value.length === 0 || value.length > 512)
14
+ throw new TypeError("trigger ids must be 1 to 512 characters");
15
+ return value;
16
+ };
17
+ /**
18
+ * Splits the platform's state out of the input. A poll or a provider delivery
19
+ * with a cursor gets `seen`, `handled` and `advance`. Handled ids are reported
20
+ * whether or not the run throws, so an item acted on is never acted on again;
21
+ * the staged cursor only on success. Only ids the incoming `seen` lacked are
22
+ * reported: billing reads an empty list as a run that found nothing new.
23
+ */
24
+ export function triggerContext(workflow, rawInput) {
25
+ const raw = asRecord(rawInput);
26
+ const { __cron, __poll, __webhook, ...input } = raw ?? {};
27
+ const declared = workflow.config.trigger;
28
+ if (!declared)
29
+ return { input: raw ? input : rawInput };
30
+ const poll = asRecord(__poll);
31
+ const webhook = asRecord(__webhook);
32
+ const delivery = typeof webhook?.provider === "string" ? webhook : null;
33
+ // A run with no trigger state, such as a manual one, still gets a trigger: an empty one.
34
+ const info = delivery
35
+ ? {
36
+ headers: asRecord(delivery.headers) ?? {},
37
+ ...(typeof delivery.deliveryId === "string" ? { deliveryId: delivery.deliveryId } : {}),
38
+ provider: delivery.provider,
39
+ }
40
+ : declared.every
41
+ ? {}
42
+ : { headers: {} };
43
+ if (!poll && declared.events)
44
+ return { input, trigger: info };
45
+ const state = poll ?? delivery ?? {};
46
+ const known = new Set(Array.isArray(state.seen) ? state.seen.map(String) : []);
47
+ const handled = [];
48
+ let staged;
49
+ const trigger = {
50
+ cursor: typeof state.cursor === "string" ? state.cursor : null,
51
+ seen: (id) => known.has(String(id)),
52
+ handled: (id) => {
53
+ const value = idOf(id);
54
+ if (known.has(value))
55
+ return;
56
+ known.add(value);
57
+ handled.push(value);
58
+ },
59
+ advance: (cursor) => {
60
+ if (typeof cursor !== "string" || cursor.length > 4096) {
61
+ throw new TypeError("trigger.advance takes a string of at most 4096 characters");
62
+ }
63
+ staged = cursor;
64
+ },
65
+ ...info,
66
+ };
67
+ const report = (succeeded) => ({
68
+ handled: handled.slice(-2000),
69
+ ...(succeeded && staged !== undefined ? { cursor: staged } : {}),
70
+ });
71
+ return { input, trigger, report };
72
+ }
73
+ /** Runs the handler in a workflow access scope: no user, and data calls allowed. */
74
+ export async function runWorkflow(workflow, rawInput) {
75
+ const { input, trigger, report } = triggerContext(workflow, rawInput);
76
+ const args = { input, ...(trigger ? { trigger } : {}) };
77
+ try {
78
+ const output = await runInScope({ kind: "workflow", user: null }, () => workflow.handler(args));
79
+ return { ok: true, output, ...(report ? { trigger: report(true) } : {}) };
80
+ }
81
+ catch (error) {
82
+ return { ok: false, error: errorOf(error), ...(report ? { trigger: report(false) } : {}) };
83
+ }
84
+ }
85
+ /** Every exported workflow in the app, by export name. */
86
+ export async function loadWorkflows(modules = workflowModules) {
87
+ const found = [];
88
+ for (const [file, load] of Object.entries(modules)) {
89
+ for (const [id, value] of Object.entries(await load()))
90
+ if (isWorkflow(value))
91
+ found.push({ id, file, workflow: value });
92
+ }
93
+ return found;
94
+ }
95
+ /** The input as the run log keeps it: a trigger's `seen` list can hold thousands of ids. */
96
+ export const loggedInput = (rawInput) => {
97
+ const raw = asRecord(rawInput);
98
+ if (!raw)
99
+ return rawInput;
100
+ const out = { ...raw };
101
+ for (const key of ["__poll", "__webhook"]) {
102
+ const state = asRecord(raw[key]);
103
+ if (state && Array.isArray(state.seen))
104
+ out[key] = { ...state, seen: `[${state.seen.length} ids]` };
105
+ }
106
+ return out;
107
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zitejs",
3
- "version": "0.9.131",
3
+ "version": "0.9.132",
4
4
  "description": "The Zite framework — build apps on Zite Database",
5
5
  "type": "module",
6
6
  "sideEffects": false,