3

I'm using Pinata's pinFileToIPFS() function that requires a ReadableStream to upload to Pinata's IPFS nodes. I have the bytearray of the file, for example of a PNG. I want to convert this byteArray to readableStream and upload this on IPFS. How can i convert that in typescript?

export async function putFileToIPFS(file:any): Promise<string>{
  readableStream = ** CONVERT FILE TO READABLE **
  let cid ;
  try {
  cid = await pinata.pinFileToIPFS(readableStream)
  console.log(cid)
  }   
  catch (error) { console.error(error);}
  return cid['IpfsHash']
}

Thanks

anthumchris
  • 8,245
  • 2
  • 28
  • 53

1 Answers1

0
export async function putFileToIPFS(file: ArrayBuffer) {
  const readableStream = new ReadableBufferStream(file)
  ...
}

function ReadableBufferStream(ab: ArrayBuffer) {
  return new ReadableStream({
    start(controller) {
      controller.enqueue(ab)
      controller.close()
    }
  })
}

Alternatively, the "read size" could be controlled by setting a chunk size with multiple enqueues. This could potentially increase/decrease the number of HTTP requests sent by pinFileToIPFS(). subarray() maintains the memory footprint by reusing the underlying ArrayBuffer.

function ReadableBufferStream(ab: ArrayBuffer, chunkSize = 64*1024) { // 64 KiB
  return new ReadableStream({
    start(controller) {
      const bytes = new Uint8Array(ab)
      for (let readIndex = 0; readIndex < bytes.byteLength;) {
        controller.enqueue(bytes.subarray(readIndex, readIndex += chunkSize))
      }
      controller.close()
    }
  })
}
anthumchris
  • 8,245
  • 2
  • 28
  • 53
  • for the first implementation I receive this error : UnhandledPromiseRejectionWarning: ReferenceError: ReadableStream is not defined –  Apr 01 '22 at 10:00
  • Are you running in the browser or via command line (Deno/NodeJS)? – anthumchris Apr 01 '22 at 17:49