Skip to content

Commit

Permalink
fix: fix all mentions of .string()
Browse files Browse the repository at this point in the history
  • Loading branch information
Zé Bateira committed Jun 14, 2021
1 parent 1296956 commit a23125f
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/tutorials/0004-mutable-file-system/02.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ When working with your IPFS node, you'll often want to check the status of a fil

For example, to check the status of a directory called `stuff` located within our root directory ( `/` ), we could call the method like so:

````javascript
```javascript
await ipfs.files.stat('/stuff')
```

Expand Down
2 changes: 1 addition & 1 deletion src/tutorials/0004-mutable-file-system/10.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ipfs.files.read(path, [options])

The `path` provided is the path of the file to read, and it must point to a file rather than a directory.

The `files.read` method returns an Async Iterable that iterates over the file's chunks of data, i.e. Buffers. In our case, we ultimately need to convert Buffers into strings using the method `toString()`. However, the chunks of data within a single file need to be reassembled (concatenated) before the conversion. The [`it-to-buffer`](https://www.npmjs.com/package/it-to-buffer) package can iterate over all of the chunks and put them back together for us. (We've made this package available to you in our challenges as `toBuffer`.)
The `files.read` method returns an Async Iterable that iterates over the file's chunks of data, i.e. Buffers. In our case, we ultimately need to convert Buffers into strings using the method `new TextDecoder().encode(buffer)`. However, the chunks of data within a single file need to be reassembled (concatenated) before the conversion. The [`it-to-buffer`](https://www.npmjs.com/package/it-to-buffer) package can iterate over all of the chunks and put them back together for us. (We've made this package available to you in our challenges as `toBuffer`.)

```js
// the toBuffer variable is globally available (just like ipfs)
Expand Down
4 changes: 2 additions & 2 deletions src/tutorials/0005-regular-files-api/04-challenge.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Using the `cat` function, retrieve the file's contents and return them as a stri

**Hints:**

- The `CID` we provide is a string, so it needs to be placed inside quotes. Also, don't forget to convert the buffered contents of the text file to a string using `.toString()` before returning your result.
- You need to concatenate all the chunks of data because the `ipfs.cat` method returns an `Async Iterable`. You can use the package `it-to-buffer` to achieve this.
- The `CID` we provide is a string, so it needs to be placed inside quotes. Also, don't forget to convert the buffered contents of the text file to a string using `new TextDecoder().decode()` before returning your result.
- You need to concatenate all the chunks of data because the `ipfs.cat` method returns an `Async Iterable`. You can use the package `it-to-buffer` to achieve this.
4 changes: 2 additions & 2 deletions src/tutorials/0005-regular-files-api/04.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The `cat` method first searches your own node for the file requested, and if it

The `cat` method returns an Async Iterable that iterates over the file's chunks of data, i.e. Buffers.

A `Buffer` is just a raw collection of bytes and as such doesn't make any assumptions about encodings or the type of data it contains. However, if we know the file being retrieved is a plain text file such as a `.txt`, we can convert its buffered contents to a UTF-8 string (an interpretation of those raw bytes) by calling the JavaScript method `.toString()`.
A `Buffer` is just a raw collection of bytes and as such doesn't make any assumptions about encodings or the type of data it contains. However, if we know the file being retrieved is a plain text file such as a `.txt`, we can convert its buffered contents to a UTF-8 string (an interpretation of those raw bytes) by calling the JavaScript method `new TextDecoder().decode()`.

But since we have multiple buffers of the same file, we need to reassemble them (concatenate) into a single buffer before converting it into a string. The [`it-to-buffer`](https://www.npmjs.com/package/it-to-buffer) package can do just that: iterate over all of the chunks of the file and put them back together for us.

Expand All @@ -25,7 +25,7 @@ So if you had the CID for a text file in an IPFS node, you could retrieve the fi
// the toBuffer variable is globally available (just like ipfs)

const bufferedContents = await toBuffer(ipfs.cat('QmWCscor6qWPdx53zEQmZvQvuWQYxx1ARRCXwYVE4s9wzJ')) // returns a Buffer
const stringContents = bufferedContents.toString() // returns a string
const stringContents = new TextEncoder().decode(bufferedContents) // returns a string
```

When you're ready to try this in the real world, you should note that the above example can result in heavy memory usage, depending on the contents of the file being read. If you're working with large files and find this to be the case, you might want to skip using the `it-to-buffer` package and instead process each chunk of data iteratively. The main reason IPFS now returns `Async Iterables` is to provide a built-in option for dealing with potential performance issues.
Expand Down
2 changes: 1 addition & 1 deletion src/tutorials/0005-regular-files-api/07-challenge.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ Based on our file structure, the path could be expressed as:
There are two valid ways to solve this challenge. Take your pick!

**Hints:**
- In the code below we've already taken care of converting the `bufferedContents` to a string before returning the result, as you did yourself previously using `.toString()`.
- In the code below we've already taken care of converting the `bufferedContents` to a string before returning the result, as you did yourself previously using `new TextDecoder().decode()`.
- Don't forget to prepend `/ipfs/` to the IPFS Path string
- You need to concatenate all the data into a single buffer because the `ipfs.cat` method returns an `Async Iterable`. You can use the package `it-to-buffer`.

0 comments on commit a23125f

Please sign in to comment.