英文:
Create a blob from a web stream in Node.js
问题
我正在尝试通过ipfs-car
(它使用Web流)来打包一个CAR文件,然后将其作为文件发送到请求中,但是在将数据流传入WritableStream
后,我不确定如何将其存储在Blob
中。
我有以下代码片段:
import { CAREncoderStream, createDirectoryEncoderStream } from 'ipfs-car'
import { WritableStream } from 'node:stream/web'
export const packCAR = async (files) => {
const blockStream = createDirectoryEncoderStream(files)
const carEncoderStream = new CAREncoderStream([])
const outStream = new WritableStream()
await blockStream.pipeThrough(carEncoderStream).pipeTo(outStream)
const blob = new Blob(/* 这里我应该放什么? */)
}
我没有找到有人这样做的例子,只看到了如何将Blob
转换为流的方法,所以希望能得到帮助
英文:
I am trying to pack a CAR file through ipfs-car
(which uses web streams) and later send it in a request as a file but I'm not sure how to store it in a Blob
after I have streamed in a WritableStream
.
I have this piece of code
import { CAREncoderStream, createDirectoryEncoderStream } from 'ipfs-car'
import { WritableStream } from 'node:stream/web'
export const packCAR = async (files) => {
const blockStream = createDirectoryEncoderStream(files)
const carEncoderStream = new CAREncoderStream([])
const outStream = new WritableStream()
await blockStream.pipeThrough(carEncoderStream).pipeTo(outStream)
const blob = new Blob(/* what do I put here? */)
}
I haven't found anyone doing this, I only saw how to convert Blob
to a stream, so help would be appreciated
答案1
得分: 2
将WritableStream的内容存储到Blob中。这可以通过将WritableStream转换为ReadableStream,然后将其转换为ArrayBuffer,最后将ArrayBuffer存储到Blob中来完成。
import { CAREncoderStream, createDirectoryEncoderStream } from 'ipfs-car'
import { WritableStream, ReadableStream } from 'node:stream/web'
export const packCAR = async (files) => {
const blockStream = createDirectoryEncoderStream(files)
const carEncoderStream = new CAREncoderStream([])
const chunks = []
const outStream = new WritableStream({
write(chunk) {
chunks.push(chunk)
},
close() {
console.log('Stream is closed.')
}
})
await blockStream.pipeThrough(carEncoderStream).pipeTo(outStream)
const arrayBuffer = await new Blob(chunks).arrayBuffer()
const blob = new Blob([arrayBuffer])
return blob
}
英文:
Store the content of the WritableStream into a Blob. This can be done by transforming the WritableStream into a ReadableStream, then converting it into an ArrayBuffer, and finally storing the ArrayBuffer into a Blob.
import { CAREncoderStream, createDirectoryEncoderStream } from 'ipfs-car'
import { WritableStream, ReadableStream } from 'node:stream/web'
export const packCAR = async (files) => {
const blockStream = createDirectoryEncoderStream(files)
const carEncoderStream = new CAREncoderStream([])
const chunks = []
const outStream = new WritableStream({
write(chunk) {
chunks.push(chunk)
},
close() {
console.log('Stream is closed.')
}
})
await blockStream.pipeThrough(carEncoderStream).pipeTo(outStream)
const arrayBuffer = await new Blob(chunks).arrayBuffer()
const blob = new Blob([arrayBuffer])
return blob
}
答案2
得分: 1
你可以使用 new Response(readable).blob()
来将一个 ReadableStream
转换为一个 Blob
,这样可以使代码更简单:
英文:
You can use new Response(readable).blob()
to convert a ReadableStream
to a Blob
, which allows simpler code:
import { CAREncoderStream, createDirectoryEncoderStream } from 'ipfs-car'
import { WritableStream, ReadableStream } from 'node:stream/web'
export const packCAR = async (files) => {
const blockStream = createDirectoryEncoderStream(files)
const carEncoderStream = new CAREncoderStream([])
const readable = blockStream.pipeThrough(carEncoderStream)
const blob = await new Response(readable).blob()
return blob
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论