|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/* This module provides a callback layer for async persistence implementations */ |
| 4 | +const { Readable } = require('node:stream') |
| 5 | +const CachedPersistence = require('./index.js') |
| 6 | + |
| 7 | +function toValue (obj, prop) { |
| 8 | + if (typeof obj === 'object' && obj !== null && prop in obj) { |
| 9 | + return obj[prop] |
| 10 | + } |
| 11 | + return obj |
| 12 | +} |
| 13 | + |
| 14 | +class CallBackPersistence extends CachedPersistence { |
| 15 | + constructor (asyncInstanceFactory, opts = {}) { |
| 16 | + super(opts) |
| 17 | + this.asyncPersistence = asyncInstanceFactory(opts) |
| 18 | + } |
| 19 | + |
| 20 | + _setup () { |
| 21 | + if (this.ready) { |
| 22 | + return |
| 23 | + } |
| 24 | + this.asyncPersistence.broker = this.broker |
| 25 | + this.asyncPersistence._trie = this._trie |
| 26 | + this.asyncPersistence.setup() |
| 27 | + .then(() => { |
| 28 | + this.emit('ready') |
| 29 | + }) |
| 30 | + .catch(err => { |
| 31 | + this.emit('error', err) |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + storeRetained (packet, cb) { |
| 36 | + if (!this.ready) { |
| 37 | + this.once('ready', this.storeRetained.bind(this, packet, cb)) |
| 38 | + return |
| 39 | + } |
| 40 | + this.asyncPersistence.storeRetained(packet).then(() => { |
| 41 | + cb(null) |
| 42 | + }).catch(cb) |
| 43 | + } |
| 44 | + |
| 45 | + createRetainedStream (pattern) { |
| 46 | + return Readable.from(this.asyncPersistence.createRetainedStream(pattern)) |
| 47 | + } |
| 48 | + |
| 49 | + createRetainedStreamCombi (patterns) { |
| 50 | + return Readable.from(this.asyncPersistence.createRetainedStreamCombi(patterns)) |
| 51 | + } |
| 52 | + |
| 53 | + addSubscriptions (client, subs, cb) { |
| 54 | + if (!this.ready) { |
| 55 | + this.once('ready', this.addSubscriptions.bind(this, client, subs, cb)) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + const addSubs1 = this.asyncPersistence.addSubscriptions(client, subs) |
| 60 | + // promisify |
| 61 | + const addSubs2 = new Promise((resolve, reject) => { |
| 62 | + this._addedSubscriptions(client, subs, (err) => { |
| 63 | + if (err) { |
| 64 | + reject(err) |
| 65 | + } else { |
| 66 | + resolve() |
| 67 | + } |
| 68 | + }) |
| 69 | + }) |
| 70 | + Promise.all([addSubs1, addSubs2]) |
| 71 | + .then(() => cb(null, client)) |
| 72 | + .catch(err => cb(err, client)) |
| 73 | + } |
| 74 | + |
| 75 | + removeSubscriptions (client, subs, cb) { |
| 76 | + if (!this.ready) { |
| 77 | + this.once('ready', this.removeSubscriptions.bind(this, client, subs, cb)) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + const remSubs1 = this.asyncPersistence.removeSubscriptions(client, subs) |
| 82 | + // promisify |
| 83 | + const mappedSubs = subs.map(sub => { return { topic: sub } }) |
| 84 | + const remSubs2 = new Promise((resolve, reject) => { |
| 85 | + this._removedSubscriptions(client, mappedSubs, (err) => { |
| 86 | + if (err) { |
| 87 | + reject(err) |
| 88 | + } else { |
| 89 | + resolve() |
| 90 | + } |
| 91 | + }) |
| 92 | + }) |
| 93 | + Promise.all([remSubs1, remSubs2]) |
| 94 | + .then(() => process.nextTick(cb, null, client)) |
| 95 | + .catch(err => cb(err, client)) |
| 96 | + } |
| 97 | + |
| 98 | + subscriptionsByClient (client, cb) { |
| 99 | + if (!this.ready) { |
| 100 | + this.once('ready', this.subscriptionsByClient.bind(this, client, cb)) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + this.asyncPersistence.subscriptionsByClient(client) |
| 105 | + .then(results => { |
| 106 | + // promisified shim returns an object, true async only the resubs |
| 107 | + const resubs = results?.resubs || results |
| 108 | + process.nextTick(cb, null, resubs.length > 0 ? resubs : null, client) |
| 109 | + }) |
| 110 | + .catch(cb) |
| 111 | + } |
| 112 | + |
| 113 | + countOffline (cb) { |
| 114 | + this.asyncPersistence.countOffline() |
| 115 | + .then(res => process.nextTick(cb, null, res.subsCount, res.clientsCount)) |
| 116 | + .catch(cb) |
| 117 | + } |
| 118 | + |
| 119 | + destroy (cb = noop) { |
| 120 | + if (!this.ready) { |
| 121 | + this.once('ready', this.destroy.bind(this, cb)) |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + if (this._destroyed) { |
| 126 | + throw new Error('destroyed called twice!') |
| 127 | + } |
| 128 | + |
| 129 | + this._destroyed = true |
| 130 | + |
| 131 | + this.asyncPersistence.destroy() |
| 132 | + .finally(cb) // swallow err in case of failure |
| 133 | + } |
| 134 | + |
| 135 | + outgoingEnqueue (sub, packet, cb) { |
| 136 | + if (!this.ready) { |
| 137 | + this.once('ready', this.outgoingEnqueue.bind(this, sub, packet, cb)) |
| 138 | + return |
| 139 | + } |
| 140 | + this.asyncPersistence.outgoingEnqueue(sub, packet) |
| 141 | + .then(() => process.nextTick(cb, null, packet)) |
| 142 | + .catch(cb) |
| 143 | + } |
| 144 | + |
| 145 | + outgoingEnqueueCombi (subs, packet, cb) { |
| 146 | + if (!this.ready) { |
| 147 | + this.once('ready', this.outgoingEnqueueCombi.bind(this, subs, packet, cb)) |
| 148 | + return |
| 149 | + } |
| 150 | + this.asyncPersistence.outgoingEnqueueCombi(subs, packet) |
| 151 | + .then(() => process.nextTick(cb, null, packet)) |
| 152 | + .catch(cb) |
| 153 | + } |
| 154 | + |
| 155 | + outgoingStream (client) { |
| 156 | + return Readable.from(this.asyncPersistence.outgoingStream(client)) |
| 157 | + } |
| 158 | + |
| 159 | + outgoingUpdate (client, packet, cb) { |
| 160 | + if (!this.ready) { |
| 161 | + this.once('ready', this.outgoingUpdate.bind(this, client, packet, cb)) |
| 162 | + return |
| 163 | + } |
| 164 | + this.asyncPersistence.outgoingUpdate(client, packet) |
| 165 | + .then(() => cb(null, client, packet)) |
| 166 | + .catch(cb) |
| 167 | + } |
| 168 | + |
| 169 | + outgoingClearMessageId (client, packet, cb) { |
| 170 | + if (!this.ready) { |
| 171 | + this.once('ready', this.outgoingClearMessageId.bind(this, client, packet, cb)) |
| 172 | + return |
| 173 | + } |
| 174 | + this.asyncPersistence.outgoingClearMessageId(client, packet) |
| 175 | + .then((packet) => cb(null, packet)) |
| 176 | + .catch(cb) |
| 177 | + } |
| 178 | + |
| 179 | + incomingStorePacket (client, packet, cb) { |
| 180 | + if (!this.ready) { |
| 181 | + this.once('ready', this.incomingStorePacket.bind(this, client, packet, cb)) |
| 182 | + return |
| 183 | + } |
| 184 | + this.asyncPersistence.incomingStorePacket(client, packet) |
| 185 | + .then(() => cb(null)) |
| 186 | + .catch(cb) |
| 187 | + } |
| 188 | + |
| 189 | + incomingGetPacket (client, packet, cb) { |
| 190 | + if (!this.ready) { |
| 191 | + this.once('ready', this.incomingGetPacket.bind(this, client, packet, cb)) |
| 192 | + return |
| 193 | + } |
| 194 | + this.asyncPersistence.incomingGetPacket(client, packet) |
| 195 | + .then((packet) => cb(null, packet, client)) |
| 196 | + .catch(cb) |
| 197 | + } |
| 198 | + |
| 199 | + incomingDelPacket (client, packet, cb) { |
| 200 | + if (!this.ready) { |
| 201 | + this.once('ready', this.incomingDelPacket.bind(this, client, packet, cb)) |
| 202 | + return |
| 203 | + } |
| 204 | + this.asyncPersistence.incomingDelPacket(client, packet) |
| 205 | + .then(() => cb(null)) |
| 206 | + .catch(cb) |
| 207 | + } |
| 208 | + |
| 209 | + putWill (client, packet, cb) { |
| 210 | + if (!this.ready) { |
| 211 | + this.once('ready', this.putWill.bind(this, client, packet, cb)) |
| 212 | + return |
| 213 | + } |
| 214 | + this.asyncPersistence.putWill(client, packet) |
| 215 | + .then(() => cb(null, client)) |
| 216 | + .catch(cb) |
| 217 | + } |
| 218 | + |
| 219 | + getWill (client, cb) { |
| 220 | + this.asyncPersistence.getWill(client) |
| 221 | + .then((result) => { |
| 222 | + // promisified shim returns an object, true async only the resubs |
| 223 | + const packet = toValue(result, 'packet') |
| 224 | + cb(null, packet, client) |
| 225 | + }) |
| 226 | + .catch(cb) |
| 227 | + } |
| 228 | + |
| 229 | + delWill (client, cb) { |
| 230 | + this.asyncPersistence.delWill(client) |
| 231 | + .then(result => { |
| 232 | + // promisified shim returns an object, true async only the resubs |
| 233 | + const packet = toValue(result, 'packet') |
| 234 | + cb(null, packet, client) |
| 235 | + }) |
| 236 | + .catch(cb) |
| 237 | + } |
| 238 | + |
| 239 | + streamWill (brokers) { |
| 240 | + return Readable.from(this.asyncPersistence.streamWill(brokers)) |
| 241 | + } |
| 242 | + |
| 243 | + getClientList (topic) { |
| 244 | + return Readable.from(this.asyncPersistence.getClientList(topic)) |
| 245 | + } |
| 246 | +} |
| 247 | + |
| 248 | +function noop () {} |
| 249 | + |
| 250 | +module.exports = { CallBackPersistence } |
0 commit comments