rimless 0.1.6 → 0.2.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 +2 -2
- package/README.md +42 -43
- package/lib/guest.d.ts +6 -6
- package/lib/helpers.d.ts +26 -26
- package/lib/host.d.ts +14 -15
- package/lib/index.d.ts +4 -3
- package/lib/rimless.js +159 -0
- package/lib/rimless.js.map +1 -0
- package/lib/rimless.min.js +1 -1
- package/lib/rimless.min.js.map +1 -1
- package/lib/rpc.d.ts +40 -40
- package/lib/types.d.ts +52 -52
- package/package.json +47 -78
- package/lib/index.js +0 -2307
- package/lib/worker.d.ts +0 -6
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) Aurélien Franky
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -12,12 +12,19 @@
|
|
|
12
12
|
|
|
13
13
|
[![npm][npm-image]][npm-url]
|
|
14
14
|
[![Commitizen friendly][commitizen-image]][commitizen-url]
|
|
15
|
+

|
|
15
16
|
|
|
16
17
|
> Rimless makes event based communication easy with a promise-based API wrapping [postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage). Works with both **iframes** and **webworkers**.
|
|
17
18
|
|
|
18
19
|
You can use `rimless` to call remote procedures, exchange data or expose local functions with **iframes**/**webworkers**.
|
|
19
20
|
|
|
20
|
-
You can see it in action
|
|
21
|
+
You can see it in action in the code sandbox below:
|
|
22
|
+
|
|
23
|
+
<a href="https://codesandbox.io/p/sandbox/3qrqfl">
|
|
24
|
+
|
|
25
|
+

|
|
26
|
+
|
|
27
|
+
</a>
|
|
21
28
|
|
|
22
29
|
## Installation
|
|
23
30
|
|
|
@@ -30,7 +37,7 @@ $ npm i -S rimless
|
|
|
30
37
|
or from a CDN
|
|
31
38
|
|
|
32
39
|
```html
|
|
33
|
-
<script src="https://unpkg.com/rimless/
|
|
40
|
+
<script src="https://unpkg.com/rimless/dist/rimless.min.js"></script>
|
|
34
41
|
```
|
|
35
42
|
|
|
36
43
|
## Example Usage
|
|
@@ -38,39 +45,42 @@ or from a CDN
|
|
|
38
45
|
**in the host website**
|
|
39
46
|
|
|
40
47
|
```js
|
|
41
|
-
import { host }
|
|
48
|
+
import { host } from "rimless";
|
|
42
49
|
|
|
43
50
|
const connection = await host.connect(iframe, {
|
|
44
|
-
|
|
45
|
-
|
|
51
|
+
someHostVariable: 12,
|
|
52
|
+
someHostFunction: (value) => `hello ${value}`,
|
|
46
53
|
});
|
|
47
54
|
|
|
48
55
|
// access variables on the iframe
|
|
49
|
-
console.log(connection.remote.
|
|
56
|
+
console.log(connection.remote.someGuestVariable); // 42
|
|
50
57
|
|
|
51
58
|
// call remote procedures on the iframe
|
|
52
|
-
const result = await connection.remote.
|
|
53
|
-
|
|
59
|
+
const result = await connection.remote.someGuestFunction("here");
|
|
60
|
+
|
|
61
|
+
console.log(result); // hello here
|
|
54
62
|
|
|
55
63
|
// close the connection
|
|
56
64
|
connection.close();
|
|
57
65
|
```
|
|
66
|
+
|
|
58
67
|
**in the iframe**
|
|
59
68
|
|
|
60
69
|
```js
|
|
61
|
-
import { guest }
|
|
70
|
+
import { guest } from "rimless";
|
|
62
71
|
|
|
63
72
|
const connection = await guest.connect({
|
|
64
|
-
|
|
65
|
-
|
|
73
|
+
someGuestVariable: 42,
|
|
74
|
+
someGuestFunction: (value) => `hello ${value}`,
|
|
66
75
|
});
|
|
67
76
|
|
|
68
77
|
// access variables on the host
|
|
69
|
-
console.log(connection.remote.
|
|
78
|
+
console.log(connection.remote.someHostVariable); // 12
|
|
70
79
|
|
|
71
80
|
// call remote procedures on host
|
|
72
|
-
const res = await connection.remote.
|
|
73
|
-
|
|
81
|
+
const res = await connection.remote.someHostFunction("there");
|
|
82
|
+
|
|
83
|
+
console.log(res); // hello there
|
|
74
84
|
|
|
75
85
|
// close the connection
|
|
76
86
|
connection.close();
|
|
@@ -83,7 +93,7 @@ connection.close();
|
|
|
83
93
|
This is how you can **connect your website** to an iframe or webworker:
|
|
84
94
|
|
|
85
95
|
```js
|
|
86
|
-
import { host }
|
|
96
|
+
import { host } from "rimless";
|
|
87
97
|
|
|
88
98
|
const iframe = document.getElementById("myIframe");
|
|
89
99
|
const worker = new Worker("myWorker");
|
|
@@ -100,7 +110,7 @@ You also need to **connect your iframe/webworker** to the host website.
|
|
|
100
110
|
Usage from an iframe:
|
|
101
111
|
|
|
102
112
|
```js
|
|
103
|
-
import { guest }
|
|
113
|
+
import { guest } from "rimless";
|
|
104
114
|
|
|
105
115
|
// connect to the parent website
|
|
106
116
|
guest.connect();
|
|
@@ -122,7 +132,7 @@ guest.connect();
|
|
|
122
132
|
To do anything meaningful with this connection you need to provide a schema that defines **the API** of the host/iframe/webworker. Any serializeable values as well as functions are ok to use. In the example below the host website provides a function that will update its background color when invoked.
|
|
123
133
|
|
|
124
134
|
```js
|
|
125
|
-
import { host }
|
|
135
|
+
import { host } from "rimless";
|
|
126
136
|
|
|
127
137
|
const api = {
|
|
128
138
|
setColor: (color) => {
|
|
@@ -142,7 +152,7 @@ The api schema must be passed on connection, the same applies to the `iframe/web
|
|
|
142
152
|
With the host API exposed we can now invoke the remote procedure from the iframe.
|
|
143
153
|
|
|
144
154
|
```js
|
|
145
|
-
import { guest }
|
|
155
|
+
import { guest } from "rimless";
|
|
146
156
|
|
|
147
157
|
// connect returns a promise that resolves in a connection object
|
|
148
158
|
// `connection.remote` contains the api you can invoke
|
|
@@ -156,12 +166,13 @@ guest.connect().then((connection) => {
|
|
|
156
166
|
Closing a connection will remove all event listeners that were registered.
|
|
157
167
|
|
|
158
168
|
```js
|
|
159
|
-
import { guest }
|
|
169
|
+
import { guest } from "rimless";
|
|
160
170
|
|
|
161
171
|
guest.connect().then((connection) => {
|
|
162
172
|
connection.close();
|
|
163
173
|
});
|
|
164
174
|
```
|
|
175
|
+
|
|
165
176
|
---
|
|
166
177
|
|
|
167
178
|
## How it Works
|
|
@@ -200,7 +211,7 @@ This library is inspired by [Postmate](https://www.npmjs.com/package/postmate) a
|
|
|
200
211
|
|
|
201
212
|
## API
|
|
202
213
|
|
|
203
|
-
Rimless exports two objects: `host` and
|
|
214
|
+
Rimless exports two objects: `host` and `guest`.
|
|
204
215
|
|
|
205
216
|
> ### `host.connect`
|
|
206
217
|
|
|
@@ -208,16 +219,15 @@ Connect your website to a "guest" (iframe/webworker).
|
|
|
208
219
|
|
|
209
220
|
```js
|
|
210
221
|
host.connect(iframe, {
|
|
211
|
-
log: (value) => console.log(value)
|
|
222
|
+
log: (value) => console.log(value),
|
|
212
223
|
});
|
|
213
224
|
```
|
|
214
225
|
|
|
215
|
-
| Name
|
|
216
|
-
|
|
|
217
|
-
| `guest`
|
|
218
|
-
| `schema`
|
|
219
|
-
| `options` | `object`
|
|
220
|
-
|
|
226
|
+
| Name | Type | Description | Required |
|
|
227
|
+
| --------- | ------------------------------- | ------------------------------------ | -------- |
|
|
228
|
+
| `guest` | `HTMLIFrameElement` or `Worker` | Target of the connection | required |
|
|
229
|
+
| `schema` | `object` | schema of the api you want to expose | - |
|
|
230
|
+
| `options` | `object` | - | - |
|
|
221
231
|
|
|
222
232
|
> ### `guest.connect`
|
|
223
233
|
|
|
@@ -225,28 +235,17 @@ Connect a "guest" to your website. The guest connection automatically happens ba
|
|
|
225
235
|
|
|
226
236
|
```js
|
|
227
237
|
guest.connect({
|
|
228
|
-
log: (value) => console.log(value)
|
|
238
|
+
log: (value) => console.log(value),
|
|
229
239
|
});
|
|
230
240
|
```
|
|
231
241
|
|
|
232
|
-
| Name
|
|
233
|
-
|
|
|
234
|
-
| `schema`
|
|
235
|
-
| `options` | `object` | -
|
|
242
|
+
| Name | Type | Description | Default |
|
|
243
|
+
| --------- | -------- | ------------------------------------ | ------- |
|
|
244
|
+
| `schema` | `object` | schema of the api you want to expose | - |
|
|
245
|
+
| `options` | `object` | - | - |
|
|
236
246
|
|
|
237
247
|
---
|
|
238
248
|
|
|
239
|
-
## Contributing
|
|
240
|
-
|
|
241
|
-
We use the [airbnb style guide](https://github.com/airbnb/javascript) when writing javascript, with
|
|
242
|
-
some minor modifications. Make sure eslint is installed and running before making changes, as it
|
|
243
|
-
will ensure your coding style matches that of the project.
|
|
244
|
-
|
|
245
|
-
We use [commitizen](https://github.com/commitizen/cz-cli) and
|
|
246
|
-
[angular's conventional changelog](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#commits)
|
|
247
|
-
to enforce a consistent commit format. When writing commits, make sure you run `npm run commit`
|
|
248
|
-
instead of `git commit`.
|
|
249
|
-
|
|
250
249
|
## License
|
|
251
250
|
|
|
252
251
|
[MIT](license-url)
|
package/lib/guest.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { IConnection, ISchema } from
|
|
2
|
-
declare function connect(schema?: ISchema
|
|
3
|
-
declare const _default: {
|
|
4
|
-
connect: typeof connect;
|
|
5
|
-
};
|
|
6
|
-
export default _default;
|
|
1
|
+
import { IConnection, ISchema } from './types';
|
|
2
|
+
declare function connect(schema?: ISchema): Promise<IConnection>;
|
|
3
|
+
declare const _default: {
|
|
4
|
+
connect: typeof connect;
|
|
5
|
+
};
|
|
6
|
+
export default _default;
|
package/lib/helpers.d.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
export declare const CONNECTION_TIMEOUT = 1000;
|
|
2
|
-
/**
|
|
3
|
-
* check if the remote is trusted
|
|
4
|
-
*
|
|
5
|
-
* @param event
|
|
6
|
-
*/
|
|
7
|
-
export declare function isTrustedRemote(
|
|
8
|
-
/**
|
|
9
|
-
* check if run in a webworker
|
|
10
|
-
*
|
|
11
|
-
* @param event
|
|
12
|
-
*/
|
|
13
|
-
export declare function isWorker(): boolean;
|
|
14
|
-
/**
|
|
15
|
-
* we cannot send functions through postMessage
|
|
16
|
-
* extract the path to all functions in the schema
|
|
17
|
-
*
|
|
18
|
-
* @param obj
|
|
19
|
-
*/
|
|
20
|
-
export declare function extractMethods(obj: any): string[];
|
|
21
|
-
/**
|
|
22
|
-
* convert the url into an origin (remove paths)
|
|
23
|
-
*
|
|
24
|
-
* @param url
|
|
25
|
-
*/
|
|
26
|
-
export declare function getOriginFromURL(url: string | null): string;
|
|
1
|
+
export declare const CONNECTION_TIMEOUT = 1000;
|
|
2
|
+
/**
|
|
3
|
+
* check if the remote is trusted
|
|
4
|
+
*
|
|
5
|
+
* @param event
|
|
6
|
+
*/
|
|
7
|
+
export declare function isTrustedRemote(_event: any): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* check if run in a webworker
|
|
10
|
+
*
|
|
11
|
+
* @param event
|
|
12
|
+
*/
|
|
13
|
+
export declare function isWorker(): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* we cannot send functions through postMessage
|
|
16
|
+
* extract the path to all functions in the schema
|
|
17
|
+
*
|
|
18
|
+
* @param obj
|
|
19
|
+
*/
|
|
20
|
+
export declare function extractMethods(obj: any): string[];
|
|
21
|
+
/**
|
|
22
|
+
* convert the url into an origin (remove paths)
|
|
23
|
+
*
|
|
24
|
+
* @param url
|
|
25
|
+
*/
|
|
26
|
+
export declare function getOriginFromURL(url: string | null): string;
|
package/lib/host.d.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { IConnection, ISchema } from
|
|
2
|
-
/**
|
|
3
|
-
* Perform a handshake with the target iframe, when the handshake is confirmed
|
|
4
|
-
* resolve the connection object containing RPCs and properties
|
|
5
|
-
*
|
|
6
|
-
* @param iframe
|
|
7
|
-
* @param schema
|
|
8
|
-
* @
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
declare
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export default _default;
|
|
1
|
+
import { IConnection, ISchema } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Perform a handshake with the target iframe, when the handshake is confirmed
|
|
4
|
+
* resolve the connection object containing RPCs and properties
|
|
5
|
+
*
|
|
6
|
+
* @param iframe
|
|
7
|
+
* @param schema
|
|
8
|
+
* @returns Promise
|
|
9
|
+
*/
|
|
10
|
+
declare function connect(guest: HTMLIFrameElement | Worker, schema?: ISchema): Promise<IConnection>;
|
|
11
|
+
declare const _default: {
|
|
12
|
+
connect: typeof connect;
|
|
13
|
+
};
|
|
14
|
+
export default _default;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import guest from
|
|
2
|
-
import host from
|
|
3
|
-
export { host, guest };
|
|
1
|
+
import { default as guest } from './guest';
|
|
2
|
+
import { default as host } from './host';
|
|
3
|
+
export { host, guest };
|
|
4
|
+
export * from './types';
|
package/lib/rimless.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import _ from "lodash.get";
|
|
2
|
+
import H from "lodash.set";
|
|
3
|
+
import { nanoid as A } from "nanoid";
|
|
4
|
+
function M() {
|
|
5
|
+
return typeof WorkerGlobalScope < "u" && self instanceof WorkerGlobalScope;
|
|
6
|
+
}
|
|
7
|
+
function L(e) {
|
|
8
|
+
const o = [];
|
|
9
|
+
return function c(n, r = "") {
|
|
10
|
+
Object.keys(n).forEach((t) => {
|
|
11
|
+
const s = r ? `${r}.${t}` : t;
|
|
12
|
+
n[t] === Object(n[t]) && c(n[t], s), typeof n[t] == "function" && o.push(s);
|
|
13
|
+
});
|
|
14
|
+
}(e), o;
|
|
15
|
+
}
|
|
16
|
+
const T = /^(https?:|file:)?\/\/([^/:]+)?(:(\d+))?/, D = { "http:": "80", "https:": "443" };
|
|
17
|
+
function C(e) {
|
|
18
|
+
const { location: o } = document, c = T.exec(e || "");
|
|
19
|
+
let n, r, t;
|
|
20
|
+
if (c ? [, n = o.protocol, r, , t] = c : (n = o.protocol, r = o.hostname, t = o.port), n === "file:")
|
|
21
|
+
return "null";
|
|
22
|
+
const s = t && t !== D[n] ? `:${t}` : "";
|
|
23
|
+
return `${n}//${r}${s}`;
|
|
24
|
+
}
|
|
25
|
+
var a = /* @__PURE__ */ ((e) => (e.MESSAGE = "message", e))(a || {}), S = /* @__PURE__ */ ((e) => (e.HANDSHAKE_REQUEST = "RIMLESS/HANDSHAKE_REQUEST", e.HANDSHAKE_REPLY = "RIMLESS/HANDSHAKE_REPLY", e.RPC_REQUEST = "RIMLESS/RPC_REQUEST", e.RPC_RESOLVE = "RIMLESS/RPC_RESOLVE", e.RPC_REJECT = "RIMLESS/RPC_REJECT", e))(S || {});
|
|
26
|
+
function P(e = {}, o = [], c, n) {
|
|
27
|
+
const r = [];
|
|
28
|
+
return o.forEach((t) => {
|
|
29
|
+
async function s(i) {
|
|
30
|
+
const { action: l, callID: E, connectionID: f, callName: u, args: p = [] } = i.data;
|
|
31
|
+
if (l !== S.RPC_REQUEST || !E || !u || u !== t || f !== c) return;
|
|
32
|
+
const d = {
|
|
33
|
+
action: S.RPC_RESOLVE,
|
|
34
|
+
callID: E,
|
|
35
|
+
callName: u,
|
|
36
|
+
connectionID: f,
|
|
37
|
+
error: null,
|
|
38
|
+
result: null
|
|
39
|
+
};
|
|
40
|
+
try {
|
|
41
|
+
const R = await _(e, t)(...p);
|
|
42
|
+
d.result = JSON.parse(JSON.stringify(R));
|
|
43
|
+
} catch (R) {
|
|
44
|
+
d.error = JSON.parse(JSON.stringify(R, Object.getOwnPropertyNames(R)));
|
|
45
|
+
}
|
|
46
|
+
n ? n.postMessage(d) : M() ? self.postMessage(d) : i.source.postMessage(d, i.origin);
|
|
47
|
+
}
|
|
48
|
+
n ? n.addEventListener(a.MESSAGE, s) : self.addEventListener(a.MESSAGE, s), r.push(() => self.removeEventListener(a.MESSAGE, s));
|
|
49
|
+
}), () => r.forEach((t) => t());
|
|
50
|
+
}
|
|
51
|
+
function y(e, o, c, n = [], r) {
|
|
52
|
+
return (...t) => new Promise((s, i) => {
|
|
53
|
+
const l = A();
|
|
54
|
+
function E(u) {
|
|
55
|
+
const { callID: p, connectionID: d, callName: R, result: N, error: I, action: g } = u.data;
|
|
56
|
+
if (!(!p || !R) && R === e && d === o) {
|
|
57
|
+
if (g === S.RPC_RESOLVE) return s(N);
|
|
58
|
+
if (g === S.RPC_REJECT) return i(I);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const f = {
|
|
62
|
+
action: S.RPC_REQUEST,
|
|
63
|
+
args: JSON.parse(JSON.stringify(t)),
|
|
64
|
+
callID: l,
|
|
65
|
+
callName: e,
|
|
66
|
+
connectionID: o
|
|
67
|
+
};
|
|
68
|
+
r ? r.addEventListener(a.MESSAGE, E) : self.addEventListener(a.MESSAGE, E), n.push(() => self.removeEventListener(a.MESSAGE, E)), r ? r.postMessage(f) : M() ? self.postMessage(f) : (c.source || c.target).postMessage(f, c.origin);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function O(e = {}, o = [], c, n, r) {
|
|
72
|
+
const t = { ...e }, s = [];
|
|
73
|
+
return o.forEach((i) => {
|
|
74
|
+
const l = y(i, c, n, s, r);
|
|
75
|
+
H(t, i, l);
|
|
76
|
+
}), {
|
|
77
|
+
remote: t,
|
|
78
|
+
unregisterRemote: () => s.forEach((i) => i())
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const G = 10, J = 3e3;
|
|
82
|
+
let m = null, h = !1;
|
|
83
|
+
function w(e = {}) {
|
|
84
|
+
return new Promise((o, c) => {
|
|
85
|
+
const n = L(e);
|
|
86
|
+
function r(s) {
|
|
87
|
+
if (s.data.action !== S.HANDSHAKE_REPLY) return;
|
|
88
|
+
const i = P(e, n, s.data.connectionID), { remote: l, unregisterRemote: E } = O(
|
|
89
|
+
s.data.schema,
|
|
90
|
+
s.data.methods,
|
|
91
|
+
s.data.connectionID,
|
|
92
|
+
s
|
|
93
|
+
), f = () => {
|
|
94
|
+
self.removeEventListener(a.MESSAGE, r), E(), i();
|
|
95
|
+
};
|
|
96
|
+
return h = !0, o({ remote: l, close: f });
|
|
97
|
+
}
|
|
98
|
+
self.addEventListener(a.MESSAGE, r);
|
|
99
|
+
const t = {
|
|
100
|
+
action: S.HANDSHAKE_REQUEST,
|
|
101
|
+
methods: n,
|
|
102
|
+
schema: JSON.parse(JSON.stringify(e))
|
|
103
|
+
};
|
|
104
|
+
m = setInterval(() => {
|
|
105
|
+
if (h) return clearInterval(m);
|
|
106
|
+
M() ? self.postMessage(t) : window.parent.postMessage(t, "*");
|
|
107
|
+
}, G), setTimeout(() => {
|
|
108
|
+
h || c("connection timeout");
|
|
109
|
+
}, J);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
const V = {
|
|
113
|
+
connect: w
|
|
114
|
+
};
|
|
115
|
+
function U(e, o) {
|
|
116
|
+
const c = e.getAttribute("src"), n = C(c), r = o.origin === n, t = o.source === e.contentWindow;
|
|
117
|
+
return r && t;
|
|
118
|
+
}
|
|
119
|
+
function Q(e, o = {}) {
|
|
120
|
+
if (!e) throw new Error("a target is required");
|
|
121
|
+
const c = e.onerror !== void 0 && e.onmessage !== void 0, n = c ? e : window;
|
|
122
|
+
return new Promise((r) => {
|
|
123
|
+
const t = A();
|
|
124
|
+
function s(i) {
|
|
125
|
+
if (!c && !U(e, i) || i.data.action !== S.HANDSHAKE_REQUEST) return;
|
|
126
|
+
const l = L(o), E = P(
|
|
127
|
+
o,
|
|
128
|
+
l,
|
|
129
|
+
t,
|
|
130
|
+
c ? e : void 0
|
|
131
|
+
), { remote: f, unregisterRemote: u } = O(
|
|
132
|
+
i.data.schema,
|
|
133
|
+
i.data.methods,
|
|
134
|
+
t,
|
|
135
|
+
i,
|
|
136
|
+
c ? e : void 0
|
|
137
|
+
), p = {
|
|
138
|
+
action: S.HANDSHAKE_REPLY,
|
|
139
|
+
connectionID: t,
|
|
140
|
+
methods: l,
|
|
141
|
+
schema: JSON.parse(JSON.stringify(o))
|
|
142
|
+
};
|
|
143
|
+
return c ? e.postMessage(p) : i.source.postMessage(p, i.origin), r({ remote: f, close: () => {
|
|
144
|
+
n.removeEventListener(a.MESSAGE, s), u(), E();
|
|
145
|
+
} });
|
|
146
|
+
}
|
|
147
|
+
n.addEventListener(a.MESSAGE, s);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
const $ = {
|
|
151
|
+
connect: Q
|
|
152
|
+
};
|
|
153
|
+
export {
|
|
154
|
+
S as actions,
|
|
155
|
+
a as events,
|
|
156
|
+
V as guest,
|
|
157
|
+
$ as host
|
|
158
|
+
};
|
|
159
|
+
//# sourceMappingURL=rimless.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rimless.js","sources":["../src/helpers.ts","../src/types.ts","../src/rpc.ts","../src/guest.ts","../src/host.ts"],"sourcesContent":["export const CONNECTION_TIMEOUT = 1000;\n\n/**\n * check if the remote is trusted\n *\n * @param event\n */\nexport function isTrustedRemote(_event: any) {\n // TODO: implement\n return true;\n}\n\n/**\n * check if run in a webworker\n *\n * @param event\n */\nexport function isWorker() {\n return typeof WorkerGlobalScope !== \"undefined\" && self instanceof WorkerGlobalScope;\n}\n\n/**\n * we cannot send functions through postMessage\n * extract the path to all functions in the schema\n *\n * @param obj\n */\nexport function extractMethods(obj: any) {\n const paths: string[] = [];\n (function parse(obj: any, path = \"\") {\n Object.keys(obj).forEach((prop) => {\n const propPath = path ? `${path}.${prop}` : prop;\n if (obj[prop] === Object(obj[prop])) {\n parse(obj[prop], propPath);\n }\n if (typeof obj[prop] === \"function\") {\n paths.push(propPath);\n }\n });\n })(obj);\n return paths;\n}\n\nconst urlRegex = /^(https?:|file:)?\\/\\/([^/:]+)?(:(\\d+))?/;\nconst ports: any = { \"http:\": \"80\", \"https:\": \"443\" };\n\n/**\n * convert the url into an origin (remove paths)\n *\n * @param url\n */\nexport function getOriginFromURL(url: string | null) {\n const { location } = document;\n\n const regexResult = urlRegex.exec(url || \"\");\n let protocol;\n let hostname;\n let port;\n\n if (regexResult) {\n // It's an absolute URL. Use the parsed info.\n // regexResult[1] will be undefined if the URL starts with //\n [, protocol = location.protocol, hostname, , port] = regexResult;\n } else {\n // It's a relative path. Use the current location's info.\n protocol = location.protocol;\n hostname = location.hostname;\n port = location.port;\n }\n\n // If the protocol is file, the origin is \"null\"\n // The origin of a document with file protocol is an opaque origin\n // and its serialization \"null\" [1]\n // [1] https://html.spec.whatwg.org/multipage/origin.html#origin\n if (protocol === \"file:\") {\n return \"null\";\n }\n\n // If the port is the default for the protocol, we don't want to add it to the origin string\n // or it won't match the message's event.origin.\n const portSuffix = port && port !== ports[protocol] ? `:${port}` : \"\";\n return `${protocol}//${hostname}${portSuffix}`;\n}\n","export enum events {\n MESSAGE = \"message\",\n}\n\nexport enum actions {\n HANDSHAKE_REQUEST = \"RIMLESS/HANDSHAKE_REQUEST\",\n HANDSHAKE_REPLY = \"RIMLESS/HANDSHAKE_REPLY\",\n RPC_REQUEST = \"RIMLESS/RPC_REQUEST\",\n RPC_RESOLVE = \"RIMLESS/RPC_RESOLVE\",\n RPC_REJECT = \"RIMLESS/RPC_REJECT\",\n}\n\nexport interface ISchema {\n [prop: string]: any;\n}\n\nexport interface IConnection {\n remote: ISchema;\n close: () => void;\n}\n\nexport interface IConnections {\n [connectionID: string]: ISchema;\n}\n\nexport interface IEvent extends EventListener {\n source?: Window;\n origin?: string;\n data?: IHandshakeRequestPayload | IHandshakeConfirmationPayload | IRPCRequestPayload | IRPCResolvePayload;\n}\n\nexport interface IHandshakeRequestPayload {\n action: actions.HANDSHAKE_REQUEST;\n connectionID?: string;\n methods: any[];\n schema: ISchema;\n}\n\nexport interface IHandshakeConfirmationPayload {\n action: actions.HANDSHAKE_REPLY;\n connectionID: string;\n methods: any[];\n schema: ISchema;\n}\n\nexport interface IRPCRequestPayload {\n action: actions.RPC_REQUEST;\n args: any[];\n callID: string;\n callName: string;\n connectionID?: string;\n}\n\nexport interface IRPCResolvePayload {\n action: actions.RPC_RESOLVE | actions.RPC_REJECT;\n result?: any | null;\n error?: Error | null;\n callID: string;\n callName: string;\n connectionID: string;\n}\n","import get from \"lodash.get\";\nimport set from \"lodash.set\";\nimport { nanoid } from \"nanoid\";\n\nimport { isTrustedRemote, isWorker } from \"./helpers\";\nimport { actions, events, IRPCRequestPayload, IRPCResolvePayload, ISchema } from \"./types\";\n\n/**\n * for each function in the schema\n * 1. subscribe to an event that the remote can call\n * 2. listen for calls from the remote. When called execute the function and emit the results.\n *\n * @param methods an array of method ids from the local schema\n * @param _connectionID\n * @return a function to cancel all subscriptions\n */\nexport function registerLocalMethods(\n schema: ISchema = {},\n methods: any[] = [],\n _connectionID: string,\n guest?: Worker\n): any {\n const listeners: any[] = [];\n methods.forEach((methodName) => {\n // handle a remote calling a local method\n async function handleCall(event: any) {\n const { action, callID, connectionID, callName, args = [] } = event.data as IRPCRequestPayload;\n\n if (action !== actions.RPC_REQUEST) return;\n if (!isTrustedRemote(event)) return;\n if (!callID || !callName) return;\n if (callName !== methodName) return;\n if (connectionID !== _connectionID) return;\n\n const payload: IRPCResolvePayload = {\n action: actions.RPC_RESOLVE,\n callID,\n callName,\n connectionID,\n error: null,\n result: null,\n };\n\n // run function and return the results to the remote\n try {\n const result = await get(schema, methodName)(...args);\n payload.result = JSON.parse(JSON.stringify(result));\n } catch (error) {\n payload.error = JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)));\n }\n\n if (guest) guest.postMessage(payload);\n else if (isWorker()) (self as any).postMessage(payload);\n else event.source.postMessage(payload, event.origin);\n }\n\n // subscribe to the call event\n if (guest) guest.addEventListener(events.MESSAGE, handleCall);\n else self.addEventListener(events.MESSAGE, handleCall);\n\n listeners.push(() => self.removeEventListener(events.MESSAGE, handleCall));\n });\n\n return () => listeners.forEach((unregister) => unregister());\n}\n\n/**\n * Create a function that will make an RPC request to the remote with some arguments.\n * Listen to an event that returns the results from the remote.\n *\n * @param _callName\n * @param _connectionID\n * @param event\n * @param listeners\n * @param guest\n *\n * @returns a promise with the result of the RPC\n */\nexport function createRPC(\n _callName: string,\n _connectionID: string,\n event: any,\n listeners: Array<() => void> = [],\n guest?: Worker\n) {\n return (...args: any) => {\n return new Promise((resolve, reject) => {\n const callID = nanoid();\n\n // on RPC response\n function handleResponse(event: any) {\n const { callID, connectionID, callName, result, error, action } = event.data as IRPCResolvePayload;\n\n if (!isTrustedRemote(event)) return;\n if (!callID || !callName) return;\n if (callName !== _callName) return;\n if (connectionID !== _connectionID) return;\n\n // resolve the response\n if (action === actions.RPC_RESOLVE) return resolve(result);\n if (action === actions.RPC_REJECT) return reject(error);\n }\n\n // send the RPC request with arguments\n const payload = {\n action: actions.RPC_REQUEST,\n args: JSON.parse(JSON.stringify(args)),\n callID,\n callName: _callName,\n connectionID: _connectionID,\n };\n\n if (guest) guest.addEventListener(events.MESSAGE, handleResponse);\n else self.addEventListener(events.MESSAGE, handleResponse);\n listeners.push(() => self.removeEventListener(events.MESSAGE, handleResponse));\n\n if (guest) guest.postMessage(payload);\n else if (isWorker()) (self as any).postMessage(payload);\n else (event.source || event.target).postMessage(payload, event.origin);\n });\n };\n}\n\n/**\n * create an object based on the remote schema and methods. Functions in that object will\n * emit an event that will trigger the RPC on the remote.\n *\n * @param schema\n * @param methods\n * @param _connectionID\n * @param event\n * @param guest\n */\nexport function registerRemoteMethods(\n schema: ISchema = {},\n methods: any[] = [],\n _connectionID: string,\n event: any,\n guest?: Worker\n) {\n const remote = { ...schema };\n const listeners: Array<() => void> = [];\n\n methods.forEach((methodName) => {\n const rpc = createRPC(methodName, _connectionID, event, listeners, guest);\n set(remote, methodName, rpc);\n });\n\n return {\n remote,\n unregisterRemote: () => listeners.forEach((unregister) => unregister()),\n };\n}\n","import { extractMethods, isWorker } from \"./helpers\";\nimport { registerLocalMethods, registerRemoteMethods } from \"./rpc\";\nimport { actions, events, IConnection, ISchema } from \"./types\";\n\nconst REQUEST_INTERVAL = 10;\nconst TIMEOUT_INTERVAL = 3000;\n\nlet interval: any = null;\nlet connected = false;\n\nfunction connect(schema: ISchema = {}): Promise<IConnection> {\n return new Promise((resolve, reject) => {\n const localMethods = extractMethods(schema);\n\n // on handshake response\n function handleHandshakeResponse(event: any) {\n if (event.data.action !== actions.HANDSHAKE_REPLY) return;\n\n // register local methods\n const unregisterLocal = registerLocalMethods(schema, localMethods, event.data.connectionID);\n\n // register remote methods\n const { remote, unregisterRemote } = registerRemoteMethods(\n event.data.schema,\n event.data.methods,\n event.data.connectionID,\n event\n );\n\n // close the connection and all listeners when called\n const close = () => {\n self.removeEventListener(events.MESSAGE, handleHandshakeResponse);\n unregisterRemote();\n unregisterLocal();\n };\n\n connected = true;\n\n // resolve connection object\n const connection = { remote, close };\n return resolve(connection);\n }\n\n // subscribe to HANDSHAKE REPLY MESSAGES\n self.addEventListener(events.MESSAGE, handleHandshakeResponse);\n\n const payload = {\n action: actions.HANDSHAKE_REQUEST,\n methods: localMethods,\n schema: JSON.parse(JSON.stringify(schema)),\n };\n\n interval = setInterval(() => {\n if (connected) return clearInterval(interval);\n\n // publish the HANDSHAKE REQUEST\n if (isWorker()) (self as any).postMessage(payload);\n else window.parent.postMessage(payload, \"*\");\n }, REQUEST_INTERVAL);\n\n // timeout the connection after a time\n setTimeout(() => {\n if (!connected) reject(\"connection timeout\");\n }, TIMEOUT_INTERVAL);\n });\n}\n\nexport default {\n connect,\n};\n","import { nanoid } from \"nanoid\";\n\nimport { extractMethods, getOriginFromURL } from \"./helpers\";\nimport { registerLocalMethods, registerRemoteMethods } from \"./rpc\";\nimport { actions, events, IConnections, IConnection, ISchema } from \"./types\";\n\nconst connections: IConnections = {};\n\nfunction isValidTarget(iframe: HTMLIFrameElement, event: any) {\n const childURL = iframe.getAttribute(\"src\");\n const childOrigin = getOriginFromURL(childURL);\n const hasProperOrigin = event.origin === childOrigin;\n const hasProperSource = event.source === iframe.contentWindow;\n\n return hasProperOrigin && hasProperSource;\n}\n\n/**\n * Perform a handshake with the target iframe, when the handshake is confirmed\n * resolve the connection object containing RPCs and properties\n *\n * @param iframe\n * @param schema\n * @returns Promise\n */\nfunction connect(guest: HTMLIFrameElement | Worker, schema: ISchema = {}): Promise<IConnection> {\n if (!guest) throw new Error(\"a target is required\");\n\n const guestIsWorker = (guest as Worker).onerror !== undefined && (guest as Worker).onmessage !== undefined;\n const listeners = guestIsWorker ? guest : window;\n\n return new Promise((resolve) => {\n const connectionID = nanoid();\n\n // on handshake request\n function handleHandshake(event: any) {\n if (!guestIsWorker && !isValidTarget(guest as HTMLIFrameElement, event)) return;\n if (event.data.action !== actions.HANDSHAKE_REQUEST) return;\n\n // register local methods\n const localMethods = extractMethods(schema);\n const unregisterLocal = registerLocalMethods(\n schema,\n localMethods,\n connectionID,\n guestIsWorker ? (guest as Worker) : undefined\n );\n\n // register remote methods\n const { remote, unregisterRemote } = registerRemoteMethods(\n event.data.schema,\n event.data.methods,\n connectionID,\n event,\n guestIsWorker ? (guest as Worker) : undefined\n );\n\n const payload = {\n action: actions.HANDSHAKE_REPLY,\n connectionID,\n methods: localMethods,\n schema: JSON.parse(JSON.stringify(schema)),\n };\n\n // confirm the connection\n if (guestIsWorker) (guest as Worker).postMessage(payload);\n else event.source.postMessage(payload, event.origin);\n\n // close the connection and all listeners when called\n const close = () => {\n listeners.removeEventListener(events.MESSAGE, handleHandshake);\n unregisterRemote();\n unregisterLocal();\n };\n\n // resolve connection object\n const connection: IConnection = { remote, close };\n connections[connectionID] = connection;\n return resolve(connection);\n }\n\n // subscribe to HANDSHAKE MESSAGES\n listeners.addEventListener(events.MESSAGE, handleHandshake);\n });\n}\n\nexport default {\n connect,\n};\n"],"names":["isWorker","extractMethods","obj","paths","parse","path","prop","propPath","urlRegex","ports","getOriginFromURL","url","location","regexResult","protocol","hostname","port","portSuffix","events","actions","registerLocalMethods","schema","methods","_connectionID","guest","listeners","methodName","handleCall","event","action","callID","connectionID","callName","args","payload","result","get","error","unregister","createRPC","_callName","resolve","reject","nanoid","handleResponse","registerRemoteMethods","remote","rpc","set","REQUEST_INTERVAL","TIMEOUT_INTERVAL","interval","connected","connect","localMethods","handleHandshakeResponse","unregisterLocal","unregisterRemote","close","isValidTarget","iframe","childURL","childOrigin","hasProperOrigin","hasProperSource","guestIsWorker","handleHandshake","host"],"mappings":";;;AAiBO,SAASA,IAAW;AAClB,SAAA,OAAO,oBAAsB,OAAe,gBAAgB;AACrE;AAQO,SAASC,EAAeC,GAAU;AACvC,QAAMC,IAAkB,CAAA;AACxB,SAAC,SAASC,EAAMF,GAAUG,IAAO,IAAI;AACnC,WAAO,KAAKH,CAAG,EAAE,QAAQ,CAACI,MAAS;AACjC,YAAMC,IAAWF,IAAO,GAAGA,CAAI,IAAIC,CAAI,KAAKA;AAC5C,MAAIJ,EAAII,CAAI,MAAM,OAAOJ,EAAII,CAAI,CAAC,KAC1BJ,EAAAA,EAAII,CAAI,GAAGC,CAAQ,GAEvB,OAAOL,EAAII,CAAI,KAAM,cACvBH,EAAM,KAAKI,CAAQ;AAAA,IACrB,CACD;AAAA,IACAL,CAAG,GACCC;AACT;AAEA,MAAMK,IAAW,2CACXC,IAAa,EAAE,SAAS,MAAM,UAAU,MAAM;AAO7C,SAASC,EAAiBC,GAAoB;AAC7C,QAAA,EAAE,UAAAC,EAAa,IAAA,UAEfC,IAAcL,EAAS,KAAKG,KAAO,EAAE;AACvC,MAAAG,GACAC,GACAC;AAiBJ,MAfIH,IAGF,CAAG,EAAAC,IAAWF,EAAS,UAAUG,GAAY,EAAAC,CAAI,IAAIH,KAGrDC,IAAWF,EAAS,UACpBG,IAAWH,EAAS,UAC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QACAqC,IAAiBzC,IAAmB;AAAA,MAAA,GAGhCU,IAAU;AAAA,QACd,QAAQf,EAAQ;AAAA,QAChB,cAAAY;AAAA,QACA,SAASuB;AAAA,QACT,QAAQ,KAAK,MAAM,KAAK,UAAUjC,CAAM,CAAC;AAAA,MAAA;AAI3C,aAAI4C,IAAgBzC,EAAiB,YAAYU,CAAO,IAC7CN,EAAA,OAAO,YAAYM,GAASN,EAAM,MAAM,GAY5Ca,EAFyB,EAAE,QAAAK,GAAQ,OAP5B,MAAM;AACR,QAAArB,EAAA,oBAAoBP,EAAO,SAASgD,CAAe,GAC5CT,KACDD;MAAA,GAMO;AAAA,IAC3B;AAGU,IAAA/B,EAAA,iBAAiBP,EAAO,SAASgD,CAAe;AAAA,EAAA,CAC3D;AACH;AAEA,MAAeC,IAAA;AAAA,EACb,SAAAd;AACF;"}
|
package/lib/rimless.min.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var rimless=function(t){"use strict";function e(){return"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope}function n(t){const e=[];return function t(n,r=""){Object.keys(n).forEach(o=>{const i=r?`${r}.${o}`:o;n[o]===Object(n[o])&&t(n[o],i),"function"==typeof n[o]&&e.push(i)})}(t),e}const r=/^(https?:|file:)?\/\/([^/:]+)?(:(\d+))?/,o={"http:":"80","https:":"443"};var i="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},a=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,c=/^\w*$/,u=/^\./,s=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,f=/\\(\\)?/g,l=/^\[object .+?Constructor\]$/,p="object"==typeof i&&i&&i.Object===Object&&i,_="object"==typeof self&&self&&self.Object===Object&&self,h=p||_||Function("return this")();var d,y=Array.prototype,v=Function.prototype,g=Object.prototype,E=h["__core-js_shared__"],S=(d=/[^.]+$/.exec(E&&E.keys&&E.keys.IE_PROTO||""))?"Symbol(src)_1."+d:"",m=v.toString,b=g.hasOwnProperty,R=g.toString,O=RegExp("^"+m.call(b).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),w=h.Symbol,j=y.splice,A=G(h,"Map"),M=G(Object,"create"),P=w?w.prototype:void 0,C=P?P.toString:void 0;function $(t){var e=-1,n=t?t.length:0;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function L(t){var e=-1,n=t?t.length:0;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function N(t){var e=-1,n=t?t.length:0;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function I(t,e){for(var n,r,o=t.length;o--;)if((n=t[o][0])===(r=e)||n!=n&&r!=r)return o;return-1}function D(t,e){for(var n,r=0,o=(e=function(t,e){if(F(t))return!1;var n=typeof t;if("number"==n||"symbol"==n||"boolean"==n||null==t||Q(t))return!0;return c.test(t)||!a.test(t)||null!=e&&t in Object(e)}(e,t)?[e]:F(n=e)?n:J(n)).length;null!=t&&r<o;)t=t[U(e[r++])];return r&&r==o?t:void 0}function T(t){return!(!K(t)||(e=t,S&&S in e))&&(function(t){var e=K(t)?R.call(t):"";return"[object Function]"==e||"[object GeneratorFunction]"==e}(t)||function(t){var e=!1;if(null!=t&&"function"!=typeof t.toString)try{e=!!(t+"")}catch(t){}return e}(t)?O:l).test(function(t){if(null!=t){try{return m.call(t)}catch(t){}try{return t+""}catch(t){}}return""}(t));var e}function H(t,e){var n,r,o=t.__data__;return("string"==(r=typeof(n=e))||"number"==r||"symbol"==r||"boolean"==r?"__proto__"!==n:null===n)?o["string"==typeof e?"string":"hash"]:o.map}function G(t,e){var n=function(t,e){return null==t?void 0:t[e]}(t,e);return T(n)?n:void 0}$.prototype.clear=function(){this.__data__=M?M(null):{}},$.prototype.delete=function(t){return this.has(t)&&delete this.__data__[t]},$.prototype.get=function(t){var e=this.__data__;if(M){var n=e[t];return"__lodash_hash_undefined__"===n?void 0:n}return b.call(e,t)?e[t]:void 0},$.prototype.has=function(t){var e=this.__data__;return M?void 0!==e[t]:b.call(e,t)},$.prototype.set=function(t,e){return this.__data__[t]=M&&void 0===e?"__lodash_hash_undefined__":e,this},L.prototype.clear=function(){this.__data__=[]},L.prototype.delete=function(t){var e=this.__data__,n=I(e,t);return!(n<0)&&(n==e.length-1?e.pop():j.call(e,n,1),!0)},L.prototype.get=function(t){var e=this.__data__,n=I(e,t);return n<0?void 0:e[n][1]},L.prototype.has=function(t){return I(this.__data__,t)>-1},L.prototype.set=function(t,e){var n=this.__data__,r=I(n,t);return r<0?n.push([t,e]):n[r][1]=e,this},N.prototype.clear=function(){this.__data__={hash:new $,map:new(A||L),string:new $}},N.prototype.delete=function(t){return H(this,t).delete(t)},N.prototype.get=function(t){return H(this,t).get(t)},N.prototype.has=function(t){return H(this,t).has(t)},N.prototype.set=function(t,e){return H(this,t).set(t,e),this};var J=V((function(t){var e;t=null==(e=t)?"":function(t){if("string"==typeof t)return t;if(Q(t))return C?C.call(t):"";var e=t+"";return"0"==e&&1/t==-1/0?"-0":e}(e);var n=[];return u.test(t)&&n.push(""),t.replace(s,(function(t,e,r,o){n.push(r?o.replace(f,"$1"):e||t)})),n}));function U(t){if("string"==typeof t||Q(t))return t;var e=t+"";return"0"==e&&1/t==-1/0?"-0":e}function V(t,e){if("function"!=typeof t||e&&"function"!=typeof e)throw new TypeError("Expected a function");var n=function(){var r=arguments,o=e?e.apply(this,r):r[0],i=n.cache;if(i.has(o))return i.get(o);var a=t.apply(this,r);return n.cache=i.set(o,a),a};return n.cache=new(V.Cache||N),n}V.Cache=N;var F=Array.isArray;function K(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function Q(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==R.call(t)}var k=function(t,e,n){var r=null==t?void 0:D(t,e);return void 0===r?n:r},x=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,Y=/^\w*$/,W=/^\./,q=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,z=/\\(\\)?/g,B=/^\[object .+?Constructor\]$/,X=/^(?:0|[1-9]\d*)$/,Z="object"==typeof i&&i&&i.Object===Object&&i,tt="object"==typeof self&&self&&self.Object===Object&&self,et=Z||tt||Function("return this")();var nt=Array.prototype,rt=Function.prototype,ot=Object.prototype,it=et["__core-js_shared__"],at=function(){var t=/[^.]+$/.exec(it&&it.keys&&it.keys.IE_PROTO||"");return t?"Symbol(src)_1."+t:""}(),ct=rt.toString,ut=ot.hasOwnProperty,st=ot.toString,ft=RegExp("^"+ct.call(ut).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),lt=et.Symbol,pt=nt.splice,_t=wt(et,"Map"),ht=wt(Object,"create"),dt=lt?lt.prototype:void 0,yt=dt?dt.toString:void 0;function vt(t){var e=-1,n=t?t.length:0;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function gt(t){var e=-1,n=t?t.length:0;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function Et(t){var e=-1,n=t?t.length:0;for(this.clear();++e<n;){var r=t[e];this.set(r[0],r[1])}}function St(t,e,n){var r=t[e];ut.call(t,e)&&Ct(r,n)&&(void 0!==n||e in t)||(t[e]=n)}function mt(t,e){for(var n=t.length;n--;)if(Ct(t[n][0],e))return n;return-1}function bt(t){return!(!Lt(t)||(e=t,at&&at in e))&&(function(t){var e=Lt(t)?st.call(t):"";return"[object Function]"==e||"[object GeneratorFunction]"==e}(t)||function(t){var e=!1;if(null!=t&&"function"!=typeof t.toString)try{e=!!(t+"")}catch(t){}return e}(t)?ft:B).test(function(t){if(null!=t){try{return ct.call(t)}catch(t){}try{return t+""}catch(t){}}return""}(t));var e}function Rt(t,e,n,r){if(!Lt(t))return t;for(var o=-1,i=(e=function(t,e){if($t(t))return!1;var n=typeof t;if("number"==n||"symbol"==n||"boolean"==n||null==t||Nt(t))return!0;return Y.test(t)||!x.test(t)||null!=e&&t in Object(e)}(e,t)?[e]:function(t){return $t(t)?t:At(t)}(e)).length,a=i-1,c=t;null!=c&&++o<i;){var u=Mt(e[o]),s=n;if(o!=a){var f=c[u];void 0===(s=r?r(f,u,c):void 0)&&(s=Lt(f)?f:jt(e[o+1])?[]:{})}St(c,u,s),c=c[u]}return t}function Ot(t,e){var n,r,o=t.__data__;return("string"==(r=typeof(n=e))||"number"==r||"symbol"==r||"boolean"==r?"__proto__"!==n:null===n)?o["string"==typeof e?"string":"hash"]:o.map}function wt(t,e){var n=function(t,e){return null==t?void 0:t[e]}(t,e);return bt(n)?n:void 0}function jt(t,e){return!!(e=null==e?9007199254740991:e)&&("number"==typeof t||X.test(t))&&t>-1&&t%1==0&&t<e}vt.prototype.clear=function(){this.__data__=ht?ht(null):{}},vt.prototype.delete=function(t){return this.has(t)&&delete this.__data__[t]},vt.prototype.get=function(t){var e=this.__data__;if(ht){var n=e[t];return"__lodash_hash_undefined__"===n?void 0:n}return ut.call(e,t)?e[t]:void 0},vt.prototype.has=function(t){var e=this.__data__;return ht?void 0!==e[t]:ut.call(e,t)},vt.prototype.set=function(t,e){return this.__data__[t]=ht&&void 0===e?"__lodash_hash_undefined__":e,this},gt.prototype.clear=function(){this.__data__=[]},gt.prototype.delete=function(t){var e=this.__data__,n=mt(e,t);return!(n<0)&&(n==e.length-1?e.pop():pt.call(e,n,1),!0)},gt.prototype.get=function(t){var e=this.__data__,n=mt(e,t);return n<0?void 0:e[n][1]},gt.prototype.has=function(t){return mt(this.__data__,t)>-1},gt.prototype.set=function(t,e){var n=this.__data__,r=mt(n,t);return r<0?n.push([t,e]):n[r][1]=e,this},Et.prototype.clear=function(){this.__data__={hash:new vt,map:new(_t||gt),string:new vt}},Et.prototype.delete=function(t){return Ot(this,t).delete(t)},Et.prototype.get=function(t){return Ot(this,t).get(t)},Et.prototype.has=function(t){return Ot(this,t).has(t)},Et.prototype.set=function(t,e){return Ot(this,t).set(t,e),this};var At=Pt((function(t){var e;t=null==(e=t)?"":function(t){if("string"==typeof t)return t;if(Nt(t))return yt?yt.call(t):"";var e=t+"";return"0"==e&&1/t==-1/0?"-0":e}(e);var n=[];return W.test(t)&&n.push(""),t.replace(q,(function(t,e,r,o){n.push(r?o.replace(z,"$1"):e||t)})),n}));function Mt(t){if("string"==typeof t||Nt(t))return t;var e=t+"";return"0"==e&&1/t==-1/0?"-0":e}function Pt(t,e){if("function"!=typeof t||e&&"function"!=typeof e)throw new TypeError("Expected a function");var n=function(){var r=arguments,o=e?e.apply(this,r):r[0],i=n.cache;if(i.has(o))return i.get(o);var a=t.apply(this,r);return n.cache=i.set(o,a),a};return n.cache=new(Pt.Cache||Et),n}function Ct(t,e){return t===e||t!=t&&e!=e}Pt.Cache=Et;var $t=Array.isArray;function Lt(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function Nt(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&"[object Symbol]"==st.call(t)}var It=function(t,e,n){return null==t?t:Rt(t,e,n)},Dt="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto),Tt=new Uint8Array(16);function Ht(){if(!Dt)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return Dt(Tt)}for(var Gt,Jt,Ut=[],Vt=0;Vt<256;++Vt)Ut[Vt]=(Vt+256).toString(16).substr(1);function Ft(t,e,n){var r=e&&n||0;"string"==typeof t&&(e="binary"===t?new Array(16):null,t=null);var o=(t=t||{}).random||(t.rng||Ht)();if(o[6]=15&o[6]|64,o[8]=63&o[8]|128,e)for(var i=0;i<16;++i)e[r+i]=o[i];return e||function(t,e){var n=e||0,r=Ut;return[r[t[n++]],r[t[n++]],r[t[n++]],r[t[n++]],"-",r[t[n++]],r[t[n++]],"-",r[t[n++]],r[t[n++]],"-",r[t[n++]],r[t[n++]],"-",r[t[n++]],r[t[n++]],r[t[n++]],r[t[n++]],r[t[n++]],r[t[n++]]].join("")}(o)}function Kt(t={},n=[],r,o){const i=[];return n.forEach(n=>{async function a(i){const{action:a,callID:c,connectionID:u,callName:s,args:f=[]}=i.data;if(a!==Jt.RPC_REQUEST)return;if(!c||!s)return;if(s!==n)return;if(u!==r)return;const l={action:Jt.RPC_RESOLVE,callID:c,callName:s,connectionID:u,error:null,result:null};try{const e=await k(t,n)(...f);l.result=JSON.parse(JSON.stringify(e))}catch(t){l.error=JSON.parse(JSON.stringify(t,Object.getOwnPropertyNames(t)))}o?o.postMessage(l):e()?self.postMessage(l):i.source.postMessage(l,i.origin)}o?o.addEventListener(Gt.MESSAGE,a):self.addEventListener(Gt.MESSAGE,a),i.push(()=>self.removeEventListener(Gt.MESSAGE,a))}),()=>i.forEach(t=>t())}function Qt(t={},n=[],r,o,i){const a={...t},c=[];return n.forEach(t=>{const n=function(t,n,r,o=[],i){return(...a)=>new Promise((c,u)=>{const s=Ft();function f(e){const{callID:r,connectionID:o,callName:i,result:a,error:s,action:f}=e.data;if(r&&i&&i===t&&o===n)return f===Jt.RPC_RESOLVE?c(a):f===Jt.RPC_REJECT?u(s):void 0}const l={action:Jt.RPC_REQUEST,args:JSON.parse(JSON.stringify(a)),callID:s,callName:t,connectionID:n};i?i.addEventListener(Gt.MESSAGE,f):self.addEventListener(Gt.MESSAGE,f),o.push(()=>self.removeEventListener(Gt.MESSAGE,f)),i?i.postMessage(l):e()?self.postMessage(l):(r.source||r.target).postMessage(l,r.origin)})}(t,r,o,c,i);It(a,t,n)}),{remote:a,unregisterRemote:()=>c.forEach(t=>t())}}!function(t){t.MESSAGE="message"}(Gt||(Gt={})),function(t){t.HANDSHAKE_REQUEST="RIMLESS/HANDSHAKE_REQUEST",t.HANDSHAKE_REPLY="RIMLESS/HANDSHAKE_REPLY",t.RPC_REQUEST="RIMLESS/RPC_REQUEST",t.RPC_RESOLVE="RIMLESS/RPC_RESOLVE",t.RPC_REJECT="RIMLESS/RPC_REJECT"}(Jt||(Jt={}));let kt=null,xt=!1;var Yt={connect:function(t={},r={}){return new Promise((r,o)=>{const i=n(t);self.addEventListener(Gt.MESSAGE,(function e(n){if(n.data.action!==Jt.HANDSHAKE_REPLY)return;const o=Kt(t,i,n.data.connectionID),{remote:a,unregisterRemote:c}=Qt(n.data.schema,n.data.methods,n.data.connectionID,n);return xt=!0,r({remote:a,close:()=>{self.removeEventListener(Gt.MESSAGE,e),c(),o()}})}));const a={action:Jt.HANDSHAKE_REQUEST,methods:i,schema:JSON.parse(JSON.stringify(t))};kt=setInterval(()=>{if(xt)return clearInterval(kt);e()?self.postMessage(a):window.parent.postMessage(a,"*")},600),setTimeout(()=>{xt||o("connection timeout")},3e3)})}};function Wt(t,e){const n=function(t){const{location:e}=document,n=r.exec(t||"");let i,a,c;return n?[,i=e.protocol,a,,c]=n:(i=e.protocol,a=e.hostname,c=e.port),"file:"===i?"null":`${i}//${a}${c&&c!==o[i]?":"+c:""}`}(t.getAttribute("src")),i=e.origin===n,a=e.source===t.contentWindow;return i&&a}var qt={connect:function(t,e={},r){if(!t)throw new Error("a target is required");const o=void 0!==t.onerror&&void 0!==t.onmessage,i=o?t:window;return new Promise((r,a)=>{const c=Ft();i.addEventListener(Gt.MESSAGE,(function a(u){if(!o&&!Wt(t,u))return;if(u.data.action!==Jt.HANDSHAKE_REQUEST)return;const s=n(e),f=Kt(e,s,c,o?t:void 0),{remote:l,unregisterRemote:p}=Qt(u.data.schema,u.data.methods,c,u,o?t:void 0),_={action:Jt.HANDSHAKE_REPLY,connectionID:c,methods:s,schema:JSON.parse(JSON.stringify(e))};return o?t.postMessage(_):u.source.postMessage(_,u.origin),r({remote:l,close:()=>{i.removeEventListener(Gt.MESSAGE,a),p(),f()}})}))})}};return t.guest=Yt,t.host=qt,t}({});
|
|
1
|
+
var rimless=function(h,I,T,A){"use strict";function M(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function L(e){const o=[];return function c(n,r=""){Object.keys(n).forEach(t=>{const s=r?`${r}.${t}`:t;n[t]===Object(n[t])&&c(n[t],s),typeof n[t]=="function"&&o.push(s)})}(e),o}const _=/^(https?:|file:)?\/\/([^/:]+)?(:(\d+))?/,H={"http:":"80","https:":"443"};function y(e){const{location:o}=document,c=_.exec(e||"");let n,r,t;if(c?[,n=o.protocol,r,,t]=c:(n=o.protocol,r=o.hostname,t=o.port),n==="file:")return"null";const s=t&&t!==H[n]?`:${t}`:"";return`${n}//${r}${s}`}var a=(e=>(e.MESSAGE="message",e))(a||{}),l=(e=>(e.HANDSHAKE_REQUEST="RIMLESS/HANDSHAKE_REQUEST",e.HANDSHAKE_REPLY="RIMLESS/HANDSHAKE_REPLY",e.RPC_REQUEST="RIMLESS/RPC_REQUEST",e.RPC_RESOLVE="RIMLESS/RPC_RESOLVE",e.RPC_REJECT="RIMLESS/RPC_REJECT",e))(l||{});function m(e={},o=[],c,n){const r=[];return o.forEach(t=>{async function s(i){const{action:E,callID:f,connectionID:u,callName:S,args:g=[]}=i.data;if(E!==l.RPC_REQUEST||!f||!S||S!==t||u!==c)return;const d={action:l.RPC_RESOLVE,callID:f,callName:S,connectionID:u,error:null,result:null};try{const R=await I(e,t)(...g);d.result=JSON.parse(JSON.stringify(R))}catch(R){d.error=JSON.parse(JSON.stringify(R,Object.getOwnPropertyNames(R)))}n?n.postMessage(d):M()?self.postMessage(d):i.source.postMessage(d,i.origin)}n?n.addEventListener(a.MESSAGE,s):self.addEventListener(a.MESSAGE,s),r.push(()=>self.removeEventListener(a.MESSAGE,s))}),()=>r.forEach(t=>t())}function D(e,o,c,n=[],r){return(...t)=>new Promise((s,i)=>{const E=A.nanoid();function f(S){const{callID:g,connectionID:d,callName:R,result:K,error:k,action:N}=S.data;if(!(!g||!R)&&R===e&&d===o){if(N===l.RPC_RESOLVE)return s(K);if(N===l.RPC_REJECT)return i(k)}}const u={action:l.RPC_REQUEST,args:JSON.parse(JSON.stringify(t)),callID:E,callName:e,connectionID:o};r?r.addEventListener(a.MESSAGE,f):self.addEventListener(a.MESSAGE,f),n.push(()=>self.removeEventListener(a.MESSAGE,f)),r?r.postMessage(u):M()?self.postMessage(u):(c.source||c.target).postMessage(u,c.origin)})}function P(e={},o=[],c,n,r){const t={...e},s=[];return o.forEach(i=>{const E=D(i,c,n,s,r);T(t,i,E)}),{remote:t,unregisterRemote:()=>s.forEach(i=>i())}}const C=10,G=3e3;let O=null,p=!1;function J(e={}){return new Promise((o,c)=>{const n=L(e);function r(s){if(s.data.action!==l.HANDSHAKE_REPLY)return;const i=m(e,n,s.data.connectionID),{remote:E,unregisterRemote:f}=P(s.data.schema,s.data.methods,s.data.connectionID,s),u=()=>{self.removeEventListener(a.MESSAGE,r),f(),i()};return p=!0,o({remote:E,close:u})}self.addEventListener(a.MESSAGE,r);const t={action:l.HANDSHAKE_REQUEST,methods:n,schema:JSON.parse(JSON.stringify(e))};O=setInterval(()=>{if(p)return clearInterval(O);M()?self.postMessage(t):window.parent.postMessage(t,"*")},C),setTimeout(()=>{p||c("connection timeout")},G)})}const w={connect:J};function U(e,o){const c=e.getAttribute("src"),n=y(c),r=o.origin===n,t=o.source===e.contentWindow;return r&&t}function v(e,o={}){if(!e)throw new Error("a target is required");const c=e.onerror!==void 0&&e.onmessage!==void 0,n=c?e:window;return new Promise(r=>{const t=A.nanoid();function s(i){if(!c&&!U(e,i)||i.data.action!==l.HANDSHAKE_REQUEST)return;const E=L(o),f=m(o,E,t,c?e:void 0),{remote:u,unregisterRemote:S}=P(i.data.schema,i.data.methods,t,i,c?e:void 0),g={action:l.HANDSHAKE_REPLY,connectionID:t,methods:E,schema:JSON.parse(JSON.stringify(o))};return c?e.postMessage(g):i.source.postMessage(g,i.origin),r({remote:u,close:()=>{n.removeEventListener(a.MESSAGE,s),S(),f()}})}n.addEventListener(a.MESSAGE,s)})}const Q={connect:v};return h.actions=l,h.events=a,h.guest=w,h.host=Q,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"}),h}({},get,set,nanoid);
|
|
2
2
|
//# sourceMappingURL=rimless.min.js.map
|