From 5bec3c0feeb8d6bc365546b674738b6d1bd47f0c Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sun, 4 Apr 2021 19:30:01 +0200 Subject: [PATCH 1/6] Add airtap for local browser tests --- .airtap.yml | 7 +++++++ package.json | 3 +++ 2 files changed, 10 insertions(+) create mode 100644 .airtap.yml diff --git a/.airtap.yml b/.airtap.yml new file mode 100644 index 0000000..225fb41 --- /dev/null +++ b/.airtap.yml @@ -0,0 +1,7 @@ +providers: + - airtap-playwright + +browsers: + - name: chromium + - name: firefox + - name: webkit diff --git a/package.json b/package.json index 099c9d3..569f013 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "main": "index.js", "scripts": { "test": "standard && hallmark && nyc tape test/*.js", + "test-browsers-local": "airtap --coverage --verbose test/*.js", "coverage": "nyc report --reporter=text-lcov | coveralls", "hallmark": "hallmark --fix", "dependency-check": "dependency-check . test/*.js", @@ -15,6 +16,8 @@ "buffer": "^5.6.0" }, "devDependencies": { + "airtap": "^4.0.3", + "airtap-playwright": "^1.0.1", "coveralls": "^3.0.2", "dependency-check": "^3.3.0", "hallmark": "^3.1.0", From 18b5ca4e3f62a88e58cfead477bbad5d218b04ac Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sun, 4 Apr 2021 19:39:31 +0200 Subject: [PATCH 2/6] Breaking: modernize syntax & bump standard to 16.x Drops support of old browsers due to use of const, let and arrow functions. --- index.js | 43 ++++++++++++++++++++----------------------- lib/encodings.js | 6 ++++-- package.json | 2 +- test/as-buffer.js | 8 ++++---- test/batch.js | 20 ++++++++++---------- test/codec.js | 8 ++++---- test/decoder.js | 20 ++++++++++---------- test/kv.js | 28 ++++++++++++++-------------- test/ltgt.js | 10 +++++----- 9 files changed, 72 insertions(+), 73 deletions(-) diff --git a/index.js b/index.js index d3e8659..f2814fb 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,6 @@ -var encodings = require('./lib/encodings') +'use strict' + +const encodings = require('./lib/encodings') module.exports = Codec @@ -45,53 +47,48 @@ Codec.prototype.decodeValue = function (value, opts) { } Codec.prototype.encodeBatch = function (ops, opts) { - var self = this - - return ops.map(function (_op) { - var op = { + return ops.map((_op) => { + const op = { type: _op.type, - key: self.encodeKey(_op.key, opts, _op) + key: this.encodeKey(_op.key, opts, _op) } - if (self.keyAsBuffer(opts, _op)) op.keyEncoding = 'binary' + if (this.keyAsBuffer(opts, _op)) op.keyEncoding = 'binary' if (_op.prefix) op.prefix = _op.prefix if ('value' in _op) { - op.value = self.encodeValue(_op.value, opts, _op) - if (self.valueAsBuffer(opts, _op)) op.valueEncoding = 'binary' + op.value = this.encodeValue(_op.value, opts, _op) + if (this.valueAsBuffer(opts, _op)) op.valueEncoding = 'binary' } return op }) } -var ltgtKeys = ['lt', 'gt', 'lte', 'gte', 'start', 'end'] +const ltgtKeys = ['lt', 'gt', 'lte', 'gte', 'start', 'end'] Codec.prototype.encodeLtgt = function (ltgt) { - var self = this - var ret = {} - Object.keys(ltgt).forEach(function (key) { + const ret = {} + Object.keys(ltgt).forEach((key) => { ret[key] = ltgtKeys.indexOf(key) > -1 - ? self.encodeKey(ltgt[key], ltgt) + ? this.encodeKey(ltgt[key], ltgt) : ltgt[key] }) return ret } Codec.prototype.createStreamDecoder = function (opts) { - var self = this - if (opts.keys && opts.values) { - return function (key, value) { + return (key, value) => { return { - key: self.decodeKey(key, opts), - value: self.decodeValue(value, opts) + key: this.decodeKey(key, opts), + value: this.decodeValue(value, opts) } } } else if (opts.keys) { - return function (key) { - return self.decodeKey(key, opts) + return (key) => { + return this.decodeKey(key, opts) } } else if (opts.values) { - return function (_, value) { - return self.decodeValue(value, opts) + return (_, value) => { + return this.decodeValue(value, opts) } } else { return function () {} diff --git a/lib/encodings.js b/lib/encodings.js index e1e8359..ec1f6d5 100644 --- a/lib/encodings.js +++ b/lib/encodings.js @@ -1,4 +1,6 @@ -var Buffer = require('buffer').Buffer +'use strict' + +const Buffer = require('buffer').Buffer exports.utf8 = exports['utf-8'] = { encode: function (data) { @@ -34,7 +36,7 @@ exports.none = { exports.id = exports.none -var bufferEncodings = [ +const bufferEncodings = [ 'hex', 'ascii', 'base64', diff --git a/package.json b/package.json index 569f013..0a63b74 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "hallmark": "^3.1.0", "level-community": "^3.0.0", "nyc": "^14.0.0", - "standard": "^14.0.0", + "standard": "^16.0.3", "tape": "^5.0.1" }, "hallmark": { diff --git a/test/as-buffer.js b/test/as-buffer.js index 5496fa1..1c281a4 100644 --- a/test/as-buffer.js +++ b/test/as-buffer.js @@ -1,8 +1,8 @@ -var test = require('tape') -var Codec = require('..') +const test = require('tape') +const Codec = require('..') test('key as buffer', function (t) { - var codec = new Codec({ keyEncoding: 'hex' }) + const codec = new Codec({ keyEncoding: 'hex' }) t.ok(codec.keyAsBuffer({})) t.ok(codec.keyAsBuffer()) t.notOk(codec.keyAsBuffer({ keyEncoding: 'utf8' })) @@ -10,7 +10,7 @@ test('key as buffer', function (t) { }) test('value as buffer', function (t) { - var codec = new Codec({ valueEncoding: 'hex' }) + const codec = new Codec({ valueEncoding: 'hex' }) t.ok(codec.valueAsBuffer({})) t.ok(codec.valueAsBuffer()) t.notOk(codec.valueAsBuffer({ valueEncoding: 'utf8' })) diff --git a/test/batch.js b/test/batch.js index 2379a04..318bf62 100644 --- a/test/batch.js +++ b/test/batch.js @@ -1,15 +1,15 @@ -var test = require('tape') -var Codec = require('..') +const test = require('tape') +const Codec = require('..') test('batch', function (t) { - var codec = new Codec({}) - var ops = [ + const codec = new Codec({}) + const ops = [ { type: 'put', key: 'string', value: 'string', valueEncoding: 'utf8' }, { type: 'put', key: 'json', value: {} } ] - var opsSerialized = JSON.stringify(ops) + const opsSerialized = JSON.stringify(ops) - var encoded = codec.encodeBatch(ops, { valueEncoding: 'json' }) + let encoded = codec.encodeBatch(ops, { valueEncoding: 'json' }) t.equal(opsSerialized, JSON.stringify(ops), 'ops not changed') @@ -28,14 +28,14 @@ test('batch', function (t) { }) test('batch - legacy', function (t) { - var codec = new Codec({}) - var ops = [ + const codec = new Codec({}) + const ops = [ { type: 'put', key: 'string', value: 'string', encoding: 'utf8' }, { type: 'put', key: 'json', value: {} } ] - var opsSerialized = JSON.stringify(ops) + const opsSerialized = JSON.stringify(ops) - var encoded = codec.encodeBatch(ops, { encoding: 'json' }) + let encoded = codec.encodeBatch(ops, { encoding: 'json' }) t.equal(opsSerialized, JSON.stringify(ops), 'ops not changed') diff --git a/test/codec.js b/test/codec.js index b8d994c..ad2e878 100644 --- a/test/codec.js +++ b/test/codec.js @@ -1,8 +1,8 @@ -var test = require('tape') -var Codec = require('..') +const test = require('tape') +const Codec = require('..') test('codec', function (t) { - var codec = new Codec({ keyEncoding: 'hex' }) + let codec = new Codec({ keyEncoding: 'hex' }) t.ok(codec.keyAsBuffer()) codec = new Codec() t.notOk(codec.keyAsBuffer()) @@ -10,7 +10,7 @@ test('codec', function (t) { }) test('codec, new not needed', function (t) { - var codec = Codec({ keyEncoding: 'hex' }) + let codec = Codec({ keyEncoding: 'hex' }) t.ok(codec.keyAsBuffer()) codec = Codec() t.notOk(codec.keyAsBuffer()) diff --git a/test/decoder.js b/test/decoder.js index fd024c2..2a55c44 100644 --- a/test/decoder.js +++ b/test/decoder.js @@ -1,13 +1,13 @@ -var test = require('tape') -var Codec = require('..') +const test = require('tape') +const Codec = require('..') test('createStreamDecoder', function (t) { - var codec = new Codec({ keyEncoding: 'hex' }) + const codec = new Codec({ keyEncoding: 'hex' }) t.plan(3) t.test('keys and values', function (t) { - var decoder = codec.createStreamDecoder({ + const decoder = codec.createStreamDecoder({ valueEncoding: 'json', keys: true, values: true @@ -20,7 +20,7 @@ test('createStreamDecoder', function (t) { }) t.test('keys', function (t) { - var decoder = codec.createStreamDecoder({ + const decoder = codec.createStreamDecoder({ keys: true }) t.equal(decoder(Buffer.from('hey')), '686579') @@ -28,7 +28,7 @@ test('createStreamDecoder', function (t) { }) t.test('values', function (t) { - var decoder = codec.createStreamDecoder({ + const decoder = codec.createStreamDecoder({ valueEncoding: 'hex', values: true }) @@ -38,12 +38,12 @@ test('createStreamDecoder', function (t) { }) test('createStreamDecoder - legacy', function (t) { - var codec = new Codec({ keyEncoding: 'hex' }) + const codec = new Codec({ keyEncoding: 'hex' }) t.plan(3) t.test('keys and values', function (t) { - var decoder = codec.createStreamDecoder({ + const decoder = codec.createStreamDecoder({ encoding: 'json', keys: true, values: true @@ -56,7 +56,7 @@ test('createStreamDecoder - legacy', function (t) { }) t.test('keys', function (t) { - var decoder = codec.createStreamDecoder({ + const decoder = codec.createStreamDecoder({ keys: true }) t.equal(decoder(Buffer.from('hey')), '686579') @@ -64,7 +64,7 @@ test('createStreamDecoder - legacy', function (t) { }) t.test('values', function (t) { - var decoder = codec.createStreamDecoder({ + const decoder = codec.createStreamDecoder({ encoding: 'hex', values: true }) diff --git a/test/kv.js b/test/kv.js index 2aa5898..3d2deef 100644 --- a/test/kv.js +++ b/test/kv.js @@ -1,10 +1,10 @@ -var test = require('tape') -var Codec = require('..') +const test = require('tape') +const Codec = require('..') test('encode key', function (t) { - var codec = new Codec({ keyEncoding: 'hex' }) + const codec = new Codec({ keyEncoding: 'hex' }) - var buf = codec.encodeKey('686579', {}) + let buf = codec.encodeKey('686579', {}) t.equal(buf.toString(), 'hey') buf = codec.encodeKey('686579') @@ -24,9 +24,9 @@ test('encode key', function (t) { }) test('encode value', function (t) { - var codec = new Codec({ valueEncoding: 'hex' }) + const codec = new Codec({ valueEncoding: 'hex' }) - var buf = codec.encodeValue('686579', {}) + let buf = codec.encodeValue('686579', {}) t.equal(buf.toString(), 'hey') buf = codec.encodeValue('686579') @@ -41,9 +41,9 @@ test('encode value', function (t) { }) test('decode key', function (t) { - var codec = new Codec({ keyEncoding: 'hex' }) + const codec = new Codec({ keyEncoding: 'hex' }) - var buf = codec.decodeKey(Buffer.from('hey'), {}) + let buf = codec.decodeKey(Buffer.from('hey'), {}) t.equal(buf, '686579') buf = codec.decodeKey(Buffer.from('hey')) @@ -58,9 +58,9 @@ test('decode key', function (t) { }) test('decode value', function (t) { - var codec = new Codec({ valueEncoding: 'hex' }) + const codec = new Codec({ valueEncoding: 'hex' }) - var buf = codec.decodeValue(Buffer.from('hey'), {}) + let buf = codec.decodeValue(Buffer.from('hey'), {}) t.equal(buf, '686579') buf = codec.decodeValue(Buffer.from('hey')) @@ -75,9 +75,9 @@ test('decode value', function (t) { }) test('encode value - legacy', function (t) { - var codec = new Codec({ encoding: 'hex' }) + const codec = new Codec({ encoding: 'hex' }) - var buf = codec.encodeValue('686579', {}) + let buf = codec.encodeValue('686579', {}) t.equal(buf.toString(), 'hey') buf = codec.encodeValue('686579') @@ -92,9 +92,9 @@ test('encode value - legacy', function (t) { }) test('decode value - legacy', function (t) { - var codec = new Codec({ encoding: 'hex' }) + const codec = new Codec({ encoding: 'hex' }) - var buf = codec.decodeValue(Buffer.from('hey'), {}) + let buf = codec.decodeValue(Buffer.from('hey'), {}) t.equal(buf, '686579') buf = codec.decodeValue(Buffer.from('hey')) diff --git a/test/ltgt.js b/test/ltgt.js index 53843ec..c109352 100644 --- a/test/ltgt.js +++ b/test/ltgt.js @@ -1,14 +1,14 @@ -var test = require('tape') -var Codec = require('..') +const test = require('tape') +const Codec = require('..') test('encode ltgt', function (t) { - var codec = new Codec({ keyEncoding: 'hex' }) + const codec = new Codec({ keyEncoding: 'hex' }) - var ltgt = { + let ltgt = { start: '686579', lte: '686579' } - var encoded = codec.encodeLtgt(ltgt) + let encoded = codec.encodeLtgt(ltgt) t.equal(encoded.start.toString(), 'hey') t.equal(encoded.lte.toString(), 'hey') From eea0a5baae4c4cbbd21e997f4b6b6cdb4c81f89b Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sun, 4 Apr 2021 20:58:33 +0200 Subject: [PATCH 3/6] Breaking: drop node 6 and 8 --- .travis.yml | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index bfe1509..1eb7e30 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: node_js node_js: - - 6 - - 8 - 10 + - 12 + - 14 after_success: npm run coverage diff --git a/package.json b/package.json index 0a63b74..3a84d13 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,6 @@ "level" ], "engines": { - "node": ">=6" + "node": ">=10" } } From a42db0308df9c3150ce6493b9c8473082be79662 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sun, 4 Apr 2021 21:00:00 +0200 Subject: [PATCH 4/6] Bump nyc from 14.x to 15.x --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3a84d13..768c44a 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dependency-check": "^3.3.0", "hallmark": "^3.1.0", "level-community": "^3.0.0", - "nyc": "^14.0.0", + "nyc": "^15.1.0", "standard": "^16.0.3", "tape": "^5.0.1" }, From 24b44b8fc59124b3e38bfbc878903b795b3dbda1 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Mon, 5 Apr 2021 00:11:23 +0200 Subject: [PATCH 5/6] Remove legacy range options (Level/community#86) --- index.js | 6 +++++- test/ltgt.js | 22 ++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index f2814fb..2601c44 100644 --- a/index.js +++ b/index.js @@ -62,11 +62,15 @@ Codec.prototype.encodeBatch = function (ops, opts) { }) } -const ltgtKeys = ['lt', 'gt', 'lte', 'gte', 'start', 'end'] +const ltgtKeys = ['lt', 'gt', 'lte', 'gte'] Codec.prototype.encodeLtgt = function (ltgt) { const ret = {} Object.keys(ltgt).forEach((key) => { + if (key === 'start' || key === 'end') { + throw new Error('Legacy range options ("start" and "end") have been removed') + } + ret[key] = ltgtKeys.indexOf(key) > -1 ? this.encodeKey(ltgt[key], ltgt) : ltgt[key] diff --git a/test/ltgt.js b/test/ltgt.js index c109352..b0168b3 100644 --- a/test/ltgt.js +++ b/test/ltgt.js @@ -5,21 +5,35 @@ test('encode ltgt', function (t) { const codec = new Codec({ keyEncoding: 'hex' }) let ltgt = { - start: '686579', + gte: '686579', lte: '686579' } let encoded = codec.encodeLtgt(ltgt) - t.equal(encoded.start.toString(), 'hey') + t.equal(encoded.gte.toString(), 'hey') t.equal(encoded.lte.toString(), 'hey') ltgt = { - start: '686579', + gte: '686579', lte: '686579', keyEncoding: 'json' } encoded = codec.encodeLtgt(ltgt) - t.equal(encoded.start, '"686579"') + t.equal(encoded.gte, '"686579"') t.equal(encoded.lte, '"686579"') t.end() }) + +test('rejects legacy range options', function (t) { + t.plan(2) + + const codec = new Codec() + + for (const k of ['start', 'end']) { + try { + codec.encodeLtgt({ [k]: 123 }) + } catch (err) { + t.is(err.message, 'Legacy range options ("start" and "end") have been removed') + } + } +}) From 56d3abaa9d76a6180a670d1487c7df6022125297 Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Fri, 9 Apr 2021 14:33:52 +0200 Subject: [PATCH 6/6] Replace use of forEach() and Array#indexOf() --- index.js | 11 ++++++----- lib/encodings.js | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 2601c44..566dae1 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict' const encodings = require('./lib/encodings') +const rangeOptions = new Set(['lt', 'gt', 'lte', 'gte']) module.exports = Codec @@ -62,19 +63,19 @@ Codec.prototype.encodeBatch = function (ops, opts) { }) } -const ltgtKeys = ['lt', 'gt', 'lte', 'gte'] - Codec.prototype.encodeLtgt = function (ltgt) { const ret = {} - Object.keys(ltgt).forEach((key) => { + + for (const key of Object.keys(ltgt)) { if (key === 'start' || key === 'end') { throw new Error('Legacy range options ("start" and "end") have been removed') } - ret[key] = ltgtKeys.indexOf(key) > -1 + ret[key] = rangeOptions.has(key) ? this.encodeKey(ltgt[key], ltgt) : ltgt[key] - }) + } + return ret } diff --git a/lib/encodings.js b/lib/encodings.js index ec1f6d5..2db1d03 100644 --- a/lib/encodings.js +++ b/lib/encodings.js @@ -1,6 +1,6 @@ 'use strict' -const Buffer = require('buffer').Buffer +const { Buffer } = require('buffer') exports.utf8 = exports['utf-8'] = { encode: function (data) { @@ -46,7 +46,7 @@ const bufferEncodings = [ 'utf-16le' ] -bufferEncodings.forEach(function (type) { +for (const type of bufferEncodings) { exports[type] = { encode: function (data) { return isBinary(data) ? data : Buffer.from(data, type) @@ -57,7 +57,7 @@ bufferEncodings.forEach(function (type) { buffer: true, type: type } -}) +} function identity (value) { return value