-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
53 lines (45 loc) · 1.18 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
53
var https = require('https')
var semver = require('semver')
var debug = require('debug')('npm-versions')
module.exports = function () {
var headers = {
'accept': 'application/json',
'accept-encoding': 'identity',
'user-agent': 'https://github.com/github-utils/npm-versions',
}
return function* versions(repo) {
var res = yield function (done) {
https.request({
method: 'GET',
host: 'registry.npmjs.org',
path: '/' + repo,
headers: headers,
agent: false,
})
.on('error', done)
.on('response', done.bind(null, null))
.end()
}
debug('%s got status code %s', repo, res.statusCode)
if (res.statusCode !== 200) {
res.destroy()
throw new Error('failed to get npmjs.org/' + repo + '\'s versions.')
}
var body = yield function (done) {
var buf = ''
res.setEncoding('utf8')
res.on('data', function (chunk) {
buf += chunk
})
res.once('end', function () {
done(null, buf)
})
}
return Object.keys(JSON.parse(body).versions)
.filter(valid)
.sort(semver.rcompare)
}
}
function valid(x) {
return semver.valid(x)
}