Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ language: node_js

matrix:
include:
- node_js: "0.12"
env: BROWSER=1
- node_js: "0.10"
- node_js: "0.12"
- node_js: "4"
- node_js: "5"
- node_js: "6"
- node_js: "7"
- node_js: "8"
- node_js: "9"
- node_js: "10"
- node_js: "11"

env:
global:
- CXX=g++-4.8
- secure: P5H38v7QLRCYOWZkDKkl5ubJK0WxaAeVPDqbRlZDXbzEthjNf/rVMF6vnjyssbgbujTaU7xJmnVXKxjKtwAZXRrv3D9pmqqAOXdBNxaIs+Y05exA0pPBfBQNxVWKx6jbCoW7a12yWBreOAVd7sqjT86Plr2LsLM72BLaSl2v+18wOAnxYPFdDTEU17DegDUeQfmt+qFVNIUfuLcHyuiNFnLisWjhJmm3JmcjBL2WuSj2JC5Qll/zr7ezZns8qzqXqFsRRkLie4nVERV0kXvZRH5t5iB9444vwXhkVcOa2HSH+aVEDXoRVkj7JLmvYBGay/DKrGUDST91f7d5DpNYwZYNi5YHfpySaEEM8RDd+TM7oHr2YeWykqjBv4lfqVq3t9WFpC3YvLujaTNtNh01P24jvx81/XCKYWtYmBeGD7I8lVmFbOt7JNRSvfz2kP5qT47AIj3bpSBfouhntOVPkFz+e3pvK4w8wBBOLhRgXaFJZnE5IK5CRqNuEps/dBmAk5ANwoTewtLXmr2pJXVBBKmaj6E/1EmOb3KBC8F5yqid4UWJMgGK9g+QtY24mYfkIB5mldK5t9sePtNxkEqfxPO22IS7CEydr/LCB6g5zftcy7WN28DhCr3UbAC9Ydsa1k8LFsgznQ0sfyxOi/8c6lOdCLR947elLy5tAaemi20=
- secure: Lqm+pi1xprYlJurnEGmShpcohkWh7BwT2WvZXbfTwFH4K1BQIzbTbmhdkBTpfrJzF05q0zUGvYOTOpmz4K0JKsJfCdrjjEkpsZxTMrOvJG/C0aDLAsaiOtXTNA4unjL4qlaOA82btry6ATOsxYDWyYtjQKBUJvFEYgjBUkLarWxywU7cw8OcL4ICfHjFVA4ODvOmxUQ9ESCc1E6JkacjyprVx5gmrzEfN0JL6wcaXewt7prx02U5iDFONkntpx8VEhTwBGM2nWGzbPgiuRfOV879YXkPPWUNJc/P1Q8r8T3hvFGmg2b1x8aNZ60ir64ArXannsQhJB3rZ15rp5cAptuhMAMuuc+aF0z0VIDVStSNsLxWa/u6pH8ctMvHokrguFUSpQpduOdke/B4y719fRLUS3IW98Z10+dw0LQUn1TDmryK4ELy0IDLYPNeJJ0NoaUCv7a6OT22II085iAjGw+vMaiTm/3MRIe4kjIg2jJaCsYUZ1VFW9O/0aqtUpjuHmLg0cAxfrjBW/+sexhfO3/Ea6NCmTjaC9T7fB5YzCiEvYCHRoDUj1aloYvMBog8eE0BvDLCtbwqGBIwa5G9TEfu1ywJLQPmRXf1U1c5HUvUc+P82XOX1fs0AeeskHRSkIBLkuzYXQNcp6hU3Iy1/FOClBcAdHSi3OExJdSFHHo=


addons:
apt:
sources:
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# msgpack-lite [![npm version](https://badge.fury.io/js/msgpack-lite.svg)](http://badge.fury.io/js/msgpack-lite) [![Build Status](https://travis-ci.org/kawanet/msgpack-lite.svg?branch=master)](https://travis-ci.org/kawanet/msgpack-lite)
# msgpack-lite [![npm version](https://badge.fury.io/js/msgpack-lite.svg)](http://badge.fury.io/js/msgpack-lite) [![Build Status](https://travis-ci.com/algorand/msgpack-lite.svg?branch=master)](https://travis-ci.com/algorand/msgpack-lite)

Fast Pure JavaScript MessagePack Encoder and Decoder

Expand Down Expand Up @@ -379,6 +379,12 @@ MessagePack map elements.
var codec = msgpack.createCodec({usemap: true});
```

`canonical`: Encodes object's keys in alphabetical order to ensure canonical representation.

```js
var codec = msgpack.createCodec({canonical: true});
```

### Compatibility Mode

The [compatibility mode](https://github.com/kawanet/msgpack-lite/issues/22) respects for [msgpack's old spec](https://github.com/msgpack/msgpack/blob/master/spec-old.md). Set `true` to `useraw`.
Expand Down
4 changes: 2 additions & 2 deletions dist/msgpack.min.js

Large diffs are not rendered by default.

21 changes: 12 additions & 9 deletions lib/write-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function getWriteType(options) {
var bin = binarraybuffer ? bin_arraybuffer : bin_buffer;
var usemap = HAS_MAP && options && options.usemap;
var map = usemap ? map_to_map : obj_to_map;
var canonical = options && options.canonical;

var writeType = {
"boolean": bool,
Expand All @@ -52,29 +53,31 @@ function getWriteType(options) {
}

function number(encoder, value) {
var ivalue = value | 0;
var type;
if (value !== ivalue) {
// if float of not safe integer, encode as float64
if (Math.floor(value) !== value || !Number.isSafeInteger(value)) {
// float 64 -- 0xcb
type = 0xcb;
token[type](encoder, value);
return;
} else if (-0x20 <= ivalue && ivalue <= 0x7F) {
} else if (-0x20 <= value && value <= 0x7F) {
// positive fixint -- 0x00 - 0x7f
// negative fixint -- 0xe0 - 0xff
type = ivalue & 0xFF;
} else if (0 <= ivalue) {
type = value & 0xFF;
} else if (0 <= value) {
// uint 8 -- 0xcc
// uint 16 -- 0xcd
// uint 32 -- 0xce
type = (ivalue <= 0xFF) ? 0xcc : (ivalue <= 0xFFFF) ? 0xcd : 0xce;
// uint 64 -- 0xcf
type = (value <= 0xFF) ? 0xcc : (value <= 0xFFFF) ? 0xcd : (value <= 0xFFFFFFFF) ? 0xce : 0xcf;
} else {
// int 8 -- 0xd0
// int 16 -- 0xd1
// int 32 -- 0xd2
type = (-0x80 <= ivalue) ? 0xd0 : (-0x8000 <= ivalue) ? 0xd1 : 0xd2;
// int 64 -- 0xd3
type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : (-0x80000000 <= value) ? 0xd2 : 0xd3;
}
token[type](encoder, ivalue);
token[type](encoder, value);
}

// uint 64 -- 0xcf
Expand Down Expand Up @@ -228,7 +231,7 @@ function getWriteType(options) {
// map 16 -- 0xde
// map 32 -- 0xdf
function obj_to_map(encoder, value) {
var keys = Object.keys(value);
var keys = canonical ? Object.keys(value).sort() : Object.keys(value);
var length = keys.length;
var type = (length < 16) ? (0x80 + length) : (length <= 0xFFFF) ? 0xde : 0xdf;
token[type](encoder, length);
Expand Down
9 changes: 6 additions & 3 deletions test/10.encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,24 @@ function run_tests(codecopt) {
// uint 8 -- 0xcc
// uint 16 -- 0xcd
// uint 32 -- 0xce
// uint 64 -- 0xcf -- NOT SUPPORTED
// uint 64 -- 0xcf
it("cc-cf: uint 8/16/32/64", function() {
assert.deepEqual(toArray(msgpack.encode(0xFF, options)), [0xcc, 0xFF]);
assert.deepEqual(toArray(msgpack.encode(0xFFFF, options)), [0xcd, 0xFF, 0xFF]);
assert.deepEqual(toArray(msgpack.encode(0x7FFFFFFF, options)), [0xce, 0x7F, 0xFF, 0xFF, 0xFF]);
assert.deepEqual(toArray(msgpack.encode(Number.MAX_SAFE_INTEGER, options)), [0xcf, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);

});

// int 8 -- 0xd0
// int 16 -- 0xd1
// int 32 -- 0xd2
// int 64 -- 0xd3 -- NOT SUPPORTED
// int 64 -- 0xd3
it("d0-d3: int 8/16/32/64", function() {
assert.deepEqual(toArray(msgpack.encode(-0x80, options)), [0xd0, 0x80]);
assert.deepEqual(toArray(msgpack.encode(-0x8000, options)), [0xd1, 0x80, 0x00]);
assert.deepEqual(toArray(msgpack.encode(-0x80000000, options)), [0xd2, 0x80, 0x00, 0x00, 0x00]);
assert.deepEqual(toArray(msgpack.encode(Number.MIN_SAFE_INTEGER, options)), [0xd3, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]);
});

// str 8 -- 0xd9
Expand Down Expand Up @@ -215,4 +218,4 @@ function concat(buf) {
}

function NOP() {
}
}
29 changes: 29 additions & 0 deletions test/19.canonical.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env mocha -R spec

var assert = require("assert");
var msgpackJS = "../index";
var isBrowser = ("undefined" !== typeof window);
var msgpack = isBrowser && window.msgpack || require(msgpackJS);
var TITLE = __filename.replace(/^.*\//, "");

function toArray(array) {
if (array instanceof ArrayBuffer) array = new Uint8Array(array);
return Array.prototype.slice.call(array);
}

describe(TITLE, function () {
var options = {codec: msgpack.createCodec({canonical: true})};
it("canonical (encode)", function () {
var a = {"b": 1, "a": 2};
var b = {"a": 2, "b": 1};
var encoded = msgpack.encode(a, options);
assert.deepEqual(toArray(encoded), toArray(msgpack.encode(b, options)));
});

it("canonical (nested)", function () {
var a = {"b": 1, "a": {"d": 3, "c": 4}};
var b = {"a": {"c": 4, "d": 3}, "b": 1};
var encoded = msgpack.encode(a, options);
assert.deepEqual(toArray(encoded), toArray(msgpack.encode(b, options)));
});
});