Skip to content

Commit 584613e

Browse files
committed
ping: stop using npm-registry-client
1 parent 6e922ae commit 584613e

3 files changed

Lines changed: 70 additions & 19 deletions

File tree

lib/doctor/check-ping.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ var ping = require('../ping.js')
44
function checkPing (cb) {
55
var tracker = log.newItem('checkPing', 1)
66
tracker.info('checkPing', 'Pinging registry')
7-
ping({}, true, (_err, pong, data, res) => {
8-
cb(null, [res.statusCode, res.statusMessage])
7+
ping({}, true, (err, pong) => {
8+
if (err && err.code && err.code.match(/^E\d{3}$/)) {
9+
return cb(null, [err.code.substr(1)])
10+
} else {
11+
cb(null, [200, 'OK'])
12+
}
913
})
1014
}
1115

lib/ping.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
var npm = require('./npm.js')
2-
var output = require('./utils/output.js')
1+
'use strict'
2+
3+
const npmConfig = require('./config/figgy-config.js')
4+
const fetch = require('libnpm/fetch')
5+
const figgyPudding = require('figgy-pudding')
6+
const log = require('npmlog')
7+
const npm = require('./npm.js')
8+
const output = require('./utils/output.js')
9+
10+
const PingConfig = figgyPudding({
11+
json: {},
12+
registry: {}
13+
})
314

415
module.exports = ping
516

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

17-
npm.registry.ping(registry, {auth: auth}, function (er, pong, data, res) {
18-
if (!silent) {
19-
if (er) {
20-
output('Ping error: ' + er)
21-
} else {
22-
output('Ping success: ' + JSON.stringify(pong))
25+
const opts = PingConfig(npmConfig())
26+
const registry = opts.registry
27+
log.notice('PING', registry)
28+
const start = Date.now()
29+
return fetch('/-/ping?write=true', opts).then(
30+
res => res.json().catch(() => ({}))
31+
).then(details => {
32+
if (silent) {
33+
} else {
34+
const time = Date.now() - start
35+
log.notice('PONG', `${time / 1000}ms`)
36+
if (npm.config.get('json')) {
37+
output(JSON.stringify({
38+
registry,
39+
time,
40+
details
41+
}, null, 2))
42+
} else if (Object.keys(details).length) {
43+
log.notice('PONG', `${JSON.stringify(details, null, 2)}`)
2344
}
2445
}
25-
cb(er, er ? null : pong, data, res)
26-
})
46+
}).nodeify(cb)
2747
}

test/tap/ping.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,41 @@ test('npm ping', function (t) {
4040
common.npm([
4141
'ping',
4242
'--registry', common.registry,
43-
'--loglevel', 'silent',
43+
'--loglevel', 'notice',
4444
'--userconfig', outfile
45-
], opts, function (err, code, stdout) {
45+
], opts, function (err, code, stdout, stderr) {
4646
s.close()
47-
t.ifError(err, 'no error output')
47+
t.ifError(err, 'command completed')
4848
t.notOk(code, 'exited OK')
4949

50-
t.same(stdout, 'Ping success: ' + JSON.stringify(pingResponse) + '\n')
50+
t.match(stderr, /PING/, 'ping notification output')
51+
t.match(stderr, /PONG/, 'pong response output')
52+
t.end()
53+
})
54+
})
55+
})
56+
57+
test('npm ping --json', function (t) {
58+
mr({ port: common.port, plugin: mocks }, function (err, s) {
59+
if (err) throw err
60+
61+
common.npm([
62+
'ping',
63+
'--json',
64+
'--registry', common.registry,
65+
'--loglevel', 'notice',
66+
'--userconfig', outfile
67+
], opts, function (err, code, stdout, stderr) {
68+
s.close()
69+
t.ifError(err, 'command completed')
70+
t.notOk(code, 'exited OK')
71+
72+
const json = JSON.parse(stdout.trim())
73+
t.similar(json, {
74+
registry: common.registry,
75+
details: pingResponse
76+
}, 'JSON info returned')
77+
t.equal(typeof json.time, 'number', 'got a timestamp')
5178
t.end()
5279
})
5380
})

0 commit comments

Comments
 (0)