From 2240f29cc32a76b36e166dda44a9eae087eaf661 Mon Sep 17 00:00:00 2001 From: Yunong Xiao Date: Thu, 8 Dec 2011 11:22:35 -0800 Subject: [PATCH] "refactored controls into its own directory, minor bug fixes" --- lib/client.js | 2 +- lib/{ => controls}/control.js | 8 +--- lib/controls/index.js | 40 +++++++++++++++++ .../persistent_search_control.js | 44 +++++++++---------- lib/index.js | 36 --------------- lib/messages/message.js | 12 +++-- package.json | 1 - tst/{ => controls}/control.test.js | 4 +- .../persistent_search_control.test.js | 21 ++++----- 9 files changed, 80 insertions(+), 88 deletions(-) rename lib/{ => controls}/control.js (85%) create mode 100644 lib/controls/index.js rename lib/{ => controls}/persistent_search_control.js (81%) rename tst/{ => controls}/control.test.js (89%) rename tst/{ => controls}/persistent_search_control.test.js (89%) diff --git a/lib/client.js b/lib/client.js index a239190..1706675 100644 --- a/lib/client.js +++ b/lib/client.js @@ -8,7 +8,7 @@ var util = require('util'); var Attribute = require('./attribute'); var Change = require('./change'); -var Control = require('./control'); +var Control = require('./controls/index').Control; var Protocol = require('./protocol'); var dn = require('./dn'); var errors = require('./errors'); diff --git a/lib/control.js b/lib/controls/control.js similarity index 85% rename from lib/control.js rename to lib/controls/control.js index 295e997..808c967 100644 --- a/lib/control.js +++ b/lib/controls/control.js @@ -5,17 +5,11 @@ var util = require('util'); var asn1 = require('asn1'); -var PersistentSearchControl = require('./persistent_search_control'); -var Protocol = require('./protocol'); - -var log4js = require('log4js'); +var Protocol = require('../protocol'); ///--- Globals -var LOG = log4js.getLogger('control.js'); var Ber = asn1.Ber; -var OID_PERSISTENT_SEARCH_CONTROL = '2.16.840.1.113730.3.4.3'; - ///--- API function Control(options) { diff --git a/lib/controls/index.js b/lib/controls/index.js new file mode 100644 index 0000000..9c7d42c --- /dev/null +++ b/lib/controls/index.js @@ -0,0 +1,40 @@ +var assert = require('assert'); + +var Control = require('./control'); +var PersistentSearchControl = require('./persistent_search_control'); + +var OID_PERSISTENT_SEARCH_CONTROL = '2.16.840.1.113730.3.4.3'; + +///--- API + +module.exports = { + getControl: function(ber) { + assert.ok(ber); + + if (ber.readSequence() === null) + return; + + var end = ber.offset + ber.length; + var options = {}; + if (ber.length) { + options.type = ber.readString(); + if (ber.offset < end) { + if (ber.peek() === 0x01) // Boolean, optional + options.criticality = ber.readBoolean(); + } + if (ber.offset < end) { + if (options.type == OID_PERSISTENT_SEARCH_CONTROL) { + // send the buffer directly to the PSC + options.value = ber.readString(0x04, true); + return new PersistentSearchControl(options); + } else { + options.value = ber.readString(); + return new Control(options); + } + } + } + }, + + Control: Control, + PersistentSearchControl: PersistentSearchControl +}; diff --git a/lib/persistent_search_control.js b/lib/controls/persistent_search_control.js similarity index 81% rename from lib/persistent_search_control.js rename to lib/controls/persistent_search_control.js index c12c14b..03d43d8 100644 --- a/lib/persistent_search_control.js +++ b/lib/controls/persistent_search_control.js @@ -4,9 +4,6 @@ var buffer = require('buffer'); var log4js = require('log4js'); var Control = require('./control'); var util = require('util'); -var Ber = asn1.Ber; - -var LOG = log4js.getLogger('PersistentSearchControl'); function PersistentSearchControl(options) { if (options) { @@ -17,25 +14,25 @@ function PersistentSearchControl(options) { if (options.criticality !== undefined && typeof(options.criticality) !== 'boolean') throw new TypeError('options.criticality must be a boolean'); - if (options.value && typeof(options.value) !== 'object') + 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 || '2.16.840.1.113730.3.4.3'; this.criticality = options.criticality || false; - this.value = options.value || undefined; - if (this.value) { + if (options.value) { // parse out this.value into the PSC object - var ber = new Ber.Reader(this.value); - ber.readSequence(); - this.value = { - changeTypes: ber.readInt(), - changesOnly: ber.readBoolean(), - returnECs: ber.readBoolean() - }; + var ber = new asn1.BerReader(options.value); + if (ber.readSequence()) { + this.value = { + changeTypes: ber.readInt(), + changesOnly: ber.readBoolean(), + returnECs: ber.readBoolean() + }; + } } var self = this; @@ -46,7 +43,6 @@ function PersistentSearchControl(options) { controlValue: self.value }; }); - } module.exports = PersistentSearchControl; @@ -82,23 +78,23 @@ PersistentSearchControl.prototype.get = function(options) { this.value = options.value || undefined; var self = this; - this.__defineGetter__('json', function() { - return { - controlType: self.type, - criticality: self.criticality, - controlValue: self.value - }; - }); + this.__defineGetter__('json', function() { + return { + controlType: self.type, + criticality: self.criticality, + controlValue: self.value + }; + }); }; PersistentSearchControl.prototype.toBer = function(ber) { assert.ok(ber); ber.startSequence(); - ber.writeString(this.type || ''); + ber.writeString(this.type); ber.writeBoolean(this.criticality); - var pscWriter = new Ber.Writer(); + var pscWriter = new asn1.BerWriter(); // write the value subsequence pscWriter.startSequence(); diff --git a/lib/index.js b/lib/index.js index 9052146..71585a7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,8 +3,6 @@ var Client = require('./client'); var Attribute = require('./attribute'); var Change = require('./change'); -var Control = require('./control'); -var PersistentSearchControl = require('./persistent_search_control'); var Protocol = require('./protocol'); var Server = require('./server'); @@ -12,15 +10,11 @@ var assert = require('assert'); var dn = require('./dn'); var errors = require('./errors'); var filters = require('./filters'); -var log4js = require('log4js'); var logStub = require('./log_stub'); var messages = require('./messages'); var schema = require('./schema'); var url = require('./url'); -var LOG = log4js.getLogger(this); -var OID_PERSISTENT_SEARCH_CONTROL = '2.16.840.1.113730.3.4.3'; - /// Hack a few things we need (i.e., "monkey patch" the prototype) if (!String.prototype.startsWith) { @@ -59,36 +53,6 @@ module.exports = { Attribute: Attribute, Change: Change, - Control: Control, - - getControl: function(ber) { - assert.ok(ber); - - if (ber.readSequence() === null) - return; - - var end = ber.offset + ber.length; - var options = {}; - if (ber.length) { - options.type = ber.readString(); - if (ber.offset < end) { - if (ber.peek() === 0x01) // Boolean, optional - options.criticality = ber.readBoolean(); - } - if (ber.offset < end) { - if (options.type == OID_PERSISTENT_SEARCH_CONTROL) { - // send the buffer directly to the PSC - options.value = ber.readString(0x04, true); - return new PersistentSearchControl(options); - } else { - options.value = ber.readString(); - return new Control(options); - } - } - } - }, - - PersistentSearchControl: PersistentSearchControl, DN: dn.DN, RDN: dn.RDN, diff --git a/lib/messages/message.js b/lib/messages/message.js index e0d62dc..031904b 100644 --- a/lib/messages/message.js +++ b/lib/messages/message.js @@ -5,10 +5,9 @@ var util = require('util'); var asn1 = require('asn1'); -var Control = require('../control'); +var Control = require('../controls/index').Control; var Protocol = require('../protocol'); -var log4js = require('log4js'); var logStub = require('../log_stub'); ///--- Globals @@ -16,11 +15,11 @@ var logStub = require('../log_stub'); var Ber = asn1.Ber; var BerReader = asn1.BerReader; var BerWriter = asn1.BerWriter; - -var LOG = log4js.getLogger('Message'); +var getControl = require('../controls/index').getControl; ///--- API + /** * LDAPMessage structure. * @@ -77,11 +76,10 @@ LDAPMessage.prototype.parse = function(ber) { ber.readSequence(); var end = ber.offset + ber.length; while (ber.offset < end) { - var getControl = require('../index').getControl; + var c = getControl(ber); - if (c) { + if (c) this.controls.push(c); - } } } diff --git a/package.json b/package.json index 1168c9a..2930b6c 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ "asn1": "0.1.8", "buffertools": "1.0.5", "dtrace-provider": "0.0.3", - "log4js": "0.4.1", "nopt": "1.0.10", "sprintf": "0.1.1" }, diff --git a/tst/control.test.js b/tst/controls/control.test.js similarity index 89% rename from tst/control.test.js rename to tst/controls/control.test.js index e9276a4..3edbf4f 100644 --- a/tst/control.test.js +++ b/tst/controls/control.test.js @@ -16,9 +16,9 @@ var getControl; ///--- Tests test('load library', function(t) { - Control = require('../lib/index').Control; + Control = require('../../lib/controls/index').Control; t.ok(Control); - getControl = require('../lib/index').getControl; + getControl = require('../../lib/controls/index').getControl; t.ok(getControl); t.end(); }); diff --git a/tst/persistent_search_control.test.js b/tst/controls/persistent_search_control.test.js similarity index 89% rename from tst/persistent_search_control.test.js rename to tst/controls/persistent_search_control.test.js index d8d1132..6341e54 100644 --- a/tst/persistent_search_control.test.js +++ b/tst/controls/persistent_search_control.test.js @@ -17,9 +17,10 @@ var LOG = log4js.getLogger('persistent_search_control.test'); ///--- Tests test('load library', function(t) { - PersistentSearchControl = require('../lib/index').PersistentSearchControl; + PersistentSearchControl = + require('../../lib/controls/index').PersistentSearchControl; t.ok(PersistentSearchControl); - getControl = require('../lib/index').getControl; + getControl = require('../../lib/controls/index').getControl; t.ok(getControl); t.end(); }); @@ -75,14 +76,14 @@ test('getControl with args', function(t) { 0xff, 0x01, 0x01, 0xff]); var options = { - type: '2.16.840.1.113730.3.4.3', - criticality: false, - value: { - changeTypes: 15, - changesOnly: true, - returnECs: true - } - }; + type: '2.16.840.1.113730.3.4.3', + criticality: false, + value: { + changeTypes: 15, + changesOnly: true, + returnECs: true + } + }; var ber = new BerReader(buf); var psc = getControl(ber);