从 Deno 中的 Fetch Reader 解压

发布于 2025-01-11 22:41:43 字数 965 浏览 0 评论 0原文

我试图弄清楚为什么我使用这段代码不断收到以下错误,

[uncaught application error]: Error - checksum error
import { Untar } from "https://deno.land/[email protected]/archive/tar.ts";
import { readerFromStreamReader } from "https://deno.land/[email protected]/streams/conversion.ts";

const res = await fetch("https://registry.npmjs.org/react/-/react-17.0.2.tgz", { keepalive: true });

if (res.status === 200) {
  const streamReader = res.body!.getReader();
  const reader = readerFromStreamReader(streamReader);
  const untar = new Untar(reader);

  for await (const block of untar) {
   // errors with [uncaught application error]: Error - checksum error
  }
}

你能从这样的流中解压缩吗?

I'm trying to figure why I keep getting the following erorr with this code

[uncaught application error]: Error - checksum error
import { Untar } from "https://deno.land/[email protected]/archive/tar.ts";
import { readerFromStreamReader } from "https://deno.land/[email protected]/streams/conversion.ts";

const res = await fetch("https://registry.npmjs.org/react/-/react-17.0.2.tgz", { keepalive: true });

if (res.status === 200) {
  const streamReader = res.body!.getReader();
  const reader = readerFromStreamReader(streamReader);
  const untar = new Untar(reader);

  for await (const block of untar) {
   // errors with [uncaught application error]: Error - checksum error
  }
}

Can you Untar from a stream like this?

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

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

发布评论

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

评论(1

岁月静好 2025-01-18 22:41:43

您正在流式传输的响应是使用 gzip 压缩进行压缩的,因此您需要通过 解压转换流先:

./so-71365204.ts

import {
  assertExists,
  assertStrictEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { readerFromStreamReader } from "https://deno.land/[email protected]/streams/conversion.ts";
import { Untar } from "https://deno.land/[email protected]/archive/tar.ts";

const res = await fetch("https://registry.npmjs.org/react/-/react-17.0.2.tgz");

assertStrictEquals(res.status, 200);
assertExists(res.body);

const streamReader = res.body
  .pipeThrough(new DecompressionStream("gzip"))
  .getReader();

const denoReader = readerFromStreamReader(streamReader);
const untar = new Untar(denoReader);

for await (const entry of untar) {
  const { fileName, type } = entry;
  console.log(type, fileName);
}

$ deno run --allow-net=registry.npmjs.org ./so-71365204.ts
file package/LICENSE
file package/index.js
file package/jsx-dev-runtime.js
# etc...

The response you are streaming is compressed with gzip compression, so you need to pipe the stream data through a decompression transform stream first:

./so-71365204.ts

import {
  assertExists,
  assertStrictEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { readerFromStreamReader } from "https://deno.land/[email protected]/streams/conversion.ts";
import { Untar } from "https://deno.land/[email protected]/archive/tar.ts";

const res = await fetch("https://registry.npmjs.org/react/-/react-17.0.2.tgz");

assertStrictEquals(res.status, 200);
assertExists(res.body);

const streamReader = res.body
  .pipeThrough(new DecompressionStream("gzip"))
  .getReader();

const denoReader = readerFromStreamReader(streamReader);
const untar = new Untar(denoReader);

for await (const entry of untar) {
  const { fileName, type } = entry;
  console.log(type, fileName);
}

$ deno run --allow-net=registry.npmjs.org ./so-71365204.ts
file package/LICENSE
file package/index.js
file package/jsx-dev-runtime.js
# etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文