@metamask/utils 8.0.0 → 8.2.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 (272) hide show
  1. package/CHANGELOG.md +25 -1
  2. package/dist/assert.js +16 -0
  3. package/dist/assert.js.map +1 -0
  4. package/dist/assert.mjs +16 -0
  5. package/dist/assert.mjs.map +1 -0
  6. package/dist/base64.js +11 -0
  7. package/dist/base64.js.map +1 -0
  8. package/dist/base64.mjs +11 -0
  9. package/dist/base64.mjs.map +1 -0
  10. package/dist/bytes.js +43 -0
  11. package/dist/bytes.js.map +1 -0
  12. package/dist/bytes.mjs +43 -0
  13. package/dist/bytes.mjs.map +1 -0
  14. package/dist/caip-types.js +40 -0
  15. package/dist/caip-types.js.map +1 -0
  16. package/dist/caip-types.mjs +40 -0
  17. package/dist/caip-types.mjs.map +1 -0
  18. package/dist/checksum.js +12 -0
  19. package/dist/checksum.js.map +1 -0
  20. package/dist/checksum.mjs +12 -0
  21. package/dist/checksum.mjs.map +1 -0
  22. package/dist/chunk-2CY4F77D.mjs +95 -0
  23. package/dist/chunk-2CY4F77D.mjs.map +1 -0
  24. package/dist/chunk-2LBGT4GH.js +15 -0
  25. package/dist/chunk-2LBGT4GH.js.map +1 -0
  26. package/dist/chunk-2TBCL6EF.js +1 -0
  27. package/dist/chunk-2TBCL6EF.js.map +1 -0
  28. package/dist/chunk-3W5G4CYI.js +25 -0
  29. package/dist/chunk-3W5G4CYI.js.map +1 -0
  30. package/dist/chunk-4JNG5UTO.js +44 -0
  31. package/dist/chunk-4JNG5UTO.js.map +1 -0
  32. package/dist/chunk-4RMX5YWE.js +34 -0
  33. package/dist/chunk-4RMX5YWE.js.map +1 -0
  34. package/dist/chunk-5WQRFJVQ.js +257 -0
  35. package/dist/chunk-5WQRFJVQ.js.map +1 -0
  36. package/dist/chunk-65KBH4FZ.mjs +257 -0
  37. package/dist/chunk-65KBH4FZ.mjs.map +1 -0
  38. package/dist/chunk-7S5AWFX7.mjs +61 -0
  39. package/dist/chunk-7S5AWFX7.mjs.map +1 -0
  40. package/dist/chunk-AY6FDCBT.mjs +1 -0
  41. package/dist/chunk-AY6FDCBT.mjs.map +1 -0
  42. package/dist/chunk-BKR45GQY.mjs +273 -0
  43. package/dist/chunk-BKR45GQY.mjs.map +1 -0
  44. package/dist/chunk-CSDMTDND.mjs +59 -0
  45. package/dist/chunk-CSDMTDND.mjs.map +1 -0
  46. package/dist/chunk-DMPG3H3G.js +61 -0
  47. package/dist/chunk-DMPG3H3G.js.map +1 -0
  48. package/dist/chunk-DYMCEQIV.mjs +1 -0
  49. package/dist/chunk-DYMCEQIV.mjs.map +1 -0
  50. package/dist/chunk-EQMZL4XU.js +1 -0
  51. package/dist/chunk-EQMZL4XU.js.map +1 -0
  52. package/dist/chunk-G66TREC6.mjs +69 -0
  53. package/dist/chunk-G66TREC6.mjs.map +1 -0
  54. package/dist/chunk-H4YFDLB7.mjs +70 -0
  55. package/dist/chunk-H4YFDLB7.mjs.map +1 -0
  56. package/dist/chunk-HLDWS2JR.mjs +35 -0
  57. package/dist/chunk-HLDWS2JR.mjs.map +1 -0
  58. package/dist/chunk-I575FZFH.mjs +1 -0
  59. package/dist/chunk-I575FZFH.mjs.map +1 -0
  60. package/dist/chunk-JCYFTGED.js +95 -0
  61. package/dist/chunk-JCYFTGED.js.map +1 -0
  62. package/dist/chunk-JPAL7Q5S.mjs +90 -0
  63. package/dist/chunk-JPAL7Q5S.mjs.map +1 -0
  64. package/dist/chunk-LC2CRSWD.js +1 -0
  65. package/dist/chunk-LC2CRSWD.js.map +1 -0
  66. package/dist/chunk-N53OILN6.mjs +16 -0
  67. package/dist/chunk-N53OILN6.mjs.map +1 -0
  68. package/dist/chunk-QVEKZRZ2.js +70 -0
  69. package/dist/chunk-QVEKZRZ2.js.map +1 -0
  70. package/dist/chunk-RIRDOQPX.mjs +15 -0
  71. package/dist/chunk-RIRDOQPX.mjs.map +1 -0
  72. package/dist/chunk-RKRGAFXY.js +1 -0
  73. package/dist/chunk-RKRGAFXY.js.map +1 -0
  74. package/dist/chunk-RRYOWRCV.mjs +1 -0
  75. package/dist/chunk-RRYOWRCV.mjs.map +1 -0
  76. package/dist/chunk-TGOMTREC.mjs +75 -0
  77. package/dist/chunk-TGOMTREC.mjs.map +1 -0
  78. package/dist/chunk-THNLGEXV.mjs +34 -0
  79. package/dist/chunk-THNLGEXV.mjs.map +1 -0
  80. package/dist/chunk-U7ZUGCE7.js +75 -0
  81. package/dist/chunk-U7ZUGCE7.js.map +1 -0
  82. package/dist/chunk-UOTVU7OQ.js +1 -0
  83. package/dist/chunk-UOTVU7OQ.js.map +1 -0
  84. package/dist/chunk-VBYRLAP6.js +59 -0
  85. package/dist/chunk-VBYRLAP6.js.map +1 -0
  86. package/dist/chunk-VL5DYXC5.mjs +122 -0
  87. package/dist/chunk-VL5DYXC5.mjs.map +1 -0
  88. package/dist/chunk-VSEW3V2L.mjs +44 -0
  89. package/dist/chunk-VSEW3V2L.mjs.map +1 -0
  90. package/dist/chunk-WA4DHIND.mjs +1 -0
  91. package/dist/chunk-WA4DHIND.mjs.map +1 -0
  92. package/dist/chunk-WQDTPIQK.js +122 -0
  93. package/dist/chunk-WQDTPIQK.js.map +1 -0
  94. package/dist/chunk-WRBN6DWM.js +35 -0
  95. package/dist/chunk-WRBN6DWM.js.map +1 -0
  96. package/dist/chunk-X66SUIEF.mjs +25 -0
  97. package/dist/chunk-X66SUIEF.mjs.map +1 -0
  98. package/dist/chunk-XYYGZF5Y.js +16 -0
  99. package/dist/chunk-XYYGZF5Y.js.map +1 -0
  100. package/dist/chunk-YQJBIHC7.js +273 -0
  101. package/dist/chunk-YQJBIHC7.js.map +1 -0
  102. package/dist/chunk-Z2RGWDD7.js +90 -0
  103. package/dist/chunk-Z2RGWDD7.js.map +1 -0
  104. package/dist/chunk-ZRNIY7OW.js +69 -0
  105. package/dist/chunk-ZRNIY7OW.js.map +1 -0
  106. package/dist/coercers.js +18 -0
  107. package/dist/coercers.js.map +1 -0
  108. package/dist/coercers.mjs +18 -0
  109. package/dist/coercers.mjs.map +1 -0
  110. package/dist/collections.js +10 -0
  111. package/dist/collections.js.map +1 -0
  112. package/dist/collections.mjs +10 -0
  113. package/dist/collections.mjs.map +1 -0
  114. package/dist/encryption-types.js +2 -0
  115. package/dist/encryption-types.js.map +1 -0
  116. package/dist/encryption-types.mjs +2 -0
  117. package/dist/encryption-types.mjs.map +1 -0
  118. package/dist/errors.js +17 -0
  119. package/dist/errors.js.map +1 -0
  120. package/dist/errors.mjs +17 -0
  121. package/dist/errors.mjs.map +1 -0
  122. package/dist/fs.js +26 -0
  123. package/dist/fs.js.map +1 -0
  124. package/dist/fs.mjs +26 -0
  125. package/dist/fs.mjs.map +1 -0
  126. package/dist/hex.js +35 -0
  127. package/dist/hex.js.map +1 -0
  128. package/dist/hex.mjs +35 -0
  129. package/dist/hex.mjs.map +1 -0
  130. package/dist/index.js +289 -0
  131. package/dist/index.js.map +1 -0
  132. package/dist/index.mjs +289 -0
  133. package/dist/index.mjs.map +1 -0
  134. package/dist/json.js +75 -0
  135. package/dist/json.js.map +1 -0
  136. package/dist/json.mjs +75 -0
  137. package/dist/json.mjs.map +1 -0
  138. package/dist/keyring.js +2 -0
  139. package/dist/keyring.js.map +1 -0
  140. package/dist/keyring.mjs +2 -0
  141. package/dist/keyring.mjs.map +1 -0
  142. package/dist/logging.js +10 -0
  143. package/dist/logging.js.map +1 -0
  144. package/dist/logging.mjs +10 -0
  145. package/dist/logging.mjs.map +1 -0
  146. package/dist/misc.js +28 -0
  147. package/dist/misc.js.map +1 -0
  148. package/dist/misc.mjs +28 -0
  149. package/dist/misc.mjs.map +1 -0
  150. package/dist/node.js +309 -0
  151. package/dist/node.js.map +1 -0
  152. package/dist/node.mjs +309 -0
  153. package/dist/node.mjs.map +1 -0
  154. package/dist/number.js +18 -0
  155. package/dist/number.js.map +1 -0
  156. package/dist/number.mjs +18 -0
  157. package/dist/number.mjs.map +1 -0
  158. package/dist/opaque.js +2 -0
  159. package/dist/opaque.js.map +1 -0
  160. package/dist/opaque.mjs +2 -0
  161. package/dist/opaque.mjs.map +1 -0
  162. package/dist/time.js +12 -0
  163. package/dist/time.js.map +1 -0
  164. package/dist/time.mjs +12 -0
  165. package/dist/time.mjs.map +1 -0
  166. package/dist/transaction-types.js +2 -0
  167. package/dist/transaction-types.js.map +1 -0
  168. package/dist/transaction-types.mjs +2 -0
  169. package/dist/transaction-types.mjs.map +1 -0
  170. package/dist/types/assert.d.ts.map +1 -1
  171. package/dist/types/bytes.d.ts +14 -0
  172. package/dist/types/bytes.d.ts.map +1 -1
  173. package/dist/types/errors.d.ts +58 -0
  174. package/dist/types/errors.d.ts.map +1 -0
  175. package/dist/types/fs.d.ts +133 -0
  176. package/dist/types/fs.d.ts.map +1 -0
  177. package/dist/types/index.d.ts +1 -0
  178. package/dist/types/index.d.ts.map +1 -1
  179. package/dist/types/json.d.ts +83 -47
  180. package/dist/types/json.d.ts.map +1 -1
  181. package/dist/types/node.d.ts +3 -0
  182. package/dist/types/node.d.ts.map +1 -0
  183. package/dist/versions.js +27 -0
  184. package/dist/versions.js.map +1 -0
  185. package/dist/versions.mjs +27 -0
  186. package/dist/versions.mjs.map +1 -0
  187. package/package.json +21 -20
  188. package/dist/cjs/assert.js +0 -116
  189. package/dist/cjs/assert.js.map +0 -1
  190. package/dist/cjs/base64.js +0 -32
  191. package/dist/cjs/base64.js.map +0 -1
  192. package/dist/cjs/bytes.js +0 -255
  193. package/dist/cjs/bytes.js.map +0 -1
  194. package/dist/cjs/caip-types.js +0 -115
  195. package/dist/cjs/caip-types.js.map +0 -1
  196. package/dist/cjs/checksum.js +0 -17
  197. package/dist/cjs/checksum.js.map +0 -1
  198. package/dist/cjs/coercers.js +0 -96
  199. package/dist/cjs/coercers.js.map +0 -1
  200. package/dist/cjs/collections.js +0 -157
  201. package/dist/cjs/collections.js.map +0 -1
  202. package/dist/cjs/encryption-types.js +0 -6
  203. package/dist/cjs/encryption-types.js.map +0 -1
  204. package/dist/cjs/hex.js +0 -107
  205. package/dist/cjs/hex.js.map +0 -1
  206. package/dist/cjs/index.js +0 -37
  207. package/dist/cjs/index.js.map +0 -1
  208. package/dist/cjs/json.js +0 -263
  209. package/dist/cjs/json.js.map +0 -1
  210. package/dist/cjs/keyring.js +0 -6
  211. package/dist/cjs/keyring.js.map +0 -1
  212. package/dist/cjs/logging.js +0 -33
  213. package/dist/cjs/logging.js.map +0 -1
  214. package/dist/cjs/misc.js +0 -111
  215. package/dist/cjs/misc.js.map +0 -1
  216. package/dist/cjs/number.js +0 -52
  217. package/dist/cjs/number.js.map +0 -1
  218. package/dist/cjs/opaque.js +0 -8
  219. package/dist/cjs/opaque.js.map +0 -1
  220. package/dist/cjs/time.js +0 -63
  221. package/dist/cjs/time.js.map +0 -1
  222. package/dist/cjs/transaction-types.js +0 -6
  223. package/dist/cjs/transaction-types.js.map +0 -1
  224. package/dist/cjs/versions.js +0 -79
  225. package/dist/cjs/versions.js.map +0 -1
  226. package/dist/esm/assert.js +0 -133
  227. package/dist/esm/assert.js.map +0 -1
  228. package/dist/esm/base64.js +0 -28
  229. package/dist/esm/base64.js.map +0 -1
  230. package/dist/esm/bytes.js +0 -334
  231. package/dist/esm/bytes.js.map +0 -1
  232. package/dist/esm/caip-types.js +0 -99
  233. package/dist/esm/caip-types.js.map +0 -1
  234. package/dist/esm/checksum.js +0 -7
  235. package/dist/esm/checksum.js.map +0 -1
  236. package/dist/esm/coercers.js +0 -153
  237. package/dist/esm/coercers.js.map +0 -1
  238. package/dist/esm/collections.js +0 -140
  239. package/dist/esm/collections.js.map +0 -1
  240. package/dist/esm/encryption-types.js +0 -3
  241. package/dist/esm/encryption-types.js.map +0 -1
  242. package/dist/esm/hex.js +0 -108
  243. package/dist/esm/hex.js.map +0 -1
  244. package/dist/esm/index.js +0 -20
  245. package/dist/esm/index.js.map +0 -1
  246. package/dist/esm/json.js +0 -301
  247. package/dist/esm/json.js.map +0 -1
  248. package/dist/esm/keyring.js +0 -3
  249. package/dist/esm/keyring.js.map +0 -1
  250. package/dist/esm/logging.js +0 -34
  251. package/dist/esm/logging.js.map +0 -1
  252. package/dist/esm/misc.js +0 -129
  253. package/dist/esm/misc.js.map +0 -1
  254. package/dist/esm/number.js +0 -92
  255. package/dist/esm/number.js.map +0 -1
  256. package/dist/esm/opaque.js +0 -5
  257. package/dist/esm/opaque.js.map +0 -1
  258. package/dist/esm/package.json +0 -1
  259. package/dist/esm/time.js +0 -53
  260. package/dist/esm/time.js.map +0 -1
  261. package/dist/esm/transaction-types.js +0 -3
  262. package/dist/esm/transaction-types.js.map +0 -1
  263. package/dist/esm/versions.js +0 -78
  264. package/dist/esm/versions.js.map +0 -1
  265. package/dist/types/caip-types.test-d.d.ts +0 -2
  266. package/dist/types/caip-types.test-d.d.ts.map +0 -1
  267. package/dist/types/hex.test-d.d.ts +0 -2
  268. package/dist/types/hex.test-d.d.ts.map +0 -1
  269. package/dist/types/json.test-d.d.ts +0 -2
  270. package/dist/types/json.test-d.d.ts.map +0 -1
  271. package/dist/types/misc.test-d.d.ts +0 -2
  272. package/dist/types/misc.test-d.d.ts.map +0 -1
@@ -1,79 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- VersionStruct: function() {
13
- return VersionStruct;
14
- },
15
- VersionRangeStruct: function() {
16
- return VersionRangeStruct;
17
- },
18
- isValidSemVerVersion: function() {
19
- return isValidSemVerVersion;
20
- },
21
- isValidSemVerRange: function() {
22
- return isValidSemVerRange;
23
- },
24
- assertIsSemVerVersion: function() {
25
- return assertIsSemVerVersion;
26
- },
27
- assertIsSemVerRange: function() {
28
- return assertIsSemVerRange;
29
- },
30
- gtVersion: function() {
31
- return gtVersion;
32
- },
33
- gtRange: function() {
34
- return gtRange;
35
- },
36
- satisfiesVersionRange: function() {
37
- return satisfiesVersionRange;
38
- }
39
- });
40
- const _semver = require("semver");
41
- const _superstruct = require("superstruct");
42
- const _assert = require("./assert");
43
- const VersionStruct = (0, _superstruct.refine)((0, _superstruct.string)(), 'Version', (value)=>{
44
- if ((0, _semver.valid)(value) === null) {
45
- return `Expected SemVer version, got "${value}"`;
46
- }
47
- return true;
48
- });
49
- const VersionRangeStruct = (0, _superstruct.refine)((0, _superstruct.string)(), 'Version range', (value)=>{
50
- if ((0, _semver.validRange)(value) === null) {
51
- return `Expected SemVer range, got "${value}"`;
52
- }
53
- return true;
54
- });
55
- function isValidSemVerVersion(version) {
56
- return (0, _superstruct.is)(version, VersionStruct);
57
- }
58
- function isValidSemVerRange(versionRange) {
59
- return (0, _superstruct.is)(versionRange, VersionRangeStruct);
60
- }
61
- function assertIsSemVerVersion(version) {
62
- (0, _assert.assertStruct)(version, VersionStruct);
63
- }
64
- function assertIsSemVerRange(range) {
65
- (0, _assert.assertStruct)(range, VersionRangeStruct);
66
- }
67
- function gtVersion(version1, version2) {
68
- return (0, _semver.gt)(version1, version2);
69
- }
70
- function gtRange(version, range) {
71
- return (0, _semver.gtr)(version, range);
72
- }
73
- function satisfiesVersionRange(version, versionRange) {
74
- return (0, _semver.satisfies)(version, versionRange, {
75
- includePrerelease: true
76
- });
77
- }
78
-
79
- //# sourceMappingURL=versions.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/versions.ts"],"sourcesContent":["import {\n gt as gtSemver,\n gtr as gtrSemver,\n satisfies as satisfiesSemver,\n valid as validSemVerVersion,\n validRange as validSemVerRange,\n} from 'semver';\nimport type { Struct } from 'superstruct';\nimport { is, refine, string } from 'superstruct';\n\nimport { assertStruct } from './assert';\nimport type { Opaque } from './opaque';\n\n/**\n * {@link https://codemix.com/opaque-types-in-javascript/ Opaque} type for SemVer ranges.\n *\n * @example Use {@link assertIsSemVerRange} and {@link isValidSemVerRange} to cast to proper type.\n * ```typescript\n * const unsafeRange: string = dataFromUser();\n * assertIsSemVerRange(unsafeRange);\n * unsafeRange\n * // ^? SemVerRange\n * ```\n * @example If you know what you're doing and want to side-step type safety, casting from a string works correctly.\n * ```typescript\n * const unsafeRange: string = dataFromUser();\n * unsafeRange as SemVerRange;\n * // ^? SemVerRange\n * ```\n * @see {@link assertIsSemVerRange}\n * @see {@link isValidSemVerRange}\n */\nexport type SemVerRange = Opaque<string, typeof semVerRange>;\ndeclare const semVerRange: unique symbol;\n\n/**\n * {@link https://codemix.com/opaque-types-in-javascript/ Opaque} type for singular SemVer version.\n *\n * @example Use {@link assertIsSemVerVersion} and {@link isValidSemVerVersion} to cast to proper type.\n * ```typescript\n * const unsafeVersion: string = dataFromUser();\n * assertIsSemVerVersion(unsafeRange);\n * unsafeVersion\n * // ^? SemVerVersion\n * ```\n * @example If you know what you're doing and want to side-step type safety, casting from a string works correctly.\n * ```typescript\n * const unsafeVersion: string = dataFromUser();\n * unsafeRange as SemVerVersion;\n * // ^? SemVerVersion\n * ```\n * @see {@link assertIsSemVerVersion}\n * @see {@link isValidSemVerVersion}\n */\nexport type SemVerVersion = Opaque<string, typeof semVerVersion>;\ndeclare const semVerVersion: unique symbol;\n\n/**\n * A struct for validating a version string.\n */\nexport const VersionStruct = refine<SemVerVersion, null>(\n string() as unknown as Struct<SemVerVersion, null>,\n 'Version',\n (value) => {\n if (validSemVerVersion(value) === null) {\n return `Expected SemVer version, got \"${value}\"`;\n }\n return true;\n },\n);\n\nexport const VersionRangeStruct = refine<SemVerRange, null>(\n string() as unknown as Struct<SemVerRange, null>,\n 'Version range',\n (value) => {\n if (validSemVerRange(value) === null) {\n return `Expected SemVer range, got \"${value}\"`;\n }\n return true;\n },\n);\n\n/**\n * Checks whether a SemVer version is valid.\n *\n * @param version - A potential version.\n * @returns `true` if the version is valid, and `false` otherwise.\n */\nexport function isValidSemVerVersion(\n version: unknown,\n): version is SemVerVersion {\n return is(version, VersionStruct);\n}\n\n/**\n * Checks whether a SemVer version range is valid.\n *\n * @param versionRange - A potential version range.\n * @returns `true` if the version range is valid, and `false` otherwise.\n */\nexport function isValidSemVerRange(\n versionRange: unknown,\n): versionRange is SemVerRange {\n return is(versionRange, VersionRangeStruct);\n}\n\n/**\n * Asserts that a value is a valid concrete SemVer version.\n *\n * @param version - A potential SemVer concrete version.\n */\nexport function assertIsSemVerVersion(\n version: unknown,\n): asserts version is SemVerVersion {\n assertStruct(version, VersionStruct);\n}\n\n/**\n * Asserts that a value is a valid SemVer range.\n *\n * @param range - A potential SemVer range.\n */\nexport function assertIsSemVerRange(\n range: unknown,\n): asserts range is SemVerRange {\n assertStruct(range, VersionRangeStruct);\n}\n\n/**\n * Checks whether a SemVer version is greater than another.\n *\n * @param version1 - The left-hand version.\n * @param version2 - The right-hand version.\n * @returns `version1 > version2`.\n */\nexport function gtVersion(\n version1: SemVerVersion,\n version2: SemVerVersion,\n): boolean {\n return gtSemver(version1, version2);\n}\n\n/**\n * Checks whether a SemVer version is greater than all possibilities in a range.\n *\n * @param version - A SemvVer version.\n * @param range - The range to check against.\n * @returns `version > range`.\n */\nexport function gtRange(version: SemVerVersion, range: SemVerRange): boolean {\n return gtrSemver(version, range);\n}\n\n/**\n * Returns whether a SemVer version satisfies a SemVer range.\n *\n * @param version - The SemVer version to check.\n * @param versionRange - The SemVer version range to check against.\n * @returns Whether the version satisfied the version range.\n */\nexport function satisfiesVersionRange(\n version: SemVerVersion,\n versionRange: SemVerRange,\n): boolean {\n return satisfiesSemver(version, versionRange, {\n includePrerelease: true,\n });\n}\n"],"names":["VersionStruct","VersionRangeStruct","isValidSemVerVersion","isValidSemVerRange","assertIsSemVerVersion","assertIsSemVerRange","gtVersion","gtRange","satisfiesVersionRange","refine","string","value","validSemVerVersion","validSemVerRange","version","is","versionRange","assertStruct","range","version1","version2","gtSemver","gtrSemver","satisfiesSemver","includePrerelease"],"mappings":";;;;;;;;;;;IA4DaA,aAAa;eAAbA;;IAWAC,kBAAkB;eAAlBA;;IAiBGC,oBAAoB;eAApBA;;IAYAC,kBAAkB;eAAlBA;;IAWAC,qBAAqB;eAArBA;;IAWAC,mBAAmB;eAAnBA;;IAaAC,SAAS;eAATA;;IAcAC,OAAO;eAAPA;;IAWAC,qBAAqB;eAArBA;;;wBA1JT;6BAE4B;wBAEN;AAkDtB,MAAMR,gBAAgBS,IAAAA,mBAAM,EACjCC,IAAAA,mBAAM,KACN,WACA,CAACC;IACC,IAAIC,IAAAA,aAAkB,EAACD,WAAW,MAAM;QACtC,OAAO,CAAC,8BAA8B,EAAEA,MAAM,CAAC,CAAC;IAClD;IACA,OAAO;AACT;AAGK,MAAMV,qBAAqBQ,IAAAA,mBAAM,EACtCC,IAAAA,mBAAM,KACN,iBACA,CAACC;IACC,IAAIE,IAAAA,kBAAgB,EAACF,WAAW,MAAM;QACpC,OAAO,CAAC,4BAA4B,EAAEA,MAAM,CAAC,CAAC;IAChD;IACA,OAAO;AACT;AASK,SAAST,qBACdY,OAAgB;IAEhB,OAAOC,IAAAA,eAAE,EAACD,SAASd;AACrB;AAQO,SAASG,mBACda,YAAqB;IAErB,OAAOD,IAAAA,eAAE,EAACC,cAAcf;AAC1B;AAOO,SAASG,sBACdU,OAAgB;IAEhBG,IAAAA,oBAAY,EAACH,SAASd;AACxB;AAOO,SAASK,oBACda,KAAc;IAEdD,IAAAA,oBAAY,EAACC,OAAOjB;AACtB;AASO,SAASK,UACda,QAAuB,EACvBC,QAAuB;IAEvB,OAAOC,IAAAA,UAAQ,EAACF,UAAUC;AAC5B;AASO,SAASb,QAAQO,OAAsB,EAAEI,KAAkB;IAChE,OAAOI,IAAAA,WAAS,EAACR,SAASI;AAC5B;AASO,SAASV,sBACdM,OAAsB,EACtBE,YAAyB;IAEzB,OAAOO,IAAAA,iBAAe,EAACT,SAASE,cAAc;QAC5CQ,mBAAmB;IACrB;AACF"}
@@ -1,133 +0,0 @@
1
- function _define_property(obj, key, value) {
2
- if (key in obj) {
3
- Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- } else {
10
- obj[key] = value;
11
- }
12
- return obj;
13
- }
14
- import { assert as assertSuperstruct } from 'superstruct';
15
- /**
16
- * Type guard for determining whether the given value is an error object with a
17
- * `message` property, such as an instance of Error.
18
- *
19
- * @param error - The object to check.
20
- * @returns True or false, depending on the result.
21
- */ function isErrorWithMessage(error) {
22
- return typeof error === 'object' && error !== null && 'message' in error;
23
- }
24
- /**
25
- * Check if a value is a constructor, i.e., a function that can be called with
26
- * the `new` keyword.
27
- *
28
- * @param fn - The value to check.
29
- * @returns `true` if the value is a constructor, or `false` otherwise.
30
- */ function isConstructable(fn) {
31
- /* istanbul ignore next */ return Boolean(typeof fn?.prototype?.constructor?.name === 'string');
32
- }
33
- /**
34
- * Get the error message from an unknown error object. If the error object has
35
- * a `message` property, that property is returned. Otherwise, the stringified
36
- * error object is returned.
37
- *
38
- * @param error - The error object to get the message from.
39
- * @returns The error message.
40
- */ function getErrorMessage(error) {
41
- const message = isErrorWithMessage(error) ? error.message : String(error);
42
- // If the error ends with a period, remove it, as we'll add our own period.
43
- if (message.endsWith('.')) {
44
- return message.slice(0, -1);
45
- }
46
- return message;
47
- }
48
- /**
49
- * Initialise an {@link AssertionErrorConstructor} error.
50
- *
51
- * @param ErrorWrapper - The error class to use.
52
- * @param message - The error message.
53
- * @returns The error object.
54
- */ // eslint-disable-next-line @typescript-eslint/naming-convention
55
- function getError(ErrorWrapper, message) {
56
- if (isConstructable(ErrorWrapper)) {
57
- return new ErrorWrapper({
58
- message
59
- });
60
- }
61
- return ErrorWrapper({
62
- message
63
- });
64
- }
65
- /**
66
- * The default error class that is thrown if an assertion fails.
67
- */ export class AssertionError extends Error {
68
- constructor(options){
69
- super(options.message);
70
- _define_property(this, "code", 'ERR_ASSERTION');
71
- }
72
- }
73
- /**
74
- * Same as Node.js assert.
75
- * If the value is falsy, throws an error, does nothing otherwise.
76
- *
77
- * @throws {@link AssertionError} If value is falsy.
78
- * @param value - The test that should be truthy to pass.
79
- * @param message - Message to be passed to {@link AssertionError} or an
80
- * {@link Error} instance to throw.
81
- * @param ErrorWrapper - The error class to throw if the assertion fails.
82
- * Defaults to {@link AssertionError}. If a custom error class is provided for
83
- * the `message` argument, this argument is ignored.
84
- */ export function assert(value, message = 'Assertion failed.', // eslint-disable-next-line @typescript-eslint/naming-convention
85
- ErrorWrapper = AssertionError) {
86
- if (!value) {
87
- if (message instanceof Error) {
88
- throw message;
89
- }
90
- throw getError(ErrorWrapper, message);
91
- }
92
- }
93
- /**
94
- * Assert a value against a Superstruct struct.
95
- *
96
- * @param value - The value to validate.
97
- * @param struct - The struct to validate against.
98
- * @param errorPrefix - A prefix to add to the error message. Defaults to
99
- * "Assertion failed".
100
- * @param ErrorWrapper - The error class to throw if the assertion fails.
101
- * Defaults to {@link AssertionError}.
102
- * @throws If the value is not valid.
103
- */ export function assertStruct(value, struct, errorPrefix = 'Assertion failed', // eslint-disable-next-line @typescript-eslint/naming-convention
104
- ErrorWrapper = AssertionError) {
105
- try {
106
- assertSuperstruct(value, struct);
107
- } catch (error) {
108
- throw getError(ErrorWrapper, `${errorPrefix}: ${getErrorMessage(error)}.`);
109
- }
110
- }
111
- /**
112
- * Use in the default case of a switch that you want to be fully exhaustive.
113
- * Using this function forces the compiler to enforce exhaustivity during
114
- * compile-time.
115
- *
116
- * @example
117
- * ```
118
- * const number = 1;
119
- * switch (number) {
120
- * case 0:
121
- * ...
122
- * case 1:
123
- * ...
124
- * default:
125
- * assertExhaustive(snapPrefix);
126
- * }
127
- * ```
128
- * @param _object - The object on which the switch is being operated.
129
- */ export function assertExhaustive(_object) {
130
- throw new Error('Invalid branch reached. Should be detected during compilation.');
131
- }
132
-
133
- //# sourceMappingURL=assert.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/assert.ts"],"sourcesContent":["import type { Struct } from 'superstruct';\nimport { assert as assertSuperstruct } from 'superstruct';\n\nexport type AssertionErrorConstructor =\n | (new (args: { message: string }) => Error)\n | ((args: { message: string }) => Error);\n\n/**\n * Type guard for determining whether the given value is an error object with a\n * `message` property, such as an instance of Error.\n *\n * @param error - The object to check.\n * @returns True or false, depending on the result.\n */\nfunction isErrorWithMessage(error: unknown): error is { message: string } {\n return typeof error === 'object' && error !== null && 'message' in error;\n}\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 * Get the error message from an unknown error object. If the error object has\n * a `message` property, that property is returned. Otherwise, the stringified\n * error object is returned.\n *\n * @param error - The error object to get the message from.\n * @returns The error message.\n */\nfunction getErrorMessage(error: unknown): string {\n const message = isErrorWithMessage(error) ? error.message : String(error);\n\n // If the error ends with a period, remove it, as we'll add our own period.\n if (message.endsWith('.')) {\n return message.slice(0, -1);\n }\n\n return message;\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(ErrorWrapper, `${errorPrefix}: ${getErrorMessage(error)}.`);\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"],"names":["assert","assertSuperstruct","isErrorWithMessage","error","isConstructable","fn","Boolean","prototype","constructor","name","getErrorMessage","message","String","endsWith","slice","getError","ErrorWrapper","AssertionError","Error","options","code","value","assertStruct","struct","errorPrefix","assertExhaustive","_object"],"mappings":";;;;;;;;;;;;;AACA,SAASA,UAAUC,iBAAiB,QAAQ,cAAc;AAM1D;;;;;;CAMC,GACD,SAASC,mBAAmBC,KAAc;IACxC,OAAO,OAAOA,UAAU,YAAYA,UAAU,QAAQ,aAAaA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AASsB,iBAAiBC,OAAc;IAC7C,MAAM,IAAIR,MACR;AAEJ"}
@@ -1,28 +0,0 @@
1
- import { pattern } from 'superstruct';
2
- import { assert } from './assert';
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
- */ export const base64 = (struct, options = {})=>{
10
- const paddingRequired = options.paddingRequired ?? false;
11
- const characterSet = options.characterSet ?? 'base64';
12
- let letters;
13
- if (characterSet === 'base64') {
14
- letters = String.raw`[A-Za-z0-9+\/]`;
15
- } else {
16
- assert(characterSet === 'base64url');
17
- letters = String.raw`[-_A-Za-z0-9]`;
18
- }
19
- let re;
20
- if (paddingRequired) {
21
- re = new RegExp(`^(?:${letters}{4})*(?:${letters}{3}=|${letters}{2}==)?$`, 'u');
22
- } else {
23
- re = new RegExp(`^(?:${letters}{4})*(?:${letters}{2,3}|${letters}{3}=|${letters}{2}==)?$`, 'u');
24
- }
25
- return pattern(struct, re);
26
- };
27
-
28
- //# sourceMappingURL=base64.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/base64.ts"],"sourcesContent":["import type { Struct } from 'superstruct';\nimport { pattern } from '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"],"names":["pattern","assert","base64","struct","options","paddingRequired","characterSet","letters","String","raw","re","RegExp"],"mappings":"AACA,SAASA,OAAO,QAAQ,cAAc;AAEtC,SAASC,MAAM,QAAQ,WAAW;AAmBlC;;;;;;CAMC,GACD,OAAO,MAAMC,SAAS,CACpBC,QACAC,UAAyB,CAAC,CAAC;IAE3B,MAAMC,kBAAkBD,QAAQC,eAAe,IAAI;IACnD,MAAMC,eAAeF,QAAQE,YAAY,IAAI;IAE7C,IAAIC;IACJ,IAAID,iBAAiB,UAAU;QAC7BC,UAAUC,OAAOC,GAAG,CAAC,cAAc,CAAC;IACtC,OAAO;QACLR,OAAOK,iBAAiB;QACxBC,UAAUC,OAAOC,GAAG,CAAC,aAAa,CAAC;IACrC;IAEA,IAAIC;IACJ,IAAIL,iBAAiB;QACnBK,KAAK,IAAIC,OACP,CAAC,IAAI,EAAEJ,QAAQ,QAAQ,EAAEA,QAAQ,KAAK,EAAEA,QAAQ,QAAQ,CAAC,EACzD;IAEJ,OAAO;QACLG,KAAK,IAAIC,OACP,CAAC,IAAI,EAAEJ,QAAQ,QAAQ,EAAEA,QAAQ,MAAM,EAAEA,QAAQ,KAAK,EAAEA,QAAQ,QAAQ,CAAC,EACzE;IAEJ;IAEA,OAAOP,QAAQG,QAAQO;AACzB,EAAE"}
package/dist/esm/bytes.js DELETED
@@ -1,334 +0,0 @@
1
- import { assert } from './assert';
2
- import { add0x, assertIsHexString, remove0x } from './hex';
3
- // '0'.charCodeAt(0) === 48
4
- const HEX_MINIMUM_NUMBER_CHARACTER = 48;
5
- // '9'.charCodeAt(0) === 57
6
- const HEX_MAXIMUM_NUMBER_CHARACTER = 58;
7
- const HEX_CHARACTER_OFFSET = 87;
8
- /**
9
- * Memoized function that returns an array to be used as a lookup table for
10
- * converting bytes to hexadecimal values.
11
- *
12
- * The array is created lazily and then cached for future use. The benefit of
13
- * this approach is that the performance of converting bytes to hex is much
14
- * better than if we were to call `toString(16)` on each byte.
15
- *
16
- * The downside is that the array is created once and then never garbage
17
- * collected. This is not a problem in practice because the array is only 256
18
- * elements long.
19
- *
20
- * @returns A function that returns the lookup table.
21
- */ function getPrecomputedHexValuesBuilder() {
22
- // To avoid issues with tree shaking, we need to use a function to return the
23
- // array. This is because the array is only used in the `bytesToHex` function
24
- // and if we were to use a global variable, the array might be removed by the
25
- // tree shaker.
26
- const lookupTable = [];
27
- return ()=>{
28
- if (lookupTable.length === 0) {
29
- for(let i = 0; i < 256; i++){
30
- lookupTable.push(i.toString(16).padStart(2, '0'));
31
- }
32
- }
33
- return lookupTable;
34
- };
35
- }
36
- /**
37
- * Function implementation of the {@link getPrecomputedHexValuesBuilder}
38
- * function.
39
- */ const getPrecomputedHexValues = getPrecomputedHexValuesBuilder();
40
- /**
41
- * Check if a value is a `Uint8Array`.
42
- *
43
- * @param value - The value to check.
44
- * @returns Whether the value is a `Uint8Array`.
45
- */ export function isBytes(value) {
46
- return value instanceof Uint8Array;
47
- }
48
- /**
49
- * Assert that a value is a `Uint8Array`.
50
- *
51
- * @param value - The value to check.
52
- * @throws If the value is not a `Uint8Array`.
53
- */ export function assertIsBytes(value) {
54
- assert(isBytes(value), 'Value must be a Uint8Array.');
55
- }
56
- /**
57
- * Convert a `Uint8Array` to a hexadecimal string.
58
- *
59
- * @param bytes - The bytes to convert to a hexadecimal string.
60
- * @returns The hexadecimal string.
61
- */ export function bytesToHex(bytes) {
62
- assertIsBytes(bytes);
63
- if (bytes.length === 0) {
64
- return '0x';
65
- }
66
- const lookupTable = getPrecomputedHexValues();
67
- const hexadecimal = new Array(bytes.length);
68
- for(let i = 0; i < bytes.length; i++){
69
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70
- hexadecimal[i] = lookupTable[bytes[i]];
71
- }
72
- return add0x(hexadecimal.join(''));
73
- }
74
- /**
75
- * Convert a `Uint8Array` to a `bigint`.
76
- *
77
- * To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.
78
- * To convert a two's complement encoded `Uint8Array` to a `bigint`, use
79
- * {@link bytesToSignedBigInt}.
80
- *
81
- * @param bytes - The bytes to convert to a `bigint`.
82
- * @returns The `bigint`.
83
- */ export function bytesToBigInt(bytes) {
84
- assertIsBytes(bytes);
85
- const hexadecimal = bytesToHex(bytes);
86
- return BigInt(hexadecimal);
87
- }
88
- /**
89
- * Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are
90
- * encoded in two's complement.
91
- *
92
- * To convert a `Uint8Array` to an unsigned `bigint` instead, use
93
- * {@link bytesToBigInt}.
94
- *
95
- * @see https://en.wikipedia.org/wiki/Two%27s_complement
96
- * @param bytes - The bytes to convert to a signed `bigint`.
97
- * @returns The signed `bigint`.
98
- */ export function bytesToSignedBigInt(bytes) {
99
- assertIsBytes(bytes);
100
- let value = BigInt(0);
101
- for (const byte of bytes){
102
- // eslint-disable-next-line no-bitwise
103
- value = (value << BigInt(8)) + BigInt(byte);
104
- }
105
- return BigInt.asIntN(bytes.length * 8, value);
106
- }
107
- /**
108
- * Convert a `Uint8Array` to a `number`.
109
- *
110
- * To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.
111
- *
112
- * @param bytes - The bytes to convert to a number.
113
- * @returns The number.
114
- * @throws If the resulting number is not a safe integer.
115
- */ export function bytesToNumber(bytes) {
116
- assertIsBytes(bytes);
117
- const bigint = bytesToBigInt(bytes);
118
- assert(bigint <= BigInt(Number.MAX_SAFE_INTEGER), 'Number is not a safe integer. Use `bytesToBigInt` instead.');
119
- return Number(bigint);
120
- }
121
- /**
122
- * Convert a UTF-8 encoded `Uint8Array` to a `string`.
123
- *
124
- * @param bytes - The bytes to convert to a string.
125
- * @returns The string.
126
- */ export function bytesToString(bytes) {
127
- assertIsBytes(bytes);
128
- return new TextDecoder().decode(bytes);
129
- }
130
- /**
131
- * Convert a hexadecimal string to a `Uint8Array`. The string can optionally be
132
- * prefixed with `0x`. It accepts even and odd length strings.
133
- *
134
- * If the value is "0x", an empty `Uint8Array` is returned.
135
- *
136
- * @param value - The hexadecimal string to convert to bytes.
137
- * @returns The bytes as `Uint8Array`.
138
- */ export function hexToBytes(value) {
139
- // "0x" is often used as empty byte array.
140
- if (value?.toLowerCase?.() === '0x') {
141
- return new Uint8Array();
142
- }
143
- assertIsHexString(value);
144
- // Remove the `0x` prefix if it exists, and pad the string to have an even
145
- // number of characters.
146
- const strippedValue = remove0x(value).toLowerCase();
147
- const normalizedValue = strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;
148
- const bytes = new Uint8Array(normalizedValue.length / 2);
149
- for(let i = 0; i < bytes.length; i++){
150
- // While this is not the prettiest way to convert a hexadecimal string to a
151
- // `Uint8Array`, it is a lot faster than using `parseInt` to convert each
152
- // character.
153
- const c1 = normalizedValue.charCodeAt(i * 2);
154
- const c2 = normalizedValue.charCodeAt(i * 2 + 1);
155
- const n1 = c1 - (c1 < HEX_MAXIMUM_NUMBER_CHARACTER ? HEX_MINIMUM_NUMBER_CHARACTER : HEX_CHARACTER_OFFSET);
156
- const n2 = c2 - (c2 < HEX_MAXIMUM_NUMBER_CHARACTER ? HEX_MINIMUM_NUMBER_CHARACTER : HEX_CHARACTER_OFFSET);
157
- bytes[i] = n1 * 16 + n2;
158
- }
159
- return bytes;
160
- }
161
- /**
162
- * Convert a `bigint` to a `Uint8Array`.
163
- *
164
- * This assumes that the `bigint` is an unsigned integer. To convert a signed
165
- * `bigint` instead, use {@link signedBigIntToBytes}.
166
- *
167
- * @param value - The bigint to convert to bytes.
168
- * @returns The bytes as `Uint8Array`.
169
- */ export function bigIntToBytes(value) {
170
- assert(typeof value === 'bigint', 'Value must be a bigint.');
171
- assert(value >= BigInt(0), 'Value must be a non-negative bigint.');
172
- const hexadecimal = value.toString(16);
173
- return hexToBytes(hexadecimal);
174
- }
175
- /**
176
- * Check if a `bigint` fits in a certain number of bytes.
177
- *
178
- * @param value - The `bigint` to check.
179
- * @param bytes - The number of bytes.
180
- * @returns Whether the `bigint` fits in the number of bytes.
181
- */ function bigIntFits(value, bytes) {
182
- assert(bytes > 0);
183
- /* eslint-disable no-bitwise */ const mask = value >> BigInt(31);
184
- return !((~value & mask) + (value & ~mask) >> BigInt(bytes * 8 + ~0));
185
- /* eslint-enable no-bitwise */ }
186
- /**
187
- * Convert a signed `bigint` to a `Uint8Array`. This uses two's complement
188
- * encoding to represent negative numbers.
189
- *
190
- * To convert an unsigned `bigint` to a `Uint8Array` instead, use
191
- * {@link bigIntToBytes}.
192
- *
193
- * @see https://en.wikipedia.org/wiki/Two%27s_complement
194
- * @param value - The number to convert to bytes.
195
- * @param byteLength - The length of the resulting `Uint8Array`. If the number
196
- * is larger than the maximum value that can be represented by the given length,
197
- * an error is thrown.
198
- * @returns The bytes as `Uint8Array`.
199
- */ export function signedBigIntToBytes(value, byteLength) {
200
- assert(typeof value === 'bigint', 'Value must be a bigint.');
201
- assert(typeof byteLength === 'number', 'Byte length must be a number.');
202
- assert(byteLength > 0, 'Byte length must be greater than 0.');
203
- assert(bigIntFits(value, byteLength), 'Byte length is too small to represent the given value.');
204
- // ESLint doesn't like mutating function parameters, so to avoid having to
205
- // disable the rule, we create a new variable.
206
- let numberValue = value;
207
- const bytes = new Uint8Array(byteLength);
208
- for(let i = 0; i < bytes.length; i++){
209
- bytes[i] = Number(BigInt.asUintN(8, numberValue));
210
- // eslint-disable-next-line no-bitwise
211
- numberValue >>= BigInt(8);
212
- }
213
- return bytes.reverse();
214
- }
215
- /**
216
- * Convert a `number` to a `Uint8Array`.
217
- *
218
- * @param value - The number to convert to bytes.
219
- * @returns The bytes as `Uint8Array`.
220
- * @throws If the number is not a safe integer.
221
- */ export function numberToBytes(value) {
222
- assert(typeof value === 'number', 'Value must be a number.');
223
- assert(value >= 0, 'Value must be a non-negative number.');
224
- assert(Number.isSafeInteger(value), 'Value is not a safe integer. Use `bigIntToBytes` instead.');
225
- const hexadecimal = value.toString(16);
226
- return hexToBytes(hexadecimal);
227
- }
228
- /**
229
- * Convert a `string` to a UTF-8 encoded `Uint8Array`.
230
- *
231
- * @param value - The string to convert to bytes.
232
- * @returns The bytes as `Uint8Array`.
233
- */ export function stringToBytes(value) {
234
- assert(typeof value === 'string', 'Value must be a string.');
235
- return new TextEncoder().encode(value);
236
- }
237
- /**
238
- * Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,
239
- * a `bigint`, a `number`, or a `string`.
240
- *
241
- * This will attempt to guess the type of the value based on its type and
242
- * contents. For more control over the conversion, use the more specific
243
- * conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.
244
- *
245
- * If the value is a `string`, and it is prefixed with `0x`, it will be
246
- * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a
247
- * UTF-8 string. To convert a hexadecimal string to bytes without interpreting
248
- * it as a UTF-8 string, use {@link hexToBytes} instead.
249
- *
250
- * If the value is a `bigint`, it is assumed to be unsigned. To convert a signed
251
- * `bigint` to bytes, use {@link signedBigIntToBytes} instead.
252
- *
253
- * If the value is a `Uint8Array`, it will be returned as-is.
254
- *
255
- * @param value - The value to convert to bytes.
256
- * @returns The bytes as `Uint8Array`.
257
- */ export function valueToBytes(value) {
258
- if (typeof value === 'bigint') {
259
- return bigIntToBytes(value);
260
- }
261
- if (typeof value === 'number') {
262
- return numberToBytes(value);
263
- }
264
- if (typeof value === 'string') {
265
- if (value.startsWith('0x')) {
266
- return hexToBytes(value);
267
- }
268
- return stringToBytes(value);
269
- }
270
- if (isBytes(value)) {
271
- return value;
272
- }
273
- throw new TypeError(`Unsupported value type: "${typeof value}".`);
274
- }
275
- /**
276
- * Concatenate multiple byte-like values into a single `Uint8Array`. The values
277
- * can be `Uint8Array`, `bigint`, `number`, or `string`. This uses
278
- * {@link valueToBytes} under the hood to convert each value to bytes. Refer to
279
- * the documentation of that function for more information.
280
- *
281
- * @param values - The values to concatenate.
282
- * @returns The concatenated bytes as `Uint8Array`.
283
- */ export function concatBytes(values) {
284
- const normalizedValues = new Array(values.length);
285
- let byteLength = 0;
286
- for(let i = 0; i < values.length; i++){
287
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
288
- const value = valueToBytes(values[i]);
289
- normalizedValues[i] = value;
290
- byteLength += value.length;
291
- }
292
- const bytes = new Uint8Array(byteLength);
293
- for(let i = 0, offset = 0; i < normalizedValues.length; i++){
294
- // While we could simply spread the values into an array and use
295
- // `Uint8Array.from`, that is a lot slower than using `Uint8Array.set`.
296
- bytes.set(normalizedValues[i], offset);
297
- offset += normalizedValues[i].length;
298
- }
299
- return bytes;
300
- }
301
- /**
302
- * Create a {@link DataView} from a {@link Uint8Array}. This is a convenience
303
- * function that avoids having to create a {@link DataView} manually, which
304
- * requires passing the `byteOffset` and `byteLength` parameters every time.
305
- *
306
- * Not passing the `byteOffset` and `byteLength` parameters can result in
307
- * unexpected behavior when the {@link Uint8Array} is a view of a larger
308
- * {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.
309
- *
310
- * This function also supports Node.js {@link Buffer}s.
311
- *
312
- * @example
313
- * ```typescript
314
- * const bytes = new Uint8Array([1, 2, 3]);
315
- *
316
- * // This is equivalent to:
317
- * // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
318
- * const dataView = createDataView(bytes);
319
- * ```
320
- * @param bytes - The bytes to create the {@link DataView} from.
321
- * @returns The {@link DataView}.
322
- */ export function createDataView(bytes) {
323
- // To maintain compatibility with Node.js, we need to check if the bytes are
324
- // a Buffer. If so, we need to slice the buffer to get the underlying
325
- // ArrayBuffer.
326
- // eslint-disable-next-line no-restricted-globals
327
- if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {
328
- const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
329
- return new DataView(buffer);
330
- }
331
- return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
332
- }
333
-
334
- //# sourceMappingURL=bytes.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/bytes.ts"],"sourcesContent":["import { assert } from './assert';\nimport type { Hex } from './hex';\nimport { add0x, assertIsHexString, remove0x } from './hex';\n\n// '0'.charCodeAt(0) === 48\nconst HEX_MINIMUM_NUMBER_CHARACTER = 48;\n\n// '9'.charCodeAt(0) === 57\nconst HEX_MAXIMUM_NUMBER_CHARACTER = 58;\nconst HEX_CHARACTER_OFFSET = 87;\n\nexport type Bytes = bigint | number | string | Uint8Array;\n\n/**\n * Memoized function that returns an array to be used as a lookup table for\n * converting bytes to hexadecimal values.\n *\n * The array is created lazily and then cached for future use. The benefit of\n * this approach is that the performance of converting bytes to hex is much\n * better than if we were to call `toString(16)` on each byte.\n *\n * The downside is that the array is created once and then never garbage\n * collected. This is not a problem in practice because the array is only 256\n * elements long.\n *\n * @returns A function that returns the lookup table.\n */\nfunction getPrecomputedHexValuesBuilder(): () => string[] {\n // To avoid issues with tree shaking, we need to use a function to return the\n // array. This is because the array is only used in the `bytesToHex` function\n // and if we were to use a global variable, the array might be removed by the\n // tree shaker.\n const lookupTable: string[] = [];\n\n return () => {\n if (lookupTable.length === 0) {\n for (let i = 0; i < 256; i++) {\n lookupTable.push(i.toString(16).padStart(2, '0'));\n }\n }\n\n return lookupTable;\n };\n}\n\n/**\n * Function implementation of the {@link getPrecomputedHexValuesBuilder}\n * function.\n */\nconst getPrecomputedHexValues = getPrecomputedHexValuesBuilder();\n\n/**\n * Check if a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @returns Whether the value is a `Uint8Array`.\n */\nexport function isBytes(value: unknown): value is Uint8Array {\n return value instanceof Uint8Array;\n}\n\n/**\n * Assert that a value is a `Uint8Array`.\n *\n * @param value - The value to check.\n * @throws If the value is not a `Uint8Array`.\n */\nexport function assertIsBytes(value: unknown): asserts value is Uint8Array {\n assert(isBytes(value), 'Value must be a Uint8Array.');\n}\n\n/**\n * Convert a `Uint8Array` to a hexadecimal string.\n *\n * @param bytes - The bytes to convert to a hexadecimal string.\n * @returns The hexadecimal string.\n */\nexport function bytesToHex(bytes: Uint8Array): Hex {\n assertIsBytes(bytes);\n\n if (bytes.length === 0) {\n return '0x';\n }\n\n const lookupTable = getPrecomputedHexValues();\n const hexadecimal = new Array(bytes.length);\n\n for (let i = 0; i < bytes.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n hexadecimal[i] = lookupTable[bytes[i]!];\n }\n\n return add0x(hexadecimal.join(''));\n}\n\n/**\n * Convert a `Uint8Array` to a `bigint`.\n *\n * To convert a `Uint8Array` to a `number` instead, use {@link bytesToNumber}.\n * To convert a two's complement encoded `Uint8Array` to a `bigint`, use\n * {@link bytesToSignedBigInt}.\n *\n * @param bytes - The bytes to convert to a `bigint`.\n * @returns The `bigint`.\n */\nexport function bytesToBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n const hexadecimal = bytesToHex(bytes);\n return BigInt(hexadecimal);\n}\n\n/**\n * Convert a `Uint8Array` to a signed `bigint`. This assumes that the bytes are\n * encoded in two's complement.\n *\n * To convert a `Uint8Array` to an unsigned `bigint` instead, use\n * {@link bytesToBigInt}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param bytes - The bytes to convert to a signed `bigint`.\n * @returns The signed `bigint`.\n */\nexport function bytesToSignedBigInt(bytes: Uint8Array): bigint {\n assertIsBytes(bytes);\n\n let value = BigInt(0);\n for (const byte of bytes) {\n // eslint-disable-next-line no-bitwise\n value = (value << BigInt(8)) + BigInt(byte);\n }\n\n return BigInt.asIntN(bytes.length * 8, value);\n}\n\n/**\n * Convert a `Uint8Array` to a `number`.\n *\n * To convert a `Uint8Array` to a `bigint` instead, use {@link bytesToBigInt}.\n *\n * @param bytes - The bytes to convert to a number.\n * @returns The number.\n * @throws If the resulting number is not a safe integer.\n */\nexport function bytesToNumber(bytes: Uint8Array): number {\n assertIsBytes(bytes);\n\n const bigint = bytesToBigInt(bytes);\n\n assert(\n bigint <= BigInt(Number.MAX_SAFE_INTEGER),\n 'Number is not a safe integer. Use `bytesToBigInt` instead.',\n );\n\n return Number(bigint);\n}\n\n/**\n * Convert a UTF-8 encoded `Uint8Array` to a `string`.\n *\n * @param bytes - The bytes to convert to a string.\n * @returns The string.\n */\nexport function bytesToString(bytes: Uint8Array): string {\n assertIsBytes(bytes);\n\n return new TextDecoder().decode(bytes);\n}\n\n/**\n * Convert a hexadecimal string to a `Uint8Array`. The string can optionally be\n * prefixed with `0x`. It accepts even and odd length strings.\n *\n * If the value is \"0x\", an empty `Uint8Array` is returned.\n *\n * @param value - The hexadecimal string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function hexToBytes(value: string): Uint8Array {\n // \"0x\" is often used as empty byte array.\n if (value?.toLowerCase?.() === '0x') {\n return new Uint8Array();\n }\n\n assertIsHexString(value);\n\n // Remove the `0x` prefix if it exists, and pad the string to have an even\n // number of characters.\n const strippedValue = remove0x(value).toLowerCase();\n const normalizedValue =\n strippedValue.length % 2 === 0 ? strippedValue : `0${strippedValue}`;\n const bytes = new Uint8Array(normalizedValue.length / 2);\n\n for (let i = 0; i < bytes.length; i++) {\n // While this is not the prettiest way to convert a hexadecimal string to a\n // `Uint8Array`, it is a lot faster than using `parseInt` to convert each\n // character.\n const c1 = normalizedValue.charCodeAt(i * 2);\n const c2 = normalizedValue.charCodeAt(i * 2 + 1);\n const n1 =\n c1 -\n (c1 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n const n2 =\n c2 -\n (c2 < HEX_MAXIMUM_NUMBER_CHARACTER\n ? HEX_MINIMUM_NUMBER_CHARACTER\n : HEX_CHARACTER_OFFSET);\n\n bytes[i] = n1 * 16 + n2;\n }\n\n return bytes;\n}\n\n/**\n * Convert a `bigint` to a `Uint8Array`.\n *\n * This assumes that the `bigint` is an unsigned integer. To convert a signed\n * `bigint` instead, use {@link signedBigIntToBytes}.\n *\n * @param value - The bigint to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function bigIntToBytes(value: bigint): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(value >= BigInt(0), 'Value must be a non-negative bigint.');\n\n const hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\n}\n\n/**\n * Check if a `bigint` fits in a certain number of bytes.\n *\n * @param value - The `bigint` to check.\n * @param bytes - The number of bytes.\n * @returns Whether the `bigint` fits in the number of bytes.\n */\nfunction bigIntFits(value: bigint, bytes: number): boolean {\n assert(bytes > 0);\n\n /* eslint-disable no-bitwise */\n const mask = value >> BigInt(31);\n return !(((~value & mask) + (value & ~mask)) >> BigInt(bytes * 8 + ~0));\n /* eslint-enable no-bitwise */\n}\n\n/**\n * Convert a signed `bigint` to a `Uint8Array`. This uses two's complement\n * encoding to represent negative numbers.\n *\n * To convert an unsigned `bigint` to a `Uint8Array` instead, use\n * {@link bigIntToBytes}.\n *\n * @see https://en.wikipedia.org/wiki/Two%27s_complement\n * @param value - The number to convert to bytes.\n * @param byteLength - The length of the resulting `Uint8Array`. If the number\n * is larger than the maximum value that can be represented by the given length,\n * an error is thrown.\n * @returns The bytes as `Uint8Array`.\n */\nexport function signedBigIntToBytes(\n value: bigint,\n byteLength: number,\n): Uint8Array {\n assert(typeof value === 'bigint', 'Value must be a bigint.');\n assert(typeof byteLength === 'number', 'Byte length must be a number.');\n assert(byteLength > 0, 'Byte length must be greater than 0.');\n assert(\n bigIntFits(value, byteLength),\n 'Byte length is too small to represent the given value.',\n );\n\n // ESLint doesn't like mutating function parameters, so to avoid having to\n // disable the rule, we create a new variable.\n let numberValue = value;\n const bytes = new Uint8Array(byteLength);\n\n for (let i = 0; i < bytes.length; i++) {\n bytes[i] = Number(BigInt.asUintN(8, numberValue));\n // eslint-disable-next-line no-bitwise\n numberValue >>= BigInt(8);\n }\n\n return bytes.reverse();\n}\n\n/**\n * Convert a `number` to a `Uint8Array`.\n *\n * @param value - The number to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n * @throws If the number is not a safe integer.\n */\nexport function numberToBytes(value: number): Uint8Array {\n assert(typeof value === 'number', 'Value must be a number.');\n assert(value >= 0, 'Value must be a non-negative number.');\n assert(\n Number.isSafeInteger(value),\n 'Value is not a safe integer. Use `bigIntToBytes` instead.',\n );\n\n const hexadecimal = value.toString(16);\n return hexToBytes(hexadecimal);\n}\n\n/**\n * Convert a `string` to a UTF-8 encoded `Uint8Array`.\n *\n * @param value - The string to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function stringToBytes(value: string): Uint8Array {\n assert(typeof value === 'string', 'Value must be a string.');\n\n return new TextEncoder().encode(value);\n}\n\n/**\n * Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`,\n * a `bigint`, a `number`, or a `string`.\n *\n * This will attempt to guess the type of the value based on its type and\n * contents. For more control over the conversion, use the more specific\n * conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.\n *\n * If the value is a `string`, and it is prefixed with `0x`, it will be\n * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a\n * UTF-8 string. To convert a hexadecimal string to bytes without interpreting\n * it as a UTF-8 string, use {@link hexToBytes} instead.\n *\n * If the value is a `bigint`, it is assumed to be unsigned. To convert a signed\n * `bigint` to bytes, use {@link signedBigIntToBytes} instead.\n *\n * If the value is a `Uint8Array`, it will be returned as-is.\n *\n * @param value - The value to convert to bytes.\n * @returns The bytes as `Uint8Array`.\n */\nexport function valueToBytes(value: Bytes): Uint8Array {\n if (typeof value === 'bigint') {\n return bigIntToBytes(value);\n }\n\n if (typeof value === 'number') {\n return numberToBytes(value);\n }\n\n if (typeof value === 'string') {\n if (value.startsWith('0x')) {\n return hexToBytes(value);\n }\n\n return stringToBytes(value);\n }\n\n if (isBytes(value)) {\n return value;\n }\n\n throw new TypeError(`Unsupported value type: \"${typeof value}\".`);\n}\n\n/**\n * Concatenate multiple byte-like values into a single `Uint8Array`. The values\n * can be `Uint8Array`, `bigint`, `number`, or `string`. This uses\n * {@link valueToBytes} under the hood to convert each value to bytes. Refer to\n * the documentation of that function for more information.\n *\n * @param values - The values to concatenate.\n * @returns The concatenated bytes as `Uint8Array`.\n */\nexport function concatBytes(values: Bytes[]): Uint8Array {\n const normalizedValues = new Array(values.length);\n let byteLength = 0;\n\n for (let i = 0; i < values.length; i++) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const value = valueToBytes(values[i]!);\n\n normalizedValues[i] = value;\n byteLength += value.length;\n }\n\n const bytes = new Uint8Array(byteLength);\n for (let i = 0, offset = 0; i < normalizedValues.length; i++) {\n // While we could simply spread the values into an array and use\n // `Uint8Array.from`, that is a lot slower than using `Uint8Array.set`.\n bytes.set(normalizedValues[i], offset);\n offset += normalizedValues[i].length;\n }\n\n return bytes;\n}\n\n/**\n * Create a {@link DataView} from a {@link Uint8Array}. This is a convenience\n * function that avoids having to create a {@link DataView} manually, which\n * requires passing the `byteOffset` and `byteLength` parameters every time.\n *\n * Not passing the `byteOffset` and `byteLength` parameters can result in\n * unexpected behavior when the {@link Uint8Array} is a view of a larger\n * {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.\n *\n * This function also supports Node.js {@link Buffer}s.\n *\n * @example\n * ```typescript\n * const bytes = new Uint8Array([1, 2, 3]);\n *\n * // This is equivalent to:\n * // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n * const dataView = createDataView(bytes);\n * ```\n * @param bytes - The bytes to create the {@link DataView} from.\n * @returns The {@link DataView}.\n */\nexport function createDataView(bytes: Uint8Array): DataView {\n // To maintain compatibility with Node.js, we need to check if the bytes are\n // a Buffer. If so, we need to slice the buffer to get the underlying\n // ArrayBuffer.\n // eslint-disable-next-line no-restricted-globals\n if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {\n const buffer = bytes.buffer.slice(\n bytes.byteOffset,\n bytes.byteOffset + bytes.byteLength,\n );\n\n return new DataView(buffer);\n }\n\n return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);\n}\n"],"names":["assert","add0x","assertIsHexString","remove0x","HEX_MINIMUM_NUMBER_CHARACTER","HEX_MAXIMUM_NUMBER_CHARACTER","HEX_CHARACTER_OFFSET","getPrecomputedHexValuesBuilder","lookupTable","length","i","push","toString","padStart","getPrecomputedHexValues","isBytes","value","Uint8Array","assertIsBytes","bytesToHex","bytes","hexadecimal","Array","join","bytesToBigInt","BigInt","bytesToSignedBigInt","byte","asIntN","bytesToNumber","bigint","Number","MAX_SAFE_INTEGER","bytesToString","TextDecoder","decode","hexToBytes","toLowerCase","strippedValue","normalizedValue","c1","charCodeAt","c2","n1","n2","bigIntToBytes","bigIntFits","mask","signedBigIntToBytes","byteLength","numberValue","asUintN","reverse","numberToBytes","isSafeInteger","stringToBytes","TextEncoder","encode","valueToBytes","startsWith","TypeError","concatBytes","values","normalizedValues","offset","set","createDataView","Buffer","buffer","slice","byteOffset","DataView"],"mappings":"AAAA,SAASA,MAAM,QAAQ,WAAW;AAElC,SAASC,KAAK,EAAEC,iBAAiB,EAAEC,QAAQ,QAAQ,QAAQ;AAE3D,2BAA2B;AAC3B,MAAMC,+BAA+B;AAErC,2BAA2B;AAC3B,MAAMC,+BAA+B;AACrC,MAAMC,uBAAuB;AAI7B;;;;;;;;;;;;;CAaC,GACD,SAASC;IACP,6EAA6E;IAC7E,6EAA6E;IAC7E,6EAA6E;IAC7E,eAAe;IACf,MAAMC,cAAwB,EAAE;IAEhC,OAAO;QACL,IAAIA,YAAYC,MAAM,KAAK,GAAG;YAC5B,IAAK,IAAIC,IAAI,GAAG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