@aztec/foundation 0.1.0-alpha10

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 (414) hide show
  1. package/.eslintrc.cjs +103 -0
  2. package/.prettierrc.json +6 -0
  3. package/.tsbuildinfo +1 -0
  4. package/README.md +1 -0
  5. package/dest/abi/index.d.ts +137 -0
  6. package/dest/abi/index.d.ts.map +1 -0
  7. package/dest/abi/index.js +18 -0
  8. package/dest/async-map/async_map.test.d.ts +2 -0
  9. package/dest/async-map/async_map.test.d.ts.map +1 -0
  10. package/dest/async-map/async_map.test.js +9 -0
  11. package/dest/async-map/index.d.ts +13 -0
  12. package/dest/async-map/index.d.ts.map +1 -0
  13. package/dest/async-map/index.js +19 -0
  14. package/dest/aztec-address/index.d.ts +96 -0
  15. package/dest/aztec-address/index.d.ts.map +1 -0
  16. package/dest/aztec-address/index.js +124 -0
  17. package/dest/bigint-buffer/bigint-buffer.test.d.ts +2 -0
  18. package/dest/bigint-buffer/bigint-buffer.test.d.ts.map +1 -0
  19. package/dest/bigint-buffer/bigint-buffer.test.js +18 -0
  20. package/dest/bigint-buffer/index.d.ts +35 -0
  21. package/dest/bigint-buffer/index.d.ts.map +1 -0
  22. package/dest/bigint-buffer/index.js +68 -0
  23. package/dest/collection/array.d.ts +30 -0
  24. package/dest/collection/array.d.ts.map +1 -0
  25. package/dest/collection/array.js +47 -0
  26. package/dest/collection/index.d.ts +2 -0
  27. package/dest/collection/index.d.ts.map +1 -0
  28. package/dest/collection/index.js +2 -0
  29. package/dest/crypto/index.d.ts +4 -0
  30. package/dest/crypto/index.d.ts.map +1 -0
  31. package/dest/crypto/index.js +4 -0
  32. package/dest/crypto/keccak/index.d.ts +20 -0
  33. package/dest/crypto/keccak/index.d.ts.map +1 -0
  34. package/dest/crypto/keccak/index.js +31 -0
  35. package/dest/crypto/random/index.d.ts +3 -0
  36. package/dest/crypto/random/index.d.ts.map +1 -0
  37. package/dest/crypto/random/index.js +36 -0
  38. package/dest/crypto/random/index.test.d.ts +2 -0
  39. package/dest/crypto/random/index.test.d.ts.map +1 -0
  40. package/dest/crypto/random/index.test.js +13 -0
  41. package/dest/crypto/sha256/index.d.ts +11 -0
  42. package/dest/crypto/sha256/index.d.ts.map +1 -0
  43. package/dest/crypto/sha256/index.js +14 -0
  44. package/dest/crypto/sha256/index.test.d.ts +2 -0
  45. package/dest/crypto/sha256/index.test.d.ts.map +1 -0
  46. package/dest/crypto/sha256/index.test.js +11 -0
  47. package/dest/errors/index.d.ts +8 -0
  48. package/dest/errors/index.d.ts.map +1 -0
  49. package/dest/errors/index.js +8 -0
  50. package/dest/eth-address/eth_address.test.d.ts +2 -0
  51. package/dest/eth-address/eth_address.test.d.ts.map +1 -0
  52. package/dest/eth-address/eth_address.test.js +87 -0
  53. package/dest/eth-address/index.d.ts +139 -0
  54. package/dest/eth-address/index.d.ts.map +1 -0
  55. package/dest/eth-address/index.js +221 -0
  56. package/dest/fields/coordinate.d.ts +67 -0
  57. package/dest/fields/coordinate.d.ts.map +1 -0
  58. package/dest/fields/coordinate.js +95 -0
  59. package/dest/fields/coordinate.test.d.ts +2 -0
  60. package/dest/fields/coordinate.test.d.ts.map +1 -0
  61. package/dest/fields/coordinate.test.js +22 -0
  62. package/dest/fields/fields.d.ts +170 -0
  63. package/dest/fields/fields.d.ts.map +1 -0
  64. package/dest/fields/fields.js +210 -0
  65. package/dest/fields/index.d.ts +4 -0
  66. package/dest/fields/index.d.ts.map +1 -0
  67. package/dest/fields/index.js +4 -0
  68. package/dest/fields/point.d.ts +111 -0
  69. package/dest/fields/point.d.ts.map +1 -0
  70. package/dest/fields/point.js +133 -0
  71. package/dest/fifo/bounded_serial_queue.d.ts +56 -0
  72. package/dest/fifo/bounded_serial_queue.d.ts.map +1 -0
  73. package/dest/fifo/bounded_serial_queue.js +94 -0
  74. package/dest/fifo/index.d.ts +5 -0
  75. package/dest/fifo/index.d.ts.map +1 -0
  76. package/dest/fifo/index.js +5 -0
  77. package/dest/fifo/memory_fifo.d.ts +55 -0
  78. package/dest/fifo/memory_fifo.d.ts.map +1 -0
  79. package/dest/fifo/memory_fifo.js +109 -0
  80. package/dest/fifo/semaphore.d.ts +23 -0
  81. package/dest/fifo/semaphore.d.ts.map +1 -0
  82. package/dest/fifo/semaphore.js +30 -0
  83. package/dest/fifo/serial_queue.d.ts +48 -0
  84. package/dest/fifo/serial_queue.d.ts.map +1 -0
  85. package/dest/fifo/serial_queue.js +74 -0
  86. package/dest/json-rpc/class_converter.d.ts +109 -0
  87. package/dest/json-rpc/class_converter.d.ts.map +1 -0
  88. package/dest/json-rpc/class_converter.js +85 -0
  89. package/dest/json-rpc/client/index.d.ts +2 -0
  90. package/dest/json-rpc/client/index.d.ts.map +1 -0
  91. package/dest/json-rpc/client/index.js +2 -0
  92. package/dest/json-rpc/client/json_rpc_client.d.ts +22 -0
  93. package/dest/json-rpc/client/json_rpc_client.d.ts.map +1 -0
  94. package/dest/json-rpc/client/json_rpc_client.js +83 -0
  95. package/dest/json-rpc/client/json_rpc_client.test.d.ts +2 -0
  96. package/dest/json-rpc/client/json_rpc_client.test.d.ts.map +1 -0
  97. package/dest/json-rpc/client/json_rpc_client.test.js +20 -0
  98. package/dest/json-rpc/convert.d.ts +22 -0
  99. package/dest/json-rpc/convert.d.ts.map +1 -0
  100. package/dest/json-rpc/convert.js +93 -0
  101. package/dest/json-rpc/convert.test.d.ts +2 -0
  102. package/dest/json-rpc/convert.test.d.ts.map +1 -0
  103. package/dest/json-rpc/convert.test.js +14 -0
  104. package/dest/json-rpc/fixtures/test_state.d.ts +47 -0
  105. package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -0
  106. package/dest/json-rpc/fixtures/test_state.js +62 -0
  107. package/dest/json-rpc/index.d.ts +4 -0
  108. package/dest/json-rpc/index.d.ts.map +1 -0
  109. package/dest/json-rpc/index.js +4 -0
  110. package/dest/json-rpc/js_utils.d.ts +13 -0
  111. package/dest/json-rpc/js_utils.d.ts.map +1 -0
  112. package/dest/json-rpc/js_utils.js +18 -0
  113. package/dest/json-rpc/server/index.d.ts +3 -0
  114. package/dest/json-rpc/server/index.d.ts.map +1 -0
  115. package/dest/json-rpc/server/index.js +3 -0
  116. package/dest/json-rpc/server/json_proxy.d.ts +18 -0
  117. package/dest/json-rpc/server/json_proxy.d.ts.map +1 -0
  118. package/dest/json-rpc/server/json_proxy.js +35 -0
  119. package/dest/json-rpc/server/json_rpc_server.d.ts +34 -0
  120. package/dest/json-rpc/server/json_rpc_server.d.ts.map +1 -0
  121. package/dest/json-rpc/server/json_rpc_server.js +109 -0
  122. package/dest/json-rpc/server/json_rpc_server.test.d.ts +2 -0
  123. package/dest/json-rpc/server/json_rpc_server.test.d.ts.map +1 -0
  124. package/dest/json-rpc/server/json_rpc_server.test.js +22 -0
  125. package/dest/log/console.d.ts +16 -0
  126. package/dest/log/console.d.ts.map +1 -0
  127. package/dest/log/console.js +39 -0
  128. package/dest/log/debug.d.ts +46 -0
  129. package/dest/log/debug.d.ts.map +1 -0
  130. package/dest/log/debug.js +75 -0
  131. package/dest/log/index.d.ts +4 -0
  132. package/dest/log/index.d.ts.map +1 -0
  133. package/dest/log/index.js +4 -0
  134. package/dest/log/log_history.d.ts +31 -0
  135. package/dest/log/log_history.d.ts.map +1 -0
  136. package/dest/log/log_history.js +42 -0
  137. package/dest/log/log_history.test.d.ts +2 -0
  138. package/dest/log/log_history.test.d.ts.map +1 -0
  139. package/dest/log/log_history.test.js +78 -0
  140. package/dest/mutex/index.d.ts +53 -0
  141. package/dest/mutex/index.d.ts.map +1 -0
  142. package/dest/mutex/index.js +74 -0
  143. package/dest/mutex/mutex.test.d.ts +9 -0
  144. package/dest/mutex/mutex.test.d.ts.map +1 -0
  145. package/dest/mutex/mutex.test.js +58 -0
  146. package/dest/mutex/mutex_database.d.ts +10 -0
  147. package/dest/mutex/mutex_database.d.ts.map +1 -0
  148. package/dest/mutex/mutex_database.js +2 -0
  149. package/dest/retry/index.d.ts +33 -0
  150. package/dest/retry/index.d.ts.map +1 -0
  151. package/dest/retry/index.js +70 -0
  152. package/dest/running-promise/index.d.ts +35 -0
  153. package/dest/running-promise/index.d.ts.map +1 -0
  154. package/dest/running-promise/index.js +59 -0
  155. package/dest/serialize/buffer_reader.d.ts +188 -0
  156. package/dest/serialize/buffer_reader.d.ts.map +1 -0
  157. package/dest/serialize/buffer_reader.js +229 -0
  158. package/dest/serialize/buffer_reader.test.d.ts +2 -0
  159. package/dest/serialize/buffer_reader.test.d.ts.map +1 -0
  160. package/dest/serialize/buffer_reader.test.js +156 -0
  161. package/dest/serialize/deserializer.d.ts +134 -0
  162. package/dest/serialize/deserializer.d.ts.map +1 -0
  163. package/dest/serialize/deserializer.js +145 -0
  164. package/dest/serialize/free_funcs.d.ts +203 -0
  165. package/dest/serialize/free_funcs.d.ts.map +1 -0
  166. package/dest/serialize/free_funcs.js +250 -0
  167. package/dest/serialize/index.d.ts +6 -0
  168. package/dest/serialize/index.d.ts.map +1 -0
  169. package/dest/serialize/index.js +6 -0
  170. package/dest/serialize/serialize.test.d.ts +2 -0
  171. package/dest/serialize/serialize.test.d.ts.map +1 -0
  172. package/dest/serialize/serialize.test.js +68 -0
  173. package/dest/serialize/serializer.d.ts +89 -0
  174. package/dest/serialize/serializer.d.ts.map +1 -0
  175. package/dest/serialize/serializer.js +111 -0
  176. package/dest/serialize/types.d.ts +33 -0
  177. package/dest/serialize/types.d.ts.map +1 -0
  178. package/dest/serialize/types.js +22 -0
  179. package/dest/sleep/index.d.ts +52 -0
  180. package/dest/sleep/index.d.ts.map +1 -0
  181. package/dest/sleep/index.js +70 -0
  182. package/dest/timer/index.d.ts +3 -0
  183. package/dest/timer/index.d.ts.map +1 -0
  184. package/dest/timer/index.js +3 -0
  185. package/dest/timer/timeout.d.ts +41 -0
  186. package/dest/timer/timeout.d.ts.map +1 -0
  187. package/dest/timer/timeout.js +62 -0
  188. package/dest/timer/timer.d.ts +33 -0
  189. package/dest/timer/timer.d.ts.map +1 -0
  190. package/dest/timer/timer.js +38 -0
  191. package/dest/transport/browser/index.d.ts +5 -0
  192. package/dest/transport/browser/index.d.ts.map +1 -0
  193. package/dest/transport/browser/index.js +5 -0
  194. package/dest/transport/browser/message_port_socket.d.ts +37 -0
  195. package/dest/transport/browser/message_port_socket.d.ts.map +1 -0
  196. package/dest/transport/browser/message_port_socket.js +46 -0
  197. package/dest/transport/browser/shared_worker_connector.d.ts +19 -0
  198. package/dest/transport/browser/shared_worker_connector.d.ts.map +1 -0
  199. package/dest/transport/browser/shared_worker_connector.js +21 -0
  200. package/dest/transport/browser/shared_worker_listener.d.ts +38 -0
  201. package/dest/transport/browser/shared_worker_listener.d.ts.map +1 -0
  202. package/dest/transport/browser/shared_worker_listener.js +37 -0
  203. package/dest/transport/browser/worker_connector.d.ts +26 -0
  204. package/dest/transport/browser/worker_connector.d.ts.map +1 -0
  205. package/dest/transport/browser/worker_connector.js +30 -0
  206. package/dest/transport/browser/worker_listener.d.ts +39 -0
  207. package/dest/transport/browser/worker_listener.d.ts.map +1 -0
  208. package/dest/transport/browser/worker_listener.js +39 -0
  209. package/dest/transport/dispatch/create_dispatch_fn.d.ts +25 -0
  210. package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +1 -0
  211. package/dest/transport/dispatch/create_dispatch_fn.js +18 -0
  212. package/dest/transport/dispatch/create_dispatch_proxy.d.ts +102 -0
  213. package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -0
  214. package/dest/transport/dispatch/create_dispatch_proxy.js +56 -0
  215. package/dest/transport/dispatch/messages.d.ts +52 -0
  216. package/dest/transport/dispatch/messages.d.ts.map +1 -0
  217. package/dest/transport/dispatch/messages.js +12 -0
  218. package/dest/transport/index.d.ts +12 -0
  219. package/dest/transport/index.d.ts.map +1 -0
  220. package/dest/transport/index.js +12 -0
  221. package/dest/transport/interface/connector.d.ts +8 -0
  222. package/dest/transport/interface/connector.d.ts.map +1 -0
  223. package/dest/transport/interface/connector.js +2 -0
  224. package/dest/transport/interface/listener.d.ts +13 -0
  225. package/dest/transport/interface/listener.d.ts.map +1 -0
  226. package/dest/transport/interface/listener.js +2 -0
  227. package/dest/transport/interface/socket.d.ts +13 -0
  228. package/dest/transport/interface/socket.d.ts.map +1 -0
  229. package/dest/transport/interface/socket.js +2 -0
  230. package/dest/transport/interface/transferable.d.ts +68 -0
  231. package/dest/transport/interface/transferable.d.ts.map +1 -0
  232. package/dest/transport/interface/transferable.js +63 -0
  233. package/dest/transport/node/index.d.ts +3 -0
  234. package/dest/transport/node/index.d.ts.map +1 -0
  235. package/dest/transport/node/index.js +3 -0
  236. package/dest/transport/node/node_connector.d.ts +28 -0
  237. package/dest/transport/node/node_connector.d.ts.map +1 -0
  238. package/dest/transport/node/node_connector.js +28 -0
  239. package/dest/transport/node/node_connector_socket.d.ts +42 -0
  240. package/dest/transport/node/node_connector_socket.d.ts.map +1 -0
  241. package/dest/transport/node/node_connector_socket.js +48 -0
  242. package/dest/transport/node/node_listener.d.ts +26 -0
  243. package/dest/transport/node/node_listener.d.ts.map +1 -0
  244. package/dest/transport/node/node_listener.js +30 -0
  245. package/dest/transport/node/node_listener_socket.d.ts +37 -0
  246. package/dest/transport/node/node_listener_socket.d.ts.map +1 -0
  247. package/dest/transport/node/node_listener_socket.js +44 -0
  248. package/dest/transport/transport_client.d.ts +61 -0
  249. package/dest/transport/transport_client.d.ts.map +1 -0
  250. package/dest/transport/transport_client.js +94 -0
  251. package/dest/transport/transport_server.d.ts +56 -0
  252. package/dest/transport/transport_server.d.ts.map +1 -0
  253. package/dest/transport/transport_server.js +101 -0
  254. package/dest/types/index.d.ts +7 -0
  255. package/dest/types/index.d.ts.map +1 -0
  256. package/dest/types/index.js +2 -0
  257. package/dest/wasm/index.d.ts +5 -0
  258. package/dest/wasm/index.d.ts.map +1 -0
  259. package/dest/wasm/index.js +5 -0
  260. package/dest/wasm/wasm/empty_wasi_sdk.d.ts +130 -0
  261. package/dest/wasm/wasm/empty_wasi_sdk.d.ts.map +1 -0
  262. package/dest/wasm/wasm/empty_wasi_sdk.js +166 -0
  263. package/dest/wasm/wasm/index.d.ts +2 -0
  264. package/dest/wasm/wasm/index.d.ts.map +1 -0
  265. package/dest/wasm/wasm/index.js +2 -0
  266. package/dest/wasm/wasm/wasm_module.d.ts +135 -0
  267. package/dest/wasm/wasm/wasm_module.d.ts.map +1 -0
  268. package/dest/wasm/wasm/wasm_module.js +205 -0
  269. package/dest/wasm/wasm/wasm_module.test.d.ts +2 -0
  270. package/dest/wasm/wasm/wasm_module.test.d.ts.map +1 -0
  271. package/dest/wasm/wasm/wasm_module.test.js +24 -0
  272. package/dest/wasm/worker/browser/index.d.ts +3 -0
  273. package/dest/wasm/worker/browser/index.d.ts.map +1 -0
  274. package/dest/wasm/worker/browser/index.js +3 -0
  275. package/dest/wasm/worker/browser/start_web_module.d.ts +7 -0
  276. package/dest/wasm/worker/browser/start_web_module.d.ts.map +1 -0
  277. package/dest/wasm/worker/browser/start_web_module.js +22 -0
  278. package/dest/wasm/worker/browser/web_data_store.d.ts +23 -0
  279. package/dest/wasm/worker/browser/web_data_store.d.ts.map +1 -0
  280. package/dest/wasm/worker/browser/web_data_store.js +32 -0
  281. package/dest/wasm/worker/browser/web_worker.d.ts +10 -0
  282. package/dest/wasm/worker/browser/web_worker.d.ts.map +1 -0
  283. package/dest/wasm/worker/browser/web_worker.js +23 -0
  284. package/dest/wasm/worker/data_store.d.ts +9 -0
  285. package/dest/wasm/worker/data_store.d.ts.map +1 -0
  286. package/dest/wasm/worker/data_store.js +2 -0
  287. package/dest/wasm/worker/index.d.ts +3 -0
  288. package/dest/wasm/worker/index.d.ts.map +1 -0
  289. package/dest/wasm/worker/index.js +2 -0
  290. package/dest/wasm/worker/node/index.d.ts +3 -0
  291. package/dest/wasm/worker/node/index.d.ts.map +1 -0
  292. package/dest/wasm/worker/node/index.js +3 -0
  293. package/dest/wasm/worker/node/node_data_store.d.ts +22 -0
  294. package/dest/wasm/worker/node/node_data_store.d.ts.map +1 -0
  295. package/dest/wasm/worker/node/node_data_store.js +31 -0
  296. package/dest/wasm/worker/node/node_worker.d.ts +6 -0
  297. package/dest/wasm/worker/node/node_worker.d.ts.map +1 -0
  298. package/dest/wasm/worker/node/node_worker.js +20 -0
  299. package/dest/wasm/worker/node/start_node_module.d.ts +7 -0
  300. package/dest/wasm/worker/node/start_node_module.d.ts.map +1 -0
  301. package/dest/wasm/worker/node/start_node_module.js +26 -0
  302. package/dest/wasm/worker/wasm_worker.d.ts +9 -0
  303. package/dest/wasm/worker/wasm_worker.d.ts.map +1 -0
  304. package/dest/wasm/worker/wasm_worker.js +2 -0
  305. package/dest/wasm/worker/worker_pool.d.ts +40 -0
  306. package/dest/wasm/worker/worker_pool.d.ts.map +1 -0
  307. package/dest/wasm/worker/worker_pool.js +62 -0
  308. package/package.json +44 -0
  309. package/src/abi/index.ts +147 -0
  310. package/src/async-map/async_map.test.ts +9 -0
  311. package/src/async-map/index.ts +18 -0
  312. package/src/aztec-address/index.ts +135 -0
  313. package/src/bigint-buffer/bigint-buffer.test.ts +21 -0
  314. package/src/bigint-buffer/index.ts +68 -0
  315. package/src/collection/array.ts +48 -0
  316. package/src/collection/index.ts +1 -0
  317. package/src/crypto/index.ts +3 -0
  318. package/src/crypto/keccak/index.ts +33 -0
  319. package/src/crypto/random/index.test.ts +13 -0
  320. package/src/crypto/random/index.ts +38 -0
  321. package/src/crypto/sha256/index.test.ts +13 -0
  322. package/src/crypto/sha256/index.ts +15 -0
  323. package/src/errors/index.ts +6 -0
  324. package/src/eth-address/eth_address.test.ts +104 -0
  325. package/src/eth-address/index.ts +236 -0
  326. package/src/fields/coordinate.test.ts +26 -0
  327. package/src/fields/coordinate.ts +104 -0
  328. package/src/fields/fields.ts +234 -0
  329. package/src/fields/index.ts +3 -0
  330. package/src/fields/point.ts +143 -0
  331. package/src/fifo/bounded_serial_queue.ts +100 -0
  332. package/src/fifo/index.ts +4 -0
  333. package/src/fifo/memory_fifo.ts +114 -0
  334. package/src/fifo/semaphore.ts +33 -0
  335. package/src/fifo/serial_queue.ts +78 -0
  336. package/src/json-rpc/README.md +55 -0
  337. package/src/json-rpc/class_converter.ts +160 -0
  338. package/src/json-rpc/client/index.ts +1 -0
  339. package/src/json-rpc/client/json_rpc_client.test.ts +20 -0
  340. package/src/json-rpc/client/json_rpc_client.ts +103 -0
  341. package/src/json-rpc/convert.test.ts +15 -0
  342. package/src/json-rpc/convert.ts +109 -0
  343. package/src/json-rpc/fixtures/test_state.ts +59 -0
  344. package/src/json-rpc/index.ts +3 -0
  345. package/src/json-rpc/js_utils.ts +20 -0
  346. package/src/json-rpc/server/index.ts +2 -0
  347. package/src/json-rpc/server/json_proxy.ts +36 -0
  348. package/src/json-rpc/server/json_rpc_server.test.ts +23 -0
  349. package/src/json-rpc/server/json_rpc_server.ts +124 -0
  350. package/src/log/console.ts +45 -0
  351. package/src/log/debug.ts +86 -0
  352. package/src/log/index.ts +3 -0
  353. package/src/log/log_history.test.ts +87 -0
  354. package/src/log/log_history.ts +44 -0
  355. package/src/mutex/index.ts +83 -0
  356. package/src/mutex/mutex.test.ts +75 -0
  357. package/src/mutex/mutex_database.ts +9 -0
  358. package/src/retry/index.ts +78 -0
  359. package/src/running-promise/index.ts +60 -0
  360. package/src/serialize/buffer_reader.test.ts +175 -0
  361. package/src/serialize/buffer_reader.ts +272 -0
  362. package/src/serialize/deserializer.ts +180 -0
  363. package/src/serialize/free_funcs.ts +285 -0
  364. package/src/serialize/index.ts +5 -0
  365. package/src/serialize/serialize.test.ts +89 -0
  366. package/src/serialize/serializer.ts +128 -0
  367. package/src/serialize/types.ts +40 -0
  368. package/src/sleep/index.ts +71 -0
  369. package/src/timer/index.ts +2 -0
  370. package/src/timer/timeout.ts +64 -0
  371. package/src/timer/timer.ts +41 -0
  372. package/src/transport/browser/index.ts +4 -0
  373. package/src/transport/browser/message_port_socket.ts +48 -0
  374. package/src/transport/browser/shared_worker_connector.ts +21 -0
  375. package/src/transport/browser/shared_worker_listener.ts +52 -0
  376. package/src/transport/browser/worker_connector.ts +30 -0
  377. package/src/transport/browser/worker_listener.ts +53 -0
  378. package/src/transport/dispatch/create_dispatch_fn.ts +33 -0
  379. package/src/transport/dispatch/create_dispatch_proxy.ts +140 -0
  380. package/src/transport/dispatch/messages.ts +58 -0
  381. package/src/transport/index.ts +11 -0
  382. package/src/transport/interface/connector.ts +8 -0
  383. package/src/transport/interface/listener.ts +14 -0
  384. package/src/transport/interface/socket.ts +12 -0
  385. package/src/transport/interface/transferable.ts +121 -0
  386. package/src/transport/node/index.ts +2 -0
  387. package/src/transport/node/node_connector.ts +29 -0
  388. package/src/transport/node/node_connector_socket.ts +51 -0
  389. package/src/transport/node/node_listener.ts +33 -0
  390. package/src/transport/node/node_listener_socket.ts +47 -0
  391. package/src/transport/transport_client.ts +125 -0
  392. package/src/transport/transport_server.ts +108 -0
  393. package/src/types/index.ts +7 -0
  394. package/src/wasm/README.md +6 -0
  395. package/src/wasm/fixtures/gcd.wasm +0 -0
  396. package/src/wasm/fixtures/gcd.wat +27 -0
  397. package/src/wasm/index.ts +4 -0
  398. package/src/wasm/wasm/empty_wasi_sdk.ts +166 -0
  399. package/src/wasm/wasm/index.ts +1 -0
  400. package/src/wasm/wasm/wasm_module.test.ts +27 -0
  401. package/src/wasm/wasm/wasm_module.ts +257 -0
  402. package/src/wasm/worker/browser/index.ts +2 -0
  403. package/src/wasm/worker/browser/start_web_module.ts +23 -0
  404. package/src/wasm/worker/browser/web_data_store.ts +37 -0
  405. package/src/wasm/worker/browser/web_worker.ts +24 -0
  406. package/src/wasm/worker/data_store.ts +7 -0
  407. package/src/wasm/worker/index.ts +2 -0
  408. package/src/wasm/worker/node/index.ts +2 -0
  409. package/src/wasm/worker/node/node_data_store.ts +36 -0
  410. package/src/wasm/worker/node/node_worker.ts +21 -0
  411. package/src/wasm/worker/node/start_node_module.ts +28 -0
  412. package/src/wasm/worker/wasm_worker.ts +7 -0
  413. package/src/wasm/worker/worker_pool.ts +73 -0
  414. package/tsconfig.json +10 -0
@@ -0,0 +1,234 @@
1
+ import { randomBytes } from 'crypto';
2
+ import { BufferReader } from '../serialize/buffer_reader.js';
3
+ import { toBigIntBE, toBufferBE, toHex } from '../bigint-buffer/index.js';
4
+
5
+ /**
6
+ * Fr represents a field of integers modulo the prime number MODULUS.
7
+ * It provides utility functions to work with elements in this field, such as conversions between different representations and checks for equality and zero values. The elements can be serialized to and deserialized from byte buffers or strings.
8
+ * Some use cases include working with cryptographic operations and finite fields.
9
+ */
10
+ export class Fr {
11
+ static ZERO = new Fr(0n);
12
+ static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
13
+ static MAX_VALUE = Fr.MODULUS - 1n;
14
+ static SIZE_IN_BYTES = 32;
15
+
16
+ /**
17
+ * The numeric value of the field element as a bigint.
18
+ */
19
+ public readonly value;
20
+
21
+ constructor(value: bigint | number) {
22
+ this.value = BigInt(value);
23
+ // if (value > Fr.MAX_VALUE) {
24
+ // throw new Error(`Fr out of range ${value}.`);
25
+ // }
26
+ }
27
+
28
+ /**
29
+ * Generates a random Fr or Fq instance with a value modulo the respective class' MODULUS.
30
+ * This method uses randomBytes to generate a random 32-byte buffer, converts it to a bigint
31
+ * and takes the modulus of the result with the class' MODULUS constant.
32
+ *
33
+ * @returns A new Fr or Fq instance with a random value.
34
+ */
35
+ static random() {
36
+ const r = toBigIntBE(randomBytes(Fr.SIZE_IN_BYTES)) % Fr.MODULUS;
37
+ return new Fr(r);
38
+ }
39
+
40
+ /**
41
+ * Returns a new zero-value field.
42
+ * @returns A new zero-value field.
43
+ */
44
+ static zero() {
45
+ return new Fr(0n);
46
+ }
47
+
48
+ /**
49
+ * Create an instance of the corresponding class (Fr or Fq) from a Buffer or a BufferReader.
50
+ * Reads 'SIZE_IN_BYTES' bytes from the given Buffer or BufferReader and constructs an instance with the decoded value.
51
+ * If the input is a Buffer, it is internally converted to a BufferReader before reading.
52
+ * Throws an error if the input length is invalid or the decoded value is out of range.
53
+ *
54
+ * @param buffer - The Buffer or BufferReader containing the bytes representing the value.
55
+ * @returns An instance of the corresponding class (Fr or Fq) with the decoded value.
56
+ */
57
+ static fromBuffer(buffer: Buffer | BufferReader) {
58
+ const reader = BufferReader.asReader(buffer);
59
+ return new Fr(toBigIntBE(reader.readBytes(Fr.SIZE_IN_BYTES)));
60
+ }
61
+
62
+ /**
63
+ * Create a Fr instance from a hex-encoded string.
64
+ * The input 'address' can be either prefixed with '0x' or not, and should have exactly 64 hex characters.
65
+ * Throws an error if the input length is invalid or the address value is out of range.
66
+ *
67
+ * @param address - The hex-encoded string representing the field element.
68
+ * @returns A Fr instance.
69
+ */
70
+ static fromString(address: string) {
71
+ return Fr.fromBuffer(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
72
+ }
73
+
74
+ /**
75
+ * Converts the value of the instance to a buffer with a specified length.
76
+ * The method uses the provided value and size in bytes to create a buffer representation
77
+ * of the numeric value. This can be useful for serialization and communication purposes.
78
+ *
79
+ * @returns A buffer representing the instance's value.
80
+ */
81
+ toBuffer() {
82
+ return toBufferBE(this.value, Fr.SIZE_IN_BYTES);
83
+ }
84
+
85
+ /**
86
+ * Converts the value of the Fr or Fq class instance to a hexadecimal string.
87
+ * The resulting string is prefixed with '0x' and represents the bigint value
88
+ * in base 16.
89
+ *
90
+ * @param padTo32 - Whether to pad the string to 32 bytes.
91
+ * @returns A hex-encoded string representing the value of the class instance.
92
+ */
93
+ toString(padTo32 = false): `0x${string}` {
94
+ return toHex(this.value, padTo32);
95
+ }
96
+
97
+ /**
98
+ * Returns a shortened string representation of the Fr value, formatted with '0x' prefix and ellipsis in the middle.
99
+ * The resulting string has first 10 characters (including '0x') and last 4 characters of the full hexadecimal value.
100
+ *
101
+ * @returns A shorter, human-readable string representation of the Fr value.
102
+ */
103
+ toShortString() {
104
+ const str = this.toString();
105
+ return `${str.slice(0, 10)}...${str.slice(-4)}`;
106
+ }
107
+
108
+ /**
109
+ * Checks if the provided Fr instance is equal to the current instance.
110
+ * Two instances are considered equal if their 'value' properties are the same.
111
+ *
112
+ * @param rhs - The Fr instance to compare with the current instance.
113
+ * @returns A boolean indicating whether the two instances are equal.
114
+ */
115
+ equals(rhs: Fr) {
116
+ return this.value === rhs.value;
117
+ }
118
+
119
+ /**
120
+ * Check if the instance value is zero.
121
+ * The method returns true if the value of the instance is 0n (zero in BigInt representation),
122
+ * otherwise, it returns false.
123
+ *
124
+ * @returns A boolean indicating whether the instance value is zero or not.
125
+ */
126
+ isZero() {
127
+ return this.value === 0n;
128
+ }
129
+
130
+ /**
131
+ * Converts the current value of the Fq or Fr instance to a friendly JSON representation.
132
+ * The output will be a hexadecimal string prefixed with '0x'.
133
+ *
134
+ * @returns A '0x' prefixed hexadecimal string representing the current value.
135
+ */
136
+ toFriendlyJSON() {
137
+ return this.toString();
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Fq represents a field element in a prime finite field with modulus defined by the constant MODULUS.
143
+ * It provides methods for creating, manipulating, and comparing field elements, as well as converting
144
+ * them to/from different data types like Buffers and hex strings. Field elements are used in various
145
+ * cryptographic protocols and operations, such as elliptic curve cryptography.
146
+ *
147
+ * @example
148
+ * const fqElem = new Fq(BigInt("123456789"));
149
+ * const randomFqElem = Fq.random();
150
+ * const fromBufferFqElem = Fq.fromBuffer(buffer);
151
+ */
152
+ export class Fq {
153
+ static MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n;
154
+ static MAX_VALUE = Fr.MODULUS - 1n;
155
+ static SIZE_IN_BYTES = 32;
156
+
157
+ constructor(
158
+ /**
159
+ * The element's value as a bigint in the finite field.
160
+ */
161
+ public readonly value: bigint,
162
+ ) {
163
+ if (value > Fq.MAX_VALUE) {
164
+ throw new Error(`Fr out of range ${value}.`);
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Generates a random Fr or Fq instance with a value within the range of their respective modulus.
170
+ * The random value is generated from a byte array of length equal to SIZE_IN_BYTES, then truncated
171
+ * to the appropriate modulus before creating the new Fr or Fq instance.
172
+ *
173
+ * @returns A new Fr or Fq instance with a randomly generated value.
174
+ */
175
+ static random() {
176
+ const r = toBigIntBE(randomBytes(64)) % Fq.MODULUS;
177
+ return new this(r);
178
+ }
179
+
180
+ /**
181
+ * Create an instance of the calling class (Fr or Fq) from a given buffer or BufferReader.
182
+ * Reads SIZE_IN_BYTES from the provided buffer and converts it to a bigint, then creates a new instance
183
+ * with that value. Throws an error if the value is out of range for the calling class.
184
+ *
185
+ * @param buffer - The input buffer or BufferReader containing the bytes representing the value.
186
+ * @returns An instance of the calling class (Fr or Fq) initialized with the bigint value.
187
+ */
188
+ static fromBuffer(buffer: Buffer | BufferReader) {
189
+ const reader = BufferReader.asReader(buffer);
190
+ return new this(toBigIntBE(reader.readBytes(this.SIZE_IN_BYTES)));
191
+ }
192
+
193
+ /**
194
+ * Converts the bigint value of the instance to a Buffer representation.
195
+ * The output buffer has a fixed size, determined by the 'SIZE_IN_BYTES' constant.
196
+ *
197
+ * @returns A Buffer containing the byte representation of the instance's value.
198
+ */
199
+ toBuffer() {
200
+ return toBufferBE(this.value, Fq.SIZE_IN_BYTES);
201
+ }
202
+
203
+ /**
204
+ * Converts the Fq value to a hexadecimal string representation.
205
+ * The resulting string is prefixed with '0x' and contains the exact number of hex characters required
206
+ * to represent the numeric value of this instance.
207
+ *
208
+ * @returns A hexadecimal string representing the Fq value.
209
+ */
210
+ toString() {
211
+ return toHex(this.value);
212
+ }
213
+
214
+ /**
215
+ * Check if the value of the current instance is zero.
216
+ * This function compares the internal 'value' property with 0n (BigInt representation of zero).
217
+ * Returns true if the value is zero, otherwise returns false.
218
+ *
219
+ * @returns A boolean indicating whether the value is zero or not.
220
+ */
221
+ isZero() {
222
+ return this.value === 0n;
223
+ }
224
+
225
+ /**
226
+ * Converts the value of the Fr or Fq instance to a friendly JSON format.
227
+ * The output is a hexadecimal string representation of the value with '0x' prefix.
228
+ *
229
+ * @returns A string representing the value in the JSON format.
230
+ */
231
+ toFriendlyJSON() {
232
+ return this.toString();
233
+ }
234
+ }
@@ -0,0 +1,3 @@
1
+ export * from './fields.js';
2
+ export * from './point.js';
3
+ export * from './coordinate.js';
@@ -0,0 +1,143 @@
1
+ import { BufferReader } from '../serialize/buffer_reader.js';
2
+ import { Coordinate } from './coordinate.js';
3
+
4
+ /**
5
+ * Represents a Point on an elliptic curve with x and y coordinates.
6
+ * The Point class provides methods for creating instances from different input types,
7
+ * converting instances to various output formats, and checking the equality of points.
8
+ */
9
+ export class Point {
10
+ static ZERO = new Point(Coordinate.ZERO, Coordinate.ZERO);
11
+
12
+ /** Used to differentiate this class from AztecAddress */
13
+ public readonly kind = 'point';
14
+
15
+ constructor(
16
+ /**
17
+ * The point's x coordinate
18
+ */
19
+ public readonly x: Coordinate,
20
+ /**
21
+ * The point's y coordinate
22
+ */
23
+ public readonly y: Coordinate,
24
+ ) {}
25
+
26
+ /**
27
+ * Generate a random Point instance.
28
+ *
29
+ * @returns A randomly generated Point instance.
30
+ */
31
+ static random() {
32
+ // TODO is this a random point on the curve?
33
+ return new Point(Coordinate.random(), Coordinate.random());
34
+ }
35
+
36
+ /**
37
+ * Create a Point instance from a given buffer or BufferReader.
38
+ * The input 'buffer' should have exactly 64 bytes representing the x and y coordinates.
39
+ *
40
+ * @param buffer - The buffer or BufferReader containing the x and y coordinates of the point.
41
+ * @returns A Point instance.
42
+ */
43
+ static fromBuffer(buffer: Buffer | BufferReader) {
44
+ const reader = BufferReader.asReader(buffer);
45
+ return new this(Coordinate.fromBuffer(reader.readBytes(32)), Coordinate.fromBuffer(reader.readBytes(32)));
46
+ }
47
+
48
+ /**
49
+ * Creates a point instance from its x and y coordinates.
50
+ * @param x - X coordinate.
51
+ * @param y - Y coordinate.
52
+ * @returns A Point instance.
53
+ */
54
+ static fromCoordinates(x: Coordinate, y: Coordinate) {
55
+ return new this(x, y);
56
+ }
57
+
58
+ /**
59
+ * Create a Point instance from a hex-encoded string.
60
+ * The input 'address' should be prefixed with '0x' or not, and have exactly 128 hex characters representing the x and y coordinates.
61
+ * Throws an error if the input length is invalid or coordinate values are out of range.
62
+ *
63
+ * @param address - The hex-encoded string representing the Point coordinates.
64
+ * @returns A Point instance.
65
+ */
66
+ static fromString(address: string) {
67
+ return this.fromBuffer(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
68
+ }
69
+
70
+ /**
71
+ * Convert the Point instance to a Buffer representation of fields
72
+ * The output buffer's length will be (128 bytes).
73
+ * This method is useful for serialization and deserialization of the Point object.
74
+ *
75
+ * @returns A Buffer representation of the Point instance.
76
+ */
77
+ toFieldsBuffer() {
78
+ return Buffer.concat([this.x.toFieldsBuffer(), this.y.toFieldsBuffer()]);
79
+ }
80
+
81
+ /**
82
+ * Returns the contents of the point as an array of 4 fields.
83
+ * @returns The point as an array of 4 fields
84
+ */
85
+ toFields() {
86
+ return this.x.toFields().concat(this.y.toFields());
87
+ }
88
+
89
+ /**
90
+ * Converts the Point instance to a Buffer representaion of the coordinates.
91
+ * The outputs buffer length will be 64, the length of both coordinates not represented as fields.
92
+ * @returns A Buffer representation of the Point instance.
93
+ */
94
+ toBuffer() {
95
+ return Buffer.concat([this.x.toBuffer(), this.y.toBuffer()]);
96
+ }
97
+
98
+ /**
99
+ * Convert the Point instance to a hexadecimal string representation.
100
+ * The output string is prefixed with '0x' and consists of exactly 128 hex characters,
101
+ * representing the concatenated x and y coordinates of the point.
102
+ *
103
+ * @returns A hex-encoded string representing the Point instance.
104
+ */
105
+ toString() {
106
+ return '0x' + this.toBuffer().toString('hex');
107
+ }
108
+
109
+ /**
110
+ * Generate a short string representation of the Point instance.
111
+ * The returned string includes the first 10 and last 4 characters of the full string representation,
112
+ * with '...' in between to indicate truncation. This is useful for displaying or logging purposes
113
+ * when the full string representation may be too long.
114
+ *
115
+ * @returns A truncated string representation of the Point instance.
116
+ */
117
+ toShortString() {
118
+ const str = this.toString();
119
+ return `${str.slice(0, 10)}...${str.slice(-4)}`;
120
+ }
121
+
122
+ /**
123
+ * Check if two Point instances are equal by comparing their buffer values.
124
+ * Returns true if the buffer values are the same, and false otherwise.
125
+ *
126
+ * @param rhs - The Point instance to compare with the current instance.
127
+ * @returns A boolean indicating whether the two Point instances are equal.
128
+ */
129
+ equals(rhs: Point) {
130
+ return this.x.equals(rhs.x) && this.y.equals(rhs.y);
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Does this object look like a point?
136
+ * @param obj - Object to test if it is a point.
137
+ * @returns Whether it looks like a point.
138
+ */
139
+ export function isPoint(obj: object): obj is Point {
140
+ if (!obj) return false;
141
+ const point = obj as Point;
142
+ return point.kind === 'point' && point.x !== undefined && point.y !== undefined;
143
+ }
@@ -0,0 +1,100 @@
1
+ import { createLogger } from '../log/console.js';
2
+ import { Semaphore } from './semaphore.js';
3
+ import { SerialQueue } from './serial_queue.js';
4
+
5
+ /**
6
+ * Leverages the unbounded SerialQueue and Semaphore to create a SerialQueue that will block when putting an item
7
+ * if the queue size = maxQueueSize.
8
+ */
9
+ export class BoundedSerialQueue {
10
+ private readonly queue = new SerialQueue();
11
+ private semaphore: Semaphore;
12
+
13
+ constructor(maxQueueSize: number, private log = createLogger('aztec:foundation:bounded_serial_queue')) {
14
+ this.semaphore = new Semaphore(maxQueueSize);
15
+ }
16
+
17
+ /**
18
+ * Initializes the underlying SerialQueue instance, allowing items to be processed from the queue.
19
+ * The start method should be called before using the BoundedSerialQueue to ensure proper functionality.
20
+ */
21
+ public start() {
22
+ this.queue.start();
23
+ }
24
+
25
+ /**
26
+ * Returns the current number of items in the queue.
27
+ * This is useful for monitoring the size of BoundedSerialQueue and understanding its utilization.
28
+ *
29
+ * @returns The length of the queue as an integer value.
30
+ */
31
+ public length() {
32
+ return this.queue.length();
33
+ }
34
+
35
+ /**
36
+ * Cancels the current operation in the SerialQueue, if any, and clears the queue.
37
+ * Any pending tasks in the queue will not be executed, and the queue will be emptied.
38
+ * This method is useful for cleaning up resources and stopping ongoing processes when they are no longer needed.
39
+ * @returns A promise, resolved once cancelled.
40
+ */
41
+ public cancel() {
42
+ return this.queue.cancel();
43
+ }
44
+
45
+ /**
46
+ * Ends the queue processing gracefully, preventing new items from being added.
47
+ * The currently executing item, if any, will complete and remaining queued items
48
+ * will be processed in order. Once all items have been processed, the queue becomes
49
+ * permanently unusable.
50
+ *
51
+ * @returns A promise that resolves when all items in the queue have been processed.
52
+ */
53
+ public end() {
54
+ return this.queue.end();
55
+ }
56
+
57
+ /**
58
+ * The caller will block until fn is succesfully enqueued.
59
+ * The fn itself is execute asyncronously and its result discarded.
60
+ * TODO(AD) do we need this if we have exec()?
61
+ * @param fn - The function to call once unblocked.
62
+ */
63
+ public async put(fn: () => Promise<void>): Promise<void> {
64
+ await this.semaphore.acquire();
65
+ this.queue
66
+ .put(async () => {
67
+ try {
68
+ await fn();
69
+ } finally {
70
+ this.semaphore.release();
71
+ }
72
+ })
73
+ .catch(err => {
74
+ this.log('BoundedSerialQueue handler exception:', err);
75
+ });
76
+ }
77
+
78
+ /**
79
+ * The caller will block until fn is successfully executed, and it's result returned.
80
+ * @param fn - The function.
81
+ * @returns A promise that resolves with the result once exceuted.
82
+ */
83
+ public async exec<T>(fn: () => Promise<T>): Promise<T> {
84
+ await this.semaphore.acquire();
85
+ return this.queue.put(async () => {
86
+ try {
87
+ return await fn();
88
+ } finally {
89
+ this.semaphore.release();
90
+ }
91
+ });
92
+ }
93
+
94
+ /**
95
+ * Awaiting this ensures the queue is empty before resuming.
96
+ */
97
+ public async syncPoint() {
98
+ await this.queue.syncPoint();
99
+ }
100
+ }
@@ -0,0 +1,4 @@
1
+ export * from './memory_fifo.js';
2
+ export * from './serial_queue.js';
3
+ export * from './bounded_serial_queue.js';
4
+ export * from './semaphore.js';
@@ -0,0 +1,114 @@
1
+ import { createLogger } from '../log/console.js';
2
+
3
+ /**
4
+ * A simple fifo queue. It can grow unbounded. It can have multiple producers and consumers.
5
+ * Putting an item onto the queue always succeeds, unless either end() or cancel() has been called in which case
6
+ * the item being pushed is simply discarded.
7
+ */
8
+ export class MemoryFifo<T> {
9
+ private waiting: ((item: T | null) => void)[] = [];
10
+ private items: T[] = [];
11
+ private flushing = false;
12
+
13
+ constructor(private log = createLogger('aztec:foundation:memory_fifo')) {}
14
+
15
+ /**
16
+ * Returns the current number of items in the queue.
17
+ * The length represents the size of the queue at the time of invocation and may change as new items are added or consumed.
18
+ *
19
+ * @returns The number of items in the queue.
20
+ */
21
+ public length() {
22
+ return this.items.length;
23
+ }
24
+
25
+ /**
26
+ * Returns next item within the queue, or blocks until and item has been put into the queue.
27
+ * If given a timeout, the promise will reject if no item is received after `timeout` seconds.
28
+ * If the queue is flushing, `null` is returned.
29
+ * @param timeout - The timeout in seconds.
30
+ * @returns A result promise.
31
+ */
32
+ public get(timeout?: number): Promise<T | null> {
33
+ if (this.items.length) {
34
+ return Promise.resolve(this.items.shift()!);
35
+ }
36
+
37
+ if (this.items.length === 0 && this.flushing) {
38
+ return Promise.resolve(null);
39
+ }
40
+
41
+ return new Promise<T | null>((resolve, reject) => {
42
+ this.waiting.push(resolve);
43
+
44
+ if (timeout) {
45
+ setTimeout(() => {
46
+ const index = this.waiting.findIndex(r => r === resolve);
47
+ if (index > -1) {
48
+ this.waiting.splice(index, 1);
49
+ const err = new Error('Timeout getting item from queue.');
50
+ reject(err);
51
+ }
52
+ }, timeout * 1000);
53
+ }
54
+ });
55
+ }
56
+
57
+ /**
58
+ * Put an item onto back of the queue.
59
+ * @param item - The item to enqueue.
60
+ */
61
+ public put(item: T) {
62
+ if (this.flushing) {
63
+ return;
64
+ } else if (this.waiting.length) {
65
+ this.waiting.shift()!(item);
66
+ } else {
67
+ this.items.push(item);
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Once ended, no further items are added to queue. Consumers will consume remaining items within the queue.
73
+ * The queue is not reusable after calling `end()`.
74
+ * Any consumers waiting for an item receive null.
75
+ */
76
+ public end() {
77
+ this.flushing = true;
78
+ this.waiting.forEach(resolve => resolve(null));
79
+ }
80
+
81
+ /**
82
+ * Once cancelled, all items are discarded from the queue, and no further items are added to the queue.
83
+ * The queue is not reusable after calling `cancel()`.
84
+ * Any consumers waiting for an item receive null.
85
+ */
86
+ public cancel() {
87
+ this.flushing = true;
88
+ this.items = [];
89
+ this.waiting.forEach(resolve => resolve(null));
90
+ }
91
+
92
+ /**
93
+ * Process items from the queue using a provided handler function.
94
+ * The function iterates over items in the queue, invoking the handler for each item until the queue is empty or flushing.
95
+ * If the handler throws an error, it will be caught and logged as 'Queue handler exception:', but the iteration will continue.
96
+ * The process function returns a promise that resolves when there are no more items in the queue or the queue is flushing.
97
+ *
98
+ * @param handler - A function that takes an item of type T and returns a Promise<void> after processing the item.
99
+ * @returns A Promise<void> that resolves when the queue is finished processing.
100
+ */
101
+ public async process(handler: (item: T) => Promise<void>) {
102
+ try {
103
+ while (true) {
104
+ const item = await this.get();
105
+ if (item === null) {
106
+ break;
107
+ }
108
+ await handler(item);
109
+ }
110
+ } catch (err) {
111
+ this.log('Queue handler exception:', err);
112
+ }
113
+ }
114
+ }
@@ -0,0 +1,33 @@
1
+ import { MemoryFifo } from './memory_fifo.js';
2
+
3
+ /**
4
+ * Allows the acquiring of up to `size` tokens before calls to acquire block, waiting for a call to release().
5
+ */
6
+ export class Semaphore {
7
+ private readonly queue = new MemoryFifo<boolean>();
8
+
9
+ constructor(size: number) {
10
+ new Array(size).fill(true).map(() => this.queue.put(true));
11
+ }
12
+
13
+ /**
14
+ * Acquires a token from the Semaphore, allowing access to a limited resource.
15
+ * If no tokens are available, the call will block and wait until a token is released.
16
+ * Use in conjunction with the release() method to manage access to resources with limited capacity.
17
+ *
18
+ * @returns A Promise that resolves when a token is acquired.
19
+ */
20
+ public async acquire() {
21
+ await this.queue.get();
22
+ }
23
+
24
+ /**
25
+ * Releases a token back into the semaphore, allowing another acquire call to proceed.
26
+ * If there are any pending calls to acquire(), one of them will be unblocked and allowed to proceed.
27
+ * This method should only be called by the holder of the acquired token to ensure proper functionality
28
+ * and avoid unexpected behavior.
29
+ */
30
+ public release() {
31
+ this.queue.put(true);
32
+ }
33
+ }