
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
bytebuffer
Advanced tools
The bytebuffer npm package is a utility for working with binary data in JavaScript. It provides a comprehensive API for reading and writing various data types to and from byte buffers, making it useful for tasks such as serialization, deserialization, and network communication.
Creating a ByteBuffer
This feature allows you to create a new ByteBuffer instance. ByteBuffers are used to manage binary data efficiently.
const ByteBuffer = require('bytebuffer');
const bb = new ByteBuffer();
Writing Data to a ByteBuffer
This feature allows you to write various data types to a ByteBuffer. In this example, an integer and a float are written to the buffer.
const ByteBuffer = require('bytebuffer');
const bb = new ByteBuffer();
bb.writeInt32(123456);
bb.writeFloat32(3.14);
Reading Data from a ByteBuffer
This feature allows you to read various data types from a ByteBuffer. In this example, an integer and a float are read from the buffer after writing.
const ByteBuffer = require('bytebuffer');
const bb = new ByteBuffer();
bb.writeInt32(123456);
bb.writeFloat32(3.14);
bb.flip();
const intVal = bb.readInt32();
const floatVal = bb.readFloat32();
Handling Endianness
This feature allows you to specify the endianness (byte order) of the ByteBuffer. In this example, the buffer is set to little-endian mode.
const ByteBuffer = require('bytebuffer');
const bb = new ByteBuffer(ByteBuffer.LITTLE_ENDIAN);
bb.writeInt32(123456);
bb.flip();
const intVal = bb.readInt32();
Converting ByteBuffer to ArrayBuffer
This feature allows you to convert a ByteBuffer to an ArrayBuffer, which can be useful for interoperability with other APIs that use ArrayBuffers.
const ByteBuffer = require('bytebuffer');
const bb = new ByteBuffer();
bb.writeInt32(123456);
const arrayBuffer = bb.toArrayBuffer();
The buffer package is a Node.js core module that provides a way of handling binary data. It is similar to bytebuffer in that it allows for reading and writing various data types, but it is more integrated into the Node.js ecosystem and is generally more performant.
The protobufjs package is a library for working with Protocol Buffers, a binary serialization format. While it offers more advanced serialization capabilities compared to bytebuffer, it is more complex and specifically designed for Protocol Buffers.
The dataview package provides a DataView implementation for Node.js, allowing for low-level manipulation of ArrayBuffers. It is similar to bytebuffer in terms of functionality but is more low-level and requires more manual management of buffers.
Provides a Java-like ByteBuffer implementation using typed arrays. It also tries to abstract the complexity away by providing convenience methods for those who just want to write stuff without caring about signed, unsigned and the actual bit sizes. It's also used for the cross-platform multiplayer component in eSoccer, a HTML5 game developed at University of Applied Sciences Bonn.
Mimics Java ByteBuffers as close as reasonable while using typed array terms
Simple allocation (new ByteBuffer(capacity[, littleEndian])
or ByteBuffer.allocate(capacity[, littleEndian])
)
Wrapping of quite everything which is or includes an ArrayBuffer (ByteBuffer.wrap(buffer[, littleEndian])
)
Cloning using the same (ByteBuffer#clone()
) and copying using an independent backing buffer (ByteBuffer#copy()
)
Slicing using the same (ByteBuffer#slice(begin, end)
) and using an indepentent backing buffer (ByteBuffer#sliceAndCompact(begin, end)
)
Manual offset (ByteBuffer#offset
and ByteBuffer#length
) and array manipulation (ByteBuffer#array
)
Remaining readable bytes (ByteBuffer#remaining()
) and backing buffer capacity getters (ByteBuffer#capacity()
)
Explicit (ByteBuffer#resize(capacity)
) and implicit resizing (ByteBuffer#ensureCapacity(capacity)
)
Efficient implicit resizing by doubling the current capacity
Flipping (ByteBuffer#flip()
) and resetting (ByteBuffer#reset()
) like known from Java ByteBuffers
Compacting of the backing buffer (ByteBuffer#compact()
)
Conversion to ArrayBuffer (ByteBuffer#toArrayBuffer([forceCopy])
) (i.e. to send data over the wire, e.g. a WebSocket
with binaryType="arraybuffer"
)
Explicit destruction (ByteBuffer#destroy()
)
ByteBuffer#writeUint/Int8/16/32(value[, offset])
and ByteBuffer#readUint/Int8/16/32([offset])
ByteBuffer#writeFloat32/64(value[, offset])
and ByteBuffer#readFloat32/64([offset])
ByteBuffer#write/readByte
, ByteBuffer#write/readShort
, ByteBuffer#write/readInt
, ByteBuffer#write/readLong
(all signed), ByteBuffer#write/readFloat
, ByteBuffer#write/readDouble
aliases for the above for convenience
ByteBuffer#writeUTF8String(str[, offset])
and ByteBuffer#readUTF8String(chars[, offset])
using the included UTF8
en-/decoder (full 6 bytes, ref)
ByteBuffer#writeLString(str[, offset]))
and ByteBuffer#readLString([offset])
to write respectively read a
length-prepended (number of characters as UTF8 char) string (recommended over ByteBuffer#write/readCString
, which
would break in the case of contained NULL characters)
ByteBuffer#writeCString(str[, offset])
and ByteBuffer#readCString([offset])
to write respectively read a
NULL-terminated (Uint8 0x00) string
ByteBuffer#writeJSON(data[, offset[, stringify]])
and ByteBuffer#readJSON([offset[, parse]])
to write respectively
read arbitraty object data. Allows overriding the default stringify (default: JSON.stringify) and parse (default:
JSON.parse) implementations.
All with implicit offset advance if the offset parameter is omitted or without, if specified
Chaining of all operations that allow this (i.e. do not return some specific value like in read operations), e.g.
var bb = new ByteBuffer();
...
bb.reset().writeInt(1).writeLString("Hello world!").flip().compact()...
ByteBuffer#toString()
, ByteBuffer#toHex([wrap])
and ByteBuffer#printDebug()
for easy debugging
npm install bytebuffer
var ByteBuffer = require("bytebuffer");
var bb = new ByteBuffer();
bb.writeLString("Hello world!");
bb.flip();
console.log(bb.readLString()+" from ByteBuffer.js");
<script src="//raw.github.com/dcodeIO/ByteBuffer.js/master/ByteBuffer.min.js"></script>
var ByteBuffer = dcodeIO.ByteBuffer;
var bb = new ByteBuffer();
bb.writeLString("Hello world!");
bb.flip();
alert(bb.readLString()+" from ByteBuffer.js");
var ByteBuffer = require("/path/to/ByteBuffer.js");
var bb = new ByteBuffer();
bb.writeLString("Hello world!");
bb.flip();
alert(bb.readLString()+" from ByteBuffer.js");
jsdoc -c jsdoc.json README.md
(you'll need to comment out the eSoccer template in jsdoc.json)npm install -g nodeunit
nodeunit tests/suite.js
Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html
FAQs
The swiss army knife for binary data in JavaScript.
The npm package bytebuffer receives a total of 248,170 weekly downloads. As such, bytebuffer popularity was classified as popular.
We found that bytebuffer 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
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.