@trigora/sdk 0.3.0 → 0.5.0

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/README.md CHANGED
@@ -184,6 +184,36 @@ If you do not provide your own payload type, `event.payload` defaults to `JsonVa
184
184
 
185
185
  For webhook flows, request metadata may also be available on `event.request`, including headers, method, URL, and the raw request body. `event.request.rawBody` is useful when you want to verify webhook signatures yourself.
186
186
 
187
+ ## Stripe Webhooks
188
+
189
+ Trigora includes a small helper for verifying Stripe webhook signatures:
190
+
191
+ ```ts
192
+ import { verifyStripeWebhook } from '@trigora/sdk/stripe';
193
+
194
+ const stripeEvent = await verifyStripeWebhook(event, {
195
+ secret: ctx.env.STRIPE_WEBHOOK_SECRET,
196
+ });
197
+ ```
198
+
199
+ This verifies against `event.request.rawBody` and the `Stripe-Signature` header.
200
+
201
+ Production Stripe webhooks should verify signatures before trusting payloads.
202
+
203
+ ## GitHub Webhooks
204
+
205
+ Trigora includes a small helper for verifying GitHub webhook signatures:
206
+
207
+ ```ts
208
+ import { verifyGitHubWebhook } from '@trigora/sdk/github';
209
+
210
+ const githubEvent = await verifyGitHubWebhook(event, {
211
+ secret: ctx.env.GITHUB_WEBHOOK_SECRET,
212
+ });
213
+ ```
214
+
215
+ This verifies against `event.request.rawBody` and the `X-Hub-Signature-256` header.
216
+
187
217
  ## Context
188
218
 
189
219
  Flows receive a context object with logging and environment access:
@@ -196,6 +226,8 @@ ctx.log.error('message')
196
226
  ctx.env
197
227
  ```
198
228
 
229
+ In local CLI runs, `ctx.env` includes the process environment after loading `.env` and `.env.local`, with existing shell variables taking precedence.
230
+
199
231
  The exact exported type is `FlowContext<TEnv>`.
200
232
 
201
233
  ## Exported Types
@@ -1,4 +1,10 @@
1
- import type { CronFlowDefinition, JsonValue, ManualFlowDefinition, WebhookFlowDefinition } from '@trigora/contracts';
1
+ import type { CronEventPayload, CronFlowDefinition, FlowDefinition, FlowRunFn, JsonValue, ManualFlowDefinition, ManualTrigger, Trigger, WebhookTrigger, WebhookFlowDefinition, CronTrigger } from '@trigora/contracts';
2
+ type DefineFlowInput<TPayload, TEnv extends Record<string, string>, TTrigger extends Trigger> = {
3
+ id: string;
4
+ trigger: TTrigger;
5
+ run: TTrigger extends ManualTrigger ? FlowRunFn<TPayload, TEnv, ManualTrigger> : TTrigger extends WebhookTrigger ? FlowRunFn<TPayload, TEnv, WebhookTrigger> : TTrigger extends CronTrigger ? FlowRunFn<CronEventPayload, TEnv, CronTrigger> : never;
6
+ };
7
+ type DefineFlowOutput<TPayload, TEnv extends Record<string, string>, TTrigger extends Trigger> = TTrigger extends ManualTrigger ? ManualFlowDefinition<TPayload, TEnv> : TTrigger extends WebhookTrigger ? WebhookFlowDefinition<TPayload, TEnv> : TTrigger extends CronTrigger ? CronFlowDefinition<TEnv> : FlowDefinition<TPayload, TEnv, TTrigger>;
2
8
  /**
3
9
  * Define a Trigora flow.
4
10
  *
@@ -29,6 +35,8 @@ import type { CronFlowDefinition, JsonValue, ManualFlowDefinition, WebhookFlowDe
29
35
  * });
30
36
  * ```
31
37
  */
38
+ export declare function defineFlow<TTrigger extends Trigger, TPayload = JsonValue, TEnv extends Record<string, string> = Record<string, string>>(flow: DefineFlowInput<TPayload, TEnv, TTrigger>): DefineFlowOutput<TPayload, TEnv, TTrigger>;
32
39
  export declare function defineFlow<TPayload = JsonValue, TEnv extends Record<string, string> = Record<string, string>>(flow: ManualFlowDefinition<TPayload, TEnv>): ManualFlowDefinition<TPayload, TEnv>;
33
40
  export declare function defineFlow<TPayload = JsonValue, TEnv extends Record<string, string> = Record<string, string>>(flow: WebhookFlowDefinition<TPayload, TEnv>): WebhookFlowDefinition<TPayload, TEnv>;
34
- export declare function defineFlow<TPayload = JsonValue, TEnv extends Record<string, string> = Record<string, string>>(flow: CronFlowDefinition<TPayload, TEnv>): CronFlowDefinition<TPayload, TEnv>;
41
+ export declare function defineFlow<TEnv extends Record<string, string> = Record<string, string>>(flow: CronFlowDefinition<TEnv>): CronFlowDefinition<TEnv>;
42
+ export {};
@@ -0,0 +1,8 @@
1
+ import type { WebhookFlowEvent } from '@trigora/contracts';
2
+ export type VerifyGitHubWebhookOptions = {
3
+ secret: string | undefined;
4
+ };
5
+ export declare class GitHubWebhookVerificationError extends Error {
6
+ name: string;
7
+ }
8
+ export declare function verifyGitHubWebhook<T = unknown>(event: WebhookFlowEvent, options: VerifyGitHubWebhookOptions): Promise<T>;
package/dist/github.js ADDED
@@ -0,0 +1,120 @@
1
+ // src/webhookSignature.ts
2
+ function encodeUtf8(value) {
3
+ return new TextEncoder().encode(value);
4
+ }
5
+ function toArrayBuffer(bytes) {
6
+ return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
7
+ }
8
+ function bytesToHex(bytes) {
9
+ return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
10
+ }
11
+ function getHeaderCaseInsensitive(headers, headerName) {
12
+ const normalizedName = headerName.toLowerCase();
13
+ for (const [key, value] of Object.entries(headers)) {
14
+ if (key.toLowerCase() === normalizedName) {
15
+ return value;
16
+ }
17
+ }
18
+ return void 0;
19
+ }
20
+ function isHexString(value) {
21
+ return value.length > 0 && value.length % 2 === 0 && /^[0-9a-f]+$/i.test(value);
22
+ }
23
+ function timingSafeEqualStrings(left, right) {
24
+ const leftBytes = encodeUtf8(left);
25
+ const rightBytes = encodeUtf8(right);
26
+ const length = Math.max(leftBytes.length, rightBytes.length);
27
+ let difference = leftBytes.length === rightBytes.length ? 0 : 1;
28
+ for (let index = 0; index < length; index += 1) {
29
+ difference |= (leftBytes[index] ?? 0) ^ (rightBytes[index] ?? 0);
30
+ }
31
+ return difference === 0;
32
+ }
33
+ async function computeHmacSha256Hex(secret, signedPayload) {
34
+ const subtle = globalThis.crypto?.subtle;
35
+ if (!subtle) {
36
+ throw new Error("Web Crypto API is unavailable in this runtime.");
37
+ }
38
+ const key = await subtle.importKey(
39
+ "raw",
40
+ toArrayBuffer(encodeUtf8(secret)),
41
+ {
42
+ name: "HMAC",
43
+ hash: "SHA-256"
44
+ },
45
+ false,
46
+ ["sign"]
47
+ );
48
+ const signature = await subtle.sign("HMAC", key, toArrayBuffer(encodeUtf8(signedPayload)));
49
+ return bytesToHex(new Uint8Array(signature));
50
+ }
51
+ function requireWebhookRequestParts(event, providerName) {
52
+ if (!event.request || typeof event.request.headers !== "object" || event.request.headers === null) {
53
+ throw new Error(`${providerName} webhook request metadata is required.`);
54
+ }
55
+ if (typeof event.request.rawBody !== "string") {
56
+ throw new Error(`${providerName} webhook raw body is required.`);
57
+ }
58
+ return {
59
+ headers: event.request.headers,
60
+ rawBody: event.request.rawBody
61
+ };
62
+ }
63
+
64
+ // src/github.ts
65
+ var GITHUB_SIGNATURE_HEADER = "x-hub-signature-256";
66
+ var GITHUB_SIGNATURE_PREFIX = "sha256=";
67
+ var GitHubWebhookVerificationError = class extends Error {
68
+ name = "GitHubWebhookVerificationError";
69
+ };
70
+ function createVerificationError(message) {
71
+ return new GitHubWebhookVerificationError(message);
72
+ }
73
+ function parseGitHubSignatureHeader(headerValue) {
74
+ const normalizedValue = headerValue.trim();
75
+ if (!normalizedValue.toLowerCase().startsWith(GITHUB_SIGNATURE_PREFIX)) {
76
+ return null;
77
+ }
78
+ const signature = normalizedValue.slice(GITHUB_SIGNATURE_PREFIX.length).trim().toLowerCase();
79
+ if (!isHexString(signature)) {
80
+ return null;
81
+ }
82
+ return signature;
83
+ }
84
+ async function verifyGitHubWebhook(event, options) {
85
+ if (!options.secret || options.secret.trim().length === 0) {
86
+ throw createVerificationError("A GitHub webhook secret is required.");
87
+ }
88
+ let requestParts;
89
+ try {
90
+ requestParts = requireWebhookRequestParts(event, "GitHub");
91
+ } catch (error) {
92
+ throw createVerificationError(error instanceof Error ? error.message : String(error));
93
+ }
94
+ const signatureHeader = getHeaderCaseInsensitive(requestParts.headers, GITHUB_SIGNATURE_HEADER);
95
+ if (!signatureHeader) {
96
+ throw createVerificationError("X-Hub-Signature-256 header is required.");
97
+ }
98
+ const parsedSignature = parseGitHubSignatureHeader(signatureHeader);
99
+ if (!parsedSignature) {
100
+ throw createVerificationError("X-Hub-Signature-256 header is invalid.");
101
+ }
102
+ let expectedSignature;
103
+ try {
104
+ expectedSignature = await computeHmacSha256Hex(options.secret, requestParts.rawBody);
105
+ } catch (error) {
106
+ throw createVerificationError(error instanceof Error ? error.message : String(error));
107
+ }
108
+ if (!timingSafeEqualStrings(parsedSignature, expectedSignature)) {
109
+ throw createVerificationError("GitHub webhook signature did not match.");
110
+ }
111
+ try {
112
+ return JSON.parse(requestParts.rawBody);
113
+ } catch {
114
+ throw createVerificationError("GitHub webhook body contained invalid JSON.");
115
+ }
116
+ }
117
+ export {
118
+ GitHubWebhookVerificationError,
119
+ verifyGitHubWebhook
120
+ };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { defineFlow } from './defineFlow';
2
- export type { FlowContext, FlowDefinition, FlowEvent, FlowRunFn, JsonValue, Trigger, WebhookFlowResult, } from '@trigora/contracts';
2
+ export type { CronEventPayload, CronFlowEvent, FlowContext, FlowDefinition, FlowEvent, FlowRunFn, JsonValue, ManualFlowEvent, Trigger, WebhookFlowEvent, WebhookFlowResult, } from '@trigora/contracts';
@@ -0,0 +1,9 @@
1
+ import type { WebhookFlowEvent } from '@trigora/contracts';
2
+ export type VerifyStripeWebhookOptions = {
3
+ secret: string | undefined;
4
+ toleranceSeconds?: number;
5
+ };
6
+ export declare class StripeWebhookVerificationError extends Error {
7
+ name: string;
8
+ }
9
+ export declare function verifyStripeWebhook<T = unknown>(event: WebhookFlowEvent, options: VerifyStripeWebhookOptions): Promise<T>;
package/dist/stripe.js ADDED
@@ -0,0 +1,147 @@
1
+ // src/webhookSignature.ts
2
+ function encodeUtf8(value) {
3
+ return new TextEncoder().encode(value);
4
+ }
5
+ function toArrayBuffer(bytes) {
6
+ return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
7
+ }
8
+ function bytesToHex(bytes) {
9
+ return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
10
+ }
11
+ function getHeaderCaseInsensitive(headers, headerName) {
12
+ const normalizedName = headerName.toLowerCase();
13
+ for (const [key, value] of Object.entries(headers)) {
14
+ if (key.toLowerCase() === normalizedName) {
15
+ return value;
16
+ }
17
+ }
18
+ return void 0;
19
+ }
20
+ function isHexString(value) {
21
+ return value.length > 0 && value.length % 2 === 0 && /^[0-9a-f]+$/i.test(value);
22
+ }
23
+ function timingSafeEqualStrings(left, right) {
24
+ const leftBytes = encodeUtf8(left);
25
+ const rightBytes = encodeUtf8(right);
26
+ const length = Math.max(leftBytes.length, rightBytes.length);
27
+ let difference = leftBytes.length === rightBytes.length ? 0 : 1;
28
+ for (let index = 0; index < length; index += 1) {
29
+ difference |= (leftBytes[index] ?? 0) ^ (rightBytes[index] ?? 0);
30
+ }
31
+ return difference === 0;
32
+ }
33
+ async function computeHmacSha256Hex(secret, signedPayload) {
34
+ const subtle = globalThis.crypto?.subtle;
35
+ if (!subtle) {
36
+ throw new Error("Web Crypto API is unavailable in this runtime.");
37
+ }
38
+ const key = await subtle.importKey(
39
+ "raw",
40
+ toArrayBuffer(encodeUtf8(secret)),
41
+ {
42
+ name: "HMAC",
43
+ hash: "SHA-256"
44
+ },
45
+ false,
46
+ ["sign"]
47
+ );
48
+ const signature = await subtle.sign("HMAC", key, toArrayBuffer(encodeUtf8(signedPayload)));
49
+ return bytesToHex(new Uint8Array(signature));
50
+ }
51
+ function requireWebhookRequestParts(event, providerName) {
52
+ if (!event.request || typeof event.request.headers !== "object" || event.request.headers === null) {
53
+ throw new Error(`${providerName} webhook request metadata is required.`);
54
+ }
55
+ if (typeof event.request.rawBody !== "string") {
56
+ throw new Error(`${providerName} webhook raw body is required.`);
57
+ }
58
+ return {
59
+ headers: event.request.headers,
60
+ rawBody: event.request.rawBody
61
+ };
62
+ }
63
+
64
+ // src/stripe.ts
65
+ var DEFAULT_TOLERANCE_SECONDS = 300;
66
+ var STRIPE_SIGNATURE_HEADER = "stripe-signature";
67
+ var StripeWebhookVerificationError = class extends Error {
68
+ name = "StripeWebhookVerificationError";
69
+ };
70
+ function createVerificationError(message) {
71
+ return new StripeWebhookVerificationError(message);
72
+ }
73
+ function parseStripeSignatureHeader(headerValue) {
74
+ let timestamp;
75
+ const signatures = [];
76
+ for (const part of headerValue.split(",")) {
77
+ const separatorIndex = part.indexOf("=");
78
+ if (separatorIndex <= 0) {
79
+ continue;
80
+ }
81
+ const key = part.slice(0, separatorIndex).trim();
82
+ const value = part.slice(separatorIndex + 1).trim();
83
+ if (key === "t") {
84
+ const parsedTimestamp = Number(value);
85
+ if (!Number.isInteger(parsedTimestamp)) {
86
+ return null;
87
+ }
88
+ timestamp = parsedTimestamp;
89
+ continue;
90
+ }
91
+ if (key === "v1" && isHexString(value)) {
92
+ signatures.push(value.toLowerCase());
93
+ }
94
+ }
95
+ if (timestamp === void 0 || signatures.length === 0) {
96
+ return null;
97
+ }
98
+ return { timestamp, signatures };
99
+ }
100
+ async function verifyStripeWebhook(event, options) {
101
+ if (!options.secret || options.secret.trim().length === 0) {
102
+ throw createVerificationError("A Stripe webhook secret is required.");
103
+ }
104
+ let requestParts;
105
+ try {
106
+ requestParts = requireWebhookRequestParts(event, "Stripe");
107
+ } catch (error) {
108
+ throw createVerificationError(error instanceof Error ? error.message : String(error));
109
+ }
110
+ const signatureHeader = getHeaderCaseInsensitive(requestParts.headers, STRIPE_SIGNATURE_HEADER);
111
+ if (!signatureHeader) {
112
+ throw createVerificationError("Stripe-Signature header is required.");
113
+ }
114
+ const parsedHeader = parseStripeSignatureHeader(signatureHeader);
115
+ if (!parsedHeader) {
116
+ throw createVerificationError("Stripe-Signature header is invalid.");
117
+ }
118
+ const toleranceSeconds = options.toleranceSeconds ?? DEFAULT_TOLERANCE_SECONDS;
119
+ if (toleranceSeconds !== 0) {
120
+ const currentTimestamp = Math.floor(Date.now() / 1e3);
121
+ if (Math.abs(currentTimestamp - parsedHeader.timestamp) > toleranceSeconds) {
122
+ throw createVerificationError("Stripe webhook timestamp is outside the allowed tolerance.");
123
+ }
124
+ }
125
+ const signedPayload = `${parsedHeader.timestamp}.${requestParts.rawBody}`;
126
+ let expectedSignature;
127
+ try {
128
+ expectedSignature = await computeHmacSha256Hex(options.secret, signedPayload);
129
+ } catch (error) {
130
+ throw createVerificationError(error instanceof Error ? error.message : String(error));
131
+ }
132
+ const matches = parsedHeader.signatures.some(
133
+ (signature) => timingSafeEqualStrings(signature, expectedSignature)
134
+ );
135
+ if (!matches) {
136
+ throw createVerificationError("Stripe webhook signature did not match.");
137
+ }
138
+ try {
139
+ return JSON.parse(requestParts.rawBody);
140
+ } catch {
141
+ throw createVerificationError("Stripe webhook body contained invalid JSON.");
142
+ }
143
+ }
144
+ export {
145
+ StripeWebhookVerificationError,
146
+ verifyStripeWebhook
147
+ };
@@ -0,0 +1,12 @@
1
+ import type { WebhookFlowEvent } from '@trigora/contracts';
2
+ export declare function encodeUtf8(value: string): Uint8Array;
3
+ export declare function toArrayBuffer(bytes: Uint8Array): ArrayBuffer;
4
+ export declare function bytesToHex(bytes: Uint8Array): string;
5
+ export declare function getHeaderCaseInsensitive(headers: Record<string, string>, headerName: string): string | undefined;
6
+ export declare function isHexString(value: string): boolean;
7
+ export declare function timingSafeEqualStrings(left: string, right: string): boolean;
8
+ export declare function computeHmacSha256Hex(secret: string, signedPayload: string): Promise<string>;
9
+ export declare function requireWebhookRequestParts(event: WebhookFlowEvent, providerName: string): {
10
+ headers: Record<string, string>;
11
+ rawBody: string;
12
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trigora/sdk",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/trigora-dev/trigora"
@@ -28,6 +28,14 @@
28
28
  ".": {
29
29
  "types": "./dist/index.d.ts",
30
30
  "default": "./dist/index.js"
31
+ },
32
+ "./github": {
33
+ "types": "./dist/github.d.ts",
34
+ "default": "./dist/github.js"
35
+ },
36
+ "./stripe": {
37
+ "types": "./dist/stripe.d.ts",
38
+ "default": "./dist/stripe.js"
31
39
  }
32
40
  },
33
41
  "files": [
@@ -37,7 +45,7 @@
37
45
  "access": "public"
38
46
  },
39
47
  "dependencies": {
40
- "@trigora/contracts": "0.3.0"
48
+ "@trigora/contracts": "0.5.5"
41
49
  },
42
50
  "devDependencies": {
43
51
  "tsup": "^8.5.1"