@webreflection/bindings 0.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 +32 -0
- package/package.json +21 -0
- package/src/message-port.js +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright © 2026-today, Andrea Giammarchi, @WebReflection
|
|
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
|
|
10
|
+
is furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included
|
|
13
|
+
in all 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
|
|
21
|
+
IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @webreflection/bindings
|
|
2
|
+
|
|
3
|
+
A (soon to be) collection of bindings for various scenarios.
|
|
4
|
+
|
|
5
|
+
### Example
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import bindings from '@webreflection/bindings/port';
|
|
9
|
+
|
|
10
|
+
const { port1, port2 } = new MessageChannel;
|
|
11
|
+
|
|
12
|
+
// expose bindings the remote can invoke
|
|
13
|
+
// the `remote` itself can invoke (asynchronously)
|
|
14
|
+
// bindings exposed via the other port
|
|
15
|
+
const remote = bindings(port1, {
|
|
16
|
+
log(...values) {
|
|
17
|
+
console.log(...values);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// assuming this is an iframe
|
|
22
|
+
parent.postMessage(null, '*', [port2]);
|
|
23
|
+
|
|
24
|
+
// later on ...
|
|
25
|
+
const value = await remote.doThings(1, 2, 3);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The remote counterpart can use or expose bindings in a similar way, this module just orchestrate a reliable way to invoke exposed APIs via `postMessage` dances.
|
|
29
|
+
|
|
30
|
+
## Currently Available
|
|
31
|
+
|
|
32
|
+
* `@webreflection/bindings/port` to add bindings via *MessagePort* orchestration
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webreflection/bindings",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"exports": {
|
|
6
|
+
"./message-port": "./src/message-port.js"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"MessagePort"
|
|
13
|
+
],
|
|
14
|
+
"author": "Andrea Giammarchi",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@webreflection/utils": "^0.3.6",
|
|
19
|
+
"next-resolver": "^0.2.0"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import nextResolver from 'next-resolver';
|
|
2
|
+
import { nil } from '@webreflection/utils/empty';
|
|
3
|
+
|
|
4
|
+
const [next, resolve] = nextResolver();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Creates a proxy that can be used to call methods remotely via the port.
|
|
8
|
+
* @param {MessagePort} port - The port to use for communication.
|
|
9
|
+
* @param {Record<string, (...args: any[]) => Promise<any>>} bindings - The bindings to use for the methods.
|
|
10
|
+
* @returns {Record<string, (...args: any[]) => Promise<any>>}
|
|
11
|
+
*/
|
|
12
|
+
export default (port, bindings) => {
|
|
13
|
+
port.onmessage = async ({ data: [exec, id, name, args] }) => {
|
|
14
|
+
if (exec) {
|
|
15
|
+
try { port.postMessage([!exec, id, await bindings[name](...args), null]) }
|
|
16
|
+
catch (error) { port.postMessage([!exec, id, null, error]) }
|
|
17
|
+
}
|
|
18
|
+
else resolve(id, name, args);
|
|
19
|
+
};
|
|
20
|
+
return new Proxy(nil, {
|
|
21
|
+
get(_, name) {
|
|
22
|
+
return (...args) => {
|
|
23
|
+
const [id, promise] = next();
|
|
24
|
+
port.postMessage([true, id, name, args]);
|
|
25
|
+
return promise;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
};
|