Comparing version 1.3.2 to 1.3.3
## Changelog | ||
1.3.3: | ||
* Fixed #98: Ensure `Notification#trim` cleanly trims Unicode characters. | ||
1.3.2: | ||
@@ -4,0 +8,0 @@ |
@@ -482,3 +482,3 @@ var Errors = require('./errors'); | ||
var token = notification.recipient.token | ||
var token = notification.recipient.token; | ||
var encoding = notification.notification.encoding || 'utf8'; | ||
@@ -531,5 +531,3 @@ var message = notification.notification.compile(); | ||
Connection.prototype.validNotification = function (notification, recipient) { | ||
var encoding = notification.encoding || 'utf8'; | ||
var message = notification.compile(); | ||
var messageLength = Buffer.byteLength(message, encoding); | ||
var messageLength = notification.length(); | ||
@@ -543,2 +541,3 @@ if (messageLength > 256) { | ||
} | ||
notification.compile(); | ||
return true; | ||
@@ -545,0 +544,0 @@ }; |
@@ -24,3 +24,3 @@ /** | ||
this.compiled = false; | ||
}; | ||
} | ||
@@ -154,3 +154,3 @@ /** | ||
Notification.prototype.trim = function() { | ||
var tooLong = this.length() - 255; | ||
var tooLong = this.length() - 256; | ||
if(tooLong <= 0) { | ||
@@ -160,16 +160,17 @@ return 0; | ||
var length; | ||
var encoding = this.encoding || 'utf8'; | ||
if(typeof this.alert == "string") { | ||
var length = Buffer.byteLength(this.alert, this.encoding || 'utf8'); | ||
length = Buffer.byteLength(this.alert, encoding); | ||
if (length < tooLong) { | ||
return length - tooLong; | ||
} | ||
this.alert = this.truncateStringToBytes(this.alert, length - tooLong); | ||
this.alert = this.truncateStringToLength(this.alert, length - tooLong); | ||
return tooLong; | ||
} | ||
else if(typeof this.alert == "object" && typeof this.alert.body == "string") { | ||
var length = Buffer.byteLength(this.alert.body, this.encoding || 'utf8'); | ||
length = Buffer.byteLength(this.alert.body, encoding); | ||
if (length < tooLong) { | ||
return length - tooLong; | ||
} | ||
this.alert.body = this.truncateStringToBytes(this.alert.body, length - tooLong); | ||
this.alert.body = this.truncateStringToLength(this.alert.body, length - tooLong); | ||
return tooLong; | ||
@@ -195,14 +196,37 @@ } | ||
/** | ||
* @param {String} [string] Unicode string to be truncated | ||
* @param {Number} [length] The maximum number of bytes permitted in the Unicode string | ||
* @returns {String} Truncated String | ||
* @private | ||
*/ | ||
Notification.prototype.truncateStringToBytes = function(string, bytes) { | ||
var truncated = string.substring(0, bytes); | ||
*/ | ||
Notification.prototype.truncateStringToLength = function (string, length) { | ||
var result = new Buffer(string, this.encoding || 'utf8').toString(this.encoding || 'utf8', 0, length); | ||
while (Buffer.byteLength(truncated, this.encoding || 'utf8') > bytes) { | ||
truncated = truncated.substring(0, truncated.length - 1); | ||
} | ||
// since we might have chopped off the end of a multi-byte sequence, remove any | ||
// invalid characters (represented as U+FFFD "REPLACEMENT CHARACTER") for UTF-8 | ||
// or orphaned lead surrogates for UTF-16 (UCS-2) - where only the tail surrogate | ||
// has been removed. | ||
var done = false; | ||
var encoding = this.encoding || 'utf8'; | ||
return truncated; | ||
} | ||
if (encoding != 'utf8' && encoding != 'utf16le' && encoding != 'ucs2') { | ||
return result; | ||
} | ||
while (! done) { | ||
var lastIndex = result.length - 1; | ||
var code = result.charCodeAt(lastIndex); | ||
if (code != 0xFFFD && encoding == 'utf8') { | ||
done = true; | ||
} | ||
else if ((code < 0xD800 || code > 0xDBFF) && (encoding == 'utf16le' || encoding == 'ucs2')) { | ||
done = true; | ||
} | ||
else { | ||
result = result.substr(0, lastIndex); | ||
} | ||
} | ||
return result; | ||
}; | ||
/** | ||
@@ -209,0 +233,0 @@ * @private |
{ | ||
"name": "apn", | ||
"description": "An interface to the Apple Push Notification service for Node.js", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"author": "Andrew Naylor <argon@mkbot.net>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -63,3 +63,3 @@ #node-apn | ||
note.sound = "ping.aiff"; | ||
note.alert = "You have a new message"; | ||
note.alert = "\uD83D\uDCE7 \u2709 You have a new message"; | ||
note.payload = {'messageFrom': 'Caroline'}; | ||
@@ -73,6 +73,10 @@ | ||
{"messageFrom":"Caroline","aps":{"badge":3,"sound":"ping.aiff","alert":"You have a new message"}} | ||
**\*N.B.:** If you wish to send notifications containing emoji or other multi-byte characters you will need to set `note.encoding = 'ucs2'`. This tells node to send the message with 16bit characters, however it also means your message payload will be limited to 128 characters. | ||
{"messageFrom":"Caroline","aps":{"badge":3,"sound":"ping.aiff","alert":"\uD83D\uDCE7 \u2709 You have a new message"}} | ||
#### A note on Unicode. | ||
If you wish to send notifications containing emoji or other multi-byte characters you will need to ensure they are encoded correctly within the string. Notifications can be transmitted to Apple in either UTF-8 or UTF-16 and strings passed in for the Alert will be converted accordingly. UTF-8 is recommended for most cases as it can represent exactly the same characters as UTF-16 but is usually more space-efficient. When manually encoding strings as above with `\uD83D\uDCE7` the character (in this case a surrogate pair) is escaped in UTF-16 form because Javascript uses UTF-16 internally for Strings but does not handle surrogate pairs automatically. | ||
If in doubt, leave the encoding as default. If you experience any problems raise an issue on GitHub. | ||
### Setting up the feedback service | ||
@@ -137,3 +141,3 @@ | ||
Thanks to: [Ian Babrou][bobrik], [dgthistle][dgthistle], [Keith Larsen][keithnlarsen], [Mike P][mypark], [Greg Bergé][neoziro], [Asad ur Rehman][AsadR], [Nebojsa Sabovic][nsabovic], [Alberto Gimeno][gimenete], [Randall Tombaugh][rwtombaugh], [Michael Stewart][thegreatmichael], [Olivier Louvignes][mgcrea], [porsager][porsager] | ||
Thanks to: [Ian Babrou][bobrik], [dgthistle][dgthistle], [Keith Larsen][keithnlarsen], [Mike P][mypark], [Greg Bergé][neoziro], [Asad ur Rehman][AsadR], [Nebojsa Sabovic][nsabovic], [Alberto Gimeno][gimenete], [Randall Tombaugh][rwtombaugh], [Michael Stewart][thegreatmichael], [Olivier Louvignes][mgcrea], [porsager][porsager], [Craig Hockenberry][chockenberry] | ||
@@ -179,2 +183,3 @@ ## License | ||
[q]: https://github.com/kriskowal/q | ||
[chockenberry]: https://github.com/chockenberry | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
1095
182
3
72728
14