0.2.6 • Published 7 years ago
encoded-buffer v0.2.6
Encoded-Buffer
Encode data into a well-formatted buffer and decode it when needed.
Example
const { encode, decode } = require("encoded-buffer");
let buf = encode( // encode data
"string",
12345,
Symbol("desc"),
/regexp/,
["a", "r", "r", "a", "y"],
{type: "object"},
null,
undefined,
Buffer.from("or a buffer"),
new Error("even an error"),
new Date()
true
);
console.log(buf);
console.log(buf.toString());
let data = decode(buf); // decode data
console.log(data); // decoded data is an array, or null when failed.Buffer Format
When data is encoded, it will be formatted with a leading character and number
that indicates the original type and the byte length of the data, and
every part of the data is separated by ;. the style would seemed like this:
s:6:string;n:5:12345;S:12:Symbol(desc);r:8:/regexp/;...
Array will be enwrapped with [], and objects will be enwrapped with {},
and they are encoded recursively.
This map shows the representations of supported types:
a => Arrayarrays will be encoded recursively.b => booleanbooleans are encoded either as 1 or empty buffer.B => Bufferbuffers will keep the original form.d => Dateencode the ISO string, when decode, generate a new instance.e => Errorencode all properties in an object, when decode, generate a new instance according to the name, message, stack and any other properties.f => functionfunctions cannot be encoded, so treated asvoid.n => numberencoded as string.o => objectobjects will be encoded recursively.r => RegExpencoded as string.s => stringbuffered as what it is, special characters like\r,\n, etc. should be escaped.S => symbolencoded as string, when decode, generate a new symbol.u => undefinedencoded as0x0.v => voidfornull, encoded as0x0.
Purpose
The major purpose of the this module is meant to encode data into a well-formatted buffer, so that it could be transferred via the Internet or other materials, and decoded automatically by the program.