iframe.io 0.0.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Fabrice K.M. Ekpetse
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,2 @@
1
+ # iframe.io
2
+ Easy and friendly API to connect and interact between content window and its containing iframe
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/index.js ADDED
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ var IFrameIO = /** @class */ (function () {
15
+ function IFrameIO(options) {
16
+ if (options && typeof options !== 'object')
17
+ throw new Error('Invalid Options');
18
+ this.options = options;
19
+ this.Events = {};
20
+ this.peer = { type: 'IFRAME' };
21
+ if (options.type)
22
+ this.peer.type = options.type;
23
+ }
24
+ IFrameIO.prototype.debug = function () {
25
+ var args = [];
26
+ for (var _i = 0; _i < arguments.length; _i++) {
27
+ args[_i] = arguments[_i];
28
+ }
29
+ this.options && this.options.debug && console.log.apply(console, args);
30
+ };
31
+ IFrameIO.prototype.initiate = function (contentWindow, iframeOrigin) {
32
+ var _this = this;
33
+ // Establish a connection with an iframe containing in the current window
34
+ if (!contentWindow || !iframeOrigin)
35
+ throw new Error('Invalid Connection initiation arguments');
36
+ if (this.peer.type === 'IFRAME')
37
+ throw new Error('Expect IFRAME to <listen> and WINDOW to <initiate> a connection');
38
+ this.peer.source = contentWindow;
39
+ this.peer.origin = iframeOrigin;
40
+ window.addEventListener('message', function (_a) {
41
+ var origin = _a.origin, data = _a.data, source = _a.source;
42
+ // Check valid message
43
+ if (origin !== _this.peer.origin
44
+ || !source
45
+ || typeof data !== 'object'
46
+ || !data.hasOwnProperty('_event'))
47
+ return;
48
+ var _b = data, _event = _b._event, payload = _b.payload;
49
+ _this.debug("[".concat(_this.peer.type, "] Message: ").concat(_event), payload || '');
50
+ // Handshake or availability check events
51
+ if (_event == 'pong')
52
+ return;
53
+ // Volatile event
54
+ if (!_this.Events[_event])
55
+ return _this.debug("[".concat(_this.peer.type, "] No <").concat(_event, "> listener defined"));
56
+ // Trigger listeners
57
+ _this.Events[_event].map(function (fn) { return fn(payload); });
58
+ // Delete once event listeners
59
+ delete _this.Events[_event + '--@once'];
60
+ }, false);
61
+ this.debug("[".concat(this.peer.type, "] Initiate connection: IFrame origin <").concat(iframeOrigin, ">"));
62
+ this.emit('ping');
63
+ };
64
+ IFrameIO.prototype.listen = function (hostOrigin) {
65
+ // Listening to connection from the content window
66
+ var _this = this;
67
+ this.peer.type = 'IFRAME'; // iframe.io connection listener is automatically set as IFRAME
68
+ this.debug("[".concat(this.peer.type, "] Listening to connect").concat(hostOrigin ? ": Host <".concat(hostOrigin, ">") : ''));
69
+ window.addEventListener('message', function (_a) {
70
+ var origin = _a.origin, data = _a.data, source = _a.source;
71
+ // Check host origin where event must only come from.
72
+ if (hostOrigin && hostOrigin !== origin)
73
+ throw new Error('Invalid Event Origin');
74
+ // Check valid message
75
+ if (!source
76
+ || typeof data !== 'object'
77
+ || !data.hasOwnProperty('_event'))
78
+ return;
79
+ // Define peer source window and origin
80
+ if (!_this.peer.source) {
81
+ _this.peer = __assign(__assign({}, _this.peer), { source: source, origin: origin });
82
+ _this.debug("[".concat(_this.peer.type, "] Connect to ").concat(origin));
83
+ }
84
+ // Origin different from handshaked source origin
85
+ else if (origin !== _this.peer.origin)
86
+ throw new Error('Invalid Origin');
87
+ var _event = data._event, payload = data.payload;
88
+ _this.debug("[".concat(_this.peer.type, "] Message: ").concat(_event), payload || '');
89
+ // Handshake or availability check events
90
+ if (_event == 'ping')
91
+ return _this.emit('pong');
92
+ // Volatile event
93
+ if (!_this.Events[_event])
94
+ return _this.debug("[".concat(_this.peer.type, "] No <").concat(_event, "> listener defined"));
95
+ // Trigger listeners
96
+ _this.Events[_event].map(function (fn) { return fn(payload); });
97
+ // Delete once event listeners
98
+ delete _this.Events[_event + '--@once'];
99
+ }, false);
100
+ };
101
+ IFrameIO.prototype.emit = function (_event, payload, fn) {
102
+ if (!this.peer.source)
103
+ throw new Error('No Connection initiated');
104
+ if (typeof payload == 'function') {
105
+ fn = payload;
106
+ payload = null;
107
+ }
108
+ this.peer.source.postMessage(JSON.parse(JSON.stringify({ _event: _event, payload: payload })), this.peer.origin);
109
+ // Acknowledge/callback event listener
110
+ if (typeof fn == 'function')
111
+ this.once(_event, fn);
112
+ return this;
113
+ };
114
+ IFrameIO.prototype.on = function (_event, fn) {
115
+ // Add Event listener
116
+ if (!this.Events[_event])
117
+ this.Events[_event] = [];
118
+ this.Events[_event].push(fn);
119
+ this.debug("[".concat(this.peer.type, "] New <").concat(_event, "> listener on"));
120
+ return this;
121
+ };
122
+ IFrameIO.prototype.once = function (_event, fn) {
123
+ // Add Once Event listener
124
+ _event += '--@once';
125
+ if (!this.Events[_event])
126
+ this.Events[_event] = [];
127
+ this.Events[_event].push(fn);
128
+ this.debug("[".concat(this.peer.type, "] New <").concat(_event, " once> listener on"));
129
+ return this;
130
+ };
131
+ IFrameIO.prototype.off = function (_event, fn) {
132
+ // Remove Event listener
133
+ delete this.Events[_event];
134
+ typeof fn == 'function' && fn();
135
+ this.debug("[".concat(this.peer.type, "] <").concat(_event, "> listener off"));
136
+ return this;
137
+ };
138
+ IFrameIO.prototype.removeListeners = function (fn) {
139
+ // Clear all event listeners
140
+ this.Events = {};
141
+ typeof fn == 'function' && fn();
142
+ this.debug("[".concat(this.peer.type, "] All listeners removed"));
143
+ return this;
144
+ };
145
+ return IFrameIO;
146
+ }());
147
+ exports.default = IFrameIO;
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "iframe.io",
3
+ "version": "0.0.1",
4
+ "description": "Easy and friendly API to connect and interact between content window and its containing iframe",
5
+ "main": "./dist/index.js",
6
+ "type": "commonjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ "require": "./dist/index.js"
10
+ },
11
+ "private": false,
12
+ "scripts": {
13
+ "compile": "rimraf ./dist && tsc",
14
+ "test": "yarn run compile && yarn run test:types && yarn run test:unit",
15
+ "test:types": "tsd",
16
+ "test:unit": "nyc mocha --require ts-node/register --reporter spec --slow 200 --bail --timeout 10000 test/iframe.io.ts",
17
+ "prepack": "yarn run compile"
18
+ },
19
+ "devDependencies": {
20
+ "@types/mocha": "^9.1.0",
21
+ "expect.js": "^0.3.1",
22
+ "mocha": "^9.2.0",
23
+ "rimraf": "^3.0.2",
24
+ "typescript": "^4.5.5"
25
+ },
26
+ "files": [
27
+ "dist/"
28
+ ],
29
+ "directories": {
30
+ "example": "example/",
31
+ "test": "test/"
32
+ },
33
+ "license": "MIT",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git://github.com/fabrice8/iframe.io"
37
+ },
38
+ "keywords": [
39
+ "realtime",
40
+ "iframe",
41
+ "browser",
42
+ "events",
43
+ "io"
44
+ ],
45
+ "author": "Fabrice K.M.E"
46
+ }