-
Notifications
You must be signed in to change notification settings - Fork 1
fix: truapi ^0.4 products connect via MessagePort handoff #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /** | ||
| * Product iframe provider serving both TrUAPI channel generations. | ||
| * | ||
| * Products embed one of two bootstraps, distinguished by how they open the | ||
| * frame channel: | ||
| * | ||
| * - `@novasamatech/host-api-wrapper` (truapi 0.3): exchanges raw Uint8Array | ||
| * frames directly with the parent window — `createIframeProvider`'s native | ||
| * channel. | ||
| * - `@parity/truapi/sandbox` (truapi 0.4): posts `{ type: "truapi-ready" }` | ||
| * to the parent and waits for `{ type: "truapi-init" }` carrying a | ||
| * transferred MessagePort, then runs all traffic over that port. It never | ||
| * listens on window postMessage. | ||
| * | ||
| * Wire frames are identical on both channels, so this provider wraps | ||
| * `createIframeProvider`, answers the ready ping with a fresh port pair, and | ||
| * routes frames to whichever channel the product opened — presenting a single | ||
| * Provider to the container. | ||
| * | ||
| * The provider's lifetime is one container generation: `setAccounts()` | ||
| * disposes the container (and this provider with it) and builds a fresh pair, | ||
| * so a handed-off port never outlives the product generation it serves. | ||
| */ | ||
|
|
||
| import type { Provider } from '@novasamatech/host-api'; | ||
| import { createIframeProvider } from '@novasamatech/host-container'; | ||
|
|
||
| export function createDualChannelIframeProvider(options: { | ||
| iframe: HTMLIFrameElement; | ||
| url: string; | ||
| }): Provider { | ||
| const { iframe, url } = options; | ||
| const inner = createIframeProvider({ iframe, url }); | ||
| const productOrigin = new URL(url, window.location.href).origin; | ||
| const subscribers = new Set<(message: Uint8Array) => void>(); | ||
| let port: MessagePort | null = null; | ||
|
|
||
| const deliver = (message: Uint8Array): void => { | ||
| for (const subscriber of subscribers) subscriber(message); | ||
| }; | ||
|
|
||
| const unsubscribeInner = inner.subscribe(deliver); | ||
|
|
||
| const onWindowMessage = (event: MessageEvent): void => { | ||
| if (event.source !== iframe.contentWindow) return; | ||
| if (event.origin !== productOrigin) return; | ||
| if ((event.data as { type?: unknown } | null)?.type !== 'truapi-ready') return; | ||
|
|
||
| // The product sends one ready ping per page load; each load needs its own | ||
| // port pair (device-permission reloads, deep-link reloads). | ||
| port?.close(); | ||
| const channel = new MessageChannel(); | ||
| port = channel.port1; | ||
| port.onmessage = (e: MessageEvent) => { | ||
| if (e.data instanceof Uint8Array) deliver(e.data); | ||
| }; | ||
| iframe.contentWindow?.postMessage({ type: 'truapi-init' }, productOrigin, [channel.port2]); | ||
| }; | ||
| window.addEventListener('message', onWindowMessage); | ||
|
|
||
| return { | ||
| logger: inner.logger, | ||
| isCorrectEnvironment: () => inner.isCorrectEnvironment(), | ||
| postMessage(message: Uint8Array): void { | ||
| // A product that completed the handoff listens only on the port; one | ||
| // that did not listens only on window postMessage. | ||
| if (port) { | ||
| port.postMessage(message); | ||
| } else { | ||
| inner.postMessage(message); | ||
| } | ||
| }, | ||
| subscribe(callback: (message: Uint8Array) => void): () => void { | ||
| subscribers.add(callback); | ||
| return () => { | ||
| subscribers.delete(callback); | ||
| }; | ||
| }, | ||
| dispose(): void { | ||
| window.removeEventListener('message', onWindowMessage); | ||
| port?.close(); | ||
| port = null; | ||
| subscribers.clear(); | ||
| unsubscribeInner(); | ||
| inner.dispose(); | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,22 @@ | ||
| import { build } from 'esbuild'; | ||
|
|
||
| await build({ | ||
| entryPoints: ['test/test-product.ts'], | ||
| bundle: true, | ||
| format: 'iife', | ||
| platform: 'browser', | ||
| target: 'es2022', | ||
| outfile: 'test/test-product-bundle.js', | ||
| sourcemap: false, | ||
| conditions: ['browser'], | ||
| }); | ||
| const products = [ | ||
| { entry: 'test/test-product.ts', outfile: 'test/test-product-bundle.js' }, | ||
| { entry: 'test/test-product-truapi.ts', outfile: 'test/test-product-truapi-bundle.js' }, | ||
| ]; | ||
|
|
||
| console.log('Test product bundle built: test/test-product-bundle.js'); | ||
| await Promise.all( | ||
| products.map(async ({ entry, outfile }) => { | ||
| await build({ | ||
| entryPoints: [entry], | ||
| bundle: true, | ||
| format: 'iife', | ||
| platform: 'browser', | ||
| target: 'es2022', | ||
| outfile, | ||
| sourcemap: false, | ||
| conditions: ['browser'], | ||
| }); | ||
| console.log(`Test product bundle built: ${outfile}`); | ||
| }), | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we move all the @novasamatech/* related code in a legacy file, to make it easier to clean up in a follow up PR if we think we do need the dual mode for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the newest version of truapi containing the core was just released and it is just now landing on product-sdk. I think it is better to first add this patch and then cleanup novasama deps as to not introduce any breaking changes