proxy-rotator-js 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Proxy.js +2 -2
- package/src/ProxyRotator.js +35 -30
- package/test/test.js +37 -2
package/package.json
CHANGED
package/src/Proxy.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
class Proxy {
|
|
2
|
-
constructor(proxy, protocol = null) {
|
|
2
|
+
constructor(proxy, protocol = null, assumeAlive = false) {
|
|
3
3
|
// if proxy string starts with protocol
|
|
4
4
|
if( proxy.includes('://') ){
|
|
5
5
|
this.protocol = proxy.split('://')[0];
|
|
@@ -13,7 +13,7 @@ class Proxy {
|
|
|
13
13
|
// the proxy
|
|
14
14
|
this.proxy = `${(this.protocol)? this.protocol+'://' : ''}${this.ip}:${this.port}`;
|
|
15
15
|
// status can be 'new', 'alive', 'dead'
|
|
16
|
-
this.status = 'new';
|
|
16
|
+
this.status = (assumeAlive)? 'alive' : 'new';
|
|
17
17
|
this.changeTimeStamp = Date.now();
|
|
18
18
|
}
|
|
19
19
|
// method to return as string
|
package/src/ProxyRotator.js
CHANGED
|
@@ -6,24 +6,13 @@ class ProxyRotator {
|
|
|
6
6
|
constructor(proxies, options={} ){
|
|
7
7
|
this.pool = new Queue();
|
|
8
8
|
this.graveyard = [];
|
|
9
|
-
// examine proxies passed
|
|
10
|
-
// check if it is a file path
|
|
11
|
-
if( typeof proxies === 'string' ){
|
|
12
|
-
// parse file
|
|
13
|
-
proxies = this._parseFile(proxies);
|
|
14
|
-
// add proxies to queue
|
|
15
|
-
proxies.forEach( p => this._add(p) );
|
|
16
|
-
}else if( this._isArray(proxies) ){
|
|
17
|
-
// add proxies to queue
|
|
18
|
-
proxies.forEach( p => this._add(p) );
|
|
19
|
-
}
|
|
20
9
|
// handle options
|
|
21
|
-
let {
|
|
10
|
+
let { returnAs, revive_timer, shuffle, protocol, assume_aliveness, check_on_next } = options;
|
|
22
11
|
// how long to wait before reviving a dead proxy
|
|
23
12
|
// default: 30 minutes
|
|
24
13
|
this.revive_timer = revive_timer ?? 1000 * 60 * 30;
|
|
25
14
|
// return type of proxy, either 'string' or 'object'
|
|
26
|
-
this.
|
|
15
|
+
this.returnAs = returnAs? this._handleReturnAsInput(returnAs) : 'string';
|
|
27
16
|
// assume a a protocol for all proxies
|
|
28
17
|
this.protocol = protocol ?? null;
|
|
29
18
|
// shuffle the proxies before adding them to the queue
|
|
@@ -32,6 +21,17 @@ class ProxyRotator {
|
|
|
32
21
|
this.assume_aliveness = assume_aliveness ?? false;
|
|
33
22
|
// check if proxies are alive when they are added to the queue
|
|
34
23
|
this.check_on_next = check_on_next ?? false;
|
|
24
|
+
// examine proxies passed
|
|
25
|
+
// check if it is a file path
|
|
26
|
+
if( typeof proxies === 'string' ){
|
|
27
|
+
// parse file
|
|
28
|
+
proxies = this._parseFile(proxies);
|
|
29
|
+
// add proxies to queue
|
|
30
|
+
proxies.forEach( p => this._add(p) );
|
|
31
|
+
}else if( this._isArray(proxies) ){
|
|
32
|
+
// add proxies to queue
|
|
33
|
+
proxies.forEach( p => this._add(p) );
|
|
34
|
+
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
getGraveyard(){ return this.graveyard.map(p => p.proxy) }
|
|
@@ -50,7 +50,7 @@ class ProxyRotator {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
_add(proxy){ // add proxy to queue
|
|
53
|
-
let p = new Proxy(proxy);
|
|
53
|
+
let p = new Proxy(proxy, this.protocol, this.assume_aliveness);
|
|
54
54
|
this.pool.enqueue(p);
|
|
55
55
|
}
|
|
56
56
|
|
|
@@ -113,7 +113,8 @@ class ProxyRotator {
|
|
|
113
113
|
return this.setDead(proxy);
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
next(
|
|
116
|
+
next(options={}){
|
|
117
|
+
let { returnAs } = options;
|
|
117
118
|
// resurect a proxy from the graveyard
|
|
118
119
|
if(this.check_on_next) _resurection();
|
|
119
120
|
// if there are no proxies in the pool
|
|
@@ -122,12 +123,13 @@ class ProxyRotator {
|
|
|
122
123
|
let proxy = this.pool.dequeue();
|
|
123
124
|
// add to back
|
|
124
125
|
this.pool.enqueue(proxy);
|
|
125
|
-
// if
|
|
126
|
-
if(
|
|
127
|
-
else
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
126
|
+
// if parameter is passed
|
|
127
|
+
if(returnAs) returnAs = this._handleReturnAsInput(returnAs)
|
|
128
|
+
// else use default
|
|
129
|
+
else returnAs = this.returnAs;
|
|
130
|
+
// retun proxy as string or object
|
|
131
|
+
if(returnAs === 'string') return proxy.toString();
|
|
132
|
+
else if(returnAs === 'object') return proxy.obj()
|
|
131
133
|
}
|
|
132
134
|
|
|
133
135
|
/* Randomize array in-place using Durstenfeld shuffle algorithm */
|
|
@@ -141,15 +143,18 @@ class ProxyRotator {
|
|
|
141
143
|
return array;
|
|
142
144
|
}
|
|
143
145
|
|
|
144
|
-
|
|
145
|
-
proxyType
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
146
|
+
_handleReturnAsInput(proxyType=null){
|
|
147
|
+
if(typeof proxyType === 'string'){
|
|
148
|
+
proxyType = proxyType.toLowerCase();
|
|
149
|
+
if(proxyType === 'str') proxyType = 'string';
|
|
150
|
+
if(proxyType === 'obj') proxyType = 'object';
|
|
151
|
+
if(proxyType !== 'string' && proxyType !== 'object'){
|
|
152
|
+
console.error('proxyType must be either "string" or "object"');
|
|
153
|
+
proxyType = null
|
|
154
|
+
}
|
|
155
|
+
return proxyType;
|
|
156
|
+
}else
|
|
157
|
+
return null;
|
|
153
158
|
}
|
|
154
159
|
|
|
155
160
|
_parseFile(filename) {
|
package/test/test.js
CHANGED
|
@@ -47,14 +47,14 @@ describe('read form proxy files', () => {
|
|
|
47
47
|
describe('basic functionality', () => {
|
|
48
48
|
// test if it can process a string point to a file
|
|
49
49
|
it('add proxies array', () => {
|
|
50
|
-
// make rotator
|
|
50
|
+
// make rotator
|
|
51
51
|
let rotator = new ProxyRotator(test_proxies);
|
|
52
52
|
// get pool
|
|
53
53
|
assert.deepEqual(test_proxies,rotator.getPool())
|
|
54
54
|
})
|
|
55
55
|
// adding one by one
|
|
56
56
|
it('add proxy one by one ', () => {
|
|
57
|
-
// make rotator
|
|
57
|
+
// make rotator
|
|
58
58
|
let rotator = new ProxyRotator();
|
|
59
59
|
// add proxies one by one
|
|
60
60
|
for(let proxy of test_proxies) rotator.add(proxy)
|
|
@@ -167,3 +167,38 @@ describe('basic functionality', () => {
|
|
|
167
167
|
}, 1000);
|
|
168
168
|
})
|
|
169
169
|
});
|
|
170
|
+
|
|
171
|
+
// check return type of proxy
|
|
172
|
+
describe('Test return type', () => {
|
|
173
|
+
// test if it return as obj when option is set
|
|
174
|
+
it('test default return string type', () => {
|
|
175
|
+
// make rotator
|
|
176
|
+
let rotator = new ProxyRotator(test_proxies);
|
|
177
|
+
// remove proxies one by one
|
|
178
|
+
let proxy = rotator.next()
|
|
179
|
+
// test if it is string
|
|
180
|
+
assert.equal(typeof proxy, 'string')
|
|
181
|
+
})
|
|
182
|
+
it('teset if it return object when passed returnAs obj', () => {
|
|
183
|
+
// make rotator
|
|
184
|
+
let rotator = new ProxyRotator(test_proxies, { returnAs: 'object' });
|
|
185
|
+
// remove proxies one by one
|
|
186
|
+
let proxy = rotator.next()
|
|
187
|
+
// test if it is string
|
|
188
|
+
assert.equal(typeof proxy, 'object')
|
|
189
|
+
})
|
|
190
|
+
it('teset if it return obj when passed as paramter', () => {
|
|
191
|
+
// make rotator
|
|
192
|
+
let rotator = new ProxyRotator(test_proxies);
|
|
193
|
+
// remove proxies one by one
|
|
194
|
+
let proxy = rotator.next({ returnAs:'obj'})
|
|
195
|
+
// test if it is string
|
|
196
|
+
assert.equal(typeof proxy, 'object')
|
|
197
|
+
// try to get sring
|
|
198
|
+
let proxy2 = rotator.next({ returnAs: 'str'})
|
|
199
|
+
// test if it is string
|
|
200
|
+
assert.equal(typeof proxy2, 'string')
|
|
201
|
+
})
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
|