因为您提到这个问题出现在多个环境中(Deno、Bun、Inslenia等),所以响应似乎可能无效,但一些解析器会更优雅地处理它(例如Chrome、Node等).注意:如果没有看到您的确切回答,我无法验证这一点--而且您在问题中没有包括二进制响应数据.
在任何情况下,您都可以创建一个抽象,以便在解析过程中优雅地处理意外的文件结尾场景--您可以在异常处理代码中判断收集的数据是否满足您的条件,如果一切正常,则在该点进行恢复.下面是我在Deno和Node中测试的一个示例:
main.mjs
:
async function parseHtmlBody(response) {
const decoder = new TextDecoder();
let html = "";
try {
for await (const u8Arr of response.body) {
const str = decoder.decode(u8Arr, { stream: true });
html += str;
}
return html;
} catch (cause) {
if (
// The exception is an error:
cause instanceof Error
// And it includes the expected message text:
&& cause.message.toLowerCase().includes("unexpected end of file")
// And it looks like the end of the document was received:
&& html.trimEnd().endsWith("</html>")
) return html;
// Else, rethrow:
throw cause;
}
}
async function main() {
// The URL in your question details:
const url = "https://www.uvic.ca/cas/login";
const response = await fetch(url, { redirect: "follow" });
const html = await parseHtmlBody(response);
// Print the beginning and end of the HTML document and its length:
console.log(html.slice(0, 100));
console.log("…");
console.log(html.slice(-100));
console.log("Total length:", html.length);
}
main();
在终端中:
% node --version
v18.16.1
% node main.mjs
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8" /><meta name="viewport" content="w
…
t src="https://kit.fontawesome.com/32795de2d2.js" crossorigin="anonymous"></script>
</body>
</html>
Total length: 7236
% deno --version
deno 1.35.0 (release, aarch64-apple-darwin)
v8 11.6.189.7
typescript 5.1.6
% deno run --allow-net main.mjs
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8" /><meta name="viewport" content="w
…
t src="https://kit.fontawesome.com/32795de2d2.js" crossorigin="anonymous"></script>
</body>
</html>
Total length: 7236
这两个输出看起来是一样的.您可以修改上面的代码以满足您的需要.