-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
363 lines (304 loc) · 8.17 KB
/
index.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
const Resource = require('nanoresource')
const toRegex = require('to-regex')
const Guard = require('nanoguard')
const Batch = require('batch')
const kResources = Symbol('resources')
/**
* The `POOL_CLOSED_ERR` is thrown when the `Pool` instance
* is used after being closed.
*/
class POOL_CLOSED_ERR extends Error {
constructor() {
super('Pool is closed.')
}
}
/**
* The `Pool` class represents a container of `NanoResource` instances
* that tracks the opening, closing, and active state of the resources
* added to it.
*/
class Pool {
/**
* `Pool` class constructor.
* @param {?(NanoResource)} Factory
* @param {?(Object)} opts
*/
constructor(Factory, opts) {
if (Factory && 'object' === typeof Factory) {
opts = Factory
Factory = null
}
if (!opts || 'object' !== typeof opts) {
opts = {}
}
this.guard = opts.guard || new Guard()
this.opened = false
this.closed = false
this.opening = false
this.closing = false
this.Factory = Factory || Resource
this.autoOpen = false !== opts.autoOpen
this[kResources] = new Set()
this.allowActive = opts.allowActive || false
if (this.autoOpen) {
process.nextTick(() => this.open())
}
}
/**
* Returns the number of open and active resources. Accumlates
* the size of any pools added
* @accessor
*/
get size() {
const resources = [ ...this[kResources] ]
const reduce = (total, r) => total + (r.size || 0)
return resources.reduce(reduce, this[kResources].size)
}
/**
* Returns the number of active resource handles.
* @accessor
*/
get actives() {
const resources = [ ...this[kResources] ]
const reduce = (total, r) => total + (r.actives || 0)
return resources.reduce(reduce, 0)
}
/**
* Returns a list of resources, optionally filtering out
* resources marked as "closed" or "closing".
* @param {?(Object)} opts
* @param {?(Boolean)} [opts.closed = false]
* @return {Array}
*/
list(opts) {
if (this.closed || this.closing) {
throw new POOL_CLOSED_ERR()
}
if (!opts || 'object' !== typeof opts) {
opts = {}
}
const resources = [ ...this[kResources] ]
return resources.filter(filter)
function filter(resource) {
return true === opts.closed
? true
: !resource.closed && !resource.closing
}
}
/**
* Queries list of resources in pool based on some
* comparable input given by a `where` clause object. Child resources
* added that are `Pool` instances will be queried with their results
* flatted into the query response list.
* @param {?(Object)} where
* @param {?(Object)} opts
*/
query(where, opts) {
if (this.closed || this.closing) {
throw new POOL_CLOSED_ERR()
}
where = Object.assign({}, where) // copy
// convert to set then to array to remove any duplicate results
return Array.from(new Set(
this.list(opts).map(map).reduce(reduce, []).filter(filter)
))
function map(item) {
if ('function' === typeof item.query) {
return item.query(where, opts)
} else {
return item
}
}
function reduce(reduced, resources) {
return reduced.concat(resources)
}
function filter(resource) {
const keys = Object.keys(where)
return Object.keys(where).every(every)
function every(key) {
let value = where[key]
if ('string' === typeof where[key]) {
value = value
.replace(/^\*/g, '.*')
.replace(/([a-z|A-Z|0-9|_|-|%|@]+)\*/g, '$1.*')
.replace(/\/\*/g, '.*')
.replace(/\\\\*/g, '.*')
}
if ('string' === typeof value || value instanceof RegExp) {
if (toRegex(value).test(resource[key])) {
return true
}
}
if (where[key] === resource[key]) {
return true
}
if ('function' === typeof value) {
if (true === value(resource[key], resource)) {
return true
}
}
return false
}
}
}
/**
* Waits for pool to be considered ready then calls `callback()`
* @param {Function} callback
*/
ready(callback) {
if ('function' !== typeof callback) {
callback = () => void 0
}
if (this.closed || this.closing) {
return process.nextTick(callback, new POOL_CLOSED_ERR())
}
const [ ...resources ] = this.list()
const ready = new Batch()
for (const resource of resources) {
if ('function' === typeof resource.ready) {
ready.push((next) => resource.ready(next))
}
}
ready.push((next) => this.guard.ready(next))
ready.end((err) => callback(err))
}
/**
* Adds a resource to the pool.
* @param {NanoResource} resource
*/
add(resource, opts) {
if (!opts || 'object' !== typeof opts) {
opts = {}
}
const { close } = resource
const resources = this[kResources]
const defaultAllowActive = Boolean(this.allowActive)
if (this.closed || this.closing) {
throw new POOL_CLOSED_ERR()
}
this.guard.wait()
resources.add(resource)
if (false === opts.autoOpen) {
const { open } = resource
resource.open = (callback) => {
return open.call(resource, (err) => {
if (err) {
resources.delete(resource)
}
this.guard.continue()
// istanbul ignore next
if ('function' === typeof callback) {
callback(err)
}
})
}
} else {
resource.open((err) => {
if (err) {
resources.delete(resource)
}
this.guard.continue()
})
}
return Object.assign(resource, {
close(allowActive, callback) {
if ('function' === typeof allowActive) {
callback = allowActive
allowActive = defaultAllowActive
}
if ('boolean' !== typeof allowActive) {
allowActive = defaultAllowActive
}
if ('function' !== typeof callback) {
callback = (err) => void err
}
return close.call(resource, allowActive, onclose)
function onclose(err) {
resources.delete(resource)
callback(err)
}
}
})
}
/**
* Acquire a new resource based on the pool factory.
* @param {...?(Mixed)} args
* @return {NanoResource}
*/
resource(...args) {
if (this.closed || this.closing) {
throw new POOL_CLOSED_ERR()
}
return this.add(new this.Factory(...args), {
autoOpen: Boolean(this.autoOpen)
})
}
/**
* Opens pool calling `callback` when opened ("ready").
* @param {?(Function)} callback
*/
open(callback) {
if ('function' !== typeof callback) {
callback = () => void 0
}
if (this.closed || this.closing) {
return process.nextTick(callback, new POOL_CLOSED_ERR())
}
if (this.opened) {
return process.nextTick(callback, null)
}
if (!this.opening) {
this.opening = true
this.guard.ready(() => {
this.opening = false
this.opened = true
})
}
this.guard.ready(callback)
}
/**
* Closes pool and all opened resources.
* @param {?(Boolean)} allowActive
* @param {?(Function)} callback
*/
close(allowActive, callback) {
if ('function' === typeof allowActive) {
callback = allowActive
allowActive = this.allowActive
}
if (this.closed || this.closing) {
return process.nextTick(callback, new POOL_CLOSED_ERR())
}
this.closing = true
const closing = new Batch()
process.nextTick(() => {
for (const resource of this[kResources]) {
closing.push((next) => resource.close(allowActive, next))
}
})
process.nextTick(() => {
closing.end((err) => {
this.closing = false
this.closed = true
process.nextTick(callback, err)
})
})
}
}
/**
* Default factory for creating `Pool` instances
* backed with `NanoResource`.
* @default
* @param {?(NanoResource)} Factory
* @param {?(Object)} opts
* @return {Pool}
*/
function createPool(...args) {
return new Pool(...args)
}
/**
* Module exports.
*/
module.exports = Object.assign(createPool, {
Pool
})