@gjsify/net 0.0.4 → 0.1.0
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/README.md +29 -1
- package/lib/esm/index.js +34 -3
- package/lib/esm/server.js +133 -0
- package/lib/esm/socket.js +332 -0
- package/lib/types/index.d.ts +30 -0
- package/lib/types/server.d.ts +40 -0
- package/lib/types/socket.d.ts +78 -0
- package/package.json +23 -22
- package/src/error.spec.ts +169 -0
- package/src/extended.spec.ts +413 -0
- package/src/index.spec.ts +1072 -80
- package/src/index.ts +47 -12
- package/src/server.spec.ts +303 -0
- package/src/server.ts +186 -0
- package/src/socket.ts +404 -0
- package/src/test.mts +6 -2
- package/src/timeout.spec.ts +464 -0
- package/tsconfig.json +22 -9
- package/tsconfig.tsbuildinfo +1 -0
- package/lib/cjs/index.js +0 -26
- package/test.gjs.mjs +0 -34766
- package/test.node.mjs +0 -366
- package/tsconfig.types.json +0 -8
package/package.json
CHANGED
|
@@ -1,32 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/net",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Node.js net module for Gjs",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "lib/cjs/index.js",
|
|
7
6
|
"module": "lib/esm/index.js",
|
|
7
|
+
"types": "lib/types/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
"default": "./lib/cjs/index.js"
|
|
17
|
-
}
|
|
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"
|
|
18
16
|
}
|
|
19
17
|
},
|
|
20
18
|
"scripts": {
|
|
21
|
-
"clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo || exit 0",
|
|
22
|
-
"
|
|
23
|
-
"build": "yarn
|
|
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",
|
|
24
22
|
"build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
|
|
25
|
-
"build:types": "tsc
|
|
23
|
+
"build:types": "tsc",
|
|
26
24
|
"build:test": "yarn build:test:gjs && yarn build:test:node",
|
|
27
25
|
"build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
|
|
28
26
|
"build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
|
|
29
|
-
"test": "yarn
|
|
27
|
+
"test": "yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
|
|
30
28
|
"test:gjs": "gjs -m test.gjs.mjs",
|
|
31
29
|
"test:node": "node test.node.mjs"
|
|
32
30
|
},
|
|
@@ -36,14 +34,17 @@
|
|
|
36
34
|
"net"
|
|
37
35
|
],
|
|
38
36
|
"devDependencies": {
|
|
39
|
-
"@gjsify/
|
|
40
|
-
"@gjsify/
|
|
41
|
-
"@
|
|
42
|
-
"
|
|
43
|
-
"@gjsify/unit": "^0.0.4",
|
|
44
|
-
"@types/node": "^20.10.5"
|
|
37
|
+
"@gjsify/cli": "^0.1.0",
|
|
38
|
+
"@gjsify/unit": "^0.1.0",
|
|
39
|
+
"@types/node": "^25.5.0",
|
|
40
|
+
"typescript": "^6.0.2"
|
|
45
41
|
},
|
|
46
42
|
"dependencies": {
|
|
47
|
-
"@girs/gio-2.0": "2.
|
|
43
|
+
"@girs/gio-2.0": "^2.88.0-4.0.0-beta.42",
|
|
44
|
+
"@girs/glib-2.0": "^2.88.0-4.0.0-beta.42",
|
|
45
|
+
"@gjsify/buffer": "^0.1.0",
|
|
46
|
+
"@gjsify/events": "^0.1.0",
|
|
47
|
+
"@gjsify/stream": "^0.1.0",
|
|
48
|
+
"@gjsify/utils": "^0.1.0"
|
|
48
49
|
}
|
|
49
50
|
}
|
|
@@ -0,0 +1,169 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
// Extended net tests — isIP edge cases, Socket/Server properties, createConnection
|
|
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
|
+
// ===================== isIP comprehensive =====================
|
|
11
|
+
await describe('net.isIP comprehensive', async () => {
|
|
12
|
+
// Valid IPv4
|
|
13
|
+
await it('should return 4 for 0.0.0.0', async () => {
|
|
14
|
+
expect(net.isIP('0.0.0.0')).toBe(4);
|
|
15
|
+
});
|
|
16
|
+
await it('should return 4 for 255.255.255.255', async () => {
|
|
17
|
+
expect(net.isIP('255.255.255.255')).toBe(4);
|
|
18
|
+
});
|
|
19
|
+
await it('should return 4 for 192.168.1.1', async () => {
|
|
20
|
+
expect(net.isIP('192.168.1.1')).toBe(4);
|
|
21
|
+
});
|
|
22
|
+
await it('should return 4 for 10.0.0.1', async () => {
|
|
23
|
+
expect(net.isIP('10.0.0.1')).toBe(4);
|
|
24
|
+
});
|
|
25
|
+
await it('should return 4 for 1.1.1.1', async () => {
|
|
26
|
+
expect(net.isIP('1.1.1.1')).toBe(4);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Invalid IPv4
|
|
30
|
+
await it('should return 0 for 256.0.0.1', async () => {
|
|
31
|
+
expect(net.isIP('256.0.0.1')).toBe(0);
|
|
32
|
+
});
|
|
33
|
+
await it('should return 0 for 1.2.3', async () => {
|
|
34
|
+
expect(net.isIP('1.2.3')).toBe(0);
|
|
35
|
+
});
|
|
36
|
+
await it('should return 0 for 1.2.3.4.5', async () => {
|
|
37
|
+
expect(net.isIP('1.2.3.4.5')).toBe(0);
|
|
38
|
+
});
|
|
39
|
+
await it('should return 0 for leading zeros in IPv4', async () => {
|
|
40
|
+
expect(net.isIP('01.02.03.04')).toBe(0);
|
|
41
|
+
});
|
|
42
|
+
await it('should return 0 for negative octets', async () => {
|
|
43
|
+
expect(net.isIP('-1.0.0.0')).toBe(0);
|
|
44
|
+
});
|
|
45
|
+
await it('should return 0 for empty octets', async () => {
|
|
46
|
+
expect(net.isIP('1..3.4')).toBe(0);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Valid IPv6
|
|
50
|
+
await it('should return 6 for ::1', async () => {
|
|
51
|
+
expect(net.isIP('::1')).toBe(6);
|
|
52
|
+
});
|
|
53
|
+
await it('should return 6 for ::', async () => {
|
|
54
|
+
expect(net.isIP('::')).toBe(6);
|
|
55
|
+
});
|
|
56
|
+
await it('should return 6 for full IPv6', async () => {
|
|
57
|
+
expect(net.isIP('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(6);
|
|
58
|
+
});
|
|
59
|
+
await it('should return 6 for compressed IPv6', async () => {
|
|
60
|
+
expect(net.isIP('2001:db8::1')).toBe(6);
|
|
61
|
+
});
|
|
62
|
+
await it('should return 6 for fe80::1', async () => {
|
|
63
|
+
expect(net.isIP('fe80::1')).toBe(6);
|
|
64
|
+
});
|
|
65
|
+
await it('should return 6 for IPv4-mapped IPv6', async () => {
|
|
66
|
+
expect(net.isIP('::ffff:192.168.1.1')).toBe(6);
|
|
67
|
+
});
|
|
68
|
+
await it('should return 6 for loopback full', async () => {
|
|
69
|
+
expect(net.isIP('0000:0000:0000:0000:0000:0000:0000:0001')).toBe(6);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Invalid IPv6
|
|
73
|
+
await it('should return 0 for triple colon', async () => {
|
|
74
|
+
expect(net.isIP(':::1')).toBe(0);
|
|
75
|
+
});
|
|
76
|
+
await it('should return 0 for too many groups', async () => {
|
|
77
|
+
expect(net.isIP('1:2:3:4:5:6:7:8:9')).toBe(0);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Non-IP strings
|
|
81
|
+
await it('should return 0 for empty string', async () => {
|
|
82
|
+
expect(net.isIP('')).toBe(0);
|
|
83
|
+
});
|
|
84
|
+
await it('should return 0 for hostname', async () => {
|
|
85
|
+
expect(net.isIP('example.com')).toBe(0);
|
|
86
|
+
});
|
|
87
|
+
await it('should return 0 for random text', async () => {
|
|
88
|
+
expect(net.isIP('not an ip')).toBe(0);
|
|
89
|
+
});
|
|
90
|
+
await it('should return 0 for number input', async () => {
|
|
91
|
+
expect(net.isIP(123 as any)).toBe(0);
|
|
92
|
+
});
|
|
93
|
+
await it('should return 0 for null', async () => {
|
|
94
|
+
expect(net.isIP(null as any)).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
await it('should return 0 for undefined', async () => {
|
|
97
|
+
expect(net.isIP(undefined as any)).toBe(0);
|
|
98
|
+
});
|
|
99
|
+
await it('should return 0 for boolean', async () => {
|
|
100
|
+
expect(net.isIP(true as any)).toBe(0);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// ===================== isIPv4 comprehensive =====================
|
|
105
|
+
await describe('net.isIPv4 comprehensive', async () => {
|
|
106
|
+
await it('should return true for valid IPv4', async () => {
|
|
107
|
+
expect(net.isIPv4('127.0.0.1')).toBe(true);
|
|
108
|
+
expect(net.isIPv4('0.0.0.0')).toBe(true);
|
|
109
|
+
expect(net.isIPv4('255.255.255.255')).toBe(true);
|
|
110
|
+
});
|
|
111
|
+
await it('should return false for IPv6', async () => {
|
|
112
|
+
expect(net.isIPv4('::1')).toBe(false);
|
|
113
|
+
expect(net.isIPv4('::')).toBe(false);
|
|
114
|
+
});
|
|
115
|
+
await it('should return false for invalid', async () => {
|
|
116
|
+
expect(net.isIPv4('256.0.0.1')).toBe(false);
|
|
117
|
+
expect(net.isIPv4('')).toBe(false);
|
|
118
|
+
expect(net.isIPv4('hello')).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
await it('should return false for non-string', async () => {
|
|
121
|
+
expect(net.isIPv4(null as any)).toBe(false);
|
|
122
|
+
expect(net.isIPv4(undefined as any)).toBe(false);
|
|
123
|
+
expect(net.isIPv4(42 as any)).toBe(false);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// ===================== isIPv6 comprehensive =====================
|
|
128
|
+
await describe('net.isIPv6 comprehensive', async () => {
|
|
129
|
+
await it('should return true for valid IPv6', async () => {
|
|
130
|
+
expect(net.isIPv6('::1')).toBe(true);
|
|
131
|
+
expect(net.isIPv6('::')).toBe(true);
|
|
132
|
+
expect(net.isIPv6('fe80::1')).toBe(true);
|
|
133
|
+
expect(net.isIPv6('2001:db8::1')).toBe(true);
|
|
134
|
+
});
|
|
135
|
+
await it('should return false for IPv4', async () => {
|
|
136
|
+
expect(net.isIPv6('127.0.0.1')).toBe(false);
|
|
137
|
+
expect(net.isIPv6('0.0.0.0')).toBe(false);
|
|
138
|
+
});
|
|
139
|
+
await it('should return false for invalid', async () => {
|
|
140
|
+
expect(net.isIPv6(':::1')).toBe(false);
|
|
141
|
+
expect(net.isIPv6('')).toBe(false);
|
|
142
|
+
expect(net.isIPv6('hello')).toBe(false);
|
|
143
|
+
});
|
|
144
|
+
await it('should return false for non-string', async () => {
|
|
145
|
+
expect(net.isIPv6(null as any)).toBe(false);
|
|
146
|
+
expect(net.isIPv6(undefined as any)).toBe(false);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// ===================== Socket extended =====================
|
|
151
|
+
await describe('net.Socket extended', async () => {
|
|
152
|
+
await it('should be constructable without args', async () => {
|
|
153
|
+
const socket = new net.Socket();
|
|
154
|
+
expect(socket).toBeDefined();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
await it('should have Duplex stream methods', async () => {
|
|
158
|
+
const socket = new net.Socket();
|
|
159
|
+
expect(typeof socket.write).toBe('function');
|
|
160
|
+
expect(typeof socket.end).toBe('function');
|
|
161
|
+
expect(typeof socket.destroy).toBe('function');
|
|
162
|
+
expect(typeof socket.pipe).toBe('function');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
await it('should have EventEmitter methods', async () => {
|
|
166
|
+
const socket = new net.Socket();
|
|
167
|
+
expect(typeof socket.on).toBe('function');
|
|
168
|
+
expect(typeof socket.emit).toBe('function');
|
|
169
|
+
expect(typeof socket.once).toBe('function');
|
|
170
|
+
expect(typeof socket.removeListener).toBe('function');
|
|
171
|
+
expect(typeof socket.removeAllListeners).toBe('function');
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
await it('bytesRead should default to 0', async () => {
|
|
175
|
+
const socket = new net.Socket();
|
|
176
|
+
expect(socket.bytesRead).toBe(0);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
await it('bytesWritten should default to 0', async () => {
|
|
180
|
+
const socket = new net.Socket();
|
|
181
|
+
expect(socket.bytesWritten).toBe(0);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
await it('connecting should be false initially', async () => {
|
|
185
|
+
const socket = new net.Socket();
|
|
186
|
+
expect(socket.connecting).toBe(false);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
await it('pending should be true initially', async () => {
|
|
190
|
+
const socket = new net.Socket();
|
|
191
|
+
expect(socket.pending).toBe(true);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
await it('destroyed should be false initially', async () => {
|
|
195
|
+
const socket = new net.Socket();
|
|
196
|
+
expect(socket.destroyed).toBe(false);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
await it('should have setTimeout method', async () => {
|
|
200
|
+
const socket = new net.Socket();
|
|
201
|
+
expect(typeof socket.setTimeout).toBe('function');
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
await it('should have setKeepAlive method', async () => {
|
|
205
|
+
const socket = new net.Socket();
|
|
206
|
+
expect(typeof socket.setKeepAlive).toBe('function');
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
await it('should have setNoDelay method', async () => {
|
|
210
|
+
const socket = new net.Socket();
|
|
211
|
+
expect(typeof socket.setNoDelay).toBe('function');
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
await it('should have ref/unref methods', async () => {
|
|
215
|
+
const socket = new net.Socket();
|
|
216
|
+
expect(typeof socket.ref).toBe('function');
|
|
217
|
+
expect(typeof socket.unref).toBe('function');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
await it('should have connect method', async () => {
|
|
221
|
+
const socket = new net.Socket();
|
|
222
|
+
expect(typeof socket.connect).toBe('function');
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
await it('should have address method', async () => {
|
|
226
|
+
const socket = new net.Socket();
|
|
227
|
+
expect(typeof socket.address).toBe('function');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
await it('remoteAddress should be undefined when not connected', async () => {
|
|
231
|
+
const socket = new net.Socket();
|
|
232
|
+
expect(socket.remoteAddress).toBeUndefined();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
await it('remotePort should be undefined when not connected', async () => {
|
|
236
|
+
const socket = new net.Socket();
|
|
237
|
+
expect(socket.remotePort).toBeUndefined();
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
await it('remoteFamily should be undefined when not connected', async () => {
|
|
241
|
+
const socket = new net.Socket();
|
|
242
|
+
expect(socket.remoteFamily).toBeUndefined();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
await it('localAddress should be undefined when not connected', async () => {
|
|
246
|
+
const socket = new net.Socket();
|
|
247
|
+
expect(socket.localAddress).toBeUndefined();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
await it('localPort should be undefined when not connected', async () => {
|
|
251
|
+
const socket = new net.Socket();
|
|
252
|
+
expect(socket.localPort).toBeUndefined();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
await it('setTimeout should not throw', async () => {
|
|
256
|
+
const socket = new net.Socket();
|
|
257
|
+
expect(() => socket.setTimeout(1000)).not.toThrow();
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
await it('setKeepAlive should not throw', async () => {
|
|
261
|
+
const socket = new net.Socket();
|
|
262
|
+
expect(() => socket.setKeepAlive(true)).not.toThrow();
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
await it('setNoDelay should not throw', async () => {
|
|
266
|
+
const socket = new net.Socket();
|
|
267
|
+
expect(() => socket.setNoDelay(true)).not.toThrow();
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
await it('ref should return socket (chainable)', async () => {
|
|
271
|
+
const socket = new net.Socket();
|
|
272
|
+
expect(socket.ref()).toBe(socket);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
await it('unref should return socket (chainable)', async () => {
|
|
276
|
+
const socket = new net.Socket();
|
|
277
|
+
expect(socket.unref()).toBe(socket);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
await it('should accept options with fd', async () => {
|
|
281
|
+
const socket = new net.Socket({ fd: undefined });
|
|
282
|
+
expect(socket).toBeDefined();
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
await it('should accept allowHalfOpen option', async () => {
|
|
286
|
+
const socket = new net.Socket({ allowHalfOpen: true });
|
|
287
|
+
expect(socket).toBeDefined();
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
await it('should accept readable/writable options', async () => {
|
|
291
|
+
const socket = new net.Socket({ readable: true, writable: true });
|
|
292
|
+
expect(socket).toBeDefined();
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
// ===================== Server extended =====================
|
|
297
|
+
await describe('net.Server extended', async () => {
|
|
298
|
+
await it('should be constructable', async () => {
|
|
299
|
+
const server = new net.Server();
|
|
300
|
+
expect(server).toBeDefined();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
await it('should accept connectionListener', async () => {
|
|
304
|
+
const server = new net.Server((_socket) => {});
|
|
305
|
+
expect(server).toBeDefined();
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
await it('should accept options', async () => {
|
|
309
|
+
const server = new net.Server({ allowHalfOpen: true });
|
|
310
|
+
expect(server).toBeDefined();
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
await it('should have listen method', async () => {
|
|
314
|
+
const server = new net.Server();
|
|
315
|
+
expect(typeof server.listen).toBe('function');
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
await it('should have close method', async () => {
|
|
319
|
+
const server = new net.Server();
|
|
320
|
+
expect(typeof server.close).toBe('function');
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
await it('should have address method', async () => {
|
|
324
|
+
const server = new net.Server();
|
|
325
|
+
expect(typeof server.address).toBe('function');
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
await it('should have getConnections method', async () => {
|
|
329
|
+
const server = new net.Server();
|
|
330
|
+
expect(typeof server.getConnections).toBe('function');
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
await it('should have ref/unref methods', async () => {
|
|
334
|
+
const server = new net.Server();
|
|
335
|
+
expect(typeof server.ref).toBe('function');
|
|
336
|
+
expect(typeof server.unref).toBe('function');
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
await it('listening should be false initially', async () => {
|
|
340
|
+
const server = new net.Server();
|
|
341
|
+
expect(server.listening).toBe(false);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
await it('should have maxConnections property', async () => {
|
|
345
|
+
const server = new net.Server();
|
|
346
|
+
expect(typeof server.maxConnections === 'number' || server.maxConnections === undefined).toBe(true);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
await it('should be an EventEmitter', async () => {
|
|
350
|
+
const server = new net.Server();
|
|
351
|
+
expect(typeof server.on).toBe('function');
|
|
352
|
+
expect(typeof server.emit).toBe('function');
|
|
353
|
+
expect(typeof server.once).toBe('function');
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
await it('address should return null when not listening', async () => {
|
|
357
|
+
const server = new net.Server();
|
|
358
|
+
expect(server.address()).toBeNull();
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
// ===================== Module exports =====================
|
|
363
|
+
await describe('net module exports', async () => {
|
|
364
|
+
await it('should export Socket', async () => {
|
|
365
|
+
expect(typeof net.Socket).toBe('function');
|
|
366
|
+
});
|
|
367
|
+
await it('should export Server', async () => {
|
|
368
|
+
expect(typeof net.Server).toBe('function');
|
|
369
|
+
});
|
|
370
|
+
await it('should export createServer', async () => {
|
|
371
|
+
expect(typeof net.createServer).toBe('function');
|
|
372
|
+
});
|
|
373
|
+
await it('should export createConnection', async () => {
|
|
374
|
+
expect(typeof net.createConnection).toBe('function');
|
|
375
|
+
});
|
|
376
|
+
await it('should export connect', async () => {
|
|
377
|
+
expect(typeof net.connect).toBe('function');
|
|
378
|
+
});
|
|
379
|
+
await it('should export isIP', async () => {
|
|
380
|
+
expect(typeof net.isIP).toBe('function');
|
|
381
|
+
});
|
|
382
|
+
await it('should export isIPv4', async () => {
|
|
383
|
+
expect(typeof net.isIPv4).toBe('function');
|
|
384
|
+
});
|
|
385
|
+
await it('should export isIPv6', async () => {
|
|
386
|
+
expect(typeof net.isIPv6).toBe('function');
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
// ===================== createServer =====================
|
|
391
|
+
await describe('net.createServer', async () => {
|
|
392
|
+
await it('should return a Server', async () => {
|
|
393
|
+
const server = net.createServer();
|
|
394
|
+
expect(server).toBeDefined();
|
|
395
|
+
expect(server instanceof net.Server).toBe(true);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
await it('should accept options', async () => {
|
|
399
|
+
const server = net.createServer({ allowHalfOpen: true });
|
|
400
|
+
expect(server).toBeDefined();
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
await it('should accept connectionListener', async () => {
|
|
404
|
+
const server = net.createServer((_socket) => {});
|
|
405
|
+
expect(server).toBeDefined();
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
await it('should accept options and connectionListener', async () => {
|
|
409
|
+
const server = net.createServer({ allowHalfOpen: false }, (_socket) => {});
|
|
410
|
+
expect(server).toBeDefined();
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
};
|