使用JS卡车读取汽车文件

发布于 2025-02-06 14:17:29 字数 1020 浏览 3 评论 0原文

我在JavaScript中有一个CAR文件对象,并希望使用js-car github 。但是我不断获得文件的意外结尾错误。这是我正在尝试的代码,

let arrayBuffer = await files[0].arrayBuffer();
let bytes=new Uint8Array(carFile); 
const reader = await CarReader.fromBytes(bytes) //throws error here
const indexer = await CarIndexer.fromBytes(bytes) //throws error here

我也疲倦了

let str = await files[0].stream() 
const reader = await CarReader.fromIterable(files[0].stream()) //throws error here

,它们都没有工作。但是,使用相同的文件,我检查了此代码

const inStream = fs.createReadStream('test.car')
const reader = await CarReader.fromIterable(inStream)

,我知道carreader.frombytes需要unit8arrey,我敢肯定files [0]是不是零。有人知道我在这里缺少什么吗?

I have a CAR file object in javascript and want to read it using js-car github. But I keep getting unexpected end of the file error. Here is my code I am trying

let arrayBuffer = await files[0].arrayBuffer();
let bytes=new Uint8Array(carFile); 
const reader = await CarReader.fromBytes(bytes) //throws error here
const indexer = await CarIndexer.fromBytes(bytes) //throws error here

I also tired this

let str = await files[0].stream() 
const reader = await CarReader.fromIterable(files[0].stream()) //throws error here

and none of them work. However with the same file this code works

const inStream = fs.createReadStream('test.car')
const reader = await CarReader.fromIterable(inStream)

I checked and I know that CarReader.fromBytes needs a Unit8Arrey and I am sure files[0] is not null. Does anyone knows what I am missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿咻 2025-02-13 14:17:29

因为人们将来可能会面临类似的问题,这是我的解决方案:
我直接使用了res.body,然后将其转换为async stream,然后使用Fromiterable读取它。

async function* streamAsyncIterator(stream) {
  // Get a lock on the stream
  const reader = stream.getReader();

  try {
    while (true) {
      // Read from the stream
      const { done, value } = await reader.read();
      // Exit if we're done
      if (done) return;
      // Else yield the chunk
      yield value;
    }
  }
  finally {
    reader.releaseLock();
  }
}

const info = await w3StorageClient.status(response)

    if (info) {
      // Fetch and verify files from web3.storage
      const res = await w3StorageClient.get(response);
      const reader = await CarReader.fromIterable(streamAsyncIterator(res.body))
      // read the list of roots from the header
      const roots = await reader.getRoots()
      // retrieve a block, as a { cid:CID, bytes:UInt8Array } pair from the archive
      const got = await reader.get(roots[0])
      // also possible: for await (const { cid, bytes } of CarIterator.fromIterable(inStream)) { ... }
      let decoded = cbor.decode(got.bytes)
      console.log('Retrieved [%s] from example.car with CID [%s]',
      decoded,
        roots[0].toString())
    }

for the people might face similar issue in future this is my solution:
I used res.body directly and converted it to an async stream and read it using fromIterable

async function* streamAsyncIterator(stream) {
  // Get a lock on the stream
  const reader = stream.getReader();

  try {
    while (true) {
      // Read from the stream
      const { done, value } = await reader.read();
      // Exit if we're done
      if (done) return;
      // Else yield the chunk
      yield value;
    }
  }
  finally {
    reader.releaseLock();
  }
}

const info = await w3StorageClient.status(response)

    if (info) {
      // Fetch and verify files from web3.storage
      const res = await w3StorageClient.get(response);
      const reader = await CarReader.fromIterable(streamAsyncIterator(res.body))
      // read the list of roots from the header
      const roots = await reader.getRoots()
      // retrieve a block, as a { cid:CID, bytes:UInt8Array } pair from the archive
      const got = await reader.get(roots[0])
      // also possible: for await (const { cid, bytes } of CarIterator.fromIterable(inStream)) { ... }
      let decoded = cbor.decode(got.bytes)
      console.log('Retrieved [%s] from example.car with CID [%s]',
      decoded,
        roots[0].toString())
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文