Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

buffer

Package Overview
Dependencies
Maintainers
1
Versions
96
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

buffer - npm Package Compare versions

Comparing version 4.5.1 to 4.6.0

bin/zuul-es5.yml

42

bin/download-node-tests.js

@@ -11,3 +11,3 @@ #!/usr/bin/env node

var url = 'https://api.github.com/repos/nodejs/io.js/contents'
var url = 'https://api.github.com/repos/nodejs/node/contents'
var dirs = [

@@ -19,3 +19,2 @@ '/test/parallel',

cp.execSync('rm -rf node/*.js', { cwd: path.join(__dirname, '../test') })
cp.execSync('rm -rf node-es6/*.js', { cwd: path.join(__dirname, '../test') })

@@ -44,15 +43,11 @@ var httpOpts = {

var out
if (file.name === 'test-buffer-iterator.js' ||
file.name === 'test-buffer-arraybuffer.js') {
out = path.join(__dirname, '../test/node-es6', file.name)
} else if (file.name === 'test-buffer-fakes.js') {
if (file.name === 'test-buffer-fakes.js') {
// These teses only apply to node, where they're calling into C++ and need to
// ensure the prototype can't be faked, or else there will be a segfault.
return
} else {
out = path.join(__dirname, '../test/node', file.name)
}
console.log(file.download_url)
var out = path.join(__dirname, '../test/node', file.name)
hyperquest(file.download_url, httpOpts)

@@ -76,4 +71,4 @@ .pipe(split())

// require buffer explicitly
var preamble = 'if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;\n' +
'var Buffer = require(\'../../\').Buffer;'
var preamble = 'var Buffer = require(\'../../\').Buffer;\n' +
'if (Buffer.TYPED_ARRAY_SUPPORT) return;'
if (/use strict/.test(line)) line += '\n' + preamble

@@ -87,7 +82,7 @@ else line + preamble + '\n' + line

// make `require('common')` work
// make `var common = require('common')` work
line = line.replace(/(var common = require.*)/g, 'var common = {};')
// use `Buffer.isBuffer` instead of `instanceof Buffer`
line = line.replace(/buf instanceof Buffer/g, 'Buffer.isBuffer(buf)')
// make `require('../common')` work
line = line.replace(/require\('\.\.\/common'\);/g, '')

@@ -97,8 +92,2 @@ // require browser buffer

// smalloc is only used for kMaxLength
line = line.replace(
/require\('smalloc'\)/g,
'{ kMaxLength: process.env.OBJECT_IMPL ? 0x3fffffff : 0x7fffffff }'
)
// comment out console logs

@@ -114,13 +103,4 @@ line = line.replace(/(.*console\..*)/g, '// $1')

// https://github.com/iojs/io.js/blob/v0.12/test/parallel/test-buffer.js#L38
// we can't run this because we need to support
// browsers that don't have typed arrays
if (filename === 'test-buffer.js') {
line = line.replace(/b\[0\] = -1;/, 'b[0] = 255;')
}
// https://github.com/iojs/io.js/blob/v0.12/test/parallel/test-buffer.js#L1138
// unfortunately we can't run this as it touches
// node streams which do an instanceof check
// and crypto-browserify doesn't work in old
// https://github.com/nodejs/node/blob/v0.12/test/parallel/test-buffer.js#L1138
// unfortunately we can't run this because crypto-browserify doesn't work in old
// versions of ie

@@ -127,0 +107,0 @@ if (filename === 'test-buffer.js') {

#!/usr/bin/env node
var cp = require('child_process')
var fs = require('fs')
var path = require('path')
var runBrowserTests = !process.env.TRAVIS_PULL_REQUEST ||
var shouldRunBrowserTests = !process.env.TRAVIS_PULL_REQUEST ||
process.env.TRAVIS_PULL_REQUEST === 'false'

@@ -10,7 +12,4 @@

node.on('close', function (code) {
if (code === 0 && runBrowserTests) {
var browser = cp.spawn('npm', ['run', 'test-browser'], { stdio: 'inherit' })
browser.on('close', function (code) {
process.exit(code)
})
if (code === 0 && shouldRunBrowserTests) {
runBrowserTests()
} else {

@@ -20,1 +19,25 @@ process.exit(code)

})
function runBrowserTests () {
var zuulYmlPath = path.join(__dirname, '..', '.zuul.yml')
writeES5ZuulYml()
cp.spawn('npm', ['run', 'test-browser-es5'], { stdio: 'inherit' })
.on('close', function (code) {
if (code !== 0) process.exit(code)
writeES6ZuulYml()
cp.spawn('npm', ['run', 'test-browser-es6'], { stdio: 'inherit' })
.on('close', function (code) {
process.exit(code)
})
})
function writeES5ZuulYml () {
fs.writeFileSync(zuulYmlPath, fs.readFileSync(path.join(__dirname, 'zuul-es5.yml')))
}
function writeES6ZuulYml () {
fs.writeFileSync(zuulYmlPath, fs.readFileSync(path.join(__dirname, 'zuul-es6.yml')))
}
}

@@ -18,6 +18,3 @@ /*!

exports.INSPECT_MAX_BYTES = 50
Buffer.poolSize = 8192 // not used by this implementation
var rootParent = {}
/**

@@ -51,2 +48,7 @@ * If `Buffer.TYPED_ARRAY_SUPPORT`:

/*
* Export kMaxLength after typed array support is determined.
*/
exports.kMaxLength = kMaxLength()
function typedArraySupport () {

@@ -70,2 +72,21 @@ try {

function createBuffer (that, length) {
if (kMaxLength() < length) {
throw new RangeError('Invalid typed array length')
}
if (Buffer.TYPED_ARRAY_SUPPORT) {
// Return an augmented `Uint8Array` instance, for best performance
that = new Uint8Array(length)
that.__proto__ = Buffer.prototype
} else {
// Fallback: Return an object instance of the Buffer class
if (that === null) {
that = new Buffer(length)
}
that.length = length
}
return that
}
/**

@@ -80,12 +101,6 @@ * The Buffer constructor returns instances of `Uint8Array` that have their

*/
function Buffer (arg) {
if (!(this instanceof Buffer)) {
// Avoid going through an ArgumentsAdaptorTrampoline in the common case.
if (arguments.length > 1) return new Buffer(arg, arguments[1])
return new Buffer(arg)
}
if (!Buffer.TYPED_ARRAY_SUPPORT) {
this.length = 0
this.parent = undefined
function Buffer (arg, encodingOrOffset, length) {
if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
return new Buffer(arg, encodingOrOffset, length)
}

@@ -95,14 +110,14 @@

if (typeof arg === 'number') {
return fromNumber(this, arg)
if (typeof encodingOrOffset === 'string') {
throw new Error(
'If encoding is specified then the first argument must be a string'
)
}
return allocUnsafe(this, arg)
}
return from(this, arg, encodingOrOffset, length)
}
// Slightly less common case.
if (typeof arg === 'string') {
return fromString(this, arg, arguments.length > 1 ? arguments[1] : 'utf8')
}
Buffer.poolSize = 8192 // not used by this implementation
// Unusual.
return fromObject(this, arg)
}
// TODO: Legacy, not needed anymore. Remove in next major version.

@@ -114,58 +129,80 @@ Buffer._augment = function (arr) {

function fromNumber (that, length) {
that = allocate(that, length < 0 ? 0 : checked(length) | 0)
if (!Buffer.TYPED_ARRAY_SUPPORT) {
for (var i = 0; i < length; i++) {
that[i] = 0
}
function from (that, value, encodingOrOffset, length) {
if (typeof value === 'number') {
throw new TypeError('"value" argument must not be a number')
}
return that
}
function fromString (that, string, encoding) {
if (typeof encoding !== 'string' || encoding === '') encoding = 'utf8'
if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
return fromArrayBuffer(that, value, encodingOrOffset, length)
}
// Assumption: byteLength() return value is always < kMaxLength.
var length = byteLength(string, encoding) | 0
that = allocate(that, length)
if (typeof value === 'string') {
return fromString(that, value, encodingOrOffset)
}
that.write(string, encoding)
return that
return fromObject(that, value)
}
function fromObject (that, object) {
if (Buffer.isBuffer(object)) return fromBuffer(that, object)
/**
* Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
* if value is a number.
* Buffer.from(str[, encoding])
* Buffer.from(array)
* Buffer.from(buffer)
* Buffer.from(arrayBuffer[, byteOffset[, length]])
**/
Buffer.from = function (value, encodingOrOffset, length) {
return from(null, value, encodingOrOffset, length)
}
if (isArray(object)) return fromArray(that, object)
if (Buffer.TYPED_ARRAY_SUPPORT) {
Buffer.prototype.__proto__ = Uint8Array.prototype
Buffer.__proto__ = Uint8Array
if (typeof Symbol !== 'undefined' && Symbol.species &&
Buffer[Symbol.species] === Buffer) {
// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
Object.defineProperty(Buffer, Symbol.species, {
value: null,
configurable: true
})
}
}
if (object == null) {
throw new TypeError('must start with number, buffer, array or string')
function assertSize (size) {
if (typeof size !== 'number') {
throw new TypeError('"size" argument must be a number')
}
}
if (typeof ArrayBuffer !== 'undefined') {
if (object.buffer instanceof ArrayBuffer) {
return fromTypedArray(that, object)
}
if (object instanceof ArrayBuffer) {
return fromArrayBuffer(that, object)
}
function alloc (that, size, fill, encoding) {
assertSize(size)
if (size <= 0) {
return createBuffer(that, size)
}
if (object.length) return fromArrayLike(that, object)
return fromJsonObject(that, object)
if (fill !== undefined) {
// Only pay attention to encoding if it's a string. This
// prevents accidentally sending in a number that would
// be interpretted as a start offset.
return typeof encoding === 'string'
? createBuffer(that, size).fill(fill, encoding)
: createBuffer(that, size).fill(fill)
}
return createBuffer(that, size)
}
function fromBuffer (that, buffer) {
var length = checked(buffer.length) | 0
that = allocate(that, length)
buffer.copy(that, 0, 0, length)
return that
/**
* Creates a new filled Buffer instance.
* alloc(size[, fill[, encoding]])
**/
Buffer.alloc = function (size, fill, encoding) {
return alloc(null, size, fill, encoding)
}
function fromArray (that, array) {
var length = checked(array.length) | 0
that = allocate(that, length)
for (var i = 0; i < length; i += 1) {
that[i] = array[i] & 255
function allocUnsafe (that, size) {
assertSize(size)
that = createBuffer(that, size < 0 ? 0 : checked(size) | 0)
if (!Buffer.TYPED_ARRAY_SUPPORT) {
for (var i = 0; i < size; i++) {
that[i] = 0
}
}

@@ -175,26 +212,28 @@ return that

// Duplicate of fromArray() to keep fromArray() monomorphic.
function fromTypedArray (that, array) {
var length = checked(array.length) | 0
that = allocate(that, length)
// Truncating the elements is probably not what people expect from typed
// arrays with BYTES_PER_ELEMENT > 1 but it's compatible with the behavior
// of the old Buffer constructor.
for (var i = 0; i < length; i += 1) {
that[i] = array[i] & 255
}
return that
/**
* Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
* */
Buffer.allocUnsafe = function (size) {
return allocUnsafe(null, size)
}
/**
* Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
*/
Buffer.allocUnsafeSlow = function (size) {
return allocUnsafe(null, size)
}
function fromArrayBuffer (that, array) {
array.byteLength // this throws if `array` is not a valid ArrayBuffer
function fromString (that, string, encoding) {
if (typeof encoding !== 'string' || encoding === '') {
encoding = 'utf8'
}
if (Buffer.TYPED_ARRAY_SUPPORT) {
// Return an augmented `Uint8Array` instance, for best performance
that = new Uint8Array(array)
that.__proto__ = Buffer.prototype
} else {
// Fallback: Return an object instance of the Buffer class
that = fromTypedArray(that, new Uint8Array(array))
if (!Buffer.isEncoding(encoding)) {
throw new TypeError('"encoding" must be a valid string encoding')
}
var length = byteLength(string, encoding) | 0
that = createBuffer(that, length)
that.write(string, encoding)
return that

@@ -205,3 +244,3 @@ }

var length = checked(array.length) | 0
that = allocate(that, length)
that = createBuffer(that, length)
for (var i = 0; i < length; i += 1) {

@@ -213,51 +252,58 @@ that[i] = array[i] & 255

// Deserialize { type: 'Buffer', data: [1,2,3,...] } into a Buffer object.
// Returns a zero-length buffer for inputs that don't conform to the spec.
function fromJsonObject (that, object) {
var array
var length = 0
function fromArrayBuffer (that, array, byteOffset, length) {
array.byteLength // this throws if `array` is not a valid ArrayBuffer
if (object.type === 'Buffer' && isArray(object.data)) {
array = object.data
length = checked(array.length) | 0
if (byteOffset < 0 || array.byteLength < byteOffset) {
throw new RangeError('\'offset\' is out of bounds')
}
that = allocate(that, length)
for (var i = 0; i < length; i += 1) {
that[i] = array[i] & 255
if (array.byteLength < byteOffset + (length || 0)) {
throw new RangeError('\'length\' is out of bounds')
}
return that
}
if (Buffer.TYPED_ARRAY_SUPPORT) {
Buffer.prototype.__proto__ = Uint8Array.prototype
Buffer.__proto__ = Uint8Array
if (typeof Symbol !== 'undefined' && Symbol.species &&
Buffer[Symbol.species] === Buffer) {
// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
Object.defineProperty(Buffer, Symbol.species, {
value: null,
configurable: true
})
if (length === undefined) {
array = new Uint8Array(array, byteOffset)
} else {
array = new Uint8Array(array, byteOffset, length)
}
} else {
// pre-set for values that may exist in the future
Buffer.prototype.length = undefined
Buffer.prototype.parent = undefined
}
function allocate (that, length) {
if (Buffer.TYPED_ARRAY_SUPPORT) {
// Return an augmented `Uint8Array` instance, for best performance
that = new Uint8Array(length)
that = array
that.__proto__ = Buffer.prototype
} else {
// Fallback: Return an object instance of the Buffer class
that.length = length
that = fromArrayLike(that, array)
}
return that
}
var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1
if (fromPool) that.parent = rootParent
function fromObject (that, obj) {
if (Buffer.isBuffer(obj)) {
var len = checked(obj.length) | 0
that = createBuffer(that, len)
return that
if (that.length === 0) {
return that
}
obj.copy(that, 0, 0, len)
return that
}
if (obj) {
if ((typeof ArrayBuffer !== 'undefined' &&
obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
if (typeof obj.length !== 'number' || isnan(obj.length)) {
return createBuffer(that, 0)
}
return fromArrayLike(that, obj)
}
if (obj.type === 'Buffer' && isArray(obj.data)) {
return fromArrayLike(that, obj.data)
}
}
throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
}

@@ -275,8 +321,7 @@

function SlowBuffer (subject, encoding) {
if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding)
var buf = new Buffer(subject, encoding)
delete buf.parent
return buf
function SlowBuffer (length) {
if (+length != length) { // eslint-disable-line eqeqeq
length = 0
}
return Buffer.alloc(+length)
}

@@ -331,6 +376,8 @@

Buffer.concat = function concat (list, length) {
if (!isArray(list)) throw new TypeError('list argument must be an Array of Buffers.')
if (!isArray(list)) {
throw new TypeError('"list" argument must be an Array of Buffers')
}
if (list.length === 0) {
return new Buffer(0)
return Buffer.alloc(0)
}

@@ -346,14 +393,26 @@

var buf = new Buffer(length)
var buffer = Buffer.allocUnsafe(length)
var pos = 0
for (i = 0; i < list.length; i++) {
var item = list[i]
item.copy(buf, pos)
pos += item.length
var buf = list[i]
if (!Buffer.isBuffer(buf)) {
throw new TypeError('"list" argument must be an Array of Buffers')
}
buf.copy(buffer, pos)
pos += buf.length
}
return buf
return buffer
}
function byteLength (string, encoding) {
if (typeof string !== 'string') string = '' + string
if (Buffer.isBuffer(string)) {
return string.length
}
if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
(ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
return string.byteLength
}
if (typeof string !== 'string') {
string = '' + string
}

@@ -375,2 +434,3 @@ var len = string.length

case 'utf-8':
case undefined:
return utf8ToBytes(string).length

@@ -398,9 +458,35 @@ case 'ucs2':

start = start | 0
end = end === undefined || end === Infinity ? this.length : end | 0
// No need to verify that "this.length <= MAX_UINT32" since it's a read-only
// property of a typed array.
// This behaves neither like String nor Uint8Array in that we set start/end
// to their upper/lower bounds if the value passed is out of range.
// undefined is handled specially as per ECMA-262 6th Edition,
// Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
if (start === undefined || start < 0) {
start = 0
}
// Return early if start > this.length. Done here to prevent potential uint32
// coercion fail below.
if (start > this.length) {
return ''
}
if (end === undefined || end > this.length) {
end = this.length
}
if (end <= 0) {
return ''
}
// Force coersion to uint32. This will also coerce falsey/NaN values to 0.
end >>>= 0
start >>>= 0
if (end <= start) {
return ''
}
if (!encoding) encoding = 'utf8'
if (start < 0) start = 0
if (end > this.length) end = this.length
if (end <= start) return ''

@@ -443,2 +529,31 @@ while (true) {

function swap (b, n, m) {
var i = b[n]
b[n] = b[m]
b[m] = i
}
Buffer.prototype.swap16 = function swap16 () {
var len = this.length
if (len % 2 !== 0) {
throw new RangeError('Buffer size must be a multiple of 16-bits')
}
for (var i = 0; i < len; i += 2) {
swap(this, i, i + 1)
}
return this
}
Buffer.prototype.swap32 = function swap32 () {
var len = this.length
if (len % 4 !== 0) {
throw new RangeError('Buffer size must be a multiple of 32-bits')
}
for (var i = 0; i < len; i += 4) {
swap(this, i, i + 3)
swap(this, i + 1, i + 2)
}
return this
}
Buffer.prototype.toString = function toString () {

@@ -467,10 +582,110 @@ var length = this.length | 0

Buffer.prototype.compare = function compare (b) {
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
return Buffer.compare(this, b)
Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
if (!Buffer.isBuffer(target)) {
throw new TypeError('Argument must be a Buffer')
}
if (start === undefined) {
start = 0
}
if (end === undefined) {
end = target ? target.length : 0
}
if (thisStart === undefined) {
thisStart = 0
}
if (thisEnd === undefined) {
thisEnd = this.length
}
if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
throw new RangeError('out of range index')
}
if (thisStart >= thisEnd && start >= end) {
return 0
}
if (thisStart >= thisEnd) {
return -1
}
if (start >= end) {
return 1
}
start >>>= 0
end >>>= 0
thisStart >>>= 0
thisEnd >>>= 0
if (this === target) return 0
var x = thisEnd - thisStart
var y = end - start
var len = Math.min(x, y)
var thisCopy = this.slice(thisStart, thisEnd)
var targetCopy = target.slice(start, end)
for (var i = 0; i < len; ++i) {
if (thisCopy[i] !== targetCopy[i]) {
x = thisCopy[i]
y = targetCopy[i]
break
}
}
if (x < y) return -1
if (y < x) return 1
return 0
}
Buffer.prototype.indexOf = function indexOf (val, byteOffset) {
if (byteOffset > 0x7fffffff) byteOffset = 0x7fffffff
else if (byteOffset < -0x80000000) byteOffset = -0x80000000
function arrayIndexOf (arr, val, byteOffset, encoding) {
var indexSize = 1
var arrLength = arr.length
var valLength = val.length
if (encoding !== undefined) {
encoding = String(encoding).toLowerCase()
if (encoding === 'ucs2' || encoding === 'ucs-2' ||
encoding === 'utf16le' || encoding === 'utf-16le') {
if (arr.length < 2 || val.length < 2) {
return -1
}
indexSize = 2
arrLength /= 2
valLength /= 2
byteOffset /= 2
}
}
function read (buf, i) {
if (indexSize === 1) {
return buf[i]
} else {
return buf.readUInt16BE(i * indexSize)
}
}
var foundIndex = -1
for (var i = 0; byteOffset + i < arrLength; i++) {
if (read(arr, byteOffset + i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
if (foundIndex === -1) foundIndex = i
if (i - foundIndex + 1 === valLength) return (byteOffset + foundIndex) * indexSize
} else {
if (foundIndex !== -1) i -= i - foundIndex
foundIndex = -1
}
}
return -1
}
Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
if (typeof byteOffset === 'string') {
encoding = byteOffset
byteOffset = 0
} else if (byteOffset > 0x7fffffff) {
byteOffset = 0x7fffffff
} else if (byteOffset < -0x80000000) {
byteOffset = -0x80000000
}
byteOffset >>= 0

@@ -485,7 +700,11 @@

if (typeof val === 'string') {
if (val.length === 0) return -1 // special case: looking for empty string always fails
return String.prototype.indexOf.call(this, val, byteOffset)
val = Buffer.from(val, encoding)
}
if (Buffer.isBuffer(val)) {
return arrayIndexOf(this, val, byteOffset)
// special case: looking for empty string/buffer always fails
if (val.length === 0) {
return -1
}
return arrayIndexOf(this, val, byteOffset, encoding)
}

@@ -496,21 +715,12 @@ if (typeof val === 'number') {

}
return arrayIndexOf(this, [ val ], byteOffset)
return arrayIndexOf(this, [ val ], byteOffset, encoding)
}
function arrayIndexOf (arr, val, byteOffset) {
var foundIndex = -1
for (var i = 0; byteOffset + i < arr.length; i++) {
if (arr[byteOffset + i] === val[foundIndex === -1 ? 0 : i - foundIndex]) {
if (foundIndex === -1) foundIndex = i
if (i - foundIndex + 1 === val.length) return byteOffset + foundIndex
} else {
foundIndex = -1
}
}
return -1
}
throw new TypeError('val must be string, number or Buffer')
}
Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
return this.indexOf(val, byteOffset, encoding) !== -1
}
function hexWrite (buf, string, offset, length) {

@@ -537,3 +747,3 @@ offset = Number(offset) || 0

var parsed = parseInt(string.substr(i * 2, 2), 16)
if (isNaN(parsed)) throw new Error('Invalid hex string')
if (isNaN(parsed)) return i
buf[offset + i] = parsed

@@ -587,6 +797,5 @@ }

} else {
var swap = encoding
encoding = offset
offset = length | 0
length = swap
throw new Error(
'Buffer.write(string, encoding, offset[, length]) is no longer supported'
)
}

@@ -598,3 +807,3 @@

if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
throw new RangeError('attempt to write outside buffer bounds')
throw new RangeError('Attempt to write outside buffer bounds')
}

@@ -824,4 +1033,2 @@

if (newBuf.length) newBuf.parent = this.parent || this
return newBuf

@@ -995,5 +1202,5 @@ }

function checkInt (buf, value, offset, ext, max, min) {
if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance')
if (value > max || value < min) throw new RangeError('value is out of bounds')
if (offset + ext > buf.length) throw new RangeError('index out of range')
if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
if (offset + ext > buf.length) throw new RangeError('Index out of range')
}

@@ -1005,3 +1212,6 @@

byteLength = byteLength | 0
if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0)
if (!noAssert) {
var maxBytes = Math.pow(2, 8 * byteLength) - 1
checkInt(this, value, offset, byteLength, maxBytes, 0)
}

@@ -1022,3 +1232,6 @@ var mul = 1

byteLength = byteLength | 0
if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0)
if (!noAssert) {
var maxBytes = Math.pow(2, 8 * byteLength) - 1
checkInt(this, value, offset, byteLength, maxBytes, 0)
}

@@ -1126,5 +1339,8 @@ var i = byteLength - 1

var mul = 1
var sub = value < 0 ? 1 : 0
var sub = 0
this[offset] = value & 0xFF
while (++i < byteLength && (mul *= 0x100)) {
if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
sub = 1
}
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF

@@ -1147,5 +1363,8 @@ }

var mul = 1
var sub = value < 0 ? 1 : 0
var sub = 0
this[offset + i] = value & 0xFF
while (--i >= 0 && (mul *= 0x100)) {
if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
sub = 1
}
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF

@@ -1225,4 +1444,4 @@ }

function checkIEEE754 (buf, value, offset, ext, max, min) {
if (offset + ext > buf.length) throw new RangeError('index out of range')
if (offset < 0) throw new RangeError('index out of range')
if (offset + ext > buf.length) throw new RangeError('Index out of range')
if (offset < 0) throw new RangeError('Index out of range')
}

@@ -1311,27 +1530,59 @@

// fill(value, start=0, end=buffer.length)
Buffer.prototype.fill = function fill (value, start, end) {
if (!value) value = 0
if (!start) start = 0
if (!end) end = this.length
// Usage:
// buffer.fill(number[, offset[, end]])
// buffer.fill(buffer[, offset[, end]])
// buffer.fill(string[, offset[, end]][, encoding])
Buffer.prototype.fill = function fill (val, start, end, encoding) {
// Handle string cases:
if (typeof val === 'string') {
if (typeof start === 'string') {
encoding = start
start = 0
end = this.length
} else if (typeof end === 'string') {
encoding = end
end = this.length
}
if (val.length === 1) {
var code = val.charCodeAt(0)
if (code < 256) {
val = code
}
}
if (encoding !== undefined && typeof encoding !== 'string') {
throw new TypeError('encoding must be a string')
}
if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
throw new TypeError('Unknown encoding: ' + encoding)
}
} else if (typeof val === 'number') {
val = val & 255
}
if (end < start) throw new RangeError('end < start')
// Invalid ranges are not set to a default, so can range check early.
if (start < 0 || this.length < start || this.length < end) {
throw new RangeError('Out of range index')
}
// Fill 0 bytes; we're done
if (end === start) return
if (this.length === 0) return
if (end <= start) {
return this
}
if (start < 0 || start >= this.length) throw new RangeError('start out of bounds')
if (end < 0 || end > this.length) throw new RangeError('end out of bounds')
start = start >>> 0
end = end === undefined ? this.length : end >>> 0
if (!val) val = 0
var i
if (typeof value === 'number') {
if (typeof val === 'number') {
for (i = start; i < end; i++) {
this[i] = value
this[i] = val
}
} else {
var bytes = utf8ToBytes(value.toString())
var bytes = Buffer.isBuffer(val)
? val
: utf8ToBytes(new Buffer(val, encoding).toString())
var len = bytes.length
for (i = start; i < end; i++) {
this[i] = bytes[i % len]
for (i = 0; i < end - start; i++) {
this[i + start] = bytes[i % len]
}

@@ -1486,1 +1737,5 @@ }

}
function isnan (val) {
return val !== val // eslint-disable-line no-self-compare
}
{
"name": "buffer",
"description": "Node.js Buffer API, for the browser",
"version": "4.5.1",
"version": "4.6.0",
"author": {

@@ -53,5 +53,7 @@ "name": "Feross Aboukhadijeh",

"test": "standard && node ./bin/test.js",
"test-browser": "zuul -- test/*.js test/node/*.js",
"test-browser-local": "zuul --local -- test/*.js test/node/*.js",
"test-node": "tape test/*.js test/node/*.js test/node-es6/*.js && OBJECT_IMPL=true tape test/*.js test/node/*.js",
"test-browser-es5": "zuul --ui tape -- test/*.js",
"test-browser-es5-local": "zuul --ui tape --local -- test/*.js",
"test-browser-es6": "zuul --ui tape -- test/*.js test/node/*.js",
"test-browser-es6-local": "zuul --ui tape --local -- test/*.js test/node/*.js",
"test-node": "tape test/*.js test/node/*.js && OBJECT_IMPL=true tape test/*.js",
"perf": "browserify --debug perf/bracket-notation.js > perf/bundle.js && open perf/index.html",

@@ -64,3 +66,2 @@ "perf-node": "node perf/bracket-notation.js && node perf/concat.js && node perf/copy-big.js && node perf/copy.js && node perf/new-big.js && node perf/new.js && node perf/readDoubleBE.js && node perf/readFloatBE.js && node perf/readUInt32LE.js && node perf/slice.js && node perf/writeFloatBE.js",

"test/node/*.js",
"test/node-es6/*.js",
"test/_polyfill.js",

@@ -67,0 +68,0 @@ "perf/*.js"

'use strict';
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
var common = {};
if (Buffer.TYPED_ARRAY_SUPPORT) return;
var assert = require('assert');

@@ -9,3 +9,3 @@

// it doesn't do transliteration.
assert.equal(Buffer('hérité').toString('ascii'), 'hC)ritC)');
assert.equal(Buffer.from('hérité').toString('ascii'), 'hC)ritC)');

@@ -20,3 +20,3 @@ // 71 characters, 78 bytes. The ’ character is a triple-byte sequence.

var buf = Buffer(input);
var buf = Buffer.from(input);

@@ -23,0 +23,0 @@ for (var i = 0; i < expected.length; ++i) {

'use strict';
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
if (Buffer.TYPED_ARRAY_SUPPORT) return;
var common = {};
var assert = require('assert');
var Buffer = require('../../').Buffer;
var SlowBuffer = require('../../').SlowBuffer;
// coerce values to string
assert.equal(Buffer.byteLength(32, 'raw'), 2);
assert.equal(Buffer.byteLength(32, 'binary'), 2);
assert.equal(Buffer.byteLength(NaN, 'utf8'), 3);
assert.equal(Buffer.byteLength({}, 'raws'), 15);
assert.equal(Buffer.byteLength({}, 'binary'), 15);
assert.equal(Buffer.byteLength(), 9);
var buff = new Buffer(10);
assert(ArrayBuffer.isView(buff));
var slowbuff = new SlowBuffer(10);
assert(ArrayBuffer.isView(slowbuff));
// buffer
var incomplete = Buffer.from([0xe4, 0xb8, 0xad, 0xe6, 0x96]);
assert.equal(Buffer.byteLength(incomplete), 5);
var ascii = Buffer.from('abc');
assert.equal(Buffer.byteLength(ascii), 3);
// ArrayBuffer
var buffer = new ArrayBuffer(8);
assert.equal(Buffer.byteLength(buffer), 8);
// TypedArray
var int8 = new Int8Array(8);
assert.equal(Buffer.byteLength(int8), 8);
var uint8 = new Uint8Array(8);
assert.equal(Buffer.byteLength(uint8), 8);
var uintc8 = new Uint8ClampedArray(2);
assert.equal(Buffer.byteLength(uintc8), 2);
var int16 = new Int16Array(8);
assert.equal(Buffer.byteLength(int16), 16);
var uint16 = new Uint16Array(8);
assert.equal(Buffer.byteLength(uint16), 16);
var int32 = new Int32Array(8);
assert.equal(Buffer.byteLength(int32), 32);
var uint32 = new Uint32Array(8);
assert.equal(Buffer.byteLength(uint32), 32);
var float32 = new Float32Array(8);
assert.equal(Buffer.byteLength(float32), 32);
var float64 = new Float64Array(8);
assert.equal(Buffer.byteLength(float64), 64);
// DataView
var dv = new DataView(new ArrayBuffer(2));
assert.equal(Buffer.byteLength(dv), 2);
// special case: zero length string

@@ -16,0 +56,0 @@ assert.equal(Buffer.byteLength('', 'ascii'), 0);

'use strict';
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
var common = {};
if (Buffer.TYPED_ARRAY_SUPPORT) return;
var assert = require('assert');
var zero = [];
var one = [ new Buffer('asdf') ];
var one = [ Buffer.from('asdf') ];
var long = [];
for (var i = 0; i < 10; i++) long.push(new Buffer('asdf'));
for (var i = 0; i < 10; i++) long.push(Buffer.from('asdf'));

@@ -25,7 +25,17 @@ var flatZero = Buffer.concat(zero);

assert.throws(function() {
Buffer.concat([42]);
}, TypeError);
assertWrongList();
assertWrongList(null);
assertWrongList(Buffer.from('hello'));
assertWrongList([42]);
assertWrongList(['hello', 'world']);
assertWrongList(['hello', Buffer.from('world')]);
// console.log('ok');
function assertWrongList(value) {
assert.throws(function() {
Buffer.concat(value);
}, function(err) {
return err instanceof TypeError &&
err.message === '"list" argument must be an Array of Buffers';
});
}
'use strict';
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
var common = {};
if (Buffer.TYPED_ARRAY_SUPPORT) return;
var assert = require('assert');

@@ -9,8 +9,8 @@

var b = new Buffer('abcdef');
var buf_a = new Buffer('a');
var buf_bc = new Buffer('bc');
var buf_f = new Buffer('f');
var buf_z = new Buffer('z');
var buf_empty = new Buffer('');
var b = Buffer.from('abcdef');
var buf_a = Buffer.from('a');
var buf_bc = Buffer.from('bc');
var buf_f = Buffer.from('f');
var buf_z = Buffer.from('z');
var buf_empty = Buffer.from('');

@@ -71,2 +71,210 @@ assert.equal(b.indexOf('a'), 0);

// test offsets
assert.equal(b.indexOf('d', 2), 3);
assert.equal(b.indexOf('f', 5), 5);
assert.equal(b.indexOf('f', -1), 5);
assert.equal(b.indexOf('f', 6), -1);
assert.equal(b.indexOf(Buffer.from('d'), 2), 3);
assert.equal(b.indexOf(Buffer.from('f'), 5), 5);
assert.equal(b.indexOf(Buffer.from('f'), -1), 5);
assert.equal(b.indexOf(Buffer.from('f'), 6), -1);
assert.equal(Buffer.from('ff').indexOf(Buffer.from('f'), 1, 'ucs2'), -1);
// test hex encoding
assert.equal(
Buffer.from(b.toString('hex'), 'hex')
.indexOf('64', 0, 'hex'), 3);
assert.equal(
Buffer.from(b.toString('hex'), 'hex')
.indexOf(Buffer.from('64', 'hex'), 0, 'hex'), 3);
// test base64 encoding
assert.equal(
Buffer.from(b.toString('base64'), 'base64')
.indexOf('ZA==', 0, 'base64'), 3);
assert.equal(
Buffer.from(b.toString('base64'), 'base64')
.indexOf(Buffer.from('ZA==', 'base64'), 0, 'base64'), 3);
// test ascii encoding
assert.equal(
Buffer.from(b.toString('ascii'), 'ascii')
.indexOf('d', 0, 'ascii'), 3);
assert.equal(
Buffer.from(b.toString('ascii'), 'ascii')
.indexOf(Buffer.from('d', 'ascii'), 0, 'ascii'), 3);
// test binary encoding
assert.equal(
Buffer.from(b.toString('binary'), 'binary')
.indexOf('d', 0, 'binary'), 3);
assert.equal(
Buffer.from(b.toString('binary'), 'binary')
.indexOf(Buffer.from('d', 'binary'), 0, 'binary'), 3);
assert.equal(
Buffer.from('aa\u00e8aa', 'binary')
.indexOf('\u00e8', 'binary'), 2);
assert.equal(
Buffer.from('\u00e8', 'binary')
.indexOf('\u00e8', 'binary'), 0);
assert.equal(
Buffer.from('\u00e8', 'binary')
.indexOf(Buffer.from('\u00e8', 'binary'), 'binary'), 0);
// test optional offset with passed encoding
assert.equal(Buffer.from('aaaa0').indexOf('30', 'hex'), 4);
assert.equal(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4);
{
// test usc2 encoding
var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
assert.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2'));
assert.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2'));
assert.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2'));
assert.equal(4, twoByteString.indexOf(
Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2'));
assert.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
}
var mixedByteStringUcs2 =
Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
assert.equal(6, mixedByteStringUcs2.indexOf('bc', 0, 'ucs2'));
assert.equal(10, mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2'));
assert.equal(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2'));
assert.equal(
6, mixedByteStringUcs2.indexOf(Buffer.from('bc', 'ucs2'), 0, 'ucs2'));
assert.equal(
10, mixedByteStringUcs2.indexOf(Buffer.from('\u03a3', 'ucs2'), 0, 'ucs2'));
assert.equal(
-1, mixedByteStringUcs2.indexOf(Buffer.from('\u0396', 'ucs2'), 0, 'ucs2'));
{
var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
// Test single char pattern
assert.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2'));
assert.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha');
assert.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma');
assert.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma');
assert.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon');
assert.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta');
// Test multi-char pattern
assert.equal(
0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
assert.equal(
2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
assert.equal(
4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
assert.equal(
6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
}
var mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395');
assert.equal(5, mixedByteStringUtf8.indexOf('bc'));
assert.equal(5, mixedByteStringUtf8.indexOf('bc', 5));
assert.equal(5, mixedByteStringUtf8.indexOf('bc', -8));
assert.equal(7, mixedByteStringUtf8.indexOf('\u03a3'));
assert.equal(-1, mixedByteStringUtf8.indexOf('\u0396'));
// Test complex string indexOf algorithms. Only trigger for long strings.
// Long string that isn't a simple repeat of a shorter string.
var longString = 'A';
for (var i = 66; i < 76; i++) { // from 'B' to 'K'
longString = longString + String.fromCharCode(i) + longString;
}
var longBufferString = Buffer.from(longString);
// pattern of 15 chars, repeated every 16 chars in long
var pattern = 'ABACABADABACABA';
for (var i = 0; i < longBufferString.length - pattern.length; i += 7) {
var index = longBufferString.indexOf(pattern, i);
assert.equal((i + 15) & ~0xf, index, 'Long ABACABA...-string at index ' + i);
}
assert.equal(510, longBufferString.indexOf('AJABACA'), 'Long AJABACA, First J');
assert.equal(
1534, longBufferString.indexOf('AJABACA', 511), 'Long AJABACA, Second J');
pattern = 'JABACABADABACABA';
assert.equal(
511, longBufferString.indexOf(pattern), 'Long JABACABA..., First J');
assert.equal(
1535, longBufferString.indexOf(pattern, 512), 'Long JABACABA..., Second J');
// Search for a non-ASCII string in a pure ASCII string.
var asciiString = Buffer.from(
'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf');
assert.equal(-1, asciiString.indexOf('\x2061'));
assert.equal(3, asciiString.indexOf('leb', 0));
// Search in string containing many non-ASCII chars.
var allCodePoints = [];
for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
var allCharsString = String.fromCharCode.apply(String, allCodePoints);
var allCharsBufferUtf8 = Buffer.from(allCharsString);
var allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2');
// Search for string long enough to trigger complex search with ASCII pattern
// and UC16 subject.
assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
{
// Find substrings in Utf8.
var lengths = [1, 3, 15]; // Single char, simple and complex.
var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
for (var i = 0; i < indices.length; i++) {
var index = indices[i];
var length = lengths[lengthIndex];
if (index + length > 0x7F) {
length = 2 * length;
}
if (index + length > 0x7FF) {
length = 3 * length;
}
if (index + length > 0xFFFF) {
length = 4 * length;
}
var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
assert.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8));
var patternStringUtf8 = patternBufferUtf8.toString();
assert.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8));
}
}
}
{
// Find substrings in Usc2.
var lengths = [2, 4, 16]; // Single char, simple and complex.
var indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
for (var i = 0; i < indices.length; i++) {
var index = indices[i] * 2;
var length = lengths[lengthIndex];
var patternBufferUcs2 =
allCharsBufferUcs2.slice(index, index + length);
assert.equal(
index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2'));
var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
assert.equal(
index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2'));
}
}
}
assert.throws(function() {

@@ -73,0 +281,0 @@ b.indexOf(function() { });

'use strict';
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
var common = {};
if (Buffer.TYPED_ARRAY_SUPPORT) return;
var assert = require('assert');

@@ -13,6 +13,6 @@

var b = new Buffer(4);
var b = Buffer.allocUnsafe(4);
b.fill('1234');
var s = new buffer.SlowBuffer(4);
var s = buffer.SlowBuffer(4);
s.fill('1234');

@@ -25,6 +25,6 @@

b = new Buffer(2);
b = Buffer.allocUnsafe(2);
b.fill('12');
s = new buffer.SlowBuffer(2);
s = buffer.SlowBuffer(2);
s.fill('12');

@@ -31,0 +31,0 @@

'use strict';
if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
if (Buffer.TYPED_ARRAY_SUPPORT) return;
var common = {};

@@ -18,3 +18,3 @@ var assert = require('assert');

b[0] = 255;
b[0] = -1;
assert.strictEqual(b[0], 255);

@@ -30,6 +30,13 @@

var c = new Buffer(512);
var c = Buffer(512);
// console.log('c.length == %d', c.length);
assert.strictEqual(512, c.length);
var d = new Buffer([]);
assert.strictEqual(0, d.length);
var ui32 = new Uint32Array(4).fill(42);
var e = Buffer(ui32);
assert.deepEqual(ui32, e);
// First check Buffer#fill() works as expected.

@@ -48,126 +55,137 @@

var buf = new Buffer(64);
buf.fill(10);
for (var i = 0; i < buf.length; i++)
assert.equal(buf[i], 10);
{
var buf = new Buffer(64);
buf.fill(10);
for (var i = 0; i < buf.length; i++)
assert.equal(buf[i], 10);
buf.fill(11, 0, buf.length >> 1);
for (var i = 0; i < buf.length >> 1; i++)
assert.equal(buf[i], 11);
for (var i = (buf.length >> 1) + 1; i < buf.length; i++)
assert.equal(buf[i], 10);
buf.fill(11, 0, buf.length >> 1);
for (var i = 0; i < buf.length >> 1; i++)
assert.equal(buf[i], 11);
for (var i = (buf.length >> 1) + 1; i < buf.length; i++)
assert.equal(buf[i], 10);
buf.fill('h');
for (var i = 0; i < buf.length; i++)
assert.equal('h'.charCodeAt(0), buf[i]);
buf.fill('h');
for (var i = 0; i < buf.length; i++)
assert.equal('h'.charCodeAt(0), buf[i]);
buf.fill(0);
for (var i = 0; i < buf.length; i++)
assert.equal(0, buf[i]);
buf.fill(0);
for (var i = 0; i < buf.length; i++)
assert.equal(0, buf[i]);
buf.fill(null);
for (var i = 0; i < buf.length; i++)
assert.equal(0, buf[i]);
buf.fill(null);
for (var i = 0; i < buf.length; i++)
assert.equal(0, buf[i]);
buf.fill(1, 16, 32);
for (var i = 0; i < 16; i++)
assert.equal(0, buf[i]);
for (; i < 32; i++)
assert.equal(1, buf[i]);
for (; i < buf.length; i++)
assert.equal(0, buf[i]);
buf.fill(1, 16, 32);
for (var i = 0; i < 16; i++)
assert.equal(0, buf[i]);
for (var i = 16; i < 32; i++)
assert.equal(1, buf[i]);
for (var i = 32; i < buf.length; i++)
assert.equal(0, buf[i]);
}
var buf = new Buffer(10);
buf.fill('abc');
assert.equal(buf.toString(), 'abcabcabca');
buf.fill('է');
assert.equal(buf.toString(), 'էէէէէ');
{
var buf = new Buffer(10);
buf.fill('abc');
assert.equal(buf.toString(), 'abcabcabca');
buf.fill('է');
assert.equal(buf.toString(), 'էէէէէ');
}
// copy 512 bytes, from 0 to 512.
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c, 0, 0, 512);
// console.log('copied %d bytes from b into c', copied);
assert.strictEqual(512, copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
{
// copy 512 bytes, from 0 to 512.
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c, 0, 0, 512);
// console.log('copied %d bytes from b into c', copied);
assert.strictEqual(512, copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}
// copy c into b, without specifying sourceEnd
b.fill(++cntr);
c.fill(++cntr);
var copied = c.copy(b, 0, 0);
// console.log('copied %d bytes from c into b w/o sourceEnd', copied);
assert.strictEqual(c.length, copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(c[i], b[i]);
{
// copy c into b, without specifying sourceEnd
b.fill(++cntr);
c.fill(++cntr);
var copied = c.copy(b, 0, 0);
// console.log('copied %d bytes from c into b w/o sourceEnd', copied);
assert.strictEqual(c.length, copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(c[i], b[i]);
}
}
// copy c into b, without specifying sourceStart
b.fill(++cntr);
c.fill(++cntr);
var copied = c.copy(b, 0);
// console.log('copied %d bytes from c into b w/o sourceStart', copied);
assert.strictEqual(c.length, copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(c[i], b[i]);
{
// copy c into b, without specifying sourceStart
b.fill(++cntr);
c.fill(++cntr);
var copied = c.copy(b, 0);
// console.log('copied %d bytes from c into b w/o sourceStart', copied);
assert.strictEqual(c.length, copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(c[i], b[i]);
}
}
// copy longer buffer b to shorter c without targetStart
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c);
// console.log('copied %d bytes from b into c w/o targetStart', copied);
assert.strictEqual(c.length, copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
{
// copy longer buffer b to shorter c without targetStart
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c);
// console.log('copied %d bytes from b into c w/o targetStart', copied);
assert.strictEqual(c.length, copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}
// copy starting near end of b to c
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
// console.log('copied %d bytes from end of b into beginning of c', copied);
assert.strictEqual(Math.floor(c.length / 2), copied);
for (var i = 0; i < Math.floor(c.length / 2); i++) {
assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]);
{
// copy starting near end of b to c
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
// console.log('copied %d bytes from end of b into beginning of c', copied);
assert.strictEqual(Math.floor(c.length / 2), copied);
for (var i = 0; i < Math.floor(c.length / 2); i++) {
assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]);
}
for (var i = Math.floor(c.length / 2) + 1; i < c.length; i++) {
assert.strictEqual(c[c.length - 1], c[i]);
}
}
for (var i = Math.floor(c.length / 2) + 1; i < c.length; i++) {
assert.strictEqual(c[c.length - 1], c[i]);
}
// try to copy 513 bytes, and check we don't overrun c
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c, 0, 0, 513);
// console.log('copied %d bytes from b trying to overrun c', copied);
assert.strictEqual(c.length, copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
{
// try to copy 513 bytes, and check we don't overrun c
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c, 0, 0, 513);
// console.log('copied %d bytes from b trying to overrun c', copied);
assert.strictEqual(c.length, copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}
// copy 768 bytes from b into b
b.fill(++cntr);
b.fill(++cntr, 256);
var copied = b.copy(b, 0, 256, 1024);
// console.log('copied %d bytes from b into b', copied);
assert.strictEqual(768, copied);
for (var i = 0; i < b.length; i++) {
assert.strictEqual(cntr, b[i]);
{
// copy 768 bytes from b into b
b.fill(++cntr);
b.fill(++cntr, 256);
var copied = b.copy(b, 0, 256, 1024);
// console.log('copied %d bytes from b into b', copied);
assert.strictEqual(768, copied);
for (var i = 0; i < b.length; i++) {
assert.strictEqual(cntr, b[i]);
}
}
// copy string longer than buffer length (failure will segfault)
var bb = new Buffer(10);
var bb = Buffer(10);
bb.fill('hello crazy world');
var caught_error = null;
// try to copy from before the beginning of b
caught_error = null;
try {
var copied = b.copy(c, 0, 100, 10);
} catch (err) {
caught_error = err;
}
assert.doesNotThrow(() => { b.copy(c, 0, 100, 10); });

@@ -179,9 +197,11 @@ // copy throws at negative sourceStart

// check sourceEnd resets to targetEnd if former is greater than the latter
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c, 0, 0, 1025);
// console.log('copied %d bytes from b into c', copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
{
// check sourceEnd resets to targetEnd if former is greater than the latter
b.fill(++cntr);
c.fill(++cntr);
var copied = b.copy(c, 0, 0, 1025);
// console.log('copied %d bytes from b into c', copied);
for (var i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}

@@ -206,3 +226,3 @@

try {
var copied = b.toString('invalid');
b.toString('invalid');
} catch (err) {

@@ -216,3 +236,3 @@ caught_error = err;

try {
var copied = b.write('test string', 0, 5, 'invalid');
b.write('test string', 0, 5, 'invalid');
} catch (err) {

@@ -227,3 +247,3 @@ caught_error = err;

new Buffer('', 'binary');
new Buffer(0);
Buffer(0);

@@ -251,19 +271,88 @@ // try to write a 0-length string beyond the end of b

// try to copy 0 bytes worth of data into an empty buffer
b.copy(new Buffer(0), 0, 0, 0);
b.copy(Buffer(0), 0, 0, 0);
// try to copy 0 bytes past the end of the target buffer
b.copy(new Buffer(0), 1, 1, 1);
b.copy(new Buffer(1), 1, 1, 1);
b.copy(Buffer(0), 1, 1, 1);
b.copy(Buffer(1), 1, 1, 1);
// try to copy 0 bytes from past the end of the source buffer
b.copy(new Buffer(1), 0, 2048, 2048);
b.copy(Buffer(1), 0, 2048, 2048);
// try to toString() a 0-length slice of a buffer, both within and without the
// valid buffer range
assert.equal(new Buffer('abc').toString('ascii', 0, 0), '');
assert.equal(new Buffer('abc').toString('ascii', -100, -100), '');
assert.equal(new Buffer('abc').toString('ascii', 100, 100), '');
var rangeBuffer = new Buffer('abc');
// if start >= buffer's length, empty string will be returned
assert.equal(rangeBuffer.toString('ascii', 3), '');
assert.equal(rangeBuffer.toString('ascii', +Infinity), '');
assert.equal(rangeBuffer.toString('ascii', 3.14, 3), '');
assert.equal(rangeBuffer.toString('ascii', 'Infinity', 3), '');
// if end <= 0, empty string will be returned
assert.equal(rangeBuffer.toString('ascii', 1, 0), '');
assert.equal(rangeBuffer.toString('ascii', 1, -1.2), '');
assert.equal(rangeBuffer.toString('ascii', 1, -100), '');
assert.equal(rangeBuffer.toString('ascii', 1, -Infinity), '');
// if start < 0, start will be taken as zero
assert.equal(rangeBuffer.toString('ascii', -1, 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', -1.99, 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', -Infinity, 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', '-1', 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', '-1.99', 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc');
// if start is an invalid integer, start will be taken as zero
assert.equal(rangeBuffer.toString('ascii', 'node.js', 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', {}, 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', [], 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', NaN, 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', null, 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', undefined, 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', false, 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', '', 3), 'abc');
// but, if start is an integer when coerced, then it will be coerced and used.
assert.equal(rangeBuffer.toString('ascii', '-1', 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', '1', 3), 'bc');
assert.equal(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', '3', 3), '');
assert.equal(rangeBuffer.toString('ascii', Number(3), 3), '');
assert.equal(rangeBuffer.toString('ascii', '3.14', 3), '');
assert.equal(rangeBuffer.toString('ascii', '1.99', 3), 'bc');
assert.equal(rangeBuffer.toString('ascii', '-1.99', 3), 'abc');
assert.equal(rangeBuffer.toString('ascii', 1.99, 3), 'bc');
assert.equal(rangeBuffer.toString('ascii', true, 3), 'bc');
// if end > buffer's length, end will be taken as buffer's length
assert.equal(rangeBuffer.toString('ascii', 0, 5), 'abc');
assert.equal(rangeBuffer.toString('ascii', 0, 6.99), 'abc');
assert.equal(rangeBuffer.toString('ascii', 0, Infinity), 'abc');
assert.equal(rangeBuffer.toString('ascii', 0, '5'), 'abc');
assert.equal(rangeBuffer.toString('ascii', 0, '6.99'), 'abc');
assert.equal(rangeBuffer.toString('ascii', 0, 'Infinity'), 'abc');
// if end is an invalid integer, end will be taken as buffer's length
assert.equal(rangeBuffer.toString('ascii', 0, 'node.js'), '');
assert.equal(rangeBuffer.toString('ascii', 0, {}), '');
assert.equal(rangeBuffer.toString('ascii', 0, NaN), '');
assert.equal(rangeBuffer.toString('ascii', 0, undefined), 'abc');
assert.equal(rangeBuffer.toString('ascii', 0), 'abc');
assert.equal(rangeBuffer.toString('ascii', 0, null), '');
assert.equal(rangeBuffer.toString('ascii', 0, []), '');
assert.equal(rangeBuffer.toString('ascii', 0, false), '');
assert.equal(rangeBuffer.toString('ascii', 0, ''), '');
// but, if end is an integer when coerced, then it will be coerced and used.
assert.equal(rangeBuffer.toString('ascii', 0, '-1'), '');
assert.equal(rangeBuffer.toString('ascii', 0, '1'), 'a');
assert.equal(rangeBuffer.toString('ascii', 0, '-Infinity'), '');
assert.equal(rangeBuffer.toString('ascii', 0, '3'), 'abc');
assert.equal(rangeBuffer.toString('ascii', 0, Number(3)), 'abc');
assert.equal(rangeBuffer.toString('ascii', 0, '3.14'), 'abc');
assert.equal(rangeBuffer.toString('ascii', 0, '1.99'), 'a');
assert.equal(rangeBuffer.toString('ascii', 0, '-1.99'), '');
assert.equal(rangeBuffer.toString('ascii', 0, 1.99), 'a');
assert.equal(rangeBuffer.toString('ascii', 0, true), 'a');
// try toString() with a object as a encoding
assert.equal(new Buffer('abc').toString({toString: function() {
assert.equal(rangeBuffer.toString({toString: function() {
return 'ascii';

@@ -275,28 +364,38 @@ }}), 'abc');

writeTest.write('n', 'ascii');
writeTest.write('o', 'ascii', '1');
writeTest.write('o', '1', 'ascii');
writeTest.write('d', '2', 'ascii');
writeTest.write('e', 3, 'ascii');
writeTest.write('j', 'ascii', 4);
writeTest.write('j', 4, 'ascii');
assert.equal(writeTest.toString(), 'nodejs');
// ASCII slice test
{
var asciiString = 'hello world';
var asciiString = 'hello world';
var offset = 100;
for (var i = 0; i < asciiString.length; i++) {
b[i] = asciiString.charCodeAt(i);
}
var asciiSlice = b.toString('ascii', 0, asciiString.length);
assert.equal(asciiString, asciiSlice);
}
for (var i = 0; i < asciiString.length; i++) {
b[i] = asciiString.charCodeAt(i);
{
var asciiString = 'hello world';
var offset = 100;
var written = b.write(asciiString, offset, 'ascii');
assert.equal(asciiString.length, written);
var asciiSlice = b.toString('ascii', offset, offset + asciiString.length);
assert.equal(asciiString, asciiSlice);
}
var asciiSlice = b.toString('ascii', 0, asciiString.length);
assert.equal(asciiString, asciiSlice);
var written = b.write(asciiString, offset, 'ascii');
assert.equal(asciiString.length, written);
var asciiSlice = b.toString('ascii', offset, offset + asciiString.length);
assert.equal(asciiString, asciiSlice);
{
var asciiString = 'hello world';
var offset = 100;
var sliceA = b.slice(offset, offset + asciiString.length);
var sliceB = b.slice(offset, offset + asciiString.length);
for (var i = 0; i < asciiString.length; i++) {
assert.equal(sliceA[i], sliceB[i]);
var sliceA = b.slice(offset, offset + asciiString.length);
var sliceB = b.slice(offset, offset + asciiString.length);
for (var i = 0; i < asciiString.length; i++) {
assert.equal(sliceA[i], sliceB[i]);
}
}

@@ -324,80 +423,101 @@

var slice = b.slice(100, 150);
assert.equal(50, slice.length);
for (var i = 0; i < 50; i++) {
assert.equal(b[100 + i], slice[i]);
{
var slice = b.slice(100, 150);
assert.equal(50, slice.length);
for (var i = 0; i < 50; i++) {
assert.equal(b[100 + i], slice[i]);
}
}
{
// make sure only top level parent propagates from allocPool
var b = new Buffer(5);
var c = b.slice(0, 4);
var d = c.slice(0, 2);
assert.equal(b.parent, c.parent);
assert.equal(b.parent, d.parent);
}
// make sure only top level parent propagates from allocPool
var b = new Buffer(5);
var c = b.slice(0, 4);
var d = c.slice(0, 2);
assert.equal(b.parent, c.parent);
assert.equal(b.parent, d.parent);
{
// also from a non-pooled instance
var b = new SlowBuffer(5);
var c = b.slice(0, 4);
var d = c.slice(0, 2);
assert.equal(c.parent, d.parent);
}
// also from a non-pooled instance
var b = new SlowBuffer(5);
var c = b.slice(0, 4);
var d = c.slice(0, 2);
{
// Bug regression test
var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語
var buffer = new Buffer(32);
var size = buffer.write(testValue, 0, 'utf8');
// console.log('bytes written to buffer: ' + size);
var slice = buffer.toString('utf8', 0, size);
assert.equal(slice, testValue);
}
{
// Test triple slice
var a = new Buffer(8);
for (var i = 0; i < 8; i++) a[i] = i;
var b = a.slice(4, 8);
assert.equal(4, b[0]);
assert.equal(5, b[1]);
assert.equal(6, b[2]);
assert.equal(7, b[3]);
var c = b.slice(2, 4);
assert.equal(6, c[0]);
assert.equal(7, c[1]);
}
// Bug regression test
var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語
var buffer = new Buffer(32);
var size = buffer.write(testValue, 0, 'utf8');
// console.log('bytes written to buffer: ' + size);
var slice = buffer.toString('utf8', 0, size);
assert.equal(slice, testValue);
{
var d = new Buffer([23, 42, 255]);
assert.equal(d.length, 3);
assert.equal(d[0], 23);
assert.equal(d[1], 42);
assert.equal(d[2], 255);
assert.deepEqual(d, new Buffer(d));
}
{
var e = new Buffer('über');
// console.error('uber: \'%s\'', e.toString());
assert.deepEqual(e, new Buffer([195, 188, 98, 101, 114]));
}
// Test triple slice
var a = new Buffer(8);
for (var i = 0; i < 8; i++) a[i] = i;
var b = a.slice(4, 8);
assert.equal(4, b[0]);
assert.equal(5, b[1]);
assert.equal(6, b[2]);
assert.equal(7, b[3]);
var c = b.slice(2, 4);
assert.equal(6, c[0]);
assert.equal(7, c[1]);
{
var f = new Buffer('über', 'ascii');
// console.error('f.length: %d (should be 4)', f.length);
assert.deepEqual(f, new Buffer([252, 98, 101, 114]));
}
var d = new Buffer([23, 42, 255]);
assert.equal(d.length, 3);
assert.equal(d[0], 23);
assert.equal(d[1], 42);
assert.equal(d[2], 255);
assert.deepEqual(d, new Buffer(d));
var e = new Buffer('über');
// console.error('uber: \'%s\'', e.toString());
assert.deepEqual(e, new Buffer([195, 188, 98, 101, 114]));
var f = new Buffer('über', 'ascii');
// console.error('f.length: %d (should be 4)', f.length);
assert.deepEqual(f, new Buffer([252, 98, 101, 114]));
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
var f = new Buffer('über', encoding);
// console.error('f.length: %d (should be 8)', f.length);
assert.deepEqual(f, new Buffer([252, 0, 98, 0, 101, 0, 114, 0]));
{
var f = new Buffer('über', encoding);
// console.error('f.length: %d (should be 8)', f.length);
assert.deepEqual(f, new Buffer([252, 0, 98, 0, 101, 0, 114, 0]));
}
var f = new Buffer('привет', encoding);
// console.error('f.length: %d (should be 12)', f.length);
assert.deepEqual(f, new Buffer([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]));
assert.equal(f.toString(encoding), 'привет');
{
var f = new Buffer('привет', encoding);
// console.error('f.length: %d (should be 12)', f.length);
assert.deepEqual(f, new Buffer([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]));
assert.equal(f.toString(encoding), 'привет');
}
var f = new Buffer([0, 0, 0, 0, 0]);
assert.equal(f.length, 5);
var size = f.write('あいうえお', encoding);
// console.error('bytes written to buffer: %d (should be 4)', size);
assert.equal(size, 4);
assert.deepEqual(f, new Buffer([0x42, 0x30, 0x44, 0x30, 0x00]));
{
var f = new Buffer([0, 0, 0, 0, 0]);
assert.equal(f.length, 5);
var size = f.write('あいうえお', encoding);
// console.error('bytes written to buffer: %d (should be 4)', size);
assert.equal(size, 4);
assert.deepEqual(f, new Buffer([0x42, 0x30, 0x44, 0x30, 0x00]));
}
});
var f = new Buffer('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D)
assert.equal(f.length, 4);
assert.deepEqual(f, new Buffer('3DD84DDC', 'hex'));
{
var f = new Buffer('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D)
assert.equal(f.length, 4);
assert.deepEqual(f, new Buffer('3DD84DDC', 'hex'));
}

@@ -418,57 +538,60 @@

// test that regular and URL-safe base64 both work
var expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff];
assert.deepEqual(Buffer('//++/++/++//', 'base64'), Buffer(expected));
assert.deepEqual(Buffer('__--_--_--__', 'base64'), Buffer(expected));
{
// test that regular and URL-safe base64 both work
var expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff];
assert.deepEqual(Buffer('//++/++/++//', 'base64'), Buffer(expected));
assert.deepEqual(Buffer('__--_--_--__', 'base64'), Buffer(expected));
}
// big example
var quote = 'Man is distinguished, not only by his reason, but by this ' +
'singular passion from other animals, which is a lust ' +
'of the mind, that by a perseverance of delight in the continued ' +
'and indefatigable generation of knowledge, exceeds the short ' +
'vehemence of any carnal pleasure.';
var expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24s' +
'IGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltY' +
'WxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZX' +
'JzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmR' +
'lZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo' +
'ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=';
assert.equal(expected, (new Buffer(quote)).toString('base64'));
{
// big example
var quote = 'Man is distinguished, not only by his reason, but by this ' +
'singular passion from other animals, which is a lust ' +
'of the mind, that by a perseverance of delight in the ' +
'continued and indefatigable generation of knowledge, ' +
'exceeds the short vehemence of any carnal pleasure.';
var expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb' +
'24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci' +
'BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQ' +
'gYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu' +
'dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZ' +
'GdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm' +
'5hbCBwbGVhc3VyZS4=';
assert.equal(expected, (new Buffer(quote)).toString('base64'));
var b = new Buffer(1024);
var bytesWritten = b.write(expected, 0, 'base64');
assert.equal(quote.length, bytesWritten);
assert.equal(quote, b.toString('ascii', 0, quote.length));
b = new Buffer(1024);
var bytesWritten = b.write(expected, 0, 'base64');
assert.equal(quote.length, bytesWritten);
assert.equal(quote, b.toString('ascii', 0, quote.length));
// check that the base64 decoder ignores whitespace
var expectedWhite = expected.slice(0, 60) + ' \n' +
expected.slice(60, 120) + ' \n' +
expected.slice(120, 180) + ' \n' +
expected.slice(180, 240) + ' \n' +
expected.slice(240, 300) + '\n' +
expected.slice(300, 360) + '\n';
b = new Buffer(1024);
bytesWritten = b.write(expectedWhite, 0, 'base64');
assert.equal(quote.length, bytesWritten);
assert.equal(quote, b.toString('ascii', 0, quote.length));
// check that the base64 decoder ignores whitespace
var expectedWhite = expected.slice(0, 60) + ' \n' +
expected.slice(60, 120) + ' \n' +
expected.slice(120, 180) + ' \n' +
expected.slice(180, 240) + ' \n' +
expected.slice(240, 300) + '\n' +
expected.slice(300, 360) + '\n';
b = new Buffer(1024);
bytesWritten = b.write(expectedWhite, 0, 'base64');
assert.equal(quote.length, bytesWritten);
assert.equal(quote, b.toString('ascii', 0, quote.length));
// check that the base64 decoder on the constructor works
// even in the presence of whitespace.
b = new Buffer(expectedWhite, 'base64');
assert.equal(quote.length, b.length);
assert.equal(quote, b.toString('ascii', 0, quote.length));
// check that the base64 decoder on the constructor works
// even in the presence of whitespace.
b = new Buffer(expectedWhite, 'base64');
assert.equal(quote.length, b.length);
assert.equal(quote, b.toString('ascii', 0, quote.length));
// check that the base64 decoder ignores illegal chars
var expectedIllegal = expected.slice(0, 60) + ' \x80' +
expected.slice(60, 120) + ' \xff' +
expected.slice(120, 180) + ' \x00' +
expected.slice(180, 240) + ' \x98' +
expected.slice(240, 300) + '\x03' +
expected.slice(300, 360);
b = new Buffer(expectedIllegal, 'base64');
assert.equal(quote.length, b.length);
assert.equal(quote, b.toString('ascii', 0, quote.length));
}
// check that the base64 decoder ignores illegal chars
var expectedIllegal = expected.slice(0, 60) + ' \x80' +
expected.slice(60, 120) + ' \xff' +
expected.slice(120, 180) + ' \x00' +
expected.slice(180, 240) + ' \x98' +
expected.slice(240, 300) + '\x03' +
expected.slice(300, 360);
b = new Buffer(expectedIllegal, 'base64');
assert.equal(quote.length, b.length);
assert.equal(quote, b.toString('ascii', 0, quote.length));
assert.equal(new Buffer('', 'base64').toString(), '');

@@ -554,35 +677,40 @@ assert.equal(new Buffer('K', 'base64').toString(), '');

// Writing base64 at a position > 0 should not mangle the result.
//
// https://github.com/joyent/node/issues/402
var segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh'];
var buf = new Buffer(64);
var pos = 0;
{
// Writing base64 at a position > 0 should not mangle the result.
//
// https://github.com/joyent/node/issues/402
var segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh'];
var b = new Buffer(64);
var pos = 0;
for (var i = 0; i < segments.length; ++i) {
pos += b.write(segments[i], pos, 'base64');
for (var i = 0; i < segments.length; ++i) {
pos += b.write(segments[i], pos, 'base64');
}
assert.equal(b.toString('binary', 0, pos), 'Madness?! This is node.js!');
}
assert.equal(b.toString('binary', 0, pos), 'Madness?! This is node.js!');
// Creating buffers larger than pool size.
var l = Buffer.poolSize + 5;
var s = '';
for (i = 0; i < l; i++) {
s += 'h';
}
// Regression test for https://github.com/nodejs/node/issues/3496.
// assert.equal(Buffer('=bad'.repeat(1e4), 'base64').length, 0);
var b = new Buffer(s);
{
// Creating buffers larger than pool size.
var l = Buffer.poolSize + 5;
var s = 'h'.repeat(l);
for (i = 0; i < l; i++) {
assert.equal('h'.charCodeAt(0), b[i]);
}
var b = new Buffer(s);
var sb = b.toString();
assert.equal(sb.length, s.length);
assert.equal(sb, s);
for (var i = 0; i < l; i++) {
assert.equal('h'.charCodeAt(0), b[i]);
}
var sb = b.toString();
assert.equal(sb.length, s.length);
assert.equal(sb, s);
}
// Single argument slice
b = new Buffer('abcde');
assert.equal('bcde', b.slice(1).toString());
{
// Single argument slice
var b = new Buffer('abcde');
assert.equal('bcde', b.slice(1).toString());
}

@@ -623,15 +751,16 @@ // slice(0,0).length === 0

// test an invalid slice end.
// console.log('Try to slice off the end of the buffer');
var b = new Buffer([1, 2, 3, 4, 5]);
var b2 = b.toString('hex', 1, 10000);
var b3 = b.toString('hex', 1, 5);
var b4 = b.toString('hex', 1);
assert.equal(b2, b3);
assert.equal(b2, b4);
{
// test an invalid slice end.
// console.log('Try to slice off the end of the buffer');
var b = new Buffer([1, 2, 3, 4, 5]);
var b2 = b.toString('hex', 1, 10000);
var b3 = b.toString('hex', 1, 5);
var b4 = b.toString('hex', 1);
assert.equal(b2, b3);
assert.equal(b2, b4);
}
function buildBuffer(data) {
if (Array.isArray(data)) {
var buffer = new Buffer(data.length);
var buffer = Buffer(data.length);
data.forEach(function(v, k) {

@@ -650,41 +779,53 @@ buffer[k] = v;

var z = x.slice(4);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(5, z.length);
assert.equal(0x6f, z[0]);
assert.equal(0xa3, z[1]);
assert.equal(0x62, z[2]);
assert.equal(0x61, z[3]);
assert.equal(0x72, z[4]);
{
var z = x.slice(4);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(5, z.length);
assert.equal(0x6f, z[0]);
assert.equal(0xa3, z[1]);
assert.equal(0x62, z[2]);
assert.equal(0x61, z[3]);
assert.equal(0x72, z[4]);
}
var z = x.slice(0);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(z.length, x.length);
{
var z = x.slice(0);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(z.length, x.length);
}
var z = x.slice(0, 4);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(4, z.length);
assert.equal(0x81, z[0]);
assert.equal(0xa3, z[1]);
{
var z = x.slice(0, 4);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(4, z.length);
assert.equal(0x81, z[0]);
assert.equal(0xa3, z[1]);
}
var z = x.slice(0, 9);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(9, z.length);
{
var z = x.slice(0, 9);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(9, z.length);
}
var z = x.slice(1, 4);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(3, z.length);
assert.equal(0xa3, z[0]);
{
var z = x.slice(1, 4);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(3, z.length);
assert.equal(0xa3, z[0]);
}
var z = x.slice(2, 4);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(2, z.length);
assert.equal(0x66, z[0]);
assert.equal(0x6f, z[1]);
{
var z = x.slice(2, 4);
// console.log(z.inspect());
// console.log(z.length);
assert.equal(2, z.length);
assert.equal(0x66, z[0]);
assert.equal(0x6f, z[1]);
}

@@ -699,119 +840,135 @@ assert.equal(0, Buffer('hello').slice(0, 0).length);

// Binary encoding should write only one byte per character.
var b = Buffer([0xde, 0xad, 0xbe, 0xef]);
var s = String.fromCharCode(0xffff);
b.write(s, 0, 'binary');
assert.equal(0xff, b[0]);
assert.equal(0xad, b[1]);
assert.equal(0xbe, b[2]);
assert.equal(0xef, b[3]);
s = String.fromCharCode(0xaaee);
b.write(s, 0, 'binary');
assert.equal(0xee, b[0]);
assert.equal(0xad, b[1]);
assert.equal(0xbe, b[2]);
assert.equal(0xef, b[3]);
{
// Binary encoding should write only one byte per character.
var b = Buffer([0xde, 0xad, 0xbe, 0xef]);
var s = String.fromCharCode(0xffff);
b.write(s, 0, 'binary');
assert.equal(0xff, b[0]);
assert.equal(0xad, b[1]);
assert.equal(0xbe, b[2]);
assert.equal(0xef, b[3]);
s = String.fromCharCode(0xaaee);
b.write(s, 0, 'binary');
assert.equal(0xee, b[0]);
assert.equal(0xad, b[1]);
assert.equal(0xbe, b[2]);
assert.equal(0xef, b[3]);
}
// #1210 Test UTF-8 string includes null character
var buf = new Buffer('\0');
assert.equal(buf.length, 1);
buf = new Buffer('\0\0');
assert.equal(buf.length, 2);
{
// #1210 Test UTF-8 string includes null character
var buf = new Buffer('\0');
assert.equal(buf.length, 1);
buf = new Buffer('\0\0');
assert.equal(buf.length, 2);
}
buf = new Buffer(2);
var written = buf.write(''); // 0byte
assert.equal(written, 0);
written = buf.write('\0'); // 1byte (v8 adds null terminator)
assert.equal(written, 1);
written = buf.write('a\0'); // 1byte * 2
assert.equal(written, 2);
written = buf.write('あ'); // 3bytes
assert.equal(written, 0);
written = buf.write('\0あ'); // 1byte + 3bytes
assert.equal(written, 1);
written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes
assert.equal(written, 2);
{
var buf = new Buffer(2);
var written = buf.write(''); // 0byte
assert.equal(written, 0);
written = buf.write('\0'); // 1byte (v8 adds null terminator)
assert.equal(written, 1);
written = buf.write('a\0'); // 1byte * 2
assert.equal(written, 2);
written = buf.write('あ'); // 3bytes
assert.equal(written, 0);
written = buf.write('\0あ'); // 1byte + 3bytes
assert.equal(written, 1);
written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes
assert.equal(written, 2);
}
buf = new Buffer(10);
written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
assert.equal(written, 9);
written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
assert.equal(written, 10);
{
var buf = new Buffer(10);
written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
assert.equal(written, 9);
written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
assert.equal(written, 10);
}
// #243 Test write() with maxLength
var buf = new Buffer(4);
buf.fill(0xFF);
var written = buf.write('abcd', 1, 2, 'utf8');
// console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0x61);
assert.equal(buf[2], 0x62);
assert.equal(buf[3], 0xFF);
{
// #243 Test write() with maxLength
var buf = new Buffer(4);
buf.fill(0xFF);
var written = buf.write('abcd', 1, 2, 'utf8');
// console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0x61);
assert.equal(buf[2], 0x62);
assert.equal(buf[3], 0xFF);
buf.fill(0xFF);
written = buf.write('abcd', 1, 4);
// console.log(buf);
assert.equal(written, 3);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0x61);
assert.equal(buf[2], 0x62);
assert.equal(buf[3], 0x63);
buf.fill(0xFF);
written = buf.write('abcd', 1, 4);
// console.log(buf);
assert.equal(written, 3);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0x61);
assert.equal(buf[2], 0x62);
assert.equal(buf[3], 0x63);
buf.fill(0xFF);
written = buf.write('abcd', 'utf8', 1, 2); // legacy style
// console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0x61);
assert.equal(buf[2], 0x62);
assert.equal(buf[3], 0xFF);
buf.fill(0xFF);
written = buf.write('abcd', 1, 2, 'utf8');
// console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0x61);
assert.equal(buf[2], 0x62);
assert.equal(buf[3], 0xFF);
buf.fill(0xFF);
written = buf.write('abcdef', 1, 2, 'hex');
// console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0xAB);
assert.equal(buf[2], 0xCD);
assert.equal(buf[3], 0xFF);
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
buf.fill(0xFF);
written = buf.write('abcd', 0, 2, encoding);
written = buf.write('abcdef', 1, 2, 'hex');
// console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0x61);
assert.equal(buf[1], 0x00);
assert.equal(buf[2], 0xFF);
assert.equal(buf[0], 0xFF);
assert.equal(buf[1], 0xAB);
assert.equal(buf[2], 0xCD);
assert.equal(buf[3], 0xFF);
});
// test offset returns are correct
var b = new Buffer(16);
assert.equal(4, b.writeUInt32LE(0, 0));
assert.equal(6, b.writeUInt16LE(0, 4));
assert.equal(7, b.writeUInt8(0, 6));
assert.equal(8, b.writeInt8(0, 7));
assert.equal(16, b.writeDoubleLE(0, 8));
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
buf.fill(0xFF);
written = buf.write('abcd', 0, 2, encoding);
// console.log(buf);
assert.equal(written, 2);
assert.equal(buf[0], 0x61);
assert.equal(buf[1], 0x00);
assert.equal(buf[2], 0xFF);
assert.equal(buf[3], 0xFF);
});
}
// test unmatched surrogates not producing invalid utf8 output
// ef bf bd = utf-8 representation of unicode replacement character
// see https://codereview.chromium.org/121173009/
buf = new Buffer('ab\ud800cd', 'utf8');
assert.equal(buf[0], 0x61);
assert.equal(buf[1], 0x62);
assert.equal(buf[2], 0xef);
assert.equal(buf[3], 0xbf);
assert.equal(buf[4], 0xbd);
assert.equal(buf[5], 0x63);
assert.equal(buf[6], 0x64);
{
// test offset returns are correct
var b = new Buffer(16);
assert.equal(4, b.writeUInt32LE(0, 0));
assert.equal(6, b.writeUInt16LE(0, 4));
assert.equal(7, b.writeUInt8(0, 6));
assert.equal(8, b.writeInt8(0, 7));
assert.equal(16, b.writeDoubleLE(0, 8));
}
// test for buffer overrun
buf = new Buffer([0, 0, 0, 0, 0]); // length: 5
var sub = buf.slice(0, 4); // length: 4
written = sub.write('12345', 'binary');
assert.equal(written, 4);
assert.equal(buf[4], 0);
{
// test unmatched surrogates not producing invalid utf8 output
// ef bf bd = utf-8 representation of unicode replacement character
// see https://codereview.chromium.org/121173009/
var buf = new Buffer('ab\ud800cd', 'utf8');
assert.equal(buf[0], 0x61);
assert.equal(buf[1], 0x62);
assert.equal(buf[2], 0xef);
assert.equal(buf[3], 0xbf);
assert.equal(buf[4], 0xbd);
assert.equal(buf[5], 0x63);
assert.equal(buf[6], 0x64);
}
{
// test for buffer overrun
var buf = new Buffer([0, 0, 0, 0, 0]); // length: 5
var sub = buf.slice(0, 4); // length: 4
written = sub.write('12345', 'binary');
assert.equal(written, 4);
assert.equal(buf[4], 0);
}
// Check for fractional length args, junk length args, etc.

@@ -837,7 +994,9 @@ // https://github.com/joyent/node/issues/1758

// Regression test, guard against buffer overrun in the base64 decoder.
var a = Buffer(3);
var b = Buffer('xxx');
a.write('aaaaaaaa', 'base64');
assert.equal(b.toString(), 'xxx');
{
// Regression test, guard against buffer overrun in the base64 decoder.
var a = Buffer(3);
var b = Buffer('xxx');
a.write('aaaaaaaa', 'base64');
assert.equal(b.toString(), 'xxx');
}

@@ -870,4 +1029,4 @@ // issue GH-3416

(function() {
var buffer = new Buffer('test'),
string = JSON.stringify(buffer);
var buffer = new Buffer('test');
var string = JSON.stringify(buffer);

@@ -895,12 +1054,22 @@ assert.equal(string, '{"type":"Buffer","data":[116,101,115,116]}');

assert.throws(function() {
new Buffer(0xFFFFFFFF);
Buffer(0xFFFFFFFF);
}, RangeError);
assert.throws(function() {
new Buffer(0xFFFFFFFFF);
Buffer(0xFFFFFFFFF);
}, RangeError);
// issue GH-5587
assert.throws(function() {
var buf = new Buffer(8);
buf.writeFloatLE(0, 5);
}, RangeError);
assert.throws(function() {
var buf = new Buffer(16);
buf.writeDoubleLE(0, 9);
}, RangeError);
// attempt to overflow buffers, similar to previous bug in array buffers
assert.throws(function() {
var buf = new Buffer(8);
var buf = Buffer(8);
buf.readFloatLE(0xffffffff);

@@ -910,3 +1079,3 @@ }, RangeError);

assert.throws(function() {
var buf = new Buffer(8);
var buf = Buffer(8);
buf.writeFloatLE(0.0, 0xffffffff);

@@ -916,3 +1085,3 @@ }, RangeError);

assert.throws(function() {
var buf = new Buffer(8);
var buf = Buffer(8);
buf.readFloatLE(0xffffffff);

@@ -922,3 +1091,3 @@ }, RangeError);

assert.throws(function() {
var buf = new Buffer(8);
var buf = Buffer(8);
buf.writeFloatLE(0.0, 0xffffffff);

@@ -930,3 +1099,3 @@ }, RangeError);

assert.throws(function() {
var buf = new Buffer(8);
var buf = Buffer(8);
buf.readFloatLE(-1);

@@ -936,3 +1105,3 @@ }, RangeError);

assert.throws(function() {
var buf = new Buffer(8);
var buf = Buffer(8);
buf.writeFloatLE(0.0, -1);

@@ -942,3 +1111,3 @@ }, RangeError);

assert.throws(function() {
var buf = new Buffer(8);
var buf = Buffer(8);
buf.readFloatLE(-1);

@@ -948,3 +1117,3 @@ }, RangeError);

assert.throws(function() {
var buf = new Buffer(8);
var buf = Buffer(8);
buf.writeFloatLE(0.0, -1);

@@ -954,11 +1123,15 @@ }, RangeError);

// offset checks
var buf = new Buffer(0);
{
var buf = new Buffer(0);
assert.throws(function() { buf.readUInt8(0); }, RangeError);
assert.throws(function() { buf.readInt8(0); }, RangeError);
assert.throws(function() { buf.readUInt8(0); }, RangeError);
assert.throws(function() { buf.readInt8(0); }, RangeError);
}
var buf = new Buffer([0xFF]);
{
var buf = new Buffer([0xFF]);
assert.equal(buf.readUInt8(0), 255);
assert.equal(buf.readInt8(0), -1);
assert.equal(buf.readUInt8(0), 255);
assert.equal(buf.readInt8(0), -1);
}

@@ -1025,3 +1198,3 @@ [16, 32].forEach(function(bits) {

(function() {
var buf = new Buffer(3);
var buf = Buffer(3);
buf.writeUIntLE(0x123456, 0, 3);

@@ -1031,3 +1204,3 @@ assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);

buf = new Buffer(3);
buf = Buffer(3);
buf.writeUIntBE(0x123456, 0, 3);

@@ -1037,3 +1210,3 @@ assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);

buf = new Buffer(3);
buf = Buffer(3);
buf.writeIntLE(0x123456, 0, 3);

@@ -1043,3 +1216,3 @@ assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);

buf = new Buffer(3);
buf = Buffer(3);
buf.writeIntBE(0x123456, 0, 3);

@@ -1049,3 +1222,3 @@ assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);

buf = new Buffer(3);
buf = Buffer(3);
buf.writeIntLE(-0x123456, 0, 3);

@@ -1055,3 +1228,3 @@ assert.deepEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]);

buf = new Buffer(3);
buf = Buffer(3);
buf.writeIntBE(-0x123456, 0, 3);

@@ -1061,3 +1234,23 @@ assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]);

buf = new Buffer(5);
buf = Buffer(3);
buf.writeIntLE(-0x123400, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]);
assert.equal(buf.readIntLE(0, 3), -0x123400);
buf = Buffer(3);
buf.writeIntBE(-0x123400, 0, 3);
assert.deepEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]);
assert.equal(buf.readIntBE(0, 3), -0x123400);
buf = Buffer(3);
buf.writeIntLE(-0x120000, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0xee]);
assert.equal(buf.readIntLE(0, 3), -0x120000);
buf = Buffer(3);
buf.writeIntBE(-0x120000, 0, 3);
assert.deepEqual(buf.toJSON().data, [0xee, 0x00, 0x00]);
assert.equal(buf.readIntBE(0, 3), -0x120000);
buf = Buffer(5);
buf.writeUIntLE(0x1234567890, 0, 5);

@@ -1067,3 +1260,3 @@ assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);

buf = new Buffer(5);
buf = Buffer(5);
buf.writeUIntBE(0x1234567890, 0, 5);

@@ -1073,3 +1266,3 @@ assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);

buf = new Buffer(5);
buf = Buffer(5);
buf.writeIntLE(0x1234567890, 0, 5);

@@ -1079,3 +1272,3 @@ assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);

buf = new Buffer(5);
buf = Buffer(5);
buf.writeIntBE(0x1234567890, 0, 5);

@@ -1085,3 +1278,3 @@ assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);

buf = new Buffer(5);
buf = Buffer(5);
buf.writeIntLE(-0x1234567890, 0, 5);

@@ -1091,6 +1284,16 @@ assert.deepEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]);

buf = new Buffer(5);
buf = Buffer(5);
buf.writeIntBE(-0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]);
assert.equal(buf.readIntBE(0, 5), -0x1234567890);
buf = Buffer(5);
buf.writeIntLE(-0x0012000000, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]);
assert.equal(buf.readIntLE(0, 5), -0x0012000000);
buf = Buffer(5);
buf.writeIntBE(-0x0012000000, 0, 5);
assert.deepEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
assert.equal(buf.readIntBE(0, 5), -0x0012000000);
})();

@@ -1104,2 +1307,17 @@

assert.equal(buf.slice(-20, -10), '');
assert.equal(buf.slice(), '0123456789');
assert.equal(buf.slice(0), '0123456789');
assert.equal(buf.slice(0, 0), '');
assert.equal(buf.slice(undefined), '0123456789');
assert.equal(buf.slice('foobar'), '0123456789');
assert.equal(buf.slice(undefined, undefined), '0123456789');
assert.equal(buf.slice(2), '23456789');
assert.equal(buf.slice(5), '56789');
assert.equal(buf.slice(10), '');
assert.equal(buf.slice(5, 8), '567');
assert.equal(buf.slice(8, -1), '8');
assert.equal(buf.slice(-10), '0123456789');
assert.equal(buf.slice(0, -9), '0');
assert.equal(buf.slice(0, -10), '');
assert.equal(buf.slice(0, -1), '012345678');

@@ -1109,6 +1327,23 @@ assert.equal(buf.slice(2, -2), '234567');

assert.equal(buf.slice(65536, 0), '');
assert.equal(buf.slice(-5, -8), '');
assert.equal(buf.slice(-5, -3), '56');
assert.equal(buf.slice(-10, 10), '0123456789');
for (var i = 0, s = buf.toString(); i < buf.length; ++i) {
assert.equal(buf.slice(i), s.slice(i));
assert.equal(buf.slice(0, i), s.slice(0, i));
assert.equal(buf.slice(-i), s.slice(-i));
assert.equal(buf.slice(0, -i), s.slice(0, -i));
}
var utf16Buf = new Buffer('0123456789', 'utf16le');
// assert.deepEqual(utf16Buf.slice(0, 6), Buffer('012', 'utf16le'));
assert.equal(buf.slice('0', '1'), '0');
assert.equal(buf.slice('-5', '10'), '56789');
assert.equal(buf.slice('-10', '10'), '0123456789');
assert.equal(buf.slice('-10', '-5'), '01234');
assert.equal(buf.slice('-10', '-0'), '');
assert.equal(buf.slice('111'), '');
assert.equal(buf.slice('0', '-111'), '');
// try to slice a zero length Buffer

@@ -1147,3 +1382,3 @@ // see https://github.com/joyent/node/issues/5881

assert.throws(function() {
new SlowBuffer((-1 >>> 0) + 1);
SlowBuffer((-1 >>> 0) + 1);
}, RangeError);

@@ -1167,21 +1402,26 @@

// Test Compare
var b = new Buffer(1).fill('a');
var c = new Buffer(1).fill('c');
var d = new Buffer(2).fill('aa');
{
var b = new Buffer(1).fill('a');
var c = new Buffer(1).fill('c');
var d = new Buffer(2).fill('aa');
assert.equal(b.compare(c), -1);
assert.equal(c.compare(d), 1);
assert.equal(d.compare(b), 1);
assert.equal(b.compare(d), -1);
assert.equal(b.compare(b), 0);
assert.equal(b.compare(c), -1);
assert.equal(c.compare(d), 1);
assert.equal(d.compare(b), 1);
assert.equal(b.compare(d), -1);
assert.equal(b.compare(b), 0);
assert.equal(Buffer.compare(b, c), -1);
assert.equal(Buffer.compare(c, d), 1);
assert.equal(Buffer.compare(d, b), 1);
assert.equal(Buffer.compare(b, d), -1);
assert.equal(Buffer.compare(c, c), 0);
assert.equal(Buffer.compare(b, c), -1);
assert.equal(Buffer.compare(c, d), 1);
assert.equal(Buffer.compare(d, b), 1);
assert.equal(Buffer.compare(b, d), -1);
assert.equal(Buffer.compare(c, c), 0);
assert.equal(Buffer.compare(Buffer(0), Buffer(0)), 0);
assert.equal(Buffer.compare(Buffer(0), Buffer(1)), -1);
assert.equal(Buffer.compare(Buffer(1), Buffer(0)), 1);
}
assert.throws(function() {
var b = new Buffer(1);
var b = Buffer(1);
Buffer.compare(b, 'abc');

@@ -1191,3 +1431,3 @@ });

assert.throws(function() {
var b = new Buffer(1);
var b = Buffer(1);
Buffer.compare('abc', b);

@@ -1197,3 +1437,3 @@ });

assert.throws(function() {
var b = new Buffer(1);
var b = Buffer(1);
b.compare('abc');

@@ -1203,18 +1443,20 @@ });

// Test Equals
var b = new Buffer(5).fill('abcdf');
var c = new Buffer(5).fill('abcdf');
var d = new Buffer(5).fill('abcde');
var e = new Buffer(6).fill('abcdef');
{
var b = new Buffer(5).fill('abcdf');
var c = new Buffer(5).fill('abcdf');
var d = new Buffer(5).fill('abcde');
var e = new Buffer(6).fill('abcdef');
assert.ok(b.equals(c));
assert.ok(!c.equals(d));
assert.ok(!d.equals(e));
assert.ok(d.equals(d));
assert.ok(b.equals(c));
assert.ok(!c.equals(d));
assert.ok(!d.equals(e));
assert.ok(d.equals(d));
}
assert.throws(function() {
var b = new Buffer(1);
var b = Buffer(1);
b.equals('abc');
});
// Regression test for https://github.com/nodejs/io.js/issues/649.
// Regression test for https://github.com/nodejs/node/issues/649.
assert.throws(function() { Buffer(1422561062959).toString('utf8'); });

@@ -1232,9 +1474,19 @@

var regErrorMsg = new RegExp('First argument must be a string, Buffer, ' +
'ArrayBuffer, Array, or array-like object.');
assert.throws(function() {
new Buffer();
}, /must start with number, buffer, array or string/);
}, regErrorMsg);
assert.throws(function() {
new Buffer(null);
}, /must start with number, buffer, array or string/);
}, regErrorMsg);
// Test prototype getters don't throw
assert.equal(Buffer.prototype.parent, undefined);
assert.equal(Buffer.prototype.offset, undefined);
assert.equal(SlowBuffer.prototype.parent, undefined);
assert.equal(SlowBuffer.prototype.offset, undefined);

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc