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

Conversation

gyzerok
Copy link
Contributor

@gyzerok gyzerok commented Jun 22, 2020

I went ahead and created this PR for microsoft/vscode#79275 (comment) so you don't need to spend your time if you agree with the idea.

No pressure though, feel free to close it if you disagree 😄

@@ -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!


const encodings = Object.keys(iconv.encodings)
encodings.forEach(function(encoding) {
if (['base64', 'hex', '_internal', '0'].indexOf(encoding) >= 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Excluded temporarily base64 and hex since they don't seem to work even with buffers: #247

@ashtuchkin
Copy link
Owner

ashtuchkin commented Jun 22, 2020 via email

Copy link
Owner

@ashtuchkin ashtuchkin left a comment

Choose a reason for hiding this comment

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

This is great. Added a couple code nits and I think we're ready to merge.

@@ -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
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!


var encodings = Object.keys(iconv.encodings)
encodings
.filter(encoding => !encoding.startsWith('_') && encoding !== '0')
Copy link
Owner

Choose a reason for hiding this comment

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

nit: hmm I'm not sure why you're filtering out "0"? I don't see this key in encodings object..

Suggested change
.filter(encoding => !encoding.startsWith('_') && encoding !== '0')
.filter(encoding => !encoding.startsWith('_'))

Comment on lines 55 to 59
var byteArray = [];
for (var i = 0; i < encoded.length; i++) {
byteArray[i] = encoded[i];
}
var uint8Array = Uint8Array.from(byteArray);
Copy link
Owner

Choose a reason for hiding this comment

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

nit: Uint8Array can be created directly from a buffer (I think)

Suggested change
var byteArray = [];
for (var i = 0; i < encoded.length; i++) {
byteArray[i] = encoded[i];
}
var uint8Array = Uint8Array.from(byteArray);
var uint8Array = Uint8Array.from(encoded);

Comment on lines 60 to 64
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);

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

@gyzerok
Copy link
Contributor Author

gyzerok commented Jun 28, 2020

@ashtuchkin thank you for the review! All the changes applied. Would you be able to publish it as a new version, so I can continue with vscode? :)

@ashtuchkin ashtuchkin merged commit dd72d9d into ashtuchkin:master Jun 28, 2020
@ashtuchkin
Copy link
Owner

Yep, will publish in a couple mins

@ashtuchkin
Copy link
Owner

ok, it's published as 0.6.1

@gyzerok gyzerok deleted the support-int8 branch June 28, 2020 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants