osra 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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/build/index.js +105 -0
  3. package/package.json +31 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Banou
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/build/index.js ADDED
@@ -0,0 +1,105 @@
1
+ // src/shared.ts
2
+ var MESSAGE_SOURCE_KEY = "osra-message";
3
+
4
+ // src/register.ts
5
+ var registerListener = ({
6
+ target,
7
+ resolvers,
8
+ filter,
9
+ key = MESSAGE_SOURCE_KEY
10
+ }) => {
11
+ const listener = async (event) => {
12
+ if (!event.data || typeof event.data !== "object")
13
+ return;
14
+ if (event.data?.source !== key)
15
+ return;
16
+ if (filter && !filter(event))
17
+ return;
18
+ const { type, data, port } = event.data;
19
+ const resolver = resolvers[type];
20
+ if (!resolver)
21
+ throw new Error(`Osra received a message of type "${type}" but no resolver was found for type.`);
22
+ resolver(data, { event, type, port });
23
+ };
24
+ target.addEventListener("message", listener);
25
+ return {
26
+ listener,
27
+ resolvers
28
+ };
29
+ };
30
+
31
+ // src/utils.ts
32
+ var isTransferable = (value) => value instanceof ArrayBuffer ? true : value instanceof MessagePort ? true : value instanceof ReadableStream ? true : value instanceof WritableStream ? true : value instanceof TransformStream ? true : value instanceof ImageBitmap ? true : false;
33
+ var getTransferableObjects = (value) => {
34
+ const transferables = [];
35
+ const recurse = (value2) => isTransferable(value2) ? transferables.push(value2) : Array.isArray(value2) ? value2.map(recurse) : value2 && typeof value2 === "object" ? Object.values(value2).map(recurse) : void 0;
36
+ recurse(value);
37
+ return transferables;
38
+ };
39
+ var PROXY_FUNCTION_PROPERTY = "__proxyFunctionPort__";
40
+ var makeProxyFunction = (func) => {
41
+ const { port1, port2 } = new MessageChannel();
42
+ port1.addEventListener("message", (ev) => {
43
+ const result = func(...ev.data);
44
+ const proxiedResult = proxyObjectFunctions(result);
45
+ const transferables = getTransferableObjects(proxiedResult);
46
+ port1.postMessage(proxiedResult, { transfer: transferables });
47
+ });
48
+ port1.start();
49
+ return port2;
50
+ };
51
+ var proxyObjectFunctions = (value) => isTransferable(value) ? value : typeof value === "function" ? { [PROXY_FUNCTION_PROPERTY]: makeProxyFunction(value) } : Array.isArray(value) ? value.map(proxyObjectFunctions) : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).map(([key, value2]) => [
52
+ key,
53
+ proxyObjectFunctions(value2)
54
+ ])) : value;
55
+ var makeProxiedFunction = (port) => (...args) => new Promise((resolve, reject) => {
56
+ const proxiedArguments = proxyObjectFunctions(args);
57
+ const transferables = getTransferableObjects(proxiedArguments);
58
+ port.addEventListener("message", (ev) => {
59
+ resolve(makeObjectProxiedFunctions(ev.data));
60
+ });
61
+ port.start();
62
+ port.postMessage(proxiedArguments, { transfer: transferables });
63
+ });
64
+ var makeObjectProxiedFunctions = (value) => isTransferable(value) ? value : value && typeof value === "object" && value[PROXY_FUNCTION_PROPERTY] ? makeProxiedFunction(value[PROXY_FUNCTION_PROPERTY]) : Array.isArray(value) ? value.map(proxyObjectFunctions) : value && typeof value === "object" ? Object.fromEntries(Object.entries(value).map(([key, value2]) => [
65
+ key,
66
+ makeObjectProxiedFunctions(value2)
67
+ ])) : value;
68
+
69
+ // src/call.ts
70
+ var call = (target, { key = MESSAGE_SOURCE_KEY } = { key: MESSAGE_SOURCE_KEY }) => (type, data) => new Promise((resolve) => {
71
+ const { port1, port2 } = new MessageChannel();
72
+ port1.addEventListener("message", ({ data: data2 }) => {
73
+ const proxiedData2 = makeObjectProxiedFunctions(data2);
74
+ resolve(proxiedData2);
75
+ port1.close();
76
+ port2.close();
77
+ }, { once: true });
78
+ port1.start();
79
+ const proxiedData = proxyObjectFunctions(data);
80
+ const transferables = getTransferableObjects(proxiedData);
81
+ target.postMessage({
82
+ source: key,
83
+ type,
84
+ data,
85
+ port: port2
86
+ }, {
87
+ targetOrigin: "*",
88
+ transfer: [port2, ...transferables ?? []]
89
+ });
90
+ });
91
+ var makeCallListener = (func) => async (data, extra) => {
92
+ const { port } = extra;
93
+ const proxiedData = makeObjectProxiedFunctions(data);
94
+ const result = await func(proxiedData, extra);
95
+ const proxyData = proxyObjectFunctions(result);
96
+ const transferables = getTransferableObjects(proxyData);
97
+ port.postMessage(proxyData, { transfer: transferables });
98
+ port.close();
99
+ return result;
100
+ };
101
+ export {
102
+ call,
103
+ makeCallListener,
104
+ registerListener
105
+ };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "osra",
3
+ "version": "0.0.1",
4
+ "description": "Easy communication between workers",
5
+ "files": [
6
+ "./build"
7
+ ],
8
+ "main": "build/index.js",
9
+ "scripts": {
10
+ "type-check": "tsc --incremental",
11
+ "type-check-watch": "tsc --watch --incremental",
12
+ "build": "esbuild ./src/index.ts --format=esm --bundle --outfile=build/index.js && npm run type-check",
13
+ "build-watch": "esbuild --watch ./src/index.ts --format=esm --bundle --outfile=build/index.js",
14
+ "dev": "concurrently \"npm run build-watch\" \"npm run type-check-watch\""
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/Banou26/osra.git"
19
+ },
20
+ "author": "Banou26",
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/Banou26/osra/issues"
24
+ },
25
+ "homepage": "https://github.com/Banou26/osra#readme",
26
+ "devDependencies": {
27
+ "concurrently": "^7.0.0",
28
+ "esbuild": "^0.14.28",
29
+ "typescript": "^4.6.3"
30
+ }
31
+ }