@gjsify/net 0.3.13 → 0.3.14

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,57 +1,49 @@
1
+ import { Socket } from "./socket.js";
2
+ import { Server } from "./server.js";
1
3
  import Gio from "@girs/gio-2.0";
2
- import { Socket, SocketConnectOptions } from "./socket.js";
3
- import { Server, ListenOptions } from "./server.js";
4
- import { Socket as Socket2 } from "./socket.js";
5
- import { Server as Server2 } from "./server.js";
4
+
5
+ //#region src/index.ts
6
+ /** Check if input is a valid IP address. Returns 0, 4, or 6. */
6
7
  function isIP(input) {
7
- if (typeof input !== "string") return 0;
8
- const stripped = input.includes("%") ? input.split("%")[0] : input;
9
- const addr = Gio.InetAddress.new_from_string(stripped);
10
- if (!addr) return 0;
11
- const family = addr.get_family();
12
- switch (family) {
13
- case Gio.SocketFamily.INVALID:
14
- return 0;
15
- case Gio.SocketFamily.IPV4:
16
- return 4;
17
- case Gio.SocketFamily.IPV6:
18
- return 6;
19
- }
8
+ if (typeof input !== "string") return 0;
9
+ const stripped = input.includes("%") ? input.split("%")[0] : input;
10
+ const addr = Gio.InetAddress.new_from_string(stripped);
11
+ if (!addr) return 0;
12
+ const family = addr.get_family();
13
+ switch (family) {
14
+ case Gio.SocketFamily.INVALID: return 0;
15
+ case Gio.SocketFamily.IPV4: return 4;
16
+ case Gio.SocketFamily.IPV6: return 6;
17
+ }
20
18
  }
19
+ /** Check if input is a valid IPv4 address. */
21
20
  function isIPv4(input) {
22
- return isIP(input) === 4;
21
+ return isIP(input) === 4;
23
22
  }
23
+ /** Check if input is a valid IPv6 address. */
24
24
  function isIPv6(input) {
25
- return isIP(input) === 6;
25
+ return isIP(input) === 6;
26
26
  }
27
+ /** Create a new TCP connection. */
27
28
  function createConnection(options, host, connectionListener) {
28
- const socket = new Socket2();
29
- return socket.connect(options, host, connectionListener);
29
+ const socket = new Socket();
30
+ return socket.connect(options, host, connectionListener);
30
31
  }
32
+ /** Alias for createConnection. */
31
33
  const connect = createConnection;
32
34
  function createServer(optionsOrListener, connectionListener) {
33
- return new Server2(optionsOrListener, connectionListener);
35
+ return new Server(optionsOrListener, connectionListener);
34
36
  }
35
- var index_default = {
36
- Socket: Socket2,
37
- Server: Server2,
38
- isIP,
39
- isIPv4,
40
- isIPv6,
41
- createConnection,
42
- connect,
43
- createServer
44
- };
45
- export {
46
- ListenOptions,
47
- Server,
48
- Socket,
49
- SocketConnectOptions,
50
- connect,
51
- createConnection,
52
- createServer,
53
- index_default as default,
54
- isIP,
55
- isIPv4,
56
- isIPv6
37
+ var src_default = {
38
+ Socket,
39
+ Server,
40
+ isIP,
41
+ isIPv4,
42
+ isIPv6,
43
+ createConnection,
44
+ connect,
45
+ createServer
57
46
  };
47
+
48
+ //#endregion
49
+ export { Server, Socket, connect, createConnection, createServer, src_default as default, isIP, isIPv4, isIPv6 };
package/lib/esm/server.js CHANGED
@@ -1,133 +1,141 @@
1
+ import { Socket } from "./socket.js";
1
2
  import Gio from "@girs/gio-2.0";
2
- import { EventEmitter } from "node:events";
3
3
  import { createNodeError, deferEmit, ensureMainLoop } from "@gjsify/utils";
4
- import { Socket } from "./socket.js";
5
- const _activeServers = /* @__PURE__ */ new Set();
6
- class Server extends EventEmitter {
7
- listening = false;
8
- maxConnections;
9
- allowHalfOpen;
10
- _service = null;
11
- _connections = /* @__PURE__ */ new Set();
12
- _address = null;
13
- constructor(optionsOrListener, connectionListener) {
14
- super();
15
- if (typeof optionsOrListener === "function") {
16
- connectionListener = optionsOrListener;
17
- this.allowHalfOpen = false;
18
- } else {
19
- this.allowHalfOpen = optionsOrListener?.allowHalfOpen ?? false;
20
- }
21
- if (connectionListener) {
22
- this.on("connection", connectionListener);
23
- }
24
- }
25
- listen(...args) {
26
- let port = 0;
27
- let host = "0.0.0.0";
28
- let backlog = 511;
29
- let callback;
30
- if (typeof args[0] === "object" && args[0] !== null && !Array.isArray(args[0])) {
31
- const opts = args[0];
32
- port = opts.port ?? 0;
33
- host = opts.host ?? "0.0.0.0";
34
- backlog = opts.backlog ?? 511;
35
- callback = args[1];
36
- } else {
37
- if (typeof args[0] === "number") port = args[0];
38
- for (let i = 1; i < args.length; i++) {
39
- if (typeof args[i] === "string") host = args[i];
40
- else if (typeof args[i] === "number") backlog = args[i];
41
- else if (typeof args[i] === "function") callback = args[i];
42
- }
43
- }
44
- if (callback) {
45
- this.once("listening", callback);
46
- }
47
- try {
48
- this._service = new Gio.SocketService();
49
- this._service.set_backlog(backlog);
50
- let actualPort;
51
- if (port === 0) {
52
- actualPort = this._service.add_any_inet_port(null);
53
- } else {
54
- this._service.add_inet_port(port, null);
55
- actualPort = port;
56
- }
57
- this._service.connect("incoming", (_service, connection) => {
58
- this._handleConnection(connection);
59
- return true;
60
- });
61
- this._service.start();
62
- ensureMainLoop();
63
- this.listening = true;
64
- _activeServers.add(this);
65
- const family = host.includes(":") ? "IPv6" : "IPv4";
66
- this._address = { port: actualPort, family, address: host };
67
- deferEmit(this, "listening");
68
- } catch (err) {
69
- const nodeErr = createNodeError(err, "listen", { address: host, port });
70
- deferEmit(this, "error", nodeErr);
71
- }
72
- return this;
73
- }
74
- _handleConnection(connection) {
75
- if (this.maxConnections && this._connections.size >= this.maxConnections) {
76
- try {
77
- connection.close(null);
78
- } catch {
79
- }
80
- return;
81
- }
82
- const socket = new Socket({ allowHalfOpen: this.allowHalfOpen });
83
- socket._setConnection(connection);
84
- socket._setupConnection({});
85
- this._connections.add(socket);
86
- socket.on("close", () => {
87
- this._connections.delete(socket);
88
- });
89
- this.emit("connection", socket);
90
- }
91
- /** Get the address the server is listening on. */
92
- address() {
93
- return this._address;
94
- }
95
- /** Close the server, stop accepting new connections. */
96
- close(callback) {
97
- if (callback) {
98
- this.once("close", callback);
99
- }
100
- if (!this._service || !this.listening) {
101
- setTimeout(() => {
102
- const err = new Error("Server is not running");
103
- err.code = "ERR_SERVER_NOT_RUNNING";
104
- this.emit("error", err);
105
- }, 0);
106
- return this;
107
- }
108
- this._service.stop();
109
- this._service.close();
110
- this._service = null;
111
- this.listening = false;
112
- _activeServers.delete(this);
113
- for (const socket of this._connections) {
114
- socket.destroy();
115
- }
116
- this._connections.clear();
117
- deferEmit(this, "close");
118
- return this;
119
- }
120
- /** Get the number of concurrent connections. */
121
- getConnections(callback) {
122
- callback(null, this._connections.size);
123
- }
124
- ref() {
125
- return this;
126
- }
127
- unref() {
128
- return this;
129
- }
130
- }
131
- export {
132
- Server
4
+ import { EventEmitter } from "node:events";
5
+
6
+ //#region src/server.ts
7
+ const _activeServers = new Set();
8
+ var Server = class extends EventEmitter {
9
+ listening = false;
10
+ maxConnections;
11
+ allowHalfOpen;
12
+ _service = null;
13
+ _connections = new Set();
14
+ _address = null;
15
+ constructor(optionsOrListener, connectionListener) {
16
+ super();
17
+ if (typeof optionsOrListener === "function") {
18
+ connectionListener = optionsOrListener;
19
+ this.allowHalfOpen = false;
20
+ } else {
21
+ this.allowHalfOpen = optionsOrListener?.allowHalfOpen ?? false;
22
+ }
23
+ if (connectionListener) {
24
+ this.on("connection", connectionListener);
25
+ }
26
+ }
27
+ listen(...args) {
28
+ let port = 0;
29
+ let host = "0.0.0.0";
30
+ let backlog = 511;
31
+ let callback;
32
+ if (typeof args[0] === "object" && args[0] !== null && !Array.isArray(args[0])) {
33
+ const opts = args[0];
34
+ port = opts.port ?? 0;
35
+ host = opts.host ?? "0.0.0.0";
36
+ backlog = opts.backlog ?? 511;
37
+ callback = args[1];
38
+ } else {
39
+ if (typeof args[0] === "number") port = args[0];
40
+ for (let i = 1; i < args.length; i++) {
41
+ if (typeof args[i] === "string") host = args[i];
42
+ else if (typeof args[i] === "number") backlog = args[i];
43
+ else if (typeof args[i] === "function") callback = args[i];
44
+ }
45
+ }
46
+ if (callback) {
47
+ this.once("listening", callback);
48
+ }
49
+ try {
50
+ this._service = new Gio.SocketService();
51
+ this._service.set_backlog(backlog);
52
+ let actualPort;
53
+ if (port === 0) {
54
+ actualPort = this._service.add_any_inet_port(null);
55
+ } else {
56
+ this._service.add_inet_port(port, null);
57
+ actualPort = port;
58
+ }
59
+ this._service.connect("incoming", (_service, connection) => {
60
+ this._handleConnection(connection);
61
+ return true;
62
+ });
63
+ this._service.start();
64
+ ensureMainLoop();
65
+ this.listening = true;
66
+ _activeServers.add(this);
67
+ const family = host.includes(":") ? "IPv6" : "IPv4";
68
+ this._address = {
69
+ port: actualPort,
70
+ family,
71
+ address: host
72
+ };
73
+ deferEmit(this, "listening");
74
+ } catch (err) {
75
+ const nodeErr = createNodeError(err, "listen", {
76
+ address: host,
77
+ port
78
+ });
79
+ deferEmit(this, "error", nodeErr);
80
+ }
81
+ return this;
82
+ }
83
+ _handleConnection(connection) {
84
+ if (this.maxConnections && this._connections.size >= this.maxConnections) {
85
+ try {
86
+ connection.close(null);
87
+ } catch {}
88
+ return;
89
+ }
90
+ const socket = new Socket({ allowHalfOpen: this.allowHalfOpen });
91
+ socket._setConnection(connection);
92
+ socket._setupConnection({});
93
+ this._connections.add(socket);
94
+ socket.on("close", () => {
95
+ this._connections.delete(socket);
96
+ });
97
+ this.emit("connection", socket);
98
+ }
99
+ /** Get the address the server is listening on. */
100
+ address() {
101
+ return this._address;
102
+ }
103
+ /** Close the server, stop accepting new connections. */
104
+ close(callback) {
105
+ if (callback) {
106
+ this.once("close", callback);
107
+ }
108
+ if (!this._service || !this.listening) {
109
+ setTimeout(() => {
110
+ const err = new Error("Server is not running");
111
+ err.code = "ERR_SERVER_NOT_RUNNING";
112
+ this.emit("error", err);
113
+ }, 0);
114
+ return this;
115
+ }
116
+ this._service.stop();
117
+ this._service.close();
118
+ this._service = null;
119
+ this.listening = false;
120
+ _activeServers.delete(this);
121
+ for (const socket of this._connections) {
122
+ socket.destroy();
123
+ }
124
+ this._connections.clear();
125
+ deferEmit(this, "close");
126
+ return this;
127
+ }
128
+ /** Get the number of concurrent connections. */
129
+ getConnections(callback) {
130
+ callback(null, this._connections.size);
131
+ }
132
+ ref() {
133
+ return this;
134
+ }
135
+ unref() {
136
+ return this;
137
+ }
133
138
  };
139
+
140
+ //#endregion
141
+ export { Server };