Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain120 authored Feb 2, 2018
1 parent 74d7873 commit e6ed0a4
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
})

0 comments on commit e6ed0a4

Please sign in to comment.