我试图将文件的数组缓冲区转换为Typescript中的可读流,但当我试图创建一个新的ReadableStream变量时,我得到了以下错误:
UnhandledPromiseRejectionWarning: ReferenceError: ReadableStream is not defined
问题出在哪里?这是我的代码:
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']
}
有人知道怎么帮我吗?我从一个缓冲区数组开始,希望能够获得ReadableStream,以便能够在IPFS上加载它。感谢
你能试试这个吗(从node
类型导入ReadableStream
(。。。
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']
}