|
8 | 8 | * [`cat`](#cat)
|
9 | 9 | * [`get`](#get)
|
10 | 10 | * [`id`](#id)
|
| 11 | +* [`read`](#read) |
11 | 12 | * [`start`](#start)
|
12 | 13 | * [`stop`](#stop)
|
13 | 14 | * [`version`](#version)
|
| 15 | +* [`write`](#write) |
14 | 16 |
|
15 | 17 | ## Getting started
|
16 | 18 |
|
@@ -219,7 +221,7 @@ Get file contents.
|
219 | 221 | | Name | Type | Description |
|
220 | 222 | |------|------|-------------|
|
221 | 223 | | path | `String`\|`Buffer`\|[`CID`](https://www.npmjs.com/package/cids) | IPFS path or CID to cat data from |
|
222 |
| -| options | `Object` | (optional) options | |
| 224 | +| options | `Object` | (optional) options TODO | |
223 | 225 |
|
224 | 226 | #### Returns
|
225 | 227 |
|
@@ -329,6 +331,36 @@ console.log(info)
|
329 | 331 | */
|
330 | 332 | ```
|
331 | 333 |
|
| 334 | +## read |
| 335 | + |
| 336 | +Read data from an MFS (Mutable File System) path. |
| 337 | + |
| 338 | +### `node.read(path, [options])` |
| 339 | + |
| 340 | +#### Parameters |
| 341 | + |
| 342 | +| Name | Type | Description | |
| 343 | +|------|------|-------------| |
| 344 | +| path | `String` | MFS path of the _file_ to read (not a directory) | |
| 345 | +| options | `Object` | (optional) options | |
| 346 | +| options.offset | `Number` | Byte offset to begin reading at, default: `0` | |
| 347 | +| options.length | `Number` | Number of bytes to read, default: `undefined` (read all bytes) | |
| 348 | + |
| 349 | +#### Returns |
| 350 | + |
| 351 | +| Type | Description | |
| 352 | +|------|-------------| |
| 353 | +| `Iterator<Buffer>` | An iterator that can be used to consume all the data | |
| 354 | + |
| 355 | +#### Example |
| 356 | + |
| 357 | +```js |
| 358 | +let data = Buffer.alloc(0) |
| 359 | +for (const chunk of node.read('/path/to/mfs/file')) { |
| 360 | + data = Buffer.concat([data, chunk]) |
| 361 | +} |
| 362 | +``` |
| 363 | + |
332 | 364 | ## start
|
333 | 365 |
|
334 | 366 | Start the IPFS node.
|
@@ -386,3 +418,48 @@ const info = await node.version()
|
386 | 418 | console.log(info)
|
387 | 419 | // { version: '0.32.2', repo: 7, commit: '' }
|
388 | 420 | ```
|
| 421 | + |
| 422 | +## write |
| 423 | + |
| 424 | +Write data to an MFS (Mutable File System) file. |
| 425 | + |
| 426 | +### `node.write(path, input, [options])` |
| 427 | + |
| 428 | +#### Parameters |
| 429 | + |
| 430 | +| Name | Type | Description | |
| 431 | +|------|------|-------------| |
| 432 | +| path | `String` | MFS path of the _file_ to write to | |
| 433 | +| input | `Buffer`\|`String`\|`Iterable`\|`Iterator` | Input data | |
| 434 | +| options | `Object` | (optional) options | |
| 435 | +| options.offset | `Number` | Byte offset to begin writing at, default: `0` | |
| 436 | +| options.length | `Number` | Number of bytes to write, default: `undefined` (write all bytes) | |
| 437 | +| options.create | `Boolean` | Create file if not exists, default: `true` | |
| 438 | +| options.parents | `Boolean` | Create parent directories if not exists, default: `false` | |
| 439 | +| options.truncate | `Boolean` | Truncate the file after writing, default: `false` | |
| 440 | +| options.rawLeaves | `Boolean` | Do not wrap leaf nodes in a protobuf, default: `false` | |
| 441 | +| options.cidVersion | `Number` | CID version to use when creating the node(s), default: 1 | |
| 442 | + |
| 443 | +#### Returns |
| 444 | + |
| 445 | +| Type | Description | |
| 446 | +|------|-------------| |
| 447 | +| `Promise` | Resolved when the write has been completed | |
| 448 | + |
| 449 | +#### Example |
| 450 | + |
| 451 | +Write a buffer/string: |
| 452 | + |
| 453 | +```js |
| 454 | +await node.write('/hello-world.txt', Buffer.from('hello world!')) |
| 455 | +``` |
| 456 | + |
| 457 | +```js |
| 458 | +await node.write('/example/hello-world.txt', 'hello world!', { parents: true }) |
| 459 | +``` |
| 460 | + |
| 461 | +Write a Node.js stream: |
| 462 | + |
| 463 | +```js |
| 464 | +await node.write('/example.js', fs.createReadStream(__filename), { create: true }) |
| 465 | +``` |
0 commit comments