proxy-rotator-js 1.0.9 → 1.1.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 (154) hide show
  1. package/README.md +12 -0
  2. package/package.json +2 -1
  3. package/src/ProxyRotator.js +32 -0
  4. package/src/utils/makeRequestWithProxy.js +21 -0
  5. package/test/test_proxies.js +22 -0
  6. package/test_proxies/node_modules/.package-lock.json +0 -98
  7. package/test_proxies/node_modules/asynckit/LICENSE +0 -21
  8. package/test_proxies/node_modules/asynckit/README.md +0 -233
  9. package/test_proxies/node_modules/asynckit/bench.js +0 -76
  10. package/test_proxies/node_modules/asynckit/index.js +0 -6
  11. package/test_proxies/node_modules/asynckit/lib/abort.js +0 -29
  12. package/test_proxies/node_modules/asynckit/lib/async.js +0 -34
  13. package/test_proxies/node_modules/asynckit/lib/defer.js +0 -26
  14. package/test_proxies/node_modules/asynckit/lib/iterate.js +0 -75
  15. package/test_proxies/node_modules/asynckit/lib/readable_asynckit.js +0 -91
  16. package/test_proxies/node_modules/asynckit/lib/readable_parallel.js +0 -25
  17. package/test_proxies/node_modules/asynckit/lib/readable_serial.js +0 -25
  18. package/test_proxies/node_modules/asynckit/lib/readable_serial_ordered.js +0 -29
  19. package/test_proxies/node_modules/asynckit/lib/state.js +0 -37
  20. package/test_proxies/node_modules/asynckit/lib/streamify.js +0 -141
  21. package/test_proxies/node_modules/asynckit/lib/terminator.js +0 -29
  22. package/test_proxies/node_modules/asynckit/package.json +0 -63
  23. package/test_proxies/node_modules/asynckit/parallel.js +0 -43
  24. package/test_proxies/node_modules/asynckit/serial.js +0 -17
  25. package/test_proxies/node_modules/asynckit/serialOrdered.js +0 -75
  26. package/test_proxies/node_modules/asynckit/stream.js +0 -21
  27. package/test_proxies/node_modules/axios/CHANGELOG.md +0 -790
  28. package/test_proxies/node_modules/axios/LICENSE +0 -7
  29. package/test_proxies/node_modules/axios/MIGRATION_GUIDE.md +0 -3
  30. package/test_proxies/node_modules/axios/README.md +0 -1605
  31. package/test_proxies/node_modules/axios/SECURITY.md +0 -6
  32. package/test_proxies/node_modules/axios/dist/axios.js +0 -3061
  33. package/test_proxies/node_modules/axios/dist/axios.js.map +0 -1
  34. package/test_proxies/node_modules/axios/dist/axios.min.js +0 -2
  35. package/test_proxies/node_modules/axios/dist/axios.min.js.map +0 -1
  36. package/test_proxies/node_modules/axios/dist/browser/axios.cjs +0 -3234
  37. package/test_proxies/node_modules/axios/dist/browser/axios.cjs.map +0 -1
  38. package/test_proxies/node_modules/axios/dist/esm/axios.js +0 -3257
  39. package/test_proxies/node_modules/axios/dist/esm/axios.js.map +0 -1
  40. package/test_proxies/node_modules/axios/dist/esm/axios.min.js +0 -2
  41. package/test_proxies/node_modules/axios/dist/esm/axios.min.js.map +0 -1
  42. package/test_proxies/node_modules/axios/dist/node/axios.cjs +0 -4327
  43. package/test_proxies/node_modules/axios/dist/node/axios.cjs.map +0 -1
  44. package/test_proxies/node_modules/axios/index.d.cts +0 -542
  45. package/test_proxies/node_modules/axios/index.d.ts +0 -559
  46. package/test_proxies/node_modules/axios/index.js +0 -43
  47. package/test_proxies/node_modules/axios/lib/adapters/README.md +0 -37
  48. package/test_proxies/node_modules/axios/lib/adapters/adapters.js +0 -77
  49. package/test_proxies/node_modules/axios/lib/adapters/http.js +0 -681
  50. package/test_proxies/node_modules/axios/lib/adapters/xhr.js +0 -260
  51. package/test_proxies/node_modules/axios/lib/axios.js +0 -89
  52. package/test_proxies/node_modules/axios/lib/cancel/CancelToken.js +0 -121
  53. package/test_proxies/node_modules/axios/lib/cancel/CanceledError.js +0 -25
  54. package/test_proxies/node_modules/axios/lib/cancel/isCancel.js +0 -5
  55. package/test_proxies/node_modules/axios/lib/core/Axios.js +0 -201
  56. package/test_proxies/node_modules/axios/lib/core/AxiosError.js +0 -100
  57. package/test_proxies/node_modules/axios/lib/core/AxiosHeaders.js +0 -298
  58. package/test_proxies/node_modules/axios/lib/core/InterceptorManager.js +0 -71
  59. package/test_proxies/node_modules/axios/lib/core/README.md +0 -8
  60. package/test_proxies/node_modules/axios/lib/core/buildFullPath.js +0 -21
  61. package/test_proxies/node_modules/axios/lib/core/dispatchRequest.js +0 -81
  62. package/test_proxies/node_modules/axios/lib/core/mergeConfig.js +0 -106
  63. package/test_proxies/node_modules/axios/lib/core/settle.js +0 -27
  64. package/test_proxies/node_modules/axios/lib/core/transformData.js +0 -28
  65. package/test_proxies/node_modules/axios/lib/defaults/index.js +0 -159
  66. package/test_proxies/node_modules/axios/lib/defaults/transitional.js +0 -7
  67. package/test_proxies/node_modules/axios/lib/env/README.md +0 -3
  68. package/test_proxies/node_modules/axios/lib/env/classes/FormData.js +0 -2
  69. package/test_proxies/node_modules/axios/lib/env/data.js +0 -1
  70. package/test_proxies/node_modules/axios/lib/helpers/AxiosTransformStream.js +0 -191
  71. package/test_proxies/node_modules/axios/lib/helpers/AxiosURLSearchParams.js +0 -58
  72. package/test_proxies/node_modules/axios/lib/helpers/HttpStatusCode.js +0 -71
  73. package/test_proxies/node_modules/axios/lib/helpers/README.md +0 -7
  74. package/test_proxies/node_modules/axios/lib/helpers/ZlibHeaderTransformStream.js +0 -28
  75. package/test_proxies/node_modules/axios/lib/helpers/bind.js +0 -7
  76. package/test_proxies/node_modules/axios/lib/helpers/buildURL.js +0 -63
  77. package/test_proxies/node_modules/axios/lib/helpers/callbackify.js +0 -16
  78. package/test_proxies/node_modules/axios/lib/helpers/combineURLs.js +0 -15
  79. package/test_proxies/node_modules/axios/lib/helpers/cookies.js +0 -42
  80. package/test_proxies/node_modules/axios/lib/helpers/deprecatedMethod.js +0 -26
  81. package/test_proxies/node_modules/axios/lib/helpers/formDataToJSON.js +0 -92
  82. package/test_proxies/node_modules/axios/lib/helpers/formDataToStream.js +0 -111
  83. package/test_proxies/node_modules/axios/lib/helpers/fromDataURI.js +0 -53
  84. package/test_proxies/node_modules/axios/lib/helpers/isAbsoluteURL.js +0 -15
  85. package/test_proxies/node_modules/axios/lib/helpers/isAxiosError.js +0 -14
  86. package/test_proxies/node_modules/axios/lib/helpers/isURLSameOrigin.js +0 -67
  87. package/test_proxies/node_modules/axios/lib/helpers/null.js +0 -2
  88. package/test_proxies/node_modules/axios/lib/helpers/parseHeaders.js +0 -55
  89. package/test_proxies/node_modules/axios/lib/helpers/parseProtocol.js +0 -6
  90. package/test_proxies/node_modules/axios/lib/helpers/readBlob.js +0 -15
  91. package/test_proxies/node_modules/axios/lib/helpers/speedometer.js +0 -55
  92. package/test_proxies/node_modules/axios/lib/helpers/spread.js +0 -28
  93. package/test_proxies/node_modules/axios/lib/helpers/throttle.js +0 -33
  94. package/test_proxies/node_modules/axios/lib/helpers/toFormData.js +0 -219
  95. package/test_proxies/node_modules/axios/lib/helpers/toURLEncodedForm.js +0 -18
  96. package/test_proxies/node_modules/axios/lib/helpers/validator.js +0 -91
  97. package/test_proxies/node_modules/axios/lib/platform/browser/classes/Blob.js +0 -3
  98. package/test_proxies/node_modules/axios/lib/platform/browser/classes/FormData.js +0 -3
  99. package/test_proxies/node_modules/axios/lib/platform/browser/classes/URLSearchParams.js +0 -4
  100. package/test_proxies/node_modules/axios/lib/platform/browser/index.js +0 -13
  101. package/test_proxies/node_modules/axios/lib/platform/common/utils.js +0 -47
  102. package/test_proxies/node_modules/axios/lib/platform/index.js +0 -7
  103. package/test_proxies/node_modules/axios/lib/platform/node/classes/FormData.js +0 -3
  104. package/test_proxies/node_modules/axios/lib/platform/node/classes/URLSearchParams.js +0 -4
  105. package/test_proxies/node_modules/axios/lib/platform/node/index.js +0 -12
  106. package/test_proxies/node_modules/axios/lib/utils.js +0 -723
  107. package/test_proxies/node_modules/axios/package.json +0 -218
  108. package/test_proxies/node_modules/combined-stream/License +0 -19
  109. package/test_proxies/node_modules/combined-stream/Readme.md +0 -138
  110. package/test_proxies/node_modules/combined-stream/lib/combined_stream.js +0 -208
  111. package/test_proxies/node_modules/combined-stream/package.json +0 -25
  112. package/test_proxies/node_modules/combined-stream/yarn.lock +0 -17
  113. package/test_proxies/node_modules/delayed-stream/License +0 -19
  114. package/test_proxies/node_modules/delayed-stream/Makefile +0 -7
  115. package/test_proxies/node_modules/delayed-stream/Readme.md +0 -141
  116. package/test_proxies/node_modules/delayed-stream/lib/delayed_stream.js +0 -107
  117. package/test_proxies/node_modules/delayed-stream/package.json +0 -27
  118. package/test_proxies/node_modules/follow-redirects/LICENSE +0 -18
  119. package/test_proxies/node_modules/follow-redirects/README.md +0 -155
  120. package/test_proxies/node_modules/follow-redirects/debug.js +0 -15
  121. package/test_proxies/node_modules/follow-redirects/http.js +0 -1
  122. package/test_proxies/node_modules/follow-redirects/https.js +0 -1
  123. package/test_proxies/node_modules/follow-redirects/index.js +0 -633
  124. package/test_proxies/node_modules/follow-redirects/package.json +0 -58
  125. package/test_proxies/node_modules/form-data/License +0 -19
  126. package/test_proxies/node_modules/form-data/README.md.bak +0 -358
  127. package/test_proxies/node_modules/form-data/Readme.md +0 -358
  128. package/test_proxies/node_modules/form-data/index.d.ts +0 -62
  129. package/test_proxies/node_modules/form-data/lib/browser.js +0 -2
  130. package/test_proxies/node_modules/form-data/lib/form_data.js +0 -501
  131. package/test_proxies/node_modules/form-data/lib/populate.js +0 -10
  132. package/test_proxies/node_modules/form-data/package.json +0 -68
  133. package/test_proxies/node_modules/mime-db/HISTORY.md +0 -507
  134. package/test_proxies/node_modules/mime-db/LICENSE +0 -23
  135. package/test_proxies/node_modules/mime-db/README.md +0 -100
  136. package/test_proxies/node_modules/mime-db/db.json +0 -8519
  137. package/test_proxies/node_modules/mime-db/index.js +0 -12
  138. package/test_proxies/node_modules/mime-db/package.json +0 -60
  139. package/test_proxies/node_modules/mime-types/HISTORY.md +0 -397
  140. package/test_proxies/node_modules/mime-types/LICENSE +0 -23
  141. package/test_proxies/node_modules/mime-types/README.md +0 -113
  142. package/test_proxies/node_modules/mime-types/index.js +0 -188
  143. package/test_proxies/node_modules/mime-types/package.json +0 -44
  144. package/test_proxies/node_modules/proxy-from-env/.eslintrc +0 -29
  145. package/test_proxies/node_modules/proxy-from-env/.travis.yml +0 -10
  146. package/test_proxies/node_modules/proxy-from-env/LICENSE +0 -20
  147. package/test_proxies/node_modules/proxy-from-env/README.md +0 -131
  148. package/test_proxies/node_modules/proxy-from-env/index.js +0 -108
  149. package/test_proxies/node_modules/proxy-from-env/package.json +0 -34
  150. package/test_proxies/node_modules/proxy-from-env/test.js +0 -483
  151. package/test_proxies/package-lock.json +0 -106
  152. package/test_proxies/package.json +0 -14
  153. package/test_proxies/proxies.txt +0 -7000
  154. package/test_proxies/test_proxies.js +0 -62
package/README.md CHANGED
@@ -100,6 +100,18 @@ const proxyRotator = new ProxyRotator(proxies, options);
100
100
  - assume_aliveness: Specifies whether to assume all proxies are alive when first added instead of 'new'. Default: false.
101
101
  - check_on_next: Specifies whether to check if proxies are alive when they are added to the queue. Default: false.
102
102
 
103
+ ## Testing your Proxies
104
+
105
+ ```javascript
106
+ import ProxyRotator from 'proxy-rotator-js'
107
+
108
+ let proxies = ['proxy1', 'proxy2', 'proxy3']
109
+
110
+ let rotator = new ProxyRotator(proxies, options={})
111
+
112
+ rotator.test()
113
+ ```
114
+
103
115
  ## Getting Started
104
116
 
105
117
  To use the ProxyRotator class in your JavaScript project, follow these steps:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "proxy-rotator-js",
3
- "version": "1.0.9",
3
+ "version": "1.1.1",
4
4
  "type": "module",
5
5
  "description": "Proxy Rotator",
6
6
  "main": "index.js",
@@ -14,6 +14,7 @@
14
14
  "anymatch": "^3.1.3",
15
15
  "argparse": "^2.0.1",
16
16
  "assertion-error": "^1.1.0",
17
+ "axios": "^1.6.8",
17
18
  "balanced-match": "^1.0.2",
18
19
  "binary-extensions": "^2.2.0",
19
20
  "brace-expansion": "^2.0.1",
@@ -1,6 +1,7 @@
1
1
  import fs from 'fs';
2
2
  import Queue from './Queue.js';
3
3
  import Proxy from './Proxy.js';
4
+ import makeRequestWithProxy from './utils/makeRequestWithProxy.js';
4
5
 
5
6
  class ProxyRotator {
6
7
  constructor(proxies, options={} ){
@@ -202,6 +203,37 @@ class ProxyRotator {
202
203
  this.resurect(proxy);
203
204
  }
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
+
205
237
  }
206
238
 
207
239
  export default ProxyRotator
@@ -0,0 +1,21 @@
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;
@@ -0,0 +1,22 @@
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
+
@@ -1,98 +0,0 @@
1
- {
2
- "name": "test_proxies",
3
- "version": "1.0.0",
4
- "lockfileVersion": 3,
5
- "requires": true,
6
- "packages": {
7
- "node_modules/asynckit": {
8
- "version": "0.4.0",
9
- "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
10
- "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
11
- },
12
- "node_modules/axios": {
13
- "version": "1.6.2",
14
- "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz",
15
- "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==",
16
- "dependencies": {
17
- "follow-redirects": "^1.15.0",
18
- "form-data": "^4.0.0",
19
- "proxy-from-env": "^1.1.0"
20
- }
21
- },
22
- "node_modules/combined-stream": {
23
- "version": "1.0.8",
24
- "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
25
- "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
26
- "dependencies": {
27
- "delayed-stream": "~1.0.0"
28
- },
29
- "engines": {
30
- "node": ">= 0.8"
31
- }
32
- },
33
- "node_modules/delayed-stream": {
34
- "version": "1.0.0",
35
- "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
36
- "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
37
- "engines": {
38
- "node": ">=0.4.0"
39
- }
40
- },
41
- "node_modules/follow-redirects": {
42
- "version": "1.15.3",
43
- "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz",
44
- "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==",
45
- "funding": [
46
- {
47
- "type": "individual",
48
- "url": "https://github.com/sponsors/RubenVerborgh"
49
- }
50
- ],
51
- "engines": {
52
- "node": ">=4.0"
53
- },
54
- "peerDependenciesMeta": {
55
- "debug": {
56
- "optional": true
57
- }
58
- }
59
- },
60
- "node_modules/form-data": {
61
- "version": "4.0.0",
62
- "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
63
- "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
64
- "dependencies": {
65
- "asynckit": "^0.4.0",
66
- "combined-stream": "^1.0.8",
67
- "mime-types": "^2.1.12"
68
- },
69
- "engines": {
70
- "node": ">= 6"
71
- }
72
- },
73
- "node_modules/mime-db": {
74
- "version": "1.52.0",
75
- "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
76
- "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
77
- "engines": {
78
- "node": ">= 0.6"
79
- }
80
- },
81
- "node_modules/mime-types": {
82
- "version": "2.1.35",
83
- "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
84
- "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
85
- "dependencies": {
86
- "mime-db": "1.52.0"
87
- },
88
- "engines": {
89
- "node": ">= 0.6"
90
- }
91
- },
92
- "node_modules/proxy-from-env": {
93
- "version": "1.1.0",
94
- "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
95
- "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
96
- }
97
- }
98
- }
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2016 Alex Indigo
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,233 +0,0 @@
1
- # asynckit [![NPM Module](https://img.shields.io/npm/v/asynckit.svg?style=flat)](https://www.npmjs.com/package/asynckit)
2
-
3
- Minimal async jobs utility library, with streams support.
4
-
5
- [![PhantomJS Build](https://img.shields.io/travis/alexindigo/asynckit/v0.4.0.svg?label=browser&style=flat)](https://travis-ci.org/alexindigo/asynckit)
6
- [![Linux Build](https://img.shields.io/travis/alexindigo/asynckit/v0.4.0.svg?label=linux:0.12-6.x&style=flat)](https://travis-ci.org/alexindigo/asynckit)
7
- [![Windows Build](https://img.shields.io/appveyor/ci/alexindigo/asynckit/v0.4.0.svg?label=windows:0.12-6.x&style=flat)](https://ci.appveyor.com/project/alexindigo/asynckit)
8
-
9
- [![Coverage Status](https://img.shields.io/coveralls/alexindigo/asynckit/v0.4.0.svg?label=code+coverage&style=flat)](https://coveralls.io/github/alexindigo/asynckit?branch=master)
10
- [![Dependency Status](https://img.shields.io/david/alexindigo/asynckit/v0.4.0.svg?style=flat)](https://david-dm.org/alexindigo/asynckit)
11
- [![bitHound Overall Score](https://www.bithound.io/github/alexindigo/asynckit/badges/score.svg)](https://www.bithound.io/github/alexindigo/asynckit)
12
-
13
- <!-- [![Readme](https://img.shields.io/badge/readme-tested-brightgreen.svg?style=flat)](https://www.npmjs.com/package/reamde) -->
14
-
15
- AsyncKit provides harness for `parallel` and `serial` iterators over list of items represented by arrays or objects.
16
- Optionally it accepts abort function (should be synchronously return by iterator for each item), and terminates left over jobs upon an error event. For specific iteration order built-in (`ascending` and `descending`) and custom sort helpers also supported, via `asynckit.serialOrdered` method.
17
-
18
- It ensures async operations to keep behavior more stable and prevent `Maximum call stack size exceeded` errors, from sync iterators.
19
-
20
- | compression | size |
21
- | :----------------- | -------: |
22
- | asynckit.js | 12.34 kB |
23
- | asynckit.min.js | 4.11 kB |
24
- | asynckit.min.js.gz | 1.47 kB |
25
-
26
-
27
- ## Install
28
-
29
- ```sh
30
- $ npm install --save asynckit
31
- ```
32
-
33
- ## Examples
34
-
35
- ### Parallel Jobs
36
-
37
- Runs iterator over provided array in parallel. Stores output in the `result` array,
38
- on the matching positions. In unlikely event of an error from one of the jobs,
39
- will terminate rest of the active jobs (if abort function is provided)
40
- and return error along with salvaged data to the main callback function.
41
-
42
- #### Input Array
43
-
44
- ```javascript
45
- var parallel = require('asynckit').parallel
46
- , assert = require('assert')
47
- ;
48
-
49
- var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ]
50
- , expectedResult = [ 2, 2, 8, 32, 128, 64, 16, 4 ]
51
- , expectedTarget = [ 1, 1, 2, 4, 8, 16, 32, 64 ]
52
- , target = []
53
- ;
54
-
55
- parallel(source, asyncJob, function(err, result)
56
- {
57
- assert.deepEqual(result, expectedResult);
58
- assert.deepEqual(target, expectedTarget);
59
- });
60
-
61
- // async job accepts one element from the array
62
- // and a callback function
63
- function asyncJob(item, cb)
64
- {
65
- // different delays (in ms) per item
66
- var delay = item * 25;
67
-
68
- // pretend different jobs take different time to finish
69
- // and not in consequential order
70
- var timeoutId = setTimeout(function() {
71
- target.push(item);
72
- cb(null, item * 2);
73
- }, delay);
74
-
75
- // allow to cancel "leftover" jobs upon error
76
- // return function, invoking of which will abort this job
77
- return clearTimeout.bind(null, timeoutId);
78
- }
79
- ```
80
-
81
- More examples could be found in [test/test-parallel-array.js](test/test-parallel-array.js).
82
-
83
- #### Input Object
84
-
85
- Also it supports named jobs, listed via object.
86
-
87
- ```javascript
88
- var parallel = require('asynckit/parallel')
89
- , assert = require('assert')
90
- ;
91
-
92
- var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 }
93
- , expectedResult = { first: 2, one: 2, four: 8, sixteen: 32, sixtyFour: 128, thirtyTwo: 64, eight: 16, two: 4 }
94
- , expectedTarget = [ 1, 1, 2, 4, 8, 16, 32, 64 ]
95
- , expectedKeys = [ 'first', 'one', 'two', 'four', 'eight', 'sixteen', 'thirtyTwo', 'sixtyFour' ]
96
- , target = []
97
- , keys = []
98
- ;
99
-
100
- parallel(source, asyncJob, function(err, result)
101
- {
102
- assert.deepEqual(result, expectedResult);
103
- assert.deepEqual(target, expectedTarget);
104
- assert.deepEqual(keys, expectedKeys);
105
- });
106
-
107
- // supports full value, key, callback (shortcut) interface
108
- function asyncJob(item, key, cb)
109
- {
110
- // different delays (in ms) per item
111
- var delay = item * 25;
112
-
113
- // pretend different jobs take different time to finish
114
- // and not in consequential order
115
- var timeoutId = setTimeout(function() {
116
- keys.push(key);
117
- target.push(item);
118
- cb(null, item * 2);
119
- }, delay);
120
-
121
- // allow to cancel "leftover" jobs upon error
122
- // return function, invoking of which will abort this job
123
- return clearTimeout.bind(null, timeoutId);
124
- }
125
- ```
126
-
127
- More examples could be found in [test/test-parallel-object.js](test/test-parallel-object.js).
128
-
129
- ### Serial Jobs
130
-
131
- Runs iterator over provided array sequentially. Stores output in the `result` array,
132
- on the matching positions. In unlikely event of an error from one of the jobs,
133
- will not proceed to the rest of the items in the list
134
- and return error along with salvaged data to the main callback function.
135
-
136
- #### Input Array
137
-
138
- ```javascript
139
- var serial = require('asynckit/serial')
140
- , assert = require('assert')
141
- ;
142
-
143
- var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ]
144
- , expectedResult = [ 2, 2, 8, 32, 128, 64, 16, 4 ]
145
- , expectedTarget = [ 0, 1, 2, 3, 4, 5, 6, 7 ]
146
- , target = []
147
- ;
148
-
149
- serial(source, asyncJob, function(err, result)
150
- {
151
- assert.deepEqual(result, expectedResult);
152
- assert.deepEqual(target, expectedTarget);
153
- });
154
-
155
- // extended interface (item, key, callback)
156
- // also supported for arrays
157
- function asyncJob(item, key, cb)
158
- {
159
- target.push(key);
160
-
161
- // it will be automatically made async
162
- // even it iterator "returns" in the same event loop
163
- cb(null, item * 2);
164
- }
165
- ```
166
-
167
- More examples could be found in [test/test-serial-array.js](test/test-serial-array.js).
168
-
169
- #### Input Object
170
-
171
- Also it supports named jobs, listed via object.
172
-
173
- ```javascript
174
- var serial = require('asynckit').serial
175
- , assert = require('assert')
176
- ;
177
-
178
- var source = [ 1, 1, 4, 16, 64, 32, 8, 2 ]
179
- , expectedResult = [ 2, 2, 8, 32, 128, 64, 16, 4 ]
180
- , expectedTarget = [ 0, 1, 2, 3, 4, 5, 6, 7 ]
181
- , target = []
182
- ;
183
-
184
- var source = { first: 1, one: 1, four: 4, sixteen: 16, sixtyFour: 64, thirtyTwo: 32, eight: 8, two: 2 }
185
- , expectedResult = { first: 2, one: 2, four: 8, sixteen: 32, sixtyFour: 128, thirtyTwo: 64, eight: 16, two: 4 }
186
- , expectedTarget = [ 1, 1, 4, 16, 64, 32, 8, 2 ]
187
- , target = []
188
- ;
189
-
190
-
191
- serial(source, asyncJob, function(err, result)
192
- {
193
- assert.deepEqual(result, expectedResult);
194
- assert.deepEqual(target, expectedTarget);
195
- });
196
-
197
- // shortcut interface (item, callback)
198
- // works for object as well as for the arrays
199
- function asyncJob(item, cb)
200
- {
201
- target.push(item);
202
-
203
- // it will be automatically made async
204
- // even it iterator "returns" in the same event loop
205
- cb(null, item * 2);
206
- }
207
- ```
208
-
209
- More examples could be found in [test/test-serial-object.js](test/test-serial-object.js).
210
-
211
- _Note: Since _object_ is an _unordered_ collection of properties,
212
- it may produce unexpected results with sequential iterations.
213
- Whenever order of the jobs' execution is important please use `serialOrdered` method._
214
-
215
- ### Ordered Serial Iterations
216
-
217
- TBD
218
-
219
- For example [compare-property](compare-property) package.
220
-
221
- ### Streaming interface
222
-
223
- TBD
224
-
225
- ## Want to Know More?
226
-
227
- More examples can be found in [test folder](test/).
228
-
229
- Or open an [issue](https://github.com/alexindigo/asynckit/issues) with questions and/or suggestions.
230
-
231
- ## License
232
-
233
- AsyncKit is licensed under the MIT license.
@@ -1,76 +0,0 @@
1
- /* eslint no-console: "off" */
2
-
3
- var asynckit = require('./')
4
- , async = require('async')
5
- , assert = require('assert')
6
- , expected = 0
7
- ;
8
-
9
- var Benchmark = require('benchmark');
10
- var suite = new Benchmark.Suite;
11
-
12
- var source = [];
13
- for (var z = 1; z < 100; z++)
14
- {
15
- source.push(z);
16
- expected += z;
17
- }
18
-
19
- suite
20
- // add tests
21
-
22
- .add('async.map', function(deferred)
23
- {
24
- var total = 0;
25
-
26
- async.map(source,
27
- function(i, cb)
28
- {
29
- setImmediate(function()
30
- {
31
- total += i;
32
- cb(null, total);
33
- });
34
- },
35
- function(err, result)
36
- {
37
- assert.ifError(err);
38
- assert.equal(result[result.length - 1], expected);
39
- deferred.resolve();
40
- });
41
- }, {'defer': true})
42
-
43
-
44
- .add('asynckit.parallel', function(deferred)
45
- {
46
- var total = 0;
47
-
48
- asynckit.parallel(source,
49
- function(i, cb)
50
- {
51
- setImmediate(function()
52
- {
53
- total += i;
54
- cb(null, total);
55
- });
56
- },
57
- function(err, result)
58
- {
59
- assert.ifError(err);
60
- assert.equal(result[result.length - 1], expected);
61
- deferred.resolve();
62
- });
63
- }, {'defer': true})
64
-
65
-
66
- // add listeners
67
- .on('cycle', function(ev)
68
- {
69
- console.log(String(ev.target));
70
- })
71
- .on('complete', function()
72
- {
73
- console.log('Fastest is ' + this.filter('fastest').map('name'));
74
- })
75
- // run async
76
- .run({ 'async': true });
@@ -1,6 +0,0 @@
1
- module.exports =
2
- {
3
- parallel : require('./parallel.js'),
4
- serial : require('./serial.js'),
5
- serialOrdered : require('./serialOrdered.js')
6
- };
@@ -1,29 +0,0 @@
1
- // API
2
- module.exports = abort;
3
-
4
- /**
5
- * Aborts leftover active jobs
6
- *
7
- * @param {object} state - current state object
8
- */
9
- function abort(state)
10
- {
11
- Object.keys(state.jobs).forEach(clean.bind(state));
12
-
13
- // reset leftover jobs
14
- state.jobs = {};
15
- }
16
-
17
- /**
18
- * Cleans up leftover job by invoking abort function for the provided job id
19
- *
20
- * @this state
21
- * @param {string|number} key - job id to abort
22
- */
23
- function clean(key)
24
- {
25
- if (typeof this.jobs[key] == 'function')
26
- {
27
- this.jobs[key]();
28
- }
29
- }
@@ -1,34 +0,0 @@
1
- var defer = require('./defer.js');
2
-
3
- // API
4
- module.exports = async;
5
-
6
- /**
7
- * Runs provided callback asynchronously
8
- * even if callback itself is not
9
- *
10
- * @param {function} callback - callback to invoke
11
- * @returns {function} - augmented callback
12
- */
13
- function async(callback)
14
- {
15
- var isAsync = false;
16
-
17
- // check if async happened
18
- defer(function() { isAsync = true; });
19
-
20
- return function async_callback(err, result)
21
- {
22
- if (isAsync)
23
- {
24
- callback(err, result);
25
- }
26
- else
27
- {
28
- defer(function nextTick_callback()
29
- {
30
- callback(err, result);
31
- });
32
- }
33
- };
34
- }
@@ -1,26 +0,0 @@
1
- module.exports = defer;
2
-
3
- /**
4
- * Runs provided function on next iteration of the event loop
5
- *
6
- * @param {function} fn - function to run
7
- */
8
- function defer(fn)
9
- {
10
- var nextTick = typeof setImmediate == 'function'
11
- ? setImmediate
12
- : (
13
- typeof process == 'object' && typeof process.nextTick == 'function'
14
- ? process.nextTick
15
- : null
16
- );
17
-
18
- if (nextTick)
19
- {
20
- nextTick(fn);
21
- }
22
- else
23
- {
24
- setTimeout(fn, 0);
25
- }
26
- }