-
Notifications
You must be signed in to change notification settings - Fork 294
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
Set default encoding to UTF-8 #315
Conversation
Also warn when started if the IRC library cannot convert between encodings, in case users rely on this behavior.
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.'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! Done.
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)) { |
There was a problem hiding this comment.
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')
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
1 similar comment
@@ -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')) { |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
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 ofproxyquire
, which is designed for specifically the purpose of stubbing requires on other files. Suggestions, comments?