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

Set default encoding to UTF-8 #315

Merged
merged 3 commits into from
Sep 30, 2017

Conversation

Throne3d
Copy link
Collaborator

Per #237, it would be good to default to using the UTF-8 encoding when the upstream IRC library is able to support this. There should also be some sort of mention in the README about how to ensure the bot can convert encodings.

This doesn't have tests for these lines. I'm not sure how to stub irc.canConvertEncoding() except by including a new development dependency of proxyquire, which is designed for specifically the purpose of stubbing requires on other files. Suggestions, comments?

Also warn when started if the IRC library cannot convert between
encodings, in case users rely on this behavior.
@coveralls
Copy link

coveralls commented Sep 30, 2017

Coverage Status

Coverage decreased (-0.4%) to 98.897% when pulling ec56eca on Throne3d:fix/237-default-utf8-encoding into 1c0cc3f on reactiflux:master.

@ekmartin
Copy link
Member

I might be misunderstanding something here, but isn't utf8 already the default encoding in node-irc? https://github.com/Throne3d/node-irc/blob/master/lib/irc.js#L866-L868

If so I guess you'd only need the check and the warning message.

lib/bot.js Outdated
if (irc.canConvertEncoding()) {
ircOptions.encoding = 'utf-8';
} else {
logger.warn('Cannot convert message encoding; you may encounter corrupted characters with non-English text.');
Copy link
Member

Choose a reason for hiding this comment

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

You might want to link to the section in your node-irc README here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea! Done.

@ekmartin
Copy link
Member

lib/bot.js Outdated
@@ -91,6 +91,15 @@ class Bot {
...this.ircOptions
};

// default encoding to UTF-8 so messages to Discord aren't corrupted
if (!('encoding' in ircOptions)) {
Copy link
Member

Choose a reason for hiding this comment

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

if (!ircOptions.encoding) or if you really want to explicitly check that the property isn't set: !ircOptions.hasOwnProperty('encoding')

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The linter seems to complain about ircOptions.hasOwnProperty('encoding'):

.../discord-irc/lib/bot.js
  95:21  error  Do not access Object.prototype method 'hasOwnProperty' from target object  no-prototype-builtins

I've made it use Object.prototype.hasOwnProperty.call(ircOptions, 'encoding') instead.

I think I decided to check for the property because if encoding is set without support for translating encodings, it will output errors for each message it attempts (and fails) to convert the encoding of. So, if the library erroneously says it can convert an encoding, and so discord-irc sets the encoding, (or if something else goes wrong along the way) it could be useful for people to manually disable the option by setting it to null.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also – what's the difference between 'encoding' in ircOptions and ircOptions.hasOwnProperty('encoding')? Whether the object ircOptions implements it itself, versus whether some superclass of it does?

Copy link
Member

Choose a reason for hiding this comment

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

Yep: https://stackoverflow.com/questions/13632999/if-key-in-object-or-ifobject-hasownpropertykey

Probably doesn't really matter here though, but I think in general hasOwnProperty is considered a bit more hygienic.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) to 98.897% when pulling 0ef0f09 on Throne3d:fix/237-default-utf8-encoding into 1c0cc3f on reactiflux:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) to 98.897% when pulling 0ef0f09 on Throne3d:fix/237-default-utf8-encoding into 1c0cc3f on reactiflux:master.

@@ -91,6 +91,16 @@ class Bot {
...this.ircOptions
};

// default encoding to UTF-8 so messages to Discord aren't corrupted
if (!Object.prototype.hasOwnProperty.call(ircOptions, 'encoding')) {
Copy link
Member

Choose a reason for hiding this comment

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

Hm, why did you use Object.prototype.hasOwnProperty instead of directly on ircOptions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The linter complained – I said in this comment.

Copy link
Member

Choose a reason for hiding this comment

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

ah lol, sorry about that :)

@ekmartin ekmartin merged commit 7fbf1d0 into reactiflux:master Sep 30, 2017
@Throne3d Throne3d deleted the fix/237-default-utf8-encoding branch September 30, 2017 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants