@fireproof/vendor 1.1.0 → 1.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 (302) hide show
  1. package/package.json +97 -4
  2. package/src/@ipld/car/LICENSE +4 -0
  3. package/src/@ipld/car/README.md +1132 -0
  4. package/src/@ipld/car/dist/index.min.js +5 -0
  5. package/src/@ipld/car/dist/src/api.d.ts +72 -0
  6. package/src/@ipld/car/dist/src/api.d.ts.map +1 -0
  7. package/src/@ipld/car/dist/src/buffer-decoder.d.ts +67 -0
  8. package/src/@ipld/car/dist/src/buffer-decoder.d.ts.map +1 -0
  9. package/src/@ipld/car/dist/src/buffer-reader-browser.d.ts +116 -0
  10. package/src/@ipld/car/dist/src/buffer-reader-browser.d.ts.map +1 -0
  11. package/src/@ipld/car/dist/src/buffer-reader.d.ts +31 -0
  12. package/src/@ipld/car/dist/src/buffer-reader.d.ts.map +1 -0
  13. package/src/@ipld/car/dist/src/buffer-writer.d.ts +86 -0
  14. package/src/@ipld/car/dist/src/buffer-writer.d.ts.map +1 -0
  15. package/src/@ipld/car/dist/src/coding.d.ts +49 -0
  16. package/src/@ipld/car/dist/src/coding.d.ts.map +1 -0
  17. package/src/@ipld/car/dist/src/decoder-common.d.ts +43 -0
  18. package/src/@ipld/car/dist/src/decoder-common.d.ts.map +1 -0
  19. package/src/@ipld/car/dist/src/decoder.d.ts +87 -0
  20. package/src/@ipld/car/dist/src/decoder.d.ts.map +1 -0
  21. package/src/@ipld/car/dist/src/encoder.d.ts +17 -0
  22. package/src/@ipld/car/dist/src/encoder.d.ts.map +1 -0
  23. package/src/@ipld/car/dist/src/header-validator.d.ts +5 -0
  24. package/src/@ipld/car/dist/src/header-validator.d.ts.map +1 -0
  25. package/src/@ipld/car/dist/src/index-browser.d.ts +10 -0
  26. package/src/@ipld/car/dist/src/index-browser.d.ts.map +1 -0
  27. package/src/@ipld/car/dist/src/index.d.ts +11 -0
  28. package/src/@ipld/car/dist/src/index.d.ts.map +1 -0
  29. package/src/@ipld/car/dist/src/indexed-reader-browser.d.ts +5 -0
  30. package/src/@ipld/car/dist/src/indexed-reader-browser.d.ts.map +1 -0
  31. package/src/@ipld/car/dist/src/indexed-reader.d.ts +152 -0
  32. package/src/@ipld/car/dist/src/indexed-reader.d.ts.map +1 -0
  33. package/src/@ipld/car/dist/src/indexer.d.ts +95 -0
  34. package/src/@ipld/car/dist/src/indexer.d.ts.map +1 -0
  35. package/src/@ipld/car/dist/src/iterator-channel.d.ts +7 -0
  36. package/src/@ipld/car/dist/src/iterator-channel.d.ts.map +1 -0
  37. package/src/@ipld/car/dist/src/iterator.d.ts +174 -0
  38. package/src/@ipld/car/dist/src/iterator.d.ts.map +1 -0
  39. package/src/@ipld/car/dist/src/promise-fs-opts.d.ts +28 -0
  40. package/src/@ipld/car/dist/src/promise-fs-opts.d.ts.map +1 -0
  41. package/src/@ipld/car/dist/src/reader-browser.d.ts +151 -0
  42. package/src/@ipld/car/dist/src/reader-browser.d.ts.map +1 -0
  43. package/src/@ipld/car/dist/src/reader.d.ts +40 -0
  44. package/src/@ipld/car/dist/src/reader.d.ts.map +1 -0
  45. package/src/@ipld/car/dist/src/writer-browser.d.ts +155 -0
  46. package/src/@ipld/car/dist/src/writer-browser.d.ts.map +1 -0
  47. package/src/@ipld/car/dist/src/writer.d.ts +45 -0
  48. package/src/@ipld/car/dist/src/writer.d.ts.map +1 -0
  49. package/src/@ipld/car/src/api.ts +90 -0
  50. package/src/@ipld/car/src/buffer-decoder.js +213 -0
  51. package/src/@ipld/car/src/buffer-reader-browser.js +144 -0
  52. package/src/@ipld/car/src/buffer-reader.js +51 -0
  53. package/src/@ipld/car/src/buffer-writer.js +286 -0
  54. package/src/@ipld/car/src/coding.ts +68 -0
  55. package/src/@ipld/car/src/decoder-common.js +82 -0
  56. package/src/@ipld/car/src/decoder.js +347 -0
  57. package/src/@ipld/car/src/encoder.js +76 -0
  58. package/src/@ipld/car/src/header-validator.js +214 -0
  59. package/src/@ipld/car/src/header.ipldsch +18 -0
  60. package/src/@ipld/car/src/index-browser.js +18 -0
  61. package/src/@ipld/car/src/index.js +21 -0
  62. package/src/@ipld/car/src/indexed-reader-browser.js +7 -0
  63. package/src/@ipld/car/src/indexed-reader.js +211 -0
  64. package/src/@ipld/car/src/indexer.js +130 -0
  65. package/src/@ipld/car/src/iterator-channel.js +91 -0
  66. package/src/@ipld/car/src/iterator.js +267 -0
  67. package/src/@ipld/car/src/promise-fs-opts.js +46 -0
  68. package/src/@ipld/car/src/reader-browser.js +194 -0
  69. package/src/@ipld/car/src/reader.js +54 -0
  70. package/src/@ipld/car/src/writer-browser.js +250 -0
  71. package/src/@ipld/car/src/writer.js +81 -0
  72. package/src/@ipld/dag-cbor/LICENSE +4 -0
  73. package/src/@ipld/dag-cbor/README.md +80 -0
  74. package/src/@ipld/dag-cbor/dist/index.min.js +3 -0
  75. package/src/@ipld/dag-cbor/dist/src/index.d.ts +67 -0
  76. package/src/@ipld/dag-cbor/dist/src/index.d.ts.map +1 -0
  77. package/src/@ipld/dag-cbor/src/index.js +147 -0
  78. package/src/@ipld/dag-json/LICENSE +4 -0
  79. package/src/@ipld/dag-json/README.md +58 -0
  80. package/src/@ipld/dag-json/dist/index.min.js +3 -0
  81. package/src/@ipld/dag-json/dist/src/index.d.ts +12 -0
  82. package/src/@ipld/dag-json/dist/src/index.d.ts.map +1 -0
  83. package/src/@ipld/dag-json/src/index.js +291 -0
  84. package/src/@web3-storage/pail/dist/src/batch/index.d.ts +53 -1
  85. package/src/@web3-storage/pail/dist/src/batch/index.d.ts.map +1 -1
  86. package/src/@web3-storage/pail/dist/src/batch/shard.d.ts +1 -1
  87. package/src/@web3-storage/pail/dist/src/batch/shard.d.ts.map +1 -1
  88. package/src/@web3-storage/pail/dist/src/block.d.ts +2 -2
  89. package/src/@web3-storage/pail/dist/src/block.d.ts.map +1 -1
  90. package/src/@web3-storage/pail/dist/src/clock/index.d.ts +2 -2
  91. package/src/@web3-storage/pail/dist/src/clock/index.d.ts.map +1 -1
  92. package/src/@web3-storage/pail/dist/src/crdt/batch/index.d.ts.map +1 -1
  93. package/src/@web3-storage/pail/dist/src/crdt/index.d.ts +2 -2
  94. package/src/@web3-storage/pail/dist/src/crdt/index.d.ts.map +1 -1
  95. package/src/@web3-storage/pail/dist/src/diff.d.ts +3 -3
  96. package/src/@web3-storage/pail/dist/src/diff.d.ts.map +1 -1
  97. package/src/@web3-storage/pail/dist/src/index.d.ts +1 -1
  98. package/src/@web3-storage/pail/dist/src/index.d.ts.map +1 -1
  99. package/src/@web3-storage/pail/dist/src/merge.d.ts.map +1 -1
  100. package/src/@web3-storage/pail/dist/src/shard.d.ts +4 -4
  101. package/src/@web3-storage/pail/dist/src/shard.d.ts.map +1 -1
  102. package/src/@web3-storage/pail/dist/tsconfig.tsbuildinfo +1 -1
  103. package/src/@web3-storage/pail/src/api.ts +1 -1
  104. package/src/@web3-storage/pail/src/clock/index.js +1 -1
  105. package/src/@web3-storage/pail/src/shard.js +1 -1
  106. package/src/cborg/.github/dependabot.yml +16 -0
  107. package/src/cborg/.github/workflows/test-and-release.yml +52 -0
  108. package/src/cborg/CHANGELOG.md +513 -0
  109. package/src/cborg/LICENSE +13 -0
  110. package/src/cborg/README.md +515 -0
  111. package/src/cborg/bench/bench.js +117 -0
  112. package/src/cborg/bench/json.js +124 -0
  113. package/src/cborg/bench/package.json +8 -0
  114. package/src/cborg/cborg/bin.js +189 -0
  115. package/src/cborg/cborg/common.js +28 -0
  116. package/src/cborg/cborg/decode.js +211 -0
  117. package/src/cborg/cborg/diagnostic.js +158 -0
  118. package/src/cborg/cborg/diagnostic_test.js +117 -0
  119. package/src/cborg/cborg/encode.js +466 -0
  120. package/src/cborg/cborg/index.js +33 -0
  121. package/src/cborg/cborg/is.js +106 -0
  122. package/src/cborg/cborg/length.js +62 -0
  123. package/src/cborg/example-bytestrings.js +180 -0
  124. package/src/cborg/example-json.js +6 -0
  125. package/src/cborg/example.js +5 -0
  126. package/src/cborg/interface.ts +59 -0
  127. package/src/cborg/json/decode.js +462 -0
  128. package/src/cborg/json/encode.js +302 -0
  129. package/src/cborg/json/json.js +4 -0
  130. package/src/cborg/taglib.js +75 -0
  131. package/src/cborg/test/appendix_a.js +647 -0
  132. package/src/cborg/test/common.js +18 -0
  133. package/src/cborg/test/node-test-bin.js +402 -0
  134. package/src/cborg/test/noop-bin-test.js +3 -0
  135. package/src/cborg/test/test-0uint.js +103 -0
  136. package/src/cborg/test/test-1negint.js +96 -0
  137. package/src/cborg/test/test-2bytes.js +198 -0
  138. package/src/cborg/test/test-3string.js +136 -0
  139. package/src/cborg/test/test-4array.js +93 -0
  140. package/src/cborg/test/test-5map.js +284 -0
  141. package/src/cborg/test/test-6tag.js +84 -0
  142. package/src/cborg/test/test-7float.js +131 -0
  143. package/src/cborg/test/test-bl.js +37 -0
  144. package/src/cborg/test/test-cbor-vectors.js +107 -0
  145. package/src/cborg/test/test-decode-errors.js +65 -0
  146. package/src/cborg/test/test-fuzz.js +42 -0
  147. package/src/cborg/test/test-json.js +219 -0
  148. package/src/cborg/test/test-length.js +65 -0
  149. package/src/cborg/test/test-partial.js +111 -0
  150. package/src/cborg/tsconfig.json +48 -0
  151. package/src/cborg/types/cborg/decode.d.ts +43 -0
  152. package/src/cborg/types/cborg/decode.d.ts.map +1 -0
  153. package/src/cborg/types/cborg/encode.d.ts +51 -0
  154. package/src/cborg/types/cborg/encode.d.ts.map +1 -0
  155. package/src/cborg/types/cborg/index.d.ts +26 -0
  156. package/src/cborg/types/cborg/index.d.ts.map +1 -0
  157. package/src/cborg/types/cborg/is.d.ts +6 -0
  158. package/src/cborg/types/cborg/is.d.ts.map +1 -0
  159. package/src/cborg/types/example.d.ts +2 -0
  160. package/src/cborg/types/example.d.ts.map +1 -0
  161. package/src/cborg/types/interface.d.ts +49 -0
  162. package/src/cborg/types/interface.d.ts.map +1 -0
  163. package/src/cborg/types/json/decode.d.ts +67 -0
  164. package/src/cborg/types/json/decode.d.ts.map +1 -0
  165. package/src/cborg/types/json/encode.d.ts +11 -0
  166. package/src/cborg/types/json/encode.d.ts.map +1 -0
  167. package/src/cborg/types/json/json.d.ts +6 -0
  168. package/src/cborg/types/json/json.d.ts.map +1 -0
  169. package/src/cborg/types/taglib.d.ts +18 -0
  170. package/src/cborg/types/taglib.d.ts.map +1 -0
  171. package/src/cborg/types/tsconfig.tsbuildinfo +1 -0
  172. package/src/cborg/types/utils/0uint.d.ts +102 -0
  173. package/src/cborg/types/utils/0uint.d.ts.map +1 -0
  174. package/src/cborg/types/utils/1negint.d.ts +59 -0
  175. package/src/cborg/types/utils/1negint.d.ts.map +1 -0
  176. package/src/cborg/types/utils/2bytes.d.ts +69 -0
  177. package/src/cborg/types/utils/2bytes.d.ts.map +1 -0
  178. package/src/cborg/types/utils/3string.d.ts +46 -0
  179. package/src/cborg/types/utils/3string.d.ts.map +1 -0
  180. package/src/cborg/types/utils/4array.d.ts +66 -0
  181. package/src/cborg/types/utils/4array.d.ts.map +1 -0
  182. package/src/cborg/types/utils/5map.d.ts +66 -0
  183. package/src/cborg/types/utils/5map.d.ts.map +1 -0
  184. package/src/cborg/types/utils/6tag.d.ts +62 -0
  185. package/src/cborg/types/utils/6tag.d.ts.map +1 -0
  186. package/src/cborg/types/utils/7float.d.ts +60 -0
  187. package/src/cborg/types/utils/7float.d.ts.map +1 -0
  188. package/src/cborg/types/utils/bl.d.ts +26 -0
  189. package/src/cborg/types/utils/bl.d.ts.map +1 -0
  190. package/src/cborg/types/utils/byte-utils.d.ts +53 -0
  191. package/src/cborg/types/utils/byte-utils.d.ts.map +1 -0
  192. package/src/cborg/types/utils/common.d.ts +8 -0
  193. package/src/cborg/types/utils/common.d.ts.map +1 -0
  194. package/src/cborg/types/utils/index.d.ts +13 -0
  195. package/src/cborg/types/utils/index.d.ts.map +1 -0
  196. package/src/cborg/types/utils/jump.d.ts +16 -0
  197. package/src/cborg/types/utils/jump.d.ts.map +1 -0
  198. package/src/cborg/types/utils/token.d.ts +59 -0
  199. package/src/cborg/types/utils/token.d.ts.map +1 -0
  200. package/src/cborg/utils/0uint.js +229 -0
  201. package/src/cborg/utils/1negint.js +111 -0
  202. package/src/cborg/utils/2bytes.js +135 -0
  203. package/src/cborg/utils/3string.js +90 -0
  204. package/src/cborg/utils/4array.js +114 -0
  205. package/src/cborg/utils/5map.js +113 -0
  206. package/src/cborg/utils/6tag.js +81 -0
  207. package/src/cborg/utils/7float.js +310 -0
  208. package/src/cborg/utils/bl.js +124 -0
  209. package/src/cborg/utils/byte-utils.js +417 -0
  210. package/src/cborg/utils/common.js +11 -0
  211. package/src/cborg/utils/index.js +12 -0
  212. package/src/cborg/utils/jump.js +222 -0
  213. package/src/cborg/utils/token.js +76 -0
  214. package/src/ipfs-unixfs-exporter/LICENSE +4 -0
  215. package/src/ipfs-unixfs-exporter/README.md +105 -0
  216. package/src/ipfs-unixfs-exporter/dist/index.min.js +3 -0
  217. package/src/ipfs-unixfs-exporter/dist/src/errors.d.ts +57 -0
  218. package/src/ipfs-unixfs-exporter/dist/src/errors.d.ts.map +1 -0
  219. package/src/ipfs-unixfs-exporter/dist/src/errors.js +73 -0
  220. package/src/ipfs-unixfs-exporter/dist/src/errors.js.map +1 -0
  221. package/src/ipfs-unixfs-exporter/dist/src/index.d.ts +355 -0
  222. package/src/ipfs-unixfs-exporter/dist/src/index.d.ts.map +1 -0
  223. package/src/ipfs-unixfs-exporter/dist/src/index.js +197 -0
  224. package/src/ipfs-unixfs-exporter/dist/src/index.js.map +1 -0
  225. package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-cbor.d.ts +4 -0
  226. package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-cbor.d.ts.map +1 -0
  227. package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-cbor.js +9 -0
  228. package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-cbor.js.map +1 -0
  229. package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-json.d.ts +4 -0
  230. package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-json.d.ts.map +1 -0
  231. package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-json.js +9 -0
  232. package/src/ipfs-unixfs-exporter/dist/src/resolvers/dag-json.js.map +1 -0
  233. package/src/ipfs-unixfs-exporter/dist/src/resolvers/identity.d.ts +4 -0
  234. package/src/ipfs-unixfs-exporter/dist/src/resolvers/identity.d.ts.map +1 -0
  235. package/src/ipfs-unixfs-exporter/dist/src/resolvers/identity.js +38 -0
  236. package/src/ipfs-unixfs-exporter/dist/src/resolvers/identity.js.map +1 -0
  237. package/src/ipfs-unixfs-exporter/dist/src/resolvers/index.d.ts +4 -0
  238. package/src/ipfs-unixfs-exporter/dist/src/resolvers/index.d.ts.map +1 -0
  239. package/src/ipfs-unixfs-exporter/dist/src/resolvers/index.js +30 -0
  240. package/src/ipfs-unixfs-exporter/dist/src/resolvers/index.js.map +1 -0
  241. package/src/ipfs-unixfs-exporter/dist/src/resolvers/json.d.ts +4 -0
  242. package/src/ipfs-unixfs-exporter/dist/src/resolvers/json.d.ts.map +1 -0
  243. package/src/ipfs-unixfs-exporter/dist/src/resolvers/json.js +9 -0
  244. package/src/ipfs-unixfs-exporter/dist/src/resolvers/json.js.map +1 -0
  245. package/src/ipfs-unixfs-exporter/dist/src/resolvers/raw.d.ts +4 -0
  246. package/src/ipfs-unixfs-exporter/dist/src/resolvers/raw.d.ts.map +1 -0
  247. package/src/ipfs-unixfs-exporter/dist/src/resolvers/raw.js +37 -0
  248. package/src/ipfs-unixfs-exporter/dist/src/resolvers/raw.js.map +1 -0
  249. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/directory.d.ts +4 -0
  250. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/directory.d.ts.map +1 -0
  251. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/directory.js +29 -0
  252. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/directory.js.map +1 -0
  253. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/file.d.ts +4 -0
  254. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/file.d.ts.map +1 -0
  255. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/file.js +153 -0
  256. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/file.js.map +1 -0
  257. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/hamt-sharded-directory.d.ts +4 -0
  258. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/hamt-sharded-directory.d.ts.map +1 -0
  259. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/hamt-sharded-directory.js +59 -0
  260. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/hamt-sharded-directory.js.map +1 -0
  261. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/raw.d.ts +4 -0
  262. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/raw.d.ts.map +1 -0
  263. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/raw.js +25 -0
  264. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/content/raw.js.map +1 -0
  265. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/index.d.ts +4 -0
  266. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/index.d.ts.map +1 -0
  267. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/index.js +104 -0
  268. package/src/ipfs-unixfs-exporter/dist/src/resolvers/unixfs-v1/index.js.map +1 -0
  269. package/src/ipfs-unixfs-exporter/dist/src/utils/extract-data-from-block.d.ts +3 -0
  270. package/src/ipfs-unixfs-exporter/dist/src/utils/extract-data-from-block.d.ts.map +1 -0
  271. package/src/ipfs-unixfs-exporter/dist/src/utils/extract-data-from-block.js +20 -0
  272. package/src/ipfs-unixfs-exporter/dist/src/utils/extract-data-from-block.js.map +1 -0
  273. package/src/ipfs-unixfs-exporter/dist/src/utils/find-cid-in-shard.d.ts +6 -0
  274. package/src/ipfs-unixfs-exporter/dist/src/utils/find-cid-in-shard.d.ts.map +1 -0
  275. package/src/ipfs-unixfs-exporter/dist/src/utils/find-cid-in-shard.js +116 -0
  276. package/src/ipfs-unixfs-exporter/dist/src/utils/find-cid-in-shard.js.map +1 -0
  277. package/src/ipfs-unixfs-exporter/dist/src/utils/resolve-object-path.d.ts +4 -0
  278. package/src/ipfs-unixfs-exporter/dist/src/utils/resolve-object-path.d.ts.map +1 -0
  279. package/src/ipfs-unixfs-exporter/dist/src/utils/resolve-object-path.js +57 -0
  280. package/src/ipfs-unixfs-exporter/dist/src/utils/resolve-object-path.js.map +1 -0
  281. package/src/ipfs-unixfs-exporter/dist/src/utils/validate-offset-and-length.d.ts +6 -0
  282. package/src/ipfs-unixfs-exporter/dist/src/utils/validate-offset-and-length.d.ts.map +1 -0
  283. package/src/ipfs-unixfs-exporter/dist/src/utils/validate-offset-and-length.js +30 -0
  284. package/src/ipfs-unixfs-exporter/dist/src/utils/validate-offset-and-length.js.map +1 -0
  285. package/src/ipfs-unixfs-exporter/dist/typedoc-urls.json +58 -0
  286. package/src/ipfs-unixfs-exporter/src/errors.ts +87 -0
  287. package/src/ipfs-unixfs-exporter/src/index.ts +498 -0
  288. package/src/ipfs-unixfs-exporter/src/resolvers/dag-cbor.ts +12 -0
  289. package/src/ipfs-unixfs-exporter/src/resolvers/dag-json.ts +12 -0
  290. package/src/ipfs-unixfs-exporter/src/resolvers/identity.ts +49 -0
  291. package/src/ipfs-unixfs-exporter/src/resolvers/index.ts +35 -0
  292. package/src/ipfs-unixfs-exporter/src/resolvers/json.ts +12 -0
  293. package/src/ipfs-unixfs-exporter/src/resolvers/raw.ts +49 -0
  294. package/src/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/directory.ts +39 -0
  295. package/src/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/file.ts +198 -0
  296. package/src/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/hamt-sharded-directory.ts +76 -0
  297. package/src/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/raw.ts +37 -0
  298. package/src/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/index.ts +121 -0
  299. package/src/ipfs-unixfs-exporter/src/utils/extract-data-from-block.ts +24 -0
  300. package/src/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.ts +149 -0
  301. package/src/ipfs-unixfs-exporter/src/utils/resolve-object-path.ts +62 -0
  302. package/src/ipfs-unixfs-exporter/src/utils/validate-offset-and-length.ts +38 -0
@@ -0,0 +1,466 @@
1
+ import { is } from './is.js'
2
+ import {
3
+ Token, Type, Bl,
4
+ quickEncodeToken,
5
+ asU8A,
6
+ encodeUint,
7
+ encodeNegint,
8
+ encodeBytes,
9
+ encodeString,
10
+ encodeArray,
11
+ encodeMap,
12
+ encodeTag,
13
+ encodeFloat
14
+ } from "@fireproof/vendor/cborg/utils"
15
+
16
+ /**
17
+ * @typedef {import('../interface').EncodeOptions} EncodeOptions
18
+ * @typedef {import('../interface').OptionalTypeEncoder} OptionalTypeEncoder
19
+ * @typedef {import('../interface').Reference} Reference
20
+ * @typedef {import('../interface').StrictTypeEncoder} StrictTypeEncoder
21
+ * @typedef {import('../interface').TokenTypeEncoder} TokenTypeEncoder
22
+ * @typedef {import('../interface').TokenOrNestedTokens} TokenOrNestedTokens
23
+ */
24
+
25
+ /** @type {EncodeOptions} */
26
+ const defaultEncodeOptions = {
27
+ float64: false,
28
+ mapSorter,
29
+ quickEncodeToken,
30
+ encodeErrPrefix: 'CBOR encode error:'
31
+
32
+ }
33
+
34
+ /** @returns {TokenTypeEncoder[]} */
35
+ export function makeCborEncoders () {
36
+ const encoders = []
37
+ encoders[Type.uint.major] = encodeUint
38
+ encoders[Type.negint.major] = encodeNegint
39
+ encoders[Type.bytes.major] = encodeBytes
40
+ encoders[Type.string.major] = encodeString
41
+ encoders[Type.array.major] = encodeArray
42
+ encoders[Type.map.major] = encodeMap
43
+ encoders[Type.tag.major] = encodeTag
44
+ encoders[Type.float.major] = encodeFloat
45
+ return encoders
46
+ }
47
+
48
+ const cborEncoders = makeCborEncoders()
49
+
50
+ const buf = new Bl()
51
+
52
+ /** @implements {Reference} */
53
+ class Ref {
54
+ /**
55
+ * @param {object|any[]} obj
56
+ * @param {Reference|undefined} parent
57
+ */
58
+ constructor (obj, parent) {
59
+ this.obj = obj
60
+ this.parent = parent
61
+ }
62
+
63
+ /**
64
+ * @param {object|any[]} obj
65
+ * @returns {boolean}
66
+ */
67
+ includes (obj) {
68
+ /** @type {Reference|undefined} */
69
+ let p = this
70
+ do {
71
+ if (p.obj === obj) {
72
+ return true
73
+ }
74
+ } while (p = p.parent) // eslint-disable-line
75
+ return false
76
+ }
77
+
78
+ /**
79
+ * @param {Reference|undefined} stack
80
+ * @param {object|any[]} obj
81
+ * @param {EncodeOptions} options
82
+ * @returns {Reference}
83
+ */
84
+ static createCheck (stack, obj, { encodeErrPrefix }) {
85
+ if (stack && stack.includes(obj)) {
86
+ throw new Error(`${encodeErrPrefix} object contains circular references`)
87
+ }
88
+ return new Ref(obj, stack)
89
+ }
90
+ }
91
+
92
+ const simpleTokens = {
93
+ null: new Token(Type.null, null),
94
+ undefined: new Token(Type.undefined, undefined),
95
+ true: new Token(Type.true, true),
96
+ false: new Token(Type.false, false),
97
+ emptyArray: new Token(Type.array, 0),
98
+ emptyMap: new Token(Type.map, 0)
99
+ }
100
+
101
+ /** @type {{[typeName: string]: StrictTypeEncoder}} */
102
+ const typeEncoders = {
103
+ /**
104
+ * @param {any} obj
105
+ * @param {string} _typ
106
+ * @param {EncodeOptions} _options
107
+ * @param {Reference} [_refStack]
108
+ * @returns {TokenOrNestedTokens}
109
+ */
110
+ number (obj, _typ, _options, _refStack) {
111
+ if (!Number.isInteger(obj) || !Number.isSafeInteger(obj)) {
112
+ return new Token(Type.float, obj)
113
+ } else if (obj >= 0) {
114
+ return new Token(Type.uint, obj)
115
+ } else {
116
+ return new Token(Type.negint, obj)
117
+ }
118
+ },
119
+
120
+ /**
121
+ * @param {any} obj
122
+ * @param {string} _typ
123
+ * @param {EncodeOptions} _options
124
+ * @param {Reference} [_refStack]
125
+ * @returns {TokenOrNestedTokens}
126
+ */
127
+ bigint (obj, _typ, _options, _refStack) {
128
+ if (obj >= BigInt(0)) {
129
+ return new Token(Type.uint, obj)
130
+ } else {
131
+ return new Token(Type.negint, obj)
132
+ }
133
+ },
134
+
135
+ /**
136
+ * @param {any} obj
137
+ * @param {string} _typ
138
+ * @param {EncodeOptions} _options
139
+ * @param {Reference} [_refStack]
140
+ * @returns {TokenOrNestedTokens}
141
+ */
142
+ Uint8Array (obj, _typ, _options, _refStack) {
143
+ return new Token(Type.bytes, obj)
144
+ },
145
+
146
+ /**
147
+ * @param {any} obj
148
+ * @param {string} _typ
149
+ * @param {EncodeOptions} _options
150
+ * @param {Reference} [_refStack]
151
+ * @returns {TokenOrNestedTokens}
152
+ */
153
+ string (obj, _typ, _options, _refStack) {
154
+ return new Token(Type.string, obj)
155
+ },
156
+
157
+ /**
158
+ * @param {any} obj
159
+ * @param {string} _typ
160
+ * @param {EncodeOptions} _options
161
+ * @param {Reference} [_refStack]
162
+ * @returns {TokenOrNestedTokens}
163
+ */
164
+ boolean (obj, _typ, _options, _refStack) {
165
+ return obj ? simpleTokens.true : simpleTokens.false
166
+ },
167
+
168
+ /**
169
+ * @param {any} _obj
170
+ * @param {string} _typ
171
+ * @param {EncodeOptions} _options
172
+ * @param {Reference} [_refStack]
173
+ * @returns {TokenOrNestedTokens}
174
+ */
175
+ null (_obj, _typ, _options, _refStack) {
176
+ return simpleTokens.null
177
+ },
178
+
179
+ /**
180
+ * @param {any} _obj
181
+ * @param {string} _typ
182
+ * @param {EncodeOptions} _options
183
+ * @param {Reference} [_refStack]
184
+ * @returns {TokenOrNestedTokens}
185
+ */
186
+ undefined (_obj, _typ, _options, _refStack) {
187
+ return simpleTokens.undefined
188
+ },
189
+
190
+ /**
191
+ * @param {any} obj
192
+ * @param {string} _typ
193
+ * @param {EncodeOptions} _options
194
+ * @param {Reference} [_refStack]
195
+ * @returns {TokenOrNestedTokens}
196
+ */
197
+ ArrayBuffer (obj, _typ, _options, _refStack) {
198
+ return new Token(Type.bytes, new Uint8Array(obj))
199
+ },
200
+
201
+ /**
202
+ * @param {any} obj
203
+ * @param {string} _typ
204
+ * @param {EncodeOptions} _options
205
+ * @param {Reference} [_refStack]
206
+ * @returns {TokenOrNestedTokens}
207
+ */
208
+ DataView (obj, _typ, _options, _refStack) {
209
+ return new Token(Type.bytes, new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength))
210
+ },
211
+
212
+ /**
213
+ * @param {any} obj
214
+ * @param {string} _typ
215
+ * @param {EncodeOptions} options
216
+ * @param {Reference} [refStack]
217
+ * @returns {TokenOrNestedTokens}
218
+ */
219
+ Array (obj, _typ, options, refStack) {
220
+ if (!obj.length) {
221
+ if (options.addBreakTokens === true) {
222
+ return [simpleTokens.emptyArray, new Token(Type.break)]
223
+ }
224
+ return simpleTokens.emptyArray
225
+ }
226
+ refStack = Ref.createCheck(refStack, obj, options)
227
+ const entries = []
228
+ let i = 0
229
+ for (const e of obj) {
230
+ entries[i++] = objectToTokens(e, options, refStack)
231
+ }
232
+ if (options.addBreakTokens) {
233
+ return [new Token(Type.array, obj.length), entries, new Token(Type.break)]
234
+ }
235
+ return [new Token(Type.array, obj.length), entries]
236
+ },
237
+
238
+ /**
239
+ * @param {any} obj
240
+ * @param {string} typ
241
+ * @param {EncodeOptions} options
242
+ * @param {Reference} [refStack]
243
+ * @returns {TokenOrNestedTokens}
244
+ */
245
+ Object (obj, typ, options, refStack) {
246
+ // could be an Object or a Map
247
+ const isMap = typ !== 'Object'
248
+ // it's slightly quicker to use Object.keys() than Object.entries()
249
+ const keys = isMap ? obj.keys() : Object.keys(obj)
250
+ const length = isMap ? obj.size : keys.length
251
+ if (!length) {
252
+ if (options.addBreakTokens === true) {
253
+ return [simpleTokens.emptyMap, new Token(Type.break)]
254
+ }
255
+ return simpleTokens.emptyMap
256
+ }
257
+ refStack = Ref.createCheck(refStack, obj, options)
258
+ /** @type {TokenOrNestedTokens[]} */
259
+ const entries = []
260
+ let i = 0
261
+ for (const key of keys) {
262
+ entries[i++] = [
263
+ objectToTokens(key, options, refStack),
264
+ objectToTokens(isMap ? obj.get(key) : obj[key], options, refStack)
265
+ ]
266
+ }
267
+ sortMapEntries(entries, options)
268
+ if (options.addBreakTokens) {
269
+ return [new Token(Type.map, length), entries, new Token(Type.break)]
270
+ }
271
+ return [new Token(Type.map, length), entries]
272
+ }
273
+ }
274
+
275
+ typeEncoders.Map = typeEncoders.Object
276
+ typeEncoders.Buffer = typeEncoders.Uint8Array
277
+ for (const typ of 'Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64'.split(' ')) {
278
+ typeEncoders[`${typ}Array`] = typeEncoders.DataView
279
+ }
280
+
281
+ /**
282
+ * @param {any} obj
283
+ * @param {EncodeOptions} options
284
+ * @param {Reference} [refStack]
285
+ * @returns {TokenOrNestedTokens}
286
+ */
287
+ function objectToTokens (obj, options, refStack) {
288
+ const typ = is(obj)
289
+ const customTypeEncoder = (options && options.typeEncoders && /** @type {OptionalTypeEncoder} */ options.typeEncoders[typ]) || typeEncoders[typ]
290
+ if (typeof customTypeEncoder === 'function') {
291
+ const tokens = customTypeEncoder(obj, typ, options, refStack)
292
+ if (tokens != null) {
293
+ return tokens
294
+ }
295
+ }
296
+ const typeEncoder = typeEncoders[typ]
297
+ if (!typeEncoder) {
298
+ throw new Error(`${options.encodeErrPrefix} unsupported type: ${typ}`)
299
+ }
300
+ return typeEncoder(obj, typ, options, refStack)
301
+ }
302
+
303
+ /*
304
+ CBOR key sorting is a mess.
305
+
306
+ The canonicalisation recommendation from https://tools.ietf.org/html/rfc7049#section-3.9
307
+ includes the wording:
308
+
309
+ > The keys in every map must be sorted lowest value to highest.
310
+ > Sorting is performed on the bytes of the representation of the key
311
+ > data items without paying attention to the 3/5 bit splitting for
312
+ > major types.
313
+ > ...
314
+ > * If two keys have different lengths, the shorter one sorts
315
+ earlier;
316
+ > * If two keys have the same length, the one with the lower value
317
+ in (byte-wise) lexical order sorts earlier.
318
+
319
+ 1. It is not clear what "bytes of the representation of the key" means: is it
320
+ the CBOR representation, or the binary representation of the object itself?
321
+ Consider the int and uint difference here.
322
+ 2. It is not clear what "without paying attention to" means: do we include it
323
+ and compare on that? Or do we omit the special prefix byte, (mostly) treating
324
+ the key in its plain binary representation form.
325
+
326
+ The FIDO 2.0: Client To Authenticator Protocol spec takes the original CBOR
327
+ wording and clarifies it according to their understanding.
328
+ https://fidoalliance.org/specs/fido-v2.0-rd-20170927/fido-client-to-authenticator-protocol-v2.0-rd-20170927.html#message-encoding
329
+
330
+ > The keys in every map must be sorted lowest value to highest. Sorting is
331
+ > performed on the bytes of the representation of the key data items without
332
+ > paying attention to the 3/5 bit splitting for major types. The sorting rules
333
+ > are:
334
+ > * If the major types are different, the one with the lower value in numerical
335
+ > order sorts earlier.
336
+ > * If two keys have different lengths, the shorter one sorts earlier;
337
+ > * If two keys have the same length, the one with the lower value in
338
+ > (byte-wise) lexical order sorts earlier.
339
+
340
+ Some other implementations, such as borc, do a full encode then do a
341
+ length-first, byte-wise-second comparison:
342
+ https://github.com/dignifiedquire/borc/blob/b6bae8b0bcde7c3976b0f0f0957208095c392a36/src/encoder.js#L358
343
+ https://github.com/dignifiedquire/borc/blob/b6bae8b0bcde7c3976b0f0f0957208095c392a36/src/utils.js#L143-L151
344
+
345
+ This has the benefit of being able to easily handle arbitrary keys, including
346
+ complex types (maps and arrays).
347
+
348
+ We'll opt for the FIDO approach, since it affords some efficies since we don't
349
+ need a full encode of each key to determine order and can defer to the types
350
+ to determine how to most efficiently order their values (i.e. int and uint
351
+ ordering can be done on the numbers, no need for byte-wise, for example).
352
+
353
+ Recommendation: stick to single key types or you'll get into trouble, and prefer
354
+ string keys because it's much simpler that way.
355
+ */
356
+
357
+ /*
358
+ (UPDATE, Dec 2020)
359
+ https://tools.ietf.org/html/rfc8949 is the updated CBOR spec and clarifies some
360
+ of the questions above with a new recommendation for sorting order being much
361
+ closer to what would be expected in other environments (i.e. no length-first
362
+ weirdness).
363
+ This new sorting order is not yet implemented here but could be added as an
364
+ option. "Determinism" (canonicity) is system dependent and it's difficult to
365
+ change existing systems that are built with existing expectations. So if a new
366
+ ordering is introduced here, the old needs to be kept as well with the user
367
+ having the option.
368
+ */
369
+
370
+ /**
371
+ * @param {TokenOrNestedTokens[]} entries
372
+ * @param {EncodeOptions} options
373
+ */
374
+ function sortMapEntries (entries, options) {
375
+ if (options.mapSorter) {
376
+ entries.sort(options.mapSorter)
377
+ }
378
+ }
379
+
380
+ /**
381
+ * @param {(Token|Token[])[]} e1
382
+ * @param {(Token|Token[])[]} e2
383
+ * @returns {number}
384
+ */
385
+ function mapSorter (e1, e2) {
386
+ // the key position ([0]) could have a single token or an array
387
+ // almost always it'll be a single token but complex key might get involved
388
+ /* c8 ignore next 2 */
389
+ const keyToken1 = Array.isArray(e1[0]) ? e1[0][0] : e1[0]
390
+ const keyToken2 = Array.isArray(e2[0]) ? e2[0][0] : e2[0]
391
+
392
+ // different key types
393
+ if (keyToken1.type !== keyToken2.type) {
394
+ return keyToken1.type.compare(keyToken2.type)
395
+ }
396
+
397
+ const major = keyToken1.type.major
398
+ // TODO: handle case where cmp === 0 but there are more keyToken e. complex type)
399
+ const tcmp = cborEncoders[major].compareTokens(keyToken1, keyToken2)
400
+ /* c8 ignore next 5 */
401
+ if (tcmp === 0) {
402
+ // duplicate key or complex type where the first token matched,
403
+ // i.e. a map or array and we're only comparing the opening token
404
+ console.warn('WARNING: complex key types used, CBOR key sorting guarantees are gone')
405
+ }
406
+ return tcmp
407
+ }
408
+
409
+ /**
410
+ * @param {Bl} buf
411
+ * @param {TokenOrNestedTokens} tokens
412
+ * @param {TokenTypeEncoder[]} encoders
413
+ * @param {EncodeOptions} options
414
+ */
415
+ function tokensToEncoded (buf, tokens, encoders, options) {
416
+ if (Array.isArray(tokens)) {
417
+ for (const token of tokens) {
418
+ tokensToEncoded(buf, token, encoders, options)
419
+ }
420
+ } else {
421
+ encoders[tokens.type.major](buf, tokens, options)
422
+ }
423
+ }
424
+
425
+ /**
426
+ * @param {any} data
427
+ * @param {TokenTypeEncoder[]} encoders
428
+ * @param {EncodeOptions} options
429
+ * @returns {Uint8Array}
430
+ */
431
+ function encodeCustom (data, encoders, options) {
432
+ const tokens = objectToTokens(data, options)
433
+ if (!Array.isArray(tokens) && options.quickEncodeToken) {
434
+ const quickBytes = options.quickEncodeToken(tokens)
435
+ if (quickBytes) {
436
+ return quickBytes
437
+ }
438
+ const encoder = encoders[tokens.type.major]
439
+ if (encoder.encodedSize) {
440
+ const size = encoder.encodedSize(tokens, options)
441
+ const buf = new Bl(size)
442
+ encoder(buf, tokens, options)
443
+ /* c8 ignore next 4 */
444
+ // this would be a problem with encodedSize() functions
445
+ if (buf.chunks.length !== 1) {
446
+ throw new Error(`Unexpected error: pre-calculated length for ${tokens} was wrong`)
447
+ }
448
+ return asU8A(buf.chunks[0])
449
+ }
450
+ }
451
+ buf.reset()
452
+ tokensToEncoded(buf, tokens, encoders, options)
453
+ return buf.toBytes(true)
454
+ }
455
+
456
+ /**
457
+ * @param {any} data
458
+ * @param {EncodeOptions} [options]
459
+ * @returns {Uint8Array}
460
+ */
461
+ function encode (data, options) {
462
+ options = Object.assign({}, defaultEncodeOptions, options)
463
+ return encodeCustom(data, cborEncoders, options)
464
+ }
465
+
466
+ export { objectToTokens, encode, encodeCustom, Ref }
@@ -0,0 +1,33 @@
1
+ import { encode } from './encode.js'
2
+ import { decode, decodeFirst, Tokeniser, tokensToObject } from './decode.js'
3
+ import { Token, Type } from "@fireproof/vendor/cborg/utils"
4
+
5
+ // is this needed for the json module and other independ encoders
6
+ export { encodeCustom } from './encode.js'
7
+ // export { asU8A, fromString, decodeCodePointsArray } from './utils/byte-utils.js'
8
+ // export { quickEncodeToken } from './utils/jump.js'
9
+ // export { encodeErrPrefix, decodeErrPrefix } from './lib/common.js'
10
+ // export { makeCborEncoders, objectToTokens } from './lib/encode.js'
11
+
12
+ /**
13
+ * Export the types that were present in the original manual cborg.d.ts
14
+ * @typedef {import('../interface').TagDecoder} TagDecoder
15
+ * There was originally just `TypeEncoder` so don't break types by renaming or not exporting
16
+ * @typedef {import('../interface').OptionalTypeEncoder} TypeEncoder
17
+ * @typedef {import('../interface').DecodeOptions} DecodeOptions
18
+ * @typedef {import('../interface').EncodeOptions} EncodeOptions
19
+ */
20
+
21
+ export {
22
+ // this is needed to prevent the bundleing trouble which happens
23
+ // due to the fact that token.js is used in lib/json and so in
24
+ // cborg/json which ends up on bundling to have two copies of token.js
25
+ // which will fail stmts like token.type === Type.array
26
+ decode,
27
+ decodeFirst,
28
+ Tokeniser as Tokenizer,
29
+ tokensToObject,
30
+ encode,
31
+ Token,
32
+ Type
33
+ }
@@ -0,0 +1,106 @@
1
+ // This is an unfortunate replacement for @sindresorhus/is that we need to
2
+ // re-implement for performance purposes. In particular the is.observable()
3
+ // check is expensive, and unnecessary for our purposes. The values returned
4
+ // are compatible with @sindresorhus/is, however.
5
+
6
+ const typeofs = [
7
+ 'string',
8
+ 'number',
9
+ 'bigint',
10
+ 'symbol'
11
+ ]
12
+
13
+ const objectTypeNames = [
14
+ 'Function',
15
+ 'Generator',
16
+ 'AsyncGenerator',
17
+ 'GeneratorFunction',
18
+ 'AsyncGeneratorFunction',
19
+ 'AsyncFunction',
20
+ 'Observable',
21
+ 'Array',
22
+ 'Buffer',
23
+ 'Object',
24
+ 'RegExp',
25
+ 'Date',
26
+ 'Error',
27
+ 'Map',
28
+ 'Set',
29
+ 'WeakMap',
30
+ 'WeakSet',
31
+ 'ArrayBuffer',
32
+ 'SharedArrayBuffer',
33
+ 'DataView',
34
+ 'Promise',
35
+ 'URL',
36
+ 'HTMLElement',
37
+ 'Int8Array',
38
+ 'Uint8Array',
39
+ 'Uint8ClampedArray',
40
+ 'Int16Array',
41
+ 'Uint16Array',
42
+ 'Int32Array',
43
+ 'Uint32Array',
44
+ 'Float32Array',
45
+ 'Float64Array',
46
+ 'BigInt64Array',
47
+ 'BigUint64Array'
48
+ ]
49
+
50
+ /**
51
+ * @param {any} value
52
+ * @returns {string}
53
+ */
54
+ export function is (value) {
55
+ if (value === null) {
56
+ return 'null'
57
+ }
58
+ if (value === undefined) {
59
+ return 'undefined'
60
+ }
61
+ if (value === true || value === false) {
62
+ return 'boolean'
63
+ }
64
+ const typeOf = typeof value
65
+ if (typeofs.includes(typeOf)) {
66
+ return typeOf
67
+ }
68
+ /* c8 ignore next 4 */
69
+ // not going to bother testing this, it's not going to be valid anyway
70
+ if (typeOf === 'function') {
71
+ return 'Function'
72
+ }
73
+ if (Array.isArray(value)) {
74
+ return 'Array'
75
+ }
76
+ if (isBuffer(value)) {
77
+ return 'Buffer'
78
+ }
79
+ const objectType = getObjectType(value)
80
+ if (objectType) {
81
+ return objectType
82
+ }
83
+ /* c8 ignore next */
84
+ return 'Object'
85
+ }
86
+
87
+ /**
88
+ * @param {any} value
89
+ * @returns {boolean}
90
+ */
91
+ function isBuffer (value) {
92
+ return value && value.constructor && value.constructor.isBuffer && value.constructor.isBuffer.call(null, value)
93
+ }
94
+
95
+ /**
96
+ * @param {any} value
97
+ * @returns {string|undefined}
98
+ */
99
+ function getObjectType (value) {
100
+ const objectTypeName = Object.prototype.toString.call(value).slice(8, -1)
101
+ if (objectTypeNames.includes(objectTypeName)) {
102
+ return objectTypeName
103
+ }
104
+ /* c8 ignore next */
105
+ return undefined
106
+ }
@@ -0,0 +1,62 @@
1
+ import { makeCborEncoders, objectToTokens } from './encode.js'
2
+ import { quickEncodeToken } from "@fireproof/vendor/cborg/utils"
3
+
4
+ /**
5
+ * @typedef {import('../interface').EncodeOptions} EncodeOptions
6
+ * @typedef {import('../interface').TokenTypeEncoder} TokenTypeEncoder
7
+ * @typedef {import('../interface').TokenOrNestedTokens} TokenOrNestedTokens
8
+ */
9
+
10
+ const cborEncoders = makeCborEncoders()
11
+
12
+ /** @type {EncodeOptions} */
13
+ const defaultEncodeOptions = {
14
+ float64: false,
15
+ quickEncodeToken,
16
+ encodeErrPrefix: 'CBOR encode error: '
17
+ }
18
+
19
+ /**
20
+ * Calculate the byte length of the given data when encoded as CBOR with the
21
+ * options provided.
22
+ * This calculation will be accurate if the same options are used as when
23
+ * performing a normal encode. Some encode options can change the encoding
24
+ * output length.
25
+ *
26
+ * @param {any} data
27
+ * @param {EncodeOptions} [options]
28
+ * @returns {number}
29
+ */
30
+ export function encodedLength (data, options) {
31
+ options = Object.assign({}, defaultEncodeOptions, options)
32
+ options.mapSorter = undefined // won't change the length
33
+ const tokens = objectToTokens(data, options)
34
+ return tokensToLength(tokens, cborEncoders, options)
35
+ }
36
+
37
+ /**
38
+ * Calculate the byte length of the data as represented by the given tokens when
39
+ * encoded as CBOR with the options provided.
40
+ * This function is for advanced users and would not normally be called
41
+ * directly. See `encodedLength()` for appropriate use.
42
+ *
43
+ * @param {TokenOrNestedTokens} tokens
44
+ * @param {TokenTypeEncoder[]} [encoders]
45
+ * @param {EncodeOptions} [options]
46
+ */
47
+ export function tokensToLength (tokens, encoders = cborEncoders, options = defaultEncodeOptions) {
48
+ if (Array.isArray(tokens)) {
49
+ let len = 0
50
+ for (const token of tokens) {
51
+ len += tokensToLength(token, encoders, options)
52
+ }
53
+ return len
54
+ } else {
55
+ const encoder = encoders[tokens.type.major]
56
+ /* c8 ignore next 3 */
57
+ if (encoder.encodedSize === undefined || typeof encoder.encodedSize !== 'function') {
58
+ throw new Error(`Encoder for ${tokens.type.name} does not have an encodedSize()`)
59
+ }
60
+ return encoder.encodedSize(tokens, options)
61
+ }
62
+ }