-
Notifications
You must be signed in to change notification settings - Fork 1
/
sieve.js
73 lines (59 loc) · 1.91 KB
/
sieve.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
var PubSub = require('pubsub-js');
var template = require('./lib/template')
, helpers = require('./lib/helpers')
, queue = require('./lib/queue');
var defaults = {
headers: { "User-Agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)" },
timeout: 10,
method: 'GET',
wait: 1, // Delay between scheduling batch requests
tries: 3, // Maximum number of attempts per url
cache: 60 * 60 * 24
};
// The main access point into Sieve
module.exports = function sieve(_entry, options){
var entry = JSON.parse(JSON.stringify(_entry));
if (typeof options === 'function'){
options = { onFinish: options }
} else if (typeof options === 'undefined'){
options = {};
}
// Template data if necessary
if (entry.data && typeof entry.data === 'object') {
entry = template(entry, entry.data);
}
// Input can be a single entry or an array of entries
if (helpers.isArray(entry)){
var expected = entry.length;
var results = new Array(expected);
var pos = 0;
entry.forEach(function(d,i){
d = helpers.extend({}, defaults, d);
var count = queue.add(d, options);
// Listen for results
PubSub.subscribe('result.' + count, function check(msg, data){
results[i] = data;
pos += 1;
if (pos === expected){
// Merge arrays
var data = {
result: results.map(function(d){ return d.result }),
response: results.map(function(d) { return d.response })
};
finish(entry, options, data);
}
});
});
} else {
entry = helpers.extend({}, defaults, entry);
var hash = queue.add(entry, options);
PubSub.subscribe('result.' + hash, function check(msg,data){
finish(entry, options, data);
});
}
};
function finish(entry, options, results){
if (options.onFinish && !entry.then) {
options.onFinish(results.result, results.response);
}
}