var hyperquest = require('hyperquest')
var concat = require('concat-stream')
-var split = require('split')
+var split = require('split')
var thru = require('through2')
var fs = require('fs')
var httpOpts = {
headers: {
- 'User-Agent': null,
-
+ 'User-Agent': null
// auth if github rate-limits you...
// 'Authorization': 'Basic ' + Buffer('username:password').toString('base64'),
}
}
-dirs.forEach(function(dir) {
+dirs.forEach(function (dir) {
var req = hyperquest(url + dir, httpOpts)
- req.pipe(concat(function(data) {
+ req.pipe(concat(function (data) {
if (req.response.statusCode !== 200) {
throw new Error(url + dir + ': ' + data.toString())
}
-
- downloadBufferTests(dir, JSON.parse(data));
+ downloadBufferTests(dir, JSON.parse(data))
}))
})
-function downloadBufferTests(dir, files) {
- files.forEach(function(file) {
+function downloadBufferTests (dir, files) {
+ files.forEach(function (file) {
if (!/test-buffer.*/.test(file.name)) return
hyperquest(file.download_url, httpOpts)
})
}
-function testfixer(filename) {
+function testfixer (filename) {
var firstline = true
- return thru(function(line, enc, cb) {
+ return thru(function (line, enc, cb) {
line = line.toString()
if (firstline) {
-
// require buffer explicitly
line = 'var Buffer = require(\'../\').Buffer\n' +
'if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false\n' + line
var buf = new ArrayBuffer(0)
var arr = new Uint8Array(buf)
arr.foo = function () { return 42 }
- return 42 === arr.foo() && // typed array instances can be augmented
+ return arr.foo() === 42 && // typed array instances can be augmented
typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`
new Uint8Array(1).subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`
} catch (e) {
if (subject.type === 'Buffer' && isArray(subject.data))
subject = subject.data
length = +subject.length > 0 ? Math.floor(+subject.length) : 0
- } else
+ } else {
throw new TypeError('must start with number, buffer, array or string')
+ }
if (length > kMaxLength)
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
'size: 0x' + kMaxLength.toString(16) + ' bytes')
- var buf
+ var self = this
if (Buffer.TYPED_ARRAY_SUPPORT) {
// Preferred: Return an augmented `Uint8Array` instance for best performance
- buf = Buffer._augment(new Uint8Array(length))
+ self = Buffer._augment(new Uint8Array(length))
} else {
// Fallback: Return THIS instance of Buffer (created by `new`)
- buf = this
- buf.length = length
- buf._isBuffer = true
+ self.length = length
+ self._isBuffer = true
}
var i
if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') {
// Speed optimization -- use set if we're copying from a typed array
- buf._set(subject)
+ self._set(subject)
} else if (isArrayish(subject)) {
// Treat array-ish objects as a byte array
if (Buffer.isBuffer(subject)) {
for (i = 0; i < length; i++)
- buf[i] = subject.readUInt8(i)
+ self[i] = subject.readUInt8(i)
} else {
for (i = 0; i < length; i++)
- buf[i] = ((subject[i] % 256) + 256) % 256
+ self[i] = ((subject[i] % 256) + 256) % 256
}
} else if (type === 'string') {
- buf.write(subject, 0, encoding)
+ self.write(subject, 0, encoding)
} else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT && !noZero) {
for (i = 0; i < length; i++) {
- buf[i] = 0
+ self[i] = 0
}
}
if (length > 0 && length <= Buffer.poolSize)
- buf.parent = rootParent
+ self.parent = rootParent
- return buf
+ return self
}
-function SlowBuffer(subject, encoding, noZero) {
+function SlowBuffer (subject, encoding, noZero) {
if (!(this instanceof SlowBuffer))
return new SlowBuffer(subject, encoding, noZero)
offset = Number(offset) || 0
if (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')
var remaining = this.length - offset
if (!length) {
end = end === undefined ? len : ~~end
if (start < 0) {
- start += len;
+ start += len
if (start < 0)
start = 0
} else if (start > len) {
var val = this[offset + --byteLength]
var mul = 1
while (byteLength > 0 && (mul *= 0x100))
- val += this[offset + --byteLength] * mul;
+ val += this[offset + --byteLength] * mul
return val
}
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
Buffer.prototype.copy = function (target, target_start, start, end) {
- var source = this
+ var self = this // source
if (!start) start = 0
if (!end && end !== 0) end = this.length
// Copy 0 bytes; we're done
if (end === start) return 0
- if (target.length === 0 || source.length === 0) return 0
+ if (target.length === 0 || self.length === 0) return 0
// Fatal error conditions
if (target_start < 0)
throw new RangeError('targetStart out of bounds')
- if (start < 0 || start >= source.length) throw new RangeError('sourceStart out of bounds')
+ if (start < 0 || start >= self.length) throw new RangeError('sourceStart out of bounds')
if (end < 0) throw new RangeError('sourceEnd out of bounds')
// Are we oob?
return n.toString(16)
}
-function utf8ToBytes(string, units) {
- var codePoint, length = string.length
- var leadSurrogate = null
+function utf8ToBytes (string, units) {
units = units || Infinity
+ var codePoint
+ var length = string.length
+ var leadSurrogate = null
var bytes = []
var i = 0
- for (; i<length; i++) {
+ for (; i < length; i++) {
codePoint = string.charCodeAt(i)
// is surrogate component
if (codePoint > 0xD7FF && codePoint < 0xE000) {
-
// last char was a lead
if (leadSurrogate) {
-
// 2 leads in a row
if (codePoint < 0xDC00) {
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
leadSurrogate = codePoint
continue
- }
-
- // valid surrogate pair
- else {
+ } else {
+ // valid surrogate pair
codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000
leadSurrogate = null
}
- }
-
- // no lead yet
- else {
+ } else {
+ // no lead yet
- // unexpected trail
if (codePoint > 0xDBFF) {
+ // unexpected trail
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
continue
- }
-
- // unpaired lead
- else if (i + 1 === length) {
+ } else if (i + 1 === length) {
+ // unpaired lead
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
continue
- }
-
- // valid lead
- else {
+ } else {
+ // valid lead
leadSurrogate = codePoint
continue
}
}
- }
-
- // valid bmp char, but last char was a lead
- else if (leadSurrogate) {
+ } else if (leadSurrogate) {
+ // valid bmp char, but last char was a lead
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
leadSurrogate = null
}
if (codePoint < 0x80) {
if ((units -= 1) < 0) break
bytes.push(codePoint)
- }
- else if (codePoint < 0x800) {
+ } else if (codePoint < 0x800) {
if ((units -= 2) < 0) break
bytes.push(
codePoint >> 0x6 | 0xC0,
codePoint & 0x3F | 0x80
- );
- }
- else if (codePoint < 0x10000) {
+ )
+ } else if (codePoint < 0x10000) {
if ((units -= 3) < 0) break
bytes.push(
codePoint >> 0xC | 0xE0,
codePoint >> 0x6 & 0x3F | 0x80,
codePoint & 0x3F | 0x80
- );
- }
- else if (codePoint < 0x200000) {
+ )
+ } else if (codePoint < 0x200000) {
if ((units -= 4) < 0) break
bytes.push(
codePoint >> 0x12 | 0xF0,
codePoint >> 0xC & 0x3F | 0x80,
codePoint >> 0x6 & 0x3F | 0x80,
codePoint & 0x3F | 0x80
- );
- }
- else {
+ )
+ } else {
throw new Error('Invalid code point')
}
}
var c, hi, lo
var byteArray = []
for (var i = 0; i < str.length; i++) {
-
if ((units -= 2) < 0) break
c = str.charCodeAt(i)
}
function blitBuffer (src, dst, offset, length, unitSize) {
- if (unitSize) length -= length % unitSize;
+ if (unitSize) length -= length % unitSize
for (var i = 0; i < length; i++) {
if ((i + offset >= dst.length) || (i >= src.length))
break
"hyperquest": "^1.0.1",
"is-nan": "^1.0.1",
"split": "^0.3.2",
+ "standard": "^2.0.0",
"tape": "^3.0.1",
"through2": "^0.6.3",
"zuul": "^1.12.0"
"url": "git://github.com/feross/buffer.git"
},
"scripts": {
- "test": "node ./bin/test.js",
+ "test": "standard && node ./bin/test.js",
"test-browser": "zuul -- test/*.js",
"test-browser-local": "zuul --local -- test/*.js",
"test-node": "tape test/*.js && OBJECT_IMPL=true tape test/*.js",
"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",
"size": "browserify -r ./ | uglifyjs -c -m | gzip | wc -c"
},
+ "standard": {
+ "ignore": [
+ "test/node-*",
+ "perf/*"
+ ]
+ },
"testling": {
"files": "test/*.js",
"browsers": [
var LENGTH = 9
var singleByte = 'abcdefghi'
-var multiByte = '\u0610' + '\u6100' + '\uD944\uDC00'
+var multiByte = '\u0610' + '\u6100' + '\uD944\uDC00'
var browserBuffer = new BrowserBuffer(LENGTH)
var nodeBuffer = new Buffer(LENGTH)
var test = require('tape')
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
-
test('base64: ignore whitespace', function (t) {
var text = '\n YW9ldQ== '
var buf = new B(text, 'base64')
)
t.end()
})
-
var test = require('tape')
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
-
test('buffer.compare', function (t) {
var b = new B(1).fill('a')
var c = new B(1).fill('c')
var test = require('tape')
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
-
test('new buffer from array', function (t) {
t.equal(
new B([1, 2, 3]).toString(),
t.ok(buf.equals(copy))
t.end()
})
-
var test = require('tape')
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
-
test('.get (deprecated)', function (t) {
var b = new B([7, 42])
t.equal(b.get(0), 7)
var test = require('tape')
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
-
-test('detect utf16 surrogate pairs', function(t) {
+test('detect utf16 surrogate pairs', function (t) {
var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D'
var buf = new B(text)
t.equal(text, buf.toString())
t.end()
})
-test('replace orphaned utf16 surrogate lead code point', function(t) {
+test('replace orphaned utf16 surrogate lead code point', function (t) {
var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D'
var 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) {
+test('replace orphaned utf16 surrogate trail code point', function (t) {
var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D'
var 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) {
+test('do not write partial utf16 code units', function (t) {
var f = new B([0, 0, 0, 0, 0])
t.equal(f.length, 5)
var size = f.write('あいうえお', 'utf16le')
t.end()
})
-test('handle partial utf16 code points when encoding to utf8 the way node does', function(t) {
+test('handle partial utf16 code points when encoding to utf8 the way node does', function (t) {
var text = '\uD83D\uDE38' + '\uD83D\uDC4D'
var buf = new B(8)
buf.write(text)
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00, 0x00 ]))
- var buf = new B(5);
+ buf = new B(5)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8, 0x00 ]))
- var buf = new B(4);
+ buf = new B(4)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0xf0, 0x9f, 0x98, 0xb8 ]))
- var buf = new B(3);
+ buf = new B(3)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x00, 0x00, 0x00 ]))
- var buf = new B(2);
+ buf = new B(2)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x00, 0x00 ]))
- var buf = new B(1);
+ buf = new B(1)
buf.fill(0)
buf.write(text)
t.deepEqual(buf, new B([ 0x00 ]))
t.end()
})
-test('handle invalid utf16 code points when encoding to utf8 the way node does', function(t) {
+test('handle invalid utf16 code points when encoding to utf8 the way node does', function (t) {
var text = 'a' + '\uDE38\uD83D' + 'b'
var buf = new B(8)
var test = require('tape')
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
-
test('buffer.toJSON', function (t) {
var data = [1, 2, 3, 4]
t.deepEqual(
new B(data).toJSON(),
- { type: 'Buffer', data: [1,2,3,4] }
+ { type: 'Buffer', data: [ 1, 2, 3, 4 ] }
)
t.end()
})
var buf2 = new B(26)
for (var i = 0 ; i < 26 ; i++) {
- buf1[i] = i + 97; // 97 is ASCII a
- buf2[i] = 33; // ASCII !
+ buf1[i] = i + 97 // 97 is ASCII a
+ buf2[i] = 33 // ASCII !
}
buf1.copy(buf2, 8, 16, 20)
test('concat() a varying number of buffers', function (t) {
var zero = []
- var one = [ new B('asdf') ]
+ var one = [ new B('asdf') ]
var long = []
for (var i = 0; i < 10; i++) {
long.push(new B('asdf'))
t.equal(flatZero.length, 0)
t.equal(flatOne.toString(), 'asdf')
t.equal(flatOne, one[0])
- t.equal(flatLong.toString(), (new Array(10+1).join('asdf')))
- t.equal(flatLongLen.toString(), (new Array(10+1).join('asdf')))
+ t.equal(flatLong.toString(), (new Array(10 + 1).join('asdf')))
+ t.equal(flatLongLen.toString(), (new Array(10 + 1).join('asdf')))
t.end()
})
var test = require('tape')
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
-
test('modifying buffer created by .slice() modifies original memory', function (t) {
if (!B._useTypedArrays) return t.end()
var test = require('tape')
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
-
test('Buffer.isEncoding', function (t) {
t.equal(B.isEncoding('HEX'), true)
t.equal(B.isEncoding('hex'), true)
var test = require('tape')
if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
-
test('utf8 buffer to base64', function (t) {
t.equal(
new B('Ձאab', 'utf8').toString('base64'),
})
test('utf16le to utf16', function (t) {
- t.equal(
- new B(new B('abcd', 'utf8').toString('utf16le'), 'utf16le').toString('utf8'),
- 'abcd'
- )
- t.end()
+ t.equal(
+ new B(new B('abcd', 'utf8').toString('utf16le'), 'utf16le').toString('utf8'),
+ 'abcd'
+ )
+ t.end()
})
test('utf16le to hex', function (t) {
- t.equal(
- new B('abcd', 'utf16le').toString('hex'),
- '6100620063006400'
- )
- t.end()
+ t.equal(
+ new B('abcd', 'utf16le').toString('hex'),
+ '6100620063006400'
+ )
+ t.end()
})
test('ascii buffer to base64', function (t) {
})
test('utf8 to binary', function (t) {
+ /* jshint -W100 */
t.equal(
new B('öäüõÖÄÜÕ', 'utf8').toString('binary'),
'öäüõÃ\96Ã\84Ã\9cÃ\95'
)
+ /* jshint +W100 */
t.end()
})
+++ /dev/null
-// var B = require('../').Buffer
-// var test = require('tape')
-// var useragent = require('useragent')
-// if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false
-
-
-// test('expected browsers get Uint8Array implementation', function (t) {
-// if (typeof navigator === 'undefined') {
-// t.pass('Not running in a browser -- skip this test')
-// t.end()
-// return
-// }
-// var agent = useragent.parse(navigator.userAgent)
-// console.log('Family: ' + agent.family)
-// console.log('Version: ' + agent.major + '.' + agent.minor)
-
-// if ((agent.family === 'Chrome' && agent.major >= 7) ||
-// (agent.family === 'Internet Explorer' && agent.major >= 10) ||
-// (agent.family === 'Firefox' && agent.major >= 30) ||
-// (agent.family === 'Opera' && agent.major >= 12) ||
-// (agent.family === 'Safari' && agent.major === 5 && agent.minor === 1) ||
-// (agent.family === 'Safari' && agent.major === 6)) {
-// t.ok(B._useTypedArrays)
-// } else {
-// t.ok(!B._useTypedArrays)
-// }
-// t.end()
-// })
'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]
+ 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']
+ var endianesses = (y === 8) ? [''] : ['LE', 'BE']
for (var k = 0; k < endianesses.length; k++) {
var z = endianesses[k]
- var v1 = new B(y / 8)
- var writefn = 'write' + x + y + z
+ var v1 = new B(y / 8)
+ var writefn = 'write' + x + y + z
var val = (x === 'Int') ? -3 : 3
v1[writefn](val, 0)
t.equal(
undefined, 3, 0, NaN, 0,
undefined, 253, -256, 16777213, -256
]
- var xs = ['UInt','Int']
- var ys = [8,16,32]
+ 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']
+ var endianesses = (y === 8) ? [''] : ['LE', 'BE']
for (var k = 0; k < endianesses.length; k++) {
var z = endianesses[k]
- var v1 = new B(y / 8 - 1)
+ var v1 = new B(y / 8 - 1)
var next = new B(4)
next.writeUInt32BE(0, 0)
- var writefn = 'write' + x + y + z
+ var writefn = 'write' + x + y + z
var val = (x === 'Int') ? -3 : 3
v1[writefn](val, 0, true)
t.equal(