UNPKG

diffusion

Version:

Diffusion JavaScript client

33 lines (28 loc) 828 B
var BEES = require('serialisers/byte-encoded-enum-serialiser'); var Codec = require('io/codec'); var Encoding = { NONE: 0, ENCRYPTED: 1, COMPRESSED: 2 }; function getBytes(content) { return (content.$buffer) ? content.$buffer.slice(content.$offset, content.$length) : new Buffer(content.toString()); } module.exports = { read: function (bis) { var encoding = BEES.read(bis, Encoding); var bytes = Codec.readBytes(bis); switch (encoding) { case Encoding.NONE : return bytes; default : throw new Error('Unable to handle encoding type ' + encoding); } }, write: function (bos, content) { BEES.write(bos, Encoding.NONE); Codec.writeBytes(bos, getBytes(content)); } };