
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
arraybufferview
Advanced tools
Provides a low-level interface for reading and writing structured data in a binary ArrayBuffer.
Provides a low-level interface for reading and writing structured data in a binary ArrayBuffer.
From NPM:
import ArrayBufferView from 'arraybufferview';
From CDN:
<script src="http://unpkg.com/arraybufferview/dist/arraybufferview.js"></script>
You can create a buffer and fill it with structed data.
const struct = {
a: 1,
b: 5,
c: 2,
};
const buffer = new ArrayBuffer(100);
const view = new ArrayBufferView(struct, buffer);
view.fill({a: 1, b: 12, c: 3});
const data0 = view.get(0);
console.log(data0.b); // 12
console.log(data0.c); // 3
Use ArrayBufferView to manipulate binary data such as blob or image data.
function loadImage(url) {
const img = new Image();
img.crossOrigin = 'anonymous';
return new Promise((resolve) => {
img.onload = function () {
resolve(img);
};
img.src = url;
});
}
(async function () {
const src = 'https://p1.ssl.qhimg.com/t011f60e5399df3d7a6.png';
const img = await loadImage(src);
const canvas = document.getElementById('app');
const context = canvas.getContext('2d');
context.drawImage(img, 0, 0, img.width, img.height);
const imageData = context.getImageData(0, 0, img.width, img.height);
const buffer = imageData.data.buffer;
const struct = {
r: 8,
g: 8,
b: 8,
a: 8,
};
const view = new ArrayBufferView(struct, buffer);
// make image color black an white
view.forEach(({r, g, b, a}, i, view) => {
view.set(i, {
r: (r + g + b) / 3,
g: (r + g + b) / 3,
b: (r + g + b) / 3,
a,
});
});
context.putImageData(imageData, 0, 0);
}());
The constructor of the ArrayBufferView.
const structure = {
a: 1, // property a is 1 bitwidth.
b: 3, // property b is 3 bitwidth.
c: 4, // property c is 4 bitwidth,
d: Number, // property d is a number type
e: Boolean, // property e is a boolean type
}
// create an array buffer with 1000 structured data items.
const view = new ArrayBufferView(structure, 1000);
Set data to ArrayBufferView.
view.set(2, {
a: 0,
c: 0xF,
d: 3.14,
e: true,
})
Get data from ArrayBufferView.
The forEach() method executes a provided function once for each structured data blocks.
const view = new ArrayBufferView(structure, 1000);
view.forEach((data, index, view) => {
data.x *= 2;
view.set(index, data);
});
Fill each structed data blocks with specified data.
const buffer = imageData.data.buffer;
const struct = {
r: 8,
g: 8,
b: 8,
a: 8,
};
const view = new ArrayBufferView(struct, buffer);
view.fill({r: 0}); // red channel set to zero.
MIT
FAQs
Provides a low-level interface for reading and writing structured data in a binary ArrayBuffer.
The npm package arraybufferview receives a total of 4 weekly downloads. As such, arraybufferview popularity was classified as not popular.
We found that arraybufferview demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.