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 1 commit
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)) {
return this.decoder.write(buf);
}

return this.decoder.write(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.

Yeah it seems internal encoder requires conversion to Buffer-s. A bit sad, but understandable, as otherwise they'd have to replicate all encoding/decoding logic from Buffer.toString.

nit: maybe structure it like this to highlight that we're just converting it into a buffer?

Suggested change
if (Buffer.isBuffer(buf)) {
return this.decoder.write(buf);
}
return this.decoder.write(Buffer.from(buf));
if (!Buffer.isBuffer(buf)) {
buf = Buffer.from(buf);
}
return this.decoder.write(buf);

}

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


//------------------------------------------------------------------------------
Expand Down
31 changes: 31 additions & 0 deletions test/webpack/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,37 @@ describe("iconv-lite", function() {
var str = iconv.decode(buf, "utf8");
assert.equal(str, "💩");
});

[
'utf8',
'utf16le',
'utf16be',
'ucs2',
'binary',
'latin1',
'big5',
'gbk',
'cesu8',
'shiftjis',
'cp1251',
'cp1253',
'cp1254',
'armscii8'
].forEach(function(encoding) {
it("supports Uint8Array for " + encoding, function() {
var expected = 'Lorem ipsum';

var encoded = iconv.encode(expected, encoding);
var byteArray = [];
for (var i = 0; i < encoded.length; i++) {
byteArray[i] = encoded[i];
}
const uint8Array = Uint8Array.from(byteArray);

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

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