
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
socket-serializer
Advanced tools
A serializer of object, buffer, string, number and boolean for socket.
Optimized BufferList reader/writer, purpose is to reduce as much as possible costly buffers creation/copy/clone/split/slice operations. This BufferList reader allows as well to manage a continuous read in the same list: removing read buffers, add new ones. Currently, not all Buffer methods are supported, but contributions or suggestions welcome ;-) We are relying on native Buffer methods and do not overwrite them (except byte read/write, 10 times faster than calling ReadUInt8/WriteUInt8).
Purpose is to serialize Object, Buffer, string, Date, TypedArray, ArrayBuffer, number and boolean with the minimum of transformations in order to improve performance. We support partial/incremental unserialization that happen with socket. It is why we do not use classic serializations like BSON or protobuf. You can provide your own JSON instance.
A socket reader/writer classes uses this BufferList to ease the management of the communication. Buffer remains untouched and go through socket without any changes (copy, merge, split...). The IpcPacketBuffer is able to read a partial buffer and accumulate incoming buffers until data is completed as socket can not ensure to deliver the same buffer as the one push (see IpcPacketBufferDecoder class).
// Pseudo code for reading data on the fly
private _bufferListReader: BufferListReader;
private _packet: IpcPacketBufferList;
handleData(data: Buffer): void {
this._bufferListReader.appendBuffer(data);
while (this._packet.decodeFromReader(this._bufferListReader)) {
this.emit('packet-received', packet);
}
this._bufferListReader.reduce();
}
npm install socket-serializer
Dependencies
export class IpcPacketBuffer[List] extends IpcPacketBufferWrap {
readonly buffer: Buffer;
readonly buffers: Buffer[];
isNotValid(): boolean;
isNotComplete(): boolean;
isNull(): boolean;
isUndefined(): boolean;
isArray(): boolean;
isObject(): boolean;
isString(): boolean;
isBuffer(): boolean;
isNumber(): boolean;
isBoolean(): boolean;
// Generic serialization
serialize(data: any): void;
// Generic parsing method
parse(): any;
// Specific parsing methods
parseArrayAt(index: number): any | null;
parseArraySlice(start?: number, end?: numbers): any | null;
write(bufferWriter: Writer, data: any): void;
read(bufferReader: Reader): any;
}
specific 'parse' methods (parseBoolean, parseNumber...) returns null if it does not mach the type of the stream.
Result of serializations is in 'buffer' readonly property.
export interface Writer {
readonly buffer: Buffer;
readonly buffers: Buffer[];
readonly length: number;
writeByte(data: number): number;
writeBytes(dataArray: number[]): number;
writeUInt32(data: number): number;
writeDouble(data: number): number;
writeString(data: string, encoding?: BufferEncoding, len?: number): number;
writeBuffer(data: Buffer, sourceStart?: number, sourceEnd?: number): number;
write(writer: Writer): number;
}
export interface Reader {
readonly length: number;
readonly offset: number;
noAssert: boolean;
pushd(): number; // save current offset
popd(): number; // restore offset
checkEOF(offsetStep?: number): boolean;
skip(offsetStep?: number): boolean;
seek(offset: number): boolean;
readByte(): number;
readUInt32(): number;
readDouble(): number;
readString(encoding?: BufferEncoding, len?: number): string;
subarray(len?: number): Buffer;
subarrayList(len?: number): Buffer[];
slice(len?: number): Buffer;
reduce(): void; // released read buffers
}
BufferWriter/BufferReader works on a single buffer, internal buffer may be changed when concatening another buffer (writeBuffer)
export interface BufferWriter extends Writer {
}
export interface BufferReader extends Reader {
constructor(buffer: Buffer, offset?: number);
}
BufferListWriter accumulates intermediate buffers in a list. It concatenates them only in a single when calling buffer method. It is faster then recreating a buffer at each modification.
export interface BufferListWriter extends Writer {
}
BufferListReader is able to read value across a set of buffers without need to concat them in a single. You can add new buffer on the fly.
export interface BufferListReader extends Reader {
constructor(buffers?: Buffer[], offset?: number);
appendBuffer(buffer: Buffer): void;
reduce(): void; // allow to garbage read buffers, offset is substracted with the len of buffers removed.
}
When serializing you can write directly to socket using a SocketWriter classes. Each one has a different strategies
const socketHelpers = require('socket-port-helpers');
const socketSerialModule = require('socket-serializer');
let port = 49152;
socketHelpers.findFirstFreePort({portRange: `>=${port}`, log: false, testConnection: true })
.then((port) => {
let server = new socketSerialModule.IpcPacketNet();
server.addListener('listening', () => {
let client = new socketSerialModule.IpcPacketNet();
client.addListener('packet', (ipcPacketBuffer) => {
let paramObject = ipcPacketBuffer.parseObject();
console.log(JSON.stringify(paramObject));
});
client.addListener('error', (err) => {
});
client.connect(port);
});
server.addListener('connection', (socket) => {
const paramObject = {
num: 10.2,
str: "test",
bool: true,
array: ["", 10.2, true]
};
var ipb = new socketSerialModule.IpcPacketBuffer();
ipb.serializeObject(paramObject);
socket.write(ipb.buffer);
});
server.addListener('error', (err) => {
});
server.listen(port);
});
Copyright (c) 2021 Emmanuel Kimmerlin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A serializer of object, buffer, string, number and boolean for socket.
We found that socket-serializer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers 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.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.