Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danieljdufour/toab
To Array Buffer: Convert Buffer, Data URLs, Files, Response, Text, and Typed Arrays to Array Buffers
https://github.com/danieljdufour/toab
arraybuffer buffer fetch files response text typedarrays
Last synced: about 2 months ago
JSON representation
To Array Buffer: Convert Buffer, Data URLs, Files, Response, Text, and Typed Arrays to Array Buffers
- Host: GitHub
- URL: https://github.com/danieljdufour/toab
- Owner: DanielJDufour
- License: cc0-1.0
- Created: 2020-09-07T02:33:29.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-21T21:22:23.000Z (over 2 years ago)
- Last Synced: 2024-09-17T00:32:47.850Z (3 months ago)
- Topics: arraybuffer, buffer, fetch, files, response, text, typedarrays
- Language: JavaScript
- Homepage:
- Size: 65.4 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# toab
To Array Buffer: Convert Buffer, Data URLs, Files, Response, Text, and Typed Arrays to Array Buffers
# install
```bash
npm install toab
```# usage
# convert file buffer to array buffer
```javascript
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
```javascript
document.querySelector('input').addEventListener('change', async event => {
const file = event.target.files[0];
const arrayBuffer = await toab(file);
});
```# convert data url to array buffer
```javascript
// 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
```javascript
const arrayBuffer = await toab(dataView)
```# convert typed arrays to array buffers
```javascript
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)
```javascript
const arrayBuffer = await toab("Hello, I'm a String.");
```# convert fetch Response to an array buffer
```javascript
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);
```