From 268e59bb9084083d9e04a3496fb2609250e69dbb Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Mon, 22 Jan 2018 09:30:12 +0100 Subject: [PATCH 1/7] run test suite in TypeScript in addition to Node.js --- global.d.ts | 7 +++++++ package.json | 8 ++++++-- tsconfig.json | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 global.d.ts create mode 100644 tsconfig.json diff --git a/global.d.ts b/global.d.ts new file mode 100644 index 00000000..44cebfe2 --- /dev/null +++ b/global.d.ts @@ -0,0 +1,7 @@ +// This file is required for the use of process.browser, which is +// not defined in @types/node but a property added by browserify. +declare namespace NodeJS { + interface Process { + browser: boolean; + } +} diff --git a/package.json b/package.json index 31fc0e0c..c7776b27 100644 --- a/package.json +++ b/package.json @@ -35,16 +35,20 @@ "xtend": "~4.0.0" }, "devDependencies": { + "@types/node": "^9.3.0", "rimraf": "^2.6.1", "sinon": "^4.0.0", "standard": "^10.0.3", - "tape": "^4.7.0" + "tape": "^4.7.0", + "ts-node": "^4.1.0", + "typescript": "^2.6.2" }, "browser": { "rimraf": false }, "scripts": { - "test": "standard && node test.js" + "test": "standard && node test.js && npm run test-ts", + "test-ts": "ts-node --type-check --no-cache test.js" }, "license": "MIT", "engines": { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 00000000..4af83144 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "target": "es2015", + "moduleResolution": "node", + "checkJs": true, + "allowJs": true + } +} From b353fb45b00165222aa616111ddc8ae00e9a860a Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Mon, 22 Jan 2018 09:36:40 +0100 Subject: [PATCH 2/7] update TypeScript typings for v4 * remove approximateSize() and isLevelDOWN() * make Batch value optional * add missing db property to AbstractIterator --- index.d.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index b2caaec4..56fb04c9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,8 +19,6 @@ interface AbstractLevelDOWN): AbstractIterator; - approximateSize(start: K, end: K, cb: (err: any, size: number) => void): void; - [index: string]: any; } @@ -42,7 +40,7 @@ export interface AbstractIteratorOptions { values?: boolean; } -export type Batch = PutBatch | DelBatch +export type Batch = PutBatch | DelBatch export interface PutBatch { type: 'put', @@ -56,6 +54,7 @@ export interface DelBatch { } interface AbstractIterator { + db: any; next(callback: (err: any, key: K, value: V) => void): void; end(callback: (err: any) => void): void; } @@ -82,4 +81,3 @@ interface AbstractChainedBatchConstructor { export const AbstractLevelDOWN: AbstractLevelDOWNConstructor export const AbstractIterator: AbstractIteratorConstructor export const AbstractChainedBatch: AbstractChainedBatchConstructor -export function isLevelDOWN(db: any): boolean; From c2f400e374be8d115345604d08c6d99219dd9703 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Mon, 22 Jan 2018 09:39:15 +0100 Subject: [PATCH 3/7] remove obsolete parameters from tests --- test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test.js b/test.js index acc8893d..7b5fed36 100644 --- a/test.js +++ b/test.js @@ -6,15 +6,15 @@ var AbstractLevelDOWN = require('./').AbstractLevelDOWN var AbstractIterator = require('./').AbstractIterator var AbstractChainedBatch = require('./').AbstractChainedBatch -function factory (location, opts) { - return new AbstractLevelDOWN(location, opts) +function factory (location) { + return new AbstractLevelDOWN(location) } /** * Compatibility with basic LevelDOWN API */ -require('./abstract/leveldown-test').args(factory, test, testCommon) +require('./abstract/leveldown-test').args(factory, test) require('./abstract/open-test').args(factory, test, testCommon) @@ -30,7 +30,7 @@ require('./abstract/put-test').args(test) require('./abstract/put-get-del-test').setUp(factory, test, testCommon) require('./abstract/put-get-del-test').errorKeys(test) // require('./abstract/put-get-del-test').nonErrorKeys(test, testCommon) -require('./abstract/put-get-del-test').errorValues(test) +require('./abstract/put-get-del-test').errorValues() require('./abstract/put-get-del-test').tearDown(test, testCommon) require('./abstract/batch-test').setUp(factory, test, testCommon) From 38b37061fa055c1261ad76a0eb37b080b804b621 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Mon, 22 Jan 2018 10:02:51 +0100 Subject: [PATCH 4/7] use ES6 classes in tests to please TypeScript Otherwise TypeScript doesn't see that Test inherits AbstractLevelDOWN, if defined like this: ``` function Test (location) { AbstractLevelDOWN.call(this, location) } util.inherits(Test, AbstractLevelDOWN) ``` And will complain about AbstractLevelDOWN methods being undefined on Test. --- abstract/put-get-del-test.js | 2 + test.js | 334 ++++++++++------------------------- 2 files changed, 100 insertions(+), 236 deletions(-) diff --git a/abstract/put-get-del-test.js b/abstract/put-get-del-test.js index 29ccba42..a740af6f 100644 --- a/abstract/put-get-del-test.js +++ b/abstract/put-get-del-test.js @@ -1,3 +1,5 @@ +'use strict' + var db const verifyNotFoundError = require('./util').verifyNotFoundError const testBuffer = Buffer.from('testbuffer') diff --git a/test.js b/test.js index 7b5fed36..f6ae826a 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,7 @@ +'use strict' + var test = require('tape') var sinon = require('sinon') -var util = require('util') var testCommon = require('./testCommon') var AbstractLevelDOWN = require('./').AbstractLevelDOWN var AbstractIterator = require('./').AbstractIterator @@ -50,23 +51,14 @@ require('./abstract/iterator-test').sequence(test) */ test('test core extensibility', function (t) { - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - + class Test extends AbstractLevelDOWN {} var test = new Test('foobar') t.equal(test.location, 'foobar', 'location set on instance') t.end() }) test('test key/value serialization', function (t) { - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) + class Test extends AbstractLevelDOWN {} var buffer = Buffer.alloc(0) var test = new Test('foobar') @@ -97,12 +89,7 @@ test('test open() extensibility', function (t) { var expectedOptions = { createIfMissing: true, errorIfExists: false } var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - + class Test extends AbstractLevelDOWN {} Test.prototype._open = spy test = new Test('foobar') @@ -129,12 +116,7 @@ test('test close() extensibility', function (t) { var expectedCb = function () {} var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - + class Test extends AbstractLevelDOWN {} Test.prototype._close = spy test = new Test('foobar') @@ -153,12 +135,7 @@ test('test get() extensibility', function (t) { var expectedKey = 'a key' var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - + class Test extends AbstractLevelDOWN {} Test.prototype._get = spy test = new Test('foobar') @@ -191,12 +168,7 @@ test('test del() extensibility', function (t) { var expectedKey = 'a key' var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - + class Test extends AbstractLevelDOWN {} Test.prototype._del = spy test = new Test('foobar') @@ -228,12 +200,7 @@ test('test put() extensibility', function (t) { var expectedValue = 'a value' var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - + class Test extends AbstractLevelDOWN {} Test.prototype._put = spy test = new Test('foobar') @@ -269,12 +236,7 @@ test('test batch() extensibility', function (t) { ] var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - + class Test extends AbstractLevelDOWN {} Test.prototype._batch = spy test = new Test('foobar') @@ -314,12 +276,7 @@ test('test chained batch() (array) extensibility', function (t) { var expectedOptions = { options: 1 } var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - + class Test extends AbstractLevelDOWN {} Test.prototype._batch = spy test = new Test('foobar') @@ -353,12 +310,7 @@ test('test chained batch() (custom _chainedBatch) extensibility', function (t) { var spy = sinon.spy() var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - + class Test extends AbstractLevelDOWN {} Test.prototype._chainedBatch = spy test = new Test('foobar') @@ -377,11 +329,7 @@ test('test chained batch() (custom _chainedBatch) extensibility', function (t) { }) test('test AbstractChainedBatch extensibility', function (t) { - function Test (db) { - AbstractChainedBatch.call(this, db) - } - - util.inherits(Test, AbstractChainedBatch) + class Test extends AbstractChainedBatch {} var test = new Test('foobar') t.equal(test._db, 'foobar', 'db set on instance') @@ -393,11 +341,7 @@ test('test write() extensibility', function (t) { var spycb = sinon.spy() var test - function Test (db) { - AbstractChainedBatch.call(this, db) - } - - util.inherits(Test, AbstractChainedBatch) + class Test extends AbstractChainedBatch {} Test.prototype._write = spy @@ -422,11 +366,7 @@ test('test put() extensibility', function (t) { var returnValue var test - function Test (db) { - AbstractChainedBatch.call(this, db) - } - - util.inherits(Test, AbstractChainedBatch) + class Test extends AbstractChainedBatch {} Test.prototype._put = spy @@ -447,12 +387,7 @@ test('test del() extensibility', function (t) { var returnValue var test - function Test (db) { - AbstractChainedBatch.call(this, db) - } - - util.inherits(Test, AbstractChainedBatch) - + class Test extends AbstractChainedBatch {} Test.prototype._del = spy test = new Test(factory('foobar')) @@ -470,12 +405,7 @@ test('test clear() extensibility', function (t) { var returnValue var test - function Test (db) { - AbstractChainedBatch.call(this, db) - } - - util.inherits(Test, AbstractChainedBatch) - + class Test extends AbstractChainedBatch {} Test.prototype._clear = spy test = new Test(factory('foobar')) @@ -500,12 +430,7 @@ test('test iterator() extensibility', function (t) { } var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - + class Test extends AbstractLevelDOWN {} Test.prototype._iterator = spy test = new Test('foobar') @@ -519,12 +444,7 @@ test('test iterator() extensibility', function (t) { }) test('test AbstractIterator extensibility', function (t) { - function Test (db) { - AbstractIterator.call(this, db) - } - - util.inherits(Test, AbstractIterator) - + class Test extends AbstractIterator {} var test = new Test('foobar') t.equal(test.db, 'foobar', 'db set on instance') t.end() @@ -535,12 +455,7 @@ test('test next() extensibility', function (t) { var spycb = sinon.spy() var test - function Test (db) { - AbstractIterator.call(this, db) - } - - util.inherits(Test, AbstractIterator) - + class Test extends AbstractIterator {} Test.prototype._next = spy test = new Test('foobar') @@ -562,12 +477,7 @@ test('test end() extensibility', function (t) { var expectedCb = function () {} var test - function Test (db) { - AbstractIterator.call(this, db) - } - - util.inherits(Test, AbstractIterator) - + class Test extends AbstractIterator {} Test.prototype._end = spy test = new Test('foobar') @@ -586,20 +496,16 @@ test('test serialization extensibility (put)', function (t) { var spy = sinon.spy() var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._serializeKey = function (key) { - t.equal(key, 'no') - return 'foo' - } + class Test extends AbstractLevelDOWN { + _serializeKey (key) { + t.equal(key, 'no') + return 'foo' + } - Test.prototype._serializeValue = function (value) { - t.equal(value, 'nope') - return 'bar' + _serializeValue (value) { + t.equal(value, 'nope') + return 'bar' + } } Test.prototype._put = spy @@ -618,19 +524,15 @@ test('test serialization extensibility (del)', function (t) { var spy = sinon.spy() var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._serializeKey = function (key) { - t.equal(key, 'no') - return 'foo' - } + class Test extends AbstractLevelDOWN { + _serializeKey (key) { + t.equal(key, 'no') + return 'foo' + } - Test.prototype._serializeValue = function (value) { - t.fail('should not be called') + _serializeValue (value) { + t.fail('should not be called') + } } Test.prototype._del = spy @@ -650,20 +552,16 @@ test('test serialization extensibility (batch array put)', function (t) { var spy = sinon.spy() var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._serializeKey = function (key) { - t.equal(key, 'no') - return 'foo' - } + class Test extends AbstractLevelDOWN { + _serializeKey (key) { + t.equal(key, 'no') + return 'foo' + } - Test.prototype._serializeValue = function (value) { - t.equal(value, 'nope') - return 'bar' + _serializeValue (value) { + t.equal(value, 'nope') + return 'bar' + } } Test.prototype._batch = spy @@ -682,20 +580,16 @@ test('test serialization extensibility (batch chain put)', function (t) { var spy = sinon.spy() var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._serializeKey = function (key) { - t.equal(key, 'no') - return 'foo' - } + class Test extends AbstractLevelDOWN { + _serializeKey (key) { + t.equal(key, 'no') + return 'foo' + } - Test.prototype._serializeValue = function (value) { - t.equal(value, 'nope') - return 'bar' + _serializeValue (value) { + t.equal(value, 'nope') + return 'bar' + } } Test.prototype._batch = spy @@ -714,19 +608,15 @@ test('test serialization extensibility (batch array del)', function (t) { var spy = sinon.spy() var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._serializeKey = function (key) { - t.equal(key, 'no') - return 'foo' - } + class Test extends AbstractLevelDOWN { + _serializeKey (key) { + t.equal(key, 'no') + return 'foo' + } - Test.prototype._serializeValue = function (value) { - t.fail('should not be called') + _serializeValue (value) { + t.fail('should not be called') + } } Test.prototype._batch = spy @@ -744,19 +634,15 @@ test('test serialization extensibility (batch chain del)', function (t) { var spy = sinon.spy() var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._serializeKey = function (key) { - t.equal(key, 'no') - return 'foo' - } + class Test extends AbstractLevelDOWN { + _serializeKey (key) { + t.equal(key, 'no') + return 'foo' + } - Test.prototype._serializeValue = function (value) { - t.fail('should not be called') + _serializeValue (value) { + t.fail('should not be called') + } } Test.prototype._batch = spy @@ -774,20 +660,16 @@ test('test serialization extensibility (batch array is not mutated)', function ( var spy = sinon.spy() var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._serializeKey = function (key) { - t.equal(key, 'no') - return 'foo' - } + class Test extends AbstractLevelDOWN { + _serializeKey (key) { + t.equal(key, 'no') + return 'foo' + } - Test.prototype._serializeValue = function (value) { - t.equal(value, 'nope') - return 'bar' + _serializeValue (value) { + t.equal(value, 'nope') + return 'bar' + } } Test.prototype._batch = spy @@ -809,11 +691,7 @@ test('.status', function (t) { t.test('empty prototype', function (t) { var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) + class Test extends AbstractLevelDOWN {} test = new Test('foobar') t.equal(test.status, 'new') @@ -833,14 +711,10 @@ test('.status', function (t) { t.test('open error', function (t) { var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._open = function (options, cb) { - cb(new Error()) + class Test extends AbstractLevelDOWN { + _open (options, cb) { + cb(new Error()) + } } test = new Test('foobar') @@ -854,14 +728,10 @@ test('.status', function (t) { t.test('close error', function (t) { var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._close = function (cb) { - cb(new Error()) + class Test extends AbstractLevelDOWN { + _close (cb) { + cb(new Error()) + } } test = new Test('foobar') @@ -877,14 +747,10 @@ test('.status', function (t) { t.test('open', function (t) { var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._open = function (options, cb) { - process.nextTick(cb) + class Test extends AbstractLevelDOWN { + _open (options, cb) { + process.nextTick(cb) + } } test = new Test('foobar') @@ -899,14 +765,10 @@ test('.status', function (t) { t.test('close', function (t) { var test - function Test (location) { - AbstractLevelDOWN.call(this, location) - } - - util.inherits(Test, AbstractLevelDOWN) - - Test.prototype._close = function (cb) { - process.nextTick(cb) + class Test extends AbstractLevelDOWN { + _close (cb) { + process.nextTick(cb) + } } test = new Test('foobar') From b1ed564881f39ee12bb6d69570d17c610fd455d1 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Mon, 22 Jan 2018 10:53:57 +0100 Subject: [PATCH 5/7] define default methods on prototype to please TypeScript --- abstract-chained-batch.js | 28 ++++++------- abstract-iterator.js | 20 +++++----- abstract-leveldown.js | 84 +++++++++++++++++++-------------------- 3 files changed, 64 insertions(+), 68 deletions(-) diff --git a/abstract-chained-batch.js b/abstract-chained-batch.js index e4004b4d..8a0ee9c4 100644 --- a/abstract-chained-batch.js +++ b/abstract-chained-batch.js @@ -29,15 +29,15 @@ AbstractChainedBatch.prototype.put = function (key, value) { key = this._serializeKey(key) value = this._serializeValue(value) - if (typeof this._put === 'function') { - this._put(key, value) - } else { - this._operations.push({ type: 'put', key: key, value: value }) - } + this._put(key, value) return this } +AbstractChainedBatch.prototype._put = function (key, value) { + this._operations.push({ type: 'put', key: key, value: value }) +} + AbstractChainedBatch.prototype.del = function (key) { this._checkWritten() @@ -45,26 +45,25 @@ AbstractChainedBatch.prototype.del = function (key) { if (err) { throw err } key = this._serializeKey(key) - - if (typeof this._del === 'function') { - this._del(key) - } else { - this._operations.push({ type: 'del', key: key }) - } + this._del(key) return this } +AbstractChainedBatch.prototype._del = function (key) { + this._operations.push({ type: 'del', key: key }) +} + AbstractChainedBatch.prototype.clear = function () { this._checkWritten() - this._operations = [] - - if (typeof this._clear === 'function') { this._clear() } + this._clear() return this } +AbstractChainedBatch.prototype._clear = function noop () {} + AbstractChainedBatch.prototype.write = function (options, callback) { this._checkWritten() @@ -76,6 +75,7 @@ AbstractChainedBatch.prototype.write = function (options, callback) { this._written = true + // @ts-ignore if (typeof this._write === 'function') { return this._write(callback) } if (typeof this._db._batch === 'function') { diff --git a/abstract-iterator.js b/abstract-iterator.js index 4c6b4860..c967257f 100644 --- a/abstract-iterator.js +++ b/abstract-iterator.js @@ -22,19 +22,16 @@ AbstractIterator.prototype.next = function (callback) { } self._nexting = true - if (typeof self._next === 'function') { - return self._next(function () { - self._nexting = false - callback.apply(null, arguments) - }) - } - - process.nextTick(function () { + self._next(function () { self._nexting = false - callback() + callback.apply(null, arguments) }) } +AbstractIterator.prototype._next = function (callback) { + process.nextTick(callback) +} + AbstractIterator.prototype.end = function (callback) { if (typeof callback !== 'function') { throw new Error('end() requires a callback argument') @@ -45,9 +42,10 @@ AbstractIterator.prototype.end = function (callback) { } this._ended = true + this._end(callback) +} - if (typeof this._end === 'function') { return this._end(callback) } - +AbstractIterator.prototype._end = function (callback) { process.nextTick(callback) } diff --git a/abstract-leveldown.js b/abstract-leveldown.js index 4637478e..ea05e9c7 100644 --- a/abstract-leveldown.js +++ b/abstract-leveldown.js @@ -34,20 +34,19 @@ AbstractLevelDOWN.prototype.open = function (options, callback) { options.createIfMissing = options.createIfMissing !== false options.errorIfExists = !!options.errorIfExists - if (typeof this._open === 'function') { - this.status = 'opening' - this._open(options, function (err) { - if (err) { - self.status = oldStatus - return callback(err) - } - self.status = 'open' - callback() - }) - } else { - this.status = 'open' - process.nextTick(callback) - } + this.status = 'opening' + this._open(options, function (err) { + if (err) { + self.status = oldStatus + return callback(err) + } + self.status = 'open' + callback() + }) +} + +AbstractLevelDOWN.prototype._open = function (options, callback) { + process.nextTick(callback) } AbstractLevelDOWN.prototype.close = function (callback) { @@ -58,20 +57,19 @@ AbstractLevelDOWN.prototype.close = function (callback) { throw new Error('close() requires a callback argument') } - if (typeof this._close === 'function') { - this.status = 'closing' - this._close(function (err) { - if (err) { - self.status = oldStatus - return callback(err) - } - self.status = 'closed' - callback() - }) - } else { - this.status = 'closed' - process.nextTick(callback) - } + this.status = 'closing' + this._close(function (err) { + if (err) { + self.status = oldStatus + return callback(err) + } + self.status = 'closed' + callback() + }) +} + +AbstractLevelDOWN.prototype._close = function (callback) { + process.nextTick(callback) } AbstractLevelDOWN.prototype.get = function (key, options, callback) { @@ -90,10 +88,10 @@ AbstractLevelDOWN.prototype.get = function (key, options, callback) { options.asBuffer = options.asBuffer !== false - if (typeof this._get === 'function') { - return this._get(key, options, callback) - } + this._get(key, options, callback) +} +AbstractLevelDOWN.prototype._get = function (key, options, callback) { process.nextTick(function () { callback(new Error('NotFound')) }) } @@ -112,10 +110,10 @@ AbstractLevelDOWN.prototype.put = function (key, value, options, callback) { if (typeof options !== 'object') { options = {} } - if (typeof this._put === 'function') { - return this._put(key, value, options, callback) - } + this._put(key, value, options, callback) +} +AbstractLevelDOWN.prototype._put = function (key, value, options, callback) { process.nextTick(callback) } @@ -133,10 +131,10 @@ AbstractLevelDOWN.prototype.del = function (key, options, callback) { if (typeof options !== 'object') { options = {} } - if (typeof this._del === 'function') { - return this._del(key, options, callback) - } + this._del(key, options, callback) +} +AbstractLevelDOWN.prototype._del = function (key, options, callback) { process.nextTick(callback) } @@ -180,10 +178,10 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) { serialized[i] = e } - if (typeof this._batch === 'function') { - return this._batch(serialized, options, callback) - } + this._batch(serialized, options, callback) +} +AbstractLevelDOWN.prototype._batch = function (array, options, callback) { process.nextTick(callback) } @@ -227,11 +225,11 @@ function isEmptyBuffer (v) { AbstractLevelDOWN.prototype.iterator = function (options) { if (typeof options !== 'object') { options = {} } - options = this._setupIteratorOptions(options) + return this._iterator(options) +} - if (typeof this._iterator === 'function') { return this._iterator(options) } - +AbstractLevelDOWN.prototype._iterator = function (options) { return new AbstractIterator(this) } From 5eb28af94510333a292978028c41d0c29012b969 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sun, 28 Jan 2018 21:24:00 +0100 Subject: [PATCH 6/7] add TypeScript smoke test --- package.json | 4 +++- test-types.ts | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test-types.ts diff --git a/package.json b/package.json index c7776b27..58ac1757 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,9 @@ }, "scripts": { "test": "standard && node test.js && npm run test-ts", - "test-ts": "ts-node --type-check --no-cache test.js" + "test-ts": "npm run test-ts-types && npm run test-ts-suite", + "test-ts-types": "ts-node --type-check --no-cache test-types.ts", + "test-ts-suite": "ts-node --type-check --no-cache test.js" }, "license": "MIT", "engines": { diff --git a/test-types.ts b/test-types.ts new file mode 100644 index 00000000..80b3cfe2 --- /dev/null +++ b/test-types.ts @@ -0,0 +1,21 @@ +import { AbstractLevelDOWN } from '.' + +const buf = Buffer.from('foo') + +// Key and value generics +new AbstractLevelDOWN('loc').put('key', 'value', (err: Error) => {}) +new AbstractLevelDOWN('loc').put(buf, buf, (err: Error) => {}) +new AbstractLevelDOWN('loc').put(1, {}, (err: Error) => {}) + +new AbstractLevelDOWN('loc').get('key', (err: Error, value: string) => {}) +new AbstractLevelDOWN('loc').get(buf, (err: Error, value: Buffer) => {}) +new AbstractLevelDOWN('loc').get(1, (err: Error, value: object) => {}) + +// The generics default to "any" +new AbstractLevelDOWN('loc').put(1, {}, (err: Error) => {}) +new AbstractLevelDOWN('loc').put('key', 'value', (err: Error) => {}) +new AbstractLevelDOWN('loc').put(buf, false, (err: Error) => {}) + +new AbstractLevelDOWN('loc').get(1, (err: Error, value: any) => {}) +new AbstractLevelDOWN('loc').get('key', (err: Error, value: any) => {}) +new AbstractLevelDOWN('loc').get(buf, (err: Error, value: any) => {}) From c68595fcab9172029fc84ae07918cc48b430ac31 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sun, 28 Jan 2018 21:31:29 +0100 Subject: [PATCH 7/7] add TypeScript readme section with stability badge --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index fce0e009..8742f115 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,12 @@ Provided with the current instance of `AbstractLevelDOWN` by default. ### AbstractChainedBatch#_serializeKey(key) ### AbstractChainedBatch#_serializeValue(value) +## TypeScript + +[![experimental](https://img.shields.io/badge/stability-experimental-orange.svg)](https://nodejs.org/api/documentation.html#documentation_stability_index) + +Ships with TypeScript definitions that enable automatic inference of options and key-value types on a typed `abstract-leveldown` implementation. + ## Contributing