proxy-rotator-js 1.0.8 → 1.0.9

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 (153) hide show
  1. package/README.md +23 -0
  2. package/package.json +1 -1
  3. package/src/ProxyRotator.js +7 -1
  4. package/test/options.js +32 -0
  5. package/test_proxies/node_modules/.package-lock.json +98 -0
  6. package/test_proxies/node_modules/asynckit/LICENSE +21 -0
  7. package/test_proxies/node_modules/asynckit/README.md +233 -0
  8. package/test_proxies/node_modules/asynckit/bench.js +76 -0
  9. package/test_proxies/node_modules/asynckit/index.js +6 -0
  10. package/test_proxies/node_modules/asynckit/lib/abort.js +29 -0
  11. package/test_proxies/node_modules/asynckit/lib/async.js +34 -0
  12. package/test_proxies/node_modules/asynckit/lib/defer.js +26 -0
  13. package/test_proxies/node_modules/asynckit/lib/iterate.js +75 -0
  14. package/test_proxies/node_modules/asynckit/lib/readable_asynckit.js +91 -0
  15. package/test_proxies/node_modules/asynckit/lib/readable_parallel.js +25 -0
  16. package/test_proxies/node_modules/asynckit/lib/readable_serial.js +25 -0
  17. package/test_proxies/node_modules/asynckit/lib/readable_serial_ordered.js +29 -0
  18. package/test_proxies/node_modules/asynckit/lib/state.js +37 -0
  19. package/test_proxies/node_modules/asynckit/lib/streamify.js +141 -0
  20. package/test_proxies/node_modules/asynckit/lib/terminator.js +29 -0
  21. package/test_proxies/node_modules/asynckit/package.json +63 -0
  22. package/test_proxies/node_modules/asynckit/parallel.js +43 -0
  23. package/test_proxies/node_modules/asynckit/serial.js +17 -0
  24. package/test_proxies/node_modules/asynckit/serialOrdered.js +75 -0
  25. package/test_proxies/node_modules/asynckit/stream.js +21 -0
  26. package/test_proxies/node_modules/axios/CHANGELOG.md +790 -0
  27. package/test_proxies/node_modules/axios/LICENSE +7 -0
  28. package/test_proxies/node_modules/axios/MIGRATION_GUIDE.md +3 -0
  29. package/test_proxies/node_modules/axios/README.md +1605 -0
  30. package/test_proxies/node_modules/axios/SECURITY.md +6 -0
  31. package/test_proxies/node_modules/axios/dist/axios.js +3061 -0
  32. package/test_proxies/node_modules/axios/dist/axios.js.map +1 -0
  33. package/test_proxies/node_modules/axios/dist/axios.min.js +2 -0
  34. package/test_proxies/node_modules/axios/dist/axios.min.js.map +1 -0
  35. package/test_proxies/node_modules/axios/dist/browser/axios.cjs +3234 -0
  36. package/test_proxies/node_modules/axios/dist/browser/axios.cjs.map +1 -0
  37. package/test_proxies/node_modules/axios/dist/esm/axios.js +3257 -0
  38. package/test_proxies/node_modules/axios/dist/esm/axios.js.map +1 -0
  39. package/test_proxies/node_modules/axios/dist/esm/axios.min.js +2 -0
  40. package/test_proxies/node_modules/axios/dist/esm/axios.min.js.map +1 -0
  41. package/test_proxies/node_modules/axios/dist/node/axios.cjs +4327 -0
  42. package/test_proxies/node_modules/axios/dist/node/axios.cjs.map +1 -0
  43. package/test_proxies/node_modules/axios/index.d.cts +542 -0
  44. package/test_proxies/node_modules/axios/index.d.ts +559 -0
  45. package/test_proxies/node_modules/axios/index.js +43 -0
  46. package/test_proxies/node_modules/axios/lib/adapters/README.md +37 -0
  47. package/test_proxies/node_modules/axios/lib/adapters/adapters.js +77 -0
  48. package/test_proxies/node_modules/axios/lib/adapters/http.js +681 -0
  49. package/test_proxies/node_modules/axios/lib/adapters/xhr.js +260 -0
  50. package/test_proxies/node_modules/axios/lib/axios.js +89 -0
  51. package/test_proxies/node_modules/axios/lib/cancel/CancelToken.js +121 -0
  52. package/test_proxies/node_modules/axios/lib/cancel/CanceledError.js +25 -0
  53. package/test_proxies/node_modules/axios/lib/cancel/isCancel.js +5 -0
  54. package/test_proxies/node_modules/axios/lib/core/Axios.js +201 -0
  55. package/test_proxies/node_modules/axios/lib/core/AxiosError.js +100 -0
  56. package/test_proxies/node_modules/axios/lib/core/AxiosHeaders.js +298 -0
  57. package/test_proxies/node_modules/axios/lib/core/InterceptorManager.js +71 -0
  58. package/test_proxies/node_modules/axios/lib/core/README.md +8 -0
  59. package/test_proxies/node_modules/axios/lib/core/buildFullPath.js +21 -0
  60. package/test_proxies/node_modules/axios/lib/core/dispatchRequest.js +81 -0
  61. package/test_proxies/node_modules/axios/lib/core/mergeConfig.js +106 -0
  62. package/test_proxies/node_modules/axios/lib/core/settle.js +27 -0
  63. package/test_proxies/node_modules/axios/lib/core/transformData.js +28 -0
  64. package/test_proxies/node_modules/axios/lib/defaults/index.js +159 -0
  65. package/test_proxies/node_modules/axios/lib/defaults/transitional.js +7 -0
  66. package/test_proxies/node_modules/axios/lib/env/README.md +3 -0
  67. package/test_proxies/node_modules/axios/lib/env/classes/FormData.js +2 -0
  68. package/test_proxies/node_modules/axios/lib/env/data.js +1 -0
  69. package/test_proxies/node_modules/axios/lib/helpers/AxiosTransformStream.js +191 -0
  70. package/test_proxies/node_modules/axios/lib/helpers/AxiosURLSearchParams.js +58 -0
  71. package/test_proxies/node_modules/axios/lib/helpers/HttpStatusCode.js +71 -0
  72. package/test_proxies/node_modules/axios/lib/helpers/README.md +7 -0
  73. package/test_proxies/node_modules/axios/lib/helpers/ZlibHeaderTransformStream.js +28 -0
  74. package/test_proxies/node_modules/axios/lib/helpers/bind.js +7 -0
  75. package/test_proxies/node_modules/axios/lib/helpers/buildURL.js +63 -0
  76. package/test_proxies/node_modules/axios/lib/helpers/callbackify.js +16 -0
  77. package/test_proxies/node_modules/axios/lib/helpers/combineURLs.js +15 -0
  78. package/test_proxies/node_modules/axios/lib/helpers/cookies.js +42 -0
  79. package/test_proxies/node_modules/axios/lib/helpers/deprecatedMethod.js +26 -0
  80. package/test_proxies/node_modules/axios/lib/helpers/formDataToJSON.js +92 -0
  81. package/test_proxies/node_modules/axios/lib/helpers/formDataToStream.js +111 -0
  82. package/test_proxies/node_modules/axios/lib/helpers/fromDataURI.js +53 -0
  83. package/test_proxies/node_modules/axios/lib/helpers/isAbsoluteURL.js +15 -0
  84. package/test_proxies/node_modules/axios/lib/helpers/isAxiosError.js +14 -0
  85. package/test_proxies/node_modules/axios/lib/helpers/isURLSameOrigin.js +67 -0
  86. package/test_proxies/node_modules/axios/lib/helpers/null.js +2 -0
  87. package/test_proxies/node_modules/axios/lib/helpers/parseHeaders.js +55 -0
  88. package/test_proxies/node_modules/axios/lib/helpers/parseProtocol.js +6 -0
  89. package/test_proxies/node_modules/axios/lib/helpers/readBlob.js +15 -0
  90. package/test_proxies/node_modules/axios/lib/helpers/speedometer.js +55 -0
  91. package/test_proxies/node_modules/axios/lib/helpers/spread.js +28 -0
  92. package/test_proxies/node_modules/axios/lib/helpers/throttle.js +33 -0
  93. package/test_proxies/node_modules/axios/lib/helpers/toFormData.js +219 -0
  94. package/test_proxies/node_modules/axios/lib/helpers/toURLEncodedForm.js +18 -0
  95. package/test_proxies/node_modules/axios/lib/helpers/validator.js +91 -0
  96. package/test_proxies/node_modules/axios/lib/platform/browser/classes/Blob.js +3 -0
  97. package/test_proxies/node_modules/axios/lib/platform/browser/classes/FormData.js +3 -0
  98. package/test_proxies/node_modules/axios/lib/platform/browser/classes/URLSearchParams.js +4 -0
  99. package/test_proxies/node_modules/axios/lib/platform/browser/index.js +13 -0
  100. package/test_proxies/node_modules/axios/lib/platform/common/utils.js +47 -0
  101. package/test_proxies/node_modules/axios/lib/platform/index.js +7 -0
  102. package/test_proxies/node_modules/axios/lib/platform/node/classes/FormData.js +3 -0
  103. package/test_proxies/node_modules/axios/lib/platform/node/classes/URLSearchParams.js +4 -0
  104. package/test_proxies/node_modules/axios/lib/platform/node/index.js +12 -0
  105. package/test_proxies/node_modules/axios/lib/utils.js +723 -0
  106. package/test_proxies/node_modules/axios/package.json +218 -0
  107. package/test_proxies/node_modules/combined-stream/License +19 -0
  108. package/test_proxies/node_modules/combined-stream/Readme.md +138 -0
  109. package/test_proxies/node_modules/combined-stream/lib/combined_stream.js +208 -0
  110. package/test_proxies/node_modules/combined-stream/package.json +25 -0
  111. package/test_proxies/node_modules/combined-stream/yarn.lock +17 -0
  112. package/test_proxies/node_modules/delayed-stream/License +19 -0
  113. package/test_proxies/node_modules/delayed-stream/Makefile +7 -0
  114. package/test_proxies/node_modules/delayed-stream/Readme.md +141 -0
  115. package/test_proxies/node_modules/delayed-stream/lib/delayed_stream.js +107 -0
  116. package/test_proxies/node_modules/delayed-stream/package.json +27 -0
  117. package/test_proxies/node_modules/follow-redirects/LICENSE +18 -0
  118. package/test_proxies/node_modules/follow-redirects/README.md +155 -0
  119. package/test_proxies/node_modules/follow-redirects/debug.js +15 -0
  120. package/test_proxies/node_modules/follow-redirects/http.js +1 -0
  121. package/test_proxies/node_modules/follow-redirects/https.js +1 -0
  122. package/test_proxies/node_modules/follow-redirects/index.js +633 -0
  123. package/test_proxies/node_modules/follow-redirects/package.json +58 -0
  124. package/test_proxies/node_modules/form-data/License +19 -0
  125. package/test_proxies/node_modules/form-data/README.md.bak +358 -0
  126. package/test_proxies/node_modules/form-data/Readme.md +358 -0
  127. package/test_proxies/node_modules/form-data/index.d.ts +62 -0
  128. package/test_proxies/node_modules/form-data/lib/browser.js +2 -0
  129. package/test_proxies/node_modules/form-data/lib/form_data.js +501 -0
  130. package/test_proxies/node_modules/form-data/lib/populate.js +10 -0
  131. package/test_proxies/node_modules/form-data/package.json +68 -0
  132. package/test_proxies/node_modules/mime-db/HISTORY.md +507 -0
  133. package/test_proxies/node_modules/mime-db/LICENSE +23 -0
  134. package/test_proxies/node_modules/mime-db/README.md +100 -0
  135. package/test_proxies/node_modules/mime-db/db.json +8519 -0
  136. package/test_proxies/node_modules/mime-db/index.js +12 -0
  137. package/test_proxies/node_modules/mime-db/package.json +60 -0
  138. package/test_proxies/node_modules/mime-types/HISTORY.md +397 -0
  139. package/test_proxies/node_modules/mime-types/LICENSE +23 -0
  140. package/test_proxies/node_modules/mime-types/README.md +113 -0
  141. package/test_proxies/node_modules/mime-types/index.js +188 -0
  142. package/test_proxies/node_modules/mime-types/package.json +44 -0
  143. package/test_proxies/node_modules/proxy-from-env/.eslintrc +29 -0
  144. package/test_proxies/node_modules/proxy-from-env/.travis.yml +10 -0
  145. package/test_proxies/node_modules/proxy-from-env/LICENSE +20 -0
  146. package/test_proxies/node_modules/proxy-from-env/README.md +131 -0
  147. package/test_proxies/node_modules/proxy-from-env/index.js +108 -0
  148. package/test_proxies/node_modules/proxy-from-env/package.json +34 -0
  149. package/test_proxies/node_modules/proxy-from-env/test.js +483 -0
  150. package/test_proxies/package-lock.json +106 -0
  151. package/test_proxies/package.json +14 -0
  152. package/test_proxies/proxies.txt +7000 -0
  153. package/test_proxies/test_proxies.js +62 -0
package/README.md CHANGED
@@ -77,6 +77,29 @@ remove(proxy) // Removes one or more proxies from the pool.
77
77
 
78
78
  graveyard: Stores proxies that are currently dead or inactive.
79
79
 
80
+ ## Options
81
+ ```javascript
82
+ const proxies = ['proxy1.example.com', 'proxy2.example.com'];
83
+ const options = {
84
+ returnAs: 'object',
85
+ revive_timer: 1000 * 60 * 30,
86
+ shuffle: true,
87
+ protocol: 'http',
88
+ assume_aliveness: true,
89
+ check_on_next: true
90
+ };
91
+
92
+ const proxyRotator = new ProxyRotator(proxies, options);
93
+ ```
94
+ The following options can be passed to customize the behavior of the ProxyRotator:
95
+
96
+ - returnAs: Specifies the return type of proxies. Can be either 'string' or 'object'. Default: 'string'.
97
+ - revive_timer: Specifies the duration in milliseconds before a dead proxy is revived. Default: 1000 * 60 * 30 (30 minutes).
98
+ - protocol: Specifies a protocol for all proxies. Default: null.
99
+ - shuffle: Specifies whether to shuffle the proxies before adding them to the queue. Default: false.
100
+ - assume_aliveness: Specifies whether to assume all proxies are alive when first added instead of 'new'. Default: false.
101
+ - check_on_next: Specifies whether to check if proxies are alive when they are added to the queue. Default: false.
102
+
80
103
  ## Getting Started
81
104
 
82
105
  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.8",
3
+ "version": "1.0.9",
4
4
  "type": "module",
5
5
  "description": "Proxy Rotator",
6
6
  "main": "index.js",
@@ -16,7 +16,7 @@ class ProxyRotator {
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
19
- this.shuffle = shuffle ?? true;
19
+ this.shuffle = shuffle ?? false;
20
20
  // assume all proxies are alive when first added instead of 'new'
21
21
  this.assume_aliveness = assume_aliveness ?? false;
22
22
  // check if proxies are alive when they are added to the queue
@@ -32,6 +32,12 @@ class ProxyRotator {
32
32
  // add proxies to queue
33
33
  proxies.forEach( p => this._add(p) );
34
34
  }
35
+ // shuffle proxies
36
+ if(this.shuffle){
37
+ let shuffled = this._shuffleArray(this.pool.toArray());
38
+ this.pool = new Queue();
39
+ shuffled.forEach( p => this.pool.enqueue(p) );
40
+ }
35
41
  }
36
42
 
37
43
  getGraveyard(){ return this.graveyard.map(p => p.proxy) }
@@ -0,0 +1,32 @@
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
+
@@ -0,0 +1,98 @@
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
+ }
@@ -0,0 +1,21 @@
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.
@@ -0,0 +1,233 @@
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.
@@ -0,0 +1,76 @@
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 });
@@ -0,0 +1,6 @@
1
+ module.exports =
2
+ {
3
+ parallel : require('./parallel.js'),
4
+ serial : require('./serial.js'),
5
+ serialOrdered : require('./serialOrdered.js')
6
+ };
@@ -0,0 +1,29 @@
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
+ }
@@ -0,0 +1,34 @@
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
+ }
@@ -0,0 +1,26 @@
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
+ }