@gjsify/net 0.4.30 → 0.4.31
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 +1 -1
- package/lib/esm/socket.js +1 -1
- package/lib/types/socket.d.ts +1 -1
- package/package.json +7 -7
package/lib/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./_virtual/_rolldown/runtime.js";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};
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";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 typeof e==`function`?new t(e):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/socket.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./_virtual/_rolldown/runtime.js";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};
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";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:typeof e==`string`?r.from(e,n):r.from(e);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/lib/types/socket.d.ts
CHANGED
|
@@ -65,7 +65,7 @@ export declare class Socket extends Duplex {
|
|
|
65
65
|
private _startReading;
|
|
66
66
|
private _readLoop;
|
|
67
67
|
_read(_size: number): void;
|
|
68
|
-
_write(chunk:
|
|
68
|
+
_write(chunk: unknown, encoding: string, callback: (error?: Error | null) => void): void;
|
|
69
69
|
_final(callback: (error?: Error | null) => void): void;
|
|
70
70
|
_destroy(err: Error | null, callback: (error?: Error | null) => void): void;
|
|
71
71
|
/** Set the socket timeout in milliseconds. 0 disables. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/net",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.31",
|
|
4
4
|
"description": "Node.js net module for Gjs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -37,17 +37,17 @@
|
|
|
37
37
|
"net"
|
|
38
38
|
],
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@gjsify/cli": "^0.4.
|
|
41
|
-
"@gjsify/unit": "^0.4.
|
|
40
|
+
"@gjsify/cli": "^0.4.31",
|
|
41
|
+
"@gjsify/unit": "^0.4.31",
|
|
42
42
|
"@types/node": "^25.9.1",
|
|
43
43
|
"typescript": "^6.0.3"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@girs/gio-2.0": "2.88.0-4.0.1",
|
|
47
47
|
"@girs/glib-2.0": "2.88.0-4.0.1",
|
|
48
|
-
"@gjsify/buffer": "^0.4.
|
|
49
|
-
"@gjsify/events": "^0.4.
|
|
50
|
-
"@gjsify/stream": "^0.4.
|
|
51
|
-
"@gjsify/utils": "^0.4.
|
|
48
|
+
"@gjsify/buffer": "^0.4.31",
|
|
49
|
+
"@gjsify/events": "^0.4.31",
|
|
50
|
+
"@gjsify/stream": "^0.4.31",
|
|
51
|
+
"@gjsify/utils": "^0.4.31"
|
|
52
52
|
}
|
|
53
53
|
}
|