Create a blob from a web stream in Node.js.

huangapple go评论69阅读模式
英文:

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转换为流的方法,所以希望能得到帮助 Create a blob from a web stream in Node.js.

英文:

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 Create a blob from a web stream in Node.js.

答案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
}

huangapple
  • 本文由 发表于 2023年5月29日 18:52:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定