Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 31ec91f

Browse files
authored
Merge pull request #265 from ipfs/add-mfs-read-methods
docs: adds stream-related mfs files.read method signatures
2 parents d3986c0 + 06668d2 commit 31ec91f

File tree

1 file changed

+74
-13
lines changed

1 file changed

+74
-13
lines changed

SPEC/FILES.md

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* [files.stat](#filesmkdir)
2020
* [files.rm](#filesrm)
2121
* [files.read](#filesread)
22+
* [files.readReadableStream](#filesreadreadablestream)
23+
* [files.readPullStream](#filesreadpullstream)
2224
* [files.write](#fileswrite)
2325
* [files.mv](#filesmv)
2426
* [files.flush](#filesflush)
@@ -599,8 +601,8 @@ The Mutable File System (MFS) is a virtual file system on top of IPFS that expos
599601

600602
Where:
601603

602-
- `from` is the path of the source object to copy.
603-
- `to` is the path of the destination object to copy to.
604+
- `from` is the path of the source file to copy.
605+
- `to` is the path of the destination file to copy to.
604606

605607
`callback` must follow the `function (err) {}` signature, where `err` is an Error if the operation was not successful.
606608

@@ -700,7 +702,7 @@ ipfs.files.stat('/file.txt', (err, stats) => {
700702

701703
Where:
702704

703-
- `path` is the path of the object to remove.
705+
- `path` is the path of the file to remove.
704706
- `options` is an optional Object that might contain the following keys:
705707
- `recursive` is a Boolean value to decide whether or not to remove directories recursively.
706708

@@ -728,33 +730,88 @@ ipfs.files.rm('/my/beautiful/directory', { recursive: true }, (err) => {
728730

729731
#### `files.read`
730732

731-
> Read a file.
733+
> Read a file into a [`Buffer`][b].
732734
733735
##### `Go` **WIP**
734736

735-
##### `JavaScript` - ipfs.files.read(path, [options, callback])
737+
##### `JavaScript` - ipfs.files.read(path, [options], [callback])
736738

737739
Where:
738740

739-
- `path` is the path of the object to read.
741+
- `path` is the path of the file to read.
740742
- `options` is an optional Object that might contain the following keys:
741743
- `offset` is an Integer with the byte offset to begin reading from.
742744
- `count` is an Integer with the maximum number of bytes to read.
743745

744-
`callback` must follow the `function (err, buf) {}` signature, where `err` is an Error if the operation was not successful and `buf` is a Buffer with the contents of `path`.
746+
`callback` must follow the `function (err, buf) {}` signature, where `err` is an Error if the operation was not successful and `buf` is a [`Buffer`][b] with the contents of `path`.
745747

746748
If no `callback` is passed, a promise is returned.
747749

748750
**Example:**
749751

750752
```JavaScript
751753
ipfs.files.read('/hello-world', (err, buf) => {
752-
console.log(buf.toString())
754+
console.log(buf.toString('utf8'))
753755
})
754756

755757
// Hello, World!
756758
```
757759

760+
#### `readReadableStream`
761+
762+
> Read a file into a [`ReadableStream`][rs].
763+
764+
##### `Go` **WIP**
765+
766+
##### `JavaScript` - ipfs.files.readReadableStream(path, [options])
767+
768+
Where:
769+
770+
- `path` is the path of the file to read.
771+
- `options` is an optional Object that might contain the following keys:
772+
- `offset` is an Integer with the byte offset to begin reading from.
773+
- `count` is an Integer with the maximum number of bytes to read.
774+
775+
Returns a [`ReadableStream`][rs] with the contents of `path`.
776+
777+
**Example:**
778+
779+
```JavaScript
780+
const stream = ipfs.files.readReadableStream('/hello-world')
781+
stream.on('data', (buf) => console.log(buf.toString('utf8')))
782+
783+
// Hello, World!
784+
```
785+
786+
#### `readPullStream`
787+
788+
> Read a file into a [`PullStream`][ps].
789+
790+
##### `Go` **WIP**
791+
792+
##### `JavaScript` - ipfs.files.readPullStream(path, [options])
793+
794+
Where:
795+
796+
- `path` is the path of the file to read.
797+
- `options` is an optional Object that might contain the following keys:
798+
- `offset` is an Integer with the byte offset to begin reading from.
799+
- `count` is an Integer with the maximum number of bytes to read.
800+
801+
Returns a [`PullStream`][ps] with the contents of `path`.
802+
803+
**Example:**
804+
805+
```JavaScript
806+
pull(
807+
ipfs.files.readPullStream('/hello-world'),
808+
through(buf => console.log(buf.toString('utf8'))),
809+
collect(err => {})
810+
)
811+
812+
// Hello, World!
813+
```
814+
758815
#### `files.write`
759816

760817
> Write to a file.
@@ -765,10 +822,13 @@ ipfs.files.read('/hello-world', (err, buf) => {
765822

766823
Where:
767824

768-
- `path` is the path of the object to write.
825+
- `path` is the path of the file to write.
769826
- `content` can be:
770-
- a Buffer instance.
771-
- a Path (caveat: will only work in Node.js).
827+
- a [`Buffer`][b]
828+
- a [`PullStream`][ps]
829+
- a [`ReadableStream`][rs]
830+
- a [`Blob`][blob] (caveat: will only work in the browser)
831+
- a string path to a file (caveat: will only work in Node.js)
772832
- `options` is an optional Object that might contain the following keys:
773833
- `offset` is an Integer with the byte offset to begin writing at.
774834
- `create` is a Boolean to indicate to create the file if it doesn't exist.
@@ -797,8 +857,8 @@ ipfs.files.write('/hello-world', Buffer.from('Hello, world!'), (err) => {
797857

798858
Where:
799859

800-
- `from` is the path of the source object to move.
801-
- `to` is the path of the destination object to move to.
860+
- `from` is the path of the source file to move.
861+
- `to` is the path of the destination file to move to.
802862

803863
`callback` must follow the `function (err) {}` signature, where `err` is an Error if the operation was not successful.
804864

@@ -881,3 +941,4 @@ ipfs.files.ls('/screenshots', function (err, files) {
881941
[rs]: https://www.npmjs.com/package/readable-stream
882942
[ps]: https://www.npmjs.com/package/pull-stream
883943
[cid]: https://www.npmjs.com/package/cids
944+
[blob]: https://developer.mozilla.org/en-US/docs/Web/API/Blob

0 commit comments

Comments
 (0)