proxy-rotator-js 1.0.5 → 1.0.7
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/README.md +1 -1
- package/package.json +1 -1
- package/src/ProxyRotator.js +23 -19
- package/test/test.js +37 -2
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ import ProxyRotator from 'proxy-rotator-js'
|
|
|
20
20
|
|
|
21
21
|
let proxies = ['proxy1', 'proxy2', 'proxy3']
|
|
22
22
|
|
|
23
|
-
let rotator = ProxyRotator(proxies, options={})
|
|
23
|
+
let rotator = new ProxyRotator(proxies, options={})
|
|
24
24
|
|
|
25
25
|
console.log( rotator.next() ) // 'proxy1'
|
|
26
26
|
console.log( rotator.next() ) // 'proxy2'
|
package/package.json
CHANGED
package/src/ProxyRotator.js
CHANGED
|
@@ -7,12 +7,12 @@ class ProxyRotator {
|
|
|
7
7
|
this.pool = new Queue();
|
|
8
8
|
this.graveyard = [];
|
|
9
9
|
// handle options
|
|
10
|
-
let {
|
|
10
|
+
let { returnAs, revive_timer, shuffle, protocol, assume_aliveness, check_on_next } = options;
|
|
11
11
|
// how long to wait before reviving a dead proxy
|
|
12
12
|
// default: 30 minutes
|
|
13
13
|
this.revive_timer = revive_timer ?? 1000 * 60 * 30;
|
|
14
14
|
// return type of proxy, either 'string' or 'object'
|
|
15
|
-
this.
|
|
15
|
+
this.returnAs = returnAs? this._handleReturnAsInput(returnAs) : 'string';
|
|
16
16
|
// assume a a protocol for all proxies
|
|
17
17
|
this.protocol = protocol ?? null;
|
|
18
18
|
// shuffle the proxies before adding them to the queue
|
|
@@ -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,16 +143,18 @@ class ProxyRotator {
|
|
|
141
143
|
return array;
|
|
142
144
|
}
|
|
143
145
|
|
|
144
|
-
|
|
145
|
-
if(proxyType ===
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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;
|
|
154
158
|
}
|
|
155
159
|
|
|
156
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('test 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('test 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
|
+
|