Skip to content

Replace buffer constructor with buffer-alloc and buffer-from #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ var Readable = require('readable-stream/readable');
var peek = require('level-peek');
var util = require('util');
var once = require('once');
var bufferAlloc = require('buffer-alloc');
var bufferFrom = require('buffer-from');

var EMPTY = new Buffer(0);
var EMPTY = bufferAlloc(0);
var ENCODER = {
encode: function(data) {
return typeof data === 'string' ? data = new Buffer(data) : data;
return typeof data === 'string' ? data = bufferFrom(data) : data;
},
decode: function(data) {
return Buffer.isBuffer(data) ? data : new Buffer(data);
return Buffer.isBuffer(data) ? data : bufferFrom(data);
},
buffer: true,
type: 'raw'
Expand All @@ -24,7 +26,7 @@ var pad = function(n) {
};

var expand = function(buf, len) {
var tmp = new Buffer(len);
var tmp = bufferAlloc(len);
buf.copy(tmp);
return tmp;
};
Expand All @@ -36,7 +38,7 @@ module.exports = function(db, opts) {

var blockSize = opts.blockSize || 65536;
var maxBatch = opts.batch || 100;
var blank = new Buffer(blockSize);
var blank = bufferAlloc(blockSize);

db.put('\x00', 'ignore', noop); // memdown#12 workaround

Expand All @@ -55,7 +57,7 @@ module.exports = function(db, opts) {
return;
}

if (!r.block) r.block = new Buffer(blockSize);
if (!r.block) r.block = bufferAlloc(blockSize);
if (r.block.length < offset + block.length) r.block = expand(r.block, offset + block.length);

block.copy(r.block, offset);
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
"store"
],
"dependencies": {
"buffer-alloc": "^1.1.0",
"buffer-from": "^1.0.0",
"level-peek": "1.0.6",
"once": "^1.3.0",
"readable-stream": "^1.0.26-4",
"level-peek": "1.0.6"
"readable-stream": "^1.0.26-4"
},
"devDependencies": {
"tape": "^2.12.3",
Expand Down
3 changes: 2 additions & 1 deletion test/multi-block.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var tape = require('tape');
var levelup = require('levelup');
var memdown = require('memdown');
var bufferAlloc = require('buffer-alloc');

var blobs = function() {
return require('../')(levelup('mem', {db:memdown}));
Expand All @@ -9,7 +10,7 @@ var blobs = function() {
var allocer = function() {
var i = 0;
return function() {
var buf = new Buffer(50000);
var buf = bufferAlloc(50000);
buf.fill(i++);
return buf;
};
Expand Down