Skip to content

Commit

Permalink
ping: stop using npm-registry-client
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Dec 10, 2018
1 parent 6e922ae commit 584613e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 19 deletions.
8 changes: 6 additions & 2 deletions lib/doctor/check-ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ var ping = require('../ping.js')
function checkPing (cb) {
var tracker = log.newItem('checkPing', 1)
tracker.info('checkPing', 'Pinging registry')
ping({}, true, (_err, pong, data, res) => {
cb(null, [res.statusCode, res.statusMessage])
ping({}, true, (err, pong) => {
if (err && err.code && err.code.match(/^E\d{3}$/)) {
return cb(null, [err.code.substr(1)])
} else {
cb(null, [200, 'OK'])
}
})
}

Expand Down
46 changes: 33 additions & 13 deletions lib/ping.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
var npm = require('./npm.js')
var output = require('./utils/output.js')
'use strict'

const npmConfig = require('./config/figgy-config.js')
const fetch = require('libnpm/fetch')
const figgyPudding = require('figgy-pudding')
const log = require('npmlog')
const npm = require('./npm.js')
const output = require('./utils/output.js')

const PingConfig = figgyPudding({
json: {},
registry: {}
})

module.exports = ping

Expand All @@ -10,18 +21,27 @@ function ping (args, silent, cb) {
cb = silent
silent = false
}
var registry = npm.config.get('registry')
if (!registry) return cb(new Error('no default registry set'))
var auth = npm.config.getCredentialsByURI(registry)

npm.registry.ping(registry, {auth: auth}, function (er, pong, data, res) {
if (!silent) {
if (er) {
output('Ping error: ' + er)
} else {
output('Ping success: ' + JSON.stringify(pong))
const opts = PingConfig(npmConfig())
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)}`)
}
}
cb(er, er ? null : pong, data, res)
})
}).nodeify(cb)
}
35 changes: 31 additions & 4 deletions test/tap/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,41 @@ test('npm ping', function (t) {
common.npm([
'ping',
'--registry', common.registry,
'--loglevel', 'silent',
'--loglevel', 'notice',
'--userconfig', outfile
], opts, function (err, code, stdout) {
], opts, function (err, code, stdout, stderr) {
s.close()
t.ifError(err, 'no error output')
t.ifError(err, 'command completed')
t.notOk(code, 'exited OK')

t.same(stdout, 'Ping success: ' + JSON.stringify(pingResponse) + '\n')
t.match(stderr, /PING/, 'ping notification output')
t.match(stderr, /PONG/, 'pong response output')
t.end()
})
})
})

test('npm ping --json', function (t) {
mr({ port: common.port, plugin: mocks }, function (err, s) {
if (err) throw err

common.npm([
'ping',
'--json',
'--registry', common.registry,
'--loglevel', 'notice',
'--userconfig', outfile
], opts, function (err, code, stdout, stderr) {
s.close()
t.ifError(err, 'command completed')
t.notOk(code, 'exited OK')

const json = JSON.parse(stdout.trim())
t.similar(json, {
registry: common.registry,
details: pingResponse
}, 'JSON info returned')
t.equal(typeof json.time, 'number', 'got a timestamp')
t.end()
})
})
Expand Down

0 comments on commit 584613e

Please sign in to comment.