
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
serial-stream
Advanced tools
This is basically DataInputStream/BinaryReader and DataOutputStream/BinaryWriter for Node, using Promises.
const { SerialStreamWriter } = require('serial-stream');
const stream = <Some Stream>;
const writer = new SerialStreamWriter(stream);
writer.writeInt32LE(123);
writer.writeDoubleLE(123.45);
writer.writeString("Hello World");
writer.writeJson({text: "Hello World"});
When writing more complex data, you may wish to write a size header first.
const array = [1,2,3,4,5];
writer.writeUInt32LE(array.length);
array.forEach((v) => writer.writeDoubleLE(v));
Each read function can be called with a callback, or used as a promise. The value returned from the callback function will override what is resolved by the promise.
const { SerialStreamReader } = require('serial-stream');
const stream = <Some Stream>;
const reader = new SerialStreamReader(stream);
reader.readInt32LE((v) => console.log(v));
console.log(await reader.readDoubleLE());
console.log(await reader.readString((str) => str.toUpperCase())) // "HELLO WORLD"
const obj = await reader.readJson();
console.log(obj.text) // "Hello World"
When decoding complex data, it may be impossible to predict what read commands to issue until you begin reading data. In these cases, these read commands must take priority over anything previously queued up.
Promises are always resolved asyncronously, even when the data is immediately available. This makes it impossible to tell when a read command is issued as a result of another read.
Callbacks are resolved immediately when data is available. This allows the library to detect if a read command is issued as a result of another read, and prioritize it ahead of other reads.
const length = reader.readUInt32LE;
const promises = new Array(length);
for (var i = 0; i < length; i++) {
promises[i] = reader.readDoubleLE();
}
Promise.all(promises).then((array) => console.log(array)) // [1,2,3,4,5]
write and read are meant to send and receive buffers as-is. write takes
a buffer, and sends it as-is. read takes an integer and returns a buffer of
that size.
writeBuffer and readBuffer are meant to send and receive buffers with size
headers. writeBuffer takes a buffer and sends the length followed by the
bytes. readBuffer reads the size parameter from the stream, then reads that
size of bytes from the stream, and returns a buffer with those bytes.
See Wikipedia: Endianness for more details. What's important is to be consistent. When in doubt, use LE functions.
FAQs
Read and write from streams in a serial fashion
We found that serial-stream 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.