-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.js
40 lines (35 loc) · 983 Bytes
/
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
'use strict'
const fetch = require('npm-registry-fetch')
const log = require('npmlog')
const npm = require('./npm.js')
const output = require('./utils/output.js')
module.exports = ping
ping.usage = 'npm ping\nping registry'
function ping (args, silent, cb) {
if (typeof cb !== 'function') {
cb = silent
silent = false
}
const opts = npm.flatOptions
const registry = opts.registry
log.notice('PING', registry)
const start = Date.now()
return fetch('/-/ping?write=true', opts).then(
res => res.json().catch(() => ({}))
).then(details => {
if (silent) {
} else {
const time = Date.now() - start
log.notice('PONG', `${time / 1000}ms`)
if (npm.config.get('json')) {
output(JSON.stringify({
registry,
time,
details
}, null, 2))
} else if (Object.keys(details).length) {
log.notice('PONG', `${JSON.stringify(details, null, 2)}`)
}
}
}).then(() => cb(), cb)
}