firstline = false
}
- // comment out require('common')
- line = line.replace(/((var|const) common = require.*)/, 'var common = {};')
+ // use `var` instead of `const`/`let`
+ line = line.replace(/(const|let) /g, 'var ')
+
+ // make `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)')
// require browser buffer
- line = line.replace(/(.*)require\('buffer'\)(.*)/, '$1require(\'../../\')$2')
+ line = line.replace(/(.*)require\('buffer'\)(.*)/g, '$1require(\'../../\')$2')
// smalloc is only used for kMaxLength
line = line.replace(
- /require\('smalloc'\)/,
+ /require\('smalloc'\)/g,
'{ kMaxLength: process.env.OBJECT_IMPL ? 0x3fffffff : 0x7fffffff }'
)
// comment out console logs
- line = line.replace(/(.*console\..*)/, '// $1')
+ line = line.replace(/(.*console\..*)/g, '// $1')
// we can't reliably test typed array max-sizes in the browser
if (filename === 'test-buffer-big.js') {
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false;
+
+var common = {};
+var assert = require('assert');
+
+var Buffer = require('../../').Buffer;
+var LENGTH = 16;
+
+var ab = new ArrayBuffer(LENGTH);
+var dv = new DataView(ab);
+var ui = new Uint8Array(ab);
+var buf = new Buffer(ab);
+
+
+assert.ok(Buffer.isBuffer(buf));
+// For backwards compatibility of old .parent property test that if buf is not
+// a slice then .parent should be undefined.
+assert.equal(buf.parent, undefined);
+assert.equal(buf.buffer, ab);
+assert.equal(buf.length, ab.byteLength);
+
+
+buf.fill(0xC);
+for (var i = 0; i < LENGTH; i++) {
+ assert.equal(ui[i], 0xC);
+ ui[i] = 0xF;
+ assert.equal(buf[i], 0xF);
+}
+
+buf.writeUInt32LE(0xF00, 0);
+buf.writeUInt32BE(0xB47, 4);
+buf.writeDoubleLE(3.1415, 8);
+
+assert.equal(dv.getUint32(0, true), 0xF00);
+assert.equal(dv.getUint32(4), 0xB47);
+assert.equal(dv.getFloat64(8, true), 3.1415);
+
+
+// Now test protecting users from doing stupid things
+
+assert.throws(function() {
+ function AB() { }
+ AB.__proto__ = ArrayBuffer;
+ AB.prototype.__proto__ = ArrayBuffer.prototype;
+ new Buffer(new AB());
+}, TypeError);
+
+++ /dev/null
-'use strict';
-var Buffer = require('../../').Buffer;
-if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false;
-var common = {};
-var assert = require('assert');
-
-// The tests below should throw an error, not abort the process...
-assert.throws(function() { new Buffer(0x3fffffff + 1); }, RangeError);
-// assert.throws(function() { new Int8Array(0x3fffffff + 1); }, RangeError);
-// assert.throws(function() { new ArrayBuffer(0x3fffffff + 1); }, RangeError);
-// assert.throws(function() { new Float64Array(0x7ffffff + 1); }, RangeError);
-
assert(flatZero.length === 0);
assert(flatOne.toString() === 'asdf');
-assert(flatOne === one[0]);
+// A special case where concat used to return the first item,
+// if the length is one. This check is to make sure that we don't do that.
+assert(flatOne !== one[0]);
assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));
+assert.throws(function() {
+ Buffer.concat([42]);
+}, TypeError);
+
// console.log('ok');
+++ /dev/null
-'use strict';
-var Buffer = require('../../').Buffer;
-if (process.env.OBJECT_IMPL) Buffer.TYPED_ARRAY_SUPPORT = false;
-var common = {};
-var assert = require('assert');
-
-var Buffer = require('../../').Buffer;
-
-var buff = new Buffer(Buffer.poolSize + 1);
-var slicedBuffer = buff.slice();
-assert.equal(slicedBuffer.parent,
- buff,
- 'slicedBufffer should have its parent set to the original ' +
- ' buffer');
-
var Buffer = require('../../').Buffer;
var SlowBuffer = require('../../').SlowBuffer;
-var smalloc = { kMaxLength: process.env.OBJECT_IMPL ? 0x3fffffff : 0x7fffffff };
// counter to ensure unique value is always copied
var cntr = 0;
var b = new SlowBuffer(5);
var c = b.slice(0, 4);
var d = c.slice(0, 2);
-assert.equal(b, c.parent);
-assert.equal(b, d.parent);
// Bug regression test
// Check for fractional length args, junk length args, etc.
// https://github.com/joyent/node/issues/1758
-Buffer(3.3).toString(); // throws bad argument error in commit 43cb4ec
+
+// Call .fill() first, stops valgrind warning about uninitialized memory reads.
+Buffer(3.3).fill().toString(); // throws bad argument error in commit 43cb4ec
assert.equal(Buffer(-1).length, 0);
assert.equal(Buffer(NaN).length, 0);
assert.equal(Buffer(3.3).length, 3);
// try to slice a zero length Buffer
// see https://github.com/joyent/node/issues/5881
SlowBuffer(0).slice(0, 1);
- // make sure a zero length slice doesn't set the .parent attribute
- assert.equal(Buffer(5).slice(0, 0).parent, undefined);
- // and make sure a proper slice does have a parent
- assert.ok(typeof Buffer(5).slice(0, 5).parent === 'object');
})();
// Regression test for #5482: should throw but not assert in C++ land.
assert.throws(function() {
- new Buffer(smalloc.kMaxLength + 1);
+ new Buffer((-1 >>> 0) + 1);
}, RangeError);
assert.throws(function() {
- new SlowBuffer(smalloc.kMaxLength + 1);
+ new SlowBuffer((-1 >>> 0) + 1);
}, RangeError);
if (common.hasCrypto) {
Buffer(10).copy();
});
+assert.throws(function() {
+ new Buffer();
+}, /must start with number, buffer, array or string/);
+
+assert.throws(function() {
+ new Buffer(null);
+}, /must start with number, buffer, array or string/);
+