@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,78 @@
1
+ import { MemoryFifo } from './memory_fifo.js';
2
+
3
+ /**
4
+ * A more specialised fifo queue that enqueues functions to execute. Enqueued functions are executed in serial.
5
+ */
6
+ export class SerialQueue {
7
+ private readonly queue = new MemoryFifo<() => Promise<void>>();
8
+ private runningPromise!: Promise<void>;
9
+
10
+ /**
11
+ * Initializes the execution of enqueued functions in the serial queue.
12
+ * Functions are executed in the order they were added to the queue, with each function
13
+ * waiting for the completion of the previous one before starting its execution.
14
+ * This method should be called once to start processing the queue.
15
+ */
16
+ public start() {
17
+ this.runningPromise = this.queue.process(fn => fn());
18
+ }
19
+
20
+ /**
21
+ * Returns the current number of enqueued functions in the serial queue.
22
+ * This provides a way to check the size of the queue and monitor its progress.
23
+ *
24
+ * @returns The length of the serial queue as a number.
25
+ */
26
+ public length() {
27
+ return this.queue.length();
28
+ }
29
+
30
+ /**
31
+ * Cancels the processing of the remaining functions in the serial queue and resolves the running promise.
32
+ * Any enqueued functions that have not yet been executed will be discarded. The queue can still accept new
33
+ * functions after cancellation, but the previously enqueued functions will not be re-processed.
34
+ *
35
+ * @returns The running promise which resolves when the current executing function (if any) completes.
36
+ */
37
+ public cancel() {
38
+ this.queue.cancel();
39
+ return this.runningPromise;
40
+ }
41
+
42
+ /**
43
+ * Signals the SerialQueue that it should finish processing its current task and stop accepting new tasks.
44
+ * The returned Promise resolves when all enqueued tasks have completed execution.
45
+ *
46
+ * @returns A Promise that resolves when the queue is completely emptied and no new tasks are allowed.
47
+ */
48
+ public end() {
49
+ this.queue.end();
50
+ return this.runningPromise;
51
+ }
52
+
53
+ /**
54
+ * Enqueues fn for execution on the serial queue.
55
+ * Returns the result of the function after execution.
56
+ * @param fn - The function to enqueue.
57
+ * @returns A resolution promise.
58
+ */
59
+ public put<T>(fn: () => Promise<T>): Promise<T> {
60
+ return new Promise((resolve, reject) => {
61
+ this.queue.put(async () => {
62
+ try {
63
+ const res = await fn();
64
+ resolve(res);
65
+ } catch (e) {
66
+ reject(e);
67
+ }
68
+ });
69
+ });
70
+ }
71
+
72
+ /**
73
+ * Awaiting this ensures the queue is empty before resuming.
74
+ */
75
+ public async syncPoint() {
76
+ await this.put(async () => {});
77
+ }
78
+ }
@@ -0,0 +1,55 @@
1
+ # json-rpc
2
+
3
+ json-rpc
4
+
5
+ ```
6
+ -- src
7
+ -- client
8
+ Code to use by a client wishing to use a json-rpc server
9
+ Includes syntax sugar for making requests with normal method syntax
10
+ -- server
11
+ Code for easily turning a class into an exposed RPC with one endpoint per method
12
+ ```
13
+
14
+ Each createJsonRpcClient and JsonRpcServer call needs a map of classes that will be translated in input and output values.
15
+ By default, Buffer is handled, but other usermade classes need to define toString() and static fromString() like so:
16
+
17
+ ```
18
+ class PublicKey {
19
+ toString() {
20
+ return '...';
21
+ }
22
+ static fromString(str) {
23
+ return new PublicKey(...);
24
+ }
25
+ }
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ In Dapp:
31
+
32
+ ```
33
+ const wallet = createJsonRpcClient<WalletImplementation>('wallet-server.com', /*register classes*/ {PublicKey, TxRequest});
34
+ const response = await wallet.signTxRequest(accountPubKey, txRequest);
35
+ ```
36
+
37
+ The client will send `[{ name: 'PublicKey', value: accountPubKey.toString() }, { name: 'TxRequest', txRequest.toString() }]` to the server.
38
+
39
+ In wallet:
40
+
41
+ ```
42
+ const publicClient = createJsonRpcClient<PublicClient>('public-client.com', /*register classes*/ ...);
43
+ const keyStore = createJsonRpcClient<KeyStore>('key-store.com', /*register classes*/ ...);
44
+ ```
45
+
46
+ Different clients for different services.
47
+
48
+ -- server
49
+ Running a wallet server:
50
+
51
+ ```
52
+ const wallet = new WalletImplementation();
53
+ const server = new JsonRpcServer(wallet, /*register classes*/ ...);
54
+ server.start(8080);
55
+ ```
@@ -0,0 +1,160 @@
1
+ import { assert, hasOwnProperty } from './js_utils.js';
2
+
3
+ /**
4
+ * Represents a class compatible with our class conversion system.
5
+ * E.g. PublicKey here satisfies 'IOClass'.
6
+ * ```
7
+ * class PublicKey {
8
+ * toString() {
9
+ * return '...';
10
+ * }
11
+ * static fromString(str) {
12
+ * return new PublicKey(...);
13
+ * }
14
+ * }
15
+ * ```
16
+ */
17
+ interface IOClass {
18
+ new (...args: any): any;
19
+ /**
20
+ * Creates an IOClass from a given string.
21
+ */
22
+ fromString?: (str: string) => any;
23
+
24
+ /**
25
+ * Creates an IOClass from a given JSON object.
26
+ */
27
+ fromJSON?: (obj: object) => any;
28
+ }
29
+
30
+ /**
31
+ * Registered classes available for conversion.
32
+ */
33
+ export interface ClassConverterInput {
34
+ [className: string]: IOClass;
35
+ }
36
+
37
+ /**
38
+ * Represents a class in a JSON-friendly encoding.
39
+ */
40
+ export interface StringEncodedClass {
41
+ /**
42
+ * The class type.
43
+ */
44
+ type: string;
45
+ /**
46
+ * The class data string.
47
+ */
48
+ data: string;
49
+ }
50
+
51
+ /**
52
+ * Represents a class in a JSON-friendly encoding.
53
+ */
54
+ export interface JsonEncodedClass {
55
+ /**
56
+ * The class type.
57
+ */
58
+ type: string;
59
+ /**
60
+ * The class data string.
61
+ */
62
+ data: object;
63
+ }
64
+ /**
65
+ * Whether a class is a complex object or simply represented by a string.
66
+ */
67
+ export type ClassEncoding = 'string' | 'object';
68
+
69
+ /**
70
+ * Handles mapping of classes to names, and calling toString and fromString to convert to and from JSON-friendly formats.
71
+ * Takes a class map as input.
72
+ */
73
+ export class ClassConverter {
74
+ private toClass = new Map<string, [IOClass, ClassEncoding]>();
75
+ private toName = new Map<IOClass, [string, ClassEncoding]>();
76
+
77
+ /**
78
+ * Create a class converter from a table of classes.
79
+ * @param stringClassMap - The class table of string encoded classes.
80
+ * @param objectClassMap - The class table of complex object classes
81
+ */
82
+ constructor(stringClassMap?: ClassConverterInput, objectClassMap?: ClassConverterInput) {
83
+ if (stringClassMap) {
84
+ for (const key of Object.keys(stringClassMap)) {
85
+ this.register(key, stringClassMap[key], 'string');
86
+ }
87
+ }
88
+ if (objectClassMap) {
89
+ for (const key of Object.keys(objectClassMap)) {
90
+ this.register(key, objectClassMap[key], 'object');
91
+ }
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Register a class with a certain name.
97
+ * This name is used for conversion from and to this class.
98
+ * @param type - The class name to use for serialization.
99
+ * @param class_ - The class object.
100
+ * @param encoding - Whether the class is a complex object or simply represented by a string.
101
+ */
102
+ register(type: string, class_: IOClass, encoding: ClassEncoding) {
103
+ assert(type !== 'Buffer', "'Buffer' handling is hardcoded. Cannot use as name.");
104
+ assert(
105
+ hasOwnProperty(class_.prototype, 'toString') || hasOwnProperty(class_.prototype, 'toJSON'),
106
+ `Class ${type} must define a toString() OR toJSON() method.`,
107
+ );
108
+ assert(
109
+ class_['fromString'] || class_['fromJSON'],
110
+ `Class ${type} must define a fromString() OR fromJSON() static method.`,
111
+ );
112
+ this.toName.set(class_, [type, encoding]);
113
+ this.toClass.set(type, [class_, encoding]);
114
+ }
115
+
116
+ /**
117
+ * Does this type name have a registered class?
118
+ * @param type - The type name.
119
+ * @returns If there's a registered class.
120
+ */
121
+ isRegisteredClassName(type: string) {
122
+ return this.toClass.has(type);
123
+ }
124
+ /**
125
+ * Is this class object registered?
126
+ * @param obj - The class object.
127
+ * @returns If it is a registered class.
128
+ */
129
+ isRegisteredClass(obj: any) {
130
+ return this.toName.has(obj);
131
+ }
132
+ /**
133
+ * Convert a JSON-like object to a class object.
134
+ * @param jsonObj - An object encoding a class.
135
+ * @returns The class object.
136
+ */
137
+ toClassObj(jsonObj: JsonEncodedClass | StringEncodedClass): any {
138
+ const result = this.toClass.get(jsonObj.type);
139
+ assert(result, `Could not find type in lookup.`);
140
+
141
+ const [class_, encoding] = result;
142
+ if (encoding === 'string' && typeof jsonObj.data === 'string') {
143
+ return class_!.fromString!(jsonObj.data);
144
+ } else {
145
+ return class_!.fromJSON!(jsonObj.data as object);
146
+ }
147
+ }
148
+ /**
149
+ * Convert a class object to a JSON object.
150
+ * @param classObj - A JSON encoding a class.
151
+ * @returns The class object.
152
+ */
153
+ toJsonObj(classObj: any): JsonEncodedClass | StringEncodedClass {
154
+ const result = this.toName.get(classObj.constructor);
155
+ assert(result, `Could not find class in lookup.`);
156
+ const [type, encoding] = result;
157
+ const data = encoding === 'string' ? classObj.toString() : classObj.toJSON();
158
+ return { type: type!, data };
159
+ }
160
+ }
@@ -0,0 +1 @@
1
+ export { createJsonRpcClient, JsonStringify } from './json_rpc_client.js';
@@ -0,0 +1,20 @@
1
+ import request from 'supertest';
2
+ import { JsonRpcServer } from '../server/json_rpc_server.js';
3
+ import { TestState, TestNote } from '../fixtures/test_state.js';
4
+ import { createJsonRpcClient } from './json_rpc_client.js';
5
+
6
+ test('test an RPC function over client', async () => {
7
+ const mockFetch = async (host: string, method: string, body: any) => {
8
+ const server = new JsonRpcServer(new TestState([new TestNote('a'), new TestNote('b')]), { TestNote }, {}, true);
9
+ const result = await request(server.getApp().callback()).post(`/${method}`).send(body);
10
+ return JSON.parse(result.text);
11
+ };
12
+ const client = createJsonRpcClient<TestState>('', { TestNote }, {}, true, mockFetch);
13
+ const result = await client.addNotes([new TestNote('c')]);
14
+ expect(result[0]).toBeInstanceOf(TestNote);
15
+ expect(result[1]).toBeInstanceOf(TestNote);
16
+ expect(result[2]).toBeInstanceOf(TestNote);
17
+ expect(result[0].toString()).toBe('a');
18
+ expect(result[1].toString()).toBe('b');
19
+ expect(result[2].toString()).toBe('c');
20
+ });
@@ -0,0 +1,103 @@
1
+ // comlink:
2
+ // Dev dependency just for the somewhat complex RemoteObject type
3
+ // This takes a {foo(): T} and makes {foo(): Promise<T>}
4
+ // while avoiding Promise of Promise.
5
+ import { RemoteObject } from 'comlink';
6
+ import { createDebugLogger } from '../../log/index.js';
7
+ import { retry } from '../../retry/index.js';
8
+ import { ClassConverter, ClassConverterInput } from '../class_converter.js';
9
+ import { JsonStringify, convertFromJsonObj, convertToJsonObj } from '../convert.js';
10
+
11
+ export { JsonStringify } from '../convert.js';
12
+
13
+ const debug = createDebugLogger('json-rpc:json_rpc_client');
14
+ /**
15
+ * A normal fetch function that does not retry.
16
+ * Alternatives are a fetch function with retries, or a mocked fetch.
17
+ * @param host - The host URL.
18
+ * @param method - The RPC method name.
19
+ * @param body - The RPC payload.
20
+ * @returns The parsed JSON response, or throws an error.
21
+ */
22
+ export async function defaultFetch(host: string, rpcMethod: string, body: any, useApiEndpoints: boolean) {
23
+ debug(`JsonRpcClient.fetch`, host, rpcMethod, '->', body);
24
+ let resp: Response;
25
+ if (useApiEndpoints) {
26
+ resp = await fetch(`${host}/${rpcMethod}`, {
27
+ method: 'POST',
28
+ body: JsonStringify(body),
29
+ headers: { 'content-type': 'application/json' },
30
+ });
31
+ } else {
32
+ resp = await fetch(host, {
33
+ method: 'POST',
34
+ body: JsonStringify({ ...body, method: rpcMethod }),
35
+ headers: { 'content-type': 'application/json' },
36
+ });
37
+ }
38
+
39
+ if (!resp.ok) {
40
+ throw new Error(resp.statusText);
41
+ }
42
+
43
+ const text = await resp.text();
44
+ try {
45
+ return JSON.parse(text);
46
+ } catch (err) {
47
+ throw new Error(`Failed to parse body as JSON: ${text}`);
48
+ }
49
+ }
50
+
51
+ /**
52
+ * A fetch function with retries.
53
+ */
54
+ export async function mustSucceedFetch(host: string, rpcMethod: string, body: any, useApiEndpoints: boolean) {
55
+ return await retry(() => defaultFetch(host, rpcMethod, body, useApiEndpoints), 'JsonRpcClient request');
56
+ }
57
+
58
+ /**
59
+ * Creates a Proxy object that delegates over RPC and satisfies RemoteObject<T>.
60
+ * The server should have ran new JsonRpcServer().
61
+ */
62
+ export function createJsonRpcClient<T extends object>(
63
+ host: string,
64
+ stringClassMap: ClassConverterInput,
65
+ objectClassMap: ClassConverterInput,
66
+ useApiEndpoints: boolean,
67
+ fetch = defaultFetch,
68
+ ) {
69
+ const classConverter = new ClassConverter(stringClassMap, objectClassMap);
70
+ let id = 0;
71
+ const request = async (method: string, params: any[]): Promise<any> => {
72
+ const body = {
73
+ jsonrpc: '2.0',
74
+ id: id++,
75
+ method,
76
+ params: params.map(param => convertToJsonObj(classConverter, param)),
77
+ };
78
+ debug(`JsonRpcClient.request`, method, '<-', params);
79
+ const res = await fetch(host, method, body, useApiEndpoints);
80
+ debug(`JsonRpcClient.result`, method, '->', res);
81
+ if (res.error) {
82
+ throw res.error;
83
+ }
84
+ if ([null, undefined, 'null', 'undefined'].includes(res.result)) {
85
+ return;
86
+ }
87
+ return convertFromJsonObj(classConverter, JSON.parse(res.result));
88
+ };
89
+
90
+ // Intercept any RPC methods with a proxy
91
+ // This wraps 'request' with a method-call syntax wrapper
92
+ return new Proxy(
93
+ {},
94
+ {
95
+ get:
96
+ (_, rpcMethod: string) =>
97
+ (...params: any[]) => {
98
+ debug(`JsonRpcClient.constructor`, 'proxy', rpcMethod, '<-', params);
99
+ return request(rpcMethod, params);
100
+ },
101
+ },
102
+ ) as RemoteObject<T>;
103
+ }
@@ -0,0 +1,15 @@
1
+ import { ClassConverter } from './class_converter.js';
2
+ import { convertFromJsonObj, convertToJsonObj } from './convert.js';
3
+ import { TestNote } from './fixtures/test_state.js';
4
+
5
+ import { Buffer } from 'buffer';
6
+
7
+ const TEST_BASE64 = 'YmFzZTY0IGRlY29kZXI=';
8
+ test('test an RPC function over client', () => {
9
+ const cc = new ClassConverter({ TestNote });
10
+ const buffer = Buffer.from(TEST_BASE64, 'base64');
11
+ expect(convertFromJsonObj(cc, convertToJsonObj(cc, buffer)).toString('base64')).toBe(TEST_BASE64);
12
+ const note = new TestNote('1');
13
+ expect(convertFromJsonObj(cc, convertToJsonObj(cc, note))).toBeInstanceOf(TestNote);
14
+ expect(convertFromJsonObj(cc, convertToJsonObj(cc, note)).toString()).toBe('1');
15
+ });
@@ -0,0 +1,109 @@
1
+ import { ClassConverter } from './class_converter.js';
2
+ import { Buffer } from 'buffer';
3
+
4
+ /**
5
+ * JSON.stringify helper that handles bigints.
6
+ * @param obj - The object to be stringified.
7
+ * @returns The resulting string.
8
+ */
9
+ export function JsonStringify(obj: object, prettify?: boolean): string {
10
+ return JSON.stringify(
11
+ obj,
12
+ (key, value) =>
13
+ typeof value === 'bigint'
14
+ ? JSON.stringify({
15
+ type: 'bigint',
16
+ data: value.toString(),
17
+ })
18
+ : value,
19
+ prettify ? 2 : 0,
20
+ );
21
+ }
22
+
23
+ /**
24
+ * Convert a JSON-friendly object, which may encode a class object.
25
+ * @param cc - The class converter.
26
+ * @param obj - The encoded object.
27
+ * @returns The decoded object.
28
+ */
29
+ export function convertFromJsonObj(cc: ClassConverter, obj: any): any {
30
+ if (obj === null) {
31
+ return undefined; // `null` doesn't work with default args.
32
+ }
33
+
34
+ if (!obj) {
35
+ return obj; // Primitive type
36
+ }
37
+ // Is this a serialized Node buffer?
38
+ if (obj.type === 'Buffer' && typeof obj.data === 'string') {
39
+ return Buffer.from(obj.data, 'base64');
40
+ }
41
+
42
+ if (obj.type === 'bigint' && typeof obj.data === 'string') {
43
+ return BigInt(obj.data);
44
+ }
45
+
46
+ // Is this a convertible type?
47
+ if (typeof obj.type === 'string' && cc.isRegisteredClassName(obj.type)) {
48
+ return cc.toClassObj(obj);
49
+ }
50
+
51
+ // Is this an array?
52
+ if (Array.isArray(obj)) {
53
+ return obj.map((x: any) => convertFromJsonObj(cc, x));
54
+ }
55
+ // Is this a dictionary?
56
+ if (obj.constructor === Object) {
57
+ const newObj: any = {};
58
+ for (const key of Object.keys(obj)) {
59
+ newObj[key] = convertFromJsonObj(cc, obj[key]);
60
+ }
61
+ return newObj;
62
+ }
63
+
64
+ // Leave alone, assume JSON primitive
65
+ return obj;
66
+ }
67
+
68
+ /**
69
+ * Convert objects or classes to a JSON-friendly object.
70
+ * @param cc - The class converter.
71
+ * @param obj - The object.
72
+ * @returns The encoded object.
73
+ */
74
+ export function convertToJsonObj(cc: ClassConverter, obj: any): any {
75
+ if (!obj) {
76
+ return obj; // Primitive type
77
+ }
78
+ // Is this a Node buffer?
79
+ if (obj instanceof Buffer) {
80
+ return { type: 'Buffer', data: obj.toString('base64') };
81
+ }
82
+
83
+ if (typeof obj === 'bigint') {
84
+ return {
85
+ type: 'bigint',
86
+ data: obj.toString(),
87
+ };
88
+ }
89
+
90
+ // Is this a convertible type?
91
+ if (cc.isRegisteredClass(obj.constructor)) {
92
+ return cc.toJsonObj(obj);
93
+ }
94
+ // Is this an array?
95
+ if (Array.isArray(obj)) {
96
+ return obj.map((x: any) => convertToJsonObj(cc, x));
97
+ }
98
+ // Is this a dictionary?
99
+ if (obj.constructor === Object) {
100
+ const newObj: any = {};
101
+ for (const key of Object.keys(obj)) {
102
+ newObj[key] = convertToJsonObj(cc, obj[key]);
103
+ }
104
+ return newObj;
105
+ }
106
+
107
+ // Leave alone, assume JSON primitive
108
+ return obj;
109
+ }
@@ -0,0 +1,59 @@
1
+ import { sleep } from '../../sleep/index.js';
2
+
3
+ /**
4
+ * Contrived example for JSON RPC tests.
5
+ */
6
+ export class TestNote {
7
+ constructor(private data: string) {}
8
+ /**
9
+ * Create a string representation of this class.
10
+ * @returns The string representation.
11
+ */
12
+ toString(): string {
13
+ return this.data;
14
+ }
15
+ /**
16
+ * Creates a string representation of this class.
17
+ * @param data - The data.
18
+ * @returns The string representation.
19
+ */
20
+ static fromString(data: string): TestNote {
21
+ return new TestNote(data);
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Represents a simple state management for TestNote instances.
27
+ * Provides functionality to get a note by index and add notes asynchronously.
28
+ * Primarily used for testing JSON RPC-related functionalities.
29
+ */
30
+ export class TestState {
31
+ constructor(private notes: TestNote[]) {}
32
+ /**
33
+ * Retrieve the TestNote instance at the specified index from the notes array.
34
+ * This method allows getting a desired TestNote from the collection of notes
35
+ * maintained by the TestState instance using the provided index value.
36
+ *
37
+ * @param index - The index of the TestNote to be retrieved from the notes array.
38
+ * @returns The TestNote instance corresponding to the given index.
39
+ */
40
+ getNote(index: number): TestNote {
41
+ return this.notes[index];
42
+ }
43
+ /**
44
+ * Add an array of TestNote instances to the current TestState's notes.
45
+ * This function simulates asynchronous behavior by waiting for a duration
46
+ * equal to the number of notes being added. It then returns the updated
47
+ * list of TestNote instances in the TestState.
48
+ *
49
+ * @param notes - An array of TestNote instances to be added.
50
+ * @returns A Promise that resolves to an array of TestNote instances, including the newly added notes.
51
+ */
52
+ async addNotes(notes: TestNote[]): Promise<TestNote[]> {
53
+ for (const note of notes) {
54
+ this.notes.push(note);
55
+ }
56
+ await sleep(notes.length);
57
+ return this.notes;
58
+ }
59
+ }
@@ -0,0 +1,3 @@
1
+ export { ClassConverterInput, JsonEncodedClass, ClassConverter } from './class_converter.js';
2
+ export { JsonRpcServer, JsonProxy } from './server/index.js';
3
+ export { createJsonRpcClient, JsonStringify } from './client/index.js';
@@ -0,0 +1,20 @@
1
+ // Make sure this property was not inherited
2
+
3
+ /**
4
+ * Does this own the property?
5
+ * @param obj - An object.
6
+ * @param method - A property name.
7
+ */
8
+ export const hasOwnProperty = (obj: any, propertyName: string) =>
9
+ Object.prototype.hasOwnProperty.call(obj, propertyName);
10
+
11
+ /**
12
+ * Helper function to assert a condition is truthy
13
+ * @param x - A boolean condition to assert.
14
+ * @param err - Error message to throw if x isn't met.
15
+ */
16
+ export function assert(x: any, err: string): asserts x {
17
+ if (!x) {
18
+ throw new Error(err);
19
+ }
20
+ }
@@ -0,0 +1,2 @@
1
+ export { JsonRpcServer } from './json_rpc_server.js';
2
+ export { JsonProxy } from './json_proxy.js';
@@ -0,0 +1,36 @@
1
+ import { createDebugLogger } from '../../log/index.js';
2
+ import { ClassConverter, ClassConverterInput } from '../class_converter.js';
3
+ import { convertFromJsonObj, convertToJsonObj } from '../convert.js';
4
+ import { assert, hasOwnProperty } from '../js_utils.js';
5
+
6
+ const debug = createDebugLogger('json-rpc:json_proxy');
7
+
8
+ /**
9
+ * Handles conversion of objects over the write.
10
+ * Delegates to a ClassConverter object.
11
+ */
12
+ export class JsonProxy {
13
+ classConverter: ClassConverter;
14
+ constructor(private handler: object, stringClassMap: ClassConverterInput, objectClassMap: ClassConverterInput) {
15
+ this.classConverter = new ClassConverter(stringClassMap, objectClassMap);
16
+ }
17
+ /**
18
+ * Call an RPC method.
19
+ * @param methodName - The RPC method.
20
+ * @param jsonParams - The RPG parameters.
21
+ * @returns The remote result.
22
+ */
23
+ public async call(methodName: string, jsonParams: any[] = []) {
24
+ // Get access to our class members
25
+ const proto = Object.getPrototypeOf(this.handler);
26
+ assert(hasOwnProperty(proto, methodName), 'JsonProxy: Method not found!');
27
+ assert(Array.isArray(jsonParams), 'JsonProxy: Params not an array!');
28
+ // convert the params from json representation to classes
29
+ const convertedParams = jsonParams.map(param => convertFromJsonObj(this.classConverter, param));
30
+ debug('JsonProxy:call', methodName, '<-', convertedParams);
31
+ const rawRet = await (this.handler as any)[methodName](...convertedParams);
32
+ const ret = convertToJsonObj(this.classConverter, rawRet);
33
+ debug('JsonProxy:call', methodName, '->', ret);
34
+ return ret;
35
+ }
36
+ }