@shasharoman/amp

1.1.1 • Public • Published

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));
});

Readme

Keywords

Package Sidebar

Install

npm i @shasharoman/amp

Weekly Downloads

19

Version

1.1.1

License

MIT

Unpacked Size

13 kB

Total Files

10

Last publish

Collaborators

  • shasharoman