proxy-rotator-js 1.1.1 → 1.2.1

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.
Files changed (49) hide show
  1. package/dist/cjs/index.cjs +12 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/{src/Proxy.js → dist/cjs/src/Proxy.cjs} +20 -31
  4. package/dist/cjs/src/Proxy.cjs.map +1 -0
  5. package/dist/cjs/src/ProxyRotator.cjs +241 -0
  6. package/dist/cjs/src/ProxyRotator.cjs.map +1 -0
  7. package/dist/cjs/src/Queue.cjs +64 -0
  8. package/dist/cjs/src/Queue.cjs.map +1 -0
  9. package/dist/cjs/src/utils/makeRequestWithProxy.cjs +24 -0
  10. package/dist/cjs/src/utils/makeRequestWithProxy.cjs.map +1 -0
  11. package/dist/index.d.ts +8 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +5 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/src/Proxy.d.ts +40 -0
  16. package/dist/src/Proxy.d.ts.map +1 -0
  17. package/dist/src/Proxy.js +93 -0
  18. package/dist/src/Proxy.js.map +1 -0
  19. package/dist/src/ProxyRotator.d.ts +52 -0
  20. package/dist/src/ProxyRotator.d.ts.map +1 -0
  21. package/dist/src/ProxyRotator.js +235 -0
  22. package/dist/src/ProxyRotator.js.map +1 -0
  23. package/dist/src/Queue.d.ts +16 -0
  24. package/dist/src/Queue.d.ts.map +1 -0
  25. package/dist/src/Queue.js +61 -0
  26. package/dist/src/Queue.js.map +1 -0
  27. package/dist/src/utils/makeRequestWithProxy.d.ts +9 -0
  28. package/dist/src/utils/makeRequestWithProxy.d.ts.map +1 -0
  29. package/dist/src/utils/makeRequestWithProxy.js +18 -0
  30. package/dist/src/utils/makeRequestWithProxy.js.map +1 -0
  31. package/dist/test/test.d.ts +2 -0
  32. package/dist/test/test.d.ts.map +1 -0
  33. package/dist/test/test.js +122 -0
  34. package/dist/test/test.js.map +1 -0
  35. package/dist/test/test_proxies.d.ts +2 -0
  36. package/dist/test/test_proxies.d.ts.map +1 -0
  37. package/dist/test/test_proxies.js +20 -0
  38. package/dist/test/test_proxies.js.map +1 -0
  39. package/package.json +23 -4
  40. package/assets/http_proxies_with_commas.txt +0 -1
  41. package/assets/http_proxies_with_newlines.txt +0 -8
  42. package/assets/http_proxies_with_spaces.txt +0 -1
  43. package/index.js +0 -2
  44. package/src/ProxyRotator.js +0 -239
  45. package/src/Queue.js +0 -61
  46. package/src/utils/makeRequestWithProxy.js +0 -21
  47. package/test/options.js +0 -32
  48. package/test/test.js +0 -204
  49. package/test/test_proxies.js +0 -22
@@ -1,8 +0,0 @@
1
- 139.59.1.14:8080
2
- 94.45.74.60:8080
3
- 161.35.70.249:3128
4
- 217.182.170.224:80
5
- 222.138.76.6:9002
6
- 218.252.206.89:80
7
- 18.214.66.210:80
8
- 120.234.203.171:9002
@@ -1 +0,0 @@
1
- 139.59.1.14:8080 94.45.74.60:8080 161.35.70.249:3128 217.182.170.224:80 222.138.76.6:9002 218.252.206.89:80 18.214.66.210:80 120.234.203.171:9002
package/index.js DELETED
@@ -1,2 +0,0 @@
1
- import ProxyRotator from './src/ProxyRotator.js'
2
- export default ProxyRotator
@@ -1,239 +0,0 @@
1
- import fs from 'fs';
2
- import Queue from './Queue.js';
3
- import Proxy from './Proxy.js';
4
- import makeRequestWithProxy from './utils/makeRequestWithProxy.js';
5
-
6
- class ProxyRotator {
7
- constructor(proxies, options={} ){
8
- this.pool = new Queue();
9
- this.graveyard = [];
10
- // handle options
11
- let { returnAs, revive_timer, shuffle, protocol, assume_aliveness, check_on_next } = options;
12
- // how long to wait before reviving a dead proxy
13
- // default: 30 minutes
14
- this.revive_timer = revive_timer ?? 1000 * 60 * 30;
15
- // return type of proxy, either 'string' or 'object'
16
- this.returnAs = returnAs? this._handleReturnAsInput(returnAs) : 'string';
17
- // assume a a protocol for all proxies
18
- this.protocol = protocol ?? null;
19
- // shuffle the proxies before adding them to the queue
20
- this.shuffle = shuffle ?? false;
21
- // assume all proxies are alive when first added instead of 'new'
22
- this.assume_aliveness = assume_aliveness ?? false;
23
- // check if proxies are alive when they are added to the queue
24
- this.check_on_next = check_on_next ?? false;
25
- // examine proxies passed
26
- // check if it is a file path
27
- if( typeof proxies === 'string' ){
28
- // parse file
29
- proxies = this._parseFile(proxies);
30
- // add proxies to queue
31
- proxies.forEach( p => this._add(p) );
32
- }else if( this._isArray(proxies) ){
33
- // add proxies to queue
34
- proxies.forEach( p => this._add(p) );
35
- }
36
- // shuffle proxies
37
- if(this.shuffle){
38
- let shuffled = this._shuffleArray(this.pool.toArray());
39
- this.pool = new Queue();
40
- shuffled.forEach( p => this.pool.enqueue(p) );
41
- }
42
- }
43
-
44
- getGraveyard(){ return this.graveyard.map(p => p.proxy) }
45
-
46
- getGraveyardSize(){ return this.graveyard.length }
47
-
48
- getPool(){ return this.pool.toArray().map(p => p.proxy) }
49
-
50
- getPoolSize(){ return this.pool.size }
51
-
52
- add(proxies){ // add proxy to queue
53
- if(this._isArray(proxies)) // if passed an array
54
- for(let proxy of proxies) this._add(proxy);
55
- else // single file
56
- this._add(proxies);
57
- }
58
-
59
- _add(proxy){ // add proxy to queue
60
- let p = new Proxy(proxy, this.protocol, this.assume_aliveness);
61
- this.pool.enqueue(p);
62
- }
63
-
64
- remove(proxy){ // remove proxy from queue
65
- if(this._isArray(proxy)) // if passed an array
66
- for(let p of proxy) this._remove(p);
67
- else // single file
68
- this._remove(proxy);
69
- }
70
-
71
- _remove(proxy){ // remove proxy from queue
72
- this.pool.toArray().forEach( (p,i) => {
73
- if(p.equals(proxy)) this.pool.remove(i);
74
- });
75
- }
76
-
77
- getAlive(){ // get a random alive proxy
78
- let proxies = this.pool.toArray();
79
- for( let proxy of proxies )
80
- if(proxy.isAlive()) return proxy.proxy;
81
- }
82
-
83
- setAlive(proxy){ // set a proxy to alive
84
- let proxies = this.pool.toArray()
85
- for( let p of proxies ) // if proxy is in queue
86
- if(p.equals(proxy)) return p.setAlive();
87
- // if proxy is in graveyard
88
- for( let p of this.graveyard )
89
- if(p.equals(proxy)) return this.resurect(p);
90
- }
91
-
92
- resurect( proxy ){
93
- // get proxy from graveyard
94
- let p = this.graveyard.find( p => p.equals(proxy) );
95
- // if proxy is not in graveyard
96
- if(!p) return;
97
- // remove from graveyard
98
- this.graveyard = this.graveyard.filter( p => !p.equals(proxy) );
99
- // set as new
100
- p.setNew();
101
- // add to queue
102
- this.pool.enqueue(p);
103
- }
104
-
105
- setDead(proxy){ // set a proxy to dead
106
- this.pool.toArray().forEach( (p,i) => {
107
- if(p.equals(proxy)){
108
- p.setDead(); // set to dead
109
- // remove from queue
110
- this.pool.remove(i);
111
- // add to graveyard
112
- this.graveyard.push(p);
113
- }
114
- })
115
- // revive proxy after revive_timer
116
- setTimeout( () => this.resurect(proxy), this.revive_timer );
117
- }
118
-
119
- kill(proxy){ // kill a proxy
120
- return this.setDead(proxy);
121
- }
122
-
123
- next(options={}){
124
- let { returnAs } = options;
125
- // resurect a proxy from the graveyard
126
- if(this.check_on_next) _resurection();
127
- // if there are no proxies in the pool
128
- if(this.pool.size === 0) return null ;
129
- // remove from front
130
- let proxy = this.pool.dequeue();
131
- // add to back
132
- this.pool.enqueue(proxy);
133
- // if parameter is passed
134
- if(returnAs) returnAs = this._handleReturnAsInput(returnAs)
135
- // else use default
136
- else returnAs = this.returnAs;
137
- // retun proxy as string or object
138
- if(returnAs === 'string') return proxy.toString();
139
- else if(returnAs === 'object') return proxy.obj()
140
- }
141
-
142
- /* Randomize array in-place using Durstenfeld shuffle algorithm */
143
- _shuffleArray(array) {
144
- for (var i = array.length - 1; i > 0; i--) {
145
- var j = Math.floor(Math.random() * (i + 1));
146
- var temp = array[i];
147
- array[i] = array[j];
148
- array[j] = temp;
149
- }
150
- return array;
151
- }
152
-
153
- _handleReturnAsInput(proxyType=null){
154
- if(typeof proxyType === 'string'){
155
- proxyType = proxyType.toLowerCase();
156
- if(proxyType === 'str') proxyType = 'string';
157
- if(proxyType === 'obj') proxyType = 'object';
158
- if(proxyType !== 'string' && proxyType !== 'object'){
159
- console.error('proxyType must be either "string" or "object"');
160
- proxyType = null
161
- }
162
- return proxyType;
163
- }else
164
- return null;
165
- }
166
-
167
- _parseFile(filename) {
168
- // read file
169
- let str = fs.readFileSync(filename, 'utf8');
170
- // this function is able to handle multiples types of files
171
- let strList = str.split('\n')
172
- // remove lines that are empty
173
- strList = strList.filter(s=>s.length>0);
174
- // if strList is has only one element
175
- if(strList.length === 1)
176
- strList = str.split(' ')
177
- // if strList is has only one check to separate by comma
178
- if(strList.length === 1)
179
- strList = str.split(',')
180
- // remove all the commas from the strings
181
- strList = strList.map(s=>s.replace(',',''))
182
- // parse list of strings into
183
- strList = strList
184
- .map(s=>s.trim())
185
- .filter(s=>s.length>0);
186
- // return proxies
187
- return strList;
188
- }
189
-
190
-
191
- _isArray(arrayValue){
192
- return ( arrayValue &&
193
- (typeof arrayValue === 'object') &&
194
- (arrayValue.constructor === Array) );
195
- }
196
-
197
- _resurection(){
198
- // look for proxies that have been
199
- // dead for a long than the resurection time
200
- // and revive them
201
- for( let proxy of this.graveyard )
202
- if( proxy.isDead() && proxy.timeSinceStatusChange >= this.revive_timer )
203
- this.resurect(proxy);
204
- }
205
-
206
- async test_proxies(){
207
- // test if proxies are working
208
- let proxies = this.pool.toArray()
209
- .map(p=>p.proxy)
210
- .map(p=> ({
211
- host:p.split(':')[0],
212
- port:p.split(':')[1]
213
- }));
214
- console.log('--- Testing Proxies ---');
215
- let workingCount = 0;
216
- let notWorkingCount = 0;
217
- // make request with proxy
218
- for (const proxy of proxies) {
219
- console.log(`Testing proxy ${proxy.host}:${proxy.port}...`);
220
- const response = await makeRequestWithProxy(proxy);
221
- if (response === proxy.host) {
222
- console.log(`Proxy ${proxy.host}:${proxy.port} is working.`);
223
- workingCount++;
224
- } else {
225
- console.log(`Proxy ${proxy.host}:${proxy.port} is not working.`);
226
- notWorkingCount++;
227
- }
228
- }
229
- console.log('--- Statistics ---');
230
- console.log(`Total proxies: ${proxies.length}`);
231
- console.log(`Working proxies: ${workingCount}`);
232
- console.log(`Not working proxies: ${notWorkingCount}`);
233
- }
234
-
235
- test = this.test_proxies;
236
-
237
- }
238
-
239
- export default ProxyRotator
package/src/Queue.js DELETED
@@ -1,61 +0,0 @@
1
- class Queue {
2
- constructor() {
3
- this.items = {}
4
- this.frontIndex = 0
5
- this.backIndex = 0
6
- }
7
-
8
- enqueue(item) {
9
- this.items[this.backIndex] = item
10
- this.backIndex++
11
- return item + ' inserted'
12
- }
13
-
14
- dequeue() {
15
- const item = this.items[this.frontIndex]
16
- delete this.items[this.frontIndex]
17
- this.frontIndex++
18
- return item
19
- }
20
-
21
- // remove value while maintaining order
22
- remove( index ) {
23
- if (index < 0 || index >= this.backIndex) {
24
- return 'Invalid index'
25
- }
26
- const item = this.items[index]
27
- for (let i = index; i < this.backIndex; i++) {
28
- this.items[i] = this.items[i + 1]
29
- }
30
- delete this.items[this.backIndex - 1]
31
- this.backIndex--
32
- return item
33
- }
34
-
35
- peek() {
36
- return this.items[this.frontIndex]
37
- }
38
-
39
- printQueue() {
40
- return this.items;
41
- }
42
-
43
- // return array of items in the order they were added
44
- toArray() {
45
- let arr = [];
46
- for(let i = this.frontIndex; i < this.backIndex; i++)
47
- arr.push(this.items[i]);
48
- return arr;
49
- }
50
-
51
- // return object of items in the order they were added
52
- toObject() {
53
- let obj = {};
54
- for(let i = this.frontIndex; i < this.backIndex; i++)
55
- obj[i] = this.items[i];
56
- return obj;
57
- }
58
-
59
- }
60
-
61
- export default Queue;
@@ -1,21 +0,0 @@
1
- import axios from 'axios';
2
- // Description: Function to make an Axios request using a proxy
3
-
4
- // Function to make an Axios request using a proxy
5
- async function makeRequestWithProxy(proxy) {
6
- try {
7
- const response = await axios.get('https://api.ipify.org?format=json', {
8
- proxy: {
9
- host: proxy.host,
10
- port: proxy.port,
11
- protocol: 'http'
12
- }
13
- });
14
- console.log(response.data);
15
- return response.data;
16
- } catch (error) {
17
- return null;
18
- }
19
- }
20
-
21
- export default makeRequestWithProxy;
package/test/options.js DELETED
@@ -1,32 +0,0 @@
1
- import ProxyRotator from '../index.js'
2
- import assert from 'assert';
3
- import chai from 'chai';
4
- const expect = chai.expect
5
-
6
- // slogan: when you like a little risk with you proxies
7
- // proxies to test with
8
- let test_proxies = [ '139.59.1.14:8080', '94.45.74.60:8080',
9
- '161.35.70.249:3128', '217.182.170.224:80', '222.138.76.6:9002',
10
- '218.252.206.89:80', '18.214.66.210:80', '120.234.203.171:9002' ]
11
-
12
- // check shuffle option
13
- describe('shuffle option', () => {
14
- it('shuffle proxies when true', () => {
15
- // make rotator with shuffle option set to true
16
- let rotator = new ProxyRotator(test_proxies, { shuffle: true });
17
- // get pool
18
- let pool = rotator.getPool();
19
- // check if the proxies are shuffled
20
- assert.notDeepEqual(test_proxies, pool);
21
- });
22
-
23
- it('do not shuffle proxies when false', () => {
24
- // make rotator with shuffle option set to false
25
- let rotator = new ProxyRotator(test_proxies, { shuffle: false });
26
- // get pool
27
- let pool = rotator.getPool();
28
- // check if the proxies are not shuffled
29
- assert.deepEqual(test_proxies, pool);
30
- });
31
- });
32
-
package/test/test.js DELETED
@@ -1,204 +0,0 @@
1
- import ProxyRotator from '../index.js'
2
- import assert from 'assert';
3
- import chai from 'chai';
4
- const expect = chai.expect
5
-
6
- // slogan: when you like a little risk with you proxies
7
- // proxies to test with
8
- let test_proxies = [ '139.59.1.14:8080', '94.45.74.60:8080',
9
- '161.35.70.249:3128', '217.182.170.224:80', '222.138.76.6:9002',
10
- '218.252.206.89:80', '18.214.66.210:80', '120.234.203.171:9002' ]
11
-
12
- // check if it is able to read from difrent files
13
- describe('read form proxy files', () => {
14
- // test if it can process a string point to a file
15
- it('reading file with newlines', () => {
16
- // make rotator with file with newlines
17
- let proxies_file = './assets/http_proxies_with_newlines.txt';
18
- // make rotator with file with newlines
19
- let rotator_with_newlines = new ProxyRotator(proxies_file);
20
- // get pool
21
- let pool = rotator_with_newlines.getPool();
22
- assert.deepEqual(test_proxies, pool)
23
- })
24
- // test if it can process a file with spaces
25
- it('reading file with spaces', () => {
26
- // make rotator with file with spaces
27
- let proxies_file = './assets/http_proxies_with_spaces.txt';
28
- // make rotator with file with spaces
29
- let rotator_with_spaces = new ProxyRotator(proxies_file);
30
- // get pool
31
- let pool = rotator_with_spaces.getPool();
32
- assert.deepEqual(test_proxies, pool)
33
- })
34
- // test if it can process a file with commas
35
- it('reading file with commas', () => {
36
- // make rotator with file with commas
37
- let proxies_file = './assets/http_proxies_with_commas.txt';
38
- // make rotator with file with spaces
39
- let rotator_with_commas = new ProxyRotator(proxies_file);
40
- // get pool
41
- let pool = rotator_with_commas.getPool();
42
- assert.deepEqual(test_proxies, pool)
43
- })
44
- });
45
-
46
- // check the functionality of the rotator
47
- describe('basic functionality', () => {
48
- // test if it can process a string point to a file
49
- it('add proxies array', () => {
50
- // make rotator
51
- let rotator = new ProxyRotator(test_proxies);
52
- // get pool
53
- assert.deepEqual(test_proxies,rotator.getPool())
54
- })
55
- // adding one by one
56
- it('add proxy one by one ', () => {
57
- // make rotator
58
- let rotator = new ProxyRotator();
59
- // add proxies one by one
60
- for(let proxy of test_proxies) rotator.add(proxy)
61
- // test
62
- assert.deepEqual(test_proxies,rotator.getPool())
63
- })
64
- // remove one by one
65
- it('remove one', () => {
66
- // make rotator
67
- let rotator = new ProxyRotator(test_proxies);
68
- // remove proxies one by one
69
- let removed = test_proxies.filter(e => e !== test_proxies[3])
70
- rotator.remove(test_proxies[3])
71
- // test
72
- assert.deepEqual(removed, rotator.getPool())
73
- })
74
- // remove one by one
75
- it('remove proxy one by one ', () => {
76
- // make rotator
77
- let rotator = new ProxyRotator(test_proxies);
78
- // remove proxies one by one
79
- for(let proxy of test_proxies) rotator.remove(proxy)
80
- // test
81
- assert.deepEqual([], rotator.getPool())
82
- })
83
- // test if set dead works
84
- it('set dead to graveyard', () => {
85
- // make rotator
86
- let rotator = new ProxyRotator(test_proxies);
87
- // set dead
88
- rotator.setDead(test_proxies[3])
89
- // test
90
- assert.deepEqual(
91
- rotator.getGraveyard().includes(test_proxies[3]),
92
- true
93
- )
94
- })
95
- it('set dead remove from pool', () => {
96
- // make rotator
97
- let rotator = new ProxyRotator(test_proxies);
98
- // set dead
99
- rotator.setDead(test_proxies[3])
100
- // test
101
- assert.equal(
102
- rotator.getPool().includes(test_proxies[3]),
103
- false
104
- )
105
- })
106
- it('resurect proxy from graveyard', () => {
107
- // make rotator
108
- let rotator = new ProxyRotator(test_proxies);
109
- // set dead
110
- rotator.setDead(test_proxies[3])
111
- // set alive
112
- rotator.resurect(test_proxies[3])
113
- // test
114
- assert.equal(rotator.getPool().includes(test_proxies[3]), true)
115
- })
116
- // test if set dead works
117
- it('set dead proxy alive', () => {
118
- // make rotator
119
- let rotator = new ProxyRotator(test_proxies);
120
- // set dead
121
- rotator.setDead(test_proxies[3])
122
- // set alive
123
- rotator.setAlive(test_proxies[3])
124
- // test
125
- assert.equal(rotator.getPool().includes(test_proxies[3]), true)
126
- })
127
- // test if set dead works
128
- it('get alive', () => {
129
- // make rotator
130
- let rotator = new ProxyRotator(test_proxies);
131
- // set alive
132
- rotator.setAlive(test_proxies[3])
133
- // get alive
134
- let proxy = rotator.getAlive()
135
- // test
136
- assert.equal(proxy, test_proxies[3])
137
- })
138
- // test rotation
139
- it('test rotation', () => {
140
- // make rotator
141
- let rotator = new ProxyRotator(test_proxies);
142
- let removed = test_proxies.filter(e => e !== test_proxies[3])
143
- // set dead
144
- rotator.setDead(test_proxies[3])
145
- // test if every proxy returned with next matches removed
146
- for(let i = 0; i < removed.length; i++){
147
- let proxy = rotator.next()
148
- assert.equal(proxy, removed[i])
149
- }
150
- })
151
- // test resurrect timer
152
- it('resurect timer', function(done){
153
- // set timeout
154
- this.timeout(1100);
155
- // make rotator
156
- let rotator = new ProxyRotator(test_proxies, { revive_timer: 1000 } );
157
- let removed = test_proxies.filter(e => e !== test_proxies[3])
158
- // set dead
159
- rotator.setDead(test_proxies[3])
160
- // wait for resurect timer
161
- setTimeout(() => {
162
- assert.equal(
163
- rotator.getPool().includes(test_proxies[3]),
164
- true
165
- );
166
- done();
167
- }, 1000);
168
- })
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
-
@@ -1,22 +0,0 @@
1
- import ProxyRotator from '../index.js'
2
- import assert from 'assert';
3
- import chai from 'chai';
4
- const expect = chai.expect
5
-
6
- // slogan: when you like a little risk with you proxies
7
- // proxies to test with
8
- let test_proxies = [ '139.59.1.14:8080', '94.45.74.60:8080',
9
- '161.35.70.249:3128', '217.182.170.224:80', '222.138.76.6:9002',
10
- '218.252.206.89:80', '18.214.66.210:80', '120.234.203.171:9002' ]
11
-
12
- // check if it is able to read from difrent files
13
- describe('testing testing proxies', () => {
14
- // test if it can process a string point to a file
15
- it('testing proxeis', () => {
16
- // make rotator
17
- let rotator = new ProxyRotator(test_proxies);
18
- rotator.test()
19
- assert.deepEqual(test_proxies,rotator.getPool())
20
- })
21
- })
22
-