jails.stdlib 1.1.0 โ†’ 1.2.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.
@@ -0,0 +1,97 @@
1
+ # channel
2
+
3
+ ```ts
4
+ Channel({
5
+ target?: Window | HTMLIFrameElement,
6
+ accept?: string[],
7
+ origin?: string
8
+ })
9
+ ```
10
+
11
+
12
+ # ๐Ÿ“ฌ Channel API Documentation
13
+
14
+ The `channel` utility provides a lightweight, secure interface for sending and receiving messages between windows or iframes using `postMessage`.
15
+
16
+ ---
17
+
18
+ ## โœ… Features
19
+
20
+ * Easy dispatch and subscription to custom message actions.
21
+ * Secure origin validation.
22
+ * Works with iframes or separate window contexts.
23
+
24
+ ---
25
+
26
+ ## ๐Ÿงช Usage
27
+
28
+ ### Parent Page
29
+
30
+ ```ts
31
+ import { Channel } from 'jails.stdlib/channel'
32
+
33
+ const channel = Channel({
34
+ target: document.getElementById('myIframe'), // <iframe id="myIframe" />
35
+ accept: ['*'] // Allowed origins
36
+ })
37
+
38
+ // Send message to iframe
39
+ channel.send('hello', { user: 'Alice' })
40
+
41
+ // Add more listeners later
42
+ channel.on('hi', (data) => {
43
+ console.log('Iframe said hi!', data)
44
+ })
45
+ ```
46
+
47
+ ### Iframe Page
48
+
49
+ ```ts
50
+ const channel = Channel({
51
+ target: window.parent,
52
+ accept: ['*']
53
+ })
54
+ ```
55
+
56
+ ---
57
+
58
+ ## ๐Ÿงน API
59
+
60
+ ### `Channel(options)`
61
+
62
+ Creates a new Channel instance.
63
+
64
+ #### Options:
65
+
66
+ | Option | Type | Description |
67
+ | --------- | ----------------------------- | --------------------------------------- |
68
+ | `target` | `Window \| HTMLIFrameElement` | Target to send messages to |
69
+ | `accept` | `string[]` | Allowed origins (`['*']` to accept all) |
70
+ | `origin` | `string` | The origin used when sending messages, `default`: location.origin |
71
+
72
+ ---
73
+
74
+ ### `.emit(event: string, payload?: any)`
75
+
76
+ Sends a message to the target window.
77
+
78
+ ```ts
79
+ channel.emit('say:hi', { name: 'Bob' })
80
+ ```
81
+
82
+ ---
83
+
84
+ ### `.on(event: string, callback: function )`
85
+
86
+ Listen to an event from the target window.
87
+
88
+ ```ts
89
+ channel.on('logout', () => console.log('Logging out...'))
90
+ ```
91
+
92
+ ---
93
+
94
+ ## ๐Ÿ”’ Security Notes
95
+
96
+ * Always **specify allowed origins** in `accept` to avoid cross-site scripting risks.
97
+ * Avoid using `'*'` unless absolutely necessary (e.g., during development).
@@ -0,0 +1,9 @@
1
+ export declare const Channel: ({ target, accept, origin }: {
2
+ target?: any;
3
+ accept?: any;
4
+ origin?: string;
5
+ }) => {
6
+ on(name: any, callback: any): void;
7
+ emit(event: any, ...payload: any[]): void;
8
+ remove(name: any): void;
9
+ };
@@ -0,0 +1,30 @@
1
+ const a = ({
2
+ target: o = null,
3
+ accept: i = [],
4
+ origin: d = location.origin
5
+ }) => {
6
+ const t = (o == null ? void 0 : o.contentWindow) || o, s = {};
7
+ return window.addEventListener("message", (n) => {
8
+ if (i.includes("*") || i.includes(n.origin)) {
9
+ const { payload: e, event: l } = n.data;
10
+ s[l] && s[l].apply(null, e);
11
+ } else
12
+ throw {
13
+ type: "ACCESS DENIED",
14
+ message: "Cant receive message from: " + n.origin
15
+ };
16
+ }), {
17
+ on(n, e) {
18
+ s[n] = e;
19
+ },
20
+ emit(n, ...e) {
21
+ t.postMessage({ event: n, payload: e }, d);
22
+ },
23
+ remove(n) {
24
+ delete s[n];
25
+ }
26
+ };
27
+ };
28
+ export {
29
+ a as Channel
30
+ };
@@ -0,0 +1,39 @@
1
+
2
+ export const Channel = ({
3
+ target = null as any,
4
+ accept = [] as any,
5
+ origin = location.origin
6
+ }) => {
7
+
8
+ const win = target?.contentWindow || target
9
+ const events = {}
10
+
11
+ window.addEventListener('message', (e) => {
12
+ if( accept.includes('*') || accept.includes(e.origin) ) {
13
+ const { payload, event } = e.data
14
+ if( events[event] ) {
15
+ events[event].apply(null, payload)
16
+ }
17
+ } else {
18
+ throw {
19
+ type : 'ACCESS DENIED',
20
+ message : 'Cant receive message from: ' + e.origin
21
+ }
22
+ }
23
+ })
24
+
25
+ return {
26
+
27
+ on(name, callback) {
28
+ events[name] = callback
29
+ },
30
+
31
+ emit(event, ...payload) {
32
+ win.postMessage({ event, payload }, origin)
33
+ },
34
+
35
+ remove(name) {
36
+ delete events[name]
37
+ }
38
+ }
39
+ }
@@ -0,0 +1 @@
1
+ (function(n,i){typeof exports=="object"&&typeof module!="undefined"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(n=typeof globalThis!="undefined"?globalThis:n||self,i(n.channel={}))})(this,(function(n){"use strict";const i=({target:s=null,accept:d=[],origin:f=location.origin})=>{const u=(s==null?void 0:s.contentWindow)||s,t={};return window.addEventListener("message",e=>{if(d.includes("*")||d.includes(e.origin)){const{payload:o,event:l}=e.data;t[l]&&t[l].apply(null,o)}else throw{type:"ACCESS DENIED",message:"Cant receive message from: "+e.origin}}),{on(e,o){t[e]=o},emit(e,...o){u.postMessage({event:e,payload:o},f)},remove(e){delete t[e]}}};n.Channel=i,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jails.stdlib",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "The Jails Standard Library",
5
5
  "types": "index.d.ts",
6
6
  "repository": {
@@ -1,109 +0,0 @@
1
- # messenger
2
-
3
- ```ts
4
- messenger({
5
- target?: Window | HTMLIFrameElement,
6
- accept?: string[],
7
- actions?: Record<string, (payload: any) => void>,
8
- origin?: string
9
- })
10
- ```
11
- # ๐Ÿ“ฌ Messenger API Documentation
12
-
13
- The `messenger` utility provides a lightweight, secure interface for sending and receiving messages between windows or iframes using `postMessage`.
14
-
15
- ---
16
-
17
- ## โœ… Features
18
-
19
- * Easy dispatch and subscription to custom message actions.
20
- * Secure origin validation.
21
- * Works with iframes or separate window contexts.
22
- * Extensible via `subscribe`.
23
-
24
- ---
25
-
26
- ## ๐Ÿงช Usage
27
-
28
- ### Parent Page
29
-
30
- ```ts
31
- import { messenger } from 'jails.stdlib/messenger'
32
-
33
- const msg = messenger({
34
- target: document.getElementById('myIframe'), // <iframe id="myIframe" />
35
- accept: ['https://child-app.com'], // Allowed origins
36
- actions: {
37
- reply(data) {
38
- console.log('Received from iframe:', data)
39
- }
40
- }
41
- })
42
-
43
- // Send message to iframe
44
- msg.dispatch('init', { user: 'Alice' })
45
-
46
- // Add more listeners later
47
- msg.subscribe({
48
- status: (msg) => console.log('Status update:', msg)
49
- })
50
- ```
51
-
52
- ### Iframe Page
53
-
54
- ```ts
55
- const msg = messenger({
56
- target: window.parent,
57
- accept: ['https://parent-app.com'],
58
- actions: {
59
- init(data){
60
- console.log('Init from parent:', data)
61
- msg.dispatch('reply', { received: true })
62
- }
63
- }
64
- })
65
- ```
66
-
67
- ---
68
-
69
- ## ๐Ÿงน API
70
-
71
- ### `messenger(options)`
72
-
73
- Creates a new Messenger instance.
74
-
75
- #### Options:
76
-
77
- | Option | Type | Description |
78
- | --------- | ----------------------------- | --------------------------------------- |
79
- | `target` | `Window \| HTMLIFrameElement` | Target to send messages to |
80
- | `accept` | `string[]` | Allowed origins (`['*']` to accept all) |
81
- | `actions` | `Record<string, Function>` | Message handlers keyed by action name |
82
- | `origin` | `string` | The origin used when sending messages |
83
-
84
- ---
85
-
86
- ### `.dispatch(action: string, payload?: any)`
87
-
88
- Sends a message to the target window.
89
-
90
- ```ts
91
- msg.dispatch('sayHello', { name: 'Bob' })
92
- ```
93
-
94
- ---
95
-
96
- ### `.subscribe(newActions: Record<string, Function>)`
97
-
98
- Adds more actions to the messenger at runtime.
99
-
100
- ```ts
101
- msg.subscribe({ logout: () => console.log('Logging out...') })
102
- ```
103
-
104
- ---
105
-
106
- ## ๐Ÿ”’ Security Notes
107
-
108
- * Always **specify allowed origins** in `accept` to avoid cross-site scripting risks.
109
- * Avoid using `'*'` unless absolutely necessary (e.g., during development).
@@ -1,9 +0,0 @@
1
- export declare const messenger: ({ target, accept, actions, origin }?: {
2
- target?: any;
3
- accept?: any;
4
- actions?: any;
5
- origin?: string;
6
- }) => {
7
- dispatch(action: string, payload?: any): void;
8
- subscribe(actions_: any): void;
9
- };
@@ -1,39 +0,0 @@
1
- var w = Object.defineProperty;
2
- var l = Object.getOwnPropertySymbols;
3
- var a = Object.prototype.hasOwnProperty, p = Object.prototype.propertyIsEnumerable;
4
- var r = (n, i, s) => i in n ? w(n, i, { enumerable: !0, configurable: !0, writable: !0, value: s }) : n[i] = s, d = (n, i) => {
5
- for (var s in i || (i = {}))
6
- a.call(i, s) && r(n, s, i[s]);
7
- if (l)
8
- for (var s of l(i))
9
- p.call(i, s) && r(n, s, i[s]);
10
- return n;
11
- };
12
- const f = ({
13
- target: n = null,
14
- accept: i = [],
15
- actions: s = {},
16
- origin: c = location.origin
17
- } = {}) => {
18
- const m = (n == null ? void 0 : n.contentWindow) || n;
19
- return window.addEventListener("message", (e) => {
20
- if (i.includes("*") || i.includes(e.origin)) {
21
- const { action: o, payload: u } = e.data;
22
- o in s && s[o](u);
23
- } else
24
- throw {
25
- type: "ACCESS DENIED",
26
- message: "Cant receive message from: " + e.origin
27
- };
28
- }), {
29
- dispatch(e, o) {
30
- m.postMessage({ action: e, payload: o }, c);
31
- },
32
- subscribe(e) {
33
- s = d(d({}, e), s);
34
- }
35
- };
36
- };
37
- export {
38
- f as messenger
39
- };
@@ -1,38 +0,0 @@
1
-
2
- export const messenger = ({
3
-
4
- target = null as any,
5
- accept = [] as any,
6
- actions = {} as any,
7
- origin = location.origin
8
-
9
- } = {}) => {
10
-
11
- const win = target?.contentWindow || target
12
-
13
- window.addEventListener('message', ( event ) => {
14
-
15
- if( accept.includes('*') || accept.includes(event.origin) ) {
16
- const { action, payload } = event.data
17
- if( action in actions ) {
18
- actions[ action ]( payload as any )
19
- }
20
- } else {
21
- throw {
22
- type : 'ACCESS DENIED',
23
- message : 'Cant receive message from: ' + event.origin
24
- }
25
- }
26
- })
27
-
28
- return {
29
-
30
- dispatch( action: string, payload?: any ) {
31
- win.postMessage({ action, payload }, origin)
32
- },
33
-
34
- subscribe( actions_: any ) {
35
- actions = { ...actions_, ...actions }
36
- }
37
- }
38
- }
@@ -1 +0,0 @@
1
- (function(i,e){typeof exports=="object"&&typeof module!="undefined"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(i=typeof globalThis!="undefined"?globalThis:i||self,e(i.messenger={}))})(this,(function(i){"use strict";var p=Object.defineProperty;var u=Object.getOwnPropertySymbols;var g=Object.prototype.hasOwnProperty,y=Object.prototype.propertyIsEnumerable;var l=(i,e,n)=>e in i?p(i,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):i[e]=n,f=(i,e)=>{for(var n in e||(e={}))g.call(e,n)&&l(i,n,e[n]);if(u)for(var n of u(e))y.call(e,n)&&l(i,n,e[n]);return i};const e=({target:n=null,accept:t=[],actions:o={},origin:r=location.origin}={})=>{const c=(n==null?void 0:n.contentWindow)||n;return window.addEventListener("message",s=>{if(t.includes("*")||t.includes(s.origin)){const{action:d,payload:m}=s.data;d in o&&o[d](m)}else throw{type:"ACCESS DENIED",message:"Cant receive message from: "+s.origin}}),{dispatch(s,d){c.postMessage({action:s,payload:d},r)},subscribe(s){o=f(f({},s),o)}}};i.messenger=e,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));