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. // Copyright 2011 Mark Cavage, Inc. All rights reserved.
var assert = require('assert'); var assert = require('assert-plus');
var util = require('util'); var util = require('util');
var asn1 = require('asn1'); var asn1 = require('asn1');
@ -8,48 +8,41 @@ var asn1 = require('asn1');
var Protocol = require('../protocol'); var Protocol = require('../protocol');
///--- Globals ///--- Globals
var Ber = asn1.Ber; var Ber = asn1.Ber;
///--- API ///--- API
function Control(options) { function Control(options) {
if (options) { assert.optionalObject(options);
if (typeof (options) !== 'object') options = options || {};
throw new TypeError('options must be an object'); assert.optionalString(options.type);
if (options.type && typeof (options.type) !== 'string') assert.optionalBool(options.criticality);
throw new TypeError('options.type must be a string'); if (options.value) {
if (options.criticality !== undefined && console.log(assert.buffer.toString());
typeof (options.criticality) !== 'boolean') assert.buffer(options.value);
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 = {};
} }
this.type = options.type || ''; this.type = options.type || '';
this.criticality = options.critical || options.criticality || false; this.criticality = options.critical || options.criticality || false;
this.value = options.value || null; 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 toBer(ber) {
Control.prototype.toBer = function (ber) {
assert.ok(ber); assert.ok(ber);
ber.startSequence(); ber.startSequence();
@ -66,7 +59,10 @@ Control.prototype.toBer = function (ber) {
return; return;
}; };
Control.prototype.toString = function toString() {
Control.prototype.toString = function () {
return this.json; 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 util = require('util');
var asn1 = require('asn1'); var asn1 = require('asn1');
@ -6,20 +6,17 @@ var asn1 = require('asn1');
var Control = require('./control'); var Control = require('./control');
///--- Globals ///--- Globals
var BerReader = asn1.BerReader; var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter; var BerWriter = asn1.BerWriter;
///--- API ///--- API
function EntryChangeNotificationControl(options) { function EntryChangeNotificationControl(options) {
if (!options) assert.optionalObject(options);
options = {}; options = options || {};
options.type = EntryChangeNotificationControl.OID; options.type = EntryChangeNotificationControl.OID;
if (options.value) { if (options.value) {
if (Buffer.isBuffer(options.value)) { if (Buffer.isBuffer(options.value)) {
@ -32,15 +29,14 @@ function EntryChangeNotificationControl(options) {
options.value = null; options.value = null;
} }
Control.call(this, options); Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || {};
});
} }
util.inherits(EntryChangeNotificationControl, Control); 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) { EntryChangeNotificationControl.prototype.parse = function parse(buffer) {
assert.ok(buffer); assert.ok(buffer);
@ -63,7 +59,6 @@ EntryChangeNotificationControl.prototype.parse = function parse(buffer) {
return false; return false;
}; };
EntryChangeNotificationControl.prototype._toBer = function (ber) { EntryChangeNotificationControl.prototype._toBer = function (ber) {
assert.ok(ber); assert.ok(ber);
@ -82,12 +77,13 @@ EntryChangeNotificationControl.prototype._toBer = function (ber) {
ber.writeBuffer(writer.buffer, 0x04); ber.writeBuffer(writer.buffer, 0x04);
}; };
EntryChangeNotificationControl.prototype._json = function (obj) { EntryChangeNotificationControl.prototype._json = function (obj) {
obj.controlValue = this.value; obj.controlValue = this.value;
return obj; return obj;
}; };
EntryChangeNotificationControl.OID = '2.16.840.1.113730.3.4.7'; EntryChangeNotificationControl.OID = '2.16.840.1.113730.3.4.7';
///--- Exports
module.exports = EntryChangeNotificationControl;

View File

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

View File

@ -1,4 +1,4 @@
var assert = require('assert'); var assert = require('assert-plus');
var util = require('util'); var util = require('util');
var asn1 = require('asn1'); var asn1 = require('asn1');
@ -6,20 +6,17 @@ var asn1 = require('asn1');
var Control = require('./control'); var Control = require('./control');
///--- Globals ///--- Globals
var BerReader = asn1.BerReader; var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter; var BerWriter = asn1.BerWriter;
///--- API ///--- API
function PagedResultsControl(options) { function PagedResultsControl(options) {
if (!options) assert.optionalObject(options);
options = {}; options = options || {};
options.type = PagedResultsControl.OID; options.type = PagedResultsControl.OID;
if (options.value) { if (options.value) {
if (Buffer.isBuffer(options.value)) { if (Buffer.isBuffer(options.value)) {
@ -32,15 +29,14 @@ function PagedResultsControl(options) {
options.value = null; options.value = null;
} }
Control.call(this, options); Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || {};
});
} }
util.inherits(PagedResultsControl, Control); 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) { PagedResultsControl.prototype.parse = function parse(buffer) {
assert.ok(buffer); assert.ok(buffer);
@ -60,7 +56,6 @@ PagedResultsControl.prototype.parse = function parse(buffer) {
return false; return false;
}; };
PagedResultsControl.prototype._toBer = function (ber) { PagedResultsControl.prototype._toBer = function (ber) {
assert.ok(ber); assert.ok(ber);
@ -80,12 +75,13 @@ PagedResultsControl.prototype._toBer = function (ber) {
ber.writeBuffer(writer.buffer, 0x04); ber.writeBuffer(writer.buffer, 0x04);
}; };
PagedResultsControl.prototype._json = function (obj) { PagedResultsControl.prototype._json = function (obj) {
obj.controlValue = this.value; obj.controlValue = this.value;
return obj; return obj;
}; };
PagedResultsControl.OID = '1.2.840.113556.1.4.319'; 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. // Copyright 2011 Mark Cavage, Inc. All rights reserved.
var assert = require('assert'); var assert = require('assert-plus');
var util = require('util'); var util = require('util');
var asn1 = require('asn1'); var asn1 = require('asn1');
@ -8,21 +8,19 @@ var asn1 = require('asn1');
var Control = require('./control'); var Control = require('./control');
///--- Globals ///--- Globals
var BerReader = asn1.BerReader; var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter; var BerWriter = asn1.BerWriter;
///--- API ///--- API
function PersistentSearchControl(options) { function PersistentSearchControl(options) {
if (!options) assert.optionalObject(options);
options = {}; options = options || {};
options.type = PersistentSearchControl.OID; options.type = PersistentSearchControl.OID;
if (options.value) { if (options.value) {
if (Buffer.isBuffer(options.value)) { if (Buffer.isBuffer(options.value)) {
this.parse(options.value); this.parse(options.value);
@ -34,15 +32,14 @@ function PersistentSearchControl(options) {
options.value = null; options.value = null;
} }
Control.call(this, options); Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || {};
});
} }
util.inherits(PersistentSearchControl, Control); 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) { PersistentSearchControl.prototype.parse = function parse(buffer) {
assert.ok(buffer); assert.ok(buffer);
@ -61,7 +58,6 @@ PersistentSearchControl.prototype.parse = function parse(buffer) {
return false; return false;
}; };
PersistentSearchControl.prototype._toBer = function (ber) { PersistentSearchControl.prototype._toBer = function (ber) {
assert.ok(ber); assert.ok(ber);
@ -78,12 +74,12 @@ PersistentSearchControl.prototype._toBer = function (ber) {
ber.writeBuffer(writer.buffer, 0x04); ber.writeBuffer(writer.buffer, 0x04);
}; };
PersistentSearchControl.prototype._json = function (obj) { PersistentSearchControl.prototype._json = function (obj) {
obj.controlValue = this.value; obj.controlValue = this.value;
return obj; return obj;
}; };
PersistentSearchControl.OID = '2.16.840.1.113730.3.4.3'; 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 util = require('util');
var asn1 = require('asn1'); var asn1 = require('asn1');
@ -6,7 +6,6 @@ var asn1 = require('asn1');
var Control = require('./control'); var Control = require('./control');
///--- Globals ///--- Globals
var BerReader = asn1.BerReader; var BerReader = asn1.BerReader;
@ -16,18 +15,16 @@ var BerWriter = asn1.BerWriter;
///--- API ///--- API
function ServerSideSortingRequestControl(options) { function ServerSideSortingRequestControl(options) {
if (!options) assert.optionalObject(options);
options = {}; options = options || {};
options.type = ServerSideSortingRequestControl.OID; options.type = ServerSideSortingRequestControl.OID;
if (options.value) { if (options.value) {
if (Buffer.isBuffer(options.value)) { if (Buffer.isBuffer(options.value)) {
this.parse(options.value); this.parse(options.value);
} else if (Array.isArray(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++) { for (var i = 0; i < options.value.length; i++) {
if (typeof (options.value[i]) !== 'object') { if (!options.value[i].hasOwnProperty('attributeType')) {
throw new TypeError('Elements of options.value must be Objects');
} else if (!options.value[i].hasOwnProperty('attributeType')) {
throw new Error('Missing required key: attributeType'); throw new Error('Missing required key: attributeType');
} }
} }
@ -43,15 +40,14 @@ function ServerSideSortingRequestControl(options) {
options.value = null; options.value = null;
} }
Control.call(this, options); Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || [];
});
} }
util.inherits(ServerSideSortingRequestControl, Control); 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) { ServerSideSortingRequestControl.prototype.parse = function parse(buffer) {
assert.ok(buffer); assert.ok(buffer);
@ -77,7 +73,6 @@ ServerSideSortingRequestControl.prototype.parse = function parse(buffer) {
return false; return false;
}; };
ServerSideSortingRequestControl.prototype._toBer = function (ber) { ServerSideSortingRequestControl.prototype._toBer = function (ber) {
assert.ok(ber); assert.ok(ber);
@ -104,11 +99,14 @@ ServerSideSortingRequestControl.prototype._toBer = function (ber) {
ber.writeBuffer(writer.buffer, 0x04); ber.writeBuffer(writer.buffer, 0x04);
}; };
ServerSideSortingRequestControl.prototype._json = function (obj) { ServerSideSortingRequestControl.prototype._json = function (obj) {
obj.controlValue = this.value; obj.controlValue = this.value;
return obj; return obj;
}; };
ServerSideSortingRequestControl.OID = '1.2.840.113556.1.4.473'; 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 util = require('util');
var asn1 = require('asn1'); var asn1 = require('asn1');
var Control = require('./control'); var Control = require('./control');
var CODES = require('../errors/codes'); var CODES = require('../errors/codes');
///--- Globals ///--- Globals
var BerReader = asn1.BerReader; var BerReader = asn1.BerReader;
@ -29,9 +27,8 @@ var VALID_CODES = [
]; ];
function ServerSideSortingResponseControl(options) { function ServerSideSortingResponseControl(options) {
if (!options) assert.optionalObject(options);
options = {}; options = options || {};
options.type = ServerSideSortingResponseControl.OID; options.type = ServerSideSortingResponseControl.OID;
options.criticality = false; options.criticality = false;
@ -54,15 +51,14 @@ function ServerSideSortingResponseControl(options) {
options.value = null; options.value = null;
} }
Control.call(this, options); Control.call(this, options);
var self = this;
this.__defineGetter__('value', function () {
return self._value || {};
});
} }
util.inherits(ServerSideSortingResponseControl, Control); 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) { ServerSideSortingResponseControl.prototype.parse = function parse(buffer) {
assert.ok(buffer); assert.ok(buffer);
@ -79,7 +75,6 @@ ServerSideSortingResponseControl.prototype.parse = function parse(buffer) {
return false; return false;
}; };
ServerSideSortingResponseControl.prototype._toBer = function (ber) { ServerSideSortingResponseControl.prototype._toBer = function (ber) {
assert.ok(ber); assert.ok(ber);
@ -96,10 +91,13 @@ ServerSideSortingResponseControl.prototype._toBer = function (ber) {
ber.writeBuffer(writer.buffer, 0x04); ber.writeBuffer(writer.buffer, 0x04);
}; };
ServerSideSortingResponseControl.prototype._json = function (obj) { ServerSideSortingResponseControl.prototype._json = function (obj) {
obj.controlValue = this.value; obj.controlValue = this.value;
return obj; return obj;
}; };
ServerSideSortingResponseControl.OID = '1.2.840.113556.1.4.474'; ServerSideSortingResponseControl.OID = '1.2.840.113556.1.4.474';
///--- Exports
module.exports = ServerSideSortingResponseControl;

View File

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