How to fix Fetch TypeError in Node.js All In One
How to fix Fetch TypeError in Node.js All In One
TypeError: terminated at Fetch.onAborted (node:internal/deps/undici/undici:11000:53)
Error
undici
// ❌❌ video url https://edu.xgqfrms.xyz/dc9bb2-1733219a4a8.mp4
TypeError: terminated
at Fetch.onAborted (node:internal/deps/undici/undici:11000:53)
at Fetch.emit (node:events:513:28)
at Fetch.terminate (node:internal/deps/undici/undici:10272:14)
at Object.onError (node:internal/deps/undici/undici:11095:36)
at Request.onError (node:internal/deps/undici/undici:6477:31)
solution
use
node-fetch
instead of Node.jsundici
fetch ❓
$ npm i node-fetch
$ npm i -D @types/node-fetch
https://www.npmjs.com/package/node-fetch
import fetch from 'node-fetch';
// fetch-polyfill.js
import fetch, {
Blob,
blobFrom,
blobFromSync,
File,
fileFrom,
fileFromSync,
FormData,
Headers,
Request,
Response,
} from 'node-fetch'
if (!globalThis.fetch) {
globalThis.fetch = fetch
globalThis.Headers = Headers
globalThis.Request = Request
globalThis.Response = Response
}
// index.js
import './fetch-polyfill'
// ...
arrayBuffer
&res.clone()
import fetch from 'node-fetch';
const response = await fetch('https://example.com', {
// About 1MB
highWaterMark: 1024 * 1024
});
const result = await res.clone().arrayBuffer();
console.dir(result);
demos
async fetchVideo(url, filepath) {
const filename = `${filepath}.mp4`;
return fetch(url)
// .then((res) => res.arrayBuffer())
.then((res) => {
console.log(`loading ... ⏳`);
return res.arrayBuffer();
})
.then((buffer) => {
console.log(`finished ✅`)
let arrayBuffer = Buffer.from(buffer)
fs.writeFile(filename, arrayBuffer, (err) => {
if (err) {
console.error(err);
console.error(`❌ video url`, url);
} else {
console.log("video downloaded successfully");
}
});
})
.catch((err) => console.error(`❌❌ video url`, url, err));
/*
❌❌ video url https://edu.xgqfrms.xyz/dc9bb2-1733219a4a8.mp4
TypeError: terminated
at Fetch.onAborted (node:internal/deps/undici/undici:11000:53)
at Fetch.emit (node:events:513:28)
at Fetch.terminate (node:internal/deps/undici/undici:10272:14)
at Object.onError (node:internal/deps/undici/undici:11095:36)
at Request.onError (node:internal/deps/undici/undici:6477:31)
at errorRequest (node:internal/deps/undici/undici:8440:17)
at TLSSocket.onSocketClose (node:internal/deps/undici/undici:7895:9)
at TLSSocket.emit (node:events:525:35)
at node:net:313:12
at TCP.done (node:_tls_wrap:587:7) {
[cause]: BodyTimeoutError: Body Timeout Error
at Timeout.onParserTimeout [as _onTimeout] (node:internal/deps/undici/undici:7839:32)
at listOnTimeout (node:internal/timers:566:11)
at process.processTimers (node:internal/timers:507:7) {
code: 'UND_ERR_BODY_TIMEOUT'
}
}
*/
}
Fetch API
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
Instance methods
- arrayBuffer()
- blob()
- clone()
- formData()
- json()
- text()
Response.clone()
Creates a clone
of a Response object.
https://developer.mozilla.org/en-US/docs/Web/API/Response/clone
The arrayBuffer()
method of the Response interface takes a Response stream
and reads it to completion.
It returns a promise
that resolves with an ArrayBuffer
.
https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer
refs
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError
https://github.com/nodejs/undici/issues/1490
https://github.com/nodejs/node/issues/45497
https://github.com/nodejs/undici/pull/1540
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17704563.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-09-15 在线文档 All In One
2022-09-15 Apple iPhone 官方原装保护壳港版与国行版区别 All In One
2021-09-15 Redux & React Hooks All In One
2020-09-15 C++ 0LL
2020-09-15 从长度为 M 的无序数组中,找出N个最小的数 All In One
2019-09-15 elm & redux
2019-09-15 约定优于配置