-
Notifications
You must be signed in to change notification settings - Fork 4
/
mqtt-nedb-store.js
executable file
·164 lines (141 loc) · 3.51 KB
/
mqtt-nedb-store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
var Datastore = require('nedb'),
Readable = require('readable-stream').Readable,
streamsOpts = { objectMode: true },
// msgpack = require('msgpack5'),
noop = function () {};
/**
* NeDB implementation of the message store
*
*/
var Store = function (options) {
if (!(this instanceof Store)) {
return new Store(options)
}
this._opts = options || {}
this.db = new Datastore({ filename: this._opts.filename, autoload: true })
if (this._opts.autocompactionInterval) {
this.db.persistence.setAutocompactionInterval(Number(this._opts.autocompactionInterval))
}
}
/**
* Adds a packet to the store, a packet is
* anything that has a messageId property.
*
*/
Store.prototype.put = function (packet, cb) {
cb = cb || noop
this.db.insert({_id: packet.messageId, packet: packet}, function (err, doc) {
if (err) {
return this.db.update({_id: packet.messageId}, {_id: packet.messageId, packet: packet}, {}, function (err) {
cb(err, packet)
}.bind(this))
}
cb(null, doc)
}.bind(this))
return this
}
/**
* Creates a stream with all the packets in the store
*
*/
Store.prototype.createStream = function () {
var stream = new Readable(streamsOpts),
destroyed = false,
skip = 0,
limit = this._opts.limit || 500,
db = this.db;
stream._read = function () {
var _skip = skip
skip += limit
db
.find({})
.skip(_skip)
.limit(limit)
.exec(function (err, packets) {
if ( err || destroyed || !packets || 0 === packets.length) {
return this.push(null)
}
packets.forEach(function (packet) {
this.push(packet.packet)
}.bind(this))
}.bind(this))
}
stream.destroy = function () {
if (destroyed) {
return
}
var self = this
destroyed = true
process.nextTick(function () {
self.emit('close')
})
}
return stream
}
/**
* deletes a packet from the store.
*/
Store.prototype.del = function (packet, cb) {
cb = cb || noop
this.get(packet, function (err, packetInDb) {
if (err) {
return cb(err)
}
this.db.remove({ _id: packet.messageId }, {}, function (err) {
cb(err, packetInDb)
}.bind(this));
}.bind(this))
return this
}
/**
* get a packet from the store.
*/
Store.prototype.get = function (packet, cb) {
cb = cb || noop
this.db.findOne({ _id: packet.messageId }, function (err, packet) {
if (packet) {
cb(null, packet.packet)
} else {
cb(err || new Error('missing packet'))
}
});
return this
}
/**
* Close the store
*/
Store.prototype.close = function (cb) {
cb = cb || noop
cb()
return this
}
var Manager = function (path, options) {
if (!(this instanceof Manager)) {
return new Manager(path, options);
}
if ('object' === typeof path) {
options = path
path = null
}
if (!path) {
path = require('path').join(require('os').homedir(), '.mqtt-nedb-store')
try {
require('fs').mkdirSync(path)
} catch (e) {
}
}
var incomingOptions = (options && options.incoming) || {}
incomingOptions.filename = require('path').join(path, 'incoming')
this.incoming = new Store(incomingOptions)
var outgoingOptions = (options && options.outgoing) || {}
outgoingOptions.filename = require('path').join(path, 'outgoing')
this.outgoing = new Store(outgoingOptions)
}
Manager.single = Store;
Manager.prototype.close = function (done) {
this.incoming.close();
this.outgoing.close();
done && done() // jshint ignore:line
};
module.exports = Manager;