@ryantest/openclaw-qqbot 0.0.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 (197) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +483 -0
  3. package/README.zh.md +478 -0
  4. package/bin/qqbot-cli.js +243 -0
  5. package/clawdbot.plugin.json +16 -0
  6. package/dist/index.d.ts +17 -0
  7. package/dist/index.js +26 -0
  8. package/dist/src/admin-resolver.d.ts +27 -0
  9. package/dist/src/admin-resolver.js +122 -0
  10. package/dist/src/api.d.ts +156 -0
  11. package/dist/src/api.js +599 -0
  12. package/dist/src/channel.d.ts +11 -0
  13. package/dist/src/channel.js +354 -0
  14. package/dist/src/config.d.ts +25 -0
  15. package/dist/src/config.js +161 -0
  16. package/dist/src/credential-backup.d.ts +31 -0
  17. package/dist/src/credential-backup.js +66 -0
  18. package/dist/src/gateway.d.ts +18 -0
  19. package/dist/src/gateway.js +1265 -0
  20. package/dist/src/image-server.d.ts +68 -0
  21. package/dist/src/image-server.js +462 -0
  22. package/dist/src/inbound-attachments.d.ts +58 -0
  23. package/dist/src/inbound-attachments.js +234 -0
  24. package/dist/src/known-users.d.ts +100 -0
  25. package/dist/src/known-users.js +263 -0
  26. package/dist/src/message-queue.d.ts +50 -0
  27. package/dist/src/message-queue.js +115 -0
  28. package/dist/src/onboarding.d.ts +10 -0
  29. package/dist/src/onboarding.js +203 -0
  30. package/dist/src/outbound-deliver.d.ts +48 -0
  31. package/dist/src/outbound-deliver.js +462 -0
  32. package/dist/src/outbound.d.ts +203 -0
  33. package/dist/src/outbound.js +1102 -0
  34. package/dist/src/proactive.d.ts +170 -0
  35. package/dist/src/proactive.js +399 -0
  36. package/dist/src/ref-index-store.d.ts +70 -0
  37. package/dist/src/ref-index-store.js +273 -0
  38. package/dist/src/reply-dispatcher.d.ts +35 -0
  39. package/dist/src/reply-dispatcher.js +311 -0
  40. package/dist/src/runtime.d.ts +3 -0
  41. package/dist/src/runtime.js +10 -0
  42. package/dist/src/session-store.d.ts +52 -0
  43. package/dist/src/session-store.js +254 -0
  44. package/dist/src/slash-commands.d.ts +71 -0
  45. package/dist/src/slash-commands.js +1179 -0
  46. package/dist/src/startup-greeting.d.ts +30 -0
  47. package/dist/src/startup-greeting.js +78 -0
  48. package/dist/src/stt.d.ts +21 -0
  49. package/dist/src/stt.js +70 -0
  50. package/dist/src/tools/channel.d.ts +16 -0
  51. package/dist/src/tools/channel.js +234 -0
  52. package/dist/src/tools/remind.d.ts +2 -0
  53. package/dist/src/tools/remind.js +247 -0
  54. package/dist/src/types.d.ts +175 -0
  55. package/dist/src/types.js +1 -0
  56. package/dist/src/typing-keepalive.d.ts +27 -0
  57. package/dist/src/typing-keepalive.js +64 -0
  58. package/dist/src/update-checker.d.ts +34 -0
  59. package/dist/src/update-checker.js +166 -0
  60. package/dist/src/user-messages.d.ts +8 -0
  61. package/dist/src/user-messages.js +8 -0
  62. package/dist/src/utils/audio-convert.d.ts +89 -0
  63. package/dist/src/utils/audio-convert.js +704 -0
  64. package/dist/src/utils/file-utils.d.ts +55 -0
  65. package/dist/src/utils/file-utils.js +150 -0
  66. package/dist/src/utils/image-size.d.ts +51 -0
  67. package/dist/src/utils/image-size.js +234 -0
  68. package/dist/src/utils/media-tags.d.ts +14 -0
  69. package/dist/src/utils/media-tags.js +164 -0
  70. package/dist/src/utils/payload.d.ts +112 -0
  71. package/dist/src/utils/payload.js +186 -0
  72. package/dist/src/utils/platform.d.ts +137 -0
  73. package/dist/src/utils/platform.js +390 -0
  74. package/dist/src/utils/text-parsing.d.ts +32 -0
  75. package/dist/src/utils/text-parsing.js +80 -0
  76. package/dist/src/utils/upload-cache.d.ts +34 -0
  77. package/dist/src/utils/upload-cache.js +93 -0
  78. package/index.ts +31 -0
  79. package/moltbot.plugin.json +16 -0
  80. package/node_modules/@eshaz/web-worker/LICENSE +201 -0
  81. package/node_modules/@eshaz/web-worker/README.md +134 -0
  82. package/node_modules/@eshaz/web-worker/browser.js +17 -0
  83. package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
  84. package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
  85. package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
  86. package/node_modules/@eshaz/web-worker/node.js +223 -0
  87. package/node_modules/@eshaz/web-worker/package.json +54 -0
  88. package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
  89. package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
  90. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
  91. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
  92. package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
  93. package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
  94. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
  95. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
  96. package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
  97. package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
  98. package/node_modules/mpg123-decoder/README.md +265 -0
  99. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
  100. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
  101. package/node_modules/mpg123-decoder/index.js +8 -0
  102. package/node_modules/mpg123-decoder/package.json +58 -0
  103. package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
  104. package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
  105. package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
  106. package/node_modules/mpg123-decoder/types.d.ts +30 -0
  107. package/node_modules/silk-wasm/LICENSE +21 -0
  108. package/node_modules/silk-wasm/README.md +85 -0
  109. package/node_modules/silk-wasm/lib/index.cjs +16 -0
  110. package/node_modules/silk-wasm/lib/index.d.ts +70 -0
  111. package/node_modules/silk-wasm/lib/index.mjs +16 -0
  112. package/node_modules/silk-wasm/lib/silk.wasm +0 -0
  113. package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
  114. package/node_modules/silk-wasm/package.json +39 -0
  115. package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
  116. package/node_modules/simple-yenc/.prettierignore +1 -0
  117. package/node_modules/simple-yenc/LICENSE +7 -0
  118. package/node_modules/simple-yenc/README.md +163 -0
  119. package/node_modules/simple-yenc/dist/esm.js +1 -0
  120. package/node_modules/simple-yenc/dist/index.js +1 -0
  121. package/node_modules/simple-yenc/package.json +50 -0
  122. package/node_modules/simple-yenc/rollup.config.js +27 -0
  123. package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
  124. package/node_modules/ws/LICENSE +20 -0
  125. package/node_modules/ws/README.md +548 -0
  126. package/node_modules/ws/browser.js +8 -0
  127. package/node_modules/ws/index.js +13 -0
  128. package/node_modules/ws/lib/buffer-util.js +131 -0
  129. package/node_modules/ws/lib/constants.js +19 -0
  130. package/node_modules/ws/lib/event-target.js +292 -0
  131. package/node_modules/ws/lib/extension.js +203 -0
  132. package/node_modules/ws/lib/limiter.js +55 -0
  133. package/node_modules/ws/lib/permessage-deflate.js +528 -0
  134. package/node_modules/ws/lib/receiver.js +706 -0
  135. package/node_modules/ws/lib/sender.js +602 -0
  136. package/node_modules/ws/lib/stream.js +161 -0
  137. package/node_modules/ws/lib/subprotocol.js +62 -0
  138. package/node_modules/ws/lib/validation.js +152 -0
  139. package/node_modules/ws/lib/websocket-server.js +554 -0
  140. package/node_modules/ws/lib/websocket.js +1393 -0
  141. package/node_modules/ws/package.json +69 -0
  142. package/node_modules/ws/wrapper.mjs +8 -0
  143. package/openclaw.plugin.json +16 -0
  144. package/package.json +76 -0
  145. package/scripts/cleanup-legacy-plugins.sh +124 -0
  146. package/scripts/proactive-api-server.ts +369 -0
  147. package/scripts/send-proactive.ts +293 -0
  148. package/scripts/set-markdown.sh +156 -0
  149. package/scripts/test-sendmedia.ts +116 -0
  150. package/scripts/upgrade-via-alt-pkg.sh +307 -0
  151. package/scripts/upgrade-via-npm.ps1 +296 -0
  152. package/scripts/upgrade-via-npm.sh +301 -0
  153. package/scripts/upgrade-via-source.sh +774 -0
  154. package/skills/qqbot-channel/SKILL.md +263 -0
  155. package/skills/qqbot-channel/references/api_references.md +521 -0
  156. package/skills/qqbot-media/SKILL.md +56 -0
  157. package/skills/qqbot-remind/SKILL.md +149 -0
  158. package/src/admin-resolver.ts +140 -0
  159. package/src/api.ts +819 -0
  160. package/src/bot-logs-2026-03-21T11-21-47(2).txt +46 -0
  161. package/src/channel.ts +381 -0
  162. package/src/config.ts +187 -0
  163. package/src/credential-backup.ts +72 -0
  164. package/src/gateway.log +43 -0
  165. package/src/gateway.ts +1404 -0
  166. package/src/image-server.ts +539 -0
  167. package/src/inbound-attachments.ts +304 -0
  168. package/src/known-users.ts +353 -0
  169. package/src/message-queue.ts +169 -0
  170. package/src/onboarding.ts +274 -0
  171. package/src/openclaw-2026-03-21.log +3729 -0
  172. package/src/openclaw-plugin-sdk.d.ts +522 -0
  173. package/src/outbound-deliver.ts +552 -0
  174. package/src/outbound.ts +1266 -0
  175. package/src/proactive.ts +530 -0
  176. package/src/ref-index-store.ts +357 -0
  177. package/src/reply-dispatcher.ts +334 -0
  178. package/src/runtime.ts +14 -0
  179. package/src/session-store.ts +303 -0
  180. package/src/slash-commands.ts +1305 -0
  181. package/src/startup-greeting.ts +98 -0
  182. package/src/stt.ts +86 -0
  183. package/src/tools/channel.ts +281 -0
  184. package/src/tools/remind.ts +296 -0
  185. package/src/types.ts +183 -0
  186. package/src/typing-keepalive.ts +59 -0
  187. package/src/update-checker.ts +179 -0
  188. package/src/user-messages.ts +7 -0
  189. package/src/utils/audio-convert.ts +803 -0
  190. package/src/utils/file-utils.ts +167 -0
  191. package/src/utils/image-size.ts +266 -0
  192. package/src/utils/media-tags.ts +182 -0
  193. package/src/utils/payload.ts +265 -0
  194. package/src/utils/platform.ts +435 -0
  195. package/src/utils/text-parsing.ts +82 -0
  196. package/src/utils/upload-cache.ts +128 -0
  197. package/tsconfig.json +16 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,134 @@
1
+ <h1 align="center">
2
+ web-worker
3
+ <a href="https://www.npmjs.org/package/web-worker"><img src="https://img.shields.io/npm/v/web-worker.svg?style=flat-square" alt="npm"></a>
4
+ </h1>
5
+ <p align="center">
6
+ Native cross-platform Web Workers. Works in published npm modules.
7
+ </p>
8
+ <p align="center">
9
+ <img src="https://user-images.githubusercontent.com/105127/79602228-1998bf00-80b8-11ea-91e4-26b212aabaa2.png" width="1000" alt="">
10
+ </p>
11
+
12
+ **In Node**, it's a web-compatible Worker implementation atop Node's [worker_threads](https://nodejs.org/api/worker_threads.html).
13
+
14
+ **In the browser** (and when bundled for the browser), it's simply an alias of `Worker`.
15
+
16
+ ### Features
17
+
18
+ _Here's how this is different from worker_threads:_
19
+
20
+ - makes Worker code compatible across browser and Node
21
+ - supports Module Workers (`{type:'module'}`) natively in Node 12.8+
22
+ - uses DOM-style events (`Event.data`, `Event.type`, etc)
23
+ - supports event handler properties (`worker.onmessage=..`)
24
+ - `Worker()` accepts a module URL, Blob URL or Data URL
25
+ - emulates browser-style [WorkerGlobalScope] within the worker
26
+
27
+ ### Usage Example
28
+
29
+ In its simplest form:
30
+
31
+ ```js
32
+ import Worker from 'web-worker';
33
+
34
+ const worker = new Worker('data:,postMessage("hello")');
35
+ worker.onmessage = e => console.log(e.data); // "hello"
36
+ ```
37
+
38
+ <table>
39
+ <thead><tr><th><strong>main.js</strong></th><th><strong>worker.js</strong></th></tr></thead>
40
+ <tbody><tr><td>
41
+
42
+ ```js
43
+ import Worker from 'web-worker';
44
+
45
+ const url = new URL('./worker.js', import.meta.url);
46
+ const worker = new Worker(url);
47
+
48
+ worker.addEventListener('message', e => {
49
+ console.log(e.data) // "hiya!"
50
+ });
51
+
52
+ worker.postMessage('hello');
53
+ ```
54
+
55
+ </td><td valign="top">
56
+
57
+ ```js
58
+ addEventListener('message', e => {
59
+ if (e.data === 'hello') {
60
+ postMessage('hiya!');
61
+ }
62
+ });
63
+ ```
64
+
65
+ </td></tr></tbody>
66
+ </table>
67
+
68
+ 👉 Notice how `new URL('./worker.js', import.meta.url)` is used above to load the worker relative to the current module instead of the application base URL. Without this, Worker URLs are relative to a document's URL, which in Node.js is interpreted to be `process.cwd()`.
69
+
70
+ > _Support for this pattern in build tools and test frameworks is still limited. We are [working on growing this](https://github.com/developit/web-worker/issues/4)._
71
+
72
+ ### Module Workers
73
+
74
+ Module Workers are supported in Node 12.8+ using this plugin, leveraging Node's native ES Modules support.
75
+ In the browser, they can be used natively in Chrome 80+, or in all browsers via [worker-plugin] or [rollup-plugin-off-main-thread]. As with classic workers, there is no difference in usage between Node and the browser:
76
+
77
+ <table>
78
+ <thead><tr><th><strong>main.mjs</strong></th><th><strong>worker.mjs</strong></th></tr></thead>
79
+ <tbody><tr><td>
80
+
81
+ ```js
82
+ import Worker from 'web-worker';
83
+
84
+ const worker = new Worker(
85
+ new URL('./worker.mjs', import.meta.url),
86
+ { type: 'module' }
87
+ );
88
+ worker.addEventListener('message', e => {
89
+ console.log(e.data) // "200 OK"
90
+ });
91
+ worker.postMessage('https://httpstat.us/200');
92
+ ```
93
+
94
+ </td><td valign="top">
95
+
96
+ ```js
97
+ import fetch from 'isomorphic-fetch';
98
+
99
+ addEventListener('message', async e => {
100
+ const url = e.data;
101
+ const res = await fetch(url)
102
+ const text = await res.text();
103
+ postMessage(text);
104
+ });
105
+ ```
106
+
107
+ </td></tr></tbody>
108
+ </table>
109
+
110
+
111
+ ### Data URLs
112
+
113
+ Instantiating Worker using a Data URL is supported in both module and classic workers:
114
+
115
+ ```js
116
+ import Worker from 'web-worker';
117
+
118
+ const worker = new Worker(`data:application/javascript,postMessage(42)`);
119
+ worker.addEventListener('message', e => {
120
+ console.log(e.data) // 42
121
+ });
122
+ ```
123
+
124
+ ### Special Thanks
125
+
126
+ This module aims to provide a simple and forgettable piece of infrastructure,
127
+ and as such it needed an obvious and descriptive name.
128
+ [@calvinmetcalf](https://github.com/calvinmetcalf), who you may recognize as the author of [Lie](https://github.com/calvinmetcalf/lie) and other fine modules, gratiously offered up the name from his `web-worker` package.
129
+ Thanks Calvin!
130
+
131
+
132
+ [worker-plugin]: https://github.com/googlechromelabs/worker-plugin
133
+ [rollup-plugin-off-main-thread]: https://github.com/surma/rollup-plugin-off-main-thread
134
+ [WorkerGlobalScope]: https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Copyright 2020 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ export default Worker;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Copyright 2020 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ module.exports = Worker;
@@ -0,0 +1,219 @@
1
+ /**
2
+ * Copyright 2020 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ const URL = require('url');
17
+ const VM = require('vm');
18
+ const threads = require('worker_threads');
19
+ const WORKER = Symbol.for('worker');
20
+ const EVENTS = Symbol.for('events');
21
+ class EventTarget {
22
+ constructor() {
23
+ Object.defineProperty(this, EVENTS, {
24
+ value: new Map()
25
+ });
26
+ }
27
+ dispatchEvent(event) {
28
+ event.target = event.currentTarget = this;
29
+ if (this['on' + event.type]) {
30
+ try {
31
+ this['on' + event.type](event);
32
+ } catch (err) {
33
+ console.error(err);
34
+ }
35
+ }
36
+ const list = this[EVENTS].get(event.type);
37
+ if (list == null) return;
38
+ list.forEach(handler => {
39
+ try {
40
+ handler.call(this, event);
41
+ } catch (err) {
42
+ console.error(err);
43
+ }
44
+ });
45
+ }
46
+ addEventListener(type, fn) {
47
+ let events = this[EVENTS].get(type);
48
+ if (!events) this[EVENTS].set(type, events = []);
49
+ events.push(fn);
50
+ }
51
+ removeEventListener(type, fn) {
52
+ let events = this[EVENTS].get(type);
53
+ if (events) {
54
+ const index = events.indexOf(fn);
55
+ if (index !== -1) events.splice(index, 1);
56
+ }
57
+ }
58
+ }
59
+ function Event(type, target) {
60
+ this.type = type;
61
+ this.timeStamp = Date.now();
62
+ this.target = this.currentTarget = this.data = null;
63
+ }
64
+
65
+ // this module is used self-referentially on both sides of the
66
+ // thread boundary, but behaves differently in each context.
67
+ module.exports = threads.isMainThread ? mainThread() : workerThread();
68
+ const baseUrl = URL.pathToFileURL(process.cwd() + '/');
69
+ function mainThread() {
70
+ /**
71
+ * A web-compatible Worker implementation atop Node's worker_threads.
72
+ * - uses DOM-style events (Event.data, Event.type, etc)
73
+ * - supports event handler properties (worker.onmessage)
74
+ * - Worker() constructor accepts a module URL
75
+ * - accepts the {type:'module'} option
76
+ * - emulates WorkerGlobalScope within the worker
77
+ * @param {string} url The URL or module specifier to load
78
+ * @param {object} [options] Worker construction options
79
+ * @param {string} [options.name] Available as `self.name` within the Worker
80
+ * @param {string} [options.type="classic"] Pass "module" to create a Module Worker.
81
+ */
82
+ class Worker extends EventTarget {
83
+ constructor(url, options) {
84
+ super();
85
+ const {
86
+ name,
87
+ type
88
+ } = options || {};
89
+ url += '';
90
+ let mod;
91
+ if (/^data:/.test(url)) {
92
+ mod = url;
93
+ } else {
94
+ mod = URL.fileURLToPath(new URL.URL(url, baseUrl));
95
+ }
96
+ const worker = new threads.Worker(__filename, {
97
+ workerData: {
98
+ mod,
99
+ name,
100
+ type
101
+ }
102
+ });
103
+ Object.defineProperty(this, WORKER, {
104
+ value: worker
105
+ });
106
+ worker.on('message', data => {
107
+ const event = new Event('message');
108
+ event.data = data;
109
+ this.dispatchEvent(event);
110
+ });
111
+ worker.on('error', error => {
112
+ error.type = 'error';
113
+ this.dispatchEvent(error);
114
+ });
115
+ worker.on('exit', () => {
116
+ this.dispatchEvent(new Event('close'));
117
+ });
118
+ }
119
+ postMessage(data, transferList) {
120
+ this[WORKER].postMessage(data, transferList);
121
+ }
122
+ terminate() {
123
+ this[WORKER].terminate();
124
+ }
125
+ }
126
+ Worker.prototype.onmessage = Worker.prototype.onerror = Worker.prototype.onclose = null;
127
+ return Worker;
128
+ }
129
+ function workerThread() {
130
+ let {
131
+ mod,
132
+ name,
133
+ type
134
+ } = threads.workerData || {};
135
+ if (!mod) return mainThread();
136
+
137
+ // turn global into a mock WorkerGlobalScope
138
+ const self = global.self = global;
139
+
140
+ // enqueue messages to dispatch after modules are loaded
141
+ let q = [];
142
+ function flush() {
143
+ const buffered = q;
144
+ q = null;
145
+ buffered.forEach(event => {
146
+ self.dispatchEvent(event);
147
+ });
148
+ }
149
+ threads.parentPort.on('message', data => {
150
+ const event = new Event('message');
151
+ event.data = data;
152
+ if (q == null) self.dispatchEvent(event);else q.push(event);
153
+ });
154
+ threads.parentPort.on('error', err => {
155
+ err.type = 'Error';
156
+ self.dispatchEvent(err);
157
+ });
158
+ class WorkerGlobalScope extends EventTarget {
159
+ postMessage(data, transferList) {
160
+ threads.parentPort.postMessage(data, transferList);
161
+ }
162
+ // Emulates https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/close
163
+ close() {
164
+ process.exit();
165
+ }
166
+ }
167
+ let proto = Object.getPrototypeOf(global);
168
+ delete proto.constructor;
169
+ Object.defineProperties(WorkerGlobalScope.prototype, proto);
170
+ proto = Object.setPrototypeOf(global, new WorkerGlobalScope());
171
+ ['postMessage', 'addEventListener', 'removeEventListener', 'dispatchEvent'].forEach(fn => {
172
+ proto[fn] = proto[fn].bind(global);
173
+ });
174
+ global.name = name;
175
+ const isDataUrl = /^data:/.test(mod);
176
+ if (type === 'module') {
177
+ import(mod).catch(err => {
178
+ if (isDataUrl && err.message === 'Not supported') {
179
+ console.warn('Worker(): Importing data: URLs requires Node 12.10+. Falling back to classic worker.');
180
+ return evaluateDataUrl(mod, name);
181
+ }
182
+ console.error(err);
183
+ }).then(flush);
184
+ } else {
185
+ try {
186
+ if (/^data:/.test(mod)) {
187
+ evaluateDataUrl(mod, name);
188
+ } else {
189
+ require(mod);
190
+ }
191
+ } catch (err) {
192
+ console.error(err);
193
+ }
194
+ Promise.resolve().then(flush);
195
+ }
196
+ }
197
+ function evaluateDataUrl(url, name) {
198
+ const {
199
+ data
200
+ } = parseDataUrl(url);
201
+ return VM.runInThisContext(data, {
202
+ filename: 'worker.<' + (name || 'data:') + '>'
203
+ });
204
+ }
205
+ function parseDataUrl(url) {
206
+ let [m, type, encoding, data] = url.match(/^data: *([^;,]*)(?: *; *([^,]*))? *,(.*)$/) || [];
207
+ if (!m) throw Error('Invalid Data URL.');
208
+ if (encoding) switch (encoding.toLowerCase()) {
209
+ case 'base64':
210
+ data = Buffer.from(data, 'base64').toString();
211
+ break;
212
+ default:
213
+ throw Error('Unknown Data URL encoding "' + encoding + '"');
214
+ }
215
+ return {
216
+ type,
217
+ data
218
+ };
219
+ }
@@ -0,0 +1,4 @@
1
+ type ConstructorOf<C> = { new (...args: any[]): C }
2
+
3
+ declare const _default: ConstructorOf<Worker>
4
+ export default _default