@gjsify/dgram 0.3.20 → 0.3.21

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 (2) hide show
  1. package/lib/esm/index.js +1 -1
  2. package/package.json +6 -6
package/lib/esm/index.js CHANGED
@@ -1 +1 @@
1
- import e from"@girs/gio-2.0";import t from"@girs/glib-2.0";import{EventEmitter as n}from"node:events";import{Buffer as r}from"node:buffer";import{deferEmit as i,ensureMainLoop as a}from"@gjsify/utils";const o=new Set;var s=class extends n{type;_socket=null;_bound=!1;_closed=!1;_receiving=!1;_address={address:`0.0.0.0`,family:`IPv4`,port:0};_cancellable=new e.Cancellable;_readSource=null;_reuseAddr;_connected=!1;_remoteAddress=null;constructor(t){super(),typeof t==`string`?(this.type=t,this._reuseAddr=!1):(this.type=t.type,this._reuseAddr=t.reuseAddr??!1);let n=this.type===`udp6`?e.SocketFamily.IPV6:e.SocketFamily.IPV4;try{this._socket=e.Socket.new(n,e.SocketType.DATAGRAM,e.SocketProtocol.UDP),this._socket.set_blocking(!1)}catch(e){this._socket=null,i(this,`error`,e)}}bind(t,n,r){if(this._closed||!this._socket)return this;let s=0,c=this.type===`udp6`?`::`:`0.0.0.0`;if(typeof t==`object`){let e=t;s=e.port||0,c=e.address||c,typeof n==`function`&&(r=n)}else typeof t==`number`?(s=t,typeof n==`string`?c=n:typeof n==`function`&&(r=n)):typeof t==`function`&&(r=t);r&&this.once(`listening`,r);try{let t=this.type===`udp6`?e.SocketFamily.IPV6:e.SocketFamily.IPV4,n=e.InetAddress.new_from_string(c)||(t===e.SocketFamily.IPV6?e.InetAddress.new_any(e.SocketFamily.IPV6):e.InetAddress.new_any(e.SocketFamily.IPV4)),r=new e.InetSocketAddress({address:n,port:s});this._socket.bind(r,this._reuseAddr),this._bound=!0,o.add(this),a();let i=this._socket.get_local_address();i&&(this._address={address:i.get_address().to_string(),family:this.type===`udp6`?`IPv6`:`IPv4`,port:i.get_port()}),setTimeout(()=>{this.emit(`listening`),this._startReceiving()},0)}catch(e){i(this,`error`,e)}return this}send(e,t,n,r,i,a){if(this._closed||!this._socket)return;let o,s,c,l;typeof t==`function`?(l=t,o=this._toBuffer(e),s=this._address.port,c=this._address.address):typeof t==`number`&&typeof n==`string`?(s=t,c=n,l=r,o=this._toBuffer(e)):typeof t==`number`&&typeof n==`number`&&typeof i==`function`?(l=i,s=r,c=this.type===`udp6`?`::1`:`127.0.0.1`,o=this._toBufferSlice(e,t,n)):typeof t==`number`&&typeof n==`number`?(s=r,c=i||(this.type===`udp6`?`::1`:`127.0.0.1`),l=a,o=this._toBufferSlice(e,t,n)):(s=Number(t)||0,c=this.type===`udp6`?`::1`:`127.0.0.1`,o=this._toBuffer(e)),this._resolveAndSend(c,s,o,l)}_autoBind(){if(this._bound||!this._socket)return;let t=this.type===`udp6`?e.InetAddress.new_any(e.SocketFamily.IPV6):e.InetAddress.new_any(e.SocketFamily.IPV4),n=new e.InetSocketAddress({address:t,port:0});this._socket.bind(n,!1),this._bound=!0,o.add(this),a()}_socketFamily(){return this.type===`udp6`?e.SocketFamily.IPV6:e.SocketFamily.IPV4}_isAddressCompatible(e){return e.get_family()===this._socketFamily()}_mismatchError(e){let t=this.type===`udp6`?`IPv6`:`IPv4`,n=Error(`EINVAL: cannot send to ${e} from a ${t} socket (address-family mismatch)`);return n.code=`EINVAL`,n.syscall=`send`,n}_resolveAndSend(t,n,r,i){if(this._closed||!this._socket){i&&i(Error(`Socket is closed`),0);return}let a=e.InetAddress.new_from_string(t),o=a=>{if(!this._isAddressCompatible(a)){let e=this._mismatchError(t);i?i(e,0):this.emit(`error`,e);return}try{this._autoBind();let t=new e.InetSocketAddress({address:a,port:n}),o=this._socket.send_to(t,r,this._cancellable);i&&i(null,o)}catch(e){i?i(e instanceof Error?e:Error(String(e)),0):this.emit(`error`,e)}};if(a){o(a);return}let s=e.Resolver.get_default();s.lookup_by_name_async(t,this._cancellable,(e,n)=>{try{let e=s.lookup_by_name_finish(n);if(!e||e.length===0){let e=Error(`ENOTFOUND ${t}`);e.code=`ENOTFOUND`,i?i(e,0):this.emit(`error`,e);return}let r=e.find(e=>this._isAddressCompatible(e));if(!r){let e=this._mismatchError(t);i?i(e,0):this.emit(`error`,e);return}o(r)}catch(e){i?i(e instanceof Error?e:Error(String(e)),0):this.emit(`error`,e)}})}_toBuffer(e){return Array.isArray(e)?r.concat(e.map(e=>r.from(e))):r.from(e)}_toBufferSlice(e,t,n){let i=this._toBuffer(e);return r.from(i.buffer,i.byteOffset+t,n)}close(e){if(this._closed)throw Error(`Not running`);if(this._closed=!0,o.delete(this),e&&this.once(`close`,e),this._readSource){try{this._readSource.destroy()}catch{}this._readSource=null}if(this._cancellable.cancel(),this._socket){try{this._socket.close()}catch{}this._socket=null}return i(this,`close`),this}connect(e,t,n){if(this._connected){let e=Error(`Already connected`);throw e.code=`ERR_SOCKET_DGRAM_IS_CONNECTED`,e}if(!e||e<=0||e>=65536){let t=RangeError(`Port should be > 0 and < 65536. Received ${e}.`);throw t.code=`ERR_SOCKET_BAD_PORT`,t}let r,i;typeof t==`function`?(i=t,r=this.type===`udp6`?`::1`:`127.0.0.1`):(r=t||(this.type===`udp6`?`::1`:`127.0.0.1`),i=n),this._connected=!0,this._remoteAddress={address:r,family:this.type===`udp6`?`IPv6`:`IPv4`,port:e},i?Promise.resolve().then(()=>{this.emit(`connect`),i()}):Promise.resolve().then(()=>this.emit(`connect`))}disconnect(){if(!this._connected){let e=Error(`Not connected`);throw e.code=`ERR_SOCKET_DGRAM_NOT_CONNECTED`,e}this._connected=!1,this._remoteAddress=null}remoteAddress(){if(!this._connected||!this._remoteAddress){let e=Error(`Not connected`);throw e.code=`ERR_SOCKET_DGRAM_NOT_CONNECTED`,e}return{...this._remoteAddress}}address(){return{...this._address}}setBroadcast(e){this._socket&&this._socket.set_broadcast(e)}setTTL(e){return this._socket&&this._socket.set_ttl(e),e}setMulticastTTL(e){return this._socket&&this._socket.set_multicast_ttl(e),e}setMulticastLoopback(e){return this._socket&&this._socket.set_multicast_loopback(e),e}addMembership(t,n){if(this._socket)try{let r=e.InetAddress.new_from_string(t);this._socket.join_multicast_group(r,!1,n||null)}catch(e){this.emit(`error`,e)}}dropMembership(t,n){if(this._socket)try{let r=e.InetAddress.new_from_string(t);this._socket.leave_multicast_group(r,!1,n||null)}catch(e){this.emit(`error`,e)}}setMulticastInterface(e){}ref(){return this}unref(){return this}getRecvBufferSize(){return 65536}setRecvBufferSize(e){}getSendBufferSize(){return 65536}setSendBufferSize(e){}_startReceiving(){this._receiving||this._closed||!this._socket||(this._receiving=!0,this._receiveLoop())}_receiveLoop(){if(this._closed||!this._socket||this._readSource)return;let n=this._socket.create_source(t.IOCondition.IN,this._cancellable);this._readSource=n,n.set_callback(()=>{if(this._closed||!this._socket)return t.SOURCE_REMOVE;try{let[e,t]=this._socket.receive_bytes_from(65536,0,this._cancellable);if(e&&t){let n=r.from(e.get_data()),i=t,a={address:i.get_address().to_string(),family:this.type===`udp6`?`IPv6`:`IPv4`,port:i.get_port()};n.length>0&&this.emit(`message`,n,a)}}catch(t){let n=t;!this._closed&&n.code!==e.IOErrorEnum.CANCELLED&&this.emit(`error`,t)}return t.SOURCE_CONTINUE}),n.attach(null)}};function c(e,t){let n=new s(typeof e==`string`?{type:e}:e);return t&&n.on(`message`,t),n}var l={Socket:s,createSocket:c};export{s as Socket,c as createSocket,l as default};
1
+ import e from"@girs/gio-2.0";import t from"@girs/glib-2.0";import{EventEmitter as n}from"node:events";import{Buffer as r}from"node:buffer";import{deferEmit as i,ensureMainLoop as a}from"@gjsify/utils";const o=new Set;var Socket=class extends n{type;_socket=null;_bound=!1;_closed=!1;_receiving=!1;_address={address:`0.0.0.0`,family:`IPv4`,port:0};_cancellable=new e.Cancellable;_readSource=null;_reuseAddr;_connected=!1;_remoteAddress=null;constructor(t){super(),typeof t==`string`?(this.type=t,this._reuseAddr=!1):(this.type=t.type,this._reuseAddr=t.reuseAddr??!1);let n=this.type===`udp6`?e.SocketFamily.IPV6:e.SocketFamily.IPV4;try{this._socket=e.Socket.new(n,e.SocketType.DATAGRAM,e.SocketProtocol.UDP),this._socket.set_blocking(!1)}catch(e){this._socket=null,i(this,`error`,e)}}bind(t,n,r){if(this._closed||!this._socket)return this;let s=0,c=this.type===`udp6`?`::`:`0.0.0.0`;if(typeof t==`object`){let e=t;s=e.port||0,c=e.address||c,typeof n==`function`&&(r=n)}else typeof t==`number`?(s=t,typeof n==`string`?c=n:typeof n==`function`&&(r=n)):typeof t==`function`&&(r=t);r&&this.once(`listening`,r);try{let t=this.type===`udp6`?e.SocketFamily.IPV6:e.SocketFamily.IPV4,n=e.InetAddress.new_from_string(c)||(t===e.SocketFamily.IPV6?e.InetAddress.new_any(e.SocketFamily.IPV6):e.InetAddress.new_any(e.SocketFamily.IPV4)),r=new e.InetSocketAddress({address:n,port:s});this._socket.bind(r,this._reuseAddr),this._bound=!0,o.add(this),a();let i=this._socket.get_local_address();i&&(this._address={address:i.get_address().to_string(),family:this.type===`udp6`?`IPv6`:`IPv4`,port:i.get_port()}),setTimeout(()=>{this.emit(`listening`),this._startReceiving()},0)}catch(e){i(this,`error`,e)}return this}send(e,t,n,r,i,a){if(this._closed||!this._socket)return;let o,s,c,l;typeof t==`function`?(l=t,o=this._toBuffer(e),s=this._address.port,c=this._address.address):typeof t==`number`&&typeof n==`string`?(s=t,c=n,l=r,o=this._toBuffer(e)):typeof t==`number`&&typeof n==`number`&&typeof i==`function`?(l=i,s=r,c=this.type===`udp6`?`::1`:`127.0.0.1`,o=this._toBufferSlice(e,t,n)):typeof t==`number`&&typeof n==`number`?(s=r,c=i||(this.type===`udp6`?`::1`:`127.0.0.1`),l=a,o=this._toBufferSlice(e,t,n)):(s=Number(t)||0,c=this.type===`udp6`?`::1`:`127.0.0.1`,o=this._toBuffer(e)),this._resolveAndSend(c,s,o,l)}_autoBind(){if(this._bound||!this._socket)return;let t=this.type===`udp6`?e.InetAddress.new_any(e.SocketFamily.IPV6):e.InetAddress.new_any(e.SocketFamily.IPV4),n=new e.InetSocketAddress({address:t,port:0});this._socket.bind(n,!1),this._bound=!0,o.add(this),a()}_socketFamily(){return this.type===`udp6`?e.SocketFamily.IPV6:e.SocketFamily.IPV4}_isAddressCompatible(e){return e.get_family()===this._socketFamily()}_mismatchError(e){let t=this.type===`udp6`?`IPv6`:`IPv4`,n=Error(`EINVAL: cannot send to ${e} from a ${t} socket (address-family mismatch)`);return n.code=`EINVAL`,n.syscall=`send`,n}_resolveAndSend(t,n,r,i){if(this._closed||!this._socket){i&&i(Error(`Socket is closed`),0);return}let a=e.InetAddress.new_from_string(t),doSend=a=>{if(!this._isAddressCompatible(a)){let e=this._mismatchError(t);i?i(e,0):this.emit(`error`,e);return}try{this._autoBind();let t=new e.InetSocketAddress({address:a,port:n}),o=this._socket.send_to(t,r,this._cancellable);i&&i(null,o)}catch(e){i?i(e instanceof Error?e:Error(String(e)),0):this.emit(`error`,e)}};if(a){doSend(a);return}let o=e.Resolver.get_default();o.lookup_by_name_async(t,this._cancellable,(e,n)=>{try{let e=o.lookup_by_name_finish(n);if(!e||e.length===0){let e=Error(`ENOTFOUND ${t}`);e.code=`ENOTFOUND`,i?i(e,0):this.emit(`error`,e);return}let r=e.find(e=>this._isAddressCompatible(e));if(!r){let e=this._mismatchError(t);i?i(e,0):this.emit(`error`,e);return}doSend(r)}catch(e){i?i(e instanceof Error?e:Error(String(e)),0):this.emit(`error`,e)}})}_toBuffer(e){return Array.isArray(e)?r.concat(e.map(e=>r.from(e))):r.from(e)}_toBufferSlice(e,t,n){let i=this._toBuffer(e);return r.from(i.buffer,i.byteOffset+t,n)}close(e){if(this._closed)throw Error(`Not running`);if(this._closed=!0,o.delete(this),e&&this.once(`close`,e),this._readSource){try{this._readSource.destroy()}catch{}this._readSource=null}if(this._cancellable.cancel(),this._socket){try{this._socket.close()}catch{}this._socket=null}return i(this,`close`),this}connect(e,t,n){if(this._connected){let e=Error(`Already connected`);throw e.code=`ERR_SOCKET_DGRAM_IS_CONNECTED`,e}if(!e||e<=0||e>=65536){let t=RangeError(`Port should be > 0 and < 65536. Received ${e}.`);throw t.code=`ERR_SOCKET_BAD_PORT`,t}let r,i;typeof t==`function`?(i=t,r=this.type===`udp6`?`::1`:`127.0.0.1`):(r=t||(this.type===`udp6`?`::1`:`127.0.0.1`),i=n),this._connected=!0,this._remoteAddress={address:r,family:this.type===`udp6`?`IPv6`:`IPv4`,port:e},i?Promise.resolve().then(()=>{this.emit(`connect`),i()}):Promise.resolve().then(()=>this.emit(`connect`))}disconnect(){if(!this._connected){let e=Error(`Not connected`);throw e.code=`ERR_SOCKET_DGRAM_NOT_CONNECTED`,e}this._connected=!1,this._remoteAddress=null}remoteAddress(){if(!this._connected||!this._remoteAddress){let e=Error(`Not connected`);throw e.code=`ERR_SOCKET_DGRAM_NOT_CONNECTED`,e}return{...this._remoteAddress}}address(){return{...this._address}}setBroadcast(e){this._socket&&this._socket.set_broadcast(e)}setTTL(e){return this._socket&&this._socket.set_ttl(e),e}setMulticastTTL(e){return this._socket&&this._socket.set_multicast_ttl(e),e}setMulticastLoopback(e){return this._socket&&this._socket.set_multicast_loopback(e),e}addMembership(t,n){if(this._socket)try{let r=e.InetAddress.new_from_string(t);this._socket.join_multicast_group(r,!1,n||null)}catch(e){this.emit(`error`,e)}}dropMembership(t,n){if(this._socket)try{let r=e.InetAddress.new_from_string(t);this._socket.leave_multicast_group(r,!1,n||null)}catch(e){this.emit(`error`,e)}}setMulticastInterface(e){}ref(){return this}unref(){return this}getRecvBufferSize(){return 65536}setRecvBufferSize(e){}getSendBufferSize(){return 65536}setSendBufferSize(e){}_startReceiving(){this._receiving||this._closed||!this._socket||(this._receiving=!0,this._receiveLoop())}_receiveLoop(){if(this._closed||!this._socket||this._readSource)return;let n=this._socket.create_source(t.IOCondition.IN,this._cancellable);this._readSource=n,n.set_callback(()=>{if(this._closed||!this._socket)return t.SOURCE_REMOVE;try{let[e,t]=this._socket.receive_bytes_from(65536,0,this._cancellable);if(e&&t){let n=r.from(e.get_data()),i=t,a={address:i.get_address().to_string(),family:this.type===`udp6`?`IPv6`:`IPv4`,port:i.get_port()};n.length>0&&this.emit(`message`,n,a)}}catch(t){let n=t;!this._closed&&n.code!==e.IOErrorEnum.CANCELLED&&this.emit(`error`,t)}return t.SOURCE_CONTINUE}),n.attach(null)}};function createSocket(e,t){let n=new Socket(typeof e==`string`?{type:e}:e);return t&&n.on(`message`,t),n}var s={Socket,createSocket};export{Socket,createSocket,s as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/dgram",
3
- "version": "0.3.20",
3
+ "version": "0.3.21",
4
4
  "description": "Node.js dgram module for Gjs",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -32,13 +32,13 @@
32
32
  "dependencies": {
33
33
  "@girs/gio-2.0": "2.88.0-4.0.0-rc.14",
34
34
  "@girs/glib-2.0": "2.88.0-4.0.0-rc.14",
35
- "@gjsify/buffer": "^0.3.20",
36
- "@gjsify/events": "^0.3.20",
37
- "@gjsify/utils": "^0.3.20"
35
+ "@gjsify/buffer": "^0.3.21",
36
+ "@gjsify/events": "^0.3.21",
37
+ "@gjsify/utils": "^0.3.21"
38
38
  },
39
39
  "devDependencies": {
40
- "@gjsify/cli": "^0.3.20",
41
- "@gjsify/unit": "^0.3.20",
40
+ "@gjsify/cli": "^0.3.21",
41
+ "@gjsify/unit": "^0.3.21",
42
42
  "@types/node": "^25.6.2",
43
43
  "typescript": "^6.0.3"
44
44
  }