UNPKG

1.91 kBJavaScriptView Raw
1// Copyright 2011 Mark Cavage, Inc. All rights reserved.
2
3var assert = require('assert-plus');
4var util = require('util');
5
6var asn1 = require('asn1');
7
8var Control = require('./control');
9
10
11///--- Globals
12
13var BerReader = asn1.BerReader;
14var BerWriter = asn1.BerWriter;
15
16
17///--- API
18
19function PersistentSearchControl(options) {
20 assert.optionalObject(options);
21 options = options || {};
22 options.type = PersistentSearchControl.OID;
23
24 if (options.value) {
25 if (Buffer.isBuffer(options.value)) {
26 this.parse(options.value);
27 } else if (typeof (options.value) === 'object') {
28 this._value = options.value;
29 } else {
30 throw new TypeError('options.value must be a Buffer or Object');
31 }
32 options.value = null;
33 }
34 Control.call(this, options);
35}
36util.inherits(PersistentSearchControl, Control);
37Object.defineProperties(PersistentSearchControl.prototype, {
38 value: {
39 get: function () { return this._value || {}; },
40 configurable: false
41 }
42});
43
44PersistentSearchControl.prototype.parse = function parse(buffer) {
45 assert.ok(buffer);
46
47 var ber = new BerReader(buffer);
48 if (ber.readSequence()) {
49 this._value = {
50 changeTypes: ber.readInt(),
51 changesOnly: ber.readBoolean(),
52 returnECs: ber.readBoolean()
53 };
54
55 return true;
56 }
57
58 return false;
59};
60
61PersistentSearchControl.prototype._toBer = function (ber) {
62 assert.ok(ber);
63
64 if (!this._value)
65 return;
66
67 var writer = new BerWriter();
68 writer.startSequence();
69 writer.writeInt(this.value.changeTypes);
70 writer.writeBoolean(this.value.changesOnly);
71 writer.writeBoolean(this.value.returnECs);
72 writer.endSequence();
73
74 ber.writeBuffer(writer.buffer, 0x04);
75};
76
77PersistentSearchControl.prototype._json = function (obj) {
78 obj.controlValue = this.value;
79 return obj;
80};
81
82PersistentSearchControl.OID = '2.16.840.1.113730.3.4.3';
83
84///--- Exports
85module.exports = PersistentSearchControl;