proxy-rotator-js 1.0.5 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "proxy-rotator-js",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "type": "module",
5
5
  "description": "Proxy Rotator",
6
6
  "main": "index.js",
@@ -7,12 +7,12 @@ class ProxyRotator {
7
7
  this.pool = new Queue();
8
8
  this.graveyard = [];
9
9
  // handle options
10
- let { proxyType, revive_timer, shuffle, protocol, assume_aliveness, check_on_next } = options;
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.proxyType = proxyType? this._handleProxyTypeInput(proxyType) : 'string';
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(proxyType=null){
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 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();
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
- _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;
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 with file with newlines
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 with file with newlines
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
+