From a2621ad70571f6ade9d2be42632ece042e068805 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 25 Nov 2017 11:57:46 +0100 Subject: [PATCH] batch array: assert elements are objects --- abstract-leveldown.js | 8 ++++---- abstract/batch-test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/abstract-leveldown.js b/abstract-leveldown.js index 1fcfdf9f..3e641d9d 100644 --- a/abstract-leveldown.js +++ b/abstract-leveldown.js @@ -161,11 +161,11 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) { if (!options || typeof options != 'object') options = {} - var serialized = [] + var serialized = new Array(array.length) for (var i = 0; i < array.length; i++) { - if (typeof array[i] !== 'object') - continue + if (typeof array[i] !== 'object' || array[i] === null) + return callback(new Error('batch(array) element must be an object and not `null`')) var e = Object.assign({}, array[i]) var err @@ -181,7 +181,7 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) { if (e.type !== 'del') e.value = this._serializeValue(e.value) - serialized.push(e) + serialized[i] = e } if (typeof this._batch == 'function') diff --git a/abstract/batch-test.js b/abstract/batch-test.js index a178dc18..61a62ceb 100644 --- a/abstract/batch-test.js +++ b/abstract/batch-test.js @@ -84,6 +84,18 @@ module.exports.args = function (test) { t.end() }) }) + + ;[null, undefined, 1, true].forEach(function (element) { + var type = element === null ? 'null' : typeof element + + test('test batch() with ' + type + ' element', function (t) { + db.batch([element], function (err) { + t.ok(err, 'got error') + t.equal(err.message, 'batch(array) element must be an object and not `null`', 'correct error message') + t.end() + }) + }) + }) } module.exports.batch = function (test) {