1

I'm trying to convert an arraybuffer of a file to a readable stream in Typescript but when i'm trying to create a new ReadableStream variable I obtain this error:

UnhandledPromiseRejectionWarning: ReferenceError: ReadableStream is not defined

What is the problem ? This is my code :

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

export async function putFileToIPFS(file:ArrayBuffer): Promise<string>{
  const readableStream = ReadableBufferStream(file)
  let cid ;
  try {
  console.log("PRINT BEFORE PIN")
  cid = await pinata.pinFileToIPFS(readableStream)
  console.log(cid)
  }   
  catch (error) { console.error(error);}
  return cid['IpfsHash']
}

does anyone know how to help me? I start from a buffer array and would like to be able to get a ReadableStream to be able to load it on IPFS. A thousand thanks

1 Answers1

0

Can you try this(importing ReadableStream from node types)...

import { ReadableStream } from 'node:stream/web'; 


import { ReadableStream } from 'node:stream/web'; // <-- Put this in the beginning of the code file
// ....
// ....
// ....
export function ReadableBufferStream(ab: ArrayBuffer) {
  return new ReadableStream({
    start(controller) {
      controller.enqueue(ab)
      controller.close()
    }
  })
}

export async function putFileToIPFS(file:ArrayBuffer): Promise<string>{
  const readableStream = ReadableBufferStream(file)
  let cid ;
  try {
  console.log("PRINT BEFORE PIN")
  cid = await pinata.pinFileToIPFS(readableStream)
  console.log(cid)
  }   
  catch (error) { console.error(error);}
  return cid['IpfsHash']
}
Nalin Ranjan
  • 1,728
  • 2
  • 9
  • 10