Home

Awesome

Bytewise Codec npm

Build Status Code Climate Test Coverage downloads license

A binary string serialization which sorts bytewise for arbitrarily complex data structures, respecting bytewise structured sorting efficiently.

Purpose

Supported Structures

This is the top level order of the various structures that may be encoded:

These specific structures can be used to serialize the vast majority of javascript values in a way that can be sorted in an efficient, complete and sensible manner. Each value is prefixed with a type tag(see above), and we do some bit munging to encode our values in such a way as to carefully preserve the desired sort behavior, even in the precense of structural nested.

For example, negative numbers are stored as a different type from positive numbers, with its sign bit stripped and its bytes inverted(xor) to ensure numbers with a larger magnitude come first. Infinity and -Infinity can also be encoded -- they are nullary types, encoded using just their type tag. The same can be said of null and undefined, and the boolean values false, true. Date instances are stored just like Number instances -- but as in IndexedDB -- Date sorts before Number . Buffer data can be stored in the raw, and is sorted before String data. Then come the collection types -- Array and Object, along with the additional types defined by es6: Map and Set. We can even serialize Function values and revive them in an isolated Secure ECMAScript context where they are powerless to do anything but calculate.

Unsupported Structures

This serialization accomodates a wide range of javascript structures, but it is not exhaustive. Objects or arrays with reference cycles cannot be serialized. NaN is also illegal anywhere in a serialized value -- its presense very likely indicates of an error, but more importantly sorting on NaN is nonsensical by definition. (Similarly we may want to reject objects which are instances of Error.) Invalid Date objects are also illegal. Since WeakMap and WeakSet objects cannot be enumerated they are impossible to serialize. Attempts to serialize any values which include these structures should throw a TypeError.

Usage

The bytewise is registered to buffer-codec.

bytewise.encode serializes any supported type and returns a encoded string, or throws if an unsupported structure is passed:


var Codec = require("buffer-codec-bytewise")
var bytewise = Codec("bytewise")
var assert = require('assert');

// Helper to encode
function encode(value) { return bytewise.encode(value) }

  // Many types can be respresented using only their type tag, a single byte
  // WARNING type tags are subject to change for the time being!
     assert.equal(encode(null), ' ')
      assert.equal(encode(false), 'F')
      assert.equal(encode(true), 'T')
      assert.equal(encode(undefined), '~')

      assert.equal(encode(-Infinity), 'N0')
      assert.equal(encode(Infinity), 'N9')
      # Serialization does not preserve the sign bit, so 0 is indistinguishable from -0
      assert.equal(encode(-0), 'Ni000000000');
      assert.equal(encode(0), 'Ni000000000');
      # Int32 Numbers are stored in 11 bytes -- 2 chars(Ni) for the type tag and 1 char for the sign
      # and lefts is 8 chars hex string.
      assert.equal(encode(12345), 'Ni000003039')
      # Int32 Negative numbers are stored as positive numbers,
      # but the sign tag is "-" and their bits inverted
      assert.equal(encode(-12345), 'Ni-ffffcfc7')
      #floating point or integer greater than MaxUInt32, are stored as IEEE 754 doubles
      # and the sub type tag is 'f', stored in 20 bytes
      assert.equal(encode(1.2345), 'Nf03ff3c083126e978d')
      assert.equal(encode(-1.2345), 'Nf-c00c3f7ced916872')

      assert.equal(encode(4294967318), 'Nf041f0000001600000')
      assert.equal(encode(-4294967318), 'Nf-be0ffffffe9fffff')


      assert.equal(encode(new Date('2014-01-31T16:00:00.000Z')), 'D042743e9073400000')
      assert.equal(encode(new Date('-002014-01-31T16:00:00.000Z')), 'D-bd236a1e7c71ffff')

      assert.equal(encode("hi world"), '"hi world"')
      assert.equal(encode(function () {}), 'function () {}')
      fn = function (x,y) {return [x,y]}
      assert.equal(encode(fn), fn.toString())
      assert.equal(encode(new Buffer([1,2,3,4,5,6,7,8])), 'B0102030405060708')
      expected = [12345, 'good:\nhi,u.', new Date("2014-01-31T16:00:00.000Z"), 1.2345, new Buffer([1,2,3,4,5,6,7,8])]
      assert.equal(encode(expected), '[Ni000003039,"good%3a\\nhi%2cu.",D042743e9073400000,Nf03ff3c083126e978d,B0102030405060708]')
      expected = {
        num:12345,
        str:'good:\nhi,u.',
        date:new Date('2014-01-31T16:00:00.000Z'),
        float:1.2345,
        buf:new Buffer([1,2,3,4,5,6,7,8])
      }
      assert.equal(encode(expected), '{buf:B0102030405060708,date:D042743e9073400000,float:Nf03ff3c083126e978d,num:Ni000003039,str:"good%3a\\nhi%2cu."}')