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

Commit 9615cac

Browse files
authored
Breaking: modernize syntax (drops node 8) (#26)
1 parent 0732684 commit 9615cac

11 files changed

Lines changed: 136 additions & 104 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: node_js
22

33
node_js:
4-
- 8
54
- 10
65
- 12
6+
- 14
77

88
before_script: git fetch --tags
99
after_success: npm run coverage

example/bulk-insert.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
var level = require('..')
2-
var path = require('path')
3-
var db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
1+
const level = require('..')
2+
const path = require('path')
3+
const db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
44

55
db.on('leader', function () {
66
console.log('i am the leader now')
77
})
88

9-
var tick = 0
9+
let tick = 0
1010

1111
function loop () {
1212
db.put('hello-' + tick, { hello: 'world-' + tick }, function () {

example/get.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var level = require('..')
2-
var path = require('path')
3-
var db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
1+
const level = require('..')
2+
const path = require('path')
3+
const db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
44

55
db.on('leader', function () {
66
console.log('i am the leader now')

example/put.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
var level = require('..')
2-
var path = require('path')
3-
var db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
1+
const level = require('..')
2+
const path = require('path')
3+
const db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
44

55
db.on('leader', function () {
66
console.log('i am the leader now')
77
})
88

9-
var n = Math.floor(Math.random() * 100000)
9+
let n = Math.floor(Math.random() * 100000)
1010

1111
setInterval(function () {
1212
db.put('a', n++, function (err) {

example/read-stream.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
var level = require('..')
2-
var path = require('path')
3-
var db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
1+
const level = require('..')
2+
const path = require('path')
3+
const db = level(path.join(__dirname, 'data'), { valueEncoding: 'json' })
44

55
db.on('leader', function () {
66
console.log('i am the leader now')
77
})
88

9-
var rs = db.createReadStream()
9+
const rs = db.createReadStream()
1010

1111
rs.on('end', function () {
1212
console.log('(end)')
1313
process.exit()
1414
})
1515

1616
setInterval(function () {
17-
var next = rs.read()
17+
const next = rs.read()
1818
if (next) console.log(next)
1919
}, 250)

index.js

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,79 @@
11
'use strict'
22

3-
var level = require('level')
4-
var has = require('has')
5-
var pump = require('pump')
6-
var fs = require('fs')
7-
var net = require('net')
8-
var path = require('path')
9-
var multileveldown = require('multileveldown')
10-
11-
module.exports = function (dir, opts) {
12-
if (!opts) opts = {}
13-
if (!has(opts, 'retry')) opts.retry = true
14-
15-
var sockPath = process.platform === 'win32'
3+
const level = require('level')
4+
const { pipeline: pump } = require('readable-stream')
5+
const fs = require('fs')
6+
const net = require('net')
7+
const path = require('path')
8+
const multileveldown = require('multileveldown')
9+
10+
module.exports = function (dir, opts = {}) {
11+
const sockPath = process.platform === 'win32'
1612
? '\\\\.\\pipe\\level-party\\' + path.resolve(dir)
1713
: path.join(dir, 'level-party.sock')
1814

19-
var client = multileveldown.client(opts)
15+
opts = { retry: true, ...opts }
16+
17+
const client = multileveldown.client(opts)
2018

2119
client.open(tryConnect)
2220

2321
function tryConnect () {
24-
if (!client.isOpen()) return
22+
if (!client.isOpen()) {
23+
return
24+
}
2525

26-
var socket = net.connect(sockPath)
27-
var connected = false
26+
const socket = net.connect(sockPath)
27+
let connected = false
2828

2929
socket.on('connect', function () {
3030
connected = true
3131
})
3232

33-
// we pass socket as the ref option so we dont hang the event loop
33+
// Pass socket as the ref option so we dont hang the event loop.
3434
pump(socket, client.createRpcStream({ ref: socket }), socket, function () {
35-
if (!client.isOpen()) return
35+
// TODO: err?
36+
37+
if (!client.isOpen()) {
38+
return
39+
}
3640

37-
var db = level(dir, opts, onopen)
41+
const db = level(dir, opts, onopen)
3842

3943
function onopen (err) {
4044
if (err) {
41-
if (connected) return tryConnect()
42-
return setTimeout(tryConnect, 100)
45+
// TODO: This can cause an invisible retry loop that never completes
46+
// and leads to memory leaks.
47+
// TODO: What errors should be retried?
48+
if (connected) {
49+
tryConnect()
50+
} else {
51+
setTimeout(tryConnect, 100)
52+
}
53+
return
4354
}
4455

4556
fs.unlink(sockPath, function (err) {
46-
if (err && err.code !== 'ENOENT') return db.emit('error', err)
47-
if (!client.isOpen()) return
57+
if (err && err.code !== 'ENOENT') {
58+
// TODO: Is this how to forward errors?
59+
db.emit('error', err)
60+
return
61+
}
62+
63+
if (!client.isOpen()) {
64+
return
65+
}
66+
67+
const sockets = new Set()
68+
const server = net.createServer(function (sock) {
69+
if (sock.unref) {
70+
sock.unref()
71+
}
4872

49-
var sockets = []
50-
var server = net.createServer(function (sock) {
51-
if (sock.unref) sock.unref()
52-
sockets.push(sock)
73+
sockets.add(sock)
5374
pump(sock, multileveldown.server(db), sock, function () {
54-
sockets.splice(sockets.indexOf(sock), 1)
75+
// TODO: err?
76+
sockets.delete(sock)
5577
})
5678
})
5779

@@ -60,30 +82,41 @@ module.exports = function (dir, opts) {
6082
client.forward(db)
6183

6284
server.listen(sockPath, onlistening)
85+
.on('error', function () {
86+
// TODO: Is this how to forward errors?
87+
// TODO: tryConnect()?
88+
db.emit('error', err)
89+
})
6390

6491
function shutdown (cb) {
65-
sockets.forEach(function (sock) {
92+
for (const sock of sockets) {
6693
sock.destroy()
67-
})
68-
server.close(function () {
94+
}
95+
server.close(() => {
6996
db.close(cb)
7097
})
7198
}
7299

73100
function onlistening () {
74-
if (server.unref) server.unref()
75-
if (client.isFlushed()) return
76-
77-
var sock = net.connect(sockPath)
78-
pump(sock, client.createRpcStream(), sock)
101+
if (server.unref) {
102+
server.unref()
103+
}
104+
if (client.isFlushed()) {
105+
return
106+
}
107+
108+
const sock = net.connect(sockPath)
109+
pump(sock, client.createRpcStream(), sock, function () {
110+
// TODO: err?
111+
})
79112
client.once('flush', function () {
80113
sock.destroy()
81114
})
82115
}
83116
})
84117
}
85118
})
86-
};
119+
}
87120

88121
return client
89122
}

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@
1717
"prepublishOnly": "npm run dependency-check"
1818
},
1919
"dependencies": {
20-
"has": "^1.0.0",
2120
"level": "^6.0.0",
2221
"multileveldown": "^3.0.0",
23-
"pump": "^3.0.0"
22+
"readable-stream": "^3.6.0"
2423
},
2524
"devDependencies": {
2625
"bytewise": "^1.1.0",
2726
"coveralls": "^3.0.9",
2827
"dependency-check": "^4.1.0",
2928
"hallmark": "^3.1.0",
3029
"level-community": "^3.0.0",
31-
"nyc": "^14.1.1",
30+
"nyc": "^15.1.0",
3231
"osenv": "~0.1.0",
33-
"standard": "^14.3.1",
32+
"standard": "^16.0.3",
3433
"subleveldown": "^5.0.1",
3534
"tape": "^5.0.1"
3635
},

test/bytewise.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 level = require('..')
3-
var path = require('path')
4-
var bytewise = require('bytewise')
5-
var tmpdir = require('osenv').tmpdir()
6-
var datadir = path.join(tmpdir, 'level-party-' + Math.random())
1+
const test = require('tape')
2+
const level = require('..')
3+
const path = require('path')
4+
const bytewise = require('bytewise')
5+
const tmpdir = require('osenv').tmpdir()
6+
const datadir = path.join(tmpdir, 'level-party-' + Math.random())
77

8-
var lopts = { keyEncoding: bytewise, valueEncoding: 'json' }
8+
const lopts = { keyEncoding: bytewise, valueEncoding: 'json' }
99

1010
test('bytewise key encoding', function (t) {
1111
t.plan(7)
12-
var adb = level(datadir, lopts)
13-
var bdb = level(datadir, lopts)
14-
var value = Math.floor(Math.random() * 100000)
12+
const adb = level(datadir, lopts)
13+
const bdb = level(datadir, lopts)
14+
const value = Math.floor(Math.random() * 100000)
1515

1616
adb.put(['a'], value, function (err) {
1717
t.ifError(err)

test/election.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
var test = require('tape')
2-
var level = require('..')
3-
var path = require('path')
4-
var tmpdir = require('osenv').tmpdir()
5-
var datadir = path.join(tmpdir, 'level-party-' + Math.random())
1+
const test = require('tape')
2+
const level = require('..')
3+
const path = require('path')
4+
const tmpdir = require('osenv').tmpdir()
5+
const datadir = path.join(tmpdir, 'level-party-' + Math.random())
66

77
test('failover election party', function (t) {
8-
var keys = ['a', 'b', 'c', 'e', 'f', 'g']
9-
var len = keys.length
8+
const keys = ['a', 'b', 'c', 'e', 'f', 'g']
9+
const len = keys.length
1010
t.plan(len * (len + 1) / 2)
11-
var pending = keys.length
12-
var handles = {}
11+
let pending = keys.length
12+
const handles = {}
1313

1414
keys.forEach(function (key) {
15-
var h = open(key)
15+
const h = open(key)
1616
h.on('open', function () {
1717
if (--pending === 0) spinDown()
1818
})
1919
})
2020

2121
function open (key) {
22-
var h = handles[key] = level(datadir, { valueEncoding: 'json' })
22+
const h = handles[key] = level(datadir, { valueEncoding: 'json' })
2323
return h
2424
}
2525

2626
function spinDown () {
27-
var alive = keys.slice();
27+
const alive = keys.slice();
2828
(function next () {
2929
if (alive.length === 0) return
3030

3131
check(alive, function () {
32-
var key = alive.shift()
32+
const key = alive.shift()
3333
handles[key].close()
3434
next()
3535
})
3636
})()
3737
}
3838

3939
function check (keys, cb) {
40-
var pending = keys.length
40+
let pending = keys.length
4141
if (pending === 0) return cb()
42-
for (var i = 0; i < keys.length; i++) {
42+
for (let i = 0; i < keys.length; i++) {
4343
(function (a, b) {
44-
var value = Math.random()
44+
const value = Math.random()
4545
handles[a].put(a, value, function (err) {
4646
if (err) t.fail(err)
4747
handles[b].get(a, function (err, x) {

test/handle.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
var test = require('tape')
2-
var level = require('..')
3-
var path = require('path')
4-
var tmpdir = require('osenv').tmpdir()
5-
var datadir = path.join(tmpdir, 'level-party-' + Math.random())
1+
const test = require('tape')
2+
const level = require('..')
3+
const path = require('path')
4+
const tmpdir = require('osenv').tmpdir()
5+
const datadir = path.join(tmpdir, 'level-party-' + Math.random())
66

77
test('two handles', function (t) {
88
t.plan(1)
99

10-
var adb = level(datadir, { valueEncoding: 'json' })
11-
var bdb = level(datadir, { valueEncoding: 'json' })
12-
var value = Math.floor(Math.random() * 100000)
10+
const adb = level(datadir, { valueEncoding: 'json' })
11+
const bdb = level(datadir, { valueEncoding: 'json' })
12+
const value = Math.floor(Math.random() * 100000)
1313

1414
adb.put('a', value, function (err) {
1515
if (err) t.fail(err)

0 commit comments

Comments
 (0)