forked from peterbraden/linkchecker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (134 loc) · 3.75 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
/*
* Test website links for valid HTTP Response codes.
*/
var child_process = require('child_process')
, path = require('path')
, resourceQueue = require('./resource-queue')
, MAX_PROCESSES = process.env.LINK_TEST_MAX_PROCESSES || 5
, MAX_PROCESS_HEAPSIZE = process.env.LINK_TEST_MAX_PROCESS_HEAPSIZE || 100000000
var testUrls = module.exports = function(startUrl, opts, cb){
// Sane Defaults
opts = opts || {}
opts.depth = opts.depth || 1
opts.domains = opts.domains || ["localhost:3000"]
var queue = resourceQueue()
queue.push(startUrl, {depth: 0, source:startUrl})
startZombiePool(queue, opts, cb)
}
module.exports.reporters = require('./reporters')
var startZombiePool = function(queue, opts, cb){
var zombies = 0
var zombieTick = function(err){
// Step 1. Check we aren't done.
if (queue.len() < 1){
if (zombies <= 0){
cb(null, queue.summary())
}
} else {
// Step 2. Check we have enough Zombies.
for (var i = zombies; i < Math.min(MAX_PROCESSES, queue.len()); i++){
var z = new Zombie(queue, opts, function(){
zombies --;
zombieTick()
});
process.nextTick(z.munch.bind(z));
zombies ++
if (opts.emitter){
opts.emitter.emit('zombie-started', zombies)
}
}
}
setTimeout(zombieTick, 1000)
return;
}
zombieTick();
}
var Zombie = function(queue, opts, done){
this.uri = null
this.cb = null
this.z = child_process.fork(require.resolve('./zombie-process'), {silent:true})
this.done = done
this.queue = queue
this.opts = opts
this.emitter = this.opts.emitter
// Bind Child process events
this.z.on('message', this.handleMessage.bind(this));
this.z.on('exit', this.handleExit.bind(this))
}
Zombie.prototype.isBusy = function(){
return !!this.cb;
}
Zombie.prototype.handleMessage = function(msg){
if (msg.stat === 'event'){
return this.handleEvent(msg);
}
if (msg.stat == 'success'){
return this.handleSuccess(msg);
}
}
Zombie.prototype.handleEvent = function(ev){
if (this.emitter)
this.emitter.emit.apply(this.emitter, ev.args)
if (ev.args[0] === 'memory-usage'){
this.handleMemory.apply(this, ev.args)
}
}
Zombie.prototype.handleMemory = function(ev, pid, rss, heapTotal, heapUsed){
if (heapTotal > MAX_PROCESS_HEAPSIZE){
if (this.emitter)
this.emitter.emit('zombie-too-much-memory', heapTotal)
this.die()
}
}
Zombie.prototype.handleSuccess = function(res){
// put into queue
var q = res.results
, self = this
q.forEach(function(v){
var url = v[0]
, depth = v[1]
, source = v[2]
self.queue.push(url, {depth:depth, source:source})
})
this.queue.resolve(this.uri, true)
var cb = this.cb;
this.uri = null
this.cb = null;
cb(null, res);
}
Zombie.prototype.handleExit = function(){
if (this.emitter)
this.emitter.emit('zombie-died')
this.queue.resolve(this.uri, false)
this.done(new Error("Unexpected exit"))
}
Zombie.prototype.die = function(){
this.z.removeListener('exit', this.handleExit)
this.z.kill("SIGTERM");
}
Zombie.prototype.munch = function(){
if (this.queue.len() > 0) {
var resource = this.queue.pop()
, uri = resource[0]
, data = resource[1]
this.checkURI(uri, data.depth, data.source, this.munch.bind(this));
} else {
return this.done(null);
}
}
Zombie.prototype.checkURI = function(uri, depth, source, cb){
if (this.isBusy()){
throw new Error("Zombie is busy")
}
if (this.emitter){
this.emitter.emit('checking', uri, depth, source, this.queue.len(), this.queue.resolved().length);
}
this.cb = cb
this.uri = uri
this.z.send(JSON.stringify({
url: uri
, depth : depth
, source : source
, opts: this.opts
}))
}