FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ionic-cli/packages/@ionic/utils-network/src/index.ts at develop · reslear/ionic-cli · GitHub
reslear
ionic-cli
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ionic-cli
/
packages
/
@ionic
/
utils-network
/
src
/
index.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
177 lines (149 loc) · 4.54 KB
Breadcrumbs
ionic-cli
/
packages
/
@ionic
/
utils-network
/
src
/
index.ts
Copy path
File metadata and controls
177 lines (149 loc) · 4.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import
{
debug
as
Debug
}
from
'debug'
;
import
*
as
net
from
'net'
;
import
os
from
'os'
;
const
debug
=
Debug
(
'ionic:utils-network'
)
;
export
const
DEFAULT_ADDRESSES
:
readonly
string
[
]
=
getDefaultAddresses
(
)
;
export
type
NetworkInterface
=
{
device
:
string
;
}
&
os
.
NetworkInterfaceInfo
;
function
getDefaultAddresses
(
)
:
string
[
]
{
const
addresses
:
string
[
]
=
[
'0.0.0.0'
]
;
try
{
const
networkInterfaces
=
os
.
networkInterfaces
(
)
;
for
(
const
device
of
Object
.
keys
(
networkInterfaces
)
)
{
const
networkInterface
=
networkInterfaces
[
device
]
;
if
(
networkInterface
)
{
addresses
.
push
(
...
networkInterface
.
map
(
i
=>
i
.
address
)
)
;
}
}
}
catch
(
e
:
any
)
{
// swallow
}
return
addresses
;
}
export
function
getExternalIPv4Interfaces
(
)
:
NetworkInterface
[
]
{
const
networkInterfaces
=
os
.
networkInterfaces
(
)
;
const
devices
:
NetworkInterface
[
]
=
[
]
;
for
(
const
device
of
Object
.
keys
(
networkInterfaces
)
)
{
const
networkInterface
=
networkInterfaces
[
device
]
;
if
(
networkInterface
)
{
for
(
const
networkAddress
of
networkInterface
)
{
if
(
!
networkAddress
.
internal
&&
networkAddress
.
family
===
'IPv4'
)
{
devices
.
push
(
{
device
,
...
networkAddress
}
)
;
}
}
}
}
return
devices
;
}
/**
* Attempts to locate a port number starting from `port` and incrementing by 1.
*
* This function looks through all internal network interfaces, attempting
* host/port combinations until it finds an available port on all interfaces.
*
*
@param
port The port at which to start checking.
*/
export
async
function
findClosestOpenPort
(
port
:
number
)
:
Promise
<
number
>
{
async
function
t
(
portToCheck
:
number
)
:
Promise
<
number
>
{
if
(
await
isPortAvailable
(
portToCheck
)
)
{
return
portToCheck
;
}
return
t
(
portToCheck
+
1
)
;
}
return
t
(
port
)
;
}
/**
* Checks whether a port is open or closed.
*
* This function looks through all internal network interfaces, checking
* whether all host/port combinations are open. If one or more is not, the port
* is not available.
*/
export
async
function
isPortAvailable
(
port
:
number
)
:
Promise
<
boolean
>
{
let
available
=
true
;
for
(
const
address
of
DEFAULT_ADDRESSES
)
{
try
{
debug
(
'checking for open port on %s:%d'
,
address
,
port
)
;
available
=
await
isPortAvailableForHost
(
address
,
port
)
;
if
(
!
available
)
{
return
false
;
}
}
catch
(
e
:
any
)
{
debug
(
'error while checking %s:%d: %o'
,
address
,
port
,
e
)
;
}
}
return
available
;
}
export
function
isPortAvailableForHost
(
host
:
string
,
port
:
number
)
:
Promise
<
boolean
>
{
return
new
Promise
<
boolean
>
(
(
resolve
,
reject
)
=>
{
const
tester
=
net
.
createServer
(
)
.
once
(
'error'
,
(
err
:
any
)
=>
{
if
(
err
.
code
===
'EADDRINUSE'
)
{
resolve
(
false
)
;
// host/port in use
}
else
{
reject
(
err
)
;
}
}
)
.
once
(
'listening'
,
(
)
=>
{
tester
.
once
(
'close'
,
(
)
=>
{
resolve
(
true
)
;
// found available host/port
}
)
.
close
(
)
;
}
)
.
on
(
'error'
,
(
err
:
any
)
=>
{
reject
(
err
)
;
}
)
.
listen
(
port
,
host
)
;
}
)
;
}
/**
* Continuously attempt TCP connections.
*
* By default, this function will only ever resolve once a host is connectable.
* This behavior can be changed with the `timeout` option, which resolves with
* `false` if the timeout is reached.
*
*
@param
host The host to connect to.
*
@param
port The port to connect to.
*
@param
options.timeout Optionally define a timeout, in milliseconds.
*/
export
async
function
isHostConnectable
(
host
:
string
,
port
:
number
,
{
timeout
}
:
{
timeout
?:
number
;
}
=
{
}
)
:
Promise
<
boolean
>
{
const
tryConnect
=
async
(
)
=>
{
return
new
Promise
<
boolean
>
(
(
resolve
,
reject
)
=>
{
if
(
host
===
'0.0.0.0'
)
{
host
=
'127.0.0.1'
;
}
const
sock
=
net
.
connect
(
{
port
,
host
}
)
;
sock
.
on
(
'connect'
,
(
)
=>
{
sock
.
destroy
(
)
;
resolve
(
true
)
;
}
)
;
sock
.
on
(
'error'
,
err
=>
{
reject
(
err
)
;
}
)
;
}
)
;
}
;
return
new
Promise
<
boolean
>
(
async
resolve
=>
{
let
timer
:
NodeJS
.
Timer
|
undefined
;
let
resolved
=
false
;
if
(
timeout
)
{
timer
=
setTimeout
(
(
)
=>
{
debug
(
'Timeout of %dms reached while waiting for host connectivity'
,
timeout
)
;
resolve
(
false
)
;
resolved
=
true
;
}
,
timeout
)
;
timer
.
unref
(
)
;
}
while
(
!
resolved
)
{
try
{
await
tryConnect
(
)
;
if
(
timer
)
{
clearTimeout
(
timer
)
;
}
resolve
(
true
)
;
resolved
=
true
;
}
catch
(
e
:
any
)
{
// try again
}
}
}
)
;
}
Back
|
FazBrowse Home
|
New Git URL