|
4 | 4 | </a>
|
5 | 5 | </p>
|
6 | 6 |
|
7 |
| -# @helia/car <!-- omit in toc --> |
| 7 | +# @helia/car |
8 | 8 |
|
9 | 9 | [](https://ipfs.tech)
|
10 | 10 | [](https://discuss.ipfs.tech)
|
|
13 | 13 |
|
14 | 14 | > Import/export car files from Helia
|
15 | 15 |
|
16 |
| -## Table of contents <!-- omit in toc --> |
| 16 | +# About |
17 | 17 |
|
18 |
| -- [Install](#install) |
19 |
| - - [Browser `<script>` tag](#browser-script-tag) |
20 |
| -- [API Docs](#api-docs) |
21 |
| -- [License](#license) |
22 |
| -- [Contribute](#contribute) |
| 18 | +`@helia/car` provides `import` and `export` methods to read/write Car files to Helia's blockstore. |
23 | 19 |
|
24 |
| -## Install |
| 20 | +See the Car interface for all available operations. |
| 21 | + |
| 22 | +By default it supports `dag-pb`, `dag-cbor`, `dag-json` and `raw` CIDs, more esoteric DAG walkers can be passed as an init option. |
| 23 | + |
| 24 | +## Example - Exporting a DAG as a CAR file |
| 25 | + |
| 26 | +```typescript |
| 27 | +import { createHelia } from 'helia' |
| 28 | +import { unixfs } from '@helia/unixfs' |
| 29 | +import { car } from '@helia/car' |
| 30 | +import { CarWriter } from '@ipld/car' |
| 31 | +import { Readable } from 'node:stream' |
| 32 | +import nodeFs from 'node:fs' |
| 33 | + |
| 34 | +const helia = createHelia({ |
| 35 | + // ... helia config |
| 36 | +}) |
| 37 | +const fs = unixfs(helia) |
| 38 | + |
| 39 | +// add some UnixFS data |
| 40 | +const cid = await fs.addBytes(fileData1) |
| 41 | + |
| 42 | +// export it as a Car |
| 43 | +const c = car(helia) |
| 44 | +const { writer, out } = await CarWriter.create(cid) |
| 45 | + |
| 46 | +// `out` needs to be directed somewhere, see the @ipld/car docs for more information |
| 47 | +Readable.from(out).pipe(nodeFs.createWriteStream('example.car')) |
| 48 | + |
| 49 | +// write the DAG behind `cid` into the writer |
| 50 | +await c.export(cid, writer) |
| 51 | +``` |
| 52 | + |
| 53 | +## Example - Importing all blocks from a CAR file |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { createHelia } from 'helia' |
| 57 | +import { unixfs } from '@helia/unixfs' |
| 58 | +import { car } from '@helia/car' |
| 59 | +import { CarReader } from '@ipld/car' |
| 60 | +import { Readable } from 'node:stream' |
| 61 | +import nodeFs from 'node:fs' |
| 62 | + |
| 63 | +const helia = createHelia({ |
| 64 | + // ... helia config |
| 65 | +}) |
| 66 | + |
| 67 | +// import the car |
| 68 | +const inStream = nodeFs.createReadStream('example.car') |
| 69 | +const reader = await CarReader.fromIterable(inStream) |
| 70 | + |
| 71 | +await c.import(reader) |
| 72 | +``` |
| 73 | + |
| 74 | +# Install |
25 | 75 |
|
26 | 76 | ```console
|
27 | 77 | $ npm i @helia/car
|
28 | 78 | ```
|
29 | 79 |
|
30 |
| -### Browser `<script>` tag |
| 80 | +## Browser `<script>` tag |
31 | 81 |
|
32 | 82 | Loading this module through a script tag will make it's exports available as `HeliaCar` in the global namespace.
|
33 | 83 |
|
34 | 84 | ```html
|
35 | 85 | <script src="https://unpkg.com/@helia/car/dist/index.min.js"></script>
|
36 | 86 | ```
|
37 | 87 |
|
38 |
| -## API Docs |
| 88 | +# API Docs |
39 | 89 |
|
40 | 90 | - <https://ipfs.github.io/helia-car/modules/_helia_car.html>
|
41 | 91 |
|
42 |
| -## License |
| 92 | +# License |
43 | 93 |
|
44 | 94 | Licensed under either of
|
45 | 95 |
|
46 | 96 | - Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
|
47 | 97 | - MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
|
48 | 98 |
|
49 |
| -## Contribute |
| 99 | +# Contribute |
50 | 100 |
|
51 | 101 | Contributions welcome! Please check out [the issues](https://github.com/ipfs/helia-car/issues).
|
52 | 102 |
|
|
0 commit comments