From 70ff15d3c27b77a188a99d646f32abddf7f54da6 Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Tue, 14 Jan 2020 02:37:24 -0500 Subject: [PATCH 1/4] doc: explain `hex` encoding in Buffer API fixes: https://github.com/nodejs/node/issues/29786 refs: https://github.com/nodejs/node/pull/29792 refs: https://github.com/nodejs/node/issues/24491 --- doc/api/buffer.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index c94f03fa065a36..9e43197f76fe38 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -203,7 +203,23 @@ The character encodings currently supported by Node.js include: * `'binary'`: Alias for `'latin1'`. -* `'hex'`: Encode each byte as two hexadecimal characters. +* `'hex'`: Encode each byte as two hexadecimal characters. Data truncation + may occur for unsanitized input. For example: + +```js +Buffer.from('1ag', 'hex'); +// Prints , data truncated when first non-hexa-decimal value +// ('g') encountered + +Buffer.from('1a7g', 'hex'); +// Prints , data truncated when data ends in single digit ('7'). + +Buffer.from('1634', 'hex'); +// Prints , fully qualified hexadecimal data + +Buffer.from((163).toString(16), 'hex'); +// Prints , sanitized hexadecimal data +``` Modern Web browsers follow the [WHATWG Encoding Standard][] which aliases both `'latin1'` and `'ISO-8859-1'` to `'win-1252'`. This means that while doing From 77cf2029d171659d9e772f25f6e8944b98b3ccc7 Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Tue, 14 Jan 2020 09:30:36 -0500 Subject: [PATCH 2/4] fixup: address review comments --- doc/api/buffer.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 9e43197f76fe38..4e0526a8609b80 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -208,17 +208,17 @@ The character encodings currently supported by Node.js include: ```js Buffer.from('1ag', 'hex'); -// Prints , data truncated when first non-hexa-decimal value +// Prints , data truncated when first non-hexadecimal value // ('g') encountered Buffer.from('1a7g', 'hex'); -// Prints , data truncated when data ends in single digit ('7'). +// Prints , data truncated when data ends in single digit ('7'). Buffer.from('1634', 'hex'); -// Prints , fully qualified hexadecimal data +// Prints , fully qualified hexadecimal data Buffer.from((163).toString(16), 'hex'); -// Prints , sanitized hexadecimal data +// Prints , sanitized hexadecimal data ``` Modern Web browsers follow the [WHATWG Encoding Standard][] which aliases From 687693e6b1770eac84320abbc6877aec60fd5690 Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Thu, 16 Jan 2020 06:56:44 -0500 Subject: [PATCH 3/4] fixup: address review comment --- doc/api/buffer.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 4e0526a8609b80..5d0209ca20c08f 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -216,9 +216,6 @@ Buffer.from('1a7g', 'hex'); Buffer.from('1634', 'hex'); // Prints , fully qualified hexadecimal data - -Buffer.from((163).toString(16), 'hex'); -// Prints , sanitized hexadecimal data ``` Modern Web browsers follow the [WHATWG Encoding Standard][] which aliases From 6b8c1c215518b61c237741d687d98e4136daffdf Mon Sep 17 00:00:00 2001 From: Harshitha KP Date: Thu, 16 Jan 2020 23:33:00 -0500 Subject: [PATCH 4/4] fixup: address review comments --- doc/api/buffer.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 5d0209ca20c08f..f033f8a20ae9dc 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -209,13 +209,13 @@ The character encodings currently supported by Node.js include: ```js Buffer.from('1ag', 'hex'); // Prints , data truncated when first non-hexadecimal value -// ('g') encountered +// ('g') encountered. Buffer.from('1a7g', 'hex'); // Prints , data truncated when data ends in single digit ('7'). Buffer.from('1634', 'hex'); -// Prints , fully qualified hexadecimal data +// Prints , all data represented. ``` Modern Web browsers follow the [WHATWG Encoding Standard][] which aliases