Skip to content

Commit

Permalink
feat!: Make codecs use streamx and test with all streams
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Apr 23, 2023
1 parent 3e98f45 commit 78f4369
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 218 deletions.
30 changes: 11 additions & 19 deletions lib/codecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable new-cap */

var iconv = require('iconv-lite');
var Transform = require('stream').Transform;
var Transform = require('streamx').Transform;

var DEFAULT_ENCODING = require('./constants').DEFAULT_ENCODING;

Expand All @@ -13,31 +13,28 @@ function Codec(codec, encoding) {
this.bomAware = codec.bomAware || false;
}


function getEncoder(codec) {
return new codec.encoder(null, codec);
}

Codec.prototype.encode = function(str) {
Codec.prototype.encode = function (str) {
var encoder = getEncoder(this.codec);
var buf = encoder.write(str);
var end = encoder.end();
return end && end.length > 0 ? Buffer.concat(buf, end) : buf;
};

Codec.prototype.encodeStream = function() {
Codec.prototype.encodeStream = function () {
var encoder = getEncoder(this.codec);
return new Transform({
objectMode: false,
decodeStrings: false,
transform: function(str, enc, cb) {
transform: function (str, cb) {
var buf = encoder.write(str);
if (buf && buf.length) {
this.push(buf);
}
cb();
},
flush: function(cb) {
flush: function (cb) {
var buf = encoder.end();
if (buf && buf.length) {
this.push(buf);
Expand All @@ -47,41 +44,37 @@ Codec.prototype.encodeStream = function() {
});
};


function getDecoder(codec) {
return new codec.decoder(null, codec);
}

Codec.prototype.decode = function(buf) {
Codec.prototype.decode = function (buf) {
var decoder = getDecoder(this.codec);
var str = decoder.write(buf);
var end = decoder.end();
return end ? str + end : str;
};

Codec.prototype.decodeStream = function() {
Codec.prototype.decodeStream = function () {
var decoder = getDecoder(this.codec);
return new Transform({
objectMode: false,
encoding: DEFAULT_ENCODING,
transform: function(buf, enc, cb) {
transform: function (buf, cb) {
var str = decoder.write(buf);
if (str && str.length) {
this.push(str, DEFAULT_ENCODING);
this.push(str);
}
cb();
},
flush: function(cb) {
flush: function (cb) {
var str = decoder.end();
if (str && str.length) {
this.push(str, DEFAULT_ENCODING);
this.push(str);
}
cb();
},
});
};


var cache = {};

function getCodec(encoding) {
Expand All @@ -100,7 +93,6 @@ function getCodec(encoding) {
return codec;
}


// Pre-load default encoding
getCodec(DEFAULT_ENCODING);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"mocha": "^8.4.0",
"nyc": "^15.1.0",
"parse-node-version": "^2.0.0",
"readable-stream": "3.6.0",
"rimraf": "^3.0.2",
"sinon": "^15.0.2"
},
Expand Down
Loading

0 comments on commit 78f4369

Please sign in to comment.