Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
How to convert an arrayBuffer to readableStream in typescript?

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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()
    }
  })
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!