proxy-rotator-js 1.0.3 → 1.0.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "proxy-rotator-js",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "type": "module",
5
5
  "description": "Proxy Rotator",
6
6
  "main": "index.js",
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
@@ -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 { returnType, revive_timer, shuffle, protocol, assume_aliveness, check_on_next } = options;
10
+ let { proxyType, 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
- // return type of proxy
26
- this.returnType = returnType ?? 'string'; // or 'object'
14
+ // return type of proxy, either 'string' or 'object'
15
+ this.proxyType = proxyType? this._handleProxyTypeInput(proxyType) : '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,7 @@ class ProxyRotator {
113
113
  return this.setDead(proxy);
114
114
  }
115
115
 
116
- next(returnType='string'){
116
+ next(proxyType=null){
117
117
  // resurect a proxy from the graveyard
118
118
  if(this.check_on_next) _resurection();
119
119
  // if there are no proxies in the pool
@@ -123,11 +123,11 @@ class ProxyRotator {
123
123
  // add to back
124
124
  this.pool.enqueue(proxy);
125
125
  // if returnType is 'string'
126
- if(this.returnType === 'string')
127
- // return proxy as string
128
- return proxy.proxy;
129
- else if(this.returnType === 'object')
130
- return proxy.toObject();
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();
131
131
  }
132
132
 
133
133
  /* Randomize array in-place using Durstenfeld shuffle algorithm */
@@ -141,6 +141,18 @@ class ProxyRotator {
141
141
  return array;
142
142
  }
143
143
 
144
+ _handleProxyTypeInput(proxyType=null){
145
+ if(proxyType === null) return null;
146
+ proxyType = proxyType.toLowerCase();
147
+ if(proxyType === 'str') proxyType = 'string';
148
+ if(proxyType === 'obj') proxyType = 'object';
149
+ if(proxyType !== 'string' && proxyType !== 'object'){
150
+ console.error('proxyType must be either "string" or "object"');
151
+ proxyType = 'string';
152
+ }
153
+ return proxyType;
154
+ }
155
+
144
156
  _parseFile(filename) {
145
157
  // read file
146
158
  let str = fs.readFileSync(filename, 'utf8');