@leoqlin/openclaw-qqbot 1.6.7-alpha1

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 (218) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +484 -0
  3. package/README.zh.md +479 -0
  4. package/bin/qqbot-cli.js +243 -0
  5. package/dist/index.d.ts +17 -0
  6. package/dist/index.js +26 -0
  7. package/dist/src/admin-resolver.d.ts +33 -0
  8. package/dist/src/admin-resolver.js +157 -0
  9. package/dist/src/api.d.ts +301 -0
  10. package/dist/src/api.js +890 -0
  11. package/dist/src/channel.d.ts +29 -0
  12. package/dist/src/channel.js +452 -0
  13. package/dist/src/config.d.ts +56 -0
  14. package/dist/src/config.js +278 -0
  15. package/dist/src/credential-backup.d.ts +31 -0
  16. package/dist/src/credential-backup.js +66 -0
  17. package/dist/src/deliver-debounce.d.ts +74 -0
  18. package/dist/src/deliver-debounce.js +174 -0
  19. package/dist/src/gateway.d.ts +18 -0
  20. package/dist/src/gateway.js +2005 -0
  21. package/dist/src/group-history.d.ts +136 -0
  22. package/dist/src/group-history.js +226 -0
  23. package/dist/src/image-server.d.ts +87 -0
  24. package/dist/src/image-server.js +570 -0
  25. package/dist/src/inbound-attachments.d.ts +60 -0
  26. package/dist/src/inbound-attachments.js +248 -0
  27. package/dist/src/known-users.d.ts +100 -0
  28. package/dist/src/known-users.js +263 -0
  29. package/dist/src/message-gating.d.ts +53 -0
  30. package/dist/src/message-gating.js +107 -0
  31. package/dist/src/message-queue.d.ts +89 -0
  32. package/dist/src/message-queue.js +257 -0
  33. package/dist/src/onboarding.d.ts +10 -0
  34. package/dist/src/onboarding.js +203 -0
  35. package/dist/src/outbound-deliver.d.ts +48 -0
  36. package/dist/src/outbound-deliver.js +392 -0
  37. package/dist/src/outbound.d.ts +205 -0
  38. package/dist/src/outbound.js +938 -0
  39. package/dist/src/proactive.d.ts +170 -0
  40. package/dist/src/proactive.js +399 -0
  41. package/dist/src/ref-index-store.d.ts +101 -0
  42. package/dist/src/ref-index-store.js +298 -0
  43. package/dist/src/reply-dispatcher.d.ts +35 -0
  44. package/dist/src/reply-dispatcher.js +311 -0
  45. package/dist/src/request-context.d.ts +25 -0
  46. package/dist/src/request-context.js +37 -0
  47. package/dist/src/runtime.d.ts +3 -0
  48. package/dist/src/runtime.js +10 -0
  49. package/dist/src/session-store.d.ts +52 -0
  50. package/dist/src/session-store.js +254 -0
  51. package/dist/src/slash-commands.d.ts +77 -0
  52. package/dist/src/slash-commands.js +1866 -0
  53. package/dist/src/startup-greeting.d.ts +30 -0
  54. package/dist/src/startup-greeting.js +97 -0
  55. package/dist/src/streaming.d.ts +247 -0
  56. package/dist/src/streaming.js +899 -0
  57. package/dist/src/stt.d.ts +21 -0
  58. package/dist/src/stt.js +70 -0
  59. package/dist/src/tools/channel.d.ts +16 -0
  60. package/dist/src/tools/channel.js +234 -0
  61. package/dist/src/tools/remind.d.ts +2 -0
  62. package/dist/src/tools/remind.js +256 -0
  63. package/dist/src/types.d.ts +367 -0
  64. package/dist/src/types.js +17 -0
  65. package/dist/src/typing-keepalive.d.ts +27 -0
  66. package/dist/src/typing-keepalive.js +64 -0
  67. package/dist/src/update-checker.d.ts +36 -0
  68. package/dist/src/update-checker.js +171 -0
  69. package/dist/src/utils/audio-convert.d.ts +98 -0
  70. package/dist/src/utils/audio-convert.js +755 -0
  71. package/dist/src/utils/chunked-upload.d.ts +68 -0
  72. package/dist/src/utils/chunked-upload.js +341 -0
  73. package/dist/src/utils/file-utils.d.ts +61 -0
  74. package/dist/src/utils/file-utils.js +172 -0
  75. package/dist/src/utils/image-size.d.ts +51 -0
  76. package/dist/src/utils/image-size.js +234 -0
  77. package/dist/src/utils/media-send.d.ts +158 -0
  78. package/dist/src/utils/media-send.js +499 -0
  79. package/dist/src/utils/media-tags.d.ts +14 -0
  80. package/dist/src/utils/media-tags.js +165 -0
  81. package/dist/src/utils/payload.d.ts +112 -0
  82. package/dist/src/utils/payload.js +186 -0
  83. package/dist/src/utils/pkg-version.d.ts +5 -0
  84. package/dist/src/utils/pkg-version.js +61 -0
  85. package/dist/src/utils/platform.d.ts +137 -0
  86. package/dist/src/utils/platform.js +390 -0
  87. package/dist/src/utils/ssrf-guard.d.ts +25 -0
  88. package/dist/src/utils/ssrf-guard.js +91 -0
  89. package/dist/src/utils/text-parsing.d.ts +36 -0
  90. package/dist/src/utils/text-parsing.js +75 -0
  91. package/dist/src/utils/upload-cache.d.ts +34 -0
  92. package/dist/src/utils/upload-cache.js +93 -0
  93. package/index.ts +31 -0
  94. package/node_modules/@eshaz/web-worker/LICENSE +201 -0
  95. package/node_modules/@eshaz/web-worker/README.md +134 -0
  96. package/node_modules/@eshaz/web-worker/browser.js +17 -0
  97. package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
  98. package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
  99. package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
  100. package/node_modules/@eshaz/web-worker/node.js +223 -0
  101. package/node_modules/@eshaz/web-worker/package.json +54 -0
  102. package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
  103. package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
  104. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
  105. package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
  106. package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
  107. package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
  108. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
  109. package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
  110. package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
  111. package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
  112. package/node_modules/mpg123-decoder/README.md +265 -0
  113. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
  114. package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
  115. package/node_modules/mpg123-decoder/index.js +8 -0
  116. package/node_modules/mpg123-decoder/package.json +58 -0
  117. package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
  118. package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
  119. package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
  120. package/node_modules/mpg123-decoder/types.d.ts +30 -0
  121. package/node_modules/silk-wasm/LICENSE +21 -0
  122. package/node_modules/silk-wasm/README.md +85 -0
  123. package/node_modules/silk-wasm/lib/index.cjs +16 -0
  124. package/node_modules/silk-wasm/lib/index.d.ts +70 -0
  125. package/node_modules/silk-wasm/lib/index.mjs +16 -0
  126. package/node_modules/silk-wasm/lib/silk.wasm +0 -0
  127. package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
  128. package/node_modules/silk-wasm/package.json +39 -0
  129. package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
  130. package/node_modules/simple-yenc/.prettierignore +1 -0
  131. package/node_modules/simple-yenc/LICENSE +7 -0
  132. package/node_modules/simple-yenc/README.md +163 -0
  133. package/node_modules/simple-yenc/dist/esm.js +1 -0
  134. package/node_modules/simple-yenc/dist/index.js +1 -0
  135. package/node_modules/simple-yenc/package.json +50 -0
  136. package/node_modules/simple-yenc/rollup.config.js +27 -0
  137. package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
  138. package/node_modules/ws/LICENSE +20 -0
  139. package/node_modules/ws/README.md +548 -0
  140. package/node_modules/ws/browser.js +8 -0
  141. package/node_modules/ws/index.js +22 -0
  142. package/node_modules/ws/lib/buffer-util.js +131 -0
  143. package/node_modules/ws/lib/constants.js +19 -0
  144. package/node_modules/ws/lib/event-target.js +292 -0
  145. package/node_modules/ws/lib/extension.js +203 -0
  146. package/node_modules/ws/lib/limiter.js +55 -0
  147. package/node_modules/ws/lib/permessage-deflate.js +528 -0
  148. package/node_modules/ws/lib/receiver.js +706 -0
  149. package/node_modules/ws/lib/sender.js +602 -0
  150. package/node_modules/ws/lib/stream.js +161 -0
  151. package/node_modules/ws/lib/subprotocol.js +62 -0
  152. package/node_modules/ws/lib/validation.js +152 -0
  153. package/node_modules/ws/lib/websocket-server.js +554 -0
  154. package/node_modules/ws/lib/websocket.js +1393 -0
  155. package/node_modules/ws/package.json +70 -0
  156. package/node_modules/ws/wrapper.mjs +21 -0
  157. package/openclaw.plugin.json +17 -0
  158. package/package.json +70 -0
  159. package/preload.cjs +33 -0
  160. package/scripts/cleanup-legacy-plugins.sh +124 -0
  161. package/scripts/link-sdk-core.cjs +185 -0
  162. package/scripts/postinstall-link-sdk.js +126 -0
  163. package/scripts/proactive-api-server.ts +369 -0
  164. package/scripts/send-proactive.ts +293 -0
  165. package/scripts/set-markdown.sh +156 -0
  166. package/scripts/test-sendmedia.ts +116 -0
  167. package/scripts/upgrade-via-npm.ps1 +460 -0
  168. package/scripts/upgrade-via-npm.sh +652 -0
  169. package/scripts/upgrade-via-source.sh +1026 -0
  170. package/skills/qqbot-channel/SKILL.md +263 -0
  171. package/skills/qqbot-channel/references/api_references.md +521 -0
  172. package/skills/qqbot-media/SKILL.md +60 -0
  173. package/skills/qqbot-remind/SKILL.md +159 -0
  174. package/src/admin-resolver.ts +181 -0
  175. package/src/api.ts +1284 -0
  176. package/src/channel.ts +477 -0
  177. package/src/config.ts +347 -0
  178. package/src/credential-backup.ts +72 -0
  179. package/src/deliver-debounce.ts +229 -0
  180. package/src/gateway.ts +2245 -0
  181. package/src/group-history.ts +328 -0
  182. package/src/image-server.ts +675 -0
  183. package/src/inbound-attachments.ts +321 -0
  184. package/src/known-users.ts +353 -0
  185. package/src/message-gating.ts +190 -0
  186. package/src/message-queue.ts +352 -0
  187. package/src/onboarding.ts +274 -0
  188. package/src/openclaw-plugin-sdk.d.ts +587 -0
  189. package/src/outbound-deliver.ts +473 -0
  190. package/src/outbound.ts +1131 -0
  191. package/src/proactive.ts +530 -0
  192. package/src/ref-index-store.ts +412 -0
  193. package/src/reply-dispatcher.ts +334 -0
  194. package/src/request-context.ts +49 -0
  195. package/src/runtime.ts +14 -0
  196. package/src/session-store.ts +303 -0
  197. package/src/slash-commands.ts +2030 -0
  198. package/src/startup-greeting.ts +120 -0
  199. package/src/streaming.ts +1077 -0
  200. package/src/stt.ts +86 -0
  201. package/src/tools/channel.ts +281 -0
  202. package/src/tools/remind.ts +308 -0
  203. package/src/types.ts +391 -0
  204. package/src/typing-keepalive.ts +59 -0
  205. package/src/update-checker.ts +186 -0
  206. package/src/utils/audio-convert.ts +859 -0
  207. package/src/utils/chunked-upload.ts +483 -0
  208. package/src/utils/file-utils.ts +193 -0
  209. package/src/utils/image-size.ts +266 -0
  210. package/src/utils/media-send.ts +631 -0
  211. package/src/utils/media-tags.ts +183 -0
  212. package/src/utils/payload.ts +265 -0
  213. package/src/utils/pkg-version.ts +64 -0
  214. package/src/utils/platform.ts +435 -0
  215. package/src/utils/ssrf-guard.ts +102 -0
  216. package/src/utils/text-parsing.ts +85 -0
  217. package/src/utils/upload-cache.ts +128 -0
  218. package/tsconfig.json +16 -0
@@ -0,0 +1,231 @@
1
+ import { decode } from "simple-yenc";
2
+
3
+ export default function WASMAudioDecoderCommon() {
4
+ // setup static methods
5
+ const uint8Array = Uint8Array;
6
+ const float32Array = Float32Array;
7
+
8
+ if (!WASMAudioDecoderCommon.modules) {
9
+ Object.defineProperties(WASMAudioDecoderCommon, {
10
+ modules: {
11
+ value: new WeakMap(),
12
+ },
13
+
14
+ setModule: {
15
+ value(Ref, module) {
16
+ WASMAudioDecoderCommon.modules.set(Ref, Promise.resolve(module));
17
+ },
18
+ },
19
+
20
+ getModule: {
21
+ value(Ref, wasmString) {
22
+ let module = WASMAudioDecoderCommon.modules.get(Ref);
23
+
24
+ if (!module) {
25
+ if (!wasmString) {
26
+ wasmString = Ref.wasm;
27
+ module = WASMAudioDecoderCommon.inflateDynEncodeString(
28
+ wasmString,
29
+ ).then((data) => WebAssembly.compile(data));
30
+ } else {
31
+ module = WebAssembly.compile(decode(wasmString));
32
+ }
33
+
34
+ WASMAudioDecoderCommon.modules.set(Ref, module);
35
+ }
36
+
37
+ return module;
38
+ },
39
+ },
40
+
41
+ concatFloat32: {
42
+ value(buffers, length) {
43
+ let ret = new float32Array(length),
44
+ i = 0,
45
+ offset = 0;
46
+
47
+ while (i < buffers.length) {
48
+ ret.set(buffers[i], offset);
49
+ offset += buffers[i++].length;
50
+ }
51
+
52
+ return ret;
53
+ },
54
+ },
55
+
56
+ getDecodedAudio: {
57
+ value: (errors, channelData, samplesDecoded, sampleRate, bitDepth) => ({
58
+ errors,
59
+ channelData,
60
+ samplesDecoded,
61
+ sampleRate,
62
+ bitDepth,
63
+ }),
64
+ },
65
+
66
+ getDecodedAudioMultiChannel: {
67
+ value(
68
+ errors,
69
+ input,
70
+ channelsDecoded,
71
+ samplesDecoded,
72
+ sampleRate,
73
+ bitDepth,
74
+ ) {
75
+ let channelData = [],
76
+ i,
77
+ j;
78
+
79
+ for (i = 0; i < channelsDecoded; i++) {
80
+ const channel = [];
81
+ for (j = 0; j < input.length; ) channel.push(input[j++][i] || []);
82
+ channelData.push(
83
+ WASMAudioDecoderCommon.concatFloat32(channel, samplesDecoded),
84
+ );
85
+ }
86
+
87
+ return WASMAudioDecoderCommon.getDecodedAudio(
88
+ errors,
89
+ channelData,
90
+ samplesDecoded,
91
+ sampleRate,
92
+ bitDepth,
93
+ );
94
+ },
95
+ },
96
+
97
+ /*
98
+ ******************
99
+ * Compression Code
100
+ ******************
101
+ */
102
+
103
+ inflateDynEncodeString: {
104
+ value(source) {
105
+ source = decode(source);
106
+
107
+ return new Promise((resolve) => {
108
+ // prettier-ignore
109
+ const puffString = String.raw`dynEncode012804c7886d(‰›•)((()>+ˆ*§§)§ˆ,§§§§)§ˆ+§§§)§+.-()(*)-+)(ˆ.7*§)i¸¸,3§(i¸¸,3/G+.••—š¡*(,˜ŽŽ(,3‡‡‰˜‡Š‰›+)2å:-œ),§H(P*DI*H(P*@I++hH)H*r,hH(H(P*<J,i)’^*<H,H(P*4’U((I-H(H*i0’J,^*DH+H-H*œšI+H,I*4)33H(H*H)“^*DH(H+H)^*@H+i§H)œi§›™3æ*).§K(iH“I/+§H,iHn,§H+i(H+i(rCJ0I,H*I-+hH,,hH(H-V)(i)œ’J.H.W)(i)’c)(H,i)“I,H-i*’I-4)33i(I.*hH(V)(H+n5(H(i*’I-i(I,i)I.+hH,i*’J+iHn,hi(I-i*I,+hH,H/’H-c)(H,iFn,hi(I,+hH,H0n5-H*V)(J(,hH/H(i)œ’J(H(V)(J(i)’c)(H)H(i)œ’H,c)(3H*i*’I*H,i)’I,4(3(-H(H,’W)(H-’I-H,i*’I,4)3(3(3H,H-’I1H+I,H.i)œH1V)(“J.i(v5(33H.-H(H,’i(c)(H,i*’I,4)333–)-§i*I*+§H*iHn,hiž73H,H(i)8(H+šJ+H)P*(H*’V)(J-“r,§H)P*,H.i)œ’H+H,“i)œ’V)(-H*i*’I*H+i)œI+H-H.’I.H,H-’i)œI,4)333Ã+)-§iø7i(^*(iü7I,*h+hH+iDn,h*hilI+i)I,+hH+,hH+iô7’H,c)(i)H+i´8’W)(œH,’I,H+i*’I+4)-+hH(H)8*J-i(p5.*h*h*hH-i')u,hH(P*(J+,hH(P*0J,H(P*,n50H+H,’H-b((3H(P*0i)’I.4)3H-i¨*n5*H-iÅ*s,hiž73H-i)œJ+V)&+I,H(H+V)æ,8(I.H(H*8*J-i(p51H-i)œJ+i¸7’V)(H(H+iø7’V)(8(’J/H(P*0J+s,hi73H+H,H.’J,’I.H(P*(m5(H.H(P*,s5.+hH,m5*H(P*(J.H+’H.H+H/“’U((b((H(H(P*0i)’J+^*0H,i)“I,4(3(3H(H.^*03H-i¨*o5)33i(73(3(3-H,H+i)žc)(H,i*’I,H+i)’I+4)33i)I-3H-3!2)0§K(i2“J,L(H,H(^*(H,H*^*4H,i(^*0H,i(^*DH,j(_*<H,H)P*(^*,H,H+P*(^*8*h*h+hH,i)8(I3i§I**h*h*h*h*h*h*hH,i*8(6+(),03H,j(_*@i*I-H,P*<J.i,’J(H,P*8J/s50H,H.i+’J0^*<i¦I*H.H,P*4J1’J.U(*H.U((J2›i')o5/H.U()I.H,H(^*<H0H1’U((H.i0œJ.i§›i0ži')™o5/H/H.H2šJ*H(’J.q50H,P*0J/H*’I-H,P*(J0,hH,P*,H-q,hi)I-423+hH*m5+H/H0’H(H1’U((b((H/i)’I/H(i)’I(H*i)“I*4(3(3H,H.^*<H,H-^*04*3iØ1U((5+i(I(i¨7i1^*(i$6iè1^*(i°7iè6^*(i¬7iÈ6^*(+hH(iÈ*n,hiÈ*I(+hH(i¨,n,hi¨,I(+hH(iØ,n,hiØ,I(+hH(iè,o,hH,i-’H(’i0c)(H(i*’I(4)33iè1i1H,i-’iÈ*8)Bi(I(+hH(ido,hH,i-’H(’i-c)(H(i*’I(4)33iÈ6iè6H,i-’iF8)BiØ1i)b((41-H,i-’H(’i/c)(H(i*’I(4)3(3(-H,i-’H(’i1c)(H(i*’I(4)3(3(-H,i-’H(’i0c)(H(i*’I(4)3(3(3H,H/^*0H,H(^*<3i(I*4*3H,H,i¸)’^*TH,H,iø-’^*PH,H,iX’^*LH,H,i(’^*HH,i-8(I(H,i-8(I-i¥I*H,i,8(I.H(iErH-iErš5)H(i©*’I1H-i)’I0i(i;H.i,’J(i(H(i(rCJ(“J*H*i;sCI*i¨1I-H(I/+hH/,hH,i-’H-V)(i)œ’H,i+8(c)(H/i)“I/H-i*’I-H*i)’I*4)-H(i)œi¨1’I/+hH(H*o,hH,i-’H/V)(i)œ’i(c)(H/i*’I/H(i)’I(4)33i¤I*H,iø-’H,i¸)’H,i-’i;8)5+H0H1’I2i(I-+hH-H2p,hH,H,iP’8*J*i(p5-H*i7u,hH,i-’H-i)œ’H*c)(H-i)’I-4*3i(I/i+I.i+I(*h*h*hH*i8“6*(*)3H-m,hi£I*403H-i)œH,’W)-I/i*I(4)3i3I.i/I(3H2H,H(8(H.’J(H-’J.p,hi¢I*4.3H,i-’H-i)œ’I*+hH(,hH*H/c)(H*i*’I*H(i)“I(4)-H.I-4+3(3(33H,W)1m,hiŸI*4,3H,iø-’H,i¸)’H,i-’H18)J(,hi¡I*H(i(p5,H1H,V)ú-H,V)ø-’o5,3H,i(’H,iX’H,i-’H1i)œ’H08)J(,hi I*H(i(p5,H0H,V)šH,V)˜’o5,3H,H,iP’H,iH’8+I*4+3(3(3H,i$6i¬78+I*3H*H3šm5(3i)I-H*i(r5)3H)H,P*0^*(H+H,P*<^*(H*I-3H,i2’L(H-33Á)+(i¨03b+(,(-(.(/(0(1(2(3(5(7(9(;(?(C(G(K(S([(c(k({(‹(›(«(Ë(ë( (*)(iø03O)()()()(*(*(*(*(+(+(+(+(,(,(,(,(-(-(-(-(i¨13M8(9(:(((0(/(1(.(2(-(3(,(4(+(5(*(6()(7(T7œ‰šœ‡Ž‰œš›*S7•œ‰Š”U”—Љ”›S0›‘–U œ`;
110
+
111
+ WASMAudioDecoderCommon.getModule(WASMAudioDecoderCommon, puffString)
112
+ .then((wasm) => WebAssembly.instantiate(wasm, {}))
113
+ .then(({ exports }) => {
114
+ // required for minifiers that mangle the __heap_base property
115
+ const instanceExports = new Map(Object.entries(exports));
116
+
117
+ const puff = instanceExports.get("puff");
118
+ const memory = instanceExports.get("memory")["buffer"];
119
+ const dataArray = new uint8Array(memory);
120
+ const heapView = new DataView(memory);
121
+
122
+ let heapPos = instanceExports.get("__heap_base");
123
+
124
+ // source length
125
+ const sourceLength = source.length;
126
+ const sourceLengthPtr = heapPos;
127
+ heapPos += 4;
128
+ heapView.setInt32(sourceLengthPtr, sourceLength, true);
129
+
130
+ // source data
131
+ const sourcePtr = heapPos;
132
+ heapPos += sourceLength;
133
+ dataArray.set(source, sourcePtr);
134
+
135
+ // destination length
136
+ const destLengthPtr = heapPos;
137
+ heapPos += 4;
138
+ heapView.setInt32(
139
+ destLengthPtr,
140
+ dataArray.byteLength - heapPos,
141
+ true,
142
+ );
143
+
144
+ // destination data fills in the rest of the heap
145
+ puff(heapPos, destLengthPtr, sourcePtr, sourceLengthPtr);
146
+
147
+ resolve(
148
+ dataArray.slice(
149
+ heapPos,
150
+ heapPos + heapView.getInt32(destLengthPtr, true),
151
+ ),
152
+ );
153
+ });
154
+ });
155
+ },
156
+ },
157
+ });
158
+ }
159
+
160
+ Object.defineProperty(this, "wasm", {
161
+ enumerable: true,
162
+ get: () => this._wasm,
163
+ });
164
+
165
+ this.getOutputChannels = (outputData, channelsDecoded, samplesDecoded) => {
166
+ let output = [],
167
+ i = 0;
168
+
169
+ while (i < channelsDecoded)
170
+ output.push(
171
+ outputData.slice(
172
+ i * samplesDecoded,
173
+ i++ * samplesDecoded + samplesDecoded,
174
+ ),
175
+ );
176
+
177
+ return output;
178
+ };
179
+
180
+ this.allocateTypedArray = (len, TypedArray, setPointer = true) => {
181
+ const ptr = this._wasm.malloc(TypedArray.BYTES_PER_ELEMENT * len);
182
+ if (setPointer) this._pointers.add(ptr);
183
+
184
+ return {
185
+ ptr: ptr,
186
+ len: len,
187
+ buf: new TypedArray(this._wasm.HEAP, ptr, len),
188
+ };
189
+ };
190
+
191
+ this.free = () => {
192
+ this._pointers.forEach((ptr) => {
193
+ this._wasm.free(ptr);
194
+ });
195
+ this._pointers.clear();
196
+ };
197
+
198
+ this.codeToString = (ptr) => {
199
+ const characters = [],
200
+ heap = new Uint8Array(this._wasm.HEAP);
201
+ for (let character = heap[ptr]; character !== 0; character = heap[++ptr])
202
+ characters.push(character);
203
+
204
+ return String.fromCharCode.apply(null, characters);
205
+ };
206
+
207
+ this.addError = (
208
+ errors,
209
+ message,
210
+ frameLength,
211
+ frameNumber,
212
+ inputBytes,
213
+ outputSamples,
214
+ ) => {
215
+ errors.push({
216
+ message: message,
217
+ frameLength: frameLength,
218
+ frameNumber: frameNumber,
219
+ inputBytes: inputBytes,
220
+ outputSamples: outputSamples,
221
+ });
222
+ };
223
+
224
+ this.instantiate = (_EmscriptenWASM, _module) => {
225
+ if (_module) WASMAudioDecoderCommon.setModule(_EmscriptenWASM, _module);
226
+ this._wasm = new _EmscriptenWASM(WASMAudioDecoderCommon).instantiate();
227
+ this._pointers = new Set();
228
+
229
+ return this._wasm.ready.then(() => this);
230
+ };
231
+ }
@@ -0,0 +1,129 @@
1
+ import NodeWorker from "@eshaz/web-worker";
2
+ import WASMAudioDecoderCommon from "./WASMAudioDecoderCommon.js";
3
+
4
+ const getWorker = () => globalThis.Worker || NodeWorker;
5
+
6
+ export default class WASMAudioDecoderWorker extends getWorker() {
7
+ constructor(options, name, Decoder, EmscriptenWASM) {
8
+ if (!WASMAudioDecoderCommon.modules) new WASMAudioDecoderCommon();
9
+
10
+ let source = WASMAudioDecoderCommon.modules.get(Decoder);
11
+
12
+ if (!source) {
13
+ let type = "text/javascript",
14
+ isNode,
15
+ webworkerSourceCode =
16
+ "'use strict';" +
17
+ // dependencies need to be manually resolved when stringifying this function
18
+ `(${((_Decoder, _WASMAudioDecoderCommon, _EmscriptenWASM) => {
19
+ // We're in a Web Worker
20
+
21
+ // setup Promise that will be resolved once the WebAssembly Module is received
22
+ let decoder,
23
+ moduleResolve,
24
+ modulePromise = new Promise((resolve) => {
25
+ moduleResolve = resolve;
26
+ });
27
+
28
+ self.onmessage = ({ data: { id, command, data } }) => {
29
+ let messagePromise = modulePromise,
30
+ messagePayload = { id },
31
+ transferList;
32
+
33
+ if (command === "init") {
34
+ Object.defineProperties(_Decoder, {
35
+ WASMAudioDecoderCommon: { value: _WASMAudioDecoderCommon },
36
+ EmscriptenWASM: { value: _EmscriptenWASM },
37
+ module: { value: data.module },
38
+ isWebWorker: { value: true },
39
+ });
40
+
41
+ decoder = new _Decoder(data.options);
42
+ moduleResolve();
43
+ } else if (command === "free") {
44
+ decoder.free();
45
+ } else if (command === "ready") {
46
+ messagePromise = messagePromise.then(() => decoder.ready);
47
+ } else if (command === "reset") {
48
+ messagePromise = messagePromise.then(() => decoder.reset());
49
+ } else {
50
+ // "decode":
51
+ // "decodeFrame":
52
+ // "decodeFrames":
53
+ Object.assign(
54
+ messagePayload,
55
+ decoder[command](
56
+ // detach buffers
57
+ Array.isArray(data)
58
+ ? data.map((data) => new Uint8Array(data))
59
+ : new Uint8Array(data),
60
+ ),
61
+ );
62
+ // The "transferList" parameter transfers ownership of channel data to main thread,
63
+ // which avoids copying memory.
64
+ transferList = messagePayload.channelData
65
+ ? messagePayload.channelData.map((channel) => channel.buffer)
66
+ : [];
67
+ }
68
+
69
+ messagePromise.then(() =>
70
+ self.postMessage(messagePayload, transferList),
71
+ );
72
+ };
73
+ }).toString()})(${Decoder}, ${WASMAudioDecoderCommon}, ${EmscriptenWASM})`;
74
+
75
+ try {
76
+ isNode = typeof process.versions.node !== "undefined";
77
+ } catch {}
78
+
79
+ source = isNode
80
+ ? `data:${type};base64,${Buffer.from(webworkerSourceCode).toString(
81
+ "base64",
82
+ )}`
83
+ : URL.createObjectURL(new Blob([webworkerSourceCode], { type }));
84
+
85
+ WASMAudioDecoderCommon.modules.set(Decoder, source);
86
+ }
87
+
88
+ super(source, { name });
89
+
90
+ this._id = Number.MIN_SAFE_INTEGER;
91
+ this._enqueuedOperations = new Map();
92
+
93
+ this.onmessage = ({ data }) => {
94
+ const { id, ...rest } = data;
95
+ this._enqueuedOperations.get(id)(rest);
96
+ this._enqueuedOperations.delete(id);
97
+ };
98
+
99
+ new EmscriptenWASM(WASMAudioDecoderCommon).getModule().then((module) => {
100
+ this.postToDecoder("init", { module, options });
101
+ });
102
+ }
103
+
104
+ async postToDecoder(command, data) {
105
+ return new Promise((resolve) => {
106
+ this.postMessage({
107
+ command,
108
+ id: this._id,
109
+ data,
110
+ });
111
+
112
+ this._enqueuedOperations.set(this._id++, resolve);
113
+ });
114
+ }
115
+
116
+ get ready() {
117
+ return this.postToDecoder("ready");
118
+ }
119
+
120
+ async free() {
121
+ await this.postToDecoder("free").finally(() => {
122
+ this.terminate();
123
+ });
124
+ }
125
+
126
+ async reset() {
127
+ await this.postToDecoder("reset");
128
+ }
129
+ }
@@ -0,0 +1,67 @@
1
+ This copy of puff has been altered from the original to be inlined into this library.
2
+
3
+ See the original code for puff here: https://github.com/madler/zlib/tree/master/contrib/puff
4
+
5
+ Puff -- A Simple Inflate
6
+ 3 Mar 2003
7
+ Mark Adler
8
+ madler@alumni.caltech.edu
9
+
10
+ What this is --
11
+
12
+ puff.c provides the routine puff() to decompress the deflate data format. It
13
+ does so more slowly than zlib, but the code is about one-fifth the size of the
14
+ inflate code in zlib, and written to be very easy to read.
15
+
16
+ Why I wrote this --
17
+
18
+ puff.c was written to document the deflate format unambiguously, by virtue of
19
+ being working C code. It is meant to supplement RFC 1951, which formally
20
+ describes the deflate format. I have received many questions on details of the
21
+ deflate format, and I hope that reading this code will answer those questions.
22
+ puff.c is heavily commented with details of the deflate format, especially
23
+ those little nooks and cranies of the format that might not be obvious from a
24
+ specification.
25
+
26
+ puff.c may also be useful in applications where code size or memory usage is a
27
+ very limited resource, and speed is not as important.
28
+
29
+ How to use it --
30
+
31
+ Well, most likely you should just be reading puff.c and using zlib for actual
32
+ applications, but if you must ...
33
+
34
+ Include puff.h in your code, which provides this prototype:
35
+
36
+ int puff(unsigned char *dest, /* pointer to destination pointer */
37
+ unsigned long *destlen, /* amount of output space */
38
+ unsigned char *source, /* pointer to source data pointer */
39
+ unsigned long *sourcelen); /* amount of input available */
40
+
41
+ Then you can call puff() to decompress a deflate stream that is in memory in
42
+ its entirety at source, to a sufficiently sized block of memory for the
43
+ decompressed data at dest. puff() is the only external symbol in puff.c The
44
+ only C library functions that puff.c needs are setjmp() and longjmp(), which
45
+ are used to simplify error checking in the code to improve readabilty. puff.c
46
+ does no memory allocation, and uses less than 2K bytes off of the stack.
47
+
48
+ If destlen is not enough space for the uncompressed data, then inflate will
49
+ return an error without writing more than destlen bytes. Note that this means
50
+ that in order to decompress the deflate data successfully, you need to know
51
+ the size of the uncompressed data ahead of time.
52
+
53
+ If needed, puff() can determine the size of the uncompressed data with no
54
+ output space. This is done by passing dest equal to (unsigned char *)0. Then
55
+ the initial value of *destlen is ignored and *destlen is set to the length of
56
+ the uncompressed data. So if the size of the uncompressed data is not known,
57
+ then two passes of puff() can be used--first to determine the size, and second
58
+ to do the actual inflation after allocating the appropriate memory. Not
59
+ pretty, but it works. (This is one of the reasons you should be using zlib.)
60
+
61
+ The deflate format is self-terminating. If the deflate stream does not end
62
+ in *sourcelen bytes, puff() will return an error without reading at or past
63
+ endsource.
64
+
65
+ On return, *sourcelen is updated to the amount of input data consumed, and
66
+ *destlen is updated to the size of the uncompressed data. See the comments
67
+ in puff.c for the possible return codes for puff().
@@ -0,0 +1,31 @@
1
+ import fs from "fs";
2
+ import { dynamicEncode } from "simple-yenc";
3
+
4
+ const puffWasmPath = "src/common/src/puff/Puff.wasm";
5
+ const wasmCommonPath = "src/common/src/WASMAudioDecoderCommon.js";
6
+
7
+ const puffWasm = fs.readFileSync(puffWasmPath);
8
+
9
+ const puffEncoded = dynamicEncode(puffWasm, "`");
10
+
11
+ const wasmCommon = fs.readFileSync(wasmCommonPath).toString();
12
+
13
+ const puffString = wasmCommon.match(/const puffString = String.raw`.*`;/s)[0];
14
+
15
+ const wasmStartIdx = wasmCommon.indexOf(puffString);
16
+ const wasmEndIdx = wasmStartIdx + puffString.length;
17
+
18
+ // Concatenate the strings as buffers to preserve extended ascii
19
+ const wasmCommonWithPuff = Buffer.concat(
20
+ [
21
+ wasmCommon.substring(0, wasmStartIdx),
22
+ "const puffString = String.raw`",
23
+ puffEncoded,
24
+ "`;",
25
+ wasmCommon.substring(wasmEndIdx),
26
+ ].map((string) => Buffer.from(string, { encoding: "binary" })),
27
+ );
28
+
29
+ fs.writeFileSync(wasmCommonPath, wasmCommonWithPuff, { encoding: "binary" });
30
+
31
+ console.log(puffWasm.length);