Skip to content

Commit

Permalink
Merge pull request #13 from compactr/fix-readme
Browse files Browse the repository at this point in the history
fixed sample in readme
  • Loading branch information
fed135 authored Oct 16, 2017
2 parents e115eda + 505ec30 commit 8852c4e
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 11 deletions.
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ const userSchema = Compactr.schema({
userSchema.write({ id: 123, name: 'John' });

// Get the header bytes
const header = userSchema.headerBytes();
const header = userSchema.headerBuffer();

// Get the content bytes
const partial = userSchema.contentBytes();
const partial = userSchema.contentBuffer();

// Get the full payload (header + content bytes)
const buffer = userSchema.bytes();
const buffer = userSchema.buffer();



Expand Down Expand Up @@ -90,7 +90,30 @@ Compactr (partial): `<Buffer 7b 4a 6f 68 6e>`: 5 bytes

## Protocol details

[Compactr Protocol](https://github.com/compactr/protocol)
### Data types

Type | Count bytes | Byte size
--- | --- | ---
boolean | 0 | 1
number | 0 | 8
int8 | 0 | 1
int16 | 0 | 2
int32 | 0 | 4
double | 0 | 8
string | 1 | 2/char
char8 | 1 | 1/char
char16 | 1 | 2/char
char32 | 1 | 4/char
array | 1 | (x)/entry
object | 1 | (x)
unsigned | 0 | 8
unsigned8 | 0 | 1
unsigned16 | 0 | 2
unsigned32 | 0 | 4

* Count bytes range can be specified per-item in the schema*

See the full [Compactr protocol](https://github.com/compactr/protocol)


## Want to help ?
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "compactr",
"version": "2.2.0",
"version": "2.2.1",
"description": "Schema based serialization made easy",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,43 @@

/* Methods -------------------------------------------------------------------*/

/** @private */
function int8(value) {
return Number(value) & 0xff;
}

/** @private */
function int16(value) {
return Number(value) & 0xffff;
}

/** @private */
function int32(value) {
return Number(value) & 0xffffffff;
}

/** @private */
function double(value) {
const ret = Number(value);
return (Number.isFinite(ret)) ? ret : 0;
}

/** @private */
function string(value) {
return '' + value;
}

/** @private */
function boolean(value) {
return !!value;
}

/** @private */
function object(value) {
return (value.constructor === Object) ? value : {};
}

/** @private */
function array(value) {
return (value.concat !== undefined) ? value : [value];
}
Expand Down
13 changes: 12 additions & 1 deletion src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ const fromChar = String.fromCharCode;

/* Methods -------------------------------------------------------------------*/

/** @private */
function boolean(bytes) {
return !!bytes[0];
}

/** @private */
function number(bytes) {
if (bytes.length === 1) return (!(bytes[0] & 0x80))?bytes[0]:((0xff - bytes[0] + 1) * -1);
if (bytes.length === 2) {
Expand All @@ -22,12 +24,14 @@ function number(bytes) {
return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | (bytes[3]);
}

/** @private */
function unsigned(bytes) {
if (bytes.length === 1) return bytes[0];
if (bytes.length === 2) return bytes[0] << 8 | bytes[1];
return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | (bytes[3]);
}

/** @private */
function string(bytes) {
let res = [];
for (let i = 0; i < bytes.length; i += 2) {
Expand All @@ -36,6 +40,7 @@ function string(bytes) {
return fromChar.apply(null, res);
}

/** @private */
function char8(bytes) {
let res = [];
for (let i = 0; i < bytes.length; i += 1) {
Expand All @@ -44,6 +49,7 @@ function char8(bytes) {
return fromChar.apply(null, res);
}

/** @private */
function char32(bytes) {
let res = [];
for (let i = 0; i < bytes.length; i += 4) {
Expand All @@ -52,6 +58,7 @@ function char32(bytes) {
return fromChar.apply(null, res);
}

/** @private */
function array(schema, bytes) {
const ret = [];
for (let i = 0; i < bytes.length;) {
Expand All @@ -64,11 +71,15 @@ function array(schema, bytes) {
return ret;
}

/** @private */
function object(schema, bytes) {
return schema.read(bytes);
}

/** Credit to @feross' ieee754 module */
/**
* Credit to @feross' ieee754 module
* @private
*/
function double(bytes) {
let s = bytes[0];
let e = (s & 127);
Expand Down
30 changes: 26 additions & 4 deletions src/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,44 @@ const fastPush = Array.prototype.push;

/* Methods -------------------------------------------------------------------*/

/** @private */
function boolean(val) {
return [val ? 1 : 0];
}

/** @private */
function int8(val) {
return [(val < 0) ? 256 + val : val];
}

/** @private */
function int16(val) {
if (val < 0) val = 0xffff + val + 1;
return [val >> 8, val & 0xff];
}

/** @private */
function int32(val) {
if (val < 0) val = 0xffffffff + val + 1;
return [val >> 24, val >> 16, val >> 8, val & 0xff];
}

function unsigned8(val) { return [val & 0xff]; }
function unsigned16(val) { return [val >> 8, val & 0xff]; }
function unsigned32(val) { return [val >> 24, val >> 16, val >> 8, val & 0xff]; }
/** @private */
function unsigned8(val) {
return [val & 0xff];
}

/** @private */
function unsigned16(val) {
return [val >> 8, val & 0xff];
}

/** @private */
function unsigned32(val) {
return [val >> 24, val >> 16, val >> 8, val & 0xff];
}

/** @private */
function string(encoding, val) {
const chars = [];
for (let i = 0; i < val.length; i++) {
Expand All @@ -48,6 +64,7 @@ function string(encoding, val) {
return chars;
}

/** @private */
function array(schema, val) {
const ret = [];
for (let i = 0; i < val.length; i++) {
Expand All @@ -58,11 +75,15 @@ function array(schema, val) {
return ret;
}

/** @private */
function object(schema, val) {
return schema.write(val).buffer();
}

/** Credit to @feross' ieee754 module */
/**
* Credit to @feross' ieee754 module
* @private
*/
function double(val) {
var buffer = [];
var e, m, c;
Expand Down Expand Up @@ -120,6 +141,7 @@ function double(val) {
return buffer;
}

/** @private */
function getSize(count, byteLength) {
return intMap[count](byteLength);
}
Expand Down
17 changes: 17 additions & 0 deletions src/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ const Decoder = require('./decoder');

function Reader(scope) {

/**
* Decodes an encoded buffer. Requires header bytes.
* @param {Buffer} bytes
* @returns {Object} The decoded buffer
*/
function read(bytes) {
readHeader(bytes);
return readContent(bytes, scope.contentBegins);
}

/**
* Reads only the header of an encoded buffer
* @param {*} bytes
*/
function readHeader(bytes) {
scope.header = [];
let caret = 1;
Expand All @@ -27,6 +36,7 @@ function Reader(scope) {
return this;
}

/** @private */
function readKey(bytes, caret, index) {
const key = getSchemaDef(bytes[caret]);

Expand All @@ -37,12 +47,19 @@ function Reader(scope) {
return caret + key.count + 1;
}

/** @private */
function getSchemaDef(index) {
for (let i = 0; i < scope.items.length; i++) {
if (scope.indices[scope.items[i]].index === index) return scope.indices[scope.items[i]];
}
}

/**
* Reads only a content buffer and returns an object with the decoded values
* @param {Buffer} bytes The content buffer
* @param {Integer} caret The content bytes offset, if the bytes also include an header
* @returns {Object} An object with the decoded values
*/
function readContent(bytes, caret) {
caret = caret || 0;
const ret = {};
Expand Down
8 changes: 8 additions & 0 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const Converter = require('./converter');

/* Methods -------------------------------------------------------------------*/

/**
* Creates a new schema definition, with a reader and writer attached
* @param {*} schema The schema to use
* @param {Object (keyOrder: {boolean})} options The options for the schema
*/
function Schema(schema, options = { keyOrder: false }) {
const sizeRef = {
boolean: 1,
Expand Down Expand Up @@ -48,6 +53,7 @@ function Schema(schema, options = { keyOrder: false }) {

applyBlank(); // Pre-load header for easy streaming

/** @private */
function preformat(schema) {
const ret = {};
Object.keys(schema)
Expand All @@ -74,6 +80,7 @@ function Schema(schema, options = { keyOrder: false }) {
return ret;
}

/** @private */
function applyBlank() {
for (let key in scope.schema) {
scope.header.push({
Expand All @@ -83,6 +90,7 @@ function Schema(schema, options = { keyOrder: false }) {
}
}

/** @private */
function computeNested(schema, key) {
const keyType = schema[key].type;
const isObject = (keyType === 'object');
Expand Down
Loading

0 comments on commit 8852c4e

Please sign in to comment.