-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
277 lines (243 loc) · 8.48 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'use strict';
var debug = require('diagnostics')('githulk')
, mana = require('mana')
, url = require('url');
/**
* GitHulk smash API.
*
* Options:
* - url: The location of the API.
* - maxdelay: Maximum delay for exponential back off.
* - mindelay: Minimum delay for exponential back off.
* - retries: The amount of retries we should before failing.
* - factor: Exponential back off factor.
* - cache: We need to use to store requests in so we can handle 304's and not
* eat the API tokens.
* - tokens: Array of tokens we should use for these requests.
* - token: Single token we should use to connect. I added to the tokens array.
* - user: Username of your GitHub account (if you don't want to use tokens)
* - password: Password of your GitHub account (if you don't want to use tokens)
* - authorization: Custom authorization we should be using instead of tokens.
*
* @constructor
* @param {Object} options GitHulk configuration
* @api public
*/
mana.extend({
initialise: function initialise(options) {
options = options || {};
options.url = 'url' in options ? options.url : 'https://api.github.com/';
options.maxdelay = 'maxdelay' in options ? options.maxdelay : 60000;
options.mindelay = 'mindelay' in options ? options.mindelay : 100;
options.retries = 'retries' in options ? options.retries : 3;
options.factor = 'factor' in options ? options.factor : 2;
options.cache = 'cache' in options ? options.cache : null;
options.tokens = 'tokens' in options ? options.tokens : [];
this.authorization = options.authorization;
this.mindelay = options.mindelay;
this.maxdelay = options.maxdelay;
this.mirrors = options.mirrors;
this.retries = options.retries;
this.factor = options.factor;
this.tokens = options.tokens;
this.user = options.user;
this.api = options.url;
//
// Pre-compile the basic authorization so we can do updates and deletes
// against the registries.
//
if (!this.authorization && options.user && options.password) {
debug('received authorization information for %s', options.user);
this.authorization = 'Basic '+ new Buffer(
options.user +':'+ options.password
).toString('base64');
}
//
// Transform tokens in an array, strings are here as they can be more
// readable.
//
if ('string' === typeof this.tokens) {
this.tokens = this.tokens.split(',');
}
//
// No user / password, no predefined authorization, so maybe we've received
// an OAuth token.
//
var token = options.token
|| process.env.GITHULK_TOKEN
|| process.env.GITHULK
|| process.env.GITHUB_TOKEN;
if (!this.authorization && token) {
this.tokens.push(token);
}
//
// We want to use a cache engine for the optimizing our responses. This
// makes us use conditional requests for the Github API making it easier to
// stay within your API rate limit.
//
if (options.cache) {
this.cache = options.cache;
}
//
// Make a second instance of mana for the v4 api
//
this.manaql = mana.extend({
initialise: function initialise(opts) {
this.api = opts.api;
this.authorization = opts.authorization;
this.cache = opts.cache;
this.tokens = opts.tokens;
}
}).new(Object.assign(options, {
api: this.api,
authorization: this.authorization,
cache: this.cache,
tokens: this.tokens,
isGraphql: true
}));
this.manaql.setRatelimitParser(function parseRatelimitFromBody(res, body, setRatelimit) {
if(body && body.data && body.data.rateLimit) {
var limitData = body.data.rateLimit;
setRatelimit(limitData.resetAt, limitData.limit, limitData.remaining);
}
});
},
/**
* Parse out github information from a given string or object. For the object
* we assume that we're given an object with repository information as seen in
* your package.json
*
* @param {String|Object} data The information that needs to be parsed.
* @returns {Object}
* @api public
*/
project: require('extract-github'),
/**
* Return the correct Accept headers for a given content type.
*
* @param {String} type
* @returns {String}
*/
accepts: function accepts(type) {
var types = {
text: 'application/vnd.github.v3.text+json',
html: 'application/vnd.github.v3.html+json',
full: 'application/vnd.github.v3.full+json',
raw: 'application/vnd.github.v3.raw+json'
};
return types[type] || types[type.toLowerCase()] || type;
},
/**
* Helper function to create some sane default options that we supply to the
* API.
*
* @param {Object} args Received optional options
* @param {Array|Object} params Optional params.
* @returns {Object} Args.
* @api private
*/
options: function options(args, params) {
args = args || {};
args.params = args.params || params || [];
//
// Add some default values.
//
var defaults = { page: 1, per_page: 100 };
Object.keys(defaults).forEach(function each(key) {
if (Array.isArray(args.params)) {
if (!~args.params.indexOf(key)) args.params.push(key);
} else {
if (!(key in args.params)) args.params[key] = defaults[key];
}
});
return args;
},
/**
* Parse Github link headers.
*
* @param {String} header The link header we need to parse.
* @returns {Object}
* @api public
*/
link: function link(header) {
return header.split(',').reduce(function reduce(memo, part) {
var chunks = /<([^<]+)?>\;\srel="([^"]+)?"/.exec(part.trim());
if (!chunks) return memo;
memo[chunks[2]] = url.parse(chunks[1], true);
return memo;
}, Object.create(null));
},
/**
* We need to override the `send` method of mana so we can attempt to parse the
* pagination headers of GitHub and follow if needed so this can all be
* handled transparently.
*
* @returns {Assign} The assign instance that receives all the things
* @api private
*/
send: function send(args) {
args = this.args(arguments);
var options = JSON.parse(JSON.stringify(args.options))
, hulk = this;
/**
* A simple optional callback.
*
* @param {Response} res Incoming HTTP response.
* @param {Assign} assign The assign instance that got returned.
* @param {Object} oargs The original args that got passed in to the request.
* @api private
*/
args.options.next = function next(res, assign, oargs) {
oargs = oargs || args;
//
// When the `nofollow` option is provided we should not follow the
// returned link headers from the GitHub API. It's something that users
// want to manage them selfs.
//
if (!res.headers.link || oargs.options.nofollow) return assign.end();
var link = hulk.link(res.headers.link);
//
// We've reached the end of the of the iteration, also bail out.
//
if (!link.next || !link.next.query) return assign.end();
//
// We've received instructions from GitHub that there are more pages with
// information that we need to parse out. Continue to follow these link
// headers to create a full set of data. We leverage the `oargs` (original
// args) from the first request and only update the request params.
//
if (link.next.query.page) {
options.page = link.next.query.page;
}
if (link.next.query.per_page) {
options.per_page = link.next.query.per_page;
}
//
// This will be the last batch of data we need to process, so we can
// remove this next function and just make this thing process as normal
// again.
//
if (!link.last || link.next.query.page === link.last.query.page) {
delete oargs.options.next;
}
//
// Merge the options, mana can delete options while it creates params for
// the connection. We've stored a copy of them before, and we're going to
// merge them over again.
//
hulk.merge(oargs.options, options);
//
// Remove oargs.str if we have an array preference.
//
if (oargs.str && oargs.array) oargs.str = '';
oargs.options.assign = assign;
// No need to enqueue another callback.
return mana.prototype.send.call(hulk,
oargs.array, oargs.nr, oargs.options, oargs.str
);
};
return mana.prototype.send.call(hulk,
args.array, args.fn, args.nr, args.options, args.str
);
}
}).drink(module);