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