native-buffer-browserify
Advanced tools
Comparing version 2.0.3 to 2.0.5
14
index.js
@@ -10,7 +10,7 @@ var base64 = require('base64-js') | ||
/** | ||
* If `browserSupport`: | ||
* If `Buffer._useTypedArrays`: | ||
* === true Use Uint8Array implementation (fastest) | ||
* === false Use Object implementation (compatible down to IE6) | ||
*/ | ||
var browserSupport = (function () { | ||
Buffer._useTypedArrays = (function () { | ||
// Detect if browser supports Typed Arrays. Supported browsers are IE 10+, | ||
@@ -74,3 +74,3 @@ // Firefox 4+, Chrome 7+, Safari 5.1+, Opera 11.6+, iOS 4.2+. | ||
var buf | ||
if (browserSupport) { | ||
if (Buffer._useTypedArrays) { | ||
// Preferred: Return an augmented `Uint8Array` instance for best performance | ||
@@ -98,3 +98,3 @@ buf = augment(new Uint8Array(length)) | ||
buf.write(subject, 0, encoding) | ||
} else if (type === 'number' && !browserSupport && !noZero) { | ||
} else if (type === 'number' && !Buffer._useTypedArrays && !noZero) { | ||
for (i = 0; i < length; i++) { | ||
@@ -387,4 +387,2 @@ buf[i] = 0 | ||
// TODO: add test that modifying the new buffer slice will modify memory in the | ||
// original buffer! Use code from: | ||
// http://nodejs.org/api/buffer.html#buffer_buf_slice_start_end | ||
@@ -396,7 +394,5 @@ Buffer.prototype.slice = function (start, end) { | ||
if (browserSupport) { | ||
if (Buffer._useTypedArrays) { | ||
return augment(this.subarray(start, end)) | ||
} else { | ||
// TODO: slicing works, with limitations (no parent tracking/update) | ||
// https://github.com/feross/native-buffer-browserify/issues/9 | ||
var sliceLen = end - start | ||
@@ -403,0 +399,0 @@ var newBuf = new Buffer(sliceLen, undefined, true) |
{ | ||
"name": "native-buffer-browserify", | ||
"version": "2.0.3", | ||
"version": "2.0.5", | ||
"description": "buffer module compatibility for browserify (backed by ArrayBuffer so its fast!)", | ||
"main": "index.js", | ||
"dependencies": { | ||
"base64-js": "feross/base64-js", | ||
"base64-js": "git://github.com/feross/base64-js.git", | ||
"ieee754": "~1.1.1" | ||
@@ -9,0 +9,0 @@ }, |
@@ -19,3 +19,3 @@ native-buffer-browserify | ||
- **Backed by `ArrayBuffer` (not `Object`, so it's fast)** | ||
- **Backed by Typed Arrays (`Uint8Array` and `ArrayBuffer`) (not `Object`, so it's fast)** | ||
- **Bundle size is nearly half of the original `buffer-browserify` (35KB vs 65KB!)** | ||
@@ -25,3 +25,3 @@ - **Excellent browser support (IE 6+, Chrome 4+, Firefox 3+, Safari 5.1+, Opera 11+, iOS).** | ||
- Faster pretty much across the board (see perf results below) | ||
- `.slice()` returns instances of the same type | ||
- `.slice()` returns instances of the same type (Buffer) | ||
- Square-bracket `buf[4]` notation works, even in old browsers like IE6! | ||
@@ -37,3 +37,7 @@ - Does not modify any browser prototypes. | ||
## Important Differences | ||
- **Use `Buffer.isBuffer` instead of `instanceof Buffer`.** `instanceof Buffer` doesn’t work because the Buffer constructor returns a `Uint8Array` (as discussed above) for performance reasons. In node `Buffer.isBuffer` just does `instanceof Buffer`, but in browserify we use a `Buffer.isBuffer` shim that detects our special `Uint8Array`-based Buffers. | ||
- **Don't rely on `slice()` to modify the memory of the parent buffer.** If the browser is using the Typed Array implementation then modifying a buffer created by `slice()` will modify the original memory, [just like in Node](http://nodejs.org/api/buffer.html#buffer_buf_slice_start_end). But for the Object implementation (used in unsupported browsers), this is not possible. Therefore, do not rely on this behavior until browser support gets better. (Note: currently even Firefox isn't using the Typed Array implementation because of [this bug](https://bugzilla.mozilla.org/show_bug.cgi?id=952403).) | ||
## Performance | ||
@@ -40,0 +44,0 @@ |
@@ -1,273 +0,263 @@ | ||
var B = require('../index.js').Buffer; | ||
var test = require('tape'); | ||
var B = require('../index.js').Buffer | ||
var test = require('tape') | ||
test('utf8 buffer to base64', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("Ձאab", "utf8").toString("base64"), | ||
'1YHXkGFi' | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test('utf8 buffer to hex', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("Ձאab", "utf8").toString("hex"), | ||
'd581d7906162' | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test('utf8 to utf8', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("öäüõÖÄÜÕ", "utf8").toString("utf8"), | ||
'öäüõÖÄÜÕ' | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test('ascii buffer to base64', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("123456!@#$%^", "ascii").toString("base64"), | ||
'MTIzNDU2IUAjJCVe' | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test('ascii buffer to hex', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("123456!@#$%^", "ascii").toString("hex"), | ||
'31323334353621402324255e' | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test('base64 buffer to utf8', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("1YHXkGFi", "base64").toString("utf8"), | ||
'Ձאab' | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test('hex buffer to utf8', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("d581d7906162", "hex").toString("utf8"), | ||
'Ձאab' | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test('base64 buffer to ascii', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("MTIzNDU2IUAjJCVe", "base64").toString("ascii"), | ||
'123456!@#$%^' | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test('hex buffer to ascii', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("31323334353621402324255e", "hex").toString("ascii"), | ||
'123456!@#$%^' | ||
); | ||
t.end(); | ||
}); | ||
/* | ||
test('utf8 to ascii', function (t) { | ||
t.plan(1); | ||
t.equal( | ||
new B("öäüõÖÄÜÕ", "utf8").toString("ascii"), | ||
new Buffer("öäüõÖÄÜÕ", "utf8").toString("ascii") | ||
); | ||
t.end(); | ||
}); | ||
*/ | ||
) | ||
t.end() | ||
}) | ||
test('base64 buffer to binary', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("MTIzNDU2IUAjJCVe", "base64").toString("binary"), | ||
'123456!@#$%^' | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test('hex buffer to binary', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("31323334353621402324255e", "hex").toString("binary"), | ||
'123456!@#$%^' | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test('utf8 to binary', function (t) { | ||
t.plan(1); | ||
t.plan(1) | ||
t.equal( | ||
new B("öäüõÖÄÜÕ", "utf8").toString("binary"), | ||
"öäüõÃÃÃÃ" | ||
); | ||
t.end(); | ||
}); | ||
) | ||
t.end() | ||
}) | ||
test("hex of write{Uint,Int}{8,16,32}{LE,BE}", function (t) { | ||
t.plan(2*(2*2*2+2)); | ||
t.plan(2*(2*2*2+2)) | ||
var 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]; | ||
] | ||
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]; | ||
var x = xs[i] | ||
for (var j = 0; j < ys.length; j++) { | ||
var y = ys[j]; | ||
var endianesses = (y === 8) ? [""] : ["LE","BE"]; | ||
var y = ys[j] | ||
var endianesses = (y === 8) ? [""] : ["LE","BE"] | ||
for (var k = 0; k < endianesses.length; k++) { | ||
var z = endianesses[k]; | ||
var z = endianesses[k] | ||
var v1 = new B(y / 8); | ||
var writefn = "write" + x + y + z; | ||
var val = (x === "Int") ? -3 : 3; | ||
v1[writefn](val, 0); | ||
var v1 = new B(y / 8) | ||
var writefn = "write" + x + y + z | ||
var val = (x === "Int") ? -3 : 3 | ||
v1[writefn](val, 0) | ||
t.equal( | ||
v1.toString("hex"), | ||
hex.shift() | ||
); | ||
var readfn = "read" + x + y + z; | ||
) | ||
var readfn = "read" + x + y + z | ||
t.equal( | ||
v1[readfn](0), | ||
reads.shift() | ||
); | ||
) | ||
} | ||
} | ||
} | ||
t.end(); | ||
}); | ||
t.end() | ||
}) | ||
test("hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow", function (t) { | ||
t.plan(3*(2*2*2+2)); | ||
t.plan(3*(2*2*2+2)) | ||
var hex = [ | ||
"", "03", "00", "030000", "000000", | ||
"", "fd", "ff", "fdffff", "ffffff" | ||
]; | ||
] | ||
var reads = [ | ||
undefined, 3, 0, 3, 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]; | ||
var x = xs[i] | ||
for (var j = 0; j < ys.length; j++) { | ||
var y = ys[j]; | ||
var endianesses = (y === 8) ? [""] : ["LE","BE"]; | ||
var y = ys[j] | ||
var endianesses = (y === 8) ? [""] : ["LE","BE"] | ||
for (var k = 0; k < endianesses.length; k++) { | ||
var z = endianesses[k]; | ||
var z = endianesses[k] | ||
var v1 = new B(y / 8 - 1); | ||
var next = new B(4); | ||
next.writeUInt32BE(0, 0); | ||
var writefn = "write" + x + y + z; | ||
var val = (x === "Int") ? -3 : 3; | ||
v1[writefn](val, 0, true); | ||
var v1 = new B(y / 8 - 1) | ||
var next = new B(4) | ||
next.writeUInt32BE(0, 0) | ||
var writefn = "write" + x + y + z | ||
var val = (x === "Int") ? -3 : 3 | ||
v1[writefn](val, 0, true) | ||
t.equal( | ||
v1.toString("hex"), | ||
hex.shift() | ||
); | ||
) | ||
// check that nothing leaked to next buffer. | ||
t.equal(next.readUInt32BE(0), 0); | ||
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; | ||
next.writeInt32BE(~0, 0) | ||
var readfn = "read" + x + y + z | ||
t.equal( | ||
v1[readfn](0, true), | ||
reads.shift() | ||
); | ||
) | ||
} | ||
} | ||
} | ||
t.end(); | ||
}); | ||
t.end() | ||
}) | ||
test("concat() a varying number of buffers", function (t) { | ||
t.plan(5); | ||
var zero = []; | ||
var one = [ new B('asdf') ]; | ||
var long = []; | ||
for (var i = 0; i < 10; i++) long.push(new B('asdf')); | ||
t.plan(5) | ||
var zero = [] | ||
var one = [ new B('asdf') ] | ||
var long = [] | ||
for (var 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); | ||
var flatZero = B.concat(zero) | ||
var flatOne = B.concat(one) | ||
var flatLong = B.concat(long) | ||
var flatLongLen = B.concat(long, 40) | ||
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.end(); | ||
}); | ||
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.end() | ||
}) | ||
test("buffer from buffer", function (t) { | ||
t.plan(1); | ||
var b1 = new B('asdf'); | ||
var b2 = new B(b1); | ||
t.equal(b1.toString('hex'), b2.toString('hex')); | ||
t.end(); | ||
}); | ||
t.plan(1) | ||
var b1 = new B('asdf') | ||
var b2 = new B(b1) | ||
t.equal(b1.toString('hex'), b2.toString('hex')) | ||
t.end() | ||
}) | ||
test("fill", function(t) { | ||
t.plan(1); | ||
var b = new B(10); | ||
b.fill(2); | ||
t.equal(b.toString('hex'), '02020202020202020202'); | ||
t.end(); | ||
}); | ||
t.plan(1) | ||
var b = new B(10) | ||
b.fill(2) | ||
t.equal(b.toString('hex'), '02020202020202020202') | ||
t.end() | ||
}) | ||
test('copy() empty buffer with sourceEnd=0', function (t) { | ||
t.plan(1); | ||
var source = new B([42]); | ||
var destination = new B([43]); | ||
source.copy(destination, 0, 0, 0); | ||
t.equal(destination.readUInt8(0), 43); | ||
t.end(); | ||
}); | ||
t.plan(1) | ||
var source = new B([42]) | ||
var destination = new B([43]) | ||
source.copy(destination, 0, 0, 0) | ||
t.equal(destination.readUInt8(0), 43) | ||
t.end() | ||
}) | ||
test('base64 ignore whitespace', function(t) { | ||
t.plan(1); | ||
var text = "\n YW9ldQ== "; | ||
var buf = new B(text, 'base64'); | ||
t.equal(buf.toString(), 'aoeu'); | ||
t.end(); | ||
}); | ||
t.plan(1) | ||
var text = "\n YW9ldQ== " | ||
var buf = new B(text, 'base64') | ||
t.equal(buf.toString(), 'aoeu') | ||
t.end() | ||
}) | ||
test('buffer.slice sets indexes', function (t) { | ||
t.plan(1); | ||
t.equal((new B('hallo')).slice(0, 5).toString(), 'hallo'); | ||
t.end(); | ||
}); | ||
t.plan(1) | ||
t.equal((new B('hallo')).slice(0, 5).toString(), 'hallo') | ||
t.end() | ||
}) | ||
test('buffer.slice out of range', function (t) { | ||
t.plan(2); | ||
t.equal((new B('hallo')).slice(0, 10).toString(), 'hallo'); | ||
t.equal((new B('hallo')).slice(10, 2).toString(), ''); | ||
t.end(); | ||
}); | ||
t.plan(2) | ||
t.equal((new B('hallo')).slice(0, 10).toString(), 'hallo') | ||
t.equal((new B('hallo')).slice(10, 2).toString(), '') | ||
t.end() | ||
}) | ||
test('base64 strings without padding', function (t) { | ||
t.plan(1); | ||
t.equal((new B('YW9ldQ', 'base64').toString()), 'aoeu'); | ||
t.end(); | ||
}); | ||
t.plan(1) | ||
t.equal((new B('YW9ldQ', 'base64').toString()), 'aoeu') | ||
t.end() | ||
}) |
@@ -1,29 +0,29 @@ | ||
var B = require('../').Buffer; | ||
var test = require('tape'); | ||
var B = require('../').Buffer | ||
var test = require('tape') | ||
test('indexes from a string', function(t) { | ||
t.plan(3); | ||
var buf = new B('abc'); | ||
t.equal(buf[0], 97); | ||
t.equal(buf[1], 98); | ||
t.equal(buf[2], 99); | ||
}); | ||
t.plan(3) | ||
var 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) { | ||
t.plan(3); | ||
var buf = new B([ 97, 98, 99 ]); | ||
t.equal(buf[0], 97); | ||
t.equal(buf[1], 98); | ||
t.equal(buf[2], 99); | ||
}); | ||
t.plan(3) | ||
var buf = new B([ 97, 98, 99 ]) | ||
t.equal(buf[0], 97) | ||
t.equal(buf[1], 98) | ||
t.equal(buf[2], 99) | ||
}) | ||
test('set then modify indexes from an array', function(t) { | ||
t.plan(4); | ||
var buf = new B([ 97, 98, 99 ]); | ||
t.equal(buf[2], 99); | ||
t.equal(buf.toString(), 'abc'); | ||
buf[2] += 10; | ||
t.equal(buf[2], 109); | ||
t.equal(buf.toString(), 'abm'); | ||
}); | ||
t.plan(4) | ||
var buf = new B([ 97, 98, 99 ]) | ||
t.equal(buf[2], 99) | ||
t.equal(buf.toString(), 'abc') | ||
buf[2] += 10 | ||
t.equal(buf[2], 109) | ||
t.equal(buf.toString(), 'abm') | ||
}) |
var B = require('../index.js').Buffer; | ||
var test = require('tape'); | ||
var B = require('../index.js').Buffer | ||
var test = require('tape') | ||
test('Buffer.isEncoding', function (t) { | ||
t.plan(3); | ||
t.equal(B.isEncoding('HEX'), true); | ||
t.equal(B.isEncoding('hex'), true); | ||
t.equal(B.isEncoding('bad'), false); | ||
t.end(); | ||
}); | ||
t.plan(3) | ||
t.equal(B.isEncoding('HEX'), true) | ||
t.equal(B.isEncoding('hex'), true) | ||
t.equal(B.isEncoding('bad'), false) | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Git dependency
Supply chain riskContains a dependency which resolves to a remote git URL. Dependencies fetched from git URLs are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
GitHub dependency
Supply chain riskContains a dependency which resolves to a GitHub URL. Dependencies fetched from GitHub specifiers are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
1211042
35
17698
136