ghcrawler/lib/request.js

240 строки
6.6 KiB
JavaScript
Исходник Обычный вид История

2016-11-11 10:59:42 +03:00
const extend = require('extend');
2016-12-01 04:54:48 +03:00
const Policy = require('./traversalPolicy');
2016-11-11 10:59:42 +03:00
/**
2016-12-01 04:54:48 +03:00
* Requests describe a resource to capture and process as well as the context for that processing.
*/
2016-11-11 10:59:42 +03:00
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-12-01 04:54:48 +03:00
this.policy = Policy.default();
2016-11-21 09:45:18 +03:00
this.context = context || {};
2016-11-18 02:33:44 +03:00
this.promises = [];
}
2016-12-01 04:54:48 +03:00
static adopt(object) {
if (object.__proto__ !== Request.prototype) {
object.__proto__ = Request.prototype;
}
2016-12-04 04:20:17 +03:00
if (object.policy && object.policy.__proto__ !== Policy.prototype) {
2016-12-01 04:54:48 +03:00
object.policy.__proto__ = Policy.prototype;
}
return object;
}
2016-11-18 02:33:44 +03:00
track(promises) {
if (!promises) {
2016-12-01 04:54:48 +03:00
return this;
2016-11-18 02:33:44 +03:00
}
if (Array.isArray(promises)) {
Array.prototype.push.apply(this.promises, promises);
} else {
this.promises.push(promises);
}
2016-12-01 04:54:48 +03:00
return this;
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() {
2016-12-01 04:54:48 +03:00
this.linkResource('self', this.getRootQualifier());
}
addSelfLink(key = 'id') {
this.linkResource('self', this.getChildQualifier(key));
}
getQualifier() {
return this.isRootType(this.type) ? this.getRootQualifier() : this.getChildQualifier();
}
getRootQualifier() {
return `urn:${this.type}:${this.document.id}`;
2016-11-12 11:13:04 +03:00
}
2016-12-01 04:54:48 +03:00
getChildQualifier(key = 'id') {
let qualifier = this.context.qualifier;
if (!qualifier || (typeof qualifier !== 'string')) {
throw new Error('Need something on which to base the self link URN');
2016-11-12 11:13:04 +03:00
}
2016-11-11 10:59:42 +03:00
qualifier = qualifier.endsWith(':') ? qualifier : qualifier + ':';
2016-12-01 04:54:48 +03:00
return `${qualifier}${this.type}:${this.document[key]}`;
2016-11-11 10:59:42 +03:00
}
linkResource(name, urn) {
2016-11-11 10:59:42 +03:00
const links = this.document._metadata.links;
const key = Array.isArray(urn) ? 'hrefs' : 'href';
2016-12-01 04:54:48 +03:00
links[name] = {};
links[name][key] = urn;
2016-12-01 04:54:48 +03:00
links[name].type = 'resource';
}
linkSiblings(href) {
const links = this.document._metadata.links;
2016-12-04 03:35:59 +03:00
links.siblings = { href: href, type: 'collection' };
2016-12-01 04:54:48 +03:00
}
linkCollection(name, href) {
const links = this.document._metadata.links;
2016-12-04 03:35:59 +03:00
links[name] = { href: href, type: 'collection' };
2016-11-11 10:59:42 +03:00
}
2016-12-01 04:54:48 +03:00
linkRelation(name, href) {
2016-11-11 10:59:42 +03:00
const links = this.document._metadata.links;
2016-12-04 03:35:59 +03:00
links[name] = { href: href, type: 'relation' };
2016-11-11 10:59:42 +03:00
}
queue(requests, name = null) {
this.track(this.crawler.queue(requests, name));
2016-11-11 10:59:42 +03:00
}
queueRoot(type, url) {
2016-12-01 04:54:48 +03:00
const policy = this.policy.createPolicyForRoot();
if (!policy) {
return;
}
const newRequest = new Request(type, url, { qualifier: 'urn:' });
2016-12-01 04:54:48 +03:00
newRequest.policy = policy;
2016-12-07 22:23:02 +03:00
// relations are not transitive so ensure any relation is stripped off
delete newRequest.context.relation;
this.queue(newRequest);
2016-11-12 11:13:04 +03:00
}
queueRoots(type, url, context = null) {
2016-12-01 04:54:48 +03:00
const policy = this.policy.createPolicyForRoot();
if (!policy) {
return;
}
const newRequest = new Request(type, url, extend({}, this.context, context));
newRequest.context.qualifier = this.document._metadata.links.self.href;
2016-12-01 04:54:48 +03:00
// We are queuing a collection so carry this request's policy over. A new policy may
// apply to the elements in the collection
newRequest.policy = this.policy;
this.queue(newRequest);
2016-11-11 10:59:42 +03:00
}
queueCollectionElement(type, url, qualifier) {
if (this.isRootType(type)) {
return this.queueRoot(type, url);
}
return this.queueChild(type, url, qualifier);
}
2016-11-11 10:59:42 +03:00
queueChild(type, url, qualifier) {
2016-12-01 04:54:48 +03:00
const policy = this.policy.createPolicyForChild();
if (!policy) {
return;
}
const newRequest = new Request(type, url, extend({}, this.context || {}));
2016-11-11 10:59:42 +03:00
newRequest.context.qualifier = qualifier;
2016-12-01 04:54:48 +03:00
newRequest.policy = policy;
2016-12-07 22:23:02 +03:00
// relations are not transitive so ensure any relation is stripped off
delete newRequest.context.relation;
this.queue(newRequest);
2016-11-11 10:59:42 +03:00
}
queueChildren(type, url, context = null) {
2016-12-01 04:54:48 +03:00
const policy = this.policy.createPolicyForChild();
if (!policy) {
return;
}
const newRequest = new Request(type, url, extend({}, this.context, context));
newRequest.context.qualifier = this.document._metadata.links.self.href;
2016-12-01 04:54:48 +03:00
// We are queuing a collection so carry this request's policy over. A new policy may
// apply to the elements in the collection
newRequest.policy = this.policy;
this.queue(newRequest);
2016-11-11 10:59:42 +03:00
}
markSkip(outcome, message) {
2016-12-04 03:35:59 +03:00
// Ensure we don't miss out on any errors. If there is already an error, log it.
if (outcome === 'Error' && this.outcome === 'Error') {
this._logCurrentError();
}
// Keep the first outcome unless the new one is an error
if (this.shouldSkip() && outcome !== 'Error') {
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-12-04 03:35:59 +03:00
// Ensure we don't miss out on any errors. If there is already an error, log it.
if (outcome === 'Error' && this.outcome === 'Error') {
this._logCurrentError();
}
// Keep the first outcome unless the new one is an error
if (this.shouldRequeue() && outcome !== 'Error') {
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;
}
2016-12-04 03:35:59 +03:00
_logCurrentError() {
if (this._crawler && this.outcome === 'Error') {
this.crawler._logOutcome(this);
}
}
markNoSave() {
this.save = false;
}
shouldSave() {
return (this.save !== false) && this.document && this.contentOrigin !== 'cacheOfOrigin';
}
2016-11-11 21:55:30 +03:00
shouldSkip() {
2016-11-12 01:06:10 +03:00
return this.processControl === 'skip' || this.processControl === 'requeue';
}
2016-11-16 09:21:33 +03:00
delayUntil(time) {
if (!this.nextRequestTime || this.nextRequestTime < time) {
this.nextRequestTime = time;
}
}
delay(milliseconds = 2000) {
2016-11-16 09:21:33 +03:00
this.delayUntil(Date.now() + milliseconds);
}
2016-11-11 21:55:30 +03:00
shouldRequeue() {
return this.processControl === 'requeue';
}
2016-12-09 08:41:28 +03:00
shouldFetchExisting() {
return this.context.relation || this.policy.shouldFetchExisting(this);
}
2016-12-04 03:35:59 +03:00
toString() {
return `${this.type}@${this.url}`;
}
toUniqueString() {
return `${this.type}@${this.url}:${this.policy.getShortForm()}`;
}
isCollectionType() {
const collections = new Set([
2016-12-15 09:54:10 +03:00
'orgs', 'repos', 'issues', 'issue_comments', 'review_comments', 'commits', 'teams', 'members', 'collaborators', 'contributors', 'subscribers', 'statuses'
2016-12-04 03:35:59 +03:00
]);
return collections.has(this.type);
2016-11-11 10:59:42 +03:00
}
isRootType(type) {
2016-11-26 08:36:11 +03:00
const roots = new Set(['orgs', 'org', 'repos', 'repo', 'teams', 'team', 'user', 'members']);
return roots.has(type);
}
2016-11-11 10:59:42 +03:00
}
module.exports = Request;