-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (41 loc) · 1.25 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
var parseLink = require('parse-link-header')
var through = require('through2').obj
var json = require('JSONStream')
var request = require('hyperquest')
exports.user = listURL(true, function(user) {
return 'https://api.github.com/users/' + user + '/repos'
})
exports.org = listURL(true, function(org) {
return 'https://api.github.com/orgs/' + org + '/repos'
})
exports.all = listURL(false, function(org) {
return 'https://api.github.com/repositories'
})
function listURL(required, startURL) {
return function getRepos(token, user) {
if (required && user.indexOf('/') !== -1) return done(new Error(
'Username may not contain a "/" character'
))
var stream = through()
var start = startURL(user)
var headers = {
'User-Agent': 'ghrepos',
}
if (token) {
headers.Authorization = ['token', token].join(' ')
}
grab(start)
return stream
function grab(uri) {
var req = request(uri, { headers: headers })
req.once('end', next)
.pipe(json.parse([true]))
.pipe(stream, { end: false })
function next() {
var links = parseLink(req.response.headers.link)
if (links && links.next) return grab(links.next.url)
stream.push(null)
}
}
}
}