@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 +36 -44
- package/lib/esm/server.js +138 -130
- package/lib/esm/socket.js +350 -376
- package/lib/types/index.d.ts +3 -3
- package/package.json +9 -9
- package/src/index.ts +4 -4
- package/tsconfig.tsbuildinfo +1 -1
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
21
|
+
return isIP(input) === 4;
|
|
23
22
|
}
|
|
23
|
+
/** Check if input is a valid IPv6 address. */
|
|
24
24
|
function isIPv6(input) {
|
|
25
|
-
|
|
25
|
+
return isIP(input) === 6;
|
|
26
26
|
}
|
|
27
|
+
/** Create a new TCP connection. */
|
|
27
28
|
function createConnection(options, host, connectionListener) {
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
35
|
+
return new Server(optionsOrListener, connectionListener);
|
|
34
36
|
}
|
|
35
|
-
var
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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 {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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 };
|