-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathport.js
128 lines (109 loc) · 2.46 KB
/
port.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const fs = require('fs'),
LusterPortError = require('./errors').LusterPortError,
UNIX_SOCKET_MASK = '*';
/**
* @param {String|Number} value
* @returns {Boolean}
* @private
*/
function isUnixSocket(value) {
return isNaN(value);
}
/**
* @constructor
* @class Port
* @param {String|Number} value
*/
class Port {
constructor(value) {
this.value = value;
}
/**
* @memberOf {Port}
* @property {String} family
* @public
* @readonly
*/
get family() {
return isUnixSocket(this.value) ? Port.UNIX : Port.INET;
}
/**
* @param {*} port
* @returns {Boolean}
* @public
*/
isEqualTo(port) {
if (!(port instanceof Port)) {
return false;
}
return this.value === port.value;
}
/**
* @param {Number|String} [it=1]
* @returns {Port}
* @public
*/
next(it) {
if (typeof it === 'undefined') {
it = 1;
}
const newVal = this.family === Port.UNIX ?
this.value.replace(UNIX_SOCKET_MASK, it.toString()) :
Number(this.value) + it;
return new Port(newVal);
}
/**
* @param {Error} [err]
* @param {Function} cb
*/
unlink(err, cb) {
if (!cb && typeof err === 'function') {
cb = err;
err = undefined;
}
if (err) {
cb(LusterPortError
.createError(LusterPortError.CODES.UNKNOWN_ERROR, err));
return;
}
const value = this.value;
if (this.family !== Port.UNIX) {
cb(LusterPortError
.createError(LusterPortError.CODES.NOT_UNIX_SOCKET)
.bind({value}));
return;
}
fs.unlink(value, err => {
if (err && err.code !== 'ENOENT') {
cb(LusterPortError
.createError(LusterPortError.CODES.CAN_NOT_UNLINK_UNIX_SOCKET, err)
.bind({socketPath: value}));
return;
}
cb();
});
}
toString() {
return this.value;
}
valueOf() {
return this.value;
}
/**
* @property {String} UNIX
* @memberOf {Port}
* @readonly
*/
static get UNIX() {
return 'unix';
}
/**
* @property {String} INET
* @memberOf {Port}
* @readonly
*/
static get INET() {
return 'inet';
}
}
module.exports = Port;