-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
176 lines (143 loc) · 4.27 KB
/
example.js
File metadata and controls
176 lines (143 loc) · 4.27 KB
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
173
174
175
176
var Cache = require('./lib/cache.js'),
http = require('http'),
hostCache = new Cache(1000),
stszCache = new Cache(1000);
/**
* Function with single argument callback
* Connects to a host and returns true or false wether a connection was made
*
* @param key Hostname
* @param Function Callback
*/
function checkHost(host,callback) {
if(hostCache.get(host,callback)) {
console.log('checkHost: Resolved %s from hostCache...',host);
return true;
}
console.log('checkHost: Looking up %s...',host);
var options = {
host: host,
port: 80,
path: '/',
method: 'GET'
};
var req = http.request(options, function(res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
hostCache.set(host,true);
});
res.on('error',function() {
hostCache.set(host,false);
});
});
req.on('error',function() {
hostCache.set(host,false);
});
req.end();
}
/**
* Function with multiple argument callback
* Downloads a page, and returns three args:
* @return mixed Error or null
* @return int Status code
* @return int Size
*
* @param key Hostname
* @param Function Callback
*/
function getStatusAndSize(host,path,callback) {
if(stszCache.get(host,callback)) {
console.log('getStatusAndSize: Resolved %s%s from stszCache...',host,path);
return true;
}
console.log('getStatusAndSize: Looking up %s%s...',host,path);
var options = {
host: host,
port: 80,
path: path,
method: 'GET'
};
var req = http.request(options, function(res) {
var data = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
stszCache.set(host,null,res.statusCode,data.length);
});
res.on('error', function (e) {
stszCache.set(host,e);
});
});
req.on('error',function(e) {
stszCache.set(host,e);
});
req.end();
}
// The first ones
console.log('\nFirst calls...\n');
checkHost('google.com',function(status) {
console.log('google.com returned %s',(status ? 'ok' : 'error'));
});
checkHost('google978345789345978.com',function(status) {
console.log('google978345789345978.com returned %s',(status ? 'ok' : 'error'));
});
getStatusAndSize('nodejs.org','/about',function(err,status,size) {
if(!err)
console.log('nodejs.org/ returned %d and has length %d',status,size);
else
console.error('nodejs.org/ returned the following error: %s',err);
});
getStatusAndSize('nnnnnpmjs.org','/about',function(err,status,size) {
if(!err)
console.log('nnnnnpmjs.org/about returned %d and has length %d',status,size);
else
console.error('nnnnnpmjs.org/about returned the following error: %s',err);
});
// The ones that will be queued
console.log('\nCalls that will be queued...\n');
checkHost('google.com',function(status) {
console.log('google.com returned %s',(status ? 'ok' : 'error'));
});
checkHost('google978345789345978.com',function(status) {
console.log('google978345789345978.com returned %s',(status ? 'ok' : 'error'));
});
getStatusAndSize('nodejs.org','/about',function(err,status,size) {
if(!err)
console.log('nodejs.org/ returned %d and has length %d',status,size);
else
console.error('nodejs.org/ returned the following error: %s',err);
});
getStatusAndSize('nnnnnpmjs.org','/about',function(err,status,size) {
if(!err)
console.log('nnnnnpmjs.org/about returned %d and has length %d',status,size);
else
console.error('nnnnnpmjs.org/about returned the following error: %s',err);
});
// The ones that will hit cache directly
setTimeout(function() {
console.log('\nCalls that will hit cache...\n');
checkHost('google.com',function(status) {
console.log('google.com returned %s',(status ? 'ok' : 'error'));
});
checkHost('google978345789345978.com',function(status) {
console.log('google978345789345978.com returned %s',(status ? 'ok' : 'error'));
});
getStatusAndSize('nodejs.org','/about',function(err,status,size) {
if(!err)
console.log('nodejs.org/ returned %d and has length %d',status,size);
else
console.error('nodejs.org/ returned the following error: %s',err);
});
getStatusAndSize('nnnnnpmjs.org','/about',function(err,status,size) {
if(!err)
console.log('nnnnnpmjs.org/about returned %d and has length %d',status,size);
else
console.error('nnnnnpmjs.org/about returned the following error: %s',err);
});
},5000);