@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,64 @@
1
+ /**
2
+ * TimeoutTask class creates an instance for managing and executing a given asynchronous function with a specified timeout duration.
3
+ * The task will be automatically interrupted if it exceeds the given timeout duration, and will throw a custom error message.
4
+ * Additional information such as execution time can be retrieved using getTime method after the task has been executed.
5
+ *
6
+ * @typeparam T - The return type of the asynchronous function to be executed.
7
+ */
8
+ export class TimeoutTask<T> {
9
+ private interruptPromise!: Promise<any>;
10
+ private interrupt = () => {};
11
+ private totalTime = 0;
12
+
13
+ constructor(private fn: () => Promise<T>, private timeout = 0, fnName = '') {
14
+ this.interruptPromise = new Promise<T>((_, reject) => {
15
+ this.interrupt = () => reject(new Error(`Timeout${fnName ? ` running ${fnName}` : ''} after ${timeout}ms.`));
16
+ });
17
+ }
18
+
19
+ /**
20
+ * Executes the given function with a specified timeout.
21
+ * If the function takes longer than the timeout, it will be interrupted and an error will be thrown.
22
+ * The total execution time of the function will be stored in the totalTime property.
23
+ *
24
+ * @returns The result of the executed function if completed within the timeout.
25
+ * @throws An error with a message indicating the function was interrupted due to exceeding the specified timeout.
26
+ */
27
+ public async exec() {
28
+ const interruptTimeout = !this.timeout ? 0 : setTimeout(this.interrupt, this.timeout);
29
+ try {
30
+ const start = Date.now();
31
+ const result = await Promise.race<T>([this.fn(), this.interruptPromise]);
32
+ this.totalTime = Date.now() - start;
33
+ return result;
34
+ } finally {
35
+ clearTimeout(interruptTimeout);
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Returns the interrupt promise associated with the TimeoutTask instance.
41
+ * The interrupt promise is used internally to reject the task when a timeout occurs.
42
+ * This method can be helpful when debugging or tracking the state of the task.
43
+ *
44
+ * @returns The interrupt promise associated with the task.
45
+ */
46
+ public getInterruptPromise() {
47
+ return this.interruptPromise;
48
+ }
49
+
50
+ /**
51
+ * Get the total time spent on the most recent execution of the wrapped function.
52
+ * This method provides the duration from the start to the end of the function execution, whether it completed or timed out.
53
+ *
54
+ * @returns The total time in milliseconds spent on the most recent function execution.
55
+ */
56
+ public getTime() {
57
+ return this.totalTime;
58
+ }
59
+ }
60
+
61
+ export const executeTimeout = async <T>(fn: () => Promise<T>, timeout = 0, fnName = '') => {
62
+ const task = new TimeoutTask(fn, timeout, fnName);
63
+ return await task.exec();
64
+ };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Timer class to measure time intervals in milliseconds and seconds.
3
+ * Upon instantiation, it stores the current timestamp as the starting point.
4
+ * The 'ms()' method returns the elapsed time in milliseconds,
5
+ * while the 's()' method returns the elapsed time in seconds.
6
+ *
7
+ * @example
8
+ * const timer = new Timer();
9
+ * setTimeout(() =\> \{
10
+ * console.log(`Elapsed time: ${timer.ms()} ms`);
11
+ * \}, 1000);
12
+ */
13
+ export class Timer {
14
+ private start: number;
15
+
16
+ constructor() {
17
+ this.start = new Date().getTime();
18
+ }
19
+
20
+ /**
21
+ * Returns the elapsed time in milliseconds since the Timer instance was created.
22
+ * Provides a simple and convenient way to measure the time duration between two events
23
+ * or monitor performance of specific code sections.
24
+ *
25
+ * @returns The elapsed time in milliseconds.
26
+ */
27
+ public ms() {
28
+ return new Date().getTime() - this.start;
29
+ }
30
+
31
+ /**
32
+ * Returns the time elapsed since the Timer instance was created, in seconds.
33
+ * The value is calculated by subtracting the initial start time from the current time
34
+ * and dividing the result by 1000 to convert milliseconds to seconds.
35
+ *
36
+ * @returns The elapsed time in seconds.
37
+ */
38
+ public s() {
39
+ return (new Date().getTime() - this.start) / 1000;
40
+ }
41
+ }
@@ -0,0 +1,4 @@
1
+ export * from './worker_connector.js';
2
+ export * from './worker_listener.js';
3
+ export * from './shared_worker_connector.js';
4
+ export * from './shared_worker_listener.js';
@@ -0,0 +1,48 @@
1
+ import { Socket } from '../interface/socket.js';
2
+
3
+ /**
4
+ * An implementation of a TransportSocket using MessagePorts.
5
+ */
6
+ export class MessagePortSocket implements Socket {
7
+ constructor(private port: MessagePort) {}
8
+
9
+ /**
10
+ * Send a message to the connected MessagePort, optionally transferring ownership of certain objects.
11
+ * The 'msg' parameter can be any structured data type and will be sent to the other end of the MessagePort.
12
+ * The optional 'transfer' parameter is an array of Transferable objects whose ownership will be transferred,
13
+ * making them inaccessible on the sending side. This can improve performance for large data transfers.
14
+ *
15
+ * @param msg - The message to be sent through the MessagePort.
16
+ * @param transfer - An optional array of Transferable objects to transfer ownership.
17
+ * @returns A Promise that resolves when the message has been sent.
18
+ */
19
+ send(msg: any, transfer: Transferable[] = []): Promise<void> {
20
+ this.port.postMessage(msg, transfer);
21
+ return Promise.resolve();
22
+ }
23
+
24
+ /**
25
+ * Register a callback function to handle incoming messages from the MessagePort.
26
+ * The provided callback will be invoked with the message data whenever a new message arrives.
27
+ * Note that only one callback can be registered at a time. Subsequent calls to this method
28
+ * will overwrite the previously registered callback.
29
+ *
30
+ * @param cb - The callback function to handle incoming messages.
31
+ */
32
+ registerHandler(cb: (msg: any) => any): void {
33
+ this.port.onmessage = event => cb(event.data);
34
+ }
35
+
36
+ /**
37
+ * Close the MessagePort, unregister the message handler, and send an undefined message.
38
+ * The 'close' function is useful for gracefully shutting down a connection between two
39
+ * endpoints by sending an undefined message as an indication of disconnection before
40
+ * closing the port. After calling this method, the MessagePortSocket instance should not
41
+ * be used again.
42
+ */
43
+ close() {
44
+ void this.send(undefined);
45
+ this.port.onmessage = null;
46
+ this.port.close();
47
+ }
48
+ }
@@ -0,0 +1,21 @@
1
+ import { Connector } from '../interface/connector.js';
2
+ import { MessagePortSocket } from './message_port_socket.js';
3
+
4
+ /**
5
+ * SharedWorkerConnector is an implementation of the Connector interface, specifically for SharedWorkers.
6
+ * It enables the creation of MessagePortSockets that communicate with a shared worker and allow
7
+ * multiple scripts to communicate with the worker using the same connection.
8
+ */
9
+ export class SharedWorkerConnector implements Connector {
10
+ constructor(private worker: SharedWorker) {}
11
+
12
+ /**
13
+ * Creates a new MessagePortSocket instance using the SharedWorker's port.
14
+ * This method allows for easy creation of sockets to communicate with the SharedWorker.
15
+ *
16
+ * @returns A Promise that resolves to a new MessagePortSocket instance.
17
+ */
18
+ createSocket() {
19
+ return Promise.resolve(new MessagePortSocket(this.worker.port));
20
+ }
21
+ }
@@ -0,0 +1,52 @@
1
+ import EventEmitter from 'events';
2
+ import { Listener } from '../interface/listener.js';
3
+ import { MessagePortSocket } from './message_port_socket.js';
4
+
5
+ /**
6
+ * Represents the global scope of a Shared Worker.
7
+ * Provides functionality to handle incoming connections and manage communication with other scripts
8
+ * running in a shared context, enabling concurrent access and efficient resource sharing among those scripts.
9
+ */
10
+ declare interface SharedWorkerGlobalScope {
11
+ /**
12
+ * Event handler for new connections to the Shared Worker.
13
+ */
14
+ onconnect: (...args: any) => any;
15
+ }
16
+
17
+ /**
18
+ * SharedWorkerListener is an extension of the EventEmitter class that implements the Listener interface.
19
+ * It provides functionality to handle incoming messages from a shared worker and emit events for new sockets
20
+ * created in response to these incoming connections. This class is meant to be used in the context of managing
21
+ * MessagePort connections within the SharedWorkerGlobalScope.
22
+ */
23
+ export class SharedWorkerListener extends EventEmitter implements Listener {
24
+ constructor(private worker: SharedWorkerGlobalScope) {
25
+ super();
26
+ }
27
+
28
+ /**
29
+ * Initializes the shared worker listener by assigning the 'handleMessageEvent' method as the event handler
30
+ * for the 'onconnect' event of the SharedWorkerGlobalScope. The 'handleMessageEvent' function will be called
31
+ * whenever a new connection is established with the shared worker.
32
+ */
33
+ open() {
34
+ this.worker.onconnect = this.handleMessageEvent;
35
+ }
36
+
37
+ /**
38
+ * Closes the SharedWorkerListener by detaching the 'onconnect' event handler.
39
+ * This stops the listener from emitting new sockets on incoming connections.
40
+ */
41
+ close() {
42
+ this.worker.onconnect = () => {};
43
+ }
44
+
45
+ private handleMessageEvent = (event: MessageEvent) => {
46
+ const [port] = event.ports;
47
+ if (!port) {
48
+ return;
49
+ }
50
+ this.emit('new_socket', new MessagePortSocket(port));
51
+ };
52
+ }
@@ -0,0 +1,30 @@
1
+ import { Connector } from '../interface/connector.js';
2
+ import { MessagePortSocket } from './message_port_socket.js';
3
+
4
+ /**
5
+ * WorkerConnector is a class implementing the Connector interface for creating communication sockets
6
+ * with Web Workers. It allows to establish a connection with the worker and create MessagePortSockets
7
+ * using MessageChannels, enabling seamless communication between the main thread and worker threads.
8
+ *
9
+ * @example
10
+ * const worker = new Worker('./myWorker.js');
11
+ * const connector = new WorkerConnector(worker);
12
+ * const socket = await connector.createSocket();
13
+ * socket.send('Hello, worker!');
14
+ */
15
+ export class WorkerConnector implements Connector {
16
+ constructor(private worker: Worker) {}
17
+
18
+ /**
19
+ * Creates a new MessagePortSocket instance by establishing a connection between the Worker and the main thread.
20
+ * A MessageChannel is created, and one of its ports is sent to the Worker using postMessage.
21
+ * The other port is used to create a new MessagePortSocket which is then returned as a Promise.
22
+ *
23
+ * @returns A Promise that resolves to a new MessagePortSocket instance.
24
+ */
25
+ createSocket() {
26
+ const channel = new MessageChannel();
27
+ this.worker.postMessage('', [channel.port2]);
28
+ return Promise.resolve(new MessagePortSocket(channel.port1));
29
+ }
30
+ }
@@ -0,0 +1,53 @@
1
+ import EventEmitter from 'events';
2
+ import { Listener } from '../interface/listener.js';
3
+ import { MessagePortSocket } from './message_port_socket.js';
4
+
5
+ /**
6
+ * Represents a DedicatedWorkerGlobalScope, which is the global execution context for a dedicated worker.
7
+ * Provides properties and methods to manage the worker's lifecycle and communication with other threads or workers.
8
+ */
9
+ declare interface DedicatedWorkerGlobalScope {
10
+ /**
11
+ * Handler for incoming messages from other threads or workers.
12
+ */
13
+ onmessage: any;
14
+ }
15
+
16
+ /**
17
+ * WorkerListener is a class that extends EventEmitter and implements the Listener interface.
18
+ * It listens for incoming connections on a dedicated worker global scope, and emits a 'new_socket' event
19
+ * with a MessagePortSocket instance for each new connection. This allows applications to communicate
20
+ * with other workers or main thread through the MessagePortSocket abstraction.
21
+ *
22
+ * The open() method starts listening for incoming connections, while the close() method stops it.
23
+ */
24
+ export class WorkerListener extends EventEmitter implements Listener {
25
+ constructor(private worker: DedicatedWorkerGlobalScope) {
26
+ super();
27
+ }
28
+
29
+ /**
30
+ * Initializes the WorkerListener by setting the 'onmessage' event handler of the worker.
31
+ * The 'onmessage' event will be triggered when the worker receives a message, and it will then
32
+ * call the handleMessageEvent method to handle incoming connections.
33
+ */
34
+ open() {
35
+ this.worker.onmessage = this.handleMessageEvent;
36
+ }
37
+
38
+ /**
39
+ * Close the worker listener by removing the 'onmessage' event handler.
40
+ * This method effectively stops the WorkerListener from reacting to new incoming messages.
41
+ */
42
+ close() {
43
+ this.worker.onmessage = () => {};
44
+ }
45
+
46
+ private handleMessageEvent = (event: MessageEvent) => {
47
+ const [port] = event.ports;
48
+ if (!port) {
49
+ return;
50
+ }
51
+ this.emit('new_socket', new MessagePortSocket(port));
52
+ };
53
+ }
@@ -0,0 +1,33 @@
1
+ import { createDebugLogger } from '../../log/debug.js';
2
+
3
+ /**
4
+ * Represents a message object for dispatching function calls.
5
+ * Contains the function name ('fn') and an array of arguments ('args') required to call the target method.
6
+ */
7
+ export interface DispatchMsg {
8
+ /**
9
+ * Name of the target method to be called.
10
+ */
11
+ fn: string;
12
+ /**
13
+ * An array of arguments to be passed to the target method.
14
+ */
15
+ args: any[];
16
+ }
17
+
18
+ /**
19
+ * Creates a dispatch function that calls the target's specified method with provided arguments.
20
+ * The created dispatch function takes a DispatchMsg object as input, which contains the name of
21
+ * the method to be called ('fn') and an array of arguments to be passed to the method ('args').
22
+ *
23
+ * @param targetFn - A function that returns the target object containing the methods to be dispatched.
24
+ * @param debug - Optional logging function for debugging purposes.
25
+ * @returns A dispatch function that accepts a DispatchMsg object and calls the target's method with provided arguments.
26
+ */
27
+ export function createDispatchFn(targetFn: () => any, debug = createDebugLogger('aztec:foundation:dispatch')) {
28
+ return async ({ fn, args }: DispatchMsg) => {
29
+ const target = targetFn();
30
+ debug(`dispatching to ${target}: ${fn}`, args);
31
+ return await target[fn](...args);
32
+ };
33
+ }
@@ -0,0 +1,140 @@
1
+ import { DispatchMsg } from './create_dispatch_fn.js';
2
+ import { TransportClient } from '../transport_client.js';
3
+ import { EventEmitter } from 'events';
4
+ import { isTransferDescriptor, TransferDescriptor } from '../interface/transferable.js';
5
+
6
+ /**
7
+ * FilterOutAttributes type filters out all non-method properties of an object, leaving only the attributes
8
+ * that are functions. This is useful for creating proxies or wrappers around objects while focusing only
9
+ * on their methods and ignoring other properties.
10
+ */
11
+ type FilterOutAttributes<Base> = {
12
+ [Key in keyof Base]: Base[Key] extends (...any: any) => any ? Base[Key] : never;
13
+ };
14
+
15
+ /**
16
+ * Takes a function type `F` and returns a new function type with the same input parameters as `F`,
17
+ * but returning a Promise of the original return type of `F`. This is useful for converting sync
18
+ * functions or functions that take callbacks into a version that returns a Promise.
19
+ */
20
+ type PromisifyFunction<F extends (...any: any) => any> = (...args: Parameters<F>) => Promise<ReturnType<F>>;
21
+
22
+ /**
23
+ * Transforms the provided object type by converting each of its function properties into their
24
+ * promise-returning counterparts. If a function property already returns a promise, it remains unchanged.
25
+ * This is useful when wrapping synchronous methods to return promises in order to standardize the API for
26
+ * asynchronous operations.
27
+ *
28
+ * @typeParam Base - The type of the object whose function properties need to be converted into their
29
+ * promise-returning versions.
30
+ */
31
+ type Promisify<Base extends { [key: string]: (...any: any) => any }> = {
32
+ [Key in keyof Base]: ReturnType<Base[Key]> extends Promise<any> ? Base[Key] : PromisifyFunction<Base[Key]>;
33
+ };
34
+
35
+ /**
36
+ * Type that transforms a tuple of types, replacing each type 'T' with either 'T' or a `TransferDescriptor<T>` if 'T' is `Transferable`.
37
+ * This is useful for handling arguments of functions that may accept both original and transferable representations of objects.
38
+ */
39
+ type TransferTypes<Tuple extends [...args: any]> = {
40
+ [Index in keyof Tuple]: Tuple[Index] | (Tuple[Index] extends Transferable ? TransferDescriptor<Tuple[Index]> : never);
41
+ };
42
+
43
+ /**
44
+ * Annoying.
45
+ * @see https://github.com/microsoft/TypeScript/issues/29919
46
+ * There's a bug that means we can't map over the tuple or function parameter types to make them transferrable, if
47
+ * we use the Parameters builtin, and then try to map.
48
+ * So instead we inline the Parameters builtin and apply the TransferTypes to the parameters within the inline.
49
+ * Once the above is fixed we could in theory just do:
50
+ *
51
+ * type MakeFunctionTransferrable\<TFunction extends (...args: any) =\> any\> = (
52
+ * ...args: TransferTypes\<Parameters\<TFunction\>\>
53
+ * ) =\> ReturnType<TFunction>;
54
+ */
55
+ type MakeFunctionTransferrable<TFunction extends (...args: any) => any> = (
56
+ ...args: TFunction extends (...args: infer P) => any ? TransferTypes<P> : never
57
+ ) => ReturnType<TFunction>;
58
+
59
+ /**
60
+ * Transferrable type represents a utility type that maps over the provided Base object's methods,
61
+ * transforming their argument types to support transferable objects. This is useful when dealing
62
+ * with operations across different environments or threads, such as Web Workers or Node.js processes,
63
+ * where certain objects need to be transferred instead of being serialized and deserialized.
64
+ */
65
+ type Transferrable<Base extends { [key: string]: (...any: any) => any }> = {
66
+ [Key in keyof Base]: MakeFunctionTransferrable<Base[Key]>;
67
+ };
68
+
69
+ /**
70
+ * Proxify is a mapped type that takes an object with functions as its properties and returns
71
+ * a new object with the same properties, but with each function transformed to return a Promise
72
+ * and accept Transferable types in place of their original parameters. This type is useful for
73
+ * creating proxies that communicate over different environments or threads while maintaining
74
+ * the original class's method signatures, allowing for type-safe interaction with remote instances.
75
+ */
76
+ export type Proxify<T> = Promisify<Transferrable<FilterOutAttributes<T>>>;
77
+
78
+ /**
79
+ * Creates a proxy object for the provided class, wrapping each method in a request function.
80
+ * The resulting proxy object allows invoking methods of the original class, but their execution
81
+ * is delegated to the request function. This is useful when executing methods across different
82
+ * environments or threads, such as Web Workers or Node.js processes.
83
+ *
84
+ * @typeParam T - The type of the class to create a proxy for.
85
+ * @param class_ - The class constructor to create a proxy for.
86
+ * @param requestFn - A higher-order function that takes a method name and returns a function
87
+ * with the same signature as the original method, which wraps the actual
88
+ * invocation in a custom logic (e.g., sending a message to another thread).
89
+ * @returns An object whose methods match those of the original class, but whose execution is
90
+ * delegated to the provided request function.
91
+ */
92
+ export function createDispatchProxyFromFn<T>(
93
+ class_: { new (...args: any[]): T },
94
+ requestFn: (fn: string) => (...args: any[]) => Promise<any>,
95
+ ): Proxify<T> {
96
+ const proxy: any = class_.prototype instanceof EventEmitter ? new EventEmitter() : {};
97
+ for (const fn of Object.getOwnPropertyNames(class_.prototype)) {
98
+ if (fn === 'constructor') {
99
+ continue;
100
+ }
101
+ proxy[fn] = requestFn(fn);
102
+ }
103
+ return proxy;
104
+ }
105
+
106
+ /**
107
+ * Creates a proxy for the given class that transparently dispatches method calls over a transport client.
108
+ * The proxy allows calling methods on remote instances of the class through the provided transport client
109
+ * while maintaining the correct return types and handling promises. If the class inherits from EventEmitter,
110
+ * it also handles event emissions through the transport client.
111
+ *
112
+ * @param class_ - The constructor function of the class to create the proxy for.
113
+ * @param transportClient - The TransportClient instance used to handle communication between proxies.
114
+ * @returns A proxified version of the given class with methods dispatched over the transport client.
115
+ */
116
+ export function createDispatchProxy<T>(
117
+ class_: { new (...args: any[]): T },
118
+ transportClient: TransportClient<DispatchMsg>,
119
+ ): Proxify<T> {
120
+ // Create a proxy of class_ that passes along methods over our transportClient
121
+ const proxy = createDispatchProxyFromFn(class_, (fn: string) => (...args: any[]) => {
122
+ // Pass our proxied function name and arguments over our transport client
123
+ const transfer: Transferable[] = args.reduce(
124
+ (acc, a) => (isTransferDescriptor(a) ? [...acc, ...a.transferables] : acc),
125
+ [] as Transferable[],
126
+ );
127
+ args = args.map(a => (isTransferDescriptor(a) ? a.send : a));
128
+ return transportClient.request({ fn, args }, transfer);
129
+ });
130
+ if (proxy instanceof EventEmitter) {
131
+ // Handle proxied 'emit' calls if our proxy object is an EventEmitter
132
+ transportClient.on('event_msg', ({ fn, args }) => {
133
+ if (fn === 'emit') {
134
+ const [eventName, ...restArgs] = args;
135
+ proxy.emit(eventName, ...restArgs);
136
+ }
137
+ });
138
+ }
139
+ return proxy;
140
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Represents a request message.
3
+ * Contains a unique identifier (msgId) and a payload object.
4
+ */
5
+ export interface RequestMessage<Payload> {
6
+ /**
7
+ * A unique identifier for a message.
8
+ */
9
+ msgId: number;
10
+ /**
11
+ * The data content carried within a message.
12
+ */
13
+ payload: Payload;
14
+ }
15
+
16
+ /**
17
+ * Represents a structured response message.
18
+ * Contains an identifier to match with the corresponding request.
19
+ */
20
+ export interface ResponseMessage<Payload> {
21
+ /**
22
+ * A unique identifier for the message.
23
+ */
24
+ msgId: number;
25
+ /**
26
+ * The data content carried within the message.
27
+ */
28
+ payload?: Payload;
29
+ /**
30
+ * An optional error description in case the response contains an error instead of a payload.
31
+ */
32
+ error?: string;
33
+ }
34
+
35
+ /**
36
+ * Represents an event-based message in a communication system.
37
+ * Contains a payload with the relevant data associated with a specific event occurrence.
38
+ */
39
+ export interface EventMessage<Payload> {
40
+ /**
41
+ * The data content associated with a message.
42
+ */
43
+ payload: Payload;
44
+ }
45
+
46
+ /**
47
+ * Determines if the given 'msg' is an EventMessage by checking if its 'msgId' property is undefined.
48
+ * Returns true if the input message is of type EventMessage, otherwise false. This utility function can be used
49
+ * to differentiate between instances of ResponseMessage and EventMessage that share a common Payload type.
50
+ *
51
+ * @param msg - The message object that can be either a ResponseMessage or EventMessage with a specific payload.
52
+ * @returns A boolean value indicating whether the input message is an EventMessage (true) or not (false).
53
+ */
54
+ export function isEventMessage<Payload>(
55
+ msg: ResponseMessage<Payload> | EventMessage<Payload>,
56
+ ): msg is EventMessage<Payload> {
57
+ return (msg as ResponseMessage<Payload>).msgId === undefined;
58
+ }
@@ -0,0 +1,11 @@
1
+ export * from './dispatch/create_dispatch_fn.js';
2
+ export * from './dispatch/create_dispatch_proxy.js';
3
+ export * from './dispatch/messages.js';
4
+ export * from './interface/connector.js';
5
+ export * from './interface/listener.js';
6
+ export * from './interface/socket.js';
7
+ export * from './interface/transferable.js';
8
+ export * from './transport_client.js';
9
+ export * from './transport_server.js';
10
+ export * from './browser/index.js';
11
+ export * from './node/index.js';
@@ -0,0 +1,8 @@
1
+ import { Socket } from './socket.js';
2
+
3
+ /**
4
+ * Opens a socket with corresponding TransportListener.
5
+ */
6
+ export interface Connector {
7
+ createSocket(): Promise<Socket>;
8
+ }
@@ -0,0 +1,14 @@
1
+ import EventEmitter from 'events';
2
+ import { Socket } from './socket.js';
3
+
4
+ /**
5
+ * Once opened, an implementation of a TransportListener will emit `new_socket` events as new clients connect.
6
+ * Possible implementations could include MessageChannels or WebSockets.
7
+ */
8
+ export interface Listener extends EventEmitter {
9
+ open(): void;
10
+
11
+ close(): void;
12
+
13
+ on(name: 'new_socket', cb: (client: Socket) => void): this;
14
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Represents one end of a socket connection.
3
+ * A message sent via `send` will be handled by the corresponding Socket's handler function at the other end.
4
+ * Implementations could use e.g. MessagePorts for communication between browser workers,
5
+ * or WebSockets for communication between processes.
6
+ * If `registerHandler` callback receives `undefined` that signals the other end closed.
7
+ */
8
+ export interface Socket {
9
+ send(msg: any, transfer?: Transferable[]): Promise<void>;
10
+ registerHandler(cb: (msg: any) => any): void;
11
+ close(): void;
12
+ }