Skip to content

JulienTD/simple-buffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple-Buffer

Facilitates the manipulation of buffers.

Installation

$ npm install simple-buffer

You must also activate experimentalDecorators and emitDecoratorMetadata in your tsconfig.json.
It should look similar to the one below:

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es2015"],
        "module": "commonjs",
        "strict": true,
        "declaration": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
    }
}

Getting started

First, we'll create a class containing the data we want to serialize.

class UserProfile {

    @Data(DataType.STRING, STRING_LENGTH_AUTO)
    name: string = "";

    @Data(DataType.STRING, 100)
    email: string = "";

    @Data(DataType.INT_8)
    age: number = 0;

    @Data(DataType.DOUBLE_BE)
    date: number = 0;
}

As you can see, on each field we put @Data with the specification of the data type.
Some DataType may require one or two extra arguments.
i.e.: a string has a variable length, so the lib needs to know how many bytes it can write. It can be defined by the user, or it can be automatically detected by putting the flag STRING_LENGTH_AUTO.

Once the packet class is created, we may proceed to the serialization and deserialization.

const packet = new UserProfile();
packet.name = "Lorem Ipsum";
packet.email = "lorem.ipsum@loremipsum.loremipsum";
packet.age = 42;
packet.date = Date.now();

const buffer = SimpleBuffer.serialize(packet);

const decodedPacket = SimpleBuffer.deserialize(buffer, UserProfile);

That's it, the serialization and deserialization only take 2 lines.
You can find this code here

About

Facilitates the manipulation of buffers in Node.js

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published