2019-08-27 21:11:49 +00:00
|
|
|
'use strict'
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const EventEmitter = require('events').EventEmitter
|
|
|
|
const util = require('util')
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const assert = require('assert-plus')
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
// var dn = require('../dn')
|
|
|
|
// var messages = require('../messages/index')
|
|
|
|
// var Protocol = require('../protocol')
|
2020-10-31 21:07:32 +00:00
|
|
|
const PagedControl = require('../controls/paged_results_control.js')
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- API
|
2014-07-24 20:41:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler object for paged search operations.
|
|
|
|
*
|
|
|
|
* Provided to consumers in place of the normal search EventEmitter it adds the
|
|
|
|
* following new events:
|
|
|
|
* 1. page - Emitted whenever the end of a result page is encountered.
|
|
|
|
* If this is the last page, 'end' will also be emitted.
|
|
|
|
* The event passes two arguments:
|
|
|
|
* 1. The result object (similar to 'end')
|
|
|
|
* 2. A callback function optionally used to continue the search
|
|
|
|
* operation if the pagePause option was specified during
|
|
|
|
* initialization.
|
|
|
|
* 2. pageError - Emitted if the server does not support paged search results
|
|
|
|
* If there are no listeners for this event, the 'error' event
|
|
|
|
* will be emitted (and 'end' will not be). By listening to
|
|
|
|
* 'pageError', a successful search that lacks paging will be
|
|
|
|
* able to emit 'end'.
|
|
|
|
* 3. search - Emitted as an internal event to trigger another client search.
|
|
|
|
*/
|
2019-08-27 21:11:49 +00:00
|
|
|
function SearchPager (opts) {
|
|
|
|
assert.object(opts)
|
|
|
|
assert.func(opts.callback)
|
|
|
|
assert.number(opts.pageSize)
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
EventEmitter.call(this, {})
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.callback = opts.callback
|
|
|
|
this.controls = opts.controls
|
|
|
|
this.pageSize = opts.pageSize
|
|
|
|
this.pagePause = opts.pagePause
|
2014-07-24 20:41:24 +00:00
|
|
|
|
|
|
|
this.controls.forEach(function (control) {
|
|
|
|
if (control.type === PagedControl.OID) {
|
|
|
|
// The point of using SearchPager is not having to do this.
|
|
|
|
// Toss an error if the pagedResultsControl is present
|
2019-08-27 21:11:49 +00:00
|
|
|
throw new Error('redundant pagedResultControl')
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
this.finished = false
|
|
|
|
this.started = false
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const emitter = new EventEmitter()
|
2019-08-27 21:11:49 +00:00
|
|
|
emitter.on('searchEntry', this.emit.bind(this, 'searchEntry'))
|
|
|
|
emitter.on('end', this._onEnd.bind(this))
|
|
|
|
emitter.on('error', this._onError.bind(this))
|
|
|
|
this.childEmitter = emitter
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(SearchPager, EventEmitter)
|
|
|
|
module.exports = SearchPager
|
2014-07-24 20:41:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the paged search.
|
|
|
|
*/
|
2019-08-27 21:11:49 +00:00
|
|
|
SearchPager.prototype.begin = function begin () {
|
2014-07-24 20:41:24 +00:00
|
|
|
// Starting first page
|
2019-08-27 21:11:49 +00:00
|
|
|
this._nextPage(null)
|
|
|
|
}
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
SearchPager.prototype._onEnd = function _onEnd (res) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const self = this
|
|
|
|
let cookie = null
|
2014-07-24 20:41:24 +00:00
|
|
|
res.controls.forEach(function (control) {
|
|
|
|
if (control.type === PagedControl.OID) {
|
2019-08-27 21:11:49 +00:00
|
|
|
cookie = control.value.cookie
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-25 18:21:02 +00:00
|
|
|
// Pass a noop callback by default for page events
|
2020-10-31 21:07:32 +00:00
|
|
|
const nullCb = function () { }
|
2014-07-25 18:21:02 +00:00
|
|
|
|
2014-07-24 20:41:24 +00:00
|
|
|
if (cookie === null) {
|
|
|
|
// paged search not supported
|
2019-08-27 21:11:49 +00:00
|
|
|
this.finished = true
|
|
|
|
this.emit('page', res, nullCb)
|
2020-10-31 21:07:32 +00:00
|
|
|
const err = new Error('missing paged control')
|
2019-08-27 21:11:49 +00:00
|
|
|
err.name = 'PagedError'
|
2014-07-25 19:16:31 +00:00
|
|
|
if (this.listeners('pageError').length > 0) {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.emit('pageError', err)
|
2014-07-24 20:41:24 +00:00
|
|
|
// If the consumer as subscribed to pageError, SearchPager is absolved
|
|
|
|
// from deliverying the fault via the 'error' event. Emitting an 'end'
|
|
|
|
// event after 'error' breaks the contract that the standard client
|
|
|
|
// provides, so it's only a possibility if 'pageError' is used instead.
|
2019-08-27 21:11:49 +00:00
|
|
|
this.emit('end', res)
|
2014-07-24 20:41:24 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.emit('error', err)
|
2014-07-24 20:41:24 +00:00
|
|
|
// No end event possible per explaination above.
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
return
|
2014-07-25 18:21:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cookie.length === 0) {
|
2014-07-24 20:41:24 +00:00
|
|
|
// end of paged results
|
2019-08-27 21:11:49 +00:00
|
|
|
this.finished = true
|
|
|
|
this.emit('page', nullCb)
|
|
|
|
this.emit('end', res)
|
2014-07-24 20:41:24 +00:00
|
|
|
} else {
|
|
|
|
if (this.pagePause) {
|
|
|
|
// Wait to fetch next page until callback is invoked
|
|
|
|
// Halt page fetching if called with error
|
|
|
|
this.emit('page', res, function (err) {
|
|
|
|
if (!err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
self._nextPage(cookie)
|
2014-07-24 20:41:24 +00:00
|
|
|
} else {
|
|
|
|
// the paged search has been canceled so emit an end
|
2019-08-27 21:11:49 +00:00
|
|
|
self.emit('end', res)
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2014-07-24 20:41:24 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.emit('page', res, nullCb)
|
|
|
|
this._nextPage(cookie)
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|
2014-07-24 20:41:24 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
SearchPager.prototype._onError = function _onError (err) {
|
|
|
|
this.finished = true
|
|
|
|
this.emit('error', err)
|
|
|
|
}
|
2014-07-24 20:41:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiate a search for the next page using the returned cookie value.
|
|
|
|
*/
|
2019-08-27 21:11:49 +00:00
|
|
|
SearchPager.prototype._nextPage = function _nextPage (cookie) {
|
2020-10-31 21:07:32 +00:00
|
|
|
const controls = this.controls.slice(0)
|
2014-07-24 20:41:24 +00:00
|
|
|
controls.push(new PagedControl({
|
|
|
|
value: {
|
|
|
|
size: this.pageSize,
|
|
|
|
cookie: cookie
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}))
|
2014-07-24 20:41:24 +00:00
|
|
|
|
|
|
|
this.emit('search', controls, this.childEmitter,
|
2019-08-27 21:11:49 +00:00
|
|
|
this._sendCallback.bind(this))
|
|
|
|
}
|
2014-07-24 20:41:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback provided to the client API for successful transmission.
|
|
|
|
*/
|
2019-08-27 21:11:49 +00:00
|
|
|
SearchPager.prototype._sendCallback = function _sendCallback (err, res) {
|
2014-07-24 20:41:24 +00:00
|
|
|
if (err) {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.finished = true
|
2014-07-24 20:41:24 +00:00
|
|
|
if (!this.started) {
|
|
|
|
// EmitSend error during the first page, bail via callback
|
2019-08-27 21:11:49 +00:00
|
|
|
this.callback(err, null)
|
2014-07-24 20:41:24 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.emit('error', err)
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// search successfully send
|
|
|
|
if (!this.started) {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.started = true
|
2014-07-24 20:41:24 +00:00
|
|
|
// send self as emitter as the client would
|
2019-08-27 21:11:49 +00:00
|
|
|
this.callback(null, this)
|
2014-07-24 20:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|