Js-ipfs: All APIs that support Buffer should support Blob

Created on 2 Apr 2019  路  4Comments  路  Source: ipfs/js-ipfs

"All APIs that support Buffer should support Blob" - @mikeal

The MFS APIs support Blob, which is really important for the browser. It will be really hard to build ProtoSchool tutorials about the non-MFS file commands without Blob support. (File objects in the browser are Blob instances, so we don't have to do any special conversion in tutorials as long as Blob is supported.)

(Please enlighten me if I'm just not understanding how to do this!)

P2 dieasy statuready

Most helpful comment

For reference, this is roughly what it takes to get a Blob as an ArrayBuffer without external dependencies in way that can be used in async/await code.

const blobToBuffer = blob => new Promise(resolve => {
  let arrayBuffer
  let fileReader = new FileReader()
  fileReader.onload = event => resolve(event.target.result)
  fileReader.readAsArrayBuffer(blob)
})

That鈥檚 without error handling, cause I don鈥檛 actually know offhand how FileReader errors work.

So, this makes the bar to using this API in the browser:

  • Learn raw promise API (which we don鈥檛 currently teach, we only use async/await).
  • Learn the difference between Blobs and Buffers.
  • Learn the FileReader API and its onload pattern which is quite foreign if you haven鈥檛 worked with raw DOM before.

That鈥檚 too much. Let鈥檚 just accept a Blob ;)

All 4 comments

The tricky thing (that I see) is how to set the user expectation correctly as we won't be able to return the data as a blob if added as a blob, for example:

  • a)

    • ipfs.add a buffer

    • ipfs.cat returns a buffer

  • b)

    • ipfs.add a blob

    • ipfs.cat returns a buffer

The reason why b) can be _ipfs.cat returns a blob_ is because IPFS doesn't have any information on how a user expects to receive the data for its app.

@daviddias this is already the case with the mfs APIs that support Blob instances.

Teaching people that we always return a buffer is significantly easier than teaching people how to convert a Blob/File to a buffer.

For reference, this is roughly what it takes to get a Blob as an ArrayBuffer without external dependencies in way that can be used in async/await code.

const blobToBuffer = blob => new Promise(resolve => {
  let arrayBuffer
  let fileReader = new FileReader()
  fileReader.onload = event => resolve(event.target.result)
  fileReader.readAsArrayBuffer(blob)
})

That鈥檚 without error handling, cause I don鈥檛 actually know offhand how FileReader errors work.

So, this makes the bar to using this API in the browser:

  • Learn raw promise API (which we don鈥檛 currently teach, we only use async/await).
  • Learn the difference between Blobs and Buffers.
  • Learn the FileReader API and its onload pattern which is quite foreign if you haven鈥檛 worked with raw DOM before.

That鈥檚 too much. Let鈥檚 just accept a Blob ;)

I just learned that @hugomrdias already has it in his OKRs to make blobs supported. 馃帀 馃弳

Was this page helpful?
0 / 5 - 0 ratings