@fkn/lib 0.4.8 → 0.5.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.
Files changed (88) hide show
  1. package/account.cjs +1 -0
  2. package/account.d.ts +17 -0
  3. package/account.js +28 -0
  4. package/api-ByfayMm1.js +67 -0
  5. package/api-CDb2pv7C.cjs +1 -0
  6. package/background-Dk_vaVP1.cjs +1 -0
  7. package/{background-CTsVv_a0.js → background-TtzKw6g0.js} +5 -4
  8. package/chunk-B6C_zzib.cjs +1 -0
  9. package/chunk-CPXzm0be.js +11 -0
  10. package/cloud/dgram.cjs +1 -0
  11. package/cloud/dgram.d.ts +537 -0
  12. package/cloud/dgram.js +4 -0
  13. package/cloud/dns.cjs +1 -0
  14. package/cloud/dns.d.ts +12 -0
  15. package/cloud/dns.js +3 -0
  16. package/cloud/fs/promises.cjs +1 -0
  17. package/cloud/fs/promises.d.ts +45 -0
  18. package/cloud/fs/promises.js +8 -0
  19. package/cloud/fs.cjs +1 -0
  20. package/cloud/fs.d.ts +75 -0
  21. package/cloud/fs.js +20 -0
  22. package/cloud/http.cjs +1 -0
  23. package/cloud/http.d.ts +767 -0
  24. package/cloud/http.js +5 -0
  25. package/cloud/net.cjs +1 -0
  26. package/cloud/net.d.ts +606 -0
  27. package/cloud/net.js +4 -0
  28. package/cloud-BVnscun4.js +33 -0
  29. package/cloud-DTL2B5pW.cjs +1 -0
  30. package/cloud.cjs +1 -0
  31. package/cloud.d.ts +1048 -0
  32. package/cloud.js +12 -0
  33. package/desktop.cjs +1 -0
  34. package/desktop.d.ts +49 -0
  35. package/desktop.js +51 -0
  36. package/dgram-5YUb70O2.js +326 -0
  37. package/dgram-Cdi6Y7zJ.cjs +1 -0
  38. package/dgram.cjs +1 -0
  39. package/dgram.d.ts +537 -0
  40. package/dgram.js +4 -0
  41. package/dns.cjs +1 -0
  42. package/dns.d.ts +12 -0
  43. package/dns.js +6 -0
  44. package/extension-9Uv_0HdS.cjs +1 -0
  45. package/extension-Dgz0UTy0.js +245 -0
  46. package/extension.cjs +1 -0
  47. package/extension.d.ts +853 -0
  48. package/extension.js +5 -0
  49. package/fs/promises.cjs +1 -0
  50. package/fs/promises.d.ts +46 -0
  51. package/fs/promises.js +8 -0
  52. package/fs-B7JumDCn.js +78 -0
  53. package/fs-DrwvwtqC.cjs +1 -0
  54. package/fs.cjs +1 -0
  55. package/fs.d.ts +84 -0
  56. package/fs.js +5 -0
  57. package/http.cjs +9 -0
  58. package/http.d.ts +767 -0
  59. package/http.js +541 -0
  60. package/index.cjs +1 -9
  61. package/index.d.ts +495 -268
  62. package/index.js +27 -1569
  63. package/locator-Cf7M3QKN.cjs +53 -0
  64. package/{locator-Q5m8i40Z.js → locator-DxAsGV_o.js} +331 -337
  65. package/net-Bl6_W3bd.js +275 -0
  66. package/net-C5kWnI4C.cjs +1 -0
  67. package/net.cjs +1 -0
  68. package/net.d.ts +606 -0
  69. package/net.js +4 -0
  70. package/node-fs-Bd_UUx1m.js +361 -0
  71. package/node-fs-lrlD_zqB.cjs +1 -0
  72. package/opfs/promises.cjs +1 -0
  73. package/opfs/promises.d.ts +46 -0
  74. package/opfs/promises.js +6 -0
  75. package/opfs-BfcYLPGb.js +55 -0
  76. package/opfs-Dsi4FkJU.cjs +1 -0
  77. package/opfs.cjs +1 -0
  78. package/opfs.d.ts +83 -0
  79. package/opfs.js +35 -0
  80. package/package.json +91 -1
  81. package/proxy-A9UvWUaF.cjs +1 -0
  82. package/proxy-wiZN4eTR.js +14 -0
  83. package/storage-BadvHtv0.cjs +1 -0
  84. package/storage-z0q3iena.js +13 -0
  85. package/utils-BsNRWMfl.cjs +1 -0
  86. package/utils-CNydj3OT.js +52 -0
  87. package/background-BqRFQggD.cjs +0 -1
  88. package/locator-Cjl21Nkt.cjs +0 -53
package/cloud.js ADDED
@@ -0,0 +1,12 @@
1
+ import "./api-ByfayMm1.js";
2
+ import { t as e } from "./proxy-wiZN4eTR.js";
3
+ import { r as t, t as n } from "./cloud-BVnscun4.js";
4
+ import { t as r } from "./dns.js";
5
+ import "./node-fs-Bd_UUx1m.js";
6
+ import "./storage-z0q3iena.js";
7
+ import { t as i } from "./cloud/fs.js";
8
+ import "./utils-CNydj3OT.js";
9
+ import { s as a } from "./net-Bl6_W3bd.js";
10
+ import { r as o } from "./dgram-5YUb70O2.js";
11
+ import { t as s } from "./http.js";
12
+ export { n as available, o as dgram, r as dns, e as fetch, i as fs, s as http, a as net, t as quota };
package/desktop.cjs ADDED
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=require(`./chunk-B6C_zzib.cjs`).n({available:()=>t,dgram:()=>o,dns:()=>i,fetch:()=>r,fs:()=>s,net:()=>a}),t=()=>!1,n=e=>(...t)=>{throw Error(`The FKN desktop app is not connected, desktop.${e} is unavailable`)},r=n(`fetch`),i={lookup:n(`dns.lookup`)},a={connect:n(`net.connect`),createConnection:n(`net.createConnection`),createServer:n(`net.createServer`)},o={createSocket:n(`dgram.createSocket`)},s={available:async()=>!1,readFile:n(`fs.readFile`),readFileSync:n(`fs.readFileSync`),writeFile:n(`fs.writeFile`),writeFileSync:n(`fs.writeFileSync`),appendFile:n(`fs.appendFile`),appendFileSync:n(`fs.appendFileSync`),existsSync:n(`fs.existsSync`),stat:n(`fs.stat`),statSync:n(`fs.statSync`),readdir:n(`fs.readdir`),readdirSync:n(`fs.readdirSync`),mkdir:n(`fs.mkdir`),mkdirSync:n(`fs.mkdirSync`),rm:n(`fs.rm`),rmSync:n(`fs.rmSync`),unlink:n(`fs.unlink`),unlinkSync:n(`fs.unlinkSync`),rename:n(`fs.rename`),renameSync:n(`fs.renameSync`),mount:n(`fs.mount`),flush:n(`fs.flush`),promises:{readFile:n(`fs.promises.readFile`),writeFile:n(`fs.promises.writeFile`),unlink:n(`fs.promises.unlink`),rename:n(`fs.promises.rename`),readdir:n(`fs.promises.readdir`),mkdir:n(`fs.promises.mkdir`),rm:n(`fs.promises.rm`),stat:n(`fs.promises.stat`)}};exports.available=t,exports.dgram=o,exports.dns=i,exports.fetch=r,exports.fs=s,exports.net=a,Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return e}});
package/desktop.d.ts ADDED
@@ -0,0 +1,49 @@
1
+ declare const available: () => boolean;
2
+ declare const fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
3
+ declare const dns: {
4
+ lookup: (..._args: unknown[]) => never;
5
+ };
6
+ declare const net: {
7
+ connect: (..._args: unknown[]) => never;
8
+ createConnection: (..._args: unknown[]) => never;
9
+ createServer: (..._args: unknown[]) => never;
10
+ };
11
+ declare const dgram: {
12
+ createSocket: (..._args: unknown[]) => never;
13
+ };
14
+ declare const fs: {
15
+ available: () => Promise<boolean>;
16
+ readFile: (..._args: unknown[]) => never;
17
+ readFileSync: (..._args: unknown[]) => never;
18
+ writeFile: (..._args: unknown[]) => never;
19
+ writeFileSync: (..._args: unknown[]) => never;
20
+ appendFile: (..._args: unknown[]) => never;
21
+ appendFileSync: (..._args: unknown[]) => never;
22
+ existsSync: (..._args: unknown[]) => never;
23
+ stat: (..._args: unknown[]) => never;
24
+ statSync: (..._args: unknown[]) => never;
25
+ readdir: (..._args: unknown[]) => never;
26
+ readdirSync: (..._args: unknown[]) => never;
27
+ mkdir: (..._args: unknown[]) => never;
28
+ mkdirSync: (..._args: unknown[]) => never;
29
+ rm: (..._args: unknown[]) => never;
30
+ rmSync: (..._args: unknown[]) => never;
31
+ unlink: (..._args: unknown[]) => never;
32
+ unlinkSync: (..._args: unknown[]) => never;
33
+ rename: (..._args: unknown[]) => never;
34
+ renameSync: (..._args: unknown[]) => never;
35
+ mount: (..._args: unknown[]) => never;
36
+ flush: (..._args: unknown[]) => never;
37
+ promises: {
38
+ readFile: (..._args: unknown[]) => never;
39
+ writeFile: (..._args: unknown[]) => never;
40
+ unlink: (..._args: unknown[]) => never;
41
+ rename: (..._args: unknown[]) => never;
42
+ readdir: (..._args: unknown[]) => never;
43
+ mkdir: (..._args: unknown[]) => never;
44
+ rm: (..._args: unknown[]) => never;
45
+ stat: (..._args: unknown[]) => never;
46
+ };
47
+ };
48
+
49
+ export { available, dgram, dns, fetch, fs, net };
package/desktop.js ADDED
@@ -0,0 +1,51 @@
1
+ import { n as e } from "./chunk-CPXzm0be.js";
2
+ //#region src/lib/desktop.ts
3
+ var t = /* @__PURE__ */ e({
4
+ available: () => n,
5
+ dgram: () => s,
6
+ dns: () => a,
7
+ fetch: () => i,
8
+ fs: () => c,
9
+ net: () => o
10
+ }), n = () => !1, r = (e) => (...t) => {
11
+ throw Error(`The FKN desktop app is not connected, desktop.${e} is unavailable`);
12
+ }, i = r("fetch"), a = { lookup: r("dns.lookup") }, o = {
13
+ connect: r("net.connect"),
14
+ createConnection: r("net.createConnection"),
15
+ createServer: r("net.createServer")
16
+ }, s = { createSocket: r("dgram.createSocket") }, c = {
17
+ available: async () => !1,
18
+ readFile: r("fs.readFile"),
19
+ readFileSync: r("fs.readFileSync"),
20
+ writeFile: r("fs.writeFile"),
21
+ writeFileSync: r("fs.writeFileSync"),
22
+ appendFile: r("fs.appendFile"),
23
+ appendFileSync: r("fs.appendFileSync"),
24
+ existsSync: r("fs.existsSync"),
25
+ stat: r("fs.stat"),
26
+ statSync: r("fs.statSync"),
27
+ readdir: r("fs.readdir"),
28
+ readdirSync: r("fs.readdirSync"),
29
+ mkdir: r("fs.mkdir"),
30
+ mkdirSync: r("fs.mkdirSync"),
31
+ rm: r("fs.rm"),
32
+ rmSync: r("fs.rmSync"),
33
+ unlink: r("fs.unlink"),
34
+ unlinkSync: r("fs.unlinkSync"),
35
+ rename: r("fs.rename"),
36
+ renameSync: r("fs.renameSync"),
37
+ mount: r("fs.mount"),
38
+ flush: r("fs.flush"),
39
+ promises: {
40
+ readFile: r("fs.promises.readFile"),
41
+ writeFile: r("fs.promises.writeFile"),
42
+ unlink: r("fs.promises.unlink"),
43
+ rename: r("fs.promises.rename"),
44
+ readdir: r("fs.promises.readdir"),
45
+ mkdir: r("fs.promises.mkdir"),
46
+ rm: r("fs.promises.rm"),
47
+ stat: r("fs.promises.stat")
48
+ }
49
+ };
50
+ //#endregion
51
+ export { n as available, s as dgram, a as dns, i as fetch, c as fs, o as net, t };
@@ -0,0 +1,326 @@
1
+ import { n as e } from "./chunk-CPXzm0be.js";
2
+ import { t } from "./api-ByfayMm1.js";
3
+ import { a as n, i as r, s as i } from "./utils-CNydj3OT.js";
4
+ import { Buffer as a } from "buffer";
5
+ import { EventEmitter as o } from "events";
6
+ import { Address4 as s, Address6 as c } from "ip-address";
7
+ //#region src/lib/webvpn/dgram.ts
8
+ var l = /* @__PURE__ */ e({
9
+ Socket: () => u,
10
+ createSocket: () => h
11
+ }), u = class extends o {
12
+ _type;
13
+ _localAddress;
14
+ _remoteAddress;
15
+ _webVPNUdpSocketPromise;
16
+ _dataPort;
17
+ _socketId;
18
+ _sendQueue = [];
19
+ _sendQueueBytes = 0;
20
+ _sendFlushScheduled = !1;
21
+ _peerTailCache = /* @__PURE__ */ new Map();
22
+ constructor(e) {
23
+ if (super(e), !e) throw Error("Missing options");
24
+ this._type = e.type;
25
+ }
26
+ _lastRecvBufferSize = 0;
27
+ _lastSendBufferSize = 0;
28
+ _lastTtl = 64;
29
+ _lastMulticastTtl = 1;
30
+ _lastMulticastLoopback = !0;
31
+ _setUdpOption(e) {
32
+ if (!this._webVPNUdpSocketPromise) {
33
+ this.emit("error", /* @__PURE__ */ Error("Cannot set socket option before bind"));
34
+ return;
35
+ }
36
+ this._webVPNUdpSocketPromise.then(async (t) => {
37
+ if (t) try {
38
+ await t.setOption(e);
39
+ } catch (e) {
40
+ this.emit("error", e);
41
+ }
42
+ });
43
+ }
44
+ _onDataPortBatch(e) {
45
+ let t = new DataView(e), n = 0;
46
+ for (; n + 4 <= e.byteLength;) {
47
+ let r = t.getUint32(n);
48
+ n += 4;
49
+ let i = n + r;
50
+ if (i > e.byteLength) break;
51
+ let o = n + 4, s = t.getUint8(o);
52
+ o += 1;
53
+ let c, l, u;
54
+ if (s === 2) {
55
+ if (!this._remoteAddress) {
56
+ n = i;
57
+ continue;
58
+ }
59
+ c = this._remoteAddress.address, l = this._remoteAddress.family, u = this._remoteAddress.port;
60
+ } else {
61
+ let n = new Uint8Array(e, o, s === 0 ? 4 : 16);
62
+ if (s === 0) l = "IPv4", c = `${n[0]}.${n[1]}.${n[2]}.${n[3]}`;
63
+ else {
64
+ l = "IPv6";
65
+ let e = [];
66
+ for (let t = 0; t < 16; t += 2) e.push(((n[t] << 8 | n[t + 1]) >>> 0).toString(16));
67
+ c = e.join(":");
68
+ }
69
+ o += n.byteLength, u = t.getUint16(o), o += 2;
70
+ }
71
+ let d = t.getUint32(o);
72
+ o += 4, this.emit("message", a.from(e.slice(o, o + d)), {
73
+ address: c,
74
+ family: l,
75
+ port: u,
76
+ size: d
77
+ }), n = i;
78
+ }
79
+ }
80
+ _peerTail(e, t) {
81
+ if (e === void 0 || t === void 0) return d;
82
+ let n = `${e}|${t}`, r = this._peerTailCache.get(n);
83
+ if (r) return r;
84
+ let i = s.isValid(e) ? p(e) : m(e), a = new Uint8Array(1 + i.byteLength + 2);
85
+ return a[0] = i.byteLength === 4 ? 0 : 1, a.set(i, 1), new DataView(a.buffer).setUint16(1 + i.byteLength, t), this._peerTailCache.set(n, a), a;
86
+ }
87
+ _portSend(e, t, n) {
88
+ let r = this._peerTail(t, n), i = new Uint8Array(8 + r.byteLength + 4 + e.byteLength), a = new DataView(i.buffer);
89
+ a.setUint32(0, i.byteLength - 4), a.setUint32(4, this._socketId), i.set(r, 8), a.setUint32(8 + r.byteLength, e.byteLength), i.set(e, 12 + r.byteLength), this._sendQueue.push(i), this._sendQueueBytes += i.byteLength, !this._sendFlushScheduled && (this._sendFlushScheduled = !0, queueMicrotask(() => {
90
+ this._sendFlushScheduled = !1;
91
+ let e = this._sendQueue, t = this._sendQueueBytes;
92
+ if (this._sendQueue = [], this._sendQueueBytes = 0, !e.length || !this._dataPort) return;
93
+ let n = new Uint8Array(t), r = 0;
94
+ for (let t of e) n.set(t, r), r += t.byteLength;
95
+ this._dataPort.postMessage(n.buffer, [n.buffer]);
96
+ }));
97
+ }
98
+ addMembership(e, t) {
99
+ if (s.isValid(e)) {
100
+ let n = p(e), r = t && s.isValid(t) ? p(t) : new Uint8Array([
101
+ 0,
102
+ 0,
103
+ 0,
104
+ 0
105
+ ]);
106
+ this._setUdpOption({
107
+ type: 5,
108
+ group: n,
109
+ interface: r
110
+ });
111
+ } else if (c.isValid(e)) {
112
+ let t = m(e);
113
+ this._setUdpOption({
114
+ type: 7,
115
+ group: t,
116
+ ifIndex: 0
117
+ });
118
+ } else this.emit("error", /* @__PURE__ */ Error(`Invalid multicast address: ${e}`));
119
+ }
120
+ address() {
121
+ if (!this._localAddress) throw Error("EBADF");
122
+ return this._localAddress;
123
+ }
124
+ bind(...e) {
125
+ let n = r(...e), i = this._type === "udp6" ? "::" : "0.0.0.0";
126
+ return this._webVPNUdpSocketPromise = t.then((e) => e.cloud.webvpn.udpSocket({
127
+ type: this._type,
128
+ address: n.address ?? i,
129
+ port: n.port,
130
+ dataPort: !0
131
+ })).then((e) => {
132
+ if (e.dataPort) this._socketId = e.socketId, this._dataPort = e.dataPort, this._dataPort.onmessage = (e) => this._onDataPortBatch(e.data);
133
+ else {
134
+ let t = e.dataReadableStream.getReader(), n = async () => {
135
+ let { value: e, done: r } = await t.read();
136
+ r || (this.emit("message", a.from(e.data), {
137
+ address: e.address,
138
+ family: e.family,
139
+ port: e.port,
140
+ size: e.size
141
+ }), n());
142
+ };
143
+ n();
144
+ }
145
+ return this._localAddress = {
146
+ address: e.localAddress,
147
+ family: e.localFamily,
148
+ port: e.localPort
149
+ }, e;
150
+ }).catch((e) => {
151
+ this.emit("error", e);
152
+ }), this._webVPNUdpSocketPromise.then((e) => {
153
+ e && this.emit("listening");
154
+ }), n.callback && this._webVPNUdpSocketPromise.then((e) => {
155
+ e && n.callback.call(this);
156
+ }), this;
157
+ }
158
+ close(e) {
159
+ if (!this._webVPNUdpSocketPromise) throw Error("Socket not bound");
160
+ return this._webVPNUdpSocketPromise.then(async (t) => {
161
+ if (!t) throw Error("Socket not bound");
162
+ this._dataPort?.close(), this._dataPort = void 0, await t.close(), e && e(), this.emit("close");
163
+ }), this;
164
+ }
165
+ connect(...e) {
166
+ if (!this._webVPNUdpSocketPromise) throw Error("Socket not bound");
167
+ this._webVPNUdpSocketPromise.then(async (t) => {
168
+ if (!t) throw Error("Socket not bound");
169
+ let n = e[0], r = this._type === "udp6" ? "::1" : "127.0.0.1", i = typeof e[1] == "string" ? e[1] : r, a = typeof e.at(-1) == "function" ? e.at(-1) : void 0;
170
+ await t.connect({
171
+ remotePort: n,
172
+ remoteAddress: i
173
+ }), this._remoteAddress = {
174
+ address: i,
175
+ family: this._type === "udp6" ? "IPv6" : "IPv4",
176
+ port: n
177
+ }, a && a();
178
+ });
179
+ }
180
+ disconnect() {
181
+ if (!this._webVPNUdpSocketPromise) throw Error("Socket not bound");
182
+ this._webVPNUdpSocketPromise.then(async (e) => {
183
+ if (!e) throw Error("Socket not bound");
184
+ await e.connect({
185
+ remotePort: 0,
186
+ remoteAddress: this._type === "udp4" ? "0.0.0.0" : "::"
187
+ });
188
+ });
189
+ }
190
+ dropMembership(e, t) {
191
+ if (s.isValid(e)) {
192
+ let n = p(e), r = t && s.isValid(t) ? p(t) : new Uint8Array([
193
+ 0,
194
+ 0,
195
+ 0,
196
+ 0
197
+ ]);
198
+ this._setUdpOption({
199
+ type: 6,
200
+ group: n,
201
+ interface: r
202
+ });
203
+ } else if (c.isValid(e)) {
204
+ let t = m(e);
205
+ this._setUdpOption({
206
+ type: 8,
207
+ group: t,
208
+ ifIndex: 0
209
+ });
210
+ } else this.emit("error", /* @__PURE__ */ Error(`Invalid multicast address: ${e}`));
211
+ }
212
+ getRecvBufferSize() {
213
+ return this._lastRecvBufferSize;
214
+ }
215
+ getSendBufferSize() {
216
+ return this._lastSendBufferSize;
217
+ }
218
+ getSendQueueSize() {
219
+ return 0;
220
+ }
221
+ getSendQueueCount() {
222
+ return 0;
223
+ }
224
+ ref() {
225
+ return this;
226
+ }
227
+ remoteAddress() {
228
+ if (!this._remoteAddress) throw Error("ERR_SOCKET_DGRAM_NOT_CONNECTED");
229
+ return {
230
+ address: this._remoteAddress.address,
231
+ family: this._remoteAddress.family,
232
+ port: this._remoteAddress.port
233
+ };
234
+ }
235
+ send(...e) {
236
+ this._webVPNUdpSocketPromise || this.bind();
237
+ let t = n(...e), r = this._type === "udp6" ? "::1" : "127.0.0.1";
238
+ if (!this._webVPNUdpSocketPromise) throw Error("Socket not bound");
239
+ this._webVPNUdpSocketPromise.then((e) => {
240
+ if (!e) throw Error("Socket not bound");
241
+ let n = f(t.message), i = t.callback, a = t.address ?? r, o = this._remoteAddress !== void 0 && t.address === void 0;
242
+ if (this._dataPort && (o || s.isValid(a) || c.isValid(a))) {
243
+ this._portSend(n, o ? void 0 : a, o ? void 0 : t.port), i?.call(this, null, n.byteLength);
244
+ return;
245
+ }
246
+ e.send({
247
+ ...t,
248
+ address: a,
249
+ message: n.byteOffset === 0 && n.byteLength === n.buffer.byteLength ? n.buffer : n.buffer.slice(n.byteOffset, n.byteOffset + n.byteLength)
250
+ }).then(() => i?.call(this, null, n.byteLength), (e) => i ? i.call(this, e, 0) : this.emit("error", e));
251
+ });
252
+ }
253
+ setBroadcast(e) {
254
+ this._setUdpOption({
255
+ type: 0,
256
+ value: e
257
+ });
258
+ }
259
+ setMulticastInterface(e) {}
260
+ setMulticastLoopback(e) {
261
+ return this._setUdpOption({
262
+ type: 3,
263
+ value: e
264
+ }), this._setUdpOption({
265
+ type: 4,
266
+ value: e
267
+ }), this._lastMulticastLoopback = e, e;
268
+ }
269
+ setMulticastTTL(e) {
270
+ return this._setUdpOption({
271
+ type: 2,
272
+ value: e
273
+ }), this._lastMulticastTtl = e, e;
274
+ }
275
+ setRecvBufferSize(e) {
276
+ this._setUdpOption({
277
+ type: 10,
278
+ value: e
279
+ }), this._lastRecvBufferSize = e;
280
+ }
281
+ setSendBufferSize(e) {
282
+ this._setUdpOption({
283
+ type: 9,
284
+ value: e
285
+ }), this._lastSendBufferSize = e;
286
+ }
287
+ setTTL(e) {
288
+ return this._setUdpOption({
289
+ type: 1,
290
+ value: e
291
+ }), this._lastTtl = e, e;
292
+ }
293
+ unref() {
294
+ return this;
295
+ }
296
+ addSourceSpecificMembership(e, t, n) {}
297
+ dropSourceSpecificMembership(e, t, n) {}
298
+ async [Symbol.asyncDispose]() {
299
+ await new Promise((e) => {
300
+ this.close(e);
301
+ });
302
+ }
303
+ }, d = new Uint8Array([2]), f = (e) => {
304
+ if (typeof e == "string") return i.encode(e);
305
+ if (e instanceof Uint8Array) return e;
306
+ if (ArrayBuffer.isView(e)) return new Uint8Array(e.buffer, e.byteOffset, e.byteLength);
307
+ if (Array.isArray(e)) {
308
+ let t = e.map(f), n = new Uint8Array(t.reduce((e, t) => e + t.byteLength, 0)), r = 0;
309
+ for (let e of t) n.set(e, r), r += e.byteLength;
310
+ return n;
311
+ }
312
+ return new Uint8Array(e);
313
+ }, p = (e) => {
314
+ let t = e.split(".").map((e) => parseInt(e, 10));
315
+ if (t.length !== 4 || t.some((e) => Number.isNaN(e) || e < 0 || e > 255)) throw Error(`Invalid IPv4 address: ${e}`);
316
+ return new Uint8Array(t);
317
+ }, m = (e) => {
318
+ let t = new c(e).getBitsBase16(0, 128).match(/.{2}/g);
319
+ if (!t || t.length !== 16) throw Error(`Invalid IPv6 address: ${e}`);
320
+ return new Uint8Array(t.map((e) => parseInt(e, 16)));
321
+ }, h = (e, t) => {
322
+ let n = new u(typeof e == "string" ? { type: e } : e);
323
+ return t && n.on("message", t), n;
324
+ };
325
+ //#endregion
326
+ export { h as n, l as r, u as t };
@@ -0,0 +1 @@
1
+ const e=require(`./chunk-B6C_zzib.cjs`),t=require(`./api-CDb2pv7C.cjs`),n=require(`./utils-BsNRWMfl.cjs`);let r=require(`buffer`),i=require(`events`),a=require(`ip-address`);var o=e.n({Socket:()=>s,createSocket:()=>f}),s=class extends i.EventEmitter{_type;_localAddress;_remoteAddress;_webVPNUdpSocketPromise;_dataPort;_socketId;_sendQueue=[];_sendQueueBytes=0;_sendFlushScheduled=!1;_peerTailCache=new Map;constructor(e){if(super(e),!e)throw Error(`Missing options`);this._type=e.type}_lastRecvBufferSize=0;_lastSendBufferSize=0;_lastTtl=64;_lastMulticastTtl=1;_lastMulticastLoopback=!0;_setUdpOption(e){if(!this._webVPNUdpSocketPromise){this.emit(`error`,Error(`Cannot set socket option before bind`));return}this._webVPNUdpSocketPromise.then(async t=>{if(t)try{await t.setOption(e)}catch(e){this.emit(`error`,e)}})}_onDataPortBatch(e){let t=new DataView(e),n=0;for(;n+4<=e.byteLength;){let i=t.getUint32(n);n+=4;let a=n+i;if(a>e.byteLength)break;let o=n+4,s=t.getUint8(o);o+=1;let c,l,u;if(s===2){if(!this._remoteAddress){n=a;continue}c=this._remoteAddress.address,l=this._remoteAddress.family,u=this._remoteAddress.port}else{let n=new Uint8Array(e,o,s===0?4:16);if(s===0)l=`IPv4`,c=`${n[0]}.${n[1]}.${n[2]}.${n[3]}`;else{l=`IPv6`;let e=[];for(let t=0;t<16;t+=2)e.push(((n[t]<<8|n[t+1])>>>0).toString(16));c=e.join(`:`)}o+=n.byteLength,u=t.getUint16(o),o+=2}let d=t.getUint32(o);o+=4,this.emit(`message`,r.Buffer.from(e.slice(o,o+d)),{address:c,family:l,port:u,size:d}),n=a}}_peerTail(e,t){if(e===void 0||t===void 0)return c;let n=`${e}|${t}`,r=this._peerTailCache.get(n);if(r)return r;let i=a.Address4.isValid(e)?u(e):d(e),o=new Uint8Array(1+i.byteLength+2);return o[0]=i.byteLength===4?0:1,o.set(i,1),new DataView(o.buffer).setUint16(1+i.byteLength,t),this._peerTailCache.set(n,o),o}_portSend(e,t,n){let r=this._peerTail(t,n),i=new Uint8Array(8+r.byteLength+4+e.byteLength),a=new DataView(i.buffer);a.setUint32(0,i.byteLength-4),a.setUint32(4,this._socketId),i.set(r,8),a.setUint32(8+r.byteLength,e.byteLength),i.set(e,12+r.byteLength),this._sendQueue.push(i),this._sendQueueBytes+=i.byteLength,!this._sendFlushScheduled&&(this._sendFlushScheduled=!0,queueMicrotask(()=>{this._sendFlushScheduled=!1;let e=this._sendQueue,t=this._sendQueueBytes;if(this._sendQueue=[],this._sendQueueBytes=0,!e.length||!this._dataPort)return;let n=new Uint8Array(t),r=0;for(let t of e)n.set(t,r),r+=t.byteLength;this._dataPort.postMessage(n.buffer,[n.buffer])}))}addMembership(e,t){if(a.Address4.isValid(e)){let n=u(e),r=t&&a.Address4.isValid(t)?u(t):new Uint8Array([0,0,0,0]);this._setUdpOption({type:5,group:n,interface:r})}else if(a.Address6.isValid(e)){let t=d(e);this._setUdpOption({type:7,group:t,ifIndex:0})}else this.emit(`error`,Error(`Invalid multicast address: ${e}`))}address(){if(!this._localAddress)throw Error(`EBADF`);return this._localAddress}bind(...e){let i=n.i(...e),a=this._type===`udp6`?`::`:`0.0.0.0`;return this._webVPNUdpSocketPromise=t.t.then(e=>e.cloud.webvpn.udpSocket({type:this._type,address:i.address??a,port:i.port,dataPort:!0})).then(e=>{if(e.dataPort)this._socketId=e.socketId,this._dataPort=e.dataPort,this._dataPort.onmessage=e=>this._onDataPortBatch(e.data);else{let t=e.dataReadableStream.getReader(),n=async()=>{let{value:e,done:i}=await t.read();i||(this.emit(`message`,r.Buffer.from(e.data),{address:e.address,family:e.family,port:e.port,size:e.size}),n())};n()}return this._localAddress={address:e.localAddress,family:e.localFamily,port:e.localPort},e}).catch(e=>{this.emit(`error`,e)}),this._webVPNUdpSocketPromise.then(e=>{e&&this.emit(`listening`)}),i.callback&&this._webVPNUdpSocketPromise.then(e=>{e&&i.callback.call(this)}),this}close(e){if(!this._webVPNUdpSocketPromise)throw Error(`Socket not bound`);return this._webVPNUdpSocketPromise.then(async t=>{if(!t)throw Error(`Socket not bound`);this._dataPort?.close(),this._dataPort=void 0,await t.close(),e&&e(),this.emit(`close`)}),this}connect(...e){if(!this._webVPNUdpSocketPromise)throw Error(`Socket not bound`);this._webVPNUdpSocketPromise.then(async t=>{if(!t)throw Error(`Socket not bound`);let n=e[0],r=this._type===`udp6`?`::1`:`127.0.0.1`,i=typeof e[1]==`string`?e[1]:r,a=typeof e.at(-1)==`function`?e.at(-1):void 0;await t.connect({remotePort:n,remoteAddress:i}),this._remoteAddress={address:i,family:this._type===`udp6`?`IPv6`:`IPv4`,port:n},a&&a()})}disconnect(){if(!this._webVPNUdpSocketPromise)throw Error(`Socket not bound`);this._webVPNUdpSocketPromise.then(async e=>{if(!e)throw Error(`Socket not bound`);await e.connect({remotePort:0,remoteAddress:this._type===`udp4`?`0.0.0.0`:`::`})})}dropMembership(e,t){if(a.Address4.isValid(e)){let n=u(e),r=t&&a.Address4.isValid(t)?u(t):new Uint8Array([0,0,0,0]);this._setUdpOption({type:6,group:n,interface:r})}else if(a.Address6.isValid(e)){let t=d(e);this._setUdpOption({type:8,group:t,ifIndex:0})}else this.emit(`error`,Error(`Invalid multicast address: ${e}`))}getRecvBufferSize(){return this._lastRecvBufferSize}getSendBufferSize(){return this._lastSendBufferSize}getSendQueueSize(){return 0}getSendQueueCount(){return 0}ref(){return this}remoteAddress(){if(!this._remoteAddress)throw Error(`ERR_SOCKET_DGRAM_NOT_CONNECTED`);return{address:this._remoteAddress.address,family:this._remoteAddress.family,port:this._remoteAddress.port}}send(...e){this._webVPNUdpSocketPromise||this.bind();let t=n.a(...e),r=this._type===`udp6`?`::1`:`127.0.0.1`;if(!this._webVPNUdpSocketPromise)throw Error(`Socket not bound`);this._webVPNUdpSocketPromise.then(e=>{if(!e)throw Error(`Socket not bound`);let n=l(t.message),i=t.callback,o=t.address??r,s=this._remoteAddress!==void 0&&t.address===void 0;if(this._dataPort&&(s||a.Address4.isValid(o)||a.Address6.isValid(o))){this._portSend(n,s?void 0:o,s?void 0:t.port),i?.call(this,null,n.byteLength);return}e.send({...t,address:o,message:n.byteOffset===0&&n.byteLength===n.buffer.byteLength?n.buffer:n.buffer.slice(n.byteOffset,n.byteOffset+n.byteLength)}).then(()=>i?.call(this,null,n.byteLength),e=>i?i.call(this,e,0):this.emit(`error`,e))})}setBroadcast(e){this._setUdpOption({type:0,value:e})}setMulticastInterface(e){}setMulticastLoopback(e){return this._setUdpOption({type:3,value:e}),this._setUdpOption({type:4,value:e}),this._lastMulticastLoopback=e,e}setMulticastTTL(e){return this._setUdpOption({type:2,value:e}),this._lastMulticastTtl=e,e}setRecvBufferSize(e){this._setUdpOption({type:10,value:e}),this._lastRecvBufferSize=e}setSendBufferSize(e){this._setUdpOption({type:9,value:e}),this._lastSendBufferSize=e}setTTL(e){return this._setUdpOption({type:1,value:e}),this._lastTtl=e,e}unref(){return this}addSourceSpecificMembership(e,t,n){}dropSourceSpecificMembership(e,t,n){}async[Symbol.asyncDispose](){await new Promise(e=>{this.close(e)})}},c=new Uint8Array([2]),l=e=>{if(typeof e==`string`)return n.s.encode(e);if(e instanceof Uint8Array)return e;if(ArrayBuffer.isView(e))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);if(Array.isArray(e)){let t=e.map(l),n=new Uint8Array(t.reduce((e,t)=>e+t.byteLength,0)),r=0;for(let e of t)n.set(e,r),r+=e.byteLength;return n}return new Uint8Array(e)},u=e=>{let t=e.split(`.`).map(e=>parseInt(e,10));if(t.length!==4||t.some(e=>Number.isNaN(e)||e<0||e>255))throw Error(`Invalid IPv4 address: ${e}`);return new Uint8Array(t)},d=e=>{let t=new a.Address6(e).getBitsBase16(0,128).match(/.{2}/g);if(!t||t.length!==16)throw Error(`Invalid IPv6 address: ${e}`);return new Uint8Array(t.map(e=>parseInt(e,16)))},f=(e,t)=>{let n=new s(typeof e==`string`?{type:e}:e);return t&&n.on(`message`,t),n};Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return f}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return o}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return s}});
package/dgram.cjs ADDED
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),require(`./api-CDb2pv7C.cjs`),require(`./utils-BsNRWMfl.cjs`);const e=require(`./dgram-Cdi6Y7zJ.cjs`);exports.Socket=e.t,exports.createSocket=e.n;