Fetch API res.buffer vs res.arrayBuffer All In One
error
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of
Buffer
,TypedArray
, orDataView
. Received an instance ofArrayBuffer
import fs from 'node:fs';
import path from 'path';
import fetch from "node-fetch";
// read JSON ✅ filename
const url = `https://cdn.xgqfrms.xyz/video/web-testing.mp4`
const filename = "web-testing.mp4";
const dirPath = "./abc";
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
// fetch data as a buffer
fetch(url)
// ❌
// .then((res) => res.arrayBuffer())
.then((res) => {
// console.log(`res =`, res)
console.log(`loading ... ⏳`);
return res.arrayBuffer();
})
// (node:40449) [node-fetch#buffer] DeprecationWarning: Please use 'response.arrayBuffer()' instead of 'response.buffer()'
.then((buffer) => {
console.log(`finished ✅`)
// Write the buffer to a file
// ❌`fs.writeFile` TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of ArrayBuffer
fs.writeFile(path.join(dirPath, filename), buffer, (err) => {
if (err) {
console.error(err);
} else {
console.log("video downloaded successfully");
}
});
})
.catch((err) => console.error(err));
warning ⚠️
The signature '(): Promise<Buffer>' of 'res.buffer' is deprecated.ts(6387)
index.d.ts(134, 6): The declaration was marked as deprecated here.
(method) BodyMixin.buffer(): Promise<Buffer>
@deprecated — Use body.arrayBuffer() instead.
Deprecation Warning: Please use 'response.arrayBuffer()
' instead of 'response.buffer()
'
import fs from 'node:fs';
import path from 'path';
import fetch from "node-fetch";
// read JSON ✅ filename
const url = `https://cdn.xgqfrms.xyz/video/web-testing.mp4`
const filename = "web-testing.mp4";
const dirPath = "./abc";
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
// fetch data as a buffer
fetch(url)
// ✅
// .then((res) => res.buffer())
.then((res) => {
// console.log(`res =`, res)
console.log(`loading ... ⏳`);
return res.buffer();
})
// (node:40449) [node-fetch#buffer] DeprecationWarning: Please use 'response.arrayBuffer()' instead of 'response.buffer()'
.then((buffer) => {
console.log(`finished ✅`)
// Write the buffer to a file
fs.writeFile(path.join(dirPath, filename), buffer, (err) => {
if (err) {
console.error(err);
} else {
console.log("video downloaded successfully");
}
});
})
.catch((err) => console.error(err));
Response.arrayBuffer()
function getData() {
const audioCtx = new AudioContext();
return fetch("viper.ogg")
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error, status = ${response.status}`);
}
// arrayBuffer❓需要转换处理
return response.arrayBuffer();
})
.then((buffer) => audioCtx.decodeAudioData(buffer))
.then((decodedData) => {
const source = new AudioBufferSourceNode();
source.buffer = decodedData;
source.connect(audioCtx.destination);
return source;
});
}
// wire up buttons to stop and play audio
play.onclick = () => {
getData().then((source) => {
source.start(0);
play.setAttribute("disabled", "disabled");
});
};
https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer