Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: modernize dependencies #36

Merged
merged 7 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, '*']
node-version: ['lts/-1', 'lts/*', 'node']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ node_modules
mydb

package-lock.json
.tap/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The first parameter is an instance of [levelup][levelup].
Example:

```js
const { Level } = require('level') // Level >= 8.0.0 is required
const { Level } = require('level') // Level >= 9.0.0 is required
const aedesPersistencelevel = require('aedes-persistence-level')

// instantiate a persistence instance
Expand Down
21 changes: 9 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,18 @@
},
"homepage": "https://github.com/mcollina/aedes-persistence-level#readme",
"devDependencies": {
"aedes": "^0.46.3",
"aedes-persistence": "^9.1.1",
"concat-stream": "^2.0.0",
"faucet": "0.0.1",
"level": "^8.0.0",
"mqemitter": "^4.5.0",
"pre-commit": "^1.2.2",
"release-it": "^15.0.0",
"standard": "^17.0.0",
"tape": "^5.5.3",
"through2": "^4.0.2"
"@fastify/pre-commit": "^2.2.0",
"aedes": "^0.51.3",
"aedes-persistence": "^9.1.2",
"level": "^9.0.0",
"release-it": "^18.1.2",
"standard": "^17.1.2"
},
"dependencies": {
"aedes-packet": "^3.0.0",
"faucet": "^0.0.4",
"msgpack-lite": "^0.1.26",
"qlobber": "^7.0.0"
"qlobber": "^8.0.1",
"tape": "^5.9.0"
}
}
134 changes: 75 additions & 59 deletions persistence.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Packet = require('aedes-packet')
const msgpack = require('msgpack-lite')
const { EventEmitter } = require('events')
const { Readable } = require('stream')
const { EventEmitter } = require('node:events')
const { Readable } = require('node:stream')
const QlobberSub = require('qlobber/aedes/qlobber-sub')
const { QlobberTrue } = require('qlobber')

Expand All @@ -21,6 +21,7 @@ const WILL = 'will:'
const encodingOption = {
valueEncoding: 'buffer'
}
const LEVEL_NOT_FOUND = 'LEVEL_NOT_FOUND'

async function * decodedDbValues (db, start) {
const opts = Object.assign({
Expand Down Expand Up @@ -136,6 +137,12 @@ function toSubKey (sub) {
return `${subByClientKey(sub.clientId)}:${sub.topic}`
}

function callCb (cb, err, value) {
if (typeof cb === 'function') {
cb(err, value)
}
}

class LevelPersistence extends EventEmitter {
// private class members start with #
#db
Expand All @@ -148,34 +155,38 @@ class LevelPersistence extends EventEmitter {
this.#trie = new QlobberSub(QlobberOpts)
this.#ready = false

const that = this

loadSubscriptions(this.#db, this.#trie)
.then(() => {
that.#ready = true
that.emit('ready')
this.#ready = true
this.emit('ready')
})
.catch((err) => {
that.emit('error', err)
this.emit('error', err)
})
}

#dbGet (key, cb) {
this.#db.get(key, encodingOption, (err, blob) => {
cb(err, (!err) ? msgpack.decode(blob) : null)
})
async #dbGet (key, cb) {
const blob = await this.#db.get(key, encodingOption)
if (blob !== undefined) {
cb(undefined, msgpack.decode(blob))
return
}
cb(LEVEL_NOT_FOUND, null)
}

#dbPut (key, value, cb) {
this.#db.put(key, msgpack.encode(value), encodingOption, cb)
async #dbPut (key, value, cb) {
await this.#db.put(key, msgpack.encode(value), encodingOption)
callCb(cb)
}

#dbDel (key, cb) {
this.#db.del(key, cb)
async #dbDel (key, cb) {
await this.#db.del(key)
callCb(cb)
}

#dbBatch (opArray, cb) {
this.#db.batch(opArray, encodingOption, cb)
async #dbBatch (opArray, cb) {
await this.#db.batch(opArray, encodingOption)
callCb(cb)
}

storeRetained (packet, cb) {
Expand Down Expand Up @@ -261,11 +272,10 @@ class LevelPersistence extends EventEmitter {
}

cleanSubscriptions (client, cb) {
const that = this
this.subscriptionsByClient(client, (err, subs) => {
if (err || !subs) return cb(err, client)

that.removeSubscriptions(
this.removeSubscriptions(
client,
subs.map(sub => sub.topic),
(err) => {
Expand Down Expand Up @@ -300,32 +310,33 @@ class LevelPersistence extends EventEmitter {
}

outgoingUpdate (client, packet, cb) {
const that = this
if (packet.brokerId) {
that.#updateWithBrokerData(client, packet, cb)
this.#updateWithBrokerData(client, packet, cb)
} else {
this.#augmentWithBrokerData(client, packet, (err) => {
if (err) return cb(err, client, packet)
that.#updateWithBrokerData(client, packet, cb)
this.#updateWithBrokerData(client, packet, cb)
})
}
}

outgoingClearMessageId (client, packet, cb) {
const that = this
const key = outgoingByIdKey(client.id, packet.messageId)
this.#dbGet(key, (err, packet) => {
if (err?.notFound) {
return cb(null)
} else if (err) {
return cb(err)
if (err === LEVEL_NOT_FOUND) {
cb(null)
return
}
if (err) {
cb(err)
return
}

const prekey = outgoingKey(client.id, packet.brokerId, packet.brokerCounter)
const batch = that.#db.batch()
const batch = this.#db.batch()
batch.del(key)
batch.del(prekey)
batch.write((err) => {
batch.write().then(err => {
cb(err, packet)
})
})
Expand All @@ -348,13 +359,15 @@ class LevelPersistence extends EventEmitter {
incomingGetPacket (client, packet, cb) {
const key = incomingKey(client.id, packet.messageId)
this.#dbGet(key, (err, packet) => {
if (err && err.notFound) {
if (err === LEVEL_NOT_FOUND) {
cb(new Error('no such packet'), client)
} else if (err) {
return
}
if (err) {
cb(err, client)
} else {
cb(null, packet, client)
return
}
cb(null, packet, client)
})
}

Expand All @@ -377,22 +390,22 @@ class LevelPersistence extends EventEmitter {
getWill (client, cb) {
const key = willKey(client.id)
this.#dbGet(key, (err, will) => {
if (err && err.notFound) {
if (err === LEVEL_NOT_FOUND) {
cb(null, null, client)
} else {
cb(err, will, client)
return
}
cb(err, will, client)
})
}

delWill (client, cb) {
const key = willKey(client.id)
const that = this
this.#dbGet(key, (err, will) => {
if (err) {
return cb(err, null, client)
cb(err, null, client)
return
}
that.#dbDel(key, (err) => {
this.#dbDel(key, (err) => {
cb(err, will, client)
})
})
Expand All @@ -409,40 +422,43 @@ class LevelPersistence extends EventEmitter {
#updateWithBrokerData (client, packet, cb) {
const prekey = outgoingKey(client.id, packet.brokerId, packet.brokerCounter)
const postkey = outgoingByIdKey(client.id, packet.messageId)
const that = this

this.#dbGet(prekey, (err, decoded) => {
if (err && err.notFound) {
if (err === LEVEL_NOT_FOUND) {
cb(new Error('no such packet'), client, packet)
return
} else if (err) {
}
if (err) {
cb(err, client, packet)
return
}

if (decoded.messageId > 0) {
that.#dbDel(outgoingByIdKey(client.id, decoded.messageId))
this.#dbDel(outgoingByIdKey(client.id, decoded.messageId))
}

that.#dbPut(postkey, packet, (err) => {
this.#dbPut(postkey, packet, (err) => {
if (err) {
cb(err, client, packet)
} else {
that.#dbPut(prekey, packet, (err) => {
cb(err, client, packet)
})
return
}
this.#dbPut(prekey, packet, (err) => {
cb(err, client, packet)
})
})
})
}

#augmentWithBrokerData (client, packet, cb) {
const postkey = outgoingByIdKey(client.id, packet.messageId)
this.#dbGet(postkey, (err, decoded) => {
if (err && err.notFound) {
return cb(new Error('no such packet'))
} else if (err) {
return cb(err)
if (err === LEVEL_NOT_FOUND) {
cb(new Error('no such packet'))
return
}
if (err) {
cb(err)
return
}

packet.brokerId = decoded.brokerId
Expand All @@ -451,8 +467,9 @@ class LevelPersistence extends EventEmitter {
})
}

destroy (cb) {
this.#db.close(cb)
async destroy (cb) {
await this.#db.close()
callCb(cb)
}
}

Expand All @@ -466,11 +483,10 @@ function addSubToTrie (trie, sub) {
if (match.qos === sub.qos) {
add = false
break
} else {
trie.remove(match.topic, match)
if (sub.qos === 0) {
add = false
}
}
trie.remove(match.topic, match)
if (sub.qos === 0) {
add = false
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const test = require('tape').test
const { test } = require('tape')
const persistence = require('./')
const abs = require('aedes-persistence/abstract')
const { Level } = require('level') // Level >= 8.0.0
Expand Down