diff --git a/lib/assert.js b/lib/assert.js new file mode 100644 index 0000000..2dc693b --- /dev/null +++ b/lib/assert.js @@ -0,0 +1,61 @@ +// Copyright 2015 Joyent, Inc. + +var assert = require('assert'); +var util = require('util'); + +var isDN = require('./dn').DN.isDN; +var isAttribute = require('./attribute').isAttribute; + + +///--- Helpers + +// Copied from mcavage/node-assert-plus +function _assert(arg, type, name) { + name = name || type; + throw new assert.AssertionError({ + message: util.format('%s (%s) required', name, type), + actual: typeof (arg), + expected: type, + operator: '===', + stackStartFunction: _assert.caller + }); +} + + +///--- API + +function stringDN(input, name) { + if (isDN(input) || typeof (input) === 'string') + return; + _assert(input, 'DN or string', name); +} + +function optionalStringDN(input, name) { + if (input === undefined || isDN(input) || typeof (input) === 'string') + return; + _assert(input, 'DN or string', name); +} + +function optionalDN(input, name) { + if (input !== undefined && !isDN(input)) + _assert(input, 'DN', name); +} + +function optionalArrayOfAttribute(input, name) { + if (input === undefined) + return; + if (!Array.isArray(input) || + input.some(function (v) { return !isAttribute(v); })) { + _assert(input, 'array of Attribute', name); + } +} + + +///--- Exports + +module.exports = { + stringDN: stringDN, + optionalStringDN: optionalStringDN, + optionalDN: optionalDN, + optionalArrayOfAttribute: optionalArrayOfAttribute +}; diff --git a/lib/messages/abandon_request.js b/lib/messages/abandon_request.js index 5902b7e..461ce49 100644 --- a/lib/messages/abandon_request.js +++ b/lib/messages/abandon_request.js @@ -1,35 +1,31 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. -var assert = require('assert'); +var assert = require('assert-plus'); var util = require('util'); var LDAPMessage = require('./message'); var Protocol = require('../protocol'); - ///--- API function AbandonRequest(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - if (options.abandonID && typeof (options.abandonID) !== 'number') - throw new TypeError('abandonID must be a number'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); + assert.optionalNumber(options.abandonID); options.protocolOp = Protocol.LDAP_REQ_ABANDON; LDAPMessage.call(this, options); this.abandonID = options.abandonID || 0; - - this.__defineGetter__('type', function () { return 'AbandonRequest'; }); } util.inherits(AbandonRequest, LDAPMessage); -module.exports = AbandonRequest; - +Object.defineProperties(AbandonRequest.prototype, { + type: { + get: function getType() { return 'AbandonRequest'; }, + configurable: false + } +}); AbandonRequest.prototype._parse = function (ber, length) { assert.ok(ber); @@ -59,7 +55,6 @@ AbandonRequest.prototype._parse = function (ber, length) { return true; }; - AbandonRequest.prototype._toBer = function (ber) { assert.ok(ber); @@ -81,7 +76,6 @@ AbandonRequest.prototype._toBer = function (ber) { return ber; }; - AbandonRequest.prototype._json = function (j) { assert.ok(j); @@ -89,3 +83,8 @@ AbandonRequest.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = AbandonRequest; diff --git a/lib/messages/abandon_response.js b/lib/messages/abandon_response.js index 874d5c7..534c218 100644 --- a/lib/messages/abandon_response.js +++ b/lib/messages/abandon_response.js @@ -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 LDAPMessage = require('./result'); @@ -8,25 +8,29 @@ var Protocol = require('../protocol'); ///--- API -// Stub this out function AbandonResponse(options) { - if (!options) - options = {}; - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); + options = options || {}; + assert.object(options); options.protocolOp = 0; LDAPMessage.call(this, options); - this.__defineGetter__('type', function () { return 'AbandonResponse'; }); } util.inherits(AbandonResponse, LDAPMessage); -module.exports = AbandonResponse; - +Object.defineProperties(AbandonResponse.prototype, { + type: { + get: function getType() { return 'AbandonResponse'; }, + configurable: false + } +}); AbandonResponse.prototype.end = function (status) {}; - AbandonResponse.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = AbandonResponse; diff --git a/lib/messages/add_request.js b/lib/messages/add_request.js index eb93772..e1d4bdd 100644 --- a/lib/messages/add_request.js +++ b/lib/messages/add_request.js @@ -1,55 +1,39 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. -var assert = require('assert'); +var assert = require('assert-plus'); var util = require('util'); var LDAPMessage = require('./message'); -var dn = require('../dn'); var Attribute = require('../attribute'); var Protocol = require('../protocol'); - - - -///--- Globals - -var isDN = dn.DN.isDN; +var lassert = require('../assert'); ///--- API function AddRequest(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - if (options.entry && - !(isDN(options.entry) || typeof (options.entry) === 'string')) { - throw new TypeError('options.entry must be a DN or string'); - } - if (options.attributes) { - if (!Array.isArray(options.attributes)) - throw new TypeError('options.attributes must be [Attribute]'); - options.attributes.forEach(function (a) { - if (!Attribute.isAttribute(a)) - throw new TypeError('options.attributes must be [Attribute]'); - }); - } - } else { - options = {}; - } + options = options || {}; + assert.object(options); + lassert.optionalStringDN(options.entry); + lassert.optionalArrayOfAttribute(options.attributes); options.protocolOp = Protocol.LDAP_REQ_ADD; LDAPMessage.call(this, options); this.entry = options.entry || null; this.attributes = options.attributes ? options.attributes.slice(0) : []; - - var self = this; - this.__defineGetter__('type', function () { return 'AddRequest'; }); - this.__defineGetter__('_dn', function () { return self.entry; }); } util.inherits(AddRequest, LDAPMessage); -module.exports = AddRequest; - +Object.defineProperties(AddRequest.prototype, { + type: { + get: function getType() { return 'AddRequest'; }, + configurable: false + }, + _dn: { + get: function getDN() { return this.entry; }, + configurable: false + } +}); AddRequest.prototype._parse = function (ber) { assert.ok(ber); @@ -74,7 +58,6 @@ AddRequest.prototype._parse = function (ber) { return true; }; - AddRequest.prototype._toBer = function (ber) { assert.ok(ber); @@ -88,7 +71,6 @@ AddRequest.prototype._toBer = function (ber) { return ber; }; - AddRequest.prototype._json = function (j) { assert.ok(j); @@ -102,7 +84,6 @@ AddRequest.prototype._json = function (j) { return j; }; - AddRequest.prototype.indexOf = function (attr) { if (!attr || typeof (attr) !== 'string') throw new TypeError('attr (string) required'); @@ -115,7 +96,6 @@ AddRequest.prototype.indexOf = function (attr) { return -1; }; - AddRequest.prototype.attributeNames = function () { var attrs = []; @@ -125,7 +105,6 @@ AddRequest.prototype.attributeNames = function () { return attrs; }; - AddRequest.prototype.getAttribute = function (name) { if (!name || typeof (name) !== 'string') throw new TypeError('attribute name (string) required'); @@ -140,7 +119,6 @@ AddRequest.prototype.getAttribute = function (name) { return null; }; - AddRequest.prototype.addAttribute = function (attr) { if (!(attr instanceof Attribute)) throw new TypeError('attribute (Attribute) required'); @@ -148,7 +126,6 @@ AddRequest.prototype.addAttribute = function (attr) { return this.attributes.push(attr); }; - /** * Returns a "pure" JS representation of this object. * @@ -187,3 +164,8 @@ AddRequest.prototype.toObject = function () { return obj; }; + + +///--- Exports + +module.exports = AddRequest; diff --git a/lib/messages/add_response.js b/lib/messages/add_response.js index b5ac773..2163552 100644 --- a/lib/messages/add_response.js +++ b/lib/messages/add_response.js @@ -1,24 +1,24 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. +var assert = require('assert-plus'); var util = require('util'); var LDAPResult = require('./result'); var Protocol = require('../protocol'); - ///--- API function AddResponse(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REP_ADD; LDAPResult.call(this, options); } util.inherits(AddResponse, LDAPResult); + + +///--- Exports + module.exports = AddResponse; diff --git a/lib/messages/bind_request.js b/lib/messages/bind_request.js index a676aeb..ca99ddf 100644 --- a/lib/messages/bind_request.js +++ b/lib/messages/bind_request.js @@ -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,6 +8,7 @@ var asn1 = require('asn1'); var LDAPMessage = require('./message'); var Protocol = require('../protocol'); + ///--- Globals var Ber = asn1.Ber; @@ -15,14 +16,11 @@ var LDAP_BIND_SIMPLE = 'simple'; var LDAP_BIND_SASL = 'sasl'; - ///--- API function BindRequest(options) { - if (options && typeof (options) !== 'object') - throw new TypeError('options must be an object'); - options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REQ_BIND; LDAPMessage.call(this, options); @@ -31,14 +29,18 @@ function BindRequest(options) { this.name = options.name || null; this.authentication = options.authentication || LDAP_BIND_SIMPLE; this.credentials = options.credentials || ''; - - var self = this; - this.__defineGetter__('type', function () { return 'BindRequest'; }); - this.__defineGetter__('_dn', function () { return self.name; }); } util.inherits(BindRequest, LDAPMessage); -module.exports = BindRequest; - +Object.defineProperties(BindRequest.prototype, { + type: { + get: function getType() { return 'BindRequest'; }, + configurable: false + }, + _dn: { + get: function getDN() { return this.name; }, + configurable: false + } +}); BindRequest.prototype._parse = function (ber) { assert.ok(ber); @@ -58,7 +60,6 @@ BindRequest.prototype._parse = function (ber) { return true; }; - BindRequest.prototype._toBer = function (ber) { assert.ok(ber); @@ -70,7 +71,6 @@ BindRequest.prototype._toBer = function (ber) { return ber; }; - BindRequest.prototype._json = function (j) { assert.ok(j); @@ -81,3 +81,8 @@ BindRequest.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = BindRequest; diff --git a/lib/messages/bind_response.js b/lib/messages/bind_response.js index edb0a71..8542613 100644 --- a/lib/messages/bind_response.js +++ b/lib/messages/bind_response.js @@ -1,24 +1,24 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. +var assert = require('assert-plus'); var util = require('util'); var LDAPResult = require('./result'); var Protocol = require('../protocol'); - ///--- API function BindResponse(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REP_BIND; LDAPResult.call(this, options); } util.inherits(BindResponse, LDAPResult); + + +///--- Exports + module.exports = BindResponse; diff --git a/lib/messages/compare_request.js b/lib/messages/compare_request.js index 318ca9b..9b7e2fe 100644 --- a/lib/messages/compare_request.js +++ b/lib/messages/compare_request.js @@ -1,36 +1,21 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. -var assert = require('assert'); +var assert = require('assert-plus'); var util = require('util'); var LDAPMessage = require('./message'); -var dn = require('../dn'); var Protocol = require('../protocol'); - - - -///--- Globals - -var isDN = dn.DN.isDN; +var lassert = require('../assert'); ///--- API function CompareRequest(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - if (options.entry && - !(isDN(options.entry) || typeof (options.entry) === 'string')) { - throw new TypeError('options.entry must be a DN or string'); - } - if (options.attribute && typeof (options.attribute) !== 'string') - throw new TypeError('options.attribute must be a string'); - if (options.value && typeof (options.value) !== 'string') - throw new TypeError('options.value must be a string'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); + assert.optionalString(options.attribute); + assert.optionalString(options.value); + lassert.optionalStringDN(options.entry); options.protocolOp = Protocol.LDAP_REQ_COMPARE; LDAPMessage.call(this, options); @@ -38,14 +23,18 @@ function CompareRequest(options) { this.entry = options.entry || null; this.attribute = options.attribute || ''; this.value = options.value || ''; - - var self = this; - this.__defineGetter__('type', function () { return 'CompareRequest'; }); - this.__defineGetter__('_dn', function () { return self.entry; }); } util.inherits(CompareRequest, LDAPMessage); -module.exports = CompareRequest; - +Object.defineProperties(CompareRequest.prototype, { + type: { + get: function getType() { return 'CompareRequest'; }, + configurable: false + }, + _dn: { + get: function getDN() { return this.entry; }, + configurable: false + } +}); CompareRequest.prototype._parse = function (ber) { assert.ok(ber); @@ -59,7 +48,6 @@ CompareRequest.prototype._parse = function (ber) { return true; }; - CompareRequest.prototype._toBer = function (ber) { assert.ok(ber); @@ -72,7 +60,6 @@ CompareRequest.prototype._toBer = function (ber) { return ber; }; - CompareRequest.prototype._json = function (j) { assert.ok(j); @@ -82,3 +69,8 @@ CompareRequest.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = CompareRequest; diff --git a/lib/messages/compare_response.js b/lib/messages/compare_response.js index 33289d0..4f949d4 100644 --- a/lib/messages/compare_response.js +++ b/lib/messages/compare_response.js @@ -1,28 +1,22 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. +var assert = require('assert-plus'); var util = require('util'); var LDAPResult = require('./result'); var Protocol = require('../protocol'); - ///--- API function CompareResponse(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REP_COMPARE; LDAPResult.call(this, options); } util.inherits(CompareResponse, LDAPResult); -module.exports = CompareResponse; - CompareResponse.prototype.end = function (matches) { var status = 0x06; @@ -35,3 +29,8 @@ CompareResponse.prototype.end = function (matches) { return LDAPResult.prototype.end.call(this, status); }; + + +///--- Exports + +module.exports = CompareResponse; diff --git a/lib/messages/del_request.js b/lib/messages/del_request.js index 92abcdc..dc66f55 100644 --- a/lib/messages/del_request.js +++ b/lib/messages/del_request.js @@ -1,45 +1,36 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. -var assert = require('assert'); +var assert = require('assert-plus'); var util = require('util'); var LDAPMessage = require('./message'); -var dn = require('../dn'); var Protocol = require('../protocol'); - - - -///--- Globals - -var isDN = dn.DN.isDN; +var lassert = require('../assert'); ///--- API function DeleteRequest(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - if (options.entry && - !(isDN(options.entry) || typeof (options.entry) === 'string')) { - throw new TypeError('options.entry must be a DN or string'); - } - } else { - options = {}; - } + options = options || {}; + assert.object(options); + lassert.optionalStringDN(options.entry); options.protocolOp = Protocol.LDAP_REQ_DELETE; LDAPMessage.call(this, options); this.entry = options.entry || null; - - var self = this; - this.__defineGetter__('type', function () { return 'DeleteRequest'; }); - this.__defineGetter__('_dn', function () { return self.entry; }); } util.inherits(DeleteRequest, LDAPMessage); -module.exports = DeleteRequest; - +Object.defineProperties(DeleteRequest.prototype, { + type: { + get: function getType() { return 'DeleteRequest'; }, + configurable: false + }, + _dn: { + get: function getDN() { return this.entry; }, + configurable: false + } +}); DeleteRequest.prototype._parse = function (ber, length) { assert.ok(ber); @@ -50,7 +41,6 @@ DeleteRequest.prototype._parse = function (ber, length) { return true; }; - DeleteRequest.prototype._toBer = function (ber) { assert.ok(ber); @@ -61,7 +51,6 @@ DeleteRequest.prototype._toBer = function (ber) { return ber; }; - DeleteRequest.prototype._json = function (j) { assert.ok(j); @@ -69,3 +58,8 @@ DeleteRequest.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = DeleteRequest; diff --git a/lib/messages/del_response.js b/lib/messages/del_response.js index de55aa1..f42322f 100644 --- a/lib/messages/del_response.js +++ b/lib/messages/del_response.js @@ -1,5 +1,6 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. +var assert = require('assert-plus'); var util = require('util'); var LDAPResult = require('./result'); @@ -9,15 +10,15 @@ var Protocol = require('../protocol'); ///--- API function DeleteResponse(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REP_DELETE; LDAPResult.call(this, options); } util.inherits(DeleteResponse, LDAPResult); + + +///--- Exports + module.exports = DeleteResponse; diff --git a/lib/messages/ext_request.js b/lib/messages/ext_request.js index f230a11..35ac072 100644 --- a/lib/messages/ext_request.js +++ b/lib/messages/ext_request.js @@ -1,26 +1,22 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. -var assert = require('assert'); +var assert = require('assert-plus'); var util = require('util'); var LDAPMessage = require('./message'); var Protocol = require('../protocol'); - ///--- API function ExtendedRequest(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - if (options.requestName && typeof (options.requestName) !== 'string') - throw new TypeError('options.requestName must be a string'); - if (options.requestValue && !(Buffer.isBuffer(options.requestValue) || - typeof (options.requestValue) === 'string')) - throw new TypeError('options.requestValue must be a buffer or a string'); - } else { - options = {}; + options = options || {}; + assert.object(options); + assert.optionalString(options.requestName); + if (options.requestValue && + !(Buffer.isBuffer(options.requestValue) || + typeof (options.requestValue) === 'string')) { + throw new TypeError('options.requestValue must be a buffer or a string'); } options.protocolOp = Protocol.LDAP_REQ_EXTENSION; @@ -28,31 +24,36 @@ function ExtendedRequest(options) { this.requestName = options.requestName || ''; this.requestValue = options.requestValue; - - this.__defineGetter__('type', function () { return 'ExtendedRequest'; }); - this.__defineGetter__('_dn', function () { return this.requestName; }); - this.__defineGetter__('name', function () { - return this.requestName; - }); - this.__defineGetter__('value', function () { - return this.requestValue; - }); - this.__defineSetter__('name', function (name) { - if (typeof (name) !== 'string') - throw new TypeError('name must be a string'); - - this.requestName = name; - }); - this.__defineSetter__('value', function (val) { - if (!(Buffer.isBuffer(val) || typeof (val) === 'string')) - throw new TypeError('value must be a buffer or a string'); - - this.requestValue = val; - }); } util.inherits(ExtendedRequest, LDAPMessage); -module.exports = ExtendedRequest; +Object.defineProperties(ExtendedRequest.prototype, { + type: { + get: function getType() { return 'ExtendedRequest'; }, + configurable: false + }, + _dn: { + get: function getDN() { return this.requestName; }, + configurable: false + }, + name: { + get: function getName() { return this.requestName; }, + set: function setName(val) { + assert.string(val); + this.requestName = val; + }, + configurable: false + }, + value: { + get: function getValue() { return this.requestValue; }, + set: function setValue(val) { + if (!(Buffer.isBuffer(val) || typeof (val) === 'string')) + throw new TypeError('value must be a buffer or a string'); + this.requestValue = val; + }, + configurable: false + } +}); ExtendedRequest.prototype._parse = function (ber) { assert.ok(ber); @@ -68,7 +69,6 @@ ExtendedRequest.prototype._parse = function (ber) { return true; }; - ExtendedRequest.prototype._toBer = function (ber) { assert.ok(ber); @@ -82,7 +82,6 @@ ExtendedRequest.prototype._toBer = function (ber) { return ber; }; - ExtendedRequest.prototype._json = function (j) { assert.ok(j); @@ -92,3 +91,8 @@ ExtendedRequest.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = ExtendedRequest; diff --git a/lib/messages/ext_response.js b/lib/messages/ext_response.js index 91360c7..4e72d0a 100644 --- a/lib/messages/ext_response.js +++ b/lib/messages/ext_response.js @@ -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 LDAPResult = require('./result'); @@ -10,45 +10,44 @@ var Protocol = require('../protocol'); ///--- API function ExtendedResponse(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - if (options.responseName && typeof (options.responseName) !== 'string') - throw new TypeError('options.responseName must be a string'); - if (options.responseValue && typeof (options.responseValue) !== 'string') - throw new TypeError('options.responseValue must be a string'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); + assert.optionalString(options.responseName); + assert.optionalString(options.responsevalue); this.responseName = options.responseName || undefined; this.responseValue = options.responseValue || undefined; options.protocolOp = Protocol.LDAP_REP_EXTENSION; LDAPResult.call(this, options); - - this.__defineGetter__('name', function () { - return this.responseName; - }); - this.__defineGetter__('value', function () { - return this.responseValue; - }); - this.__defineSetter__('name', function (name) { - if (typeof (name) !== 'string') - throw new TypeError('name must be a string'); - - this.responseName = name; - }); - this.__defineSetter__('value', function (val) { - if (typeof (val) !== 'string') - throw new TypeError('value must be a string'); - - this.responseValue = val; - }); } util.inherits(ExtendedResponse, LDAPResult); -module.exports = ExtendedResponse; - +Object.defineProperties(ExtendedResponse.prototype, { + type: { + get: function getType() { return 'ExtendedResponse'; }, + configurable: false + }, + _dn: { + get: function getDN() { return this.responseName; }, + configurable: false + }, + name: { + get: function getName() { return this.responseName; }, + set: function setName(val) { + assert.string(val); + this.responseName = val; + }, + configurable: false + }, + value: { + get: function getValue() { return this.responseValue; }, + set: function (val) { + assert.string(val); + this.responseValue = val; + }, + configurable: false + } +}); ExtendedResponse.prototype._parse = function (ber) { assert.ok(ber); @@ -64,7 +63,6 @@ ExtendedResponse.prototype._parse = function (ber) { return true; }; - ExtendedResponse.prototype._toBer = function (ber) { assert.ok(ber); @@ -79,7 +77,6 @@ ExtendedResponse.prototype._toBer = function (ber) { return ber; }; - ExtendedResponse.prototype._json = function (j) { assert.ok(j); @@ -90,3 +87,8 @@ ExtendedResponse.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = ExtendedResponse; diff --git a/lib/messages/index.js b/lib/messages/index.js index 899ee6f..3586377 100644 --- a/lib/messages/index.js +++ b/lib/messages/index.js @@ -28,7 +28,6 @@ var UnbindRequest = require('./unbind_request'); var UnbindResponse = require('./unbind_response'); - ///--- API module.exports = { diff --git a/lib/messages/message.js b/lib/messages/message.js index 571611d..aa8ce5e 100644 --- a/lib/messages/message.js +++ b/lib/messages/message.js @@ -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'); @@ -9,7 +9,6 @@ var Control = require('../controls').Control; var Protocol = require('../protocol'); - ///--- Globals var Ber = asn1.Ber; @@ -18,7 +17,6 @@ var BerWriter = asn1.BerWriter; var getControl = require('../controls').getControl; - ///--- API @@ -28,37 +26,44 @@ var getControl = require('../controls').getControl; * @param {Object} options stuff. */ function LDAPMessage(options) { - if (!options || typeof (options) !== 'object') - throw new TypeError('options (object) required'); + assert.object(options); this.messageID = options.messageID || 0; this.protocolOp = options.protocolOp || undefined; this.controls = options.controls ? options.controls.slice(0) : []; this.log = options.log; - - var self = this; - this.__defineGetter__('id', function () { return self.messageID; }); - this.__defineGetter__('dn', function () { return self._dn || ''; }); - this.__defineGetter__('type', function () { return 'LDAPMessage'; }); - this.__defineGetter__('json', function () { - var j = { - messageID: self.messageID, - protocolOp: self.type - }; - j = self._json(j); - j.controls = self.controls; - return j; - }); } -module.exports = LDAPMessage; - +Object.defineProperties(LDAPMessage.prototype, { + id: { + get: function getId() { return this.messageID; }, + configurable: false + }, + dn: { + get: function getDN() { return this._dn || ''; }, + configurable: false + }, + type: { + get: function getType() { return 'LDAPMessage'; }, + configurable: false + }, + json: { + get: function () { + var out = this._json({ + messageID: this.messageID, + protocolOp: this.type + }); + out.controls = this.controls; + return out; + }, + configurable: false + } +}); LDAPMessage.prototype.toString = function () { return JSON.stringify(this.json); }; - LDAPMessage.prototype.parse = function (ber) { assert.ok(ber); @@ -84,7 +89,6 @@ LDAPMessage.prototype.parse = function (ber) { return true; }; - LDAPMessage.prototype.toBer = function () { var writer = new BerWriter(); writer.startSequence(); @@ -106,3 +110,8 @@ LDAPMessage.prototype.toBer = function () { writer.endSequence(); return writer.buffer; }; + + +///--- Exports + +module.exports = LDAPMessage; diff --git a/lib/messages/moddn_request.js b/lib/messages/moddn_request.js index 9fb7961..6a63156 100644 --- a/lib/messages/moddn_request.js +++ b/lib/messages/moddn_request.js @@ -1,39 +1,23 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. -var assert = require('assert'); +var assert = require('assert-plus'); var util = require('util'); var LDAPMessage = require('./message'); var Protocol = require('../protocol'); var dn = require('../dn'); - - -///--- Globals - -var isDN = dn.DN.isDN; +var lassert = require('../assert'); ///--- API function ModifyDNRequest(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - if (options.entry && - !(isDN(options.entry) || typeof (options.entry) === 'string')) { - throw new TypeError('options.entry must be a DN or string'); - } - if (options.newRdn && !isDN(options.newRdn)) - throw new TypeError('options.newRdn must be a DN'); - if (options.deleteOldRdn !== undefined && - typeof (options.deleteOldRdn) !== 'boolean') - throw new TypeError('options.deleteOldRdn must be a boolean'); - if (options.newSuperior && !isDN(options.newSuperior)) - throw new TypeError('options.newSuperior must be a DN'); - - } else { - options = {}; - } + options = options || {}; + assert.object(options); + assert.optionalBool(options.deleteOldRdn); + lassert.optionalStringDN(options.entry); + lassert.optionalDN(options.newRdn); + lassert.optionalDN(options.newSuperior); options.protocolOp = Protocol.LDAP_REQ_MODRDN; LDAPMessage.call(this, options); @@ -42,14 +26,18 @@ function ModifyDNRequest(options) { this.newRdn = options.newRdn || null; this.deleteOldRdn = options.deleteOldRdn || true; this.newSuperior = options.newSuperior || null; - - var self = this; - this.__defineGetter__('type', function () { return 'ModifyDNRequest'; }); - this.__defineGetter__('_dn', function () { return self.entry; }); } util.inherits(ModifyDNRequest, LDAPMessage); -module.exports = ModifyDNRequest; - +Object.defineProperties(ModifyDNRequest.prototype, { + type: { + get: function getType() { return 'ModifyDNRequest'; }, + configurable: false + }, + _dn: { + get: function getDN() { return this.entry; }, + configurable: false + } +}); ModifyDNRequest.prototype._parse = function (ber) { assert.ok(ber); @@ -63,7 +51,6 @@ ModifyDNRequest.prototype._parse = function (ber) { return true; }; - ModifyDNRequest.prototype._toBer = function (ber) { //assert.ok(ber); @@ -84,7 +71,6 @@ ModifyDNRequest.prototype._toBer = function (ber) { return ber; }; - ModifyDNRequest.prototype._json = function (j) { assert.ok(j); @@ -95,3 +81,8 @@ ModifyDNRequest.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = ModifyDNRequest; diff --git a/lib/messages/moddn_response.js b/lib/messages/moddn_response.js index 288cfe8..57f245f 100644 --- a/lib/messages/moddn_response.js +++ b/lib/messages/moddn_response.js @@ -1,5 +1,6 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. +var assert = require('assert-plus'); var util = require('util'); var LDAPResult = require('./result'); @@ -9,15 +10,15 @@ var Protocol = require('../protocol'); ///--- API function ModifyDNResponse(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REP_MODRDN; LDAPResult.call(this, options); } util.inherits(ModifyDNResponse, LDAPResult); + + +///--- Exports + module.exports = ModifyDNResponse; diff --git a/lib/messages/modify_request.js b/lib/messages/modify_request.js index 2f6af12..38f70b3 100644 --- a/lib/messages/modify_request.js +++ b/lib/messages/modify_request.js @@ -1,56 +1,39 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. -var assert = require('assert'); +var assert = require('assert-plus'); var util = require('util'); var LDAPMessage = require('./message'); -var dn = require('../dn'); -var Attribute = require('../attribute'); var Change = require('../change'); var Protocol = require('../protocol'); - - - -///--- API - -var isDN = dn.DN.isDN; +var lassert = require('../assert'); ///--- API function ModifyRequest(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - if (options.object && - !(isDN(options.object) || typeof (options.object) === 'string')) { - throw new TypeError('options.object must be a DN or string'); - } - if (options.attributes) { - if (!Array.isArray(options.attributes)) - throw new TypeError('options.attributes must be [Attribute]'); - options.attributes.forEach(function (a) { - if (!(a instanceof Attribute)) - throw new TypeError('options.attributes must be [Attribute]'); - }); - } - } else { - options = {}; - } + options = options || {}; + assert.object(options); + lassert.optionalStringDN(options.object); + lassert.optionalArrayOfAttribute(options.attributes); options.protocolOp = Protocol.LDAP_REQ_MODIFY; LDAPMessage.call(this, options); this.object = options.object || null; this.changes = options.changes ? options.changes.slice(0) : []; - - var self = this; - this.__defineGetter__('type', function () { return 'ModifyRequest'; }); - this.__defineGetter__('_dn', function () { return self.object; }); } util.inherits(ModifyRequest, LDAPMessage); -module.exports = ModifyRequest; - +Object.defineProperties(ModifyRequest.prototype, { + type: { + get: function getType() { return 'ModifyRequest'; }, + configurable: false + }, + _dn: { + get: function getDN() { return this.object; }, + configurable: false + } +}); ModifyRequest.prototype._parse = function (ber) { assert.ok(ber); @@ -70,7 +53,6 @@ ModifyRequest.prototype._parse = function (ber) { return true; }; - ModifyRequest.prototype._toBer = function (ber) { assert.ok(ber); @@ -84,7 +66,6 @@ ModifyRequest.prototype._toBer = function (ber) { return ber; }; - ModifyRequest.prototype._json = function (j) { assert.ok(j); @@ -97,3 +78,8 @@ ModifyRequest.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = ModifyRequest; diff --git a/lib/messages/modify_response.js b/lib/messages/modify_response.js index deecd4c..5e568fc 100644 --- a/lib/messages/modify_response.js +++ b/lib/messages/modify_response.js @@ -1,5 +1,6 @@ // Copyright 2011 Mark Cavage, Inc. All rights reserved. +var assert = require('assert-plus'); var util = require('util'); var LDAPResult = require('./result'); @@ -9,15 +10,15 @@ var Protocol = require('../protocol'); ///--- API function ModifyResponse(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REP_MODIFY; LDAPResult.call(this, options); } util.inherits(ModifyResponse, LDAPResult); + + +///--- Exports + module.exports = ModifyResponse; diff --git a/lib/messages/noop_response.js.sav b/lib/messages/noop_response.js.sav deleted file mode 100644 index 42fa9e5..0000000 --- a/lib/messages/noop_response.js.sav +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2014 Joyent, Inc. All rights reserved. - - -function NoOpResponse(options) { - if (!options) - options = {}; - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - - options.protocolOp = 0; - LDAPMessage.call(this, options); - this.__defineGetter__('type', function () { return 'NoOpResponse'; }); -} -util.inherits(NoOpResponse, LDAPMessage); -module.exports = NoOpResponse; - -NoOpResponse.prototype.end = function () {}; - -NoOpResponse.prototype._json = function (j) { return j; }; diff --git a/lib/messages/parser.js b/lib/messages/parser.js index 548b433..60de51e 100644 --- a/lib/messages/parser.js +++ b/lib/messages/parser.js @@ -35,21 +35,17 @@ var Message = require('./message'); var Protocol = require('../protocol'); - ///--- Globals var Ber = asn1.Ber; var BerReader = asn1.BerReader; - ///--- API function Parser(options) { - if (!options || typeof (options) !== 'object') - throw new TypeError('options (object) required'); - if (typeof (options.log) !== 'object') - throw new TypeError('options.log (object) required'); + assert.object(options); + assert.object(options.log); EventEmitter.call(this); @@ -57,8 +53,6 @@ function Parser(options) { this.log = options.log; } util.inherits(Parser, EventEmitter); -module.exports = Parser; - Parser.prototype.write = function (data) { if (!data || !Buffer.isBuffer(data)) @@ -118,7 +112,6 @@ Parser.prototype.write = function (data) { return end(); }; - Parser.prototype.getMessage = function (ber) { assert.ok(ber); @@ -228,3 +221,8 @@ Parser.prototype.getMessage = function (ber) { log: self.log }); }; + + +///--- Exports + +module.exports = Parser; diff --git a/lib/messages/result.js b/lib/messages/result.js index 54f8d5c..c0987d1 100644 --- a/lib/messages/result.js +++ b/lib/messages/result.js @@ -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'); @@ -10,38 +10,21 @@ var LDAPMessage = require('./message'); var Protocol = require('../protocol'); - ///--- Globals var Ber = asn1.Ber; var BerWriter = asn1.BerWriter; - ///--- API function LDAPResult(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options (object) required'); - if (options.status && typeof (options.status) !== 'number') - throw new TypeError('options.status must be a number'); - if (options.matchedDN && typeof (options.matchedDN) !== 'string') - throw new TypeError('options.matchedDN must be a string'); - if (options.errorMessage && typeof (options.errorMessage) !== 'string') - throw new TypeError('options.errorMessage must be a string'); - - if (options.referrals) { - if (!(options.referrals instanceof Array)) - throw new TypeError('options.referrrals must be an array[string]'); - options.referrals.forEach(function (r) { - if (typeof (r) !== 'string') - throw new TypeError('options.referrals must be an array[string]'); - }); - } - } else { - options = {}; - } + options = options || {}; + assert.object(options); + assert.optionalNumber(options.status); + assert.optionalString(options.matchedDN); + assert.optionalString(options.errorMessage); + assert.optionalArrayOfString(options.referrals); LDAPMessage.call(this, options); @@ -51,12 +34,14 @@ function LDAPResult(options) { this.referrals = options.referrals || []; this.connection = options.connection || null; - - this.__defineGetter__('type', function () { return 'LDAPResult'; }); } util.inherits(LDAPResult, LDAPMessage); -module.exports = LDAPResult; - +Object.defineProperties(LDAPResult.prototype, { + type: { + get: function getType() { return 'LDAPResult'; }, + configurable: false + } +}); LDAPResult.prototype.end = function (status) { assert.ok(this.connection); @@ -93,7 +78,6 @@ LDAPResult.prototype.end = function (status) { }; - LDAPResult.prototype._parse = function (ber) { assert.ok(ber); @@ -112,7 +96,6 @@ LDAPResult.prototype._parse = function (ber) { return true; }; - LDAPResult.prototype._toBer = function (ber) { assert.ok(ber); @@ -129,7 +112,6 @@ LDAPResult.prototype._toBer = function (ber) { return ber; }; - LDAPResult.prototype._json = function (j) { assert.ok(j); @@ -140,3 +122,8 @@ LDAPResult.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = LDAPResult; diff --git a/lib/messages/search_entry.js b/lib/messages/search_entry.js index 3a8a1a2..57b1458 100644 --- a/lib/messages/search_entry.js +++ b/lib/messages/search_entry.js @@ -1,15 +1,14 @@ // 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'); var LDAPMessage = require('./message'); var Attribute = require('../attribute'); -var dn = require('../dn'); var Protocol = require('../protocol'); - +var lassert = require('../assert'); ///--- Globals @@ -17,79 +16,79 @@ var Protocol = require('../protocol'); var BerWriter = asn1.BerWriter; - ///--- API function SearchEntry(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - if (options.objectName && !(options.objectName instanceof dn.DN)) - throw new TypeError('options.objectName must be a DN'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); + lassert.optionalStringDN(options.objectName); options.protocolOp = Protocol.LDAP_REP_SEARCH_ENTRY; LDAPMessage.call(this, options); this.objectName = options.objectName || null; this.setAttributes(options.attributes || []); - - var self = this; - this.__defineGetter__('type', function () { return 'SearchEntry'; }); - this.__defineGetter__('object', function () { - var obj = { - dn: self.dn.toString(), - controls: [] - }; - self.attributes.forEach(function (a) { - if (a.vals && a.vals.length) { - if (a.vals.length > 1) { - obj[a.type] = a.vals.slice(); - } else { - obj[a.type] = a.vals[0]; - } - } else { - obj[a.type] = []; - } - }); - self.controls.forEach(function (element, index, array) { - obj.controls.push(element.json); - }); - return obj; - }); - - this.__defineGetter__('raw', function () { - var obj = { - dn: self.dn.toString(), - controls: [] - }; - - self.attributes.forEach(function (a) { - if (a.buffers && a.buffers.length) { - if (a.buffers.length > 1) { - obj[a.type] = a.buffers.slice(); - } else { - obj[a.type] = a.buffers[0]; - } - } else { - obj[a.type] = []; - } - }); - self.controls.forEach(function (element, index, array) { - obj.controls.push(element.json); - }); - return obj; - }); - - this.__defineGetter__('_dn', function () { - return self.objectName; - }); } util.inherits(SearchEntry, LDAPMessage); -module.exports = SearchEntry; +Object.defineProperties(SearchEntry.prototype, { + type: { + get: function getType() { return 'SearchEntry'; }, + configurable: false + }, + _dn: { + get: function getDN() { return this.objectName; }, + configurable: false + }, + object: { + get: function getObject() { + var obj = { + dn: this.dn.toString(), + controls: [] + }; + this.attributes.forEach(function (a) { + if (a.vals && a.vals.length) { + if (a.vals.length > 1) { + obj[a.type] = a.vals.slice(); + } else { + obj[a.type] = a.vals[0]; + } + } else { + obj[a.type] = []; + } + }); + this.controls.forEach(function (element, index, array) { + obj.controls.push(element.json); + }); + return obj; + }, + configurable: false + }, + raw: { + get: function getRaw() { + var obj = { + dn: this.dn.toString(), + controls: [] + }; + this.attributes.forEach(function (a) { + if (a.buffers && a.buffers.length) { + if (a.buffers.length > 1) { + obj[a.type] = a.buffers.slice(); + } else { + obj[a.type] = a.buffers[0]; + } + } else { + obj[a.type] = []; + } + }); + this.controls.forEach(function (element, index, array) { + obj.controls.push(element.json); + }); + return obj; + }, + configurable: false + } +}); SearchEntry.prototype.addAttribute = function (attr) { if (!attr || typeof (attr) !== 'object') @@ -98,12 +97,10 @@ SearchEntry.prototype.addAttribute = function (attr) { this.attributes.push(attr); }; - SearchEntry.prototype.toObject = function () { return this.object; }; - SearchEntry.prototype.fromObject = function (obj) { if (typeof (obj) !== 'object') throw new TypeError('object required'); @@ -151,7 +148,6 @@ SearchEntry.prototype.setAttributes = function (obj) { } }; - SearchEntry.prototype._json = function (j) { assert.ok(j); @@ -164,7 +160,6 @@ SearchEntry.prototype._json = function (j) { return j; }; - SearchEntry.prototype._parse = function (ber) { assert.ok(ber); @@ -181,7 +176,6 @@ SearchEntry.prototype._parse = function (ber) { return true; }; - SearchEntry.prototype._toBer = function (ber) { assert.ok(ber); @@ -195,3 +189,8 @@ SearchEntry.prototype._toBer = function (ber) { return ber; }; + + +///--- Exports + +module.exports = SearchEntry; diff --git a/lib/messages/search_reference.js b/lib/messages/search_reference.js index 2b1ee9e..f88b038 100644 --- a/lib/messages/search_reference.js +++ b/lib/messages/search_reference.js @@ -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'); @@ -11,58 +11,57 @@ var dn = require('../dn'); var url = require('../url'); - ///--- Globals var BerWriter = asn1.BerWriter; var parseURL = url.parse; - ///--- API function SearchReference(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - if (options.objectName && !(options.objectName instanceof dn.DN)) - throw new TypeError('options.objectName must be a DN'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REP_SEARCH_REF; LDAPMessage.call(this, options); this.uris = options.uris || []; - - var self = this; - this.__defineGetter__('type', function () { return 'SearchReference'; }); - this.__defineGetter__('object', function () { - return { - dn: self.dn.toString(), - uris: self.uris.slice() - }; - }); - this.__defineGetter__('_dn', function () { - return new dn.DN(''); - }); - this.__defineGetter__('urls', function () { - return self.uris; - }); - this.__defineSetter__('urls', function (u) { - self.uris = u.slice(); - }); } util.inherits(SearchReference, LDAPMessage); -module.exports = SearchReference; - +Object.defineProperties(SearchReference.prototype, { + type: { + get: function getType() { return 'SearchReference'; }, + configurable: false + }, + _dn: { + get: function getDN() { return new dn.DN(''); }, + configurable: false + }, + object: { + get: function getObject() { + return { + dn: this.dn.toString(), + uris: this.uris.slice() + }; + }, + configurable: false + }, + urls: { + get: function getUrls() { return this.uris; }, + set: function setUrls(val) { + assert.ok(val); + assert.ok(Array.isArray(val)); + this.uris = val.slice(); + }, + configurable: false + } +}); SearchReference.prototype.toObject = function () { return this.object; }; - SearchReference.prototype.fromObject = function (obj) { if (typeof (obj) !== 'object') throw new TypeError('object required'); @@ -78,7 +77,6 @@ SearchReference.prototype._json = function (j) { return j; }; - SearchReference.prototype._parse = function (ber, length) { assert.ok(ber); @@ -91,7 +89,6 @@ SearchReference.prototype._parse = function (ber, length) { return true; }; - SearchReference.prototype._toBer = function (ber) { assert.ok(ber); @@ -101,3 +98,8 @@ SearchReference.prototype._toBer = function (ber) { return ber; }; + + +///--- Exports + +module.exports = SearchReference; diff --git a/lib/messages/search_request.js b/lib/messages/search_request.js index d68f5b9..d55e59e 100644 --- a/lib/messages/search_request.js +++ b/lib/messages/search_request.js @@ -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'); @@ -12,58 +12,20 @@ var filters = require('../filters'); var Protocol = require('../protocol'); - ///--- Globals -var DN = dn.DN; var Ber = asn1.Ber; ///--- API function SearchRequest(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REQ_SEARCH; LDAPMessage.call(this, options); - var self = this; - this.__defineGetter__('type', function () { return 'SearchRequest'; }); - this.__defineGetter__('_dn', function () { return self.baseObject; }); - this.__defineGetter__('scope', function () { - switch (self._scope) { - case Protocol.SCOPE_BASE_OBJECT: return 'base'; - case Protocol.SCOPE_ONE_LEVEL: return 'one'; - case Protocol.SCOPE_SUBTREE: return 'sub'; - default: - throw new Error(self._scope + ' is an invalid search scope'); - } - }); - this.__defineSetter__('scope', function (s) { - if (typeof (s) === 'string') { - switch (s) { - case 'base': - self._scope = Protocol.SCOPE_BASE_OBJECT; - break; - case 'one': - self._scope = Protocol.SCOPE_ONE_LEVEL; - break; - case 'sub': - self._scope = Protocol.SCOPE_SUBTREE; - break; - default: - throw new Error(s + ' is an invalid search scope'); - } - } else { - self._scope = s; - } - }); - if (options.baseObject !== undefined) { this.baseObject = options.baseObject; } else { @@ -78,18 +40,47 @@ function SearchRequest(options) { this.attributes = options.attributes ? options.attributes.slice(0) : []; } util.inherits(SearchRequest, LDAPMessage); -module.exports = SearchRequest; - - -SearchRequest.prototype.newResult = function () { - var self = this; - - return new LDAPResult({ - messageID: self.messageID, - protocolOp: Protocol.LDAP_REP_SEARCH - }); -}; - +Object.defineProperties(SearchRequest.prototype, { + type: { + get: function getType() { return 'SearchRequest'; }, + configurable: false + }, + _dn: { + get: function getDN() { return this.baseObject; }, + configurable: false + }, + scope: { + get: function getScope() { + switch (this._scope) { + case Protocol.SCOPE_BASE_OBJECT: return 'base'; + case Protocol.SCOPE_ONE_LEVEL: return 'one'; + case Protocol.SCOPE_SUBTREE: return 'sub'; + default: + throw new Error(this._scope + ' is an invalid search scope'); + } + }, + set: function setScope(val) { + if (typeof (val) === 'string') { + switch (val) { + case 'base': + this._scope = Protocol.SCOPE_BASE_OBJECT; + break; + case 'one': + this._scope = Protocol.SCOPE_ONE_LEVEL; + break; + case 'sub': + this._scope = Protocol.SCOPE_SUBTREE; + break; + default: + throw new Error(val + ' is an invalid search scope'); + } + } else { + this._scope = val; + } + }, + configurable: false + } +}); SearchRequest.prototype._parse = function (ber) { assert.ok(ber); @@ -114,7 +105,6 @@ SearchRequest.prototype._parse = function (ber) { return true; }; - SearchRequest.prototype._toBer = function (ber) { assert.ok(ber); @@ -139,7 +129,6 @@ SearchRequest.prototype._toBer = function (ber) { return ber; }; - SearchRequest.prototype._json = function (j) { assert.ok(j); @@ -154,3 +143,8 @@ SearchRequest.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = SearchRequest; diff --git a/lib/messages/search_response.js b/lib/messages/search_response.js index 33d8523..9c09024 100644 --- a/lib/messages/search_response.js +++ b/lib/messages/search_response.js @@ -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 LDAPResult = require('./result'); @@ -13,14 +13,11 @@ var parseURL = require('../url').parse; var Protocol = require('../protocol'); - ///--- API function SearchResponse(options) { - if (!options) - options = {}; - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); + options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REP_SEARCH; LDAPResult.call(this, options); @@ -30,8 +27,6 @@ function SearchResponse(options) { this.sentEntries = 0; } util.inherits(SearchResponse, LDAPResult); -module.exports = SearchResponse; - /** * Allows you to send a SearchEntry back to the client. @@ -118,25 +113,18 @@ SearchResponse.prototype.send = function (entry, nofiltering) { } }; - - SearchResponse.prototype.createSearchEntry = function (object) { - if (!object || typeof (object) !== 'object') - throw new TypeError('object required'); - - var self = this; + assert.object(object); var entry = new SearchEntry({ - messageID: self.messageID, - log: self.log, + messageID: this.messageID, + log: this.log, objectName: object.objectName || object.dn }); entry.fromObject((object.attributes || object)); - return entry; }; - SearchResponse.prototype.createSearchReference = function (uris) { if (!uris) throw new TypeError('uris ([string]) required'); @@ -156,3 +144,8 @@ SearchResponse.prototype.createSearchReference = function (uris) { uris: uris }); }; + + +///--- Exports + +module.exports = SearchResponse; diff --git a/lib/messages/unbind_request.js b/lib/messages/unbind_request.js index f8b7cca..c1366a6 100644 --- a/lib/messages/unbind_request.js +++ b/lib/messages/unbind_request.js @@ -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 LDAPMessage = require('./message'); @@ -8,7 +8,6 @@ var dn = require('../dn'); var Protocol = require('../protocol'); - ///--- Globals var DN = dn.DN; @@ -18,49 +17,29 @@ var RDN = dn.RDN; ///--- API function UnbindRequest(options) { - if (options) { - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); - } else { - options = {}; - } + options = options || {}; + assert.object(options); options.protocolOp = Protocol.LDAP_REQ_UNBIND; LDAPMessage.call(this, options); - - var self = this; - this.__defineGetter__('type', function () { return 'UnbindRequest'; }); - this.__defineGetter__('_dn', function () { - if (self.connection) - return self.connection.ldap.bindDN; - - return new DN([new RDN({cn: 'anonymous'})]); - }); } util.inherits(UnbindRequest, LDAPMessage); -module.exports = UnbindRequest; - - -UnbindRequest.prototype.newResult = function () { - // This one is special, so just hack up the result object - function UnbindResponse(options) { - LDAPMessage.call(this, options); - this.__defineGetter__('type', function () { return 'UnbindResponse'; }); +Object.defineProperties(UnbindRequest.prototype, { + type: { + get: function getType() { return 'UnbindRequest'; }, + configurable: false + }, + _dn: { + get: function getDN() { + if (this.connection) { + return this.connection.ldap.bindDN; + } else { + return new DN([new RDN({cn: 'anonymous'})]); + } + }, + configurable: false } - util.inherits(UnbindResponse, LDAPMessage); - UnbindResponse.prototype.end = function (status) { - this.log.trace('%s: unbinding!', this.connection.ldap.id); - this.connection.end(); - }; - UnbindResponse.prototype._json = function (j) { return j; }; - - return new UnbindResponse({ - messageID: 0, - protocolOp: 0, - status: 0 // Success - }); -}; - +}); UnbindRequest.prototype._parse = function (ber) { assert.ok(ber); @@ -68,16 +47,19 @@ UnbindRequest.prototype._parse = function (ber) { return true; }; - UnbindRequest.prototype._toBer = function (ber) { assert.ok(ber); return ber; }; - UnbindRequest.prototype._json = function (j) { assert.ok(j); return j; }; + + +///--- Exports + +module.exports = UnbindRequest; diff --git a/lib/messages/unbind_response.js b/lib/messages/unbind_response.js index c1d8d87..e3ca56c 100644 --- a/lib/messages/unbind_response.js +++ b/lib/messages/unbind_response.js @@ -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 dtrace = require('../dtrace'); @@ -15,18 +15,19 @@ var Protocol = require('../protocol'); // not such a one-off. function UnbindResponse(options) { - if (!options) - options = {}; - if (typeof (options) !== 'object') - throw new TypeError('options must be an object'); + options = options || {}; + assert.object(options); options.protocolOp = 0; LDAPMessage.call(this, options); - this.__defineGetter__('type', function () { return 'UnbindResponse'; }); } util.inherits(UnbindResponse, LDAPMessage); -module.exports = UnbindResponse; - +Object.defineProperties(UnbindResponse.prototype, { + type: { + get: function getType() { return 'UnbindResponse'; }, + configurable: false + } +}); /** * Special override that just ends the connection, if present. @@ -56,7 +57,11 @@ UnbindResponse.prototype.end = function (status) { } }; - UnbindResponse.prototype._json = function (j) { return j; }; + + +///--- Exports + +module.exports = UnbindResponse;