hyper-html-api 0.3.0 → 0.5.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.
@@ -1,92 +0,0 @@
1
- /**
2
- * Iframe-based upgrade runner.
3
- *
4
- * Loads the v2 source URL in a hidden iframe with the `_hyperHtmlApi=upgrade-helper`
5
- * query param. The helper inside the iframe handles transform + apply +
6
- * serialize; the parent only orchestrates the postMessage handshake and
7
- * returns the resulting HTML + summary to the caller.
8
- *
9
- * const { html, summary } = await run({ sourceUrl, v1Data })
10
- *
11
- * The caller decides what to do with `html` (e.g. POST to `/save`, render
12
- * in a preview pane, etc.). The runner deliberately doesn't touch the
13
- * network beyond the iframe load.
14
- */
15
-
16
- const DEFAULT_TIMEOUT_MS = 15000
17
-
18
- export async function run({
19
- sourceUrl,
20
- v1Data,
21
- v1Version = null,
22
- parent = typeof window !== 'undefined' ? window : null,
23
- timeoutMs = DEFAULT_TIMEOUT_MS,
24
- hidden = true,
25
- } = {}) {
26
- if (!parent) throw new Error('upgrade.run requires a window context')
27
- if (!sourceUrl) throw new Error('upgrade.run requires sourceUrl')
28
-
29
- const helperUrl = buildHelperUrl(sourceUrl, parent.location.origin)
30
- const expectedOrigin = new URL(helperUrl, parent.location.href).origin
31
-
32
- const iframe = parent.document.createElement('iframe')
33
- if (hidden) {
34
- iframe.style.cssText =
35
- 'position:absolute;left:-9999px;top:-9999px;width:1px;height:1px;border:0;'
36
- }
37
- iframe.setAttribute('aria-hidden', 'true')
38
- iframe.src = helperUrl
39
- parent.document.body.appendChild(iframe)
40
-
41
- let cleanup = () => {}
42
- try {
43
- return await new Promise((resolve, reject) => {
44
- const timer = setTimeout(() => {
45
- reject(new Error(`upgrade.run: helper iframe timed out after ${timeoutMs}ms`))
46
- }, timeoutMs)
47
-
48
- const onMessage = (e) => {
49
- if (e.source !== iframe.contentWindow) return
50
- if (e.origin !== expectedOrigin) return
51
- const msg = e.data
52
- if (!msg || typeof msg !== 'object') return
53
-
54
- if (msg.type === 'hha:upgrade-ready') {
55
- iframe.contentWindow.postMessage(
56
- { type: 'hha:upgrade-data', v1Data, v1Version },
57
- expectedOrigin,
58
- )
59
- return
60
- }
61
- if (msg.type === 'hha:upgrade-result') {
62
- clearTimeout(timer)
63
- resolve({ html: msg.html, summary: msg.summary })
64
- return
65
- }
66
- if (msg.type === 'hha:upgrade-error') {
67
- clearTimeout(timer)
68
- const err = new Error(msg.message || 'helper error')
69
- err.name = msg.name || 'UpgradeHelperError'
70
- reject(err)
71
- return
72
- }
73
- }
74
-
75
- parent.addEventListener('message', onMessage)
76
- cleanup = () => {
77
- parent.removeEventListener('message', onMessage)
78
- clearTimeout(timer)
79
- if (iframe.parentNode) iframe.parentNode.removeChild(iframe)
80
- }
81
- })
82
- } finally {
83
- cleanup()
84
- }
85
- }
86
-
87
- function buildHelperUrl(sourceUrl, parentOrigin) {
88
- const url = new URL(sourceUrl, typeof location !== 'undefined' ? location.href : undefined)
89
- url.searchParams.set('_hyperHtmlApi', 'upgrade-helper')
90
- url.searchParams.set('parentOrigin', parentOrigin)
91
- return url.toString()
92
- }