@craftile/messenger 0.1.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.md +21 -0
- package/package.json +32 -0
- package/src/index.ts +72 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Craftile Team
|
|
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/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@craftile/messenger",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A type wrapper of window.postMessage for iframe/parent communication",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"craftile",
|
|
7
|
+
"messenger",
|
|
8
|
+
"postMessage",
|
|
9
|
+
"events"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"authors": [
|
|
13
|
+
{
|
|
14
|
+
"name": "Eldo Magan",
|
|
15
|
+
"email": "magan.eldo@gmail.com"
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "src/index.ts",
|
|
20
|
+
"types": "src/index.ts",
|
|
21
|
+
"exports": "./src/index.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"src"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/craftile/editor.git"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export interface TypedMessage<T = any> {
|
|
2
|
+
type: string;
|
|
3
|
+
payload: T | undefined;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export class WindowMessenger<Messages extends Record<string, any>> {
|
|
7
|
+
private handlers = new Map<keyof Messages, Function[]>();
|
|
8
|
+
private targetWindow: Window;
|
|
9
|
+
private targetOrigin: string;
|
|
10
|
+
private fallbackHandler?: (data: any, event: MessageEvent) => void;
|
|
11
|
+
|
|
12
|
+
constructor(targetWindow: Window, targetOrigin: string = '*') {
|
|
13
|
+
this.targetWindow = targetWindow;
|
|
14
|
+
this.targetOrigin = targetOrigin;
|
|
15
|
+
|
|
16
|
+
window.addEventListener('message', this.handleMessage.bind(this));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private handleMessage(event: MessageEvent) {
|
|
20
|
+
const message = event.data as TypedMessage;
|
|
21
|
+
|
|
22
|
+
if (!message.type) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const handlers = this.handlers.get(message.type);
|
|
27
|
+
|
|
28
|
+
if (handlers) {
|
|
29
|
+
handlers.forEach((handler) => handler(message.payload, event));
|
|
30
|
+
} else if (this.fallbackHandler) {
|
|
31
|
+
this.fallbackHandler(event.data, event);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
send<K extends keyof Messages>(type: K, payload?: Messages[K]): void {
|
|
36
|
+
const message: TypedMessage<Messages[K]> = { type: type as string, payload };
|
|
37
|
+
this.targetWindow.postMessage(message, this.targetOrigin);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
listen<K extends keyof Messages>(type: K, handler: (data: Messages[K], event: MessageEvent) => void): () => void {
|
|
41
|
+
if (!this.handlers.has(type)) {
|
|
42
|
+
this.handlers.set(type, []);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this.handlers.get(type)!.push(handler);
|
|
46
|
+
|
|
47
|
+
return () => {
|
|
48
|
+
const handlers = this.handlers.get(type);
|
|
49
|
+
if (handlers) {
|
|
50
|
+
const index = handlers.indexOf(handler);
|
|
51
|
+
if (index > -1) {
|
|
52
|
+
handlers.splice(index, 1);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
registerFallbackHandler(handler: (data: any, event: MessageEvent) => void) {
|
|
59
|
+
this.fallbackHandler = handler;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function createIframeMessenger<T extends Record<string, any>>(iframe: HTMLIFrameElement, targetOrigin = '*') {
|
|
64
|
+
return new WindowMessenger<T>(iframe.contentWindow!, targetOrigin);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function createParentMessenger<T extends Record<string, any>>(targetOrigin = '*') {
|
|
68
|
+
if (window.parent === window) {
|
|
69
|
+
throw new Error('Not inside an iframe');
|
|
70
|
+
}
|
|
71
|
+
return new WindowMessenger<T>(window.parent, targetOrigin);
|
|
72
|
+
}
|