@icp-sdk/core 5.0.0 → 5.2.0-beta.0

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 (331) hide show
  1. package/lib/cjs/agent/actor.d.ts +15 -1
  2. package/lib/cjs/agent/actor.d.ts.map +1 -1
  3. package/lib/cjs/agent/actor.js +37 -91
  4. package/lib/cjs/agent/actor.js.map +1 -1
  5. package/lib/cjs/agent/agent/api.d.ts +29 -6
  6. package/lib/cjs/agent/agent/api.d.ts.map +1 -1
  7. package/lib/cjs/agent/agent/api.js.map +1 -1
  8. package/lib/cjs/agent/agent/http/expiry.d.ts +36 -0
  9. package/lib/cjs/agent/agent/http/expiry.d.ts.map +1 -0
  10. package/lib/cjs/agent/agent/http/expiry.js +92 -0
  11. package/lib/cjs/agent/agent/http/expiry.js.map +1 -0
  12. package/lib/cjs/agent/agent/http/index.d.ts +28 -2
  13. package/lib/cjs/agent/agent/http/index.d.ts.map +1 -1
  14. package/lib/cjs/agent/agent/http/index.js +182 -90
  15. package/lib/cjs/agent/agent/http/index.js.map +1 -1
  16. package/lib/cjs/agent/agent/http/transforms.d.ts +1 -35
  17. package/lib/cjs/agent/agent/http/transforms.d.ts.map +1 -1
  18. package/lib/cjs/agent/agent/http/transforms.js +6 -90
  19. package/lib/cjs/agent/agent/http/transforms.js.map +1 -1
  20. package/lib/cjs/agent/agent/http/types.d.ts +1 -1
  21. package/lib/cjs/agent/agent/http/types.d.ts.map +1 -1
  22. package/lib/cjs/agent/auth.d.ts +1 -1
  23. package/lib/cjs/agent/auth.d.ts.map +1 -1
  24. package/lib/cjs/agent/canisterStatus/index.d.ts +5 -4
  25. package/lib/cjs/agent/canisterStatus/index.d.ts.map +1 -1
  26. package/lib/cjs/agent/canisterStatus/index.js +6 -10
  27. package/lib/cjs/agent/canisterStatus/index.js.map +1 -1
  28. package/lib/cjs/agent/cbor.js +2 -2
  29. package/lib/cjs/agent/cbor.js.map +1 -1
  30. package/lib/cjs/agent/certificate.d.ts +4 -4
  31. package/lib/cjs/agent/certificate.d.ts.map +1 -1
  32. package/lib/cjs/agent/certificate.js +6 -10
  33. package/lib/cjs/agent/certificate.js.map +1 -1
  34. package/lib/cjs/agent/constants.d.ts +4 -4
  35. package/lib/cjs/agent/constants.d.ts.map +1 -1
  36. package/lib/cjs/agent/der.d.ts.map +1 -1
  37. package/lib/cjs/agent/der.js +26 -21
  38. package/lib/cjs/agent/der.js.map +1 -1
  39. package/lib/cjs/agent/errors.d.ts +45 -19
  40. package/lib/cjs/agent/errors.d.ts.map +1 -1
  41. package/lib/cjs/agent/errors.js +58 -18
  42. package/lib/cjs/agent/errors.js.map +1 -1
  43. package/lib/cjs/agent/index.d.ts +1 -0
  44. package/lib/cjs/agent/index.d.ts.map +1 -1
  45. package/lib/cjs/agent/index.js.map +1 -1
  46. package/lib/cjs/agent/observable.d.ts +1 -1
  47. package/lib/cjs/agent/observable.d.ts.map +1 -1
  48. package/lib/cjs/agent/polling/backoff.d.ts +7 -7
  49. package/lib/cjs/agent/polling/backoff.d.ts.map +1 -1
  50. package/lib/cjs/agent/polling/backoff.js +3 -5
  51. package/lib/cjs/agent/polling/backoff.js.map +1 -1
  52. package/lib/cjs/agent/polling/index.d.ts +11 -10
  53. package/lib/cjs/agent/polling/index.d.ts.map +1 -1
  54. package/lib/cjs/agent/polling/index.js +8 -6
  55. package/lib/cjs/agent/polling/index.js.map +1 -1
  56. package/lib/cjs/agent/polling/strategy.d.ts +2 -5
  57. package/lib/cjs/agent/polling/strategy.d.ts.map +1 -1
  58. package/lib/cjs/agent/polling/strategy.js.map +1 -1
  59. package/lib/cjs/agent/polling/types.d.ts +18 -0
  60. package/lib/cjs/agent/polling/types.d.ts.map +1 -0
  61. package/lib/cjs/agent/polling/types.js +3 -0
  62. package/lib/cjs/agent/polling/types.js.map +1 -0
  63. package/lib/cjs/agent/public_key.d.ts +1 -1
  64. package/lib/cjs/agent/public_key.d.ts.map +1 -1
  65. package/lib/cjs/agent/request_id.d.ts.map +1 -1
  66. package/lib/cjs/agent/request_id.js +7 -7
  67. package/lib/cjs/agent/request_id.js.map +1 -1
  68. package/lib/cjs/agent/subnetStatus/index.d.ts +5 -4
  69. package/lib/cjs/agent/subnetStatus/index.d.ts.map +1 -1
  70. package/lib/cjs/agent/subnetStatus/index.js +6 -10
  71. package/lib/cjs/agent/subnetStatus/index.js.map +1 -1
  72. package/lib/cjs/agent/utils/buffer.d.ts.map +1 -1
  73. package/lib/cjs/agent/utils/buffer.js +4 -2
  74. package/lib/cjs/agent/utils/buffer.js.map +1 -1
  75. package/lib/cjs/agent/utils/certificateReject.d.ts +11 -0
  76. package/lib/cjs/agent/utils/certificateReject.d.ts.map +1 -0
  77. package/lib/cjs/agent/utils/certificateReject.js +19 -0
  78. package/lib/cjs/agent/utils/certificateReject.js.map +1 -0
  79. package/lib/cjs/agent/utils/createExpirableStore.d.ts +23 -0
  80. package/lib/cjs/agent/utils/createExpirableStore.d.ts.map +1 -0
  81. package/lib/cjs/agent/utils/createExpirableStore.js +18 -0
  82. package/lib/cjs/agent/utils/createExpirableStore.js.map +1 -0
  83. package/lib/cjs/agent/utils/expirableStore.d.ts +39 -0
  84. package/lib/cjs/agent/utils/expirableStore.d.ts.map +1 -0
  85. package/lib/cjs/agent/utils/expirableStore.js +14 -0
  86. package/lib/cjs/agent/utils/expirableStore.js.map +1 -0
  87. package/lib/cjs/agent/utils/inMemoryExpirableStore.d.ts +14 -0
  88. package/lib/cjs/agent/utils/inMemoryExpirableStore.d.ts.map +1 -0
  89. package/lib/cjs/agent/utils/inMemoryExpirableStore.js +46 -0
  90. package/lib/cjs/agent/utils/inMemoryExpirableStore.js.map +1 -0
  91. package/lib/cjs/agent/utils/indexedDBExpirableStore.d.ts +28 -0
  92. package/lib/cjs/agent/utils/indexedDBExpirableStore.d.ts.map +1 -0
  93. package/lib/cjs/agent/utils/indexedDBExpirableStore.js +139 -0
  94. package/lib/cjs/agent/utils/indexedDBExpirableStore.js.map +1 -0
  95. package/lib/cjs/agent/utils/readState.d.ts +3 -3
  96. package/lib/cjs/agent/utils/readState.d.ts.map +1 -1
  97. package/lib/cjs/candid/candid-core.d.ts +1 -1
  98. package/lib/cjs/candid/candid-core.d.ts.map +1 -1
  99. package/lib/cjs/candid/candid-core.js +7 -9
  100. package/lib/cjs/candid/candid-core.js.map +1 -1
  101. package/lib/cjs/candid/candid-ui.d.ts.map +1 -1
  102. package/lib/cjs/candid/candid-ui.js +8 -24
  103. package/lib/cjs/candid/candid-ui.js.map +1 -1
  104. package/lib/cjs/candid/idl.d.ts +14 -1
  105. package/lib/cjs/candid/idl.d.ts.map +1 -1
  106. package/lib/cjs/candid/idl.js +164 -125
  107. package/lib/cjs/candid/idl.js.map +1 -1
  108. package/lib/cjs/candid/utils/leb128.d.ts.map +1 -1
  109. package/lib/cjs/candid/utils/leb128.js +1 -3
  110. package/lib/cjs/candid/utils/leb128.js.map +1 -1
  111. package/lib/cjs/identity/identity/delegation.d.ts +4 -3
  112. package/lib/cjs/identity/identity/delegation.d.ts.map +1 -1
  113. package/lib/cjs/identity/identity/delegation.js +3 -3
  114. package/lib/cjs/identity/identity/delegation.js.map +1 -1
  115. package/lib/cjs/identity/identity/ecdsa.d.ts +2 -2
  116. package/lib/cjs/identity/identity/ecdsa.d.ts.map +1 -1
  117. package/lib/cjs/identity/identity/ecdsa.js +2 -4
  118. package/lib/cjs/identity/identity/ecdsa.js.map +1 -1
  119. package/lib/cjs/identity/identity/ed25519.d.ts.map +1 -1
  120. package/lib/cjs/identity/identity/ed25519.js +9 -10
  121. package/lib/cjs/identity/identity/ed25519.js.map +1 -1
  122. package/lib/cjs/identity/identity/partial.d.ts +1 -1
  123. package/lib/cjs/identity/identity/partial.d.ts.map +1 -1
  124. package/lib/cjs/identity/identity/webauthn.js +4 -5
  125. package/lib/cjs/identity/identity/webauthn.js.map +1 -1
  126. package/lib/cjs/identity/secp256k1/der.d.ts.map +1 -1
  127. package/lib/cjs/identity/secp256k1/der.js +27 -22
  128. package/lib/cjs/identity/secp256k1/der.js.map +1 -1
  129. package/lib/cjs/identity/secp256k1/pem.js +1 -1
  130. package/lib/cjs/identity/secp256k1/pem.js.map +1 -1
  131. package/lib/cjs/identity/secp256k1/secp256k1.d.ts.map +1 -1
  132. package/lib/cjs/identity/secp256k1/secp256k1.js +6 -6
  133. package/lib/cjs/identity/secp256k1/secp256k1.js.map +1 -1
  134. package/lib/cjs/principal/principal.d.ts +2 -2
  135. package/lib/cjs/principal/principal.d.ts.map +1 -1
  136. package/lib/cjs/principal/principal.js +10 -6
  137. package/lib/cjs/principal/principal.js.map +1 -1
  138. package/lib/esm/agent/actor.d.ts +15 -1
  139. package/lib/esm/agent/actor.d.ts.map +1 -1
  140. package/lib/esm/agent/actor.js +40 -94
  141. package/lib/esm/agent/actor.js.map +1 -1
  142. package/lib/esm/agent/agent/api.d.ts +29 -6
  143. package/lib/esm/agent/agent/api.d.ts.map +1 -1
  144. package/lib/esm/agent/agent/api.js.map +1 -1
  145. package/lib/esm/agent/agent/http/expiry.d.ts +36 -0
  146. package/lib/esm/agent/agent/http/expiry.d.ts.map +1 -0
  147. package/lib/esm/agent/agent/http/expiry.js +88 -0
  148. package/lib/esm/agent/agent/http/expiry.js.map +1 -0
  149. package/lib/esm/agent/agent/http/index.d.ts +28 -2
  150. package/lib/esm/agent/agent/http/index.d.ts.map +1 -1
  151. package/lib/esm/agent/agent/http/index.js +185 -93
  152. package/lib/esm/agent/agent/http/index.js.map +1 -1
  153. package/lib/esm/agent/agent/http/transforms.d.ts +1 -35
  154. package/lib/esm/agent/agent/http/transforms.d.ts.map +1 -1
  155. package/lib/esm/agent/agent/http/transforms.js +2 -87
  156. package/lib/esm/agent/agent/http/transforms.js.map +1 -1
  157. package/lib/esm/agent/agent/http/types.d.ts +1 -1
  158. package/lib/esm/agent/agent/http/types.d.ts.map +1 -1
  159. package/lib/esm/agent/auth.d.ts +1 -1
  160. package/lib/esm/agent/auth.d.ts.map +1 -1
  161. package/lib/esm/agent/canister-env/index.d.ts +2 -2
  162. package/lib/esm/agent/canister-env/index.d.ts.map +1 -1
  163. package/lib/esm/agent/canisterStatus/index.d.ts +5 -4
  164. package/lib/esm/agent/canisterStatus/index.d.ts.map +1 -1
  165. package/lib/esm/agent/canisterStatus/index.js +6 -10
  166. package/lib/esm/agent/canisterStatus/index.js.map +1 -1
  167. package/lib/esm/agent/cbor.js +1 -1
  168. package/lib/esm/agent/cbor.js.map +1 -1
  169. package/lib/esm/agent/certificate.d.ts +4 -4
  170. package/lib/esm/agent/certificate.d.ts.map +1 -1
  171. package/lib/esm/agent/certificate.js +6 -10
  172. package/lib/esm/agent/certificate.js.map +1 -1
  173. package/lib/esm/agent/constants.d.ts +4 -4
  174. package/lib/esm/agent/constants.d.ts.map +1 -1
  175. package/lib/esm/agent/der.d.ts.map +1 -1
  176. package/lib/esm/agent/der.js +26 -21
  177. package/lib/esm/agent/der.js.map +1 -1
  178. package/lib/esm/agent/errors.d.ts +45 -19
  179. package/lib/esm/agent/errors.d.ts.map +1 -1
  180. package/lib/esm/agent/errors.js +54 -16
  181. package/lib/esm/agent/errors.js.map +1 -1
  182. package/lib/esm/agent/index.d.ts +1 -0
  183. package/lib/esm/agent/index.d.ts.map +1 -1
  184. package/lib/esm/agent/index.js.map +1 -1
  185. package/lib/esm/agent/observable.d.ts +1 -1
  186. package/lib/esm/agent/observable.d.ts.map +1 -1
  187. package/lib/esm/agent/polling/backoff.d.ts +7 -7
  188. package/lib/esm/agent/polling/backoff.d.ts.map +1 -1
  189. package/lib/esm/agent/polling/backoff.js +3 -5
  190. package/lib/esm/agent/polling/backoff.js.map +1 -1
  191. package/lib/esm/agent/polling/index.d.ts +11 -10
  192. package/lib/esm/agent/polling/index.d.ts.map +1 -1
  193. package/lib/esm/agent/polling/index.js +9 -7
  194. package/lib/esm/agent/polling/index.js.map +1 -1
  195. package/lib/esm/agent/polling/strategy.d.ts +2 -5
  196. package/lib/esm/agent/polling/strategy.d.ts.map +1 -1
  197. package/lib/esm/agent/polling/strategy.js.map +1 -1
  198. package/lib/esm/agent/polling/types.d.ts +18 -0
  199. package/lib/esm/agent/polling/types.d.ts.map +1 -0
  200. package/lib/esm/agent/polling/types.js +2 -0
  201. package/lib/esm/agent/polling/types.js.map +1 -0
  202. package/lib/esm/agent/public_key.d.ts +1 -1
  203. package/lib/esm/agent/public_key.d.ts.map +1 -1
  204. package/lib/esm/agent/request_id.d.ts.map +1 -1
  205. package/lib/esm/agent/request_id.js +7 -7
  206. package/lib/esm/agent/request_id.js.map +1 -1
  207. package/lib/esm/agent/subnetStatus/index.d.ts +5 -4
  208. package/lib/esm/agent/subnetStatus/index.d.ts.map +1 -1
  209. package/lib/esm/agent/subnetStatus/index.js +6 -10
  210. package/lib/esm/agent/subnetStatus/index.js.map +1 -1
  211. package/lib/esm/agent/utils/buffer.d.ts.map +1 -1
  212. package/lib/esm/agent/utils/buffer.js +4 -2
  213. package/lib/esm/agent/utils/buffer.js.map +1 -1
  214. package/lib/esm/agent/utils/certificateReject.d.ts +11 -0
  215. package/lib/esm/agent/utils/certificateReject.d.ts.map +1 -0
  216. package/lib/esm/agent/utils/certificateReject.js +16 -0
  217. package/lib/esm/agent/utils/certificateReject.js.map +1 -0
  218. package/lib/esm/agent/utils/createExpirableStore.d.ts +23 -0
  219. package/lib/esm/agent/utils/createExpirableStore.d.ts.map +1 -0
  220. package/lib/esm/agent/utils/createExpirableStore.js +15 -0
  221. package/lib/esm/agent/utils/createExpirableStore.js.map +1 -0
  222. package/lib/esm/agent/utils/expirableStore.d.ts +39 -0
  223. package/lib/esm/agent/utils/expirableStore.d.ts.map +1 -0
  224. package/lib/esm/agent/utils/expirableStore.js +11 -0
  225. package/lib/esm/agent/utils/expirableStore.js.map +1 -0
  226. package/lib/esm/agent/utils/inMemoryExpirableStore.d.ts +14 -0
  227. package/lib/esm/agent/utils/inMemoryExpirableStore.d.ts.map +1 -0
  228. package/lib/esm/agent/utils/inMemoryExpirableStore.js +42 -0
  229. package/lib/esm/agent/utils/inMemoryExpirableStore.js.map +1 -0
  230. package/lib/esm/agent/utils/indexedDBExpirableStore.d.ts +28 -0
  231. package/lib/esm/agent/utils/indexedDBExpirableStore.d.ts.map +1 -0
  232. package/lib/esm/agent/utils/indexedDBExpirableStore.js +135 -0
  233. package/lib/esm/agent/utils/indexedDBExpirableStore.js.map +1 -0
  234. package/lib/esm/agent/utils/readState.d.ts +3 -3
  235. package/lib/esm/agent/utils/readState.d.ts.map +1 -1
  236. package/lib/esm/candid/candid-core.d.ts +1 -1
  237. package/lib/esm/candid/candid-core.d.ts.map +1 -1
  238. package/lib/esm/candid/candid-core.js +7 -9
  239. package/lib/esm/candid/candid-core.js.map +1 -1
  240. package/lib/esm/candid/candid-ui.d.ts.map +1 -1
  241. package/lib/esm/candid/candid-ui.js +7 -23
  242. package/lib/esm/candid/candid-ui.js.map +1 -1
  243. package/lib/esm/candid/idl.d.ts +14 -1
  244. package/lib/esm/candid/idl.d.ts.map +1 -1
  245. package/lib/esm/candid/idl.js +162 -124
  246. package/lib/esm/candid/idl.js.map +1 -1
  247. package/lib/esm/candid/utils/leb128.d.ts.map +1 -1
  248. package/lib/esm/candid/utils/leb128.js +1 -3
  249. package/lib/esm/candid/utils/leb128.js.map +1 -1
  250. package/lib/esm/identity/identity/delegation.d.ts +4 -3
  251. package/lib/esm/identity/identity/delegation.d.ts.map +1 -1
  252. package/lib/esm/identity/identity/delegation.js +3 -3
  253. package/lib/esm/identity/identity/delegation.js.map +1 -1
  254. package/lib/esm/identity/identity/ecdsa.d.ts +2 -2
  255. package/lib/esm/identity/identity/ecdsa.d.ts.map +1 -1
  256. package/lib/esm/identity/identity/ecdsa.js +2 -4
  257. package/lib/esm/identity/identity/ecdsa.js.map +1 -1
  258. package/lib/esm/identity/identity/ed25519.d.ts.map +1 -1
  259. package/lib/esm/identity/identity/ed25519.js +9 -10
  260. package/lib/esm/identity/identity/ed25519.js.map +1 -1
  261. package/lib/esm/identity/identity/partial.d.ts +1 -1
  262. package/lib/esm/identity/identity/partial.d.ts.map +1 -1
  263. package/lib/esm/identity/identity/webauthn.js +4 -5
  264. package/lib/esm/identity/identity/webauthn.js.map +1 -1
  265. package/lib/esm/identity/secp256k1/der.d.ts.map +1 -1
  266. package/lib/esm/identity/secp256k1/der.js +27 -22
  267. package/lib/esm/identity/secp256k1/der.js.map +1 -1
  268. package/lib/esm/identity/secp256k1/pem.js +1 -1
  269. package/lib/esm/identity/secp256k1/pem.js.map +1 -1
  270. package/lib/esm/identity/secp256k1/secp256k1.d.ts.map +1 -1
  271. package/lib/esm/identity/secp256k1/secp256k1.js +6 -6
  272. package/lib/esm/identity/secp256k1/secp256k1.js.map +1 -1
  273. package/lib/esm/principal/principal.d.ts +2 -2
  274. package/lib/esm/principal/principal.d.ts.map +1 -1
  275. package/lib/esm/principal/principal.js +10 -6
  276. package/lib/esm/principal/principal.js.map +1 -1
  277. package/package.json +14 -13
  278. package/src/agent/actor.ts +74 -126
  279. package/src/agent/agent/api.ts +36 -6
  280. package/src/agent/agent/http/expiry.ts +107 -0
  281. package/src/agent/agent/http/index.ts +262 -104
  282. package/src/agent/agent/http/transforms.ts +2 -106
  283. package/src/agent/agent/http/types.ts +1 -1
  284. package/src/agent/auth.ts +1 -1
  285. package/src/agent/canister-env/index.ts +2 -2
  286. package/src/agent/canisterStatus/index.ts +10 -12
  287. package/src/agent/cbor.ts +1 -1
  288. package/src/agent/certificate.ts +11 -13
  289. package/src/agent/der.ts +40 -19
  290. package/src/agent/errors.ts +85 -34
  291. package/src/agent/fetch_candid.ts +1 -1
  292. package/src/agent/index.ts +1 -0
  293. package/src/agent/observable.ts +1 -1
  294. package/src/agent/polling/backoff.ts +9 -10
  295. package/src/agent/polling/index.ts +14 -26
  296. package/src/agent/polling/strategy.ts +5 -9
  297. package/src/agent/polling/types.ts +28 -0
  298. package/src/agent/public_key.ts +1 -1
  299. package/src/agent/request_id.ts +15 -8
  300. package/src/agent/subnetStatus/index.ts +10 -12
  301. package/src/agent/utils/buffer.ts +6 -2
  302. package/src/agent/utils/certificateReject.ts +25 -0
  303. package/src/agent/utils/createExpirableStore.ts +31 -0
  304. package/src/agent/utils/expirableStore.ts +47 -0
  305. package/src/agent/utils/inMemoryExpirableStore.ts +49 -0
  306. package/src/agent/utils/indexedDBExpirableStore.ts +191 -0
  307. package/src/agent/utils/random.ts +2 -2
  308. package/src/agent/utils/readState.ts +3 -3
  309. package/src/candid/candid-core.ts +9 -11
  310. package/src/candid/candid-ui.ts +8 -16
  311. package/src/candid/idl.ts +210 -112
  312. package/src/candid/utils/leb128.ts +1 -2
  313. package/src/identity/identity/delegation.ts +5 -5
  314. package/src/identity/identity/ecdsa.ts +12 -8
  315. package/src/identity/identity/ed25519.ts +16 -9
  316. package/src/identity/identity/partial.ts +1 -1
  317. package/src/identity/identity/webauthn.ts +21 -21
  318. package/src/identity/secp256k1/der.ts +41 -20
  319. package/src/identity/secp256k1/pem.ts +1 -1
  320. package/src/identity/secp256k1/secp256k1.ts +12 -6
  321. package/src/principal/principal.ts +18 -8
  322. package/LICENSE +0 -201
  323. package/lib/cjs/agent/utils/expirableMap.d.ts +0 -80
  324. package/lib/cjs/agent/utils/expirableMap.d.ts.map +0 -1
  325. package/lib/cjs/agent/utils/expirableMap.js +0 -148
  326. package/lib/cjs/agent/utils/expirableMap.js.map +0 -1
  327. package/lib/esm/agent/utils/expirableMap.d.ts +0 -80
  328. package/lib/esm/agent/utils/expirableMap.d.ts.map +0 -1
  329. package/lib/esm/agent/utils/expirableMap.js +0 -144
  330. package/lib/esm/agent/utils/expirableMap.js.map +0 -1
  331. package/src/agent/utils/expirableMap.ts +0 -163
package/src/candid/idl.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
3
  import { Principal as PrincipalId } from '#principal';
4
- import { type JsonValue } from './types.ts';
4
+ import type { JsonValue } from './types.ts';
5
5
  import { concat, PipeArrayBuffer as Pipe, uint8ToDataView } from './utils/buffer.ts';
6
6
  import { idlLabelToId } from './utils/hash.ts';
7
7
  import {
@@ -24,6 +24,19 @@ import { iexp2 } from './utils/bigint-math.ts';
24
24
  * as documented at https://github.com/dfinity/candid/blob/119703ba342d2fef6ab4972d2541b9fe36ae8e36/spec/Candid.md
25
25
  */
26
26
 
27
+ /**
28
+ * Error thrown when candid decoding fails due to a type mismatch
29
+ * between the expected and received types.
30
+ */
31
+ export class CandidDecodeError extends Error {
32
+ public override name = 'CandidDecodeError';
33
+
34
+ constructor(message: string) {
35
+ super(message);
36
+ Object.setPrototypeOf(this, CandidDecodeError.prototype);
37
+ }
38
+ }
39
+
27
40
  enum IDLTypeIds {
28
41
  Null = -1,
29
42
  Bool = -2,
@@ -75,10 +88,10 @@ class TypeTable {
75
88
  const idx = this._idx.get(obj.name);
76
89
  const knotIdx = this._idx.get(knot);
77
90
  if (idx === undefined) {
78
- throw new Error('Missing type index for ' + obj);
91
+ throw new Error(`Missing type index for ${obj}`);
79
92
  }
80
93
  if (knotIdx === undefined) {
81
- throw new Error('Missing type index for ' + knot);
94
+ throw new Error(`Missing type index for ${knot}`);
82
95
  }
83
96
  // Point the recursive placeholder (obj) to the concrete type bytes
84
97
  this._typs[idx] = this._typs[knotIdx];
@@ -122,7 +135,7 @@ class TypeTable {
122
135
 
123
136
  public indexOf(typeName: string): Uint8Array {
124
137
  if (!this._idx.has(typeName)) {
125
- throw new Error('Missing type index for ' + typeName);
138
+ throw new Error(`Missing type index for ${typeName}`);
126
139
  }
127
140
  return slebEncode(this._idx.get(typeName) || 0);
128
141
  }
@@ -286,7 +299,6 @@ export abstract class PrimitiveType<T = any> extends Type<T> {
286
299
 
287
300
  public _buildTypeTableImpl(_typeTable: TypeTable): void {
288
301
  // No type table encoding for Primitive types.
289
- return;
290
302
  }
291
303
  }
292
304
 
@@ -295,7 +307,7 @@ export abstract class ConstructType<T = any> extends Type<T> {
295
307
  if (t instanceof RecClass) {
296
308
  const ty = t.getType();
297
309
  if (typeof ty === 'undefined') {
298
- throw new Error('type mismatch with uninitialized type');
310
+ throw new CandidDecodeError('Type mismatch: cannot decode from uninitialized recursive type');
299
311
  }
300
312
  return ty;
301
313
  }
@@ -444,7 +456,9 @@ export class BoolClass extends PrimitiveType<boolean> {
444
456
  }
445
457
 
446
458
  public covariant(x: any): x is boolean {
447
- if (typeof x === 'boolean') return true;
459
+ if (typeof x === 'boolean') {
460
+ return true;
461
+ }
448
462
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
449
463
  }
450
464
 
@@ -490,7 +504,9 @@ export class NullClass extends PrimitiveType<null> {
490
504
  }
491
505
 
492
506
  public covariant(x: any): x is null {
493
- if (x === null) return true;
507
+ if (x === null) {
508
+ return true;
509
+ }
494
510
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
495
511
  }
496
512
 
@@ -569,7 +585,9 @@ export class TextClass extends PrimitiveType<string> {
569
585
  }
570
586
 
571
587
  public covariant(x: any): x is string {
572
- if (typeof x === 'string') return true;
588
+ if (typeof x === 'string') {
589
+ return true;
590
+ }
573
591
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
574
592
  }
575
593
 
@@ -596,7 +614,7 @@ export class TextClass extends PrimitiveType<string> {
596
614
  }
597
615
 
598
616
  public valueToString(x: string) {
599
- return '"' + x + '"';
617
+ return `"${x}"`;
600
618
  }
601
619
  }
602
620
 
@@ -619,7 +637,9 @@ export class IntClass extends PrimitiveType<bigint> {
619
637
  public covariant(x: any): x is bigint {
620
638
  // We allow encoding of JavaScript plain numbers.
621
639
  // But we will always decode to bigint.
622
- if (typeof x === 'bigint' || Number.isInteger(x)) return true;
640
+ if (typeof x === 'bigint' || Number.isInteger(x)) {
641
+ return true;
642
+ }
623
643
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
624
644
  }
625
645
 
@@ -664,7 +684,9 @@ export class NatClass extends PrimitiveType<bigint> {
664
684
  public covariant(x: any): x is bigint {
665
685
  // We allow encoding of JavaScript plain numbers.
666
686
  // But we will always decode to bigint.
667
- if ((typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0)) return true;
687
+ if ((typeof x === 'bigint' && x >= BigInt(0)) || (Number.isInteger(x) && x >= 0)) {
688
+ return true;
689
+ }
668
690
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
669
691
  }
670
692
 
@@ -713,7 +735,9 @@ export class FloatClass extends PrimitiveType<number> {
713
735
  }
714
736
 
715
737
  public covariant(x: any): x is number {
716
- if (typeof x === 'number' || x instanceof Number) return true;
738
+ if (typeof x === 'number' || x instanceof Number) {
739
+ return true;
740
+ }
717
741
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
718
742
  }
719
743
 
@@ -739,13 +763,12 @@ export class FloatClass extends PrimitiveType<number> {
739
763
  const view = uint8ToDataView(bytes);
740
764
  if (this._bits === 32) {
741
765
  return view.getFloat32(0, true);
742
- } else {
743
- return view.getFloat64(0, true);
744
766
  }
767
+ return view.getFloat64(0, true);
745
768
  }
746
769
 
747
770
  get name() {
748
- return 'float' + this._bits;
771
+ return `float${this._bits}`;
749
772
  }
750
773
 
751
774
  public valueToString(x: number) {
@@ -776,17 +799,16 @@ export class FixedIntClass extends PrimitiveType<bigint | number> {
776
799
  public covariant(x: any): x is bigint {
777
800
  const min = iexp2(this._bits - 1) * BigInt(-1);
778
801
  const max = iexp2(this._bits - 1) - BigInt(1);
779
- let ok = false;
780
802
  if (typeof x === 'bigint') {
781
- ok = x >= min && x <= max;
803
+ if (x >= min && x <= max) {
804
+ return true;
805
+ }
782
806
  } else if (Number.isInteger(x)) {
783
807
  const v = BigInt(x);
784
- ok = v >= min && v <= max;
785
- } else {
786
- ok = false;
808
+ if (v >= min && v <= max) {
809
+ return true;
810
+ }
787
811
  }
788
-
789
- if (ok) return true;
790
812
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
791
813
  }
792
814
 
@@ -804,9 +826,8 @@ export class FixedIntClass extends PrimitiveType<bigint | number> {
804
826
  const num = readIntLE(b, this._bits / 8);
805
827
  if (this._bits <= 32) {
806
828
  return Number(num);
807
- } else {
808
- return num;
809
829
  }
830
+ return num;
810
831
  }
811
832
 
812
833
  get name() {
@@ -840,16 +861,16 @@ export class FixedNatClass extends PrimitiveType<bigint | number> {
840
861
 
841
862
  public covariant(x: any): x is bigint {
842
863
  const max = iexp2(this._bits);
843
- let ok = false;
844
864
  if (typeof x === 'bigint' && x >= BigInt(0)) {
845
- ok = x < max;
865
+ if (x < max) {
866
+ return true;
867
+ }
846
868
  } else if (Number.isInteger(x) && x >= 0) {
847
869
  const v = BigInt(x);
848
- ok = v < max;
849
- } else {
850
- ok = false;
870
+ if (v < max) {
871
+ return true;
872
+ }
851
873
  }
852
- if (ok) return true;
853
874
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
854
875
  }
855
876
 
@@ -867,9 +888,8 @@ export class FixedNatClass extends PrimitiveType<bigint | number> {
867
888
  const num = readUIntLE(b, this._bits / 8);
868
889
  if (this._bits <= 32) {
869
890
  return Number(num);
870
- } else {
871
- return num;
872
891
  }
892
+ return num;
873
893
  }
874
894
 
875
895
  get name() {
@@ -932,11 +952,15 @@ export class VecClass<T> extends ConstructType<T[]> {
932
952
  try {
933
953
  return this._type.covariant(v);
934
954
  } catch (e: any) {
935
- throw new Error(`Invalid ${this.display()} argument: \n\nindex ${idx} -> ${e.message}`);
955
+ throw new Error(
956
+ `Invalid ${this.display()} argument: \n\nindex ${idx} -> ${e.message}`,
957
+ { cause: e },
958
+ );
936
959
  }
937
960
  }))
938
- )
961
+ ) {
939
962
  return true;
963
+ }
940
964
 
941
965
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
942
966
  }
@@ -959,7 +983,8 @@ export class VecClass<T> extends ConstructType<T[]> {
959
983
  }
960
984
  }
961
985
  return concat(len, new Uint8Array(buffer.buffer));
962
- } else if (x instanceof Int32Array || x instanceof Uint32Array) {
986
+ }
987
+ if (x instanceof Int32Array || x instanceof Uint32Array) {
963
988
  const buffer = new DataView(new ArrayBuffer(x.length * 4));
964
989
  for (let i = 0; i < x.length; i++) {
965
990
  if (x instanceof Int32Array) {
@@ -969,7 +994,8 @@ export class VecClass<T> extends ConstructType<T[]> {
969
994
  }
970
995
  }
971
996
  return concat(len, new Uint8Array(buffer.buffer));
972
- } else if (x instanceof BigInt64Array || x instanceof BigUint64Array) {
997
+ }
998
+ if (x instanceof BigInt64Array || x instanceof BigUint64Array) {
973
999
  const buffer = new DataView(new ArrayBuffer(x.length * 8));
974
1000
  for (let i = 0; i < x.length; i++) {
975
1001
  if (x instanceof BigInt64Array) {
@@ -979,10 +1005,9 @@ export class VecClass<T> extends ConstructType<T[]> {
979
1005
  }
980
1006
  }
981
1007
  return concat(len, new Uint8Array(buffer.buffer));
982
- } else {
983
- // For Uint8Array, Int8Array, etc. that don't have endianness concerns
984
- return concat(len, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
985
1008
  }
1009
+ // For Uint8Array, Int8Array, etc. that don't have endianness concerns
1010
+ return concat(len, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
986
1011
  }
987
1012
  const buf = new Pipe(new Uint8Array(len.byteLength + x.length), 0);
988
1013
  buf.write(len);
@@ -1004,7 +1029,7 @@ export class VecClass<T> extends ConstructType<T[]> {
1004
1029
  public decodeValue(b: Pipe, t: Type): T[] {
1005
1030
  const vec = this.checkType(t);
1006
1031
  if (!(vec instanceof VecClass)) {
1007
- throw new Error('Not a vector type');
1032
+ throw new CandidDecodeError(`Expected type '${this.display()}', but received non-vector type '${vec.display()}'`);
1008
1033
  }
1009
1034
  const len = Number(lebDecode(b));
1010
1035
 
@@ -1084,7 +1109,7 @@ export class VecClass<T> extends ConstructType<T[]> {
1084
1109
 
1085
1110
  public valueToString(x: T[]) {
1086
1111
  const elements = x.map(e => this._type.valueToString(e));
1087
- return 'vec {' + elements.join('; ') + '}';
1112
+ return `vec {${elements.join('; ')}}`;
1088
1113
  }
1089
1114
  }
1090
1115
 
@@ -1111,11 +1136,13 @@ export class OptClass<T> extends ConstructType<[T] | []> {
1111
1136
 
1112
1137
  public covariant(x: any): x is [T] | [] {
1113
1138
  try {
1114
- if (Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0]))))
1139
+ if (Array.isArray(x) && (x.length === 0 || (x.length === 1 && this._type.covariant(x[0])))) {
1115
1140
  return true;
1141
+ }
1116
1142
  } catch (e: any) {
1117
1143
  throw new Error(
1118
1144
  `Invalid ${this.display()} argument: ${toReadableString(x)} \n\n-> ${e.message}`,
1145
+ { cause: e },
1119
1146
  );
1120
1147
  }
1121
1148
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
@@ -1124,9 +1151,8 @@ export class OptClass<T> extends ConstructType<[T] | []> {
1124
1151
  public encodeValue(x: [T] | []): Uint8Array {
1125
1152
  if (x.length === 0) {
1126
1153
  return new Uint8Array([0]);
1127
- } else {
1128
- return concat(new Uint8Array([1]), this._type.encodeValue(x[0]));
1129
1154
  }
1155
+ return concat(new Uint8Array([1]), this._type.encodeValue(x[0]));
1130
1156
  }
1131
1157
 
1132
1158
  public _buildTypeTableImpl(typeTable: TypeTable) {
@@ -1151,8 +1177,10 @@ export class OptClass<T> extends ConstructType<[T] | []> {
1151
1177
  if (t instanceof RecClass) {
1152
1178
  const ty = t.getType();
1153
1179
  if (typeof ty === 'undefined') {
1154
- throw new Error('type mismatch with uninitialized type');
1155
- } else wireType = ty;
1180
+ throw new CandidDecodeError('Type mismatch: cannot decode from uninitialized recursive type');
1181
+ } else {
1182
+ wireType = ty;
1183
+ }
1156
1184
  }
1157
1185
 
1158
1186
  if (wireType instanceof OptClass) {
@@ -1176,7 +1204,7 @@ export class OptClass<T> extends ConstructType<[T] | []> {
1176
1204
  }
1177
1205
  }
1178
1206
  default:
1179
- throw new Error('Not an option value');
1207
+ throw new CandidDecodeError(`Invalid option value: expected 0 (None) or 1 (Some) for type '${this.display()}'`);
1180
1208
  }
1181
1209
  } else if (
1182
1210
  // this check corresponds to `not (null <: <t>)` in the spec
@@ -1217,9 +1245,8 @@ export class OptClass<T> extends ConstructType<[T] | []> {
1217
1245
  public valueToString(x: [T] | []) {
1218
1246
  if (x.length === 0) {
1219
1247
  return 'null';
1220
- } else {
1221
- return `opt ${this._type.valueToString(x[0])}`;
1222
1248
  }
1249
+ return `opt ${this._type.valueToString(x[0])}`;
1223
1250
  }
1224
1251
  }
1225
1252
 
@@ -1273,11 +1300,14 @@ export class RecordClass extends ConstructType<Record<string, any>> {
1273
1300
  try {
1274
1301
  return t.covariant(x[k]);
1275
1302
  } catch (e: any) {
1276
- throw new Error(`Invalid ${this.display()} argument: \n\nfield ${k} -> ${e.message}`);
1303
+ throw new Error(`Invalid ${this.display()} argument: \n\nfield ${k} -> ${e.message}`, {
1304
+ cause: e,
1305
+ });
1277
1306
  }
1278
1307
  })
1279
- )
1308
+ ) {
1280
1309
  return true;
1310
+ }
1281
1311
 
1282
1312
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1283
1313
  }
@@ -1302,7 +1332,7 @@ export class RecordClass extends ConstructType<Record<string, any>> {
1302
1332
  public decodeValue(b: Pipe, t: Type) {
1303
1333
  const record = this.checkType(t);
1304
1334
  if (!(record instanceof RecordClass)) {
1305
- throw new Error('Not a record type');
1335
+ throw new CandidDecodeError(`Expected type '${this.display()}', but received non-record type '${record.display()}'`);
1306
1336
  }
1307
1337
  const x: Record<string, any> = {};
1308
1338
 
@@ -1332,7 +1362,11 @@ export class RecordClass extends ConstructType<Record<string, any>> {
1332
1362
  x[expectKey] = [];
1333
1363
  expectedRecordIdx++;
1334
1364
  } else {
1335
- throw new Error('Cannot find required field ' + expectKey);
1365
+ const expectedFields = this._fields.map(([name]) => name).join(', ');
1366
+ const receivedFields = record._fields.map(([name]) => name).join(', ');
1367
+ throw new CandidDecodeError(
1368
+ `Cannot find required field '${expectKey}' in record, expected fields: [${expectedFields}], received fields: [${receivedFields}]`,
1369
+ );
1336
1370
  }
1337
1371
  } else {
1338
1372
  // The field on the wire does not exist in the output type, so we can skip it
@@ -1347,7 +1381,11 @@ export class RecordClass extends ConstructType<Record<string, any>> {
1347
1381
  // TODO this assumes null value in opt is represented as []
1348
1382
  x[expectKey] = [];
1349
1383
  } else {
1350
- throw new Error('Cannot find required field ' + expectKey);
1384
+ const expectedFields = this._fields.map(([name]) => name).join(', ');
1385
+ const receivedFields = record._fields.map(([name]) => name).join(', ');
1386
+ throw new CandidDecodeError(
1387
+ `Cannot find required field '${expectKey}' in record, expected fields: [${expectedFields}], received fields: [${receivedFields}]`,
1388
+ );
1351
1389
  }
1352
1390
  }
1353
1391
  return x;
@@ -1362,18 +1400,18 @@ export class RecordClass extends ConstructType<Record<string, any>> {
1362
1400
  }
1363
1401
 
1364
1402
  get name() {
1365
- const fields = this._fields.map(([key, value]) => key + ':' + value.name);
1403
+ const fields = this._fields.map(([key, value]) => `${key}:${value.name}`);
1366
1404
  return `record {${fields.join('; ')}}`;
1367
1405
  }
1368
1406
 
1369
1407
  public display() {
1370
- const fields = this._fields.map(([key, value]) => key + ':' + value.display());
1408
+ const fields = this._fields.map(([key, value]) => `${key}:${value.display()}`);
1371
1409
  return `record {${fields.join('; ')}}`;
1372
1410
  }
1373
1411
 
1374
1412
  public valueToString(x: Record<string, any>) {
1375
1413
  const values = this._fields.map(([key]) => x[key]);
1376
- const fields = zipWith(this._fields, values, ([k, c], d) => k + '=' + c.valueToString(d));
1414
+ const fields = zipWith(this._fields, values, ([k, c], d) => `${k}=${c.valueToString(d)}`);
1377
1415
  return `record {${fields.join('; ')}}`;
1378
1416
  }
1379
1417
  }
@@ -1395,7 +1433,7 @@ export class TupleClass<T extends any[]> extends RecordClass {
1395
1433
 
1396
1434
  constructor(_components: Type[]) {
1397
1435
  const x: Record<string, any> = {};
1398
- _components.forEach((e, i) => (x['_' + i + '_'] = e));
1436
+ _components.forEach((e, i) => (x[`_${i}_`] = e));
1399
1437
  super(x);
1400
1438
  this._components = _components;
1401
1439
  }
@@ -1414,11 +1452,14 @@ export class TupleClass<T extends any[]> extends RecordClass {
1414
1452
  try {
1415
1453
  return t.covariant(x[i]);
1416
1454
  } catch (e: any) {
1417
- throw new Error(`Invalid ${this.display()} argument: \n\nindex ${i} -> ${e.message}`);
1455
+ throw new Error(`Invalid ${this.display()} argument: \n\nindex ${i} -> ${e.message}`, {
1456
+ cause: e,
1457
+ });
1418
1458
  }
1419
1459
  })
1420
- )
1460
+ ) {
1421
1461
  return true;
1462
+ }
1422
1463
 
1423
1464
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1424
1465
  }
@@ -1431,10 +1472,12 @@ export class TupleClass<T extends any[]> extends RecordClass {
1431
1472
  public decodeValue(b: Pipe, t: Type): T {
1432
1473
  const tuple = this.checkType(t);
1433
1474
  if (!(tuple instanceof TupleClass)) {
1434
- throw new Error('not a tuple type');
1475
+ throw new CandidDecodeError(`Expected type '${this.display()}', but received non-tuple type '${tuple.display()}'`);
1435
1476
  }
1436
1477
  if (tuple._components.length < this._components.length) {
1437
- throw new Error('tuple mismatch');
1478
+ throw new CandidDecodeError(
1479
+ `Tuple mismatch: expected ${this._components.length} components, but received ${tuple._components.length}`,
1480
+ );
1438
1481
  }
1439
1482
  const res = [];
1440
1483
  for (const [i, wireType] of tuple._components.entries()) {
@@ -1492,11 +1535,14 @@ export class VariantClass extends ConstructType<Record<string, any>> {
1492
1535
  // eslint-disable-next-line
1493
1536
  return !x.hasOwnProperty(k) || v.covariant(x[k]);
1494
1537
  } catch (e: any) {
1495
- throw new Error(`Invalid ${this.display()} argument: \n\nvariant ${k} -> ${e.message}`);
1538
+ throw new Error(`Invalid ${this.display()} argument: \n\nvariant ${k} -> ${e.message}`, {
1539
+ cause: e,
1540
+ });
1496
1541
  }
1497
1542
  })
1498
- )
1543
+ ) {
1499
1544
  return true;
1545
+ }
1500
1546
 
1501
1547
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1502
1548
  }
@@ -1512,7 +1558,7 @@ export class VariantClass extends ConstructType<Record<string, any>> {
1512
1558
  return concat(idx, buf);
1513
1559
  }
1514
1560
  }
1515
- throw Error('Variant has no data: ' + x);
1561
+ throw Error(`Variant has no data: ${x}`);
1516
1562
  }
1517
1563
 
1518
1564
  public _buildTypeTableImpl(typeTable: TypeTable) {
@@ -1530,11 +1576,11 @@ export class VariantClass extends ConstructType<Record<string, any>> {
1530
1576
  public decodeValue(b: Pipe, t: Type) {
1531
1577
  const variant = this.checkType(t);
1532
1578
  if (!(variant instanceof VariantClass)) {
1533
- throw new Error('Not a variant type');
1579
+ throw new CandidDecodeError(`Expected type '${this.display()}', but received non-variant type '${variant.display()}'`);
1534
1580
  }
1535
1581
  const idx = Number(lebDecode(b));
1536
1582
  if (idx >= variant._fields.length) {
1537
- throw Error('Invalid variant index: ' + idx);
1583
+ throw Error(`Invalid variant index: ${idx}`);
1538
1584
  }
1539
1585
  const [wireHash, wireType] = variant._fields[idx];
1540
1586
  for (const [key, expectType] of this._fields) {
@@ -1543,11 +1589,14 @@ export class VariantClass extends ConstructType<Record<string, any>> {
1543
1589
  return { [key]: value };
1544
1590
  }
1545
1591
  }
1546
- throw new Error('Cannot find field hash ' + wireHash);
1592
+ const expectedFields = this._fields.map(([name]) => name).join(', ');
1593
+ throw new CandidDecodeError(
1594
+ `Cannot find field hash ${wireHash} in variant, expected fields: [${expectedFields}]`,
1595
+ );
1547
1596
  }
1548
1597
 
1549
1598
  get name() {
1550
- const fields = this._fields.map(([key, type]) => key + ':' + type.name);
1599
+ const fields = this._fields.map(([key, type]) => `${key}:${type.name}`);
1551
1600
  return `variant {${fields.join('; ')}}`;
1552
1601
  }
1553
1602
 
@@ -1565,12 +1614,11 @@ export class VariantClass extends ConstructType<Record<string, any>> {
1565
1614
  const value = type.valueToString(x[name]);
1566
1615
  if (value === 'null') {
1567
1616
  return `variant {${name}}`;
1568
- } else {
1569
- return `variant {${name}=${value}}`;
1570
1617
  }
1618
+ return `variant {${name}=${value}}`;
1571
1619
  }
1572
1620
  }
1573
- throw new Error('Variant has no data: ' + x);
1621
+ throw new Error(`Variant has no data: ${x}`);
1574
1622
  }
1575
1623
 
1576
1624
  get alternativesAsObject(): Record<number, Type> {
@@ -1615,7 +1663,9 @@ export class RecClass<T = any> extends ConstructType<T> {
1615
1663
  }
1616
1664
 
1617
1665
  public covariant(x: any): x is T {
1618
- if (this._type ? this._type.covariant(x) : false) return true;
1666
+ if (this._type ? this._type.covariant(x) : false) {
1667
+ return true;
1668
+ }
1619
1669
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1620
1670
  }
1621
1671
 
@@ -1688,7 +1738,9 @@ export class PrincipalClass extends PrimitiveType<PrincipalId> {
1688
1738
  }
1689
1739
 
1690
1740
  public covariant(x: any): x is PrincipalId {
1691
- if (x && x._isPrincipal) return true;
1741
+ if (x && x._isPrincipal) {
1742
+ return true;
1743
+ }
1692
1744
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1693
1745
  }
1694
1746
 
@@ -1746,7 +1798,7 @@ export class FuncClass<
1746
1798
  if (types.length !== v.length) {
1747
1799
  throw new Error('arity mismatch');
1748
1800
  }
1749
- return '(' + types.map((t, i) => t.valueToString(v[i])).join(', ') + ')';
1801
+ return `(${types.map((t, i) => t.valueToString(v[i])).join(', ')})`;
1750
1802
  }
1751
1803
 
1752
1804
  constructor(
@@ -1761,8 +1813,15 @@ export class FuncClass<
1761
1813
  return v.visitFunc(this, d);
1762
1814
  }
1763
1815
  public covariant(x: any): x is [PrincipalId, string] {
1764
- if (Array.isArray(x) && x.length === 2 && x[0] && x[0]._isPrincipal && typeof x[1] === 'string')
1816
+ if (
1817
+ Array.isArray(x) &&
1818
+ x.length === 2 &&
1819
+ x[0] &&
1820
+ x[0]._isPrincipal &&
1821
+ typeof x[1] === 'string'
1822
+ ) {
1765
1823
  return true;
1824
+ }
1766
1825
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1767
1826
  }
1768
1827
 
@@ -1815,7 +1874,7 @@ export class FuncClass<
1815
1874
  get name() {
1816
1875
  const args = this.argTypes.map(arg => arg.name).join(', ');
1817
1876
  const rets = this.retTypes.map(arg => arg.name).join(', ');
1818
- const annon = ' ' + this.annotations.join(' ');
1877
+ const annon = ` ${this.annotations.join(' ')}`;
1819
1878
  return `(${args}) -> (${rets})${annon}`;
1820
1879
  }
1821
1880
 
@@ -1826,20 +1885,21 @@ export class FuncClass<
1826
1885
  public display(): string {
1827
1886
  const args = this.argTypes.map(arg => arg.display()).join(', ');
1828
1887
  const rets = this.retTypes.map(arg => arg.display()).join(', ');
1829
- const annon = ' ' + this.annotations.join(' ');
1888
+ const annon = ` ${this.annotations.join(' ')}`;
1830
1889
  return `(${args}) → (${rets})${annon}`;
1831
1890
  }
1832
1891
 
1833
1892
  private encodeAnnotation(ann: string): Uint8Array {
1834
1893
  if (ann === 'query') {
1835
1894
  return new Uint8Array([1]);
1836
- } else if (ann === 'oneway') {
1895
+ }
1896
+ if (ann === 'oneway') {
1837
1897
  return new Uint8Array([2]);
1838
- } else if (ann === 'composite_query') {
1898
+ }
1899
+ if (ann === 'composite_query') {
1839
1900
  return new Uint8Array([3]);
1840
- } else {
1841
- throw new Error('Illegal function annotation');
1842
1901
  }
1902
+ throw new Error('Illegal function annotation');
1843
1903
  }
1844
1904
  }
1845
1905
 
@@ -1877,7 +1937,9 @@ export class ServiceClass<
1877
1937
  return v.visitService(this, d);
1878
1938
  }
1879
1939
  public covariant(x: any): x is PrincipalId {
1880
- if (x && x._isPrincipal) return true;
1940
+ if (x && x._isPrincipal) {
1941
+ return true;
1942
+ }
1881
1943
  throw new Error(`Invalid ${this.display()} argument: ${toReadableString(x)}`);
1882
1944
  }
1883
1945
 
@@ -1910,7 +1972,7 @@ export class ServiceClass<
1910
1972
  return decodePrincipalId(b);
1911
1973
  }
1912
1974
  get name() {
1913
- const fields = this._fields.map(([key, value]) => key + ':' + value.name);
1975
+ const fields = this._fields.map(([key, value]) => `${key}:${value.name}`);
1914
1976
  return `service {${fields.join('; ')}}`;
1915
1977
  }
1916
1978
 
@@ -1938,7 +2000,7 @@ function toReadableString(x: unknown): string {
1938
2000
  );
1939
2001
 
1940
2002
  return str && str.length > toReadableString_max
1941
- ? str.substring(0, toReadableString_max - 3) + '...'
2003
+ ? `${str.substring(0, toReadableString_max - 3)}...`
1942
2004
  : str;
1943
2005
  }
1944
2006
 
@@ -1964,7 +2026,7 @@ export function encode(argTypes: Array<Type<any>>, args: any[]): Uint8Array {
1964
2026
  try {
1965
2027
  t.covariant(x);
1966
2028
  } catch (e: any) {
1967
- const err = new Error(e.message + '\n\n');
2029
+ const err = new Error(`${e.message}\n\n`);
1968
2030
  throw err;
1969
2031
  }
1970
2032
 
@@ -1990,7 +2052,7 @@ export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
1990
2052
  const magicBuffer = safeRead(b, magicNumber.length);
1991
2053
  const magic = new TextDecoder().decode(magicBuffer);
1992
2054
  if (magic !== magicNumber) {
1993
- throw new Error('Wrong magic number: ' + JSON.stringify(magic));
2055
+ throw new Error(`Wrong magic number: ${JSON.stringify(magic)}`);
1994
2056
  }
1995
2057
 
1996
2058
  function readTypeTable(pipe: Pipe): [Array<[IDLTypeIds, any]>, number[]] {
@@ -2074,7 +2136,7 @@ export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
2074
2136
  break;
2075
2137
  }
2076
2138
  default:
2077
- throw new Error('Illegal op_code: ' + ty);
2139
+ throw new Error(`Illegal op_code: ${ty}`);
2078
2140
  }
2079
2141
  }
2080
2142
 
@@ -2134,7 +2196,7 @@ export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
2134
2196
  case -24:
2135
2197
  return Principal;
2136
2198
  default:
2137
- throw new Error('Illegal op_code: ' + t);
2199
+ throw new Error(`Illegal op_code: ${t}`);
2138
2200
  }
2139
2201
  }
2140
2202
  if (t >= rawTable.length) {
@@ -2162,9 +2224,8 @@ export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
2162
2224
  const tuple = record.tryAsTuple();
2163
2225
  if (Array.isArray(tuple)) {
2164
2226
  return Tuple(...tuple);
2165
- } else {
2166
- return record;
2167
2227
  }
2228
+ return record;
2168
2229
  }
2169
2230
  case IDLTypeIds.Variant: {
2170
2231
  const fields: Record<string, Type> = {};
@@ -2200,7 +2261,7 @@ export function decode(retTypes: Type[], bytes: Uint8Array): JsonValue[] {
2200
2261
  return Service(rec);
2201
2262
  }
2202
2263
  default:
2203
- throw new Error('Illegal op_code: ' + entry[0]);
2264
+ throw new Error(`Illegal op_code: ${entry[0]}`);
2204
2265
  }
2205
2266
  }
2206
2267
 
@@ -2455,7 +2516,9 @@ function eqFunctionAnnotations(t1: FuncClass, t2: FuncClass): boolean {
2455
2516
  return false;
2456
2517
  }
2457
2518
  for (const a of t1Annotations) {
2458
- if (!t2Annotations.has(a)) return false;
2519
+ if (!t2Annotations.has(a)) {
2520
+ return false;
2521
+ }
2459
2522
  }
2460
2523
  return true;
2461
2524
  }
@@ -2481,46 +2544,73 @@ export function subtype(t1: Type, t2: Type): boolean {
2481
2544
  }
2482
2545
 
2483
2546
  function subtype_(relations: Relations, t1: Type, t2: Type): boolean {
2484
- if (t1.name === t2.name) return true;
2547
+ if (t1.name === t2.name) {
2548
+ return true;
2549
+ }
2485
2550
  const known = relations.known(t1, t2);
2486
- if (known !== undefined) return known;
2551
+ if (known !== undefined) {
2552
+ return known;
2553
+ }
2487
2554
  relations.add(t1, t2);
2488
2555
 
2489
- if (t2 instanceof ReservedClass) return true;
2490
- if (t1 instanceof EmptyClass) return true;
2491
- if (t1 instanceof NatClass && t2 instanceof IntClass) return true;
2492
- if (t1 instanceof VecClass && t2 instanceof VecClass)
2556
+ if (t2 instanceof ReservedClass) {
2557
+ return true;
2558
+ }
2559
+ if (t1 instanceof EmptyClass) {
2560
+ return true;
2561
+ }
2562
+ if (t1 instanceof NatClass && t2 instanceof IntClass) {
2563
+ return true;
2564
+ }
2565
+ if (t1 instanceof VecClass && t2 instanceof VecClass) {
2493
2566
  return subtype_(relations, t1._type, t2._type);
2494
- if (t2 instanceof OptClass) return true;
2567
+ }
2568
+ if (t2 instanceof OptClass) {
2569
+ return true;
2570
+ }
2495
2571
  if (t1 instanceof RecordClass && t2 instanceof RecordClass) {
2496
2572
  const t1Object = t1.fieldsAsObject;
2497
2573
  for (const [label, ty2] of t2._fields) {
2498
2574
  const ty1 = t1Object[idlLabelToId(label)];
2499
2575
  if (!ty1) {
2500
- if (!canBeOmmitted(ty2)) return false;
2576
+ if (!canBeOmmitted(ty2)) {
2577
+ return false;
2578
+ }
2501
2579
  } else {
2502
- if (!subtype_(relations, ty1, ty2)) return false;
2580
+ if (!subtype_(relations, ty1, ty2)) {
2581
+ return false;
2582
+ }
2503
2583
  }
2504
2584
  }
2505
2585
  return true;
2506
2586
  }
2507
2587
 
2508
2588
  if (t1 instanceof FuncClass && t2 instanceof FuncClass) {
2509
- if (!eqFunctionAnnotations(t1, t2)) return false;
2589
+ if (!eqFunctionAnnotations(t1, t2)) {
2590
+ return false;
2591
+ }
2510
2592
  for (let i = 0; i < t1.argTypes.length; i++) {
2511
2593
  const argTy1 = t1.argTypes[i];
2512
2594
  if (i < t2.argTypes.length) {
2513
- if (!subtype_(relations, t2.argTypes[i], argTy1)) return false;
2595
+ if (!subtype_(relations, t2.argTypes[i], argTy1)) {
2596
+ return false;
2597
+ }
2514
2598
  } else {
2515
- if (!canBeOmmitted(argTy1)) return false;
2599
+ if (!canBeOmmitted(argTy1)) {
2600
+ return false;
2601
+ }
2516
2602
  }
2517
2603
  }
2518
2604
  for (let i = 0; i < t2.retTypes.length; i++) {
2519
2605
  const retTy2 = t2.retTypes[i];
2520
2606
  if (i < t1.retTypes.length) {
2521
- if (!subtype_(relations, t1.retTypes[i], retTy2)) return false;
2607
+ if (!subtype_(relations, t1.retTypes[i], retTy2)) {
2608
+ return false;
2609
+ }
2522
2610
  } else {
2523
- if (!canBeOmmitted(retTy2)) return false;
2611
+ if (!canBeOmmitted(retTy2)) {
2612
+ return false;
2613
+ }
2524
2614
  }
2525
2615
  }
2526
2616
  return true;
@@ -2530,8 +2620,12 @@ function subtype_(relations: Relations, t1: Type, t2: Type): boolean {
2530
2620
  const t2Object = t2.alternativesAsObject;
2531
2621
  for (const [label, ty1] of t1._fields) {
2532
2622
  const ty2 = t2Object[idlLabelToId(label)];
2533
- if (!ty2) return false;
2534
- if (!subtype_(relations, ty1, ty2)) return false;
2623
+ if (!ty2) {
2624
+ return false;
2625
+ }
2626
+ if (!subtype_(relations, ty1, ty2)) {
2627
+ return false;
2628
+ }
2535
2629
  }
2536
2630
  return true;
2537
2631
  }
@@ -2540,8 +2634,12 @@ function subtype_(relations: Relations, t1: Type, t2: Type): boolean {
2540
2634
  const t1Object = t1.fieldsAsObject();
2541
2635
  for (const [name, ty2] of t2._fields) {
2542
2636
  const ty1 = t1Object[name];
2543
- if (!ty1) return false;
2544
- if (!subtype_(relations, ty1, ty2)) return false;
2637
+ if (!ty1) {
2638
+ return false;
2639
+ }
2640
+ if (!subtype_(relations, ty1, ty2)) {
2641
+ return false;
2642
+ }
2545
2643
  }
2546
2644
  return true;
2547
2645
  }