@notionhq/custom-blocks-dev-shell 0.4.1 → 0.4.3

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.
@@ -1,145 +0,0 @@
1
- # Host initialization and lifecycle
2
-
3
- The host prepares setup values and manages the connection until the iframe is removed. The
4
- [embed example](../README.md#create-a-host) shows how to use the lifecycle callbacks. See
5
- [Connection compatibility](compatibility.md) for protocol negotiation and version requirements.
6
-
7
- ## Manifest and initial state
8
-
9
- Pass the full block URL as `sandboxUrl`. The host derives the origin used to authenticate incoming
10
- messages and target outgoing messages. `CustomBlockManifestSource` selects the manifest:
11
-
12
- | `manifestSource` | Manifest source |
13
- | ---------------- | ---------------------------------------------------------------------------------------------------------- |
14
- | `"host"` | The manifest returned by `getPersistedManifest({ signal })` |
15
- | `"sandbox"` | The local declaration supplied in `connect.manifest`, subject to the [local URL policy](#local-block-urls) |
16
-
17
- `getPersistedManifest` is required in both modes and starts when the host is created. It returns a
18
- `CustomBlockHostHandlerResult` with either `{ status: "success", manifest }` or
19
- `{ status: "error", error }`. The manifest follows `manifestSchema` from the protocol subpath. The
20
- callback must not resolve to `null` or `undefined`. A definition that is still loading keeps the
21
- promise pending. A failed load returns an error result. The host validates the result and normalizes
22
- the manifest before data reads. Omitted `dataSources` and each source's omitted `properties` default
23
- to `{}`.
24
-
25
- The sandbox loads independently and sends `connect`. In sandbox mode, the SDK reads the manifest
26
- from JSON metadata injected into the local block HTML by dev tooling. The host validates the
27
- declaration in `connect`. The host does not wait for the unselected manifest source. Sandbox mode
28
- does not fall back to the persisted manifest. The
29
- [wire reference](../../bridge/protocol.md#initialization) describes the declaration and its
30
- validation errors.
31
-
32
- Supply initial `theme`, `contrastMode`, `containingPage`, and `parent` as required constructor
33
- options, using `null` for values that have not loaded yet. Set resolved values with the
34
- corresponding setters.
35
-
36
- Setup waits for the selected manifest, theme, and contrast mode. The host sends the fixed `instance`
37
- identity, the manifest in `init.manifest`, and appearance values in `init.preferences`.
38
-
39
- Parent and containing page values do not delay setup. After the sandbox acknowledges setup, the host
40
- sends any resolved parent and containing page values. The host sends unresolved values when their
41
- setters supply them, then sends later changes. Setters accept resolved values only. For
42
- `containingPage`, `{ type: "unavailable" }` means that the containing page is inaccessible. It is
43
- distinct from `null`, which means that the value has not loaded.
44
-
45
- Profile and source metadata reads run independently after setup. Their failures do not fail setup or
46
- unrelated operations. See [Current viewer](host-contract.md#current-viewer) and
47
- [Metadata and configuration](host-contract.md#metadata-and-configuration) for the provider
48
- contracts.
49
-
50
- Errors from the selected manifest source retain their `code`, `message`, and `isRetryable`. A
51
- missing sandbox declaration produces `manifest_unavailable`. Unexpected exceptions or invalid
52
- results during preparation produce `unknown_error`. Diagnostics go to `onLog("warn", payload)`.
53
- Stopping aborts preparation and suppresses late results without a cancellation warning.
54
-
55
- ## Timeouts
56
-
57
- | Option | Waits for | Default | Failure code |
58
- | ----------------------- | ------------------------------------- | ---------- | ----------------------- |
59
- | `noConnectTimeoutMs` | `connect` after the first iframe load | 5 seconds | `no_connect` |
60
- | `initialStateTimeoutMs` | State preparation after `connect` | 5 seconds | `initial_state_timeout` |
61
- | `noInitResultTimeoutMs` | `initResult` after `init` | 5 seconds | `no_init_result` |
62
- | `noReadyTimeoutMs` | `ready` after successful `initResult` | 60 seconds | `ready_timeout` |
63
-
64
- `initialStateTimeoutMs` starts when the host accepts `connect`. It includes the remaining wait for
65
- the selected manifest, theme, and contrast mode. Parent and containing page loading do not count
66
- toward this timeout.
67
-
68
- A successful `initResult` permits data operations while the host waits for `ready`. The callbacks
69
- `getPersistedManifest`, `handlers.getUser`, and `handlers.getDataSource` must respect their
70
- `signal`. Stopping the host aborts pending work and suppresses late results.
71
-
72
- ## Live state
73
-
74
- `setTheme()`, `setContrastMode()`, `setParent()`, and `setContainingPage()` retain the latest value
75
- during setup and send updates after a successful `initResult`. Use `setBindings()` and
76
- `refreshDataSource()` to refresh source configuration. Each host instance belongs to one viewer.
77
- Stop it and create a fresh host and iframe when the viewer changes. See
78
- [updates](host-contract.md#updates) for their results and cancellation behavior.
79
-
80
- ## Callbacks
81
-
82
- The required `onStatusChange(state)` callback receives the `CustomBlockHostStatus` union:
83
-
84
- | Status | Behavior | Payload |
85
- | ---------------------- | ------------------------------------------------------------- | ------------------------------------------------ |
86
- | `waitingForConnect` | The host is listening, reported synchronously during creation | None |
87
- | `preparingInit` | The host accepted the protocol offer and is preparing setup | `localManifest`, `sdkVersion`, `protocolVersion` |
88
- | `waitingForInitResult` | The host sent `init` and is waiting for setup acknowledgment | None |
89
- | `waitingForReady` | Setup succeeded and the host is waiting for the first render | None |
90
- | `ready` | The sandbox reported that its first render is ready | `resolvedManifest`, optional `initialHeight` |
91
- | `failed` | Initialization or runtime failed | `error` |
92
- | `stopped` | The application explicitly disposed of the host | None |
93
-
94
- `localManifest` is optional and comes only from a permitted local sandbox. It is available before
95
- initialization completes, so the application can use its requirements to prepare bindings.
96
- `resolvedManifest` is the selected declaration sent in `init`. Rejected connections can skip
97
- intermediate statuses. Both `failed` and `stopped` are terminal. Stopping a failed host releases
98
- resources without replacing its failure status.
99
-
100
- Connection failures contain `{ phase, source, code, message, isRetryable }`. The phase is `connect`,
101
- `init`, `initResult`, `ready`, or `runtime`. The source identifies who reported the failure.
102
- Failures can occur after setup succeeds.
103
-
104
- During setup, use the failure code and the bridge log to locate the failed exchange.
105
-
106
- | Code | Meaning |
107
- | ------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
108
- | `invalid_connect_payload` | The host could read the `connect` request ID but could not validate the request. The host sends a failed `connectResult`. |
109
- | `invalid_init_payload` | The sandbox could read the `init` request ID but could not validate the setup data. The sandbox sends a failed `initResult`. |
110
-
111
- If the host receives no valid setup acknowledgment before `noInitResultTimeoutMs`, it reports
112
- `no_init_result`. A malformed acknowledgment leaves that timer running. See the
113
- [wire reference](../../bridge/protocol.md#validation-and-errors) for message validation and
114
- request matching.
115
-
116
- At `ready`, apply a supplied `initialHeight` in CSS pixels before revealing the iframe. If the
117
- height is absent, use your application's default size. Resize messages can arrive after setup
118
- succeeds. Apply height changes without revealing the iframe until `ready`. Reveal, sizing, and
119
- failure UI belong to the embedding application.
120
-
121
- The optional `onLog(direction, payload)` callback receives bridge messages and warnings.
122
-
123
- ## Iframe lifetime
124
-
125
- Each host handles one handshake. Create a new host and iframe to retry or replace the block URL. The
126
- [wire reference](../../bridge/protocol.md#initialization) defines handshake and validation
127
- errors.
128
-
129
- The first observed iframe `load` is allowed. A second load stops the host and reports retryable
130
- `iframe_reloaded`. The host detects navigation at the `load` event. Until that event, a replacement
131
- document at the same origin can receive messages and invoke handlers.
132
-
133
- `stop()` removes listeners, cancels preparation, clears timers, ends subscriptions, and suppresses
134
- pending responses. Call it when removing the iframe.
135
-
136
- ## Local block URLs
137
-
138
- `isAllowedLocalPreviewUrl({ url, hostOrigin })` accepts HTTP or HTTPS URLs on `localhost`,
139
- `*.localhost`, or `127.0.0.1` without credentials. It rejects the embedding page's origin.
140
- `hostOrigin` must be nonempty. Pass the embedding page's serialized origin.
141
-
142
- The protocol helper `isLocalBlockServerUrl(url)` checks the server URL without requiring origin
143
- isolation. The host adds the restriction on the embedding origin. `manifestSource: "sandbox"`
144
- enforces this policy before the host starts. An invalid URL for sandbox mode throws
145
- `CustomBlockInitializationError` with code `invalid_local_preview_url`.