@base44-preview/platform 0.2.0-pr.313.0299b5a → 0.2.0-pr.313.147bf84

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
@@ -115,7 +115,9 @@ their exceptions are isolated so they cannot interrupt another app's delivery.
115
115
  event is `{room, data}`; the SDK routes by `room` and validates the envelope, while
116
116
  payload schemas and private-field filtering are the server's. Omitted keys mean
117
117
  unchanged and explicit null clears. Events carrying `branch_id` belong to that branch;
118
- null or absent is main. Unknown event names are not forwarded. `PlatformSnapshot` is
118
+ null or absent is main. An event name the SDK doesn't know is forwarded as
119
+ `{type: "unknown", name, appId, data}` with only the envelope validated, so a new event reaches
120
+ partners when the service deploys it; new `room.*` notices are dropped. `PlatformSnapshot` is
119
121
  the service's `Snapshot` plus the app's `room`. Structural filtering does not promise
120
122
  redaction of generated prose or user content.
121
123
 
@@ -1,7 +1,9 @@
1
- import type { PlatformEvent, PlatformEventMap, PlatformSnapshot } from "./builder.events.types.js";
1
+ import type { PlatformEvent, PlatformSnapshot } from "./builder.events.types.js";
2
2
  import type { SocketSession } from "./builder.types.js";
3
3
  /** App events delivered to `onEvent`: every server event except the session events the socket handles. */
4
4
  export declare const eventNames: ("message.updated" | "message.removed" | "app.status_changed" | "preview.reload_requested" | "preview.navigation_requested" | "queue.updated" | "task.progressed" | "image.resolved" | "conversation.changed" | "files.changed" | "branch.deleted" | "repository.changed" | "pull_request.changed")[];
5
+ /** A name newer than this SDK's protocol, delivered to `onEvent` as `unknown`. New `room.*` notices have no error code, so they are dropped. */
6
+ export declare const isUnknownEvent: (name: string) => boolean;
5
7
  /** Notices that name an app room, by the error code each one reports. A new notice fails to compile until it has one. */
6
8
  export declare const roomNotices: {
7
9
  readonly "room.access_denied": "access_denied";
@@ -18,7 +20,7 @@ export declare const roomFor: (appId: string) => string;
18
20
  export declare function object(value: unknown): Record<string, unknown>;
19
21
  export declare function appFromRoom(value: unknown): string | undefined;
20
22
  /** Every event is `{room, data}`; payload schemas are the service's, so only the envelope is validated. */
21
- export declare function decode(type: keyof PlatformEventMap, appId: string, raw: unknown): PlatformEvent;
23
+ export declare function decode(name: string, appId: string, raw: unknown): PlatformEvent;
22
24
  export declare function decodeSnapshot(raw: unknown): PlatformSnapshot;
23
25
  /** Keeps only the fields the socket needs; a URL with credentials, a path, query or fragment is refused. */
24
26
  export declare function decodeSession(raw: unknown): SocketSession;
@@ -2,6 +2,9 @@ import { serverEventNames } from "./builder.events.generated.js";
2
2
  const sessionEvents = ["app.snapshot", "session.ended"];
3
3
  /** App events delivered to `onEvent`: every server event except the session events the socket handles. */
4
4
  export const eventNames = serverEventNames.filter((name) => !sessionEvents.includes(name) && !name.startsWith("room."));
5
+ const knownNames = serverEventNames;
6
+ /** A name newer than this SDK's protocol, delivered to `onEvent` as `unknown`. New `room.*` notices have no error code, so they are dropped. */
7
+ export const isUnknownEvent = (name) => !knownNames.includes(name) && !name.startsWith("room.");
5
8
  /** Notices that name an app room, by the error code each one reports. A new notice fails to compile until it has one. */
6
9
  export const roomNotices = {
7
10
  "room.access_denied": "access_denied",
@@ -25,8 +28,9 @@ export function appFromRoom(value) {
25
28
  return typeof value === "string" && /^\/apps\/[a-f0-9]{24}$/.test(value) ? value.slice(6) : undefined;
26
29
  }
27
30
  /** Every event is `{room, data}`; payload schemas are the service's, so only the envelope is validated. */
28
- export function decode(type, appId, raw) {
29
- return { type, appId, data: object(object(raw).data) };
31
+ export function decode(name, appId, raw) {
32
+ const data = object(object(raw).data);
33
+ return (knownNames.includes(name) ? { type: name, appId, data } : { type: "unknown", name, appId, data });
30
34
  }
31
35
  export function decodeSnapshot(raw) {
32
36
  const frame = object(raw);
@@ -31,6 +31,7 @@ export declare class BuilderSocket implements BuilderSession {
31
31
  /** Stop delivery, cancel reconnection and release all listeners. Idempotent and terminal. */
32
32
  close(): void;
33
33
  private open;
34
+ private appEvent;
34
35
  private discard;
35
36
  private join;
36
37
  /** Without a callback, or when a new session moves the socket, opens a socket at the session's address. */
@@ -1,6 +1,6 @@
1
1
  import { io } from "socket.io-client";
2
2
  import { PlatformSocketError } from "../errors.js";
3
- import { appFromRoom, appPattern, decode, decodeSession, decodeSnapshot, eventNames, object, roomFor, roomNotices, sessionEndings } from "./builder-protocol.js";
3
+ import { appFromRoom, appPattern, decode, decodeSession, decodeSnapshot, eventNames, isUnknownEvent, object, roomFor, roomNotices, sessionEndings } from "./builder-protocol.js";
4
4
  import { notify, Subscription } from "./builder-subscription.js";
5
5
  const MAX_DENIAL_RETRIES = 5;
6
6
  /** @internal */
@@ -121,29 +121,36 @@ export class BuilderSocket {
121
121
  socket.on("session.ended", (raw) => this.sessionEnded(raw));
122
122
  for (const [name, code] of Object.entries(roomNotices))
123
123
  socket.on(name, (raw) => this.roomNotice(code, raw));
124
- for (const type of eventNames)
125
- socket.on(type, (raw) => {
126
- try {
127
- const appId = appFromRoom(object(raw).room);
128
- if (!appId)
129
- throw new Error("Invalid app");
130
- const event = decode(type, appId, raw);
131
- const subscription = this.subscriptions.get(appId);
132
- subscription === null || subscription === void 0 ? void 0 : subscription.event(event);
133
- // The server does not resend a snapshot after a rewrite of main: rejoining asks for one.
134
- if (subscription && event.type === "conversation.changed" && event.data.branch_id == null)
135
- this.join(subscription);
136
- }
137
- catch (_a) {
138
- this.protocolError(raw);
139
- }
140
- });
124
+ for (const name of eventNames)
125
+ socket.on(name, (raw) => this.appEvent(name, raw));
126
+ // An unknown event with no app room (a future session-wide event) has nowhere to go: ignore it.
127
+ socket.onAny((name, raw) => {
128
+ if (isUnknownEvent(name) && appFromRoom(raw === null || raw === void 0 ? void 0 : raw.room))
129
+ this.appEvent(name, raw);
130
+ });
141
131
  socket.connect();
142
132
  }
133
+ appEvent(name, raw) {
134
+ try {
135
+ const appId = appFromRoom(object(raw).room);
136
+ if (!appId)
137
+ throw new Error("Invalid app");
138
+ const event = decode(name, appId, raw);
139
+ const subscription = this.subscriptions.get(appId);
140
+ subscription === null || subscription === void 0 ? void 0 : subscription.event(event);
141
+ // The server does not resend a snapshot after a rewrite of main: rejoining asks for one.
142
+ if (subscription && event.type === "conversation.changed" && event.data.branch_id == null)
143
+ this.join(subscription);
144
+ }
145
+ catch (_a) {
146
+ this.protocolError(raw);
147
+ }
148
+ }
143
149
  discard() {
144
150
  if (!this.socket)
145
151
  return;
146
152
  this.socket.removeAllListeners();
153
+ this.socket.offAny();
147
154
  this.socket.io.removeAllListeners();
148
155
  this.socket.disconnect();
149
156
  }
@@ -16,6 +16,10 @@ export type PlatformEventMap = {
16
16
  * is unchanged, and an explicit `null` clears it. For every event and its payload, see
17
17
  * [Socket messages](/developers/references/apps-api/sections/socket-messages).
18
18
  *
19
+ * An event added to the service after this version of the SDK arrives with `type: "unknown"`, its
20
+ * name in `name` and an unchecked `data`, so check a field before using it. After you upgrade, it
21
+ * arrives under its own `type`.
22
+ *
19
23
  * @example
20
24
  * ```typescript
21
25
  * // Narrow an event by type
@@ -24,6 +28,9 @@ export type PlatformEventMap = {
24
28
  * const { message } = event.data;
25
29
  * console.log(`Message ${message.id} changed`);
26
30
  * }
31
+ * if (event.type === "unknown" && event.name === "app.renamed" && typeof event.data.name === "string") {
32
+ * console.log(`App renamed to ${event.data.name}`);
33
+ * }
27
34
  * }
28
35
  * ```
29
36
  */
@@ -36,7 +43,16 @@ export type PlatformEvent = {
36
43
  /** Event payload. */
37
44
  data: PlatformEventMap[K];
38
45
  };
39
- }[keyof PlatformEventMap];
46
+ }[keyof PlatformEventMap] | {
47
+ /** An event this version of the SDK doesn't know. */
48
+ type: "unknown";
49
+ /** The event's name on the socket. */
50
+ name: string;
51
+ /** ID of the app the event belongs to. */
52
+ appId: string;
53
+ /** Event payload, not checked by the SDK. */
54
+ data: Record<string, unknown>;
55
+ };
40
56
  /**
41
57
  * An app's current state.
42
58
  *
@@ -163,7 +163,8 @@ export interface SubscriptionOptions {
163
163
  /**
164
164
  * Called with each live event, a [`PlatformEvent`](#platformevent). Events for an app arrive one at a time, in order. If the callback
165
165
  * throws or rejects, the subscription ends with `handler_failed`. New event types can be added at
166
- * any time, so ignore types you don't handle. For every event and its payload,
166
+ * any time, so ignore types you don't handle. An event newer than this SDK arrives with
167
+ * `type: "unknown"` and its name in `name`. For every event and its payload,
167
168
  * see [Socket messages](/developers/references/apps-api/sections/socket-messages).
168
169
  */
169
170
  onEvent: (event: PlatformEvent) => void | Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/platform",
3
- "version": "0.2.0-pr.313.0299b5a",
3
+ "version": "0.2.0-pr.313.147bf84",
4
4
  "description": "Browser client for building on the Base44 platform",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",