1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const SlowBuffer = require('buffer').SlowBuffer; 6const vm = require('vm'); 7 8[ 9 [32, 'latin1'], 10 [NaN, 'utf8'], 11 [{}, 'latin1'], 12 [] 13].forEach((args) => { 14 assert.throws( 15 () => Buffer.byteLength(...args), 16 { 17 code: 'ERR_INVALID_ARG_TYPE', 18 name: 'TypeError', 19 message: 'The "string" argument must be of type string or an instance ' + 20 'of Buffer or ArrayBuffer.' + 21 common.invalidArgTypeHelper(args[0]) 22 } 23 ); 24}); 25 26assert.strictEqual(Buffer.byteLength('', undefined, true), -1); 27 28assert(ArrayBuffer.isView(new Buffer(10))); 29assert(ArrayBuffer.isView(new SlowBuffer(10))); 30assert(ArrayBuffer.isView(Buffer.alloc(10))); 31assert(ArrayBuffer.isView(Buffer.allocUnsafe(10))); 32assert(ArrayBuffer.isView(Buffer.allocUnsafeSlow(10))); 33assert(ArrayBuffer.isView(Buffer.from(''))); 34 35// buffer 36const incomplete = Buffer.from([0xe4, 0xb8, 0xad, 0xe6, 0x96]); 37assert.strictEqual(Buffer.byteLength(incomplete), 5); 38const ascii = Buffer.from('abc'); 39assert.strictEqual(Buffer.byteLength(ascii), 3); 40 41// ArrayBuffer 42const buffer = new ArrayBuffer(8); 43assert.strictEqual(Buffer.byteLength(buffer), 8); 44 45// TypedArray 46const int8 = new Int8Array(8); 47assert.strictEqual(Buffer.byteLength(int8), 8); 48const uint8 = new Uint8Array(8); 49assert.strictEqual(Buffer.byteLength(uint8), 8); 50const uintc8 = new Uint8ClampedArray(2); 51assert.strictEqual(Buffer.byteLength(uintc8), 2); 52const int16 = new Int16Array(8); 53assert.strictEqual(Buffer.byteLength(int16), 16); 54const uint16 = new Uint16Array(8); 55assert.strictEqual(Buffer.byteLength(uint16), 16); 56const int32 = new Int32Array(8); 57assert.strictEqual(Buffer.byteLength(int32), 32); 58const uint32 = new Uint32Array(8); 59assert.strictEqual(Buffer.byteLength(uint32), 32); 60const float32 = new Float32Array(8); 61assert.strictEqual(Buffer.byteLength(float32), 32); 62const float64 = new Float64Array(8); 63assert.strictEqual(Buffer.byteLength(float64), 64); 64 65// DataView 66const dv = new DataView(new ArrayBuffer(2)); 67assert.strictEqual(Buffer.byteLength(dv), 2); 68 69// Special case: zero length string 70assert.strictEqual(Buffer.byteLength('', 'ascii'), 0); 71assert.strictEqual(Buffer.byteLength('', 'HeX'), 0); 72 73// utf8 74assert.strictEqual(Buffer.byteLength('∑éllö wørl∂!', 'utf-8'), 19); 75assert.strictEqual(Buffer.byteLength('κλμνξο', 'utf8'), 12); 76assert.strictEqual(Buffer.byteLength('挵挶挷挸挹', 'utf-8'), 15); 77assert.strictEqual(Buffer.byteLength('', 'UTF8'), 12); 78// Without an encoding, utf8 should be assumed 79assert.strictEqual(Buffer.byteLength('hey there'), 9); 80assert.strictEqual(Buffer.byteLength('挶νξ#xx :)'), 17); 81assert.strictEqual(Buffer.byteLength('hello world', ''), 11); 82// It should also be assumed with unrecognized encoding 83assert.strictEqual(Buffer.byteLength('hello world', 'abc'), 11); 84assert.strictEqual(Buffer.byteLength('ßœ∑≈', 'unkn0wn enc0ding'), 10); 85 86// base64 87assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'base64'), 11); 88assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ=', 'BASE64'), 11); 89assert.strictEqual(Buffer.byteLength('bm9kZS5qcyByb2NrcyE=', 'base64'), 14); 90assert.strictEqual(Buffer.byteLength('aGkk', 'base64'), 3); 91assert.strictEqual( 92 Buffer.byteLength('bHNrZGZsa3NqZmtsc2xrZmFqc2RsZmtqcw==', 'base64'), 25 93); 94// special padding 95assert.strictEqual(Buffer.byteLength('aaa=', 'base64'), 2); 96assert.strictEqual(Buffer.byteLength('aaaa==', 'base64'), 3); 97 98assert.strictEqual(Buffer.byteLength('Il était tué'), 14); 99assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14); 100 101['ascii', 'latin1', 'binary'] 102 .reduce((es, e) => es.concat(e, e.toUpperCase()), []) 103 .forEach((encoding) => { 104 assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 12); 105 }); 106 107['ucs2', 'ucs-2', 'utf16le', 'utf-16le'] 108 .reduce((es, e) => es.concat(e, e.toUpperCase()), []) 109 .forEach((encoding) => { 110 assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 24); 111 }); 112 113// Test that ArrayBuffer from a different context is detected correctly 114const arrayBuf = vm.runInNewContext('new ArrayBuffer()'); 115assert.strictEqual(Buffer.byteLength(arrayBuf), 0); 116 117// Verify that invalid encodings are treated as utf8 118for (let i = 1; i < 10; i++) { 119 const encoding = String(i).repeat(i); 120 121 assert.ok(!Buffer.isEncoding(encoding)); 122 assert.strictEqual(Buffer.byteLength('foo', encoding), 123 Buffer.byteLength('foo', 'utf8')); 124} 125