From a23125fa917287aa0e1efc53254a1fc1fb69b7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=C3=A9=20Bateira?= Date: Mon, 14 Jun 2021 12:16:48 +0100 Subject: [PATCH] fix: fix all mentions of .string() --- src/tutorials/0004-mutable-file-system/02.md | 2 +- src/tutorials/0004-mutable-file-system/10.md | 2 +- src/tutorials/0005-regular-files-api/04-challenge.md | 4 ++-- src/tutorials/0005-regular-files-api/04.md | 4 ++-- src/tutorials/0005-regular-files-api/07-challenge.md | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/tutorials/0004-mutable-file-system/02.md b/src/tutorials/0004-mutable-file-system/02.md index 0c68735a3..a8c952228 100644 --- a/src/tutorials/0004-mutable-file-system/02.md +++ b/src/tutorials/0004-mutable-file-system/02.md @@ -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') ``` diff --git a/src/tutorials/0004-mutable-file-system/10.md b/src/tutorials/0004-mutable-file-system/10.md index 8babbf8bd..2fe56b036 100644 --- a/src/tutorials/0004-mutable-file-system/10.md +++ b/src/tutorials/0004-mutable-file-system/10.md @@ -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) diff --git a/src/tutorials/0005-regular-files-api/04-challenge.md b/src/tutorials/0005-regular-files-api/04-challenge.md index b9372a0e6..07fc69a0c 100644 --- a/src/tutorials/0005-regular-files-api/04-challenge.md +++ b/src/tutorials/0005-regular-files-api/04-challenge.md @@ -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. \ No newline at end of file +- 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. diff --git a/src/tutorials/0005-regular-files-api/04.md b/src/tutorials/0005-regular-files-api/04.md index 4861740ac..0a189112e 100644 --- a/src/tutorials/0005-regular-files-api/04.md +++ b/src/tutorials/0005-regular-files-api/04.md @@ -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. @@ -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. diff --git a/src/tutorials/0005-regular-files-api/07-challenge.md b/src/tutorials/0005-regular-files-api/07-challenge.md index 3abfb8b46..8cdd7609a 100644 --- a/src/tutorials/0005-regular-files-api/07-challenge.md +++ b/src/tutorials/0005-regular-files-api/07-challenge.md @@ -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`.