process.nextTick(function genPrime() { var primes = []; var len = (max >>> 3) + 1; var sieve = new SB(len); sieve.fill(0xff, 0, len); var cntr, x, j; for (cntr = 0, x = 2; x <= max; ++x) { if (sieve[x >>> 3] & (1 << (x & 7))) { primes[cntr++] = x for(j = 2 * x; j <= max; j += x) sieve[j >>> 3] &= ~(1 << (j & 7)) } } return primes; });
assert.throws(function() { var len = 0xfffff; var sbuf = new SlowBuffer(len); var buf = new Buffer(sbuf, len, 0); SlowBuffer.makeFastBuffer(sbuf, buf, -len, len); // Should throw. for (var i = 0; i < len; ++i) buf[i] = 0x42; // Try to force segfault. }, RangeError);
export function encode(value) { if (typeof value === 'string' && rEscaped.test(value)) { let expectedLen = value.length * 3; if (bufLen < expectedLen) { do { bufLen <<= 1 } while (bufLen < expectedLen); buf = new SlowBuffer(bufLen); } return buf.toString('binary', 0, buf.write(value)); } return value; }
it("SlowBuffer is supported", function() { var SlowBuffer = require('buffer').SlowBuffer; var buf = new SlowBuffer(10); assert.equal(buf.write("hello world", "windows-1251"), 10); assert.equal(buf.toString(), "hello worl"); assert.equal(buf.write("hello world", 2, "windows-1251"), 8); assert.equal(buf.toString(), "hehello wo"); assert.equal(buf.write("abcde abcde", 3, 4, "windows-1251"), 4); assert.equal(buf.toString(), "hehabcd wo"); assert.equal(buf.write("活洽派洶洛泵洹洧", 1, "big5"), 9); assert.equal(buf.toString('big5'), "h活洽派洶�"); // TODO: the following line is more correct. //assert.equal(buf.toString('big5'), "h活洽派洶o"); // TODO: Set _charsWritten. });
function FastBuffer (length) { this.length = length; if (length > POOLSIZE) { // Big buffer, just alloc one. this.parent = new Buffer(length); this.offset = 0; } else { // Small buffer. if (!pool || pool.length - pool.used < length) allocPool(); this.parent = pool; this.offset = pool.used; pool.used += length; } // HERE HERE HERE SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length); }
'use strict'; var common = require('../common'); var assert = require('assert'); var util = require('util'); var buffer = require('buffer'); buffer.INSPECT_MAX_BYTES = 2; var b = new Buffer(4); b.fill('1234'); var s = new buffer.SlowBuffer(4); s.fill('1234'); var expected = '<Buffer 31 32 ... >'; assert.strictEqual(util.inspect(b), expected); assert.strictEqual(util.inspect(s), expected); b = new Buffer(2); b.fill('12'); s = new buffer.SlowBuffer(2); s.fill('12'); expected = '<Buffer 31 32>'; assert.strictEqual(util.inspect(b), expected); assert.strictEqual(util.inspect(s), expected);
assert.throws(function() { var buf = new SlowBuffer(8); buf.writeFloatLE(0.0, -1); }, /offset is not uint/);
assert.throws(function() { var buf = new SlowBuffer(8); buf.readFloatLE(-1); }, /offset is not uint/);
assert.throws(function() { var buf = new SlowBuffer(8); buf.writeFloatLE(0.0, 0xfffffff); }, /Trying to access beyond buffer length/);
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) { var b = new SlowBuffer(10); b.write('あいうえお', encoding); assert.equal(b.toString(encoding), 'あいうえお'); });
['base64','binary','ucs2','utf8','ascii'].forEach(function(encoding) { var buf = new SlowBuffer(0); buf.write('', encoding); });
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 from fast to slow buffer var sb = new SlowBuffer(b.length); var copied = b.copy(sb); console.log('copied %d bytes from b into sb'); for (var i = 0; i < sb.length; i++) { assert.strictEqual(sb[i], b[i]); } 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.throws(function() { var buf = new SlowBuffer(8); buf.readFloatLE(0xffffffff); }, /Trying to read beyond buffer length/);
assert.throws(function() { var buf = new SlowBuffer(8); buf.writeFloatLE(0.0, 0xffffffff); // TRIREME: access instead of read, ok? }, /Trying to access beyond buffer length/);
assert.doesNotThrow(function () { var slow = new SlowBuffer(1); assert(slow.write('', Buffer.poolSize * 10) === 0); var fast = new Buffer(1); assert(fast.write('', Buffer.poolSize * 10) === 0); });
assert.equal(b[100 + i], slice[i]); } } { // make sure only top level parent propagates from allocPool const b = new Buffer(5); const c = b.slice(0, 4); const d = c.slice(0, 2); assert.equal(b.parent, c.parent); assert.equal(b.parent, d.parent); } { // also from a non-pooled instance const b = new SlowBuffer(5); const c = b.slice(0, 4); const d = c.slice(0, 2); assert.equal(c.parent, d.parent); } { // Bug regression test const testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語 const buffer = new Buffer(32); const size = buffer.write(testValue, 0, 'utf8'); console.log('bytes written to buffer: ' + size); const slice = buffer.toString('utf8', 0, size); assert.equal(slice, testValue); }
assert.throws(function() { var len = 0xfffff; var sbuf = new SlowBuffer(len); sbuf = sbuf.slice(-len); // Should throw. for (var i = 0; i < len; ++i) sbuf[i] = 0x42; // Try to force segfault. }, RangeError);