JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 238
  • Score
    100M100P100Q84985F
  • License CC0-1.0

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

Package Exports

  • toab
  • toab/toab.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (toab) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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