2019-04-18 21:32:38 +00:00
|
|
|
// Copyright IBM Corp. 2016,2019. All Rights Reserved.
|
2016-10-19 11:14:55 +00:00
|
|
|
// Node module: loopback-connector
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-08-02 13:50:51 +00:00
|
|
|
const createPromiseCallback = require('./utils').createPromiseCallback;
|
|
|
|
const msgpack = require('msgpack5');
|
2016-10-19 11:14:55 +00:00
|
|
|
|
|
|
|
module.exports = BinaryPacker;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new Packer instance that can be used to convert between JavaScript
|
|
|
|
* objects and a binary representation in a Buffer.
|
|
|
|
*
|
|
|
|
* Compared to JSON, this encoding preserves the following JavaScript types:
|
|
|
|
* - Date
|
|
|
|
*/
|
|
|
|
function BinaryPacker() {
|
2017-03-06 23:40:47 +00:00
|
|
|
this._packer = msgpack({forceFloat64: true});
|
2016-10-19 11:14:55 +00:00
|
|
|
this._packer.register(1, Date, encodeDate, decodeDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode the provided value to a `Buffer`.
|
|
|
|
*
|
|
|
|
* @param {*} value Any value (string, number, object)
|
|
|
|
* @callback {Function} cb The callback to receive the parsed result.
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Buffer} data The encoded value
|
|
|
|
* @promise
|
|
|
|
*/
|
|
|
|
BinaryPacker.prototype.encode = function(value, cb) {
|
|
|
|
cb = cb || createPromiseCallback();
|
|
|
|
try {
|
|
|
|
// msgpack5 returns https://www.npmjs.com/package/bl instead of Buffer
|
|
|
|
// use .slice() to convert to a Buffer
|
2019-08-02 13:50:51 +00:00
|
|
|
const data = this._packer.encode(value).slice();
|
2016-10-19 11:14:55 +00:00
|
|
|
setImmediate(function() {
|
|
|
|
cb(null, data);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
setImmediate(function() {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return cb.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode the binary value back to a JavaScript value.
|
|
|
|
* @param {Buffer} binary The binary input.
|
|
|
|
* @callback {Function} cb The callback to receive the composed value.
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {*} value Decoded value.
|
|
|
|
* @promise
|
|
|
|
*/
|
|
|
|
BinaryPacker.prototype.decode = function(binary, cb) {
|
|
|
|
cb = cb || createPromiseCallback();
|
|
|
|
try {
|
2019-08-02 13:50:51 +00:00
|
|
|
const value = this._packer.decode(binary);
|
2016-10-19 11:14:55 +00:00
|
|
|
setImmediate(function() {
|
|
|
|
cb(null, value);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
setImmediate(function() {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return cb.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
function encodeDate(obj) {
|
|
|
|
return new Buffer(obj.toISOString(), 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeDate(buf) {
|
|
|
|
return new Date(buf.toString('utf8'));
|
|
|
|
}
|