toab
TypeScript icon, indicating that this package has built-in type declarations

2.3.0 • Public • Published

toab

To Array Buffer: Convert Buffer, Data URLs, Files, Response, Text, and Typed Arrays to Array Buffers

install

npm install toab

usage

convert file buffer to array buffer

const fs = require("fs");
const toab = require("toab");

const buffer = fs.readFileSync("test.png");
const arrayBuffer = await toab(buffer);

convert File to an array buffer

document.querySelector('input').addEventListener('change', async event => {
    const file = event.target.files[0];
    const arrayBuffer = await toab(file);
});

convert data url to array buffer

// 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);

convert data view to array buffer

const arrayBuffer = await toab(dataView)

convert typed arrays to array buffers

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)

convert text to an array buffer (in UTF-8)

const arrayBuffer = await toab("Hello, I'm a String.");

convert fetch Response to an array buffer

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);

Package Sidebar

Install

npm i toab

Weekly Downloads

883

Version

2.3.0

License

CC0-1.0

Unpacked Size

12.3 kB

Total Files

5

Last publish

Collaborators

  • danieljdufour