Skip to content

Commit

Permalink
Support Uint8Array-s instead of Buffers when decoding (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyzerok committed Jun 28, 2020
1 parent 3331bbc commit dd72d9d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
14 changes: 12 additions & 2 deletions encodings/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,20 @@ if (!StringDecoder.prototype.end) // Node v0.8 doesn't have this method.


function InternalDecoder(options, codec) {
StringDecoder.call(this, codec.enc);
this.decoder = new StringDecoder(codec.enc);
}

InternalDecoder.prototype = StringDecoder.prototype;
InternalDecoder.prototype.write = function(buf) {
if (!Buffer.isBuffer(buf)) {
buf = Buffer.from(buf);
}

return this.decoder.write(buf);
}

InternalDecoder.prototype.end = function() {
return this.decoder.end();
}


//------------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions encodings/utf16.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ function Utf16Decoder(options, codec) {

Utf16Decoder.prototype.write = function(buf) {
if (!this.decoder) {
// Support Uint8Array
if (!Buffer.isBuffer(buf)) {
buf = Buffer.from(buf)
}

// Codec is not chosen yet. Accumulate initial bytes.
this.initialBytes.push(buf);
this.initialBytesLen += buf.length;
Expand Down
12 changes: 11 additions & 1 deletion encodings/utf32.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ Utf32Decoder.prototype.write = function(src) {
if (src.length === 0)
return '';

// Support Uint8Array
if (!Buffer.isBuffer(src)) {
src = Buffer.from(src);
}

if (this.overflow)
src = Buffer.concat([this.overflow, src]);

Expand Down Expand Up @@ -203,7 +208,12 @@ function Utf32AutoDecoder(options, codec) {
}

Utf32AutoDecoder.prototype.write = function(buf) {
if (!this.decoder) {
if (!this.decoder) {
// Support Uint8Array
if (!Buffer.isBuffer(buf)) {
buf = Buffer.from(buf);
}

// Codec is not chosen yet. Accumulate initial bytes.
this.initialBytes.push(buf);
this.initialBytesLen += buf.length;
Expand Down
21 changes: 21 additions & 0 deletions test/webpack/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ describe("iconv-lite", function() {
var str = iconv.decode(buf, "utf8");
assert.equal(str, "💩");
});

it("supports passing Uint8Array to decode for all encodings", function() {
iconv.encode('', 'utf8'); // Load all encodings.

var encodings = Object.keys(iconv.encodings)
encodings
.filter(encoding =>
!encoding.startsWith('_')
// https://github.com/ashtuchkin/iconv-lite/issues/231
&& encoding !== 'base64' && encoding !== 'hex'
)
.forEach(function(encoding) {
var expected = 'Lorem ipsum';

var encoded = iconv.encode(expected, encoding);
var uint8Array = Uint8Array.from(encoded);

var actual = iconv.decode(uint8Array, encoding);
assert.equal(actual, expected, encoding);
})
});
});

describe("stream module", function() {
Expand Down
3 changes: 2 additions & 1 deletion test/webpack/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Karma configuration
// Generated on Sat May 23 2020 18:02:48 GMT-0400 (Eastern Daylight Time)
process.env.CHROME_BIN = require('puppeteer').executablePath()

module.exports = function(config) {
config.set({
Expand Down Expand Up @@ -64,7 +65,7 @@ module.exports = function(config) {

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
browsers: ['ChromeHeadless'],


// Continuous Integration mode
Expand Down
4 changes: 2 additions & 2 deletions test/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
},
"devDependencies": {
"karma": "^5.0.9",
"karma-chrome-launcher": "^3.1.0",
"karma-mocha": "^2.0.1",
"karma-phantomjs-launcher": "^1.0.4",
"karma-webpack": "^4.0.2",
"mocha": "^7.2.0",
"phantomjs-prebuilt": "^2.1.16",
"puppeteer": "^4.0.0",
"webpack": "^4.43.0"
},
"dependencies": {
Expand Down

0 comments on commit dd72d9d

Please sign in to comment.