osra 0.0.11 → 0.0.12
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/build/call.d.ts +6 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +165 -124
- package/build/register.d.ts +3 -2
- package/build/types.d.ts +1 -1
- package/package.json +1 -1
package/build/call.d.ts
CHANGED
|
@@ -9,3 +9,9 @@ export declare const call: <T2 extends Resolvers>(target: Target, { key }?: {
|
|
|
9
9
|
* Make a listener for a call
|
|
10
10
|
*/
|
|
11
11
|
export declare const makeCallListener: <T extends (data: any, extra: ApiResolverOptions) => unknown>(func: T) => T;
|
|
12
|
+
/**
|
|
13
|
+
* Make a listener for a call
|
|
14
|
+
*/
|
|
15
|
+
export declare const makeProxyCallListener: <T extends (data: any, extra: ApiResolverOptions) => unknown>(target: Target, { key }?: {
|
|
16
|
+
key?: string | undefined;
|
|
17
|
+
}) => T;
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -1,70 +1,87 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}) => {
|
|
8
|
-
const o = (s) => {
|
|
9
|
-
if (!s.data || typeof s.data != "object" || s.data?.source !== a || r && !r(s))
|
|
10
|
-
return;
|
|
11
|
-
const { type: c, data: i, port: d } = s.data, m = t[c];
|
|
12
|
-
if (!m)
|
|
13
|
-
throw new Error(`Osra received a message of type "${String(c)}" but no resolver was found for type.`);
|
|
14
|
-
n ? m(...n(i, { event: s, type: c, port: d })) : m(i, { event: s, type: c, port: d });
|
|
15
|
-
};
|
|
16
|
-
return e.addEventListener("message", o), {
|
|
17
|
-
listener: o,
|
|
18
|
-
resolvers: t
|
|
19
|
-
};
|
|
20
|
-
}, b = (e) => !!(globalThis.SharedArrayBuffer && e instanceof globalThis.SharedArrayBuffer), y = (e) => globalThis.ArrayBuffer && e instanceof globalThis.ArrayBuffer || globalThis.MessagePort && e instanceof globalThis.MessagePort || globalThis.ReadableStream && e instanceof globalThis.ReadableStream || globalThis.WritableStream && e instanceof globalThis.WritableStream || globalThis.TransformStream && e instanceof globalThis.TransformStream ? !0 : !!(globalThis.ImageBitmap && e instanceof globalThis.ImageBitmap), h = (e) => {
|
|
21
|
-
const t = [], r = (n) => b(n) ? void 0 : y(n) ? t.push(n) : Array.isArray(n) ? n.map(r) : n && typeof n == "object" ? Object.values(n).map(r) : void 0;
|
|
22
|
-
return r(e), t;
|
|
23
|
-
}, p = "__proxyFunctionPort__", M = "__proxyMessageChannelPort__", j = (e) => {
|
|
24
|
-
const { port1: t, port2: r } = new MessageChannel();
|
|
25
|
-
return t.addEventListener("message", async (n) => {
|
|
1
|
+
const f = "osra-message", P = (t) => !!(globalThis.SharedArrayBuffer && t instanceof globalThis.SharedArrayBuffer), h = (t) => globalThis.ArrayBuffer && t instanceof globalThis.ArrayBuffer || globalThis.MessagePort && t instanceof globalThis.MessagePort || globalThis.ReadableStream && t instanceof globalThis.ReadableStream || globalThis.WritableStream && t instanceof globalThis.WritableStream || globalThis.TransformStream && t instanceof globalThis.TransformStream ? !0 : !!(globalThis.ImageBitmap && t instanceof globalThis.ImageBitmap), b = (t) => {
|
|
2
|
+
const e = [], s = (r) => P(r) ? void 0 : h(r) ? e.push(r) : Array.isArray(r) ? r.map(s) : r && typeof r == "object" ? Object.values(r).map(s) : void 0;
|
|
3
|
+
return s(t), e;
|
|
4
|
+
}, p = "__proxyFunctionPort__", L = "__proxyMessageChannelPort__", A = (t) => {
|
|
5
|
+
const { port1: e, port2: s } = new MessageChannel();
|
|
6
|
+
return e.addEventListener("message", async (r) => {
|
|
26
7
|
try {
|
|
27
|
-
const a = await
|
|
28
|
-
|
|
8
|
+
const a = await t(...r.data), n = m(a), o = b(n);
|
|
9
|
+
e.postMessage({ result: n }, { transfer: o });
|
|
29
10
|
} catch (a) {
|
|
30
|
-
|
|
11
|
+
e.postMessage({ error: a });
|
|
31
12
|
}
|
|
32
|
-
}),
|
|
33
|
-
},
|
|
34
|
-
Object.entries(
|
|
35
|
-
|
|
36
|
-
|
|
13
|
+
}), e.start(), s;
|
|
14
|
+
}, m = (t) => P(t) || h(t) ? t : typeof t == "function" ? { [p]: A(t) } : Array.isArray(t) ? t.map(m) : t && typeof t == "object" ? Object.fromEntries(
|
|
15
|
+
Object.entries(t).map(([e, s]) => [
|
|
16
|
+
e,
|
|
17
|
+
m(s)
|
|
37
18
|
])
|
|
38
|
-
) :
|
|
39
|
-
const a =
|
|
40
|
-
c.data.error ?
|
|
19
|
+
) : t, _ = (t) => (...e) => new Promise((s, r) => {
|
|
20
|
+
const a = m(e), n = b(a), o = (c) => {
|
|
21
|
+
c.data.error ? r(c.data.error) : s(M(c.data.result)), t.removeEventListener("message", o);
|
|
41
22
|
};
|
|
42
|
-
|
|
43
|
-
}),
|
|
44
|
-
Object.entries(
|
|
45
|
-
|
|
46
|
-
|
|
23
|
+
t.addEventListener("message", o), t.start(), t.postMessage(a, { transfer: n });
|
|
24
|
+
}), M = (t) => P(t) || h(t) ? t : t && typeof t == "object" && t[p] ? _(t[p]) : Array.isArray(t) ? t.map(m) : t && typeof t == "object" ? Object.fromEntries(
|
|
25
|
+
Object.entries(t).map(([e, s]) => [
|
|
26
|
+
e,
|
|
27
|
+
M(s)
|
|
47
28
|
])
|
|
48
|
-
) :
|
|
49
|
-
|
|
50
|
-
|
|
29
|
+
) : t, C = ({
|
|
30
|
+
target: t,
|
|
31
|
+
resolvers: e,
|
|
32
|
+
filter: s,
|
|
33
|
+
map: r,
|
|
34
|
+
key: a = f,
|
|
35
|
+
proxyTarget: n
|
|
36
|
+
}) => {
|
|
37
|
+
const o = (c) => {
|
|
38
|
+
if (!c.data || typeof c.data != "object" || c.data?.source !== a || s && !s(c))
|
|
39
|
+
return;
|
|
40
|
+
if (n) {
|
|
41
|
+
const { type: l, data: O, port: T } = c.data, x = b(O);
|
|
42
|
+
n.postMessage(
|
|
43
|
+
{
|
|
44
|
+
source: a,
|
|
45
|
+
type: l,
|
|
46
|
+
data: O,
|
|
47
|
+
port: T
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
targetOrigin: "*",
|
|
51
|
+
transfer: [T, ...x ?? []]
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const { type: i, data: d, port: g } = c.data, y = e[i];
|
|
57
|
+
if (!y)
|
|
58
|
+
throw new Error(`Osra received a message of type "${String(i)}" but no resolver was found for type.`);
|
|
59
|
+
r ? y(...r(d, { event: c, type: i, port: g })) : y(d, { event: c, type: i, port: g });
|
|
60
|
+
};
|
|
61
|
+
return t.addEventListener("message", o), {
|
|
62
|
+
listener: o,
|
|
63
|
+
resolvers: e
|
|
64
|
+
};
|
|
65
|
+
}, R = (t, { key: e = f } = { key: f }) => (s, r) => new Promise((a, n) => {
|
|
66
|
+
const { port1: o, port2: c } = new MessageChannel();
|
|
67
|
+
o.addEventListener(
|
|
51
68
|
"message",
|
|
52
|
-
({ data:
|
|
53
|
-
if (
|
|
54
|
-
|
|
69
|
+
({ data: g }) => {
|
|
70
|
+
if (g.error)
|
|
71
|
+
n(g.error);
|
|
55
72
|
else {
|
|
56
|
-
const
|
|
57
|
-
a(
|
|
73
|
+
const y = M(g.result);
|
|
74
|
+
a(y);
|
|
58
75
|
}
|
|
59
|
-
|
|
76
|
+
o.close(), c.close();
|
|
60
77
|
},
|
|
61
78
|
{ once: !0 }
|
|
62
|
-
),
|
|
63
|
-
const i =
|
|
64
|
-
|
|
79
|
+
), o.start();
|
|
80
|
+
const i = m(r), d = b(i);
|
|
81
|
+
t.postMessage(
|
|
65
82
|
{
|
|
66
|
-
source:
|
|
67
|
-
type:
|
|
83
|
+
source: e,
|
|
84
|
+
type: s,
|
|
68
85
|
data: i,
|
|
69
86
|
port: c
|
|
70
87
|
},
|
|
@@ -73,109 +90,133 @@ const g = "osra-message", l = ({
|
|
|
73
90
|
transfer: [c, ...d ?? []]
|
|
74
91
|
}
|
|
75
92
|
);
|
|
76
|
-
}),
|
|
77
|
-
const { port:
|
|
93
|
+
}), D = (t) => async (e, s) => {
|
|
94
|
+
const { port: r } = s, a = M(e);
|
|
78
95
|
try {
|
|
79
|
-
const
|
|
80
|
-
return
|
|
81
|
-
} catch (
|
|
82
|
-
throw
|
|
96
|
+
const n = await t(a, s), o = m(n), c = b(o);
|
|
97
|
+
return r.postMessage({ result: o }, { transfer: c }), r.close(), n;
|
|
98
|
+
} catch (n) {
|
|
99
|
+
throw r.postMessage({ error: n }), r.close(), n;
|
|
83
100
|
}
|
|
84
|
-
},
|
|
85
|
-
const
|
|
101
|
+
}, $ = (t, { key: e = f } = { key: f }) => (s, r) => {
|
|
102
|
+
const { type: a, port: n } = r, o = b(s);
|
|
103
|
+
t.postMessage(
|
|
104
|
+
{
|
|
105
|
+
source: e,
|
|
106
|
+
type: a,
|
|
107
|
+
data: s,
|
|
108
|
+
port: n
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
targetOrigin: "*",
|
|
112
|
+
transfer: [n, ...o ?? []]
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
}, j = ({ key: t = f }) => {
|
|
116
|
+
const e = /* @__PURE__ */ new Map(), s = async (a) => e.get(a) ?? new Promise((n, o) => {
|
|
86
117
|
let c = !1;
|
|
87
118
|
setTimeout(() => {
|
|
88
|
-
c ||
|
|
119
|
+
c || o(new Error(`Osra getPort for "${a}" timed out`));
|
|
89
120
|
}, 1e3), chrome.runtime.onConnect.addListener((i) => {
|
|
90
|
-
i.name === `${
|
|
121
|
+
i.name === `${t}:${a}` && (e.set(a, i), n(i), c = !0);
|
|
91
122
|
});
|
|
92
|
-
}),
|
|
93
|
-
addPort: (a,
|
|
123
|
+
}), r = {
|
|
124
|
+
addPort: (a, n) => e.set(a, n),
|
|
94
125
|
proxyPort: (a) => {
|
|
95
|
-
const
|
|
96
|
-
return
|
|
97
|
-
|
|
98
|
-
}),
|
|
126
|
+
const n = self.crypto.randomUUID(), o = chrome.runtime.connect({ name: `${t}:${n}` });
|
|
127
|
+
return e.set(n, o), o.onDisconnect.addListener(() => {
|
|
128
|
+
e.delete(n);
|
|
129
|
+
}), o.onMessage.addListener((c) => {
|
|
99
130
|
a.postMessage(c);
|
|
100
131
|
}), a.addEventListener("message", async (c) => {
|
|
101
|
-
|
|
132
|
+
o.postMessage(c.data);
|
|
102
133
|
}), a.addEventListener("close", () => {
|
|
103
|
-
|
|
134
|
+
o.disconnect();
|
|
104
135
|
}), {
|
|
105
|
-
[
|
|
136
|
+
[L]: n
|
|
106
137
|
};
|
|
107
138
|
},
|
|
108
|
-
proxiedPort: async ({ [
|
|
109
|
-
const { port1:
|
|
139
|
+
proxiedPort: async ({ [L]: a }) => {
|
|
140
|
+
const { port1: n, port2: o } = new MessageChannel(), c = await s(a);
|
|
110
141
|
c.onMessage.addListener((d) => {
|
|
111
|
-
|
|
142
|
+
n.postMessage(d);
|
|
112
143
|
}), c.onDisconnect.addListener(() => {
|
|
113
|
-
|
|
114
|
-
}),
|
|
144
|
+
n.close();
|
|
145
|
+
}), n.addEventListener("close", () => {
|
|
115
146
|
c.disconnect();
|
|
116
|
-
}),
|
|
147
|
+
}), n.addEventListener("message", async (d) => {
|
|
117
148
|
c.postMessage(d.data);
|
|
118
|
-
}),
|
|
149
|
+
}), n.start(), o.addEventListener("message", async (d) => {
|
|
119
150
|
c.postMessage(d.data);
|
|
120
|
-
}),
|
|
121
|
-
const i =
|
|
151
|
+
}), o.start();
|
|
152
|
+
const i = S(r, o);
|
|
122
153
|
return i.uuid = a, i;
|
|
123
154
|
}
|
|
124
155
|
};
|
|
125
|
-
return
|
|
126
|
-
},
|
|
127
|
-
Object.entries(
|
|
128
|
-
|
|
129
|
-
|
|
156
|
+
return r;
|
|
157
|
+
}, E = (t, e) => e instanceof MessagePort ? t.proxyPort(e) : P(e) || h(e) ? e : typeof e == "object" && p in e ? { [p]: t.proxyPort(e[p]) } : Array.isArray(e) ? e.map((s) => E(t, s)) : e && typeof e == "object" ? Object.fromEntries(
|
|
158
|
+
Object.entries(e).map(([s, r]) => [
|
|
159
|
+
s,
|
|
160
|
+
E(t, r)
|
|
130
161
|
])
|
|
131
|
-
) :
|
|
162
|
+
) : e, w = async (t, e) => P(e) || h(e) ? e : typeof e == "object" && L in e ? await t.proxiedPort(e) : typeof e == "object" && p in e ? { [p]: await t.proxiedPort(e[p]) } : Array.isArray(e) ? await Promise.all(e.map((s) => w(t, s))) : e && typeof e == "object" ? Object.fromEntries(
|
|
132
163
|
await Promise.all(
|
|
133
|
-
Object.entries(
|
|
134
|
-
|
|
135
|
-
await
|
|
164
|
+
Object.entries(e).map(async ([s, r]) => [
|
|
165
|
+
s,
|
|
166
|
+
await w(t, r)
|
|
136
167
|
])
|
|
137
168
|
)
|
|
138
|
-
) :
|
|
139
|
-
const
|
|
140
|
-
return
|
|
141
|
-
|
|
142
|
-
},
|
|
143
|
-
},
|
|
144
|
-
target:
|
|
145
|
-
key:
|
|
169
|
+
) : e, S = (t, e) => {
|
|
170
|
+
const s = e.postMessage;
|
|
171
|
+
return e.postMessage = (r) => {
|
|
172
|
+
s.apply(e, [E(t, r)]);
|
|
173
|
+
}, e;
|
|
174
|
+
}, B = ({
|
|
175
|
+
target: t,
|
|
176
|
+
key: e = f
|
|
146
177
|
}) => {
|
|
147
|
-
const
|
|
178
|
+
const s = j({ key: e });
|
|
148
179
|
return {
|
|
149
|
-
...
|
|
150
|
-
postMessage: (
|
|
151
|
-
|
|
180
|
+
...t,
|
|
181
|
+
postMessage: (r) => {
|
|
182
|
+
t.runtime.sendMessage(E(s, r));
|
|
152
183
|
}
|
|
153
184
|
};
|
|
154
|
-
},
|
|
155
|
-
target:
|
|
156
|
-
key:
|
|
185
|
+
}, F = ({
|
|
186
|
+
target: t,
|
|
187
|
+
key: e = f
|
|
157
188
|
}) => {
|
|
158
|
-
const
|
|
189
|
+
const s = j({ key: e });
|
|
159
190
|
return {
|
|
160
|
-
...
|
|
161
|
-
addEventListener: (
|
|
162
|
-
if (
|
|
163
|
-
return
|
|
164
|
-
chrome.runtime.onMessage.addListener((
|
|
165
|
-
|
|
166
|
-
}), chrome.runtime.onConnect.addListener((
|
|
167
|
-
|
|
191
|
+
...t,
|
|
192
|
+
addEventListener: (r, a, n) => {
|
|
193
|
+
if (r !== "message")
|
|
194
|
+
return t.addEventListener(r, a, n);
|
|
195
|
+
chrome.runtime.onMessage.addListener((o, c, i) => {
|
|
196
|
+
w(s, o).then((d) => a({ data: d }));
|
|
197
|
+
}), chrome.runtime.onConnect.addListener((o) => {
|
|
198
|
+
o.name.startsWith(`${e}:`) && s.addPort(o.name.split(":")[1], o);
|
|
168
199
|
});
|
|
169
200
|
}
|
|
170
201
|
};
|
|
171
202
|
};
|
|
172
203
|
export {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
P as
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
204
|
+
p as PROXY_FUNCTION_PROPERTY,
|
|
205
|
+
L as PROXY_MESSAGE_CHANNEL_PROPERTY,
|
|
206
|
+
R as call,
|
|
207
|
+
b as getTransferableObjects,
|
|
208
|
+
P as isClonable,
|
|
209
|
+
h as isTransferable,
|
|
210
|
+
D as makeCallListener,
|
|
211
|
+
M as makeObjectProxiedFunctions,
|
|
212
|
+
_ as makeProxiedFunction,
|
|
213
|
+
$ as makeProxyCallListener,
|
|
214
|
+
A as makeProxyFunction,
|
|
215
|
+
m as proxyObjectFunctions,
|
|
216
|
+
C as registerListener,
|
|
217
|
+
w as replaceProxied,
|
|
218
|
+
E as replaceProxies,
|
|
219
|
+
B as wrapExtensionTarget,
|
|
220
|
+
F as wrapListenerExtensionTarget,
|
|
221
|
+
S as wrapPort
|
|
181
222
|
};
|
package/build/register.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { ApiMessageData, ApiResolverOptions, Resolvers, ValidateResolvers } from './types';
|
|
2
|
-
export declare const registerListener: <T extends Record<PropertyKey, (data: any, extra: ApiResolverOptions) => unknown>>({ target, resolvers, filter, map, key }: {
|
|
1
|
+
import type { ApiMessageData, ApiResolverOptions, Resolvers, Target, ValidateResolvers } from './types';
|
|
2
|
+
export declare const registerListener: <T extends Record<PropertyKey, (data: any, extra: ApiResolverOptions) => unknown>>({ target, resolvers, filter, map, key, proxyTarget }: {
|
|
3
3
|
target: WindowEventHandlers | ServiceWorkerContainer | Worker | SharedWorker;
|
|
4
4
|
resolvers: ValidateResolvers<T>;
|
|
5
5
|
filter?: ((event: MessageEvent<any>) => boolean) | undefined;
|
|
@@ -13,6 +13,7 @@ export declare const registerListener: <T extends Record<PropertyKey, (data: any
|
|
|
13
13
|
port: MessagePort;
|
|
14
14
|
}]) | undefined;
|
|
15
15
|
key?: string | undefined;
|
|
16
|
+
proxyTarget?: Target | undefined;
|
|
16
17
|
}) => {
|
|
17
18
|
listener: (event: MessageEvent<ApiMessageData<Resolvers>>) => void;
|
|
18
19
|
resolvers: ValidateResolvers<T>;
|
package/build/types.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export type StructuredCloneTransferableObject = {
|
|
|
8
8
|
};
|
|
9
9
|
export type ProxiedType = (...args: StructuredCloneTransferableType[]) => StructuredCloneTransferableType | Promise<StructuredCloneTransferableType>;
|
|
10
10
|
export type StructuredCloneTransferableType = StructuredCloneType | TransferableObject | Array<StructuredCloneTransferableType> | StructuredCloneTransferableObject | Map<StructuredCloneTransferableType, StructuredCloneTransferableType> | Set<StructuredCloneTransferableType> | ProxiedType;
|
|
11
|
-
export type Target = Window | ServiceWorker | Worker;
|
|
11
|
+
export type Target = Window | ServiceWorker | Worker | MessagePort;
|
|
12
12
|
type NormalizeRecord<T> = T extends Record<any, any> ? {
|
|
13
13
|
[K in keyof T]: NormalizeRecord<T[K]>;
|
|
14
14
|
} : T;
|