2020-10-31 21:07:32 +00:00
|
|
|
const assert = require('assert-plus')
|
|
|
|
const util = require('util')
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const asn1 = require('asn1')
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const Control = require('./control')
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Globals
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const BerReader = asn1.BerReader
|
|
|
|
const BerWriter = asn1.BerWriter
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- API
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function PagedResultsControl (options) {
|
|
|
|
assert.optionalObject(options)
|
|
|
|
options = options || {}
|
|
|
|
options.type = PagedResultsControl.OID
|
2012-03-26 18:53:30 +00:00
|
|
|
if (options.value) {
|
|
|
|
if (Buffer.isBuffer(options.value)) {
|
2019-08-27 21:11:49 +00:00
|
|
|
this.parse(options.value)
|
2012-03-26 18:53:30 +00:00
|
|
|
} else if (typeof (options.value) === 'object') {
|
2019-08-27 21:11:49 +00:00
|
|
|
this._value = options.value
|
2012-03-26 18:53:30 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
throw new TypeError('options.value must be a Buffer or Object')
|
2012-03-26 18:53:30 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
options.value = null
|
2012-03-26 18:53:30 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
Control.call(this, options)
|
2012-03-26 18:53:30 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
util.inherits(PagedResultsControl, Control)
|
2015-11-12 04:36:21 +00:00
|
|
|
Object.defineProperties(PagedResultsControl.prototype, {
|
|
|
|
value: {
|
2019-08-27 21:11:49 +00:00
|
|
|
get: function () { return this._value || {} },
|
2015-11-12 04:36:21 +00:00
|
|
|
configurable: false
|
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
PagedResultsControl.prototype.parse = function parse (buffer) {
|
|
|
|
assert.ok(buffer)
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const ber = new BerReader(buffer)
|
2012-03-26 18:53:30 +00:00
|
|
|
if (ber.readSequence()) {
|
2019-08-27 21:11:49 +00:00
|
|
|
this._value = {}
|
|
|
|
this._value.size = ber.readInt()
|
|
|
|
this._value.cookie = ber.readString(asn1.Ber.OctetString, true)
|
|
|
|
// readString returns '' instead of a zero-length buffer
|
|
|
|
if (!this._value.cookie) { this._value.cookie = Buffer.alloc(0) }
|
|
|
|
|
|
|
|
return true
|
2012-03-26 18:53:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
return false
|
|
|
|
}
|
2012-03-26 18:53:30 +00:00
|
|
|
|
|
|
|
PagedResultsControl.prototype._toBer = function (ber) {
|
2019-08-27 21:11:49 +00:00
|
|
|
assert.ok(ber)
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
if (!this._value) { return }
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const writer = new BerWriter()
|
2019-08-27 21:11:49 +00:00
|
|
|
writer.startSequence()
|
|
|
|
writer.writeInt(this.value.size)
|
2012-04-27 03:23:43 +00:00
|
|
|
if (this.value.cookie && this.value.cookie.length > 0) {
|
2019-08-27 21:11:49 +00:00
|
|
|
writer.writeBuffer(this.value.cookie, asn1.Ber.OctetString)
|
2012-04-27 03:23:43 +00:00
|
|
|
} else {
|
2019-08-27 21:11:49 +00:00
|
|
|
writer.writeString('') // writeBuffer rejects zero-length buffers
|
2012-04-27 03:23:43 +00:00
|
|
|
}
|
2019-08-27 21:11:49 +00:00
|
|
|
writer.endSequence()
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
ber.writeBuffer(writer.buffer, 0x04)
|
|
|
|
}
|
2012-03-26 18:53:30 +00:00
|
|
|
|
|
|
|
PagedResultsControl.prototype._json = function (obj) {
|
2019-08-27 21:11:49 +00:00
|
|
|
obj.controlValue = this.value
|
|
|
|
return obj
|
|
|
|
}
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
PagedResultsControl.OID = '1.2.840.113556.1.4.319'
|
2012-03-26 18:53:30 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Exports
|
|
|
|
module.exports = PagedResultsControl
|