@notionhq/custom-blocks 0.6.0 → 0.7.0

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 (191) hide show
  1. package/README.md +32 -18
  2. package/dist/bridge/SandboxBridge.d.ts +7 -8
  3. package/dist/bridge/SandboxBridge.d.ts.map +1 -1
  4. package/dist/bridge/SandboxBridge.js +39 -114
  5. package/dist/bridge/applyHostStateMessage.d.ts.map +1 -1
  6. package/dist/bridge/createBrowserSandboxBridge.d.ts +3 -0
  7. package/dist/bridge/createBrowserSandboxBridge.d.ts.map +1 -0
  8. package/dist/bridge/createBrowserSandboxBridge.js +54 -0
  9. package/dist/bridge/getBridge.d.ts +1 -1
  10. package/dist/bridge/getBridge.d.ts.map +1 -1
  11. package/dist/bridge/getBridge.js +2 -2
  12. package/dist/bridge/getInitializedHostState.d.ts.map +1 -1
  13. package/dist/bridge/hostState.d.ts +2 -2
  14. package/dist/bridge/hostState.d.ts.map +1 -1
  15. package/dist/bridge/initialization/SandboxInitialization.d.ts +6 -0
  16. package/dist/bridge/initialization/SandboxInitialization.d.ts.map +1 -1
  17. package/dist/bridge/initialization/SandboxInitialization.js +13 -10
  18. package/dist/bridge/messageInterceptor.d.ts.map +1 -1
  19. package/dist/bridge/requests.d.ts.map +1 -1
  20. package/dist/bridge/sandboxEnvironment.d.ts +30 -0
  21. package/dist/bridge/sandboxEnvironment.d.ts.map +1 -0
  22. package/dist/bridge/sandboxEnvironment.js +1 -0
  23. package/dist/bridge/subscribeToHostStateField.d.ts.map +1 -1
  24. package/dist/bridge.d.ts.map +1 -1
  25. package/dist/data-sources.d.ts.map +1 -1
  26. package/dist/dataSources/NotionDataSource.d.ts.map +1 -1
  27. package/dist/dataSources/bridge/DataSourceCache.d.ts +18 -0
  28. package/dist/dataSources/bridge/DataSourceCache.d.ts.map +1 -0
  29. package/dist/dataSources/bridge/DataSourceCache.js +75 -0
  30. package/dist/dataSources/bridge/applyDataSourceBindings.d.ts.map +1 -1
  31. package/dist/dataSources/bridge/makeDataSourceReadResponseSchema.d.ts.map +1 -1
  32. package/dist/dataSources/bridge/resolveDataSourceQuery.d.ts.map +1 -1
  33. package/dist/dataSources/bridge/resolveDataSources.d.ts.map +1 -1
  34. package/dist/dataSources/bridge/resolveSorts.d.ts.map +1 -1
  35. package/dist/dataSources/bridge/reuseDataSourcesForUnchangedBindings.d.ts +0 -3
  36. package/dist/dataSources/bridge/reuseDataSourcesForUnchangedBindings.d.ts.map +1 -1
  37. package/dist/dataSources/bridge/reuseDataSourcesForUnchangedBindings.js +2 -5
  38. package/dist/dataSources/dataSourceInaccessibleError.d.ts.map +1 -1
  39. package/dist/dataSources/dataSourceState.d.ts.map +1 -1
  40. package/dist/dataSources/ensureDataSource.d.ts.map +1 -1
  41. package/dist/dataSources/filters/resolveFilter.d.ts.map +1 -1
  42. package/dist/dataSources/filters/types.d.ts.map +1 -1
  43. package/dist/dataSources/getDataSourcePage.d.ts.map +1 -1
  44. package/dist/dataSources/observeDataSource.d.ts.map +1 -1
  45. package/dist/dataSources/properties/resolvePropertyAddress.d.ts.map +1 -1
  46. package/dist/dataSources/properties/resolvePropertyWriteMapForDataSource.d.ts.map +1 -1
  47. package/dist/dataSources/properties/resolvePropertyWriteMapForDataSource.js +2 -2
  48. package/dist/dataSources/resolvePageDataSource.d.ts.map +1 -1
  49. package/dist/dataSources/subscribeToDataSource.d.ts.map +1 -1
  50. package/dist/dataSources/types.d.ts.map +1 -1
  51. package/dist/files/files.d.ts +1 -1
  52. package/dist/files/files.d.ts.map +1 -1
  53. package/dist/files/icons.d.ts.map +1 -1
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/initialization/NotInIframeError.d.ts.map +1 -1
  56. package/dist/initialization/initCustomBlock.d.ts.map +1 -1
  57. package/dist/initialization/notifyReady.d.ts.map +1 -1
  58. package/dist/initialization/react/useCustomBlockInit.d.ts.map +1 -1
  59. package/dist/initialization/resolveInitialState.d.ts.map +1 -1
  60. package/dist/initialization/types.d.ts.map +1 -1
  61. package/dist/layout/autoResize.d.ts.map +1 -1
  62. package/dist/layout/getInitialContentHeight.d.ts.map +1 -1
  63. package/dist/location/getContainingPage.d.ts.map +1 -1
  64. package/dist/location/getInstance.d.ts.map +1 -1
  65. package/dist/location/getParent.d.ts.map +1 -1
  66. package/dist/location/mapContainingPage.d.ts +2 -2
  67. package/dist/location/mapContainingPage.d.ts.map +1 -1
  68. package/dist/location/mapParent.d.ts.map +1 -1
  69. package/dist/location/subscribeToContainingPage.d.ts.map +1 -1
  70. package/dist/location/subscribeToParent.d.ts.map +1 -1
  71. package/dist/location/types.d.ts.map +1 -1
  72. package/dist/manifest/getManifest.d.ts.map +1 -1
  73. package/dist/manifest/readLocalManifest.d.ts.map +1 -1
  74. package/dist/manifest/types.d.ts.map +1 -1
  75. package/dist/navigation/openExternalUrl.d.ts.map +1 -1
  76. package/dist/navigation/types.d.ts.map +1 -1
  77. package/dist/navigation.d.ts.map +1 -1
  78. package/dist/pages/NotionPage.d.ts.map +1 -1
  79. package/dist/pages/bridge/buildCreatePageRequest.d.ts.map +1 -1
  80. package/dist/pages/bridge/getPageForDataSource.d.ts.map +1 -1
  81. package/dist/pages/bridge/resolveCreatePageDestination.d.ts.map +1 -1
  82. package/dist/pages/bridge/sendCreatePageRequest.d.ts.map +1 -1
  83. package/dist/pages/bridge/toNotionPageFromBridgePage.d.ts.map +1 -1
  84. package/dist/pages/bridge/toNotionPagePropertyFromBridgeProperty.d.ts +2 -2
  85. package/dist/pages/bridge/toNotionPagePropertyFromBridgeProperty.d.ts.map +1 -1
  86. package/dist/pages/bridge/updatePage.d.ts.map +1 -1
  87. package/dist/pages/createPage.d.ts.map +1 -1
  88. package/dist/pages/getPage.d.ts.map +1 -1
  89. package/dist/pages/identityBlockKey.d.ts.map +1 -1
  90. package/dist/pages/identityPageKey.d.ts.map +1 -1
  91. package/dist/pages/openPage.d.ts.map +1 -1
  92. package/dist/pages/parent.d.ts.map +1 -1
  93. package/dist/pages/types.d.ts +5 -3
  94. package/dist/pages/types.d.ts.map +1 -1
  95. package/dist/pages.d.ts.map +1 -1
  96. package/dist/preferences/backgroundColors.d.ts.map +1 -1
  97. package/dist/preferences/getContrastMode.d.ts.map +1 -1
  98. package/dist/preferences/getTheme.d.ts.map +1 -1
  99. package/dist/preferences/react/useContrastMode.d.ts.map +1 -1
  100. package/dist/preferences/react/useTheme.d.ts.map +1 -1
  101. package/dist/preferences/subscribeToContrastMode.d.ts.map +1 -1
  102. package/dist/preferences/subscribeToTheme.d.ts.map +1 -1
  103. package/dist/preferences/syncDocumentAppearance.d.ts +1 -1
  104. package/dist/preferences/syncDocumentAppearance.d.ts.map +1 -1
  105. package/dist/preferences/syncDocumentAppearance.js +2 -1
  106. package/dist/preferences.d.ts.map +1 -1
  107. package/dist/protocol/compatibility/protocol12/hostToSandbox.d.ts +3356 -0
  108. package/dist/protocol/compatibility/protocol12/hostToSandbox.js +38 -0
  109. package/dist/protocol/compatibility/protocol12/page.d.ts +296 -0
  110. package/dist/protocol/compatibility/protocol12/page.js +7 -0
  111. package/dist/protocol/context/containingPage.d.ts +4 -2
  112. package/dist/protocol/context/containingPage.js +1 -1
  113. package/dist/protocol/context/messages/pageChanged.js +2 -2
  114. package/dist/protocol/dataSources/areDataSourceBindingsEqual.d.ts +3 -0
  115. package/dist/protocol/dataSources/areDataSourceBindingsEqual.js +26 -0
  116. package/dist/protocol/dataSources/messages/queryDataSource.d.ts +24 -19
  117. package/dist/protocol/dataSources/messages/queryDataSource.js +2 -0
  118. package/dist/protocol/files.d.ts +18 -0
  119. package/dist/protocol/files.js +9 -0
  120. package/dist/protocol/messages/hostToSandbox.d.ts +2200 -68
  121. package/dist/protocol/messages/hostToSandbox.js +6 -2
  122. package/dist/protocol/messages/sandboxToHost.d.ts +51 -51
  123. package/dist/protocol/pages/bridgePage.d.ts +20 -17
  124. package/dist/protocol/pages/messages/createPage.d.ts +88 -85
  125. package/dist/protocol/pages/messages/getPage.d.ts +24 -19
  126. package/dist/protocol/pages/messages/getPage.js +2 -0
  127. package/dist/protocol/pages/messages/updatePage.d.ts +37 -34
  128. package/dist/protocol/pages/properties/bridgePagePropertyMaps.d.ts +37 -34
  129. package/dist/protocol/pages/properties/bridgePagePropertyMaps.js +3 -3
  130. package/dist/protocol/pages/properties/bridgePagePropertyValue.d.ts +236 -2
  131. package/dist/protocol/pages/properties/bridgePagePropertyValue.js +20 -7
  132. package/dist/protocol/utils/errors.d.ts +6 -0
  133. package/dist/protocol/utils/errors.js +8 -0
  134. package/dist/protocol/utils/protocolVersion.d.ts +4 -2
  135. package/dist/protocol/utils/protocolVersion.js +4 -2
  136. package/dist/react/InitializationErrorDetails.d.ts.map +1 -1
  137. package/dist/react/MissingHostCommand.d.ts.map +1 -1
  138. package/dist/react/MissingHostScreen.d.ts.map +1 -1
  139. package/dist/react/NotionCustomBlock.d.ts.map +1 -1
  140. package/dist/react/index.d.ts +0 -2
  141. package/dist/react/index.d.ts.map +1 -1
  142. package/dist/react/index.js +0 -1
  143. package/dist/react/useAsyncRead.d.ts.map +1 -1
  144. package/dist/react/useContainingPage.d.ts.map +1 -1
  145. package/dist/react/useLiveLocationRead.d.ts.map +1 -1
  146. package/dist/react/usePage.d.ts.map +1 -1
  147. package/dist/react/useParent.d.ts.map +1 -1
  148. package/dist/react/useQuerySubscription.d.ts.map +1 -1
  149. package/dist/react/useUserFields.d.ts.map +1 -1
  150. package/dist/react/useViewer.d.ts.map +1 -1
  151. package/dist/styles/scrollbars.css +18 -0
  152. package/dist/users/NotionUser.d.ts.map +1 -1
  153. package/dist/users/bridge/toNotionUserDataFromBridgeUser.d.ts.map +1 -1
  154. package/dist/users/getUser.d.ts.map +1 -1
  155. package/dist/users/getViewer.d.ts.map +1 -1
  156. package/dist/users/listUsers.d.ts.map +1 -1
  157. package/dist/users/types.d.ts.map +1 -1
  158. package/dist/users.d.ts.map +1 -1
  159. package/dist/utils/makeUuid.d.ts.map +1 -1
  160. package/dist/utils/notifyListener.d.ts.map +1 -1
  161. package/dist/utils/typeUtils.d.ts.map +1 -1
  162. package/dist/utils/unreachable.d.ts.map +1 -1
  163. package/dist/utils/version.d.ts.map +1 -1
  164. package/dist/utils/version.js +1 -1
  165. package/docs/block-location.md +22 -9
  166. package/docs/deployment.md +5 -11
  167. package/docs/errors.md +4 -0
  168. package/docs/grants.md +7 -2
  169. package/docs/local-development.md +39 -0
  170. package/docs/pages.md +97 -62
  171. package/package.json +2 -1
  172. package/src/bridge/SandboxBridge.ts +49 -144
  173. package/src/bridge/createBrowserSandboxBridge.ts +64 -0
  174. package/src/bridge/getBridge.ts +3 -2
  175. package/src/bridge/hostState.ts +2 -2
  176. package/src/bridge/initialization/SandboxInitialization.ts +24 -10
  177. package/src/bridge/sandboxEnvironment.ts +38 -0
  178. package/src/dataSources/bridge/DataSourceCache.ts +94 -0
  179. package/src/dataSources/bridge/reuseDataSourcesForUnchangedBindings.ts +5 -5
  180. package/src/dataSources/properties/resolvePropertyWriteMapForDataSource.ts +2 -2
  181. package/src/files/files.ts +1 -1
  182. package/src/location/mapContainingPage.ts +2 -2
  183. package/src/pages/bridge/toNotionPagePropertyFromBridgeProperty.ts +2 -2
  184. package/src/pages/types.ts +9 -3
  185. package/src/preferences/syncDocumentAppearance.ts +2 -1
  186. package/src/react/index.ts +0 -2
  187. package/src/styles/scrollbars.css +18 -0
  188. package/dist/react/NotionTokenScope.d.ts +0 -10
  189. package/dist/react/NotionTokenScope.d.ts.map +0 -1
  190. package/dist/react/NotionTokenScope.js +0 -12
  191. package/src/react/NotionTokenScope.tsx +0 -26
package/README.md CHANGED
@@ -17,7 +17,7 @@ For local preview, run `ntn workers customblocks dev` from the worker project. O
17
17
  printed by the command. Install
18
18
  [`@notionhq/custom-blocks-dev-shell`](https://www.npmjs.com/package/@notionhq/custom-blocks-dev-shell)
19
19
  as a devDependency to pin the shell and get its reference docs in `node_modules`. See
20
- [`docs/deployment.md`](docs/deployment.md) for local serving details.
20
+ [`docs/local-development.md`](docs/local-development.md) to verify your block in the dev shell.
21
21
 
22
22
  ## Quick start
23
23
 
@@ -36,12 +36,14 @@ worker.customBlock("hello", {
36
36
  })
37
37
  ```
38
38
 
39
- Wrap your React app in `<NotionCustomBlock>` at the block's entry point:
39
+ Wrap your React app in `<NotionCustomBlock>` at the block's entry point. For a new block, we
40
+ recommend importing `nds.css` and using Notion design tokens when no design is specified. Use your
41
+ own styles when you're following a supplied design.
40
42
 
41
43
  ```tsx
42
44
  // blocks/hello/src/index.tsx
43
45
  import "@notionhq/custom-blocks/nds.css"
44
- import { NotionCustomBlock, NotionTokenScope } from "@notionhq/custom-blocks/react"
46
+ import { NotionCustomBlock } from "@notionhq/custom-blocks/react"
45
47
  import { createRoot } from "react-dom/client"
46
48
  import { App } from "./App"
47
49
  import "./style.css"
@@ -51,9 +53,7 @@ if (!root) throw new Error("The root element is missing.")
51
53
 
52
54
  createRoot(root).render(
53
55
  <NotionCustomBlock>
54
- <NotionTokenScope>
55
- <App />
56
- </NotionTokenScope>
56
+ <App />
57
57
  </NotionCustomBlock>,
58
58
  )
59
59
  ```
@@ -67,14 +67,14 @@ import { getInstance } from "@notionhq/custom-blocks"
67
67
  export function App() {
68
68
  const instance = getInstance()
69
69
  const id = instance.type === "view" ? instance.viewId : instance.blockId
70
- return <div className="app">Hello from {id}.</div>
70
+ return <div className="card">Hello from {id}.</div>
71
71
  }
72
72
  ```
73
73
 
74
- Save these styles in `blocks/hello/src/style.css`:
74
+ Save these styles in `blocks/hello/src/style.css`. The card follows Notion's light and dark themes:
75
75
 
76
76
  ```css
77
- .app {
77
+ .card {
78
78
  color: var(--content-primary);
79
79
  background: var(--bg-base);
80
80
  padding: var(--spacing-12);
@@ -90,16 +90,18 @@ automatically.
90
90
 
91
91
  ## Notion design tokens
92
92
 
93
- The optional `@notionhq/custom-blocks/nds.css` stylesheet provides Notion CSS variables for matching
94
- Notion's UI. These variables cover colors, spacing, typography, and borders. Import the stylesheet
95
- once. Place components that use the variables inside `<NotionTokenScope>`, as shown above.
93
+ Import `@notionhq/custom-blocks/nds.css` to use Notion’s colors, spacing, typography, and borders.
94
+ The card above uses semantic color tokens that follow Notion’s theme and contrast settings. Use
95
+ semantic colors for hover and selected states too.
96
96
 
97
- `<NotionTokenScope>` keeps Notion CSS variables in sync with the host’s theme and contrast settings.
97
+ Choose background and text tokens together. Notion’s filled button uses `--bg-interactive-strong`
98
+ with `--content-on-interactive-strong`. Its tinted button uses
99
+ `--bg-interactive-secondary-translucent` with `--content-interactive-primary`.
98
100
 
99
- `NotionTokenScopeProps` has one required `children` prop.
101
+ If you use custom colors, check text against its background in both light and dark mode. To read
102
+ Notion’s appearance settings in code, see [Preferences](docs/block-location.md#preferences).
100
103
 
101
- CSS variables apply only within `<NotionTokenScope>`. Place portal containers inside that scope, or
102
- wrap the portal content in another `<NotionTokenScope>`.
104
+ ## SDK entrypoints
103
105
 
104
106
  The root entrypoint exports initialization, block location, manifest, shared types, and errors.
105
107
  Theme and contrast APIs are available from `/preferences`; page, user, data-source, navigation, and
@@ -108,6 +110,16 @@ React APIs use their named subpaths. Reference factories such as `NotionPage.fro
108
110
  requesting host data. For plain JavaScript, the SDK reports readiness on the next animation frame
109
111
  after setup. See the [lifecycle guide](docs/lifecycle.md) for manual readiness and sizing.
110
112
 
113
+ ## Scrollbar colors
114
+
115
+ For blocks with internal scroll areas, we recommend opting into Notion's scrollbar colors. Import
116
+ `@notionhq/custom-blocks/scrollbars.css` alongside `nds.css`. The styles cover the iframe and nested
117
+ scroll areas when Notion tokens are in scope. High contrast and forced colors use browser defaults.
118
+ Scrollbar size and visibility follow the browser and OS.
119
+
120
+ Omit the import to keep browser colors. Set `--notion-scrollbar-thumb` on a scroll area to choose a
121
+ different thumb color, or override `scrollbar-color` in your own stylesheet.
122
+
111
123
  ## Reference
112
124
 
113
125
  Each page covers one API category. Import framework-neutral APIs from their domain subpaths, and
@@ -117,8 +129,8 @@ shared types.
117
129
  - [Lifecycle](docs/lifecycle.md) covers setup with `<NotionCustomBlock>`, `useCustomBlockInit()`,
118
130
  or `initCustomBlock()`, as well as readiness and sizing.
119
131
  - [`docs/block-location.md`](docs/block-location.md) — `getInstance`, `useParent`,
120
- `useContainingPage`, `useTheme`, `useContrastMode`, `NotionTokenScope`, and
121
- `NotionTokenScopeProps`. Where the block sits and how to read the host's appearance.
132
+ `useContainingPage`, `useTheme`, and `useContrastMode`. Where the block sits and how to read the
133
+ host's appearance.
122
134
  - [Data sources](docs/data-sources.md) covers source bindings, `NotionDataSource`, and live
123
135
  queries with filters and sorts.
124
136
  - [`docs/pages.md`](docs/pages.md) — `NotionPage.fromKey`, `createPage`, `usePage`, and page
@@ -132,6 +144,8 @@ shared types.
132
144
  - [`docs/grants.md`](docs/grants.md): the block manifest's `grants` and viewer approval.
133
145
  - [`docs/bridge-interceptors.md`](docs/bridge-interceptors.md): `registerMessageInterceptor` and
134
146
  `MessageInterceptor` for controlled bridge responses.
147
+ - [Verify a custom block locally](docs/local-development.md) shows how to preview your block with
148
+ sample data and check that your changes work.
135
149
  - [`docs/deployment.md`](docs/deployment.md): worker deployment, local previews, and manifest
136
150
  sources.
137
151
 
@@ -8,22 +8,22 @@ import type { InitCustomBlockOptions } from "../initialization/types.js";
8
8
  import type { CustomBlockHostState } from "./hostState.js";
9
9
  import type { MessageInterceptor } from "./messageInterceptor.js";
10
10
  import { type RequestMessage, type ResponseSchema } from "./requests.js";
11
+ import type { SandboxEnvironment } from "./sandboxEnvironment.js";
11
12
  export declare class SandboxBridge {
13
+ private readonly environment;
12
14
  private hostState;
13
15
  private listeners;
14
16
  private dataSourceQueryListeners;
15
17
  private messageInterceptors;
16
18
  private readonly requests;
17
- private latestDataSourceBindings;
18
- private readonly pendingDataSourceReads;
19
- private dataSourceReadGeneration;
19
+ private readonly dataSources;
20
20
  private readonly locationLifetime;
21
- private isListening;
21
+ private stopListening;
22
22
  private isDisposed;
23
23
  private readonly initialization;
24
24
  private automaticReadinessCleanup;
25
25
  private resizeObserverCleanup;
26
- constructor();
26
+ constructor(environment: SandboxEnvironment);
27
27
  initialize(readiness?: InitCustomBlockOptions["readiness"]): Promise<import("../index.js").CustomBlockInitData>;
28
28
  /**
29
29
  * Stops this bridge permanently and ignores further host messages.
@@ -32,11 +32,10 @@ export declare class SandboxBridge {
32
32
  * messages and acknowledgment callbacks.
33
33
  */
34
34
  dispose(): void;
35
- startListening(): void;
35
+ private startListening;
36
36
  registerMessageInterceptor(interceptor: MessageInterceptor): () => void;
37
37
  postToHost(message: SandboxToHostMessage): void;
38
38
  private sendToHost;
39
- private handleMessage;
40
39
  private receiveMessage;
41
40
  subscribe: (listener: () => void) => () => boolean;
42
41
  getHostState: () => CustomBlockHostState;
@@ -45,6 +44,7 @@ export declare class SandboxBridge {
45
44
  subscribeToDataSourceQueryResults(subscriptionId: string, listener: (message: QueryDataSourceResultMessage) => void): () => void;
46
45
  notifyReady(): NotifyReadyResult;
47
46
  private scheduleAutomaticReadiness;
47
+ private cancelAutomaticReadiness;
48
48
  private applyInit;
49
49
  waitForInitialization(): Promise<{
50
50
  status: "success";
@@ -121,7 +121,6 @@ export declare class SandboxBridge {
121
121
  */
122
122
  private waitForLocation;
123
123
  readDataSource(key: string): Promise<DataSourceReadResult>;
124
- private requestDataSource;
125
124
  request<Schema extends ResponseSchema>(message: RequestMessage, responseSchema: Schema): Promise<import("./requests.js").RequestResult<Schema>>;
126
125
  }
127
126
  //# sourceMappingURL=SandboxBridge.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"SandboxBridge.d.ts","sourceRoot":"","sources":["../../src/bridge/SandboxBridge.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEX,4BAA4B,EAC5B,MAAM,0EAA0E,CAAA;AAMjF,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4DAA4D,CAAA;AACtG,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kDAAkD,CAAA;AAI5F,OAAO,KAAK,CAAC,MAAM,SAAS,CAAA;AAG5B,OAAO,KAAK,EAAE,oBA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,MAAM,EACtB,QAAQ,EAAE,CAAC,OAAO,EAAE,4BAA4B,KAAK,IAAI,GACvD,MAAM,IAAI,CAKZ;IAED,WAAW,IAAI,iBAAiB,CA0B/B;IAED,OAAO,CAAC,0BAA0B;IAkBlC,OAAO,CAAC,wBAAwB;IAMhC,OAAO,CAAC,SAAS;IASX,qBAAqB;gBAGb,SAAS;;;gBACT,OAAO;;OACpB;IAED,2BAA2B,IAAI,MAAM,CAEpC;IAEK,UAAU;gBAPF,OAAO;;;gBAcP,SAAS;;;;;;;;;;;;;;;;;;;;;;OACtB;IAEK,kBAAkB;gBAjBV,OAAO;;;gBAwBP,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OACtB;IAED;;;OAGG;YACW,eAAe;IAwEvB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAM/D;IAED,OAAO,CAAC,MAAM,SAAS,cAAc,EACpC,OAAO,EAAE,cAAc,EACvB,cAAc,EAAE,MAAM,0DAGtB;CACD","sourceRoot":""}
@@ -1,22 +1,20 @@
1
+ import { hostToSandboxMessageForProtocol12Schema } from "../protocol/compatibility/protocol12/hostToSandbox.js";
1
2
  import { DEFAULT_CONTRAST_MODE } from "../protocol/context/contrast.js";
2
3
  import { hostToSandboxMessageSchema } from "../protocol/messages/hostToSandbox.js";
3
4
  import { readIncomingType } from "../protocol/messages/incomingType.js";
5
+ import { PROTOCOL_VERSION_12_MINIMAL_INIT } from "../protocol/utils/protocolVersion.js";
4
6
  import { readRequestId } from "../protocol/utils/readRequestId.js";
5
7
  import { readSubscriptionId } from "../protocol/utils/readSubscriptionId.js";
6
8
  import * as v from "valibot";
7
- import { applyDataSourceBindings } from "../dataSources/bridge/applyDataSourceBindings.js";
8
- import { makeDataSourceReadResponseSchema } from "../dataSources/bridge/makeDataSourceReadResponseSchema.js";
9
+ import { DataSourceCache } from "../dataSources/bridge/DataSourceCache.js";
9
10
  import { resolveInitialState } from "../initialization/resolveInitialState.js";
10
- import { autoResize } from "../layout/autoResize.js";
11
- import { getInitialContentHeight } from "../layout/getInitialContentHeight.js";
12
- import { syncDocumentAppearance } from "../preferences/syncDocumentAppearance.js";
13
11
  import { notifyListener } from "../utils/notifyListener.js";
14
12
  import { unreachable } from "../utils/unreachable.js";
15
13
  import { applyHostStateMessage } from "./applyHostStateMessage.js";
16
14
  import { SandboxInitialization } from "./initialization/SandboxInitialization.js";
17
15
  import { DEFAULT_REQUEST_TIMEOUT_MS, Requests, } from "./requests.js";
18
- const AUTOMATIC_READINESS_FALLBACK_MS = 100;
19
16
  export class SandboxBridge {
17
+ environment;
20
18
  hostState = {
21
19
  status: "uninitialized",
22
20
  theme: "light",
@@ -26,22 +24,23 @@ export class SandboxBridge {
26
24
  dataSourceQueryListeners = new Map();
27
25
  messageInterceptors = new Set();
28
26
  requests = new Requests(message => this.postToHost(message));
29
- latestDataSourceBindings = {};
30
- pendingDataSourceReads = new Map();
31
- dataSourceReadGeneration = 0;
27
+ dataSources = new DataSourceCache(this.requests, () => this.hostState, state => this.commitHostState(state));
32
28
  locationLifetime = new AbortController();
33
- isListening = false;
29
+ stopListening;
34
30
  isDisposed = false;
35
31
  initialization;
36
32
  automaticReadinessCleanup;
37
33
  resizeObserverCleanup;
38
- constructor() {
34
+ constructor(environment) {
35
+ this.environment = environment;
39
36
  this.initialization = new SandboxInitialization({
40
37
  start: () => this.startListening(),
38
+ isEmbedded: () => this.environment.transport.isEmbedded(),
39
+ readLocalManifest: () => this.environment.document.readLocalManifest(),
41
40
  post: message => this.sendToHost(message),
42
41
  apply: (message, protocolVersion) => this.applyInit(message, protocolVersion),
43
42
  getState: () => this.hostState,
44
- getInitialHeight: getInitialContentHeight,
43
+ getInitialHeight: () => this.environment.document.getInitialHeight(),
45
44
  });
46
45
  }
47
46
  initialize(readiness = "automatic") {
@@ -57,26 +56,23 @@ export class SandboxBridge {
57
56
  */
58
57
  dispose() {
59
58
  this.isDisposed = true;
60
- this.automaticReadinessCleanup?.();
59
+ this.cancelAutomaticReadiness();
61
60
  this.locationLifetime.abort();
62
- this.dataSourceReadGeneration++;
61
+ this.dataSources.invalidate();
63
62
  this.resizeObserverCleanup?.();
64
63
  this.resizeObserverCleanup = undefined;
65
64
  this.requests.disposeAll();
66
65
  this.initialization.dispose();
67
- if (this.isListening) {
68
- window.removeEventListener("message", this.handleMessage);
69
- this.isListening = false;
70
- }
66
+ this.stopListening?.();
67
+ this.stopListening = undefined;
71
68
  this.listeners.clear();
72
69
  this.dataSourceQueryListeners.clear();
73
70
  }
74
71
  startListening() {
75
- if (this.isDisposed || this.isListening || typeof window === "undefined") {
72
+ if (this.isDisposed || this.stopListening !== undefined) {
76
73
  return;
77
74
  }
78
- this.isListening = true;
79
- window.addEventListener("message", this.handleMessage);
75
+ this.stopListening = this.environment.transport.subscribe(message => this.receiveMessage(message));
80
76
  }
81
77
  registerMessageInterceptor(interceptor) {
82
78
  this.messageInterceptors.add(interceptor);
@@ -93,19 +89,16 @@ export class SandboxBridge {
93
89
  return;
94
90
  }
95
91
  }
96
- window.parent.postMessage(message, "*");
92
+ this.environment.transport.send(message);
97
93
  }
98
- handleMessage = (event) => {
99
- if (event.source !== window.parent) {
100
- return;
101
- }
102
- this.receiveMessage(event.data);
103
- };
104
94
  receiveMessage(data) {
105
95
  if (this.isDisposed) {
106
96
  return;
107
97
  }
108
- const parsed = v.safeParse(hostToSandboxMessageSchema, data);
98
+ // Protocol 12 uses the previous page schema, which has no unavailable Files entry.
99
+ const parsed = this.initialization.protocolVersion === PROTOCOL_VERSION_12_MINIMAL_INIT
100
+ ? v.safeParse(hostToSandboxMessageForProtocol12Schema, data)
101
+ : v.safeParse(hostToSandboxMessageSchema, data);
109
102
  if (!parsed.success) {
110
103
  console.warn("[custom-blocks-sdk] ignoring malformed host message", parsed.issues);
111
104
  const incomingType = readIncomingType(data);
@@ -220,8 +213,7 @@ export class SandboxBridge {
220
213
  this.commitHostState(applyHostStateMessage(hostState, message));
221
214
  return;
222
215
  case "dataSourcesInvalidated": {
223
- this.dataSourceReadGeneration++;
224
- this.latestDataSourceBindings = {};
216
+ this.dataSources.invalidate();
225
217
  this.commitHostState({ ...hostState, dataSources: [] });
226
218
  return;
227
219
  }
@@ -249,7 +241,7 @@ export class SandboxBridge {
249
241
  if (previous.status !== state.status ||
250
242
  previous.theme !== state.theme ||
251
243
  previous.contrastMode !== state.contrastMode) {
252
- syncDocumentAppearance(state);
244
+ this.environment.document.applyAppearance(state);
253
245
  }
254
246
  for (const listener of this.listeners) {
255
247
  notifyListener(listener);
@@ -274,18 +266,13 @@ export class SandboxBridge {
274
266
  }
275
267
  notifyReady() {
276
268
  const result = this.initialization.notifyReady();
277
- if (result.status === "success") {
278
- this.automaticReadinessCleanup?.();
269
+ if (result.status === "error") {
270
+ return result;
279
271
  }
280
- if (result.status === "success" &&
281
- this.resizeObserverCleanup === undefined) {
272
+ this.cancelAutomaticReadiness();
273
+ if (this.resizeObserverCleanup === undefined) {
282
274
  try {
283
- this.resizeObserverCleanup = autoResize({
284
- target: typeof document === "undefined"
285
- ? null
286
- : document.getElementById("root"),
287
- onResize: height => this.postToHost({ type: "resize", height }),
288
- });
275
+ this.resizeObserverCleanup = this.environment.document.observeHeight(height => this.postToHost({ type: "resize", height }));
289
276
  }
290
277
  catch (error) {
291
278
  return {
@@ -307,25 +294,21 @@ export class SandboxBridge {
307
294
  return;
308
295
  }
309
296
  const reportReady = () => {
310
- this.automaticReadinessCleanup?.();
297
+ this.cancelAutomaticReadiness();
311
298
  const result = this.notifyReady();
312
299
  if (result.status === "error") {
313
300
  console.error("[custom-blocks-sdk] Could not notify the host that the block is ready.", result.error);
314
301
  }
315
302
  };
316
- // Hidden iframes may not receive animation frames.
317
- const timeout = window.setTimeout(reportReady, AUTOMATIC_READINESS_FALLBACK_MS);
318
- const frame = window.requestAnimationFrame?.(reportReady);
319
- this.automaticReadinessCleanup = () => {
320
- window.clearTimeout(timeout);
321
- if (frame !== undefined) {
322
- window.cancelAnimationFrame(frame);
323
- }
324
- this.automaticReadinessCleanup = undefined;
325
- };
303
+ this.automaticReadinessCleanup =
304
+ this.environment.document.scheduleReady(reportReady);
305
+ }
306
+ cancelAutomaticReadiness() {
307
+ const cancel = this.automaticReadinessCleanup;
308
+ this.automaticReadinessCleanup = undefined;
309
+ cancel?.();
326
310
  }
327
311
  applyInit(message, protocolVersion) {
328
- this.latestDataSourceBindings = {};
329
312
  const state = resolveInitialState(message, protocolVersion);
330
313
  this.commitHostState(state);
331
314
  return undefined;
@@ -337,7 +320,7 @@ export class SandboxBridge {
337
320
  : { status: "error", error };
338
321
  }
339
322
  getDataSourceReadGeneration() {
340
- return this.dataSourceReadGeneration;
323
+ return this.dataSources.getGeneration();
341
324
  }
342
325
  async readParent() {
343
326
  const result = await this.waitForLocation(state => state.parent, state => state.parentError);
@@ -422,55 +405,7 @@ export class SandboxBridge {
422
405
  if (setup.status === "error") {
423
406
  return setup;
424
407
  }
425
- const generation = this.dataSourceReadGeneration;
426
- const existing = this.pendingDataSourceReads.get(key);
427
- if (existing?.generation === generation) {
428
- return existing.result;
429
- }
430
- const pending = {
431
- generation,
432
- result: this.requestDataSource(key, generation),
433
- };
434
- this.pendingDataSourceReads.set(key, pending);
435
- try {
436
- return await pending.result;
437
- }
438
- finally {
439
- if (this.pendingDataSourceReads.get(key) === pending) {
440
- this.pendingDataSourceReads.delete(key);
441
- }
442
- }
443
- }
444
- async requestDataSource(key, generation) {
445
- const initialState = this.hostState;
446
- if (initialState.status !== "initialized") {
447
- throw new Error("Source read started without initialized state.");
448
- }
449
- const result = await this.request({ type: "getDataSource", key }, makeDataSourceReadResponseSchema(initialState.manifest, key));
450
- if (result.status === "error") {
451
- return result;
452
- }
453
- if (generation !== this.dataSourceReadGeneration || this.isDisposed) {
454
- return staleReadError();
455
- }
456
- const state = this.hostState;
457
- if (state.status !== "initialized") {
458
- throw new Error("Source read completed without initialized state.");
459
- }
460
- const previousBindings = this.latestDataSourceBindings;
461
- this.latestDataSourceBindings = {
462
- ...previousBindings,
463
- [key]: result.binding,
464
- };
465
- const next = applyDataSourceBindings(state, this.latestDataSourceBindings, previousBindings);
466
- // Only explicitly read source keys belong in the cache.
467
- next.dataSources = next.dataSources.filter(source => Object.hasOwn(this.latestDataSourceBindings, source.key));
468
- this.commitHostState(next);
469
- const dataSource = next.dataSources.find(source => source.key === key);
470
- if (dataSource === undefined) {
471
- throw new Error("The host returned metadata for an undeclared source.");
472
- }
473
- return { status: "success", dataSource };
408
+ return this.dataSources.read(key);
474
409
  }
475
410
  request(message, responseSchema) {
476
411
  return this.requests.send(message, responseSchema);
@@ -499,13 +434,3 @@ function formatInvalidHostReason(incomingType, issues) {
499
434
  const extra = issues.length > 1 ? ` (+${issues.length - 1} more)` : "";
500
435
  return `Could not parse ${labelled}: ${detail}${extra}`;
501
436
  }
502
- function staleReadError() {
503
- return {
504
- status: "error",
505
- error: {
506
- code: "host_state_changed",
507
- message: "The host configuration changed during the read.",
508
- isRetryable: true,
509
- },
510
- };
511
- }
@@ -1 +1 @@
1
- {"version":3,"file":"applyHostStateMessage.d.ts","sourceRoot":"","sources":["../../src/bridge/applyHostStateMessage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4DAA4D,CAAA;AACtG,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAE1D,wBAAgB,qBAAqB,CACpC,SAAS,EAAE,oBAAoB,EAC/B,OAAO,EAAE,OAAO,CACf,oBAAoB,EACpB;IACC,IAAI,EACD,cAAc,GACd,qBAAqB,GACrB,eAAe,GACf,aAAa,GACb,WAAW,GACX,aAAa,CAAA;CAChB,CACD,GACC,oBAAoB,CAqCtB"}
1
+ {"version":3,"file":"applyHostStateMessage.d.ts","sources":["../../src/bridge/applyHostStateMessage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4DAA4D,CAAA;AACtG,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAE1D,wBAAgB,qBAAqB,CACpC,SAAS,EAAE,oBAAoB,EAC/B,OAAO,EAAE,OAAO,CACf,oBAAoB,EACpB;IACC,IAAI,EACD,cAAc,GACd,qBAAqB,GACrB,eAAe,GACf,aAAa,GACb,WAAW,GACX,aAAa,CAAA;CAChB,CACD,GACC,oBAAoB,CAqCtB","sourceRoot":""}
@@ -0,0 +1,3 @@
1
+ import { SandboxBridge } from "./SandboxBridge.js";
2
+ export declare function createBrowserSandboxBridge(): SandboxBridge;
3
+ //# sourceMappingURL=createBrowserSandboxBridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createBrowserSandboxBridge.d.ts","sources":["../../src/bridge/createBrowserSandboxBridge.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAKlD,wBAAgB,0BAA0B,IAAI,aAAa,CAE1D","sourceRoot":""}
@@ -0,0 +1,54 @@
1
+ import { autoResize } from "../layout/autoResize.js";
2
+ import { getInitialContentHeight } from "../layout/getInitialContentHeight.js";
3
+ import { readLocalManifest } from "../manifest/readLocalManifest.js";
4
+ import { syncDocumentAppearance } from "../preferences/syncDocumentAppearance.js";
5
+ import { SandboxBridge } from "./SandboxBridge.js";
6
+ const AUTOMATIC_READINESS_FALLBACK_MS = 100;
7
+ export function createBrowserSandboxBridge() {
8
+ return new SandboxBridge(browserEnvironment);
9
+ }
10
+ const browserEnvironment = {
11
+ transport: {
12
+ isEmbedded: () => typeof window !== "undefined" && window.parent !== window,
13
+ subscribe(receive) {
14
+ if (typeof window === "undefined") {
15
+ return undefined;
16
+ }
17
+ const sandbox = window;
18
+ const onMessage = (event) => {
19
+ if (event.source === sandbox.parent) {
20
+ receive(event.data);
21
+ }
22
+ };
23
+ sandbox.addEventListener("message", onMessage);
24
+ return () => sandbox.removeEventListener("message", onMessage);
25
+ },
26
+ send: message => window.parent.postMessage(message, "*"),
27
+ },
28
+ document: {
29
+ readLocalManifest: () => readLocalManifest({
30
+ document: window.document,
31
+ url: new URL(window.location.href),
32
+ }),
33
+ applyAppearance: syncDocumentAppearance,
34
+ getInitialHeight: getInitialContentHeight,
35
+ scheduleReady(reportReady) {
36
+ const sandbox = window;
37
+ // Hidden iframes may not receive animation frames.
38
+ const timeout = sandbox.setTimeout(reportReady, AUTOMATIC_READINESS_FALLBACK_MS);
39
+ const frame = sandbox.requestAnimationFrame?.(reportReady);
40
+ return () => {
41
+ sandbox.clearTimeout(timeout);
42
+ if (frame !== undefined) {
43
+ sandbox.cancelAnimationFrame(frame);
44
+ }
45
+ };
46
+ },
47
+ observeHeight: onResize => autoResize({
48
+ target: typeof document === "undefined"
49
+ ? null
50
+ : document.getElementById("root"),
51
+ onResize,
52
+ }),
53
+ },
54
+ };
@@ -1,3 +1,3 @@
1
- import { SandboxBridge } from "./SandboxBridge.js";
1
+ import type { SandboxBridge } from "./SandboxBridge.js";
2
2
  export declare function getBridge(): SandboxBridge;
3
3
  //# sourceMappingURL=getBridge.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getBridge.d.ts","sourceRoot":"","sources":["../../src/bridge/getBridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAIlD,wBAAgB,SAAS,IAAI,aAAa,CAKzC"}
1
+ {"version":3,"file":"getBridge.d.ts","sources":["../../src/bridge/getBridge.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAIvD,wBAAgB,SAAS,IAAI,aAAa,CAKzC","sourceRoot":""}
@@ -1,8 +1,8 @@
1
- import { SandboxBridge } from "./SandboxBridge.js";
1
+ import { createBrowserSandboxBridge } from "./createBrowserSandboxBridge.js";
2
2
  let bridge;
3
3
  export function getBridge() {
4
4
  if (!bridge) {
5
- bridge = new SandboxBridge();
5
+ bridge = createBrowserSandboxBridge();
6
6
  }
7
7
  return bridge;
8
8
  }
@@ -1 +1 @@
1
- {"version":3,"file":"getInitializedHostState.d.ts","sourceRoot":"","sources":["../../src/bridge/getInitializedHostState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAE1D,wBAAgB,uBAAuB,CACtC,UAAU,EAAE,MAAM,GAChB,oBAAoB,CAQtB"}
1
+ {"version":3,"file":"getInitializedHostState.d.ts","sources":["../../src/bridge/getInitializedHostState.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAE1D,wBAAgB,uBAAuB,CACtC,UAAU,EAAE,MAAM,GAChB,oBAAoB,CAQtB","sourceRoot":""}
@@ -1,4 +1,4 @@
1
- import type { CustomBlockPage } from "../protocol/context/containingPage.js";
1
+ import type { ContainingPage } from "../protocol/context/containingPage.js";
2
2
  import type { NotionContrastMode } from "../protocol/context/contrast.js";
3
3
  import type { CustomBlockInstance } from "../protocol/context/instance.js";
4
4
  import type { NotionParent } from "../protocol/context/parent.js";
@@ -19,7 +19,7 @@ export type InitializedHostState = {
19
19
  theme: NotionTheme;
20
20
  contrastMode: NotionContrastMode;
21
21
  parent?: NotionParent;
22
- page?: CustomBlockPage;
22
+ page?: ContainingPage;
23
23
  parentError?: CustomBlockErrorInfo;
24
24
  pageError?: CustomBlockErrorInfo;
25
25
  dataSources: NotionDataSource[];
@@ -1 +1 @@
1
- {"version":3,"file":"hostState.d.ts","sourceRoot":"","sources":["../../src/bridge/hostState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,4DAA4D,CAAA;AACjG,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sDAAsD,CAAA;AAC9F,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,sDAAsD,CAAA;AAC/F,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oDAAoD,CAAA;AACtF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mD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EAAkE,CAAA;AACxG,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,uDAAuD,CAAA;AACtG,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kDAAkD,CAAA;AAE5F,MAAM,MAAM,oBAAoB,GAAG,sBAAsB,GAAG,oBAAoB,CAAA;AAEhF,MAAM,MAAM,sBAAsB,GAAG;IACpC,MAAM,EAAE,eAAe,CAAA;IACvB,KAAK,EAAE,WAAW,CAAA;IAClB,YAAY,EAAE,kBAAkB,CAAA;CAChC,CAAA;AAED,MAAM,MAAM,oBAAoB,GAAG;IAClC,4FAA4F;IAC5F,eAAe,EAAE,MAAM,CAAA;IACvB,QAAQ,EAAE,mBAAmB,CAAA;IAC7B,KAAK,EAAE,WAAW,CAAA;IAClB,YAAY,EAAE,kBAAkB,CAAA;IAChC,MAAM,CAAC,EAAE,YAAY,CAAA;IACrB,IAAI,CAAC,EAAE,cAAc,CAAA;IACrB,WAAW,CAAC,EAAE,oBAAoB,CAAA;IAClC,SAAS,CAAC,EAAE,oBAAoB,CAAA;IAChC,WAAW,EAAE,gBAAgB,EAAE,CAAA;IAC/B,MAAM,EAAE,aAAa,CAAA;IACrB,QAAQ,EAAE,yBAAyB,CAAA;CACnC,CAAA","sourceRoot":""}
@@ -4,11 +4,16 @@ import type { CustomBlockInitResultErrorInfo } from "../../protocol/handshake/in
4
4
  import type { SandboxToHostMessage } from "../../protocol/messages/sandboxToHost.js";
5
5
  import type { CustomBlockErrorInfo } from "../../protocol/utils/errors.js";
6
6
  import type { CustomBlockInitData } from "../../initialization/types.js";
7
+ import type { LocalManifestReadResult } from "../../manifest/readLocalManifest.js";
7
8
  import type { CustomBlockHostState } from "../hostState.js";
8
9
  /** Connects startup sequencing to the bridge's transport, state, and height measurement. */
9
10
  export type InitializationOptions = {
10
11
  /** Starts listening for host messages on the first initialization call, before `connect` is sent. */
11
12
  start: () => void;
13
+ /** Checks whether the sandbox is embedded in a host before connecting. */
14
+ isEmbedded: () => boolean;
15
+ /** Reads the local development manifest, if the sandbox has one. */
16
+ readLocalManifest: () => LocalManifestReadResult;
12
17
  /** Sends directly to the host, bypassing the outbound queue. */
13
18
  post: (message: SandboxToHostMessage) => void;
14
19
  /**
@@ -32,6 +37,7 @@ export declare class SandboxInitialization {
32
37
  private initPromise;
33
38
  private connectRequestId;
34
39
  private negotiatedProtocolVersion;
40
+ get protocolVersion(): number | undefined;
35
41
  private initRequestId;
36
42
  private earlyInit;
37
43
  private hostResponseTimeout;
@@ -1 +1 @@
1
- {"version":3,"file":"SandboxInitialization.d.ts","sourceRoot":"","sources":["../../../src/bridge/initialization/SandboxInitialization.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,6DAA6D,CAAA;AACvG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oDAAoD,CAAA;AAE1F,OAAO,KAAK,EACX,8BAA8B,EAE9B,MAAM,0DAA0D,CAAA;AAEjE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4DAA4D,CAAA;AACtG,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kDAAkD,CAAA;AAE5F,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAA;AAIxE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AAgC3D,4FAA4F;AAC5F,MAAM,MAAM,qBAAqB,GAAG;IACnC,qGAAqG;IACrG,KAAK,EAAE,MAAM,IAAI,CAAA;IAEjB,gEAAgE;IAChE,IAAI,EAAE,CAAC,OAAO,EAAE,oBAAoB,KAAK,IAAI,CAAA;IAE7C;;;OAGG;IACH,KAAK,EAAE,CACN,OAAO,EAAE,gBAAgB,EACzB,eAAe,EAAE,MAAM,KACnB,8BAA8B,GAAG,SAAS,CAAA;IAE/C,mFAAmF;IACnF,QAAQ,EAAE,MAAM,oBAAoB,CAAA;IAEpC,qEAAqE;IAErE,gBAAgB,EAAE,MAAM,MAAM,CAAA;C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