@gjsify/net 0.3.19 → 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.
package/lib/esm/index.js CHANGED
@@ -1 +1 @@
1
- import{Socket as e}from"./socket.js";import{Server as t}from"./server.js";import n from"@girs/gio-2.0";function r(e){if(typeof e!=`string`)return 0;let t=e.includes(`%`)?e.split(`%`)[0]:e,r=n.InetAddress.new_from_string(t);if(!r)return 0;switch(r.get_family()){case n.SocketFamily.INVALID:return 0;case n.SocketFamily.IPV4:return 4;case n.SocketFamily.IPV6:return 6}}function i(e){return r(e)===4}function a(e){return r(e)===6}function o(t,n,r){return new e().connect(t,n,r)}const s=o;function c(e,n){return new t(e,n)}var l={Socket:e,Server:t,isIP:r,isIPv4:i,isIPv6:a,createConnection:o,connect:s,createServer:c};export{t as Server,e as Socket,s as connect,o as createConnection,c as createServer,l as default,r as isIP,i as isIPv4,a as isIPv6};
1
+ import{Socket as e}from"./socket.js";import{Server as t}from"./server.js";import n from"@girs/gio-2.0";function isIP(e){if(typeof e!=`string`)return 0;let t=e.includes(`%`)?e.split(`%`)[0]:e,r=n.InetAddress.new_from_string(t);if(!r)return 0;switch(r.get_family()){case n.SocketFamily.INVALID:return 0;case n.SocketFamily.IPV4:return 4;case n.SocketFamily.IPV6:return 6}}function isIPv4(e){return isIP(e)===4}function isIPv6(e){return isIP(e)===6}function createConnection(t,n,r){return new e().connect(t,n,r)}const r=createConnection;function createServer(e,n){return new t(e,n)}var i={Socket:e,Server:t,isIP,isIPv4,isIPv6,createConnection,connect:r,createServer};export{t as Server,e as Socket,r as connect,createConnection,createServer,i as default,isIP,isIPv4,isIPv6};
package/lib/esm/server.js CHANGED
@@ -1 +1 @@
1
- import{Socket as e}from"./socket.js";import t from"@girs/gio-2.0";import{createNodeError as n,deferEmit as r,ensureMainLoop as i}from"@gjsify/utils";import{EventEmitter as a}from"node:events";const o=new Set;var s=class extends a{listening=!1;maxConnections;allowHalfOpen;_service=null;_connections=new Set;_address=null;constructor(e,t){super(),typeof e==`function`?(t=e,this.allowHalfOpen=!1):this.allowHalfOpen=e?.allowHalfOpen??!1,t&&this.on(`connection`,t)}listen(...e){let a=0,s=`0.0.0.0`,c=511,l;if(typeof e[0]==`object`&&e[0]!==null&&!Array.isArray(e[0])){let t=e[0];a=t.port??0,s=t.host??`0.0.0.0`,c=t.backlog??511,l=e[1]}else{typeof e[0]==`number`&&(a=e[0]);for(let t=1;t<e.length;t++)typeof e[t]==`string`?s=e[t]:typeof e[t]==`number`?c=e[t]:typeof e[t]==`function`&&(l=e[t])}l&&this.once(`listening`,l);try{this._service=new t.SocketService,this._service.set_backlog(c);let e;a===0?e=this._service.add_any_inet_port(null):(this._service.add_inet_port(a,null),e=a),this._service.connect(`incoming`,(e,t)=>(this._handleConnection(t),!0)),this._service.start(),i(),this.listening=!0,o.add(this);let n=s.includes(`:`)?`IPv6`:`IPv4`;this._address={port:e,family:n,address:s},r(this,`listening`)}catch(e){let t=n(e,`listen`,{address:s,port:a});r(this,`error`,t)}return this}_handleConnection(t){if(this.maxConnections&&this._connections.size>=this.maxConnections){try{t.close(null)}catch{}return}let n=new e({allowHalfOpen:this.allowHalfOpen});n._setConnection(t),n._setupConnection({}),this._connections.add(n),n.on(`close`,()=>{this._connections.delete(n)}),this.emit(`connection`,n)}address(){return this._address}close(e){if(e&&this.once(`close`,e),!this._service||!this.listening)return setTimeout(()=>{let e=Error(`Server is not running`);e.code=`ERR_SERVER_NOT_RUNNING`,this.emit(`error`,e)},0),this;this._service.stop(),this._service.close(),this._service=null,this.listening=!1,o.delete(this);for(let e of this._connections)e.destroy();return this._connections.clear(),r(this,`close`),this}getConnections(e){e(null,this._connections.size)}ref(){return this}unref(){return this}};export{s as Server};
1
+ import{Socket as e}from"./socket.js";import t from"@girs/gio-2.0";import{createNodeError as n,deferEmit as r,ensureMainLoop as i}from"@gjsify/utils";import{EventEmitter as a}from"node:events";const o=new Set;var Server=class extends a{listening=!1;maxConnections;allowHalfOpen;_service=null;_connections=new Set;_address=null;constructor(e,t){super(),typeof e==`function`?(t=e,this.allowHalfOpen=!1):this.allowHalfOpen=e?.allowHalfOpen??!1,t&&this.on(`connection`,t)}listen(...e){let a=0,s=`0.0.0.0`,c=511,l;if(typeof e[0]==`object`&&e[0]!==null&&!Array.isArray(e[0])){let t=e[0];a=t.port??0,s=t.host??`0.0.0.0`,c=t.backlog??511,l=e[1]}else{typeof e[0]==`number`&&(a=e[0]);for(let t=1;t<e.length;t++)typeof e[t]==`string`?s=e[t]:typeof e[t]==`number`?c=e[t]:typeof e[t]==`function`&&(l=e[t])}l&&this.once(`listening`,l);try{this._service=new t.SocketService,this._service.set_backlog(c);let e;a===0?e=this._service.add_any_inet_port(null):(this._service.add_inet_port(a,null),e=a),this._service.connect(`incoming`,(e,t)=>(this._handleConnection(t),!0)),this._service.start(),i(),this.listening=!0,o.add(this);let n=s.includes(`:`)?`IPv6`:`IPv4`;this._address={port:e,family:n,address:s},r(this,`listening`)}catch(e){let t=n(e,`listen`,{address:s,port:a});r(this,`error`,t)}return this}_handleConnection(t){if(this.maxConnections&&this._connections.size>=this.maxConnections){try{t.close(null)}catch{}return}let n=new e({allowHalfOpen:this.allowHalfOpen});n._setConnection(t),n._setupConnection({}),this._connections.add(n),n.on(`close`,()=>{this._connections.delete(n)}),this.emit(`connection`,n)}address(){return this._address}close(e){if(e&&this.once(`close`,e),!this._service||!this.listening)return setTimeout(()=>{let e=Error(`Server is not running`);e.code=`ERR_SERVER_NOT_RUNNING`,this.emit(`error`,e)},0),this;this._service.stop(),this._service.close(),this._service=null,this.listening=!1,o.delete(this);for(let e of this._connections)e.destroy();return this._connections.clear(),r(this,`close`),this}getConnections(e){e(null,this._connections.size)}ref(){return this}unref(){return this}};export{Server};
package/lib/esm/socket.js CHANGED
@@ -1 +1 @@
1
- import e from"@girs/gio-2.0";import t from"@girs/glib-2.0";import{Duplex as n}from"node:stream";import{Buffer as r}from"node:buffer";import{createNodeError as i,gbytesToUint8Array as a}from"@gjsify/utils";var o=class extends n{remoteAddress;remotePort;remoteFamily;localAddress;localPort;bytesRead=0;bytesWritten=0;connecting=!1;destroyed=!1;pending=!0;readyState=`closed`;_connection=null;_ioStream=null;_inputStream=null;_outputStream=null;_cancellable=new e.Cancellable;_reading=!1;_timeout=0;_timeoutId=null;constructor(e){super(e),this.allowHalfOpen=e?.allowHalfOpen??!1}_setConnection(e){this._connection=e}_setupFromIOStream(t){this._ioStream=t;try{let n=t;if(typeof n.get_socket==`function`){this._connection=n;let t=n.get_remote_address();this.remoteAddress=t.get_address().to_string(),this.remotePort=t.get_port(),this.remoteFamily=t.get_address().get_family()===e.SocketFamily.IPV6?`IPv6`:`IPv4`;let r=n.get_local_address();this.localAddress=r.get_address().to_string(),this.localPort=r.get_port()}}catch{}this._inputStream=t.get_input_stream(),this._outputStream=t.get_output_stream(),this.connecting=!1,this.pending=!1,this.readyState=`open`,this.emit(`connect`),this.emit(`ready`),this._startReading()}_attachOutputOnly(t){this._ioStream=t;try{let n=t;if(typeof n.get_socket==`function`){this._connection=n;let t=n.get_remote_address();this.remoteAddress=t.get_address().to_string(),this.remotePort=t.get_port(),this.remoteFamily=t.get_address().get_family()===e.SocketFamily.IPV6?`IPv6`:`IPv4`;let r=n.get_local_address();this.localAddress=r.get_address().to_string(),this.localPort=r.get_port()}}catch{}this._inputStream=t.get_input_stream(),this._outputStream=t.get_output_stream(),this.connecting=!1,this.pending=!1,this.readyState=`open`,this.emit(`connect`),this.emit(`ready`)}_releaseIOStream(){let e=this._ioStream;return this._ioStream=null,this._inputStream=null,this._outputStream=null,this._connection=null,e}connect(t,n,r){let a;typeof t==`number`?(a={port:t,host:typeof n==`string`?n:`localhost`},typeof n==`function`&&(r=n)):(a=t,typeof n==`function`&&(r=n)),r&&this.once(`connect`,r),this.connecting=!0,this.readyState=`opening`,this.pending=!0;let o=a.host||`localhost`,s=a.port,c=new e.SocketClient;return a.timeout&&c.set_timeout(Math.ceil(a.timeout/1e3)),c.connect_to_host_async(o,s,this._cancellable,(e,t)=>{try{this._connection=c.connect_to_host_finish(t),this._setupConnection(a)}catch(e){this.connecting=!1,this.readyState=`closed`;let t=i(e,`connect`,{address:o,port:s});this.destroy(t)}}),this}_setupConnection(t){if(!this._connection)return;let n=this._connection.get_socket();if(this._inputStream=this._connection.get_input_stream(),this._outputStream=this._connection.get_output_stream(),`keepAlive`in t&&t.keepAlive&&n.set_keepalive(!0),!(`noDelay`in t)||t.noDelay!==!1)try{n.set_option(6,1,1)}catch{}try{let t=this._connection.get_remote_address();this.remoteAddress=t.get_address().to_string(),this.remotePort=t.get_port(),this.remoteFamily=t.get_address().get_family()===e.SocketFamily.IPV6?`IPv6`:`IPv4`}catch{}try{let e=this._connection.get_local_address();this.localAddress=e.get_address().to_string(),this.localPort=e.get_port()}catch{}this.connecting=!1,this.pending=!1,this.readyState=`open`,this.emit(`connect`),this.emit(`ready`),this._startReading()}_startReading(){this._reading||!this._inputStream||(this._reading=!0,this._readLoop())}async _readLoop(){let e=this._inputStream;if(e)try{for(;this._reading&&e;){let n=await new Promise((n,r)=>{e.read_bytes_async(16384,t.PRIORITY_DEFAULT,this._cancellable,(t,i)=>{try{n(e.read_bytes_finish(i))}catch(e){r(e)}})});if(!n||n.get_size()===0){this._reading=!1,this.allowHalfOpen?this.readyState=this.writable?`writeOnly`:`closed`:this.once(`end`,()=>{this.end(),this.readyState=`closed`}),this.push(null);break}let i=a(n);if(this.bytesRead+=i.length,this._resetTimeout(),!this.push(r.from(i))){this._reading=!1;break}}}catch(e){this._cancellable.is_cancelled()||this.destroy(i(e,`read`,{address:this.remoteAddress}))}}_read(e){!this._reading&&this._inputStream&&this._startReading()}_write(e,n,a){if(!this._outputStream){a(Error(`Socket is not connected`));return}let o=r.isBuffer(e)?e:r.from(e,n);this._outputStream.write_bytes_async(new t.Bytes(o),t.PRIORITY_DEFAULT,this._cancellable,(e,t)=>{try{let e=this._outputStream.write_bytes_finish(t);this.bytesWritten+=e,this._resetTimeout(),a(null)}catch(e){a(i(e,`write`,{address:this.remoteAddress}))}})}_final(e){if(this._connection)try{this._connection.get_socket().shutdown(!1,!0),this.readyState=this.readable?`readOnly`:`closed`}catch{}else if(this._ioStream){try{this._ioStream.close(null),this.readyState=`closed`}catch{}this._ioStream=null,this._inputStream=null,this._outputStream=null}e()}_destroy(e,t){if(this._reading=!1,this._clearTimeout(),this._cancellable.cancel(),this._connection){try{this._connection.close(null)}catch{}this._connection=null}else if(this._ioStream)try{this._ioStream.close(null)}catch{}this._ioStream=null,this._inputStream=null,this._outputStream=null,this.readyState=`closed`,this.destroyed=!0,t(e)}setTimeout(e,t){return this._timeout=e,this._clearTimeout(),t&&this.once(`timeout`,t),e>0&&this._resetTimeout(),this}_resetTimeout(){this._clearTimeout(),this._timeout>0&&(this._timeoutId=setTimeout(()=>{this.emit(`timeout`)},this._timeout))}_clearTimeout(){this._timeoutId!==null&&(clearTimeout(this._timeoutId),this._timeoutId=null)}setKeepAlive(e,t){if(this._connection)try{this._connection.get_socket().set_keepalive(e??!1)}catch{}return this}setNoDelay(e){if(this._connection)try{this._connection.get_socket().set_option(6,1,e===!1?0:1)}catch{}return this}destroySoon(){this.writable&&this.end(),this.writableFinished?this.destroy():this.once(`finish`,this.destroy.bind(this))}address(){return this.localAddress&&this.localPort?{port:this.localPort,family:this.remoteFamily||`IPv4`,address:this.localAddress}:{}}ref(){return this}unref(){return this}};export{o as Socket};
1
+ import e from"@girs/gio-2.0";import t from"@girs/glib-2.0";import{Duplex as n}from"node:stream";import{Buffer as r}from"node:buffer";import{createNodeError as i,gbytesToUint8Array as a}from"@gjsify/utils";var Socket=class extends n{remoteAddress;remotePort;remoteFamily;localAddress;localPort;bytesRead=0;bytesWritten=0;connecting=!1;destroyed=!1;pending=!0;readyState=`closed`;_connection=null;_ioStream=null;_inputStream=null;_outputStream=null;_cancellable=new e.Cancellable;_reading=!1;_timeout=0;_timeoutId=null;constructor(e){super(e),this.allowHalfOpen=e?.allowHalfOpen??!1}_setConnection(e){this._connection=e}_setupFromIOStream(t){this._ioStream=t;try{let n=t;if(typeof n.get_socket==`function`){this._connection=n;let t=n.get_remote_address();this.remoteAddress=t.get_address().to_string(),this.remotePort=t.get_port(),this.remoteFamily=t.get_address().get_family()===e.SocketFamily.IPV6?`IPv6`:`IPv4`;let r=n.get_local_address();this.localAddress=r.get_address().to_string(),this.localPort=r.get_port()}}catch{}this._inputStream=t.get_input_stream(),this._outputStream=t.get_output_stream(),this.connecting=!1,this.pending=!1,this.readyState=`open`,this.emit(`connect`),this.emit(`ready`),this._startReading()}_attachOutputOnly(t){this._ioStream=t;try{let n=t;if(typeof n.get_socket==`function`){this._connection=n;let t=n.get_remote_address();this.remoteAddress=t.get_address().to_string(),this.remotePort=t.get_port(),this.remoteFamily=t.get_address().get_family()===e.SocketFamily.IPV6?`IPv6`:`IPv4`;let r=n.get_local_address();this.localAddress=r.get_address().to_string(),this.localPort=r.get_port()}}catch{}this._inputStream=t.get_input_stream(),this._outputStream=t.get_output_stream(),this.connecting=!1,this.pending=!1,this.readyState=`open`,this.emit(`connect`),this.emit(`ready`)}_releaseIOStream(){let e=this._ioStream;return this._ioStream=null,this._inputStream=null,this._outputStream=null,this._connection=null,e}connect(t,n,r){let a;typeof t==`number`?(a={port:t,host:typeof n==`string`?n:`localhost`},typeof n==`function`&&(r=n)):(a=t,typeof n==`function`&&(r=n)),r&&this.once(`connect`,r),this.connecting=!0,this.readyState=`opening`,this.pending=!0;let o=a.host||`localhost`,s=a.port,c=new e.SocketClient;return a.timeout&&c.set_timeout(Math.ceil(a.timeout/1e3)),c.connect_to_host_async(o,s,this._cancellable,(e,t)=>{try{this._connection=c.connect_to_host_finish(t),this._setupConnection(a)}catch(e){this.connecting=!1,this.readyState=`closed`;let t=i(e,`connect`,{address:o,port:s});this.destroy(t)}}),this}_setupConnection(t){if(!this._connection)return;let n=this._connection.get_socket();if(this._inputStream=this._connection.get_input_stream(),this._outputStream=this._connection.get_output_stream(),`keepAlive`in t&&t.keepAlive&&n.set_keepalive(!0),!(`noDelay`in t)||t.noDelay!==!1)try{n.set_option(6,1,1)}catch{}try{let t=this._connection.get_remote_address();this.remoteAddress=t.get_address().to_string(),this.remotePort=t.get_port(),this.remoteFamily=t.get_address().get_family()===e.SocketFamily.IPV6?`IPv6`:`IPv4`}catch{}try{let e=this._connection.get_local_address();this.localAddress=e.get_address().to_string(),this.localPort=e.get_port()}catch{}this.connecting=!1,this.pending=!1,this.readyState=`open`,this.emit(`connect`),this.emit(`ready`),this._startReading()}_startReading(){this._reading||!this._inputStream||(this._reading=!0,this._readLoop())}async _readLoop(){let e=this._inputStream;if(e)try{for(;this._reading&&e;){let n=await new Promise((n,r)=>{e.read_bytes_async(16384,t.PRIORITY_DEFAULT,this._cancellable,(t,i)=>{try{n(e.read_bytes_finish(i))}catch(e){r(e)}})});if(!n||n.get_size()===0){this._reading=!1,this.allowHalfOpen?this.readyState=this.writable?`writeOnly`:`closed`:this.once(`end`,()=>{this.end(),this.readyState=`closed`}),this.push(null);break}let i=a(n);if(this.bytesRead+=i.length,this._resetTimeout(),!this.push(r.from(i))){this._reading=!1;break}}}catch(e){this._cancellable.is_cancelled()||this.destroy(i(e,`read`,{address:this.remoteAddress}))}}_read(e){!this._reading&&this._inputStream&&this._startReading()}_write(e,n,a){if(!this._outputStream){a(Error(`Socket is not connected`));return}let o=r.isBuffer(e)?e:r.from(e,n);this._outputStream.write_bytes_async(new t.Bytes(o),t.PRIORITY_DEFAULT,this._cancellable,(e,t)=>{try{let e=this._outputStream.write_bytes_finish(t);this.bytesWritten+=e,this._resetTimeout(),a(null)}catch(e){a(i(e,`write`,{address:this.remoteAddress}))}})}_final(e){if(this._connection)try{this._connection.get_socket().shutdown(!1,!0),this.readyState=this.readable?`readOnly`:`closed`}catch{}else if(this._ioStream){try{this._ioStream.close(null),this.readyState=`closed`}catch{}this._ioStream=null,this._inputStream=null,this._outputStream=null}e()}_destroy(e,t){if(this._reading=!1,this._clearTimeout(),this._cancellable.cancel(),this._connection){try{this._connection.close(null)}catch{}this._connection=null}else if(this._ioStream)try{this._ioStream.close(null)}catch{}this._ioStream=null,this._inputStream=null,this._outputStream=null,this.readyState=`closed`,this.destroyed=!0,t(e)}setTimeout(e,t){return this._timeout=e,this._clearTimeout(),t&&this.once(`timeout`,t),e>0&&this._resetTimeout(),this}_resetTimeout(){this._clearTimeout(),this._timeout>0&&(this._timeoutId=setTimeout(()=>{this.emit(`timeout`)},this._timeout))}_clearTimeout(){this._timeoutId!==null&&(clearTimeout(this._timeoutId),this._timeoutId=null)}setKeepAlive(e,t){if(this._connection)try{this._connection.get_socket().set_keepalive(e??!1)}catch{}return this}setNoDelay(e){if(this._connection)try{this._connection.get_socket().set_option(6,1,e===!1?0:1)}catch{}return this}destroySoon(){this.writable&&this.end(),this.writableFinished?this.destroy():this.once(`finish`,this.destroy.bind(this))}address(){return this.localAddress&&this.localPort?{port:this.localPort,family:this.remoteFamily||`IPv4`,address:this.localAddress}:{}}ref(){return this}unref(){return this}};export{Socket};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/net",
3
- "version": "0.3.19",
3
+ "version": "0.3.21",
4
4
  "description": "Node.js net module for Gjs",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -34,17 +34,17 @@
34
34
  "net"
35
35
  ],
36
36
  "devDependencies": {
37
- "@gjsify/cli": "^0.3.19",
38
- "@gjsify/unit": "^0.3.19",
37
+ "@gjsify/cli": "^0.3.21",
38
+ "@gjsify/unit": "^0.3.21",
39
39
  "@types/node": "^25.6.2",
40
40
  "typescript": "^6.0.3"
41
41
  },
42
42
  "dependencies": {
43
43
  "@girs/gio-2.0": "2.88.0-4.0.0-rc.14",
44
44
  "@girs/glib-2.0": "2.88.0-4.0.0-rc.14",
45
- "@gjsify/buffer": "^0.3.19",
46
- "@gjsify/events": "^0.3.19",
47
- "@gjsify/stream": "^0.3.19",
48
- "@gjsify/utils": "^0.3.19"
45
+ "@gjsify/buffer": "^0.3.21",
46
+ "@gjsify/events": "^0.3.21",
47
+ "@gjsify/stream": "^0.3.21",
48
+ "@gjsify/utils": "^0.3.21"
49
49
  }
50
50
  }