effect 4.0.7 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (863) hide show
  1. package/README.md +31 -1
  2. package/THIRD_PARTY_NOTICES.md +62 -0
  3. package/dist/BigDecimal.d.ts +139 -7
  4. package/dist/BigDecimal.d.ts.map +1 -1
  5. package/dist/BigDecimal.js +86 -14
  6. package/dist/BigDecimal.js.map +1 -1
  7. package/dist/Channel.d.ts +12 -6
  8. package/dist/Channel.d.ts.map +1 -1
  9. package/dist/Channel.js +9 -3
  10. package/dist/Channel.js.map +1 -1
  11. package/dist/Config.d.ts +7 -1
  12. package/dist/Config.d.ts.map +1 -1
  13. package/dist/Config.js.map +1 -1
  14. package/dist/Context.d.ts +18 -1
  15. package/dist/Context.d.ts.map +1 -1
  16. package/dist/Context.js +27 -8
  17. package/dist/Context.js.map +1 -1
  18. package/dist/Cron.d.ts +81 -11
  19. package/dist/Cron.d.ts.map +1 -1
  20. package/dist/Cron.js +101 -26
  21. package/dist/Cron.js.map +1 -1
  22. package/dist/Duration.d.ts +78 -0
  23. package/dist/Duration.d.ts.map +1 -1
  24. package/dist/Duration.js +123 -0
  25. package/dist/Duration.js.map +1 -1
  26. package/dist/Effect.d.ts +164 -7
  27. package/dist/Effect.d.ts.map +1 -1
  28. package/dist/Effect.js +11 -1
  29. package/dist/Effect.js.map +1 -1
  30. package/dist/Equal.d.ts +2 -2
  31. package/dist/Equal.d.ts.map +1 -1
  32. package/dist/Equal.js +3 -3
  33. package/dist/Equal.js.map +1 -1
  34. package/dist/FileSystem.d.ts +59 -4
  35. package/dist/FileSystem.d.ts.map +1 -1
  36. package/dist/FileSystem.js +9 -2
  37. package/dist/FileSystem.js.map +1 -1
  38. package/dist/Glob.d.ts +165 -0
  39. package/dist/Glob.d.ts.map +1 -0
  40. package/dist/Glob.js +127 -0
  41. package/dist/Glob.js.map +1 -0
  42. package/dist/Hash.d.ts +3 -2
  43. package/dist/Hash.d.ts.map +1 -1
  44. package/dist/Hash.js +5 -4
  45. package/dist/Hash.js.map +1 -1
  46. package/dist/Iterable.d.ts.map +1 -1
  47. package/dist/Iterable.js +11 -8
  48. package/dist/Iterable.js.map +1 -1
  49. package/dist/Match.d.ts +86 -0
  50. package/dist/Match.d.ts.map +1 -1
  51. package/dist/Match.js +72 -0
  52. package/dist/Match.js.map +1 -1
  53. package/dist/Path.d.ts.map +1 -1
  54. package/dist/Path.js +5 -1
  55. package/dist/Path.js.map +1 -1
  56. package/dist/Pool.d.ts +51 -0
  57. package/dist/Pool.d.ts.map +1 -1
  58. package/dist/Pool.js +61 -10
  59. package/dist/Pool.js.map +1 -1
  60. package/dist/Random.d.ts.map +1 -1
  61. package/dist/Random.js +3 -3
  62. package/dist/Random.js.map +1 -1
  63. package/dist/RcMap.d.ts.map +1 -1
  64. package/dist/RcMap.js +8 -2
  65. package/dist/RcMap.js.map +1 -1
  66. package/dist/Schedule.d.ts +18 -1
  67. package/dist/Schedule.d.ts.map +1 -1
  68. package/dist/Schedule.js +7 -2
  69. package/dist/Schedule.js.map +1 -1
  70. package/dist/Schema.d.ts +668 -42
  71. package/dist/Schema.d.ts.map +1 -1
  72. package/dist/Schema.js +652 -1
  73. package/dist/Schema.js.map +1 -1
  74. package/dist/SchemaGetter.d.ts +6 -1
  75. package/dist/SchemaGetter.d.ts.map +1 -1
  76. package/dist/SchemaGetter.js +22 -11
  77. package/dist/SchemaGetter.js.map +1 -1
  78. package/dist/SchemaRepresentation.d.ts +18 -2
  79. package/dist/SchemaRepresentation.d.ts.map +1 -1
  80. package/dist/SchemaRepresentation.js +20 -2
  81. package/dist/SchemaRepresentation.js.map +1 -1
  82. package/dist/SchemaTransformation.d.ts +10 -5
  83. package/dist/SchemaTransformation.d.ts.map +1 -1
  84. package/dist/SchemaTransformation.js +8 -5
  85. package/dist/SchemaTransformation.js.map +1 -1
  86. package/dist/ScopedCache.d.ts.map +1 -1
  87. package/dist/ScopedCache.js +5 -7
  88. package/dist/ScopedCache.js.map +1 -1
  89. package/dist/Semaphore.d.ts +130 -0
  90. package/dist/Semaphore.d.ts.map +1 -1
  91. package/dist/Semaphore.js +40 -0
  92. package/dist/Semaphore.js.map +1 -1
  93. package/dist/Stream.d.ts +13 -4
  94. package/dist/Stream.d.ts.map +1 -1
  95. package/dist/Stream.js +9 -0
  96. package/dist/Stream.js.map +1 -1
  97. package/dist/Terminal.d.ts +18 -1
  98. package/dist/Terminal.d.ts.map +1 -1
  99. package/dist/Terminal.js +1 -1
  100. package/dist/Terminal.js.map +1 -1
  101. package/dist/ai/AiError.d.ts +43 -116
  102. package/dist/ai/AiError.d.ts.map +1 -1
  103. package/dist/ai/AiError.js +53 -72
  104. package/dist/ai/AiError.js.map +1 -1
  105. package/dist/ai/Decision.d.ts +196 -15
  106. package/dist/ai/Decision.d.ts.map +1 -1
  107. package/dist/ai/Decision.js +130 -0
  108. package/dist/ai/Decision.js.map +1 -1
  109. package/dist/ai/DecisionModel.d.ts +54 -13
  110. package/dist/ai/DecisionModel.d.ts.map +1 -1
  111. package/dist/ai/DecisionModel.js +123 -12
  112. package/dist/ai/DecisionModel.js.map +1 -1
  113. package/dist/ai/McpClient.d.ts +171 -19
  114. package/dist/ai/McpClient.d.ts.map +1 -1
  115. package/dist/ai/McpClient.js +277 -66
  116. package/dist/ai/McpClient.js.map +1 -1
  117. package/dist/ai/McpSchema.d.ts +108 -0
  118. package/dist/ai/McpSchema.d.ts.map +1 -1
  119. package/dist/ai/McpSchema.js +99 -0
  120. package/dist/ai/McpSchema.js.map +1 -1
  121. package/dist/ai/McpServer.d.ts +744 -29
  122. package/dist/ai/McpServer.d.ts.map +1 -1
  123. package/dist/ai/McpServer.js +763 -136
  124. package/dist/ai/McpServer.js.map +1 -1
  125. package/dist/ai/McpTest.d.ts +130 -0
  126. package/dist/ai/McpTest.d.ts.map +1 -0
  127. package/dist/ai/McpTest.js +237 -0
  128. package/dist/ai/McpTest.js.map +1 -0
  129. package/dist/ai/Prompt.d.ts.map +1 -1
  130. package/dist/ai/Prompt.js +20 -11
  131. package/dist/ai/Prompt.js.map +1 -1
  132. package/dist/ai/Response.d.ts +1 -25
  133. package/dist/ai/Response.d.ts.map +1 -1
  134. package/dist/ai/Response.js +1 -10
  135. package/dist/ai/Response.js.map +1 -1
  136. package/dist/ai/Tool.d.ts +19 -1
  137. package/dist/ai/Tool.d.ts.map +1 -1
  138. package/dist/ai/Tool.js +5 -7
  139. package/dist/ai/Tool.js.map +1 -1
  140. package/dist/ai/index.d.ts +5 -0
  141. package/dist/ai/index.d.ts.map +1 -1
  142. package/dist/ai/index.js +5 -0
  143. package/dist/ai/index.js.map +1 -1
  144. package/dist/ai/internal/mcpCore.js +89 -15
  145. package/dist/ai/internal/mcpCore.js.map +1 -1
  146. package/dist/ai/internal/mcpProtocol/v2024_11_05.js +7 -6
  147. package/dist/ai/internal/mcpProtocol/v2024_11_05.js.map +1 -1
  148. package/dist/ai/internal/mcpProtocol/v2025_03_26.js +7 -6
  149. package/dist/ai/internal/mcpProtocol/v2025_03_26.js.map +1 -1
  150. package/dist/ai/internal/mcpProtocol/v2025_06_18.js +7 -6
  151. package/dist/ai/internal/mcpProtocol/v2025_06_18.js.map +1 -1
  152. package/dist/ai/internal/mcpProtocol/v2025_11_25.js +7 -6
  153. package/dist/ai/internal/mcpProtocol/v2025_11_25.js.map +1 -1
  154. package/dist/ai/internal/mcpProtocol/v2026_07_28.js +60 -17
  155. package/dist/ai/internal/mcpProtocol/v2026_07_28.js.map +1 -1
  156. package/dist/ai/internal/mcpProtocol.js +25 -0
  157. package/dist/ai/internal/mcpProtocol.js.map +1 -1
  158. package/dist/ai/internal/mcpRuntime.js +8 -6
  159. package/dist/ai/internal/mcpRuntime.js.map +1 -1
  160. package/dist/ai/internal/mcpSkills.d.ts +2 -0
  161. package/dist/ai/internal/mcpSkills.d.ts.map +1 -0
  162. package/dist/ai/internal/mcpSkills.js +428 -0
  163. package/dist/ai/internal/mcpSkills.js.map +1 -0
  164. package/dist/ai/internal/mcpStatefulRuntime.js +6 -1
  165. package/dist/ai/internal/mcpStatefulRuntime.js.map +1 -1
  166. package/dist/cli/Argument.d.ts +6 -0
  167. package/dist/cli/Argument.d.ts.map +1 -1
  168. package/dist/cli/Argument.js +2 -0
  169. package/dist/cli/Argument.js.map +1 -1
  170. package/dist/cli/CliOutput.d.ts +65 -1
  171. package/dist/cli/CliOutput.d.ts.map +1 -1
  172. package/dist/cli/CliOutput.js +94 -37
  173. package/dist/cli/CliOutput.js.map +1 -1
  174. package/dist/cli/Command.d.ts +23 -0
  175. package/dist/cli/Command.d.ts.map +1 -1
  176. package/dist/cli/Command.js +8 -0
  177. package/dist/cli/Command.js.map +1 -1
  178. package/dist/cli/Flag.d.ts +107 -0
  179. package/dist/cli/Flag.d.ts.map +1 -1
  180. package/dist/cli/Flag.js +37 -0
  181. package/dist/cli/Flag.js.map +1 -1
  182. package/dist/cli/HelpDoc.d.ts +15 -1
  183. package/dist/cli/HelpDoc.d.ts.map +1 -1
  184. package/dist/cli/Param.d.ts +6 -0
  185. package/dist/cli/Param.d.ts.map +1 -1
  186. package/dist/cli/Param.js +2 -0
  187. package/dist/cli/Param.js.map +1 -1
  188. package/dist/cli/Primitive.d.ts +14 -0
  189. package/dist/cli/Primitive.d.ts.map +1 -1
  190. package/dist/cli/Primitive.js +15 -1
  191. package/dist/cli/Primitive.js.map +1 -1
  192. package/dist/cli/Prompt.d.ts +1 -0
  193. package/dist/cli/Prompt.d.ts.map +1 -1
  194. package/dist/cli/Prompt.js +1 -0
  195. package/dist/cli/Prompt.js.map +1 -1
  196. package/dist/cli/internal/help.d.ts.map +1 -1
  197. package/dist/cli/internal/help.js +17 -10
  198. package/dist/cli/internal/help.js.map +1 -1
  199. package/dist/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
  200. package/dist/cluster/ClusterWorkflowEngine.js +15 -5
  201. package/dist/cluster/ClusterWorkflowEngine.js.map +1 -1
  202. package/dist/cluster/Reply.d.ts +1 -1
  203. package/dist/cluster/Reply.d.ts.map +1 -1
  204. package/dist/cluster/Reply.js.map +1 -1
  205. package/dist/cluster/Sharding.d.ts.map +1 -1
  206. package/dist/cluster/Sharding.js +93 -28
  207. package/dist/cluster/Sharding.js.map +1 -1
  208. package/dist/cluster/ShardingConfig.d.ts +72 -6
  209. package/dist/cluster/ShardingConfig.d.ts.map +1 -1
  210. package/dist/cluster/ShardingConfig.js +4 -0
  211. package/dist/cluster/ShardingConfig.js.map +1 -1
  212. package/dist/cluster/SingleRunner.d.ts +3 -3
  213. package/dist/cluster/SingleRunner.d.ts.map +1 -1
  214. package/dist/cluster/SingleRunner.js +1 -1
  215. package/dist/cluster/SqlMessageStorage.d.ts +37 -6
  216. package/dist/cluster/SqlMessageStorage.d.ts.map +1 -1
  217. package/dist/cluster/SqlMessageStorage.js +34 -7
  218. package/dist/cluster/SqlMessageStorage.js.map +1 -1
  219. package/dist/cluster/SqlRunnerStorage.d.ts.map +1 -1
  220. package/dist/cluster/SqlRunnerStorage.js +23 -8
  221. package/dist/cluster/SqlRunnerStorage.js.map +1 -1
  222. package/dist/cluster/internal/entityManager.js +15 -7
  223. package/dist/cluster/internal/entityManager.js.map +1 -1
  224. package/dist/crypto/Aead.d.ts +323 -0
  225. package/dist/crypto/Aead.d.ts.map +1 -0
  226. package/dist/crypto/Aead.js +331 -0
  227. package/dist/crypto/Aead.js.map +1 -0
  228. package/dist/crypto/Cipher.d.ts +236 -0
  229. package/dist/crypto/Cipher.d.ts.map +1 -0
  230. package/dist/crypto/Cipher.js +233 -0
  231. package/dist/crypto/Cipher.js.map +1 -0
  232. package/dist/crypto/CryptoError.d.ts +212 -0
  233. package/dist/crypto/CryptoError.d.ts.map +1 -0
  234. package/dist/crypto/CryptoError.js +202 -0
  235. package/dist/crypto/CryptoError.js.map +1 -0
  236. package/dist/crypto/Digest.d.ts +143 -0
  237. package/dist/crypto/Digest.d.ts.map +1 -0
  238. package/dist/crypto/Digest.js +146 -0
  239. package/dist/crypto/Digest.js.map +1 -0
  240. package/dist/crypto/Kdf.d.ts +220 -0
  241. package/dist/crypto/Kdf.d.ts.map +1 -0
  242. package/dist/crypto/Kdf.js +212 -0
  243. package/dist/crypto/Kdf.js.map +1 -0
  244. package/dist/crypto/KeyAgreement.d.ts +324 -0
  245. package/dist/crypto/KeyAgreement.d.ts.map +1 -0
  246. package/dist/crypto/KeyAgreement.js +303 -0
  247. package/dist/crypto/KeyAgreement.js.map +1 -0
  248. package/dist/crypto/KeyFormat.d.ts +341 -0
  249. package/dist/crypto/KeyFormat.d.ts.map +1 -0
  250. package/dist/crypto/KeyFormat.js +556 -0
  251. package/dist/crypto/KeyFormat.js.map +1 -0
  252. package/dist/crypto/KeyWrap.d.ts +390 -0
  253. package/dist/crypto/KeyWrap.d.ts.map +1 -0
  254. package/dist/crypto/KeyWrap.js +381 -0
  255. package/dist/crypto/KeyWrap.js.map +1 -0
  256. package/dist/crypto/Mac.d.ts +278 -0
  257. package/dist/crypto/Mac.d.ts.map +1 -0
  258. package/dist/crypto/Mac.js +288 -0
  259. package/dist/crypto/Mac.js.map +1 -0
  260. package/dist/crypto/SecureRandom.d.ts +340 -0
  261. package/dist/crypto/SecureRandom.d.ts.map +1 -0
  262. package/dist/crypto/SecureRandom.js +257 -0
  263. package/dist/crypto/SecureRandom.js.map +1 -0
  264. package/dist/crypto/Signature.d.ts +449 -0
  265. package/dist/crypto/Signature.d.ts.map +1 -0
  266. package/dist/crypto/Signature.js +484 -0
  267. package/dist/crypto/Signature.js.map +1 -0
  268. package/dist/crypto/WebCrypto.d.ts +80 -0
  269. package/dist/crypto/WebCrypto.d.ts.map +1 -0
  270. package/dist/crypto/WebCrypto.js +63 -0
  271. package/dist/crypto/WebCrypto.js.map +1 -0
  272. package/dist/crypto/index.d.ts +65 -0
  273. package/dist/crypto/index.d.ts.map +1 -0
  274. package/dist/crypto/index.js +66 -0
  275. package/dist/crypto/index.js.map +1 -0
  276. package/dist/crypto/internal/asymmetric.d.ts +2 -0
  277. package/dist/crypto/internal/asymmetric.d.ts.map +1 -0
  278. package/dist/crypto/internal/asymmetric.js +210 -0
  279. package/dist/crypto/internal/asymmetric.js.map +1 -0
  280. package/dist/crypto/internal/chacha.d.ts +46 -0
  281. package/dist/crypto/internal/chacha.d.ts.map +1 -0
  282. package/dist/crypto/internal/chacha.js +885 -0
  283. package/dist/crypto/internal/chacha.js.map +1 -0
  284. package/dist/crypto/internal/der.d.ts +6 -0
  285. package/dist/crypto/internal/der.d.ts.map +1 -0
  286. package/dist/crypto/internal/der.js +86 -0
  287. package/dist/crypto/internal/der.js.map +1 -0
  288. package/dist/crypto/internal/digest.d.ts +2 -0
  289. package/dist/crypto/internal/digest.d.ts.map +1 -0
  290. package/dist/crypto/internal/digest.js +15 -0
  291. package/dist/crypto/internal/digest.js.map +1 -0
  292. package/dist/crypto/internal/key.d.ts +2 -0
  293. package/dist/crypto/internal/key.d.ts.map +1 -0
  294. package/dist/crypto/internal/key.js +171 -0
  295. package/dist/crypto/internal/key.js.map +1 -0
  296. package/dist/crypto/internal/md5.d.ts +39 -0
  297. package/dist/crypto/internal/md5.d.ts.map +1 -0
  298. package/dist/crypto/internal/md5.js +179 -0
  299. package/dist/crypto/internal/md5.js.map +1 -0
  300. package/dist/crypto/internal/webCrypto.d.ts +2 -0
  301. package/dist/crypto/internal/webCrypto.d.ts.map +1 -0
  302. package/dist/crypto/internal/webCrypto.js +111 -0
  303. package/dist/crypto/internal/webCrypto.js.map +1 -0
  304. package/dist/encoding/Base64Url.d.ts.map +1 -1
  305. package/dist/encoding/Base64Url.js +29 -5
  306. package/dist/encoding/Base64Url.js.map +1 -1
  307. package/dist/encoding/Csv.d.ts +608 -0
  308. package/dist/encoding/Csv.d.ts.map +1 -0
  309. package/dist/encoding/Csv.js +718 -0
  310. package/dist/encoding/Csv.js.map +1 -0
  311. package/dist/encoding/Hex.d.ts +3 -2
  312. package/dist/encoding/Hex.d.ts.map +1 -1
  313. package/dist/encoding/Hex.js +3 -2
  314. package/dist/encoding/Hex.js.map +1 -1
  315. package/dist/encoding/Ndjson.d.ts +5 -4
  316. package/dist/encoding/Ndjson.d.ts.map +1 -1
  317. package/dist/encoding/Ndjson.js +6 -5
  318. package/dist/encoding/Ndjson.js.map +1 -1
  319. package/dist/encoding/SchemaBinary.d.ts.map +1 -1
  320. package/dist/encoding/SchemaBinary.js +42 -4
  321. package/dist/encoding/SchemaBinary.js.map +1 -1
  322. package/dist/encoding/Sse.d.ts +3 -1
  323. package/dist/encoding/Sse.d.ts.map +1 -1
  324. package/dist/encoding/Sse.js +5 -3
  325. package/dist/encoding/Sse.js.map +1 -1
  326. package/dist/encoding/Toml.d.ts +17 -3
  327. package/dist/encoding/Toml.d.ts.map +1 -1
  328. package/dist/encoding/Toml.js +104 -16
  329. package/dist/encoding/Toml.js.map +1 -1
  330. package/dist/encoding/Yaml.d.ts +8 -1
  331. package/dist/encoding/Yaml.d.ts.map +1 -1
  332. package/dist/encoding/Yaml.js +54 -18
  333. package/dist/encoding/Yaml.js.map +1 -1
  334. package/dist/encoding/index.d.ts +5 -0
  335. package/dist/encoding/index.d.ts.map +1 -1
  336. package/dist/encoding/index.js +5 -0
  337. package/dist/encoding/index.js.map +1 -1
  338. package/dist/eventlog/EventJournal.d.ts.map +1 -1
  339. package/dist/eventlog/EventJournal.js +6 -3
  340. package/dist/eventlog/EventJournal.js.map +1 -1
  341. package/dist/eventlog/EventLog.d.ts +126 -4
  342. package/dist/eventlog/EventLog.d.ts.map +1 -1
  343. package/dist/eventlog/EventLog.js +207 -12
  344. package/dist/eventlog/EventLog.js.map +1 -1
  345. package/dist/eventlog/EventLogEncryption.d.ts +41 -4
  346. package/dist/eventlog/EventLogEncryption.d.ts.map +1 -1
  347. package/dist/eventlog/EventLogEncryption.js +83 -64
  348. package/dist/eventlog/EventLogEncryption.js.map +1 -1
  349. package/dist/eventlog/EventLogMessage.d.ts +279 -0
  350. package/dist/eventlog/EventLogMessage.d.ts.map +1 -1
  351. package/dist/eventlog/EventLogMessage.js +217 -1
  352. package/dist/eventlog/EventLogMessage.js.map +1 -1
  353. package/dist/eventlog/EventLogRemote.d.ts +200 -28
  354. package/dist/eventlog/EventLogRemote.d.ts.map +1 -1
  355. package/dist/eventlog/EventLogRemote.js +186 -16
  356. package/dist/eventlog/EventLogRemote.js.map +1 -1
  357. package/dist/eventlog/EventLogServer.d.ts +10 -1
  358. package/dist/eventlog/EventLogServer.d.ts.map +1 -1
  359. package/dist/eventlog/EventLogServer.js +45 -22
  360. package/dist/eventlog/EventLogServer.js.map +1 -1
  361. package/dist/eventlog/EventLogServerEncrypted.d.ts +336 -5
  362. package/dist/eventlog/EventLogServerEncrypted.d.ts.map +1 -1
  363. package/dist/eventlog/EventLogServerEncrypted.js +769 -14
  364. package/dist/eventlog/EventLogServerEncrypted.js.map +1 -1
  365. package/dist/eventlog/EventLogServerUnencrypted.d.ts +17 -0
  366. package/dist/eventlog/EventLogServerUnencrypted.d.ts.map +1 -1
  367. package/dist/eventlog/EventLogServerUnencrypted.js +14 -2
  368. package/dist/eventlog/EventLogServerUnencrypted.js.map +1 -1
  369. package/dist/eventlog/EventLogSessionAuth.d.ts +26 -3
  370. package/dist/eventlog/EventLogSessionAuth.d.ts.map +1 -1
  371. package/dist/eventlog/EventLogSessionAuth.js +63 -64
  372. package/dist/eventlog/EventLogSessionAuth.js.map +1 -1
  373. package/dist/eventlog/EventRow.d.ts +4 -0
  374. package/dist/eventlog/EventRow.d.ts.map +1 -1
  375. package/dist/eventlog/EventRow.js +12 -2
  376. package/dist/eventlog/EventRow.js.map +1 -1
  377. package/dist/eventlog/SqlEventJournal.js +2 -2
  378. package/dist/eventlog/SqlEventJournal.js.map +1 -1
  379. package/dist/eventlog/SqlEventLogServerEncrypted.d.ts +21 -5
  380. package/dist/eventlog/SqlEventLogServerEncrypted.d.ts.map +1 -1
  381. package/dist/eventlog/SqlEventLogServerEncrypted.js +1048 -113
  382. package/dist/eventlog/SqlEventLogServerEncrypted.js.map +1 -1
  383. package/dist/eventlog/SqlEventLogServerUnencrypted.js +2 -2
  384. package/dist/eventlog/SqlEventLogServerUnencrypted.js.map +1 -1
  385. package/dist/eventlog/internal/batches.d.ts +12 -0
  386. package/dist/eventlog/internal/batches.d.ts.map +1 -0
  387. package/dist/eventlog/internal/batches.js +26 -0
  388. package/dist/eventlog/internal/batches.js.map +1 -0
  389. package/dist/eventlog/internal/compaction.d.ts +38 -0
  390. package/dist/eventlog/internal/compaction.d.ts.map +1 -0
  391. package/dist/eventlog/internal/compaction.js +33 -0
  392. package/dist/eventlog/internal/compaction.js.map +1 -0
  393. package/dist/eventlog/internal/identityRootSecretDerivation.js +66 -67
  394. package/dist/eventlog/internal/identityRootSecretDerivation.js.map +1 -1
  395. package/dist/eventlog/internal/remoteCompaction.d.ts +31 -0
  396. package/dist/eventlog/internal/remoteCompaction.d.ts.map +1 -0
  397. package/dist/eventlog/internal/remoteCompaction.js +76 -0
  398. package/dist/eventlog/internal/remoteCompaction.js.map +1 -0
  399. package/dist/graphql/GraphQLClient.d.ts +3 -1
  400. package/dist/graphql/GraphQLClient.d.ts.map +1 -1
  401. package/dist/graphql/GraphQLClient.js.map +1 -1
  402. package/dist/graphql/GraphQLClientError.d.ts +14 -9
  403. package/dist/graphql/GraphQLClientError.d.ts.map +1 -1
  404. package/dist/graphql/GraphQLClientError.js +14 -21
  405. package/dist/graphql/GraphQLClientError.js.map +1 -1
  406. package/dist/graphql/GraphQLProtocol.d.ts +12 -3
  407. package/dist/graphql/GraphQLProtocol.d.ts.map +1 -1
  408. package/dist/graphql/GraphQLProtocol.js +4 -1
  409. package/dist/graphql/GraphQLProtocol.js.map +1 -1
  410. package/dist/http/Cookies.d.ts +172 -0
  411. package/dist/http/Cookies.d.ts.map +1 -1
  412. package/dist/http/Cookies.js +143 -1
  413. package/dist/http/Cookies.js.map +1 -1
  414. package/dist/http/FetchHttpClient.d.ts +26 -2
  415. package/dist/http/FetchHttpClient.d.ts.map +1 -1
  416. package/dist/http/FetchHttpClient.js +78 -15
  417. package/dist/http/FetchHttpClient.js.map +1 -1
  418. package/dist/http/HttpClient.d.ts +268 -15
  419. package/dist/http/HttpClient.d.ts.map +1 -1
  420. package/dist/http/HttpClient.js +140 -7
  421. package/dist/http/HttpClient.js.map +1 -1
  422. package/dist/http/HttpClientRequest.d.ts +9 -5
  423. package/dist/http/HttpClientRequest.d.ts.map +1 -1
  424. package/dist/http/HttpClientRequest.js +2 -3
  425. package/dist/http/HttpClientRequest.js.map +1 -1
  426. package/dist/http/HttpClientResponse.d.ts.map +1 -1
  427. package/dist/http/HttpClientResponse.js +3 -2
  428. package/dist/http/HttpClientResponse.js.map +1 -1
  429. package/dist/http/HttpMethod.d.ts +7 -1
  430. package/dist/http/HttpMethod.d.ts.map +1 -1
  431. package/dist/http/HttpMethod.js +3 -8
  432. package/dist/http/HttpMethod.js.map +1 -1
  433. package/dist/http/HttpServerRequest.d.ts +5 -2
  434. package/dist/http/HttpServerRequest.d.ts.map +1 -1
  435. package/dist/http/HttpServerRequest.js +44 -39
  436. package/dist/http/HttpServerRequest.js.map +1 -1
  437. package/dist/http/HttpServerResponse.d.ts.map +1 -1
  438. package/dist/http/HttpServerResponse.js +3 -2
  439. package/dist/http/HttpServerResponse.js.map +1 -1
  440. package/dist/http/HttpTraceContext.d.ts +2 -1
  441. package/dist/http/HttpTraceContext.d.ts.map +1 -1
  442. package/dist/http/HttpTraceContext.js +7 -5
  443. package/dist/http/HttpTraceContext.js.map +1 -1
  444. package/dist/http/ProblemDetails.d.ts +96 -0
  445. package/dist/http/ProblemDetails.d.ts.map +1 -0
  446. package/dist/http/ProblemDetails.js +98 -0
  447. package/dist/http/ProblemDetails.js.map +1 -0
  448. package/dist/http/index.d.ts +5 -0
  449. package/dist/http/index.d.ts.map +1 -1
  450. package/dist/http/index.js +5 -0
  451. package/dist/http/index.js.map +1 -1
  452. package/dist/http/internal/decompression.js +3 -2
  453. package/dist/http/internal/decompression.js.map +1 -1
  454. package/dist/http/internal/mimeTypes.d.ts +4 -1
  455. package/dist/http/internal/mimeTypes.d.ts.map +1 -1
  456. package/dist/http/internal/mimeTypes.js +4 -1
  457. package/dist/http/internal/mimeTypes.js.map +1 -1
  458. package/dist/http/internal/urlParams.js +8 -0
  459. package/dist/http/internal/urlParams.js.map +1 -1
  460. package/dist/http-api/HttpApiBuilder.js +19 -5
  461. package/dist/http-api/HttpApiBuilder.js.map +1 -1
  462. package/dist/http-api/HttpApiClient.d.ts +63 -0
  463. package/dist/http-api/HttpApiClient.d.ts.map +1 -1
  464. package/dist/http-api/HttpApiClient.js +51 -0
  465. package/dist/http-api/HttpApiClient.js.map +1 -1
  466. package/dist/http-api/HttpApiEndpoint.d.ts.map +1 -1
  467. package/dist/http-api/HttpApiEndpoint.js +14 -5
  468. package/dist/http-api/HttpApiEndpoint.js.map +1 -1
  469. package/dist/http-api/HttpApiError.d.ts +185 -13
  470. package/dist/http-api/HttpApiError.d.ts.map +1 -1
  471. package/dist/http-api/HttpApiError.js +128 -0
  472. package/dist/http-api/HttpApiError.js.map +1 -1
  473. package/dist/http-api/HttpApiMiddleware.d.ts +64 -17
  474. package/dist/http-api/HttpApiMiddleware.d.ts.map +1 -1
  475. package/dist/http-api/HttpApiMiddleware.js +85 -1
  476. package/dist/http-api/HttpApiMiddleware.js.map +1 -1
  477. package/dist/http-api/HttpApiSchema.d.ts +76 -14
  478. package/dist/http-api/HttpApiSchema.d.ts.map +1 -1
  479. package/dist/http-api/HttpApiSchema.js +54 -1
  480. package/dist/http-api/HttpApiSchema.js.map +1 -1
  481. package/dist/index.d.ts +5 -5
  482. package/dist/index.d.ts.map +1 -1
  483. package/dist/index.js +5 -5
  484. package/dist/index.js.map +1 -1
  485. package/dist/internal/dateTime.js +49 -24
  486. package/dist/internal/dateTime.js.map +1 -1
  487. package/dist/internal/effect.js +35 -14
  488. package/dist/internal/effect.js.map +1 -1
  489. package/dist/internal/glob.d.ts +2 -0
  490. package/dist/internal/glob.d.ts.map +1 -0
  491. package/dist/internal/glob.js +808 -0
  492. package/dist/internal/glob.js.map +1 -0
  493. package/dist/internal/graphql.js +21 -7
  494. package/dist/internal/graphql.js.map +1 -1
  495. package/dist/internal/graphqlHeaders.d.ts +7 -0
  496. package/dist/internal/graphqlHeaders.d.ts.map +1 -1
  497. package/dist/internal/graphqlHeaders.js +11 -0
  498. package/dist/internal/graphqlHeaders.js.map +1 -1
  499. package/dist/internal/matcher.js +10 -0
  500. package/dist/internal/matcher.js.map +1 -1
  501. package/dist/internal/persistence.js +9 -0
  502. package/dist/internal/persistence.js.map +1 -1
  503. package/dist/internal/rcRef.js +4 -1
  504. package/dist/internal/rcRef.js.map +1 -1
  505. package/dist/internal/request.js +29 -42
  506. package/dist/internal/request.js.map +1 -1
  507. package/dist/internal/schema/fromJsonSchemaDocument.js +7 -8
  508. package/dist/internal/schema/fromJsonSchemaDocument.js.map +1 -1
  509. package/dist/internal/schema/toCodeDocument.js +6 -3
  510. package/dist/internal/schema/toCodeDocument.js.map +1 -1
  511. package/dist/internal/trie.js +42 -13
  512. package/dist/internal/trie.js.map +1 -1
  513. package/dist/internal/ulid.js +41 -0
  514. package/dist/internal/ulid.js.map +1 -1
  515. package/dist/internal/uuid.js +52 -8
  516. package/dist/internal/uuid.js.map +1 -1
  517. package/dist/internal/version.js +1 -1
  518. package/dist/jose/JoseError.d.ts +285 -0
  519. package/dist/jose/JoseError.d.ts.map +1 -0
  520. package/dist/jose/JoseError.js +269 -0
  521. package/dist/jose/JoseError.js.map +1 -0
  522. package/dist/jose/Jwe.d.ts +128 -0
  523. package/dist/jose/Jwe.d.ts.map +1 -0
  524. package/dist/jose/Jwe.js +134 -0
  525. package/dist/jose/Jwe.js.map +1 -0
  526. package/dist/jose/Jwk.d.ts +597 -0
  527. package/dist/jose/Jwk.d.ts.map +1 -0
  528. package/dist/jose/Jwk.js +503 -0
  529. package/dist/jose/Jwk.js.map +1 -0
  530. package/dist/jose/Jwks.d.ts +236 -0
  531. package/dist/jose/Jwks.d.ts.map +1 -0
  532. package/dist/jose/Jwks.js +306 -0
  533. package/dist/jose/Jwks.js.map +1 -0
  534. package/dist/jose/Jws.d.ts +177 -0
  535. package/dist/jose/Jws.d.ts.map +1 -0
  536. package/dist/jose/Jws.js +183 -0
  537. package/dist/jose/Jws.js.map +1 -0
  538. package/dist/jose/Jwt.d.ts +270 -0
  539. package/dist/jose/Jwt.d.ts.map +1 -0
  540. package/dist/jose/Jwt.js +293 -0
  541. package/dist/jose/Jwt.js.map +1 -0
  542. package/dist/jose/index.d.ts +51 -0
  543. package/dist/jose/index.d.ts.map +1 -0
  544. package/dist/jose/index.js +52 -0
  545. package/dist/jose/index.js.map +1 -0
  546. package/dist/jose/internal/compact.d.ts +2 -0
  547. package/dist/jose/internal/compact.d.ts.map +1 -0
  548. package/dist/jose/internal/compact.js +179 -0
  549. package/dist/jose/internal/compact.js.map +1 -0
  550. package/dist/net/Host.d.ts.map +1 -1
  551. package/dist/net/Host.js +5 -10
  552. package/dist/net/Host.js.map +1 -1
  553. package/dist/observability/Otlp.d.ts +15 -4
  554. package/dist/observability/Otlp.d.ts.map +1 -1
  555. package/dist/observability/Otlp.js +10 -0
  556. package/dist/observability/Otlp.js.map +1 -1
  557. package/dist/observability/OtlpResource.d.ts +7 -0
  558. package/dist/observability/OtlpResource.d.ts.map +1 -1
  559. package/dist/observability/OtlpResource.js +11 -1
  560. package/dist/observability/OtlpResource.js.map +1 -1
  561. package/dist/observability/OtlpSerialization.d.ts +1 -1
  562. package/dist/observability/OtlpSerialization.d.ts.map +1 -1
  563. package/dist/observability/OtlpSerialization.js +10 -4
  564. package/dist/observability/OtlpSerialization.js.map +1 -1
  565. package/dist/observability/internal/otlpProtobuf.js +3 -3
  566. package/dist/observability/internal/otlpProtobuf.js.map +1 -1
  567. package/dist/observability/internal/protobuf.js +6 -0
  568. package/dist/observability/internal/protobuf.js.map +1 -1
  569. package/dist/persistence/CircuitBreaker.d.ts +98 -38
  570. package/dist/persistence/CircuitBreaker.d.ts.map +1 -1
  571. package/dist/persistence/CircuitBreaker.js +67 -29
  572. package/dist/persistence/CircuitBreaker.js.map +1 -1
  573. package/dist/persistence/KeyValueStore.d.ts +47 -2
  574. package/dist/persistence/KeyValueStore.d.ts.map +1 -1
  575. package/dist/persistence/KeyValueStore.js +129 -18
  576. package/dist/persistence/KeyValueStore.js.map +1 -1
  577. package/dist/persistence/PersistedQueue.d.ts +9 -0
  578. package/dist/persistence/PersistedQueue.d.ts.map +1 -1
  579. package/dist/persistence/PersistedQueue.js +94 -52
  580. package/dist/persistence/PersistedQueue.js.map +1 -1
  581. package/dist/persistence/Persistence.d.ts +10 -4
  582. package/dist/persistence/Persistence.d.ts.map +1 -1
  583. package/dist/persistence/Persistence.js +18 -11
  584. package/dist/persistence/Persistence.js.map +1 -1
  585. package/dist/persistence/RateLimiter.d.ts +8 -0
  586. package/dist/persistence/RateLimiter.d.ts.map +1 -1
  587. package/dist/persistence/RateLimiter.js +63 -12
  588. package/dist/persistence/RateLimiter.js.map +1 -1
  589. package/dist/persistence/index.d.ts +1 -1
  590. package/dist/persistence/index.js +1 -1
  591. package/dist/process/ChildProcess.d.ts +207 -67
  592. package/dist/process/ChildProcess.d.ts.map +1 -1
  593. package/dist/process/ChildProcess.js +56 -158
  594. package/dist/process/ChildProcess.js.map +1 -1
  595. package/dist/process/ChildProcessSpawner.d.ts +5 -0
  596. package/dist/process/ChildProcessSpawner.d.ts.map +1 -1
  597. package/dist/process/ChildProcessSpawner.js.map +1 -1
  598. package/dist/process/CommandLine.d.ts +294 -0
  599. package/dist/process/CommandLine.d.ts.map +1 -0
  600. package/dist/process/CommandLine.js +481 -0
  601. package/dist/process/CommandLine.js.map +1 -0
  602. package/dist/process/index.d.ts +5 -0
  603. package/dist/process/index.d.ts.map +1 -1
  604. package/dist/process/index.js +5 -0
  605. package/dist/process/index.js.map +1 -1
  606. package/dist/reactivity/Atom.d.ts +2 -1
  607. package/dist/reactivity/Atom.d.ts.map +1 -1
  608. package/dist/reactivity/Atom.js +3 -4
  609. package/dist/reactivity/Atom.js.map +1 -1
  610. package/dist/reactivity/AtomRegistry.js +17 -7
  611. package/dist/reactivity/AtomRegistry.js.map +1 -1
  612. package/dist/rpc/Rpc.d.ts +1 -1
  613. package/dist/rpc/Rpc.d.ts.map +1 -1
  614. package/dist/rpc/Rpc.js.map +1 -1
  615. package/dist/rpc/RpcClient.d.ts +28 -1
  616. package/dist/rpc/RpcClient.d.ts.map +1 -1
  617. package/dist/rpc/RpcClient.js +78 -27
  618. package/dist/rpc/RpcClient.js.map +1 -1
  619. package/dist/rpc/RpcMessage.d.ts +13 -3
  620. package/dist/rpc/RpcMessage.d.ts.map +1 -1
  621. package/dist/rpc/RpcMessage.js +1 -0
  622. package/dist/rpc/RpcMessage.js.map +1 -1
  623. package/dist/rpc/RpcSerialization.d.ts.map +1 -1
  624. package/dist/rpc/RpcSerialization.js +6 -0
  625. package/dist/rpc/RpcSerialization.js.map +1 -1
  626. package/dist/rpc/RpcServer.d.ts +31 -0
  627. package/dist/rpc/RpcServer.d.ts.map +1 -1
  628. package/dist/rpc/RpcServer.js +139 -66
  629. package/dist/rpc/RpcServer.js.map +1 -1
  630. package/dist/schema/Model.d.ts +14 -0
  631. package/dist/schema/Model.d.ts.map +1 -1
  632. package/dist/schema/Model.js +18 -3
  633. package/dist/schema/Model.js.map +1 -1
  634. package/dist/sql/Migrator.d.ts +9 -20
  635. package/dist/sql/Migrator.d.ts.map +1 -1
  636. package/dist/sql/Migrator.js +20 -53
  637. package/dist/sql/Migrator.js.map +1 -1
  638. package/dist/sql/MigratorFileSystem.d.ts +21 -0
  639. package/dist/sql/MigratorFileSystem.d.ts.map +1 -0
  640. package/dist/sql/MigratorFileSystem.js +57 -0
  641. package/dist/sql/MigratorFileSystem.js.map +1 -0
  642. package/dist/sql/SqlClient.d.ts +38 -3
  643. package/dist/sql/SqlClient.d.ts.map +1 -1
  644. package/dist/sql/SqlClient.js +9 -12
  645. package/dist/sql/SqlClient.js.map +1 -1
  646. package/dist/sql/Statement.d.ts +61 -9
  647. package/dist/sql/Statement.d.ts.map +1 -1
  648. package/dist/sql/Statement.js +24 -33
  649. package/dist/sql/Statement.js.map +1 -1
  650. package/dist/sql/index.js +1 -1
  651. package/dist/testing/index.d.ts +0 -5
  652. package/dist/testing/index.d.ts.map +1 -1
  653. package/dist/testing/index.js +0 -5
  654. package/dist/testing/index.js.map +1 -1
  655. package/dist/workflow/Workflow.d.ts +4 -0
  656. package/dist/workflow/Workflow.d.ts.map +1 -1
  657. package/dist/workflow/Workflow.js.map +1 -1
  658. package/dist/workflow/WorkflowEngine.d.ts.map +1 -1
  659. package/dist/workflow/WorkflowEngine.js +5 -0
  660. package/dist/workflow/WorkflowEngine.js.map +1 -1
  661. package/dist/workflow/internal/crypto.js +9 -8
  662. package/dist/workflow/internal/crypto.js.map +1 -1
  663. package/package.json +7 -1
  664. package/src/BigDecimal.ts +173 -16
  665. package/src/Channel.ts +17 -11
  666. package/src/Config.ts +7 -1
  667. package/src/Context.ts +34 -10
  668. package/src/Cron.ts +178 -29
  669. package/src/Duration.ts +140 -0
  670. package/src/Effect.ts +914 -85
  671. package/src/Equal.ts +3 -3
  672. package/src/FileSystem.ts +65 -4
  673. package/src/Glob.ts +205 -0
  674. package/src/Hash.ts +5 -4
  675. package/src/Iterable.ts +8 -5
  676. package/src/Match.ts +127 -0
  677. package/src/Path.ts +6 -1
  678. package/src/Pool.ts +75 -16
  679. package/src/Random.ts +3 -3
  680. package/src/RcMap.ts +5 -1
  681. package/src/Schedule.ts +26 -3
  682. package/src/Schema.ts +1191 -63
  683. package/src/SchemaGetter.ts +27 -11
  684. package/src/SchemaRepresentation.ts +23 -2
  685. package/src/SchemaTransformation.ts +10 -5
  686. package/src/ScopedCache.ts +5 -9
  687. package/src/Semaphore.ts +141 -0
  688. package/src/Stream.ts +15 -6
  689. package/src/Terminal.ts +19 -1
  690. package/src/ai/AiError.ts +56 -80
  691. package/src/ai/Decision.ts +257 -6
  692. package/src/ai/DecisionModel.ts +171 -28
  693. package/src/ai/McpClient.ts +488 -91
  694. package/src/ai/McpSchema.ts +119 -0
  695. package/src/ai/McpServer.ts +1525 -319
  696. package/src/ai/McpTest.ts +380 -0
  697. package/src/ai/Prompt.ts +16 -11
  698. package/src/ai/Response.ts +2 -22
  699. package/src/ai/Tool.ts +32 -18
  700. package/src/ai/index.ts +6 -0
  701. package/src/ai/internal/mcpCore.ts +243 -56
  702. package/src/ai/internal/mcpProtocol/v2024_11_05.ts +7 -8
  703. package/src/ai/internal/mcpProtocol/v2025_03_26.ts +7 -8
  704. package/src/ai/internal/mcpProtocol/v2025_06_18.ts +7 -8
  705. package/src/ai/internal/mcpProtocol/v2025_11_25.ts +7 -8
  706. package/src/ai/internal/mcpProtocol/v2026_07_28.ts +56 -17
  707. package/src/ai/internal/mcpProtocol.ts +44 -0
  708. package/src/ai/internal/mcpRuntime.ts +15 -8
  709. package/src/ai/internal/mcpSkills.ts +480 -0
  710. package/src/ai/internal/mcpStatefulRuntime.ts +14 -2
  711. package/src/cli/Argument.ts +6 -0
  712. package/src/cli/CliOutput.ts +117 -49
  713. package/src/cli/Command.ts +23 -0
  714. package/src/cli/Flag.ts +107 -0
  715. package/src/cli/HelpDoc.ts +18 -1
  716. package/src/cli/Param.ts +6 -0
  717. package/src/cli/Primitive.ts +16 -1
  718. package/src/cli/Prompt.ts +1 -0
  719. package/src/cli/internal/help.ts +21 -9
  720. package/src/cluster/ClusterWorkflowEngine.ts +22 -8
  721. package/src/cluster/Reply.ts +1 -1
  722. package/src/cluster/Sharding.ts +96 -26
  723. package/src/cluster/ShardingConfig.ts +41 -3
  724. package/src/cluster/SingleRunner.ts +3 -3
  725. package/src/cluster/SqlMessageStorage.ts +49 -17
  726. package/src/cluster/SqlRunnerStorage.ts +37 -27
  727. package/src/cluster/internal/entityManager.ts +24 -10
  728. package/src/crypto/Aead.ts +517 -0
  729. package/src/crypto/Cipher.ts +326 -0
  730. package/src/crypto/CryptoError.ts +242 -0
  731. package/src/crypto/Digest.ts +167 -0
  732. package/src/crypto/Kdf.ts +315 -0
  733. package/src/crypto/KeyAgreement.ts +489 -0
  734. package/src/crypto/KeyFormat.ts +741 -0
  735. package/src/crypto/KeyWrap.ts +639 -0
  736. package/src/crypto/Mac.ts +386 -0
  737. package/src/crypto/SecureRandom.ts +459 -0
  738. package/src/crypto/Signature.ts +673 -0
  739. package/src/crypto/WebCrypto.ts +84 -0
  740. package/src/crypto/index.ts +78 -0
  741. package/src/crypto/internal/asymmetric.ts +330 -0
  742. package/src/crypto/internal/chacha.ts +1088 -0
  743. package/src/crypto/internal/der.ts +110 -0
  744. package/src/crypto/internal/digest.ts +16 -0
  745. package/src/crypto/internal/key.ts +242 -0
  746. package/src/crypto/internal/md5.ts +170 -0
  747. package/src/crypto/internal/webCrypto.ts +150 -0
  748. package/src/encoding/Base64Url.ts +32 -5
  749. package/src/encoding/Csv.ts +1298 -0
  750. package/src/encoding/Hex.ts +3 -2
  751. package/src/encoding/Ndjson.ts +6 -5
  752. package/src/encoding/SchemaBinary.ts +40 -4
  753. package/src/encoding/Sse.ts +5 -3
  754. package/src/encoding/Toml.ts +85 -20
  755. package/src/encoding/Yaml.ts +61 -18
  756. package/src/encoding/index.ts +6 -0
  757. package/src/eventlog/EventJournal.ts +4 -3
  758. package/src/eventlog/EventLog.ts +347 -15
  759. package/src/eventlog/EventLogEncryption.ts +118 -110
  760. package/src/eventlog/EventLogMessage.ts +273 -1
  761. package/src/eventlog/EventLogRemote.ts +367 -23
  762. package/src/eventlog/EventLogServer.ts +54 -20
  763. package/src/eventlog/EventLogServerEncrypted.ts +1135 -67
  764. package/src/eventlog/EventLogServerUnencrypted.ts +19 -2
  765. package/src/eventlog/EventLogSessionAuth.ts +82 -70
  766. package/src/eventlog/EventRow.ts +12 -3
  767. package/src/eventlog/SqlEventJournal.ts +2 -2
  768. package/src/eventlog/SqlEventLogServerEncrypted.ts +1244 -136
  769. package/src/eventlog/SqlEventLogServerUnencrypted.ts +2 -2
  770. package/src/eventlog/internal/batches.ts +30 -0
  771. package/src/eventlog/internal/compaction.ts +64 -0
  772. package/src/eventlog/internal/identityRootSecretDerivation.ts +93 -123
  773. package/src/eventlog/internal/remoteCompaction.ts +92 -0
  774. package/src/graphql/GraphQLClient.ts +3 -1
  775. package/src/graphql/GraphQLClientError.ts +15 -22
  776. package/src/graphql/GraphQLProtocol.ts +12 -3
  777. package/src/http/Cookies.ts +281 -1
  778. package/src/http/FetchHttpClient.ts +88 -16
  779. package/src/http/HttpClient.ts +299 -33
  780. package/src/http/HttpClientRequest.ts +12 -9
  781. package/src/http/HttpClientResponse.ts +3 -2
  782. package/src/http/HttpMethod.ts +10 -2
  783. package/src/http/HttpServerRequest.ts +76 -53
  784. package/src/http/HttpServerResponse.ts +3 -2
  785. package/src/http/HttpTraceContext.ts +7 -4
  786. package/src/http/ProblemDetails.ts +149 -0
  787. package/src/http/index.ts +6 -0
  788. package/src/http/internal/decompression.ts +4 -4
  789. package/src/http/internal/mimeTypes.ts +4 -1
  790. package/src/http/internal/urlParams.ts +8 -0
  791. package/src/http-api/HttpApiBuilder.ts +24 -12
  792. package/src/http-api/HttpApiClient.ts +67 -1
  793. package/src/http-api/HttpApiEndpoint.ts +14 -5
  794. package/src/http-api/HttpApiError.ts +250 -0
  795. package/src/http-api/HttpApiMiddleware.ts +95 -1
  796. package/src/http-api/HttpApiSchema.ts +106 -15
  797. package/src/index.ts +6 -6
  798. package/src/internal/dateTime.ts +54 -24
  799. package/src/internal/effect.ts +40 -18
  800. package/src/internal/glob.ts +952 -0
  801. package/src/internal/graphql.ts +27 -9
  802. package/src/internal/graphqlHeaders.ts +13 -0
  803. package/src/internal/matcher.ts +17 -0
  804. package/src/internal/persistence.ts +11 -0
  805. package/src/internal/rcRef.ts +4 -1
  806. package/src/internal/request.ts +51 -58
  807. package/src/internal/schema/fromJsonSchemaDocument.ts +11 -10
  808. package/src/internal/schema/toCodeDocument.ts +7 -3
  809. package/src/internal/trie.ts +43 -14
  810. package/src/internal/ulid.ts +44 -0
  811. package/src/internal/uuid.ts +57 -16
  812. package/src/internal/version.ts +1 -1
  813. package/src/jose/JoseError.ts +329 -0
  814. package/src/jose/Jwe.ts +196 -0
  815. package/src/jose/Jwk.ts +777 -0
  816. package/src/jose/Jwks.ts +418 -0
  817. package/src/jose/Jws.ts +312 -0
  818. package/src/jose/Jwt.ts +493 -0
  819. package/src/jose/index.ts +58 -0
  820. package/src/jose/internal/compact.ts +195 -0
  821. package/src/net/Host.ts +5 -10
  822. package/src/observability/Otlp.ts +21 -4
  823. package/src/observability/OtlpResource.ts +9 -1
  824. package/src/observability/OtlpSerialization.ts +13 -5
  825. package/src/observability/internal/otlpProtobuf.ts +6 -6
  826. package/src/observability/internal/protobuf.ts +10 -0
  827. package/src/persistence/CircuitBreaker.ts +113 -48
  828. package/src/persistence/KeyValueStore.ts +219 -22
  829. package/src/persistence/PersistedQueue.ts +128 -59
  830. package/src/persistence/Persistence.ts +20 -13
  831. package/src/persistence/RateLimiter.ts +84 -15
  832. package/src/persistence/index.ts +1 -1
  833. package/src/process/ChildProcess.ts +255 -260
  834. package/src/process/ChildProcessSpawner.ts +5 -0
  835. package/src/process/CommandLine.ts +630 -0
  836. package/src/process/index.ts +6 -0
  837. package/src/reactivity/Atom.ts +5 -5
  838. package/src/reactivity/AtomRegistry.ts +20 -9
  839. package/src/rpc/Rpc.ts +3 -1
  840. package/src/rpc/RpcClient.ts +94 -38
  841. package/src/rpc/RpcMessage.ts +13 -3
  842. package/src/rpc/RpcSerialization.ts +3 -0
  843. package/src/rpc/RpcServer.ts +176 -78
  844. package/src/schema/Model.ts +26 -3
  845. package/src/sql/Migrator.ts +38 -80
  846. package/src/sql/MigratorFileSystem.ts +71 -0
  847. package/src/sql/SqlClient.ts +45 -24
  848. package/src/sql/Statement.ts +60 -29
  849. package/src/sql/index.ts +1 -1
  850. package/src/testing/index.ts +0 -6
  851. package/src/workflow/Workflow.ts +4 -0
  852. package/src/workflow/WorkflowEngine.ts +5 -0
  853. package/src/workflow/internal/crypto.ts +15 -11
  854. package/dist/Crypto.d.ts +0 -244
  855. package/dist/Crypto.d.ts.map +0 -1
  856. package/dist/Crypto.js +0 -139
  857. package/dist/Crypto.js.map +0 -1
  858. package/dist/testing/TestCrypto.d.ts +0 -73
  859. package/dist/testing/TestCrypto.d.ts.map +0 -1
  860. package/dist/testing/TestCrypto.js +0 -87
  861. package/dist/testing/TestCrypto.js.map +0 -1
  862. package/src/Crypto.ts +0 -347
  863. package/src/testing/TestCrypto.ts +0 -94
@@ -0,0 +1,1088 @@
1
+ /**
2
+ * ChaCha20-Poly1305 (RFC 8439) and XChaCha20-Poly1305
3
+ * (draft-irtf-cfrg-xchacha-03), vendored from `@noble/ciphers`.
4
+ *
5
+ * Upstream: https://github.com/paulmillr/noble-ciphers
6
+ * Version: 2.4.0, commit d9e8a6a599e7ed729d9be03854c46a3c73bd9a79
7
+ * Files: `src/chacha.ts` (`chachaCore`, `hchacha`, `chacha20`, `xchacha20`,
8
+ * `_poly1305_aead`), `src/_arx.ts` (`createCipher`), `src/_poly1305.ts`
9
+ * (`Poly1305`), and the helpers they use from `src/utils.ts`. `@noble/ciphers`
10
+ * 1.0.0 was audited by Cure53 in 2024.
11
+ *
12
+ * Changes from upstream: merged into one file, removed the `TArg`/`TRet` type
13
+ * helpers, `@example` blocks, the reduced-round and original ChaCha variants,
14
+ * the PRGs, and the `wrapCipher` argument wrapper, whose checks the `Aead`
15
+ * layers run before calling this code. `Poly1305` is constructed directly
16
+ * instead of through `wrapMacConstructor`, and `decrypt` returns `undefined`
17
+ * on a tag mismatch instead of throwing, so any exception is a bug. The
18
+ * cipher core, Poly1305 arithmetic, and AEAD construction are unchanged.
19
+ *
20
+ * The MIT License (MIT)
21
+ *
22
+ * Copyright (c) 2022 Paul Miller (https://paulmillr.com)
23
+ * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
24
+ *
25
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
26
+ * of this software and associated documentation files (the "Software"), to deal
27
+ * in the Software without restriction, including without limitation the rights
28
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29
+ * copies of the Software, and to permit persons to whom the Software is
30
+ * furnished to do so, subject to the following conditions:
31
+ *
32
+ * The above copyright notice and this permission notice shall be included in
33
+ * all copies or substantial portions of the Software.
34
+ *
35
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41
+ * THE SOFTWARE.
42
+ *
43
+ * @internal
44
+ */
45
+
46
+ /**
47
+ * A ChaCha AEAD bound to a key, nonce, and additional data. `encrypt` returns
48
+ * `ciphertext‖tag`, and `decrypt` returns `undefined` when the tag does not
49
+ * match.
50
+ *
51
+ * @internal
52
+ */
53
+ export interface Aead {
54
+ encrypt(plaintext: Uint8Array, output?: Uint8Array): Uint8Array
55
+ decrypt(ciphertext: Uint8Array, output?: Uint8Array): Uint8Array | undefined
56
+ }
57
+
58
+ // utils.ts
59
+ type TypedArray =
60
+ | Int8Array
61
+ | Uint8ClampedArray
62
+ | Uint8Array
63
+ | Uint16Array
64
+ | Int16Array
65
+ | Uint32Array
66
+ | Int32Array
67
+
68
+ function isBytes(a: unknown): a is Uint8Array {
69
+ // Plain `instanceof Uint8Array` is too strict for some Buffer / proxy /
70
+ // cross-realm cases. The fallback still requires a real ArrayBuffer view
71
+ // so plain JSON-deserialized `{ constructor: ... }`
72
+ // spoofing is rejected, and `BYTES_PER_ELEMENT === 1` keeps the fallback on byte-oriented views.
73
+ return (
74
+ a instanceof Uint8Array ||
75
+ (ArrayBuffer.isView(a) &&
76
+ a.constructor.name === "Uint8Array" &&
77
+ "BYTES_PER_ELEMENT" in a &&
78
+ a.BYTES_PER_ELEMENT === 1)
79
+ )
80
+ }
81
+
82
+ const atitle = (title: string): string => (title ? `"${title}" ` : "")
83
+
84
+ function abool(value: boolean, title: string = ""): boolean {
85
+ if (typeof value !== "boolean") {
86
+ throw new TypeError(atitle(title) + "expected boolean, got type=" + typeof value)
87
+ }
88
+ return value
89
+ }
90
+
91
+ function anumber(n: number, title: string = ""): number {
92
+ if (typeof n !== "number") {
93
+ throw new TypeError(atitle(title) + "expected number, got " + typeof n)
94
+ }
95
+ if (!Number.isSafeInteger(n) || n < 0) {
96
+ throw new RangeError(atitle(title) + "expected integer >= 0, got " + n)
97
+ }
98
+ return n
99
+ }
100
+
101
+ function abytes(
102
+ value: Uint8Array,
103
+ length?: number,
104
+ title: string = ""
105
+ ): Uint8Array {
106
+ // Success path first: this runs at the start of every update() / digestInto(), and the
107
+ // common `abytes(data)` form must not pay for length handling it does not use.
108
+ if (isBytes(value) && (length === undefined || value.length === length)) {
109
+ return value
110
+ }
111
+ // Error path: recompute freely to build the exact message.
112
+ if (length !== undefined) anumber(length, "length")
113
+ const bytes = isBytes(value)
114
+ const ofLen = length !== undefined ? ` of length ${length}` : ""
115
+ const got = bytes ? `length=${value.length}` : `type=${typeof value}`
116
+ const message = atitle(title) + "expected Uint8Array" + ofLen + ", got " + got
117
+ if (!bytes) throw new TypeError(message)
118
+ throw new RangeError(message)
119
+ }
120
+
121
+ const aobject = (value: Record<string, any>, label: string) => {
122
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
123
+ throw new TypeError(
124
+ label === "object"
125
+ ? "expected valid options object"
126
+ : `"${label}" expected object, got type=${typeof value}`
127
+ )
128
+ }
129
+ }
130
+
131
+ type EmptyObj = {}
132
+ function checkOpts<T1 extends EmptyObj, T2 extends EmptyObj>(
133
+ defaults: T1,
134
+ opts: T2
135
+ ): T1 & T2 {
136
+ aobject(defaults as Record<string, any>, "defaults")
137
+ aobject(opts as Record<string, any>, "opts")
138
+ // Mutates defaults by design. __proto__ follows Object.assign semantics here and can only
139
+ // affect this local option object; callers already control this low-level options surface.
140
+ const merged = Object.assign(defaults, opts)
141
+ return merged as T1 & T2
142
+ }
143
+
144
+ function aexists(instance: any, checkFinished = true): void {
145
+ // Runs on every update()/digestInto(); the flags are library-owned booleans, so only their
146
+ // truthiness is checked - re-validating their type per call was pure hot-path overhead.
147
+ if (instance.destroyed) throw new Error("hash was destroyed")
148
+ if (checkFinished && instance.finished) throw new Error("digest() was already called")
149
+ }
150
+
151
+ function aoutput(out: any, instance: any): void {
152
+ abytes(out, undefined, "output")
153
+ // `outputLen` is a library-owned readonly number; the negated comparison keeps failing fast
154
+ // when it is missing/NaN (comparisons with undefined/NaN are false) without an anumber() call.
155
+ const min = instance.outputLen
156
+ if (!(out.length >= min)) {
157
+ throw new RangeError("\"output\" expected length >= " + min)
158
+ }
159
+ }
160
+
161
+ function u32(arr: TypedArray): Uint32Array {
162
+ return new Uint32Array(
163
+ arr.buffer,
164
+ arr.byteOffset,
165
+ Math.floor(arr.byteLength / 4)
166
+ )
167
+ }
168
+
169
+ function clean(...arrays: Array<TypedArray>): void {
170
+ for (let i = 0; i < arrays.length; i++) {
171
+ arrays[i].fill(0)
172
+ }
173
+ }
174
+
175
+ function createView(arr: TypedArray): DataView {
176
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength)
177
+ }
178
+
179
+ const isLE: boolean = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)()
180
+
181
+ function byteSwap(word: number): number {
182
+ return (
183
+ ((word << 24) & 0xff000000) |
184
+ ((word << 8) & 0xff0000) |
185
+ ((word >>> 8) & 0xff00) |
186
+ ((word >>> 24) & 0xff)
187
+ )
188
+ }
189
+
190
+ function byteSwap32(arr: Uint32Array): Uint32Array {
191
+ for (let i = 0; i < arr.length; i++) {
192
+ arr[i] = byteSwap(arr[i])
193
+ }
194
+ return arr
195
+ }
196
+
197
+ const swap32IfBE: (u: Uint32Array) => Uint32Array = isLE
198
+ ? (u: Uint32Array) => u
199
+ : byteSwap32
200
+
201
+ function overlapBytes(a: Uint8Array, b: Uint8Array): boolean {
202
+ // Zero-length views cannot overwrite anything, even if their offset sits inside another range.
203
+ if (!a.byteLength || !b.byteLength) return false
204
+ return (
205
+ a.buffer === b.buffer && // best we can do, may fail with an obscure Proxy
206
+ a.byteOffset < b.byteOffset + b.byteLength && // a starts before b end
207
+ b.byteOffset < a.byteOffset + a.byteLength // b starts before a end
208
+ )
209
+ }
210
+
211
+ function complexOverlapBytes(input: Uint8Array, output: Uint8Array): void {
212
+ // This is very cursed. It works somehow, but I'm completely unsure,
213
+ // reasoning about overlapping aligned windows is very hard.
214
+ if (overlapBytes(input, output) && input.byteOffset < output.byteOffset) {
215
+ throw new Error("complex overlap of input and output is not supported")
216
+ }
217
+ }
218
+
219
+ function equalBytes(a: Uint8Array, b: Uint8Array): boolean {
220
+ a = abytes(a)
221
+ b = abytes(b)
222
+ if (a.length !== b.length) return false
223
+ let diff = 0
224
+ for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i]
225
+ return diff === 0
226
+ }
227
+
228
+ function getOutput(
229
+ expectedLength: number,
230
+ out?: Uint8Array,
231
+ onlyAligned = true
232
+ ): Uint8Array {
233
+ if (out === undefined) return new Uint8Array(expectedLength)
234
+ // Keep Buffer/cross-realm Uint8Array support here instead of trusting a shape-compatible object.
235
+ abytes(out, expectedLength, "output")
236
+ if (onlyAligned && !isAligned32(out)) throw new Error("invalid output, must be aligned")
237
+ return out
238
+ }
239
+
240
+ function u64Lengths(dataLength: number, aadLength: number, isLE: boolean): Uint8Array {
241
+ // Reject coercible non-number lengths like '10' and true before BigInt(...) accepts them.
242
+ anumber(dataLength)
243
+ anumber(aadLength)
244
+ abool(isLE)
245
+ const num = new Uint8Array(16)
246
+ const view = createView(num)
247
+ view.setBigUint64(0, BigInt(aadLength), isLE)
248
+ view.setBigUint64(8, BigInt(dataLength), isLE)
249
+ return num
250
+ }
251
+
252
+ function isAligned32(bytes: Uint8Array): boolean {
253
+ return bytes.byteOffset % 4 === 0
254
+ }
255
+
256
+ function copyBytes(bytes: Uint8Array): Uint8Array {
257
+ // `Uint8Array.from(...)` would also accept arrays / other typed arrays. Keep this helper strict
258
+ // because callers use it at byte-validation boundaries before mutating the detached copy.
259
+ return Uint8Array.from(abytes(bytes))
260
+ }
261
+
262
+ type XorStream = (
263
+ key: Uint8Array,
264
+ nonce: Uint8Array,
265
+ data: Uint8Array,
266
+ output?: Uint8Array,
267
+ counter?: number
268
+ ) => Uint8Array
269
+
270
+ // _arx.ts
271
+ // Replaces `TextEncoder` for ASCII literals, which is enough for sigma constants.
272
+ // Non-ASCII input would not match UTF-8 `TextEncoder` output.
273
+ const encodeStr = (str: string) => Uint8Array.from(str.split(""), (c) => c.charCodeAt(0))
274
+ // Raw `createCipher(...)` exports consume these native-endian `u32(...)` views directly.
275
+ // RFC 8439 §2.3 / RFC 7539 §2.3 only define the 256-bit-key constants; this 16-byte sigma is
276
+ // kept for legacy allowShortKeys Salsa/ChaCha variants.
277
+ const sigma16_32 = /* @__PURE__ */ (() => swap32IfBE(u32(encodeStr("expand 16-byte k"))))()
278
+ // RFC 8439 §2.3 / RFC 7539 §2.3 define words 0-3 as
279
+ // `0x61707865 0x3320646e 0x79622d32 0x6b206574`, i.e. `expand 32-byte k`.
280
+ const sigma32_32 = /* @__PURE__ */ (() => swap32IfBE(u32(encodeStr("expand 32-byte k"))))()
281
+
282
+ function rotl(a: number, b: number): number {
283
+ return (a << b) | (a >>> (32 - b))
284
+ }
285
+
286
+ type CipherCoreFn = (
287
+ sigma: Uint32Array,
288
+ key: Uint32Array,
289
+ nonce: Uint32Array,
290
+ output: Uint32Array,
291
+ counter: number,
292
+ rounds?: number
293
+ ) => void
294
+
295
+ type ExtendNonceFn = (
296
+ sigma: Uint32Array,
297
+ key: Uint32Array,
298
+ input: Uint32Array,
299
+ output: Uint32Array
300
+ ) => void
301
+
302
+ type CipherOpts = {
303
+ /** Whether 16-byte keys are accepted for legacy Salsa and ChaCha variants. */
304
+ allowShortKeys?: boolean
305
+ /** Optional nonce-expansion hook used by extended-nonce variants. */
306
+ extendNonceFn?: ExtendNonceFn
307
+ /** Counter length in bytes inside the nonce/counter layout. */
308
+ counterLength?: number
309
+ /** Whether the layout is `nonce|counter` instead of `counter|nonce`. */
310
+ counterRight?: boolean
311
+ /** Number of core rounds to execute. */
312
+ rounds?: number
313
+ }
314
+
315
+ // Salsa and Chacha block length is always 512-bit
316
+ const BLOCK_LEN = 64
317
+ // RFC 8439 §2.2 / RFC 7539 §2.2: the ChaCha state has 16 32-bit words.
318
+ const BLOCK_LEN32 = 16
319
+
320
+ // Counter policy for the shared public `counter` argument:
321
+ // - RFC/IETF ChaCha20 uses a 32-bit counter.
322
+ // - OpenSSL/Node `chacha20` instead treat the full 16-byte IV as a 128-bit
323
+ // counter state and carry into the next word.
324
+ // - Raw `chacha20orig`, `salsa20`, `xsalsa20`, and `xchacha20` use 64-bit counters in libsodium
325
+ // and libtomcrypt, while some libs (for example libtomcrypt's RFC/IETF path) reject the max
326
+ // boundary instead of carrying.
327
+ // - AEAD wrappers diverge too: libsodium `xchacha20poly1305` uses the IETF payload counter from
328
+ // block 1, while `secretstream_xchacha20poly1305` is a different protocol with rekey/reset.
329
+ // Noble intentionally throws instead of silently picking one wrap model for users. In the default
330
+ // path, even a 32-bit boundary would take 2^32 blocks * 64 bytes = 256 GiB, which is practically
331
+ // unreachable for normal JS callers; advanced users who pass `counter` explicitly can implement
332
+ // whatever wider carry / wrap policy they need on top.
333
+ const MAX_COUNTER = /* @__PURE__ */ (() => 2 ** 32 - 1)()
334
+ const U32_EMPTY = /* @__PURE__ */ Uint32Array.of()
335
+ function runCipher(
336
+ core: CipherCoreFn,
337
+ sigma: Uint32Array,
338
+ key: Uint32Array,
339
+ nonce: Uint32Array,
340
+ data: Uint8Array,
341
+ output: Uint8Array,
342
+ counter: number,
343
+ rounds: number
344
+ ): void {
345
+ const len = data.length
346
+ const block = new Uint8Array(BLOCK_LEN)
347
+ const b32 = u32(block)
348
+ // Make sure that buffers aligned to 4 bytes
349
+ const isAligned = isLE && isAligned32(data) && isAligned32(output)
350
+ const d32 = isAligned ? u32(data) : U32_EMPTY
351
+ const o32 = isAligned ? u32(output) : U32_EMPTY
352
+ // RFC 8439 §2.4.1 / RFC 7539 §2.4.1 allow XORing one keystream block at a time and
353
+ // truncating the final partial block instead of materializing the whole keystream.
354
+ if (!isLE) {
355
+ for (let pos = 0; pos < len; counter++) {
356
+ core(
357
+ sigma,
358
+ key,
359
+ nonce,
360
+ b32,
361
+ counter,
362
+ rounds
363
+ )
364
+ // RFC 8439 §2.4 / RFC 7539 §2.4 serialize keystream words in little-endian order.
365
+ swap32IfBE(b32)
366
+ if (counter >= MAX_COUNTER) throw new Error("arx: counter overflow")
367
+ const take = Math.min(BLOCK_LEN, len - pos)
368
+ for (let j = 0, posj; j < take; j++) {
369
+ posj = pos + j
370
+ output[posj] = data[posj] ^ block[j]
371
+ }
372
+ pos += take
373
+ }
374
+ return
375
+ }
376
+ for (let pos = 0; pos < len; counter++) {
377
+ core(
378
+ sigma,
379
+ key,
380
+ nonce,
381
+ b32,
382
+ counter,
383
+ rounds
384
+ )
385
+ // See MAX_COUNTER policy note above: never silently wrap the shared public counter.
386
+ if (counter >= MAX_COUNTER) throw new Error("arx: counter overflow")
387
+ const take = Math.min(BLOCK_LEN, len - pos)
388
+ // aligned to 4 bytes
389
+ if (isAligned && take === BLOCK_LEN) {
390
+ const pos32 = pos / 4
391
+ if (pos % 4 !== 0) throw new Error("arx: invalid block position")
392
+ for (let j = 0, posj: number; j < BLOCK_LEN32; j++) {
393
+ posj = pos32 + j
394
+ o32[posj] = d32[posj] ^ b32[j]
395
+ }
396
+ pos += BLOCK_LEN
397
+ continue
398
+ }
399
+ for (let j = 0, posj; j < take; j++) {
400
+ posj = pos + j
401
+ output[posj] = data[posj] ^ block[j]
402
+ }
403
+ pos += take
404
+ }
405
+ }
406
+
407
+ function createCipher(core: CipherCoreFn, opts: CipherOpts): XorStream {
408
+ const { allowShortKeys, extendNonceFn, counterLength, counterRight, rounds } = checkOpts(
409
+ { allowShortKeys: false, counterLength: 8, counterRight: false, rounds: 20 },
410
+ opts
411
+ )
412
+ if (typeof core !== "function") throw new Error("core must be a function")
413
+ anumber(counterLength)
414
+ anumber(rounds)
415
+ abool(counterRight)
416
+ abool(allowShortKeys)
417
+ return (
418
+ key: Uint8Array,
419
+ nonce: Uint8Array,
420
+ data: Uint8Array,
421
+ output?: Uint8Array,
422
+ counter = 0
423
+ ): Uint8Array => {
424
+ abytes(key, undefined, "key")
425
+ abytes(nonce, undefined, "nonce")
426
+ abytes(data, undefined, "data")
427
+ const len = data.length
428
+ // Raw XorStream APIs return ciphertext/plaintext bytes directly, so caller-provided outputs
429
+ // must match the logical result length exactly instead of returning an oversized workspace.
430
+ const hasOutput = output !== undefined
431
+ output = getOutput(len, output, false)
432
+ if (hasOutput) complexOverlapBytes(data, output)
433
+ anumber(counter)
434
+ // See MAX_COUNTER policy note above: reject advanced explicit-counter requests before any wrap.
435
+ if (counter < 0 || counter >= MAX_COUNTER) throw new Error("arx: counter overflow")
436
+ const toClean = []
437
+
438
+ // Key & sigma
439
+ // key=16 -> sigma16, k=key|key
440
+ // key=32 -> sigma32, k=key
441
+ let l = key.length
442
+ let k: Uint8Array
443
+ let sigma: Uint32Array
444
+ if (l === 32) {
445
+ // Copy caller keys too: big-endian normalization, extended-nonce subkey derivation, and
446
+ // final clean(...) all mutate or wipe the temporary buffer in place.
447
+ toClean.push(k = copyBytes(key))
448
+ sigma = sigma32_32
449
+ } else if (l === 16 && allowShortKeys) {
450
+ k = new Uint8Array(32)
451
+ k.set(key)
452
+ k.set(key, 16)
453
+ sigma = sigma16_32
454
+ toClean.push(k)
455
+ } else {
456
+ abytes(key, 32, "arx key")
457
+ throw new Error("invalid key size")
458
+ // throw new Error(`"arx key" expected Uint8Array of length 32, got length=${l}`);
459
+ }
460
+
461
+ // Nonce
462
+ // salsa20: 8 (8-byte counter)
463
+ // chacha20orig: 8 (8-byte counter)
464
+ // chacha20: 12 (4-byte counter)
465
+ // xsalsa20: 24 (16 -> hsalsa, 8 -> old nonce)
466
+ // xchacha20: 24 (16 -> hchacha, 8 -> old nonce)
467
+ // Copy before taking u32(...) views on misaligned inputs, and on big-endian so later
468
+ // swap32IfBE(...) never mutates caller nonce bytes in place.
469
+ if (!isLE || !isAligned32(nonce)) toClean.push(nonce = copyBytes(nonce))
470
+
471
+ let k32 = u32(k)
472
+ // hsalsa & hchacha: handle extended nonce
473
+ if (extendNonceFn) {
474
+ if (nonce.length !== 24) throw new Error("arx: extended nonce must be 24 bytes")
475
+ const n16 = nonce.subarray(0, 16)
476
+ if (isLE) extendNonceFn(sigma, k32, u32(n16), k32)
477
+ else {
478
+ const sigmaRaw = swap32IfBE(Uint32Array.from(sigma))
479
+ extendNonceFn(sigmaRaw, k32, u32(n16), k32)
480
+ clean(sigmaRaw)
481
+ swap32IfBE(k32)
482
+ }
483
+ nonce = nonce.subarray(16)
484
+ } else if (!isLE) swap32IfBE(k32)
485
+
486
+ // Handle nonce counter
487
+ const nonceNcLen = 16 - counterLength
488
+ if (nonceNcLen !== nonce.length) {
489
+ throw new Error(`arx: nonce must be ${nonceNcLen} or 16 bytes`)
490
+ }
491
+
492
+ // Normalize 64-bit-nonce layouts to the 12-byte core input: ChaCha/XChaCha prefix 4 zero
493
+ // counter bytes, while Salsa/XSalsa append them after the nonce words.
494
+ if (nonceNcLen !== 12) {
495
+ const nc = new Uint8Array(12)
496
+ nc.set(nonce, counterRight ? 0 : 12 - nonce.length)
497
+ nonce = nc
498
+ toClean.push(nonce)
499
+ }
500
+ const n32 = swap32IfBE(u32(nonce))
501
+ // Ensure temporary key/nonce copies are wiped even if the remaining
502
+ // runtime guard in runCipher(...) throws on counter overflow.
503
+ try {
504
+ runCipher(core, sigma, k32, n32, data, output, counter, rounds)
505
+ return output
506
+ } finally {
507
+ clean(...toClean)
508
+ }
509
+ }
510
+ }
511
+
512
+ // _poly1305.ts
513
+ // Little-endian 2-byte load used by the Poly1305 limb decomposition.
514
+ function u8to16(a: Uint8Array, i: number) {
515
+ return (a[i++] & 0xff) | ((a[i++] & 0xff) << 8)
516
+ }
517
+
518
+ class Poly1305 {
519
+ readonly blockLen = 16
520
+ readonly outputLen = 16
521
+ private buffer = new Uint8Array(16)
522
+ private r = new Uint16Array(10) // Allocating 1 array with .subarray() here is slower than 3
523
+ private h = new Uint16Array(10)
524
+ private pad = new Uint16Array(8)
525
+ private pos = 0
526
+ protected finished = false
527
+ protected destroyed = false
528
+
529
+ // Can be speed-up using BigUint64Array, at the cost of complexity
530
+ constructor(key: Uint8Array) {
531
+ key = copyBytes(abytes(key, 32, "key"))
532
+ const t0 = u8to16(key, 0)
533
+ const t1 = u8to16(key, 2)
534
+ const t2 = u8to16(key, 4)
535
+ const t3 = u8to16(key, 6)
536
+ const t4 = u8to16(key, 8)
537
+ const t5 = u8to16(key, 10)
538
+ const t6 = u8to16(key, 12)
539
+ const t7 = u8to16(key, 14)
540
+
541
+ // RFC 8439 §2.5.1 / RFC 7539 §2.5.1 clamp r before multiplication.
542
+ // These masks unpack that clamped value into 13-bit limbs, while pad
543
+ // keeps the raw s half for finalize().
544
+ // {@link https://github.com/floodyberry/poly1305-donna/blob/e6ad6e091d30d7f4ec2d4f978be1fcfcbce72781/poly1305-donna-16.h#L47 | poly1305-donna reference}
545
+ this.r[0] = t0 & 0x1fff
546
+ this.r[1] = ((t0 >>> 13) | (t1 << 3)) & 0x1fff
547
+ this.r[2] = ((t1 >>> 10) | (t2 << 6)) & 0x1f03
548
+ this.r[3] = ((t2 >>> 7) | (t3 << 9)) & 0x1fff
549
+ this.r[4] = ((t3 >>> 4) | (t4 << 12)) & 0x00ff
550
+ this.r[5] = (t4 >>> 1) & 0x1ffe
551
+ this.r[6] = ((t4 >>> 14) | (t5 << 2)) & 0x1fff
552
+ this.r[7] = ((t5 >>> 11) | (t6 << 5)) & 0x1f81
553
+ this.r[8] = ((t6 >>> 8) | (t7 << 8)) & 0x1fff
554
+ this.r[9] = (t7 >>> 5) & 0x007f
555
+ for (let i = 0; i < 8; i++) this.pad[i] = u8to16(key, 16 + 2 * i)
556
+ }
557
+
558
+ private process(data: Uint8Array, offset: number, isLast = false) {
559
+ // RFC 8439 §2.5 / §2.5.1 and RFC 7539 §2.5 / §2.5.1 add an extra high
560
+ // bit to every full 16-byte block. The final partial block gets its
561
+ // explicit `1` byte during digestInto(), so `hibit` stays zero there.
562
+ const hibit = isLast ? 0 : 1 << 11
563
+ const { h, r } = this
564
+ const r0 = r[0]
565
+ const r1 = r[1]
566
+ const r2 = r[2]
567
+ const r3 = r[3]
568
+ const r4 = r[4]
569
+ const r5 = r[5]
570
+ const r6 = r[6]
571
+ const r7 = r[7]
572
+ const r8 = r[8]
573
+ const r9 = r[9]
574
+
575
+ const t0 = u8to16(data, offset + 0)
576
+ const t1 = u8to16(data, offset + 2)
577
+ const t2 = u8to16(data, offset + 4)
578
+ const t3 = u8to16(data, offset + 6)
579
+ const t4 = u8to16(data, offset + 8)
580
+ const t5 = u8to16(data, offset + 10)
581
+ const t6 = u8to16(data, offset + 12)
582
+ const t7 = u8to16(data, offset + 14)
583
+
584
+ let h0 = h[0] + (t0 & 0x1fff)
585
+ let h1 = h[1] + (((t0 >>> 13) | (t1 << 3)) & 0x1fff)
586
+ let h2 = h[2] + (((t1 >>> 10) | (t2 << 6)) & 0x1fff)
587
+ let h3 = h[3] + (((t2 >>> 7) | (t3 << 9)) & 0x1fff)
588
+ let h4 = h[4] + (((t3 >>> 4) | (t4 << 12)) & 0x1fff)
589
+ let h5 = h[5] + ((t4 >>> 1) & 0x1fff)
590
+ let h6 = h[6] + (((t4 >>> 14) | (t5 << 2)) & 0x1fff)
591
+ let h7 = h[7] + (((t5 >>> 11) | (t6 << 5)) & 0x1fff)
592
+ let h8 = h[8] + (((t6 >>> 8) | (t7 << 8)) & 0x1fff)
593
+ let h9 = h[9] + ((t7 >>> 5) | hibit)
594
+
595
+ let c = 0
596
+
597
+ let d0 = c + h0 * r0 + h1 * (5 * r9) + h2 * (5 * r8) + h3 * (5 * r7) + h4 * (5 * r6)
598
+ c = d0 >>> 13
599
+ d0 &= 0x1fff
600
+ d0 += h5 * (5 * r5) + h6 * (5 * r4) + h7 * (5 * r3) + h8 * (5 * r2) + h9 * (5 * r1)
601
+ c += d0 >>> 13
602
+ d0 &= 0x1fff
603
+
604
+ let d1 = c + h0 * r1 + h1 * r0 + h2 * (5 * r9) + h3 * (5 * r8) + h4 * (5 * r7)
605
+ c = d1 >>> 13
606
+ d1 &= 0x1fff
607
+ d1 += h5 * (5 * r6) + h6 * (5 * r5) + h7 * (5 * r4) + h8 * (5 * r3) + h9 * (5 * r2)
608
+ c += d1 >>> 13
609
+ d1 &= 0x1fff
610
+
611
+ let d2 = c + h0 * r2 + h1 * r1 + h2 * r0 + h3 * (5 * r9) + h4 * (5 * r8)
612
+ c = d2 >>> 13
613
+ d2 &= 0x1fff
614
+ d2 += h5 * (5 * r7) + h6 * (5 * r6) + h7 * (5 * r5) + h8 * (5 * r4) + h9 * (5 * r3)
615
+ c += d2 >>> 13
616
+ d2 &= 0x1fff
617
+
618
+ let d3 = c + h0 * r3 + h1 * r2 + h2 * r1 + h3 * r0 + h4 * (5 * r9)
619
+ c = d3 >>> 13
620
+ d3 &= 0x1fff
621
+ d3 += h5 * (5 * r8) + h6 * (5 * r7) + h7 * (5 * r6) + h8 * (5 * r5) + h9 * (5 * r4)
622
+ c += d3 >>> 13
623
+ d3 &= 0x1fff
624
+
625
+ let d4 = c + h0 * r4 + h1 * r3 + h2 * r2 + h3 * r1 + h4 * r0
626
+ c = d4 >>> 13
627
+ d4 &= 0x1fff
628
+ d4 += h5 * (5 * r9) + h6 * (5 * r8) + h7 * (5 * r7) + h8 * (5 * r6) + h9 * (5 * r5)
629
+ c += d4 >>> 13
630
+ d4 &= 0x1fff
631
+
632
+ let d5 = c + h0 * r5 + h1 * r4 + h2 * r3 + h3 * r2 + h4 * r1
633
+ c = d5 >>> 13
634
+ d5 &= 0x1fff
635
+ d5 += h5 * r0 + h6 * (5 * r9) + h7 * (5 * r8) + h8 * (5 * r7) + h9 * (5 * r6)
636
+ c += d5 >>> 13
637
+ d5 &= 0x1fff
638
+
639
+ let d6 = c + h0 * r6 + h1 * r5 + h2 * r4 + h3 * r3 + h4 * r2
640
+ c = d6 >>> 13
641
+ d6 &= 0x1fff
642
+ d6 += h5 * r1 + h6 * r0 + h7 * (5 * r9) + h8 * (5 * r8) + h9 * (5 * r7)
643
+ c += d6 >>> 13
644
+ d6 &= 0x1fff
645
+
646
+ let d7 = c + h0 * r7 + h1 * r6 + h2 * r5 + h3 * r4 + h4 * r3
647
+ c = d7 >>> 13
648
+ d7 &= 0x1fff
649
+ d7 += h5 * r2 + h6 * r1 + h7 * r0 + h8 * (5 * r9) + h9 * (5 * r8)
650
+ c += d7 >>> 13
651
+ d7 &= 0x1fff
652
+
653
+ let d8 = c + h0 * r8 + h1 * r7 + h2 * r6 + h3 * r5 + h4 * r4
654
+ c = d8 >>> 13
655
+ d8 &= 0x1fff
656
+ d8 += h5 * r3 + h6 * r2 + h7 * r1 + h8 * r0 + h9 * (5 * r9)
657
+ c += d8 >>> 13
658
+ d8 &= 0x1fff
659
+
660
+ let d9 = c + h0 * r9 + h1 * r8 + h2 * r7 + h3 * r6 + h4 * r5
661
+ c = d9 >>> 13
662
+ d9 &= 0x1fff
663
+ d9 += h5 * r4 + h6 * r3 + h7 * r2 + h8 * r1 + h9 * r0
664
+ c += d9 >>> 13
665
+ d9 &= 0x1fff
666
+
667
+ c = ((c << 2) + c) | 0
668
+ c = (c + d0) | 0
669
+ d0 = c & 0x1fff
670
+ c = c >>> 13
671
+ d1 += c
672
+
673
+ h[0] = d0
674
+ h[1] = d1
675
+ h[2] = d2
676
+ h[3] = d3
677
+ h[4] = d4
678
+ h[5] = d5
679
+ h[6] = d6
680
+ h[7] = d7
681
+ h[8] = d8
682
+ h[9] = d9
683
+ }
684
+
685
+ private finalize() {
686
+ const { h, pad } = this
687
+ const g = new Uint16Array(10)
688
+ let c = h[1] >>> 13
689
+ h[1] &= 0x1fff
690
+ for (let i = 2; i < 10; i++) {
691
+ h[i] += c
692
+ c = h[i] >>> 13
693
+ h[i] &= 0x1fff
694
+ }
695
+ h[0] += c * 5
696
+ c = h[0] >>> 13
697
+ h[0] &= 0x1fff
698
+ h[1] += c
699
+ c = h[1] >>> 13
700
+ h[1] &= 0x1fff
701
+ h[2] += c
702
+
703
+ // RFC 8439 §2.5 / RFC 7539 §2.5 reduce modulo 2^130-5 before repacking
704
+ // to 16-bit words and adding the raw s half.
705
+ g[0] = h[0] + 5
706
+ c = g[0] >>> 13
707
+ g[0] &= 0x1fff
708
+ for (let i = 1; i < 10; i++) {
709
+ g[i] = h[i] + c
710
+ c = g[i] >>> 13
711
+ g[i] &= 0x1fff
712
+ }
713
+ g[9] -= 1 << 13
714
+
715
+ let mask = (c ^ 1) - 1
716
+ for (let i = 0; i < 10; i++) g[i] &= mask
717
+ mask = ~mask
718
+ for (let i = 0; i < 10; i++) h[i] = (h[i] & mask) | g[i]
719
+ h[0] = (h[0] | (h[1] << 13)) & 0xffff
720
+ h[1] = ((h[1] >>> 3) | (h[2] << 10)) & 0xffff
721
+ h[2] = ((h[2] >>> 6) | (h[3] << 7)) & 0xffff
722
+ h[3] = ((h[3] >>> 9) | (h[4] << 4)) & 0xffff
723
+ h[4] = ((h[4] >>> 12) | (h[5] << 1) | (h[6] << 14)) & 0xffff
724
+ h[5] = ((h[6] >>> 2) | (h[7] << 11)) & 0xffff
725
+ h[6] = ((h[7] >>> 5) | (h[8] << 8)) & 0xffff
726
+ h[7] = ((h[8] >>> 8) | (h[9] << 5)) & 0xffff
727
+
728
+ let f = h[0] + pad[0]
729
+ h[0] = f & 0xffff
730
+ for (let i = 1; i < 8; i++) {
731
+ f = (((h[i] + pad[i]) | 0) + (f >>> 16)) | 0
732
+ h[i] = f & 0xffff
733
+ }
734
+ clean(g)
735
+ }
736
+ update(data: Uint8Array): this {
737
+ aexists(this)
738
+ abytes(data)
739
+ data = copyBytes(data)
740
+ const { buffer, blockLen } = this
741
+ const len = data.length
742
+
743
+ for (let pos = 0; pos < len;) {
744
+ const take = Math.min(blockLen - this.pos, len - pos)
745
+ // Fast path: we have at least one block in input
746
+ if (take === blockLen) {
747
+ for (; blockLen <= len - pos; pos += blockLen) this.process(data, pos)
748
+ continue
749
+ }
750
+ buffer.set(data.subarray(pos, pos + take), this.pos)
751
+ this.pos += take
752
+ pos += take
753
+ if (this.pos === blockLen) {
754
+ this.process(buffer, 0, false)
755
+ this.pos = 0
756
+ }
757
+ }
758
+ return this
759
+ }
760
+ destroy(): void {
761
+ // `aexists(this)` guards update/digest paths, so destroy must mark the instance unusable too.
762
+ this.destroyed = true
763
+ clean(this.h, this.r, this.buffer, this.pad)
764
+ }
765
+ digestInto(out: Uint8Array): void {
766
+ aexists(this)
767
+ aoutput(out, this)
768
+ this.finished = true
769
+ const { buffer, h } = this
770
+ let { pos } = this
771
+ if (pos) {
772
+ // RFC 8439 §2.5 / RFC 7539 §2.5: the final short block appends a
773
+ // single `0x01` byte and zero-fills the remaining bytes before the
774
+ // last multiplication step.
775
+ buffer[pos++] = 1
776
+ for (; pos < 16; pos++) buffer[pos] = 0
777
+ this.process(buffer, 0, true)
778
+ }
779
+ this.finalize()
780
+ let opos = 0
781
+ for (let i = 0; i < 8; i++) {
782
+ out[opos++] = h[i] >>> 0
783
+ out[opos++] = h[i] >>> 8
784
+ }
785
+ }
786
+ digest(): Uint8Array {
787
+ const { buffer, outputLen } = this
788
+ this.digestInto(buffer)
789
+ // Copy out before destroy() zeroes the internal buffer.
790
+ const res = buffer.slice(0, outputLen)
791
+ this.destroy()
792
+ return res
793
+ }
794
+ }
795
+
796
+ // chacha.ts
797
+ /** RFC 8439 §2.3 block core for `state = constants | key | counter | nonce`. */
798
+ // prettier-ignore
799
+ function chachaCore(
800
+ s: Uint32Array,
801
+ k: Uint32Array,
802
+ n: Uint32Array,
803
+ out: Uint32Array,
804
+ cnt: number,
805
+ rounds = 20
806
+ ): void {
807
+ let y00 = s[0],
808
+ y01 = s[1],
809
+ y02 = s[2],
810
+ y03 = s[3], // "expa" "nd 3" "2-by" "te k"
811
+ y04 = k[0],
812
+ y05 = k[1],
813
+ y06 = k[2],
814
+ y07 = k[3], // Key Key Key Key
815
+ y08 = k[4],
816
+ y09 = k[5],
817
+ y10 = k[6],
818
+ y11 = k[7], // Key Key Key Key
819
+ y12 = cnt,
820
+ y13 = n[0],
821
+ y14 = n[1],
822
+ y15 = n[2] // Counter Nonce Nonce Nonce
823
+ // Save state to temporary variables
824
+ let x00 = y00,
825
+ x01 = y01,
826
+ x02 = y02,
827
+ x03 = y03,
828
+ x04 = y04,
829
+ x05 = y05,
830
+ x06 = y06,
831
+ x07 = y07,
832
+ x08 = y08,
833
+ x09 = y09,
834
+ x10 = y10,
835
+ x11 = y11,
836
+ x12 = y12,
837
+ x13 = y13,
838
+ x14 = y14,
839
+ x15 = y15
840
+ for (let r = 0; r < rounds; r += 2) {
841
+ x00 = (x00 + x04) | 0
842
+ x12 = rotl(x12 ^ x00, 16)
843
+ x08 = (x08 + x12) | 0
844
+ x04 = rotl(x04 ^ x08, 12)
845
+ x00 = (x00 + x04) | 0
846
+ x12 = rotl(x12 ^ x00, 8)
847
+ x08 = (x08 + x12) | 0
848
+ x04 = rotl(x04 ^ x08, 7)
849
+
850
+ x01 = (x01 + x05) | 0
851
+ x13 = rotl(x13 ^ x01, 16)
852
+ x09 = (x09 + x13) | 0
853
+ x05 = rotl(x05 ^ x09, 12)
854
+ x01 = (x01 + x05) | 0
855
+ x13 = rotl(x13 ^ x01, 8)
856
+ x09 = (x09 + x13) | 0
857
+ x05 = rotl(x05 ^ x09, 7)
858
+
859
+ x02 = (x02 + x06) | 0
860
+ x14 = rotl(x14 ^ x02, 16)
861
+ x10 = (x10 + x14) | 0
862
+ x06 = rotl(x06 ^ x10, 12)
863
+ x02 = (x02 + x06) | 0
864
+ x14 = rotl(x14 ^ x02, 8)
865
+ x10 = (x10 + x14) | 0
866
+ x06 = rotl(x06 ^ x10, 7)
867
+
868
+ x03 = (x03 + x07) | 0
869
+ x15 = rotl(x15 ^ x03, 16)
870
+ x11 = (x11 + x15) | 0
871
+ x07 = rotl(x07 ^ x11, 12)
872
+ x03 = (x03 + x07) | 0
873
+ x15 = rotl(x15 ^ x03, 8)
874
+ x11 = (x11 + x15) | 0
875
+ x07 = rotl(x07 ^ x11, 7)
876
+
877
+ x00 = (x00 + x05) | 0
878
+ x15 = rotl(x15 ^ x00, 16)
879
+ x10 = (x10 + x15) | 0
880
+ x05 = rotl(x05 ^ x10, 12)
881
+ x00 = (x00 + x05) | 0
882
+ x15 = rotl(x15 ^ x00, 8)
883
+ x10 = (x10 + x15) | 0
884
+ x05 = rotl(x05 ^ x10, 7)
885
+
886
+ x01 = (x01 + x06) | 0
887
+ x12 = rotl(x12 ^ x01, 16)
888
+ x11 = (x11 + x12) | 0
889
+ x06 = rotl(x06 ^ x11, 12)
890
+ x01 = (x01 + x06) | 0
891
+ x12 = rotl(x12 ^ x01, 8)
892
+ x11 = (x11 + x12) | 0
893
+ x06 = rotl(x06 ^ x11, 7)
894
+
895
+ x02 = (x02 + x07) | 0
896
+ x13 = rotl(x13 ^ x02, 16)
897
+ x08 = (x08 + x13) | 0
898
+ x07 = rotl(x07 ^ x08, 12)
899
+ x02 = (x02 + x07) | 0
900
+ x13 = rotl(x13 ^ x02, 8)
901
+ x08 = (x08 + x13) | 0
902
+ x07 = rotl(x07 ^ x08, 7)
903
+
904
+ x03 = (x03 + x04) | 0
905
+ x14 = rotl(x14 ^ x03, 16)
906
+ x09 = (x09 + x14) | 0
907
+ x04 = rotl(x04 ^ x09, 12)
908
+ x03 = (x03 + x04) | 0
909
+ x14 = rotl(x14 ^ x03, 8)
910
+ x09 = (x09 + x14) | 0
911
+ x04 = rotl(x04 ^ x09, 7)
912
+ }
913
+ // RFC 8439 §2.3 / §2.3.1: add the original state words back in state order.
914
+ let oi = 0
915
+ out[oi++] = (y00 + x00) | 0
916
+ out[oi++] = (y01 + x01) | 0
917
+ out[oi++] = (y02 + x02) | 0
918
+ out[oi++] = (y03 + x03) | 0
919
+ out[oi++] = (y04 + x04) | 0
920
+ out[oi++] = (y05 + x05) | 0
921
+ out[oi++] = (y06 + x06) | 0
922
+ out[oi++] = (y07 + x07) | 0
923
+ out[oi++] = (y08 + x08) | 0
924
+ out[oi++] = (y09 + x09) | 0
925
+ out[oi++] = (y10 + x10) | 0
926
+ out[oi++] = (y11 + x11) | 0
927
+ out[oi++] = (y12 + x12) | 0
928
+ out[oi++] = (y13 + x13) | 0
929
+ out[oi++] = (y14 + x14) | 0
930
+ out[oi++] = (y15 + x15) | 0
931
+ }
932
+
933
+ // prettier-ignore
934
+ function hchacha(
935
+ s: Uint32Array,
936
+ k: Uint32Array,
937
+ i: Uint32Array,
938
+ out: Uint32Array
939
+ ): void {
940
+ // Runs the shared chachaCore permutation, then subtracts the RFC 8439 feed-forward
941
+ // it applies, recovering the raw permutation words hchacha needs.
942
+ // LE hosts read the caller arrays in place (no copies); BE hosts get
943
+ // byte-swapped scratch copies, wiped before returning.
944
+ const s2 = isLE ? s : swap32IfBE(s.slice(0, 4))
945
+ const k2 = isLE ? k : swap32IfBE(k.slice(0, 8))
946
+ const i2 = isLE ? i : swap32IfBE(i.slice(0, 4))
947
+ const t = new Uint32Array(16)
948
+ chachaCore(s2, k2, i2.subarray(1), t, i2[0])
949
+ let oi = 0
950
+ out[oi++] = (t[0] - s2[0]) | 0
951
+ out[oi++] = (t[1] - s2[1]) | 0
952
+ out[oi++] = (t[2] - s2[2]) | 0
953
+ out[oi++] = (t[3] - s2[3]) | 0
954
+ out[oi++] = (t[12] - i2[0]) | 0
955
+ out[oi++] = (t[13] - i2[1]) | 0
956
+ out[oi++] = (t[14] - i2[2]) | 0
957
+ out[oi++] = (t[15] - i2[3]) | 0
958
+ swap32IfBE(out)
959
+ if (!isLE) clean(s2, k2, i2)
960
+ clean(t)
961
+ }
962
+
963
+ const chacha20: XorStream = /* @__PURE__ */ createCipher(chachaCore, {
964
+ counterRight: false,
965
+ counterLength: 4,
966
+ allowShortKeys: false
967
+ })
968
+
969
+ const xchacha20: XorStream = /* @__PURE__ */ createCipher(chachaCore, {
970
+ counterRight: false,
971
+ counterLength: 8,
972
+ extendNonceFn: hchacha,
973
+ allowShortKeys: false
974
+ })
975
+
976
+ // RFC 8439 §2.8.1 pad16(x): shared zero block for AAD/ciphertext padding.
977
+ const ZEROS16 = /* @__PURE__ */ new Uint8Array(16)
978
+ // RFC 8439 §2.8 / §2.8.1: aligned inputs add nothing, otherwise append 16-(len%16) zero bytes.
979
+ const updatePadded = (h: Poly1305, msg: Uint8Array) => {
980
+ h.update(msg)
981
+ const leftover = msg.length % 16
982
+ if (leftover) h.update(ZEROS16.subarray(leftover))
983
+ }
984
+
985
+ // RFC 8439 §2.6.1 poly1305_key_gen returns `block[0..31]`, so AEAD key
986
+ // generation only needs 32 zero bytes.
987
+ const ZEROS32 = /* @__PURE__ */ new Uint8Array(32)
988
+ function computeTag(
989
+ fn: XorStream,
990
+ key: Uint8Array,
991
+ nonce: Uint8Array,
992
+ ciphertext: Uint8Array,
993
+ AAD?: Uint8Array
994
+ ): Uint8Array {
995
+ if (AAD !== undefined) abytes(AAD, undefined, "AAD")
996
+ // RFC 8439 §2.6 / §2.8: derive the Poly1305 one-time key from counter 0,
997
+ // then MAC AAD || pad16(AAD) || ciphertext || pad16(ciphertext) || len(AAD) || len(ciphertext).
998
+ const authKey = fn(
999
+ key,
1000
+ nonce,
1001
+ ZEROS32
1002
+ )
1003
+ const lengths = u64Lengths(ciphertext.length, AAD ? AAD.length : 0, true)
1004
+
1005
+ // Methods below can be replaced with
1006
+ // `return poly1305_computeTag_small(authKey, lengths, ciphertext, AAD)`
1007
+ // from `test/misc/micro-ciphers.ts`.
1008
+ const h = new Poly1305(authKey)
1009
+ if (AAD) updatePadded(h, AAD)
1010
+ updatePadded(h, ciphertext)
1011
+ h.update(lengths)
1012
+ const res = h.digest()
1013
+ clean(authKey, lengths)
1014
+ return res
1015
+ }
1016
+
1017
+ /**
1018
+ * AEAD algorithm from RFC 8439.
1019
+ * Salsa20 and chacha (RFC 8439) use poly1305 differently.
1020
+ * We could have composed them, but it's hard because of authKey:
1021
+ * In salsa20, authKey changes position in salsa stream.
1022
+ * In chacha, authKey can't be computed inside computeTag, it modifies the counter.
1023
+ */
1024
+ const _poly1305_aead = (xorStream: XorStream) => (key: Uint8Array, nonce: Uint8Array, AAD?: Uint8Array): Aead => {
1025
+ // This borrows caller key/nonce/AAD buffers by reference; mutating them after construction
1026
+ // changes future encrypt/decrypt results.
1027
+ const tagLength = 16
1028
+ return {
1029
+ encrypt(plaintext: Uint8Array, output?: Uint8Array): Uint8Array {
1030
+ const plength = plaintext.length
1031
+ output = getOutput(plength + tagLength, output, false)
1032
+ output.set(plaintext)
1033
+ const oPlain = output.subarray(0, -tagLength)
1034
+ // RFC 8439 §2.8: payload encryption starts at counter 1 because counter 0 produced the OTK.
1035
+ xorStream(
1036
+ key,
1037
+ nonce,
1038
+ oPlain,
1039
+ oPlain,
1040
+ 1
1041
+ )
1042
+ const tag = computeTag(xorStream, key, nonce, oPlain, AAD)
1043
+ output.set(tag, plength) // append tag
1044
+ clean(tag)
1045
+ return output
1046
+ },
1047
+ decrypt(ciphertext: Uint8Array, output?: Uint8Array): Uint8Array | undefined {
1048
+ output = getOutput(ciphertext.length - tagLength, output, false)
1049
+ const data = ciphertext.subarray(0, -tagLength)
1050
+ const passedTag = ciphertext.subarray(-tagLength)
1051
+ const tag = computeTag(xorStream, key, nonce, data, AAD)
1052
+ // RFC 8439 §2.8 / §4: authenticate ciphertext before decrypting it, and compare tags with
1053
+ // the constant-time equalBytes() helper rather than decrypting speculative plaintext first.
1054
+ if (!equalBytes(passedTag, tag)) {
1055
+ clean(tag)
1056
+ return undefined
1057
+ }
1058
+ output.set(ciphertext.subarray(0, -tagLength))
1059
+ // Actual decryption
1060
+ xorStream(
1061
+ key,
1062
+ nonce,
1063
+ output,
1064
+ output,
1065
+ 1
1066
+ ) // start stream with i=1
1067
+ clean(tag)
1068
+ return output
1069
+ }
1070
+ }
1071
+ }
1072
+
1073
+ /**
1074
+ * ChaCha20-Poly1305 from RFC 8439, with a 32-byte key and a 12-byte nonce.
1075
+ *
1076
+ * @internal
1077
+ */
1078
+ export const chacha20Poly1305: (key: Uint8Array, nonce: Uint8Array, aad?: Uint8Array) => Aead =
1079
+ /* @__PURE__ */ _poly1305_aead(chacha20)
1080
+
1081
+ /**
1082
+ * XChaCha20-Poly1305 from draft-irtf-cfrg-xchacha-03, with a 32-byte key and a
1083
+ * 24-byte nonce.
1084
+ *
1085
+ * @internal
1086
+ */
1087
+ export const xchacha20Poly1305: (key: Uint8Array, nonce: Uint8Array, aad?: Uint8Array) => Aead =
1088
+ /* @__PURE__ */ _poly1305_aead(xchacha20)