2016-12-30 01:58:25 +03:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
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-11-24 16:46:39 +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
|
|
|
}
|
|
|
|
|
2016-12-01 04:54:48 +03:00
|
|
|
static adopt(object) {
|
|
|
|
if (object.__proto__ !== Request.prototype) {
|
|
|
|
object.__proto__ = Request.prototype;
|
|
|
|
}
|
2017-01-06 10:00:32 +03:00
|
|
|
object.policy = Request._getExpandedPolicy(object.policy);
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-06 10:00:32 +03:00
|
|
|
static _getExpandedPolicy(policyOrSpec) {
|
|
|
|
return typeof policyOrSpec === 'string' ? Policy.getPolicy(policyOrSpec) : policyOrSpec;
|
|
|
|
}
|
|
|
|
|
2017-01-02 22:42:52 +03:00
|
|
|
getTrackedPromises() {
|
|
|
|
return this.promises || [];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-01-02 22:42:52 +03:00
|
|
|
this.promises = this.promises || [];
|
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;
|
2016-11-24 16:46:39 +03:00
|
|
|
if (!qualifier || (typeof qualifier !== 'string')) {
|
2016-11-26 05:47:47 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-18 22:12:26 +03:00
|
|
|
linkResource(name, urn) {
|
2016-11-11 10:59:42 +03:00
|
|
|
const links = this.document._metadata.links;
|
2016-12-18 22:12:26 +03:00
|
|
|
const key = Array.isArray(urn) ? 'hrefs' : 'href';
|
2016-12-01 04:54:48 +03:00
|
|
|
links[name] = {};
|
2016-12-18 22:12:26 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-18 22:12:26 +03:00
|
|
|
queue(requests, name = null) {
|
|
|
|
this.track(this.crawler.queue(requests, name));
|
2016-11-11 10:59:42 +03:00
|
|
|
}
|
|
|
|
|
2016-11-24 16:46:39 +03:00
|
|
|
queueRoot(type, url) {
|
2016-12-01 04:54:48 +03:00
|
|
|
const policy = this.policy.createPolicyForRoot();
|
|
|
|
if (!policy) {
|
2016-11-24 16:46:39 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-12-18 22:12:26 +03:00
|
|
|
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;
|
2016-12-16 21:26:19 +03:00
|
|
|
this.queue(newRequest);
|
2016-11-12 11:13:04 +03:00
|
|
|
}
|
|
|
|
|
2016-11-26 05:47:47 +03:00
|
|
|
queueRoots(type, url, context = null) {
|
2016-12-01 04:54:48 +03:00
|
|
|
const policy = this.policy.createPolicyForRoot();
|
|
|
|
if (!policy) {
|
2016-11-24 16:46:39 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-12-18 22:12:26 +03:00
|
|
|
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;
|
2016-12-16 21:26:19 +03:00
|
|
|
this.queue(newRequest);
|
2016-11-11 10:59:42 +03:00
|
|
|
}
|
|
|
|
|
2016-11-24 16:46:39 +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) {
|
2016-11-24 16:46:39 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-12-18 22:12:26 +03:00
|
|
|
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;
|
2016-12-16 21:26:19 +03:00
|
|
|
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) {
|
2016-11-24 16:46:39 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-12-18 22:12:26 +03:00
|
|
|
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;
|
2016-12-16 21:26:19 +03:00
|
|
|
this.queue(newRequest);
|
2016-11-11 10:59:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
markSkip(outcome, message) {
|
2016-12-26 19:47:57 +03:00
|
|
|
// if we are already skipping/requeuing, keep the original as the official outcome but log this new one so its not missed
|
|
|
|
if (this.shouldSkip()) {
|
|
|
|
this._log('verbose', `Redundant skip: ${outcome}, ${message}`, this.meta);
|
2016-11-14 10:38:43 +03:00
|
|
|
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-26 19:47:57 +03:00
|
|
|
// if we are already skipping/requeuing, keep the original as the official outcome but log this new one so its not missed
|
|
|
|
if (this.shouldSkip()) {
|
|
|
|
this._log('verbose', `Redundant requeue: ${outcome}, ${message}`, this.meta);
|
2016-11-14 10:38:43 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-06 10:00:32 +03:00
|
|
|
markSave() {
|
|
|
|
this.save = true;
|
|
|
|
}
|
|
|
|
|
2016-12-12 07:07:07 +03:00
|
|
|
markNoSave() {
|
|
|
|
this.save = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldSave() {
|
2017-01-06 10:00:32 +03:00
|
|
|
return this.document && (this.save === true || (this.save !== false && this.contentOrigin !== 'cacheOfOrigin'));
|
2016-12-12 07:07:07 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 05:47:47 +03:00
|
|
|
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() {
|
2017-01-06 10:00:32 +03:00
|
|
|
return `${this.type}@${this.url}:${Request._getExpandedPolicy(this.policy).getShortForm()}`;
|
2016-12-04 03:35:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
isCollectionType() {
|
|
|
|
const collections = new Set([
|
2016-12-26 19:47:57 +03:00
|
|
|
'collaborators', 'commit_comments', 'commits', 'contributors', 'events', 'issues', 'issue_comments', 'members', 'orgs', 'repos', 'review_comments', 'subscribers', 'statuses', 'teams'
|
2016-12-04 03:35:59 +03:00
|
|
|
]);
|
|
|
|
return collections.has(this.type);
|
2016-11-11 10:59:42 +03:00
|
|
|
}
|
2016-11-24 16:46:39 +03:00
|
|
|
|
|
|
|
isRootType(type) {
|
2016-11-26 08:36:11 +03:00
|
|
|
const roots = new Set(['orgs', 'org', 'repos', 'repo', 'teams', 'team', 'user', 'members']);
|
2016-11-24 16:46:39 +03:00
|
|
|
return roots.has(type);
|
|
|
|
}
|
2016-12-26 19:47:57 +03:00
|
|
|
|
|
|
|
_log(level, message, meta = null) {
|
|
|
|
if (this.crawler) {
|
|
|
|
this.crawler.logger.log(level, message, meta);
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 10:59:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Request;
|