@awacloud/fw 0.1.0 → 1.0.0

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 (609) hide show
  1. package/CHANGELOG.md +125 -0
  2. package/NOTICE +8 -0
  3. package/README.md +163 -31
  4. package/dist/build/fw.core.pure.meta.json +6 -6
  5. package/dist/build/fw.core.pure.min.js +15 -12
  6. package/dist/build/fw.full.pure.meta.json +13 -7
  7. package/dist/build/fw.full.pure.min.js +45 -42
  8. package/dist/build/fw.minimal.pure.meta.json +6 -6
  9. package/dist/build/fw.minimal.pure.min.js +16 -13
  10. package/dist/build/fw.pack.compress-heavy.pure.meta.json +6 -6
  11. package/dist/build/fw.pack.compress-heavy.pure.min.js +3 -3
  12. package/dist/build/fw.pack.crypto-advanced.pure.meta.json +6 -6
  13. package/dist/build/fw.pack.crypto-advanced.pure.min.js +1 -1
  14. package/dist/build/fw.pack.crypto-basic.pure.meta.json +6 -6
  15. package/dist/build/fw.pack.crypto-basic.pure.min.js +1 -1
  16. package/dist/build/fw.pack.crypto-identity.pure.meta.json +7 -6
  17. package/dist/build/fw.pack.crypto-identity.pure.min.js +4 -4
  18. package/dist/build/fw.pwa.pure.meta.json +7 -6
  19. package/dist/build/fw.pwa.pure.min.js +38 -35
  20. package/dist/build/fw.site-interactive.pure.meta.json +6 -6
  21. package/dist/build/fw.site-interactive.pure.min.js +37 -34
  22. package/dist/build/fw.site.pure.meta.json +6 -6
  23. package/dist/build/fw.site.pure.min.js +24 -21
  24. package/dist/build/fw.spa.pure.meta.json +8 -7
  25. package/dist/build/fw.spa.pure.min.js +41 -38
  26. package/dist/build/sanity-base-classic.min.js +1 -1
  27. package/dist/build/sanity.min.js +1 -1
  28. package/dist/types/core/internal.d.ts +29 -0
  29. package/dist/types/core/internal.d.ts.map +1 -0
  30. package/dist/types/core/logger.d.ts +23 -4
  31. package/dist/types/core/logger.d.ts.map +1 -1
  32. package/dist/types/core/modules.d.ts +374 -61
  33. package/dist/types/core/modules.d.ts.map +1 -1
  34. package/dist/types/core/runtime.d.ts +10 -6
  35. package/dist/types/core/runtime.d.ts.map +1 -1
  36. package/dist/types/core/worker-helper.d.ts +35 -0
  37. package/dist/types/core/worker-helper.d.ts.map +1 -1
  38. package/dist/types/crypto/cipher/aes.d.ts +1 -7
  39. package/dist/types/crypto/cipher/aes.d.ts.map +1 -1
  40. package/dist/types/crypto/cipher/chacha20.d.ts +0 -10
  41. package/dist/types/crypto/cipher/chacha20.d.ts.map +1 -1
  42. package/dist/types/crypto/hash/argon2.d.ts +0 -15
  43. package/dist/types/crypto/hash/argon2.d.ts.map +1 -1
  44. package/dist/types/crypto/hash/blake2b.d.ts.map +1 -1
  45. package/dist/types/crypto/hash/hmac.d.ts.map +1 -1
  46. package/dist/types/crypto/hash/pbkdf2.d.ts.map +1 -1
  47. package/dist/types/crypto/hash/poly1305.d.ts.map +1 -1
  48. package/dist/types/crypto/hash/sha224.d.ts +1 -1
  49. package/dist/types/crypto/hash/sha256.d.ts +4 -13
  50. package/dist/types/crypto/hash/sha256.d.ts.map +1 -1
  51. package/dist/types/crypto/hash/sha3.d.ts.map +1 -1
  52. package/dist/types/crypto/hash/sha384.d.ts +1 -1
  53. package/dist/types/crypto/hash/sha512.d.ts +4 -13
  54. package/dist/types/crypto/hash/sha512.d.ts.map +1 -1
  55. package/dist/types/crypto/mode/cmac.d.ts +0 -6
  56. package/dist/types/crypto/mode/cmac.d.ts.map +1 -1
  57. package/dist/types/crypto/mode/ctr.d.ts +0 -6
  58. package/dist/types/crypto/mode/ctr.d.ts.map +1 -1
  59. package/dist/types/crypto/mode/gcm.d.ts +0 -9
  60. package/dist/types/crypto/mode/gcm.d.ts.map +1 -1
  61. package/dist/types/crypto/pkc/ecc.d.ts +0 -1
  62. package/dist/types/crypto/pkc/ecc.d.ts.map +1 -1
  63. package/dist/types/crypto/pkc/ed25519.d.ts +4 -25
  64. package/dist/types/crypto/pkc/ed25519.d.ts.map +1 -1
  65. package/dist/types/crypto/pkc/hybridSign.d.ts +25 -5
  66. package/dist/types/crypto/pkc/hybridSign.d.ts.map +1 -1
  67. package/dist/types/crypto/pkc/ml_dsa.d.ts +6 -26
  68. package/dist/types/crypto/pkc/ml_dsa.d.ts.map +1 -1
  69. package/dist/types/crypto/pkc/ml_kem.d.ts +0 -43
  70. package/dist/types/crypto/pkc/ml_kem.d.ts.map +1 -1
  71. package/dist/types/crypto/pkc/rsa.d.ts +0 -24
  72. package/dist/types/crypto/pkc/rsa.d.ts.map +1 -1
  73. package/dist/types/crypto/pkc/slh_dsa.d.ts +6 -16
  74. package/dist/types/crypto/pkc/slh_dsa.d.ts.map +1 -1
  75. package/dist/types/crypto/pkc/x25519.d.ts +0 -6
  76. package/dist/types/crypto/pkc/x25519.d.ts.map +1 -1
  77. package/dist/types/crypto/utils/bitArray.d.ts +0 -8
  78. package/dist/types/crypto/utils/bitArray.d.ts.map +1 -1
  79. package/dist/types/crypto/utils/jws.d.ts +6 -14
  80. package/dist/types/crypto/utils/jws.d.ts.map +1 -1
  81. package/dist/types/crypto/utils/keyformat.d.ts +52 -0
  82. package/dist/types/crypto/utils/keyformat.d.ts.map +1 -1
  83. package/dist/types/crypto/utils/random.d.ts +1 -10
  84. package/dist/types/crypto/utils/random.d.ts.map +1 -1
  85. package/dist/types/crypto/utils/rsaKeygen.d.ts +0 -10
  86. package/dist/types/crypto/utils/rsaKeygen.d.ts.map +1 -1
  87. package/dist/types/crypto/utils/uuid.d.ts.map +1 -1
  88. package/dist/types/crypto/utils/x509.d.ts +397 -0
  89. package/dist/types/crypto/utils/x509.d.ts.map +1 -0
  90. package/dist/types/crypto/wasm/argon2.d.ts +2 -1
  91. package/dist/types/crypto/wasm/argon2.d.ts.map +1 -1
  92. package/dist/types/crypto/wasm/pbkdf2.d.ts +2 -2
  93. package/dist/types/crypto/wasm/pbkdf2.d.ts.map +1 -1
  94. package/dist/types/crypto/wasm/rsa.d.ts.map +1 -1
  95. package/dist/types/crypto/wasm/runtime.d.ts +13 -0
  96. package/dist/types/crypto/wasm/runtime.d.ts.map +1 -1
  97. package/dist/types/crypto/webcrypto/aes.d.ts.map +1 -1
  98. package/dist/types/crypto/webcrypto/aeskw.d.ts.map +1 -1
  99. package/dist/types/crypto/webcrypto/digest.d.ts +0 -4
  100. package/dist/types/crypto/webcrypto/digest.d.ts.map +1 -1
  101. package/dist/types/crypto/webcrypto/rsa.d.ts.map +1 -1
  102. package/dist/types/dom/fs/fileIntake.d.ts +59 -0
  103. package/dist/types/dom/fs/fileIntake.d.ts.map +1 -0
  104. package/dist/types/dom/fs/fsAccess.d.ts.map +1 -1
  105. package/dist/types/dom/query/dnd.d.ts.map +1 -1
  106. package/dist/types/dom/query/sound.d.ts +89 -0
  107. package/dist/types/dom/query/sound.d.ts.map +1 -0
  108. package/dist/types/dom/rendering/chart.d.ts.map +1 -1
  109. package/dist/types/dom/rendering/component.d.ts +9 -3
  110. package/dist/types/dom/rendering/component.d.ts.map +1 -1
  111. package/dist/types/dom/rendering/parser.d.ts +39 -0
  112. package/dist/types/dom/rendering/parser.d.ts.map +1 -1
  113. package/dist/types/dom/rendering/render.d.ts.map +1 -1
  114. package/dist/types/dom/rendering/sanitize.d.ts +4 -0
  115. package/dist/types/dom/rendering/sanitize.d.ts.map +1 -1
  116. package/dist/types/dom/rendering/secPolicy.d.ts.map +1 -1
  117. package/dist/types/dom/rendering/styleSheet.d.ts +74 -0
  118. package/dist/types/dom/rendering/styleSheet.d.ts.map +1 -0
  119. package/dist/types/dom/rendering/template.d.ts +9 -3
  120. package/dist/types/dom/rendering/template.d.ts.map +1 -1
  121. package/dist/types/dom/rendering/themeTokens.d.ts +9 -3
  122. package/dist/types/dom/rendering/themeTokens.d.ts.map +1 -1
  123. package/dist/types/dom/rendering/uiSession-core.d.ts +9 -3
  124. package/dist/types/dom/rendering/uiSession-core.d.ts.map +1 -1
  125. package/dist/types/dom/rendering/uiSession-list.d.ts +9 -3
  126. package/dist/types/dom/rendering/uiSession-list.d.ts.map +1 -1
  127. package/dist/types/dom/rendering/uiSession.d.ts +18 -6
  128. package/dist/types/dom/rendering/uiSession.d.ts.map +1 -1
  129. package/dist/types/dom/sw/installPrompt.d.ts +89 -0
  130. package/dist/types/dom/sw/installPrompt.d.ts.map +1 -0
  131. package/dist/types/dom/sw/precache.d.ts +149 -0
  132. package/dist/types/dom/sw/precache.d.ts.map +1 -0
  133. package/dist/types/dom/sw/swUpdate.d.ts +129 -0
  134. package/dist/types/dom/sw/swUpdate.d.ts.map +1 -0
  135. package/dist/types/dom/utils/clipboard.d.ts +8 -0
  136. package/dist/types/dom/utils/clipboard.d.ts.map +1 -1
  137. package/dist/types/dom/utils/keybindings.d.ts.map +1 -1
  138. package/dist/types/dom/utils/route.d.ts.map +1 -1
  139. package/dist/types/dom/utils/ua.d.ts +2 -2
  140. package/dist/types/dom/utils/ua.d.ts.map +1 -1
  141. package/dist/types/io/calc/crc32.d.ts.map +1 -1
  142. package/dist/types/io/codec/b64.d.ts +24 -0
  143. package/dist/types/io/codec/b64.d.ts.map +1 -1
  144. package/dist/types/io/codec/cbor.d.ts +1 -0
  145. package/dist/types/io/codec/cbor.d.ts.map +1 -1
  146. package/dist/types/io/codec/hex.d.ts.map +1 -1
  147. package/dist/types/io/codec/msgpack.d.ts +1 -0
  148. package/dist/types/io/codec/msgpack.d.ts.map +1 -1
  149. package/dist/types/io/codec/url.d.ts +3 -1
  150. package/dist/types/io/codec/url.d.ts.map +1 -1
  151. package/dist/types/io/codec/utf8.d.ts.map +1 -1
  152. package/dist/types/io/compress/bitstream.d.ts +1 -1
  153. package/dist/types/io/compress/brotli.d.ts +0 -1
  154. package/dist/types/io/compress/brotli.d.ts.map +1 -1
  155. package/dist/types/io/compress/brotli_shared.d.ts +0 -4
  156. package/dist/types/io/compress/brotli_shared.d.ts.map +1 -1
  157. package/dist/types/io/compress/deflate.d.ts +14 -1
  158. package/dist/types/io/compress/deflate.d.ts.map +1 -1
  159. package/dist/types/io/compress/gzip.d.ts +4 -0
  160. package/dist/types/io/compress/gzip.d.ts.map +1 -1
  161. package/dist/types/io/compress/huffman.d.ts +7 -0
  162. package/dist/types/io/compress/huffman.d.ts.map +1 -1
  163. package/dist/types/io/compress/lz4.d.ts +4 -2
  164. package/dist/types/io/compress/lz4.d.ts.map +1 -1
  165. package/dist/types/io/compress/lz77.d.ts +17 -0
  166. package/dist/types/io/compress/lz77.d.ts.map +1 -1
  167. package/dist/types/io/compress/lzw.d.ts +2 -0
  168. package/dist/types/io/compress/lzw.d.ts.map +1 -1
  169. package/dist/types/io/compress/zip.d.ts.map +1 -1
  170. package/dist/types/io/compress/zlib.d.ts +9 -2
  171. package/dist/types/io/compress/zlib.d.ts.map +1 -1
  172. package/dist/types/io/math/linalg.d.ts.map +1 -1
  173. package/dist/types/process/rpc.d.ts.map +1 -1
  174. package/dist/types/process/workerPool.d.ts.map +1 -1
  175. package/dist/types/sanity/base.d.ts +12 -2
  176. package/dist/types/sanity/base.d.ts.map +1 -1
  177. package/dist/types/sanity/lockdown.d.ts +6 -5
  178. package/dist/types/sanity/lockdown.d.ts.map +1 -1
  179. package/dist/types/sanity/wasm-gate.d.ts +1 -1
  180. package/docs/README.md +17 -10
  181. package/docs/api/README.md +5 -5
  182. package/docs/api/core/logger.md +2 -1
  183. package/docs/api/core/modules.md +3 -3
  184. package/docs/api/core/runtime.md +9 -9
  185. package/docs/api/core/worker-helper.md +12 -2
  186. package/docs/api/crypto/README.md +1 -1
  187. package/docs/api/crypto/cipher/README.md +2 -2
  188. package/docs/api/crypto/cipher/aes.md +5 -10
  189. package/docs/api/crypto/cipher/chacha20.md +3 -8
  190. package/docs/api/crypto/hash/README.md +5 -5
  191. package/docs/api/crypto/hash/adf.md +21 -11
  192. package/docs/api/crypto/hash/argon2.md +3 -12
  193. package/docs/api/crypto/hash/blake2b.md +12 -14
  194. package/docs/api/crypto/hash/pbkdf2.md +1 -1
  195. package/docs/api/crypto/hash/sha224.md +2 -2
  196. package/docs/api/crypto/hash/sha256.md +4 -4
  197. package/docs/api/crypto/hash/sha3.md +2 -2
  198. package/docs/api/crypto/hash/sha384.md +1 -1
  199. package/docs/api/crypto/hash/sha512.md +5 -5
  200. package/docs/api/crypto/hash/sha512_224.md +1 -1
  201. package/docs/api/crypto/hash/sha512_256.md +1 -1
  202. package/docs/api/crypto/mode/README.md +3 -3
  203. package/docs/api/crypto/mode/cbc.md +2 -2
  204. package/docs/api/crypto/mode/chacha20poly1305.md +1 -1
  205. package/docs/api/crypto/mode/cmac.md +4 -4
  206. package/docs/api/crypto/mode/ctr.md +2 -3
  207. package/docs/api/crypto/mode/gcm.md +5 -5
  208. package/docs/api/crypto/mode/kw.md +4 -4
  209. package/docs/api/crypto/pkc/README.md +8 -8
  210. package/docs/api/crypto/pkc/ecc.md +19 -17
  211. package/docs/api/crypto/pkc/ed25519.md +10 -15
  212. package/docs/api/crypto/pkc/hybridKem.md +8 -2
  213. package/docs/api/crypto/pkc/hybridSign.md +34 -7
  214. package/docs/api/crypto/pkc/ml_dsa.md +20 -19
  215. package/docs/api/crypto/pkc/ml_kem.md +3 -3
  216. package/docs/api/crypto/pkc/rsa.md +7 -9
  217. package/docs/api/crypto/pkc/slh_dsa.md +12 -12
  218. package/docs/api/crypto/pkc/x25519.md +5 -4
  219. package/docs/api/crypto/utils/README.md +7 -7
  220. package/docs/api/crypto/utils/aes_modes.md +2 -5
  221. package/docs/api/crypto/utils/asn1.md +25 -16
  222. package/docs/api/crypto/utils/jws.md +6 -4
  223. package/docs/api/crypto/utils/keyformat.md +24 -15
  224. package/docs/api/crypto/utils/pad.md +1 -1
  225. package/docs/api/crypto/utils/pem.md +3 -3
  226. package/docs/api/crypto/utils/random.md +3 -4
  227. package/docs/api/crypto/utils/rsaKeygen.md +2 -2
  228. package/docs/api/crypto/utils/totp.md +2 -2
  229. package/docs/api/crypto/utils/uuid.md +5 -5
  230. package/docs/api/crypto/utils/x509.md +124 -0
  231. package/docs/api/crypto/wasm/aes.md +1 -1
  232. package/docs/api/crypto/wasm/argon2.md +6 -4
  233. package/docs/api/crypto/wasm/ml_kem.md +1 -0
  234. package/docs/api/crypto/wasm/pbkdf2.md +8 -8
  235. package/docs/api/crypto/wasm/rsa.md +1 -1
  236. package/docs/api/crypto/wasm/runtime.md +27 -10
  237. package/docs/api/crypto/webcrypto/aes.md +1 -1
  238. package/docs/api/crypto/webcrypto/aeskw.md +2 -2
  239. package/docs/api/crypto/webcrypto/digest.md +3 -5
  240. package/docs/api/crypto/webcrypto/ecc.md +1 -1
  241. package/docs/api/crypto/webcrypto/ed25519.md +1 -1
  242. package/docs/api/crypto/webcrypto/rsa.md +3 -2
  243. package/docs/api/crypto/webcrypto/x25519.md +1 -1
  244. package/docs/api/dom/README.md +6 -1
  245. package/docs/api/dom/fs/README.md +1 -0
  246. package/docs/api/dom/fs/fileIntake.md +120 -0
  247. package/docs/api/dom/fs/fsAccess.md +1 -0
  248. package/docs/api/dom/fs/indexedDB.md +1 -1
  249. package/docs/api/dom/fs/storage.md +1 -1
  250. package/docs/api/dom/query/README.md +2 -1
  251. package/docs/api/dom/query/dnd.md +4 -1
  252. package/docs/api/dom/query/dom.md +5 -0
  253. package/docs/api/dom/query/sound.md +103 -0
  254. package/docs/api/dom/rendering/README.md +4 -3
  255. package/docs/api/dom/rendering/chart.md +5 -4
  256. package/docs/api/dom/rendering/component.md +25 -3
  257. package/docs/api/dom/rendering/parser.md +5 -2
  258. package/docs/api/dom/rendering/reactiveBind.md +1 -1
  259. package/docs/api/dom/rendering/render.md +2 -2
  260. package/docs/api/dom/rendering/sanitize.md +10 -2
  261. package/docs/api/dom/rendering/secPolicy.md +4 -4
  262. package/docs/api/dom/rendering/styleSheet.md +85 -0
  263. package/docs/api/dom/rendering/template.md +20 -5
  264. package/docs/api/dom/rendering/themeTokens.md +6 -3
  265. package/docs/api/dom/rendering/uiSession.md +18 -5
  266. package/docs/api/dom/rendering/virtualScroll.md +2 -2
  267. package/docs/api/dom/sensors/geolocation.md +1 -1
  268. package/docs/api/dom/sw/README.md +7 -1
  269. package/docs/api/dom/sw/cache.md +2 -1
  270. package/docs/api/dom/sw/installPrompt.md +111 -0
  271. package/docs/api/dom/sw/precache.md +124 -0
  272. package/docs/api/dom/sw/serviceWorker.md +4 -2
  273. package/docs/api/dom/sw/swUpdate.md +112 -0
  274. package/docs/api/dom/utils/clipboard.md +56 -5
  275. package/docs/api/dom/utils/entropyCollector.md +1 -1
  276. package/docs/api/dom/utils/keybindings.md +17 -2
  277. package/docs/api/dom/utils/leaderElection.md +2 -2
  278. package/docs/api/dom/utils/route.md +1 -0
  279. package/docs/api/dom/utils/ua.md +15 -10
  280. package/docs/api/io/calc/crc32.md +4 -1
  281. package/docs/api/io/codec/README.md +2 -2
  282. package/docs/api/io/codec/b64.md +31 -2
  283. package/docs/api/io/codec/buffer.md +1 -1
  284. package/docs/api/io/codec/cbor.md +6 -4
  285. package/docs/api/io/codec/csv.md +178 -178
  286. package/docs/api/io/codec/hex.md +10 -2
  287. package/docs/api/io/codec/msgpack.md +4 -3
  288. package/docs/api/io/codec/url.md +4 -3
  289. package/docs/api/io/codec/utf8.md +3 -3
  290. package/docs/api/io/compress/bitstream.md +6 -3
  291. package/docs/api/io/compress/brotli.md +36 -4
  292. package/docs/api/io/compress/brotli_shared.md +1 -1
  293. package/docs/api/io/compress/deflate.md +42 -34
  294. package/docs/api/io/compress/gzip.md +26 -6
  295. package/docs/api/io/compress/huffman.md +2 -1
  296. package/docs/api/io/compress/lz4.md +5 -3
  297. package/docs/api/io/compress/lz77.md +26 -3
  298. package/docs/api/io/compress/lzw.md +13 -8
  299. package/docs/api/io/compress/zip.md +36 -10
  300. package/docs/api/io/compress/zlib.md +21 -5
  301. package/docs/api/io/math/linalg.md +1 -1
  302. package/docs/api/io/utils/valid.md +1 -1
  303. package/docs/api/process/rpc.md +3 -2
  304. package/docs/api/process/workerPool.md +1 -1
  305. package/docs/api/sanity/base.md +9 -5
  306. package/docs/api/sanity/lockdown.md +7 -6
  307. package/docs/api/surface.json +12311 -0
  308. package/docs/api/typed.md +5 -3
  309. package/docs/dev/README.md +2 -0
  310. package/docs/dev/bench.md +76 -0
  311. package/docs/dev/crypto-references.md +54 -0
  312. package/docs/dev/provenance.md +30 -7
  313. package/docs/dev/publishing.md +7 -7
  314. package/docs/dev/runtime.md +1 -1
  315. package/docs/dev/testing.md +13 -0
  316. package/docs/evidence/l4-evidence.md +86 -0
  317. package/docs/evidence/pqc-hybrid-evidence.md +7 -6
  318. package/docs/guide/README.md +1 -0
  319. package/docs/guide/_QUICK_REF.md +5 -5
  320. package/docs/guide/doc-format.md +4 -3
  321. package/docs/guide/integrations-hmr.md +1 -1
  322. package/docs/guide/module-creation-workflow.md +8 -8
  323. package/docs/guide/rendering-pipeline.md +11 -4
  324. package/docs/guide/security.md +7 -5
  325. package/docs/guide/stability.md +123 -0
  326. package/docs/guide/tutorial-todo.md +20 -8
  327. package/docs/guide/typescript.md +9 -0
  328. package/docs/guide/worker-confinement.md +30 -17
  329. package/docs/notes/lz77-deflate-factorization.md +3 -3
  330. package/docs/tools/README.md +1 -1
  331. package/docs/tools/bundler.md +3 -3
  332. package/docs/tools/codegen.md +1 -1
  333. package/docs/tools/standalone.md +0 -1
  334. package/fw.config.json +2 -2
  335. package/integrations/_shared/catalog.generated.json +7 -1
  336. package/integrations/_shared/core.js +1 -1
  337. package/integrations/astro/index.js +1 -1
  338. package/integrations/eslint/README.md +7 -1
  339. package/integrations/nextjs/index.js +1 -1
  340. package/integrations/nodejs/README.md +3 -3
  341. package/integrations/turbopack/index.js +2 -2
  342. package/integrations/typedoc/categorize.js +1 -1
  343. package/integrations/typedoc/typedoc.json +2 -1
  344. package/integrations/vite/index.js +2 -2
  345. package/integrations/vite/vite-env.d.ts +7 -4
  346. package/integrations/webpack/index.js +3 -3
  347. package/package.json +76 -10
  348. package/src/core/internal.js +31 -0
  349. package/src/core/logger.js +95 -23
  350. package/src/core/modules.js +17 -8
  351. package/src/core/readyState.js +1 -1
  352. package/src/core/runtime.js +74 -27
  353. package/src/core/worker-helper.js +83 -4
  354. package/src/crypto/NIST_CONFORMANCE.md +47 -34
  355. package/src/crypto/cipher/aes.acvp.md +7 -8
  356. package/src/crypto/cipher/aes.js +280 -229
  357. package/src/crypto/cipher/chacha20.acvp.md +6 -7
  358. package/src/crypto/cipher/chacha20.js +91 -61
  359. package/src/crypto/hash/adf.js +1 -1
  360. package/src/crypto/hash/argon2.acvp.md +11 -15
  361. package/src/crypto/hash/argon2.js +9 -24
  362. package/src/crypto/hash/blake2b.js +157 -125
  363. package/src/crypto/hash/hkdf.acvp.md +15 -15
  364. package/src/crypto/hash/hkdf.js +1 -1
  365. package/src/crypto/hash/hmac.acvp.md +10 -12
  366. package/src/crypto/hash/hmac.js +5 -1
  367. package/src/crypto/hash/pbkdf2.acvp.md +13 -13
  368. package/src/crypto/hash/pbkdf2.js +36 -8
  369. package/src/crypto/hash/poly1305.acvp.md +10 -6
  370. package/src/crypto/hash/poly1305.js +171 -50
  371. package/src/crypto/hash/sha224.acvp.md +3 -3
  372. package/src/crypto/hash/sha224.js +4 -4
  373. package/src/crypto/hash/sha256.acvp.md +2 -2
  374. package/src/crypto/hash/sha256.js +514 -50
  375. package/src/crypto/hash/sha3.acvp.md +9 -9
  376. package/src/crypto/hash/sha3.js +127 -71
  377. package/src/crypto/hash/sha384.acvp.md +3 -3
  378. package/src/crypto/hash/sha384.js +4 -4
  379. package/src/crypto/hash/sha512.acvp.md +7 -8
  380. package/src/crypto/hash/sha512.js +107 -108
  381. package/src/crypto/hash/sha512_224.acvp.md +6 -6
  382. package/src/crypto/hash/sha512_224.js +2 -2
  383. package/src/crypto/hash/sha512_256.acvp.md +6 -6
  384. package/src/crypto/hash/sha512_256.js +2 -2
  385. package/src/crypto/mode/cbc.acvp.md +2 -2
  386. package/src/crypto/mode/cbc.js +1 -1
  387. package/src/crypto/mode/chacha20poly1305.acvp.md +6 -6
  388. package/src/crypto/mode/chacha20poly1305.js +3 -3
  389. package/src/crypto/mode/cmac.acvp.md +5 -5
  390. package/src/crypto/mode/cmac.js +8 -8
  391. package/src/crypto/mode/ctr.acvp.md +2 -2
  392. package/src/crypto/mode/ctr.js +1 -3
  393. package/src/crypto/mode/gcm.acvp.md +6 -6
  394. package/src/crypto/mode/gcm.js +7 -7
  395. package/src/crypto/mode/kw.acvp.md +4 -4
  396. package/src/crypto/mode/kw.js +5 -5
  397. package/src/crypto/pkc/ecc.acvp.md +13 -13
  398. package/src/crypto/pkc/ecc.js +9 -11
  399. package/src/crypto/pkc/ed25519.acvp.md +30 -34
  400. package/src/crypto/pkc/ed25519.js +453 -94
  401. package/src/crypto/pkc/hybrid-kat.provenance.md +4 -4
  402. package/src/crypto/pkc/hybridSign.js +103 -24
  403. package/src/crypto/pkc/ml_dsa.acvp.md +18 -18
  404. package/src/crypto/pkc/ml_dsa.js +29 -32
  405. package/src/crypto/pkc/ml_kem.acvp.md +7 -7
  406. package/src/crypto/pkc/ml_kem.js +15 -21
  407. package/src/crypto/pkc/rsa.acvp.md +11 -15
  408. package/src/crypto/pkc/rsa.js +7 -44
  409. package/src/crypto/pkc/slh_dsa.acvp.md +16 -16
  410. package/src/crypto/pkc/slh_dsa.js +16 -18
  411. package/src/crypto/pkc/x25519.acvp.md +13 -10
  412. package/src/crypto/pkc/x25519.js +273 -29
  413. package/src/crypto/utils/aes_modes.acvp.md +1 -2
  414. package/src/crypto/utils/aes_modes.js +2 -2
  415. package/src/crypto/utils/asn1-oid.js +1 -1
  416. package/src/crypto/utils/asn1.js +1 -1
  417. package/src/crypto/utils/bitArray.js +33 -48
  418. package/src/crypto/utils/bn.js +1 -1
  419. package/src/crypto/utils/jws.js +37 -41
  420. package/src/crypto/utils/keyformat.js +181 -2
  421. package/src/crypto/utils/pem.js +1 -1
  422. package/src/crypto/utils/random.acvp.md +5 -5
  423. package/src/crypto/utils/random.js +845 -846
  424. package/src/crypto/utils/rsaKeygen.acvp.md +3 -2
  425. package/src/crypto/utils/rsaKeygen.js +6 -6
  426. package/src/crypto/utils/totp.js +1 -1
  427. package/src/crypto/utils/uuid.acvp.md +5 -5
  428. package/src/crypto/utils/uuid.js +11 -10
  429. package/src/crypto/utils/x509.js +591 -0
  430. package/src/crypto/wasm/aes.js +1 -1
  431. package/src/crypto/wasm/aes.scalar.wasm +0 -0
  432. package/src/crypto/wasm/argon2.js +17 -1
  433. package/src/crypto/wasm/argon2.scalar.wasm +0 -0
  434. package/src/crypto/wasm/blake2b.scalar.wasm +0 -0
  435. package/src/crypto/wasm/blake2b.simd.wasm +0 -0
  436. package/src/crypto/wasm/chacha20poly1305.scalar.wasm +0 -0
  437. package/src/crypto/wasm/chacha20poly1305.simd.wasm +0 -0
  438. package/src/crypto/wasm/cmac.scalar.wasm +0 -0
  439. package/src/crypto/wasm/ecc.js +3 -3
  440. package/src/crypto/wasm/ecc.scalar.wasm +0 -0
  441. package/src/crypto/wasm/ed25519.js +1 -1
  442. package/src/crypto/wasm/ed25519.scalar.wasm +0 -0
  443. package/src/crypto/wasm/hkdf.scalar.wasm +0 -0
  444. package/src/crypto/wasm/hmac.scalar.wasm +0 -0
  445. package/src/crypto/wasm/ml_dsa.js +2 -2
  446. package/src/crypto/wasm/ml_dsa.scalar.wasm +0 -0
  447. package/src/crypto/wasm/ml_dsa.simd.wasm +0 -0
  448. package/src/crypto/wasm/ml_kem.js +2 -2
  449. package/src/crypto/wasm/ml_kem.scalar.wasm +0 -0
  450. package/src/crypto/wasm/ml_kem.simd.wasm +0 -0
  451. package/src/crypto/wasm/pbkdf2.js +8 -6
  452. package/src/crypto/wasm/pbkdf2.scalar.wasm +0 -0
  453. package/src/crypto/wasm/rsa.js +6 -5
  454. package/src/crypto/wasm/rsa.scalar.wasm +0 -0
  455. package/src/crypto/wasm/runtime.js +84 -17
  456. package/src/crypto/wasm/sha2.scalar.wasm +0 -0
  457. package/src/crypto/wasm/sha3.scalar.wasm +0 -0
  458. package/src/crypto/wasm/sha3.simd.wasm +0 -0
  459. package/src/crypto/wasm/slh_dsa.js +1 -1
  460. package/src/crypto/wasm/slh_dsa.scalar.wasm +0 -0
  461. package/src/crypto/wasm/x25519.js +1 -1
  462. package/src/crypto/wasm/x25519.scalar.wasm +0 -0
  463. package/src/crypto/webcrypto/aes.js +8 -2
  464. package/src/crypto/webcrypto/aeskw.js +7 -2
  465. package/src/crypto/webcrypto/digest.js +1 -15
  466. package/src/crypto/webcrypto/ecc.js +2 -2
  467. package/src/crypto/webcrypto/ed25519.js +2 -2
  468. package/src/crypto/webcrypto/hmac.js +2 -2
  469. package/src/crypto/webcrypto/rsa.js +7 -2
  470. package/src/crypto/webcrypto/x25519.js +2 -2
  471. package/src/dom/display/animate.js +1 -1
  472. package/src/dom/display/fullscreen.js +1 -1
  473. package/src/dom/fs/download.js +1 -1
  474. package/src/dom/fs/fileIntake.js +192 -0
  475. package/src/dom/fs/fsAccess.js +2 -3
  476. package/src/dom/fs/indexedDB.js +1 -1
  477. package/src/dom/fs/remoteStore.js +1 -1
  478. package/src/dom/fs/storage.js +1 -1
  479. package/src/dom/lifecycle/idle.js +1 -1
  480. package/src/dom/lifecycle/visibility.js +1 -1
  481. package/src/dom/lifecycle/wakeLock.js +1 -1
  482. package/src/dom/net/ajax.js +1 -1
  483. package/src/dom/net/network.js +1 -1
  484. package/src/dom/net/sse.js +1 -1
  485. package/src/dom/net/webrtc.js +1 -1
  486. package/src/dom/net/ws.js +1 -1
  487. package/src/dom/query/dnd.js +56 -4
  488. package/src/dom/query/dom.js +1 -1
  489. package/src/dom/query/events.js +1 -1
  490. package/src/dom/query/gesture.js +1 -1
  491. package/src/dom/query/media.js +1 -1
  492. package/src/dom/query/sound.js +333 -0
  493. package/src/dom/rendering/chart.js +736 -663
  494. package/src/dom/rendering/component.js +25 -14
  495. package/src/dom/rendering/devtools.js +2 -2
  496. package/src/dom/rendering/parser.js +155 -12
  497. package/src/dom/rendering/render.js +14 -4
  498. package/src/dom/rendering/sanitize.js +11 -4
  499. package/src/dom/rendering/secPolicy.js +6 -4
  500. package/src/dom/rendering/styleSheet.js +217 -0
  501. package/src/dom/rendering/template.js +72 -13
  502. package/src/dom/rendering/themeTokens.js +542 -522
  503. package/src/dom/rendering/uiSession-core.js +51 -6
  504. package/src/dom/rendering/uiSession-direct.js +1 -1
  505. package/src/dom/rendering/uiSession-list.js +4 -4
  506. package/src/dom/rendering/uiSession.js +1 -1
  507. package/src/dom/rendering/virtualScroll.js +341 -341
  508. package/src/dom/sensors/battery.js +1 -1
  509. package/src/dom/sensors/geolocation.js +1 -1
  510. package/src/dom/sensors/networkInfo.js +1 -1
  511. package/src/dom/sensors/sensors.js +1 -1
  512. package/src/dom/sw/backgroundSync.js +1 -1
  513. package/src/dom/sw/cache.js +1 -1
  514. package/src/dom/sw/installPrompt.js +151 -0
  515. package/src/dom/sw/precache.js +257 -0
  516. package/src/dom/sw/push.js +1 -1
  517. package/src/dom/sw/sharedWorker.js +1 -1
  518. package/src/dom/sw/swUpdate.js +239 -0
  519. package/src/dom/utils/a11y.js +1 -1
  520. package/src/dom/utils/clipboard.js +56 -6
  521. package/src/dom/utils/entropyCollector.js +1 -1
  522. package/src/dom/utils/focus.js +1 -1
  523. package/src/dom/utils/form.js +1 -1
  524. package/src/dom/utils/keybindings.js +600 -574
  525. package/src/dom/utils/leaderElection.js +1 -1
  526. package/src/dom/utils/notifications.js +1 -1
  527. package/src/dom/utils/permissions.js +107 -107
  528. package/src/dom/utils/route.js +14 -2
  529. package/src/dom/utils/ua.js +42 -28
  530. package/src/dom/utils/webauthn.js +1 -1
  531. package/src/io/binary/reader.js +1 -1
  532. package/src/io/binary/writer.js +1 -1
  533. package/src/io/calc/bigint.js +1 -1
  534. package/src/io/calc/crc32.js +28 -2
  535. package/src/io/calc/easing.js +514 -514
  536. package/src/io/codec/b64.js +189 -52
  537. package/src/io/codec/base32.js +1 -1
  538. package/src/io/codec/base58.js +1 -1
  539. package/src/io/codec/buffer.js +1 -1
  540. package/src/io/codec/cbor.js +111 -18
  541. package/src/io/codec/csv.js +1 -1
  542. package/src/io/codec/hex.js +51 -6
  543. package/src/io/codec/mime.js +1 -1
  544. package/src/io/codec/msgpack.js +47 -6
  545. package/src/io/codec/url.js +24 -7
  546. package/src/io/codec/utf8.js +5 -3
  547. package/src/io/codec/xml.js +1 -1
  548. package/src/io/compress/bitstream.js +2 -2
  549. package/src/io/compress/brotli.js +663 -163
  550. package/src/io/compress/brotli_dict.js +1 -1
  551. package/src/io/compress/brotli_dict_words.js +1 -1
  552. package/src/io/compress/brotli_frame.js +1 -1
  553. package/src/io/compress/brotli_shared.js +7 -7
  554. package/src/io/compress/deflate.js +87 -53
  555. package/src/io/compress/gzip.js +187 -74
  556. package/src/io/compress/huffman.js +24 -1
  557. package/src/io/compress/lz4.js +45 -7
  558. package/src/io/compress/lz77.js +148 -63
  559. package/src/io/compress/lzw.js +35 -19
  560. package/src/io/compress/zip.js +862 -815
  561. package/src/io/compress/zlib.js +87 -7
  562. package/src/io/i18n/i18n.js +1 -1
  563. package/src/io/math/fixed-point.js +1 -1
  564. package/src/io/math/geom.js +1 -1
  565. package/src/io/math/interp.js +1 -1
  566. package/src/io/math/linalg.js +10 -4
  567. package/src/io/math/stats.js +1 -1
  568. package/src/io/structures/btree.js +1 -1
  569. package/src/io/structures/heap.js +1 -1
  570. package/src/io/structures/lruCache.js +1 -1
  571. package/src/io/structures/ringBuffer.js +1 -1
  572. package/src/io/structures/tree-walker.js +1 -1
  573. package/src/io/structures/trie.js +1 -1
  574. package/src/io/sync/abort.js +1 -1
  575. package/src/io/sync/atomics.js +1 -1
  576. package/src/io/sync/cancellable.js +1 -1
  577. package/src/io/sync/channel.js +1 -1
  578. package/src/io/sync/mutex.js +1 -1
  579. package/src/io/sync/semaphore.js +1 -1
  580. package/src/io/sync/tokenBucket.js +1 -1
  581. package/src/io/text/ansi.js +1 -1
  582. package/src/io/text/html-entities.js +1 -1
  583. package/src/io/time/date.js +1 -1
  584. package/src/io/timing/clock.js +1 -1
  585. package/src/io/timing/rateLimit.js +1 -1
  586. package/src/io/timing/scheduler.js +1 -1
  587. package/src/io/utils/errors.js +1 -1
  588. package/src/io/utils/eventBus.js +1 -1
  589. package/src/io/utils/signal.js +1 -1
  590. package/src/io/utils/valid.js +1 -1
  591. package/src/main.js +1 -1
  592. package/src/process/message.js +1 -1
  593. package/src/process/rpc.js +7 -3
  594. package/src/process/workerPool.js +11 -4
  595. package/src/sanity/base.js +85 -39
  596. package/src/sanity/lockdown-apply.js +3 -3
  597. package/src/sanity/lockdown.js +58 -32
  598. package/src/sanity/wasm-gate.js +2 -2
  599. package/src/typed.js +7 -3
  600. package/third-party/NOTICE-brotli-dictionary.txt +69 -0
  601. package/third-party/NOTICE-fw-wasm-crypto.txt +576 -0
  602. package/third-party/NOTICE-whatwg-entities.txt +44 -0
  603. package/types/registry.generated.d.ts +7 -1
  604. package/types/typed.d.ts +7 -3
  605. package/dist/types/crypto/utils/aes_ctr.d.ts +0 -84
  606. package/dist/types/crypto/utils/aes_ctr.d.ts.map +0 -1
  607. package/docs/api/crypto/utils/aes_ctr.md +0 -71
  608. package/src/crypto/utils/aes_ctr.acvp.md +0 -67
  609. package/src/crypto/utils/aes_ctr.js +0 -134
@@ -1,663 +1,736 @@
1
- // Copyright (c) 2026 AwaCloud SAS
2
- // Author: Matthieu Bouilloux
3
- // SPDX-License-Identifier: Apache-2.0
4
-
5
- /**
6
- * @description
7
- * Minimal Canvas 2D charts for internal observability (taskmgr, metrics).
8
- * Provides the chart types: `line`, `area`, `bar`, `sparkline`.
9
- *
10
- * Out of MVP scope: pie/donut, log axes, interactive annotations, complex legends,
11
- * tooltips, PNG export. For those needs, use an external library.
12
- *
13
- * Worker-safe: no (Canvas 2D is main-thread only; OffscreenCanvas is out of MVP scope).
14
- *
15
- * @example
16
- * const chart = runtime.resolve('chart');
17
- * const handle = chart.line({ el: document.querySelector('canvas'), data: [1, 2, 3, 4] });
18
- * handle.update([2, 4, 3, 5]);
19
- * handle.dispose();
20
- */
21
- import { dom } from '../query/dom.js';
22
- import { animate } from '../display/animate.js';
23
- import { stats } from '../../io/math/stats.js';
24
- import { linalg } from '../../io/math/linalg.js';
25
-
26
- /**
27
- * A live chart handle returned by each chart constructor.
28
- * @typedef {object} ChartHandle
29
- * @property {(newData: number[]|{x:number,y:number}[]) => void} update - Replace the data set and redraw.
30
- * @property {() => void} resize - Force a dimension recompute and redraw.
31
- * @property {(newOpts: Object) => void} setOpts - Merge new options (and optionally data) then redraw.
32
- * @property {() => void} dispose - Detach observers and release resources.
33
- */
34
-
35
- /**
36
- * Canvas 2D chart surface returned by `factory()`.
37
- * @typedef {object} ChartAPI
38
- * @property {(opts: {el: HTMLElement|HTMLCanvasElement, data?: number[]|{x:number,y:number}[], xAxis?: Object, yAxis?: Object, color?: string, smoothing?: 'none'|'monotone', animate?: boolean|number}) => ChartHandle} line - Create a line chart.
39
- * @property {(opts: {el: HTMLElement|HTMLCanvasElement, data?: number[]|{x:number,y:number}[], xAxis?: Object, yAxis?: Object, color?: string, smoothing?: 'none'|'monotone', animate?: boolean|number}) => ChartHandle} area - Create an area chart (line + fill).
40
- * @property {(opts: {el: HTMLElement|HTMLCanvasElement, data?: number[]|{x:number,y:number}[], xAxis?: Object, yAxis?: Object, color?: string, animate?: boolean|number}) => ChartHandle} bar - Create a vertical bar chart.
41
- * @property {(opts: {el: HTMLElement|HTMLCanvasElement, data?: number[]|{x:number,y:number}[], color?: string, smoothing?: 'none'|'monotone', animate?: boolean|number}) => ChartHandle} sparkline - Create a minimal axis-less sparkline.
42
- */
43
-
44
- export const chart = {
45
- name: 'chart',
46
- version: '1.0.0',
47
- type: 'fw.dom.rendering',
48
- dependencies: ['dom', 'animate', 'stats', 'linalg'],
49
- deps: [dom, animate, stats, linalg],
50
-
51
- /**
52
- * @param {Object} dom - fw dom module.
53
- * @param {Object} animate - fw animate module.
54
- * @param {Object} stats - fw stats module.
55
- * @param {Object} linalg - fw linalg module.
56
- * @returns {ChartAPI}
57
- */
58
- factory(dom, animate, stats, linalg) {
59
-
60
- // ─────────────────────────────────────────────────────────────────
61
- // Internal helpers
62
- // ─────────────────────────────────────────────────────────────────
63
-
64
- /**
65
- * Normalise `data` to a `{x, y}[]` array.
66
- * Accepts `number[]` (x = index) or `{x, y}[]`.
67
- *
68
- * @param {number[]|{x:number,y:number}[]} data
69
- * @returns {{x:number,y:number}[]}
70
- */
71
- function _normalizeData(data) {
72
- if (!Array.isArray(data) || data.length === 0) return [];
73
- if (typeof data[0] === 'number') {
74
- return data.map((y, i) => ({ x: i, y }));
75
- }
76
- return data.map(p => ({ x: Number(p.x), y: Number(p.y) }));
77
- }
78
-
79
- /**
80
- * Compute the [min, max] domain along one dimension.
81
- * Returns `[0, 1]` when the domain is degenerate (flat data).
82
- *
83
- * @param {number[]} values
84
- * @returns {[number, number]}
85
- */
86
- function _domain(values) {
87
- if (!values.length) return [0, 1];
88
- const mn = stats.min(values);
89
- const mx = stats.max(values);
90
- if (mn === mx) return [mn - 1, mx + 1];
91
- return [mn, mx];
92
- }
93
-
94
- /**
95
- * Compute the domain with proportional padding (5 %).
96
- *
97
- * @param {number[]} values
98
- * @param {[number, number]|undefined} explicit
99
- * @returns {[number, number]}
100
- */
101
- function _fitDomain(values, explicit) {
102
- if (explicit && explicit.length === 2) return [explicit[0], explicit[1]];
103
- const [mn, mx] = _domain(values);
104
- const pad = (mx - mn) * 0.05;
105
- return [mn - pad, mx + pad];
106
- }
107
-
108
- /**
109
- * Generate readable ticks for a domain.
110
- *
111
- * @param {number} min
112
- * @param {number} max
113
- * @param {number} [count=5]
114
- * @returns {number[]}
115
- */
116
- function _ticks(min, max, count = 5) {
117
- const step = (max - min) / (count - 1 || 1);
118
- const out = [];
119
- for (let i = 0; i < count; i++) out.push(min + step * i);
120
- return out;
121
- }
122
-
123
- /**
124
- * Map a value from a domain to a pixel coordinate in [0, size].
125
- *
126
- * @param {number} v
127
- * @param {number} min
128
- * @param {number} max
129
- * @param {number} size
130
- * @returns {number}
131
- */
132
- function _scale(v, min, max, size) {
133
- if (max === min) return size / 2;
134
- return ((v - min) / (max - min)) * size;
135
- }
136
-
137
- /**
138
- * Compute a Fritsch-Carlson monotone tangent (single point).
139
- * Returns the slope for point i in the sorted {x,y}[] array.
140
- *
141
- * @param {{x:number,y:number}[]} pts
142
- * @param {number} i
143
- * @returns {number}
144
- */
145
- function _monotoneSlope(pts, i) {
146
- const n = pts.length;
147
- if (n < 2) return 0;
148
- if (i === 0) return (pts[1].y - pts[0].y) / (pts[1].x - pts[0].x || 1);
149
- if (i === n - 1) return (pts[n - 1].y - pts[n - 2].y) / (pts[n - 1].x - pts[n - 2].x || 1);
150
- const d0 = (pts[i].y - pts[i - 1].y) / (pts[i].x - pts[i - 1].x || 1);
151
- const d1 = (pts[i + 1].y - pts[i].y) / (pts[i + 1].x - pts[i].x || 1);
152
- if (d0 * d1 <= 0) return 0;
153
- const avg = (d0 + d1) / 2;
154
- // Clamp to 3 * delta to preserve monotonicity
155
- return Math.sign(avg) * Math.min(Math.abs(avg), 3 * Math.min(Math.abs(d0), Math.abs(d1)));
156
- }
157
-
158
- /**
159
- * Get or create a `<canvas>` inside `el`.
160
- * If `el` is already an `HTMLCanvasElement`, returns it as-is.
161
- *
162
- * @param {HTMLElement|HTMLCanvasElement} el
163
- * @returns {HTMLCanvasElement}
164
- */
165
- function _ensureCanvas(el) {
166
- // @ts-ignore - el may be HTMLElement; CANVAS check ensures correct type at runtime
167
- if (el && el.tagName === 'CANVAS') return el;
168
- const canvas = document.createElement('canvas');
169
- el.appendChild(canvas);
170
- return canvas;
171
- }
172
-
173
- /**
174
- * Apply the Device Pixel Ratio to a canvas.
175
- *
176
- * @param {HTMLCanvasElement} canvas
177
- * @param {number} w - Display width (CSS px)
178
- * @param {number} h - Display height (CSS px)
179
- * @returns {CanvasRenderingContext2D}
180
- */
181
- function _setupContext(canvas, w, h) {
182
- const dpr = (typeof devicePixelRatio === 'number' ? devicePixelRatio : 1) || 1;
183
- canvas.width = Math.round(w * dpr);
184
- canvas.height = Math.round(h * dpr);
185
- canvas.style.width = w + 'px';
186
- canvas.style.height = h + 'px';
187
- const ctx = canvas.getContext('2d');
188
- if (ctx) ctx.scale(dpr, dpr);
189
- return ctx;
190
- }
191
-
192
- /**
193
- * Display dimensions of a canvas (CSS px).
194
- *
195
- * @param {HTMLCanvasElement} canvas
196
- * @returns {{w: number, h: number}}
197
- */
198
- function _displaySize(canvas) {
199
- const w = canvas.clientWidth || canvas.width || 300;
200
- const h = canvas.clientHeight || canvas.height || 150;
201
- return { w, h };
202
- }
203
-
204
- // ─────────────────────────────────────────────────────────────────
205
- // Axis drawing
206
- // ─────────────────────────────────────────────────────────────────
207
-
208
- const PAD = { top: 10, right: 10, bottom: 30, left: 40 };
209
-
210
- /**
211
- * Draw the X and Y axes with their tick marks.
212
- *
213
- * @param {CanvasRenderingContext2D} ctx
214
- * @param {{w:number,h:number}} size
215
- * @param {number} xMin
216
- * @param {number} xMax
217
- * @param {number} yMin
218
- * @param {number} yMax
219
- * @param {Object} [xAxisOpts]
220
- * @param {Object} [yAxisOpts]
221
- */
222
- function _drawAxes(ctx, size, xMin, xMax, yMin, yMax, xAxisOpts, yAxisOpts) {
223
- const { w, h } = size;
224
- const pl = PAD.left, pr = PAD.right, pt = PAD.top, pb = PAD.bottom;
225
- const cw = w - pl - pr;
226
- const ch = h - pt - pb;
227
-
228
- ctx.save();
229
- ctx.strokeStyle = '#888';
230
- ctx.fillStyle = '#888';
231
- ctx.lineWidth = 1;
232
- ctx.font = '10px sans-serif';
233
- ctx.textAlign = 'right';
234
- ctx.textBaseline = 'middle';
235
-
236
- // Axe Y
237
- const yTicks = (yAxisOpts && yAxisOpts.ticks) || _ticks(yMin, yMax, 5);
238
- const yFmt = (yAxisOpts && yAxisOpts.format) || (v => String(Math.round(v * 100) / 100));
239
- for (const t of yTicks) {
240
- const py = pt + ch - _scale(t, yMin, yMax, ch);
241
- ctx.beginPath();
242
- ctx.moveTo(pl - 4, py);
243
- ctx.lineTo(pl, py);
244
- ctx.stroke();
245
- ctx.fillText(yFmt(t), pl - 6, py);
246
- }
247
-
248
- // Axe X
249
- const xTicks = (xAxisOpts && xAxisOpts.ticks) || _ticks(xMin, xMax, 5);
250
- const xFmt = (xAxisOpts && xAxisOpts.format) || (v => String(Math.round(v * 100) / 100));
251
- ctx.textAlign = 'center';
252
- ctx.textBaseline = 'top';
253
- for (const t of xTicks) {
254
- const px = pl + _scale(t, xMin, xMax, cw);
255
- ctx.beginPath();
256
- ctx.moveTo(px, pt + ch);
257
- ctx.lineTo(px, pt + ch + 4);
258
- ctx.stroke();
259
- ctx.fillText(xFmt(t), px, pt + ch + 6);
260
- }
261
-
262
- // Baseline axes
263
- ctx.beginPath();
264
- ctx.moveTo(pl, pt);
265
- ctx.lineTo(pl, pt + ch);
266
- ctx.lineTo(pl + cw, pt + ch);
267
- ctx.stroke();
268
-
269
- ctx.restore();
270
- }
271
-
272
- // ─────────────────────────────────────────────────────────────────
273
- // Renderers
274
- // ─────────────────────────────────────────────────────────────────
275
-
276
- /**
277
- * Draw a line (or area) on a Canvas 2D context.
278
- *
279
- * @param {CanvasRenderingContext2D} ctx
280
- * @param {{w:number,h:number}} size
281
- * @param {{x:number,y:number}[]} pts
282
- * @param {number} xMin
283
- * @param {number} xMax
284
- * @param {number} yMin
285
- * @param {number} yMax
286
- * @param {string} color
287
- * @param {'none'|'monotone'} smoothing
288
- * @param {boolean} fillArea
289
- */
290
- function _renderLine(ctx, size, pts, xMin, xMax, yMin, yMax, color, smoothing, fillArea) {
291
- if (!pts.length) return;
292
- const { w, h } = size;
293
- const pl = PAD.left, pr = PAD.right, pt = PAD.top, pb = PAD.bottom;
294
- const cw = w - pl - pr;
295
- const ch = h - pt - pb;
296
-
297
- ctx.save();
298
- ctx.beginPath();
299
-
300
- const toX = v => pl + _scale(v, xMin, xMax, cw);
301
- const toY = v => pt + ch - _scale(v, yMin, yMax, ch);
302
-
303
- const x0 = toX(pts[0].x);
304
- const y0 = toY(pts[0].y);
305
- ctx.moveTo(x0, y0);
306
-
307
- if (smoothing === 'monotone' && pts.length > 2) {
308
- for (let i = 0; i < pts.length - 1; i++) {
309
- const cp1x = toX(pts[i].x + (pts[i + 1].x - pts[i].x) / 3);
310
- const cp1y = toY(pts[i].y + _monotoneSlope(pts, i) * (pts[i + 1].x - pts[i].x) / 3);
311
- const cp2x = toX(pts[i + 1].x - (pts[i + 1].x - pts[i].x) / 3);
312
- const cp2y = toY(pts[i + 1].y - _monotoneSlope(pts, i + 1) * (pts[i + 1].x - pts[i].x) / 3);
313
- ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, toX(pts[i + 1].x), toY(pts[i + 1].y));
314
- }
315
- } else {
316
- for (let i = 1; i < pts.length; i++) {
317
- ctx.lineTo(toX(pts[i].x), toY(pts[i].y));
318
- }
319
- }
320
-
321
- if (fillArea) {
322
- const baseline = pt + ch;
323
- ctx.lineTo(toX(pts[pts.length - 1].x), baseline);
324
- ctx.lineTo(toX(pts[0].x), baseline);
325
- ctx.closePath();
326
- ctx.fillStyle = color + '33';
327
- ctx.fill();
328
- // Re-draw the stroke on top of the fill
329
- ctx.beginPath();
330
- ctx.moveTo(x0, y0);
331
- if (smoothing === 'monotone' && pts.length > 2) {
332
- for (let i = 0; i < pts.length - 1; i++) {
333
- const cp1x = toX(pts[i].x + (pts[i + 1].x - pts[i].x) / 3);
334
- const cp1y = toY(pts[i].y + _monotoneSlope(pts, i) * (pts[i + 1].x - pts[i].x) / 3);
335
- const cp2x = toX(pts[i + 1].x - (pts[i + 1].x - pts[i].x) / 3);
336
- const cp2y = toY(pts[i + 1].y - _monotoneSlope(pts, i + 1) * (pts[i + 1].x - pts[i].x) / 3);
337
- ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, toX(pts[i + 1].x), toY(pts[i + 1].y));
338
- }
339
- } else {
340
- for (let i = 1; i < pts.length; i++) {
341
- ctx.lineTo(toX(pts[i].x), toY(pts[i].y));
342
- }
343
- }
344
- }
345
-
346
- ctx.strokeStyle = color;
347
- ctx.lineWidth = 1.5;
348
- ctx.stroke();
349
- ctx.restore();
350
- }
351
-
352
- /**
353
- * Draw vertical bars.
354
- *
355
- * @param {CanvasRenderingContext2D} ctx
356
- * @param {{w:number,h:number}} size
357
- * @param {{x:number,y:number}[]} pts
358
- * @param {number} xMin
359
- * @param {number} xMax
360
- * @param {number} yMin
361
- * @param {number} yMax
362
- * @param {string} color
363
- */
364
- function _renderBar(ctx, size, pts, xMin, xMax, yMin, yMax, color) {
365
- if (!pts.length) return;
366
- const { w, h } = size;
367
- const pl = PAD.left, pr = PAD.right, pt = PAD.top, pb = PAD.bottom;
368
- const cw = w - pl - pr;
369
- const ch = h - pt - pb;
370
-
371
- const toX = v => pl + _scale(v, xMin, xMax, cw);
372
- const toY = v => pt + ch - _scale(v, yMin, yMax, ch);
373
-
374
- const barW = Math.max(2, cw / pts.length * 0.7);
375
- const baseline = pt + ch;
376
-
377
- ctx.save();
378
- ctx.fillStyle = color + 'aa';
379
- ctx.strokeStyle = color;
380
- ctx.lineWidth = 1;
381
-
382
- for (const p of pts) {
383
- const bx = toX(p.x) - barW / 2;
384
- const by = toY(p.y);
385
- const bh = baseline - by;
386
- if (bh > 0) {
387
- ctx.fillRect(bx, by, barW, bh);
388
- ctx.strokeRect(bx, by, barW, bh);
389
- }
390
- }
391
-
392
- ctx.restore();
393
- }
394
-
395
- // ─────────────────────────────────────────────────────────────────
396
- // Handle constructor
397
- // ─────────────────────────────────────────────────────────────────
398
-
399
- /**
400
- * Create a chart handle (line, area or bar).
401
- *
402
- * @param {Object} opts
403
- * @param {'line'|'area'|'bar'} chartType
404
- * @returns {{ update, resize, setOpts, dispose, _state }}
405
- */
406
- function _createHandle(opts, chartType) {
407
- let _opts = Object.assign({
408
- color: '#4a9eff',
409
- smoothing: 'none',
410
- animate: false,
411
- }, opts);
412
-
413
- let _data = _normalizeData(_opts.data || []);
414
- const _canvas = _ensureCanvas(_opts.el);
415
- let _ro = null;
416
- let _disposed = false;
417
- let _animRafId = null;
418
-
419
- // Internal state exposed for debug/tests
420
- const _state = {
421
- data: _data,
422
- xDomain: [0, 1],
423
- yDomain: [0, 1],
424
- };
425
-
426
- function _computeDomains() {
427
- const xs = _data.map(p => p.x);
428
- const ys = _data.map(p => p.y);
429
- _state.xDomain = _fitDomain(xs, _opts.xAxis && _opts.xAxis.domain);
430
- _state.yDomain = _fitDomain(ys, _opts.yAxis && _opts.yAxis.domain);
431
- }
432
-
433
- function _draw() {
434
- if (_disposed) return;
435
- // Domains always recomputed (independent of the canvas context)
436
- _computeDomains();
437
- const { w, h } = _displaySize(_canvas);
438
- const ctx = _setupContext(_canvas, w, h);
439
- if (!ctx) return;
440
-
441
- ctx.clearRect(0, 0, w, h);
442
-
443
- const [xMin, xMax] = _state.xDomain;
444
- const [yMin, yMax] = _state.yDomain;
445
-
446
- if (chartType !== 'bar') {
447
- _drawAxes(ctx, { w, h }, xMin, xMax, yMin, yMax,
448
- _opts.xAxis, _opts.yAxis);
449
- _renderLine(ctx, { w, h }, _data, xMin, xMax, yMin, yMax,
450
- _opts.color, _opts.smoothing || 'none', chartType === 'area');
451
- } else {
452
- _drawAxes(ctx, { w, h }, xMin, xMax, yMin, yMax,
453
- _opts.xAxis, _opts.yAxis);
454
- _renderBar(ctx, { w, h }, _data, xMin, xMax, yMin, yMax,
455
- _opts.color);
456
- }
457
- }
458
-
459
- function _drawAnimated() {
460
- if (_disposed) return;
461
- if (_animRafId !== null) cancelAnimationFrame(_animRafId);
462
- const dur = typeof _opts.animate === 'number' ? _opts.animate : 300;
463
- // Simple progression: alpha from 0 to 1 over `dur` ms
464
- const { w, h } = _displaySize(_canvas);
465
- const ctx = _setupContext(_canvas, w, h);
466
- if (!ctx) { _draw(); return; }
467
-
468
- const startTs = { ts: null };
469
- function frame(ts) {
470
- if (_disposed) return;
471
- if (startTs.ts === null) startTs.ts = ts;
472
- const elapsed = ts - startTs.ts;
473
- const t = Math.min(elapsed / dur, 1);
474
- ctx.globalAlpha = t;
475
- _draw();
476
- ctx.globalAlpha = 1;
477
- if (t < 1) {
478
- _animRafId = requestAnimationFrame(frame);
479
- } else {
480
- _animRafId = null;
481
- }
482
- }
483
- _animRafId = requestAnimationFrame(frame);
484
- }
485
-
486
- function _render() {
487
- if (_opts.animate) {
488
- _drawAnimated();
489
- } else {
490
- _draw();
491
- }
492
- }
493
-
494
- // Setup ResizeObserver if the canvas is not the explicit element (i.e. a container)
495
- if (_opts.el && _opts.el.tagName !== 'CANVAS' && typeof ResizeObserver !== 'undefined') {
496
- _ro = new ResizeObserver(() => resize());
497
- _ro.observe(_opts.el);
498
- }
499
-
500
- _render();
501
-
502
- /**
503
- * Update the data set and redraw.
504
- *
505
- * @param {number[]|{x:number,y:number}[]} newData
506
- */
507
- function update(newData) {
508
- _data = _normalizeData(newData);
509
- _state.data = _data;
510
- _render();
511
- }
512
-
513
- /**
514
- * Force a dimension recompute and redraw.
515
- */
516
- function resize() {
517
- if (_disposed) return;
518
- _draw();
519
- }
520
-
521
- /**
522
- * Merge new options and redraw.
523
- *
524
- * @param {Object} newOpts
525
- */
526
- function setOpts(newOpts) {
527
- _opts = Object.assign(_opts, newOpts);
528
- if (newOpts.data !== undefined) {
529
- _data = _normalizeData(newOpts.data);
530
- _state.data = _data;
531
- }
532
- _render();
533
- }
534
-
535
- /**
536
- * Detach observers and release resources.
537
- */
538
- function dispose() {
539
- _disposed = true;
540
- if (_ro) { _ro.disconnect(); _ro = null; }
541
- if (_animRafId !== null) { cancelAnimationFrame(_animRafId); _animRafId = null; }
542
- }
543
-
544
- return { update, resize, setOpts, dispose, _state };
545
- }
546
-
547
- // ─────────────────────────────────────────────────────────────────
548
- // Public API
549
- // ─────────────────────────────────────────────────────────────────
550
-
551
- /**
552
- * Line chart.
553
- *
554
- * @param {{el, data, xAxis?, yAxis?, color?, smoothing?, animate?}} opts
555
- * @returns {{ update, resize, setOpts, dispose }}
556
- */
557
- function line(opts) {
558
- return _createHandle(opts, 'line');
559
- }
560
-
561
- /**
562
- * Area chart (line + fill under the curve).
563
- *
564
- * @param {{el, data, xAxis?, yAxis?, color?, smoothing?, animate?}} opts
565
- * @returns {{ update, resize, setOpts, dispose }}
566
- */
567
- function area(opts) {
568
- return _createHandle(opts, 'area');
569
- }
570
-
571
- /**
572
- * Vertical bar chart.
573
- *
574
- * @param {{el, data, xAxis?, yAxis?, color?, animate?}} opts
575
- * @returns {{ update, resize, setOpts, dispose }}
576
- */
577
- function bar(opts) {
578
- return _createHandle(opts, 'bar');
579
- }
580
-
581
- /**
582
- * Sparkline: minimal line with no axes or ticks.
583
- *
584
- * @param {{el, data, color?, smoothing?, animate?}} opts
585
- * @returns {{ update, resize, setOpts, dispose }}
586
- */
587
- function sparkline(opts) {
588
- // Override PAD to zero locally: draws directly onto the full canvas
589
- const canvas = _ensureCanvas(opts.el);
590
- let _data = _normalizeData(opts.data || []);
591
- let _opts = Object.assign({ color: '#4a9eff', smoothing: 'none', animate: false }, opts);
592
- let _ro = null;
593
- let _disposed = false;
594
-
595
- const _state = { data: _data, xDomain: [0, 1], yDomain: [0, 1] };
596
-
597
- function _draw() {
598
- if (_disposed) return;
599
- const { w, h } = _displaySize(canvas);
600
- const ctx = _setupContext(canvas, w, h);
601
- if (!ctx) return;
602
- ctx.clearRect(0, 0, w, h);
603
-
604
- if (!_data.length) return;
605
-
606
- const xs = _data.map(p => p.x);
607
- const ys = _data.map(p => p.y);
608
- const [xMin, xMax] = _fitDomain(xs, undefined);
609
- const [yMin, yMax] = _fitDomain(ys, undefined);
610
- _state.xDomain = [xMin, xMax];
611
- _state.yDomain = [yMin, yMax];
612
-
613
- const toX = v => _scale(v, xMin, xMax, w);
614
- const toY = v => h - _scale(v, yMin, yMax, h);
615
-
616
- ctx.save();
617
- ctx.beginPath();
618
- ctx.moveTo(toX(_data[0].x), toY(_data[0].y));
619
- for (let i = 1; i < _data.length; i++) {
620
- ctx.lineTo(toX(_data[i].x), toY(_data[i].y));
621
- }
622
- ctx.strokeStyle = _opts.color;
623
- ctx.lineWidth = 1.5;
624
- ctx.stroke();
625
- ctx.restore();
626
- }
627
-
628
- if (_opts.el && _opts.el.tagName !== 'CANVAS' && typeof ResizeObserver !== 'undefined') {
629
- _ro = new ResizeObserver(() => _draw());
630
- _ro.observe(_opts.el);
631
- }
632
-
633
- _draw();
634
-
635
- function update(newData) {
636
- _data = _normalizeData(newData);
637
- _state.data = _data;
638
- _draw();
639
- }
640
-
641
- function resize() { if (!_disposed) _draw(); }
642
-
643
- function setOpts(newOpts) {
644
- _opts = Object.assign(_opts, newOpts);
645
- if (newOpts.data !== undefined) {
646
- _data = _normalizeData(newOpts.data);
647
- _state.data = _data;
648
- }
649
- _draw();
650
- }
651
-
652
- function dispose() {
653
- _disposed = true;
654
- if (_ro) { _ro.disconnect(); _ro = null; }
655
- }
656
-
657
- // @ts-ignore - _state is internal debug state not in public typedef
658
- return { update, resize, setOpts, dispose, _state };
659
- }
660
-
661
- return { line, area, bar, sparkline };
662
- }
663
- };
1
+ // Copyright (c) 2026 AwaCloud SAS
2
+ // Author: Matthieu Bouilloux
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ /**
6
+ * @description
7
+ * Minimal Canvas 2D charts for internal observability (taskmgr, metrics).
8
+ * Provides the chart types: `line`, `area`, `bar`, `sparkline`.
9
+ *
10
+ * Out of MVP scope: pie/donut, log axes, interactive annotations, complex legends,
11
+ * tooltips, PNG export. For those needs, use an external library.
12
+ *
13
+ * Worker-safe: no (Canvas 2D is main-thread only; OffscreenCanvas is out of MVP scope).
14
+ *
15
+ * @example
16
+ * const chart = runtime.resolve('chart');
17
+ * const handle = chart.line({ el: document.querySelector('canvas'), data: [1, 2, 3, 4] });
18
+ * handle.update([2, 4, 3, 5]);
19
+ * handle.dispose();
20
+ */
21
+ import { dom } from '../query/dom.js';
22
+ import { animate } from '../display/animate.js';
23
+ import { stats } from '../../io/math/stats.js';
24
+ import { linalg } from '../../io/math/linalg.js';
25
+
26
+ /**
27
+ * A live chart handle returned by each chart constructor.
28
+ * @typedef {object} ChartHandle
29
+ * @property {(newData: number[]|{x:number,y:number}[]) => void} update - Replace the data set and redraw.
30
+ * @property {() => void} resize - Force a dimension recompute and redraw.
31
+ * @property {(newOpts: Object) => void} setOpts - Merge new options (and optionally data) then redraw.
32
+ * @property {() => void} dispose - Detach observers and release resources.
33
+ */
34
+
35
+ /**
36
+ * Canvas 2D chart surface returned by `factory()`.
37
+ * @typedef {object} ChartAPI
38
+ * @property {(opts: {el: HTMLElement|HTMLCanvasElement, data?: number[]|{x:number,y:number}[], xAxis?: Object, yAxis?: Object, color?: string, smoothing?: 'none'|'monotone', animate?: boolean|number}) => ChartHandle} line - Create a line chart.
39
+ * @property {(opts: {el: HTMLElement|HTMLCanvasElement, data?: number[]|{x:number,y:number}[], xAxis?: Object, yAxis?: Object, color?: string, smoothing?: 'none'|'monotone', animate?: boolean|number}) => ChartHandle} area - Create an area chart (line + fill).
40
+ * @property {(opts: {el: HTMLElement|HTMLCanvasElement, data?: number[]|{x:number,y:number}[], xAxis?: Object, yAxis?: Object, color?: string, animate?: boolean|number}) => ChartHandle} bar - Create a vertical bar chart.
41
+ * @property {(opts: {el: HTMLElement|HTMLCanvasElement, data?: number[]|{x:number,y:number}[], color?: string, smoothing?: 'none'|'monotone', animate?: boolean|number}) => ChartHandle} sparkline - Create a minimal axis-less sparkline.
42
+ */
43
+
44
+ export const chart = {
45
+ name: 'chart',
46
+ version: '1.0.0',
47
+ type: 'fw.dom.rendering',
48
+ dependencies: ['dom', 'animate', 'stats', 'linalg'],
49
+ deps: [dom, animate, stats, linalg],
50
+
51
+ /**
52
+ * @param {Object} dom - fw dom module.
53
+ * @param {Object} animate - fw animate module.
54
+ * @param {Object} stats - fw stats module.
55
+ * @param {Object} linalg - fw linalg module.
56
+ * @returns {ChartAPI}
57
+ */
58
+ factory(dom, animate, stats, linalg) {
59
+
60
+ // ─────────────────────────────────────────────────────────────────
61
+ // Internal helpers
62
+ // ─────────────────────────────────────────────────────────────────
63
+
64
+ /**
65
+ * Normalise `data` to a `{x, y}[]` array.
66
+ * Accepts `number[]` (x = index) or `{x, y}[]`.
67
+ *
68
+ * @param {number[]|{x:number,y:number}[]} data
69
+ * @returns {{x:number,y:number}[]}
70
+ */
71
+ function _normalizeData(data) {
72
+ if (!Array.isArray(data) || data.length === 0) return [];
73
+ if (typeof data[0] === 'number') {
74
+ return data.map((y, i) => ({ x: i, y }));
75
+ }
76
+ return data.map(p => ({ x: Number(p.x), y: Number(p.y) }));
77
+ }
78
+
79
+ /**
80
+ * Compute the [min, max] domain along one dimension.
81
+ * Returns `[0, 1]` when the domain is degenerate (flat data).
82
+ *
83
+ * @param {number[]} values
84
+ * @returns {[number, number]}
85
+ */
86
+ function _domain(values) {
87
+ if (!values.length) return [0, 1];
88
+ const mn = stats.min(values);
89
+ const mx = stats.max(values);
90
+ if (mn === mx) return [mn - 1, mx + 1];
91
+ return [mn, mx];
92
+ }
93
+
94
+ /**
95
+ * Compute the domain with proportional padding (5 %).
96
+ *
97
+ * @param {number[]} values
98
+ * @param {[number, number]|undefined} explicit
99
+ * @returns {[number, number]}
100
+ */
101
+ function _fitDomain(values, explicit) {
102
+ if (explicit && explicit.length === 2) return [explicit[0], explicit[1]];
103
+ const [mn, mx] = _domain(values);
104
+ const pad = (mx - mn) * 0.05;
105
+ return [mn - pad, mx + pad];
106
+ }
107
+
108
+ // A "nice" step is 1, 2, 2.5 or 5 times a power of ten.
109
+ const _NICE = [1, 2, 2.5, 5, 10];
110
+
111
+ /**
112
+ * Round ticks inside [min, max]: the smallest nice step that gives at
113
+ * most `count - 1` intervals, starting at the first multiple of the
114
+ * step at or above `min`; each value rounded to the step's decimals
115
+ * (no 0.30000000000000004). A degenerate domain gives its one value.
116
+ *
117
+ * @param {number} min
118
+ * @param {number} max
119
+ * @param {number} [count=5]
120
+ * @returns {{ticks: number[], decimals: number}}
121
+ */
122
+ function _niceTicks(min, max, count = 5) {
123
+ if (!(isFinite(min) && isFinite(max)) || !(max > min)) return { ticks: [min], decimals: 2 };
124
+ const raw = (max - min) / Math.max(1, count - 1);
125
+ const exp = Math.floor(Math.log10(raw));
126
+ const base = Math.pow(10, exp);
127
+ let nice = 10;
128
+ for (const n of _NICE) { if (raw <= n * base * (1 + 1e-9)) { nice = n; break; } }
129
+ const step = nice * base;
130
+ const decimals = Math.max(0, -exp) + (nice === 2.5 ? 1 : 0);
131
+ const out = [];
132
+ const first = Math.ceil(min / step - 1e-9);
133
+ for (let k = first; k * step <= max + step * 1e-9; k++) out.push(Number((k * step).toFixed(Math.min(decimals, 20))));
134
+ return { ticks: out, decimals };
135
+ }
136
+
137
+ /**
138
+ * Map a value from a domain to a pixel coordinate in [0, size].
139
+ *
140
+ * @param {number} v
141
+ * @param {number} min
142
+ * @param {number} max
143
+ * @param {number} size
144
+ * @returns {number}
145
+ */
146
+ function _scale(v, min, max, size) {
147
+ if (max === min) return size / 2;
148
+ return ((v - min) / (max - min)) * size;
149
+ }
150
+
151
+ /**
152
+ * Compute a Fritsch-Carlson monotone tangent (single point).
153
+ * Returns the slope for point i in the sorted {x,y}[] array.
154
+ *
155
+ * @param {{x:number,y:number}[]} pts
156
+ * @param {number} i
157
+ * @returns {number}
158
+ */
159
+ function _monotoneSlope(pts, i) {
160
+ const n = pts.length;
161
+ if (n < 2) return 0;
162
+ if (i === 0) return (pts[1].y - pts[0].y) / (pts[1].x - pts[0].x || 1);
163
+ if (i === n - 1) return (pts[n - 1].y - pts[n - 2].y) / (pts[n - 1].x - pts[n - 2].x || 1);
164
+ const d0 = (pts[i].y - pts[i - 1].y) / (pts[i].x - pts[i - 1].x || 1);
165
+ const d1 = (pts[i + 1].y - pts[i].y) / (pts[i + 1].x - pts[i].x || 1);
166
+ if (d0 * d1 <= 0) return 0;
167
+ const avg = (d0 + d1) / 2;
168
+ // Clamp to 3 * delta to preserve monotonicity
169
+ return Math.sign(avg) * Math.min(Math.abs(avg), 3 * Math.min(Math.abs(d0), Math.abs(d1)));
170
+ }
171
+
172
+ /**
173
+ * Get or create a `<canvas>` inside `el`.
174
+ * If `el` is already an `HTMLCanvasElement`, returns it as-is.
175
+ *
176
+ * @param {HTMLElement|HTMLCanvasElement} el
177
+ * @returns {HTMLCanvasElement}
178
+ */
179
+ function _ensureCanvas(el) {
180
+ // @ts-ignore - el may be HTMLElement; CANVAS check ensures correct type at runtime
181
+ if (el && el.tagName === 'CANVAS') return el;
182
+ const canvas = document.createElement('canvas');
183
+ el.appendChild(canvas);
184
+ return canvas;
185
+ }
186
+
187
+ /**
188
+ * Apply the Device Pixel Ratio to a canvas.
189
+ *
190
+ * @param {HTMLCanvasElement} canvas
191
+ * @param {number} w - Display width (CSS px)
192
+ * @param {number} h - Display height (CSS px)
193
+ * @returns {CanvasRenderingContext2D}
194
+ */
195
+ function _setupContext(canvas, w, h) {
196
+ const dpr = (typeof devicePixelRatio === 'number' ? devicePixelRatio : 1) || 1;
197
+ canvas.width = Math.round(w * dpr);
198
+ canvas.height = Math.round(h * dpr);
199
+ canvas.style.width = w + 'px';
200
+ canvas.style.height = h + 'px';
201
+ const ctx = canvas.getContext('2d');
202
+ if (ctx) ctx.scale(dpr, dpr);
203
+ return ctx;
204
+ }
205
+
206
+ /**
207
+ * Display dimensions of a canvas (CSS px).
208
+ *
209
+ * @param {HTMLCanvasElement} canvas
210
+ * @returns {{w: number, h: number}}
211
+ */
212
+ function _displaySize(canvas) {
213
+ const w = canvas.clientWidth || canvas.width || 300;
214
+ const h = canvas.clientHeight || canvas.height || 150;
215
+ return { w, h };
216
+ }
217
+
218
+ // ─────────────────────────────────────────────────────────────────
219
+ // Axis drawing
220
+ // ─────────────────────────────────────────────────────────────────
221
+
222
+ const PAD = { top: 10, right: 10, bottom: 30, left: 40 };
223
+ const AXIS_FONT = '10px sans-serif';
224
+
225
+ /** Width of `text` at the axis font: `measureText` when the context has it, else 6 px a character. */
226
+ function _textWidth(ctx, text) {
227
+ if (ctx && typeof ctx.measureText === 'function') {
228
+ const m = ctx.measureText(text);
229
+ if (m && typeof m.width === 'number') return m.width;
230
+ }
231
+ return 6 * String(text).length;
232
+ }
233
+
234
+ /**
235
+ * The axes' ticks, label formatters and the plot padding for one draw:
236
+ * default ticks are round (`_niceTicks`) and labelled at their step's
237
+ * precision; `ticks` / `format` given in the axis options are kept.
238
+ * The left padding fits the widest y label and half the first x
239
+ * label, the right one half the last x label — never below `PAD`.
240
+ *
241
+ * @returns {{pad: {top:number,right:number,bottom:number,left:number}, xTicks: number[], yTicks: number[], xFmt: Function, yFmt: Function}}
242
+ */
243
+ function _layout(ctx, xMin, xMax, yMin, yMax, xAxisOpts, yAxisOpts) {
244
+ const axis = (opts, min, max) => {
245
+ const given = opts && opts.ticks;
246
+ const nt = given ? null : _niceTicks(min, max, 5);
247
+ const ticks = given || nt.ticks;
248
+ const fmt = (opts && opts.format) ||
249
+ (nt ? (v => String(Number(v.toFixed(Math.min(nt.decimals, 20))))) : (v => String(Math.round(v * 100) / 100)));
250
+ return { ticks, fmt };
251
+ };
252
+ const y = axis(yAxisOpts, yMin, yMax), x = axis(xAxisOpts, xMin, xMax);
253
+ if (ctx) ctx.font = AXIS_FONT;
254
+ let yw = 0;
255
+ for (const t of y.ticks) yw = Math.max(yw, _textWidth(ctx, y.fmt(t)));
256
+ const firstX = x.ticks.length ? _textWidth(ctx, x.fmt(x.ticks[0])) : 0;
257
+ const lastX = x.ticks.length ? _textWidth(ctx, x.fmt(x.ticks[x.ticks.length - 1])) : 0;
258
+ const pad = {
259
+ top: PAD.top,
260
+ bottom: PAD.bottom,
261
+ // label right edge at left - 6, plus 2 px of margin
262
+ left: Math.max(PAD.left, Math.ceil(yw) + 8, Math.ceil(firstX / 2) + 2),
263
+ right: Math.max(PAD.right, Math.ceil(lastX / 2) + 2),
264
+ };
265
+ return { pad, xTicks: x.ticks, yTicks: y.ticks, xFmt: x.fmt, yFmt: y.fmt };
266
+ }
267
+
268
+ /**
269
+ * Draw the X and Y axes with their tick marks.
270
+ *
271
+ * @param {CanvasRenderingContext2D} ctx
272
+ * @param {{w:number,h:number}} size
273
+ * @param {number} xMin
274
+ * @param {number} xMax
275
+ * @param {number} yMin
276
+ * @param {number} yMax
277
+ * @param {Object} [xAxisOpts]
278
+ * @param {Object} [yAxisOpts]
279
+ * @param {ReturnType<typeof _layout>} [layout] The draw's ticks and padding (computed when absent).
280
+ */
281
+ function _drawAxes(ctx, size, xMin, xMax, yMin, yMax, xAxisOpts, yAxisOpts, layout) {
282
+ const { w, h } = size;
283
+ const L = layout || _layout(ctx, xMin, xMax, yMin, yMax, xAxisOpts, yAxisOpts);
284
+ const pl = L.pad.left, pr = L.pad.right, pt = L.pad.top, pb = L.pad.bottom;
285
+ const cw = w - pl - pr;
286
+ const ch = h - pt - pb;
287
+
288
+ ctx.save();
289
+ ctx.strokeStyle = '#888';
290
+ ctx.fillStyle = '#888';
291
+ ctx.lineWidth = 1;
292
+ ctx.font = AXIS_FONT;
293
+ ctx.textAlign = 'right';
294
+ ctx.textBaseline = 'middle';
295
+
296
+ // Axe Y
297
+ const yTicks = L.yTicks;
298
+ const yFmt = L.yFmt;
299
+ for (const t of yTicks) {
300
+ const py = pt + ch - _scale(t, yMin, yMax, ch);
301
+ ctx.beginPath();
302
+ ctx.moveTo(pl - 4, py);
303
+ ctx.lineTo(pl, py);
304
+ ctx.stroke();
305
+ ctx.fillText(yFmt(t), pl - 6, py);
306
+ }
307
+
308
+ // Axe X
309
+ const xTicks = L.xTicks;
310
+ const xFmt = L.xFmt;
311
+ ctx.textAlign = 'center';
312
+ ctx.textBaseline = 'top';
313
+ for (const t of xTicks) {
314
+ const px = pl + _scale(t, xMin, xMax, cw);
315
+ ctx.beginPath();
316
+ ctx.moveTo(px, pt + ch);
317
+ ctx.lineTo(px, pt + ch + 4);
318
+ ctx.stroke();
319
+ ctx.fillText(xFmt(t), px, pt + ch + 6);
320
+ }
321
+
322
+ // Baseline axes
323
+ ctx.beginPath();
324
+ ctx.moveTo(pl, pt);
325
+ ctx.lineTo(pl, pt + ch);
326
+ ctx.lineTo(pl + cw, pt + ch);
327
+ ctx.stroke();
328
+
329
+ ctx.restore();
330
+ }
331
+
332
+ // ─────────────────────────────────────────────────────────────────
333
+ // Renderers
334
+ // ─────────────────────────────────────────────────────────────────
335
+
336
+ /**
337
+ * Draw a line (or area) on a Canvas 2D context.
338
+ *
339
+ * @param {CanvasRenderingContext2D} ctx
340
+ * @param {{w:number,h:number}} size
341
+ * @param {{x:number,y:number}[]} pts
342
+ * @param {number} xMin
343
+ * @param {number} xMax
344
+ * @param {number} yMin
345
+ * @param {number} yMax
346
+ * @param {string} color
347
+ * @param {'none'|'monotone'} smoothing
348
+ * @param {boolean} fillArea
349
+ * @param {{top:number,right:number,bottom:number,left:number}} [pad] Plot padding (default `PAD`).
350
+ */
351
+ function _renderLine(ctx, size, pts, xMin, xMax, yMin, yMax, color, smoothing, fillArea, pad) {
352
+ if (!pts.length) return;
353
+ const { w, h } = size;
354
+ const P = pad || PAD;
355
+ const pl = P.left, pr = P.right, pt = P.top, pb = P.bottom;
356
+ const cw = w - pl - pr;
357
+ const ch = h - pt - pb;
358
+
359
+ ctx.save();
360
+ ctx.beginPath();
361
+
362
+ const toX = v => pl + _scale(v, xMin, xMax, cw);
363
+ const toY = v => pt + ch - _scale(v, yMin, yMax, ch);
364
+
365
+ const x0 = toX(pts[0].x);
366
+ const y0 = toY(pts[0].y);
367
+ ctx.moveTo(x0, y0);
368
+
369
+ if (smoothing === 'monotone' && pts.length > 2) {
370
+ for (let i = 0; i < pts.length - 1; i++) {
371
+ const cp1x = toX(pts[i].x + (pts[i + 1].x - pts[i].x) / 3);
372
+ const cp1y = toY(pts[i].y + _monotoneSlope(pts, i) * (pts[i + 1].x - pts[i].x) / 3);
373
+ const cp2x = toX(pts[i + 1].x - (pts[i + 1].x - pts[i].x) / 3);
374
+ const cp2y = toY(pts[i + 1].y - _monotoneSlope(pts, i + 1) * (pts[i + 1].x - pts[i].x) / 3);
375
+ ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, toX(pts[i + 1].x), toY(pts[i + 1].y));
376
+ }
377
+ } else {
378
+ for (let i = 1; i < pts.length; i++) {
379
+ ctx.lineTo(toX(pts[i].x), toY(pts[i].y));
380
+ }
381
+ }
382
+
383
+ if (fillArea) {
384
+ const baseline = pt + ch;
385
+ ctx.lineTo(toX(pts[pts.length - 1].x), baseline);
386
+ ctx.lineTo(toX(pts[0].x), baseline);
387
+ ctx.closePath();
388
+ // Translucent by alpha, not by a suffix on the colour: any CSS
389
+ // colour works, and a hex renders as `color + '33'` did.
390
+ const alpha = ctx.globalAlpha;
391
+ ctx.fillStyle = color;
392
+ ctx.globalAlpha = alpha * 0x33 / 255;
393
+ ctx.fill();
394
+ ctx.globalAlpha = alpha;
395
+ // Re-draw the stroke on top of the fill
396
+ ctx.beginPath();
397
+ ctx.moveTo(x0, y0);
398
+ if (smoothing === 'monotone' && pts.length > 2) {
399
+ for (let i = 0; i < pts.length - 1; i++) {
400
+ const cp1x = toX(pts[i].x + (pts[i + 1].x - pts[i].x) / 3);
401
+ const cp1y = toY(pts[i].y + _monotoneSlope(pts, i) * (pts[i + 1].x - pts[i].x) / 3);
402
+ const cp2x = toX(pts[i + 1].x - (pts[i + 1].x - pts[i].x) / 3);
403
+ const cp2y = toY(pts[i + 1].y - _monotoneSlope(pts, i + 1) * (pts[i + 1].x - pts[i].x) / 3);
404
+ ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, toX(pts[i + 1].x), toY(pts[i + 1].y));
405
+ }
406
+ } else {
407
+ for (let i = 1; i < pts.length; i++) {
408
+ ctx.lineTo(toX(pts[i].x), toY(pts[i].y));
409
+ }
410
+ }
411
+ }
412
+
413
+ ctx.strokeStyle = color;
414
+ ctx.lineWidth = 1.5;
415
+ ctx.stroke();
416
+ ctx.restore();
417
+ }
418
+
419
+ /**
420
+ * Draw vertical bars.
421
+ *
422
+ * @param {CanvasRenderingContext2D} ctx
423
+ * @param {{w:number,h:number}} size
424
+ * @param {{x:number,y:number}[]} pts
425
+ * @param {number} xMin
426
+ * @param {number} xMax
427
+ * @param {number} yMin
428
+ * @param {number} yMax
429
+ * @param {string} color
430
+ * @param {{top:number,right:number,bottom:number,left:number}} [pad] Plot padding (default `PAD`).
431
+ */
432
+ function _renderBar(ctx, size, pts, xMin, xMax, yMin, yMax, color, pad) {
433
+ if (!pts.length) return;
434
+ const { w, h } = size;
435
+ const P = pad || PAD;
436
+ const pl = P.left, pr = P.right, pt = P.top, pb = P.bottom;
437
+ const cw = w - pl - pr;
438
+ const ch = h - pt - pb;
439
+
440
+ const toX = v => pl + _scale(v, xMin, xMax, cw);
441
+ const toY = v => pt + ch - _scale(v, yMin, yMax, ch);
442
+
443
+ const barW = Math.max(2, cw / pts.length * 0.7);
444
+ const baseline = pt + ch;
445
+
446
+ ctx.save();
447
+ ctx.fillStyle = color;
448
+ ctx.strokeStyle = color;
449
+ ctx.lineWidth = 1;
450
+ // The fill translucent by alpha (as `color + 'aa'` did for a hex).
451
+ const alpha = ctx.globalAlpha;
452
+
453
+ for (const p of pts) {
454
+ const bx = toX(p.x) - barW / 2;
455
+ const by = toY(p.y);
456
+ const bh = baseline - by;
457
+ if (bh > 0) {
458
+ ctx.globalAlpha = alpha * 0xaa / 255;
459
+ ctx.fillRect(bx, by, barW, bh);
460
+ ctx.globalAlpha = alpha;
461
+ ctx.strokeRect(bx, by, barW, bh);
462
+ }
463
+ }
464
+
465
+ ctx.restore();
466
+ }
467
+
468
+ // ─────────────────────────────────────────────────────────────────
469
+ // Handle constructor
470
+ // ─────────────────────────────────────────────────────────────────
471
+
472
+ /**
473
+ * Create a chart handle (line, area or bar).
474
+ *
475
+ * @param {Object} opts
476
+ * @param {'line'|'area'|'bar'} chartType
477
+ * @returns {{ update, resize, setOpts, dispose, _state }}
478
+ */
479
+ function _createHandle(opts, chartType) {
480
+ let _opts = Object.assign({
481
+ color: '#4a9eff',
482
+ smoothing: 'none',
483
+ animate: false,
484
+ }, opts);
485
+
486
+ let _data = _normalizeData(_opts.data || []);
487
+ const _canvas = _ensureCanvas(_opts.el);
488
+ let _ro = null;
489
+ let _disposed = false;
490
+ let _animRafId = null;
491
+
492
+ // Internal state exposed for debug/tests
493
+ const _state = {
494
+ data: _data,
495
+ xDomain: [0, 1],
496
+ yDomain: [0, 1],
497
+ pad: PAD,
498
+ };
499
+
500
+ function _computeDomains() {
501
+ const xs = _data.map(p => p.x);
502
+ const ys = _data.map(p => p.y);
503
+ _state.xDomain = _fitDomain(xs, _opts.xAxis && _opts.xAxis.domain);
504
+ _state.yDomain = _fitDomain(ys, _opts.yAxis && _opts.yAxis.domain);
505
+ }
506
+
507
+ function _draw() {
508
+ if (_disposed) return;
509
+ // Domains always recomputed (independent of the canvas context)
510
+ _computeDomains();
511
+ const { w, h } = _displaySize(_canvas);
512
+ const ctx = _setupContext(_canvas, w, h);
513
+ if (!ctx) return;
514
+
515
+ ctx.clearRect(0, 0, w, h);
516
+
517
+ const [xMin, xMax] = _state.xDomain;
518
+ const [yMin, yMax] = _state.yDomain;
519
+
520
+ const layout = _layout(ctx, xMin, xMax, yMin, yMax, _opts.xAxis, _opts.yAxis);
521
+ _state.pad = layout.pad;
522
+ _drawAxes(ctx, { w, h }, xMin, xMax, yMin, yMax, _opts.xAxis, _opts.yAxis, layout);
523
+ if (chartType !== 'bar') {
524
+ _renderLine(ctx, { w, h }, _data, xMin, xMax, yMin, yMax,
525
+ _opts.color, _opts.smoothing || 'none', chartType === 'area', layout.pad);
526
+ } else {
527
+ _renderBar(ctx, { w, h }, _data, xMin, xMax, yMin, yMax,
528
+ _opts.color, layout.pad);
529
+ }
530
+ }
531
+
532
+ function _drawAnimated() {
533
+ if (_disposed) return;
534
+ if (_animRafId !== null) cancelAnimationFrame(_animRafId);
535
+ const dur = typeof _opts.animate === 'number' ? _opts.animate : 300;
536
+ // Simple progression: alpha from 0 to 1 over `dur` ms
537
+ const { w, h } = _displaySize(_canvas);
538
+ const ctx = _setupContext(_canvas, w, h);
539
+ if (!ctx) { _draw(); return; }
540
+
541
+ const startTs = { ts: null };
542
+ function frame(ts) {
543
+ if (_disposed) return;
544
+ if (startTs.ts === null) startTs.ts = ts;
545
+ const elapsed = ts - startTs.ts;
546
+ const t = Math.min(elapsed / dur, 1);
547
+ ctx.globalAlpha = t;
548
+ _draw();
549
+ ctx.globalAlpha = 1;
550
+ if (t < 1) {
551
+ _animRafId = requestAnimationFrame(frame);
552
+ } else {
553
+ _animRafId = null;
554
+ }
555
+ }
556
+ _animRafId = requestAnimationFrame(frame);
557
+ }
558
+
559
+ function _render() {
560
+ if (_opts.animate) {
561
+ _drawAnimated();
562
+ } else {
563
+ _draw();
564
+ }
565
+ }
566
+
567
+ // Setup ResizeObserver if the canvas is not the explicit element (i.e. a container)
568
+ if (_opts.el && _opts.el.tagName !== 'CANVAS' && typeof ResizeObserver !== 'undefined') {
569
+ _ro = new ResizeObserver(() => resize());
570
+ _ro.observe(_opts.el);
571
+ }
572
+
573
+ _render();
574
+
575
+ /**
576
+ * Update the data set and redraw.
577
+ *
578
+ * @param {number[]|{x:number,y:number}[]} newData
579
+ */
580
+ function update(newData) {
581
+ _data = _normalizeData(newData);
582
+ _state.data = _data;
583
+ _render();
584
+ }
585
+
586
+ /**
587
+ * Force a dimension recompute and redraw.
588
+ */
589
+ function resize() {
590
+ if (_disposed) return;
591
+ _draw();
592
+ }
593
+
594
+ /**
595
+ * Merge new options and redraw.
596
+ *
597
+ * @param {Object} newOpts
598
+ */
599
+ function setOpts(newOpts) {
600
+ _opts = Object.assign(_opts, newOpts);
601
+ if (newOpts.data !== undefined) {
602
+ _data = _normalizeData(newOpts.data);
603
+ _state.data = _data;
604
+ }
605
+ _render();
606
+ }
607
+
608
+ /**
609
+ * Detach observers and release resources.
610
+ */
611
+ function dispose() {
612
+ _disposed = true;
613
+ if (_ro) { _ro.disconnect(); _ro = null; }
614
+ if (_animRafId !== null) { cancelAnimationFrame(_animRafId); _animRafId = null; }
615
+ }
616
+
617
+ return { update, resize, setOpts, dispose, _state };
618
+ }
619
+
620
+ // ─────────────────────────────────────────────────────────────────
621
+ // Public API
622
+ // ─────────────────────────────────────────────────────────────────
623
+
624
+ /**
625
+ * Line chart.
626
+ *
627
+ * @param {{el, data, xAxis?, yAxis?, color?, smoothing?, animate?}} opts
628
+ * @returns {{ update, resize, setOpts, dispose }}
629
+ */
630
+ function line(opts) {
631
+ return _createHandle(opts, 'line');
632
+ }
633
+
634
+ /**
635
+ * Area chart (line + fill under the curve).
636
+ *
637
+ * @param {{el, data, xAxis?, yAxis?, color?, smoothing?, animate?}} opts
638
+ * @returns {{ update, resize, setOpts, dispose }}
639
+ */
640
+ function area(opts) {
641
+ return _createHandle(opts, 'area');
642
+ }
643
+
644
+ /**
645
+ * Vertical bar chart.
646
+ *
647
+ * @param {{el, data, xAxis?, yAxis?, color?, animate?}} opts
648
+ * @returns {{ update, resize, setOpts, dispose }}
649
+ */
650
+ function bar(opts) {
651
+ return _createHandle(opts, 'bar');
652
+ }
653
+
654
+ /**
655
+ * Sparkline: minimal line with no axes or ticks.
656
+ *
657
+ * @param {{el, data, color?, smoothing?, animate?}} opts
658
+ * @returns {{ update, resize, setOpts, dispose }}
659
+ */
660
+ function sparkline(opts) {
661
+ // Override PAD to zero locally: draws directly onto the full canvas
662
+ const canvas = _ensureCanvas(opts.el);
663
+ let _data = _normalizeData(opts.data || []);
664
+ let _opts = Object.assign({ color: '#4a9eff', smoothing: 'none', animate: false }, opts);
665
+ let _ro = null;
666
+ let _disposed = false;
667
+
668
+ const _state = { data: _data, xDomain: [0, 1], yDomain: [0, 1] };
669
+
670
+ function _draw() {
671
+ if (_disposed) return;
672
+ const { w, h } = _displaySize(canvas);
673
+ const ctx = _setupContext(canvas, w, h);
674
+ if (!ctx) return;
675
+ ctx.clearRect(0, 0, w, h);
676
+
677
+ if (!_data.length) return;
678
+
679
+ const xs = _data.map(p => p.x);
680
+ const ys = _data.map(p => p.y);
681
+ const [xMin, xMax] = _fitDomain(xs, undefined);
682
+ const [yMin, yMax] = _fitDomain(ys, undefined);
683
+ _state.xDomain = [xMin, xMax];
684
+ _state.yDomain = [yMin, yMax];
685
+
686
+ const toX = v => _scale(v, xMin, xMax, w);
687
+ const toY = v => h - _scale(v, yMin, yMax, h);
688
+
689
+ ctx.save();
690
+ ctx.beginPath();
691
+ ctx.moveTo(toX(_data[0].x), toY(_data[0].y));
692
+ for (let i = 1; i < _data.length; i++) {
693
+ ctx.lineTo(toX(_data[i].x), toY(_data[i].y));
694
+ }
695
+ ctx.strokeStyle = _opts.color;
696
+ ctx.lineWidth = 1.5;
697
+ ctx.stroke();
698
+ ctx.restore();
699
+ }
700
+
701
+ if (_opts.el && _opts.el.tagName !== 'CANVAS' && typeof ResizeObserver !== 'undefined') {
702
+ _ro = new ResizeObserver(() => _draw());
703
+ _ro.observe(_opts.el);
704
+ }
705
+
706
+ _draw();
707
+
708
+ function update(newData) {
709
+ _data = _normalizeData(newData);
710
+ _state.data = _data;
711
+ _draw();
712
+ }
713
+
714
+ function resize() { if (!_disposed) _draw(); }
715
+
716
+ function setOpts(newOpts) {
717
+ _opts = Object.assign(_opts, newOpts);
718
+ if (newOpts.data !== undefined) {
719
+ _data = _normalizeData(newOpts.data);
720
+ _state.data = _data;
721
+ }
722
+ _draw();
723
+ }
724
+
725
+ function dispose() {
726
+ _disposed = true;
727
+ if (_ro) { _ro.disconnect(); _ro = null; }
728
+ }
729
+
730
+ // @ts-ignore - _state is internal debug state not in public typedef
731
+ return { update, resize, setOpts, dispose, _state };
732
+ }
733
+
734
+ return { line, area, bar, sparkline };
735
+ }
736
+ };