Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure support for Uint8Array #246

Merged
merged 6 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately I'd want to get rid of Buffer.concat so that we can avoid conversion to Buffers at all. Meanwhile, I think this stop gap is fine. (Same with utf32).


// 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'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if use of PhantomJS is intentional or accidental. In case it's intentional I would love to learn the reasons. In case it was accidental - here is my rationale for switching.

PhantomJS is suspended for about 2 years already. When I was writing tests it was giving me all kinds of weird errors for Uint8Array. Probably the support there is limited. So I figured it's easier to switch to something more up-to-date and stable than figure out those errors.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, Chrome Headless might be better here. Thank you!



// 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