"refactored controls into its own directory, minor bug fixes"

This commit is contained in:
Yunong Xiao 2011-12-08 11:22:35 -08:00
parent ee88fc7019
commit 2240f29cc3
9 changed files with 80 additions and 88 deletions

View File

@ -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');

View File

@ -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) {

40
lib/controls/index.js Normal file
View File

@ -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
};

View File

@ -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,26 +14,26 @@ 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();
var ber = new asn1.BerReader(options.value);
if (ber.readSequence()) {
this.value = {
changeTypes: ber.readInt(),
changesOnly: ber.readBoolean(),
returnECs: ber.readBoolean()
};
}
}
var self = this;
this.__defineGetter__('json', function() {
@ -46,7 +43,6 @@ function PersistentSearchControl(options) {
controlValue: self.value
};
});
}
module.exports = PersistentSearchControl;
@ -95,10 +91,10 @@ 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();

View File

@ -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,

View File

@ -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,13 +76,12 @@ 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);
}
}
}
if (this.log.isTraceEnabled())
this.log.trace('Parsing done: %j', this.json);

View File

@ -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"
},

View File

@ -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();
});

View File

@ -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();
});