@useragent-kit/sdk 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (352) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +61 -3
  3. package/dist/adapters.d.ts +94 -0
  4. package/dist/adapters.d.ts.map +1 -0
  5. package/dist/adapters.js +188 -0
  6. package/dist/adapters.js.map +1 -0
  7. package/dist/api-protocol.d.ts +9 -0
  8. package/dist/api-protocol.d.ts.map +1 -0
  9. package/dist/api-protocol.js +9 -0
  10. package/dist/api-protocol.js.map +1 -0
  11. package/dist/bridge/contract.d.ts +66 -0
  12. package/dist/bridge/contract.d.ts.map +1 -0
  13. package/dist/bridge/contract.js +160 -0
  14. package/dist/bridge/contract.js.map +1 -0
  15. package/dist/bridge/index.d.ts +53 -0
  16. package/dist/bridge/index.d.ts.map +1 -0
  17. package/dist/bridge/index.js +302 -0
  18. package/dist/bridge/index.js.map +1 -0
  19. package/dist/bridge/types.d.ts +93 -0
  20. package/dist/bridge/types.d.ts.map +1 -0
  21. package/dist/bridge/types.js +9 -0
  22. package/dist/bridge/types.js.map +1 -0
  23. package/dist/chain.d.ts +198 -0
  24. package/dist/chain.d.ts.map +1 -0
  25. package/dist/chain.js +424 -0
  26. package/dist/chain.js.map +1 -0
  27. package/dist/errors.d.ts +22 -0
  28. package/dist/errors.d.ts.map +1 -0
  29. package/dist/errors.js +41 -0
  30. package/dist/errors.js.map +1 -0
  31. package/dist/events.d.ts +252 -0
  32. package/dist/events.d.ts.map +1 -0
  33. package/dist/events.js +54 -0
  34. package/dist/events.js.map +1 -0
  35. package/dist/experimental/file-transfer.d.ts +97 -0
  36. package/dist/experimental/file-transfer.d.ts.map +1 -0
  37. package/dist/experimental/file-transfer.js +1377 -0
  38. package/dist/experimental/file-transfer.js.map +1 -0
  39. package/dist/experimental/index.d.ts +7 -0
  40. package/dist/experimental/index.d.ts.map +1 -0
  41. package/dist/experimental/index.js +4 -0
  42. package/dist/experimental/index.js.map +1 -0
  43. package/dist/experimental/logger.d.ts +5 -0
  44. package/dist/experimental/logger.d.ts.map +1 -0
  45. package/dist/experimental/logger.js +74 -0
  46. package/dist/experimental/logger.js.map +1 -0
  47. package/dist/experimental/transport.d.ts +34 -0
  48. package/dist/experimental/transport.d.ts.map +1 -0
  49. package/dist/experimental/transport.js +43 -0
  50. package/dist/experimental/transport.js.map +1 -0
  51. package/dist/guest/claw.d.ts +10 -0
  52. package/dist/guest/claw.d.ts.map +1 -0
  53. package/dist/guest/claw.js +21 -0
  54. package/dist/guest/claw.js.map +1 -0
  55. package/dist/guest/crdt.d.ts +19 -0
  56. package/dist/guest/crdt.d.ts.map +1 -0
  57. package/dist/guest/crdt.js +64 -0
  58. package/dist/guest/crdt.js.map +1 -0
  59. package/dist/guest/data.d.ts +27 -0
  60. package/dist/guest/data.d.ts.map +1 -0
  61. package/dist/guest/data.js +61 -0
  62. package/dist/guest/data.js.map +1 -0
  63. package/dist/guest/files.d.ts +15 -0
  64. package/dist/guest/files.d.ts.map +1 -0
  65. package/dist/guest/files.js +24 -0
  66. package/dist/guest/files.js.map +1 -0
  67. package/dist/guest/index.d.ts +43 -0
  68. package/dist/guest/index.d.ts.map +1 -0
  69. package/dist/guest/index.js +44 -0
  70. package/dist/guest/index.js.map +1 -0
  71. package/dist/guest/media.d.ts +44 -0
  72. package/dist/guest/media.d.ts.map +1 -0
  73. package/dist/guest/media.js +97 -0
  74. package/dist/guest/media.js.map +1 -0
  75. package/dist/guest/mesh.d.ts +79 -0
  76. package/dist/guest/mesh.d.ts.map +1 -0
  77. package/dist/guest/mesh.js +153 -0
  78. package/dist/guest/mesh.js.map +1 -0
  79. package/dist/guest/navigate.d.ts +17 -0
  80. package/dist/guest/navigate.d.ts.map +1 -0
  81. package/dist/guest/navigate.js +169 -0
  82. package/dist/guest/navigate.js.map +1 -0
  83. package/dist/guest/statements.d.ts +18 -0
  84. package/dist/guest/statements.d.ts.map +1 -0
  85. package/dist/guest/statements.js +26 -0
  86. package/dist/guest/statements.js.map +1 -0
  87. package/dist/guest/telemetry.d.ts +88 -0
  88. package/dist/guest/telemetry.d.ts.map +1 -0
  89. package/dist/guest/telemetry.js +184 -0
  90. package/dist/guest/telemetry.js.map +1 -0
  91. package/dist/guest/types.d.ts +255 -0
  92. package/dist/guest/types.d.ts.map +1 -0
  93. package/dist/guest/types.js +9 -0
  94. package/dist/guest/types.js.map +1 -0
  95. package/dist/host-runtime-contract.d.ts +19 -0
  96. package/dist/host-runtime-contract.d.ts.map +1 -0
  97. package/dist/host-runtime-contract.js +80 -0
  98. package/dist/host-runtime-contract.js.map +1 -0
  99. package/dist/host-runtime-frame.d.ts +128 -0
  100. package/dist/host-runtime-frame.d.ts.map +1 -0
  101. package/dist/host-runtime-frame.js +881 -0
  102. package/dist/host-runtime-frame.js.map +1 -0
  103. package/dist/host-runtime-registry.d.ts +16 -0
  104. package/dist/host-runtime-registry.d.ts.map +1 -0
  105. package/dist/host-runtime-registry.js +53 -0
  106. package/dist/host-runtime-registry.js.map +1 -0
  107. package/dist/host-runtime.d.ts +106 -0
  108. package/dist/host-runtime.d.ts.map +1 -0
  109. package/dist/host-runtime.js +290 -0
  110. package/dist/host-runtime.js.map +1 -0
  111. package/dist/identity-adapter.d.ts +42 -0
  112. package/dist/identity-adapter.d.ts.map +1 -0
  113. package/dist/identity-adapter.js +43 -0
  114. package/dist/identity-adapter.js.map +1 -0
  115. package/dist/identity.d.ts +86 -0
  116. package/dist/identity.d.ts.map +1 -0
  117. package/dist/identity.js +223 -0
  118. package/dist/identity.js.map +1 -0
  119. package/dist/index.d.ts +91 -0
  120. package/dist/index.d.ts.map +1 -0
  121. package/dist/index.js +356 -0
  122. package/dist/index.js.map +1 -0
  123. package/dist/internal/api-protocol/api/protocol.d.ts +1402 -0
  124. package/dist/internal/api-protocol/api/protocol.d.ts.map +1 -0
  125. package/dist/internal/api-protocol/api/protocol.js +311 -0
  126. package/dist/internal/api-protocol/api/protocol.js.map +1 -0
  127. package/dist/internal/api-protocol/api/types.d.ts +24 -0
  128. package/dist/internal/api-protocol/api/types.d.ts.map +1 -0
  129. package/dist/internal/api-protocol/api/types.js +9 -0
  130. package/dist/internal/api-protocol/api/types.js.map +1 -0
  131. package/dist/internal/api-protocol/host-facade/connectionManager.d.ts +44 -0
  132. package/dist/internal/api-protocol/host-facade/connectionManager.d.ts.map +1 -0
  133. package/dist/internal/api-protocol/host-facade/connectionManager.js +349 -0
  134. package/dist/internal/api-protocol/host-facade/connectionManager.js.map +1 -0
  135. package/dist/internal/api-protocol/host-facade/protocolHandler.d.ts +24 -0
  136. package/dist/internal/api-protocol/host-facade/protocolHandler.d.ts.map +1 -0
  137. package/dist/internal/api-protocol/host-facade/protocolHandler.js +505 -0
  138. package/dist/internal/api-protocol/host-facade/protocolHandler.js.map +1 -0
  139. package/dist/internal/api-protocol/host-facade/types.d.ts +82 -0
  140. package/dist/internal/api-protocol/host-facade/types.d.ts.map +1 -0
  141. package/dist/internal/api-protocol/host-facade/types.js +14 -0
  142. package/dist/internal/api-protocol/host-facade/types.js.map +1 -0
  143. package/dist/internal/api-protocol/index.d.ts +39 -0
  144. package/dist/internal/api-protocol/index.d.ts.map +1 -0
  145. package/dist/internal/api-protocol/index.js +41 -0
  146. package/dist/internal/api-protocol/index.js.map +1 -0
  147. package/dist/internal/api-protocol/product-facade/hostApi.d.ts +392 -0
  148. package/dist/internal/api-protocol/product-facade/hostApi.d.ts.map +1 -0
  149. package/dist/internal/api-protocol/product-facade/hostApi.js +241 -0
  150. package/dist/internal/api-protocol/product-facade/hostApi.js.map +1 -0
  151. package/dist/internal/api-protocol/shared/codec/adapter.d.ts +41 -0
  152. package/dist/internal/api-protocol/shared/codec/adapter.d.ts.map +1 -0
  153. package/dist/internal/api-protocol/shared/codec/adapter.js +10 -0
  154. package/dist/internal/api-protocol/shared/codec/adapter.js.map +1 -0
  155. package/dist/internal/api-protocol/shared/codec/negotiation.d.ts +80 -0
  156. package/dist/internal/api-protocol/shared/codec/negotiation.d.ts.map +1 -0
  157. package/dist/internal/api-protocol/shared/codec/negotiation.js +129 -0
  158. package/dist/internal/api-protocol/shared/codec/negotiation.js.map +1 -0
  159. package/dist/internal/api-protocol/shared/codec/scale/adapter.d.ts +18 -0
  160. package/dist/internal/api-protocol/shared/codec/scale/adapter.d.ts.map +1 -0
  161. package/dist/internal/api-protocol/shared/codec/scale/adapter.js +23 -0
  162. package/dist/internal/api-protocol/shared/codec/scale/adapter.js.map +1 -0
  163. package/dist/internal/api-protocol/shared/codec/scale/primitives.d.ts +35 -0
  164. package/dist/internal/api-protocol/shared/codec/scale/primitives.d.ts.map +1 -0
  165. package/dist/internal/api-protocol/shared/codec/scale/primitives.js +96 -0
  166. package/dist/internal/api-protocol/shared/codec/scale/primitives.js.map +1 -0
  167. package/dist/internal/api-protocol/shared/codec/scale/v1/accounts.d.ts +65 -0
  168. package/dist/internal/api-protocol/shared/codec/scale/v1/accounts.d.ts.map +1 -0
  169. package/dist/internal/api-protocol/shared/codec/scale/v1/accounts.js +43 -0
  170. package/dist/internal/api-protocol/shared/codec/scale/v1/accounts.js.map +1 -0
  171. package/dist/internal/api-protocol/shared/codec/scale/v1/chainInteraction.d.ts +162 -0
  172. package/dist/internal/api-protocol/shared/codec/scale/v1/chainInteraction.d.ts.map +1 -0
  173. package/dist/internal/api-protocol/shared/codec/scale/v1/chainInteraction.js +81 -0
  174. package/dist/internal/api-protocol/shared/codec/scale/v1/chainInteraction.js.map +1 -0
  175. package/dist/internal/api-protocol/shared/codec/scale/v1/chat.d.ts +307 -0
  176. package/dist/internal/api-protocol/shared/codec/scale/v1/chat.d.ts.map +1 -0
  177. package/dist/internal/api-protocol/shared/codec/scale/v1/chat.js +105 -0
  178. package/dist/internal/api-protocol/shared/codec/scale/v1/chat.js.map +1 -0
  179. package/dist/internal/api-protocol/shared/codec/scale/v1/commonCodecs.d.ts +5 -0
  180. package/dist/internal/api-protocol/shared/codec/scale/v1/commonCodecs.d.ts.map +1 -0
  181. package/dist/internal/api-protocol/shared/codec/scale/v1/commonCodecs.js +6 -0
  182. package/dist/internal/api-protocol/shared/codec/scale/v1/commonCodecs.js.map +1 -0
  183. package/dist/internal/api-protocol/shared/codec/scale/v1/createTransaction.d.ts +71 -0
  184. package/dist/internal/api-protocol/shared/codec/scale/v1/createTransaction.d.ts.map +1 -0
  185. package/dist/internal/api-protocol/shared/codec/scale/v1/createTransaction.js +34 -0
  186. package/dist/internal/api-protocol/shared/codec/scale/v1/createTransaction.js.map +1 -0
  187. package/dist/internal/api-protocol/shared/codec/scale/v1/customRenderer.d.ts +106 -0
  188. package/dist/internal/api-protocol/shared/codec/scale/v1/customRenderer.d.ts.map +1 -0
  189. package/dist/internal/api-protocol/shared/codec/scale/v1/customRenderer.js +84 -0
  190. package/dist/internal/api-protocol/shared/codec/scale/v1/customRenderer.js.map +1 -0
  191. package/dist/internal/api-protocol/shared/codec/scale/v1/devicePermission.d.ts +4 -0
  192. package/dist/internal/api-protocol/shared/codec/scale/v1/devicePermission.d.ts.map +1 -0
  193. package/dist/internal/api-protocol/shared/codec/scale/v1/devicePermission.js +4 -0
  194. package/dist/internal/api-protocol/shared/codec/scale/v1/devicePermission.js.map +1 -0
  195. package/dist/internal/api-protocol/shared/codec/scale/v1/feature.d.ts +7 -0
  196. package/dist/internal/api-protocol/shared/codec/scale/v1/feature.d.ts.map +1 -0
  197. package/dist/internal/api-protocol/shared/codec/scale/v1/feature.js +7 -0
  198. package/dist/internal/api-protocol/shared/codec/scale/v1/feature.js.map +1 -0
  199. package/dist/internal/api-protocol/shared/codec/scale/v1/handshake.d.ts +13 -0
  200. package/dist/internal/api-protocol/shared/codec/scale/v1/handshake.d.ts.map +1 -0
  201. package/dist/internal/api-protocol/shared/codec/scale/v1/handshake.js +10 -0
  202. package/dist/internal/api-protocol/shared/codec/scale/v1/handshake.js.map +1 -0
  203. package/dist/internal/api-protocol/shared/codec/scale/v1/localStorage.d.ts +15 -0
  204. package/dist/internal/api-protocol/shared/codec/scale/v1/localStorage.d.ts.map +1 -0
  205. package/dist/internal/api-protocol/shared/codec/scale/v1/localStorage.js +12 -0
  206. package/dist/internal/api-protocol/shared/codec/scale/v1/localStorage.js.map +1 -0
  207. package/dist/internal/api-protocol/shared/codec/scale/v1/navigation.d.ts +10 -0
  208. package/dist/internal/api-protocol/shared/codec/scale/v1/navigation.d.ts.map +1 -0
  209. package/dist/internal/api-protocol/shared/codec/scale/v1/navigation.js +10 -0
  210. package/dist/internal/api-protocol/shared/codec/scale/v1/navigation.js.map +1 -0
  211. package/dist/internal/api-protocol/shared/codec/scale/v1/notification.d.ts +7 -0
  212. package/dist/internal/api-protocol/shared/codec/scale/v1/notification.d.ts.map +1 -0
  213. package/dist/internal/api-protocol/shared/codec/scale/v1/notification.js +7 -0
  214. package/dist/internal/api-protocol/shared/codec/scale/v1/notification.js.map +1 -0
  215. package/dist/internal/api-protocol/shared/codec/scale/v1/payment.d.ts +74 -0
  216. package/dist/internal/api-protocol/shared/codec/scale/v1/payment.d.ts.map +1 -0
  217. package/dist/internal/api-protocol/shared/codec/scale/v1/payment.js +38 -0
  218. package/dist/internal/api-protocol/shared/codec/scale/v1/payment.js.map +1 -0
  219. package/dist/internal/api-protocol/shared/codec/scale/v1/preimage.d.ts +12 -0
  220. package/dist/internal/api-protocol/shared/codec/scale/v1/preimage.d.ts.map +1 -0
  221. package/dist/internal/api-protocol/shared/codec/scale/v1/preimage.js +11 -0
  222. package/dist/internal/api-protocol/shared/codec/scale/v1/preimage.js.map +1 -0
  223. package/dist/internal/api-protocol/shared/codec/scale/v1/remotePermission.d.ts +10 -0
  224. package/dist/internal/api-protocol/shared/codec/scale/v1/remotePermission.d.ts.map +1 -0
  225. package/dist/internal/api-protocol/shared/codec/scale/v1/remotePermission.js +8 -0
  226. package/dist/internal/api-protocol/shared/codec/scale/v1/remotePermission.js.map +1 -0
  227. package/dist/internal/api-protocol/shared/codec/scale/v1/sign.d.ts +61 -0
  228. package/dist/internal/api-protocol/shared/codec/scale/v1/sign.d.ts.map +1 -0
  229. package/dist/internal/api-protocol/shared/codec/scale/v1/sign.js +42 -0
  230. package/dist/internal/api-protocol/shared/codec/scale/v1/sign.js.map +1 -0
  231. package/dist/internal/api-protocol/shared/codec/scale/v1/statementStore.d.ts +137 -0
  232. package/dist/internal/api-protocol/shared/codec/scale/v1/statementStore.d.ts.map +1 -0
  233. package/dist/internal/api-protocol/shared/codec/scale/v1/statementStore.js +55 -0
  234. package/dist/internal/api-protocol/shared/codec/scale/v1/statementStore.js.map +1 -0
  235. package/dist/internal/api-protocol/shared/codec/structured/index.d.ts +10 -0
  236. package/dist/internal/api-protocol/shared/codec/structured/index.d.ts.map +1 -0
  237. package/dist/internal/api-protocol/shared/codec/structured/index.js +19 -0
  238. package/dist/internal/api-protocol/shared/codec/structured/index.js.map +1 -0
  239. package/dist/internal/api-protocol/shared/transport/messagePortProvider.d.ts +18 -0
  240. package/dist/internal/api-protocol/shared/transport/messagePortProvider.d.ts.map +1 -0
  241. package/dist/internal/api-protocol/shared/transport/messagePortProvider.js +98 -0
  242. package/dist/internal/api-protocol/shared/transport/messagePortProvider.js.map +1 -0
  243. package/dist/internal/api-protocol/shared/transport/provider.d.ts +42 -0
  244. package/dist/internal/api-protocol/shared/transport/provider.d.ts.map +1 -0
  245. package/dist/internal/api-protocol/shared/transport/provider.js +13 -0
  246. package/dist/internal/api-protocol/shared/transport/provider.js.map +1 -0
  247. package/dist/internal/api-protocol/shared/transport/transport.d.ts +72 -0
  248. package/dist/internal/api-protocol/shared/transport/transport.d.ts.map +1 -0
  249. package/dist/internal/api-protocol/shared/transport/transport.js +420 -0
  250. package/dist/internal/api-protocol/shared/transport/transport.js.map +1 -0
  251. package/dist/internal/api-protocol/shared/transport/windowProvider.d.ts +13 -0
  252. package/dist/internal/api-protocol/shared/transport/windowProvider.d.ts.map +1 -0
  253. package/dist/internal/api-protocol/shared/transport/windowProvider.js +85 -0
  254. package/dist/internal/api-protocol/shared/transport/windowProvider.js.map +1 -0
  255. package/dist/internal/api-protocol/shared/util/helpers.d.ts +27 -0
  256. package/dist/internal/api-protocol/shared/util/helpers.d.ts.map +1 -0
  257. package/dist/internal/api-protocol/shared/util/helpers.js +43 -0
  258. package/dist/internal/api-protocol/shared/util/helpers.js.map +1 -0
  259. package/dist/internal/api-protocol/shared/util/idFactory.d.ts +9 -0
  260. package/dist/internal/api-protocol/shared/util/idFactory.d.ts.map +1 -0
  261. package/dist/internal/api-protocol/shared/util/idFactory.js +12 -0
  262. package/dist/internal/api-protocol/shared/util/idFactory.js.map +1 -0
  263. package/dist/internal/api-protocol/shared/util/logger.d.ts +15 -0
  264. package/dist/internal/api-protocol/shared/util/logger.d.ts.map +1 -0
  265. package/dist/internal/api-protocol/shared/util/logger.js +20 -0
  266. package/dist/internal/api-protocol/shared/util/logger.js.map +1 -0
  267. package/dist/mesh-attachment.d.ts +19 -0
  268. package/dist/mesh-attachment.d.ts.map +1 -0
  269. package/dist/mesh-attachment.js +43 -0
  270. package/dist/mesh-attachment.js.map +1 -0
  271. package/dist/mesh-chat.d.ts +48 -0
  272. package/dist/mesh-chat.d.ts.map +1 -0
  273. package/dist/mesh-chat.js +84 -0
  274. package/dist/mesh-chat.js.map +1 -0
  275. package/dist/mesh-delivery-diagnostics.d.ts +12 -0
  276. package/dist/mesh-delivery-diagnostics.d.ts.map +1 -0
  277. package/dist/mesh-delivery-diagnostics.js +45 -0
  278. package/dist/mesh-delivery-diagnostics.js.map +1 -0
  279. package/dist/mesh-notification.d.ts +29 -0
  280. package/dist/mesh-notification.d.ts.map +1 -0
  281. package/dist/mesh-notification.js +52 -0
  282. package/dist/mesh-notification.js.map +1 -0
  283. package/dist/mesh-pairing.d.ts +40 -0
  284. package/dist/mesh-pairing.d.ts.map +1 -0
  285. package/dist/mesh-pairing.js +78 -0
  286. package/dist/mesh-pairing.js.map +1 -0
  287. package/dist/product-host-runtime.d.ts +59 -0
  288. package/dist/product-host-runtime.d.ts.map +1 -0
  289. package/dist/product-host-runtime.js +150 -0
  290. package/dist/product-host-runtime.js.map +1 -0
  291. package/dist/product-protocol.d.ts +49 -0
  292. package/dist/product-protocol.d.ts.map +1 -0
  293. package/dist/product-protocol.js +389 -0
  294. package/dist/product-protocol.js.map +1 -0
  295. package/dist/product-view-hosted.d.ts +63 -0
  296. package/dist/product-view-hosted.d.ts.map +1 -0
  297. package/dist/product-view-hosted.js +272 -0
  298. package/dist/product-view-hosted.js.map +1 -0
  299. package/dist/product-view-service-worker.d.ts +6 -0
  300. package/dist/product-view-service-worker.d.ts.map +1 -0
  301. package/dist/product-view-service-worker.js +57 -0
  302. package/dist/product-view-service-worker.js.map +1 -0
  303. package/dist/product-view.d.ts +459 -0
  304. package/dist/product-view.d.ts.map +1 -0
  305. package/dist/product-view.js +2671 -0
  306. package/dist/product-view.js.map +1 -0
  307. package/dist/protocol.d.ts +39 -0
  308. package/dist/protocol.d.ts.map +1 -0
  309. package/dist/protocol.js +25 -0
  310. package/dist/protocol.js.map +1 -0
  311. package/dist/remote-runtime-provider.d.ts +9 -0
  312. package/dist/remote-runtime-provider.d.ts.map +1 -0
  313. package/dist/remote-runtime-provider.js +1002 -0
  314. package/dist/remote-runtime-provider.js.map +1 -0
  315. package/dist/runtime-chain-service.d.ts +109 -0
  316. package/dist/runtime-chain-service.d.ts.map +1 -0
  317. package/dist/runtime-chain-service.js +846 -0
  318. package/dist/runtime-chain-service.js.map +1 -0
  319. package/dist/runtime-chain.d.ts +8 -0
  320. package/dist/runtime-chain.d.ts.map +1 -0
  321. package/dist/runtime-chain.js +4 -0
  322. package/dist/runtime-chain.js.map +1 -0
  323. package/dist/runtime-storage-access.d.ts +39 -0
  324. package/dist/runtime-storage-access.d.ts.map +1 -0
  325. package/dist/runtime-storage-access.js +209 -0
  326. package/dist/runtime-storage-access.js.map +1 -0
  327. package/dist/scoped-frame.d.ts +55 -0
  328. package/dist/scoped-frame.d.ts.map +1 -0
  329. package/dist/scoped-frame.js +234 -0
  330. package/dist/scoped-frame.js.map +1 -0
  331. package/dist/sdk-interfaces.d.ts +159 -0
  332. package/dist/sdk-interfaces.d.ts.map +1 -0
  333. package/dist/sdk-interfaces.js +2 -0
  334. package/dist/sdk-interfaces.js.map +1 -0
  335. package/dist/statement-store.d.ts +166 -0
  336. package/dist/statement-store.d.ts.map +1 -0
  337. package/dist/statement-store.js +627 -0
  338. package/dist/statement-store.js.map +1 -0
  339. package/dist/telemetry.d.ts +218 -0
  340. package/dist/telemetry.d.ts.map +1 -0
  341. package/dist/telemetry.js +162 -0
  342. package/dist/telemetry.js.map +1 -0
  343. package/dist/types.d.ts +255 -0
  344. package/dist/types.d.ts.map +1 -0
  345. package/dist/types.js +77 -0
  346. package/dist/types.js.map +1 -0
  347. package/dist/wallet-session.d.ts +89 -0
  348. package/dist/wallet-session.d.ts.map +1 -0
  349. package/dist/wallet-session.js +135 -0
  350. package/dist/wallet-session.js.map +1 -0
  351. package/package.json +86 -5
  352. package/index.js +0 -1
@@ -0,0 +1,1002 @@
1
+ import { AUDITED_SUBSCRIPTION_STOP_METHODS } from "./runtime-chain-service.js";
2
+ const SUBSCRIPTION_STOP_METHODS = new Set(Object.values(AUDITED_SUBSCRIPTION_STOP_METHODS));
3
+ const FOLLOW_START_METHOD = "chainHead_v1_follow";
4
+ const FOLLOW_STOP_METHOD = "chainHead_v1_unfollow";
5
+ const FOLLOW_BOUND_RPC_METHODS = new Set([
6
+ "chainHead_v1_body",
7
+ "chainHead_v1_call",
8
+ "chainHead_v1_continue",
9
+ "chainHead_v1_header",
10
+ "chainHead_v1_stopOperation",
11
+ "chainHead_v1_storage",
12
+ ]);
13
+ const sharedFollowBridges = new WeakMap();
14
+ function toErrorMessage(error) {
15
+ if (error instanceof Error) {
16
+ return error.message;
17
+ }
18
+ return String(error);
19
+ }
20
+ function recordRemoteProviderTrace(entry) {
21
+ if (typeof window === "undefined") {
22
+ return;
23
+ }
24
+ if (!window.location.hostname.endsWith(".localhost")) {
25
+ return;
26
+ }
27
+ const key = "__hostSdkRemoteProviderTrace";
28
+ const target = window;
29
+ const trace = target[key] ?? [];
30
+ trace.push(entry);
31
+ if (trace.length > 200) {
32
+ trace.splice(0, trace.length - 200);
33
+ }
34
+ target[key] = trace;
35
+ }
36
+ function safeParseJson(text) {
37
+ try {
38
+ return JSON.parse(text);
39
+ }
40
+ catch {
41
+ return null;
42
+ }
43
+ }
44
+ function isJsonRecord(value) {
45
+ return typeof value === "object" && value !== null;
46
+ }
47
+ function parseJsonRpcId(value) {
48
+ if (typeof value === "string" || typeof value === "number") {
49
+ return value;
50
+ }
51
+ return null;
52
+ }
53
+ function parseSubscriptionResultId(message) {
54
+ const parsed = safeParseJson(message);
55
+ if (!isJsonRecord(parsed) || typeof parsed.result !== "string") {
56
+ return null;
57
+ }
58
+ return parsed.result;
59
+ }
60
+ function remapJsonRpcResponseId(message, id) {
61
+ const parsed = safeParseJson(message);
62
+ if (!isJsonRecord(parsed)) {
63
+ return message;
64
+ }
65
+ return JSON.stringify({
66
+ ...parsed,
67
+ id,
68
+ });
69
+ }
70
+ function normalizeRuntimeSpec(spec) {
71
+ if (!isJsonRecord(spec)) {
72
+ return spec;
73
+ }
74
+ const apis = spec.apis;
75
+ if (Array.isArray(apis) || !isJsonRecord(apis)) {
76
+ return spec;
77
+ }
78
+ return {
79
+ ...spec,
80
+ apis: Object.entries(apis)
81
+ .filter((entry) => typeof entry[1] === "number")
82
+ .map(([name, version]) => [name, version]),
83
+ };
84
+ }
85
+ function normalizeChainHeadFollowEvent(message) {
86
+ const parsed = safeParseJson(message);
87
+ if (!isJsonRecord(parsed) ||
88
+ parsed.method !== "chainHead_v1_followEvent" ||
89
+ !isJsonRecord(parsed.params) ||
90
+ !isJsonRecord(parsed.params.result)) {
91
+ return message;
92
+ }
93
+ const result = parsed.params.result;
94
+ const event = result.event;
95
+ if (event === "initialized" && isJsonRecord(result.finalizedBlockRuntime)) {
96
+ const runtime = result.finalizedBlockRuntime;
97
+ if (!isJsonRecord(runtime.spec)) {
98
+ return message;
99
+ }
100
+ return JSON.stringify({
101
+ ...parsed,
102
+ params: {
103
+ ...parsed.params,
104
+ result: {
105
+ ...result,
106
+ finalizedBlockRuntime: {
107
+ ...runtime,
108
+ spec: normalizeRuntimeSpec(runtime.spec),
109
+ },
110
+ },
111
+ },
112
+ });
113
+ }
114
+ if (event === "newBlock" && isJsonRecord(result.newRuntime)) {
115
+ const runtime = result.newRuntime;
116
+ if (!isJsonRecord(runtime.spec)) {
117
+ return message;
118
+ }
119
+ return JSON.stringify({
120
+ ...parsed,
121
+ params: {
122
+ ...parsed.params,
123
+ result: {
124
+ ...result,
125
+ newRuntime: {
126
+ ...runtime,
127
+ spec: normalizeRuntimeSpec(runtime.spec),
128
+ },
129
+ },
130
+ },
131
+ });
132
+ }
133
+ return message;
134
+ }
135
+ function normalizeJsonRpcMessage(message) {
136
+ return normalizeChainHeadFollowEvent(message);
137
+ }
138
+ function buildJsonRpcSuccess(id, result) {
139
+ return JSON.stringify({
140
+ jsonrpc: "2.0",
141
+ id,
142
+ result,
143
+ });
144
+ }
145
+ function buildJsonRpcError(id, reason) {
146
+ return JSON.stringify({
147
+ jsonrpc: "2.0",
148
+ id,
149
+ error: {
150
+ code: -32000,
151
+ message: reason,
152
+ },
153
+ });
154
+ }
155
+ function parseHeaderParentHashFromResponse(message) {
156
+ const parsed = safeParseJson(message);
157
+ if (!isJsonRecord(parsed)) {
158
+ return null;
159
+ }
160
+ if (isJsonRecord(parsed.result) && typeof parsed.result.parentHash === "string") {
161
+ return parsed.result.parentHash;
162
+ }
163
+ if (typeof parsed.result !== "string") {
164
+ return null;
165
+ }
166
+ const headerHex = parsed.result.startsWith("0x") ? parsed.result.slice(2) : parsed.result;
167
+ if (headerHex.length < 64) {
168
+ return null;
169
+ }
170
+ return `0x${headerHex.slice(0, 64)}`;
171
+ }
172
+ function buildSyntheticNewBlockEvent(subscriptionId, blockHash, parentBlockHash) {
173
+ return JSON.stringify({
174
+ jsonrpc: "2.0",
175
+ method: "chainHead_v1_followEvent",
176
+ params: {
177
+ subscription: subscriptionId,
178
+ result: {
179
+ event: "newBlock",
180
+ blockHash,
181
+ parentBlockHash,
182
+ newRuntime: null,
183
+ },
184
+ },
185
+ });
186
+ }
187
+ function parseFollowEventMessage(message) {
188
+ const parsed = safeParseJson(message);
189
+ if (!isJsonRecord(parsed) ||
190
+ parsed.method !== "chainHead_v1_followEvent" ||
191
+ !isJsonRecord(parsed.params) ||
192
+ !isJsonRecord(parsed.params.result) ||
193
+ typeof parsed.params.subscription !== "string") {
194
+ return null;
195
+ }
196
+ const event = parsed.params.result.event;
197
+ if (typeof event !== "string") {
198
+ return null;
199
+ }
200
+ return {
201
+ event,
202
+ operationId: typeof parsed.params.result.operationId === "string"
203
+ ? parsed.params.result.operationId
204
+ : null,
205
+ };
206
+ }
207
+ function parseInitializedFinalizedHashes(message) {
208
+ const parsed = safeParseJson(message);
209
+ if (!isJsonRecord(parsed) ||
210
+ parsed.method !== "chainHead_v1_followEvent" ||
211
+ !isJsonRecord(parsed.params) ||
212
+ !isJsonRecord(parsed.params.result) ||
213
+ parsed.params.result.event !== "initialized" ||
214
+ !Array.isArray(parsed.params.result.finalizedBlockHashes)) {
215
+ return null;
216
+ }
217
+ return parsed.params.result.finalizedBlockHashes.filter((value) => typeof value === "string" && value !== "");
218
+ }
219
+ function parseNewBlockEvent(message) {
220
+ const parsed = safeParseJson(message);
221
+ if (!isJsonRecord(parsed) ||
222
+ parsed.method !== "chainHead_v1_followEvent" ||
223
+ !isJsonRecord(parsed.params) ||
224
+ !isJsonRecord(parsed.params.result) ||
225
+ parsed.params.result.event !== "newBlock" ||
226
+ typeof parsed.params.result.blockHash !== "string" ||
227
+ typeof parsed.params.result.parentBlockHash !== "string") {
228
+ return null;
229
+ }
230
+ return {
231
+ blockHash: parsed.params.result.blockHash,
232
+ parentBlockHash: parsed.params.result.parentBlockHash,
233
+ };
234
+ }
235
+ function parseBestBlockChangedHash(message) {
236
+ const parsed = safeParseJson(message);
237
+ if (!isJsonRecord(parsed) ||
238
+ parsed.method !== "chainHead_v1_followEvent" ||
239
+ !isJsonRecord(parsed.params) ||
240
+ !isJsonRecord(parsed.params.result) ||
241
+ parsed.params.result.event !== "bestBlockChanged" ||
242
+ typeof parsed.params.result.bestBlockHash !== "string") {
243
+ return null;
244
+ }
245
+ return parsed.params.result.bestBlockHash;
246
+ }
247
+ function parseFinalizedEventHashes(message) {
248
+ const parsed = safeParseJson(message);
249
+ if (!isJsonRecord(parsed) ||
250
+ parsed.method !== "chainHead_v1_followEvent" ||
251
+ !isJsonRecord(parsed.params) ||
252
+ !isJsonRecord(parsed.params.result) ||
253
+ parsed.params.result.event !== "finalized" ||
254
+ !Array.isArray(parsed.params.result.finalizedBlockHashes)) {
255
+ return null;
256
+ }
257
+ return parsed.params.result.finalizedBlockHashes.filter((value) => typeof value === "string" && value !== "");
258
+ }
259
+ function rewriteSubscriptionId(message, subscriptionId) {
260
+ const parsed = safeParseJson(message);
261
+ if (!isJsonRecord(parsed) ||
262
+ !isJsonRecord(parsed.params) ||
263
+ typeof parsed.params.subscription !== "string") {
264
+ return message;
265
+ }
266
+ return JSON.stringify({
267
+ ...parsed,
268
+ params: {
269
+ ...parsed.params,
270
+ subscription: subscriptionId,
271
+ },
272
+ });
273
+ }
274
+ function parseOperationIdFromResponse(message) {
275
+ const parsed = safeParseJson(message);
276
+ if (!isJsonRecord(parsed) ||
277
+ !isJsonRecord(parsed.result) ||
278
+ typeof parsed.result.operationId !== "string") {
279
+ return null;
280
+ }
281
+ return parsed.result.operationId;
282
+ }
283
+ class SharedFollowBridge {
284
+ constructor(service, chainName) {
285
+ this.nextClientId = 1;
286
+ this.upstreamSubscriptionId = null;
287
+ this.startPromise = null;
288
+ this.clients = new Map();
289
+ this.operationOwners = new Map();
290
+ this.pendingOperationMessages = new Map();
291
+ this.lastInitializedMessage = null;
292
+ this.replayMessages = [];
293
+ this.rootedBlockHashes = new Set();
294
+ this.pendingNewBlocks = new Map();
295
+ this.pendingBestBlockChanged = new Map();
296
+ this.pendingFinalized = new Map();
297
+ this.hydrationPromises = new Map();
298
+ this.service = service;
299
+ this.chainName = chainName;
300
+ }
301
+ async attachClient(onMessage) {
302
+ await this.ensureStarted();
303
+ const clientSubscriptionId = `host-sdk-remote-follow-${String(this.nextClientId++)}`;
304
+ this.clients.set(clientSubscriptionId, {
305
+ onMessage,
306
+ ready: false,
307
+ pendingMessages: [],
308
+ });
309
+ return clientSubscriptionId;
310
+ }
311
+ replayClient(clientSubscriptionId) {
312
+ const client = this.clients.get(clientSubscriptionId);
313
+ if (client === undefined) {
314
+ return;
315
+ }
316
+ if (this.lastInitializedMessage !== null) {
317
+ client.onMessage(rewriteSubscriptionId(this.lastInitializedMessage, clientSubscriptionId));
318
+ }
319
+ for (const message of this.replayMessages) {
320
+ client.onMessage(rewriteSubscriptionId(message, clientSubscriptionId));
321
+ }
322
+ client.ready = true;
323
+ const pendingMessages = client.pendingMessages.splice(0);
324
+ for (const message of pendingMessages) {
325
+ client.onMessage(rewriteSubscriptionId(message, clientSubscriptionId));
326
+ }
327
+ }
328
+ detachClient(clientSubscriptionId) {
329
+ this.clients.delete(clientSubscriptionId);
330
+ for (const [operationId, owner] of this.operationOwners) {
331
+ if (owner === clientSubscriptionId) {
332
+ this.operationOwners.delete(operationId);
333
+ }
334
+ }
335
+ }
336
+ rememberReplayMessage(message) {
337
+ if (this.lastInitializedMessage === null || message === this.lastInitializedMessage) {
338
+ return;
339
+ }
340
+ this.replayMessages.push(message);
341
+ if (this.replayMessages.length > 512) {
342
+ this.replayMessages.splice(0, this.replayMessages.length - 512);
343
+ }
344
+ }
345
+ deliverToClient(clientSubscriptionId, client, message) {
346
+ if (!client.ready) {
347
+ client.pendingMessages.push(message);
348
+ if (client.pendingMessages.length > 512) {
349
+ client.pendingMessages.splice(0, client.pendingMessages.length - 512);
350
+ }
351
+ return;
352
+ }
353
+ client.onMessage(rewriteSubscriptionId(message, clientSubscriptionId));
354
+ }
355
+ bufferOperationMessage(operationId, message) {
356
+ const pending = this.pendingOperationMessages.get(operationId) ?? [];
357
+ pending.push(message);
358
+ if (pending.length > 64) {
359
+ pending.splice(0, pending.length - 64);
360
+ }
361
+ this.pendingOperationMessages.set(operationId, pending);
362
+ }
363
+ flushPendingOperationMessages(operationId, clientSubscriptionId) {
364
+ const pending = this.pendingOperationMessages.get(operationId);
365
+ if (pending === undefined) {
366
+ return;
367
+ }
368
+ this.pendingOperationMessages.delete(operationId);
369
+ const client = this.clients.get(clientSubscriptionId);
370
+ if (client === undefined) {
371
+ return;
372
+ }
373
+ let sawTerminalEvent = false;
374
+ for (const message of pending) {
375
+ const followEvent = parseFollowEventMessage(message);
376
+ if (followEvent?.event === "operationStorageDone" ||
377
+ followEvent?.event === "operationCallDone" ||
378
+ followEvent?.event === "operationBodyDone" ||
379
+ followEvent?.event === "operationInaccessible" ||
380
+ followEvent?.event === "operationError") {
381
+ sawTerminalEvent = true;
382
+ }
383
+ this.deliverToClient(clientSubscriptionId, client, message);
384
+ }
385
+ if (sawTerminalEvent) {
386
+ this.operationOwners.delete(operationId);
387
+ }
388
+ }
389
+ broadcast(message) {
390
+ this.rememberReplayMessage(message);
391
+ recordRemoteProviderTrace({
392
+ direction: "recv",
393
+ method: "sharedFollow.broadcast",
394
+ message,
395
+ });
396
+ for (const [clientSubscriptionId, client] of this.clients) {
397
+ this.deliverToClient(clientSubscriptionId, client, message);
398
+ }
399
+ }
400
+ markRooted(blockHash) {
401
+ if (this.rootedBlockHashes.has(blockHash)) {
402
+ return;
403
+ }
404
+ this.rootedBlockHashes.add(blockHash);
405
+ const pendingBest = this.pendingBestBlockChanged.get(blockHash);
406
+ if (pendingBest !== undefined) {
407
+ this.pendingBestBlockChanged.delete(blockHash);
408
+ for (const message of pendingBest) {
409
+ this.broadcast(message);
410
+ }
411
+ }
412
+ const pendingFinalized = this.pendingFinalized.get(blockHash);
413
+ if (pendingFinalized !== undefined) {
414
+ this.pendingFinalized.delete(blockHash);
415
+ for (const message of pendingFinalized) {
416
+ this.broadcast(message);
417
+ }
418
+ }
419
+ const pendingChildren = this.pendingNewBlocks.get(blockHash);
420
+ if (pendingChildren !== undefined) {
421
+ this.pendingNewBlocks.delete(blockHash);
422
+ for (const message of pendingChildren) {
423
+ this.forwardNewBlock(message);
424
+ }
425
+ }
426
+ }
427
+ async hydrateAncestorChain(blockHash, depth = 0) {
428
+ if (this.rootedBlockHashes.has(blockHash)) {
429
+ return;
430
+ }
431
+ if (depth > 256) {
432
+ throw new Error(`shared follow hydration exceeded depth for ${blockHash}`);
433
+ }
434
+ const existing = this.hydrationPromises.get(blockHash);
435
+ if (existing !== undefined) {
436
+ await existing;
437
+ return;
438
+ }
439
+ if (this.upstreamSubscriptionId === null) {
440
+ throw new Error("shared chainHead follow is not ready");
441
+ }
442
+ const hydration = (async () => {
443
+ let response = await this.service.sendRpc(this.chainName, "chain_getHeader", JSON.stringify([blockHash]));
444
+ let parentBlockHash = parseHeaderParentHashFromResponse(response);
445
+ if (parentBlockHash === null) {
446
+ response = await this.service.sendRpc(this.chainName, "chainHead_v1_header", JSON.stringify([this.upstreamSubscriptionId, blockHash]));
447
+ parentBlockHash = parseHeaderParentHashFromResponse(response);
448
+ }
449
+ if (parentBlockHash === null) {
450
+ throw new Error(`Unable to decode header parent for ${blockHash}`);
451
+ }
452
+ if (!this.rootedBlockHashes.has(parentBlockHash)) {
453
+ await this.hydrateAncestorChain(parentBlockHash, depth + 1);
454
+ }
455
+ if (!this.rootedBlockHashes.has(parentBlockHash)) {
456
+ return;
457
+ }
458
+ const upstreamSubscriptionId = this.upstreamSubscriptionId;
459
+ if (upstreamSubscriptionId === null) {
460
+ throw new Error("shared chainHead follow is not ready");
461
+ }
462
+ const syntheticMessage = buildSyntheticNewBlockEvent(upstreamSubscriptionId, blockHash, parentBlockHash);
463
+ recordRemoteProviderTrace({
464
+ direction: "recv",
465
+ method: "sharedFollow.syntheticNewBlock",
466
+ message: syntheticMessage,
467
+ detail: `parent:${parentBlockHash}`,
468
+ });
469
+ this.broadcast(syntheticMessage);
470
+ this.markRooted(blockHash);
471
+ })().finally(() => {
472
+ this.hydrationPromises.delete(blockHash);
473
+ });
474
+ this.hydrationPromises.set(blockHash, hydration);
475
+ await hydration;
476
+ }
477
+ forwardNewBlock(message) {
478
+ const block = parseNewBlockEvent(message);
479
+ if (block === null) {
480
+ this.broadcast(message);
481
+ return;
482
+ }
483
+ if (!this.rootedBlockHashes.has(block.parentBlockHash)) {
484
+ recordRemoteProviderTrace({
485
+ direction: "recv",
486
+ method: "sharedFollow.bufferNewBlock",
487
+ message,
488
+ detail: `missing-parent:${block.parentBlockHash}`,
489
+ });
490
+ const pending = this.pendingNewBlocks.get(block.parentBlockHash) ?? [];
491
+ pending.push(message);
492
+ this.pendingNewBlocks.set(block.parentBlockHash, pending);
493
+ void this.hydrateAncestorChain(block.parentBlockHash).catch((error) => {
494
+ recordRemoteProviderTrace({
495
+ direction: "abort",
496
+ method: "sharedFollow.hydrateAncestorChain",
497
+ reason: toErrorMessage(error),
498
+ detail: `block:${block.parentBlockHash}`,
499
+ });
500
+ });
501
+ return;
502
+ }
503
+ recordRemoteProviderTrace({
504
+ direction: "recv",
505
+ method: "sharedFollow.forwardNewBlock",
506
+ message,
507
+ detail: `parent:${block.parentBlockHash}`,
508
+ });
509
+ this.broadcast(message);
510
+ this.markRooted(block.blockHash);
511
+ }
512
+ forwardBestBlockChanged(message) {
513
+ const blockHash = parseBestBlockChangedHash(message);
514
+ if (blockHash === null) {
515
+ this.broadcast(message);
516
+ return;
517
+ }
518
+ if (!this.rootedBlockHashes.has(blockHash)) {
519
+ recordRemoteProviderTrace({
520
+ direction: "recv",
521
+ method: "sharedFollow.bufferBestBlockChanged",
522
+ message,
523
+ detail: `missing-block:${blockHash}`,
524
+ });
525
+ const pending = this.pendingBestBlockChanged.get(blockHash) ?? [];
526
+ pending.push(message);
527
+ this.pendingBestBlockChanged.set(blockHash, pending);
528
+ return;
529
+ }
530
+ this.broadcast(message);
531
+ }
532
+ forwardFinalized(message) {
533
+ const blockHashes = parseFinalizedEventHashes(message);
534
+ if (blockHashes === null || blockHashes.length === 0) {
535
+ this.broadcast(message);
536
+ return;
537
+ }
538
+ const unknown = blockHashes.filter((blockHash) => !this.rootedBlockHashes.has(blockHash));
539
+ if (unknown.length > 0) {
540
+ recordRemoteProviderTrace({
541
+ direction: "recv",
542
+ method: "sharedFollow.bufferFinalized",
543
+ message,
544
+ detail: `missing-blocks:${unknown.join(",")}`,
545
+ });
546
+ for (const blockHash of unknown) {
547
+ const pending = this.pendingFinalized.get(blockHash) ?? [];
548
+ pending.push(message);
549
+ this.pendingFinalized.set(blockHash, pending);
550
+ }
551
+ return;
552
+ }
553
+ this.broadcast(message);
554
+ }
555
+ async sendFollowBoundRpc(clientSubscriptionId, method, params) {
556
+ await this.ensureStarted();
557
+ if (this.upstreamSubscriptionId === null) {
558
+ throw new Error("shared chainHead follow is not ready");
559
+ }
560
+ const rewrittenParams = [...params];
561
+ rewrittenParams[0] = this.upstreamSubscriptionId;
562
+ const response = normalizeJsonRpcMessage(await this.service.sendRpc(this.chainName, method, JSON.stringify(rewrittenParams)));
563
+ const operationId = parseOperationIdFromResponse(response);
564
+ if (operationId !== null) {
565
+ this.operationOwners.set(operationId, clientSubscriptionId);
566
+ this.flushPendingOperationMessages(operationId, clientSubscriptionId);
567
+ }
568
+ return response;
569
+ }
570
+ async ensureStarted() {
571
+ if (this.startPromise !== null) {
572
+ await this.startPromise;
573
+ return;
574
+ }
575
+ this.startPromise = new Promise((resolve, reject) => {
576
+ let ready = false;
577
+ const markReady = () => {
578
+ if (ready) {
579
+ return;
580
+ }
581
+ ready = true;
582
+ resolve();
583
+ };
584
+ void Promise.resolve(this.service.startSubscription(this.chainName, FOLLOW_START_METHOD, JSON.stringify([true]), (message) => {
585
+ const normalizedMessage = normalizeJsonRpcMessage(message);
586
+ const subscriptionId = parseSubscriptionResultId(normalizedMessage);
587
+ if (subscriptionId !== null) {
588
+ this.upstreamSubscriptionId = subscriptionId;
589
+ markReady();
590
+ return;
591
+ }
592
+ const followEvent = parseFollowEventMessage(normalizedMessage);
593
+ if (followEvent === null) {
594
+ return;
595
+ }
596
+ if (followEvent.event === "initialized") {
597
+ this.lastInitializedMessage = normalizedMessage;
598
+ this.replayMessages = [];
599
+ this.rootedBlockHashes.clear();
600
+ this.pendingNewBlocks.clear();
601
+ this.pendingBestBlockChanged.clear();
602
+ this.pendingFinalized.clear();
603
+ const finalizedBlockHashes = parseInitializedFinalizedHashes(normalizedMessage) ?? [];
604
+ for (const blockHash of finalizedBlockHashes) {
605
+ this.rootedBlockHashes.add(blockHash);
606
+ }
607
+ recordRemoteProviderTrace({
608
+ direction: "recv",
609
+ method: "sharedFollow.initialized",
610
+ message: normalizedMessage,
611
+ detail: `rooted:${finalizedBlockHashes.join(",")}`,
612
+ });
613
+ }
614
+ if (followEvent.operationId !== null) {
615
+ const owner = this.operationOwners.get(followEvent.operationId);
616
+ if (owner === undefined) {
617
+ this.bufferOperationMessage(followEvent.operationId, normalizedMessage);
618
+ return;
619
+ }
620
+ const client = this.clients.get(owner);
621
+ if (client !== undefined) {
622
+ this.deliverToClient(owner, client, normalizedMessage);
623
+ }
624
+ if (followEvent.event === "operationStorageDone" ||
625
+ followEvent.event === "operationCallDone" ||
626
+ followEvent.event === "operationBodyDone" ||
627
+ followEvent.event === "operationInaccessible" ||
628
+ followEvent.event === "operationError") {
629
+ this.operationOwners.delete(followEvent.operationId);
630
+ }
631
+ return;
632
+ }
633
+ switch (followEvent.event) {
634
+ case "initialized":
635
+ this.broadcast(normalizedMessage);
636
+ return;
637
+ case "newBlock":
638
+ this.forwardNewBlock(normalizedMessage);
639
+ return;
640
+ case "bestBlockChanged":
641
+ this.forwardBestBlockChanged(normalizedMessage);
642
+ return;
643
+ case "finalized":
644
+ this.forwardFinalized(normalizedMessage);
645
+ return;
646
+ default:
647
+ this.broadcast(normalizedMessage);
648
+ return;
649
+ }
650
+ }, (reason) => {
651
+ this.startPromise = null;
652
+ this.upstreamSubscriptionId = null;
653
+ if (!ready) {
654
+ reject(new Error(reason));
655
+ }
656
+ for (const client of this.clients.values()) {
657
+ client.onMessage(JSON.stringify({
658
+ jsonrpc: "2.0",
659
+ error: { code: -32000, message: reason },
660
+ }));
661
+ }
662
+ })).catch((error) => {
663
+ this.startPromise = null;
664
+ this.upstreamSubscriptionId = null;
665
+ reject(error instanceof Error ? error : new Error(String(error)));
666
+ });
667
+ });
668
+ await this.startPromise;
669
+ }
670
+ }
671
+ function getSharedFollowBridge(service, chainName) {
672
+ let byChain = sharedFollowBridges.get(service);
673
+ if (byChain === undefined) {
674
+ byChain = new Map();
675
+ sharedFollowBridges.set(service, byChain);
676
+ }
677
+ let bridge = byChain.get(chainName);
678
+ if (bridge === undefined) {
679
+ bridge = new SharedFollowBridge(service, chainName);
680
+ byChain.set(chainName, bridge);
681
+ }
682
+ return bridge;
683
+ }
684
+ export function createRemoteRuntimeProvider(service, chainName) {
685
+ return (onMessage) => {
686
+ const debug = typeof window !== "undefined" && window.location.hostname.endsWith(".localhost");
687
+ let disconnected = false;
688
+ const activeSubscriptions = new Map();
689
+ const followSubscriptions = new Map();
690
+ const sharedFollowBridge = getSharedFollowBridge(service, chainName);
691
+ const sanitizeFollowEventForConnection = (rawMessage) => {
692
+ const parsed = safeParseJson(rawMessage);
693
+ if (!isJsonRecord(parsed) ||
694
+ parsed.method !== "chainHead_v1_followEvent" ||
695
+ !isJsonRecord(parsed.params) ||
696
+ typeof parsed.params.subscription !== "string" ||
697
+ !isJsonRecord(parsed.params.result)) {
698
+ return rawMessage;
699
+ }
700
+ const followState = followSubscriptions.get(parsed.params.subscription);
701
+ if (followState === undefined) {
702
+ return rawMessage;
703
+ }
704
+ const result = parsed.params.result;
705
+ const event = result.event;
706
+ if (event === "initialized") {
707
+ const hashes = Array.isArray(result.finalizedBlockHashes)
708
+ ? result.finalizedBlockHashes.filter((value) => typeof value === "string" && value !== "")
709
+ : [];
710
+ followState.knownBlockHashes.clear();
711
+ for (const hash of hashes) {
712
+ followState.knownBlockHashes.add(hash);
713
+ }
714
+ return rawMessage;
715
+ }
716
+ if (event === "newBlock") {
717
+ if (typeof result.parentBlockHash !== "string" ||
718
+ !followState.knownBlockHashes.has(result.parentBlockHash)) {
719
+ return null;
720
+ }
721
+ if (typeof result.blockHash === "string") {
722
+ followState.knownBlockHashes.add(result.blockHash);
723
+ }
724
+ return rawMessage;
725
+ }
726
+ if (event === "bestBlockChanged") {
727
+ if (typeof result.bestBlockHash === "string" &&
728
+ !followState.knownBlockHashes.has(result.bestBlockHash)) {
729
+ return null;
730
+ }
731
+ return rawMessage;
732
+ }
733
+ if (event === "finalized") {
734
+ const finalizedBlockHashes = Array.isArray(result.finalizedBlockHashes)
735
+ ? result.finalizedBlockHashes.filter((value) => typeof value === "string" && followState.knownBlockHashes.has(value))
736
+ : [];
737
+ if (finalizedBlockHashes.length === 0) {
738
+ return null;
739
+ }
740
+ if (Array.isArray(result.prunedBlockHashes)) {
741
+ for (const hash of result.prunedBlockHashes) {
742
+ if (typeof hash === "string") {
743
+ followState.knownBlockHashes.delete(hash);
744
+ }
745
+ }
746
+ }
747
+ return JSON.stringify({
748
+ ...parsed,
749
+ params: {
750
+ ...parsed.params,
751
+ result: {
752
+ ...result,
753
+ finalizedBlockHashes,
754
+ },
755
+ },
756
+ });
757
+ }
758
+ return rawMessage;
759
+ };
760
+ return {
761
+ send(message) {
762
+ if (disconnected) {
763
+ return;
764
+ }
765
+ const parsed = safeParseJson(message);
766
+ if (!isJsonRecord(parsed) || typeof parsed.method !== "string") {
767
+ return;
768
+ }
769
+ const method = parsed.method;
770
+ const requestId = parseJsonRpcId(parsed.id);
771
+ const params = parsed.params ?? [];
772
+ const paramsJson = JSON.stringify(params);
773
+ if (method === FOLLOW_STOP_METHOD) {
774
+ if (Array.isArray(params) && typeof params[0] === "string") {
775
+ const subscriptionId = params[0];
776
+ const stop = activeSubscriptions.get(subscriptionId);
777
+ if (stop !== undefined) {
778
+ activeSubscriptions.delete(subscriptionId);
779
+ followSubscriptions.delete(subscriptionId);
780
+ void Promise.resolve(stop()).then(() => {
781
+ if (!disconnected && requestId !== null) {
782
+ onMessage(buildJsonRpcSuccess(requestId, true));
783
+ }
784
+ });
785
+ return;
786
+ }
787
+ }
788
+ }
789
+ if (method === FOLLOW_START_METHOD) {
790
+ void sharedFollowBridge
791
+ .attachClient((jsonRpcMessage) => {
792
+ if (!disconnected) {
793
+ const sanitizedMessage = sanitizeFollowEventForConnection(jsonRpcMessage);
794
+ if (sanitizedMessage !== null) {
795
+ onMessage(sanitizedMessage);
796
+ }
797
+ }
798
+ })
799
+ .then((subscriptionId) => {
800
+ if (disconnected) {
801
+ sharedFollowBridge.detachClient(subscriptionId);
802
+ return;
803
+ }
804
+ activeSubscriptions.set(subscriptionId, () => {
805
+ sharedFollowBridge.detachClient(subscriptionId);
806
+ });
807
+ followSubscriptions.set(subscriptionId, {
808
+ knownBlockHashes: new Set(),
809
+ });
810
+ if (requestId !== null) {
811
+ onMessage(buildJsonRpcSuccess(requestId, subscriptionId));
812
+ }
813
+ sharedFollowBridge.replayClient(subscriptionId);
814
+ })
815
+ .catch((error) => {
816
+ if (!disconnected && requestId !== null) {
817
+ onMessage(buildJsonRpcError(requestId, toErrorMessage(error)));
818
+ }
819
+ });
820
+ return;
821
+ }
822
+ if (FOLLOW_BOUND_RPC_METHODS.has(method) &&
823
+ Array.isArray(params) &&
824
+ typeof params[0] === "string") {
825
+ const subscriptionId = params[0];
826
+ if (!activeSubscriptions.has(subscriptionId)) {
827
+ if (requestId !== null) {
828
+ onMessage(buildJsonRpcError(requestId, `unknown chainHead subscription: ${subscriptionId}`));
829
+ }
830
+ return;
831
+ }
832
+ void sharedFollowBridge
833
+ .sendFollowBoundRpc(subscriptionId, method, params)
834
+ .then((jsonRpc) => {
835
+ const response = requestId === null ? jsonRpc : remapJsonRpcResponseId(jsonRpc, requestId);
836
+ if (!disconnected) {
837
+ onMessage(response);
838
+ }
839
+ })
840
+ .catch((error) => {
841
+ if (!disconnected && requestId !== null) {
842
+ onMessage(buildJsonRpcError(requestId, toErrorMessage(error)));
843
+ }
844
+ });
845
+ return;
846
+ }
847
+ if (SUBSCRIPTION_STOP_METHODS.has(parsed.method) &&
848
+ Array.isArray(params) &&
849
+ typeof params[0] === "string") {
850
+ recordRemoteProviderTrace({
851
+ direction: "send",
852
+ method,
853
+ message,
854
+ });
855
+ if (debug) {
856
+ console.warn(`[host-sdk remote-provider] stop ${method} sub=${params[0]}`);
857
+ }
858
+ const subscriptionId = params[0];
859
+ const stop = activeSubscriptions.get(subscriptionId);
860
+ if (stop === undefined) {
861
+ return;
862
+ }
863
+ activeSubscriptions.delete(subscriptionId);
864
+ void Promise.resolve(stop())
865
+ .then(() => {
866
+ if (!disconnected && requestId !== null) {
867
+ onMessage(buildJsonRpcSuccess(requestId, true));
868
+ }
869
+ })
870
+ .catch((error) => {
871
+ if (!disconnected && requestId !== null) {
872
+ onMessage(buildJsonRpcError(requestId, toErrorMessage(error)));
873
+ }
874
+ });
875
+ return;
876
+ }
877
+ if (method in AUDITED_SUBSCRIPTION_STOP_METHODS) {
878
+ recordRemoteProviderTrace({
879
+ direction: "send",
880
+ method,
881
+ message,
882
+ });
883
+ if (debug) {
884
+ console.warn(`[host-sdk remote-provider] start sub ${method} params=${paramsJson}`);
885
+ }
886
+ let knownSubscriptionId = null;
887
+ let stopPromise = null;
888
+ let stopWrapper = null;
889
+ stopPromise = Promise.resolve(service.startSubscription(chainName, method, paramsJson, (jsonRpcMessage) => {
890
+ const normalizedMessage = normalizeJsonRpcMessage(jsonRpcMessage);
891
+ recordRemoteProviderTrace({
892
+ direction: "recv",
893
+ method,
894
+ message: normalizedMessage,
895
+ });
896
+ if (debug) {
897
+ console.warn(`[host-sdk remote-provider] sub message ${method}: ${normalizedMessage}`);
898
+ }
899
+ if (disconnected) {
900
+ return;
901
+ }
902
+ if (knownSubscriptionId === null) {
903
+ const subscriptionId = parseSubscriptionResultId(normalizedMessage);
904
+ if (subscriptionId !== null) {
905
+ knownSubscriptionId = subscriptionId;
906
+ stopWrapper = () => {
907
+ const currentStopPromise = stopPromise;
908
+ if (currentStopPromise === null) {
909
+ return;
910
+ }
911
+ return Promise.resolve(currentStopPromise).then((stop) => stop());
912
+ };
913
+ activeSubscriptions.set(subscriptionId, stopWrapper);
914
+ if (requestId !== null) {
915
+ const response = buildJsonRpcSuccess(requestId, subscriptionId);
916
+ if (debug) {
917
+ console.warn(`[host-sdk remote-provider] sub response ${method}: ${response}`);
918
+ }
919
+ onMessage(response);
920
+ }
921
+ return;
922
+ }
923
+ }
924
+ if (debug) {
925
+ console.warn(`[host-sdk remote-provider] forward sub ${method}: ${normalizedMessage}`);
926
+ }
927
+ onMessage(normalizedMessage);
928
+ }, (reason) => {
929
+ recordRemoteProviderTrace({
930
+ direction: "abort",
931
+ method,
932
+ reason,
933
+ });
934
+ if (debug) {
935
+ console.warn(`[host-sdk remote-provider] sub abort ${method}: ${reason}`);
936
+ }
937
+ if (knownSubscriptionId !== null) {
938
+ activeSubscriptions.delete(knownSubscriptionId);
939
+ }
940
+ stopWrapper = null;
941
+ if (!disconnected && requestId !== null) {
942
+ onMessage(buildJsonRpcError(requestId, reason));
943
+ }
944
+ }));
945
+ void stopPromise.catch((error) => {
946
+ if (knownSubscriptionId !== null) {
947
+ activeSubscriptions.delete(knownSubscriptionId);
948
+ }
949
+ stopWrapper = null;
950
+ if (!disconnected && requestId !== null) {
951
+ onMessage(buildJsonRpcError(requestId, toErrorMessage(error)));
952
+ }
953
+ });
954
+ return;
955
+ }
956
+ recordRemoteProviderTrace({
957
+ direction: "send",
958
+ method,
959
+ message,
960
+ });
961
+ if (debug) {
962
+ console.warn(`[host-sdk remote-provider] rpc ${method} params=${paramsJson}`);
963
+ }
964
+ void service
965
+ .sendRpc(chainName, method, paramsJson)
966
+ .then((jsonRpc) => {
967
+ const normalizedMessage = normalizeJsonRpcMessage(jsonRpc);
968
+ const response = requestId === null
969
+ ? normalizedMessage
970
+ : remapJsonRpcResponseId(normalizedMessage, requestId);
971
+ recordRemoteProviderTrace({
972
+ direction: "recv",
973
+ method,
974
+ message: response,
975
+ });
976
+ if (debug) {
977
+ console.warn(`[host-sdk remote-provider] rpc response ${method}: ${response}`);
978
+ }
979
+ if (!disconnected) {
980
+ onMessage(response);
981
+ }
982
+ })
983
+ .catch((error) => {
984
+ if (!disconnected && requestId !== null) {
985
+ onMessage(buildJsonRpcError(requestId, toErrorMessage(error)));
986
+ }
987
+ });
988
+ },
989
+ disconnect() {
990
+ if (disconnected) {
991
+ return;
992
+ }
993
+ disconnected = true;
994
+ for (const stop of activeSubscriptions.values()) {
995
+ void Promise.resolve(stop());
996
+ }
997
+ activeSubscriptions.clear();
998
+ },
999
+ };
1000
+ };
1001
+ }
1002
+ //# sourceMappingURL=remote-runtime-provider.js.map