Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 540a3a1

Browse files
committed
Clean up asserts and getters in controls
1 parent 69c72dc commit 540a3a1

8 files changed

+106
-142
lines changed

lib/controls/control.js

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,48 @@
11
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
22

3-
var assert = require('assert');
3+
var assert = require('assert-plus');
44
var util = require('util');
55

66
var asn1 = require('asn1');
77

88
var Protocol = require('../protocol');
99

1010

11-
1211
///--- Globals
1312

1413
var Ber = asn1.Ber;
1514

1615

17-
1816
///--- API
1917

2018
function Control(options) {
21-
if (options) {
22-
if (typeof (options) !== 'object')
23-
throw new TypeError('options must be an object');
24-
if (options.type && typeof (options.type) !== 'string')
25-
throw new TypeError('options.type must be a string');
26-
if (options.criticality !== undefined &&
27-
typeof (options.criticality) !== 'boolean')
28-
throw new TypeError('options.criticality must be a boolean');
29-
if (options.value && !Buffer.isBuffer(options.value))
30-
throw new TypeError('options.value must be a Buffer');
31-
} else {
32-
options = {};
19+
assert.optionalObject(options);
20+
options = options || {};
21+
assert.optionalString(options.type);
22+
assert.optionalBool(options.criticality);
23+
if (options.value) {
24+
console.log(assert.buffer.toString());
25+
assert.buffer(options.value);
3326
}
3427

3528
this.type = options.type || '';
3629
this.criticality = options.critical || options.criticality || false;
3730
this.value = options.value || null;
38-
39-
var self = this;
40-
this.__defineGetter__('json', function () {
41-
var obj = {
42-
controlType: self.type,
43-
criticality: self.criticality,
44-
controlValue: self.value
45-
};
46-
return (typeof (self._json) === 'function' ? self._json(obj) : obj);
47-
});
4831
}
49-
module.exports = Control;
50-
32+
Object.defineProperties(Control.prototype, {
33+
json: {
34+
get: function getJson() {
35+
var obj = {
36+
controlType: this.type,
37+
criticality: this.criticality,
38+
controlValue: this.value
39+
};
40+
return (typeof (this._json) === 'function' ? this._json(obj) : obj);
41+
}
42+
}
43+
});
5144

52-
Control.prototype.toBer = function (ber) {
45+
Control.prototype.toBer = function toBer(ber) {
5346
assert.ok(ber);
5447

5548
ber.startSequence();
@@ -66,7 +59,10 @@ Control.prototype.toBer = function (ber) {
6659
return;
6760
};
6861

69-
70-
Control.prototype.toString = function () {
62+
Control.prototype.toString = function toString() {
7163
return this.json;
7264
};
65+
66+
67+
///--- Exports
68+
module.exports = Control;

lib/controls/entry_change_notification_control.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
var assert = require('assert');
1+
var assert = require('assert-plus');
22
var util = require('util');
33

44
var asn1 = require('asn1');
55

66
var Control = require('./control');
77

88

9-
109
///--- Globals
1110

1211
var BerReader = asn1.BerReader;
1312
var BerWriter = asn1.BerWriter;
1413

1514

16-
1715
///--- API
1816

1917
function EntryChangeNotificationControl(options) {
20-
if (!options)
21-
options = {};
22-
18+
assert.optionalObject(options);
19+
options = options || {};
2320
options.type = EntryChangeNotificationControl.OID;
2421
if (options.value) {
2522
if (Buffer.isBuffer(options.value)) {
@@ -32,15 +29,14 @@ function EntryChangeNotificationControl(options) {
3229
options.value = null;
3330
}
3431
Control.call(this, options);
35-
36-
var self = this;
37-
this.__defineGetter__('value', function () {
38-
return self._value || {};
39-
});
4032
}
4133
util.inherits(EntryChangeNotificationControl, Control);
42-
module.exports = EntryChangeNotificationControl;
43-
34+
Object.defineProperties(EntryChangeNotificationControl.prototype, {
35+
value: {
36+
get: function () { return this._value || {}; },
37+
configurable: false
38+
}
39+
});
4440

4541
EntryChangeNotificationControl.prototype.parse = function parse(buffer) {
4642
assert.ok(buffer);
@@ -63,7 +59,6 @@ EntryChangeNotificationControl.prototype.parse = function parse(buffer) {
6359
return false;
6460
};
6561

66-
6762
EntryChangeNotificationControl.prototype._toBer = function (ber) {
6863
assert.ok(ber);
6964

@@ -82,12 +77,13 @@ EntryChangeNotificationControl.prototype._toBer = function (ber) {
8277
ber.writeBuffer(writer.buffer, 0x04);
8378
};
8479

85-
8680
EntryChangeNotificationControl.prototype._json = function (obj) {
8781
obj.controlValue = this.value;
8882
return obj;
8983
};
9084

85+
EntryChangeNotificationControl.OID = '2.16.840.1.113730.3.4.7';
9186

9287

93-
EntryChangeNotificationControl.OID = '2.16.840.1.113730.3.4.7';
88+
///--- Exports
89+
module.exports = EntryChangeNotificationControl;

lib/controls/index.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,60 +26,44 @@ module.exports = {
2626
return null;
2727

2828
var type;
29-
var critical = false;
30-
var value;
29+
var opts = {
30+
criticality: false,
31+
value: null
32+
};
3133

3234
if (ber.length) {
3335
var end = ber.offset + ber.length;
3436

3537
type = ber.readString();
3638
if (ber.offset < end) {
3739
if (ber.peek() === Ber.Boolean)
38-
critical = ber.readBoolean();
40+
opts.criticality = ber.readBoolean();
3941
}
4042

4143
if (ber.offset < end)
42-
value = ber.readString(Ber.OctetString, true);
44+
opts.value = ber.readString(Ber.OctetString, true);
4345
}
4446

4547
var control;
4648
switch (type) {
4749
case PersistentSearchControl.OID:
48-
control = new PersistentSearchControl({
49-
critical: critical,
50-
value: value
51-
});
50+
control = new PersistentSearchControl(opts);
5251
break;
5352
case EntryChangeNotificationControl.OID:
54-
control = new EntryChangeNotificationControl({
55-
critical: critical,
56-
value: value
57-
});
53+
control = new EntryChangeNotificationControl(opts);
5854
break;
5955
case PagedResultsControl.OID:
60-
control = new PagedResultsControl({
61-
critical: critical,
62-
value: value
63-
});
56+
control = new PagedResultsControl(opts);
6457
break;
6558
case ServerSideSortingRequestControl.OID:
66-
control = new ServerSideSortingRequestControl({
67-
critical: critical,
68-
value: value
69-
});
59+
control = new ServerSideSortingRequestControl(opts);
7060
break;
7161
case ServerSideSortingResponseControl.OID:
72-
control = new ServerSideSortingResponseControl({
73-
critical: critical,
74-
value: value
75-
});
62+
control = new ServerSideSortingResponseControl(opts);
7663
break;
7764
default:
78-
control = new Control({
79-
type: type,
80-
critical: critical,
81-
value: value
82-
});
65+
opts.type = type;
66+
control = new Control(opts);
8367
break;
8468
}
8569

lib/controls/paged_results_control.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
var assert = require('assert');
1+
var assert = require('assert-plus');
22
var util = require('util');
33

44
var asn1 = require('asn1');
55

66
var Control = require('./control');
77

88

9-
109
///--- Globals
1110

1211
var BerReader = asn1.BerReader;
1312
var BerWriter = asn1.BerWriter;
1413

1514

16-
1715
///--- API
1816

1917
function PagedResultsControl(options) {
20-
if (!options)
21-
options = {};
22-
18+
assert.optionalObject(options);
19+
options = options || {};
2320
options.type = PagedResultsControl.OID;
2421
if (options.value) {
2522
if (Buffer.isBuffer(options.value)) {
@@ -32,15 +29,14 @@ function PagedResultsControl(options) {
3229
options.value = null;
3330
}
3431
Control.call(this, options);
35-
36-
var self = this;
37-
this.__defineGetter__('value', function () {
38-
return self._value || {};
39-
});
4032
}
4133
util.inherits(PagedResultsControl, Control);
42-
module.exports = PagedResultsControl;
43-
34+
Object.defineProperties(PagedResultsControl.prototype, {
35+
value: {
36+
get: function () { return this._value || {}; },
37+
configurable: false
38+
}
39+
});
4440

4541
PagedResultsControl.prototype.parse = function parse(buffer) {
4642
assert.ok(buffer);
@@ -60,7 +56,6 @@ PagedResultsControl.prototype.parse = function parse(buffer) {
6056
return false;
6157
};
6258

63-
6459
PagedResultsControl.prototype._toBer = function (ber) {
6560
assert.ok(ber);
6661

@@ -80,12 +75,13 @@ PagedResultsControl.prototype._toBer = function (ber) {
8075
ber.writeBuffer(writer.buffer, 0x04);
8176
};
8277

83-
8478
PagedResultsControl.prototype._json = function (obj) {
8579
obj.controlValue = this.value;
8680
return obj;
8781
};
8882

83+
PagedResultsControl.OID = '1.2.840.113556.1.4.319';
8984

9085

91-
PagedResultsControl.OID = '1.2.840.113556.1.4.319';
86+
///--- Exports
87+
module.exports = PagedResultsControl;

lib/controls/persistent_search_control.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
22

3-
var assert = require('assert');
3+
var assert = require('assert-plus');
44
var util = require('util');
55

66
var asn1 = require('asn1');
77

88
var Control = require('./control');
99

1010

11-
1211
///--- Globals
1312

1413
var BerReader = asn1.BerReader;
1514
var BerWriter = asn1.BerWriter;
1615

1716

18-
1917
///--- API
2018

2119
function PersistentSearchControl(options) {
22-
if (!options)
23-
options = {};
24-
20+
assert.optionalObject(options);
21+
options = options || {};
2522
options.type = PersistentSearchControl.OID;
23+
2624
if (options.value) {
2725
if (Buffer.isBuffer(options.value)) {
2826
this.parse(options.value);
@@ -34,15 +32,14 @@ function PersistentSearchControl(options) {
3432
options.value = null;
3533
}
3634
Control.call(this, options);
37-
38-
var self = this;
39-
this.__defineGetter__('value', function () {
40-
return self._value || {};
41-
});
4235
}
4336
util.inherits(PersistentSearchControl, Control);
44-
module.exports = PersistentSearchControl;
45-
37+
Object.defineProperties(PersistentSearchControl.prototype, {
38+
value: {
39+
get: function () { return this._value || {}; },
40+
configurable: false
41+
}
42+
});
4643

4744
PersistentSearchControl.prototype.parse = function parse(buffer) {
4845
assert.ok(buffer);
@@ -61,7 +58,6 @@ PersistentSearchControl.prototype.parse = function parse(buffer) {
6158
return false;
6259
};
6360

64-
6561
PersistentSearchControl.prototype._toBer = function (ber) {
6662
assert.ok(ber);
6763

@@ -78,12 +74,12 @@ PersistentSearchControl.prototype._toBer = function (ber) {
7874
ber.writeBuffer(writer.buffer, 0x04);
7975
};
8076

81-
8277
PersistentSearchControl.prototype._json = function (obj) {
8378
obj.controlValue = this.value;
8479
return obj;
8580
};
8681

87-
88-
8982
PersistentSearchControl.OID = '2.16.840.1.113730.3.4.3';
83+
84+
///--- Exports
85+
module.exports = PersistentSearchControl;

0 commit comments

Comments
 (0)