英文:
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: "unexpected EOF during chunk size line" }))
This is the code:
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",
},
});
});
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论