@base44-preview/platform 0.2.0-pr.335.2bd5f4f → 0.2.0-pr.336.059c4cd
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 -62
- 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 +176 -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/server.d.ts +28 -3
- package/dist/server/server.js +31 -7
- package/dist/server/server.types.d.ts +8 -8
- package/package.json +14 -28
- 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/chat-items.d.ts +0 -51
- package/dist/react/chat-items.js +0 -174
- package/dist/react/chat.types.d.ts +0 -299
- 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/react/index.d.ts +0 -8
- package/dist/react/index.js +0 -5
- package/dist/react/useBase44Chat.d.ts +0 -48
- package/dist/react/useBase44Chat.js +0 -312
- package/dist/server/server-url.d.ts +0 -2
- package/dist/server/server-url.js +0 -8
- /package/dist/{react/chat.types.js → server/errors.types.js} +0 -0
- /package/dist/{react/components.types.js → server/modules/webhooks.types.js} +0 -0
package/README.md
CHANGED
|
@@ -4,10 +4,15 @@
|
|
|
4
4
|
npm install @base44/platform
|
|
5
5
|
```
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
The Platform SDK for white-label platforms built on Base44. It is independent of the app SDK
|
|
8
|
+
(`@base44/sdk`) and has two entrypoints:
|
|
9
|
+
|
|
10
|
+
- `@base44/platform`, for the browser: implements the read-only white-label builder socket
|
|
11
|
+
contract. A partner's browser watches the builder chat of up to eight apps over the platform's
|
|
12
|
+
shared socket. The workspace must have white-label sockets enabled; this SDK does not open sessions.
|
|
13
|
+
- `@base44/platform/server`, for your backend: verify the webhooks Base44 sends when apps are
|
|
14
|
+
created, published, unpublished, trashed or restored. See
|
|
15
|
+
[Receiving webhooks](#receiving-webhooks-server).
|
|
11
16
|
|
|
12
17
|
```ts
|
|
13
18
|
import { createPlatformClient } from "@base44/platform";
|
|
@@ -33,6 +38,63 @@ subscription.unsubscribe();
|
|
|
33
38
|
builder.close();
|
|
34
39
|
```
|
|
35
40
|
|
|
41
|
+
## Receiving webhooks (server)
|
|
42
|
+
|
|
43
|
+
`@base44/platform/server` verifies Base44 outbound webhooks and answers the endpoint
|
|
44
|
+
activation challenge. It uses WebCrypto only (Node 20+, Deno, Bun, Workers, edge runtimes) and
|
|
45
|
+
makes no network calls: pass your workspace's public keys (`whpk_…`) to each call that verifies.
|
|
46
|
+
|
|
47
|
+
The simplest route is `createHandler`. It reads the raw body, verifies it, answers the
|
|
48
|
+
activation challenge and picks the status code: 400 for a delivery that fails verification,
|
|
49
|
+
500 when your keys are misconfigured, the body can't be read or a callback throws, and 204
|
|
50
|
+
otherwise, including for event types you have no handler for. Base44 retries any answer that
|
|
51
|
+
isn't 2xx; the status only shows in its delivery log which side failed.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { createPlatformServer } from "@base44/platform/server";
|
|
55
|
+
|
|
56
|
+
const platform = createPlatformServer();
|
|
57
|
+
const publicKeys = process.env.BASE44_WEBHOOK_PUBLIC_KEYS!; // two keys during a rotation
|
|
58
|
+
|
|
59
|
+
export const POST = platform.webhooks.createHandler({
|
|
60
|
+
publicKeys,
|
|
61
|
+
on: {
|
|
62
|
+
"app.deleted.v1": async event => forget(event.data.app_id), // event is narrowed per type
|
|
63
|
+
},
|
|
64
|
+
onEvent: event => log(event.type), // every event, before its handler
|
|
65
|
+
onSuccess: event => {}, // return a Response to replace the 204
|
|
66
|
+
onError: (error, event) => report(error), // return a Response to replace the 400/500
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
For full control, verify and dispatch yourself:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
export async function POST(request: Request) {
|
|
74
|
+
const rawBody = await request.text(); // exactly as received
|
|
75
|
+
let event;
|
|
76
|
+
try {
|
|
77
|
+
event = await platform.webhooks.verifyEvent(rawBody, request.headers, { publicKeys });
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (error instanceof WebhookVerificationError) return new Response(null, { status: 400 });
|
|
80
|
+
throw error; // missing/malformed keys: a 500. Base44 retries any non-2xx answer
|
|
81
|
+
}
|
|
82
|
+
switch (event.type) {
|
|
83
|
+
case "webhook.test.v1": return Response.json({ challenge: event.data.challenge }); // activates the endpoint
|
|
84
|
+
case "app.deleted.v1": await forget(event.data.app_id); break;
|
|
85
|
+
}
|
|
86
|
+
return new Response(null, { status: 204 });
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
An event type added to Base44 after your SDK version arrives as `{ type: "unknown", name, data }`,
|
|
91
|
+
with its real type in `name` and an unchecked `data`, the same shape the client uses. After you
|
|
92
|
+
upgrade, it arrives under its own `type`.
|
|
93
|
+
|
|
94
|
+
Delivery is at least once with no ordering guarantee: deduplicate on `event.id`, and answer
|
|
95
|
+
2xx to event types you do not handle. Event types are generated from the schemas published at
|
|
96
|
+
`https://base44.com/webhooks/schemas`; run `npm run generate:webhooks` to refresh them.
|
|
97
|
+
|
|
36
98
|
## Sessions
|
|
37
99
|
|
|
38
100
|
`/api/builder-socket-session` is your backend's route, not an SDK endpoint. It opens a
|
|
@@ -147,7 +209,8 @@ Every exported shape and field has JSDoc. `npm run docs` validates it into `docs
|
|
|
147
209
|
`npm run create-docs` runs the app SDK's TypeDoc and Mintlify pipeline for this package,
|
|
148
210
|
and `npm run copy-docs-local -- --target <mintlify-docs>` copies the result into
|
|
149
211
|
`developers/references/platform-sdk/docs`. The pipeline's config for this package lives
|
|
150
|
-
in `scripts/mintlify-post-processing
|
|
212
|
+
in `scripts/mintlify-post-processing/`, including `reference.ts`, the docs-only entry that combines
|
|
213
|
+
the client and server entrypoints into one reference. The Socket messages page is built in mintlify-docs from
|
|
151
214
|
the service's AsyncAPI document, not here.
|
|
152
215
|
|
|
153
216
|
`PlatformSocketError` extends `Error` with `code` and optional `appId`. Original
|
|
@@ -170,60 +233,3 @@ exceptions, payloads and credentials are never attached.
|
|
|
170
233
|
|
|
171
234
|
No SDK dependency, package version or lockfile changes are required. Socket.IO
|
|
172
235
|
remains the existing SDK dependency. Chat commands and HTTP APIs are outside this entry point.
|
|
173
|
-
|
|
174
|
-
## React
|
|
175
|
-
|
|
176
|
-
`@base44/platform/react` adds one hook, `useBase44Chat`, that runs a builder chat for one
|
|
177
|
-
app on top of this client: it opens the live session and renews it, turns messages into items
|
|
178
|
-
made for rendering, binds every question the builder asks to the step it belongs to. It renders nothing. React is an optional peer dependency, loaded only by this
|
|
179
|
-
entry; the root entry stays framework-free.
|
|
180
|
-
|
|
181
|
-
```tsx
|
|
182
|
-
import { useBase44Chat } from "@base44/platform/react";
|
|
183
|
-
|
|
184
|
-
const chat = useBase44Chat({ appId, backend, onAppCreated });
|
|
185
|
-
chat.items.map((item) =>
|
|
186
|
-
item.steps.map((step) => step.question?.kind === "approval" && <button onClick={step.question.approve}>Allow</button>),
|
|
187
|
-
);
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
`backend` is four functions your backend provides, in any language behind them: `createApp`,
|
|
191
|
-
`openLiveSession`, `sendMessage` and `submitToolCallInput`, each wrapping one Base44 REST
|
|
192
|
-
call with your credentials, so the browser never holds a key. The hook returns `items`, a
|
|
193
|
-
`status` (`ready`, `submitted`, `building`, `waiting`, `error`), a `connection` (`connecting`,
|
|
194
|
-
`connected`, `disconnected`), an `error`, and the actions `send`, `create` and `clearError`. `send` needs an app; `create` makes
|
|
195
|
-
the first, and the chat watches it while `appId` is `null`. A sent prompt shows at once as a
|
|
196
|
-
`pending` item until Base44's own message arrives; a prompt sent mid-build waits in Base44's
|
|
197
|
-
queue meanwhile. A question is a union on `kind` (`choice`, `secrets`, `approval`) and carries only the
|
|
198
|
-
actions its kind allows, and declining is always one of them. As in Base44's builder, only the
|
|
199
|
-
last assistant message makes the chat wait; a kind this release doesn't know is declined for you. Every shape has JSDoc in `src/react/chat.types.ts`.
|
|
200
|
-
|
|
201
|
-
### Provider and parts
|
|
202
|
-
|
|
203
|
-
`<Base44ChatProvider>` calls the hook once and shares the chat through context. Any component
|
|
204
|
-
below it reads the chat with `useChatState()` (`items`, `status`, `connection`, `error`) or
|
|
205
|
-
`useChatActions()` (`send`, `create`, `clearError`), and `<Message>` draws an item with your parts.
|
|
206
|
-
|
|
207
|
-
```tsx
|
|
208
|
-
import { Base44ChatProvider, Message, useChatState } from "@base44/platform/react";
|
|
209
|
-
|
|
210
|
-
<Base44ChatProvider appId={appId} backend={backend} onAppCreated={selectApp} components={{ question: { Choice, Secrets, Approval } }}>
|
|
211
|
-
<Messages />
|
|
212
|
-
</Base44ChatProvider>
|
|
213
|
-
|
|
214
|
-
function Messages() {
|
|
215
|
-
const { items } = useChatState();
|
|
216
|
-
return items.map((item) => <div key={item.id} className={item.role}><Message item={item} /></div>);
|
|
217
|
-
}
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
- **A part for every question kind is required.** TypeScript refuses a `components.question`
|
|
221
|
-
without `Choice`, `Secrets` and `Approval`, and a release that adds a kind adds a part, so an
|
|
222
|
-
upgrade tells you what to write. Until then the chat declines the new kind for you.
|
|
223
|
-
- **Message parts are optional.** `Text` and `Step` left out are drawn as plain HTML, with no
|
|
224
|
-
classes and a `data-base44` attribute for CSS.
|
|
225
|
-
- **A fixed order.** `<Message>` draws the text, then each step followed by its open question, if
|
|
226
|
-
it has one. It adds no element of its own. `<Question>` draws a question anywhere else below the provider.
|
|
227
|
-
- **Stable actions.** `useChatActions()` keeps its identity while the app stays the same, also with
|
|
228
|
-
an inline `backend` and `onAppCreated`, so a component that reads only actions doesn't re-render
|
|
229
|
-
when a message arrives.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { WebhookConfigurationErrorCode, WebhookVerificationErrorCode } from "./errors.types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The error the SDK reports when a webhook delivery can't be trusted.
|
|
4
|
+
*
|
|
5
|
+
* You don't create it. The [`verifyEvent()`](/developers/references/platform-sdk/docs/interfaces/webhooks#verifyevent) method rejects with
|
|
6
|
+
* it, and [`createHandler()`](/developers/references/platform-sdk/docs/interfaces/webhooks#createhandler) answers it with a 400 and passes it
|
|
7
|
+
* to your `onError`. Check `code` to see what happened, then look the code up below.
|
|
8
|
+
*
|
|
9
|
+
* The error never carries the request body or its signature.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Refuse a delivery that doesn't verify
|
|
14
|
+
* try {
|
|
15
|
+
* const event = await platform.webhooks.verifyEvent(rawBody, request.headers, { publicKeys });
|
|
16
|
+
* } catch (error) {
|
|
17
|
+
* if (error instanceof WebhookVerificationError) {
|
|
18
|
+
* return Response.json({ error: error.code }, { status: 400 });
|
|
19
|
+
* }
|
|
20
|
+
* throw error;
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class WebhookVerificationError extends Error {
|
|
25
|
+
/** What happened. One of the [verification error codes](#webhookverificationerrorcode). */
|
|
26
|
+
readonly code: WebhookVerificationErrorCode;
|
|
27
|
+
/** @internal */
|
|
28
|
+
constructor(code: WebhookVerificationErrorCode);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The error the SDK reports when the public keys you passed can't be used, so no delivery can be verified.
|
|
32
|
+
*
|
|
33
|
+
* You don't create it. The [`verifyEvent()`](/developers/references/platform-sdk/docs/interfaces/webhooks#verifyevent) method rejects with
|
|
34
|
+
* it, and [`createHandler()`](/developers/references/platform-sdk/docs/interfaces/webhooks#createhandler) answers it with a 500 and passes it
|
|
35
|
+
* to your `onError`. Base44 retries the delivery, and the retries succeed once you fix the problem.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* // Log a misconfigured deployment, and let it answer 500
|
|
40
|
+
* export const POST = platform.webhooks.createHandler({
|
|
41
|
+
* publicKeys: process.env.BASE44_WEBHOOK_PUBLIC_KEYS,
|
|
42
|
+
* onError(error) {
|
|
43
|
+
* if (error instanceof WebhookConfigurationError) console.error('Check BASE44_WEBHOOK_PUBLIC_KEYS', error.code);
|
|
44
|
+
* },
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare class WebhookConfigurationError extends Error {
|
|
49
|
+
/** What's wrong with the keys. One of the [configuration error codes](#webhookconfigurationerrorcode). */
|
|
50
|
+
readonly code: WebhookConfigurationErrorCode;
|
|
51
|
+
/** @internal */
|
|
52
|
+
constructor(code: WebhookConfigurationErrorCode, detail: string);
|
|
53
|
+
}
|
|
@@ -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,176 @@
|
|
|
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
|
+
* Your own handle for the owner: the `service_external_id` you gave when you created that service user. Absent when the owner is not a service user.
|
|
19
|
+
*/
|
|
20
|
+
owner_service_external_id?: string;
|
|
21
|
+
/**
|
|
22
|
+
* The kind of app. New values arrive as a new event version.
|
|
23
|
+
*/
|
|
24
|
+
app_type: "imported_app" | "user_agent" | "user_app" | "user_game";
|
|
25
|
+
/**
|
|
26
|
+
* When the app was created (UTC).
|
|
27
|
+
*/
|
|
28
|
+
created_at: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The `data` object of `app.published.v1`. An app version was published.
|
|
32
|
+
*/
|
|
33
|
+
export interface AppPublishedV1Data {
|
|
34
|
+
/**
|
|
35
|
+
* The app the event is about.
|
|
36
|
+
*/
|
|
37
|
+
app_id: string;
|
|
38
|
+
/**
|
|
39
|
+
* The workspace the app belongs to.
|
|
40
|
+
*/
|
|
41
|
+
workspace_id: string;
|
|
42
|
+
/**
|
|
43
|
+
* Opaque platform id of the app's current owner.
|
|
44
|
+
*/
|
|
45
|
+
owner_id: string;
|
|
46
|
+
/**
|
|
47
|
+
* Your own handle for the owner: the `service_external_id` you gave when you created that service user. Absent when the owner is not a service user.
|
|
48
|
+
*/
|
|
49
|
+
owner_service_external_id?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Id of the version now live.
|
|
52
|
+
*/
|
|
53
|
+
published_version: string;
|
|
54
|
+
/**
|
|
55
|
+
* When the version went live (UTC).
|
|
56
|
+
*/
|
|
57
|
+
published_at: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The `data` object of `app.unpublished.v1`. An app was taken offline.
|
|
61
|
+
*/
|
|
62
|
+
export interface AppUnpublishedV1Data {
|
|
63
|
+
/**
|
|
64
|
+
* The app the event is about.
|
|
65
|
+
*/
|
|
66
|
+
app_id: string;
|
|
67
|
+
/**
|
|
68
|
+
* The workspace the app belongs to.
|
|
69
|
+
*/
|
|
70
|
+
workspace_id: string;
|
|
71
|
+
/**
|
|
72
|
+
* Opaque platform id of the app's current owner.
|
|
73
|
+
*/
|
|
74
|
+
owner_id: string;
|
|
75
|
+
/**
|
|
76
|
+
* Your own handle for the owner: the `service_external_id` you gave when you created that service user. Absent when the owner is not a service user.
|
|
77
|
+
*/
|
|
78
|
+
owner_service_external_id?: string;
|
|
79
|
+
/**
|
|
80
|
+
* When the app was taken offline (UTC).
|
|
81
|
+
*/
|
|
82
|
+
unpublished_at: string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Tombstone identifiers only.
|
|
86
|
+
*
|
|
87
|
+
* Deletion is **not** erasure: the app moves to trash and is self-serve
|
|
88
|
+
* restorable for 30 days, with unbounded restore through support. A receiver
|
|
89
|
+
* that purges on this event will be wrong for every restored app, which is why
|
|
90
|
+
* ``app.restored.v1`` exists and why this is documented as a trash signal.
|
|
91
|
+
*/
|
|
92
|
+
export interface AppDeletedV1Data {
|
|
93
|
+
/**
|
|
94
|
+
* The app the event is about.
|
|
95
|
+
*/
|
|
96
|
+
app_id: string;
|
|
97
|
+
/**
|
|
98
|
+
* The workspace the app belongs to.
|
|
99
|
+
*/
|
|
100
|
+
workspace_id: string;
|
|
101
|
+
/**
|
|
102
|
+
* Opaque platform id of the app's current owner.
|
|
103
|
+
*/
|
|
104
|
+
owner_id: string;
|
|
105
|
+
/**
|
|
106
|
+
* Your own handle for the owner: the `service_external_id` you gave when you created that service user. Absent when the owner is not a service user.
|
|
107
|
+
*/
|
|
108
|
+
owner_service_external_id?: string;
|
|
109
|
+
/**
|
|
110
|
+
* When the app was moved to trash (UTC). It can still be restored.
|
|
111
|
+
*/
|
|
112
|
+
deleted_at: string;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The `data` object of `app.restored.v1`. An app was restored from trash.
|
|
116
|
+
*/
|
|
117
|
+
export interface AppRestoredV1Data {
|
|
118
|
+
/**
|
|
119
|
+
* The app the event is about.
|
|
120
|
+
*/
|
|
121
|
+
app_id: string;
|
|
122
|
+
/**
|
|
123
|
+
* The workspace the app belongs to.
|
|
124
|
+
*/
|
|
125
|
+
workspace_id: string;
|
|
126
|
+
/**
|
|
127
|
+
* Opaque platform id of the app's current owner.
|
|
128
|
+
*/
|
|
129
|
+
owner_id: string;
|
|
130
|
+
/**
|
|
131
|
+
* Your own handle for the owner: the `service_external_id` you gave when you created that service user. Absent when the owner is not a service user.
|
|
132
|
+
*/
|
|
133
|
+
owner_service_external_id?: string;
|
|
134
|
+
/**
|
|
135
|
+
* When the app was restored (UTC).
|
|
136
|
+
*/
|
|
137
|
+
restored_at: string;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Carries no domain data — its only job is to be verifiable and echoed.
|
|
141
|
+
*/
|
|
142
|
+
export interface WebhookTestV1Data {
|
|
143
|
+
/**
|
|
144
|
+
* The endpoint being activated.
|
|
145
|
+
*/
|
|
146
|
+
endpoint_id: string;
|
|
147
|
+
/**
|
|
148
|
+
* The workspace the endpoint belongs to.
|
|
149
|
+
*/
|
|
150
|
+
workspace_id: string;
|
|
151
|
+
/**
|
|
152
|
+
* When the test was sent (UTC).
|
|
153
|
+
*/
|
|
154
|
+
sent_at: string;
|
|
155
|
+
/**
|
|
156
|
+
* Return this value in your response body to activate the endpoint.
|
|
157
|
+
*/
|
|
158
|
+
challenge: string;
|
|
159
|
+
}
|
|
160
|
+
/** Every event type Base44 sends, mapped to the shape of its `data` object. */
|
|
161
|
+
export interface WebhookEventMap {
|
|
162
|
+
/** An app was created. */
|
|
163
|
+
"app.created.v1": AppCreatedV1Data;
|
|
164
|
+
/** An app version was published. */
|
|
165
|
+
"app.published.v1": AppPublishedV1Data;
|
|
166
|
+
/** An app was taken offline. */
|
|
167
|
+
"app.unpublished.v1": AppUnpublishedV1Data;
|
|
168
|
+
/** Tombstone identifiers only. */
|
|
169
|
+
"app.deleted.v1": AppDeletedV1Data;
|
|
170
|
+
/** An app was restored from trash. */
|
|
171
|
+
"app.restored.v1": AppRestoredV1Data;
|
|
172
|
+
/** Carries no domain data — its only job is to be verifiable and echoed. */
|
|
173
|
+
"webhook.test.v1": WebhookTestV1Data;
|
|
174
|
+
}
|
|
175
|
+
/** Every event type in {@link WebhookEventMap}, so events from a newer Base44 can be told apart at runtime. @internal */
|
|
176
|
+
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"];
|
|
@@ -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"];
|