diffusion
Version:
Diffusion JavaScript client
50 lines (37 loc) • 1.34 kB
JavaScript
var _implements = require('util/interface')._implements;
var BinaryType = require('../../../data/binary/binary');
var BinaryDeltaTypeImpl = require('data/binary/binary-delta-type-impl');
var BytesImpl = require('data/bytes-impl');
var identity = require('util/function').identity;
var BINARY_DELTA_TYPE = new BinaryDeltaTypeImpl(BinaryImpl, from, identity);
function from(value) {
if (value instanceof BinaryImpl) {
return value;
} else if (Buffer.isBuffer(value)) {
return new BinaryImpl(value);
} else {
throw new Error("Unable to read Binary value from: " + value);
}
}
function BinaryImpl(buffer, offset, length) {
// eslint-disable-next-line no-underscore-dangle
BytesImpl.__constructor.call(this, buffer, offset, length);
var self = this;
this.get = function() {
return self.asBuffer();
};
this.diff = function(original) {
return BINARY_DELTA_TYPE.diff(original, self);
};
this.apply = function(delta) {
return BINARY_DELTA_TYPE.apply(self, delta);
};
this.toString = function() {
return "Binary <" + self.$length + " bytes> " + self.get().toString();
};
}
module.exports = _implements(BinaryType, BinaryImpl);
module.exports.from = from;
module.exports.toString = function() {
return "BinaryImpl";
};