How to fetch content from IPFS with jsipfs?

I have an issue with the Documentation of jsipfs for:

I just want to fetch the content of a binary file into a single Uint8Array.
And I do not know how to do this.

const validCID = '<CID>'

ipfs.files.get(validCID, function(err, files) {
    files.forEach((file) => {
        console.log("File content >> ", file.content.toString('utf8'))

First hit on google: Getting file from hash in js-ipfs - Stack Overflow
Was it that difficult to search first?

Note that in the snippit it does file.content.toString('utf8'), you’re probably looking for file.content (so without the toString('utf8').

That function: ipfs.files.get does not appear to exist in the Documentation File:

And in my jsipfs ipfs.files.get does not exist.

Try the read method instead.
I don’t know why .get is gone (or renamed?).

And the only equivalent I found was refers to uint8ArrayConcat.
I don’t know what that is or how to implement it.

From: js-ipfs/read.js at master · ipfs/js-ipfs · GitHub

   * Read a file
   * @param {string | CID} path - An MFS path, IPFS Path or CID to read
   * @param {ReadOptions & AbortOptions} [options]
   * @returns {AsyncIterable<Uint8Array>}
   * @example
   * ```js
   * const chunks = []
   * for await (const chunk of'/hello-world')) {
   *   chunks.push(chunk)
   * }
   * console.log(uint8ArrayConcat(chunks).toString())
   * // Hello, World!
   * ```

Read carefully. This part:

   * for await (const chunk of'/hello-world')) {
   *   chunks.push(chunk)
   * }

Seems to be iterating over the Uint8Array chunks. (it gets the chunks from the network and stores them locally in chunks.

the uint8ArrayConcat is a way of concatenating chunks which gives you back one big Uint8Array.
See the uint8arrays package for details, that’s what’s being used here.

I just found this by reading the code and following what it does…

Is there any better function or shouldn’t there be one?

I mean, what applications are there, that need to work on the chunks, instead of the files in their entirety?

This is the js-ipfs library. It’s low level.
It’s should be the js version comparable to the go version. The API’s should be comparable.

I think what you get is perfectly fine.
If you need it to be represented differently then make your helper functions on top of it. And if you think it’s useful for a wide audience, make a library out of it for others to use.

Is there a reason why you can’t use this as-is?

You can also use which has a nice example in this (quite big) examples file:

const data = uint8ArrayConcat(await all(

Provided you have it-all, if not you need to add it:

const all = require('it-all')
const data = uint8ArrayConcat(await all(

A one-liner… What more do you want?