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// base64url 95assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ', 'base64url'), 11); 96assert.strictEqual(Buffer.byteLength('aGVsbG8gd29ybGQ', 'BASE64URL'), 11); 97assert.strictEqual(Buffer.byteLength('bm9kZS5qcyByb2NrcyE', 'base64url'), 14); 98assert.strictEqual(Buffer.byteLength('aGkk', 'base64url'), 3); 99assert.strictEqual( 100 Buffer.byteLength('bHNrZGZsa3NqZmtsc2xrZmFqc2RsZmtqcw', 'base64url'), 25 101); 102// special padding 103assert.strictEqual(Buffer.byteLength('aaa=', 'base64'), 2); 104assert.strictEqual(Buffer.byteLength('aaaa==', 'base64'), 3); 105assert.strictEqual(Buffer.byteLength('aaa=', 'base64url'), 2); 106assert.strictEqual(Buffer.byteLength('aaaa==', 'base64url'), 3); 107 108assert.strictEqual(Buffer.byteLength('Il était tué'), 14); 109assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14); 110 111['ascii', 'latin1', 'binary'] 112 .reduce((es, e) => es.concat(e, e.toUpperCase()), []) 113 .forEach((encoding) => { 114 assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 12); 115 }); 116 117['ucs2', 'ucs-2', 'utf16le', 'utf-16le'] 118 .reduce((es, e) => es.concat(e, e.toUpperCase()), []) 119 .forEach((encoding) => { 120 assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 24); 121 }); 122 123// Test that ArrayBuffer from a different context is detected correctly 124const arrayBuf = vm.runInNewContext('new ArrayBuffer()'); 125assert.strictEqual(Buffer.byteLength(arrayBuf), 0); 126 127// Verify that invalid encodings are treated as utf8 128for (let i = 1; i < 10; i++) { 129 const encoding = String(i).repeat(i); 130 131 assert.ok(!Buffer.isEncoding(encoding)); 132 assert.strictEqual(Buffer.byteLength('foo', encoding), 133 Buffer.byteLength('foo', 'utf8')); 134} 135