@gjsify/dgram 0.3.21 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/_virtual/_rolldown/runtime.js +1 -0
- package/lib/esm/index.js +1 -1
- package/package.json +46 -43
- package/src/index.spec.ts +0 -1214
- package/src/index.ts +0 -577
- package/src/test.mts +0 -3
- package/tsconfig.json +0 -29
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=Object.defineProperty,__name=(t,n)=>e(t,`name`,{value:n,configurable:!0});export{__name};
|
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 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};
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";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,45 +1,48 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
"name": "@gjsify/dgram",
|
|
3
|
+
"version": "0.4.3",
|
|
4
|
+
"description": "Node.js dgram module for Gjs",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "lib/esm/index.js",
|
|
7
|
+
"types": "lib/types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"default": "./lib/esm/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
|
|
19
|
+
"check": "tsc --noEmit",
|
|
20
|
+
"build": "gjsify run build:gjsify && gjsify run build:types",
|
|
21
|
+
"build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
|
|
22
|
+
"build:types": "tsc",
|
|
23
|
+
"build:test": "gjsify run build:test:gjs && gjsify run build:test:node",
|
|
24
|
+
"build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
|
|
25
|
+
"build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
|
|
26
|
+
"test": "gjsify run build:gjsify && gjsify run build:test && gjsify run test:node && gjsify run test:gjs",
|
|
27
|
+
"test:gjs": "gjsify run test.gjs.mjs",
|
|
28
|
+
"test:node": "node test.node.mjs"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"gjs",
|
|
32
|
+
"node",
|
|
33
|
+
"dgram"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@girs/gio-2.0": "2.88.0-4.0.0-rc.15",
|
|
37
|
+
"@girs/glib-2.0": "2.88.0-4.0.0-rc.15",
|
|
38
|
+
"@gjsify/buffer": "workspace:^",
|
|
39
|
+
"@gjsify/events": "workspace:^",
|
|
40
|
+
"@gjsify/utils": "workspace:^"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@gjsify/cli": "workspace:^",
|
|
44
|
+
"@gjsify/unit": "workspace:^",
|
|
45
|
+
"@types/node": "^25.6.2",
|
|
46
|
+
"typescript": "^6.0.3"
|
|
12
47
|
}
|
|
13
|
-
|
|
14
|
-
"scripts": {
|
|
15
|
-
"clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
|
|
16
|
-
"check": "tsc --noEmit",
|
|
17
|
-
"build": "yarn build:gjsify && yarn build:types",
|
|
18
|
-
"build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
|
|
19
|
-
"build:types": "tsc",
|
|
20
|
-
"build:test": "yarn build:test:gjs && yarn build:test:node",
|
|
21
|
-
"build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
|
|
22
|
-
"build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
|
|
23
|
-
"test": "yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
|
|
24
|
-
"test:gjs": "gjsify run test.gjs.mjs",
|
|
25
|
-
"test:node": "node test.node.mjs"
|
|
26
|
-
},
|
|
27
|
-
"keywords": [
|
|
28
|
-
"gjs",
|
|
29
|
-
"node",
|
|
30
|
-
"dgram"
|
|
31
|
-
],
|
|
32
|
-
"dependencies": {
|
|
33
|
-
"@girs/gio-2.0": "2.88.0-4.0.0-rc.14",
|
|
34
|
-
"@girs/glib-2.0": "2.88.0-4.0.0-rc.14",
|
|
35
|
-
"@gjsify/buffer": "^0.3.21",
|
|
36
|
-
"@gjsify/events": "^0.3.21",
|
|
37
|
-
"@gjsify/utils": "^0.3.21"
|
|
38
|
-
},
|
|
39
|
-
"devDependencies": {
|
|
40
|
-
"@gjsify/cli": "^0.3.21",
|
|
41
|
-
"@gjsify/unit": "^0.3.21",
|
|
42
|
-
"@types/node": "^25.6.2",
|
|
43
|
-
"typescript": "^6.0.3"
|
|
44
|
-
}
|
|
45
|
-
}
|
|
48
|
+
}
|