#!/usr/bin/env node
-var concat = require('concat-stream')
-var cp = require('child_process')
-var fs = require('fs')
-var hyperquest = require('hyperquest')
-var path = require('path')
-var split = require('split')
-var through = require('through2')
-
-var url = 'https://api.github.com/repos/nodejs/node/contents'
-var dirs = [
+const concat = require('concat-stream')
+const cp = require('child_process')
+const fs = require('fs')
+const hyperquest = require('hyperquest')
+const path = require('path')
+const split = require('split')
+const through = require('through2')
+
+const url = 'https://api.github.com/repos/nodejs/node/contents'
+const dirs = [
'/test/parallel',
'/test/pummel'
]
cp.execSync('rm -rf node/test-*.js', { cwd: path.join(__dirname, '../test') })
-var httpOpts = {
+const httpOpts = {
headers: {
'User-Agent': null
// auth if github rate-limits you...
}
dirs.forEach(function (dir) {
- var req = hyperquest(url + dir, httpOpts)
+ const req = hyperquest(url + dir, httpOpts)
req.pipe(concat(function (data) {
if (req.response.statusCode !== 200) {
throw new Error(url + dir + ': ' + data.toString())
console.log(file.download_url)
- var out = path.join(__dirname, '../test/node', file.name)
+ const out = path.join(__dirname, '../test/node', file.name)
hyperquest(file.download_url, httpOpts)
.pipe(split())
.pipe(testfixer(file.name))
}
function testfixer (filename) {
- var firstline = true
+ let firstline = true
return through(function (line, enc, cb) {
line = line.toString()
if (firstline) {
// require buffer explicitly
- var preamble = 'var Buffer = require(\'../../\').Buffer;'
+ const preamble = 'var Buffer = require(\'../../\').Buffer;'
if (/use strict/.test(line)) line += '\n' + preamble
else line += preamble + '\n' + line
firstline = false
#!/usr/bin/env node
-var cp = require('child_process')
-var fs = require('fs')
-var path = require('path')
+const cp = require('child_process')
+const fs = require('fs')
+const path = require('path')
-var node = cp.spawn('npm', ['run', 'test-node'], { stdio: 'inherit' })
+const node = cp.spawn('npm', ['run', 'test-node'], { stdio: 'inherit' })
node.on('close', function (code) {
if (code !== 0) return process.exit(code)
runBrowserTests()
})
function runBrowserTests () {
- var airtapYmlPath = path.join(__dirname, '..', '.airtap.yml')
+ const airtapYmlPath = path.join(__dirname, '..', '.airtap.yml')
writeES5AirtapYml()
cp.spawn('npm', ['run', 'test-browser-es5'], { stdio: 'inherit' })
'use strict'
-var base64 = require('base64-js')
-var ieee754 = require('ieee754')
-var customInspectSymbol =
+const base64 = require('base64-js')
+const ieee754 = require('ieee754')
+const customInspectSymbol =
(typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation
? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation
: null
exports.SlowBuffer = SlowBuffer
exports.INSPECT_MAX_BYTES = 50
-var K_MAX_LENGTH = 0x7fffffff
+const K_MAX_LENGTH = 0x7fffffff
exports.kMaxLength = K_MAX_LENGTH
/**
function typedArraySupport () {
// Can typed array instances can be augmented?
try {
- var arr = new Uint8Array(1)
- var proto = { foo: function () { return 42 } }
+ const arr = new Uint8Array(1)
+ const proto = { foo: function () { return 42 } }
Object.setPrototypeOf(proto, Uint8Array.prototype)
Object.setPrototypeOf(arr, proto)
return arr.foo() === 42
throw new RangeError('The value "' + length + '" is invalid for option "size"')
}
// Return an augmented `Uint8Array` instance
- var buf = new Uint8Array(length)
+ const buf = new Uint8Array(length)
Object.setPrototypeOf(buf, Buffer.prototype)
return buf
}
)
}
- var valueOf = value.valueOf && value.valueOf()
+ const valueOf = value.valueOf && value.valueOf()
if (valueOf != null && valueOf !== value) {
return Buffer.from(valueOf, encodingOrOffset, length)
}
- var b = fromObject(value)
+ const b = fromObject(value)
if (b) return b
if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null &&
throw new TypeError('Unknown encoding: ' + encoding)
}
- var length = byteLength(string, encoding) | 0
- var buf = createBuffer(length)
+ const length = byteLength(string, encoding) | 0
+ let buf = createBuffer(length)
- var actual = buf.write(string, encoding)
+ const actual = buf.write(string, encoding)
if (actual !== length) {
// Writing a hex string, for example, that contains invalid characters will
}
function fromArrayLike (array) {
- var length = array.length < 0 ? 0 : checked(array.length) | 0
- var buf = createBuffer(length)
- for (var i = 0; i < length; i += 1) {
+ const length = array.length < 0 ? 0 : checked(array.length) | 0
+ const buf = createBuffer(length)
+ for (let i = 0; i < length; i += 1) {
buf[i] = array[i] & 255
}
return buf
function fromArrayView (arrayView) {
if (isInstance(arrayView, Uint8Array)) {
- var copy = new Uint8Array(arrayView)
+ const copy = new Uint8Array(arrayView)
return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength)
}
return fromArrayLike(arrayView)
throw new RangeError('"length" is outside of buffer bounds')
}
- var buf
+ let buf
if (byteOffset === undefined && length === undefined) {
buf = new Uint8Array(array)
} else if (length === undefined) {
function fromObject (obj) {
if (Buffer.isBuffer(obj)) {
- var len = checked(obj.length) | 0
- var buf = createBuffer(len)
+ const len = checked(obj.length) | 0
+ const buf = createBuffer(len)
if (buf.length === 0) {
return buf
if (a === b) return 0
- var x = a.length
- var y = b.length
+ let x = a.length
+ let y = b.length
- for (var i = 0, len = Math.min(x, y); i < len; ++i) {
+ for (let i = 0, len = Math.min(x, y); i < len; ++i) {
if (a[i] !== b[i]) {
x = a[i]
y = b[i]
return Buffer.alloc(0)
}
- var i
+ let i
if (length === undefined) {
length = 0
for (i = 0; i < list.length; ++i) {
}
}
- var buffer = Buffer.allocUnsafe(length)
- var pos = 0
+ const buffer = Buffer.allocUnsafe(length)
+ let pos = 0
for (i = 0; i < list.length; ++i) {
- var buf = list[i]
+ const buf = list[i]
if (isInstance(buf, Uint8Array)) {
Uint8Array.prototype.set.call(
buffer,
)
}
- var len = string.length
- var mustMatch = (arguments.length > 2 && arguments[2] === true)
+ const len = string.length
+ const mustMatch = (arguments.length > 2 && arguments[2] === true)
if (!mustMatch && len === 0) return 0
// Use a for loop to avoid recursion
- var loweredCase = false
+ let loweredCase = false
for (;;) {
switch (encoding) {
case 'ascii':
Buffer.byteLength = byteLength
function slowToString (encoding, start, end) {
- var loweredCase = false
+ let loweredCase = false
// No need to verify that "this.length <= MAX_UINT32" since it's a read-only
// property of a typed array.
Buffer.prototype._isBuffer = true
function swap (b, n, m) {
- var i = b[n]
+ const i = b[n]
b[n] = b[m]
b[m] = i
}
Buffer.prototype.swap16 = function swap16 () {
- var len = this.length
+ const 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) {
+ for (let i = 0; i < len; i += 2) {
swap(this, i, i + 1)
}
return this
}
Buffer.prototype.swap32 = function swap32 () {
- var len = this.length
+ const 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) {
+ for (let i = 0; i < len; i += 4) {
swap(this, i, i + 3)
swap(this, i + 1, i + 2)
}
}
Buffer.prototype.swap64 = function swap64 () {
- var len = this.length
+ const len = this.length
if (len % 8 !== 0) {
throw new RangeError('Buffer size must be a multiple of 64-bits')
}
- for (var i = 0; i < len; i += 8) {
+ for (let i = 0; i < len; i += 8) {
swap(this, i, i + 7)
swap(this, i + 1, i + 6)
swap(this, i + 2, i + 5)
}
Buffer.prototype.toString = function toString () {
- var length = this.length
+ const length = this.length
if (length === 0) return ''
if (arguments.length === 0) return utf8Slice(this, 0, length)
return slowToString.apply(this, arguments)
}
Buffer.prototype.inspect = function inspect () {
- var str = ''
- var max = exports.INSPECT_MAX_BYTES
+ let str = ''
+ const max = exports.INSPECT_MAX_BYTES
str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim()
if (this.length > max) str += ' ... '
return '<Buffer ' + str + '>'
if (this === target) return 0
- var x = thisEnd - thisStart
- var y = end - start
- var len = Math.min(x, y)
+ let x = thisEnd - thisStart
+ let y = end - start
+ const len = Math.min(x, y)
- var thisCopy = this.slice(thisStart, thisEnd)
- var targetCopy = target.slice(start, end)
+ const thisCopy = this.slice(thisStart, thisEnd)
+ const targetCopy = target.slice(start, end)
- for (var i = 0; i < len; ++i) {
+ for (let i = 0; i < len; ++i) {
if (thisCopy[i] !== targetCopy[i]) {
x = thisCopy[i]
y = targetCopy[i]
}
function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
- var indexSize = 1
- var arrLength = arr.length
- var valLength = val.length
+ let indexSize = 1
+ let arrLength = arr.length
+ let valLength = val.length
if (encoding !== undefined) {
encoding = String(encoding).toLowerCase()
}
}
- var i
+ let i
if (dir) {
- var foundIndex = -1
+ let foundIndex = -1
for (i = byteOffset; i < arrLength; i++) {
if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
if (foundIndex === -1) foundIndex = i
} else {
if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength
for (i = byteOffset; i >= 0; i--) {
- var found = true
- for (var j = 0; j < valLength; j++) {
+ let found = true
+ for (let j = 0; j < valLength; j++) {
if (read(arr, i + j) !== read(val, j)) {
found = false
break
function hexWrite (buf, string, offset, length) {
offset = Number(offset) || 0
- var remaining = buf.length - offset
+ const remaining = buf.length - offset
if (!length) {
length = remaining
} else {
}
}
- var strLen = string.length
+ const strLen = string.length
if (length > strLen / 2) {
length = strLen / 2
}
- for (var i = 0; i < length; ++i) {
- var parsed = parseInt(string.substr(i * 2, 2), 16)
+ let i
+ for (i = 0; i < length; ++i) {
+ const parsed = parseInt(string.substr(i * 2, 2), 16)
if (numberIsNaN(parsed)) return i
buf[offset + i] = parsed
}
)
}
- var remaining = this.length - offset
+ const remaining = this.length - offset
if (length === undefined || length > remaining) length = remaining
if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
if (!encoding) encoding = 'utf8'
- var loweredCase = false
+ let loweredCase = false
for (;;) {
switch (encoding) {
case 'hex':
function utf8Slice (buf, start, end) {
end = Math.min(buf.length, end)
- var res = []
+ const res = []
- var i = start
+ let i = start
while (i < end) {
- var firstByte = buf[i]
- var codePoint = null
- var bytesPerSequence = (firstByte > 0xEF)
+ const firstByte = buf[i]
+ let codePoint = null
+ let bytesPerSequence = (firstByte > 0xEF)
? 4
: (firstByte > 0xDF)
? 3
: 1
if (i + bytesPerSequence <= end) {
- var secondByte, thirdByte, fourthByte, tempCodePoint
+ let secondByte, thirdByte, fourthByte, tempCodePoint
switch (bytesPerSequence) {
case 1:
// Based on http://stackoverflow.com/a/22747272/680742, the browser with
// the lowest limit is Chrome, with 0x10000 args.
// We go 1 magnitude less, for safety
-var MAX_ARGUMENTS_LENGTH = 0x1000
+const MAX_ARGUMENTS_LENGTH = 0x1000
function decodeCodePointsArray (codePoints) {
- var len = codePoints.length
+ const len = codePoints.length
if (len <= MAX_ARGUMENTS_LENGTH) {
return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
}
// Decode in chunks to avoid "call stack size exceeded".
- var res = ''
- var i = 0
+ let res = ''
+ let i = 0
while (i < len) {
res += String.fromCharCode.apply(
String,
}
function asciiSlice (buf, start, end) {
- var ret = ''
+ let ret = ''
end = Math.min(buf.length, end)
- for (var i = start; i < end; ++i) {
+ for (let i = start; i < end; ++i) {
ret += String.fromCharCode(buf[i] & 0x7F)
}
return ret
}
function latin1Slice (buf, start, end) {
- var ret = ''
+ let ret = ''
end = Math.min(buf.length, end)
- for (var i = start; i < end; ++i) {
+ for (let i = start; i < end; ++i) {
ret += String.fromCharCode(buf[i])
}
return ret
}
function hexSlice (buf, start, end) {
- var len = buf.length
+ const len = buf.length
if (!start || start < 0) start = 0
if (!end || end < 0 || end > len) end = len
- var out = ''
- for (var i = start; i < end; ++i) {
+ let out = ''
+ for (let i = start; i < end; ++i) {
out += hexSliceLookupTable[buf[i]]
}
return out
}
function utf16leSlice (buf, start, end) {
- var bytes = buf.slice(start, end)
- var res = ''
+ const bytes = buf.slice(start, end)
+ let res = ''
// If bytes.length is odd, the last 8 bits must be ignored (same as node.js)
- for (var i = 0; i < bytes.length - 1; i += 2) {
+ for (let i = 0; i < bytes.length - 1; i += 2) {
res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256))
}
return res
}
Buffer.prototype.slice = function slice (start, end) {
- var len = this.length
+ const len = this.length
start = ~~start
end = end === undefined ? len : ~~end
if (end < start) end = start
- var newBuf = this.subarray(start, end)
+ const newBuf = this.subarray(start, end)
// Return an augmented `Uint8Array` instance
Object.setPrototypeOf(newBuf, Buffer.prototype)
byteLength = byteLength >>> 0
if (!noAssert) checkOffset(offset, byteLength, this.length)
- var val = this[offset]
- var mul = 1
- var i = 0
+ let val = this[offset]
+ let mul = 1
+ let i = 0
while (++i < byteLength && (mul *= 0x100)) {
val += this[offset + i] * mul
}
checkOffset(offset, byteLength, this.length)
}
- var val = this[offset + --byteLength]
- var mul = 1
+ let val = this[offset + --byteLength]
+ let mul = 1
while (byteLength > 0 && (mul *= 0x100)) {
val += this[offset + --byteLength] * mul
}
byteLength = byteLength >>> 0
if (!noAssert) checkOffset(offset, byteLength, this.length)
- var val = this[offset]
- var mul = 1
- var i = 0
+ let val = this[offset]
+ let mul = 1
+ let i = 0
while (++i < byteLength && (mul *= 0x100)) {
val += this[offset + i] * mul
}
byteLength = byteLength >>> 0
if (!noAssert) checkOffset(offset, byteLength, this.length)
- var i = byteLength
- var mul = 1
- var val = this[offset + --i]
+ let i = byteLength
+ let mul = 1
+ let val = this[offset + --i]
while (i > 0 && (mul *= 0x100)) {
val += this[offset + --i] * mul
}
Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 2, this.length)
- var val = this[offset] | (this[offset + 1] << 8)
+ const val = this[offset] | (this[offset + 1] << 8)
return (val & 0x8000) ? val | 0xFFFF0000 : val
}
Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
offset = offset >>> 0
if (!noAssert) checkOffset(offset, 2, this.length)
- var val = this[offset + 1] | (this[offset] << 8)
+ const val = this[offset + 1] | (this[offset] << 8)
return (val & 0x8000) ? val | 0xFFFF0000 : val
}
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert) {
- var maxBytes = Math.pow(2, 8 * byteLength) - 1
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1
checkInt(this, value, offset, byteLength, maxBytes, 0)
}
- var mul = 1
- var i = 0
+ let mul = 1
+ let i = 0
this[offset] = value & 0xFF
while (++i < byteLength && (mul *= 0x100)) {
this[offset + i] = (value / mul) & 0xFF
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert) {
- var maxBytes = Math.pow(2, 8 * byteLength) - 1
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1
checkInt(this, value, offset, byteLength, maxBytes, 0)
}
- var i = byteLength - 1
- var mul = 1
+ let i = byteLength - 1
+ let mul = 1
this[offset + i] = value & 0xFF
while (--i >= 0 && (mul *= 0x100)) {
this[offset + i] = (value / mul) & 0xFF
value = +value
offset = offset >>> 0
if (!noAssert) {
- var limit = Math.pow(2, (8 * byteLength) - 1)
+ const limit = Math.pow(2, (8 * byteLength) - 1)
checkInt(this, value, offset, byteLength, limit - 1, -limit)
}
- var i = 0
- var mul = 1
- var sub = 0
+ let i = 0
+ let mul = 1
+ let sub = 0
this[offset] = value & 0xFF
while (++i < byteLength && (mul *= 0x100)) {
if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
value = +value
offset = offset >>> 0
if (!noAssert) {
- var limit = Math.pow(2, (8 * byteLength) - 1)
+ const limit = Math.pow(2, (8 * byteLength) - 1)
checkInt(this, value, offset, byteLength, limit - 1, -limit)
}
- var i = byteLength - 1
- var mul = 1
- var sub = 0
+ let i = byteLength - 1
+ let mul = 1
+ let sub = 0
this[offset + i] = value & 0xFF
while (--i >= 0 && (mul *= 0x100)) {
if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
end = target.length - targetStart + start
}
- var len = end - start
+ const len = end - start
if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') {
// Use built-in when available, missing from IE11
throw new TypeError('Unknown encoding: ' + encoding)
}
if (val.length === 1) {
- var code = val.charCodeAt(0)
+ const code = val.charCodeAt(0)
if ((encoding === 'utf8' && code < 128) ||
encoding === 'latin1') {
// Fast path: If `val` fits into a single byte, use that numeric value.
if (!val) val = 0
- var i
+ let i
if (typeof val === 'number') {
for (i = start; i < end; ++i) {
this[i] = val
}
} else {
- var bytes = Buffer.isBuffer(val)
+ const bytes = Buffer.isBuffer(val)
? val
: Buffer.from(val, encoding)
- var len = bytes.length
+ const len = bytes.length
if (len === 0) {
throw new TypeError('The value "' + val +
'" is invalid for argument "value"')
// HELPER FUNCTIONS
// ================
-var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
+const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g
function base64clean (str) {
// Node takes equal signs as end of the Base64 encoding
function utf8ToBytes (string, units) {
units = units || Infinity
- var codePoint
- var length = string.length
- var leadSurrogate = null
- var bytes = []
+ let codePoint
+ const length = string.length
+ let leadSurrogate = null
+ const bytes = []
- for (var i = 0; i < length; ++i) {
+ for (let i = 0; i < length; ++i) {
codePoint = string.charCodeAt(i)
// is surrogate component
}
function asciiToBytes (str) {
- var byteArray = []
- for (var i = 0; i < str.length; ++i) {
+ const byteArray = []
+ for (let i = 0; i < str.length; ++i) {
// Node's code seems to be doing this and not & 0x7F..
byteArray.push(str.charCodeAt(i) & 0xFF)
}
}
function utf16leToBytes (str, units) {
- var c, hi, lo
- var byteArray = []
- for (var i = 0; i < str.length; ++i) {
+ let c, hi, lo
+ const byteArray = []
+ for (let i = 0; i < str.length; ++i) {
if ((units -= 2) < 0) break
c = str.charCodeAt(i)
}
function blitBuffer (src, dst, offset, length) {
- for (var i = 0; i < length; ++i) {
+ let i
+ for (i = 0; i < length; ++i) {
if ((i + offset >= dst.length) || (i >= src.length)) break
dst[i + offset] = src[i]
}
// Create lookup table for `toString('hex')`
// See: https://github.com/feross/buffer/issues/219
-var hexSliceLookupTable = (function () {
- var alphabet = '0123456789abcdef'
- var table = new Array(256)
- for (var i = 0; i < 16; ++i) {
- var i16 = i * 16
- for (var j = 0; j < 16; ++j) {
+const hexSliceLookupTable = (function () {
+ const alphabet = '0123456789abcdef'
+ const table = new Array(256)
+ for (let i = 0; i < 16; ++i) {
+ const i16 = i * 16
+ for (let j = 0; j < 16; ++j) {
table[i16 + j] = alphabet[i] + alphabet[j]
}
}
-var B = require('../').Buffer
-var test = require('tape')
+const B = require('../').Buffer
+const test = require('tape')
test('base64: ignore whitespace', function (t) {
- var text = '\n YW9ldQ== '
- var buf = new B(text, 'base64')
+ const text = '\n YW9ldQ== '
+ const buf = new B(text, 'base64')
t.equal(buf.toString(), 'aoeu')
t.end()
})
})
test('base64: high byte', function (t) {
- var highByte = B.from([128])
+ const highByte = B.from([128])
t.deepEqual(
B.alloc(1, highByte.toString('base64'), 'base64'),
highByte
-var B = require('../').Buffer
-var test = require('tape')
+const B = require('../').Buffer
+const test = require('tape')
test('instanceof Buffer', function (t) {
- var buf = new B([1, 2])
+ const buf = new B([1, 2])
t.ok(buf instanceof B)
t.end()
})
test('convert to Uint8Array in modern browsers', function (t) {
- var buf = new B([1, 2])
- var uint8array = new Uint8Array(buf.buffer)
+ const buf = new B([1, 2])
+ const uint8array = new Uint8Array(buf.buffer)
t.ok(uint8array instanceof Uint8Array)
t.equal(uint8array[0], 1)
t.equal(uint8array[1], 2)
})
test('indexes from a string', function (t) {
- var buf = new B('abc')
+ const buf = new B('abc')
t.equal(buf[0], 97)
t.equal(buf[1], 98)
t.equal(buf[2], 99)
})
test('indexes from an array', function (t) {
- var buf = new B([97, 98, 99])
+ const buf = new B([97, 98, 99])
t.equal(buf[0], 97)
t.equal(buf[1], 98)
t.equal(buf[2], 99)
})
test('setting index value should modify buffer contents', function (t) {
- var buf = new B([97, 98, 99])
+ const buf = new B([97, 98, 99])
t.equal(buf[2], 99)
t.equal(buf.toString(), 'abc')
})
test('storing negative number should cast to unsigned', function (t) {
- var buf = new B(1)
+ let buf = new B(1)
buf[0] = -3
t.equal(buf[0], 253)
})
test('test that memory is copied from array-like', function (t) {
- var u = new Uint8Array(4)
- var b = new B(u)
+ const u = new Uint8Array(4)
+ const b = new B(u)
b[0] = 1
b[1] = 2
b[2] = 3
-var B = require('../').Buffer
-var test = require('tape')
+const B = require('../').Buffer
+const test = require('tape')
test('buffer.compare', function (t) {
- var b = new B(1).fill('a')
- var c = new B(1).fill('c')
- var d = new B(2).fill('aa')
+ const b = new B(1).fill('a')
+ const c = new B(1).fill('c')
+ const d = new B(2).fill('aa')
t.equal(b.compare(c), -1)
t.equal(c.compare(d), 1)
test('buffer.compare argument validation', function (t) {
t.throws(function () {
- var b = new B(1)
+ const b = new B(1)
B.compare(b, 'abc')
})
t.throws(function () {
- var b = new B(1)
+ const b = new B(1)
B.compare('abc', b)
})
t.throws(function () {
- var b = new B(1)
+ const b = new B(1)
b.compare('abc')
})
t.end()
})
test('buffer.equals', function (t) {
- var b = new B(5).fill('abcdf')
- var c = new B(5).fill('abcdf')
- var d = new B(5).fill('abcde')
- var e = new B(6).fill('abcdef')
+ const b = new B(5).fill('abcdf')
+ const c = new B(5).fill('abcdf')
+ const d = new B(5).fill('abcde')
+ const e = new B(6).fill('abcdef')
t.ok(b.equals(c))
t.ok(!c.equals(d))
test('buffer.equals argument validation', function (t) {
t.throws(function () {
- var b = new B(1)
+ const b = new B(1)
b.equals('abc')
})
t.end()
-var B = require('../').Buffer
-var test = require('tape')
+const B = require('../').Buffer
+const test = require('tape')
test('new buffer from array', function (t) {
t.equal(
})
test('new buffer from buffer', function (t) {
- var b1 = new B('asdf')
- var b2 = new B(b1)
+ const b1 = new B('asdf')
+ const b2 = new B(b1)
t.equal(b1.toString('hex'), b2.toString('hex'))
t.end()
})
test('new buffer from ArrayBuffer', function (t) {
if (typeof ArrayBuffer !== 'undefined') {
- var arraybuffer = new Uint8Array([0, 1, 2, 3]).buffer
- var b = new B(arraybuffer)
+ const arraybuffer = new Uint8Array([0, 1, 2, 3]).buffer
+ const b = new B(arraybuffer)
t.equal(b.length, 4)
t.equal(b[0], 0)
t.equal(b[1], 1)
})
test('new buffer from ArrayBuffer, shares memory', function (t) {
- var u = new Uint8Array([0, 1, 2, 3])
- var arraybuffer = u.buffer
- var b = new B(arraybuffer)
+ const u = new Uint8Array([0, 1, 2, 3])
+ const arraybuffer = u.buffer
+ const b = new B(arraybuffer)
t.equal(b.length, 4)
t.equal(b[0], 0)
t.equal(b[1], 1)
test('new buffer from Uint8Array', function (t) {
if (typeof Uint8Array !== 'undefined') {
- var b1 = new Uint8Array([0, 1, 2, 3])
- var b2 = new B(b1)
+ const b1 = new Uint8Array([0, 1, 2, 3])
+ const b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
test('new buffer from Uint16Array', function (t) {
if (typeof Uint16Array !== 'undefined') {
- var b1 = new Uint16Array([0, 1, 2, 3])
- var b2 = new B(b1)
+ const b1 = new Uint16Array([0, 1, 2, 3])
+ const b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
test('new buffer from Uint32Array', function (t) {
if (typeof Uint32Array !== 'undefined') {
- var b1 = new Uint32Array([0, 1, 2, 3])
- var b2 = new B(b1)
+ const b1 = new Uint32Array([0, 1, 2, 3])
+ const b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
test('new buffer from Int16Array', function (t) {
if (typeof Int16Array !== 'undefined') {
- var b1 = new Int16Array([0, 1, 2, 3])
- var b2 = new B(b1)
+ const b1 = new Int16Array([0, 1, 2, 3])
+ const b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
test('new buffer from Int32Array', function (t) {
if (typeof Int32Array !== 'undefined') {
- var b1 = new Int32Array([0, 1, 2, 3])
- var b2 = new B(b1)
+ const b1 = new Int32Array([0, 1, 2, 3])
+ const b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
test('new buffer from Float32Array', function (t) {
if (typeof Float32Array !== 'undefined') {
- var b1 = new Float32Array([0, 1, 2, 3])
- var b2 = new B(b1)
+ const b1 = new Float32Array([0, 1, 2, 3])
+ const b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
test('new buffer from Float64Array', function (t) {
if (typeof Float64Array !== 'undefined') {
- var b1 = new Float64Array([0, 1, 2, 3])
- var b2 = new B(b1)
+ const b1 = new Float64Array([0, 1, 2, 3])
+ const b2 = new B(b1)
t.equal(b1.length, b2.length)
t.equal(b1[0], 0)
t.equal(b1[1], 1)
t.end()
return
}
- var buf = new B('test')
- var json = JSON.stringify(buf)
- var obj = JSON.parse(json)
- var copy = new B(obj)
+ const buf = new B('test')
+ const json = JSON.stringify(buf)
+ const obj = JSON.parse(json)
+ const copy = new B(obj)
t.ok(buf.equals(copy))
t.end()
})
-var B = require('../').Buffer
-var test = require('tape')
+const B = require('../').Buffer
+const test = require('tape')
test('detect utf16 surrogate pairs', function (t) {
- var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D'
- var buf = new B(text)
+ const text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D'
+ const buf = new B(text)
t.equal(text, buf.toString())
t.end()
})
test('detect utf16 surrogate pairs over U+20000 until U+10FFFF', function (t) {
- var text = '\uD842\uDFB7' + '\uD93D\uDCAD' + '\uDBFF\uDFFF'
- var buf = new B(text)
+ const text = '\uD842\uDFB7' + '\uD93D\uDCAD' + '\uDBFF\uDFFF'
+ const buf = new B(text)
t.equal(text, buf.toString())
t.end()
})
test('replace orphaned utf16 surrogate lead code point', function (t) {
- var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D'
- var buf = new B(text)
+ const text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D'
+ const buf = new B(text)
t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d]))
t.end()
})
test('replace orphaned utf16 surrogate trail code point', function (t) {
- var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D'
- var buf = new B(text)
+ const text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D'
+ const buf = new B(text)
t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d]))
t.end()
})
test('do not write partial utf16 code units', function (t) {
- var f = new B([0, 0, 0, 0, 0])
+ const f = new B([0, 0, 0, 0, 0])
t.equal(f.length, 5)
- var size = f.write('あいうえお', 'utf16le')
+ const size = f.write('あいうえお', 'utf16le')
t.equal(size, 4)
t.deepEqual(f, new B([0x42, 0x30, 0x44, 0x30, 0x00]))
t.end()
})
test('handle partial utf16 code points when encoding to utf8 the way node does', function (t) {
- var text = '\uD83D\uDE38' + '\uD83D\uDC4D'
+ const text = '\uD83D\uDE38' + '\uD83D\uDC4D'
- var buf = new B(8)
+ let buf = new B(8)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0xf0, 0x9f, 0x91, 0x8d]))
})
test('handle invalid utf16 code points when encoding to utf8 the way node does', function (t) {
- var text = 'a' + '\uDE38\uD83D' + 'b'
+ const text = 'a' + '\uDE38\uD83D' + 'b'
- var buf = new B(8)
+ let buf = new B(8)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x62]))
-var B = require('../').Buffer
-var isBuffer = require('is-buffer')
-var test = require('tape')
+const B = require('../').Buffer
+const isBuffer = require('is-buffer')
+const test = require('tape')
test('is-buffer tests', function (t) {
t.ok(isBuffer(new B(4)), 'new Buffer(4)')
-var B = require('../').Buffer
-var test = require('tape')
+const B = require('../').Buffer
+const test = require('tape')
test('buffer.toJSON', function (t) {
- var data = [1, 2, 3, 4]
+ const data = [1, 2, 3, 4]
t.deepEqual(
new B(data).toJSON(),
{ type: 'Buffer', data: [1, 2, 3, 4] }
test('buffer.copy', function (t) {
// copied from nodejs.org example
- var buf1 = new B(26)
- var buf2 = new B(26)
+ const buf1 = new B(26)
+ const buf2 = new B(26)
- for (var i = 0; i < 26; i++) {
+ for (let i = 0; i < 26; i++) {
buf1[i] = i + 97 // 97 is ASCII a
buf2[i] = 33 // ASCII !
}
})
test('test offset returns are correct', function (t) {
- var b = new B(16)
+ const b = new B(16)
t.equal(4, b.writeUInt32LE(0, 0))
t.equal(6, b.writeUInt16LE(0, 4))
t.equal(7, b.writeUInt8(0, 6))
})
test('concat() a varying number of buffers', function (t) {
- var zero = []
- var one = [new B('asdf')]
- var long = []
- for (var i = 0; i < 10; i++) {
+ const zero = []
+ const one = [new B('asdf')]
+ const long = []
+ for (let i = 0; i < 10; i++) {
long.push(new B('asdf'))
}
- var flatZero = B.concat(zero)
- var flatOne = B.concat(one)
- var flatLong = B.concat(long)
- var flatLongLen = B.concat(long, 40)
+ const flatZero = B.concat(zero)
+ const flatOne = B.concat(one)
+ const flatLong = B.concat(long)
+ const flatLongLen = B.concat(long, 40)
t.equal(flatZero.length, 0)
t.equal(flatOne.toString(), 'asdf')
})
test('concat() works on Uint8Array instances', function (t) {
- var result = B.concat([new Uint8Array([1, 2]), new Uint8Array([3, 4])])
- var expected = B.from([1, 2, 3, 4])
+ const result = B.concat([new Uint8Array([1, 2]), new Uint8Array([3, 4])])
+ const expected = B.from([1, 2, 3, 4])
t.deepEqual(result, expected)
t.end()
})
test('fill', function (t) {
- var b = new B(10)
+ const b = new B(10)
b.fill(2)
t.equal(b.toString('hex'), '02020202020202020202')
t.end()
})
test('fill (string)', function (t) {
- var b = new B(10)
+ const b = new B(10)
b.fill('abc')
t.equal(b.toString(), 'abcabcabca')
b.fill('է')
})
test('copy() empty buffer with sourceEnd=0', function (t) {
- var source = new B([42])
- var destination = new B([43])
+ const source = new B([42])
+ const destination = new B([43])
source.copy(destination, 0, 0, 0)
t.equal(destination.readUInt8(0), 43)
t.end()
})
test('copy() after slice()', function (t) {
- var source = new B(200)
- var dest = new B(200)
- var expected = new B(200)
- for (var i = 0; i < 200; i++) {
+ const source = new B(200)
+ const dest = new B(200)
+ const expected = new B(200)
+ for (let i = 0; i < 200; i++) {
source[i] = i
dest[i] = 0
}
})
test('copy() ascending', function (t) {
- var b = new B('abcdefghij')
+ const b = new B('abcdefghij')
b.copy(b, 0, 3, 10)
t.equal(b.toString(), 'defghijhij')
t.end()
})
test('copy() descending', function (t) {
- var b = new B('abcdefghij')
+ const b = new B('abcdefghij')
b.copy(b, 3, 0, 7)
t.equal(b.toString(), 'abcabcdefg')
t.end()
-var B = require('../').Buffer
-var test = require('tape')
+const B = require('../').Buffer
+const test = require('tape')
test('modifying buffer created by .slice() modifies original memory', function (t) {
- var buf1 = new B(26)
- for (var i = 0; i < 26; i++) {
+ const buf1 = new B(26)
+ for (let i = 0; i < 26; i++) {
buf1[i] = i + 97 // 97 is ASCII a
}
- var buf2 = buf1.slice(0, 3)
+ const buf2 = buf1.slice(0, 3)
t.equal(buf2.toString('ascii', 0, buf2.length), 'abc')
buf2[0] = '!'.charCodeAt(0)
})
test('modifying parent buffer modifies .slice() buffer\'s memory', function (t) {
- var buf1 = new B(26)
- for (var i = 0; i < 26; i++) {
+ const buf1 = new B(26)
+ for (let i = 0; i < 26; i++) {
buf1[i] = i + 97 // 97 is ASCII a
}
- var buf2 = buf1.slice(0, 3)
+ const buf2 = buf1.slice(0, 3)
t.equal(buf2.toString('ascii', 0, buf2.length), 'abc')
buf1[0] = '!'.charCodeAt(0)
-var B = require('../').Buffer
-var test = require('tape')
+const B = require('../').Buffer
+const test = require('tape')
test('Buffer.isEncoding', function (t) {
t.equal(B.isEncoding('HEX'), true)
-var B = require('../').Buffer
-var test = require('tape')
+const B = require('../').Buffer
+const test = require('tape')
test('utf8 buffer to base64', function (t) {
t.equal(
-var B = require('../').Buffer
-var test = require('tape')
-var isnan = require('is-nan')
+const B = require('../').Buffer
+const test = require('tape')
+const isnan = require('is-nan')
test('buffer.write string should get parsed as number', function (t) {
- var b = new B(64)
+ const b = new B(64)
b.writeUInt16LE('1003', 0)
t.equal(b.readUInt16LE(0), 1003)
t.end()
test('buffer.writeUInt8 a fractional number will get Math.floored', function (t) {
// Some extra work is necessary to make this test pass with the Object implementation
- var b = new B(1)
+ const b = new B(1)
b.writeInt8(5.5, 0)
t.equal(b[0], 5)
t.end()
})
test('writeUint8 with a negative number throws', function (t) {
- var buf = new B(1)
+ const buf = new B(1)
t.throws(function () {
buf.writeUInt8(-3, 0)
test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) {
t.plan(2 * ((2 * 2 * 2) + 2))
- var hex = [
+ const hex = [
'03', '0300', '0003', '03000000', '00000003',
'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd'
]
- var reads = [3, 3, 3, 3, 3, -3, -3, -3, -3, -3]
- var xs = ['UInt', 'Int']
- var ys = [8, 16, 32]
- for (var i = 0; i < xs.length; i++) {
- var x = xs[i]
- for (var j = 0; j < ys.length; j++) {
- var y = ys[j]
- var endianesses = (y === 8) ? [''] : ['LE', 'BE']
- for (var k = 0; k < endianesses.length; k++) {
- var z = endianesses[k]
+ const reads = [3, 3, 3, 3, 3, -3, -3, -3, -3, -3]
+ const xs = ['UInt', 'Int']
+ const ys = [8, 16, 32]
+ for (let i = 0; i < xs.length; i++) {
+ const x = xs[i]
+ for (let j = 0; j < ys.length; j++) {
+ const y = ys[j]
+ const endianesses = (y === 8) ? [''] : ['LE', 'BE']
+ for (let k = 0; k < endianesses.length; k++) {
+ const z = endianesses[k]
- var v1 = new B(y / 8)
- var writefn = 'write' + x + y + z
- var val = (x === 'Int') ? -3 : 3
+ const v1 = new B(y / 8)
+ const writefn = 'write' + x + y + z
+ const val = (x === 'Int') ? -3 : 3
v1[writefn](val, 0)
t.equal(
v1.toString('hex'),
hex.shift()
)
- var readfn = 'read' + x + y + z
+ const readfn = 'read' + x + y + z
t.equal(
v1[readfn](0),
reads.shift()
test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) {
t.plan(3 * ((2 * 2 * 2) + 2))
- var hex = [
+ const hex = [
'', '03', '00', '030000', '000000',
'', 'fd', 'ff', 'fdffff', 'ffffff'
]
- var reads = [
+ const reads = [
undefined, 3, 0, NaN, 0,
undefined, 253, -256, 16777213, -256
]
- var xs = ['UInt', 'Int']
- var ys = [8, 16, 32]
- for (var i = 0; i < xs.length; i++) {
- var x = xs[i]
- for (var j = 0; j < ys.length; j++) {
- var y = ys[j]
- var endianesses = (y === 8) ? [''] : ['LE', 'BE']
- for (var k = 0; k < endianesses.length; k++) {
- var z = endianesses[k]
+ const xs = ['UInt', 'Int']
+ const ys = [8, 16, 32]
+ for (let i = 0; i < xs.length; i++) {
+ const x = xs[i]
+ for (let j = 0; j < ys.length; j++) {
+ const y = ys[j]
+ const endianesses = (y === 8) ? [''] : ['LE', 'BE']
+ for (let k = 0; k < endianesses.length; k++) {
+ const z = endianesses[k]
- var v1 = new B((y / 8) - 1)
- var next = new B(4)
+ const v1 = new B((y / 8) - 1)
+ const next = new B(4)
next.writeUInt32BE(0, 0)
- var writefn = 'write' + x + y + z
- var val = (x === 'Int') ? -3 : 3
+ const writefn = 'write' + x + y + z
+ const val = (x === 'Int') ? -3 : 3
v1[writefn](val, 0, true)
t.equal(
v1.toString('hex'),
t.equal(next.readUInt32BE(0), 0)
// check that no bytes are read from next buffer.
next.writeInt32BE(~0, 0)
- var readfn = 'read' + x + y + z
- var r = reads.shift()
+ const readfn = 'read' + x + y + z
+ const r = reads.shift()
if (isnan(r)) t.pass('equal')
else t.equal(v1[readfn](0, true), r)
}
t.end()
})
test('large values do not improperly roll over (ref #80)', function (t) {
- var nums = [-25589992, -633756690, -898146932]
- var out = new B(12)
+ const nums = [-25589992, -633756690, -898146932]
+ const out = new B(12)
out.fill(0)
out.writeInt32BE(nums[0], 0)
- var newNum = out.readInt32BE(0)
+ let newNum = out.readInt32BE(0)
t.equal(nums[0], newNum)
out.writeInt32BE(nums[1], 4)
newNum = out.readInt32BE(4)
-var B = require('../').Buffer
-var test = require('tape')
+const B = require('../').Buffer
+const test = require('tape')
test('write/read Infinity as a float', function (t) {
- var buf = new B(4)
+ const buf = new B(4)
t.equal(buf.writeFloatBE(Infinity, 0), 4)
t.equal(buf.readFloatBE(0), Infinity)
t.end()
})
test('write/read -Infinity as a float', function (t) {
- var buf = new B(4)
+ const buf = new B(4)
t.equal(buf.writeFloatBE(-Infinity, 0), 4)
t.equal(buf.readFloatBE(0), -Infinity)
t.end()
})
test('write/read Infinity as a double', function (t) {
- var buf = new B(8)
+ const buf = new B(8)
t.equal(buf.writeDoubleBE(Infinity, 0), 8)
t.equal(buf.readDoubleBE(0), Infinity)
t.end()
})
test('write/read -Infinity as a double', function (t) {
- var buf = new B(8)
+ const buf = new B(8)
t.equal(buf.writeDoubleBE(-Infinity, 0), 8)
t.equal(buf.readDoubleBE(0), -Infinity)
t.end()
})
test('write/read float greater than max', function (t) {
- var buf = new B(4)
+ const buf = new B(4)
t.equal(buf.writeFloatBE(4e38, 0), 4)
t.equal(buf.readFloatBE(0), Infinity)
t.end()
})
test('write/read float less than min', function (t) {
- var buf = new B(4)
+ const buf = new B(4)
t.equal(buf.writeFloatBE(-4e40, 0), 4)
t.equal(buf.readFloatBE(0), -Infinity)
t.end()