From 2ad5759d7e57563db5fcb0e312c87926b8c1692c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Rebours?= Date: Mon, 25 Oct 2021 11:26:55 +0200 Subject: [PATCH] remove async dependency --- benchmarks/ensureIndex.js | 9 ++--- benchmarks/find.js | 10 +++--- benchmarks/findOne.js | 10 +++--- benchmarks/findWithIn.js | 10 +++--- benchmarks/insert.js | 8 ++--- benchmarks/loadDatabase.js | 10 +++--- benchmarks/profiler.js | 1 + benchmarks/remove.js | 14 ++++---- benchmarks/update.js | 14 ++++---- karma.conf.template.js | 2 +- package-lock.json | 4 +-- package.json | 1 - test/browser/load.spec.js | 68 +++++++++++++++++++------------------- test/cursor.test.js | 36 ++++++++++---------- test/db.test.js | 36 ++++++++++---------- test/executor.test.js | 4 +-- test/persistence.test.js | 10 +++--- test/utils.test.js | 33 ++++++++++++++++++ test_lac/openFds.test.js | 10 +++--- webpack.config.js | 5 +-- 20 files changed, 164 insertions(+), 131 deletions(-) create mode 100644 test/utils.test.js diff --git a/benchmarks/ensureIndex.js b/benchmarks/ensureIndex.js index 43b06b7..cf33c16 100755 --- a/benchmarks/ensureIndex.js +++ b/benchmarks/ensureIndex.js @@ -1,5 +1,5 @@ -const async = require('async') const program = require('commander') +const { apply, waterfall } = require('../test/utils.test.js') const Datastore = require('../lib/datastore') const commonUtilities = require('./commonUtilities') const Profiler = require('./profiler') @@ -19,8 +19,8 @@ console.log('----------------------------') console.log('Test with ' + n + ' documents') console.log('----------------------------') -async.waterfall([ - async.apply(commonUtilities.prepareDb, benchDb), +waterfall([ + apply(commonUtilities.prepareDb, benchDb), function (cb) { d.loadDatabase(function (err) { if (err) { return cb(err) } @@ -28,7 +28,7 @@ async.waterfall([ }) }, function (cb) { profiler.beginProfiling(); return cb() }, - async.apply(commonUtilities.insertDocs, d, n, profiler), + apply(commonUtilities.insertDocs, d, n, profiler), function (cb) { let i @@ -41,6 +41,7 @@ async.waterfall([ console.log('Average time for one ensureIndex: ' + (profiler.elapsedSinceLastStep() / n) + 'ms') profiler.step('Finished calling ensureIndex ' + n + ' times') + cb() } ], function (err) { profiler.step('Benchmark finished') diff --git a/benchmarks/find.js b/benchmarks/find.js index b81b03d..d3c9a83 100755 --- a/benchmarks/find.js +++ b/benchmarks/find.js @@ -1,4 +1,4 @@ -const async = require('async') +const { apply, waterfall } = require('../test/utils.test.js') const commonUtilities = require('./commonUtilities') const Profiler = require('./profiler') @@ -8,8 +8,8 @@ const config = commonUtilities.getConfiguration(benchDb) const d = config.d const n = config.n -async.waterfall([ - async.apply(commonUtilities.prepareDb, benchDb), +waterfall([ + apply(commonUtilities.prepareDb, benchDb), function (cb) { d.loadDatabase(function (err) { if (err) { return cb(err) } @@ -18,8 +18,8 @@ async.waterfall([ }) }, function (cb) { profiler.beginProfiling(); return cb() }, - async.apply(commonUtilities.insertDocs, d, n, profiler), - async.apply(commonUtilities.findDocs, d, n, profiler) + apply(commonUtilities.insertDocs, d, n, profiler), + apply(commonUtilities.findDocs, d, n, profiler) ], function (err) { profiler.step('Benchmark finished') diff --git a/benchmarks/findOne.js b/benchmarks/findOne.js index 93911bd..b965709 100755 --- a/benchmarks/findOne.js +++ b/benchmarks/findOne.js @@ -1,4 +1,4 @@ -const async = require('async') +const { apply, waterfall } = require('../test/utils.test.js') const commonUtilities = require('./commonUtilities') const Profiler = require('./profiler') @@ -8,8 +8,8 @@ const config = commonUtilities.getConfiguration(benchDb) const d = config.d const n = config.n -async.waterfall([ - async.apply(commonUtilities.prepareDb, benchDb), +waterfall([ + apply(commonUtilities.prepareDb, benchDb), function (cb) { d.loadDatabase(function (err) { if (err) { return cb(err) } @@ -18,9 +18,9 @@ async.waterfall([ }) }, function (cb) { profiler.beginProfiling(); return cb() }, - async.apply(commonUtilities.insertDocs, d, n, profiler), + apply(commonUtilities.insertDocs, d, n, profiler), function (cb) { setTimeout(function () { cb() }, 500) }, - async.apply(commonUtilities.findOneDocs, d, n, profiler) + apply(commonUtilities.findOneDocs, d, n, profiler) ], function (err) { profiler.step('Benchmark finished') diff --git a/benchmarks/findWithIn.js b/benchmarks/findWithIn.js index e167a6f..8df0c41 100755 --- a/benchmarks/findWithIn.js +++ b/benchmarks/findWithIn.js @@ -1,4 +1,4 @@ -const async = require('async') +const { apply, waterfall } = require('../test/utils.test.js') const commonUtilities = require('./commonUtilities') const Profiler = require('./profiler') @@ -8,8 +8,8 @@ const config = commonUtilities.getConfiguration(benchDb) const d = config.d const n = config.n -async.waterfall([ - async.apply(commonUtilities.prepareDb, benchDb), +waterfall([ + apply(commonUtilities.prepareDb, benchDb), function (cb) { d.loadDatabase(function (err) { if (err) { return cb(err) } @@ -18,8 +18,8 @@ async.waterfall([ }) }, function (cb) { profiler.beginProfiling(); return cb() }, - async.apply(commonUtilities.insertDocs, d, n, profiler), - async.apply(commonUtilities.findDocsWithIn, d, n, profiler) + apply(commonUtilities.insertDocs, d, n, profiler), + apply(commonUtilities.findDocsWithIn, d, n, profiler) ], function (err) { profiler.step('Benchmark finished') diff --git a/benchmarks/insert.js b/benchmarks/insert.js index f78d8b5..4938212 100755 --- a/benchmarks/insert.js +++ b/benchmarks/insert.js @@ -1,4 +1,4 @@ -const async = require('async') +const { apply, waterfall } = require('../test/utils.test.js') const commonUtilities = require('./commonUtilities') const Profiler = require('./profiler') @@ -8,8 +8,8 @@ const config = commonUtilities.getConfiguration(benchDb) const d = config.d let n = config.n -async.waterfall([ - async.apply(commonUtilities.prepareDb, benchDb), +waterfall([ + apply(commonUtilities.prepareDb, benchDb), function (cb) { d.loadDatabase(function (err) { if (err) { return cb(err) } @@ -24,7 +24,7 @@ async.waterfall([ }) }, function (cb) { profiler.beginProfiling(); return cb() }, - async.apply(commonUtilities.insertDocs, d, n, profiler) + apply(commonUtilities.insertDocs, d, n, profiler) ], function (err) { profiler.step('Benchmark finished') diff --git a/benchmarks/loadDatabase.js b/benchmarks/loadDatabase.js index a9910d3..b007dfb 100755 --- a/benchmarks/loadDatabase.js +++ b/benchmarks/loadDatabase.js @@ -1,4 +1,4 @@ -const async = require('async') +const { apply, waterfall } = require('../test/utils.test.js') const program = require('commander') const Datastore = require('../lib/datastore') const commonUtilities = require('./commonUtilities') @@ -20,14 +20,14 @@ console.log('Test with ' + n + ' documents') console.log(program.withIndex ? 'Use an index' : "Don't use an index") console.log('----------------------------') -async.waterfall([ - async.apply(commonUtilities.prepareDb, benchDb), +waterfall([ + apply(commonUtilities.prepareDb, benchDb), function (cb) { d.loadDatabase(cb) }, function (cb) { profiler.beginProfiling(); return cb() }, - async.apply(commonUtilities.insertDocs, d, n, profiler), - async.apply(commonUtilities.loadDatabase, d, n, profiler) + apply(commonUtilities.insertDocs, d, n, profiler), + apply(commonUtilities.loadDatabase, d, n, profiler) ], function (err) { profiler.step('Benchmark finished') diff --git a/benchmarks/profiler.js b/benchmarks/profiler.js index a5d73af..184aed2 100644 --- a/benchmarks/profiler.js +++ b/benchmarks/profiler.js @@ -1,3 +1,4 @@ +const { apply, waterfall } = require('../test/utils.test.js') const util = require('util') function formatTime (time, precision) { diff --git a/benchmarks/remove.js b/benchmarks/remove.js index a262d52..e3607f2 100755 --- a/benchmarks/remove.js +++ b/benchmarks/remove.js @@ -1,4 +1,4 @@ -const async = require('async') +const { apply, waterfall } = require('../test/utils.test.js') const commonUtilities = require('./commonUtilities') const Profiler = require('./profiler') @@ -8,8 +8,8 @@ const config = commonUtilities.getConfiguration(benchDb) const d = config.d const n = config.n -async.waterfall([ - async.apply(commonUtilities.prepareDb, benchDb), +waterfall([ + apply(commonUtilities.prepareDb, benchDb), function (cb) { d.loadDatabase(function (err) { if (err) { return cb(err) } @@ -18,16 +18,16 @@ async.waterfall([ }) }, function (cb) { profiler.beginProfiling(); return cb() }, - async.apply(commonUtilities.insertDocs, d, n, profiler), + apply(commonUtilities.insertDocs, d, n, profiler), // Test with remove only one document function (cb) { profiler.step('MULTI: FALSE'); return cb() }, - async.apply(commonUtilities.removeDocs, { multi: false }, d, n, profiler), + apply(commonUtilities.removeDocs, { multi: false }, d, n, profiler), // Test with multiple documents function (cb) { d.remove({}, { multi: true }, function () { return cb() }) }, - async.apply(commonUtilities.insertDocs, d, n, profiler), + apply(commonUtilities.insertDocs, d, n, profiler), function (cb) { profiler.step('MULTI: TRUE'); return cb() }, - async.apply(commonUtilities.removeDocs, { multi: true }, d, n, profiler) + apply(commonUtilities.removeDocs, { multi: true }, d, n, profiler) ], function (err) { profiler.step('Benchmark finished') diff --git a/benchmarks/update.js b/benchmarks/update.js index 1304f2f..f36dfd8 100755 --- a/benchmarks/update.js +++ b/benchmarks/update.js @@ -1,4 +1,4 @@ -const async = require('async') +const { apply, waterfall } = require('../test/utils.test.js') const commonUtilities = require('./commonUtilities') const Profiler = require('./profiler') @@ -8,8 +8,8 @@ const config = commonUtilities.getConfiguration(benchDb) const d = config.d const n = config.n -async.waterfall([ - async.apply(commonUtilities.prepareDb, benchDb), +waterfall([ + apply(commonUtilities.prepareDb, benchDb), function (cb) { d.loadDatabase(function (err) { if (err) { return cb(err) } @@ -18,18 +18,18 @@ async.waterfall([ }) }, function (cb) { profiler.beginProfiling(); return cb() }, - async.apply(commonUtilities.insertDocs, d, n, profiler), + apply(commonUtilities.insertDocs, d, n, profiler), // Test with update only one document function (cb) { profiler.step('MULTI: FALSE'); return cb() }, - async.apply(commonUtilities.updateDocs, { multi: false }, d, n, profiler), + apply(commonUtilities.updateDocs, { multi: false }, d, n, profiler), // Test with multiple documents // eslint-disable-next-line node/handle-callback-err function (cb) { d.remove({}, { multi: true }, function (err) { return cb() }) }, - async.apply(commonUtilities.insertDocs, d, n, profiler), + apply(commonUtilities.insertDocs, d, n, profiler), function (cb) { profiler.step('MULTI: TRUE'); return cb() }, - async.apply(commonUtilities.updateDocs, { multi: true }, d, n, profiler) + apply(commonUtilities.updateDocs, { multi: true }, d, n, profiler) ], function (err) { profiler.step('Benchmark finished') diff --git a/karma.conf.template.js b/karma.conf.template.js index ce493df..0eab6eb 100644 --- a/karma.conf.template.js +++ b/karma.conf.template.js @@ -14,7 +14,7 @@ module.exports = (config) => ({ // list of files / patterns to load in the browser files: [ 'node_modules/localforage/dist/localforage.min.js', - 'node_modules/async/lib/async.js', + 'browser-version/out/testUtils.min.js', 'browser-version/out/nedb.min.js', 'test/browser/nedb-browser.spec.js', 'test/browser/load.spec.js' diff --git a/package-lock.json b/package-lock.json index b530f55..c0d5dd9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,6 @@ "devDependencies": { "@react-native-async-storage/async-storage": "^1.15.9", "@types/jest": "^27.0.2", - "async": "0.2.10", "browser-resolve": "^2.0.0", "chai": "^4.3.4", "commander": "^7.2.0", @@ -20073,8 +20072,7 @@ "dev": true }, "async": { - "version": "0.2.10", - "resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz", + "version": "https://registry.npmjs.org/async/-/async-0.2.10.tgz", "integrity": "sha1-trvgsGdLnXGXCMo43owjfLUmw9E=", "dev": true }, diff --git a/package.json b/package.json index 691fd36..9028670 100755 --- a/package.json +++ b/package.json @@ -48,7 +48,6 @@ "@react-native-async-storage/async-storage": "^1.15.9", "@types/jest": "^27.0.2", "browser-resolve": "^2.0.0", - "async": "0.2.10", "chai": "^4.3.4", "commander": "^7.2.0", "events": "^3.3.0", diff --git a/test/browser/load.spec.js b/test/browser/load.spec.js index 4390a19..b2a6b39 100755 --- a/test/browser/load.spec.js +++ b/test/browser/load.spec.js @@ -1,5 +1,5 @@ /* eslint-env mocha, browser */ -/* global async, Nedb, localforage */ +/* global Nedb, localforage, testUtils */ const N = 5000 const db = new Nedb({ filename: 'loadTest', autoload: true }) @@ -9,7 +9,7 @@ const sample = JSON.stringify({ data: Math.random(), _id: Math.random() }) const someInserts = (sn, N, callback) => { const beg = Date.now() let i = 0 - async.whilst(() => i < N, _cb => { + testUtils.whilst(() => i < N, _cb => { db.insert({ data: Math.random() }, err => { i += 1; return _cb(err) }) }, err => { console.log('Inserts, series ' + sn + ' ' + (Date.now() - beg)) @@ -41,7 +41,7 @@ const someLSDiff = (sn, N, callback) => { function someLF (sn, N, callback) { const beg = Date.now() let i = 0 - async.whilst(() => i < N, _cb => { + testUtils.whilst(() => i < N, _cb => { localforage.getItem('loadTestLF', (err, value) => { if (err) return _cb(err) localforage.setItem('loadTestLF', value + sample, err => { i += 1; return _cb(err) }) @@ -56,7 +56,7 @@ function someLF (sn, N, callback) { const someLFDiff = (sn, N, callback) => { const beg = Date.now() let i = 0 - async.whilst(() => i < N, _cb => { + testUtils.whilst(() => i < N, _cb => { localforage.setItem('loadTestLF-' + i, sample, err => { i += 1; return _cb(err) }) }, err => { console.log('localForage/IDB, series ' + sn + ' ' + (Date.now() - beg)) @@ -72,54 +72,54 @@ describe.skip('Load tests', function () { db.remove({}, { multi: true }, err => done(err)) }) - it.skip('Inserts', function (done) { - async.waterfall([ + it('Inserts', function (done) { + testUtils.waterfall([ // Slow and gets slower with database size - async.apply(someInserts, '#1', N), // N=5000, 141s - async.apply(someInserts, '#2', N), // N=5000, 208s - async.apply(someInserts, '#3', N), // N=5000, 281s - async.apply(someInserts, '#4', N) // N=5000, 350s + testUtils.apply(someInserts, '#1', N), // N=5000, 141s + testUtils.apply(someInserts, '#2', N), // N=5000, 208s + testUtils.apply(someInserts, '#3', N), // N=5000, 281s + testUtils.apply(someInserts, '#4', N) // N=5000, 350s ], done) }) - it.skip('Localstorage', function (done) { - async.waterfall([ + it('Localstorage', function (done) { + testUtils.waterfall([ // Slow and gets slower really fast with database size, then outright crashes - async.apply(someLS, '#1', N), // N=4000, 2.5s - async.apply(someLS, '#2', N), // N=4000, 8.0s - async.apply(someLS, '#3', N), // N=4000, 26.5s - async.apply(someLS, '#4', N) // N=4000, 47.8s then crash, can't get string (with N=5000 crash happens on second pass) + testUtils.apply(someLS, '#1', N), // N=4000, 2.5s + testUtils.apply(someLS, '#2', N), // N=4000, 8.0s + testUtils.apply(someLS, '#3', N), // N=4000, 26.5s + testUtils.apply(someLS, '#4', N) // N=4000, 47.8s then crash, can't get string (with N=5000 crash happens on second pass) ], done) }) - it.skip('Localstorage Diff', function (done) { - async.waterfall([ + it('Localstorage Diff', function (done) { + testUtils.waterfall([ // Much faster and more consistent - async.apply(someLSDiff, '#1', N), // N=50000, 0.7s - async.apply(someLSDiff, '#2', N), // N=50000, 0.5s - async.apply(someLSDiff, '#3', N), // N=50000, 0.5s - async.apply(someLSDiff, '#4', N) // N=50000, 0.5s + testUtils.apply(someLSDiff, '#1', N), // N=50000, 0.7s + testUtils.apply(someLSDiff, '#2', N), // N=50000, 0.5s + testUtils.apply(someLSDiff, '#3', N), // N=50000, 0.5s + testUtils.apply(someLSDiff, '#4', N) // N=50000, 0.5s ], done) }) - it.skip('LocalForage', function (done) { - async.waterfall([ + it('LocalForage', function (done) { + testUtils.waterfall([ // Slow and gets slower with database size cb => { localforage.setItem('loadTestLF', '', err => cb(err)) }, - async.apply(someLF, '#1', N), // N=5000, 69s - async.apply(someLF, '#2', N), // N=5000, 108s - async.apply(someLF, '#3', N), // N=5000, 137s - async.apply(someLF, '#4', N) // N=5000, 169s + testUtils.apply(someLF, '#1', N), // N=5000, 69s + testUtils.apply(someLF, '#2', N), // N=5000, 108s + testUtils.apply(someLF, '#3', N), // N=5000, 137s + testUtils.apply(someLF, '#4', N) // N=5000, 169s ], done) }) - it.skip('LocalForage diff', function (done) { - async.waterfall([ + it('LocalForage diff', function (done) { + testUtils.waterfall([ // Quite fast and speed doesn't change with database size (tested with N=10000 and N=50000, still no slow-down) - async.apply(someLFDiff, '#1', N), // N=5000, 18s - async.apply(someLFDiff, '#2', N), // N=5000, 18s - async.apply(someLFDiff, '#3', N), // N=5000, 18s - async.apply(someLFDiff, '#4', N) // N=5000, 18s + testUtils.apply(someLFDiff, '#1', N), // N=5000, 18s + testUtils.apply(someLFDiff, '#2', N), // N=5000, 18s + testUtils.apply(someLFDiff, '#3', N), // N=5000, 18s + testUtils.apply(someLFDiff, '#4', N) // N=5000, 18s ], done) }) }) diff --git a/test/cursor.test.js b/test/cursor.test.js index 42d8566..fbce0c4 100755 --- a/test/cursor.test.js +++ b/test/cursor.test.js @@ -3,7 +3,7 @@ const chai = require('chai') const testDb = 'workspace/test.db' const fs = require('fs') const path = require('path') -const async = require('async') +const { each, waterfall } = require('./utils.test.js') const Datastore = require('../lib/datastore') const Persistence = require('../lib/persistence') const Cursor = require('../lib/cursor') @@ -19,7 +19,7 @@ describe('Cursor', function () { d.filename.should.equal(testDb) d.inMemoryOnly.should.equal(false) - async.waterfall([ + waterfall([ function (cb) { Persistence.ensureDirectoryExists(path.dirname(testDb), function () { fs.access(testDb, fs.constants.F_OK, function (err) { @@ -58,7 +58,7 @@ describe('Cursor', function () { }) it('Without query, an empty query or a simple query and no skip or limit', function (done) { - async.waterfall([ + waterfall([ function (cb) { const cursor = new Cursor(d) cursor.exec(function (err, docs) { @@ -100,7 +100,7 @@ describe('Cursor', function () { }) it('With an empty collection', function (done) { - async.waterfall([ + waterfall([ function (cb) { d.remove({}, { multi: true }, function (err) { return cb(err) }) }, @@ -222,7 +222,7 @@ describe('Cursor', function () { }) it('With an empty collection', function (done) { - async.waterfall([ + waterfall([ function (cb) { d.remove({}, { multi: true }, function (err) { return cb(err) }) }, @@ -240,7 +240,7 @@ describe('Cursor', function () { it('Ability to chain sorting and exec', function (done) { let i - async.waterfall([ + waterfall([ function (cb) { const cursor = new Cursor(d) cursor.sort({ age: 1 }).exec(function (err, docs) { @@ -267,7 +267,7 @@ describe('Cursor', function () { }) it('Using limit and sort', function (done) { - async.waterfall([ + waterfall([ function (cb) { const cursor = new Cursor(d) cursor.sort({ age: 1 }).limit(3).exec(function (err, docs) { @@ -293,7 +293,7 @@ describe('Cursor', function () { }) it('Using a limit higher than total number of docs shouldnt cause an error', function (done) { - async.waterfall([ + waterfall([ function (cb) { const cursor = new Cursor(d) cursor.sort({ age: 1 }).limit(7).exec(function (err, docs) { @@ -311,7 +311,7 @@ describe('Cursor', function () { }) it('Using limit and skip with sort', function (done) { - async.waterfall([ + waterfall([ function (cb) { const cursor = new Cursor(d) cursor.sort({ age: 1 }).limit(1).skip(2).exec(function (err, docs) { @@ -346,7 +346,7 @@ describe('Cursor', function () { }) it('Using too big a limit and a skip with sort', function (done) { - async.waterfall([ + waterfall([ function (cb) { const cursor = new Cursor(d) cursor.sort({ age: 1 }).limit(8).skip(2).exec(function (err, docs) { @@ -362,7 +362,7 @@ describe('Cursor', function () { }) it('Using too big a skip with sort should return no result', function (done) { - async.waterfall([ + waterfall([ function (cb) { const cursor = new Cursor(d) cursor.sort({ age: 1 }).skip(5).exec(function (err, docs) { @@ -399,7 +399,7 @@ describe('Cursor', function () { }) it('Sorting strings', function (done) { - async.waterfall([ + waterfall([ function (cb) { d.remove({}, { multi: true }, function (err) { if (err) { return cb(err) } @@ -443,7 +443,7 @@ describe('Cursor', function () { let doc2 let doc3 - async.waterfall([ + waterfall([ function (cb) { d.remove({}, { multi: true }, function (err) { if (err) { return cb(err) } @@ -489,7 +489,7 @@ describe('Cursor', function () { }) it('Sorting when some fields are undefined', function (done) { - async.waterfall([ + waterfall([ function (cb) { d.remove({}, { multi: true }, function (err) { if (err) { return cb(err) } @@ -537,7 +537,7 @@ describe('Cursor', function () { }) it('Sorting when all fields are undefined', function (done) { - async.waterfall([ + waterfall([ function (cb) { d.remove({}, { multi: true }, function (err) { if (err) { return cb(err) } @@ -571,7 +571,7 @@ describe('Cursor', function () { }) it('Multiple consecutive sorts', function (done) { - async.waterfall([ + waterfall([ function (cb) { d.remove({}, { multi: true }, function (err) { if (err) { return cb(err) } @@ -655,7 +655,7 @@ describe('Cursor', function () { const companies = ['acme', 'milkman', 'zoinks'] const entities = [] - async.waterfall([ + waterfall([ function (cb) { d.remove({}, { multi: true }, function (err) { if (err) { return cb(err) } @@ -672,7 +672,7 @@ describe('Cursor', function () { } } - async.each(entities, function (entity, callback) { + each(entities, function (entity, callback) { d.insert(entity, function () { callback() }) diff --git a/test/db.test.js b/test/db.test.js index aff8e50..a11fef2 100755 --- a/test/db.test.js +++ b/test/db.test.js @@ -3,7 +3,7 @@ const chai = require('chai') const testDb = 'workspace/test.db' const fs = require('fs') const path = require('path') -const async = require('async') +const { apply, each, waterfall } = require('./utils.test.js') const model = require('../lib/model') const Datastore = require('../lib/datastore') const Persistence = require('../lib/persistence') @@ -20,7 +20,7 @@ describe('Database', function () { d.filename.should.equal(testDb) d.inMemoryOnly.should.equal(false) - async.waterfall([ + waterfall([ function (cb) { Persistence.ensureDirectoryExists(path.dirname(testDb), function () { fs.access(testDb, fs.constants.FS_OK, function (err) { @@ -697,7 +697,7 @@ describe('Database', function () { describe('Find', function () { it('Can find all documents if an empty query is used', function (done) { - async.waterfall([ + waterfall([ function (cb) { // eslint-disable-next-line node/handle-callback-err d.insert({ somedata: 'ok' }, function (err) { @@ -722,7 +722,7 @@ describe('Database', function () { }) it('Can find all documents matching a basic query', function (done) { - async.waterfall([ + waterfall([ function (cb) { // eslint-disable-next-line node/handle-callback-err d.insert({ somedata: 'ok' }, function (err) { @@ -751,7 +751,7 @@ describe('Database', function () { }) it('Can find one document matching a basic query and return null if none is found', function (done) { - async.waterfall([ + waterfall([ function (cb) { // eslint-disable-next-line node/handle-callback-err d.insert({ somedata: 'ok' }, function (err) { @@ -1025,7 +1025,7 @@ describe('Database', function () { describe('Count', function () { it('Count all documents if an empty query is used', function (done) { - async.waterfall([ + waterfall([ function (cb) { // eslint-disable-next-line node/handle-callback-err d.insert({ somedata: 'ok' }, function (err) { @@ -1046,7 +1046,7 @@ describe('Database', function () { }) it('Count all documents matching a basic query', function (done) { - async.waterfall([ + waterfall([ function (cb) { // eslint-disable-next-line node/handle-callback-err d.insert({ somedata: 'ok' }, function (err) { @@ -1115,7 +1115,7 @@ describe('Database', function () { describe('Update', function () { it('If the query doesn\'t match anything, database is not modified', function (done) { - async.waterfall([ + waterfall([ function (cb) { // eslint-disable-next-line node/handle-callback-err d.insert({ somedata: 'ok' }, function (err) { @@ -1212,7 +1212,7 @@ describe('Database', function () { } // Actually launch the tests - async.waterfall([ + waterfall([ function (cb) { // eslint-disable-next-line node/handle-callback-err d.insert({ somedata: 'ok' }, function (err, doc1) { @@ -1234,11 +1234,11 @@ describe('Database', function () { return cb() }) }, - async.apply(testPostUpdateState), + apply(testPostUpdateState), function (cb) { d.loadDatabase(function (err) { cb(err) }) }, - async.apply(testPostUpdateState) + apply(testPostUpdateState) ], done) }) @@ -1274,7 +1274,7 @@ describe('Database', function () { } // Actually launch the test - async.waterfall([ + waterfall([ function (cb) { // eslint-disable-next-line node/handle-callback-err d.insert({ somedata: 'ok' }, function (err, doc1) { @@ -1296,11 +1296,11 @@ describe('Database', function () { return cb() }) }, - async.apply(testPostUpdateState), + apply(testPostUpdateState), function (cb) { d.loadDatabase(function (err) { return cb(err) }) }, - async.apply(testPostUpdateState) // The persisted state has been updated + apply(testPostUpdateState) // The persisted state has been updated ], done) }) @@ -1895,7 +1895,7 @@ describe('Database', function () { } // Actually launch the test - async.waterfall([ + waterfall([ function (cb) { // eslint-disable-next-line node/handle-callback-err d.insert({ somedata: 'ok' }, function (err, doc1) { @@ -1915,11 +1915,11 @@ describe('Database', function () { return cb() }) }, - async.apply(testPostUpdateState), + apply(testPostUpdateState), function (cb) { d.loadDatabase(function (err) { return cb(err) }) }, - async.apply(testPostUpdateState) + apply(testPostUpdateState) ], done) }) @@ -1934,7 +1934,7 @@ describe('Database', function () { // Remove two docs simultaneously const toRemove = ['Mars', 'Saturn'] - async.each(toRemove, function (planet, cb) { + each(toRemove, function (planet, cb) { d.remove({ planet: planet }, function (err) { return cb(err) }) // eslint-disable-next-line node/handle-callback-err }, function (err) { diff --git a/test/executor.test.js b/test/executor.test.js index fda65ff..a9d2de9 100755 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -3,7 +3,7 @@ const chai = require('chai') const testDb = 'workspace/test.db' const fs = require('fs') const path = require('path') -const async = require('async') +const { waterfall } = require('./utils.test.js') const Datastore = require('../lib/datastore') const Persistence = require('../lib/persistence') @@ -151,7 +151,7 @@ describe('Executor', function () { d.filename.should.equal(testDb) d.inMemoryOnly.should.equal(false) - async.waterfall([ + waterfall([ function (cb) { Persistence.ensureDirectoryExists(path.dirname(testDb), function () { fs.access(testDb, fs.constants.F_OK, function (err) { diff --git a/test/persistence.test.js b/test/persistence.test.js index 578707b..a19ae04 100755 --- a/test/persistence.test.js +++ b/test/persistence.test.js @@ -3,7 +3,7 @@ const chai = require('chai') const testDb = 'workspace/test.db' const fs = require('fs') const path = require('path') -const async = require('async') +const { apply, waterfall } = require('./utils.test.js') const model = require('../lib/model') const Datastore = require('../lib/datastore') const Persistence = require('../lib/persistence') @@ -22,7 +22,7 @@ describe('Persistence', function () { d.filename.should.equal(testDb) d.inMemoryOnly.should.equal(false) - async.waterfall([ + waterfall([ function (cb) { Persistence.ensureDirectoryExists(path.dirname(testDb), function () { fs.access(testDb, fs.constants.FS_OK, function (err) { @@ -911,9 +911,9 @@ describe('Persistence', function () { const dbFile = 'workspace/test2.db' let theDb, theDb2, doc1, doc2 - async.waterfall([ - async.apply(storage.ensureFileDoesntExist, dbFile), - async.apply(storage.ensureFileDoesntExist, dbFile + '~'), + waterfall([ + apply(storage.ensureFileDoesntExist, dbFile), + apply(storage.ensureFileDoesntExist, dbFile + '~'), function (cb) { theDb = new Datastore({ filename: dbFile }) theDb.loadDatabase(cb) diff --git a/test/utils.test.js b/test/utils.test.js new file mode 100644 index 0000000..2aa5f62 --- /dev/null +++ b/test/utils.test.js @@ -0,0 +1,33 @@ +const { callbackify, promisify } = require('util') + +const waterfallAsync = async tasks => { + for (const task of tasks) { + await promisify(task)() + } +} + +const waterfall = callbackify(waterfallAsync) + +const eachAsync = async (arr, iterator) => Promise.all(arr.map(el => promisify(iterator)(el))) + +const each = callbackify(eachAsync) + +const apply = function (fn) { + const args = Array.prototype.slice.call(arguments, 1) + return function () { + return fn.apply( + null, args.concat(Array.prototype.slice.call(arguments)) + ) + } +} + +const whilstAsync = async (test, fn) => { + while (test()) await promisify(fn)() +} + +const whilst = callbackify(whilstAsync) + +module.exports.whilst = whilst +module.exports.apply = apply +module.exports.waterfall = waterfall +module.exports.each = each diff --git a/test_lac/openFds.test.js b/test_lac/openFds.test.js index b93c06d..4512087 100644 --- a/test_lac/openFds.test.js +++ b/test_lac/openFds.test.js @@ -1,5 +1,5 @@ const fs = require('fs') -const async = require('async') +const { waterfall, whilst } = require('../test/utils.test.js') const Nedb = require('../lib/datastore') const db = new Nedb({ filename: './workspace/openfds.db', autoload: true }) const N = 64 @@ -7,7 +7,7 @@ let i let fds function multipleOpen (filename, N, callback) { - async.whilst(function () { return i < N } + whilst(function () { return i < N } , function (cb) { fs.open(filename, 'r', function (err, fd) { i += 1 @@ -18,7 +18,7 @@ function multipleOpen (filename, N, callback) { , callback) } -async.waterfall([ +waterfall([ // Check that ulimit has been set to the correct value function (cb) { i = 0 @@ -46,7 +46,7 @@ async.waterfall([ if (err) { console.log(err) } i = 0 - async.whilst(function () { return i < 2 * N + 1 } + whilst(function () { return i < 2 * N + 1 } , function (cb) { db.persistence.persistCachedDatabase(function (err) { if (err) { return cb(err) } @@ -61,4 +61,4 @@ async.waterfall([ }) }) } -]) +], () => {}) diff --git a/webpack.config.js b/webpack.config.js index 37a2cb1..6ba8737 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -37,11 +37,12 @@ module.exports = (env, argv) => { }) ], entry: { - Nedb: path.join(__dirname, 'lib', 'datastore.js') + Nedb: path.join(__dirname, 'lib', 'datastore.js'), + testUtils: path.join(__dirname, 'test', 'utils.test.js') }, output: { path: path.join(__dirname, 'browser-version/out'), - filename: minimize ? 'nedb.min.js' : 'nedb.js', + filename: `[name]${minimize ? '.min' : ''}.js`, libraryTarget: 'window', library: '[name]' }