proxy-rotator-js 1.0.2 → 1.0.4
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/ProxyRotator.js +21 -4
package/package.json
CHANGED
package/src/ProxyRotator.js
CHANGED
|
@@ -18,10 +18,12 @@ class ProxyRotator {
|
|
|
18
18
|
proxies.forEach( p => this._add(p) );
|
|
19
19
|
}
|
|
20
20
|
// handle options
|
|
21
|
-
let { revive_timer, shuffle, protocol, assume_aliveness, check_on_next } = options;
|
|
21
|
+
let { proxyType, revive_timer, shuffle, protocol, assume_aliveness, check_on_next } = options;
|
|
22
22
|
// how long to wait before reviving a dead proxy
|
|
23
23
|
// default: 30 minutes
|
|
24
24
|
this.revive_timer = revive_timer ?? 1000 * 60 * 30;
|
|
25
|
+
// return type of proxy, either 'string' or 'object'
|
|
26
|
+
this.proxyType = _handleProxyTypeInput(proxyType);
|
|
25
27
|
// assume a a protocol for all proxies
|
|
26
28
|
this.protocol = protocol ?? null;
|
|
27
29
|
// shuffle the proxies before adding them to the queue
|
|
@@ -111,7 +113,7 @@ class ProxyRotator {
|
|
|
111
113
|
return this.setDead(proxy);
|
|
112
114
|
}
|
|
113
115
|
|
|
114
|
-
next(){
|
|
116
|
+
next(proxyType=null){
|
|
115
117
|
// resurect a proxy from the graveyard
|
|
116
118
|
if(this.check_on_next) _resurection();
|
|
117
119
|
// if there are no proxies in the pool
|
|
@@ -120,8 +122,12 @@ class ProxyRotator {
|
|
|
120
122
|
let proxy = this.pool.dequeue();
|
|
121
123
|
// add to back
|
|
122
124
|
this.pool.enqueue(proxy);
|
|
123
|
-
//
|
|
124
|
-
|
|
125
|
+
// if returnType is 'string'
|
|
126
|
+
if(proxyType === null) proxyType = this.proxyType;
|
|
127
|
+
else proxyType = this._handleProxyTypeInput(proxyType);
|
|
128
|
+
// return proxy
|
|
129
|
+
if(proxyType === 'string') return proxy.proxy;
|
|
130
|
+
else if(this.returnType === 'object') return proxy.toObject();
|
|
125
131
|
}
|
|
126
132
|
|
|
127
133
|
/* Randomize array in-place using Durstenfeld shuffle algorithm */
|
|
@@ -135,6 +141,17 @@ class ProxyRotator {
|
|
|
135
141
|
return array;
|
|
136
142
|
}
|
|
137
143
|
|
|
144
|
+
_handleProxyTypeInput(proxyType){
|
|
145
|
+
proxyType = proxyType.toLowerCase();
|
|
146
|
+
if(proxyType === 'str') proxyType = 'string';
|
|
147
|
+
if(proxyType === 'obj') proxyType = 'object';
|
|
148
|
+
if(proxyType !== 'string' && proxyType !== 'object'){
|
|
149
|
+
console.error('proxyType must be either "string" or "object"');
|
|
150
|
+
proxyType = 'string';
|
|
151
|
+
}
|
|
152
|
+
return proxyType;
|
|
153
|
+
}
|
|
154
|
+
|
|
138
155
|
_parseFile(filename) {
|
|
139
156
|
// read file
|
|
140
157
|
let str = fs.readFileSync(filename, 'utf8');
|