summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJackson Tian <shyvo1987@gmail.com>2016-02-16 11:08:49 +0800
committerJames M Snell <jasnell@gmail.com>2016-03-27 09:19:39 -0700
commit293fd0453591ef0d87437a265ff515916d35d715 (patch)
tree5194f284fa4e16829890c5355c2d2eabd6351af0 /lib
parentafd821a91d14a13becdf989184adf721ae7365eb (diff)
downloadandroid-node-v8-293fd0453591ef0d87437a265ff515916d35d715.tar.gz
android-node-v8-293fd0453591ef0d87437a265ff515916d35d715.tar.bz2
android-node-v8-293fd0453591ef0d87437a265ff515916d35d715.zip
buffer: make byteLength work with ArrayBuffer & DataView
Convert anything to string, but Buffer, TypedArray and ArrayBuffer ``` var uint8 = new Uint8Array([0xf0, 0x9f, 0x90]); Buffer.byteLength(uint8); // should be 3, but returns 11 Buffer.byteLength(uint8.buffer); // should be 3, but return 20 ``` PR-URL: https://github.com/nodejs/node/pull/5255 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/buffer.js7
1 files changed, 4 insertions, 3 deletions
diff --git a/lib/buffer.js b/lib/buffer.js
index a83361cc9b..7150debf35 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -364,11 +364,12 @@ function base64ByteLength(str, bytes) {
function byteLength(string, encoding) {
- if (string instanceof Buffer)
- return string.length;
+ if (typeof string !== 'string') {
+ if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)
+ return string.byteLength;
- if (typeof string !== 'string')
string = '' + string;
+ }
var len = string.length;
if (len === 0)