“unexpected EOF during chunk size line”错误是在Deno中出现的错误。

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

What is a "unexpected EOF during chunk size line" error in Deno?

问题

我正在本地开发一个基于Supabase的边缘函数,它在Deno上运行。当我执行下面的代码时,出现以下错误:

CPU时间限制已达到。隔离: 16597602940236451129
CPU时间使用: 560毫秒
hyper::Error(User(Body), hyper::Error(Body, Custom { kind: UnexpectedEof, error: "unexpected EOF during chunk size line" }))

这是代码:

import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
import { corsHeaders } from "../_shared/cors.ts";

function fetchArtistPage(artistName: string): Promise<string> {
  return new Promise((resolve, reject) => {
    setTimeout(async () => {
      try {
        const response = await fetch("https://www.last.fm/music/" + artistName.replace(" ", "+"));
        const page = await response.text();
        resolve(page);
      } catch (error) {
        reject(error);
      }
    }, 1000);
  });
}

serve(async (req) => {
  if (req.method === "OPTIONS") return new Response("ok", { headers: corsHeaders });

  const textEncoder = new TextEncoder();
  const artistsNames = await req.json();
  const body = new ReadableStream({
    async start(controller) {
      for (const artistName of artistsNames) {
        controller.enqueue(textEncoder.encode("START_OF_JSON "));
        const artistsData = {
          name: artistName,
          page: await fetchArtistPage(artistName),
        };
        controller.enqueue(textEncoder.encode(JSON.stringify(artistsData)));
        controller.enqueue(textEncoder.encode("END_OF_JSON "));
      }
      controller.close();
    },
  });
  return new Response(body, {
    headers: {
      ...corsHeaders,
      "Content-Type": "text/event-stream",
    },
  });
});

上下文

artistsNames变量将保存一个包含最多50个字符串的数组,其中每个字符串代表一个艺术家的名字。我将获取艺术家的last.fm页面,并将其作为响应发送回来,放在artistsData对象中。

当我运行这段代码时,错误出现在第35-40个艺术家附近。如果只使用10个艺术家,错误就不会出现。我尝试了很多方法,但都没有起作用。如果只返回文本而不是页面,它可以工作。但是如果解析HTML页面并返回标签的文本内容,它就无法工作。

编辑:我尝试使用数据和事件字段,如服务器端事件文档所述,但问题没有解决。

英文:

I'm developing an Edge Function from Supabase locally. It runs on Deno. When I execute the code below, the following error appears:

CPU time limit reached. isolate: 16597602940236451129
CPU time used: 560ms
hyper::Error(User(Body), hyper::Error(Body, Custom { kind: UnexpectedEof, error: &quot;unexpected EOF during chunk size line&quot; }))

This is the code:

import { serve } from &quot;https://deno.land/std@0.168.0/http/server.ts&quot;;
import { corsHeaders } from &quot;../_shared/cors.ts&quot;;

function fetchArtistPage(artistName: string): Promise&lt;string&gt; {
  return new Promise((resolve, reject) =&gt; {
    setTimeout(async () =&gt; {
      try {
        const response = await fetch(&quot;https://www.last.fm/music/&quot; + artistName.replace(&quot; &quot;, &quot;+&quot;));
        const page = await response.text();
        resolve(page);
      } catch (error) {
        reject(error);
      }
    }, 1000);
  });
}

serve(async (req) =&gt; {
  if (req.method === &quot;OPTIONS&quot;) return new Response(&quot;ok&quot;, { headers: corsHeaders });

  const textEncoder = new TextEncoder();
  const artistsNames = await req.json();
  const body = new ReadableStream({
    async start(controller) {
      for (const artistName of artistsNames) {
        controller.enqueue(textEncoder.encode(&quot;START_OF_JSON &quot;));
        const artistsData = {
          name: artistName,
          page: await fetchArtistPage(artistName),
        };
        controller.enqueue(textEncoder.encode(JSON.stringify(artistsData)));
        controller.enqueue(textEncoder.encode(&quot;END_OF_JSON &quot;));
      }
      controller.close();
    },
  });
  return new Response(body, {
    headers: {
      ...corsHeaders,
      &quot;Content-Type&quot;: &quot;text/event-stream&quot;,
    },
  });
});

Context

The artistsNames variable will hold an array containing a maximum of 50 strings, where each string represent an artist name. I will fetch the artist last.fm page and send it back in the response inside the artistsData object.

When I run this code, the error appears near the 35-40th artist. If I use just 10 artists, the error doesn't appear. I just tried so many things and nothing works. If I just return text and not the page, it works. But if I parse the html page and return the text content of a tag, it doesn't work.

Edit: I have tried using the data and event fields as the Server Side Events documentations tells to do but I didn't solve the problem.

答案1

得分: 1

如@Bergi所提到的,真正的问题是在达到CPU时间限制后,隔离环境会被销毁,这会触发你所看到的Hyper(服务器)错误,因为服务器被关闭。

你可以在这里查看Deno Deploy的限制:https://deno.com/deploy/docs/pricing-and-limits

> 在初始公测期间,以下硬性限制适用。如果超过任何运行时限制,所有相关请求将被立即终止,并且会在部署日志中记录警告。


你可以实现一个限制/分页机制来避免超时,因为你提到当获取10个艺术家时不会出现错误。

让你的服务器在每个请求中只返回1-10个艺术家。

英文:

As @Bergi mentioned, the real issue is CPU time limit reached after which the isolate is destroyed, which triggers the Hyper (Server) error you're seeing, since the server is shutdown.

You can see the Deno Deploy limits here https://deno.com/deploy/docs/pricing-and-limits

> During the initial public beta, the following hard limits apply. If
> any runtime limits are exceeded, all related requests will be
> immediately terminated, and a warning will be logged to the
> deployment’s logs.


You could implement a limit/paging mechanism to avoid the timeout since you mention that you don't get the error when fetching 10 artists.

Make your server only return 1-10 artists on each request.

huangapple
  • 本文由 发表于 2023年8月9日 02:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862258.html
匿名

发表评论

匿名网友

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

确定