Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit e7c67e2

Browse files
authored
Breaking: modernize syntax (#22)
Drops support of old browsers that don't support `const` and `let`.
1 parent 77849a7 commit e7c67e2

File tree

12 files changed

+247
-240
lines changed

12 files changed

+247
-240
lines changed

client.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
var levelup = require('levelup')
2-
var encode = require('encoding-down')
3-
var leveldown = require('./leveldown')
1+
const levelup = require('levelup')
2+
const encode = require('encoding-down')
3+
const leveldown = require('./leveldown')
44

55
module.exports = function (opts) {
66
if (!opts) opts = {}
77

8-
var down = leveldown(Object.assign({}, opts, { onflush: onflush }))
9-
var db = levelup(encode(down, opts), opts)
8+
const down = leveldown(Object.assign({}, opts, { onflush: onflush }))
9+
const db = levelup(encode(down, opts), opts)
1010

1111
db.createRpcStream = db.connect = connect
1212
db.isFlushed = isFlushed

leveldown.js

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
var duplexify = require('duplexify')
2-
var abstract = require('abstract-leveldown')
3-
var util = require('util')
4-
var eos = require('end-of-stream')
5-
var ids = require('numeric-id-map')
6-
var lpstream = require('length-prefixed-stream')
7-
var reachdown = require('reachdown')
8-
var messages = require('./messages')
9-
var matchdown = require('./matchdown')
10-
11-
var ENCODERS = [
1+
const duplexify = require('duplexify')
2+
const abstract = require('abstract-leveldown')
3+
const util = require('util')
4+
const eos = require('end-of-stream')
5+
const ids = require('numeric-id-map')
6+
const lpstream = require('length-prefixed-stream')
7+
const reachdown = require('reachdown')
8+
const messages = require('./messages')
9+
const matchdown = require('./matchdown')
10+
11+
const ENCODERS = [
1212
messages.Get,
1313
messages.Put,
1414
messages.Delete,
1515
messages.Batch,
1616
messages.Iterator
1717
]
1818

19-
var DECODERS = [
19+
const DECODERS = [
2020
messages.Callback,
2121
messages.IteratorData
2222
]
@@ -47,18 +47,19 @@ Multilevel.prototype.createRpcStream = function (opts, proxy) {
4747
if (!opts) opts = {}
4848
this._ref = opts.ref || null
4949

50-
var self = this
51-
var encode = this._encode
52-
var decode = lpstream.decode()
50+
const self = this
51+
const encode = this._encode
52+
const decode = lpstream.decode()
5353

5454
decode.on('data', function (data) {
5555
if (!data.length) return
56-
var tag = data[0]
56+
const tag = data[0]
5757
if (tag >= DECODERS.length) return
5858

59-
var dec = DECODERS[tag]
59+
const dec = DECODERS[tag]
60+
let res
6061
try {
61-
var res = dec.decode(data, 1)
62+
res = dec.decode(data, 1)
6263
} catch (err) {
6364
return
6465
}
@@ -93,29 +94,29 @@ Multilevel.prototype.createRpcStream = function (opts, proxy) {
9394
return
9495
}
9596

96-
for (var i = 0; i < self._requests.length; i++) {
97-
var req = self._requests.get(i)
97+
for (let i = 0; i < self._requests.length; i++) {
98+
const req = self._requests.get(i)
9899
if (!req) continue
99100
self._write(req)
100101
}
101102

102-
for (var j = 0; j < self._iterators.length; j++) {
103-
var ite = self._iterators.get(j)
103+
for (let j = 0; j < self._iterators.length; j++) {
104+
const ite = self._iterators.get(j)
104105
if (!ite) continue
105106
ite.options = ite.iterator._options
106107
self._write(ite)
107108
}
108109
}
109110

110111
function oniteratordata (res) {
111-
var req = self._iterators.get(res.id)
112+
const req = self._iterators.get(res.id)
112113
if (!req) return
113114
req.pending.push(res)
114115
if (req.callback) req.iterator.next(req.callback)
115116
}
116117

117118
function oncallback (res) {
118-
var req = self._requests.remove(res.id)
119+
const req = self._requests.remove(res.id)
119120
if (req) req.callback(decodeError(res.error), decodeValue(res.value, req.valueAsBuffer))
120121
}
121122
}
@@ -135,13 +136,13 @@ Multilevel.prototype._flushMaybe = function () {
135136
}
136137

137138
Multilevel.prototype._clearRequests = function (closing) {
138-
for (var i = 0; i < this._requests.length; i++) {
139-
var req = this._requests.remove(i)
139+
for (let i = 0; i < this._requests.length; i++) {
140+
const req = this._requests.remove(i)
140141
if (req) req.callback(new Error('Connection to leader lost'))
141142
}
142143

143-
for (var j = 0; j < this._iterators.length; j++) {
144-
var ite = this._iterators.remove(j)
144+
for (let j = 0; j < this._iterators.length; j++) {
145+
const ite = this._iterators.remove(j)
145146
if (ite) {
146147
if (ite.callback && !closing) ite.callback(new Error('Connection to leader lost'))
147148
ite.iterator.end()
@@ -160,7 +161,7 @@ Multilevel.prototype._serializeValue = function (value) {
160161
Multilevel.prototype._get = function (key, opts, cb) {
161162
if (this._db) return this._db._get(key, opts, cb)
162163

163-
var req = {
164+
const req = {
164165
tag: 0,
165166
id: 0,
166167
key: key,
@@ -175,7 +176,7 @@ Multilevel.prototype._get = function (key, opts, cb) {
175176
Multilevel.prototype._put = function (key, value, opts, cb) {
176177
if (this._db) return this._db._put(key, value, opts, cb)
177178

178-
var req = {
179+
const req = {
179180
tag: 1,
180181
id: 0,
181182
key: key,
@@ -190,7 +191,7 @@ Multilevel.prototype._put = function (key, value, opts, cb) {
190191
Multilevel.prototype._del = function (key, opts, cb) {
191192
if (this._db) return this._db._del(key, opts, cb)
192193

193-
var req = {
194+
const req = {
194195
tag: 2,
195196
id: 0,
196197
key: key,
@@ -204,7 +205,7 @@ Multilevel.prototype._del = function (key, opts, cb) {
204205
Multilevel.prototype._batch = function (batch, opts, cb) {
205206
if (this._db) return this._db._batch(batch, opts, cb)
206207

207-
var req = {
208+
const req = {
208209
tag: 3,
209210
id: 0,
210211
ops: batch,
@@ -217,8 +218,8 @@ Multilevel.prototype._batch = function (batch, opts, cb) {
217218

218219
Multilevel.prototype._write = function (req) {
219220
if (this._requests.length + this._iterators.length === 1) ref(this._ref)
220-
var enc = ENCODERS[req.tag]
221-
var buf = Buffer.allocUnsafe(enc.encodingLength(req) + 1)
221+
const enc = ENCODERS[req.tag]
222+
const buf = Buffer.allocUnsafe(enc.encodingLength(req) + 1)
222223
buf[0] = req.tag
223224
enc.encode(req, buf, 1)
224225
this._encode.write(buf)
@@ -252,7 +253,7 @@ function Iterator (parent, opts) {
252253
this._valueAsBuffer = opts.valueAsBuffer
253254
this._options = opts
254255

255-
var req = {
256+
const req = {
256257
tag: 4,
257258
id: 0,
258259
batch: 32,
@@ -281,16 +282,16 @@ Iterator.prototype.next = function (cb) {
281282
this._parent._write(this._req)
282283
}
283284

284-
var next = this._req.pending.shift()
285+
const next = this._req.pending.shift()
285286
if (next.error) return cb(decodeError(next.error))
286287

287288
if (!next.key && !next.value) return cb()
288289

289290
this._options.gt = next.key
290291
if (this._options.limit > 0) this._options.limit--
291292

292-
var key = decodeValue(next.key, this._keyAsBuffer)
293-
var val = decodeValue(next.value, this._valueAsBuffer)
293+
const key = decodeValue(next.key, this._keyAsBuffer)
294+
const val = decodeValue(next.value, this._valueAsBuffer)
294295
return cb(undefined, key, val)
295296
}
296297

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
"level-community": "^3.0.0",
3535
"level-compose": "^0.0.2",
3636
"memdown": "^5.0.0",
37-
"nyc": "^14.1.1",
37+
"nyc": "^15.1.0",
3838
"protocol-buffers": "^4.0.2",
39-
"standard": "^14.1.0",
39+
"standard": "^16.0.3",
4040
"subleveldown": "^5.0.1",
4141
"tape": "^5.0.1"
4242
},
@@ -52,6 +52,11 @@
5252
},
5353
"homepage": "https://github.com/Level/multileveldown",
5454
"engines": {
55-
"node": ">=8"
55+
"node": ">=10"
56+
},
57+
"standard": {
58+
"ignore": [
59+
"messages.js"
60+
]
5661
}
5762
}

server.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
var lpstream = require('length-prefixed-stream')
2-
var eos = require('end-of-stream')
3-
var duplexify = require('duplexify')
4-
var reachdown = require('reachdown')
5-
var messages = require('./messages')
6-
var rangeOptions = 'gt gte lt lte'.split(' ')
7-
var matchdown = require('./matchdown')
8-
9-
var DECODERS = [
1+
const lpstream = require('length-prefixed-stream')
2+
const eos = require('end-of-stream')
3+
const duplexify = require('duplexify')
4+
const reachdown = require('reachdown')
5+
const messages = require('./messages')
6+
const rangeOptions = 'gt gte lt lte'.split(' ')
7+
const matchdown = require('./matchdown')
8+
9+
const DECODERS = [
1010
messages.Get,
1111
messages.Put,
1212
messages.Delete,
@@ -17,39 +17,40 @@ var DECODERS = [
1717
module.exports = function (db, opts) {
1818
if (!opts) opts = {}
1919

20-
var readonly = !!(opts.readonly)
21-
var decode = lpstream.decode()
22-
var encode = lpstream.encode()
23-
var stream = duplexify(decode, encode)
20+
const readonly = !!(opts.readonly)
21+
const decode = lpstream.decode()
22+
const encode = lpstream.encode()
23+
const stream = duplexify(decode, encode)
2424

25-
var preput = opts.preput || function (key, val, cb) { cb(null) }
26-
var predel = opts.predel || function (key, cb) { cb(null) }
27-
var prebatch = opts.prebatch || function (ops, cb) { cb(null) }
25+
const preput = opts.preput || function (key, val, cb) { cb(null) }
26+
const predel = opts.predel || function (key, cb) { cb(null) }
27+
const prebatch = opts.prebatch || function (ops, cb) { cb(null) }
2828

2929
if (db.isOpen()) ready()
3030
else db.open(ready)
3131

3232
return stream
3333

3434
function ready () {
35-
var down = reachdown(db, matchdown, false)
36-
var iterators = []
35+
const down = reachdown(db, matchdown, false)
36+
const iterators = []
3737

3838
eos(stream, function () {
3939
while (iterators.length) {
40-
var next = iterators.shift()
40+
const next = iterators.shift()
4141
if (next) next.end()
4242
}
4343
})
4444

4545
decode.on('data', function (data) {
4646
if (!data.length) return
47-
var tag = data[0]
47+
const tag = data[0]
4848
if (tag >= DECODERS.length) return
4949

50-
var dec = DECODERS[tag]
50+
const dec = DECODERS[tag]
51+
let req
5152
try {
52-
var req = dec.decode(data, 1)
53+
req = dec.decode(data, 1)
5354
} catch (err) {
5455
return
5556
}
@@ -74,8 +75,8 @@ module.exports = function (db, opts) {
7475
})
7576

7677
function callback (id, err, value) {
77-
var msg = { id: id, error: err && err.message, value: value }
78-
var buf = Buffer.allocUnsafe(messages.Callback.encodingLength(msg) + 1)
78+
const msg = { id: id, error: err && err.message, value: value }
79+
const buf = Buffer.allocUnsafe(messages.Callback.encodingLength(msg) + 1)
7980
buf[0] = 0
8081
messages.Callback.encode(msg, buf, 1)
8182
encode.write(buf)
@@ -122,7 +123,7 @@ module.exports = function (db, opts) {
122123
function oniterator (req) {
123124
while (iterators.length < req.id) iterators.push(null)
124125

125-
var prev = iterators[req.id]
126+
let prev = iterators[req.id]
126127
if (!prev) prev = iterators[req.id] = new Iterator(down, req, encode)
127128

128129
if (!req.batch) {
@@ -137,7 +138,7 @@ module.exports = function (db, opts) {
137138
}
138139

139140
function Iterator (down, req, encode) {
140-
var self = this
141+
const self = this
141142

142143
this.batch = req.batch || 0
143144
this._iterator = down.iterator(cleanRangeOptions(req.options))
@@ -159,7 +160,7 @@ function Iterator (down, req, encode) {
159160
self._data.key = key
160161
self._data.value = value
161162
self.batch--
162-
var buf = Buffer.allocUnsafe(messages.IteratorData.encodingLength(self._data) + 1)
163+
const buf = Buffer.allocUnsafe(messages.IteratorData.encodingLength(self._data) + 1)
163164
buf[0] = 1
164165
messages.IteratorData.encode(self._data, buf, 1)
165166
encode.write(buf)
@@ -185,9 +186,9 @@ function noop () {}
185186
function cleanRangeOptions (options) {
186187
if (!options) return
187188

188-
var result = {}
189+
const result = {}
189190

190-
for (var k in options) {
191+
for (const k in options) {
191192
if (!hasOwnProperty.call(options, k)) continue
192193

193194
if (!isRangeOption(k) || options[k] != null) {

streams.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var pbs = require('pbs')
2-
var fs = require('fs')
3-
var path = require('path')
1+
const pbs = require('pbs')
2+
const fs = require('fs')
3+
const path = require('path')
44

55
module.exports = pbs(fs.readFileSync(path.join(__dirname, 'schema.proto')))

test/abstract.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
var test = require('tape')
2-
var memdown = require('memdown')
3-
var levelup = require('levelup')
4-
var encode = require('encoding-down')
5-
var factory = require('level-compose')(memdown, encode, levelup)
6-
var multileveldown = require('..')
7-
var suite = require('abstract-leveldown/test')
1+
const test = require('tape')
2+
const memdown = require('memdown')
3+
const levelup = require('levelup')
4+
const encode = require('encoding-down')
5+
const factory = require('level-compose')(memdown, encode, levelup)
6+
const multileveldown = require('..')
7+
const suite = require('abstract-leveldown/test')
88

99
suite({
1010
test: test,
1111
factory: function () {
12-
var db = factory()
13-
var stream = multileveldown.server(db)
14-
var client = multileveldown.client()
12+
const db = factory()
13+
const stream = multileveldown.server(db)
14+
const client = multileveldown.client()
1515

1616
stream.pipe(client.createRpcStream()).pipe(stream)
1717

0 commit comments

Comments
 (0)