-
Notifications
You must be signed in to change notification settings - Fork 108
/
ping.js
52 lines (48 loc) · 1.14 KB
/
ping.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
const { URL } = require('url')
const https = require('https')
const pingCount = 5
const paths = {
npm: `https://registry.npmjs.org/vue/latest`,
yarn: `https://registry.yarnpkg.com/vue/latest`,
tb: `https://registry.npm.taobao.org/vue/latest`
}
const ping = url => {
return new Promise((resolve, reject) => {
const start = Date.now()
const { hostname, pathname } = new URL(url)
const req = https.request({
hostname,
path: pathname
}, () => {
process
.stdout
.write('.')
resolve(Date.now() - start)
})
req.on('error', reject)
req.end()
})
}
const pingX = (registry, times) => {
return ping(paths[registry]).then(latency => {
return times > 1
? pingX(registry, times - 1).then(results => [
latency, ...results
])
: [latency]
})
}
const pings = Object
.keys(paths)
.map(registry => {
return pingX(registry, pingCount).then(results => ( { registry, results } ))
})
Promise
.all(pings)
.then(results => {
console.log()
console.log(results)
})
.catch(err => {
console.log(err)
})