"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 Attribute = require('./attribute');
var Change = require('./change'); var Change = require('./change');
var Control = require('./control'); var Control = require('./controls/index').Control;
var Protocol = require('./protocol'); var Protocol = require('./protocol');
var dn = require('./dn'); var dn = require('./dn');
var errors = require('./errors'); var errors = require('./errors');

View File

@ -5,17 +5,11 @@ var util = require('util');
var asn1 = require('asn1'); var asn1 = require('asn1');
var PersistentSearchControl = require('./persistent_search_control'); var Protocol = require('../protocol');
var Protocol = require('./protocol');
var log4js = require('log4js');
///--- Globals ///--- Globals
var LOG = log4js.getLogger('control.js');
var Ber = asn1.Ber; var Ber = asn1.Ber;
var OID_PERSISTENT_SEARCH_CONTROL = '2.16.840.1.113730.3.4.3';
///--- API ///--- API
function Control(options) { 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 log4js = require('log4js');
var Control = require('./control'); var Control = require('./control');
var util = require('util'); var util = require('util');
var Ber = asn1.Ber;
var LOG = log4js.getLogger('PersistentSearchControl');
function PersistentSearchControl(options) { function PersistentSearchControl(options) {
if (options) { if (options) {
@ -17,26 +14,26 @@ function PersistentSearchControl(options) {
if (options.criticality !== undefined && if (options.criticality !== undefined &&
typeof(options.criticality) !== 'boolean') typeof(options.criticality) !== 'boolean')
throw new TypeError('options.criticality must be a 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'); throw new TypeError('options.value must be a buffer');
} else { } else {
options = {}; options = {};
} }
this.type = options.type || ''; this.type = options.type || '2.16.840.1.113730.3.4.3';
this.criticality = options.criticality || false; this.criticality = options.criticality || false;
this.value = options.value || undefined;
if (this.value) { if (options.value) {
// parse out this.value into the PSC object // parse out this.value into the PSC object
var ber = new Ber.Reader(this.value); var ber = new asn1.BerReader(options.value);
ber.readSequence(); if (ber.readSequence()) {
this.value = { this.value = {
changeTypes: ber.readInt(), changeTypes: ber.readInt(),
changesOnly: ber.readBoolean(), changesOnly: ber.readBoolean(),
returnECs: ber.readBoolean() returnECs: ber.readBoolean()
}; };
} }
}
var self = this; var self = this;
this.__defineGetter__('json', function() { this.__defineGetter__('json', function() {
@ -46,7 +43,6 @@ function PersistentSearchControl(options) {
controlValue: self.value controlValue: self.value
}; };
}); });
} }
module.exports = PersistentSearchControl; module.exports = PersistentSearchControl;
@ -95,10 +91,10 @@ PersistentSearchControl.prototype.toBer = function(ber) {
assert.ok(ber); assert.ok(ber);
ber.startSequence(); ber.startSequence();
ber.writeString(this.type || ''); ber.writeString(this.type);
ber.writeBoolean(this.criticality); ber.writeBoolean(this.criticality);
var pscWriter = new Ber.Writer(); var pscWriter = new asn1.BerWriter();
// write the value subsequence // write the value subsequence
pscWriter.startSequence(); pscWriter.startSequence();

View File

@ -3,8 +3,6 @@
var Client = require('./client'); var Client = require('./client');
var Attribute = require('./attribute'); var Attribute = require('./attribute');
var Change = require('./change'); var Change = require('./change');
var Control = require('./control');
var PersistentSearchControl = require('./persistent_search_control');
var Protocol = require('./protocol'); var Protocol = require('./protocol');
var Server = require('./server'); var Server = require('./server');
@ -12,15 +10,11 @@ var assert = require('assert');
var dn = require('./dn'); var dn = require('./dn');
var errors = require('./errors'); var errors = require('./errors');
var filters = require('./filters'); var filters = require('./filters');
var log4js = require('log4js');
var logStub = require('./log_stub'); var logStub = require('./log_stub');
var messages = require('./messages'); var messages = require('./messages');
var schema = require('./schema'); var schema = require('./schema');
var url = require('./url'); 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) /// Hack a few things we need (i.e., "monkey patch" the prototype)
if (!String.prototype.startsWith) { if (!String.prototype.startsWith) {
@ -59,36 +53,6 @@ module.exports = {
Attribute: Attribute, Attribute: Attribute,
Change: Change, 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, DN: dn.DN,
RDN: dn.RDN, RDN: dn.RDN,

View File

@ -5,10 +5,9 @@ var util = require('util');
var asn1 = require('asn1'); var asn1 = require('asn1');
var Control = require('../control'); var Control = require('../controls/index').Control;
var Protocol = require('../protocol'); var Protocol = require('../protocol');
var log4js = require('log4js');
var logStub = require('../log_stub'); var logStub = require('../log_stub');
///--- Globals ///--- Globals
@ -16,11 +15,11 @@ var logStub = require('../log_stub');
var Ber = asn1.Ber; var Ber = asn1.Ber;
var BerReader = asn1.BerReader; var BerReader = asn1.BerReader;
var BerWriter = asn1.BerWriter; var BerWriter = asn1.BerWriter;
var getControl = require('../controls/index').getControl;
var LOG = log4js.getLogger('Message');
///--- API ///--- API
/** /**
* LDAPMessage structure. * LDAPMessage structure.
* *
@ -77,13 +76,12 @@ LDAPMessage.prototype.parse = function(ber) {
ber.readSequence(); ber.readSequence();
var end = ber.offset + ber.length; var end = ber.offset + ber.length;
while (ber.offset < end) { while (ber.offset < end) {
var getControl = require('../index').getControl;
var c = getControl(ber); var c = getControl(ber);
if (c) { if (c)
this.controls.push(c); this.controls.push(c);
} }
} }
}
if (this.log.isTraceEnabled()) if (this.log.isTraceEnabled())
this.log.trace('Parsing done: %j', this.json); this.log.trace('Parsing done: %j', this.json);

View File

@ -20,7 +20,6 @@
"asn1": "0.1.8", "asn1": "0.1.8",
"buffertools": "1.0.5", "buffertools": "1.0.5",
"dtrace-provider": "0.0.3", "dtrace-provider": "0.0.3",
"log4js": "0.4.1",
"nopt": "1.0.10", "nopt": "1.0.10",
"sprintf": "0.1.1" "sprintf": "0.1.1"
}, },

View File

@ -16,9 +16,9 @@ var getControl;
///--- Tests ///--- Tests
test('load library', function(t) { test('load library', function(t) {
Control = require('../lib/index').Control; Control = require('../../lib/controls/index').Control;
t.ok(Control); t.ok(Control);
getControl = require('../lib/index').getControl; getControl = require('../../lib/controls/index').getControl;
t.ok(getControl); t.ok(getControl);
t.end(); t.end();
}); });

View File

@ -17,9 +17,10 @@ var LOG = log4js.getLogger('persistent_search_control.test');
///--- Tests ///--- Tests
test('load library', function(t) { test('load library', function(t) {
PersistentSearchControl = require('../lib/index').PersistentSearchControl; PersistentSearchControl =
require('../../lib/controls/index').PersistentSearchControl;
t.ok(PersistentSearchControl); t.ok(PersistentSearchControl);
getControl = require('../lib/index').getControl; getControl = require('../../lib/controls/index').getControl;
t.ok(getControl); t.ok(getControl);
t.end(); t.end();
}); });