├── .github
├── dependabot.yml
└── workflows
│ ├── generated-pr.yml
│ ├── js-test-and-release.yml
│ └── stale.yml
├── .gitignore
├── CHANGELOG.md
├── LICENSE
├── LICENSE-APACHE
├── LICENSE-MIT
├── README.md
├── package.json
├── src
└── index.js
├── test
├── test-basics.spec.js
└── ts-use
│ ├── .gitignore
│ ├── package.json
│ ├── src
│ └── main.ts
│ └── tsconfig.json
└── tsconfig.json
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: npm
4 | directory: "/"
5 | schedule:
6 | interval: daily
7 | time: "10:00"
8 | open-pull-requests-limit: 10
9 | commit-message:
10 | prefix: "deps"
11 | prefix-development: "deps(dev)"
12 |
--------------------------------------------------------------------------------
/.github/workflows/generated-pr.yml:
--------------------------------------------------------------------------------
1 | name: Close Generated PRs
2 |
3 | on:
4 | schedule:
5 | - cron: '0 0 * * *'
6 | workflow_dispatch:
7 |
8 | permissions:
9 | issues: write
10 | pull-requests: write
11 |
12 | jobs:
13 | stale:
14 | uses: ipdxco/unified-github-workflows/.github/workflows/reusable-generated-pr.yml@v1
15 |
--------------------------------------------------------------------------------
/.github/workflows/js-test-and-release.yml:
--------------------------------------------------------------------------------
1 | name: test & maybe release
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 | pull_request:
8 | workflow_dispatch:
9 |
10 | permissions:
11 | contents: write
12 | id-token: write
13 | packages: write
14 | pull-requests: write
15 |
16 | concurrency:
17 | group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event_name == 'push' && github.sha || github.ref }}
18 | cancel-in-progress: true
19 |
20 | jobs:
21 | js-test-and-release:
22 | uses: ipdxco/unified-github-workflows/.github/workflows/js-test-and-release.yml@v1.0
23 | secrets:
24 | DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
25 | DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
26 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
27 | UCI_GITHUB_TOKEN: ${{ secrets.UCI_GITHUB_TOKEN }}
28 | CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
29 |
--------------------------------------------------------------------------------
/.github/workflows/stale.yml:
--------------------------------------------------------------------------------
1 | name: Close Stale Issues
2 |
3 | on:
4 | schedule:
5 | - cron: '0 0 * * *'
6 | workflow_dispatch:
7 |
8 | permissions:
9 | issues: write
10 | pull-requests: write
11 |
12 | jobs:
13 | stale:
14 | uses: ipdxco/unified-github-workflows/.github/workflows/reusable-stale-issue.yml@v1
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | docs
2 | package-lock.json
3 | yarn.lock
4 | .nyc_output
5 | # Logs
6 | logs
7 | *.log
8 |
9 | # Runtime data
10 | pids
11 | *.pid
12 | *.seed
13 |
14 | # Directory for instrumented libs generated by jscoverage/JSCover
15 | lib-cov
16 |
17 | # Coverage directory used by tools like istanbul
18 | coverage
19 | .coverage
20 |
21 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
22 | .grunt
23 |
24 | # node-waf configuration
25 | .lock-wscript
26 |
27 | # Compiled binary addons (http://nodejs.org/api/addons.html)
28 | build/Release
29 |
30 | # Dependency directory
31 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
32 | node_modules
33 |
34 | dist
35 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## [9.2.4](https://github.com/ipld/js-dag-cbor/compare/v9.2.3...v9.2.4) (2025-05-22)
2 |
3 | ### Bug Fixes
4 |
5 | * address lint errors ([467d98c](https://github.com/ipld/js-dag-cbor/commit/467d98c6142b5e1369b468a565e28eb4f39dfbcc))
6 |
7 | ### Trivial Changes
8 |
9 | * omit lockfile from release commit ([#126](https://github.com/ipld/js-dag-cbor/issues/126)) ([2013bda](https://github.com/ipld/js-dag-cbor/commit/2013bda1b7ca0cc4e89e6f586642885513960cbf))
10 | * remove package-lock ([6445f53](https://github.com/ipld/js-dag-cbor/commit/6445f5324f06678dc4b6639836abf79a306f4865))
11 |
12 | ### Dependencies
13 |
14 | * **dev:** bump aegir from 46.0.5 to 47.0.10 ([c0b2879](https://github.com/ipld/js-dag-cbor/commit/c0b287933d29177cd6ff1232c27bd0a8ae5e661b))
15 |
16 | ## [9.2.3](https://github.com/ipld/js-dag-cbor/compare/v9.2.2...v9.2.3) (2025-05-08)
17 |
18 | ### Dependencies
19 |
20 | * **dev:** bump aegir from 45.2.1 to 46.0.0 ([0c42027](https://github.com/ipld/js-dag-cbor/commit/0c42027694e9a0b8b5cb705a4b8bec85befb638b))
21 |
22 | ## [9.2.2](https://github.com/ipld/js-dag-cbor/compare/v9.2.1...v9.2.2) (2024-10-29)
23 |
24 | ### Dependencies
25 |
26 | * **dev:** bump aegir from 44.1.4 to 45.0.1 ([4e3216c](https://github.com/ipld/js-dag-cbor/commit/4e3216c1849a226bf7ab04f557ec2cd2cc86e34d))
27 |
28 | ## [9.2.1](https://github.com/ipld/js-dag-cbor/compare/v9.2.0...v9.2.1) (2024-06-24)
29 |
30 | ### Dependencies
31 |
32 | * **dev:** bump aegir from 42.2.11 to 44.0.0 ([f703384](https://github.com/ipld/js-dag-cbor/commit/f7033847ae4576fc92096471a0ed796ee95005f4))
33 |
34 | ## [9.2.0](https://github.com/ipld/js-dag-cbor/compare/v9.1.0...v9.2.0) (2024-02-16)
35 |
36 |
37 | ### Features
38 |
39 | * support decoding ArrayBuffers ([#114](https://github.com/ipld/js-dag-cbor/issues/114)) ([a10269d](https://github.com/ipld/js-dag-cbor/commit/a10269d776099f44520171adfd86e65499607f94))
40 |
41 | ## [9.1.0](https://github.com/ipld/js-dag-cbor/compare/v9.0.8...v9.1.0) (2024-01-30)
42 |
43 |
44 | ### Features
45 |
46 | * export encode/decode options ([#113](https://github.com/ipld/js-dag-cbor/issues/113)) ([d2094ca](https://github.com/ipld/js-dag-cbor/commit/d2094cab5c6d95250a569e2ac50c11caf77e1338))
47 |
48 | ## [9.0.8](https://github.com/ipld/js-dag-cbor/compare/v9.0.7...v9.0.8) (2024-01-10)
49 |
50 |
51 | ### Dependencies
52 |
53 | * **dev:** bump aegir from 41.3.5 to 42.1.0 ([c335c50](https://github.com/ipld/js-dag-cbor/commit/c335c50e88917581ac1a79a156a4a8f04080b269))
54 |
55 | ## [9.0.7](https://github.com/ipld/js-dag-cbor/compare/v9.0.6...v9.0.7) (2023-12-30)
56 |
57 |
58 | ### Dependencies
59 |
60 | * bump multiformats from 12.1.3 to 13.0.0 ([#109](https://github.com/ipld/js-dag-cbor/issues/109)) ([7033a31](https://github.com/ipld/js-dag-cbor/commit/7033a31ecfd69e36cf83791c04c4bd2a92cd1cd1))
61 |
62 | ## [9.0.6](https://github.com/ipld/js-dag-cbor/compare/v9.0.5...v9.0.6) (2023-10-03)
63 |
64 |
65 | ### Dependencies
66 |
67 | * **dev:** bump aegir from 40.0.13 to 41.0.0 ([b57fb02](https://github.com/ipld/js-dag-cbor/commit/b57fb02444c4e8f986177ca6d55498d07bc0d114))
68 |
69 | ## [9.0.5](https://github.com/ipld/js-dag-cbor/compare/v9.0.4...v9.0.5) (2023-09-12)
70 |
71 |
72 | ### Trivial Changes
73 |
74 | * add or force update .github/workflows/js-test-and-release.yml ([1ac562b](https://github.com/ipld/js-dag-cbor/commit/1ac562bcd43443c10e4c8ae450dd167761a2472b))
75 | * delete templates [skip ci] ([#103](https://github.com/ipld/js-dag-cbor/issues/103)) ([683e127](https://github.com/ipld/js-dag-cbor/commit/683e1272d40611e79a40ad1f874817cb4b1c7f52))
76 |
77 |
78 | ### Dependencies
79 |
80 | * bump cborg from 2.0.5 to 4.0.0 ([2adc833](https://github.com/ipld/js-dag-cbor/commit/2adc833ef225ab9b8069520853289f1632ab67da))
81 |
82 | ## [9.0.4](https://github.com/ipld/js-dag-cbor/compare/v9.0.3...v9.0.4) (2023-08-08)
83 |
84 |
85 | ### Dependencies
86 |
87 | * **dev:** bump aegir from 39.0.13 to 40.0.8 ([ca7b37e](https://github.com/ipld/js-dag-cbor/commit/ca7b37e7e63542241b219b8e9cdac4d1bba8f653))
88 |
89 | ## [9.0.3](https://github.com/ipld/js-dag-cbor/compare/v9.0.2...v9.0.3) (2023-06-19)
90 |
91 |
92 | ### Dependencies
93 |
94 | * bump multiformats from 11.0.2 to 12.0.1 ([ca10d92](https://github.com/ipld/js-dag-cbor/commit/ca10d92f830ba53eaf7af94d0bf584ebe917635a))
95 |
96 | ## [9.0.2](https://github.com/ipld/js-dag-cbor/compare/v9.0.1...v9.0.2) (2023-06-14)
97 |
98 |
99 | ### Dependencies
100 |
101 | * bump cborg from 1.10.2 to 2.0.1 ([d5fd52a](https://github.com/ipld/js-dag-cbor/commit/d5fd52a50219f2bbac6b0ba3c6977bb6d32ef9a1))
102 |
103 | ## [9.0.1](https://github.com/ipld/js-dag-cbor/compare/v9.0.0...v9.0.1) (2023-05-22)
104 |
105 |
106 | ### Bug Fixes
107 |
108 | * address linting problems ([1e61b12](https://github.com/ipld/js-dag-cbor/commit/1e61b12dcad9aa4b973fbe270217bdcd27e5f465))
109 |
110 |
111 | ### Dependencies
112 |
113 | * **dev:** bump aegir from 37.12.1 to 39.0.8 ([45f01de](https://github.com/ipld/js-dag-cbor/commit/45f01de2eb089135d669fee6f0b5e05ad21cc750))
114 |
115 | ## [9.0.0](https://github.com/ipld/js-dag-cbor/compare/v8.0.1...v9.0.0) (2023-01-06)
116 |
117 |
118 | ### ⚠ BREAKING CHANGES
119 |
120 | * reject duplicate map keys
121 |
122 | ### Bug Fixes
123 |
124 | * reject duplicate map keys ([2e90e34](https://github.com/ipld/js-dag-cbor/commit/2e90e34d959cfcc581acb8f813d8eb5a41e6ff98))
125 |
126 | ## [8.0.1](https://github.com/ipld/js-dag-cbor/compare/v8.0.0...v8.0.1) (2023-01-03)
127 |
128 |
129 | ### Dependencies
130 |
131 | * bump multiformats from 10.0.3 to 11.0.0 ([6c918c0](https://github.com/ipld/js-dag-cbor/commit/6c918c0f45a293d195025b90874b79498be78b97))
132 |
133 | ## [8.0.0](https://github.com/ipld/js-dag-cbor/compare/v7.0.3...v8.0.0) (2022-10-19)
134 |
135 |
136 | ### ⚠ BREAKING CHANGES
137 |
138 | * publish as esm-only (#71)
139 |
140 | ### Features
141 |
142 | * publish as esm-only ([#71](https://github.com/ipld/js-dag-cbor/issues/71)) ([807fdd4](https://github.com/ipld/js-dag-cbor/commit/807fdd465c7d60de63c58256db0ac4df26476c3d))
143 |
144 |
145 | ### Trivial Changes
146 |
147 | * **no-release:** bump actions/setup-node from 3.4.1 to 3.5.0 ([#68](https://github.com/ipld/js-dag-cbor/issues/68)) ([4716850](https://github.com/ipld/js-dag-cbor/commit/47168509e6571c729604ebc29013f314f618379d))
148 | * **no-release:** bump actions/setup-node from 3.5.0 to 3.5.1 ([#70](https://github.com/ipld/js-dag-cbor/issues/70)) ([14f9cdc](https://github.com/ipld/js-dag-cbor/commit/14f9cdca80d9df3be902107b789b1197bc03e6d9))
149 |
150 | ## [7.0.3](https://github.com/ipld/js-dag-cbor/compare/v7.0.2...v7.0.3) (2022-08-27)
151 |
152 |
153 | ### Trivial Changes
154 |
155 | * **deps-dev:** bump typescript from 4.7.4 to 4.8.2 ([#67](https://github.com/ipld/js-dag-cbor/issues/67)) ([3338761](https://github.com/ipld/js-dag-cbor/commit/3338761129e01245fe15eb954bc79c8f481095b1))
156 | * **no-release:** bump actions/setup-node from 3.2.0 to 3.3.0 ([#63](https://github.com/ipld/js-dag-cbor/issues/63)) ([c6249cb](https://github.com/ipld/js-dag-cbor/commit/c6249cbb6a9f0cbbcae1dd021b5d2b4362b47323))
157 | * **no-release:** bump actions/setup-node from 3.3.0 to 3.4.0 ([#65](https://github.com/ipld/js-dag-cbor/issues/65)) ([ec1f3ef](https://github.com/ipld/js-dag-cbor/commit/ec1f3efd11523b5084aed0b0d4e78b41f7797011))
158 | * **no-release:** bump actions/setup-node from 3.4.0 to 3.4.1 ([#66](https://github.com/ipld/js-dag-cbor/issues/66)) ([e2e20cf](https://github.com/ipld/js-dag-cbor/commit/e2e20cfbe0ce0b25e3dfc0cffc06fab89a5b8811))
159 | * **no-release:** bump ipld-garbage from 4.0.10 to 5.0.0 ([#64](https://github.com/ipld/js-dag-cbor/issues/64)) ([80f71cb](https://github.com/ipld/js-dag-cbor/commit/80f71cb038a60de05225fa48be8d3759457155b1))
160 |
161 | ### [7.0.2](https://github.com/ipld/js-dag-cbor/compare/v7.0.1...v7.0.2) (2022-05-25)
162 |
163 |
164 | ### Trivial Changes
165 |
166 | * **deps-dev:** bump typescript from 4.6.4 to 4.7.2 ([a4e2e3b](https://github.com/ipld/js-dag-cbor/commit/a4e2e3b749ce315432545dc5bce7c6c6a56a9b9b))
167 | * **no-release:** bump actions/setup-node from 3.0.0 to 3.1.0 ([#54](https://github.com/ipld/js-dag-cbor/issues/54)) ([bf33215](https://github.com/ipld/js-dag-cbor/commit/bf332155724fb83643286374084783d27a03664a))
168 | * **no-release:** bump actions/setup-node from 3.1.0 to 3.1.1 ([#55](https://github.com/ipld/js-dag-cbor/issues/55)) ([ba23a66](https://github.com/ipld/js-dag-cbor/commit/ba23a6627ea7dfb72caf07234424efaf0b1242e7))
169 | * **no-release:** bump actions/setup-node from 3.1.1 to 3.2.0 ([#61](https://github.com/ipld/js-dag-cbor/issues/61)) ([ac1f42e](https://github.com/ipld/js-dag-cbor/commit/ac1f42efd8a254f5ebf90d17ebd2443f6159a44d))
170 | * **no-release:** bump mocha from 9.2.2 to 10.0.0 ([#58](https://github.com/ipld/js-dag-cbor/issues/58)) ([c024006](https://github.com/ipld/js-dag-cbor/commit/c024006bc30b0000aca57e3573d4c77e1195ade9))
171 | * **no-release:** bump polendina from 2.0.15 to 3.0.0 ([#59](https://github.com/ipld/js-dag-cbor/issues/59)) ([929ff18](https://github.com/ipld/js-dag-cbor/commit/929ff1819a0368a3449eafeab1fce5c385fa734c))
172 | * **no-release:** bump polendina from 3.0.0 to 3.1.0 ([#60](https://github.com/ipld/js-dag-cbor/issues/60)) ([ce5616b](https://github.com/ipld/js-dag-cbor/commit/ce5616b6227fa9355d8a3d02a5e796121f7c501b))
173 | * **no-release:** bump standard from 16.0.4 to 17.0.0 ([#56](https://github.com/ipld/js-dag-cbor/issues/56)) ([0095b44](https://github.com/ipld/js-dag-cbor/commit/0095b441b40745ee53aa8c6edf81d70387cc052e))
174 |
175 | ### [7.0.1](https://github.com/ipld/js-dag-cbor/compare/v7.0.0...v7.0.1) (2022-03-02)
176 |
177 |
178 | ### Trivial Changes
179 |
180 | * **deps-dev:** bump typescript from 4.5.5 to 4.6.2 ([#53](https://github.com/ipld/js-dag-cbor/issues/53)) ([dae80a2](https://github.com/ipld/js-dag-cbor/commit/dae80a2ff6a1675f7f19c796d50a5ada6537b51e))
181 | * **no-release:** bump actions/checkout from 2.4.0 to 3 ([#52](https://github.com/ipld/js-dag-cbor/issues/52)) ([4dfdf4e](https://github.com/ipld/js-dag-cbor/commit/4dfdf4e313d9f8457ce3127a9ca3df5976912c0e))
182 | * **no-release:** bump actions/setup-node from 2.5.0 to 2.5.1 ([#49](https://github.com/ipld/js-dag-cbor/issues/49)) ([e3cd6e9](https://github.com/ipld/js-dag-cbor/commit/e3cd6e9cbc4ddae58e954d4bccc7830e6de681fb))
183 | * **no-release:** bump actions/setup-node from 2.5.1 to 3.0.0 ([#51](https://github.com/ipld/js-dag-cbor/issues/51)) ([b0a94de](https://github.com/ipld/js-dag-cbor/commit/b0a94de18a699eb7727d29be4821fcea55bde97e))
184 |
185 | ## [7.0.0](https://github.com/ipld/js-dag-cbor/compare/v6.0.15...v7.0.0) (2021-12-13)
186 |
187 |
188 | ### ⚠ BREAKING CHANGES
189 |
190 | * coerce undefined to null on decode
191 |
192 | ### Features
193 |
194 | * coerce undefined to null on decode ([8145728](https://github.com/ipld/js-dag-cbor/commit/8145728a1aaf6c5c80c3f600e1f8c621846427af))
195 |
196 | ### [6.0.15](https://github.com/ipld/js-dag-cbor/compare/v6.0.14...v6.0.15) (2021-12-09)
197 |
198 |
199 | ### Bug Fixes
200 |
201 | * update dep descriptors ([22dd509](https://github.com/ipld/js-dag-cbor/commit/22dd509bfa76a98d847277e4856fcd961cb71e6d))
202 |
203 |
204 | ### Trivial Changes
205 |
206 | * use semantic-release, update testing ([e1f6252](https://github.com/ipld/js-dag-cbor/commit/e1f6252914fb3df1308905d2c30e4c333d37dfb5))
207 |
208 |
209 | ## [0.15.2](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.15.1...v0.15.2) (2020-03-27)
210 |
211 |
212 | ### Bug Fixes
213 |
214 | * remove node globals ([#124](https://github.com/ipld/js-ipld-dag-cbor/issues/124)) ([7acfeae](https://github.com/ipld/js-ipld-dag-cbor/commit/7acfeae))
215 | * **package:** update cids to version 0.8.0 ([bc6ac54](https://github.com/ipld/js-ipld-dag-cbor/commit/bc6ac54))
216 |
217 |
218 |
219 |
220 | ## [0.15.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.15.0...v0.15.1) (2020-01-13)
221 |
222 |
223 | ### Bug Fixes
224 |
225 | * **package:** update multicodec to version 1.0.0 ([d1d78a0](https://github.com/ipld/js-ipld-dag-cbor/commit/d1d78a0))
226 | * **package:** update multihashing-async to version 0.8.0 ([b8d4a55](https://github.com/ipld/js-ipld-dag-cbor/commit/b8d4a55))
227 |
228 |
229 |
230 |
231 | # [0.15.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.14.0...v0.15.0) (2019-05-10)
232 |
233 |
234 | ### Bug Fixes
235 |
236 | * **package:** update cids to version 0.7.0 ([2dc3378](https://github.com/ipld/js-ipld-dag-cbor/commit/2dc3378))
237 |
238 |
239 | ### BREAKING CHANGES
240 |
241 | * **package:** Returned v1 CIDs now default to base32 encoding
242 |
243 | Previous versions returned a base58 encoded string when `toString()`/
244 | `toBaseEncodedString()` was called on a CIDv1. It now returns a base32
245 | encoded string.
246 |
247 |
248 |
249 |
250 | # [0.14.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.13.1...v0.14.0) (2019-05-08)
251 |
252 |
253 | ### Bug Fixes
254 |
255 | * make cbor Decoder configurable ([#90](https://github.com/ipld/js-ipld-dag-cbor/issues/90)) ([dfb9137](https://github.com/ipld/js-ipld-dag-cbor/commit/dfb9137))
256 | * remove console.log statement ([a413cb2](https://github.com/ipld/js-ipld-dag-cbor/commit/a413cb2))
257 | * typo in travis badge ([94122f0](https://github.com/ipld/js-ipld-dag-cbor/commit/94122f0))
258 | * **package:** update cids to version 0.6.0 ([1d507f7](https://github.com/ipld/js-ipld-dag-cbor/commit/1d507f7))
259 | * **package:** update multihashing-async to version 0.6.0 ([b328072](https://github.com/ipld/js-ipld-dag-cbor/commit/b328072))
260 |
261 |
262 | ### Features
263 |
264 | * allow decoder heap to grow dynamically ([1f7b7f1](https://github.com/ipld/js-ipld-dag-cbor/commit/1f7b7f1)), closes [#73](https://github.com/ipld/js-ipld-dag-cbor/issues/73)
265 | * new IPLD Format API ([cfc8519](https://github.com/ipld/js-ipld-dag-cbor/commit/cfc8519))
266 |
267 |
268 | ### BREAKING CHANGES
269 |
270 | * The API is now async/await based
271 |
272 | There are numerous changes, the most significant one is that the API
273 | is no longer callback based, but it using async/await.
274 |
275 | For the full new API please see the [IPLD Formats spec].
276 |
277 | [IPLD Formats spec]: https://github.com/ipld/interface-ipld-format
278 |
279 |
280 |
281 |
282 | ## [0.13.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.13.0...v0.13.1) (2019-01-08)
283 |
284 |
285 | ### Bug Fixes
286 |
287 | * reduce bundle size ([#87](https://github.com/ipld/js-ipld-dag-cbor/issues/87)) ([ab637f3](https://github.com/ipld/js-ipld-dag-cbor/commit/ab637f3))
288 |
289 |
290 | ### Features
291 |
292 | * support the hashLen option of multihashing ([#83](https://github.com/ipld/js-ipld-dag-cbor/issues/83)) ([9ffb5e2](https://github.com/ipld/js-ipld-dag-cbor/commit/9ffb5e2))
293 |
294 |
295 |
296 |
297 | # [0.13.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.12.1...v0.13.0) (2018-10-01)
298 |
299 |
300 | ### Bug Fixes
301 |
302 | * resolve falsy values ([0a49705](https://github.com/ipld/js-ipld-dag-cbor/commit/0a49705))
303 |
304 |
305 | ### Features
306 |
307 | * serialize and de-serialize CID instances ([8585d65](https://github.com/ipld/js-ipld-dag-cbor/commit/8585d65))
308 |
309 |
310 | ### BREAKING CHANGES
311 |
312 | * return values from de-serializer are now CID instances.
313 | Serializer still supports old link objects.
314 |
315 |
316 |
317 |
318 | ## [0.12.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.12.0...v0.12.1) (2018-06-29)
319 |
320 |
321 | ### Bug Fixes
322 |
323 | * pass serialized blob to util.cid ([#67](https://github.com/ipld/js-ipld-dag-cbor/issues/67)) ([1ec7744](https://github.com/ipld/js-ipld-dag-cbor/commit/1ec7744))
324 |
325 |
326 | ### Features
327 |
328 | * add defaultHashAlg ([#65](https://github.com/ipld/js-ipld-dag-cbor/issues/65)) ([e095ef5](https://github.com/ipld/js-ipld-dag-cbor/commit/e095ef5))
329 | * add util.cid options ([#66](https://github.com/ipld/js-ipld-dag-cbor/issues/66)) ([1aed60e](https://github.com/ipld/js-ipld-dag-cbor/commit/1aed60e))
330 |
331 |
332 | ### BREAKING CHANGES
333 |
334 | * the first argument is now the serialized output NOT the dag node.
335 | See https://github.com/ipld/interface-ipld-format/issues/32
336 |
337 |
338 |
339 |
340 | # [0.12.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.11.2...v0.12.0) (2018-02-12)
341 |
342 |
343 | ### Bug Fixes
344 |
345 | * use binary blobs directly ([5321d6a](https://github.com/ipld/js-ipld-dag-cbor/commit/5321d6a))
346 |
347 |
348 | ### BREAKING CHANGES
349 |
350 | * Everyone calling the functions of `resolve` need to
351 | pass in the binary data instead of an IPFS block.
352 |
353 | So if your input is an IPFS block, the code changes from
354 |
355 | resolver.resolve(block, path, (err, result) => {…}
356 |
357 | to
358 |
359 | resolver.resolve(block.data, path, (err, result) => {…}
360 |
361 |
362 |
363 |
364 | # [0.12.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.11.2...v0.12.0) (2018-02-12)
365 |
366 |
367 | ### Bug Fixes
368 |
369 | * use binary blobs directly ([5321d6a](https://github.com/ipld/js-ipld-dag-cbor/commit/5321d6a))
370 |
371 |
372 | ### BREAKING CHANGES
373 |
374 | * Everyone calling the functions of `resolve` need to
375 | pass in the binary data instead of an IPFS block.
376 |
377 | So if your input is an IPFS block, the code changes from
378 |
379 | resolver.resolve(block, path, (err, result) => {…}
380 |
381 | to
382 |
383 | resolver.resolve(block.data, path, (err, result) => {…}
384 |
385 |
386 |
387 |
388 | ## [0.11.2](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.11.1...v0.11.2) (2017-11-07)
389 |
390 |
391 | ### Features
392 |
393 | * next Aegir (fix tests in the middle) ([#53](https://github.com/ipld/js-ipld-dag-cbor/issues/53)) ([02940a0](https://github.com/ipld/js-ipld-dag-cbor/commit/02940a0))
394 |
395 |
396 |
397 |
398 | ## [0.11.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.11.0...v0.11.1) (2017-04-04)
399 |
400 |
401 |
402 |
403 | # [0.11.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.10.1...v0.11.0) (2017-03-21)
404 |
405 |
406 | ### Features
407 |
408 | * use new block api ([4ec9228](https://github.com/ipld/js-ipld-dag-cbor/commit/4ec9228))
409 |
410 |
411 |
412 |
413 | ## [0.10.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.10.0...v0.10.1) (2017-03-16)
414 |
415 |
416 |
417 |
418 | # [0.10.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.9.1...v0.10.0) (2017-03-13)
419 |
420 |
421 |
422 |
423 | ## [0.9.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.9.0...v0.9.1) (2017-02-09)
424 |
425 |
426 |
427 |
428 | # [0.9.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.8.6...v0.9.0) (2017-02-02)
429 |
430 |
431 |
432 |
433 | ## [0.8.6](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.8.5...v0.8.6) (2017-01-31)
434 |
435 |
436 | ### Features
437 |
438 | * CBOR TAG ([#38](https://github.com/ipld/js-ipld-dag-cbor/issues/38)) ([13323a2](https://github.com/ipld/js-ipld-dag-cbor/commit/13323a2))
439 |
440 |
441 |
442 |
443 | ## [0.8.5](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.8.4...v0.8.5) (2017-01-29)
444 |
445 |
446 |
447 |
448 | ## [0.8.4](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.8.3...v0.8.4) (2017-01-29)
449 |
450 |
451 |
452 |
453 | ## [0.8.3](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.8.2...v0.8.3) (2016-12-11)
454 |
455 |
456 | ### Features
457 |
458 | * switch to borc ([#31](https://github.com/ipld/js-ipld-dag-cbor/issues/31)) ([3164a81](https://github.com/ipld/js-ipld-dag-cbor/commit/3164a81)), closes [#23](https://github.com/ipld/js-ipld-dag-cbor/issues/23)
459 |
460 |
461 |
462 |
463 | ## [0.8.2](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.8.1...v0.8.2) (2016-12-01)
464 |
465 |
466 |
467 |
468 | ## [0.8.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.8.0...v0.8.1) (2016-11-21)
469 |
470 |
471 |
472 |
473 | # [0.8.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.7.1...v0.8.0) (2016-11-03)
474 |
475 |
476 |
477 |
478 | ## [0.7.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.7.0...v0.7.1) (2016-10-30)
479 |
480 |
481 |
482 |
483 | # [0.7.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.6.0...v0.7.0) (2016-10-26)
484 |
485 |
486 | ### Bug Fixes
487 |
488 | * add array handling to .tree ([656ad84](https://github.com/ipld/js-ipld-dag-cbor/commit/656ad84))
489 | * complete migration to async API ([2e91d7c](https://github.com/ipld/js-ipld-dag-cbor/commit/2e91d7c))
490 | * out of scope traversal for 2 or more levels deep ([b7a565b](https://github.com/ipld/js-ipld-dag-cbor/commit/b7a565b))
491 |
492 |
493 | ### Features
494 |
495 | * add util.serialize, util.deserialize and util.cid ([fcc2ab5](https://github.com/ipld/js-ipld-dag-cbor/commit/fcc2ab5))
496 | * resolve out of scope ([bea41ea](https://github.com/ipld/js-ipld-dag-cbor/commit/bea41ea))
497 | * resolver.resolve within scope ([1158fa4](https://github.com/ipld/js-ipld-dag-cbor/commit/1158fa4))
498 | * resolver.tree and resolver.multicodec ([21ddefc](https://github.com/ipld/js-ipld-dag-cbor/commit/21ddefc))
499 | * use async interfaces ([48eb863](https://github.com/ipld/js-ipld-dag-cbor/commit/48eb863))
500 |
501 |
502 |
503 |
504 | # [0.6.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.4.0...v0.6.0) (2016-05-22)
505 |
506 |
507 | ### Bug Fixes
508 |
509 | * **cbor:** Typo in lodash.clonedeep ([2f617b0](https://github.com/ipld/js-ipld-dag-cbor/commit/2f617b0))
510 | * Add missing babel-runtime dep ([5c11ce8](https://github.com/ipld/js-ipld-dag-cbor/commit/5c11ce8))
511 | * correct references in package.json ([12f18ab](https://github.com/ipld/js-ipld-dag-cbor/commit/12f18ab))
512 | * Ensure inputs are not modified ([b20f90b](https://github.com/ipld/js-ipld-dag-cbor/commit/b20f90b))
513 |
514 |
515 | ### Features
516 |
517 | * upgrade to latest spec ([7375f99](https://github.com/ipld/js-ipld-dag-cbor/commit/7375f99))
518 |
519 |
520 |
521 |
522 | # [0.4.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.3.1...v0.4.0) (2016-03-22)
523 |
524 |
525 |
526 |
527 | ## [0.3.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.3.0...v0.3.1) (2015-11-13)
528 |
529 |
530 |
531 |
532 | # [0.3.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.2.1...v0.3.0) (2015-11-13)
533 |
534 |
535 |
536 |
537 | ## [0.2.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.2.0...v0.2.1) (2015-10-29)
538 |
539 |
540 |
541 |
542 | # [0.2.0](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.1.3...v0.2.0) (2015-09-14)
543 |
544 |
545 |
546 |
547 | ## [0.1.3](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.1.2...v0.1.3) (2015-09-04)
548 |
549 |
550 |
551 |
552 | ## [0.1.2](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.1.1...v0.1.2) (2015-09-04)
553 |
554 |
555 |
556 |
557 | ## [0.1.1](https://github.com/ipld/js-ipld-dag-cbor/compare/v0.1.0...v0.1.1) (2015-09-01)
558 |
559 |
560 |
561 |
562 | # 0.1.0 (2015-09-01)
563 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | This project is dual licensed under MIT and Apache-2.0.
2 |
3 | MIT: https://www.opensource.org/licenses/mit
4 | Apache-2.0: https://www.apache.org/licenses/license-2.0
5 |
--------------------------------------------------------------------------------
/LICENSE-APACHE:
--------------------------------------------------------------------------------
1 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2 |
3 | http://www.apache.org/licenses/LICENSE-2.0
4 |
5 | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
6 |
--------------------------------------------------------------------------------
/LICENSE-MIT:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # @ipld/dag-cbor
2 |
3 | [](https://codecov.io/gh/ipld/js-dag-cbor)
4 | [](https://github.com/ipld/js-dag-cbor/actions/workflows/js-test-and-release.yml)
5 |
6 | > JS implementation of DAG-CBOR
7 |
8 | ## Table of contents
9 |
10 | - [Install](#install)
11 | - [Spec](#spec)
12 | - [License](#license)
13 | - [Contribute](#contribute)
14 |
15 | ## Install
16 |
17 | ```console
18 | $ npm i @ipld/dag-cbor
19 | ```
20 |
21 | This is the *new* interface meant for use by itself or with `multiformats` and
22 | `@ipld/block`. It is not used by `js-ipld-format` which is currently
23 | used in IPFS. That library is [here](https://github.com/ipld/js-ipld-dag-cbor).
24 |
25 | Usage:
26 |
27 | ```javascript
28 | import { encode, decode } from '@ipld/dag-cbor'
29 | import { CID } from 'multiformats'
30 |
31 | const obj = {
32 | x: 1,
33 | /* CID instances are encoded as links */
34 | y: [2, 3, CID.parse('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4')],
35 | z: {
36 | a: CID.parse('QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'),
37 | b: null,
38 | c: 'string'
39 | }
40 | }
41 |
42 | let data = encode(obj)
43 | let decoded = decode(data)
44 | decoded.y[0] // 2
45 | CID.asCID(decoded.z.a) // cid instance
46 |
47 | // encode/decode options are exported for use with cborg's encodedLength and decodeFirst
48 | import { encodeOptions, decodeOptions } from '@ipld/dag-cbor'
49 | import { encodedLength } from 'cborg/length'
50 | import { decodeFirst } from 'cborg'
51 |
52 | // dag-cbor encoded length of obj in bytes
53 | const byteLength = encodedLength(obj, encodeOptions)
54 | byteLength // 104
55 |
56 | // concatenate two dag-cbor encoded obj
57 | const concatenatedData = new Uint8Array(data.length * 2)
58 | concatenatedData.set(data)
59 | concatenatedData.set(data, data.length)
60 |
61 | // returns dag-cbor decoded obj at the beginning of the buffer as well as the remaining bytes
62 | const [first, remainder] = decodeFirst(concatenatedData, decodeOptions)
63 | assert.deepStrictEqual(first, obj)
64 | assert.deepStrictEqual(remainder, data)
65 | ```
66 |
67 | ## Spec
68 |
69 | The [`dag-cbor` specification is in the IPLD specs repo](https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-cbor.md).
70 |
71 | ## License
72 |
73 | Licensed under either of
74 |
75 | - Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / )
76 | - MIT ([LICENSE-MIT](LICENSE-MIT) / )
77 |
78 | ## Contribute
79 |
80 | Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
81 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "@ipld/dag-cbor",
3 | "version": "9.2.4",
4 | "description": "JS implementation of DAG-CBOR",
5 | "author": "Rod (http://r.va.gg/)",
6 | "license": "Apache-2.0 OR MIT",
7 | "homepage": "https://github.com/ipld/js-dag-cbor#readme",
8 | "repository": {
9 | "type": "git",
10 | "url": "git+https://github.com/ipld/js-dag-cbor.git"
11 | },
12 | "bugs": {
13 | "url": "https://github.com/ipld/js-dag-cbor/issues"
14 | },
15 | "keywords": [
16 | "ipfs",
17 | "ipld",
18 | "multiformats"
19 | ],
20 | "engines": {
21 | "node": ">=16.0.0",
22 | "npm": ">=7.0.0"
23 | },
24 | "type": "module",
25 | "types": "./dist/src/index.d.ts",
26 | "typesVersions": {
27 | "*": {
28 | "*": [
29 | "*",
30 | "dist/*",
31 | "dist/src/*",
32 | "dist/src/*/index"
33 | ],
34 | "src/*": [
35 | "*",
36 | "dist/*",
37 | "dist/src/*",
38 | "dist/src/*/index"
39 | ]
40 | }
41 | },
42 | "files": [
43 | "src",
44 | "dist",
45 | "!dist/test",
46 | "!**/*.tsbuildinfo"
47 | ],
48 | "exports": {
49 | ".": {
50 | "types": "./dist/src/index.d.ts",
51 | "import": "./src/index.js"
52 | }
53 | },
54 | "eslintConfig": {
55 | "extends": "ipfs",
56 | "parserOptions": {
57 | "sourceType": "module"
58 | },
59 | "ignorePatterns": [
60 | "test/ts-use"
61 | ]
62 | },
63 | "release": {
64 | "branches": [
65 | "master"
66 | ],
67 | "plugins": [
68 | [
69 | "@semantic-release/commit-analyzer",
70 | {
71 | "preset": "conventionalcommits",
72 | "releaseRules": [
73 | {
74 | "breaking": true,
75 | "release": "major"
76 | },
77 | {
78 | "revert": true,
79 | "release": "patch"
80 | },
81 | {
82 | "type": "feat",
83 | "release": "minor"
84 | },
85 | {
86 | "type": "fix",
87 | "release": "patch"
88 | },
89 | {
90 | "type": "docs",
91 | "release": "patch"
92 | },
93 | {
94 | "type": "test",
95 | "release": "patch"
96 | },
97 | {
98 | "type": "deps",
99 | "release": "patch"
100 | },
101 | {
102 | "scope": "no-release",
103 | "release": false
104 | }
105 | ]
106 | }
107 | ],
108 | [
109 | "@semantic-release/release-notes-generator",
110 | {
111 | "preset": "conventionalcommits",
112 | "presetConfig": {
113 | "types": [
114 | {
115 | "type": "feat",
116 | "section": "Features"
117 | },
118 | {
119 | "type": "fix",
120 | "section": "Bug Fixes"
121 | },
122 | {
123 | "type": "chore",
124 | "section": "Trivial Changes"
125 | },
126 | {
127 | "type": "docs",
128 | "section": "Documentation"
129 | },
130 | {
131 | "type": "deps",
132 | "section": "Dependencies"
133 | },
134 | {
135 | "type": "test",
136 | "section": "Tests"
137 | }
138 | ]
139 | }
140 | }
141 | ],
142 | "@semantic-release/changelog",
143 | "@semantic-release/npm",
144 | "@semantic-release/github",
145 | [
146 | "@semantic-release/git",
147 | {
148 | "assets": [
149 | "CHANGELOG.md",
150 | "package.json"
151 | ]
152 | }
153 | ]
154 | ]
155 | },
156 | "scripts": {
157 | "clean": "aegir clean",
158 | "lint": "aegir lint",
159 | "build": "aegir build",
160 | "release": "aegir release",
161 | "test": "npm run lint && aegir test",
162 | "test:ts": "npm run test --prefix test/ts-use",
163 | "test:node": "aegir test -t node --cov",
164 | "test:chrome": "aegir test -t browser --cov",
165 | "test:chrome-webworker": "aegir test -t webworker",
166 | "test:firefox": "aegir test -t browser -- --browser firefox",
167 | "test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
168 | "test:electron-main": "aegir test -t electron-main",
169 | "dep-check": "aegir dep-check"
170 | },
171 | "dependencies": {
172 | "cborg": "^4.0.0",
173 | "multiformats": "^13.1.0"
174 | },
175 | "devDependencies": {
176 | "@ipld/garbage": "^6.0.0",
177 | "aegir": "^47.0.10"
178 | }
179 | }
180 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import * as cborg from 'cborg'
2 | import { CID } from 'multiformats/cid'
3 |
4 | // https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
5 | const CID_CBOR_TAG = 42
6 |
7 | /**
8 | * @template T
9 | * @typedef {import('multiformats/codecs/interface').ByteView} ByteView
10 | */
11 |
12 | /**
13 | * @template T
14 | * @typedef {import('multiformats/codecs/interface').ArrayBufferView} ArrayBufferView
15 | */
16 |
17 | /**
18 | * @template T
19 | * @param {ByteView | ArrayBufferView} buf
20 | * @returns {ByteView}
21 | */
22 | export function toByteView (buf) {
23 | if (buf instanceof ArrayBuffer) {
24 | return new Uint8Array(buf, 0, buf.byteLength)
25 | }
26 |
27 | return buf
28 | }
29 |
30 | /**
31 | * cidEncoder will receive all Objects during encode, it needs to filter out
32 | * anything that's not a CID and return `null` for that so it's encoded as
33 | * normal.
34 | *
35 | * @param {any} obj
36 | * @returns {cborg.Token[]|null}
37 | */
38 | function cidEncoder (obj) {
39 | if (obj.asCID !== obj && obj['/'] !== obj.bytes) {
40 | return null // any other kind of object
41 | }
42 | const cid = CID.asCID(obj)
43 | /* c8 ignore next 4 */
44 | // very unlikely case, and it'll probably throw a recursion error in cborg
45 | if (!cid) {
46 | return null
47 | }
48 | const bytes = new Uint8Array(cid.bytes.byteLength + 1)
49 | bytes.set(cid.bytes, 1) // prefix is 0x00, for historical reasons
50 | return [
51 | new cborg.Token(cborg.Type.tag, CID_CBOR_TAG),
52 | new cborg.Token(cborg.Type.bytes, bytes)
53 | ]
54 | }
55 |
56 | // eslint-disable-next-line jsdoc/require-returns-check
57 | /**
58 | * Intercept all `undefined` values from an object walk and reject the entire
59 | * object if we find one.
60 | *
61 | * @returns {null}
62 | */
63 | function undefinedEncoder () {
64 | throw new Error('`undefined` is not supported by the IPLD Data Model and cannot be encoded')
65 | }
66 |
67 | /**
68 | * Intercept all `number` values from an object walk and reject the entire
69 | * object if we find something that doesn't fit the IPLD data model (NaN &
70 | * Infinity).
71 | *
72 | * @param {number} num
73 | * @returns {null}
74 | */
75 | function numberEncoder (num) {
76 | if (Number.isNaN(num)) {
77 | throw new Error('`NaN` is not supported by the IPLD Data Model and cannot be encoded')
78 | }
79 | if (num === Infinity || num === -Infinity) {
80 | throw new Error('`Infinity` and `-Infinity` is not supported by the IPLD Data Model and cannot be encoded')
81 | }
82 | return null
83 | }
84 |
85 | const _encodeOptions = {
86 | float64: true,
87 | typeEncoders: {
88 | Object: cidEncoder,
89 | undefined: undefinedEncoder,
90 | number: numberEncoder
91 | }
92 | }
93 |
94 | export const encodeOptions = {
95 | ..._encodeOptions,
96 | typeEncoders: {
97 | ..._encodeOptions.typeEncoders
98 | }
99 | }
100 |
101 | /**
102 | * @param {Uint8Array} bytes
103 | * @returns {CID}
104 | */
105 | function cidDecoder (bytes) {
106 | if (bytes[0] !== 0) {
107 | throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00')
108 | }
109 | return CID.decode(bytes.subarray(1)) // ignore leading 0x00
110 | }
111 |
112 | const _decodeOptions = {
113 | allowIndefinite: false,
114 | coerceUndefinedToNull: true,
115 | allowNaN: false,
116 | allowInfinity: false,
117 | allowBigInt: true, // this will lead to BigInt for ints outside of
118 | // safe-integer range, which may surprise users
119 | strict: true,
120 | useMaps: false,
121 | rejectDuplicateMapKeys: true,
122 | /** @type {import('cborg').TagDecoder[]} */
123 | tags: []
124 | }
125 | _decodeOptions.tags[CID_CBOR_TAG] = cidDecoder
126 |
127 | export const decodeOptions = {
128 | ..._decodeOptions,
129 | tags: _decodeOptions.tags.slice()
130 | }
131 |
132 | export const name = 'dag-cbor'
133 | export const code = 0x71
134 |
135 | /**
136 | * @template T
137 | * @param {T} node
138 | * @returns {ByteView}
139 | */
140 | export const encode = (node) => cborg.encode(node, _encodeOptions)
141 |
142 | /**
143 | * @template T
144 | * @param {ByteView | ArrayBufferView} data
145 | * @returns {T}
146 | */
147 | export const decode = (data) => cborg.decode(toByteView(data), _decodeOptions)
148 |
--------------------------------------------------------------------------------
/test/test-basics.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env mocha */
2 |
3 | import { Buffer } from 'buffer'
4 | import { garbage } from '@ipld/garbage'
5 | import { assert } from 'aegir/chai'
6 | import { decodeFirst } from 'cborg'
7 | import { encodedLength } from 'cborg/length'
8 | import { bytes, CID } from 'multiformats'
9 | import * as dagcbor from '../src/index.js'
10 |
11 | const { encode, decode } = dagcbor
12 | const test = it
13 | const same = assert.deepStrictEqual
14 |
15 | describe('dag-cbor', () => {
16 | const obj = {
17 | someKey: 'someValue',
18 | link: CID.parse('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL'),
19 | links: [
20 | CID.parse('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL'),
21 | CID.parse('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL')
22 | ],
23 | nested: {
24 | hello: 'world',
25 | link: CID.parse('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL')
26 | },
27 | bytes: new TextEncoder().encode('asdf')
28 | }
29 | const serializedObj = encode(obj)
30 |
31 | test('.serialize and .deserialize', () => {
32 | same(bytes.isBinary(serializedObj), true)
33 |
34 | // Check for the tag 42
35 | // d8 = tag, 2a = 42
36 | same(bytes.toHex(serializedObj).match(/d82a/g)?.length, 4)
37 |
38 | const deserializedObj = decode(serializedObj)
39 | same(deserializedObj, obj)
40 | })
41 |
42 | test('.serialize and .deserialize with ArrayBuffer', () => {
43 | same(bytes.isBinary(serializedObj), true)
44 |
45 | // Check for the tag 42
46 | // d8 = tag, 2a = 42
47 | same(bytes.toHex(serializedObj).match(/d82a/g)?.length, 4)
48 |
49 | const deserializedObj = decode(new Uint8Array(
50 | serializedObj.buffer.slice(
51 | serializedObj.byteOffset,
52 | serializedObj.byteOffset + serializedObj.byteLength)
53 | ))
54 | same(deserializedObj, obj)
55 | })
56 |
57 | test('.serialize and .deserialize large objects', () => {
58 | // larger than the default borc heap size, should auto-grow the heap
59 | const dataSize = 128 * 1024
60 | const largeObj = { someKey: [].slice.call(new Uint8Array(dataSize)) }
61 |
62 | const serialized = encode(largeObj)
63 | same(bytes.isBinary(serialized), true)
64 |
65 | const deserialized = decode(serialized)
66 | same(largeObj, deserialized)
67 | })
68 |
69 | test('.serialize and .deserialize object with slash as property', () => {
70 | const slashObject = { '/': true }
71 | const serialized = encode(slashObject)
72 | const deserialized = decode(serialized)
73 | same(deserialized, slashObject)
74 | })
75 |
76 | test('CIDs have clean for deep comparison', () => {
77 | const deserializedObj = decode(serializedObj)
78 | // backing buffer must be pristine as some comparison libraries go that deep
79 | const actual = deserializedObj.link.bytes.join(',')
80 | const expected = obj.link.bytes.join(',')
81 | same(actual, expected)
82 | })
83 |
84 | test('error on circular references', () => {
85 | const circularObj = {}
86 | circularObj.a = circularObj
87 | assert.throws(() => encode(circularObj), /object contains circular references/)
88 | const circularArr = [circularObj]
89 | circularObj.a = circularArr
90 | assert.throws(() => encode(circularArr), /object contains circular references/)
91 | })
92 |
93 | test('error on encoding undefined', () => {
94 | assert.throws(() => encode(undefined), /\Wundefined\W.*not supported/)
95 | const objWithUndefined = { a: 'a', b: undefined }
96 | assert.throws(() => encode(objWithUndefined), /\Wundefined\W.*not supported/)
97 | })
98 |
99 | test('error on encoding IEEE 754 specials', () => {
100 | for (const special of [NaN, Infinity, -Infinity]) {
101 | assert.throws(() => encode(special), new RegExp(`\\W${String(special)}\\W.*not supported`))
102 | const objWithSpecial = { a: 'a', b: special }
103 | assert.throws(() => encode(objWithSpecial), new RegExp(`\\W${String(special)}\\W.*not supported`))
104 | const arrWithSpecial = [1, 1.1, -1, -1.1, Number.MAX_SAFE_INTEGER, special, Number.MIN_SAFE_INTEGER]
105 | assert.throws(() => encode(arrWithSpecial), new RegExp(`\\W${String(special)}\\W.*not supported`))
106 | }
107 | })
108 |
109 | test('error on decoding IEEE 754 specials', () => {
110 | // encoded forms of each of the previous encode() tests
111 | const cases = [
112 | ['NaN', 'f97e00'],
113 | ['NaN', 'f97ff8'],
114 | ['NaN', 'fa7ff80000'],
115 | ['NaN', 'fb7ff8000000000000'],
116 | ['NaN', 'a2616161616162fb7ff8000000000000'],
117 | ['NaN', '8701fb3ff199999999999a20fbbff199999999999a1b001ffffffffffffffb7ff80000000000003b001ffffffffffffe'],
118 | ['Infinity', 'f97c00'],
119 | ['Infinity', 'fb7ff0000000000000'],
120 | ['Infinity', 'a2616161616162fb7ff0000000000000'],
121 | ['Infinity', '8701fb3ff199999999999a20fbbff199999999999a1b001ffffffffffffffb7ff00000000000003b001ffffffffffffe'],
122 | ['-Infinity', 'f9fc00'],
123 | ['-Infinity', 'fbfff0000000000000'],
124 | ['-Infinity', 'a2616161616162fbfff0000000000000'],
125 | ['-Infinity', '8701fb3ff199999999999a20fbbff199999999999a1b001ffffffffffffffbfff00000000000003b001ffffffffffffe']
126 | ]
127 | for (const [typ, hex] of cases) {
128 | const byts = bytes.fromHex(hex)
129 | assert.throws(() => decode(byts), new RegExp(`\\W${typ.replace(/^-/, '')}\\W.*not supported`))
130 | }
131 | })
132 |
133 | test('fuzz serialize and deserialize with garbage', function () {
134 | this.timeout(5000)
135 | for (let ii = 0; ii < 1000; ii++) {
136 | const original = garbage(100)
137 | const encoded = encode(original)
138 | const decoded = decode(encoded)
139 | same(decoded, original)
140 | }
141 | })
142 |
143 | test('CIDv1', () => {
144 | const link = CID.parse('zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS')
145 | const encoded = encode({ link })
146 | const decoded = decode(encoded)
147 | same(decoded, { link })
148 | })
149 |
150 | test('encode and decode consistency with Uint8Array and Buffer fields', () => {
151 | const buffer = Buffer.from('some data')
152 | const bytes = Uint8Array.from(buffer)
153 |
154 | const s1 = encode({ data: buffer })
155 | const s2 = encode({ data: bytes })
156 |
157 | same(s1, s2)
158 |
159 | const verify = (/** @type {{ data: Uint8Array }} */ s) => {
160 | same(typeof s, 'object')
161 | same(Object.keys(s), ['data'])
162 | assert(s.data instanceof Uint8Array)
163 | same(s.data.buffer, bytes.buffer)
164 | }
165 | verify(decode(s1))
166 | verify(decode(s2))
167 | })
168 |
169 | test('reject extraneous, but valid CBOR data after initial top-level object', () => {
170 | assert.throws(() => {
171 | // two top-level CBOR objects, the original and a single uint=0, valid if using
172 | // CBOR in streaming mode, not valid here
173 | const big = new Uint8Array(serializedObj.length + 1)
174 | big.set(serializedObj, 0)
175 | decode(big)
176 | }, /too many terminals/)
177 | })
178 |
179 | test('reject bad CID lead-in', () => {
180 | // this is the same data as the CIDv1 produces but has the lead-in to the
181 | // CID replaced with 0x01 ....................... ↓↓ here
182 | const encoded = bytes.fromHex('a1646c696e6bd82a582501017012207252523e6591fb8fe553d67ff55a86f84044b46a3e4176e10c58fa529a4aabd5')
183 | assert.throws(() => decode(encoded), /Invalid CID for CBOR tag 42; expected leading 0x00/)
184 | })
185 |
186 | test('sloppy decode: coerce undefined', () => {
187 | // See https://github.com/ipld/js-dag-cbor/issues/44 for context on this
188 | let encoded = bytes.fromHex('f7')
189 | let decoded = decode(encoded)
190 | same(null, decoded)
191 |
192 | encoded = bytes.fromHex('a26362617af763666f6f63626172')
193 | decoded = decode(encoded)
194 | same({ foo: 'bar', baz: null }, decoded)
195 | })
196 |
197 | test('reject duplicate map keys', () => {
198 | const encoded = bytes.fromHex('a3636261720363666f6f0163666f6f02')
199 | assert.throws(() => decode(encoded), /CBOR decode error: found repeat map key "foo"/)
200 | })
201 |
202 | test('determine encoded length of obj', () => {
203 | const { encodeOptions } = dagcbor
204 |
205 | const length = encodedLength(obj, encodeOptions)
206 | same(length, serializedObj.length)
207 | })
208 |
209 | test('.deserialize the first of concatenated serialized objects', () => {
210 | const { decodeOptions } = dagcbor
211 |
212 | const concatSerializedObjs = new Uint8Array(serializedObj.length * 2)
213 | concatSerializedObjs.set(serializedObj)
214 | concatSerializedObjs.set(serializedObj, serializedObj.length)
215 |
216 | const [deserializedObj, remainder] = decodeFirst(concatSerializedObjs, decodeOptions)
217 | same(deserializedObj, obj)
218 | same(remainder, serializedObj)
219 | })
220 | })
221 |
--------------------------------------------------------------------------------
/test/ts-use/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | src/main.js
3 | tsconfig.tsbuildinfo
4 |
--------------------------------------------------------------------------------
/test/ts-use/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ts-use",
3 | "private": true,
4 | "dependencies": {
5 | "@ipld/dag-cbor": "file:../../dist/",
6 | "multiformats": "file:../../node_modules/multiformats"
7 | },
8 | "scripts": {
9 | "test": "npm install && npm_config_yes=true npx -p typescript tsc && node src/main.js"
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/test/ts-use/src/main.ts:
--------------------------------------------------------------------------------
1 | /* eslint-disable no-console */
2 |
3 | import { deepStrictEqual } from 'assert'
4 | import * as dagCbor from '@ipld/dag-cbor'
5 | import { BlockEncoder, BlockDecoder, BlockCodec } from 'multiformats/codecs/interface'
6 |
7 | const main = (): void => {
8 | // make sure we have a full CodecFeature
9 | useCodecFeature(dagCbor)
10 | }
11 |
12 | function useCodecFeature (codec: BlockCodec<0x71, any>): void {
13 | // use only as a BlockEncoder
14 | useEncoder(codec)
15 |
16 | // use only as a BlockDecoder
17 | useDecoder(codec)
18 |
19 | // use with ArrayBuffer input type
20 | useDecoderWithArrayBuffer(codec)
21 |
22 | // use as a full BlockCodec which does both BlockEncoder & BlockDecoder
23 | useBlockCodec(codec)
24 | }
25 |
26 | function useEncoder (encoder: BlockEncoder): void {
27 | deepStrictEqual(encoder.code, 0x71)
28 | deepStrictEqual(encoder.name, 'dag-cbor')
29 | deepStrictEqual(Array.from(encoder.encode('blip')), [100, 98, 108, 105, 112])
30 | console.log('[TS] ✓ { encoder: BlockEncoder }')
31 | }
32 |
33 | function useDecoder (decoder: BlockDecoder): void {
34 | deepStrictEqual(decoder.code, 0x71)
35 | deepStrictEqual(decoder.decode(Uint8Array.from([100, 98, 108, 105, 112])), 'blip')
36 | console.log('[TS] ✓ { decoder: BlockDecoder }')
37 | }
38 |
39 | function useDecoderWithArrayBuffer (decoder: BlockDecoder): void {
40 | deepStrictEqual(decoder.code, 0x70)
41 | deepStrictEqual(decoder.decode(Uint8Array.from([100, 98, 108, 105, 112]).buffer), 'blip')
42 | console.log('[TS] ✓ { decoder: BlockDecoder }')
43 | }
44 |
45 | function useBlockCodec (blockCodec: BlockCodec): void {
46 | deepStrictEqual(blockCodec.code, 0x71)
47 | deepStrictEqual(blockCodec.name, 'dag-cbor')
48 | deepStrictEqual(Array.from(blockCodec.encode('blip')), [100, 98, 108, 105, 112])
49 | deepStrictEqual(blockCodec.decode(Uint8Array.from([100, 98, 108, 105, 112])), 'blip')
50 | console.log('[TS] ✓ {}:BlockCodec')
51 | }
52 |
53 | main()
54 |
55 | export default main
56 |
--------------------------------------------------------------------------------
/test/ts-use/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "strict": true,
4 | "moduleResolution": "node",
5 | "noImplicitAny": true,
6 | "skipLibCheck": false,
7 | "incremental": true
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "aegir/src/config/tsconfig.aegir.json",
3 | "compilerOptions": {
4 | "outDir": "dist",
5 | "emitDeclarationOnly": true
6 | },
7 | "include": [
8 | "src",
9 | "test"
10 | ],
11 | "exclude": [
12 | "test/ts-use"
13 | ]
14 | }
15 |
--------------------------------------------------------------------------------