@aws/lsp-codewhisperer 0.0.9 → 0.0.10

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 (263) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/node_modules/@amzn/codewhisperer-streaming/package.json +4 -4
  3. package/node_modules/@aws/lsp-fqn/out/common/commonFqnWorkerPool.js +12 -25
  4. package/node_modules/@aws/lsp-fqn/out/common/commonFqnWorkerPool.js.map +1 -1
  5. package/node_modules/@aws/lsp-fqn/out/common/fqnExtractor.js +1 -2
  6. package/node_modules/@aws/lsp-fqn/out/common/fqnExtractor.js.map +1 -1
  7. package/node_modules/@aws/lsp-fqn/package.json +1 -1
  8. package/node_modules/@aws/lsp-fqn/tsconfig.tsbuildinfo +1 -1
  9. package/out/client/streamingClient/codewhispererStreamingClient.d.ts +5 -0
  10. package/out/client/streamingClient/codewhispererStreamingClient.js +25 -0
  11. package/out/client/streamingClient/codewhispererStreamingClient.js.map +1 -0
  12. package/out/client/token/bearer-token-service.json +55 -2
  13. package/out/index.d.ts +1 -0
  14. package/out/index.js +1 -0
  15. package/out/index.js.map +1 -1
  16. package/out/language-server/chat/chatController.js +104 -108
  17. package/out/language-server/chat/chatController.js.map +1 -1
  18. package/out/language-server/chat/chatController.test.js +16 -1
  19. package/out/language-server/chat/chatController.test.js.map +1 -1
  20. package/out/language-server/chat/chatEventParser.js +28 -34
  21. package/out/language-server/chat/chatEventParser.js.map +1 -1
  22. package/out/language-server/chat/chatSessionManagementService.js +22 -36
  23. package/out/language-server/chat/chatSessionManagementService.js.map +1 -1
  24. package/out/language-server/chat/chatSessionService.js +24 -37
  25. package/out/language-server/chat/chatSessionService.js.map +1 -1
  26. package/out/language-server/chat/constants.d.ts +2 -0
  27. package/out/language-server/chat/constants.js +32 -0
  28. package/out/language-server/chat/constants.js.map +1 -0
  29. package/out/language-server/chat/contexts/documentContext.js +11 -24
  30. package/out/language-server/chat/contexts/documentContext.js.map +1 -1
  31. package/out/language-server/chat/contexts/documentFqnExtractor.js +67 -78
  32. package/out/language-server/chat/contexts/documentFqnExtractor.js.map +1 -1
  33. package/out/language-server/chat/contexts/triggerContext.js +32 -44
  34. package/out/language-server/chat/contexts/triggerContext.js.map +1 -1
  35. package/out/language-server/chat/quickActions.d.ts +0 -1
  36. package/out/language-server/chat/quickActions.js +1 -28
  37. package/out/language-server/chat/quickActions.js.map +1 -1
  38. package/out/language-server/chat/telemetry/chatTelemetryController.js +114 -126
  39. package/out/language-server/chat/telemetry/chatTelemetryController.js.map +1 -1
  40. package/out/language-server/chat/utils.d.ts +6 -0
  41. package/out/language-server/chat/utils.js +60 -0
  42. package/out/language-server/chat/utils.js.map +1 -0
  43. package/out/language-server/codeWhispererService.js +13 -11
  44. package/out/language-server/codeWhispererService.js.map +1 -1
  45. package/out/language-server/constants.d.ts +3 -0
  46. package/out/language-server/constants.js +7 -0
  47. package/out/language-server/constants.js.map +1 -0
  48. package/out/language-server/dependencyGraph/csharpDependencyGraph.js +2 -2
  49. package/out/language-server/dependencyGraph/csharpDependencyGraph.js.map +1 -1
  50. package/out/language-server/dependencyGraph/dependencyGraph.js +12 -9
  51. package/out/language-server/dependencyGraph/dependencyGraph.js.map +1 -1
  52. package/out/language-server/dependencyGraph/gitIgnoreFilter.js +1 -0
  53. package/out/language-server/dependencyGraph/gitIgnoreFilter.js.map +1 -1
  54. package/out/language-server/netTransform/artifactManager.d.ts +29 -0
  55. package/out/language-server/netTransform/artifactManager.js +191 -0
  56. package/out/language-server/netTransform/artifactManager.js.map +1 -0
  57. package/out/language-server/netTransform/converter.d.ts +7 -0
  58. package/out/language-server/netTransform/converter.js +88 -0
  59. package/out/language-server/netTransform/converter.js.map +1 -0
  60. package/out/language-server/netTransform/metrics.d.ts +17 -0
  61. package/out/language-server/netTransform/metrics.js +222 -0
  62. package/out/language-server/netTransform/metrics.js.map +1 -0
  63. package/out/language-server/netTransform/models.d.ts +84 -0
  64. package/out/language-server/netTransform/models.js +12 -0
  65. package/out/language-server/netTransform/models.js.map +1 -0
  66. package/out/language-server/netTransform/resources/SupportedProjects.d.ts +2 -0
  67. package/out/language-server/netTransform/resources/SupportedProjects.js +26 -0
  68. package/out/language-server/netTransform/resources/SupportedProjects.js.map +1 -0
  69. package/out/language-server/netTransform/tests/converter.test.d.ts +1 -0
  70. package/out/language-server/netTransform/tests/converter.test.js +151 -0
  71. package/out/language-server/netTransform/tests/converter.test.js.map +1 -0
  72. package/out/language-server/netTransform/tests/mockData.d.ts +21 -0
  73. package/out/language-server/netTransform/tests/mockData.js +60 -0
  74. package/out/language-server/netTransform/tests/mockData.js.map +1 -0
  75. package/out/language-server/netTransform/tests/transformHandler.test.d.ts +1 -0
  76. package/out/language-server/netTransform/tests/transformHandler.test.js +242 -0
  77. package/out/language-server/netTransform/tests/transformHandler.test.js.map +1 -0
  78. package/out/language-server/netTransform/tests/validation.test.d.ts +1 -0
  79. package/out/language-server/netTransform/tests/validation.test.js +80 -0
  80. package/out/language-server/netTransform/tests/validation.test.js.map +1 -0
  81. package/out/language-server/netTransform/transformHandler.d.ts +27 -0
  82. package/out/language-server/netTransform/transformHandler.js +315 -0
  83. package/out/language-server/netTransform/transformHandler.js.map +1 -0
  84. package/out/language-server/netTransform/validation.d.ts +9 -0
  85. package/out/language-server/netTransform/validation.js +79 -0
  86. package/out/language-server/netTransform/validation.js.map +1 -0
  87. package/out/language-server/netTransformServer.d.ts +5 -6
  88. package/out/language-server/netTransformServer.js +149 -15
  89. package/out/language-server/netTransformServer.js.map +1 -1
  90. package/out/language-server/proxy-server.d.ts +1 -0
  91. package/out/language-server/proxy-server.js +16 -1
  92. package/out/language-server/proxy-server.js.map +1 -1
  93. package/out/language-server/securityScan/securityScanDiagnosticsProvider.js +10 -6
  94. package/out/language-server/securityScan/securityScanDiagnosticsProvider.js.map +1 -1
  95. package/out/language-server/securityScan/securityScanHandler.js +4 -0
  96. package/out/language-server/securityScan/securityScanHandler.js.map +1 -1
  97. package/out/language-server/session/sessionManager.js +35 -15
  98. package/out/language-server/session/sessionManager.js.map +1 -1
  99. package/out/language-server/telemetry/codeDiffTracker.js +69 -80
  100. package/out/language-server/telemetry/codeDiffTracker.js.map +1 -1
  101. package/out/language-server/telemetry/codePercentage.js +3 -0
  102. package/out/language-server/telemetry/codePercentage.js.map +1 -1
  103. package/out/language-server/telemetry/metric.js +8 -21
  104. package/out/language-server/telemetry/metric.js.map +1 -1
  105. package/out/language-server/telemetry/types.d.ts +38 -0
  106. package/out/language-server/telemetry/types.js.map +1 -1
  107. package/out/language-server/utils.d.ts +2 -5
  108. package/out/language-server/utils.js +25 -49
  109. package/out/language-server/utils.js.map +1 -1
  110. package/package.json +5 -2
  111. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/HISTORY.md +0 -395
  112. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/LICENSE +0 -201
  113. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/README.md +0 -530
  114. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/dist/worker.js +0 -288
  115. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/dist/worker.js.map +0 -1
  116. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/dist/worker.min.js +0 -2
  117. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/dist/worker.min.js.map +0 -1
  118. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/dist/workerpool.js +0 -1737
  119. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/dist/workerpool.js.map +0 -1
  120. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/dist/workerpool.min.js +0 -3
  121. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/dist/workerpool.min.js.LICENSE.txt +0 -24
  122. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/dist/workerpool.min.js.map +0 -1
  123. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/package.json +0 -57
  124. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/Pool.js +0 -476
  125. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/Promise.js +0 -293
  126. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/WorkerHandler.js +0 -547
  127. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/debug-port-allocator.js +0 -28
  128. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/environment.js +0 -30
  129. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/generated/embeddedWorker.js +0 -6
  130. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/header.js +0 -24
  131. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/index.js +0 -60
  132. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/requireFoolWebpack.js +0 -8
  133. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/transfer.js +0 -12
  134. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/types.js +0 -46
  135. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/validateOptions.js +0 -51
  136. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/src/worker.js +0 -256
  137. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/Pool.d.ts +0 -130
  138. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/Promise.d.ts +0 -125
  139. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/WorkerHandler.d.ts +0 -89
  140. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/debug-port-allocator.d.ts +0 -8
  141. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/environment.d.ts +0 -4
  142. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/generated/embeddedWorker.d.ts +0 -2
  143. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/index.d.ts +0 -43
  144. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/transfer.d.ts +0 -19
  145. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/types.d.ts +0 -99
  146. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/validateOptions.d.ts +0 -4
  147. package/node_modules/@aws/lsp-fqn/node_modules/workerpool/types/worker.d.ts +0 -8
  148. package/node_modules/@smithy/eventstream-serde-config-resolver/LICENSE +0 -201
  149. package/node_modules/@smithy/eventstream-serde-config-resolver/README.md +0 -10
  150. package/node_modules/@smithy/eventstream-serde-config-resolver/dist-cjs/EventStreamSerdeConfig.js +0 -1
  151. package/node_modules/@smithy/eventstream-serde-config-resolver/dist-cjs/index.js +0 -37
  152. package/node_modules/@smithy/eventstream-serde-config-resolver/dist-es/EventStreamSerdeConfig.js +0 -4
  153. package/node_modules/@smithy/eventstream-serde-config-resolver/dist-es/index.js +0 -1
  154. package/node_modules/@smithy/eventstream-serde-config-resolver/dist-types/EventStreamSerdeConfig.d.ts +0 -24
  155. package/node_modules/@smithy/eventstream-serde-config-resolver/dist-types/index.d.ts +0 -4
  156. package/node_modules/@smithy/eventstream-serde-config-resolver/dist-types/ts3.4/EventStreamSerdeConfig.d.ts +0 -24
  157. package/node_modules/@smithy/eventstream-serde-config-resolver/dist-types/ts3.4/index.d.ts +0 -4
  158. package/node_modules/@smithy/eventstream-serde-config-resolver/package.json +0 -61
  159. package/node_modules/bowser/CHANGELOG.md +0 -218
  160. package/node_modules/bowser/LICENSE +0 -39
  161. package/node_modules/bowser/README.md +0 -179
  162. package/node_modules/bowser/bundled.js +0 -1
  163. package/node_modules/bowser/es5.js +0 -1
  164. package/node_modules/bowser/index.d.ts +0 -250
  165. package/node_modules/bowser/package.json +0 -83
  166. package/node_modules/bowser/src/bowser.js +0 -77
  167. package/node_modules/bowser/src/constants.js +0 -116
  168. package/node_modules/bowser/src/parser-browsers.js +0 -700
  169. package/node_modules/bowser/src/parser-engines.js +0 -120
  170. package/node_modules/bowser/src/parser-os.js +0 -199
  171. package/node_modules/bowser/src/parser-platforms.js +0 -266
  172. package/node_modules/bowser/src/parser.js +0 -496
  173. package/node_modules/bowser/src/utils.js +0 -309
  174. package/node_modules/tslib/CopyrightNotice.txt +0 -15
  175. package/node_modules/tslib/LICENSE.txt +0 -12
  176. package/node_modules/tslib/README.md +0 -164
  177. package/node_modules/tslib/SECURITY.md +0 -41
  178. package/node_modules/tslib/modules/index.d.ts +0 -37
  179. package/node_modules/tslib/modules/index.js +0 -68
  180. package/node_modules/tslib/modules/package.json +0 -3
  181. package/node_modules/tslib/package.json +0 -47
  182. package/node_modules/tslib/tslib.d.ts +0 -453
  183. package/node_modules/tslib/tslib.es6.html +0 -1
  184. package/node_modules/tslib/tslib.es6.js +0 -374
  185. package/node_modules/tslib/tslib.es6.mjs +0 -373
  186. package/node_modules/tslib/tslib.html +0 -1
  187. package/node_modules/tslib/tslib.js +0 -424
  188. package/node_modules/uuid/CHANGELOG.md +0 -274
  189. package/node_modules/uuid/CONTRIBUTING.md +0 -18
  190. package/node_modules/uuid/LICENSE.md +0 -9
  191. package/node_modules/uuid/README.md +0 -466
  192. package/node_modules/uuid/dist/bin/uuid +0 -2
  193. package/node_modules/uuid/dist/commonjs-browser/index.js +0 -79
  194. package/node_modules/uuid/dist/commonjs-browser/md5.js +0 -223
  195. package/node_modules/uuid/dist/commonjs-browser/native.js +0 -11
  196. package/node_modules/uuid/dist/commonjs-browser/nil.js +0 -8
  197. package/node_modules/uuid/dist/commonjs-browser/parse.js +0 -45
  198. package/node_modules/uuid/dist/commonjs-browser/regex.js +0 -8
  199. package/node_modules/uuid/dist/commonjs-browser/rng.js +0 -25
  200. package/node_modules/uuid/dist/commonjs-browser/sha1.js +0 -104
  201. package/node_modules/uuid/dist/commonjs-browser/stringify.js +0 -44
  202. package/node_modules/uuid/dist/commonjs-browser/v1.js +0 -107
  203. package/node_modules/uuid/dist/commonjs-browser/v3.js +0 -16
  204. package/node_modules/uuid/dist/commonjs-browser/v35.js +0 -80
  205. package/node_modules/uuid/dist/commonjs-browser/v4.js +0 -43
  206. package/node_modules/uuid/dist/commonjs-browser/v5.js +0 -16
  207. package/node_modules/uuid/dist/commonjs-browser/validate.js +0 -17
  208. package/node_modules/uuid/dist/commonjs-browser/version.js +0 -21
  209. package/node_modules/uuid/dist/esm-browser/index.js +0 -9
  210. package/node_modules/uuid/dist/esm-browser/md5.js +0 -215
  211. package/node_modules/uuid/dist/esm-browser/native.js +0 -4
  212. package/node_modules/uuid/dist/esm-browser/nil.js +0 -1
  213. package/node_modules/uuid/dist/esm-browser/parse.js +0 -35
  214. package/node_modules/uuid/dist/esm-browser/regex.js +0 -1
  215. package/node_modules/uuid/dist/esm-browser/rng.js +0 -18
  216. package/node_modules/uuid/dist/esm-browser/sha1.js +0 -96
  217. package/node_modules/uuid/dist/esm-browser/stringify.js +0 -33
  218. package/node_modules/uuid/dist/esm-browser/v1.js +0 -95
  219. package/node_modules/uuid/dist/esm-browser/v3.js +0 -4
  220. package/node_modules/uuid/dist/esm-browser/v35.js +0 -66
  221. package/node_modules/uuid/dist/esm-browser/v4.js +0 -29
  222. package/node_modules/uuid/dist/esm-browser/v5.js +0 -4
  223. package/node_modules/uuid/dist/esm-browser/validate.js +0 -7
  224. package/node_modules/uuid/dist/esm-browser/version.js +0 -11
  225. package/node_modules/uuid/dist/esm-node/index.js +0 -9
  226. package/node_modules/uuid/dist/esm-node/md5.js +0 -13
  227. package/node_modules/uuid/dist/esm-node/native.js +0 -4
  228. package/node_modules/uuid/dist/esm-node/nil.js +0 -1
  229. package/node_modules/uuid/dist/esm-node/parse.js +0 -35
  230. package/node_modules/uuid/dist/esm-node/regex.js +0 -1
  231. package/node_modules/uuid/dist/esm-node/rng.js +0 -12
  232. package/node_modules/uuid/dist/esm-node/sha1.js +0 -13
  233. package/node_modules/uuid/dist/esm-node/stringify.js +0 -33
  234. package/node_modules/uuid/dist/esm-node/v1.js +0 -95
  235. package/node_modules/uuid/dist/esm-node/v3.js +0 -4
  236. package/node_modules/uuid/dist/esm-node/v35.js +0 -66
  237. package/node_modules/uuid/dist/esm-node/v4.js +0 -29
  238. package/node_modules/uuid/dist/esm-node/v5.js +0 -4
  239. package/node_modules/uuid/dist/esm-node/validate.js +0 -7
  240. package/node_modules/uuid/dist/esm-node/version.js +0 -11
  241. package/node_modules/uuid/dist/index.js +0 -79
  242. package/node_modules/uuid/dist/md5-browser.js +0 -223
  243. package/node_modules/uuid/dist/md5.js +0 -23
  244. package/node_modules/uuid/dist/native-browser.js +0 -11
  245. package/node_modules/uuid/dist/native.js +0 -15
  246. package/node_modules/uuid/dist/nil.js +0 -8
  247. package/node_modules/uuid/dist/parse.js +0 -45
  248. package/node_modules/uuid/dist/regex.js +0 -8
  249. package/node_modules/uuid/dist/rng-browser.js +0 -25
  250. package/node_modules/uuid/dist/rng.js +0 -24
  251. package/node_modules/uuid/dist/sha1-browser.js +0 -104
  252. package/node_modules/uuid/dist/sha1.js +0 -23
  253. package/node_modules/uuid/dist/stringify.js +0 -44
  254. package/node_modules/uuid/dist/uuid-bin.js +0 -85
  255. package/node_modules/uuid/dist/v1.js +0 -107
  256. package/node_modules/uuid/dist/v3.js +0 -16
  257. package/node_modules/uuid/dist/v35.js +0 -80
  258. package/node_modules/uuid/dist/v4.js +0 -43
  259. package/node_modules/uuid/dist/v5.js +0 -16
  260. package/node_modules/uuid/dist/validate.js +0 -17
  261. package/node_modules/uuid/dist/version.js +0 -21
  262. package/node_modules/uuid/package.json +0 -135
  263. package/node_modules/uuid/wrapper.mjs +0 -10
@@ -1,530 +0,0 @@
1
- # workerpool
2
-
3
- **workerpool** offers an easy way to create a pool of workers for both dynamically offloading computations as well as managing a pool of dedicated workers. **workerpool** basically implements a [thread pool pattern](http://en.wikipedia.org/wiki/Thread_pool_pattern). There is a pool of workers to execute tasks. New tasks are put in a queue. A worker executes one task at a time, and once finished, picks a new task from the queue. Workers can be accessed via a natural, promise based proxy, as if they are available straight in the main application.
4
-
5
- **workerpool** runs on node.js, Chrome, Firefox, Opera, Safari, and IE10+.
6
-
7
- ## Features
8
-
9
- - Easy to use
10
- - Runs in the browser and on node.js
11
- - Dynamically offload functions to a worker
12
- - Access workers via a proxy
13
- - Cancel running tasks
14
- - Set a timeout on tasks
15
- - Handles crashed workers
16
- - Small: 7 kB minified and gzipped
17
- - Supports transferable objects (only for web workers and worker_threads)
18
-
19
- ## Why
20
-
21
- JavaScript is based upon a single event loop which handles one event at a time. Jeremy Epstein [explains this clearly](http://greenash.net.au/thoughts/2012/11/nodejs-itself-is-blocking-only-its-io-is-non-blocking/):
22
-
23
- > In Node.js everything runs in parallel, except your code.
24
- > What this means is that all I/O code that you write in Node.js is non-blocking,
25
- > while (conversely) all non-I/O code that you write in Node.js is blocking.
26
-
27
- This means that CPU heavy tasks will block other tasks from being executed. In case of a browser environment, the browser will not react to user events like a mouse click while executing a CPU intensive task (the browser "hangs"). In case of a node.js server, the server will not respond to any new request while executing a single, heavy request.
28
-
29
- For front-end processes, this is not a desired situation.
30
- Therefore, CPU intensive tasks should be offloaded from the main event loop onto dedicated _workers_. In a browser environment, [Web Workers](http://www.html5rocks.com/en/tutorials/workers/basics/) can be used. In node.js, [child processes](https://nodejs.org/api/child_process.html) and [worker_threads](https://nodejs.org/api/worker_threads.html) are available. An application should be split in separate, decoupled parts, which can run independent of each other in a parallelized way. Effectively, this results in an architecture which achieves concurrency by means of isolated processes and message passing.
31
-
32
- ## Install
33
-
34
- Install via npm:
35
-
36
- npm install workerpool
37
-
38
- ## Load
39
-
40
- To load workerpool in a node.js application (both main application as well as workers):
41
-
42
- ```js
43
- const workerpool = require('workerpool');
44
- ```
45
-
46
- To load workerpool in the browser:
47
-
48
- ```html
49
- <script src="workerpool.js"></script>
50
- ```
51
-
52
- To load workerpool in a web worker in the browser:
53
-
54
- ```js
55
- importScripts('workerpool.js');
56
- ```
57
-
58
- ## Use
59
-
60
- ### Offload functions dynamically
61
-
62
- In the following example there is a function `add`, which is offloaded dynamically to a worker to be executed for a given set of arguments.
63
-
64
- **myApp.js**
65
-
66
- ```js
67
- const workerpool = require('workerpool');
68
- const pool = workerpool.pool();
69
-
70
- function add(a, b) {
71
- return a + b;
72
- }
73
-
74
- pool
75
- .exec(add, [3, 4])
76
- .then(function (result) {
77
- console.log('result', result); // outputs 7
78
- })
79
- .catch(function (err) {
80
- console.error(err);
81
- })
82
- .then(function () {
83
- pool.terminate(); // terminate all workers when done
84
- });
85
- ```
86
-
87
- Note that both function and arguments must be static and stringifiable, as they need to be sent to the worker in a serialized form. In case of large functions or function arguments, the overhead of sending the data to the worker can be significant.
88
-
89
- ### Dedicated workers
90
-
91
- A dedicated worker can be created in a separate script, and then used via a worker pool.
92
-
93
- **myWorker.js**
94
-
95
- ```js
96
- const workerpool = require('workerpool');
97
-
98
- // a deliberately inefficient implementation of the fibonacci sequence
99
- function fibonacci(n) {
100
- if (n < 2) return n;
101
- return fibonacci(n - 2) + fibonacci(n - 1);
102
- }
103
-
104
- // create a worker and register public functions
105
- workerpool.worker({
106
- fibonacci: fibonacci,
107
- });
108
- ```
109
-
110
- This worker can be used by a worker pool:
111
-
112
- **myApp.js**
113
-
114
- ```js
115
- const workerpool = require('workerpool');
116
-
117
- // create a worker pool using an external worker script
118
- const pool = workerpool.pool(__dirname + '/myWorker.js');
119
-
120
- // run registered functions on the worker via exec
121
- pool
122
- .exec('fibonacci', [10])
123
- .then(function (result) {
124
- console.log('Result: ' + result); // outputs 55
125
- })
126
- .catch(function (err) {
127
- console.error(err);
128
- })
129
- .then(function () {
130
- pool.terminate(); // terminate all workers when done
131
- });
132
-
133
- // or run registered functions on the worker via a proxy:
134
- pool
135
- .proxy()
136
- .then(function (worker) {
137
- return worker.fibonacci(10);
138
- })
139
- .then(function (result) {
140
- console.log('Result: ' + result); // outputs 55
141
- })
142
- .catch(function (err) {
143
- console.error(err);
144
- })
145
- .then(function () {
146
- pool.terminate(); // terminate all workers when done
147
- });
148
- ```
149
-
150
- Worker can also initialize asynchronously:
151
-
152
- **myAsyncWorker.js**
153
-
154
- ```js
155
- define(['workerpool/dist/workerpool'], function (workerpool) {
156
- // a deliberately inefficient implementation of the fibonacci sequence
157
- function fibonacci(n) {
158
- if (n < 2) return n;
159
- return fibonacci(n - 2) + fibonacci(n - 1);
160
- }
161
-
162
- // create a worker and register public functions
163
- workerpool.worker({
164
- fibonacci: fibonacci,
165
- });
166
- });
167
- ```
168
-
169
- ## Examples
170
-
171
- Examples are available in the examples directory:
172
-
173
- [https://github.com/josdejong/workerpool/tree/master/examples](https://github.com/josdejong/workerpool/tree/master/examples)
174
-
175
- ## API
176
-
177
- The API of workerpool consists of two parts: a function `workerpool.pool` to create a worker pool, and a function `workerpool.worker` to create a worker.
178
-
179
- ### pool
180
-
181
- A workerpool can be created using the function `workerpool.pool`:
182
-
183
- `workerpool.pool([script: string] [, options: Object]) : Pool`
184
-
185
- When a `script` argument is provided, the provided script will be started as a dedicated worker. When no `script` argument is provided, a default worker is started which can be used to offload functions dynamically via `Pool.exec`. Note that on node.js, `script` must be an absolute file path like `__dirname + '/myWorker.js'`. In a browser environment, `script` can also be a data URL like `'data:application/javascript;base64,...'`. This allows embedding the bundled code of a worker in your main application. See `examples/embeddedWorker` for a demo.
186
-
187
- The following options are available:
188
-
189
- - `minWorkers: number | 'max'`. The minimum number of workers that must be initialized and kept available. Setting this to `'max'` will create `maxWorkers` default workers (see below).
190
- - `maxWorkers: number`. The default number of maxWorkers is the number of CPU's minus one. When the number of CPU's could not be determined (for example in older browsers), `maxWorkers` is set to 3.
191
- - `maxQueueSize: number`. The maximum number of tasks allowed to be queued. Can be used to prevent running out of memory. If the maximum is exceeded, adding a new task will throw an error. The default value is `Infinity`.
192
- - `workerType: 'auto' | 'web' | 'process' | 'thread'`.
193
- - In case of `'auto'` (default), workerpool will automatically pick a suitable type of worker: when in a browser environment, `'web'` will be used. When in a node.js environment, `worker_threads` will be used if available (Node.js >= 11.7.0), else `child_process` will be used.
194
- - In case of `'web'`, a Web Worker will be used. Only available in a browser environment.
195
- - In case of `'process'`, `child_process` will be used. Only available in a node.js environment.
196
- - In case of `'thread'`, `worker_threads` will be used. If `worker_threads` are not available, an error is thrown. Only available in a node.js environment.
197
- - `workerTerminateTimeout: number`. The timeout in milliseconds to wait for a worker to cleanup it's resources on termination before stopping it forcefully. Default value is `1000`.
198
- - `forkArgs: String[]`. For `process` worker type. An array passed as `args` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options)
199
- - `forkOpts: Object`. For `process` worker type. An object passed as `options` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options). See nodejs documentation for available options.
200
- - `workerOpts: Object`. For `web` worker type. An object passed to the [constructor of the web worker](https://html.spec.whatwg.org/multipage/workers.html#dom-worker). See [WorkerOptions specification](https://html.spec.whatwg.org/multipage/workers.html#workeroptions) for available options.
201
- - `workerThreadOpts: Object`. For `worker` worker type. An object passed to [worker_threads.options](https://nodejs.org/api/worker_threads.html#new-workerfilename-options). See nodejs documentation for available options.
202
- - `onCreateWorker: Function`. A callback that is called whenever a worker is being created. It can be used to allocate resources for each worker for example. The callback is passed as argument an object with the following properties:
203
- - `forkArgs: String[]`: the `forkArgs` option of this pool
204
- - `forkOpts: Object`: the `forkOpts` option of this pool
205
- - `workerOpts: Object`: the `workerOpts` option of this pool
206
- - `script: string`: the `script` option of this pool
207
- Optionally, this callback can return an object containing one or more of the above properties. The provided properties will be used to override the Pool properties for the worker being created.
208
- - `onTerminateWorker: Function`. A callback that is called whenever a worker is being terminated. It can be used to release resources that might have been allocated for this specific worker. The callback is passed as argument an object as described for `onCreateWorker`, with each property sets with the value for the worker being terminated.
209
- - `emitStdStreams: boolean`. For `process` or `thread` worker type. If `true`, the worker will emit `stdout` and `stderr` events instead of passing it through to the parent streams. Default value is `false`.
210
-
211
- > Important note on `'workerType'`: when sending and receiving primitive data types (plain JSON) from and to a worker, the different worker types (`'web'`, `'process'`, `'thread'`) can be used interchangeably. However, when using more advanced data types like buffers, the API and returned results can vary. In these cases, it is best not to use the `'auto'` setting but have a fixed `'workerType'` and good unit testing in place.
212
-
213
- A worker pool contains the following functions:
214
-
215
- - `Pool.exec(method: Function | string, params: Array | null [, options: Object]) : Promise<any, Error>`<br>
216
- Execute a function on a worker with given arguments.
217
-
218
- - When `method` is a string, a method with this name must exist at the worker and must be registered to make it accessible via the pool. The function will be executed on the worker with given parameters.
219
- - When `method` is a function, the provided function `fn` will be stringified, send to the worker, and executed there with the provided parameters. The provided function must be static, it must not depend on variables in a surrounding scope.
220
- - The following options are available:
221
- - `on: (payload: any) => void`. An event listener, to handle events sent by the worker for this execution. See [Events](#events) for more details.
222
- - `transfer: Object[]`. A list of transferable objects to send to the worker. Not supported by `process` worker type. See [example](./examples/transferableObjects.js) for usage.
223
-
224
- - `Pool.proxy() : Promise<Object, Error>`<br>
225
- Create a proxy for the worker pool. The proxy contains a proxy for all methods available on the worker. All methods return promises resolving the methods result.
226
-
227
- - `Pool.stats() : Object`<br>
228
- Retrieve statistics on workers, and active and pending tasks.
229
-
230
- Returns an object containing the following properties:
231
-
232
- ```
233
- {
234
- totalWorkers: 0,
235
- busyWorkers: 0,
236
- idleWorkers: 0,
237
- pendingTasks: 0,
238
- activeTasks: 0
239
- }
240
- ```
241
-
242
- - `Pool.terminate([force: boolean [, timeout: number]]) : Promise<void, Error>`
243
-
244
- If parameter `force` is false (default), workers will finish the tasks they are working on before terminating themselves. Any pending tasks will be rejected with an error 'Pool terminated'. When `force` is true, all workers are terminated immediately without finishing running tasks. If `timeout` is provided, worker will be forced to terminate when the timeout expires and the worker has not finished.
245
-
246
- The function `Pool.exec` and the proxy functions all return a `Promise`. The promise has the following functions available:
247
-
248
- - `Promise.then(fn: Function<result: any>) : Promise<any, Error>`<br>
249
- Get the result of the promise once resolve.
250
- - `Promise.catch(fn: Function<error: Error>) : Promise<any, Error>`<br>
251
- Get the error of the promise when rejected.
252
- - `Promise.cancel() : Promise<any, Error>`<br>
253
- A running task can be cancelled. The worker executing the task is enforced to terminate immediately.
254
- The promise will be rejected with a `Promise.CancellationError`.
255
- - `Promise.timeout(delay: number) : Promise<any, Error>`<br>
256
- Cancel a running task when it is not resolved or rejected within given delay in milliseconds. The timer will start when the task is actually started, not when the task is created and queued.
257
- The worker executing the task is enforced to terminate immediately.
258
- The promise will be rejected with a `Promise.TimeoutError`.
259
-
260
- Example usage:
261
-
262
- ```js
263
- const workerpool = require('workerpool');
264
-
265
- function add(a, b) {
266
- return a + b;
267
- }
268
-
269
- const pool1 = workerpool.pool();
270
-
271
- // offload a function to a worker
272
- pool1
273
- .exec(add, [2, 4])
274
- .then(function (result) {
275
- console.log(result); // will output 6
276
- })
277
- .catch(function (err) {
278
- console.error(err);
279
- });
280
-
281
- // create a dedicated worker
282
- const pool2 = workerpool.pool(__dirname + '/myWorker.js');
283
-
284
- // supposed myWorker.js contains a function 'fibonacci'
285
- pool2
286
- .exec('fibonacci', [10])
287
- .then(function (result) {
288
- console.log(result); // will output 55
289
- })
290
- .catch(function (err) {
291
- console.error(err);
292
- });
293
-
294
- // send a transferable object to the worker
295
- // supposed myWorker.js contains a function 'sum'
296
- const toTransfer = new Uint8Array(2).map((_v, i) => i)
297
- pool2
298
- .exec('sum', [toTransfer], { transfer: [toTransfer.buffer] })
299
- .then(function (result) {
300
- console.log(result); // will output 3
301
- })
302
- .catch(function (err) {
303
- console.error(err);
304
- });
305
-
306
- // create a proxy to myWorker.js
307
- pool2
308
- .proxy()
309
- .then(function (myWorker) {
310
- return myWorker.fibonacci(10);
311
- })
312
- .then(function (result) {
313
- console.log(result); // will output 55
314
- })
315
- .catch(function (err) {
316
- console.error(err);
317
- });
318
-
319
- // create a pool with a specified maximum number of workers
320
- const pool3 = workerpool.pool({ maxWorkers: 7 });
321
- ```
322
-
323
- ### worker
324
-
325
- A worker is constructed as:
326
-
327
- `workerpool.worker([methods: Object<String, Function>] [, options: Object]) : void`
328
-
329
- Argument `methods` is optional and can be an object with functions available in the worker. Registered functions will be available via the worker pool.
330
-
331
- The following options are available:
332
-
333
- - `onTerminate: ([code: number]) => Promise<void> | void`. A callback that is called whenever a worker is being terminated. It can be used to release resources that might have been allocated for this specific worker. The difference with pool's `onTerminateWorker` is that this callback runs in the worker context, while `onTerminateWorker` is executed on the main thread.
334
-
335
-
336
- Example usage:
337
-
338
- ```js
339
- // file myWorker.js
340
- const workerpool = require('workerpool');
341
-
342
- function add(a, b) {
343
- return a + b;
344
- }
345
-
346
- function multiply(a, b) {
347
- return a * b;
348
- }
349
-
350
- // create a worker and register functions
351
- workerpool.worker({
352
- add: add,
353
- multiply: multiply,
354
- });
355
- ```
356
-
357
- Asynchronous results can be handled by returning a Promise from a function in the worker:
358
-
359
- ```js
360
- // file myWorker.js
361
- const workerpool = require('workerpool');
362
-
363
- function timeout(delay) {
364
- return new Promise(function (resolve, reject) {
365
- setTimeout(resolve, delay);
366
- });
367
- }
368
-
369
- // create a worker and register functions
370
- workerpool.worker({
371
- timeout: timeout,
372
- });
373
- ```
374
-
375
- Transferable objects can be sent back to the pool using `Transfer` helper class:
376
-
377
- ```js
378
- // file myWorker.js
379
- const workerpool = require('workerpool');
380
-
381
- function array(size) {
382
- var array = new Uint8Array(size).map((_v, i) => i);
383
- return new workerpool.Transfer(array, [array.buffer]);
384
- }
385
-
386
- // create a worker and register functions
387
- workerpool.worker({
388
- array: array,
389
- });
390
- ```
391
-
392
- ### Events
393
-
394
- You can send data back from workers to the pool while the task is being executed using the `workerEmit` function:
395
-
396
- `workerEmit(payload: any) : unknown`
397
-
398
- This function only works inside a worker **and** during a task.
399
-
400
- Example:
401
-
402
- ```js
403
- // file myWorker.js
404
- const workerpool = require('workerpool');
405
-
406
- function eventExample(delay) {
407
- workerpool.workerEmit({
408
- status: 'in_progress',
409
- });
410
-
411
- workerpool.workerEmit({
412
- status: 'complete',
413
- });
414
-
415
- return true;
416
- }
417
-
418
- // create a worker and register functions
419
- workerpool.worker({
420
- eventExample: eventExample,
421
- });
422
- ```
423
-
424
- To receive those events, you can use the `on` option of the pool `exec` method:
425
-
426
- ```js
427
- pool.exec('eventExample', [], {
428
- on: function (payload) {
429
- if (payload.status === 'in_progress') {
430
- console.log('In progress...');
431
- } else if (payload.status === 'complete') {
432
- console.log('Done!');
433
- }
434
- },
435
- });
436
- ```
437
-
438
- ### Utilities
439
-
440
- Following properties are available for convenience:
441
-
442
- - **platform**: The Javascript platform. Either _node_ or _browser_
443
- - **isMainThread**: Whether the code is running in main thread or not (Workers)
444
- - **cpus**: The number of CPUs/cores available
445
-
446
- ## Roadmap
447
-
448
- - Implement functions for parallel processing: `map`, `reduce`, `forEach`,
449
- `filter`, `some`, `every`, ...
450
- - Implement graceful degradation on old browsers not supporting webworkers:
451
- fallback to processing tasks in the main application.
452
- - Implement session support: be able to handle a series of related tasks by a
453
- single worker, which can keep a state for the session.
454
-
455
- ## Related libraries
456
-
457
- - https://github.com/andywer/threads.js
458
- - https://github.com/piscinajs/piscina
459
- - https://github.com/learnboost/cluster
460
- - https://github.com/adambom/parallel.js
461
- - https://github.com/padolsey/operative
462
- - https://github.com/calvinmetcalf/catiline
463
- - https://github.com/Unitech/pm2
464
- - https://github.com/godaddy/node-cluster-service
465
- - https://github.com/ramesaliyev/EasyWebWorker
466
- - https://github.com/rvagg/node-worker-farm
467
-
468
- ## Build
469
-
470
- First clone the project from github:
471
-
472
- git clone git://github.com/josdejong/workerpool.git
473
- cd workerpool
474
-
475
- Install the project dependencies:
476
-
477
- npm install
478
-
479
- Then, the project can be build by executing the build script via npm:
480
-
481
- npm run build
482
-
483
- This will build the library workerpool.js and workerpool.min.js from the source
484
- files and put them in the folder dist.
485
-
486
- ## Test
487
-
488
- To execute tests for the library, install the project dependencies once:
489
-
490
- npm install
491
-
492
- Then, the tests can be executed:
493
-
494
- npm test
495
-
496
- To test code coverage of the tests:
497
-
498
- npm run coverage
499
-
500
- To see the coverage results, open the generated report in your browser:
501
-
502
- ./coverage/index.html
503
-
504
- ## Publish
505
-
506
- - Describe changes in HISTORY.md
507
- - Update version in package.json, run `npm install` to update it in `package-lock.json` too.
508
- - Push to Github
509
- - Deploy to npm via `npm publish`
510
- - Add a git tag with the version number like:
511
- ```
512
- git tag v1.2.3
513
- git push --tags
514
- ```
515
-
516
- ## License
517
-
518
- Copyright (C) 2014-2024 Jos de Jong <wjosdejong@gmail.com>
519
-
520
- Licensed under the Apache License, Version 2.0 (the "License");
521
- you may not use this file except in compliance with the License.
522
- You may obtain a copy of the License at
523
-
524
- http://www.apache.org/licenses/LICENSE-2.0
525
-
526
- Unless required by applicable law or agreed to in writing, software
527
- distributed under the License is distributed on an "AS IS" BASIS,
528
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
529
- See the License for the specific language governing permissions and
530
- limitations under the License.