@notionhq/custom-blocks-host 0.1.8 → 0.1.10

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 (33) hide show
  1. package/README.md +43 -24
  2. package/dist/createCustomBlockHost.d.ts +13 -11
  3. package/dist/createCustomBlockHost.d.ts.map +1 -1
  4. package/dist/createCustomBlockHost.js +35 -106
  5. package/dist/lifecycle/initializationController.d.ts +9 -4
  6. package/dist/lifecycle/initializationController.d.ts.map +1 -1
  7. package/dist/lifecycle/initializationController.js +34 -26
  8. package/dist/lifecycle/types.d.ts +5 -1
  9. package/dist/lifecycle/types.d.ts.map +1 -1
  10. package/dist/messages/invalidSandboxMessage.js +1 -1
  11. package/dist/messages/sandboxMessageRouter.d.ts +35 -0
  12. package/dist/messages/sandboxMessageRouter.d.ts.map +1 -0
  13. package/dist/messages/sandboxMessageRouter.js +107 -0
  14. package/dist/messages/types.d.ts +1 -1
  15. package/dist/messages/types.d.ts.map +1 -1
  16. package/dist/protocol/index.d.ts +3 -2
  17. package/dist/protocol/index.js +3 -2
  18. package/dist/protocol/messages/isInitHandshakeMessage.d.ts +4 -0
  19. package/dist/protocol/messages/isInitHandshakeMessage.js +11 -0
  20. package/dist/protocol/{messagePayload.d.ts → messages/messagePayload.d.ts} +1 -1
  21. package/dist/queries/types.d.ts +1 -1
  22. package/dist/queries/types.d.ts.map +1 -1
  23. package/package.json +1 -1
  24. package/src/createCustomBlockHost.ts +49 -134
  25. package/src/lifecycle/initializationController.ts +54 -33
  26. package/src/lifecycle/types.ts +5 -1
  27. package/src/messages/invalidSandboxMessage.ts +1 -1
  28. package/src/messages/sandboxMessageRouter.ts +176 -0
  29. package/src/messages/types.ts +1 -1
  30. package/src/queries/types.ts +1 -1
  31. /package/dist/protocol/{incomingType.d.ts → messages/incomingType.d.ts} +0 -0
  32. /package/dist/protocol/{incomingType.js → messages/incomingType.js} +0 -0
  33. /package/dist/protocol/{messagePayload.js → messages/messagePayload.js} +0 -0
package/README.md CHANGED
@@ -72,29 +72,42 @@ const host = createCustomBlockHost({
72
72
  listUsers: async (message) => listUsers(message),
73
73
  resize: (message) => resizeIframe(message.height),
74
74
  },
75
- onConnect: (message) => {
76
- console.log("Connected SDK", message.sdkVersion);
75
+ onManifest: (manifest) => {
76
+ console.log("Selected manifest", manifest);
77
77
  },
78
- onIgnoredConnect: () => {
79
- remountHostAndIframe();
78
+ onInitialized: (state) => {
79
+ console.log("Initialized manifest", state.manifest);
80
80
  },
81
81
  onInitializationFailure: (error) => {
82
82
  showInitializationError(error);
83
83
  },
84
+ onRestartRequired: () => {
85
+ remountHostAndIframe();
86
+ },
84
87
  onLog: (direction, payload) => {
85
88
  console.log(direction, payload);
86
89
  },
87
90
  });
88
91
  ```
89
92
 
90
- The host runs one initialization per instance. `onIgnoredConnect` fires when a
91
- `connect` arrives that the settled host cannot serve: the iframe document was
92
- replaced, or the document's first `connect` came after the no-connect timeout.
93
- Recreate the host and the iframe together so the document runs a fresh
94
- handshake against current host state.
93
+ Each host instance initializes once:
94
+
95
+ - `onManifest` provides the selected manifest as soon as it is available after
96
+ `connect`, before binding validation and `onInitialized`. Use it to configure
97
+ bindings even when initialization cannot yet succeed.
95
98
 
96
- `onInitializationFailure` receives structured errors from failed `init`
97
- messages, failed `initResult` messages, and initialization timeouts.
99
+ - `onInitialized` reports success after the sandbox accepts initialization. It
100
+ provides the selected `manifest` and the sandbox's optional `initialHeight`.
101
+ - `onInitializationFailure` reports an initialization error or timeout.
102
+ - `onRestartRequired` reports a connection attempt after the host accepts its
103
+ first connection or stops waiting. Recreate the host and iframe together
104
+ to accept a new connection.
105
+ This can occur after the iframe document changes or a connection arrives
106
+ after a timeout.
107
+
108
+ The host uses the manifest from `connect`, or `initialState.manifest` if
109
+ `connect` omits it. If neither exists, initialization fails with
110
+ `manifest_unavailable`.
98
111
 
99
112
  `queryDataSource` is required. Other request handlers are optional. Every
100
113
  request handler may return its result synchronously or as a promise. Results
@@ -138,26 +151,32 @@ the ID in the `init` message. The host ignores a result with a different ID.
138
151
  If no result arrives, or only a stale valid result arrives, before the timeout,
139
152
  initialization ends with `no_init_result`.
140
153
 
141
- Hosts validate each inbound sandbox message with `sandboxToHostMessageSchema`
142
- before acting on it. If validation fails, the host uses `readIncomingType()` to
143
- identify the message and applies these rules:
154
+ The host validates each inbound sandbox message with `sandboxToHostMessageSchema`
155
+ before it handles the message. If parsing fails, the host uses `readIncomingType()`
156
+ to identify the attempted message. It replies with `invalidSandboxMessage`, a
157
+ negative acknowledgment (NACK). The host never sends a NACK in response to a NACK.
158
+
159
+ Malformed sandbox messages do not change the host's initialization state or
160
+ timeout, except in these two cases:
161
+
162
+ - The host expects `connect` and receives a malformed `connect` with a readable
163
+ `initializationId`. The host sends `init.error` with `invalid_connect_payload`
164
+ instead of a NACK, then fails initialization.
165
+ - The host expects `initResult` and receives a malformed `initResult` with the
166
+ active `initializationId`. The host sends a NACK and fails initialization with
167
+ `invalid_init_result_payload`. This error code describes a local host failure.
168
+ The host does not send it in `initResult.error`.
144
169
 
145
- - A malformed `connect` with a readable `initializationId` gets `init.error`
146
- with code `invalid_connect_payload`.
147
- - A malformed `initResult` gets one `invalidSandboxMessage` NACK. The host then
148
- cancels the handshake and fails with the host-only `invalid_init_result_payload`
149
- error. The host does not wait for the `no_init_result` timeout.
150
- - Any other malformed sandbox message gets one `invalidSandboxMessage` NACK.
151
- The sandbox logs this NACK and does not reply.
152
- - Never reply to a sandbox-sent NACK with a host-sent NACK. The host should
153
- not respond to `invalidHostMessage` or `invalidSandboxMessage` messages.
170
+ A malformed `initResult` with a missing or stale ID does not end initialization
171
+ or reset its timeout. Malformed messages do not restart initialization after
172
+ it succeeds or fails.
154
173
 
155
174
  The host accepts a valid `initResult.error` as a terminal initialization
156
175
  failure. It logs and ignores a valid `initResult` with a stale
157
176
  `initializationId` or an unexpected state.
158
177
 
159
178
  Hosts do not retry malformed messages. A host that receives a `connect` after
160
- initialization has settled must use `onIgnoredConnect` to recreate the host
179
+ initialization has settled must use `onRestartRequired` to recreate the host
161
180
  and iframe together.
162
181
 
163
182
  ## Updating live state
@@ -1,13 +1,12 @@
1
1
  import { type NotionContrastMode } from "./protocol/contrast.js";
2
2
  import type { NotionDataSourceId } from "./protocol/ids.js";
3
- import type { ConnectMessage } from "./protocol/messages/connect.js";
3
+ import type { CustomBlockManifest } from "./protocol/manifest.js";
4
4
  import type { HostToSandboxMessage } from "./protocol/messages/hostToSandbox.js";
5
- import type { InitResultMessage } from "./protocol/messages/initResult.js";
6
5
  import type { CustomBlockPage } from "./protocol/pages/page.js";
7
6
  import type { NotionParent } from "./protocol/parent.js";
8
7
  import type { NotionTheme } from "./protocol/theme.js";
9
8
  import type { NotionUser } from "./protocol/users/user.js";
10
- import type { ConnectSuccessMessage, CustomBlockHostDataSourcesPayload, CustomBlockHostInitialState } from "./lifecycle/types.js";
9
+ import type { CustomBlockHostDataSourcesPayload, CustomBlockHostInitialState } from "./lifecycle/types.js";
11
10
  import type { CustomBlockHostHandlers, CustomBlockHostInitializationFailure, CustomBlockHostLogDirection } from "./messages/types.js";
12
11
  export type CustomBlockHostOptions = {
13
12
  iframe: HTMLIFrameElement;
@@ -17,17 +16,20 @@ export type CustomBlockHostOptions = {
17
16
  noInitResultTimeoutMs?: number;
18
17
  initialState: CustomBlockHostInitialState;
19
18
  handlers: CustomBlockHostHandlers;
20
- onConnect?: (message: ConnectSuccessMessage) => void;
21
- onInitResult?: (message: InitResultMessage) => void;
19
+ /** Called as soon as the `manifest` is available after `connect`, before `onInitialized` and binding validation. */
20
+ onManifest?: (manifest: CustomBlockManifest) => void;
21
+ /** Callback fired when the initialization handshake with the sandbox completed successfully. */
22
+ onInitialized?: (state: {
23
+ manifest: CustomBlockManifest;
24
+ initialHeight?: number;
25
+ }) => void;
26
+ /** Callback fired when the initialization handshake with the sandbox failed or timed out. */
22
27
  onInitializationFailure?: (error: CustomBlockHostInitializationFailure) => void;
23
28
  /**
24
- * A `connect` arrived after this host's initialization settled the
25
- * document was replaced, or its first `connect` came after the host
26
- * stopped waiting. Either way the host cannot serve it: the embedder
27
- * should recreate host and iframe together so the document can run a
28
- * fresh handshake.
29
+ * Callback fired when the current host and iframe must restart.
30
+ * Recreate both to continue.
29
31
  */
30
- onIgnoredConnect?: (message: ConnectMessage) => void;
32
+ onRestartRequired?: () => void;
31
33
  onLog?: (direction: CustomBlockHostLogDirection, payload: unknown) => void;
32
34
  };
33
35
  export type CustomBlockHostHandle = {
@@ -1 +1 @@
1
- {"version":3,"file":"createCustomBlockHost.d.ts","sourceRoot":"","sources":["../src/createCustomBlockHost.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,kBAAkB,EACvB,MAAM,8CAA8C,CAAA;AACrD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAA;AAEjF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAa1F,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4DAA4D,CAAA;AAEtG,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAahG,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gDAAgD,CAAA;AACrF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4CAA4C,CAAA;AAE9E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2CAA2C,CAAA;AAC5E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gDAAgD,CAAA;AAIhF,OAAO,KAAK,EACX,qBAAqB,EACrB,iCAAiC,EACjC,2BAA2B,EAC3B,MAAM,sBAAsB,CAAA;AAE7B,OAAO,KAAK,EACX,uBAAuB,EACvB,oCAAoC,EACpC,2BAA2B,EAC3B,MAAM,qBAAqB,CAAA;AAU5B,MAAM,MAAM,sBAAsB,GAAG;IACpC,MAAM,EAAE,iBAAiB,CAAA;IACzB,aAAa,EAAE,MAAM,CAAA;IACrB,wBAAwB,EAAE,MAAM,CAAA;IAChC,kBAAk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AC,EAAE,CACzB,KAAK,EAAE,oCAAoC,KACvC,IAAI,CAAA;IACT;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,IAAI,CAAA;IAC9B,KAAK,CAAC,EAAE,CAAC,SAAS,EAAE,2BAA2B,EAAE,OAAO,EAAE,OAAO,KAAK,IAAI,CAAA;CAC1E,CAAA;AAED,MAAM,MAAM,qBAAqB,GAAG;IACnC,IAAI,EAAE,MAAM,IAAI,CAAA;IAChB,IAAI,EAAE,CAAC,OAAO,EAAE,oBAAoB,KAAK,IAAI,CAAA;IAC7C,QAAQ,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAA;IACtC,eAAe,EAAE,CAAC,YAAY,EAAE,kBAAkB,KAAK,IAAI,CAAA;IAC3D,SAAS,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAA;IACzC,OAAO,EAAE,CAAC,IAAI,EAAE,eAAe,KAAK,IAAI,CAAA;IACxC,cAAc,EAAE,CAAC,WAAW,EAAE,iCAAiC,KAAK,IAAI,CAAA;IACxE,cAAc,EAAE,CAAC,WAAW,EAAE,UAAU,KAAK,IAAI,CAAA;IACjD,YAAY,EAAE,CAAC,IAAI,EAAE;QAAE,YAAY,EAAE,kBAAkB,CAAA;KAAE,KAAK,IAAI,CAAA;CAClE,CAAA;AAED,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,sBAAsB,GAC7B,qBAAqB,CAsYvB"}
@@ -1,19 +1,13 @@
1
1
  import { DEFAULT_CONTRAST_MODE, } from "./protocol/contrast.js";
2
- import { sandboxToHostMessageSchema } from "./protocol/messages/sandboxToHost.js";
3
2
  import { CUSTOM_BLOCK_BRIDGE_PROTOCOL_VERSION } from "./protocol/protocolVersion.js";
4
- import * as v from "valibot";
5
3
  import { initErrorForFailureReason } from "./lifecycle/initErrors.js";
6
4
  import { createInitializationController } from "./lifecycle/initializationController.js";
7
- import { getInvalidSandboxMessageResponse } from "./messages/invalidSandboxMessage.js";
5
+ import { createSandboxMessageRouter } from "./messages/sandboxMessageRouter.js";
8
6
  import { QuerySubscriptions, } from "./queries/querySubscriptions.js";
9
7
  import { validateQueryDataSourceFilter } from "./queries/validateQueryDataSourceFilter.js";
10
8
  import { validateQueryDataSourceSorts } from "./queries/validateQueryDataSourceSorts.js";
11
- import { unreachable } from "./utils.js";
12
- const initializationIdentitySchema = v.object({
13
- initializationId: v.string(),
14
- });
15
9
  export function createCustomBlockHost(options) {
16
- const { iframe, initialState, handlers, onConnect, onInitResult, onInitializationFailure, onIgnoredConnect, onLog, sandboxOrigin, minBridgeProtocolVersion: configuredMinBridgeProtocolVersion, noConnectTimeoutMs, noInitResultTimeoutMs, } = options;
10
+ const { iframe, initialState, handlers, onManifest, onInitialized, onInitializationFailure, onRestartRequired, onLog, sandboxOrigin, minBridgeProtocolVersion: configuredMinBridgeProtocolVersion, noConnectTimeoutMs, noInitResultTimeoutMs, } = options;
17
11
  const minBridgeProtocolVersion = Math.max(configuredMinBridgeProtocolVersion ?? CUSTOM_BLOCK_BRIDGE_PROTOCOL_VERSION, CUSTOM_BLOCK_BRIDGE_PROTOCOL_VERSION);
18
12
  let theme = initialState.theme;
19
13
  let contrastMode = initialState.contrastMode ?? DEFAULT_CONTRAST_MODE;
@@ -188,6 +182,13 @@ export function createCustomBlockHost(options) {
188
182
  }),
189
183
  prepareInit(connect) {
190
184
  manifest = connect.manifest ?? initialState.manifest;
185
+ if (manifest === undefined) {
186
+ return {
187
+ status: "error",
188
+ error: initErrorForFailureReason("manifest_unavailable"),
189
+ };
190
+ }
191
+ onManifest?.(manifest);
191
192
  const resolvedDataSources = typeof dataSources === "function" ? dataSources(connect) : dataSources;
192
193
  const bindingErrorCode = getBindingErrorCode(manifest, resolvedDataSources);
193
194
  if (bindingErrorCode !== undefined) {
@@ -209,106 +210,33 @@ export function createCustomBlockHost(options) {
209
210
  minBridgeProtocolVersion,
210
211
  noConnectTimeoutMs,
211
212
  noInitResultTimeoutMs,
212
- onConnect,
213
- onInitResult,
214
- onIgnoredConnect,
213
+ onInitResult(message) {
214
+ if (message.status === "success" && manifest !== undefined) {
215
+ onInitialized?.({ manifest, initialHeight: message.initialHeight });
216
+ }
217
+ },
218
+ onRestartRequired,
215
219
  onInitializationFailure,
216
220
  });
217
- function onInvalidSandboxMessage(data, issues) {
218
- emit("warn", {
219
- message: "Ignored unsupported sandbox message",
220
- payload: data,
221
- issues,
222
- });
223
- const response = getInvalidSandboxMessageResponse(data);
224
- const parsedIdentity = v.safeParse(initializationIdentitySchema, data);
225
- const initializationId = parsedIdentity.success
226
- ? parsedIdentity.output.initializationId
227
- : undefined;
228
- if (response.incomingType === "connect") {
229
- if (initializationController.handleInvalidConnect(initializationId)) {
230
- if (initializationId === undefined && response.nack !== undefined) {
231
- post(response.nack);
232
- }
233
- return;
234
- }
235
- }
236
- if (response.incomingType === "initResult" &&
237
- initializationController.handleInvalidInitResult()) {
238
- if (response.nack !== undefined) {
239
- post(response.nack);
240
- }
241
- return;
242
- }
243
- if (response.nack !== undefined) {
244
- post(response.nack);
245
- }
246
- }
247
- function onMessage(event) {
248
- // Ignore messages from windows other than this host's iframe.
249
- if (event.source !== iframe.contentWindow) {
250
- return;
251
- }
252
- // Ignore messages that do not come from the configured sandbox origin.
253
- if (event.origin !== sandboxOrigin) {
254
- return;
255
- }
256
- const parsed = v.safeParse(sandboxToHostMessageSchema, event.data);
257
- if (!parsed.success) {
258
- onInvalidSandboxMessage(event.data, parsed.issues);
259
- return;
260
- }
261
- const message = parsed.output;
262
- emit("in", message);
263
- if (message.type !== "connect" &&
264
- message.type !== "initResult" &&
265
- initializationController.status !== "success") {
266
- // Ignore all non-handshake messages until initialization succeeds.
267
- if (message.type !== "resize") {
268
- emit("warn", {
269
- message: `Ignored ${message.type} before initialization completed`,
270
- payload: message,
271
- });
272
- }
273
- return;
274
- }
275
- switch (message.type) {
276
- case "connect":
277
- initializationController.handleConnect(message);
278
- return;
279
- case "initResult":
280
- initializationController.handleInitResult(message);
281
- return;
282
- case "queryDataSource":
283
- void handleQuery(message);
284
- return;
285
- case "unsubscribeDataSourceQuery":
286
- querySubscriptions.unsubscribe(message.subscriptionId);
287
- return;
288
- case "createPage":
289
- void handleCreatePage(message);
290
- return;
291
- case "getPage":
292
- void handleGetPage(message);
293
- return;
294
- case "getUser":
295
- void handleGetUser(message);
296
- return;
297
- case "listUsers":
298
- void handleListUsers(message);
299
- return;
300
- case "updatePage":
301
- void handleUpdatePage(message);
302
- return;
303
- case "resize":
304
- handlers.resize?.(message);
305
- return;
306
- case "invalidHostMessage":
307
- return;
308
- default:
309
- unreachable(message);
310
- }
311
- }
221
+ const onMessage = createSandboxMessageRouter({
222
+ iframe,
223
+ sandboxOrigin,
224
+ getInitStatus: () => initializationController.status,
225
+ emit,
226
+ post,
227
+ onInvalidConnect: initializationController.handleInvalidConnect,
228
+ onInvalidInitResult: initializationController.handleInvalidInitResult,
229
+ onConnect: initializationController.handleConnect,
230
+ onInitResult: initializationController.handleInitResult,
231
+ onQueryDataSource: handleQuery,
232
+ onUnsubscribeDataSourceQuery: message => querySubscriptions.unsubscribe(message.subscriptionId),
233
+ onCreatePage: handleCreatePage,
234
+ onGetPage: handleGetPage,
235
+ onGetUser: handleGetUser,
236
+ onListUsers: handleListUsers,
237
+ onUpdatePage: handleUpdatePage,
238
+ onResize: message => handlers.resize?.(message),
239
+ });
312
240
  window.addEventListener("message", onMessage);
313
241
  iframe.addEventListener("load", initializationController.onIframeLoad);
314
242
  return {
@@ -368,6 +296,7 @@ export function createCustomBlockHost(options) {
368
296
  },
369
297
  setDataSources(nextDataSources) {
370
298
  if (initializationController.hasConnectedSandbox &&
299
+ manifest !== undefined &&
371
300
  getBindingErrorCode(manifest, nextDataSources) !== undefined) {
372
301
  emit("warn", {
373
302
  message: "Ignored invalid data source bindings update",
@@ -36,9 +36,8 @@ export type InitializationControllerOptions = {
36
36
  minBridgeProtocolVersion: number;
37
37
  noConnectTimeoutMs?: number;
38
38
  noInitResultTimeoutMs?: number;
39
- onConnect?: (message: ConnectSuccessMessage) => void;
40
39
  onInitResult?: (message: InitResultMessage) => void;
41
- onIgnoredConnect?: (message: ConnectMessage) => void;
40
+ onRestartRequired?: () => void;
42
41
  onInitializationFailure?: (error: CustomBlockHostInitializationFailure) => void;
43
42
  };
44
43
  export type InitializationController = {
@@ -46,8 +45,14 @@ export type InitializationController = {
46
45
  readonly hasConnectedSandbox: boolean;
47
46
  handleConnect: (message: ConnectMessage) => void;
48
47
  handleInitResult: (message: InitResultMessage) => void;
49
- handleInvalidConnect: (initializationId?: string) => boolean;
50
- handleInvalidInitResult: () => boolean;
48
+ handleInvalidConnect: (args: {
49
+ initializationId: string;
50
+ error: CustomBlockInitErrorInfo;
51
+ }) => "initErrorSent" | "nack";
52
+ handleInvalidInitResult: (args: {
53
+ initializationId: string | undefined;
54
+ error: CustomBlockInitErrorInfo;
55
+ }) => void;
51
56
  onIframeLoad: () => void;
52
57
  stop: () => void;
53
58
  };
@@ -1 +1 @@
1
- {"version":3,"file":"initializationController.d.ts","sourceRoot":"","sources":["../../src/lifecycle/initializationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8CAA8C,CAAA;AACtF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAA;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAA;AACvF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4DAA4D,CAAA;AACtG,OAAO,KAAK,EACX,wBAAwB,EAExB,MAAM,mDAAmD,CAAA;AAC1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAChG,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gDAAgD,CAAA;AACrF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4CAA4C,CAAA;AAC9E,OAAO,KAAK,EAAE,W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C,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAA;IACpD,uBAAuB,CAAC,EAAE,CACzB,KAAK,EAAE,oCAAoC,KACvC,IAAI,CAAA;CACT,CAAA;AAED,MAAM,MAAM,wBAAwB,GAAG;IACtC,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAA;IAC3B,QAAQ,CAAC,mBAAmB,EAAE,OAAO,CAAA;IACrC,aAAa,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAA;IAChD,gBAAgB,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,IAAI,CAAA;IACtD,oBAAoB,EAAE,CAAC,gBAAgB,CAAC,EAAE,MAAM,KAAK,OAAO,CAAA;IAC5D,uBAAuB,EAAE,MAAM,OAAO,CAAA;IACtC,YAAY,EAAE,MAAM,IAAI,CAAA;IACxB,IAAI,EAAE,MAAM,IAAI,CAAA;CAChB,CAAA;AAED,wBAAgB,8BAA8B,CAC7C,OAAO,EAAE,+BAA+B,GACtC,wBAAwB,CAyO1B"}
1
+ {"version":3,"file":"initializationController.d.ts","sourceRoot":"","sources":["../../src/lifecycle/initializationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8CAA8C,CAAA;AACtF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAA;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAA;AACvF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4DAA4D,CAAA;AACtG,OAAO,KAAK,EACX,wBAAwB,EAExB,MAAM,mDAAmD,CAAA;AAC1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAChG,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gDAAgD,CAAA;AACrF,OAAO,KAAK,EAAE,YAAY,EAAE,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AAA;IAC9B,uBAAuB,CAAC,EAAE,CACzB,KAAK,EAAE,oCAAoC,KACvC,IAAI,CAAA;CACT,CAAA;AAED,MAAM,MAAM,wBAAwB,GAAG;IACtC,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAA;IAC3B,QAAQ,CAAC,mBAAmB,EAAE,OAAO,CAAA;IACrC,aAAa,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,CAAA;IAChD,gBAAgB,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,IAAI,CAAA;IACtD,oBAAoB,EAAE,CAAC,IAAI,EAAE;QAC5B,gBAAgB,EAAE,MAAM,CAAA;QACxB,KAAK,EAAE,wBAAwB,CAAA;KAC/B,KAAK,eAAe,GAAG,MAAM,CAAA;IAC9B,uBAAuB,EAAE,CAAC,IAAI,EAAE;QAC/B,gBAAgB,EAAE,MAAM,GAAG,SAAS,CAAA;QACpC,KAAK,EAAE,wBAAwB,CAAA;KAC/B,KAAK,IAAI,CAAA;IACV,YAAY,EAAE,MAAM,IAAI,CAAA;IACxB,IAAI,EAAE,MAAM,IAAI,CAAA;CAChB,CAAA;AAED,wBAAgB,8BAA8B,CAC7C,OAAO,EAAE,+BAA+B,GACtC,wBAAwB,CAyP1B"}
@@ -3,7 +3,7 @@ import { isValidBridgeProtocolVersion } from "./protocolVersion.js";
3
3
  const DEFAULT_NO_CONNECT_TIMEOUT_MS = 5_000;
4
4
  const DEFAULT_NO_INIT_RESULT_TIMEOUT_MS = 5_000;
5
5
  export function createInitializationController(options) {
6
- const { blockId, getState, prepareInit, post, warn, flushPendingHostStateChanges, minBridgeProtocolVersion, noConnectTimeoutMs = DEFAULT_NO_CONNECT_TIMEOUT_MS, noInitResultTimeoutMs = DEFAULT_NO_INIT_RESULT_TIMEOUT_MS, onConnect, onInitResult, onIgnoredConnect, onInitializationFailure, } = options;
6
+ const { blockId, getState, prepareInit, post, warn, flushPendingHostStateChanges, minBridgeProtocolVersion, noConnectTimeoutMs = DEFAULT_NO_CONNECT_TIMEOUT_MS, noInitResultTimeoutMs = DEFAULT_NO_INIT_RESULT_TIMEOUT_MS, onInitResult, onRestartRequired, onInitializationFailure, } = options;
7
7
  let initStatus = "waitingForConnect";
8
8
  let noConnectTimeoutId;
9
9
  let noInitResultTimeoutId;
@@ -21,7 +21,8 @@ export function createInitializationController(options) {
21
21
  noInitResultTimeoutId = undefined;
22
22
  }
23
23
  }
24
- function sendInitError(initializationId, error) {
24
+ function sendInitError(args) {
25
+ const { initializationId, error } = args;
25
26
  if (initStatus !== "waitingForConnect") {
26
27
  return;
27
28
  }
@@ -40,10 +41,12 @@ export function createInitializationController(options) {
40
41
  if (initStatus !== "waitingForConnect") {
41
42
  return false;
42
43
  }
43
- onConnect?.(connect);
44
44
  const preparation = prepareInit(connect);
45
45
  if (preparation.status === "error") {
46
- sendInitError(connect.initializationId, preparation.error);
46
+ sendInitError({
47
+ initializationId: connect.initializationId,
48
+ error: preparation.error,
49
+ });
47
50
  return false;
48
51
  }
49
52
  initStatus = "waitingForInitResult";
@@ -89,7 +92,7 @@ export function createInitializationController(options) {
89
92
  message: "Ignored connect after initialization settled; recreate the host and iframe to re-initialize",
90
93
  initializationId: message.initializationId,
91
94
  });
92
- onIgnoredConnect?.(message);
95
+ onRestartRequired?.();
93
96
  return;
94
97
  }
95
98
  // Verify the bridge protocol version is valid.
@@ -97,7 +100,10 @@ export function createInitializationController(options) {
97
100
  const initError = initErrorForFailureReason("invalid_protocol_version", {
98
101
  currentBridgeProtocolVersion: message.bridgeProtocolVersion,
99
102
  });
100
- sendInitError(message.initializationId, initError);
103
+ sendInitError({
104
+ initializationId: message.initializationId,
105
+ error: initError,
106
+ });
101
107
  return;
102
108
  }
103
109
  if (message.bridgeProtocolVersion < minBridgeProtocolVersion) {
@@ -105,44 +111,46 @@ export function createInitializationController(options) {
105
111
  currentBridgeProtocolVersion: message.bridgeProtocolVersion,
106
112
  minBridgeProtocolVersion,
107
113
  });
108
- sendInitError(message.initializationId, initError);
114
+ sendInitError({
115
+ initializationId: message.initializationId,
116
+ error: initError,
117
+ });
109
118
  return;
110
119
  }
111
120
  // If the sandbox reported an error during `connect`, return it as an `init` error.
112
121
  if (message.status === "error") {
113
- sendInitError(message.initializationId, message.error);
122
+ sendInitError({
123
+ initializationId: message.initializationId,
124
+ error: message.error,
125
+ });
114
126
  return;
115
127
  }
116
128
  if (sendInit(message)) {
117
129
  hasConnectedSandbox = true;
118
130
  }
119
131
  }
120
- function handleInvalidConnect(initializationId) {
132
+ function handleInvalidConnect(args) {
133
+ const { initializationId, error } = args;
121
134
  if (initStatus !== "waitingForConnect") {
122
- return false;
135
+ // `connect` messages received when not waiting for one are NACKed like generic invalid messages.
136
+ return "nack";
123
137
  }
124
- const error = initErrorForFailureReason("invalid_connect_payload");
125
- if (initializationId === undefined) {
126
- initStatus = "error";
127
- clearNoConnectTimeout();
128
- warn(error);
129
- onInitializationFailure?.(error);
130
- }
131
- else {
132
- sendInitError(initializationId, error);
133
- }
134
- return true;
138
+ // The sandbox awaits `init`. Reply with `init.error` using its initialization ID.
139
+ sendInitError({ initializationId, error });
140
+ return "initErrorSent";
135
141
  }
136
- function handleInvalidInitResult() {
137
- if (initStatus !== "waitingForInitResult") {
138
- return false;
142
+ function handleInvalidInitResult(args) {
143
+ const { initializationId, error } = args;
144
+ if (initStatus !== "waitingForInitResult" ||
145
+ initializationId === undefined ||
146
+ initializationId !== activeInitializationId) {
147
+ return;
139
148
  }
149
+ clearNoConnectTimeout();
140
150
  clearNoInitResultTimeout();
141
151
  initStatus = "error";
142
- const error = initErrorForFailureReason("invalid_init_result_payload");
143
152
  warn(error);
144
153
  onInitializationFailure?.(error);
145
- return true;
146
154
  }
147
155
  function onIframeLoad() {
148
156
  clearNoConnectTimeout();
@@ -21,7 +21,11 @@ export type CustomBlockHostInitialState = {
21
21
  blockId: NotionBlockId;
22
22
  parent: NotionParent;
23
23
  page: CustomBlockPage;
24
- manifest: CustomBlockManifest;
24
+ /**
25
+ * The host's persisted manifest, when one exists. If omitted, the host
26
+ * requires the sandbox to provide a manifest in `connect`.
27
+ */
28
+ manifest?: CustomBlockManifest;
25
29
  dataSources: CustomBlockHostInitialDataSources;
26
30
  currentUser: NotionUser;
27
31
  };
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lifecycle/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8CAA8C,CAAA;AACtF,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,4DAA4D,CAAA;AAC1G,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAA;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8CAA8C,CAAA;AACvF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gDAAgD,CAAA;AACrF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4CAA4C,CAAA;AAC9E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2CAA2C,CAAA;AAC5E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gDAAgD,CAAA;AAEhF,MAAM,MAAM,qBAAqB,GAAG,OAAO,CAC1C,cAAc,EACd;IAAE,MAAM,EAAE,SAAS,CAAA;CAAE,CACrB,CAAA;AAED,MAAM,MAAM,UAAU,GACnB,mBAAmB,GACnB,sBAAsB,GACtB,SAAS,GACT,OAAO,CAAA;AAEV,MAAM,MAAM,iCAAiC,GAAG;IAC/C,QAAQ,EAAE,wBAAwB,CAAA;CAClC,CAAA;AAED,MAAM,MAAM,iCAAiC,GAC1C,iCAAiC,GACjC,CAAC,CAAC,OAAO,EAAE,qBAAqB,KAAK,iCAAiC,CAAC,CAAA;AAE1E,MAAM,MAAM,2BAA2B,GAAG;IACzC,KAAK,EAAE,WAAW,CAAA;IAElB,YAAY,CAAC,EAAE,kBAAkB,CAAA;IACjC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ElB,YAAY,CAAC,EAAE,kBAAkB,CAAA;IACjC,OAAO,EAAE,aAAa,CAAA;IACtB,MAAM,EAAE,YAAY,CAAA;IACpB,IAAI,EAAE,eAAe,CAAA;IACrB;;;OAGG;IACH,QAAQ,CAAC,EAAE,mBAAmB,CAAA;IAC9B,WAAW,EAAE,iCAAiC,CAAA;IAC9C,WAAW,EAAE,UAAU,CAAA;CACvB,CAAA"}
@@ -1,4 +1,4 @@
1
- import { readIncomingType } from "../protocol/incomingType.js";
1
+ import { readIncomingType } from "../protocol/messages/incomingType.js";
2
2
  export function getInvalidSandboxMessageResponse(data) {
3
3
  const incomingType = readIncomingType(data);
4
4
  if (incomingType === "invalidHostMessage" ||
@@ -0,0 +1,35 @@
1
+ import type { ConnectMessage } from "../protocol/messages/connect.js";
2
+ import type { CreatePageMessage } from "../protocol/messages/createPage.js";
3
+ import type { GetPageMessage } from "../protocol/messages/getPage.js";
4
+ import type { GetUserMessage } from "../protocol/messages/getUser.js";
5
+ import type { HostToSandboxMessage } from "../protocol/messages/hostToSandbox.js";
6
+ import type { InitResultMessage } from "../protocol/messages/initResult.js";
7
+ import type { ListUsersMessage } from "../protocol/messages/listUsers.js";
8
+ import type { QueryDataSourceMessage } from "../protocol/messages/queryDataSource.js";
9
+ import type { ResizeMessage } from "../protocol/messages/resize.js";
10
+ import type { UnsubscribeDataSourceQueryMessage } from "../protocol/messages/unsubscribeDataSourceQuery.js";
11
+ import type { UpdatePageMessage } from "../protocol/messages/updatePage.js";
12
+ import type { InitializationController } from "../lifecycle/initializationController.js";
13
+ import type { InitStatus } from "../lifecycle/types.js";
14
+ import type { CustomBlockHostLogDirection } from "./types.js";
15
+ export type SandboxMessageRouterOptions = {
16
+ iframe: HTMLIFrameElement;
17
+ sandboxOrigin: string;
18
+ getInitStatus: () => InitStatus;
19
+ emit: (direction: CustomBlockHostLogDirection, payload: unknown) => void;
20
+ post: (message: HostToSandboxMessage) => void;
21
+ onInvalidConnect: InitializationController["handleInvalidConnect"];
22
+ onInvalidInitResult: InitializationController["handleInvalidInitResult"];
23
+ onConnect: (message: ConnectMessage) => void;
24
+ onInitResult: (message: InitResultMessage) => void;
25
+ onQueryDataSource: (message: QueryDataSourceMessage) => void | Promise<void>;
26
+ onUnsubscribeDataSourceQuery: (message: UnsubscribeDataSourceQueryMessage) => void;
27
+ onCreatePage: (message: CreatePageMessage) => void | Promise<void>;
28
+ onGetPage: (message: GetPageMessage) => void | Promise<void>;
29
+ onGetUser: (message: GetUserMessage) => void | Promise<void>;
30
+ onListUsers: (message: ListUsersMessage) => void | Promise<void>;
31
+ onUpdatePage: (message: UpdatePageMessage) => void | Promise<void>;
32
+ onResize: (message: ResizeMessage) => void;
33
+ };
34
+ export declare function createSandboxMessageRouter(options: SandboxMessageRouterOptions): (event: MessageEvent) => void;
35
+ //# sourceMappingURL=sandboxMessageRouter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sandboxMessageRouter.d.ts","sourceRoot":"","sources":["../../src/messages/sandboxMessageRouter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAChG,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sDAAsD,CAAA;AAC1F,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4DAA4D,CAAA;AACtG,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yDAAyD,CAAA;AAEhG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wDAAwD,CAAA;AAC9F,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,8DAA8D,CAAA;AAC1G,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qDAAqD,CAAA;AAExF,OAAO,KAAK,EAAE,iCAAiC,EAAE,MAAM,yEAAyE,CAA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