Clean up asserts and getters in controls

This commit is contained in:
Patrick Mooney 2015-11-11 22:36:21 -06:00
parent 69c72dc45f
commit 540a3a1f5c
8 changed files with 108 additions and 144 deletions

View File

@ -1,6 +1,6 @@
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
var assert = require('assert');
var assert = require('assert-plus');
var util = require('util');
var asn1 = require('asn1');
@ -8,48 +8,41 @@ var asn1 = require('asn1');
var Protocol = require('../protocol');
///--- Globals
var Ber = asn1.Ber;
///--- API
function Control(options) {
if (options) {
if (typeof (options) !== 'object')
throw new TypeError('options must be an object');
if (options.type && typeof (options.type) !== 'string')
throw new TypeError('options.type must be a string');
if (options.criticality !== undefined &&
typeof (options.criticality) !== 'boolean')
throw new TypeError('options.criticality must be a boolean');
if (options.value && !Buffer.isBuffer(options.value))
throw new TypeError('options.value must be a Buffer');
} else {
options = {};
assert.optionalObject(options);
options = options || {};
assert.optionalString(options.type);
assert.optionalBool(options.criticality);
if (options.value) {
console.log(assert.buffer.toString());
assert.buffer(options.value);
}
this.type = options.type || '';
this.criticality = options.critical || options.criticality || false;
this.value = options.value || null;
var self = this;
this.__defineGetter__('json', function () {
var obj = {
controlType: self.type,
criticality: self.criticality,
controlValue: self.value
};
return (typeof (self._json) === 'function' ? self._json(obj) : obj);
});
}
module.exports = Control;
Object.defineProperties(Control.prototype, {
json: {
get: function getJson() {
var obj = {
controlType: this.type,
criticality: this.criticality,
controlValue: this.value
};
return (typeof (this._json) === 'function' ? this._json(obj) : obj);
}
}
});
Control.prototype.toBer = function (ber) {
Control.prototype.toBer = function toBer(ber) {
assert.ok(ber);
ber.startSequence();
@ -66,7 +59,10 @@ Control.prototype.toBer = function (ber) {
return;
};
Control.prototype.toString = function () {
Control.prototype.toString = function toString() {
return this.json;
};
///--- Exports
module.exports = Control;

View File

@ -1,4 +1,4 @@
var assert = require('assert');
var assert = require('assert-plus');
var util = require('util');
var asn1 = require('asn1');
@ -6,20 +6,17 @@ var asn1 = require('asn1');
var Control = require('./control');
///--- Globals
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
///--- API
function EntryChangeNotificationControl(options) {
if (!options)
options = {};
assert.optionalObject(options);
options = options || {};
options.type = EntryChangeNotificationControl.OID;
if (options.value) {
if (Buffer.isBuffer(options.value)) {
@ -32,15 +29,14 @@ function EntryChangeNotificationControl(options) {
options.value = null;
}
Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || {};
});
}
util.inherits(EntryChangeNotificationControl, Control);
module.exports = EntryChangeNotificationControl;
Object.defineProperties(EntryChangeNotificationControl.prototype, {
value: {
get: function () { return this._value || {}; },
configurable: false
}
});
EntryChangeNotificationControl.prototype.parse = function parse(buffer) {
assert.ok(buffer);
@ -63,7 +59,6 @@ EntryChangeNotificationControl.prototype.parse = function parse(buffer) {
return false;
};
EntryChangeNotificationControl.prototype._toBer = function (ber) {
assert.ok(ber);
@ -82,12 +77,13 @@ EntryChangeNotificationControl.prototype._toBer = function (ber) {
ber.writeBuffer(writer.buffer, 0x04);
};
EntryChangeNotificationControl.prototype._json = function (obj) {
obj.controlValue = this.value;
return obj;
};
EntryChangeNotificationControl.OID = '2.16.840.1.113730.3.4.7';
///--- Exports
module.exports = EntryChangeNotificationControl;

View File

@ -26,8 +26,10 @@ module.exports = {
return null;
var type;
var critical = false;
var value;
var opts = {
criticality: false,
value: null
};
if (ber.length) {
var end = ber.offset + ber.length;
@ -35,51 +37,33 @@ module.exports = {
type = ber.readString();
if (ber.offset < end) {
if (ber.peek() === Ber.Boolean)
critical = ber.readBoolean();
opts.criticality = ber.readBoolean();
}
if (ber.offset < end)
value = ber.readString(Ber.OctetString, true);
opts.value = ber.readString(Ber.OctetString, true);
}
var control;
switch (type) {
case PersistentSearchControl.OID:
control = new PersistentSearchControl({
critical: critical,
value: value
});
control = new PersistentSearchControl(opts);
break;
case EntryChangeNotificationControl.OID:
control = new EntryChangeNotificationControl({
critical: critical,
value: value
});
control = new EntryChangeNotificationControl(opts);
break;
case PagedResultsControl.OID:
control = new PagedResultsControl({
critical: critical,
value: value
});
control = new PagedResultsControl(opts);
break;
case ServerSideSortingRequestControl.OID:
control = new ServerSideSortingRequestControl({
critical: critical,
value: value
});
control = new ServerSideSortingRequestControl(opts);
break;
case ServerSideSortingResponseControl.OID:
control = new ServerSideSortingResponseControl({
critical: critical,
value: value
});
control = new ServerSideSortingResponseControl(opts);
break;
default:
control = new Control({
type: type,
critical: critical,
value: value
});
opts.type = type;
control = new Control(opts);
break;
}

View File

@ -1,4 +1,4 @@
var assert = require('assert');
var assert = require('assert-plus');
var util = require('util');
var asn1 = require('asn1');
@ -6,20 +6,17 @@ var asn1 = require('asn1');
var Control = require('./control');
///--- Globals
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
///--- API
function PagedResultsControl(options) {
if (!options)
options = {};
assert.optionalObject(options);
options = options || {};
options.type = PagedResultsControl.OID;
if (options.value) {
if (Buffer.isBuffer(options.value)) {
@ -32,15 +29,14 @@ function PagedResultsControl(options) {
options.value = null;
}
Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || {};
});
}
util.inherits(PagedResultsControl, Control);
module.exports = PagedResultsControl;
Object.defineProperties(PagedResultsControl.prototype, {
value: {
get: function () { return this._value || {}; },
configurable: false
}
});
PagedResultsControl.prototype.parse = function parse(buffer) {
assert.ok(buffer);
@ -60,7 +56,6 @@ PagedResultsControl.prototype.parse = function parse(buffer) {
return false;
};
PagedResultsControl.prototype._toBer = function (ber) {
assert.ok(ber);
@ -80,12 +75,13 @@ PagedResultsControl.prototype._toBer = function (ber) {
ber.writeBuffer(writer.buffer, 0x04);
};
PagedResultsControl.prototype._json = function (obj) {
obj.controlValue = this.value;
return obj;
};
PagedResultsControl.OID = '1.2.840.113556.1.4.319';
///--- Exports
module.exports = PagedResultsControl;

View File

@ -1,6 +1,6 @@
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
var assert = require('assert');
var assert = require('assert-plus');
var util = require('util');
var asn1 = require('asn1');
@ -8,21 +8,19 @@ var asn1 = require('asn1');
var Control = require('./control');
///--- Globals
var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter;
///--- API
function PersistentSearchControl(options) {
if (!options)
options = {};
assert.optionalObject(options);
options = options || {};
options.type = PersistentSearchControl.OID;
if (options.value) {
if (Buffer.isBuffer(options.value)) {
this.parse(options.value);
@ -34,15 +32,14 @@ function PersistentSearchControl(options) {
options.value = null;
}
Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || {};
});
}
util.inherits(PersistentSearchControl, Control);
module.exports = PersistentSearchControl;
Object.defineProperties(PersistentSearchControl.prototype, {
value: {
get: function () { return this._value || {}; },
configurable: false
}
});
PersistentSearchControl.prototype.parse = function parse(buffer) {
assert.ok(buffer);
@ -61,7 +58,6 @@ PersistentSearchControl.prototype.parse = function parse(buffer) {
return false;
};
PersistentSearchControl.prototype._toBer = function (ber) {
assert.ok(ber);
@ -78,12 +74,12 @@ PersistentSearchControl.prototype._toBer = function (ber) {
ber.writeBuffer(writer.buffer, 0x04);
};
PersistentSearchControl.prototype._json = function (obj) {
obj.controlValue = this.value;
return obj;
};
PersistentSearchControl.OID = '2.16.840.1.113730.3.4.3';
///--- Exports
module.exports = PersistentSearchControl;

View File

@ -1,4 +1,4 @@
var assert = require('assert');
var assert = require('assert-plus');
var util = require('util');
var asn1 = require('asn1');
@ -6,7 +6,6 @@ var asn1 = require('asn1');
var Control = require('./control');
///--- Globals
var BerReader = asn1.BerReader;
@ -16,18 +15,16 @@ var BerWriter = asn1.BerWriter;
///--- API
function ServerSideSortingRequestControl(options) {
if (!options)
options = {};
assert.optionalObject(options);
options = options || {};
options.type = ServerSideSortingRequestControl.OID;
if (options.value) {
if (Buffer.isBuffer(options.value)) {
this.parse(options.value);
} else if (Array.isArray(options.value)) {
assert.arrayOfObject(options.value, 'options.value must be Objects');
for (var i = 0; i < options.value.length; i++) {
if (typeof (options.value[i]) !== 'object') {
throw new TypeError('Elements of options.value must be Objects');
} else if (!options.value[i].hasOwnProperty('attributeType')) {
if (!options.value[i].hasOwnProperty('attributeType')) {
throw new Error('Missing required key: attributeType');
}
}
@ -43,15 +40,14 @@ function ServerSideSortingRequestControl(options) {
options.value = null;
}
Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || [];
});
}
util.inherits(ServerSideSortingRequestControl, Control);
module.exports = ServerSideSortingRequestControl;
Object.defineProperties(ServerSideSortingRequestControl.prototype, {
value: {
get: function () { return this._value || []; },
configurable: false
}
});
ServerSideSortingRequestControl.prototype.parse = function parse(buffer) {
assert.ok(buffer);
@ -77,7 +73,6 @@ ServerSideSortingRequestControl.prototype.parse = function parse(buffer) {
return false;
};
ServerSideSortingRequestControl.prototype._toBer = function (ber) {
assert.ok(ber);
@ -104,11 +99,14 @@ ServerSideSortingRequestControl.prototype._toBer = function (ber) {
ber.writeBuffer(writer.buffer, 0x04);
};
ServerSideSortingRequestControl.prototype._json = function (obj) {
obj.controlValue = this.value;
return obj;
};
ServerSideSortingRequestControl.OID = '1.2.840.113556.1.4.473';
///---Exports
module.exports = ServerSideSortingRequestControl;

View File

@ -1,14 +1,12 @@
var assert = require('assert');
var assert = require('assert-plus');
var util = require('util');
var asn1 = require('asn1');
var Control = require('./control');
var CODES = require('../errors/codes');
///--- Globals
var BerReader = asn1.BerReader;
@ -29,9 +27,8 @@ var VALID_CODES = [
];
function ServerSideSortingResponseControl(options) {
if (!options)
options = {};
assert.optionalObject(options);
options = options || {};
options.type = ServerSideSortingResponseControl.OID;
options.criticality = false;
@ -54,15 +51,14 @@ function ServerSideSortingResponseControl(options) {
options.value = null;
}
Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || {};
});
}
util.inherits(ServerSideSortingResponseControl, Control);
module.exports = ServerSideSortingResponseControl;
Object.defineProperties(ServerSideSortingResponseControl.prototype, {
value: {
get: function () { return this._value || {}; },
configurable: false
}
});
ServerSideSortingResponseControl.prototype.parse = function parse(buffer) {
assert.ok(buffer);
@ -79,7 +75,6 @@ ServerSideSortingResponseControl.prototype.parse = function parse(buffer) {
return false;
};
ServerSideSortingResponseControl.prototype._toBer = function (ber) {
assert.ok(ber);
@ -96,10 +91,13 @@ ServerSideSortingResponseControl.prototype._toBer = function (ber) {
ber.writeBuffer(writer.buffer, 0x04);
};
ServerSideSortingResponseControl.prototype._json = function (obj) {
obj.controlValue = this.value;
return obj;
};
ServerSideSortingResponseControl.OID = '1.2.840.113556.1.4.474';
///--- Exports
module.exports = ServerSideSortingResponseControl;

View File

@ -30,7 +30,7 @@
},
"dependencies": {
"asn1": "0.2.3",
"assert-plus": "0.1.5",
"assert-plus": "0.2.0",
"bunyan": "1.5.1",
"dashdash": "1.10.1",
"backoff": "2.4.1",