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