Skip to content

Commit

Permalink
fix: browser compatibility
Browse files Browse the repository at this point in the history
Use through2 and pumpify.
  • Loading branch information
KlausTrainer committed Jan 22, 2017
1 parent d3b3346 commit 0dbb9b9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
52 changes: 21 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
'use strict'

var snappy = require('snappyjs')
var msgpack = require('msgpack-lite')
var pumpify = require('pumpify')
var through = require('through2')
var lpStream = require('length-prefixed-stream')
var Transform = require('readable-stream/transform')
var msgpack = require('msgpack-lite')
var snappy = require('snappyjs')

var msgpackEncodeStream = new Transform({
objectMode: true,
transform: function (chunk, encoding, next) {
next(null, msgpack.encode(chunk))
}
})
module.exports.createEncodeStream = function SnappyMsgpackEncodeStream (stream) {
var msgEncode = through.obj(function (data, enc, next) {
next(null, msgpack.encode(data))
})

var msgpackDecodeStream = new Transform({
objectMode: true,
transform: function (chunk, encoding, callback) {
callback(null, msgpack.decode(chunk))
}
})
var snappyCompress = through.obj(function (data, enc, next) {
next(null, snappy.compress(data))
})

var snappyCompressStream = new Transform({
transform: function (chunk, encoding, next) {
next(null, snappy.compress(chunk))
}
})
return pumpify.obj(msgEncode, snappyCompress, lpStream.encode())
}

var snappyUncompressStream = new Transform({
transform: function (chunk, encoding, callback) {
callback(null, snappy.uncompress(chunk))
}
})
module.exports.createDecodeStream = function SnappyMsgpackDecodeStream (stream) {
var msgDecode = through.obj(function (data, enc, next) {
next(null, msgpack.decode(data))
})

exports.createEncodeStream = function SnappyMsgpackEncodeStream () {
msgpackEncodeStream.pipe(snappyCompressStream).pipe(lpStream.encode())
return msgpackEncodeStream
}
var snappyUncompress = through.obj(function (data, enc, next) {
next(null, snappy.uncompress(data))
})

exports.createDecodeStream = function SnappyMsgpackDecodeStream () {
return lpStream.decode().pipe(snappyUncompressStream).pipe(msgpackDecodeStream)
return pumpify.obj(lpStream.decode(), snappyUncompress, msgDecode)
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"dependencies": {
"length-prefixed-stream": "^1.5.1",
"msgpack-lite": "^0.1.26",
"readable-stream": "^2.2.2",
"snappyjs": "^0.5.0"
"pumpify": "^1.3.5",
"snappyjs": "^0.5.0",
"through2": "^2.0.3"
},
"keywords": [
"snappy",
Expand Down

0 comments on commit 0dbb9b9

Please sign in to comment.