2016-11-11 10:59:42 +03:00
|
|
|
const extend = require('extend');
|
|
|
|
|
|
|
|
class Request {
|
2016-11-21 09:45:18 +03:00
|
|
|
constructor(type, url, context = null) {
|
2016-11-12 03:46:31 +03:00
|
|
|
this.type = type;
|
|
|
|
this.url = url;
|
2016-11-21 09:45:18 +03:00
|
|
|
this.context = context || {};
|
2016-11-18 02:33:44 +03:00
|
|
|
this.promises = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
track(promises) {
|
|
|
|
if (!promises) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Array.isArray(promises)) {
|
|
|
|
Array.prototype.push.apply(this.promises, promises);
|
|
|
|
} else {
|
|
|
|
this.promises.push(promises);
|
|
|
|
}
|
2016-11-11 10:59:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
addMeta(data) {
|
|
|
|
this.meta = extend({}, this.meta, data);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-11-12 11:13:04 +03:00
|
|
|
addRootSelfLink() {
|
|
|
|
this.addSelfLink('id', 'urn:');
|
|
|
|
}
|
|
|
|
|
|
|
|
addSelfLink(key = 'id', base = null) {
|
2016-11-11 10:59:42 +03:00
|
|
|
let qualifier = base ? base : this.context.qualifier;
|
2016-11-12 11:13:04 +03:00
|
|
|
if (!qualifier || (typeof qualifier !== 'string' )) {
|
|
|
|
console.log('bummer');
|
|
|
|
}
|
2016-11-11 10:59:42 +03:00
|
|
|
qualifier = qualifier.endsWith(':') ? qualifier : qualifier + ':';
|
|
|
|
this.linkSelf('self', `${qualifier}${this.type}:${this.document[key]}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
linkSelf(name, value) {
|
|
|
|
const links = this.document._metadata.links;
|
|
|
|
const key = Array.isArray(value) ? 'hrefs' : 'href';
|
|
|
|
links[name] = { type: 'self' };
|
|
|
|
links[name][key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
linkSiblings(name, href) {
|
|
|
|
const links = this.document._metadata.links;
|
|
|
|
links[name] = { type: 'siblings', href: href };
|
|
|
|
}
|
|
|
|
|
2016-11-18 02:33:44 +03:00
|
|
|
queue(type, url, context) {
|
2016-11-12 03:46:31 +03:00
|
|
|
const newRequest = new Request(type, url);
|
2016-11-11 10:59:42 +03:00
|
|
|
newRequest.context = context;
|
2016-11-18 02:33:44 +03:00
|
|
|
this.track(this.crawler.queue(newRequest));
|
2016-11-11 10:59:42 +03:00
|
|
|
}
|
|
|
|
|
2016-11-12 11:13:04 +03:00
|
|
|
queueRoot(type, url, force = false) {
|
|
|
|
const newRequest = new Request(type, url);
|
|
|
|
newRequest.context = { qualifier: 'urn:' };
|
|
|
|
newRequest.force = force;
|
2016-11-18 02:33:44 +03:00
|
|
|
this.track(this.crawler.queue(newRequest));
|
2016-11-12 11:13:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
queueRoots(type, url, force = false) {
|
|
|
|
const newRequest = new Request(type, url);
|
|
|
|
const newContext = {};
|
|
|
|
newContext.qualifier = this.document._metadata.links.self.href;
|
|
|
|
newRequest.context = newContext;
|
|
|
|
newRequest.force = force;
|
2016-11-18 02:33:44 +03:00
|
|
|
this.track(this.crawler.queue(newRequest));
|
2016-11-11 10:59:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
queueChild(type, url, qualifier) {
|
2016-11-12 03:46:31 +03:00
|
|
|
const newRequest = new Request(type, url);
|
2016-11-11 10:59:42 +03:00
|
|
|
newRequest.context = this.context || {};
|
|
|
|
newRequest.context.qualifier = qualifier;
|
2016-11-12 11:13:04 +03:00
|
|
|
newRequest.force = this.force;
|
2016-11-18 02:33:44 +03:00
|
|
|
this.track(this.crawler.queue(newRequest));
|
2016-11-11 10:59:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
queueChildren(type, url, context = null) {
|
2016-11-12 03:46:31 +03:00
|
|
|
const newRequest = new Request(type, url);
|
2016-11-11 10:59:42 +03:00
|
|
|
const newContext = extend(this.context || {}, context);
|
|
|
|
newContext.qualifier = this.document._metadata.links.self.href;
|
2016-11-12 11:13:04 +03:00
|
|
|
newRequest.context = newContext;
|
|
|
|
newRequest.force = this.force;
|
2016-11-18 02:33:44 +03:00
|
|
|
this.track(this.crawler.queue(newRequest));
|
2016-11-11 10:59:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
markSkip(outcome, message) {
|
2016-11-14 10:38:43 +03:00
|
|
|
if (this.shouldSkip()) {
|
|
|
|
return this;
|
|
|
|
}
|
2016-11-11 21:55:30 +03:00
|
|
|
this.processControl = 'skip';
|
2016-11-11 10:59:42 +03:00
|
|
|
this.outcome = this.outcome || outcome;
|
|
|
|
this.message = this.message || message;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-11-12 01:06:10 +03:00
|
|
|
markRequeue(outcome, message) {
|
2016-11-14 10:38:43 +03:00
|
|
|
if (this.shouldRequeue()) {
|
|
|
|
return this;
|
|
|
|
}
|
2016-11-11 21:55:30 +03:00
|
|
|
this.processControl = 'requeue';
|
2016-11-12 01:06:10 +03:00
|
|
|
this.outcome = this.outcome || outcome;
|
2016-11-11 21:55:30 +03:00
|
|
|
this.message = this.message || message;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldSkip() {
|
2016-11-12 01:06:10 +03:00
|
|
|
return this.processControl === 'skip' || this.processControl === 'requeue';
|
|
|
|
}
|
|
|
|
|
|
|
|
markDelay() {
|
2016-11-14 10:38:43 +03:00
|
|
|
if (this.shouldDelay()) {
|
|
|
|
return this;
|
|
|
|
}
|
2016-11-12 01:06:10 +03:00
|
|
|
this.flowControl = 'delay';
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldDelay() {
|
|
|
|
return this.flowControl === 'delay';
|
2016-11-11 21:55:30 +03:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:21:33 +03:00
|
|
|
delayUntil(time) {
|
|
|
|
if (!this.nextRequestTime || this.nextRequestTime < time) {
|
|
|
|
this.nextRequestTime = time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delayFor(milliseconds) {
|
|
|
|
this.delayUntil(Date.now() + milliseconds);
|
|
|
|
}
|
|
|
|
|
2016-11-11 21:55:30 +03:00
|
|
|
shouldRequeue() {
|
|
|
|
return this.processControl === 'requeue';
|
|
|
|
}
|
|
|
|
|
2016-11-11 10:59:42 +03:00
|
|
|
eventHelper(references) {
|
|
|
|
const document = this.document;
|
|
|
|
// TODO understand if the actor is typically the same as the creator or pusher in the payload
|
|
|
|
const repo = document.repo ? document.repo.id : null;
|
|
|
|
const urn = repo ? `urn:repo:${repo}` : `urn:org:${document.org.id}`;
|
|
|
|
this.linkSelf('self', `${urn}:${this.type}:${document.id}`);
|
|
|
|
this.linkSelf('actor', `urn:login:${document.actor.id}`);
|
|
|
|
this.linkSelf('repo', `urn:repo:${document.repo.id}`);
|
|
|
|
this.linkSelf('org', `urn:org:${document.org.id}`);
|
|
|
|
this.queueRoot('login', document.actor.url);
|
|
|
|
this.queueRoot('repo', document.repo.url);
|
|
|
|
this.queueRoot('org', document.org.url);
|
|
|
|
return document.payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCollectionType() {
|
|
|
|
const collections = {
|
|
|
|
orgs: 'org', repos: 'repo', issues: 'issue', issue_comments: 'issue_comment', commits: 'commit', teams: 'team', users: 'user'
|
|
|
|
};
|
|
|
|
return collections[this.type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Request;
|