@base44-preview/platform 0.1.0-pr.319.cf89f7f → 0.1.0-pr.332.00d7174

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 (41) hide show
  1. package/README.md +71 -124
  2. package/dist/client/client.d.ts +30 -9
  3. package/dist/client/client.js +30 -7
  4. package/dist/client/client.types.d.ts +9 -11
  5. package/dist/client/errors.d.ts +41 -4
  6. package/dist/client/errors.js +39 -2
  7. package/dist/client/errors.types.d.ts +98 -2
  8. package/dist/client/index.d.ts +6 -5
  9. package/dist/client/index.js +1 -1
  10. package/dist/client/modules/builder-protocol.d.ts +16 -5
  11. package/dist/client/modules/builder-protocol.js +30 -8
  12. package/dist/client/modules/builder-socket.d.ts +10 -5
  13. package/dist/client/modules/builder-socket.js +114 -87
  14. package/dist/client/modules/builder-subscription.d.ts +2 -2
  15. package/dist/client/modules/builder.d.ts +1 -2
  16. package/dist/client/modules/builder.events.generated.d.ts +2194 -0
  17. package/dist/client/modules/builder.events.generated.js +3 -0
  18. package/dist/client/modules/builder.events.types.d.ts +70 -361
  19. package/dist/client/modules/builder.js +2 -2
  20. package/dist/client/modules/builder.types.d.ts +169 -23
  21. package/dist/server/index.d.ts +2 -5
  22. package/dist/server/index.js +1 -2
  23. package/dist/server/server-url.d.ts +2 -0
  24. package/dist/{shared → server}/server-url.js +1 -1
  25. package/dist/server/server.d.ts +2 -2
  26. package/dist/server/server.js +3 -6
  27. package/dist/server/server.types.d.ts +2 -7
  28. package/package.json +17 -13
  29. package/dist/server/errors.d.ts +0 -15
  30. package/dist/server/errors.js +0 -18
  31. package/dist/server/errors.types.d.ts +0 -4
  32. package/dist/server/errors.types.js +0 -1
  33. package/dist/server/modules/webhooks-signature.d.ts +0 -11
  34. package/dist/server/modules/webhooks-signature.js +0 -65
  35. package/dist/server/modules/webhooks.d.ts +0 -3
  36. package/dist/server/modules/webhooks.events.generated.d.ts +0 -174
  37. package/dist/server/modules/webhooks.events.generated.js +0 -3
  38. package/dist/server/modules/webhooks.js +0 -77
  39. package/dist/server/modules/webhooks.types.d.ts +0 -102
  40. package/dist/server/modules/webhooks.types.js +0 -1
  41. package/dist/shared/server-url.d.ts +0 -2
@@ -1,77 +0,0 @@
1
- import { WebhookConfigurationError, WebhookVerificationError } from "../errors.js";
2
- import { anySignatureVerifies, importPublicKey, parsePublicKeys, parseSignatures, readHeader, signedPayload } from "./webhooks-signature.js";
3
- const DEFAULT_TOLERANCE_SECONDS = 300;
4
- /** Webhook receiving utilities. Verification is local: no network calls, no timers. */
5
- export function createWebhooks(options = {}) {
6
- // Imported keys are cached by their wire form so a pinned key is imported once.
7
- const imported = new Map();
8
- const keysFor = (publicKeys = options.publicKeys) => Promise.all(parsePublicKeys(publicKeys).map(raw => {
9
- const id = raw.join(",");
10
- if (!imported.has(id))
11
- imported.set(id, importPublicKey(raw));
12
- return imported.get(id);
13
- }));
14
- const verifyEvent = async (body, headers, verifyOptions = {}) => {
15
- var _a;
16
- // Configuration problems surface first, whatever the request looks like: a missing key is ours to fix.
17
- const keys = await keysFor(verifyOptions.publicKeys);
18
- const id = readHeader(headers, "webhook-id");
19
- const timestamp = readHeader(headers, "webhook-timestamp");
20
- const signature = readHeader(headers, "webhook-signature", " ");
21
- if (!id || !timestamp || !signature)
22
- throw new WebhookVerificationError("missing_headers");
23
- // Negated so a non-numeric timestamp (NaN) is refused too.
24
- if (!(Math.abs(Date.now() / 1000 - Number(timestamp)) <= ((_a = verifyOptions.toleranceSeconds) !== null && _a !== void 0 ? _a : DEFAULT_TOLERANCE_SECONDS))) {
25
- throw new WebhookVerificationError("timestamp_outside_tolerance");
26
- }
27
- const signatures = parseSignatures(signature);
28
- if (signatures.length === 0)
29
- throw new WebhookVerificationError("no_signature_entries");
30
- if (!(await anySignatureVerifies(keys, signatures, signedPayload(id, timestamp, body)))) {
31
- throw new WebhookVerificationError("signature_mismatch");
32
- }
33
- // Only now is the body trusted enough to parse.
34
- let event;
35
- try {
36
- event = JSON.parse(typeof body === "string" ? body : new TextDecoder().decode(body));
37
- }
38
- catch (_b) {
39
- throw new WebhookVerificationError("invalid_payload");
40
- }
41
- const { type, id: eventId, data } = (event !== null && event !== void 0 ? event : {});
42
- if (typeof type !== "string" || typeof eventId !== "string" || typeof data !== "object" || data === null) {
43
- throw new WebhookVerificationError("invalid_payload");
44
- }
45
- return event;
46
- };
47
- return {
48
- verifyEvent,
49
- createHandler({ on = {}, onEvent, onSuccess, onError, ...verifyOptions } = {}) {
50
- const fail = async (error, status, event) => {
51
- var _a;
52
- return (_a = (await (onError === null || onError === void 0 ? void 0 : onError(error, event)))) !== null && _a !== void 0 ? _a : Response.json({ error: error instanceof WebhookVerificationError ? error.code : error instanceof WebhookConfigurationError ? "receiver_misconfigured" : "handler_failed" }, { status });
53
- };
54
- return async (request) => {
55
- var _a, _b;
56
- let event;
57
- try {
58
- event = await verifyEvent(new Uint8Array(await request.arrayBuffer()), request.headers, verifyOptions);
59
- }
60
- catch (error) {
61
- return fail(error, error instanceof WebhookVerificationError ? 400 : 500);
62
- }
63
- // Activation probe: echo the challenge, only after the signature verified. Never reaches your callbacks.
64
- if (event.type === "webhook.test.v1")
65
- return Response.json({ challenge: event.data.challenge });
66
- try {
67
- await (onEvent === null || onEvent === void 0 ? void 0 : onEvent(event));
68
- await ((_a = on[event.type]) === null || _a === void 0 ? void 0 : _a.call(on, event));
69
- return (_b = (await (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(event)))) !== null && _b !== void 0 ? _b : new Response(null, { status: 204 });
70
- }
71
- catch (error) {
72
- return fail(error, 500, event);
73
- }
74
- };
75
- },
76
- };
77
- }
@@ -1,102 +0,0 @@
1
- import type { WebhookEventMap, WebhookTestV1Data } from "./webhooks.events.generated.js";
2
- export type * from "./webhooks.events.generated.js";
3
- /** Name of a webhook event, e.g. `app.deleted.v1`. */
4
- export type WebhookEventType = keyof WebhookEventMap;
5
- /** CloudEvents 1.0 envelope every Base44 webhook is delivered in. */
6
- export interface CloudEvent<TType extends string, TData> {
7
- /** CloudEvents version. Always `1.0`. */
8
- specversion: "1.0";
9
- /** Unique event id (`evt_…`). Identical across redeliveries, so use it to deduplicate. */
10
- id: string;
11
- /** The workspace the event happened in, as a URL. */
12
- source: string;
13
- /** Event type, including its version. */
14
- type: TType;
15
- /** The resource the event is about, e.g. `apps/<app id>`. */
16
- subject: string;
17
- /** When the event occurred (RFC 3339, UTC). */
18
- time: string;
19
- /** Content type of `data`. Always `application/json`. */
20
- datacontenttype: string;
21
- /** URL of the JSON Schema describing `data`. */
22
- dataschema: string;
23
- /** W3C trace context, when the event was produced inside a trace. */
24
- traceparent?: string;
25
- /** W3C trace state, when present. */
26
- tracestate?: string;
27
- /** Kind of actor that caused the event. */
28
- authtype: string;
29
- /** Identifier of the actor that caused the event. */
30
- authid: string;
31
- /** Event-specific payload. */
32
- data: TData;
33
- }
34
- /**
35
- * A verified Base44 webhook event. Narrow on `type` to read `data`.
36
- * Base44 may add event types later, so give a `switch` a default branch that still answers 2xx.
37
- */
38
- export type WebhookEvent = {
39
- [K in WebhookEventType]: CloudEvent<K, WebhookEventMap[K]>;
40
- }[WebhookEventType];
41
- /** Request headers in any common shape: a fetch `Headers`, or a Node-style object. Names are case-insensitive. */
42
- export type WebhookHeaders = Headers | Record<string, string | string[] | undefined>;
43
- /** One Ed25519 public key (`whpk_…`), or several separated by commas or whitespace. */
44
- export type WebhookPublicKeys = string | string[];
45
- /** Webhook settings for `createBase44PlatformServer`. */
46
- export interface WebhooksOptions {
47
- /** Public keys of your workspace. Pass more than one while a key rotation is in progress. */
48
- publicKeys?: WebhookPublicKeys;
49
- }
50
- /** Per-call overrides for `verifyEvent`. */
51
- export interface VerifyEventOptions {
52
- /** Public keys to verify against, instead of the ones the client was created with. */
53
- publicKeys?: WebhookPublicKeys;
54
- /** Maximum age or clock skew of a delivery, in seconds. Defaults to 300. */
55
- toleranceSeconds?: number;
56
- }
57
- /** An event your handler receives: every event except the `webhook.test.v1` probe, which `createHandler` answers itself. */
58
- export type DeliveredWebhookEvent = Exclude<WebhookEvent, CloudEvent<"webhook.test.v1", WebhookTestV1Data>>;
59
- /** Per-type handlers for `createHandler`, each receiving the event already narrowed to its type. */
60
- export type WebhookEventHandlers = {
61
- [K in DeliveredWebhookEvent["type"]]?: (event: Extract<DeliveredWebhookEvent, {
62
- type: K;
63
- }>) => unknown;
64
- };
65
- /** Options for `createHandler`. Every callback may be async. */
66
- export interface CreateHandlerOptions extends VerifyEventOptions {
67
- /** Handlers keyed by event type. Types without a handler are still answered 2xx, so Base44 does not retry them. */
68
- on?: WebhookEventHandlers;
69
- /** Called for every verified event, before its handler in `on`. */
70
- onEvent?: (event: DeliveredWebhookEvent) => unknown;
71
- /**
72
- * Called once `onEvent` and the event's handler finished without throwing. Return a `Response` to replace the default
73
- * empty 204.
74
- */
75
- onSuccess?: (event: DeliveredWebhookEvent) => Response | void | Promise<Response | void>;
76
- /**
77
- * Called when a delivery fails: a `WebhookVerificationError` (default response 400), a
78
- * `WebhookConfigurationError` (500), or whatever a handler threw (500, so Base44 retries). `event`
79
- * is set when verification passed and a callback failed. Return a `Response` to replace the default.
80
- */
81
- onError?: (error: unknown, event?: DeliveredWebhookEvent) => Response | void | Promise<Response | void>;
82
- }
83
- /** Receive and verify Base44 outbound webhooks. Makes no network requests. */
84
- export interface WebhooksModule {
85
- /**
86
- * Verify a delivery and return the event.
87
- *
88
- * Pass the body exactly as received, before any JSON parsing. Nothing is parsed until the
89
- * signature checks out. Rejects with `WebhookVerificationError` for a delivery you should
90
- * refuse (respond 400) and `WebhookConfigurationError` when the public keys are missing
91
- * or malformed (let it surface as a 500 so Base44 retries). To activate an endpoint, answer
92
- * the `webhook.test.v1` event with `{ "challenge": event.data.challenge }`.
93
- */
94
- verifyEvent(body: string | Uint8Array, headers: WebhookHeaders, options?: VerifyEventOptions): Promise<WebhookEvent>;
95
- /**
96
- * Build a fetch-style route handler (`(request: Request) => Promise<Response>`) for Next.js, Hono,
97
- * Cloudflare Workers, Deno or Bun. It reads the raw body, verifies it, answers the activation
98
- * challenge, then runs your callbacks. Delivery is at least once, so handlers must tolerate
99
- * the same event (same `event.id`) arriving twice.
100
- */
101
- createHandler(options?: CreateHandlerOptions): (request: Request) => Promise<Response>;
102
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- /** Validate a platform origin and return it normalized. Shared by the client and server entrypoints. */
2
- export declare function normalizeServerUrl(serverUrl: string): string;