@metamask/utils 8.3.0 → 8.5.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 (346) hide show
  1. package/CHANGELOG.md +26 -1
  2. package/dist/assert.cjs +127 -0
  3. package/dist/assert.cjs.map +1 -0
  4. package/dist/{types/assert.d.ts → assert.d.cts} +3 -3
  5. package/dist/assert.d.cts.map +1 -0
  6. package/dist/assert.d.mts +61 -0
  7. package/dist/assert.d.mts.map +1 -0
  8. package/dist/assert.mjs +119 -15
  9. package/dist/assert.mjs.map +1 -1
  10. package/dist/base64.cjs +34 -0
  11. package/dist/base64.cjs.map +1 -0
  12. package/dist/{types/base64.d.ts → base64.d.cts} +3 -3
  13. package/dist/base64.d.cts.map +1 -0
  14. package/dist/base64.d.mts +25 -0
  15. package/dist/base64.d.mts.map +1 -0
  16. package/dist/base64.mjs +28 -9
  17. package/dist/base64.mjs.map +1 -1
  18. package/dist/bytes.cjs +400 -0
  19. package/dist/{chunk-QEPVHEP7.js.map → bytes.cjs.map} +1 -1
  20. package/dist/{types/bytes.d.ts → bytes.d.cts} +3 -3
  21. package/dist/bytes.d.cts.map +1 -0
  22. package/dist/bytes.d.mts +183 -0
  23. package/dist/bytes.d.mts.map +1 -0
  24. package/dist/bytes.mjs +379 -42
  25. package/dist/bytes.mjs.map +1 -1
  26. package/dist/caip-types.cjs +151 -0
  27. package/dist/caip-types.cjs.map +1 -0
  28. package/dist/{types/caip-types.d.ts → caip-types.d.cts} +33 -12
  29. package/dist/caip-types.d.cts.map +1 -0
  30. package/dist/caip-types.d.mts +114 -0
  31. package/dist/caip-types.d.mts.map +1 -0
  32. package/dist/caip-types.mjs +139 -39
  33. package/dist/caip-types.mjs.map +1 -1
  34. package/dist/checksum.cjs +7 -0
  35. package/dist/checksum.cjs.map +1 -0
  36. package/dist/checksum.d.cts +2 -0
  37. package/dist/checksum.d.cts.map +1 -0
  38. package/dist/checksum.d.mts +2 -0
  39. package/dist/checksum.d.mts.map +1 -0
  40. package/dist/checksum.mjs +3 -11
  41. package/dist/checksum.mjs.map +1 -1
  42. package/dist/coercers.cjs +162 -0
  43. package/dist/coercers.cjs.map +1 -0
  44. package/dist/{types/coercers.d.ts → coercers.d.cts} +7 -7
  45. package/dist/coercers.d.cts.map +1 -0
  46. package/dist/coercers.d.mts +97 -0
  47. package/dist/coercers.d.mts.map +1 -0
  48. package/dist/coercers.mjs +154 -17
  49. package/dist/coercers.mjs.map +1 -1
  50. package/dist/collections.cjs +109 -0
  51. package/dist/collections.cjs.map +1 -0
  52. package/dist/{types/collections.d.ts → collections.d.cts} +1 -1
  53. package/dist/collections.d.cts.map +1 -0
  54. package/dist/collections.d.mts +39 -0
  55. package/dist/collections.d.mts.map +1 -0
  56. package/dist/collections.mjs +103 -8
  57. package/dist/collections.mjs.map +1 -1
  58. package/dist/encryption-types.cjs +3 -0
  59. package/dist/encryption-types.cjs.map +1 -0
  60. package/dist/{types/encryption-types.d.ts → encryption-types.d.cts} +2 -2
  61. package/dist/encryption-types.d.cts.map +1 -0
  62. package/dist/encryption-types.d.mts +7 -0
  63. package/dist/encryption-types.d.mts.map +1 -0
  64. package/dist/encryption-types.mjs +1 -1
  65. package/dist/encryption-types.mjs.map +1 -1
  66. package/dist/errors.cjs +111 -0
  67. package/dist/errors.cjs.map +1 -0
  68. package/dist/{types/errors.d.ts → errors.d.cts} +1 -1
  69. package/dist/errors.d.cts.map +1 -0
  70. package/dist/errors.d.mts +58 -0
  71. package/dist/errors.d.mts.map +1 -0
  72. package/dist/errors.mjs +102 -16
  73. package/dist/errors.mjs.map +1 -1
  74. package/dist/fs.cjs +248 -0
  75. package/dist/fs.cjs.map +1 -0
  76. package/dist/{types/fs.d.ts → fs.d.cts} +3 -3
  77. package/dist/fs.d.cts.map +1 -0
  78. package/dist/fs.d.mts +133 -0
  79. package/dist/fs.d.mts.map +1 -0
  80. package/dist/fs.mjs +209 -25
  81. package/dist/fs.mjs.map +1 -1
  82. package/dist/hex.cjs +134 -0
  83. package/dist/hex.cjs.map +1 -0
  84. package/dist/{types/hex.d.ts → hex.d.cts} +3 -3
  85. package/dist/hex.d.cts.map +1 -0
  86. package/dist/hex.d.mts +77 -0
  87. package/dist/hex.d.mts.map +1 -0
  88. package/dist/hex.mjs +121 -34
  89. package/dist/hex.mjs.map +1 -1
  90. package/dist/index.cjs +37 -0
  91. package/dist/index.cjs.map +1 -0
  92. package/dist/index.d.cts +21 -0
  93. package/dist/index.d.cts.map +1 -0
  94. package/dist/index.d.mts +21 -0
  95. package/dist/index.d.mts.map +1 -0
  96. package/dist/index.mjs +20 -292
  97. package/dist/index.mjs.map +1 -1
  98. package/dist/json.cjs +413 -0
  99. package/dist/json.cjs.map +1 -0
  100. package/dist/{types/json.d.ts → json.d.cts} +21 -22
  101. package/dist/json.d.cts.map +1 -0
  102. package/dist/json.d.mts +383 -0
  103. package/dist/json.d.mts.map +1 -0
  104. package/dist/json.mjs +389 -74
  105. package/dist/json.mjs.map +1 -1
  106. package/dist/keyring.cjs +3 -0
  107. package/dist/keyring.cjs.map +1 -0
  108. package/dist/{types/keyring.d.ts → keyring.d.cts} +7 -7
  109. package/dist/keyring.d.cts.map +1 -0
  110. package/dist/keyring.d.mts +224 -0
  111. package/dist/keyring.d.mts.map +1 -0
  112. package/dist/keyring.mjs +1 -1
  113. package/dist/keyring.mjs.map +1 -1
  114. package/dist/logging.cjs +43 -0
  115. package/dist/logging.cjs.map +1 -0
  116. package/dist/{types/logging.d.ts → logging.d.cts} +2 -2
  117. package/dist/logging.d.cts.map +1 -0
  118. package/dist/logging.d.mts +30 -0
  119. package/dist/logging.d.mts.map +1 -0
  120. package/dist/logging.mjs +34 -9
  121. package/dist/logging.mjs.map +1 -1
  122. package/dist/misc.cjs +149 -0
  123. package/dist/misc.cjs.map +1 -0
  124. package/dist/{types/misc.d.ts → misc.d.cts} +6 -6
  125. package/dist/misc.d.cts.map +1 -0
  126. package/dist/misc.d.mts +119 -0
  127. package/dist/misc.d.mts.map +1 -0
  128. package/dist/misc.mjs +136 -27
  129. package/dist/misc.mjs.map +1 -1
  130. package/dist/node.cjs +19 -0
  131. package/dist/node.cjs.map +1 -0
  132. package/dist/node.d.cts +3 -0
  133. package/dist/node.d.cts.map +1 -0
  134. package/dist/node.d.mts +3 -0
  135. package/dist/node.d.mts.map +1 -0
  136. package/dist/node.mjs +2 -312
  137. package/dist/node.mjs.map +1 -1
  138. package/dist/number.cjs +102 -0
  139. package/dist/number.cjs.map +1 -0
  140. package/dist/{types/number.d.ts → number.d.cts} +1 -1
  141. package/dist/number.d.cts.map +1 -0
  142. package/dist/number.d.mts +73 -0
  143. package/dist/number.d.mts.map +1 -0
  144. package/dist/number.mjs +93 -16
  145. package/dist/number.mjs.map +1 -1
  146. package/dist/opaque.cjs +3 -0
  147. package/dist/opaque.cjs.map +1 -0
  148. package/dist/opaque.d.cts +6 -0
  149. package/dist/opaque.d.cts.map +1 -0
  150. package/dist/opaque.d.mts +6 -0
  151. package/dist/opaque.d.mts.map +1 -0
  152. package/dist/opaque.mjs +1 -1
  153. package/dist/opaque.mjs.map +1 -1
  154. package/dist/promise.cjs +40 -0
  155. package/dist/promise.cjs.map +1 -0
  156. package/dist/{types/promise.d.ts → promise.d.cts} +12 -2
  157. package/dist/promise.d.cts.map +1 -0
  158. package/dist/promise.d.mts +45 -0
  159. package/dist/promise.d.mts.map +1 -0
  160. package/dist/promise.mjs +35 -7
  161. package/dist/promise.mjs.map +1 -1
  162. package/dist/time.cjs +67 -0
  163. package/dist/time.cjs.map +1 -0
  164. package/dist/{types/time.d.ts → time.d.cts} +1 -1
  165. package/dist/time.d.cts.map +1 -0
  166. package/dist/time.d.mts +49 -0
  167. package/dist/time.d.mts.map +1 -0
  168. package/dist/time.mjs +60 -10
  169. package/dist/time.mjs.map +1 -1
  170. package/dist/transaction-types.cjs +3 -0
  171. package/dist/transaction-types.cjs.map +1 -0
  172. package/dist/{types/transaction-types.d.ts → transaction-types.d.cts} +11 -11
  173. package/dist/transaction-types.d.cts.map +1 -0
  174. package/dist/transaction-types.d.mts +117 -0
  175. package/dist/transaction-types.d.mts.map +1 -0
  176. package/dist/transaction-types.mjs +1 -1
  177. package/dist/transaction-types.mjs.map +1 -1
  178. package/dist/versions.cjs +95 -0
  179. package/dist/versions.cjs.map +1 -0
  180. package/dist/{types/versions.d.ts → versions.d.cts} +5 -5
  181. package/dist/versions.d.cts.map +1 -0
  182. package/dist/versions.d.mts +101 -0
  183. package/dist/versions.d.mts.map +1 -0
  184. package/dist/versions.mjs +85 -26
  185. package/dist/versions.mjs.map +1 -1
  186. package/package.json +28 -17
  187. package/dist/assert.js +0 -16
  188. package/dist/assert.js.map +0 -1
  189. package/dist/base64.js +0 -11
  190. package/dist/base64.js.map +0 -1
  191. package/dist/bytes.js +0 -43
  192. package/dist/bytes.js.map +0 -1
  193. package/dist/caip-types.js +0 -40
  194. package/dist/caip-types.js.map +0 -1
  195. package/dist/checksum.js +0 -12
  196. package/dist/checksum.js.map +0 -1
  197. package/dist/chunk-2LBGT4GH.js +0 -15
  198. package/dist/chunk-2LBGT4GH.js.map +0 -1
  199. package/dist/chunk-3W5G4CYI.js +0 -25
  200. package/dist/chunk-3W5G4CYI.js.map +0 -1
  201. package/dist/chunk-4D6XQBHA.js +0 -69
  202. package/dist/chunk-4D6XQBHA.js.map +0 -1
  203. package/dist/chunk-4NIRTM4M.js +0 -23
  204. package/dist/chunk-4NIRTM4M.js.map +0 -1
  205. package/dist/chunk-4RMX5YWE.js +0 -34
  206. package/dist/chunk-4RMX5YWE.js.map +0 -1
  207. package/dist/chunk-52OU772R.mjs +0 -122
  208. package/dist/chunk-52OU772R.mjs.map +0 -1
  209. package/dist/chunk-5AVWINSB.js +0 -1
  210. package/dist/chunk-5AVWINSB.js.map +0 -1
  211. package/dist/chunk-622IOGVI.mjs +0 -1
  212. package/dist/chunk-622IOGVI.mjs.map +0 -1
  213. package/dist/chunk-6C35XQOF.mjs +0 -257
  214. package/dist/chunk-6C35XQOF.mjs.map +0 -1
  215. package/dist/chunk-6NZW4WK4.js +0 -35
  216. package/dist/chunk-6NZW4WK4.js.map +0 -1
  217. package/dist/chunk-6ZDHSOUV.js +0 -59
  218. package/dist/chunk-6ZDHSOUV.js.map +0 -1
  219. package/dist/chunk-74DGVJVE.mjs +0 -59
  220. package/dist/chunk-74DGVJVE.mjs.map +0 -1
  221. package/dist/chunk-AY6FDCBT.mjs +0 -1
  222. package/dist/chunk-AY6FDCBT.mjs.map +0 -1
  223. package/dist/chunk-B7LIM2PK.mjs +0 -23
  224. package/dist/chunk-B7LIM2PK.mjs.map +0 -1
  225. package/dist/chunk-BFQDMI3M.js +0 -122
  226. package/dist/chunk-BFQDMI3M.js.map +0 -1
  227. package/dist/chunk-DHVKFDHQ.js +0 -95
  228. package/dist/chunk-DHVKFDHQ.js.map +0 -1
  229. package/dist/chunk-E4C7EW4R.js +0 -16
  230. package/dist/chunk-E4C7EW4R.js.map +0 -1
  231. package/dist/chunk-EQMZL4XU.js +0 -1
  232. package/dist/chunk-EQMZL4XU.js.map +0 -1
  233. package/dist/chunk-GZS3IQBZ.mjs +0 -16
  234. package/dist/chunk-GZS3IQBZ.mjs.map +0 -1
  235. package/dist/chunk-H4YFDLB7.mjs +0 -70
  236. package/dist/chunk-H4YFDLB7.mjs.map +0 -1
  237. package/dist/chunk-I575FZFH.mjs +0 -1
  238. package/dist/chunk-I575FZFH.mjs.map +0 -1
  239. package/dist/chunk-IZC266HS.js +0 -55
  240. package/dist/chunk-IZC266HS.js.map +0 -1
  241. package/dist/chunk-JPAL7Q5S.mjs +0 -90
  242. package/dist/chunk-JPAL7Q5S.mjs.map +0 -1
  243. package/dist/chunk-LC2CRSWD.js +0 -1
  244. package/dist/chunk-LC2CRSWD.js.map +0 -1
  245. package/dist/chunk-NQMRFZHB.mjs +0 -35
  246. package/dist/chunk-NQMRFZHB.mjs.map +0 -1
  247. package/dist/chunk-O3EIM33O.mjs +0 -95
  248. package/dist/chunk-O3EIM33O.mjs.map +0 -1
  249. package/dist/chunk-OLLG4H35.js +0 -257
  250. package/dist/chunk-OLLG4H35.js.map +0 -1
  251. package/dist/chunk-QEPVHEP7.js +0 -273
  252. package/dist/chunk-QVEKZRZ2.js +0 -70
  253. package/dist/chunk-QVEKZRZ2.js.map +0 -1
  254. package/dist/chunk-RIRDOQPX.mjs +0 -15
  255. package/dist/chunk-RIRDOQPX.mjs.map +0 -1
  256. package/dist/chunk-RKRGAFXY.js +0 -1
  257. package/dist/chunk-RKRGAFXY.js.map +0 -1
  258. package/dist/chunk-ROQSKDP5.mjs +0 -69
  259. package/dist/chunk-ROQSKDP5.mjs.map +0 -1
  260. package/dist/chunk-RRYOWRCV.mjs +0 -1
  261. package/dist/chunk-RRYOWRCV.mjs.map +0 -1
  262. package/dist/chunk-S3UHBN2Z.mjs +0 -44
  263. package/dist/chunk-S3UHBN2Z.mjs.map +0 -1
  264. package/dist/chunk-TGOMTREC.mjs +0 -75
  265. package/dist/chunk-TGOMTREC.mjs.map +0 -1
  266. package/dist/chunk-THNLGEXV.mjs +0 -34
  267. package/dist/chunk-THNLGEXV.mjs.map +0 -1
  268. package/dist/chunk-U7ZUGCE7.js +0 -75
  269. package/dist/chunk-U7ZUGCE7.js.map +0 -1
  270. package/dist/chunk-UOTVU7OQ.js +0 -1
  271. package/dist/chunk-UOTVU7OQ.js.map +0 -1
  272. package/dist/chunk-VA2DRBDE.mjs +0 -273
  273. package/dist/chunk-VA2DRBDE.mjs.map +0 -1
  274. package/dist/chunk-VFXTVNXN.js +0 -44
  275. package/dist/chunk-VFXTVNXN.js.map +0 -1
  276. package/dist/chunk-WA4DHIND.mjs +0 -1
  277. package/dist/chunk-WA4DHIND.mjs.map +0 -1
  278. package/dist/chunk-X66SUIEF.mjs +0 -25
  279. package/dist/chunk-X66SUIEF.mjs.map +0 -1
  280. package/dist/chunk-XYGUOY6N.mjs +0 -55
  281. package/dist/chunk-XYGUOY6N.mjs.map +0 -1
  282. package/dist/chunk-Z2RGWDD7.js +0 -90
  283. package/dist/chunk-Z2RGWDD7.js.map +0 -1
  284. package/dist/coercers.js +0 -18
  285. package/dist/coercers.js.map +0 -1
  286. package/dist/collections.js +0 -10
  287. package/dist/collections.js.map +0 -1
  288. package/dist/encryption-types.js +0 -2
  289. package/dist/encryption-types.js.map +0 -1
  290. package/dist/errors.js +0 -17
  291. package/dist/errors.js.map +0 -1
  292. package/dist/fs.js +0 -26
  293. package/dist/fs.js.map +0 -1
  294. package/dist/hex.js +0 -35
  295. package/dist/hex.js.map +0 -1
  296. package/dist/index.js +0 -293
  297. package/dist/index.js.map +0 -1
  298. package/dist/json.js +0 -75
  299. package/dist/json.js.map +0 -1
  300. package/dist/keyring.js +0 -2
  301. package/dist/keyring.js.map +0 -1
  302. package/dist/logging.js +0 -10
  303. package/dist/logging.js.map +0 -1
  304. package/dist/misc.js +0 -28
  305. package/dist/misc.js.map +0 -1
  306. package/dist/node.js +0 -313
  307. package/dist/node.js.map +0 -1
  308. package/dist/number.js +0 -18
  309. package/dist/number.js.map +0 -1
  310. package/dist/opaque.js +0 -2
  311. package/dist/opaque.js.map +0 -1
  312. package/dist/promise.js +0 -8
  313. package/dist/promise.js.map +0 -1
  314. package/dist/time.js +0 -12
  315. package/dist/time.js.map +0 -1
  316. package/dist/transaction-types.js +0 -2
  317. package/dist/transaction-types.js.map +0 -1
  318. package/dist/types/assert.d.ts.map +0 -1
  319. package/dist/types/base64.d.ts.map +0 -1
  320. package/dist/types/bytes.d.ts.map +0 -1
  321. package/dist/types/caip-types.d.ts.map +0 -1
  322. package/dist/types/checksum.d.ts +0 -2
  323. package/dist/types/checksum.d.ts.map +0 -1
  324. package/dist/types/coercers.d.ts.map +0 -1
  325. package/dist/types/collections.d.ts.map +0 -1
  326. package/dist/types/encryption-types.d.ts.map +0 -1
  327. package/dist/types/errors.d.ts.map +0 -1
  328. package/dist/types/fs.d.ts.map +0 -1
  329. package/dist/types/hex.d.ts.map +0 -1
  330. package/dist/types/index.d.ts +0 -21
  331. package/dist/types/index.d.ts.map +0 -1
  332. package/dist/types/json.d.ts.map +0 -1
  333. package/dist/types/keyring.d.ts.map +0 -1
  334. package/dist/types/logging.d.ts.map +0 -1
  335. package/dist/types/misc.d.ts.map +0 -1
  336. package/dist/types/node.d.ts +0 -3
  337. package/dist/types/node.d.ts.map +0 -1
  338. package/dist/types/number.d.ts.map +0 -1
  339. package/dist/types/opaque.d.ts +0 -6
  340. package/dist/types/opaque.d.ts.map +0 -1
  341. package/dist/types/promise.d.ts.map +0 -1
  342. package/dist/types/time.d.ts.map +0 -1
  343. package/dist/types/transaction-types.d.ts.map +0 -1
  344. package/dist/types/versions.d.ts.map +0 -1
  345. package/dist/versions.js +0 -27
  346. package/dist/versions.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -6,6 +6,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [8.5.0]
10
+ ### Changed
11
+ - Bump dependency `semver` from `^5.7.1` to `^7.6.0` ([#181](https://github.com/MetaMask/utils/pull/181)).
12
+
13
+ ### Fixed
14
+ - Replace dependency `superstruct` `^1.0.3` with ESM-compatible `@metamask/superstruct` `^3.0.0` ([#185](https://github.com/MetaMask/utils/pull/185)).
15
+ - This fixes the issue of this package being unusable by any TypeScript project that uses `Node16` or `NodeNext` as its `moduleResolution` option.
16
+ - Produce and export ESM-compatible TypeScript type declaration files in addition to CommonJS-compatible declaration files ([#182](https://github.com/MetaMask/utils/pull/182))
17
+ - Previously, this package shipped with only one variant of type declaration files, and these files were only CommonJS-compatible, and the `exports` field in `package.json` linked to these files. This is an anti-pattern and was rightfully flagged by the ["Are the Types Wrong?"](https://arethetypeswrong.github.io/) tool as ["masquerading as CJS"](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md). All of the ATTW checks now pass.
18
+ - Remove chunk files ([#182](https://github.com/MetaMask/utils/pull/182)).
19
+ - Previously, the build tool we used to generate JavaScript files extracted common code to "chunk" files. While this was intended to make this package more tree-shakeable, it also made debugging more difficult for our development teams. These chunk files are no longer present.
20
+
21
+ ## [8.4.0]
22
+ ### Added
23
+ - Add `toCaipChainId` utility function ([#175](https://github.com/MetaMask/utils/pull/175))
24
+ - Add `KnownCaipNamespace` enum ([#175](https://github.com/MetaMask/utils/pull/175))
25
+
26
+ ### Changed
27
+ - Update docs for `createDeferredPromise` to caution against using `suppressUnhandledRejection` ([#174](https://github.com/MetaMask/utils/pull/174))
28
+
29
+ ### Fixed
30
+ - Fix `createSandbox` to assign a unique name to the sandbox directory, so that it can be used in multiple concurrently running Jest tests ([#171](https://github.com/MetaMask/utils/pull/171))
31
+
9
32
  ## [8.3.0]
10
33
  ### Added
11
34
  - Add `createDeferredPromise` ([#164](https://github.com/MetaMask/utils/pull/164))
@@ -212,7 +235,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
212
235
  ### Added
213
236
  - Initial release
214
237
 
215
- [Unreleased]: https://github.com/MetaMask/utils/compare/v8.3.0...HEAD
238
+ [Unreleased]: https://github.com/MetaMask/utils/compare/v8.5.0...HEAD
239
+ [8.5.0]: https://github.com/MetaMask/utils/compare/v8.4.0...v8.5.0
240
+ [8.4.0]: https://github.com/MetaMask/utils/compare/v8.3.0...v8.4.0
216
241
  [8.3.0]: https://github.com/MetaMask/utils/compare/v8.2.1...v8.3.0
217
242
  [8.2.1]: https://github.com/MetaMask/utils/compare/v8.2.0...v8.2.1
218
243
  [8.2.0]: https://github.com/MetaMask/utils/compare/v8.1.0...v8.2.0
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertExhaustive = exports.assertStruct = exports.assert = exports.AssertionError = void 0;
4
+ const superstruct_1 = require("@metamask/superstruct");
5
+ const errors_1 = require("./errors.cjs");
6
+ /**
7
+ * Check if a value is a constructor, i.e., a function that can be called with
8
+ * the `new` keyword.
9
+ *
10
+ * @param fn - The value to check.
11
+ * @returns `true` if the value is a constructor, or `false` otherwise.
12
+ */
13
+ function isConstructable(fn) {
14
+ /* istanbul ignore next */
15
+ return Boolean(typeof fn?.prototype?.constructor?.name === 'string');
16
+ }
17
+ /**
18
+ * Attempts to obtain the message from a possible error object. If it is
19
+ * possible to do so, any trailing period will be removed from the message;
20
+ * otherwise an empty string is returned.
21
+ *
22
+ * @param error - The error object to get the message from.
23
+ * @returns The message without any trailing period if `error` is an object
24
+ * with a `message` property; the string version of `error` without any trailing
25
+ * period if it is not `undefined` or `null`; otherwise an empty string.
26
+ */
27
+ function getErrorMessageWithoutTrailingPeriod(error) {
28
+ // We'll add our own period.
29
+ return (0, errors_1.getErrorMessage)(error).replace(/\.$/u, '');
30
+ }
31
+ /**
32
+ * Initialise an {@link AssertionErrorConstructor} error.
33
+ *
34
+ * @param ErrorWrapper - The error class to use.
35
+ * @param message - The error message.
36
+ * @returns The error object.
37
+ */
38
+ // eslint-disable-next-line @typescript-eslint/naming-convention
39
+ function getError(ErrorWrapper, message) {
40
+ if (isConstructable(ErrorWrapper)) {
41
+ return new ErrorWrapper({
42
+ message,
43
+ });
44
+ }
45
+ return ErrorWrapper({
46
+ message,
47
+ });
48
+ }
49
+ /**
50
+ * The default error class that is thrown if an assertion fails.
51
+ */
52
+ class AssertionError extends Error {
53
+ constructor(options) {
54
+ super(options.message);
55
+ this.code = 'ERR_ASSERTION';
56
+ }
57
+ }
58
+ exports.AssertionError = AssertionError;
59
+ /**
60
+ * Same as Node.js assert.
61
+ * If the value is falsy, throws an error, does nothing otherwise.
62
+ *
63
+ * @throws {@link AssertionError} If value is falsy.
64
+ * @param value - The test that should be truthy to pass.
65
+ * @param message - Message to be passed to {@link AssertionError} or an
66
+ * {@link Error} instance to throw.
67
+ * @param ErrorWrapper - The error class to throw if the assertion fails.
68
+ * Defaults to {@link AssertionError}. If a custom error class is provided for
69
+ * the `message` argument, this argument is ignored.
70
+ */
71
+ function assert(value, message = 'Assertion failed.',
72
+ // eslint-disable-next-line @typescript-eslint/naming-convention
73
+ ErrorWrapper = AssertionError) {
74
+ if (!value) {
75
+ if (message instanceof Error) {
76
+ throw message;
77
+ }
78
+ throw getError(ErrorWrapper, message);
79
+ }
80
+ }
81
+ exports.assert = assert;
82
+ /**
83
+ * Assert a value against a Superstruct struct.
84
+ *
85
+ * @param value - The value to validate.
86
+ * @param struct - The struct to validate against.
87
+ * @param errorPrefix - A prefix to add to the error message. Defaults to
88
+ * "Assertion failed".
89
+ * @param ErrorWrapper - The error class to throw if the assertion fails.
90
+ * Defaults to {@link AssertionError}.
91
+ * @throws If the value is not valid.
92
+ */
93
+ function assertStruct(value, struct, errorPrefix = 'Assertion failed',
94
+ // eslint-disable-next-line @typescript-eslint/naming-convention
95
+ ErrorWrapper = AssertionError) {
96
+ try {
97
+ (0, superstruct_1.assert)(value, struct);
98
+ }
99
+ catch (error) {
100
+ throw getError(ErrorWrapper, `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`);
101
+ }
102
+ }
103
+ exports.assertStruct = assertStruct;
104
+ /**
105
+ * Use in the default case of a switch that you want to be fully exhaustive.
106
+ * Using this function forces the compiler to enforce exhaustivity during
107
+ * compile-time.
108
+ *
109
+ * @example
110
+ * ```
111
+ * const number = 1;
112
+ * switch (number) {
113
+ * case 0:
114
+ * ...
115
+ * case 1:
116
+ * ...
117
+ * default:
118
+ * assertExhaustive(snapPrefix);
119
+ * }
120
+ * ```
121
+ * @param _object - The object on which the switch is being operated.
122
+ */
123
+ function assertExhaustive(_object) {
124
+ throw new Error('Invalid branch reached. Should be detected during compilation.');
125
+ }
126
+ exports.assertExhaustive = assertExhaustive;
127
+ //# sourceMappingURL=assert.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert.cjs","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":";;;AACA,uDAAoE;AAEpE,yCAA2C;AAM3C;;;;;;GAMG;AACH,SAAS,eAAe,CACtB,EAA6B;IAE7B,0BAA0B;IAC1B,OAAO,OAAO,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,WAAW,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC;AACvE,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,oCAAoC,CAAC,KAAc;IAC1D,4BAA4B;IAC5B,OAAO,IAAA,wBAAe,EAAC,KAAK,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;GAMG;AACH,gEAAgE;AAChE,SAAS,QAAQ,CAAC,YAAuC,EAAE,OAAe;IACxE,IAAI,eAAe,CAAC,YAAY,CAAC,EAAE;QACjC,OAAO,IAAI,YAAY,CAAC;YACtB,OAAO;SACR,CAAC,CAAC;KACJ;IACD,OAAO,YAAY,CAAC;QAClB,OAAO;KACR,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAa,cAAe,SAAQ,KAAK;IAGvC,YAAY,OAA4B;QACtC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAHhB,SAAI,GAAG,eAAe,CAAC;IAIhC,CAAC;CACF;AAND,wCAMC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,MAAM,CACpB,KAAU,EACV,UAA0B,mBAAmB;AAC7C,gEAAgE;AAChE,eAA0C,cAAc;IAExD,IAAI,CAAC,KAAK,EAAE;QACV,IAAI,OAAO,YAAY,KAAK,EAAE;YAC5B,MAAM,OAAO,CAAC;SACf;QAED,MAAM,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;KACvC;AACH,CAAC;AAbD,wBAaC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,YAAY,CAC1B,KAAc,EACd,MAA4B,EAC5B,WAAW,GAAG,kBAAkB;AAChC,gEAAgE;AAChE,eAA0C,cAAc;IAExD,IAAI;QACF,IAAA,oBAAiB,EAAC,KAAK,EAAE,MAAM,CAAC,CAAC;KAClC;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,QAAQ,CACZ,YAAY,EACZ,GAAG,WAAW,KAAK,oCAAoC,CAAC,KAAK,CAAC,GAAG,CAClE,CAAC;KACH;AACH,CAAC;AAfD,oCAeC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,gBAAgB,CAAC,OAAc;IAC7C,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;AACJ,CAAC;AAJD,4CAIC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { assert as assertSuperstruct } from '@metamask/superstruct';\n\nimport { getErrorMessage } from './errors';\n\nexport type AssertionErrorConstructor =\n | (new (args: { message: string }) => Error)\n | ((args: { message: string }) => Error);\n\n/**\n * Check if a value is a constructor, i.e., a function that can be called with\n * the `new` keyword.\n *\n * @param fn - The value to check.\n * @returns `true` if the value is a constructor, or `false` otherwise.\n */\nfunction isConstructable(\n fn: AssertionErrorConstructor,\n): fn is new (args: { message: string }) => Error {\n /* istanbul ignore next */\n return Boolean(typeof fn?.prototype?.constructor?.name === 'string');\n}\n\n/**\n * Attempts to obtain the message from a possible error object. If it is\n * possible to do so, any trailing period will be removed from the message;\n * otherwise an empty string is returned.\n *\n * @param error - The error object to get the message from.\n * @returns The message without any trailing period if `error` is an object\n * with a `message` property; the string version of `error` without any trailing\n * period if it is not `undefined` or `null`; otherwise an empty string.\n */\nfunction getErrorMessageWithoutTrailingPeriod(error: unknown): string {\n // We'll add our own period.\n return getErrorMessage(error).replace(/\\.$/u, '');\n}\n\n/**\n * Initialise an {@link AssertionErrorConstructor} error.\n *\n * @param ErrorWrapper - The error class to use.\n * @param message - The error message.\n * @returns The error object.\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nfunction getError(ErrorWrapper: AssertionErrorConstructor, message: string) {\n if (isConstructable(ErrorWrapper)) {\n return new ErrorWrapper({\n message,\n });\n }\n return ErrorWrapper({\n message,\n });\n}\n\n/**\n * The default error class that is thrown if an assertion fails.\n */\nexport class AssertionError extends Error {\n readonly code = 'ERR_ASSERTION';\n\n constructor(options: { message: string }) {\n super(options.message);\n }\n}\n\n/**\n * Same as Node.js assert.\n * If the value is falsy, throws an error, does nothing otherwise.\n *\n * @throws {@link AssertionError} If value is falsy.\n * @param value - The test that should be truthy to pass.\n * @param message - Message to be passed to {@link AssertionError} or an\n * {@link Error} instance to throw.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}. If a custom error class is provided for\n * the `message` argument, this argument is ignored.\n */\nexport function assert(\n value: any,\n message: string | Error = 'Assertion failed.',\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value {\n if (!value) {\n if (message instanceof Error) {\n throw message;\n }\n\n throw getError(ErrorWrapper, message);\n }\n}\n\n/**\n * Assert a value against a Superstruct struct.\n *\n * @param value - The value to validate.\n * @param struct - The struct to validate against.\n * @param errorPrefix - A prefix to add to the error message. Defaults to\n * \"Assertion failed\".\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the value is not valid.\n */\nexport function assertStruct<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n errorPrefix = 'Assertion failed',\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value is Type {\n try {\n assertSuperstruct(value, struct);\n } catch (error) {\n throw getError(\n ErrorWrapper,\n `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`,\n );\n }\n}\n\n/**\n * Use in the default case of a switch that you want to be fully exhaustive.\n * Using this function forces the compiler to enforce exhaustivity during\n * compile-time.\n *\n * @example\n * ```\n * const number = 1;\n * switch (number) {\n * case 0:\n * ...\n * case 1:\n * ...\n * default:\n * assertExhaustive(snapPrefix);\n * }\n * ```\n * @param _object - The object on which the switch is being operated.\n */\nexport function assertExhaustive(_object: never): never {\n throw new Error(\n 'Invalid branch reached. Should be detected during compilation.',\n );\n}\n"]}
@@ -1,5 +1,5 @@
1
- import type { Struct } from 'superstruct';
2
- export declare type AssertionErrorConstructor = (new (args: {
1
+ import type { Struct } from "@metamask/superstruct";
2
+ export type AssertionErrorConstructor = (new (args: {
3
3
  message: string;
4
4
  }) => Error) | ((args: {
5
5
  message: string;
@@ -58,4 +58,4 @@ export declare function assertStruct<Type, Schema>(value: unknown, struct: Struc
58
58
  * @param _object - The object on which the switch is being operated.
59
59
  */
60
60
  export declare function assertExhaustive(_object: never): never;
61
- //# sourceMappingURL=assert.d.ts.map
61
+ //# sourceMappingURL=assert.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert.d.cts","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAKpD,MAAM,MAAM,yBAAyB,GACjC,CAAC,KAAK,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,GAC1C,CAAC,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,CAAC;AAkD3C;;GAEG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC,QAAQ,CAAC,IAAI,mBAAmB;gBAEpB,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE;CAGzC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CACpB,KAAK,EAAE,GAAG,EACV,OAAO,GAAE,MAAM,GAAG,KAA2B,EAE7C,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,CAQf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EACvC,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,EAC5B,WAAW,SAAqB,EAEhC,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,IAAI,IAAI,CASvB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,KAAK,GAAG,KAAK,CAItD"}
@@ -0,0 +1,61 @@
1
+ import type { Struct } from "@metamask/superstruct";
2
+ export type AssertionErrorConstructor = (new (args: {
3
+ message: string;
4
+ }) => Error) | ((args: {
5
+ message: string;
6
+ }) => Error);
7
+ /**
8
+ * The default error class that is thrown if an assertion fails.
9
+ */
10
+ export declare class AssertionError extends Error {
11
+ readonly code = "ERR_ASSERTION";
12
+ constructor(options: {
13
+ message: string;
14
+ });
15
+ }
16
+ /**
17
+ * Same as Node.js assert.
18
+ * If the value is falsy, throws an error, does nothing otherwise.
19
+ *
20
+ * @throws {@link AssertionError} If value is falsy.
21
+ * @param value - The test that should be truthy to pass.
22
+ * @param message - Message to be passed to {@link AssertionError} or an
23
+ * {@link Error} instance to throw.
24
+ * @param ErrorWrapper - The error class to throw if the assertion fails.
25
+ * Defaults to {@link AssertionError}. If a custom error class is provided for
26
+ * the `message` argument, this argument is ignored.
27
+ */
28
+ export declare function assert(value: any, message?: string | Error, ErrorWrapper?: AssertionErrorConstructor): asserts value;
29
+ /**
30
+ * Assert a value against a Superstruct struct.
31
+ *
32
+ * @param value - The value to validate.
33
+ * @param struct - The struct to validate against.
34
+ * @param errorPrefix - A prefix to add to the error message. Defaults to
35
+ * "Assertion failed".
36
+ * @param ErrorWrapper - The error class to throw if the assertion fails.
37
+ * Defaults to {@link AssertionError}.
38
+ * @throws If the value is not valid.
39
+ */
40
+ export declare function assertStruct<Type, Schema>(value: unknown, struct: Struct<Type, Schema>, errorPrefix?: string, ErrorWrapper?: AssertionErrorConstructor): asserts value is Type;
41
+ /**
42
+ * Use in the default case of a switch that you want to be fully exhaustive.
43
+ * Using this function forces the compiler to enforce exhaustivity during
44
+ * compile-time.
45
+ *
46
+ * @example
47
+ * ```
48
+ * const number = 1;
49
+ * switch (number) {
50
+ * case 0:
51
+ * ...
52
+ * case 1:
53
+ * ...
54
+ * default:
55
+ * assertExhaustive(snapPrefix);
56
+ * }
57
+ * ```
58
+ * @param _object - The object on which the switch is being operated.
59
+ */
60
+ export declare function assertExhaustive(_object: never): never;
61
+ //# sourceMappingURL=assert.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert.d.mts","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAKpD,MAAM,MAAM,yBAAyB,GACjC,CAAC,KAAK,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,GAC1C,CAAC,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,KAAK,KAAK,CAAC,CAAC;AAkD3C;;GAEG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC,QAAQ,CAAC,IAAI,mBAAmB;gBAEpB,OAAO,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE;CAGzC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CACpB,KAAK,EAAE,GAAG,EACV,OAAO,GAAE,MAAM,GAAG,KAA2B,EAE7C,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,CAQf;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EACvC,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,EAC5B,WAAW,SAAqB,EAEhC,YAAY,GAAE,yBAA0C,GACvD,OAAO,CAAC,KAAK,IAAI,IAAI,CASvB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,KAAK,GAAG,KAAK,CAItD"}
package/dist/assert.mjs CHANGED
@@ -1,16 +1,120 @@
1
- import {
2
- AssertionError,
3
- assert,
4
- assertExhaustive,
5
- assertStruct
6
- } from "./chunk-74DGVJVE.mjs";
7
- import "./chunk-XYGUOY6N.mjs";
8
- import "./chunk-H4YFDLB7.mjs";
9
- import "./chunk-X66SUIEF.mjs";
10
- export {
11
- AssertionError,
12
- assert,
13
- assertExhaustive,
14
- assertStruct
15
- };
1
+ import { assert as assertSuperstruct } from "@metamask/superstruct";
2
+ import { getErrorMessage } from "./errors.mjs";
3
+ /**
4
+ * Check if a value is a constructor, i.e., a function that can be called with
5
+ * the `new` keyword.
6
+ *
7
+ * @param fn - The value to check.
8
+ * @returns `true` if the value is a constructor, or `false` otherwise.
9
+ */
10
+ function isConstructable(fn) {
11
+ /* istanbul ignore next */
12
+ return Boolean(typeof fn?.prototype?.constructor?.name === 'string');
13
+ }
14
+ /**
15
+ * Attempts to obtain the message from a possible error object. If it is
16
+ * possible to do so, any trailing period will be removed from the message;
17
+ * otherwise an empty string is returned.
18
+ *
19
+ * @param error - The error object to get the message from.
20
+ * @returns The message without any trailing period if `error` is an object
21
+ * with a `message` property; the string version of `error` without any trailing
22
+ * period if it is not `undefined` or `null`; otherwise an empty string.
23
+ */
24
+ function getErrorMessageWithoutTrailingPeriod(error) {
25
+ // We'll add our own period.
26
+ return getErrorMessage(error).replace(/\.$/u, '');
27
+ }
28
+ /**
29
+ * Initialise an {@link AssertionErrorConstructor} error.
30
+ *
31
+ * @param ErrorWrapper - The error class to use.
32
+ * @param message - The error message.
33
+ * @returns The error object.
34
+ */
35
+ // eslint-disable-next-line @typescript-eslint/naming-convention
36
+ function getError(ErrorWrapper, message) {
37
+ if (isConstructable(ErrorWrapper)) {
38
+ return new ErrorWrapper({
39
+ message,
40
+ });
41
+ }
42
+ return ErrorWrapper({
43
+ message,
44
+ });
45
+ }
46
+ /**
47
+ * The default error class that is thrown if an assertion fails.
48
+ */
49
+ export class AssertionError extends Error {
50
+ constructor(options) {
51
+ super(options.message);
52
+ this.code = 'ERR_ASSERTION';
53
+ }
54
+ }
55
+ /**
56
+ * Same as Node.js assert.
57
+ * If the value is falsy, throws an error, does nothing otherwise.
58
+ *
59
+ * @throws {@link AssertionError} If value is falsy.
60
+ * @param value - The test that should be truthy to pass.
61
+ * @param message - Message to be passed to {@link AssertionError} or an
62
+ * {@link Error} instance to throw.
63
+ * @param ErrorWrapper - The error class to throw if the assertion fails.
64
+ * Defaults to {@link AssertionError}. If a custom error class is provided for
65
+ * the `message` argument, this argument is ignored.
66
+ */
67
+ export function assert(value, message = 'Assertion failed.',
68
+ // eslint-disable-next-line @typescript-eslint/naming-convention
69
+ ErrorWrapper = AssertionError) {
70
+ if (!value) {
71
+ if (message instanceof Error) {
72
+ throw message;
73
+ }
74
+ throw getError(ErrorWrapper, message);
75
+ }
76
+ }
77
+ /**
78
+ * Assert a value against a Superstruct struct.
79
+ *
80
+ * @param value - The value to validate.
81
+ * @param struct - The struct to validate against.
82
+ * @param errorPrefix - A prefix to add to the error message. Defaults to
83
+ * "Assertion failed".
84
+ * @param ErrorWrapper - The error class to throw if the assertion fails.
85
+ * Defaults to {@link AssertionError}.
86
+ * @throws If the value is not valid.
87
+ */
88
+ export function assertStruct(value, struct, errorPrefix = 'Assertion failed',
89
+ // eslint-disable-next-line @typescript-eslint/naming-convention
90
+ ErrorWrapper = AssertionError) {
91
+ try {
92
+ assertSuperstruct(value, struct);
93
+ }
94
+ catch (error) {
95
+ throw getError(ErrorWrapper, `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`);
96
+ }
97
+ }
98
+ /**
99
+ * Use in the default case of a switch that you want to be fully exhaustive.
100
+ * Using this function forces the compiler to enforce exhaustivity during
101
+ * compile-time.
102
+ *
103
+ * @example
104
+ * ```
105
+ * const number = 1;
106
+ * switch (number) {
107
+ * case 0:
108
+ * ...
109
+ * case 1:
110
+ * ...
111
+ * default:
112
+ * assertExhaustive(snapPrefix);
113
+ * }
114
+ * ```
115
+ * @param _object - The object on which the switch is being operated.
116
+ */
117
+ export function assertExhaustive(_object) {
118
+ throw new Error('Invalid branch reached. Should be detected during compilation.');
119
+ }
16
120
  //# sourceMappingURL=assert.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"file":"assert.mjs","sourceRoot":"","sources":["../src/assert.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,IAAI,iBAAiB,EAAE,8BAA8B;AAEpE,OAAO,EAAE,eAAe,EAAE,qBAAiB;AAM3C;;;;;;GAMG;AACH,SAAS,eAAe,CACtB,EAA6B;IAE7B,0BAA0B;IAC1B,OAAO,OAAO,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,WAAW,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC;AACvE,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,oCAAoC,CAAC,KAAc;IAC1D,4BAA4B;IAC5B,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;GAMG;AACH,gEAAgE;AAChE,SAAS,QAAQ,CAAC,YAAuC,EAAE,OAAe;IACxE,IAAI,eAAe,CAAC,YAAY,CAAC,EAAE;QACjC,OAAO,IAAI,YAAY,CAAC;YACtB,OAAO;SACR,CAAC,CAAC;KACJ;IACD,OAAO,YAAY,CAAC;QAClB,OAAO;KACR,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,cAAe,SAAQ,KAAK;IAGvC,YAAY,OAA4B;QACtC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAHhB,SAAI,GAAG,eAAe,CAAC;IAIhC,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,MAAM,CACpB,KAAU,EACV,UAA0B,mBAAmB;AAC7C,gEAAgE;AAChE,eAA0C,cAAc;IAExD,IAAI,CAAC,KAAK,EAAE;QACV,IAAI,OAAO,YAAY,KAAK,EAAE;YAC5B,MAAM,OAAO,CAAC;SACf;QAED,MAAM,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;KACvC;AACH,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CAC1B,KAAc,EACd,MAA4B,EAC5B,WAAW,GAAG,kBAAkB;AAChC,gEAAgE;AAChE,eAA0C,cAAc;IAExD,IAAI;QACF,iBAAiB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;KAClC;IAAC,OAAO,KAAK,EAAE;QACd,MAAM,QAAQ,CACZ,YAAY,EACZ,GAAG,WAAW,KAAK,oCAAoC,CAAC,KAAK,CAAC,GAAG,CAClE,CAAC;KACH;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAc;IAC7C,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;AACJ,CAAC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { assert as assertSuperstruct } from '@metamask/superstruct';\n\nimport { getErrorMessage } from './errors';\n\nexport type AssertionErrorConstructor =\n | (new (args: { message: string }) => Error)\n | ((args: { message: string }) => Error);\n\n/**\n * Check if a value is a constructor, i.e., a function that can be called with\n * the `new` keyword.\n *\n * @param fn - The value to check.\n * @returns `true` if the value is a constructor, or `false` otherwise.\n */\nfunction isConstructable(\n fn: AssertionErrorConstructor,\n): fn is new (args: { message: string }) => Error {\n /* istanbul ignore next */\n return Boolean(typeof fn?.prototype?.constructor?.name === 'string');\n}\n\n/**\n * Attempts to obtain the message from a possible error object. If it is\n * possible to do so, any trailing period will be removed from the message;\n * otherwise an empty string is returned.\n *\n * @param error - The error object to get the message from.\n * @returns The message without any trailing period if `error` is an object\n * with a `message` property; the string version of `error` without any trailing\n * period if it is not `undefined` or `null`; otherwise an empty string.\n */\nfunction getErrorMessageWithoutTrailingPeriod(error: unknown): string {\n // We'll add our own period.\n return getErrorMessage(error).replace(/\\.$/u, '');\n}\n\n/**\n * Initialise an {@link AssertionErrorConstructor} error.\n *\n * @param ErrorWrapper - The error class to use.\n * @param message - The error message.\n * @returns The error object.\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nfunction getError(ErrorWrapper: AssertionErrorConstructor, message: string) {\n if (isConstructable(ErrorWrapper)) {\n return new ErrorWrapper({\n message,\n });\n }\n return ErrorWrapper({\n message,\n });\n}\n\n/**\n * The default error class that is thrown if an assertion fails.\n */\nexport class AssertionError extends Error {\n readonly code = 'ERR_ASSERTION';\n\n constructor(options: { message: string }) {\n super(options.message);\n }\n}\n\n/**\n * Same as Node.js assert.\n * If the value is falsy, throws an error, does nothing otherwise.\n *\n * @throws {@link AssertionError} If value is falsy.\n * @param value - The test that should be truthy to pass.\n * @param message - Message to be passed to {@link AssertionError} or an\n * {@link Error} instance to throw.\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}. If a custom error class is provided for\n * the `message` argument, this argument is ignored.\n */\nexport function assert(\n value: any,\n message: string | Error = 'Assertion failed.',\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value {\n if (!value) {\n if (message instanceof Error) {\n throw message;\n }\n\n throw getError(ErrorWrapper, message);\n }\n}\n\n/**\n * Assert a value against a Superstruct struct.\n *\n * @param value - The value to validate.\n * @param struct - The struct to validate against.\n * @param errorPrefix - A prefix to add to the error message. Defaults to\n * \"Assertion failed\".\n * @param ErrorWrapper - The error class to throw if the assertion fails.\n * Defaults to {@link AssertionError}.\n * @throws If the value is not valid.\n */\nexport function assertStruct<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n errorPrefix = 'Assertion failed',\n // eslint-disable-next-line @typescript-eslint/naming-convention\n ErrorWrapper: AssertionErrorConstructor = AssertionError,\n): asserts value is Type {\n try {\n assertSuperstruct(value, struct);\n } catch (error) {\n throw getError(\n ErrorWrapper,\n `${errorPrefix}: ${getErrorMessageWithoutTrailingPeriod(error)}.`,\n );\n }\n}\n\n/**\n * Use in the default case of a switch that you want to be fully exhaustive.\n * Using this function forces the compiler to enforce exhaustivity during\n * compile-time.\n *\n * @example\n * ```\n * const number = 1;\n * switch (number) {\n * case 0:\n * ...\n * case 1:\n * ...\n * default:\n * assertExhaustive(snapPrefix);\n * }\n * ```\n * @param _object - The object on which the switch is being operated.\n */\nexport function assertExhaustive(_object: never): never {\n throw new Error(\n 'Invalid branch reached. Should be detected during compilation.',\n );\n}\n"]}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.base64 = void 0;
4
+ const superstruct_1 = require("@metamask/superstruct");
5
+ const assert_1 = require("./assert.cjs");
6
+ /**
7
+ * Ensure that a provided string-based struct is valid base64.
8
+ *
9
+ * @param struct - The string based struct.
10
+ * @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.
11
+ * @returns A superstruct validating base64.
12
+ */
13
+ const base64 = (struct, options = {}) => {
14
+ const paddingRequired = options.paddingRequired ?? false;
15
+ const characterSet = options.characterSet ?? 'base64';
16
+ let letters;
17
+ if (characterSet === 'base64') {
18
+ letters = String.raw `[A-Za-z0-9+\/]`;
19
+ }
20
+ else {
21
+ (0, assert_1.assert)(characterSet === 'base64url');
22
+ letters = String.raw `[-_A-Za-z0-9]`;
23
+ }
24
+ let re;
25
+ if (paddingRequired) {
26
+ re = new RegExp(`^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`, 'u');
27
+ }
28
+ else {
29
+ re = new RegExp(`^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`, 'u');
30
+ }
31
+ return (0, superstruct_1.pattern)(struct, re);
32
+ };
33
+ exports.base64 = base64;
34
+ //# sourceMappingURL=base64.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.cjs","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":";;;AACA,uDAAgD;AAEhD,yCAAkC;AAmBlC;;;;;;GAMG;AACI,MAAM,MAAM,GAAG,CACpB,MAA4B,EAC5B,UAAyB,EAAE,EAC3B,EAAE;IACF,MAAM,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,KAAK,CAAC;IACzD,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,QAAQ,CAAC;IAEtD,IAAI,OAAe,CAAC;IACpB,IAAI,YAAY,KAAK,QAAQ,EAAE;QAC7B,OAAO,GAAG,MAAM,CAAC,GAAG,CAAA,gBAAgB,CAAC;KACtC;SAAM;QACL,IAAA,eAAM,EAAC,YAAY,KAAK,WAAW,CAAC,CAAC;QACrC,OAAO,GAAG,MAAM,CAAC,GAAG,CAAA,eAAe,CAAC;KACrC;IAED,IAAI,EAAU,CAAC;IACf,IAAI,eAAe,EAAE;QACnB,EAAE,GAAG,IAAI,MAAM,CACb,OAAO,OAAO,WAAW,OAAO,QAAQ,OAAO,UAAU,EACzD,GAAG,CACJ,CAAC;KACH;SAAM;QACL,EAAE,GAAG,IAAI,MAAM,CACb,OAAO,OAAO,WAAW,OAAO,SAAS,OAAO,QAAQ,OAAO,UAAU,EACzE,GAAG,CACJ,CAAC;KACH;IAED,OAAO,IAAA,qBAAO,EAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAC7B,CAAC,CAAC;AA7BW,QAAA,MAAM,UA6BjB","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { pattern } from '@metamask/superstruct';\n\nimport { assert } from './assert';\n\nexport type Base64Options = {\n /**\n * Is the `=` padding at the end required or not.\n *\n * @default false\n */\n // Padding is optional in RFC 4648, that's why the default value is false\n paddingRequired?: boolean;\n /**\n * Which character set should be used.\n * The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.\n *\n * @default 'base64'\n */\n characterSet?: 'base64' | 'base64url';\n};\n\n/**\n * Ensure that a provided string-based struct is valid base64.\n *\n * @param struct - The string based struct.\n * @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.\n * @returns A superstruct validating base64.\n */\nexport const base64 = <Type extends string, Schema>(\n struct: Struct<Type, Schema>,\n options: Base64Options = {},\n) => {\n const paddingRequired = options.paddingRequired ?? false;\n const characterSet = options.characterSet ?? 'base64';\n\n let letters: string;\n if (characterSet === 'base64') {\n letters = String.raw`[A-Za-z0-9+\\/]`;\n } else {\n assert(characterSet === 'base64url');\n letters = String.raw`[-_A-Za-z0-9]`;\n }\n\n let re: RegExp;\n if (paddingRequired) {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n } else {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n }\n\n return pattern(struct, re);\n};\n"]}
@@ -1,5 +1,5 @@
1
- import type { Struct } from 'superstruct';
2
- export declare type Base64Options = {
1
+ import type { Struct } from "@metamask/superstruct";
2
+ export type Base64Options = {
3
3
  /**
4
4
  * Is the `=` padding at the end required or not.
5
5
  *
@@ -22,4 +22,4 @@ export declare type Base64Options = {
22
22
  * @returns A superstruct validating base64.
23
23
  */
24
24
  export declare const base64: <Type extends string, Schema>(struct: Struct<Type, Schema>, options?: Base64Options) => Struct<Type, Schema>;
25
- //# sourceMappingURL=base64.d.ts.map
25
+ //# sourceMappingURL=base64.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.d.cts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAKpD,MAAM,MAAM,aAAa,GAAG;IAC1B;;;;OAIG;IAEH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;;OAKG;IACH,YAAY,CAAC,EAAE,QAAQ,GAAG,WAAW,CAAC;CACvC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,MAAM,wEAER,aAAa,yBA2BvB,CAAC"}
@@ -0,0 +1,25 @@
1
+ import type { Struct } from "@metamask/superstruct";
2
+ export type Base64Options = {
3
+ /**
4
+ * Is the `=` padding at the end required or not.
5
+ *
6
+ * @default false
7
+ */
8
+ paddingRequired?: boolean;
9
+ /**
10
+ * Which character set should be used.
11
+ * The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.
12
+ *
13
+ * @default 'base64'
14
+ */
15
+ characterSet?: 'base64' | 'base64url';
16
+ };
17
+ /**
18
+ * Ensure that a provided string-based struct is valid base64.
19
+ *
20
+ * @param struct - The string based struct.
21
+ * @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.
22
+ * @returns A superstruct validating base64.
23
+ */
24
+ export declare const base64: <Type extends string, Schema>(struct: Struct<Type, Schema>, options?: Base64Options) => Struct<Type, Schema>;
25
+ //# sourceMappingURL=base64.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.d.mts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAKpD,MAAM,MAAM,aAAa,GAAG;IAC1B;;;;OAIG;IAEH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;;OAKG;IACH,YAAY,CAAC,EAAE,QAAQ,GAAG,WAAW,CAAC;CACvC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,MAAM,wEAER,aAAa,yBA2BvB,CAAC"}
package/dist/base64.mjs CHANGED
@@ -1,11 +1,30 @@
1
- import {
2
- base64
3
- } from "./chunk-NQMRFZHB.mjs";
4
- import "./chunk-74DGVJVE.mjs";
5
- import "./chunk-XYGUOY6N.mjs";
6
- import "./chunk-H4YFDLB7.mjs";
7
- import "./chunk-X66SUIEF.mjs";
8
- export {
9
- base64
1
+ import { pattern } from "@metamask/superstruct";
2
+ import { assert } from "./assert.mjs";
3
+ /**
4
+ * Ensure that a provided string-based struct is valid base64.
5
+ *
6
+ * @param struct - The string based struct.
7
+ * @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.
8
+ * @returns A superstruct validating base64.
9
+ */
10
+ export const base64 = (struct, options = {}) => {
11
+ const paddingRequired = options.paddingRequired ?? false;
12
+ const characterSet = options.characterSet ?? 'base64';
13
+ let letters;
14
+ if (characterSet === 'base64') {
15
+ letters = String.raw `[A-Za-z0-9+\/]`;
16
+ }
17
+ else {
18
+ assert(characterSet === 'base64url');
19
+ letters = String.raw `[-_A-Za-z0-9]`;
20
+ }
21
+ let re;
22
+ if (paddingRequired) {
23
+ re = new RegExp(`^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`, 'u');
24
+ }
25
+ else {
26
+ re = new RegExp(`^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`, 'u');
27
+ }
28
+ return pattern(struct, re);
10
29
  };
11
30
  //# sourceMappingURL=base64.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"file":"base64.mjs","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,8BAA8B;AAEhD,OAAO,EAAE,MAAM,EAAE,qBAAiB;AAmBlC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,MAA4B,EAC5B,UAAyB,EAAE,EAC3B,EAAE;IACF,MAAM,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,KAAK,CAAC;IACzD,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,QAAQ,CAAC;IAEtD,IAAI,OAAe,CAAC;IACpB,IAAI,YAAY,KAAK,QAAQ,EAAE;QAC7B,OAAO,GAAG,MAAM,CAAC,GAAG,CAAA,gBAAgB,CAAC;KACtC;SAAM;QACL,MAAM,CAAC,YAAY,KAAK,WAAW,CAAC,CAAC;QACrC,OAAO,GAAG,MAAM,CAAC,GAAG,CAAA,eAAe,CAAC;KACrC;IAED,IAAI,EAAU,CAAC;IACf,IAAI,eAAe,EAAE;QACnB,EAAE,GAAG,IAAI,MAAM,CACb,OAAO,OAAO,WAAW,OAAO,QAAQ,OAAO,UAAU,EACzD,GAAG,CACJ,CAAC;KACH;SAAM;QACL,EAAE,GAAG,IAAI,MAAM,CACb,OAAO,OAAO,WAAW,OAAO,SAAS,OAAO,QAAQ,OAAO,UAAU,EACzE,GAAG,CACJ,CAAC;KACH;IAED,OAAO,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AAC7B,CAAC,CAAC","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport { pattern } from '@metamask/superstruct';\n\nimport { assert } from './assert';\n\nexport type Base64Options = {\n /**\n * Is the `=` padding at the end required or not.\n *\n * @default false\n */\n // Padding is optional in RFC 4648, that's why the default value is false\n paddingRequired?: boolean;\n /**\n * Which character set should be used.\n * The sets are based on {@link https://datatracker.ietf.org/doc/html/rfc4648 RFC 4648}.\n *\n * @default 'base64'\n */\n characterSet?: 'base64' | 'base64url';\n};\n\n/**\n * Ensure that a provided string-based struct is valid base64.\n *\n * @param struct - The string based struct.\n * @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.\n * @returns A superstruct validating base64.\n */\nexport const base64 = <Type extends string, Schema>(\n struct: Struct<Type, Schema>,\n options: Base64Options = {},\n) => {\n const paddingRequired = options.paddingRequired ?? false;\n const characterSet = options.characterSet ?? 'base64';\n\n let letters: string;\n if (characterSet === 'base64') {\n letters = String.raw`[A-Za-z0-9+\\/]`;\n } else {\n assert(characterSet === 'base64url');\n letters = String.raw`[-_A-Za-z0-9]`;\n }\n\n let re: RegExp;\n if (paddingRequired) {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n } else {\n re = new RegExp(\n `^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`,\n 'u',\n );\n }\n\n return pattern(struct, re);\n};\n"]}