@base44-preview/platform 0.2.0-pr.335.2bd5f4f → 0.2.0-pr.337.2f0349e
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 +68 -35
- package/dist/react/chat-items.d.ts +6 -6
- package/dist/react/chat-items.js +79 -19
- package/dist/react/chat.types.d.ts +39 -4
- package/dist/react/index.d.ts +2 -6
- package/dist/react/index.js +1 -4
- package/dist/react/useBase44Chat.js +55 -15
- package/dist/server/errors.d.ts +53 -0
- package/dist/server/errors.js +56 -0
- package/dist/server/errors.types.d.ts +60 -0
- package/dist/server/index.d.ts +6 -2
- package/dist/server/index.js +2 -1
- package/dist/server/modules/webhooks-signature.d.ts +11 -0
- package/dist/server/modules/webhooks-signature.js +65 -0
- package/dist/server/modules/webhooks.d.ts +3 -0
- package/dist/server/modules/webhooks.events.generated.d.ts +235 -0
- package/dist/server/modules/webhooks.events.generated.js +4 -0
- package/dist/server/modules/webhooks.js +91 -0
- package/dist/server/modules/webhooks.types.d.ts +218 -0
- package/dist/server/modules/webhooks.types.js +1 -0
- package/dist/server/server.d.ts +28 -3
- package/dist/server/server.js +31 -7
- package/dist/server/server.types.d.ts +8 -8
- package/package.json +11 -5
- package/dist/react/Base44ChatProvider.d.ts +0 -43
- package/dist/react/Base44ChatProvider.js +0 -68
- package/dist/react/Message.d.ts +0 -39
- package/dist/react/Message.js +0 -55
- package/dist/react/components.types.d.ts +0 -63
- package/dist/react/context.d.ts +0 -50
- package/dist/react/context.js +0 -57
- package/dist/react/fallbacks.d.ts +0 -3
- package/dist/react/fallbacks.js +0 -13
- package/dist/server/server-url.d.ts +0 -2
- package/dist/server/server-url.js +0 -8
- /package/dist/{react/components.types.js → server/errors.types.js} +0 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error the SDK reports when a webhook delivery can't be trusted.
|
|
3
|
+
*
|
|
4
|
+
* You don't create it. The [`verifyEvent()`](/developers/references/platform-sdk/docs/interfaces/webhooks#verifyevent) method rejects with
|
|
5
|
+
* it, and [`createHandler()`](/developers/references/platform-sdk/docs/interfaces/webhooks#createhandler) answers it with a 400 and passes it
|
|
6
|
+
* to your `onError`. Check `code` to see what happened, then look the code up below.
|
|
7
|
+
*
|
|
8
|
+
* The error never carries the request body or its signature.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Refuse a delivery that doesn't verify
|
|
13
|
+
* try {
|
|
14
|
+
* const event = await platform.webhooks.verifyEvent(rawBody, request.headers, { publicKeys });
|
|
15
|
+
* } catch (error) {
|
|
16
|
+
* if (error instanceof WebhookVerificationError) {
|
|
17
|
+
* return Response.json({ error: error.code }, { status: 400 });
|
|
18
|
+
* }
|
|
19
|
+
* throw error;
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export class WebhookVerificationError extends Error {
|
|
24
|
+
/** @internal */
|
|
25
|
+
constructor(code) {
|
|
26
|
+
super(`Webhook verification failed: ${code}`);
|
|
27
|
+
this.name = "WebhookVerificationError";
|
|
28
|
+
this.code = code;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The error the SDK reports when the public keys you passed can't be used, so no delivery can be verified.
|
|
33
|
+
*
|
|
34
|
+
* You don't create it. The [`verifyEvent()`](/developers/references/platform-sdk/docs/interfaces/webhooks#verifyevent) method rejects with
|
|
35
|
+
* it, and [`createHandler()`](/developers/references/platform-sdk/docs/interfaces/webhooks#createhandler) answers it with a 500 and passes it
|
|
36
|
+
* to your `onError`. Base44 retries the delivery, and the retries succeed once you fix the problem.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // Log a misconfigured deployment, and let it answer 500
|
|
41
|
+
* export const POST = platform.webhooks.createHandler({
|
|
42
|
+
* publicKeys: process.env.BASE44_WEBHOOK_PUBLIC_KEYS,
|
|
43
|
+
* onError(error) {
|
|
44
|
+
* if (error instanceof WebhookConfigurationError) console.error('Check BASE44_WEBHOOK_PUBLIC_KEYS', error.code);
|
|
45
|
+
* },
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export class WebhookConfigurationError extends Error {
|
|
50
|
+
/** @internal */
|
|
51
|
+
constructor(code, detail) {
|
|
52
|
+
super(`Webhook configuration: ${detail}`);
|
|
53
|
+
this.name = "WebhookConfigurationError";
|
|
54
|
+
this.code = code;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codes of errors for a delivery that can't be trusted. Answer these with a 400. Base44 retries any
|
|
3
|
+
* answer that isn't 2xx, so a genuine delivery that failed because of a passing problem verifies on the retry.
|
|
4
|
+
*/
|
|
5
|
+
export type WebhookVerificationErrorCode =
|
|
6
|
+
/**
|
|
7
|
+
* The request is missing one of the `webhook-id`, `webhook-timestamp` or `webhook-signature` headers.
|
|
8
|
+
*
|
|
9
|
+
* Check that a proxy in front of your endpoint forwards all request headers.
|
|
10
|
+
*/
|
|
11
|
+
"missing_headers"
|
|
12
|
+
/**
|
|
13
|
+
* The `webhook-timestamp` header is more than `toleranceSeconds` (300 by default) away from your server's clock, or isn't a number.
|
|
14
|
+
*
|
|
15
|
+
* Check your server's clock. A replayed old delivery fails this way too.
|
|
16
|
+
*/
|
|
17
|
+
| "timestamp_outside_tolerance"
|
|
18
|
+
/**
|
|
19
|
+
* The `webhook-signature` header has no signature in the `v1a` scheme Base44 uses.
|
|
20
|
+
*
|
|
21
|
+
* The request didn't come from Base44, or a proxy rewrote the header.
|
|
22
|
+
*/
|
|
23
|
+
| "no_signature_entries"
|
|
24
|
+
/**
|
|
25
|
+
* No signature matches any of your public keys.
|
|
26
|
+
*
|
|
27
|
+
* Check that you pass the request body exactly as received. Parsing the JSON and serializing it again
|
|
28
|
+
* changes the bytes. Also check that your public keys belong to the workspace that sends the webhook.
|
|
29
|
+
*/
|
|
30
|
+
| "signature_mismatch"
|
|
31
|
+
/**
|
|
32
|
+
* The signature is valid, but the body isn't a JSON event.
|
|
33
|
+
*
|
|
34
|
+
* This shouldn't happen with deliveries from Base44.
|
|
35
|
+
*/
|
|
36
|
+
| "invalid_payload";
|
|
37
|
+
/**
|
|
38
|
+
* Codes of errors that stop verification before it starts, because the public keys you passed can't
|
|
39
|
+
* be used. They're about your deployment, not the request, so answer them with a 500. Base44 retries
|
|
40
|
+
* the delivery, as it does for any answer that isn't 2xx, and it succeeds once you fix the problem.
|
|
41
|
+
*/
|
|
42
|
+
export type WebhookConfigurationErrorCode =
|
|
43
|
+
/**
|
|
44
|
+
* No public keys were passed, for example because an environment variable is unset.
|
|
45
|
+
*
|
|
46
|
+
* Pass your workspace's public keys to `verifyEvent()` or `createHandler()`.
|
|
47
|
+
*/
|
|
48
|
+
"missing_public_keys"
|
|
49
|
+
/**
|
|
50
|
+
* A public key isn't a base64-encoded 32-byte Ed25519 key, with or without the `whpk_` prefix.
|
|
51
|
+
*
|
|
52
|
+
* Copy the key again. A truncated key fails this way.
|
|
53
|
+
*/
|
|
54
|
+
| "invalid_public_key"
|
|
55
|
+
/**
|
|
56
|
+
* The runtime can't import Ed25519 keys with WebCrypto.
|
|
57
|
+
*
|
|
58
|
+
* Run your server on Node.js 20 or later, or on an edge runtime that supports Ed25519 in WebCrypto.
|
|
59
|
+
*/
|
|
60
|
+
| "unsupported_runtime";
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
/** Server platform modules,
|
|
1
|
+
/** Server platform modules, such as webhook verification. */
|
|
2
2
|
export { createPlatformServer } from "./server.js";
|
|
3
|
-
export
|
|
3
|
+
export { WebhookConfigurationError, WebhookVerificationError } from "./errors.js";
|
|
4
|
+
export type { WebhookConfigurationErrorCode, WebhookVerificationErrorCode } from "./errors.types.js";
|
|
5
|
+
export type { PlatformServer } from "./server.types.js";
|
|
6
|
+
export type { CloudEvent, CreateHandlerOptions, DeliveredWebhookEvent, UnknownWebhookEvent, VerifyEventOptions, WebhookEventHandlers, WebhookEvent, WebhookEventType, WebhookHeaders, WebhookPublicKeys, WebhooksModule, } from "./modules/webhooks.types.js";
|
|
7
|
+
export type * from "./modules/webhooks.events.generated.js";
|
package/dist/server/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
/** Server platform modules,
|
|
1
|
+
/** Server platform modules, such as webhook verification. */
|
|
2
2
|
export { createPlatformServer } from "./server.js";
|
|
3
|
+
export { WebhookConfigurationError, WebhookVerificationError } from "./errors.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { WebhookHeaders, WebhookPublicKeys } from "./webhooks.types.js";
|
|
2
|
+
/** Split the configured keys into raw 32-byte Ed25519 keys. A truncated paste throws: a wrong key reads as every event being forged. */
|
|
3
|
+
export declare function parsePublicKeys(keys: WebhookPublicKeys | undefined): Uint8Array[];
|
|
4
|
+
export declare function importPublicKey(raw: Uint8Array): Promise<CryptoKey>;
|
|
5
|
+
/** Read one header from a `Headers` or a Node-style object. */
|
|
6
|
+
export declare function readHeader(headers: WebhookHeaders, name: string, separator?: string): string | null;
|
|
7
|
+
/** The `v1a,<base64>` entries of a `webhook-signature` header, decoded. */
|
|
8
|
+
export declare function parseSignatures(header: string): Uint8Array[];
|
|
9
|
+
/** The signed bytes: `<id>.<timestamp>.<raw body>`. */
|
|
10
|
+
export declare function signedPayload(id: string, timestamp: string, body: string | Uint8Array): Uint8Array;
|
|
11
|
+
export declare function anySignatureVerifies(keys: CryptoKey[], signatures: Uint8Array[], payload: Uint8Array): Promise<boolean>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { WebhookConfigurationError } from "../errors.js";
|
|
2
|
+
const SIGNATURE_SCHEME = "v1a";
|
|
3
|
+
const encoder = new TextEncoder();
|
|
4
|
+
/** Standard base64 to bytes, or `null` when it is not valid base64. */
|
|
5
|
+
function decodeBase64(value) {
|
|
6
|
+
try {
|
|
7
|
+
const binary = atob(value);
|
|
8
|
+
return Uint8Array.from(binary, char => char.charCodeAt(0));
|
|
9
|
+
}
|
|
10
|
+
catch (_a) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** Split the configured keys into raw 32-byte Ed25519 keys. A truncated paste throws: a wrong key reads as every event being forged. */
|
|
15
|
+
export function parsePublicKeys(keys) {
|
|
16
|
+
const entries = (Array.isArray(keys) ? keys : (keys !== null && keys !== void 0 ? keys : "").split(/[\s,]+/)).map(key => key.trim()).filter(Boolean);
|
|
17
|
+
if (entries.length === 0)
|
|
18
|
+
throw new WebhookConfigurationError("missing_public_keys", "no public keys were provided");
|
|
19
|
+
return entries.map(entry => {
|
|
20
|
+
const raw = decodeBase64(entry.replace(/^whpk_/, ""));
|
|
21
|
+
if (!raw || raw.length !== 32) {
|
|
22
|
+
throw new WebhookConfigurationError("invalid_public_key", "a public key is not a base64-encoded 32-byte Ed25519 key");
|
|
23
|
+
}
|
|
24
|
+
return raw;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
export function importPublicKey(raw) {
|
|
28
|
+
return crypto.subtle.importKey("raw", raw, "Ed25519", false, ["verify"]);
|
|
29
|
+
}
|
|
30
|
+
/** Read one header from a `Headers` or a Node-style object. */
|
|
31
|
+
export function readHeader(headers, name, separator = ", ") {
|
|
32
|
+
if (typeof headers.get === "function")
|
|
33
|
+
return headers.get(name);
|
|
34
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
35
|
+
if (key.toLowerCase() === name && value !== undefined)
|
|
36
|
+
return Array.isArray(value) ? value.join(separator) : value;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
/** The `v1a,<base64>` entries of a `webhook-signature` header, decoded. */
|
|
41
|
+
export function parseSignatures(header) {
|
|
42
|
+
return header
|
|
43
|
+
.split(" ")
|
|
44
|
+
.filter(entry => entry.startsWith(`${SIGNATURE_SCHEME},`))
|
|
45
|
+
.map(entry => decodeBase64(entry.slice(SIGNATURE_SCHEME.length + 1)))
|
|
46
|
+
.filter((signature) => signature !== null);
|
|
47
|
+
}
|
|
48
|
+
/** The signed bytes: `<id>.<timestamp>.<raw body>`. */
|
|
49
|
+
export function signedPayload(id, timestamp, body) {
|
|
50
|
+
const prefix = encoder.encode(`${id}.${timestamp}.`);
|
|
51
|
+
const bytes = typeof body === "string" ? encoder.encode(body) : body;
|
|
52
|
+
const payload = new Uint8Array(prefix.length + bytes.length);
|
|
53
|
+
payload.set(prefix);
|
|
54
|
+
payload.set(bytes, prefix.length);
|
|
55
|
+
return payload;
|
|
56
|
+
}
|
|
57
|
+
export async function anySignatureVerifies(keys, signatures, payload) {
|
|
58
|
+
for (const key of keys) {
|
|
59
|
+
for (const signature of signatures) {
|
|
60
|
+
if (await crypto.subtle.verify("Ed25519", key, signature, payload))
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `data` object of `app.created.v1`. An app was created.
|
|
3
|
+
*/
|
|
4
|
+
export interface AppCreatedV1Data {
|
|
5
|
+
/**
|
|
6
|
+
* The app the event is about.
|
|
7
|
+
*/
|
|
8
|
+
app_id: string;
|
|
9
|
+
/**
|
|
10
|
+
* The workspace the app belongs to.
|
|
11
|
+
*/
|
|
12
|
+
workspace_id: string;
|
|
13
|
+
/**
|
|
14
|
+
* Opaque platform id of the app's current owner.
|
|
15
|
+
*/
|
|
16
|
+
owner_id: string;
|
|
17
|
+
/**
|
|
18
|
+
* The kind of app. New values arrive as a new event version.
|
|
19
|
+
*/
|
|
20
|
+
app_type: "imported_app" | "user_agent" | "user_app" | "user_game";
|
|
21
|
+
/**
|
|
22
|
+
* When the app was created (UTC).
|
|
23
|
+
*/
|
|
24
|
+
created_at: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The `data` object of `app.published.v1`. An app version was published.
|
|
28
|
+
*/
|
|
29
|
+
export interface AppPublishedV1Data {
|
|
30
|
+
/**
|
|
31
|
+
* The app the event is about.
|
|
32
|
+
*/
|
|
33
|
+
app_id: string;
|
|
34
|
+
/**
|
|
35
|
+
* The workspace the app belongs to.
|
|
36
|
+
*/
|
|
37
|
+
workspace_id: string;
|
|
38
|
+
/**
|
|
39
|
+
* Opaque platform id of the app's current owner.
|
|
40
|
+
*/
|
|
41
|
+
owner_id: string;
|
|
42
|
+
/**
|
|
43
|
+
* Id of the version now live.
|
|
44
|
+
*/
|
|
45
|
+
published_version: string;
|
|
46
|
+
/**
|
|
47
|
+
* When the version went live (UTC).
|
|
48
|
+
*/
|
|
49
|
+
published_at: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The `data` object of `app.unpublished.v1`. An app was taken offline.
|
|
53
|
+
*/
|
|
54
|
+
export interface AppUnpublishedV1Data {
|
|
55
|
+
/**
|
|
56
|
+
* The app the event is about.
|
|
57
|
+
*/
|
|
58
|
+
app_id: string;
|
|
59
|
+
/**
|
|
60
|
+
* The workspace the app belongs to.
|
|
61
|
+
*/
|
|
62
|
+
workspace_id: string;
|
|
63
|
+
/**
|
|
64
|
+
* Opaque platform id of the app's current owner.
|
|
65
|
+
*/
|
|
66
|
+
owner_id: string;
|
|
67
|
+
/**
|
|
68
|
+
* When the app was taken offline (UTC).
|
|
69
|
+
*/
|
|
70
|
+
unpublished_at: string;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Tombstone identifiers only.
|
|
74
|
+
*
|
|
75
|
+
* Deletion is **not** erasure: the app moves to trash and is self-serve
|
|
76
|
+
* restorable for 30 days, with unbounded restore through support. A receiver
|
|
77
|
+
* that purges on this event will be wrong for every restored app, which is why
|
|
78
|
+
* ``app.restored.v1`` exists and why this is documented as a trash signal.
|
|
79
|
+
*/
|
|
80
|
+
export interface AppDeletedV1Data {
|
|
81
|
+
/**
|
|
82
|
+
* The app the event is about.
|
|
83
|
+
*/
|
|
84
|
+
app_id: string;
|
|
85
|
+
/**
|
|
86
|
+
* The workspace the app belongs to.
|
|
87
|
+
*/
|
|
88
|
+
workspace_id: string;
|
|
89
|
+
/**
|
|
90
|
+
* Opaque platform id of the app's current owner.
|
|
91
|
+
*/
|
|
92
|
+
owner_id: string;
|
|
93
|
+
/**
|
|
94
|
+
* When the app was moved to trash (UTC). It can still be restored.
|
|
95
|
+
*/
|
|
96
|
+
deleted_at: string;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* The `data` object of `app.restored.v1`. An app was restored from trash.
|
|
100
|
+
*/
|
|
101
|
+
export interface AppRestoredV1Data {
|
|
102
|
+
/**
|
|
103
|
+
* The app the event is about.
|
|
104
|
+
*/
|
|
105
|
+
app_id: string;
|
|
106
|
+
/**
|
|
107
|
+
* The workspace the app belongs to.
|
|
108
|
+
*/
|
|
109
|
+
workspace_id: string;
|
|
110
|
+
/**
|
|
111
|
+
* Opaque platform id of the app's current owner.
|
|
112
|
+
*/
|
|
113
|
+
owner_id: string;
|
|
114
|
+
/**
|
|
115
|
+
* When the app was restored (UTC).
|
|
116
|
+
*/
|
|
117
|
+
restored_at: string;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Carries no domain data — its only job is to be verifiable and echoed.
|
|
121
|
+
*/
|
|
122
|
+
export interface WebhookTestV1Data {
|
|
123
|
+
/**
|
|
124
|
+
* The endpoint being activated.
|
|
125
|
+
*/
|
|
126
|
+
endpoint_id: string;
|
|
127
|
+
/**
|
|
128
|
+
* The workspace the endpoint belongs to.
|
|
129
|
+
*/
|
|
130
|
+
workspace_id: string;
|
|
131
|
+
/**
|
|
132
|
+
* When the test was sent (UTC).
|
|
133
|
+
*/
|
|
134
|
+
sent_at: string;
|
|
135
|
+
/**
|
|
136
|
+
* Return this value in your response body to activate the endpoint.
|
|
137
|
+
*/
|
|
138
|
+
challenge: string;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Sent when the builder picks the message up, not when it is received: a
|
|
142
|
+
* message sent while another turn runs waits in the queue until then.
|
|
143
|
+
*/
|
|
144
|
+
export interface BuilderTurnStartedV1Data {
|
|
145
|
+
/**
|
|
146
|
+
* The app the event is about.
|
|
147
|
+
*/
|
|
148
|
+
app_id: string;
|
|
149
|
+
/**
|
|
150
|
+
* The workspace the app belongs to.
|
|
151
|
+
*/
|
|
152
|
+
workspace_id: string;
|
|
153
|
+
/**
|
|
154
|
+
* Opaque platform id of the app's current owner.
|
|
155
|
+
*/
|
|
156
|
+
owner_id: string;
|
|
157
|
+
/**
|
|
158
|
+
* The message that started the turn: the id returned when you sent it, and the app's `status.turn_id` while the turn runs.
|
|
159
|
+
*/
|
|
160
|
+
message_id: string;
|
|
161
|
+
/**
|
|
162
|
+
* The conversation the message belongs to.
|
|
163
|
+
*/
|
|
164
|
+
conversation_id?: string;
|
|
165
|
+
/**
|
|
166
|
+
* When the builder started working on the message (UTC).
|
|
167
|
+
*/
|
|
168
|
+
started_at: string;
|
|
169
|
+
/**
|
|
170
|
+
* Set when this turn continues one that was interrupted: the `message_id` of that turn. Follow it back to add up the time the whole response took.
|
|
171
|
+
*/
|
|
172
|
+
continues_message_id?: string;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The `data` object of `builder.turn_ended.v1`. The builder finished responding to a message.
|
|
176
|
+
*/
|
|
177
|
+
export interface BuilderTurnEndedV1Data {
|
|
178
|
+
/**
|
|
179
|
+
* The app the event is about.
|
|
180
|
+
*/
|
|
181
|
+
app_id: string;
|
|
182
|
+
/**
|
|
183
|
+
* The workspace the app belongs to.
|
|
184
|
+
*/
|
|
185
|
+
workspace_id: string;
|
|
186
|
+
/**
|
|
187
|
+
* Opaque platform id of the app's current owner.
|
|
188
|
+
*/
|
|
189
|
+
owner_id: string;
|
|
190
|
+
/**
|
|
191
|
+
* The message that started the turn: the id returned when you sent it, and the app's `status.turn_id` while the turn runs.
|
|
192
|
+
*/
|
|
193
|
+
message_id: string;
|
|
194
|
+
/**
|
|
195
|
+
* The conversation the message belongs to.
|
|
196
|
+
*/
|
|
197
|
+
conversation_id?: string;
|
|
198
|
+
/**
|
|
199
|
+
* When the builder started working on the message (UTC).
|
|
200
|
+
*/
|
|
201
|
+
started_at: string;
|
|
202
|
+
/**
|
|
203
|
+
* Set when this turn continues one that was interrupted: the `message_id` of that turn. Follow it back to add up the time the whole response took.
|
|
204
|
+
*/
|
|
205
|
+
continues_message_id?: string;
|
|
206
|
+
/**
|
|
207
|
+
* How the turn ended. `finished`: the builder replied and has nothing pending. `waiting_for_user_input`: the builder stopped to ask something, such as a clarifying question or a plan to approve, and continues when the next message answers it. `failed`: the turn hit an error. `cancelled`: the turn was stopped before it ended. `interrupted`: the builder was interrupted, for example by a restart, and continues the work in a new turn whose `continues_message_id` is this message.
|
|
208
|
+
*/
|
|
209
|
+
status: "finished" | "waiting_for_user_input" | "failed" | "cancelled" | "interrupted";
|
|
210
|
+
/**
|
|
211
|
+
* When the turn ended (UTC).
|
|
212
|
+
*/
|
|
213
|
+
ended_at: string;
|
|
214
|
+
}
|
|
215
|
+
/** Every event type Base44 sends, mapped to the shape of its `data` object. */
|
|
216
|
+
export interface WebhookEventMap {
|
|
217
|
+
/** An app was created. */
|
|
218
|
+
"app.created.v1": AppCreatedV1Data;
|
|
219
|
+
/** An app version was published. */
|
|
220
|
+
"app.published.v1": AppPublishedV1Data;
|
|
221
|
+
/** An app was taken offline. */
|
|
222
|
+
"app.unpublished.v1": AppUnpublishedV1Data;
|
|
223
|
+
/** Tombstone identifiers only. */
|
|
224
|
+
"app.deleted.v1": AppDeletedV1Data;
|
|
225
|
+
/** An app was restored from trash. */
|
|
226
|
+
"app.restored.v1": AppRestoredV1Data;
|
|
227
|
+
/** Carries no domain data — its only job is to be verifiable and echoed. */
|
|
228
|
+
"webhook.test.v1": WebhookTestV1Data;
|
|
229
|
+
/** Sent when the builder picks the message up, not when it is received: a */
|
|
230
|
+
"builder.turn_started.v1": BuilderTurnStartedV1Data;
|
|
231
|
+
/** The builder finished responding to a message. */
|
|
232
|
+
"builder.turn_ended.v1": BuilderTurnEndedV1Data;
|
|
233
|
+
}
|
|
234
|
+
/** Every event type in {@link WebhookEventMap}, so events from a newer Base44 can be told apart at runtime. @internal */
|
|
235
|
+
export declare const WEBHOOK_EVENT_TYPES: readonly ["app.created.v1", "app.published.v1", "app.unpublished.v1", "app.deleted.v1", "app.restored.v1", "webhook.test.v1", "builder.turn_started.v1", "builder.turn_ended.v1"];
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// Generated by scripts/generate-webhook-types.mjs from the schemas published at
|
|
2
|
+
// https://base44.com/webhooks/schemas. Do not edit by hand; run `npm run generate:webhooks`.
|
|
3
|
+
/** Every event type in {@link WebhookEventMap}, so events from a newer Base44 can be told apart at runtime. @internal */
|
|
4
|
+
export const WEBHOOK_EVENT_TYPES = ["app.created.v1", "app.published.v1", "app.unpublished.v1", "app.deleted.v1", "app.restored.v1", "webhook.test.v1", "builder.turn_started.v1", "builder.turn_ended.v1"];
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { WebhookConfigurationError, WebhookVerificationError } from "../errors.js";
|
|
2
|
+
import { anySignatureVerifies, importPublicKey, parsePublicKeys, parseSignatures, readHeader, signedPayload } from "./webhooks-signature.js";
|
|
3
|
+
import { WEBHOOK_EVENT_TYPES } from "./webhooks.events.generated.js";
|
|
4
|
+
const knownTypes = new Set(WEBHOOK_EVENT_TYPES);
|
|
5
|
+
const DEFAULT_TOLERANCE_SECONDS = 300;
|
|
6
|
+
/** Webhook receiving utilities. Verification is local: no network calls, no timers. */
|
|
7
|
+
export function createWebhooks() {
|
|
8
|
+
// Imported keys are cached by their wire form so a pinned key is imported once.
|
|
9
|
+
const imported = new Map();
|
|
10
|
+
const keysFor = (publicKeys) => Promise.all(parsePublicKeys(publicKeys).map(raw => {
|
|
11
|
+
const id = raw.join(",");
|
|
12
|
+
if (!imported.has(id)) {
|
|
13
|
+
imported.set(id, importPublicKey(raw).catch(() => {
|
|
14
|
+
// Evicted so a later call retries the import instead of replaying this failure.
|
|
15
|
+
imported.delete(id);
|
|
16
|
+
throw new WebhookConfigurationError("unsupported_runtime", "this runtime can't import Ed25519 keys with WebCrypto");
|
|
17
|
+
}));
|
|
18
|
+
}
|
|
19
|
+
return imported.get(id);
|
|
20
|
+
}));
|
|
21
|
+
const verifyEvent = async (body, headers, verifyOptions) => {
|
|
22
|
+
var _a;
|
|
23
|
+
// Configuration problems surface first, whatever the request looks like: a missing key is ours to fix.
|
|
24
|
+
const keys = await keysFor(verifyOptions === null || verifyOptions === void 0 ? void 0 : verifyOptions.publicKeys);
|
|
25
|
+
const id = readHeader(headers, "webhook-id");
|
|
26
|
+
const timestamp = readHeader(headers, "webhook-timestamp");
|
|
27
|
+
const signature = readHeader(headers, "webhook-signature", " ");
|
|
28
|
+
if (!id || !timestamp || !signature)
|
|
29
|
+
throw new WebhookVerificationError("missing_headers");
|
|
30
|
+
// Negated so a non-numeric timestamp (NaN) is refused too.
|
|
31
|
+
if (!(Math.abs(Date.now() / 1000 - Number(timestamp)) <= ((_a = verifyOptions === null || verifyOptions === void 0 ? void 0 : verifyOptions.toleranceSeconds) !== null && _a !== void 0 ? _a : DEFAULT_TOLERANCE_SECONDS))) {
|
|
32
|
+
throw new WebhookVerificationError("timestamp_outside_tolerance");
|
|
33
|
+
}
|
|
34
|
+
const signatures = parseSignatures(signature);
|
|
35
|
+
if (signatures.length === 0)
|
|
36
|
+
throw new WebhookVerificationError("no_signature_entries");
|
|
37
|
+
if (!(await anySignatureVerifies(keys, signatures, signedPayload(id, timestamp, body)))) {
|
|
38
|
+
throw new WebhookVerificationError("signature_mismatch");
|
|
39
|
+
}
|
|
40
|
+
// Only now is the body trusted enough to parse.
|
|
41
|
+
let event;
|
|
42
|
+
try {
|
|
43
|
+
event = JSON.parse(typeof body === "string" ? body : new TextDecoder().decode(body));
|
|
44
|
+
}
|
|
45
|
+
catch (_b) {
|
|
46
|
+
throw new WebhookVerificationError("invalid_payload");
|
|
47
|
+
}
|
|
48
|
+
const { type, id: eventId, data } = (event !== null && event !== void 0 ? event : {});
|
|
49
|
+
if (typeof type !== "string" || typeof eventId !== "string" || typeof data !== "object" || data === null) {
|
|
50
|
+
throw new WebhookVerificationError("invalid_payload");
|
|
51
|
+
}
|
|
52
|
+
// A type from a newer Base44 gets the client's unknown-event shape, so known types keep narrowing.
|
|
53
|
+
return (knownTypes.has(type) ? event : { ...event, type: "unknown", name: type });
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
verifyEvent,
|
|
57
|
+
createHandler({ on = {}, onEvent, onSuccess, onError, ...verifyOptions }) {
|
|
58
|
+
const fail = async (error, status, label, event) => { var _a; return (_a = (await (onError === null || onError === void 0 ? void 0 : onError(error, event)))) !== null && _a !== void 0 ? _a : Response.json({ error: label }, { status }); };
|
|
59
|
+
return async (request) => {
|
|
60
|
+
var _a, _b;
|
|
61
|
+
let body;
|
|
62
|
+
try {
|
|
63
|
+
body = new Uint8Array(await request.arrayBuffer());
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
return fail(error, 500, "request_failed");
|
|
67
|
+
}
|
|
68
|
+
let event;
|
|
69
|
+
try {
|
|
70
|
+
event = await verifyEvent(body, request.headers, verifyOptions);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (error instanceof WebhookVerificationError)
|
|
74
|
+
return fail(error, 400, error.code);
|
|
75
|
+
return fail(error, 500, error instanceof WebhookConfigurationError ? "receiver_misconfigured" : "request_failed");
|
|
76
|
+
}
|
|
77
|
+
// Activation probe: echo the challenge, only after the signature verified. Never reaches your callbacks.
|
|
78
|
+
if (event.type === "webhook.test.v1")
|
|
79
|
+
return Response.json({ challenge: event.data.challenge });
|
|
80
|
+
try {
|
|
81
|
+
await (onEvent === null || onEvent === void 0 ? void 0 : onEvent(event));
|
|
82
|
+
await ((_a = on[event.type]) === null || _a === void 0 ? void 0 : _a.call(on, event));
|
|
83
|
+
return (_b = (await (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(event)))) !== null && _b !== void 0 ? _b : new Response(null, { status: 204 });
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
return fail(error, 500, "handler_failed", event);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|