Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
buffer-serializer
Advanced tools
Convert JavaScript objects into Buffers and vice-versa. Serializes objects using a compact storage mechanism. Expandable to handle your own objects.
Convert nearly anything into a buffer, and then convert it back into the original object.
Does not handle values that are or contain the following:
Function
: It is impossible to recreate the scope properly for the function.Symbol
: Because one can not get a name for a symbol that's the same between two runs of the code, this can not be supported.undefined
: If it is undefined, it is assumed you do not want to write this value to the buffer.You may implement your own serialization routines to override this behavior if you require a Symbol
or Function
to be encoded in the buffer.
This is presented as an alternative to js-binary. That one requires a schema where as this one acts more like JSON.
First, include buffer-serializer
in your list of dependencies in your package.json
file. You can do this with one command.
npm install --save buffer-serializer
Next, you write some code.
var aBuffer, BufferSerializer, myThing, result, serializer;
BufferSerializer = require("buffer-serializer");
serializer = new BufferSerializer();
myThing = {
key: "value",
number: 123.456,
date: new Date(),
buffer: new Buffer(10)
}
console.log("before serialization", myThing);
aBuffer = serializer.toBuffer(myThing);
console.log("serialized", aBuffer.toString("hex"));
result = serializer.fromBuffer(aBuffer);
console.log("buffer bytes consumed", result.bufferBytes);
console.log("after serialization", result.data);
You can teach the serializer about other object types as well. To do that you need to register custom handlers to convert the data into a buffer and a second function that converts the buffer back into your object. Here's an example for a Date object.
// This example is NOT used inside the serializer.
// This code is provided only for illustrative purposes.
serializer.register("Date", function checkFn(value) {
return value instanceof Date;
}, function toBufferFn(value, bufferWriter) {
// Convert to just a Unix timestamp and call the serializer to
// change this number into a Buffer. You can, if you wish,
// return a Buffer object yourself or an array of Buffer
// objects.
serializer.toBufferInternal(value.getTime(), bufferWriter);
}, function fromBufferFn(bufferReader) {
var date, time;
// The Buffer stores a number. Convert it into a usable number.
// Then create a new Date object and set it to the Unix timestamp
// returned by `serializer.fromBuffer()`.
time = serializer.fromBufferInternal(bufferReader);
date = new Date();
date.setTime(time);
return date;
});
The returned buffer has a structure like this:
Version Record
Each data type is encoded differently. Here's a rundown of the types:
There's built in support for these objects:
Custom objects are encoded slightly differently.
Sizes are encoded in a way to try to conserve bytes. This does mean that there's a limit on the size of the data that's being encoded, but the limit is currently 2^29 bytes (500mb). The format allows for expansion at a later time.
serializer = new BufferSerializer()
Create a new instance of the serializer.
serializer.register(name, checkFn, toBufferFn, fromBufferFn)
The following objects are already supported by the library using more efficient functions. Registering any of these will not use the native serializer for the objects.
Buffer
Date
Will throw an Error
when there are invalid parameters.
result = serializer.fromBuffer(buffer)
Converts the Buffer
that's passed in back into the original object.
The result
object contains the following properties:
bufferBytes
: how many bytes in the buffer were used.data
: the deserialized value.When an object was registered with a custom handler when toBuffer()
was called, but it is not registered with that same custom handler when fromBuffer()
is invoked, this will throw an Error
.
result = serializer.fromBufferInternal(bufferReader)
Returns the deserialized version of the data. Meant to be used by the library and registered object handlers. This does not use the version number that's encoded in the record. The buffer reader object is a helper to consume bytes and track the position inside the buffer.
buffer = serializer.toBuffer(anything)
Converts anything
into a Buffer
. May use registered helpers for objects.
If an object is found and is not registered, it will be converted into a plain object. That means when converting back from a Buffer, the fromBuffer()
method will not return it to the original state.
Throws an Error
if a custom handler does not return a Buffer
.
arrayOrBuffer = serializer.toBufferInternal(anything, bufferWriter)
Returns an array of buffers or a single buffer. Used internally and can be used for custom object handlers. These buffers do not have the version number at the beginning of the record.
The buffer writer object is available to help make writing to the buffer easier.
This project is placed under an MIT License.
FAQs
Convert JavaScript objects into Buffers and vice-versa. Serializes objects using a compact storage mechanism. Expandable to handle your own objects.
The npm package buffer-serializer receives a total of 1 weekly downloads. As such, buffer-serializer popularity was classified as not popular.
We found that buffer-serializer 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
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.