To Array Buffer: Convert Buffer, Data URLs, Files, Response, Text, and Typed Arrays to Array Buffers
npm install toab
const fs = require("fs");
const toab = require("toab");
const buffer = fs.readFileSync("test.png");
const arrayBuffer = await toab(buffer);
document.querySelector('input').addEventListener('change', async event => {
const file = event.target.files[0];
const arrayBuffer = await toab(file);
});
// the context comes from a canvas element
const url = context.toDataURL('image/jpeg');
// url is data:image/jpeg;base64,/9j/4AAQSkZJRgABA"
const arrayBuffer = await toab(url);
const arrayBuffer = await toab(dataView)
const arrayBuffer = await toab(int8Array)
const arrayBuffer = await toab(uint8Array)
const arrayBuffer = await toab(int16Array)
const arrayBuffer = await toab(uint16Array)
const arrayBuffer = await toab(int32Array)
const arrayBuffer = await toab(uint32Array)
const arrayBuffer = await toab(float32Array)
const arrayBuffer = await toab(float64Array)
const arrayBuffer = await toab(bigInt64Array)
const arrayBuffer = await toab(bigUint64Array)
const arrayBuffer = await toab("Hello, I'm a String.");
const response = await fetch("https://example.org/file.dat");
const arrayBuffer = await toab(response);
// or for a one-line solution
const arrayBuffer = await fetch("https://example.org/file.dat").then(toab);