iframe-link 1.0.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.
- package/LICENSE +21 -0
- package/README.md +103 -0
- package/dist/index.cjs +994 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +80 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.js +984 -0
- package/dist/index.js.map +1 -0
- package/docs/README.ja.md +103 -0
- package/docs/README.zh-CN.md +103 -0
- package/docs/THIRD_PARTY_NOTICES.md +45 -0
- package/docs/assets/sdk-demo.en.png +0 -0
- package/docs/assets/sdk-demo.ja.png +0 -0
- package/docs/assets/sdk-demo.zh-CN.png +0 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 yuukiLike
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# iframe-link
|
|
2
|
+
|
|
3
|
+
Simple communication between an iframe and its parent.
|
|
4
|
+
|
|
5
|
+
Promise-based remote calls and events, built on `window.postMessage`.
|
|
6
|
+
|
|
7
|
+
English · [简体中文](docs/README.zh-CN.md) · [日本語](docs/README.ja.md)
|
|
8
|
+
|
|
9
|
+
## Why
|
|
10
|
+
|
|
11
|
+
`postMessage` moves messages, but it does not provide request IDs, Promise resolution, event routing, or a reliable way to know when the other side is ready. Rebuilding that layer for every iframe is repetitive, and Origin checks are easy to miss.
|
|
12
|
+
|
|
13
|
+
This SDK turns the parent and iframe into two ends of one small channel: expose methods, await remote calls, emit events, and handle load-order races with a READY/ACK handshake.
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add iframe-link
|
|
21
|
+
# npm install iframe-link
|
|
22
|
+
# yarn add iframe-link
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
### Parent
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { connectToIframe } from 'iframe-link'
|
|
31
|
+
|
|
32
|
+
type ChildApi = {
|
|
33
|
+
getStatus(input: { userId: string }): Promise<{ online: boolean }>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const channel = connectToIframe<ChildApi>({
|
|
37
|
+
iframe: '#profile-frame',
|
|
38
|
+
origin: 'https://child.example.com',
|
|
39
|
+
methods: {
|
|
40
|
+
getTheme: async () => ({ accent: '#3b82f6' })
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
channel.on('STATUS_CHANGED', console.log)
|
|
45
|
+
|
|
46
|
+
const child = await channel.promise
|
|
47
|
+
await child.getStatus({ userId: '42' })
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Child
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { connectToParent } from 'iframe-link'
|
|
54
|
+
|
|
55
|
+
type ParentApi = {
|
|
56
|
+
getTheme(): Promise<{ accent: string }>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const channel = connectToParent<ParentApi>({
|
|
60
|
+
allowedOrigins: ['https://app.example.com'],
|
|
61
|
+
methods: {
|
|
62
|
+
getStatus: async ({ userId }) => ({ userId, online: true })
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const parent = await channel.promise
|
|
67
|
+
await parent.getTheme()
|
|
68
|
+
|
|
69
|
+
channel.emit('STATUS_CHANGED', { online: true })
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Call `channel.destroy()` when the iframe is removed.
|
|
73
|
+
|
|
74
|
+
## API
|
|
75
|
+
|
|
76
|
+
`connectToIframe({ iframe, origin, methods })` connects a parent page to an iframe. `connectToParent({ allowedOrigins, methods })` connects an iframe to its parent page.
|
|
77
|
+
|
|
78
|
+
| Member | Purpose |
|
|
79
|
+
| --- | --- |
|
|
80
|
+
| `promise` | Resolves to the remote method proxy after the handshake. |
|
|
81
|
+
| `on(type, handler)` | Subscribes to an event. |
|
|
82
|
+
| `off(type, handler)` | Unsubscribes from an event. |
|
|
83
|
+
| `emit(type, data?)` | Emits an event to the other side. |
|
|
84
|
+
| `destroy()` | Removes listeners and rejects pending RPC calls. |
|
|
85
|
+
|
|
86
|
+
`methods` exposes local functions to the other side.
|
|
87
|
+
|
|
88
|
+
## Safety & Limits
|
|
89
|
+
|
|
90
|
+
- Configure exact trusted Origins in production; do not configure a wildcard Origin.
|
|
91
|
+
- The parent validates the configured Origin and iframe source. The child validates `allowedOrigins` and requires `event.source === window.parent`.
|
|
92
|
+
- Before its parent Origin is confirmed, the child sends only READY messages, using `targetOrigin: '*'`. Await `channel.promise` before calling `emit()`; events emitted by the child before it is ready are ignored.
|
|
93
|
+
- Message values must be JSON-serializable. Each RPC accepts one optional payload.
|
|
94
|
+
- Timeout and cancellation are not built in. Without an ACK after five READY attempts, `promise` stays pending.
|
|
95
|
+
- An SDK-less child can call methods exposed by an SDK parent, but it does not complete the SDK handshake.
|
|
96
|
+
|
|
97
|
+
## Contributing
|
|
98
|
+
|
|
99
|
+
See the [contributing guide](https://github.com/yuukiLike/iframe-link/blob/main/docs/CONTRIBUTING.md) for local development and testing.
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
[MIT](LICENSE) · [Third-party notices](docs/THIRD_PARTY_NOTICES.md)
|