osra 0.1.2 → 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.
Files changed (35) hide show
  1. package/README.md +287 -42
  2. package/build/extension/background.js +2 -0
  3. package/build/extension/background.js.map +1 -0
  4. package/build/extension/chunks/index.js +2 -0
  5. package/build/extension/chunks/index.js.map +1 -0
  6. package/build/extension/content.js +2 -0
  7. package/build/extension/content.js.map +1 -0
  8. package/build/extension/manifest.json +21 -0
  9. package/build/extension/popup.html +120 -0
  10. package/build/extension/popup.js +2 -0
  11. package/build/extension/popup.js.map +1 -0
  12. package/build/extension-test/background.js +753 -0
  13. package/build/extension-test/background.js.map +1 -0
  14. package/build/extension-test/content.js +4585 -0
  15. package/build/extension-test/content.js.map +1 -0
  16. package/build/extension-test/manifest.json +22 -0
  17. package/build/extension-test/popup.html +106 -0
  18. package/build/extension-test/popup.js +4610 -0
  19. package/build/extension-test/popup.js.map +1 -0
  20. package/build/extension-test-firefox/background.js +5464 -0
  21. package/build/extension-test-firefox/background.js.map +1 -0
  22. package/build/extension-test-firefox/content.js +5286 -0
  23. package/build/extension-test-firefox/content.js.map +1 -0
  24. package/build/extension-test-firefox/manifest.json +27 -0
  25. package/build/extension-test-firefox/popup.html +106 -0
  26. package/build/extension-test-firefox/popup.js +5213 -0
  27. package/build/extension-test-firefox/popup.js.map +1 -0
  28. package/build/index.d.ts +155 -8
  29. package/build/index.js +432 -259
  30. package/build/index.js.map +1 -1
  31. package/build/test.js +24021 -2578
  32. package/build/test.js.map +1 -1
  33. package/package.json +34 -16
  34. package/build/types.d.ts +0 -66
  35. package/build/utils.d.ts +0 -60
package/build/index.js CHANGED
@@ -1,280 +1,453 @@
1
- const p = [];
2
- for (let e = 0; e < 256; ++e)
3
- p.push((e + 256).toString(16).slice(1));
4
- function N(e, t = 0) {
5
- return (p[e[t + 0]] + p[e[t + 1]] + p[e[t + 2]] + p[e[t + 3]] + "-" + p[e[t + 4]] + p[e[t + 5]] + "-" + p[e[t + 6]] + p[e[t + 7]] + "-" + p[e[t + 8]] + p[e[t + 9]] + "-" + p[e[t + 10]] + p[e[t + 11]] + p[e[t + 12]] + p[e[t + 13]] + p[e[t + 14]] + p[e[t + 15]]).toLowerCase();
6
- }
7
- let _;
8
- const D = new Uint8Array(16);
9
- function z() {
10
- if (!_) {
11
- if (typeof crypto > "u" || !crypto.getRandomValues)
12
- throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
13
- _ = crypto.getRandomValues.bind(crypto);
14
- }
15
- return _(D);
16
- }
17
- const Y = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), j = { randomUUID: Y };
18
- function V(e, t, r) {
19
- if (j.randomUUID && !t && !e)
20
- return j.randomUUID();
21
- e = e || {};
22
- const o = e.random || (e.rng || z)();
23
- return o[6] = o[6] & 15 | 64, o[8] = o[8] & 63 | 128, N(o);
24
- }
25
- const E = "__OSRA__", G = "__OSRA_DEFAULT_KEY__", w = "__OSRA_PROXY__", x = (e) => !!(globalThis.SharedArrayBuffer && e instanceof globalThis.SharedArrayBuffer), B = (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), M = (e) => {
26
- const t = [], r = (o) => x(o) ? void 0 : B(o) ? t.push(o) : Array.isArray(o) ? o.map(r) : o && typeof o == "object" ? Object.values(o).map(r) : void 0;
27
- return r(e), t;
28
- }, K = (e, t) => {
29
- const r = "portId" in e ? e.portId : void 0, o = "port" in e ? e.port : r ? t.addIncomingProxiedMessagePort(r) : void 0;
30
- if (!o) throw new Error("No ports received for proxied function");
31
- const c = (...n) => new Promise((i, f) => {
32
- const { port1: g, port2: l } = new MessageChannel(), m = y([l, n], t), A = M(m), S = (h) => {
33
- const b = R(h.data, t);
34
- if (!(b instanceof Promise)) throw new Error("Proxied function did not return a promise");
35
- b.then(i).catch(f).finally(() => g.close());
36
- };
37
- g.addEventListener("message", S, { once: !0 }), g.start(), o.postMessage(m, { transfer: A });
38
- });
39
- return r && t.finalizationRegistry.register(c, Number(r.split("/")[1])), c;
40
- }, W = (e, t) => {
41
- const r = t.envCheck.supportsPorts && "port" in e ? e.port : "portId" in e ? t.addIncomingProxiedMessagePort(e.portId) : void 0;
42
- if (!r) throw new Error("No ports received for proxied message port");
1
+ const M = "__OSRA_KEY__", oe = "__OSRA_DEFAULT_KEY__", E = "__OSRA_BOX__", ae = () => {
2
+ const e = /* @__PURE__ */ new Map(), r = {
3
+ getUniqueUuid: () => {
4
+ let t = globalThis.crypto.randomUUID();
5
+ for (; e.has(t); )
6
+ t = globalThis.crypto.randomUUID();
7
+ return t;
8
+ },
9
+ set: (t, n) => {
10
+ e.set(t, { uuid: t, ...n });
11
+ },
12
+ alloc: (t = r.getUniqueUuid(), n) => {
13
+ if (n)
14
+ return e.set(t, { uuid: t, ...n }), { uuid: t, ...n };
15
+ const o = new MessageChannel(), s = {
16
+ uuid: t,
17
+ port1: o.port1,
18
+ port2: o.port2
19
+ };
20
+ return e.set(t, s), s;
21
+ },
22
+ has: (t) => e.has(t),
23
+ get: (t) => e.get(t),
24
+ free: (t) => e.delete(t),
25
+ getOrAlloc: (t = r.getUniqueUuid(), n) => {
26
+ const o = r.get(t);
27
+ return o || r.alloc(t, n);
28
+ }
29
+ };
43
30
  return r;
44
- }, X = (e, t) => new Error(e.message, { cause: e.stack }), H = (e, t) => new Promise((r, o) => {
45
- const c = "port" in e ? e.port : "portId" in e ? t.addIncomingProxiedMessagePort(e.portId) : void 0;
46
- if (!c) throw new Error("No ports received for proxied promise");
47
- const n = async (i) => {
48
- const f = await R(i.data, t);
49
- f instanceof Error ? o(f) : r(f), c.close();
31
+ }, $ = (e, r) => le(e) && e[M] === r, ie = ({ listener: e, transport: r, remoteName: t, key: n = M, unregisterSignal: o }) => {
32
+ const s = (a) => {
33
+ if (typeof a == "function")
34
+ a(e);
35
+ else if (I(a) || O(a) || L(a)) {
36
+ const c = (i, l) => {
37
+ const f = (d, g) => {
38
+ $(d, n) && (t && d.name !== t || e(d, { port: l, sender: g }));
39
+ };
40
+ i.addListener(f), o && o.addEventListener(
41
+ "abort",
42
+ () => i.removeListener(f)
43
+ );
44
+ };
45
+ if (O(a)) {
46
+ const i = (l) => {
47
+ c(l.onMessage, l);
48
+ };
49
+ a.addListener(i), o && o.addEventListener(
50
+ "abort",
51
+ () => a.removeListener(i)
52
+ );
53
+ } else L(a) ? c(a) : c(a.onMessage);
54
+ } else {
55
+ const c = (i) => {
56
+ $(i.data, n) && (t && i.data.name !== t || e(i.data, { receiveTransport: a, source: i.source }));
57
+ };
58
+ a.addEventListener("message", c), o && o.addEventListener(
59
+ "abort",
60
+ () => a.removeEventListener("message", c)
61
+ );
62
+ }
50
63
  };
51
- c.addEventListener("message", n, { once: !0 }), c.start();
52
- }), R = (e, t) => O(
53
- e,
54
- (r) => !!(r && typeof r == "object" && w in r && r[w]),
55
- (r) => {
56
- if (r.type === "function")
57
- return K(r, t);
58
- if (r.type === "error")
59
- return X(r);
60
- if (r.type === "messagePort")
61
- return W(r, t);
62
- if (r.type === "promise")
63
- return H(r, t);
64
- throw new Error(`Unknown incoming proxied type: ${e}`);
65
- }
66
- ), q = (e, t) => ({
67
- [w]: !0,
68
- type: "error",
69
- message: e.message,
70
- stack: e.stack
71
- }), F = (e, t) => ({
72
- [w]: !0,
73
- type: "messagePort",
74
- ...t.envCheck.supportsPorts ? { port: e } : { portId: t.addOutgoingProxiedMessagePort(e) }
75
- }), J = (e, t) => {
76
- const { port1: r, port2: o } = new MessageChannel(), c = (n) => {
77
- const i = y(n, t), f = M(i);
78
- r.postMessage(i, { transfer: f }), r.close();
64
+ K(r) ? s(r.receive) : s(r);
65
+ }, ce = (e, r, t = "*", n = []) => {
66
+ const o = (s) => {
67
+ typeof s == "function" ? s(r, n) : I(s) ? s.postMessage(r) : q(s) ? s.postMessage(r, t, n) : k(s) ? s.send(JSON.stringify(r)) : W(s) ? s.port.postMessage(r, n) : s.postMessage(r, n);
79
68
  };
80
- return e.then(c).catch(c), {
81
- [w]: !0,
69
+ K(e) ? o(e.emit) : o(e);
70
+ }, ye = [
71
+ Int8Array,
72
+ Uint8Array,
73
+ Uint8ClampedArray,
74
+ Int16Array,
75
+ Uint16Array,
76
+ Int32Array,
77
+ Uint32Array,
78
+ Float16Array,
79
+ Float32Array,
80
+ Float64Array,
81
+ BigInt64Array,
82
+ BigUint64Array
83
+ ];
84
+ new Int8Array(), new Uint8Array(), new Uint8ClampedArray(), new Int16Array(), new Uint16Array(), new Int32Array(), new Uint32Array(), new Float16Array(), new Float32Array(), new Float64Array(), new BigInt64Array(), new BigUint64Array();
85
+ const fe = (e) => {
86
+ const r = e instanceof Int8Array ? "Int8Array" : e instanceof Uint8Array ? "Uint8Array" : e instanceof Uint8ClampedArray ? "Uint8ClampedArray" : e instanceof Int16Array ? "Int16Array" : e instanceof Uint16Array ? "Uint16Array" : e instanceof Int32Array ? "Int32Array" : e instanceof Uint32Array ? "Uint32Array" : e instanceof Float16Array ? "Float16Array" : e instanceof Float32Array ? "Float32Array" : e instanceof Float64Array ? "Float64Array" : e instanceof BigInt64Array ? "BigInt64Array" : e instanceof BigUint64Array ? "BigUint64Array" : void 0;
87
+ if (r === void 0) throw new Error("Unknown typed array type");
88
+ return r;
89
+ }, de = (e) => {
90
+ const r = e === "Int8Array" ? Int8Array : e === "Uint8Array" ? Uint8Array : e === "Uint8ClampedArray" ? Uint8ClampedArray : e === "Int16Array" ? Int16Array : e === "Uint16Array" ? Uint16Array : e === "Int32Array" ? Int32Array : e === "Uint32Array" ? Uint32Array : e === "Float16Array" ? Float16Array : e === "Float32Array" ? Float32Array : e === "Float64Array" ? Float64Array : e === "BigInt64Array" ? BigInt64Array : e === "BigUint64Array" ? BigUint64Array : void 0;
91
+ if (r === void 0) throw new Error("Unknown typed array type");
92
+ return r;
93
+ }, j = (e) => ye.some((r) => e instanceof r), k = (e) => e instanceof WebSocket, H = (e) => globalThis.ServiceWorkerContainer && e instanceof ServiceWorkerContainer, Q = (e) => globalThis.Worker && e instanceof Worker, Z = (e) => globalThis.DedicatedWorkerGlobalScope && e instanceof DedicatedWorkerGlobalScope, W = (e) => globalThis.SharedWorker && e instanceof SharedWorker, C = (e) => e instanceof MessagePort, D = (e) => e instanceof Promise, _ = (e) => typeof e == "function", T = (e) => e instanceof ArrayBuffer, B = (e) => e instanceof ReadableStream, V = (e) => e instanceof Date, J = (e) => e instanceof Error, pe = (e) => _(e) || D(e) || j(e) || V(e) || J(e), le = (e) => !!(e && typeof e == "object" && e[M]), ge = (e) => !!(globalThis.SharedArrayBuffer && e instanceof globalThis.SharedArrayBuffer), N = (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), I = (e, r = !1) => !!(e && typeof e == "object" && e.name && e.disconnect && e.postMessage && (!r || e.sender && e.onMessage && e.onDisconnect)), O = (e) => !!(e && typeof e == "object" && e.addListener && e.hasListener && e.removeListener), L = (e) => !!(e && typeof e == "object" && e.addListener && e.hasListener && e.removeListener), q = (e) => !!(e && typeof e == "object" && e.document && e.location && e.navigator && e.screen && e.history), x = (e) => k(e) || I(e), v = (e) => k(e) || I(e) || O(e) || L(e), Ae = (e) => x(e) || v(e), P = (e) => x(e) || q(e) || H(e) || Q(e) || Z(e) || W(e) || C(e) || ee(e), F = (e) => v(e) || q(e) || H(e) || Q(e) || Z(e) || W(e) || C(e) || re(e), ee = (e) => !!(e && typeof e == "object" && "emit" in e && (P(e.emit) || typeof e.emit == "function")), re = (e) => !!(e && typeof e == "object" && "receive" in e && (F(e.receive) || typeof e.receive == "function")), K = (e) => ee(e) || re(e), te = (e) => C(e) || _(e) || D(e) || j(e) || T(e) || B(e) || V(e) || J(e), p = (e) => e && typeof e == "object" && E in e && e[E] === "revivable", me = (e) => p(e) && e.type === "messagePort", Y = (e) => p(e) && e.type === "promise", be = (e) => p(e) && e.type === "function", he = (e) => p(e) && e.type === "typedArray", ue = (e) => p(e) && e.type === "arrayBuffer", we = (e) => p(e) && e.type === "readableStream", Ue = (e) => p(e) && e.type === "error", Be = (e) => p(e) && e.type === "date", u = (e) => {
94
+ const r = [], t = (n) => ge(n) ? void 0 : N(n) ? r.push(n) : Array.isArray(n) ? n.map(t) : n && typeof n == "object" ? Object.values(n).map(t) : void 0;
95
+ return t(e), r;
96
+ }, S = (e, r = !1) => {
97
+ const { port1: t, port2: n } = new MessageChannel(), o = new Promise(
98
+ (s) => t.addEventListener(
99
+ "message",
100
+ (a) => s(a.data)
101
+ )
102
+ );
103
+ return t.start(), n.postMessage(e, r ? u(e) : []), o;
104
+ }, Ce = async () => {
105
+ const { port1: e } = new MessageChannel();
106
+ return await S(e, !0) instanceof MessagePort;
107
+ }, Pe = async () => {
108
+ const e = new ArrayBuffer(1);
109
+ return await S(e) instanceof ArrayBuffer;
110
+ }, Ee = async () => {
111
+ const e = new ArrayBuffer(1);
112
+ return await S(e, !0) instanceof ArrayBuffer;
113
+ }, Me = async () => {
114
+ const e = new ReadableStream({
115
+ start(t) {
116
+ t.enqueue(new Uint8Array(1)), t.close();
117
+ }
118
+ });
119
+ return await S(e, !0) instanceof ReadableStream;
120
+ }, Ie = async () => {
121
+ const [
122
+ e,
123
+ r,
124
+ t,
125
+ n
126
+ ] = await Promise.all([
127
+ Ce().catch(() => !1),
128
+ Pe().catch(() => !1),
129
+ Ee().catch(() => !1),
130
+ Me().catch(() => !1)
131
+ ]);
132
+ return {
133
+ jsonOnly: !e && !r && !t && !n,
134
+ messagePort: e,
135
+ arrayBuffer: r,
136
+ transferable: t,
137
+ transferableStream: n
138
+ };
139
+ }, Se = (e, r) => {
140
+ const t = e, { uuid: n } = r.messageChannels.alloc(void 0, { port1: t });
141
+ return t.addEventListener("message", ({ data: o }) => {
142
+ r.sendMessage({
143
+ type: "message",
144
+ remoteUuid: r.remoteUuid,
145
+ data: p(o) ? o : m(o, r),
146
+ portId: n
147
+ });
148
+ }), t.start(), r.eventTarget.addEventListener("message", function o({ detail: s }) {
149
+ if (s.type === "message-port-close") {
150
+ if (s.portId !== n) return;
151
+ r.eventTarget.removeEventListener("message", o), t.close(), r.messageChannels.free(n);
152
+ return;
153
+ }
154
+ s.type !== "message" || s.portId !== n || t.postMessage(s.data, u(s.data));
155
+ }), {
156
+ type: "messagePort",
157
+ portId: n
158
+ };
159
+ }, Re = (e, r) => {
160
+ const { port1: t, port2: n } = new MessageChannel();
161
+ n.addEventListener("message", ({ data: a }) => {
162
+ r.sendMessage({
163
+ type: "message",
164
+ remoteUuid: r.remoteUuid,
165
+ data: p(a) ? a : m(a, r),
166
+ portId: e.portId
167
+ });
168
+ }), n.start();
169
+ const o = r.messageChannels.get(e.portId), { port1: s } = o || r.messageChannels.alloc(e.portId);
170
+ return s.addEventListener("message", function a({ data: c }) {
171
+ if (c.type === "message-port-close") {
172
+ if (c.portId !== e.portId) return;
173
+ s.removeEventListener("message", a), n.close(), r.messageChannels.free(e.portId);
174
+ return;
175
+ }
176
+ if (!(c.type !== "message" || c.portId !== e.portId))
177
+ if (r.messagePorts.has(t))
178
+ n.postMessage(c.data);
179
+ else {
180
+ const i = h(c.data, r);
181
+ n.postMessage(i, u(i));
182
+ }
183
+ }), s.start(), t;
184
+ }, Te = (e, r) => {
185
+ const { port1: t, port2: n } = new MessageChannel();
186
+ r.messagePorts.add(n);
187
+ const o = (s) => {
188
+ const a = m(s, r);
189
+ t.postMessage(a, u(a)), t.close();
190
+ };
191
+ return e.then((s) => o({ type: "resolve", data: s })).catch((s) => o({ type: "reject", error: s.stack })), {
82
192
  type: "promise",
83
- port: y(o, t)
193
+ port: n
84
194
  };
85
- }, Q = (e, t) => {
86
- const { port1: r, port2: o } = new MessageChannel();
87
- return r.addEventListener("message", async (c) => {
88
- const [n, i] = R(c.data, t), f = (async () => e(...i))(), g = y(f, t), l = M(g);
89
- n.postMessage(g, { transfer: l }), n.close();
90
- }), r.start(), {
91
- [w]: !0,
195
+ }, Oe = (e, r) => (r.messagePorts.add(e.port), new Promise((t, n) => {
196
+ e.port.addEventListener("message", ({ data: o }) => {
197
+ const s = h(o, r);
198
+ s.type === "resolve" ? t(s.data) : n(s.error), e.port.close();
199
+ }, { once: !0 }), e.port.start();
200
+ })), Le = (e, r) => {
201
+ const { port1: t, port2: n } = new MessageChannel();
202
+ return r.messagePorts.add(n), t.addEventListener("message", ({ data: o }) => {
203
+ const [s, a] = h(o, r), c = (async () => e(...a))(), i = m(c, r);
204
+ s.postMessage(i, u(i));
205
+ }), t.start(), {
92
206
  type: "function",
93
- port: y(o, t)
207
+ port: n
94
208
  };
95
- }, y = (e, t) => O(
96
- e,
97
- (r) => typeof r == "function" || r instanceof Error || r instanceof MessagePort || r instanceof Promise,
98
- (r) => {
99
- if (typeof r == "function")
100
- return Q(r, t);
101
- if (r instanceof Error)
102
- return q(r);
103
- if (r instanceof MessagePort)
104
- return F(r, t);
105
- if (r instanceof Promise)
106
- return J(r, t);
107
- throw new Error(`Unknown outgoing proxied type: ${r}`);
209
+ }, Fe = (e, r) => (...n) => new Promise((o, s) => {
210
+ const { port1: a, port2: c } = new MessageChannel();
211
+ r.messagePorts.add(c);
212
+ const i = m([c, n], r);
213
+ e.port.postMessage(i, u(i)), a.addEventListener("message", ({ data: l }) => {
214
+ if (!Y(l)) throw new Error("Proxied function did not return a promise");
215
+ h(l, r).then(o).catch(s).finally(() => a.close());
216
+ }), a.start();
217
+ }), je = (e, r) => ({
218
+ type: "typedArray",
219
+ typedArrayType: fe(e),
220
+ arrayBuffer: e.buffer
221
+ }), ke = (e, r) => {
222
+ const t = de(e.typedArrayType);
223
+ return new t(e.arrayBuffer);
224
+ }, We = (e, r) => ({
225
+ type: "arrayBuffer",
226
+ base64Buffer: new Uint8Array(e).toBase64()
227
+ }), De = (e, r) => Uint8Array.fromBase64(e.base64Buffer).buffer, _e = (e, r) => ({
228
+ type: "error",
229
+ message: e.message,
230
+ stack: e.stack || e.toString()
231
+ }), Ve = (e, r) => new Error(e.message, { cause: e.stack }), z = (e, r) => {
232
+ const { port1: t, port2: n } = new MessageChannel();
233
+ r.messagePorts.add(n);
234
+ const o = e.getReader();
235
+ return t.addEventListener("message", async ({ data: s }) => {
236
+ const { type: a } = h(s, r);
237
+ if (a === "pull") {
238
+ const c = o.read(), i = m(c, r);
239
+ t.postMessage(i, u(i));
240
+ } else
241
+ o.cancel(), t.close();
242
+ }), t.start(), {
243
+ type: "readableStream",
244
+ port: n
245
+ };
246
+ }, Je = (e, r) => (r.messagePorts.add(e.port), e.port.start(), new ReadableStream({
247
+ start(t) {
248
+ },
249
+ pull(t) {
250
+ return new Promise((n, o) => {
251
+ e.port.addEventListener("message", async ({ data: s }) => {
252
+ if (!Y(s)) throw new Error("Proxied function did not return a promise");
253
+ h(s, r).then((c) => {
254
+ c.done ? t.close() : t.enqueue(c.value), n();
255
+ }).catch(o);
256
+ }, { once: !0 }), e.port.postMessage(m({ type: "pull" }, r));
257
+ });
258
+ },
259
+ cancel() {
260
+ e.port.postMessage(m({ type: "cancel" }, r)), e.port.close();
108
261
  }
109
- ), O = (e, t, r) => x(e) || B(e) ? e : t(e) ? r(e) : Array.isArray(e) ? e.map((o) => O(o, t, r)) : e && typeof e == "object" ? Object.fromEntries(
110
- Object.entries(e).map(([o, c]) => [
111
- o,
112
- O(c, t, r)
113
- ])
114
- ) : e, Z = () => {
115
- let e = 0;
116
- const t = /* @__PURE__ */ new Set();
117
- return {
118
- alloc: () => {
119
- if (t.size > 0) {
120
- const r = t.values().next().value;
121
- if (r === void 0)
122
- throw new Error("Tried to allocate number from freedUnused but result was undefined");
123
- return t.delete(r), r;
124
- }
125
- return e++, e;
126
- },
127
- free: (r) => {
128
- t.add(r);
129
- }
262
+ })), qe = (e, r) => ({
263
+ type: "date",
264
+ ISOString: e.toISOString()
265
+ }), Ke = (e, r) => new Date(e.ISOString), Ye = (e, r) => pe(e) || B(e) && !r.platformCapabilities.transferableStream ? {
266
+ [E]: "revivable",
267
+ ..._(e) ? Le(e, r) : D(e) ? Te(e, r) : j(e) ? je(e) : B(e) ? z(e, r) : V(e) ? qe(e) : J(e) ? _e(e) : e
268
+ } : {
269
+ [E]: "revivable",
270
+ ..."isJson" in r.transport && r.transport.isJson ? C(e) ? Se(e, r) : T(e) ? We(e) : B(e) ? z(e, r) : { type: "unknown", value: e } : {
271
+ type: C(e) ? "messagePort" : T(e) ? "arrayBuffer" : B(e) ? "readableStream" : "unknown",
272
+ value: e
273
+ }
274
+ }, m = (e, r) => {
275
+ const t = te(e) ? Ye(e, r) : e;
276
+ return Array.isArray(t) ? t.map((n) => m(n, r)) : t && typeof t == "object" && Object.getPrototypeOf(t) === Object.prototype ? Object.fromEntries(
277
+ Object.entries(t).map(([n, o]) => [
278
+ n,
279
+ p(t) && t.type === "messagePort" && t.value instanceof MessagePort || p(t) && t.type === "arrayBuffer" && t.value instanceof ArrayBuffer || p(t) && t.type === "readableStream" && t.value instanceof ReadableStream ? o : m(o, r)
280
+ ])
281
+ ) : t;
282
+ }, Ge = (e, r) => te(e.value) ? e.value : me(e) ? Re(e, r) : be(e) ? Fe(e, r) : Y(e) ? Oe(e, r) : Ue(e) ? Ve(e) : he(e) ? ke(e) : ue(e) ? De(e) : we(e) ? Je(e, r) : Be(e) ? Ke(e) : e, h = (e, r) => {
283
+ const t = N(e) ? e : Array.isArray(e) ? e.map((n) => h(n, r)) : e && typeof e == "object" ? Object.fromEntries(
284
+ Object.entries(e).map(([n, o]) => [
285
+ n,
286
+ h(o, r)
287
+ ])
288
+ ) : e;
289
+ return p(t) ? Ge(t, r) : t;
290
+ }, Xe = ({ transport: e, value: r, uuid: t, remoteUuid: n, platformCapabilities: o, eventTarget: s, send: a, close: c }) => {
291
+ const i = {
292
+ platformCapabilities: o,
293
+ transport: e,
294
+ remoteUuid: n,
295
+ messagePorts: /* @__PURE__ */ new Set(),
296
+ messageChannels: ae(),
297
+ sendMessage: a,
298
+ eventTarget: s
130
299
  };
131
- }, v = ({ numberAllocator: e }) => {
132
- const t = /* @__PURE__ */ new Map();
133
- return {
134
- alloc: (n) => {
135
- const i = e.alloc();
136
- return t.set(i, n), i;
300
+ let l;
301
+ const f = new Promise((d, g) => {
302
+ l = d;
303
+ });
304
+ return s.addEventListener("message", ({ detail: d }) => {
305
+ if (d.type === "init") {
306
+ l(d);
307
+ return;
308
+ } else d.type === "message" && i.messageChannels.getOrAlloc(d.portId).port2?.postMessage(d);
309
+ }), a({
310
+ type: "init",
311
+ remoteUuid: n,
312
+ data: m(r, i)
313
+ }), {
314
+ revivableContext: i,
315
+ close: () => {
137
316
  },
138
- get: (n) => t.get(n),
139
- free: (n) => {
140
- t.delete(n), e.free(n);
141
- }
317
+ remoteValue: f.then((d) => h(d.data, i))
142
318
  };
143
- }, ee = async (e, {
144
- remote: t,
145
- local: r,
146
- key: o = G,
147
- origin: c = "*"
319
+ }, $e = ({ value: e, uuid: r, platformCapabilities: t, send: n, close: o }) => ({
320
+ close: () => {
321
+ },
322
+ remoteValueProxy: new Proxy(
323
+ new Function(),
324
+ {
325
+ apply: (s, a, c) => {
326
+ },
327
+ get: (s, a) => {
328
+ }
329
+ }
330
+ )
331
+ });
332
+ var ze = class extends EventTarget {
333
+ dispatchTypedEvent(e, r) {
334
+ return super.dispatchEvent(r);
335
+ }
336
+ };
337
+ const He = async (e, {
338
+ transport: r,
339
+ name: t,
340
+ remoteName: n,
341
+ key: o = oe,
342
+ origin: s = "*",
343
+ unregisterSignal: a,
344
+ platformCapabilities: c,
345
+ transferAll: i,
346
+ logger: l
148
347
  }) => {
149
- const n = V();
150
- let i;
151
- const f = new FinalizationRegistry((s) => {
152
- const a = l.get(s);
153
- if (!a) throw new Error(`Osra received a port-closed message with an invalid portId "${s}".`);
154
- a(), l.free(s);
155
- }), g = Z(), l = v({ numberAllocator: g }), m = /* @__PURE__ */ new Map(), A = (s) => {
156
- const { port1: a, port2: d } = new MessageChannel();
157
- return m.set(s, a), d;
158
- }, S = (s) => {
159
- const a = l.alloc(() => {
160
- s.close();
161
- });
162
- return s.addEventListener("message", async (d) => {
163
- T(
164
- {
165
- [E]: !0,
166
- key: o,
167
- type: "message",
168
- portId: `${n}/${a}`,
169
- data: y(d.data, h())
170
- },
171
- []
172
- );
173
- }), s.addEventListener("close", () => {
174
- g.free(a), T(
175
- {
176
- [E]: !0,
177
- key: o,
178
- type: "port-closed",
179
- portId: `${n}/${a}`
180
- },
181
- []
182
- );
183
- }), `${n}/${a}`;
184
- }, h = () => {
185
- if (!i) throw new Error("Osra context was accessed before the ready message was received.");
186
- return {
187
- addIncomingProxiedMessagePort: A,
188
- addOutgoingProxiedMessagePort: S,
189
- envCheck: i,
190
- finalizationRegistry: f
191
- };
192
- };
193
- let b;
194
- const L = new Promise((s, a) => {
195
- b = s;
196
- }), U = () => {
197
- const s = new ArrayBuffer(1), { port1: a } = new MessageChannel();
198
- T(
348
+ const f = {
349
+ isJson: "isJson" in r && r.isJson !== void 0 ? r.isJson : Ae(r),
350
+ ...K(r) ? r : {
351
+ emit: r,
352
+ receive: r
353
+ }
354
+ }, d = c ?? await Ie(), g = /* @__PURE__ */ new Map();
355
+ let G;
356
+ const ne = new Promise((y) => {
357
+ G = y;
358
+ });
359
+ let b = globalThis.crypto.randomUUID();
360
+ const w = (y, U) => {
361
+ const A = u(U);
362
+ ce(
363
+ y,
199
364
  {
200
- [E]: !0,
201
- key: o,
202
- type: "ready",
203
- envCheck: { buffer: s, port: a }
365
+ [M]: o,
366
+ name: t,
367
+ uuid: b,
368
+ ...U
204
369
  },
205
- [s, a]
370
+ s,
371
+ A
206
372
  );
207
- };
208
- let C = !1;
209
- const $ = async (s) => {
210
- if (!s.data || typeof s.data != "object" || !s.data[E] || s.data.key !== o) return;
211
- const { type: a } = s.data;
212
- if (a === "ready" && !C) {
213
- C = !0, i = {
214
- uuid: n,
215
- supportsPorts: s.data.envCheck.port instanceof MessagePort,
216
- jsonOnly: s.data.envCheck.buffer instanceof ArrayBuffer
217
- }, U();
218
- const d = y(e, h()), u = M(d);
219
- T(
220
- {
221
- [E]: !0,
222
- key: o,
223
- type: "init",
224
- data: d
225
- },
226
- u
227
- );
228
- return;
229
- }
230
- if (!C || !i) throw new Error("Osra received a message before the ready message.");
231
- if (a === "init") {
232
- b(
233
- R(
234
- s.data.data,
235
- h()
236
- )
237
- );
238
- return;
239
- }
240
- if (a === "message") {
241
- const { portId: d, data: u } = s.data, I = m.get(d);
242
- if (!I) throw new Error(`Osra received a message with portId set to "${d}" but no port was found.`);
243
- I.postMessage(u);
244
- } else if (a === "port-closed") {
245
- const { portId: d } = s.data, [u, I] = d.split("/"), k = Number(I);
246
- if (!k) throw new Error(`Osra received a port-closed message with an invalid portId "${d}".`);
247
- if (u !== n && u !== i.uuid) throw new Error(`Osra received a port-closed message with an invalid portId's uuid "${d}".`);
248
- if (u === n) {
249
- const P = l.get(k);
250
- if (!P) throw new Error(`Osra received a port-closed message with an invalid portId "${d}".`);
251
- P();
252
- } else if (u === i.uuid) {
253
- const P = m.get(d);
254
- if (!P) throw new Error(`Osra received a message with portId set to "${d}" but no port was found.`);
255
- P.close(), m.delete(d);
373
+ }, se = async (y, U) => {
374
+ if (y.uuid !== b) {
375
+ if (!P(f))
376
+ throw new Error("Unidirectional receiving mode not implemented");
377
+ if (y.type === "announce") {
378
+ if (!y.remoteUuid) {
379
+ w(f, { type: "announce", remoteUuid: y.uuid });
380
+ return;
381
+ }
382
+ if (y.remoteUuid !== b) return;
383
+ if (g.has(y.uuid)) {
384
+ w(
385
+ f,
386
+ { type: "reject-uuid-taken", remoteUuid: y.uuid }
387
+ );
388
+ return;
389
+ }
390
+ const A = new ze(), X = {
391
+ type: "bidirectional",
392
+ eventTarget: A,
393
+ connection: Xe({
394
+ transport: f,
395
+ value: e,
396
+ uuid: b,
397
+ remoteUuid: y.uuid,
398
+ platformCapabilities: d,
399
+ eventTarget: A,
400
+ send: (R) => w(f, R),
401
+ close: () => void g.delete(y.uuid)
402
+ })
403
+ };
404
+ g.set(y.uuid, X), X.connection.remoteValue.then(
405
+ (R) => G(R)
406
+ );
407
+ } else if (y.type === "reject-uuid-taken") {
408
+ if (y.remoteUuid !== b) return;
409
+ b = globalThis.crypto.randomUUID(), w(f, { type: "announce" });
410
+ } else if (y.type === "close") {
411
+ if (y.remoteUuid !== b) return;
412
+ const A = g.get(y.uuid);
413
+ if (!A) {
414
+ console.warn(`Connection not found for remoteUuid: ${y.uuid}`);
415
+ return;
416
+ }
417
+ A.connection.close(), g.delete(y.uuid);
418
+ } else {
419
+ if (y.remoteUuid !== b) return;
420
+ const A = g.get(y.uuid);
421
+ if (!A) {
422
+ console.warn(`Connection not found for remoteUuid: ${y.uuid}`);
423
+ return;
424
+ }
425
+ A.type !== "unidirectional-emitting" && A.eventTarget.dispatchTypedEvent(
426
+ "message",
427
+ new CustomEvent("message", { detail: y })
428
+ );
256
429
  }
257
430
  }
258
- }, T = typeof t == "function" ? t : (s, a = []) => t.postMessage(s, { targetOrigin: c, transfer: a });
259
- return typeof r == "function" ? r($) : r.addEventListener("message", $), U(), L;
431
+ };
432
+ if (F(f) && ie({
433
+ listener: se,
434
+ transport: f,
435
+ remoteName: n,
436
+ key: o,
437
+ unregisterSignal: a
438
+ }), P(f) && w(f, { type: "announce" }), P(f) && !F(f)) {
439
+ const { remoteValueProxy: y } = $e({
440
+ value: e,
441
+ uuid: b,
442
+ platformCapabilities: d,
443
+ send: (U) => w(f, U),
444
+ close: () => g.delete(b)
445
+ });
446
+ return y;
447
+ }
448
+ return ne;
260
449
  };
261
450
  export {
262
- q as errorToProxiedError,
263
- ee as expose,
264
- Q as functionToProxiedFunction,
265
- M as getTransferableObjects,
266
- x as isClonable,
267
- B as isTransferable,
268
- v as makeAllocator,
269
- Z as makeNumberAllocator,
270
- F as messagePortToProxiedMessagePort,
271
- J as promiseToProxiedPromise,
272
- X as proxiedErrorToError,
273
- K as proxiedFunctionToFunction,
274
- W as proxiedMessagePortToMessagePort,
275
- H as proxiedPromiseToPromise,
276
- R as replaceIncomingProxiedTypes,
277
- y as replaceOutgoingProxiedTypes,
278
- O as replaceRecursive
451
+ He as expose
279
452
  };
280
453
  //# sourceMappingURL=index.js.map