@protobase/workflows 0.68.4 → 0.68.6

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.
@@ -1,8 +1,9 @@
1
1
  import { StepError } from "../run/errors.js";
2
2
  import { FileValue, JsonSchema, PluginConnection, PluginFetch, PluginFetchInit, PluginFiles, PluginMail, PluginMailMessage, PluginRecords, PluginServices, StepContext } from "./context.js";
3
- import { ConnectionDeclaration, Plugin, PollingTrigger, ReadyTrigger, StepType, TriggerContext, TriggerItem, TriggerType, WebhookTrigger } from "./types.js";
3
+ import { ConnectionDeclaration, Plugin, PollEvery, PollingTrigger, ReadyTrigger, StepType, TriggerContext, TriggerItem, TriggerType, WebhookEvent, WebhookItem, WebhookTrigger } from "./types.js";
4
4
  import { ConnectionError } from "./connection-error.js";
5
5
  import { definePlugin, pluginApi } from "./define-plugin.js";
6
6
  import { AddressRefusedError, ResponseTooLargeError, fetchMaxBytes } from "./fetch-errors.js";
7
7
  import { isFileValue } from "./file-value.js";
8
- export { AddressRefusedError, type ConnectionDeclaration, ConnectionError, type FileValue, type JsonSchema, type Plugin, type PluginConnection, type PluginFetch, type PluginFetchInit, type PluginFiles, type PluginMail, type PluginMailMessage, type PluginRecords, type PluginServices, type PollingTrigger, type ReadyTrigger, ResponseTooLargeError, type StepContext, StepError, type StepType, type TriggerContext, type TriggerItem, type TriggerType, type WebhookTrigger, definePlugin, fetchMaxBytes, isFileValue, pluginApi };
8
+ import { webhookBodyCeiling, webhookBodyDefault, webhookInputCheck, webhookInputProperties } from "./webhook-input.js";
9
+ export { AddressRefusedError, type ConnectionDeclaration, ConnectionError, type FileValue, type JsonSchema, type Plugin, type PluginConnection, type PluginFetch, type PluginFetchInit, type PluginFiles, type PluginMail, type PluginMailMessage, type PluginRecords, type PluginServices, type PollEvery, type PollingTrigger, type ReadyTrigger, ResponseTooLargeError, type StepContext, StepError, type StepType, type TriggerContext, type TriggerItem, type TriggerType, type WebhookEvent, type WebhookItem, type WebhookTrigger, definePlugin, fetchMaxBytes, isFileValue, pluginApi, webhookBodyCeiling, webhookBodyDefault, webhookInputCheck, webhookInputProperties };
@@ -3,4 +3,5 @@ import { definePlugin, pluginApi } from "./define-plugin.js";
3
3
  import { ConnectionError } from "./connection-error.js";
4
4
  import { AddressRefusedError, ResponseTooLargeError, fetchMaxBytes } from "./fetch-errors.js";
5
5
  import { isFileValue } from "./file-value.js";
6
- export { AddressRefusedError, ConnectionError, ResponseTooLargeError, StepError, definePlugin, fetchMaxBytes, isFileValue, pluginApi };
6
+ import { webhookBodyCeiling, webhookBodyDefault, webhookInputCheck, webhookInputProperties } from "./webhook-input.js";
7
+ export { AddressRefusedError, ConnectionError, ResponseTooLargeError, StepError, definePlugin, fetchMaxBytes, isFileValue, pluginApi, webhookBodyCeiling, webhookBodyDefault, webhookInputCheck, webhookInputProperties };
@@ -20,7 +20,7 @@ const manual = {
20
20
  const typeCatalog = (types) => ({
21
21
  step_types: types.filter((type) => type.kind === "step").map(entry),
22
22
  triggers: [manual, ...types.filter((type) => type.kind !== "step").map(entry)],
23
- note: "A definition holds its trigger as { \"<id>\": <input> }, such as { \"app:record\": { \"resource\": \"orders\" } }. A type with available false cannot be saved here; its reason says why. A trigger other than manual starts runs only while its workflow is enabled, its enabled_version set; run_workflow starts a run of any workflow, enabled or not, so a manual workflow needs no enabled_version."
23
+ note: "A definition holds its trigger as { \"<id>\": <input> }, such as { \"app:record\": { \"resource\": \"orders\" } }. A type with available false cannot be saved here; its reason says why. A trigger other than manual starts runs only while its workflow is enabled, its enabled_version set, as enable_workflow sets it; run_workflow starts a run of any workflow, enabled or not, so a manual workflow needs no enabled_version."
24
24
  });
25
25
  //#endregion
26
26
  export { typeCatalog };
@@ -51,34 +51,57 @@ export type TriggerItem = {
51
51
  key: string;
52
52
  data: unknown;
53
53
  };
54
+ /** How often a polling trigger polls: a duration such as `1 minute`, or a cron in an IANA time zone (default UTC). */
55
+ export type PollEvery = string | {
56
+ cron: string;
57
+ timezone?: string;
58
+ };
54
59
  /** A trigger that asks a service for new items on a schedule. */
55
60
  export type PollingTrigger = TriggerBase & {
56
61
  kind: 'polling';
57
- /** A duration such as `1 minute`, or a cron in an IANA time zone. */
58
- every: string | {
59
- cron: string;
60
- timezone?: string;
61
- };
62
+ /** How often it polls, or how often for a given input, such as a schedule's own cron. */
63
+ every: PollEvery | ((input: Record<string, unknown>) => PollEvery);
62
64
  /** The most items one poll returns, at most 100. */
63
65
  limit?: number;
66
+ /** Called once, when the trigger has no cursor yet: its first cursor, such as the newest item's id. */
64
67
  start: (context: TriggerContext) => Promise<unknown>;
65
- /** Items after `cursor`, and the next cursor, which must move forward. */
68
+ /** Items after `cursor`, and the next cursor, which must move forward. `due` is the time this poll was queued for. */
66
69
  poll: (context: TriggerContext & {
67
70
  cursor: unknown;
68
71
  store: unknown;
69
72
  limit: number;
73
+ due: Date;
70
74
  }) => Promise<{
71
75
  items: TriggerItem[];
72
76
  cursor: unknown;
73
77
  store?: unknown;
74
78
  }>;
75
79
  };
80
+ /**
81
+ * A webhook call as the run sees it, once the route checked its signature: the method, the query, the headers without
82
+ * any credential (`authorization`, `cookie`, `webhook-*`, the signature header, `x-forwarded-*`), the body as JSON, form
83
+ * fields or text, or as a file of the run when the call would be over 1 MiB as JSON, and when it came. The address is left out: it holds
84
+ * the key.
85
+ */
86
+ export type WebhookEvent = {
87
+ method: string;
88
+ query: Record<string, string>;
89
+ headers: Record<string, string>;
90
+ body: unknown;
91
+ received_at: string;
92
+ };
93
+ /** One run a webhook call starts: `key` starts it once per workflow, such as the sender's message id. */
94
+ export type WebhookItem = {
95
+ key?: string;
96
+ data: unknown;
97
+ };
76
98
  /** A trigger that the app's webhook URL starts, after the route checked the call's signature. */
77
99
  export type WebhookTrigger = TriggerBase & {
78
100
  kind: 'webhook';
79
- /** Registers the URL with the service, returning what `unsubscribe` and `renew` need. */
101
+ /** Registers the URL and its secret with the service, returning what `unsubscribe` and `renew` need. */
80
102
  subscribe?: (context: TriggerContext & {
81
103
  url: string;
104
+ secret: string;
82
105
  }) => Promise<unknown>;
83
106
  unsubscribe?: (context: TriggerContext & {
84
107
  state: unknown;
@@ -87,11 +110,17 @@ export type WebhookTrigger = TriggerBase & {
87
110
  state: unknown;
88
111
  }) => Promise<unknown>;
89
112
  renewEvery?: string;
113
+ /**
114
+ * The runs a call starts, at most 100: `event` is the call as above, `request` the same call with its bytes, and
115
+ * `idempotencyKey` the signed `webhook-id` or the configured idempotency header, when the call has one.
116
+ */
90
117
  receive: (context: {
91
118
  request: Request;
119
+ event: WebhookEvent;
120
+ idempotencyKey: string | undefined;
92
121
  input: Record<string, unknown>;
93
122
  }) => Promise<{
94
- items: TriggerItem[];
123
+ items: WebhookItem[];
95
124
  response?: Response;
96
125
  }>;
97
126
  };
@@ -0,0 +1,18 @@
1
+ import { JsonSchema } from "./context.js";
2
+ //#region src/plugin/webhook-input.d.ts
3
+ /** The largest body a webhook call may have, whatever its trigger's `body_limit` says: 10 MiB. */
4
+ export declare const webhookBodyCeiling: number;
5
+ /** The body a webhook call may have when its trigger names no `body_limit`: 1 MiB. */
6
+ export declare const webhookBodyDefault: number;
7
+ /**
8
+ * What the route reads of every webhook trigger's input, before any plugin code sees the call: how a call proves it
9
+ * comes from the sender (`auth`), its largest body, how many calls a minute start runs, and the header that keys a run
10
+ * when the sender signs no `webhook-id`. A webhook type spreads these into its own input schema.
11
+ */
12
+ export declare const webhookInputProperties: Record<string, JsonSchema>;
13
+ /** What `hmac` needs beyond its mode: the header. */
14
+ export declare const webhookInputCheck: (input: Record<string, unknown>) => {
15
+ field: string;
16
+ message: string;
17
+ }[];
18
+ //#endregion
@@ -0,0 +1,80 @@
1
+ //#region src/plugin/webhook-input.ts
2
+ /** The largest body a webhook call may have, whatever its trigger's `body_limit` says: 10 MiB. */
3
+ const webhookBodyCeiling = 10485760;
4
+ /** The body a webhook call may have when its trigger names no `body_limit`: 1 MiB. */
5
+ const webhookBodyDefault = 1048576;
6
+ /**
7
+ * What the route reads of every webhook trigger's input, before any plugin code sees the call: how a call proves it
8
+ * comes from the sender (`auth`), its largest body, how many calls a minute start runs, and the header that keys a run
9
+ * when the sender signs no `webhook-id`. A webhook type spreads these into its own input schema.
10
+ */
11
+ const webhookInputProperties = {
12
+ auth: {
13
+ type: "object",
14
+ additionalProperties: false,
15
+ required: ["mode"],
16
+ description: "how a call proves it comes from the sender; default { \"mode\": \"standard_webhooks\" }",
17
+ properties: {
18
+ mode: {
19
+ enum: [
20
+ "standard_webhooks",
21
+ "hmac",
22
+ "bearer"
23
+ ],
24
+ description: "standard_webhooks: webhook-id, webhook-timestamp and webhook-signature headers; hmac: an HMAC of the body in header; bearer: Authorization: Bearer <secret>"
25
+ },
26
+ header: {
27
+ type: "string",
28
+ minLength: 1,
29
+ maxLength: 100,
30
+ description: "hmac: the header that carries the signature, such as x-hub-signature-256"
31
+ },
32
+ prefix: {
33
+ type: "string",
34
+ maxLength: 50,
35
+ description: "hmac: text before the signature, such as sha256="
36
+ },
37
+ encoding: {
38
+ enum: ["hex", "base64"],
39
+ description: "hmac: how the signature is written; default hex"
40
+ },
41
+ hash: {
42
+ enum: [
43
+ "SHA-1",
44
+ "SHA-256",
45
+ "SHA-512"
46
+ ],
47
+ description: "hmac: default SHA-256"
48
+ }
49
+ }
50
+ },
51
+ body_limit: {
52
+ type: "integer",
53
+ minimum: 1,
54
+ maximum: webhookBodyCeiling,
55
+ description: "the largest body in bytes; default 1048576 (1 MiB), at most 10485760 (10 MiB)"
56
+ },
57
+ per_minute: {
58
+ type: "integer",
59
+ minimum: 1,
60
+ maximum: 600,
61
+ description: "calls a minute that start runs; default 60"
62
+ },
63
+ idempotency_header: {
64
+ type: "string",
65
+ minLength: 1,
66
+ maxLength: 100,
67
+ description: "a header whose value starts one run per value, when the sender signs no webhook-id"
68
+ }
69
+ };
70
+ /** What `hmac` needs beyond its mode: the header. */
71
+ const webhookInputCheck = (input) => {
72
+ const auth = input.auth;
73
+ if (auth?.mode === "hmac" && !auth.header) return [{
74
+ field: "auth.header",
75
+ message: "is missing: hmac reads its signature from this header"
76
+ }];
77
+ return [];
78
+ };
79
+ //#endregion
80
+ export { webhookBodyCeiling, webhookBodyDefault, webhookInputCheck, webhookInputProperties };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/workflows",
3
- "version": "0.68.4",
3
+ "version": "0.68.6",
4
4
  "description": "Workflows as state machines with JSONata expressions: their check, a runner that saves every step, and the plugin interface for trigger and step types.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {