@gjsify/net 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.
@@ -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{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 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 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};
1
+ import"./_virtual/_rolldown/runtime.js";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 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: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,50 +1,53 @@
1
1
  {
2
- "name": "@gjsify/net",
3
- "version": "0.3.21",
4
- "description": "Node.js net 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"
2
+ "name": "@gjsify/net",
3
+ "version": "0.4.3",
4
+ "description": "Node.js net 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
+ "./socket": {
14
+ "types": "./lib/types/socket.d.ts",
15
+ "default": "./lib/esm/socket.js"
16
+ }
12
17
  },
13
- "./socket": {
14
- "types": "./lib/types/socket.d.ts",
15
- "default": "./lib/esm/socket.js"
18
+ "files": [
19
+ "lib"
20
+ ],
21
+ "scripts": {
22
+ "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
23
+ "check": "tsc --noEmit",
24
+ "build": "gjsify run build:gjsify && gjsify run build:types",
25
+ "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
26
+ "build:types": "tsc",
27
+ "build:test": "gjsify run build:test:gjs && gjsify run build:test:node",
28
+ "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
29
+ "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
30
+ "test": "gjsify run build:gjsify && gjsify run build:test && gjsify run test:node && gjsify run test:gjs",
31
+ "test:gjs": "gjsify run test.gjs.mjs",
32
+ "test:node": "node test.node.mjs"
33
+ },
34
+ "keywords": [
35
+ "gjs",
36
+ "node",
37
+ "net"
38
+ ],
39
+ "devDependencies": {
40
+ "@gjsify/cli": "workspace:^",
41
+ "@gjsify/unit": "workspace:^",
42
+ "@types/node": "^25.6.2",
43
+ "typescript": "^6.0.3"
44
+ },
45
+ "dependencies": {
46
+ "@girs/gio-2.0": "2.88.0-4.0.0-rc.15",
47
+ "@girs/glib-2.0": "2.88.0-4.0.0-rc.15",
48
+ "@gjsify/buffer": "workspace:^",
49
+ "@gjsify/events": "workspace:^",
50
+ "@gjsify/stream": "workspace:^",
51
+ "@gjsify/utils": "workspace:^"
16
52
  }
17
- },
18
- "scripts": {
19
- "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
20
- "check": "tsc --noEmit",
21
- "build": "yarn build:gjsify && yarn build:types",
22
- "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
23
- "build:types": "tsc",
24
- "build:test": "yarn build:test:gjs && yarn build:test:node",
25
- "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
26
- "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
27
- "test": "yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
28
- "test:gjs": "gjsify run test.gjs.mjs",
29
- "test:node": "node test.node.mjs"
30
- },
31
- "keywords": [
32
- "gjs",
33
- "node",
34
- "net"
35
- ],
36
- "devDependencies": {
37
- "@gjsify/cli": "^0.3.21",
38
- "@gjsify/unit": "^0.3.21",
39
- "@types/node": "^25.6.2",
40
- "typescript": "^6.0.3"
41
- },
42
- "dependencies": {
43
- "@girs/gio-2.0": "2.88.0-4.0.0-rc.14",
44
- "@girs/glib-2.0": "2.88.0-4.0.0-rc.14",
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
- }
50
- }
53
+ }
package/src/error.spec.ts DELETED
@@ -1,169 +0,0 @@
1
- // Net error handling and lifecycle tests
2
- // Ported from refs/node-test/parallel/test-net-*.js
3
- // Original: MIT license, Node.js contributors
4
-
5
- import { describe, it, expect } from '@gjsify/unit';
6
- import * as net from 'node:net';
7
-
8
- export default async () => {
9
-
10
- // ===================== Socket destroy idempotency =====================
11
- await describe('net.Socket destroy', async () => {
12
- await it('should be safe to call destroy() multiple times', async () => {
13
- const socket = new net.Socket();
14
- socket.destroy();
15
- socket.destroy();
16
- socket.destroy();
17
- expect(socket.destroyed).toBe(true);
18
- });
19
-
20
- await it('should emit close after destroy', async () => {
21
- await new Promise<void>((resolve) => {
22
- const socket = new net.Socket();
23
- socket.on('close', () => {
24
- expect(socket.destroyed).toBe(true);
25
- resolve();
26
- });
27
- socket.destroy();
28
- });
29
- });
30
-
31
- await it('should emit error event when destroyed with error', async () => {
32
- await new Promise<void>((resolve) => {
33
- const socket = new net.Socket();
34
- let errorEmitted = false;
35
- socket.on('error', (err: Error) => {
36
- errorEmitted = true;
37
- expect(err.message).toBe('test');
38
- });
39
- socket.on('close', () => {
40
- expect(errorEmitted).toBe(true);
41
- resolve();
42
- });
43
- socket.destroy(new Error('test'));
44
- });
45
- });
46
- });
47
-
48
- // ===================== Server getConnections =====================
49
- await describe('net.Server.getConnections', async () => {
50
- await it('should return 0 when no connections', async () => {
51
- const server = net.createServer();
52
- await new Promise<void>((resolve, reject) => {
53
- server.listen(0, () => {
54
- server.getConnections((err, count) => {
55
- expect(err).toBeNull();
56
- expect(count).toBe(0);
57
- server.close(() => resolve());
58
- });
59
- });
60
- server.on('error', reject);
61
- });
62
- });
63
-
64
- await it('should count active connections', async () => {
65
- const server = net.createServer();
66
- await new Promise<void>((resolve, reject) => {
67
- server.listen(0, () => {
68
- const addr = server.address() as { port: number };
69
- const client = net.createConnection({ port: addr.port, host: '127.0.0.1' }, () => {
70
- // Give server time to accept the connection
71
- setTimeout(() => {
72
- server.getConnections((err, count) => {
73
- expect(err).toBeNull();
74
- expect(count).toBe(1);
75
- client.destroy();
76
- server.close(() => resolve());
77
- });
78
- }, 50);
79
- });
80
- client.on('error', reject);
81
- });
82
- server.on('error', reject);
83
- });
84
- });
85
- });
86
-
87
- // ===================== Server maxConnections =====================
88
- await describe('net.Server.maxConnections', async () => {
89
- await it('should accept maxConnections property', async () => {
90
- const server = net.createServer();
91
- server.maxConnections = 5;
92
- expect(server.maxConnections).toBe(5);
93
- server.close();
94
- });
95
- });
96
-
97
- // Connection refused is already tested in index.spec.ts and server.spec.ts
98
-
99
- // ===================== Socket address =====================
100
- await describe('net.Socket address info', async () => {
101
- await it('should have correct address info after connect', async () => {
102
- const server = net.createServer();
103
- await new Promise<void>((resolve, reject) => {
104
- server.listen(0, () => {
105
- const addr = server.address() as { port: number };
106
- const client = net.createConnection({ port: addr.port, host: '127.0.0.1' }, () => {
107
- expect(client.remoteAddress).toBe('127.0.0.1');
108
- expect(client.remotePort).toBe(addr.port);
109
- expect(client.localAddress).toBeDefined();
110
- expect(client.localPort).toBeDefined();
111
- expect(typeof client.localPort).toBe('number');
112
- client.destroy();
113
- server.close(() => resolve());
114
- });
115
- client.on('error', reject);
116
- });
117
- server.on('error', reject);
118
- });
119
- });
120
- });
121
-
122
- // ===================== Server close without listening =====================
123
- await describe('net.Server close edge cases', async () => {
124
- await it('should handle closing a non-listening server', async () => {
125
- const server = net.createServer();
126
- let errorOrClose = false;
127
- server.on('error', () => { errorOrClose = true; });
128
- server.close((err) => {
129
- // Node.js passes an error to the callback for non-listening servers
130
- errorOrClose = true;
131
- });
132
- // Wait for async callback
133
- await new Promise<void>((resolve) => setTimeout(() => {
134
- expect(errorOrClose).toBe(true);
135
- resolve();
136
- }, 100));
137
- });
138
- });
139
-
140
- // ===================== Socket bytesRead/bytesWritten =====================
141
- await describe('net.Socket bytes tracking', async () => {
142
- await it('should track bytesWritten and bytesRead', async () => {
143
- const server = net.createServer((socket) => {
144
- socket.on('data', (data) => {
145
- socket.write(data); // echo
146
- });
147
- });
148
-
149
- await new Promise<void>((resolve, reject) => {
150
- server.listen(0, () => {
151
- const addr = server.address() as { port: number };
152
- const client = net.createConnection({ port: addr.port, host: '127.0.0.1' }, () => {
153
- const testData = 'Hello, World!';
154
- client.write(testData);
155
-
156
- client.on('data', () => {
157
- expect(client.bytesWritten).toBeGreaterThan(0);
158
- expect(client.bytesRead).toBeGreaterThan(0);
159
- client.destroy();
160
- server.close(() => resolve());
161
- });
162
- });
163
- client.on('error', reject);
164
- });
165
- server.on('error', reject);
166
- });
167
- });
168
- });
169
- };