Comparing version 1.1.1 to 1.2.0
116
index.js
@@ -9,3 +9,4 @@ | ||
var types = [] | ||
var encodingTypes = [] | ||
, decodingTypes = [] | ||
@@ -343,3 +344,3 @@ function encode(obj) { | ||
for (key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
if (obj.hasOwnProperty(key) && obj[key] !== undefined) { | ||
++length | ||
@@ -369,13 +370,21 @@ acc.push(encode(key)) | ||
function register(type, constructor, encode, decode) { | ||
function registerEncoder(check, encode) { | ||
assert(check, 'must have an encode function') | ||
assert(encode, 'must have an encode function') | ||
encodingTypes.push({ | ||
check: check | ||
, encode: encode | ||
}) | ||
return this | ||
} | ||
function registerDecoder(type, decode) { | ||
assert(type > 0, 'must have a type > 0') | ||
assert(constructor, 'must have a constructor') | ||
assert(decode, 'must have a decode function') | ||
assert(encode, 'must have an encode function') | ||
types.push({ | ||
type: type, | ||
constructor: constructor, | ||
encode: encode, | ||
decode: decode | ||
decodingTypes.push({ | ||
type: type | ||
, decode: decode | ||
}) | ||
@@ -386,10 +395,39 @@ | ||
function register(type, constructor, encode, decode) { | ||
assert(constructor, 'must have a constructor') | ||
assert(encode, 'must have an encode function') | ||
assert(type > 0, 'must have a type > 0') | ||
assert(decode, 'must have a decode function') | ||
function check(obj) { | ||
return (obj instanceof constructor) | ||
} | ||
function reEncode(obj) { | ||
var buf = bl() | ||
, header = new Buffer(1) | ||
header.writeInt8(type, 0) | ||
buf.append(header) | ||
buf.append(encode(obj)) | ||
return buf | ||
} | ||
this.registerEncoder(check, reEncode) | ||
this.registerDecoder(type, decode) | ||
return this | ||
} | ||
function encodeExt(obj) { | ||
var i | ||
, encoded | ||
, length = -1 | ||
, headers = [] | ||
for (i = 0; i < types.length; i++) { | ||
if (obj instanceof types[i].constructor) { | ||
encoded = types[i].encode(obj) | ||
for (i = 0; i < encodingTypes.length; i++) { | ||
if (encodingTypes[i].check(obj)) { | ||
encoded = encodingTypes[i].encode(obj) | ||
break; | ||
@@ -403,29 +441,31 @@ } | ||
if (encoded.length === 1) { | ||
// we subtract 1 because the length does not | ||
// include the type | ||
length = encoded.length - 1 | ||
if (length === 1) { | ||
headers.push(0xd4) | ||
} else if (encoded.length === 2) { | ||
} else if (length === 2) { | ||
headers.push(0xd5) | ||
} else if (encoded.length === 4) { | ||
} else if (length === 4) { | ||
headers.push(0xd6) | ||
} else if (encoded.length === 8) { | ||
} else if (length === 8) { | ||
headers.push(0xd7) | ||
} else if (encoded.length === 16) { | ||
} else if (length === 16) { | ||
headers.push(0xd8) | ||
} else if (encoded.length < 256) { | ||
} else if (length < 256) { | ||
headers.push(0xc7) | ||
headers.push(encoded.length) | ||
} else if (encoded.length < 0x10000) { | ||
headers.push(length) | ||
} else if (length < 0x10000) { | ||
headers.push(0xc8) | ||
headers.push(encoded.length >> 8) | ||
headers.push(encoded.length & 0x00ff) | ||
headers.push(length >> 8) | ||
headers.push(length & 0x00ff) | ||
} else { | ||
headers.push(0xc9) | ||
headers.push(encoded.length >> 24) | ||
headers.push((encoded.length >> 16) & 0x000000ff) | ||
headers.push((encoded.length >> 8) & 0x000000ff) | ||
headers.push(encoded.length & 0x000000ff) | ||
headers.push(length >> 24) | ||
headers.push((length >> 16) & 0x000000ff) | ||
headers.push((length >> 8) & 0x000000ff) | ||
headers.push(length & 0x000000ff) | ||
} | ||
headers.push(types[i].type) | ||
return bl().append(new Buffer(headers)).append(encoded) | ||
@@ -446,7 +486,7 @@ } | ||
for (i = 0; i < types.length; i++) { | ||
if (type === types[i].type) { | ||
for (i = 0; i < decodingTypes.length; i++) { | ||
if (type === decodingTypes[i].type) { | ||
toDecode = buf.slice(0, size) | ||
buf.consume(size) | ||
return types[i].decode(toDecode) | ||
return decodingTypes[i].decode(toDecode) | ||
} | ||
@@ -459,7 +499,9 @@ } | ||
return { | ||
encode: encode, | ||
decode: decode, | ||
register: register, | ||
encoder: streams.encoder, | ||
decoder: streams.decoder | ||
encode: encode | ||
, decode: decode | ||
, register: register | ||
, registerEncoder: registerEncoder | ||
, registerDecoder: registerDecoder | ||
, encoder: streams.encoder | ||
, decoder: streams.decoder | ||
} | ||
@@ -466,0 +508,0 @@ } |
{ | ||
"name": "msgpack5", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A msgpack v5 implementation for node.js, with extension points", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -73,2 +73,4 @@ msgpack5 [![Build Status](https://travis-ci.org/mcollina/msgpack5.png)](https://travis-ci.org/mcollina/msgpack5) | ||
* <a href="#decode"><code>msgpack().<b>decode()</b></code></a> | ||
* <a href="#registerEncoder"><code>msgpack().<b>registerEncoder()</b></code></a> | ||
* <a href="#registerDecoder"><code>msgpack().<b>registerDecoder()</b></code></a> | ||
* <a href="#register"><code>msgpack().<b>register()</b></code></a> | ||
@@ -98,4 +100,29 @@ * <a href="#encoder"><code>msgpack().<b>encoder()</b></code></a> | ||
------------------------------------------------------- | ||
<a name="registerEncoder"></a> | ||
### registerEncoder(check(obj), encode(obj)) | ||
Register a new custom object type for being automatically encoded. | ||
The arguments are: | ||
- `check`, a function that will be called to check if the passed | ||
object should be encoded with the `encode` function | ||
- `encode`, a function that will be called to encode an object in binary | ||
form; this function __must__ return a `Buffer` which include the same type | ||
for [registerDecoder](#registerDecoder). | ||
------------------------------------------------------- | ||
<a name="registerDecoder"></a> | ||
### registerDecoder(type, decode(buf)) | ||
Register a new custom objet type for being automatically decoded. | ||
The arguments are: | ||
- `type`, is a positive integer identificating the type once serialized | ||
- `decode`, a function that will be called to decode the object from | ||
the passed `Buffer` | ||
------------------------------------------------------- | ||
<a name="register"></a> | ||
### register(type, constructor, encode, decode) | ||
### register(type, constructor, encode(obj), decode(buf)) | ||
@@ -109,6 +136,11 @@ Register a new custom objet type for being automatically encoded and | ||
- `encode`, a function that will be called to encode an object in binary | ||
form | ||
form; this function __must__ return a `Buffer` that can be | ||
deserialized by the `decode` function | ||
- `decode`, a function that will be called to decode the object from | ||
binary form | ||
the passed `Buffer` | ||
This is just a commodity that calls | ||
[`registerEncoder`](#registerEncoder) and | ||
[`registerDecoder`](#registerDecoder) internally. | ||
------------------------------------------------------- | ||
@@ -115,0 +147,0 @@ <a name="encoder"></a> |
@@ -63,1 +63,11 @@ | ||
}) | ||
test('do not encode undefined in a map', function(t) { | ||
var instance = msgpack() | ||
, expected = { hello: 'world' } | ||
, toEncode = { a: undefined, hello: 'world' } | ||
, buf = instance.encode(toEncode) | ||
t.deepEqual(expected, instance.decode(buf), 'must ignore undefined') | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
117199
42
2100
174