From 584613ea8ff94b927db4957e5647504b30ca2b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Thu, 5 Apr 2018 21:29:23 -0700 Subject: [PATCH] ping: stop using npm-registry-client --- lib/doctor/check-ping.js | 8 +++++-- lib/ping.js | 46 ++++++++++++++++++++++++++++------------ test/tap/ping.js | 35 ++++++++++++++++++++++++++---- 3 files changed, 70 insertions(+), 19 deletions(-) diff --git a/lib/doctor/check-ping.js b/lib/doctor/check-ping.js index e7e82902a7165..70db255480c37 100644 --- a/lib/doctor/check-ping.js +++ b/lib/doctor/check-ping.js @@ -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']) + } }) } diff --git a/lib/ping.js b/lib/ping.js index 13f390397ce18..3023bab00e994 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -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 @@ -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) } diff --git a/test/tap/ping.js b/test/tap/ping.js index 76d115a482343..3562f25a3be97 100644 --- a/test/tap/ping.js +++ b/test/tap/ping.js @@ -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() }) })