1
2
3
4
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
|
'use strict';
const common = require('../common');
const {
ok,
strictEqual,
throws,
} = require('assert');
const {
SocketAddress,
} = require('net');
const {
MessageChannel,
} = require('worker_threads');
{
const sa = new SocketAddress();
strictEqual(sa.address, '127.0.0.1');
strictEqual(sa.port, 0);
strictEqual(sa.family, 'ipv4');
strictEqual(sa.flowlabel, 0);
const mc = new MessageChannel();
mc.port1.onmessage = common.mustCall(({ data }) => {
ok(SocketAddress.isSocketAddress(data));
strictEqual(data.address, '127.0.0.1');
strictEqual(data.port, 0);
strictEqual(data.family, 'ipv4');
strictEqual(data.flowlabel, 0);
mc.port1.close();
});
mc.port2.postMessage(sa);
}
{
const sa = new SocketAddress({});
strictEqual(sa.address, '127.0.0.1');
strictEqual(sa.port, 0);
strictEqual(sa.family, 'ipv4');
strictEqual(sa.flowlabel, 0);
}
{
const sa = new SocketAddress({
address: '123.123.123.123',
});
strictEqual(sa.address, '123.123.123.123');
strictEqual(sa.port, 0);
strictEqual(sa.family, 'ipv4');
strictEqual(sa.flowlabel, 0);
}
{
const sa = new SocketAddress({
address: '123.123.123.123',
port: 80
});
strictEqual(sa.address, '123.123.123.123');
strictEqual(sa.port, 80);
strictEqual(sa.family, 'ipv4');
strictEqual(sa.flowlabel, 0);
}
{
const sa = new SocketAddress({
family: 'ipv6'
});
strictEqual(sa.address, '::');
strictEqual(sa.port, 0);
strictEqual(sa.family, 'ipv6');
strictEqual(sa.flowlabel, 0);
}
{
const sa = new SocketAddress({
family: 'ipv6',
flowlabel: 1,
});
strictEqual(sa.address, '::');
strictEqual(sa.port, 0);
strictEqual(sa.family, 'ipv6');
strictEqual(sa.flowlabel, 1);
}
[1, false, 'hello'].forEach((i) => {
throws(() => new SocketAddress(i), {
code: 'ERR_INVALID_ARG_TYPE'
});
});
[1, false, {}, [], 'test'].forEach((family) => {
throws(() => new SocketAddress({ family }), {
code: 'ERR_INVALID_ARG_VALUE'
});
});
[1, false, {}, []].forEach((address) => {
throws(() => new SocketAddress({ address }), {
code: 'ERR_INVALID_ARG_TYPE'
});
});
[-1, false, {}, []].forEach((port) => {
throws(() => new SocketAddress({ port }), {
code: 'ERR_SOCKET_BAD_PORT'
});
});
throws(() => new SocketAddress({ flowlabel: -1 }), {
code: 'ERR_OUT_OF_RANGE'
});
|