Skip to content

encode message to buffer and decode buffer to message

Notifications You must be signed in to change notification settings

shasharoman/amp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AMP

AMP is for encode message to buffer and decode buffer to message.

Examples

json mode

const amp = require('amp');

let parser = new amp.Parser({
    mode: 'json'
});
let bufferMsgs = parser.encode({
    msg: 'msgA'
}, {
    msg: 'msgB' 
});
let msgs = parser.decode(bufferMsgs);

console.log(msgs); // [{msg:'msgA'}, {msg: 'msgB'}]

string mode

const amp = require('amp');

let parser = new amp.Parser({
    mode: 'string'
});
let bufferMsgs = parser.encode('msgA', 'msgB');
let msgs = parser.decode(bufferMsgs);

console.log(msgs); // ['msgA', 'msgB']

buffer mode

const amp = require('amp');

let parser = new amp.Parser({
    mode: 'buffer'
});
let bufferMsgs = parser.encode(Buffer.from('msgA'), Buffer('msgB'));
let msgs = parser.decode(bufferMsgs);

console.log(msgs); // [<Buffer...>, <Buffer...>]

stream with parser

const amp = require('amp');

let inner = new amp.Parser({
    mode: 'json'
});
let parser = new amp.ChunkParser(inner);

let msgs = ['string', ['array'], {
    json: true
}];

parser.on('msg', msg => {
    console.log(msg); // 'string' ['array'] {json: true}
});
msgs.forEach(item => {
    parser.write(inner.encode(item));
});

About

encode message to buffer and decode buffer to message

Resources

Stars

Watchers

Forks

Packages

No packages published