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

Commit df43cab

Browse files
committed
Clean up getters/setters and asserts in messages
1 parent cefd6f4 commit df43cab

28 files changed

+592
-621
lines changed

lib/assert.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2015 Joyent, Inc.
2+
3+
var assert = require('assert');
4+
var util = require('util');
5+
6+
var isDN = require('./dn').DN.isDN;
7+
var isAttribute = require('./attribute').isAttribute;
8+
9+
10+
///--- Helpers
11+
12+
// Copied from mcavage/node-assert-plus
13+
function _assert(arg, type, name) {
14+
name = name || type;
15+
throw new assert.AssertionError({
16+
message: util.format('%s (%s) required', name, type),
17+
actual: typeof (arg),
18+
expected: type,
19+
operator: '===',
20+
stackStartFunction: _assert.caller
21+
});
22+
}
23+
24+
25+
///--- API
26+
27+
function stringDN(input, name) {
28+
if (isDN(input) || typeof (input) === 'string')
29+
return;
30+
_assert(input, 'DN or string', name);
31+
}
32+
33+
function optionalStringDN(input, name) {
34+
if (input === undefined || isDN(input) || typeof (input) === 'string')
35+
return;
36+
_assert(input, 'DN or string', name);
37+
}
38+
39+
function optionalDN(input, name) {
40+
if (input !== undefined && !isDN(input))
41+
_assert(input, 'DN', name);
42+
}
43+
44+
function optionalArrayOfAttribute(input, name) {
45+
if (input === undefined)
46+
return;
47+
if (!Array.isArray(input) ||
48+
input.some(function (v) { return !isAttribute(v); })) {
49+
_assert(input, 'array of Attribute', name);
50+
}
51+
}
52+
53+
54+
///--- Exports
55+
56+
module.exports = {
57+
stringDN: stringDN,
58+
optionalStringDN: optionalStringDN,
59+
optionalDN: optionalDN,
60+
optionalArrayOfAttribute: optionalArrayOfAttribute
61+
};

lib/messages/abandon_request.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
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 LDAPMessage = require('./message');
77
var Protocol = require('../protocol');
88

99

10-
1110
///--- API
1211

1312
function AbandonRequest(options) {
14-
if (options) {
15-
if (typeof (options) !== 'object')
16-
throw new TypeError('options must be an object');
17-
if (options.abandonID && typeof (options.abandonID) !== 'number')
18-
throw new TypeError('abandonID must be a number');
19-
} else {
20-
options = {};
21-
}
13+
options = options || {};
14+
assert.object(options);
15+
assert.optionalNumber(options.abandonID);
2216

2317
options.protocolOp = Protocol.LDAP_REQ_ABANDON;
2418
LDAPMessage.call(this, options);
2519

2620
this.abandonID = options.abandonID || 0;
27-
28-
this.__defineGetter__('type', function () { return 'AbandonRequest'; });
2921
}
3022
util.inherits(AbandonRequest, LDAPMessage);
31-
module.exports = AbandonRequest;
32-
23+
Object.defineProperties(AbandonRequest.prototype, {
24+
type: {
25+
get: function getType() { return 'AbandonRequest'; },
26+
configurable: false
27+
}
28+
});
3329

3430
AbandonRequest.prototype._parse = function (ber, length) {
3531
assert.ok(ber);
@@ -59,7 +55,6 @@ AbandonRequest.prototype._parse = function (ber, length) {
5955
return true;
6056
};
6157

62-
6358
AbandonRequest.prototype._toBer = function (ber) {
6459
assert.ok(ber);
6560

@@ -81,11 +76,15 @@ AbandonRequest.prototype._toBer = function (ber) {
8176
return ber;
8277
};
8378

84-
8579
AbandonRequest.prototype._json = function (j) {
8680
assert.ok(j);
8781

8882
j.abandonID = this.abandonID;
8983

9084
return j;
9185
};
86+
87+
88+
///--- Exports
89+
90+
module.exports = AbandonRequest;

lib/messages/abandon_response.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
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 LDAPMessage = require('./result');
77
var Protocol = require('../protocol');
88

99

1010
///--- API
11-
// Stub this out
1211

1312
function AbandonResponse(options) {
14-
if (!options)
15-
options = {};
16-
if (typeof (options) !== 'object')
17-
throw new TypeError('options must be an object');
13+
options = options || {};
14+
assert.object(options);
1815

1916
options.protocolOp = 0;
2017
LDAPMessage.call(this, options);
21-
this.__defineGetter__('type', function () { return 'AbandonResponse'; });
2218
}
2319
util.inherits(AbandonResponse, LDAPMessage);
24-
module.exports = AbandonResponse;
25-
20+
Object.defineProperties(AbandonResponse.prototype, {
21+
type: {
22+
get: function getType() { return 'AbandonResponse'; },
23+
configurable: false
24+
}
25+
});
2626

2727
AbandonResponse.prototype.end = function (status) {};
2828

29-
3029
AbandonResponse.prototype._json = function (j) {
3130
return j;
3231
};
32+
33+
34+
///--- Exports
35+
36+
module.exports = AbandonResponse;

lib/messages/add_request.js

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,39 @@
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 LDAPMessage = require('./message');
7-
var dn = require('../dn');
87
var Attribute = require('../attribute');
98
var Protocol = require('../protocol');
10-
11-
12-
13-
///--- Globals
14-
15-
var isDN = dn.DN.isDN;
9+
var lassert = require('../assert');
1610

1711

1812
///--- API
1913

2014
function AddRequest(options) {
21-
if (options) {
22-
if (typeof (options) !== 'object')
23-
throw new TypeError('options must be an object');
24-
if (options.entry &&
25-
!(isDN(options.entry) || typeof (options.entry) === 'string')) {
26-
throw new TypeError('options.entry must be a DN or string');
27-
}
28-
if (options.attributes) {
29-
if (!Array.isArray(options.attributes))
30-
throw new TypeError('options.attributes must be [Attribute]');
31-
options.attributes.forEach(function (a) {
32-
if (!Attribute.isAttribute(a))
33-
throw new TypeError('options.attributes must be [Attribute]');
34-
});
35-
}
36-
} else {
37-
options = {};
38-
}
15+
options = options || {};
16+
assert.object(options);
17+
lassert.optionalStringDN(options.entry);
18+
lassert.optionalArrayOfAttribute(options.attributes);
3919

4020
options.protocolOp = Protocol.LDAP_REQ_ADD;
4121
LDAPMessage.call(this, options);
4222

4323
this.entry = options.entry || null;
4424
this.attributes = options.attributes ? options.attributes.slice(0) : [];
45-
46-
var self = this;
47-
this.__defineGetter__('type', function () { return 'AddRequest'; });
48-
this.__defineGetter__('_dn', function () { return self.entry; });
4925
}
5026
util.inherits(AddRequest, LDAPMessage);
51-
module.exports = AddRequest;
52-
27+
Object.defineProperties(AddRequest.prototype, {
28+
type: {
29+
get: function getType() { return 'AddRequest'; },
30+
configurable: false
31+
},
32+
_dn: {
33+
get: function getDN() { return this.entry; },
34+
configurable: false
35+
}
36+
});
5337

5438
AddRequest.prototype._parse = function (ber) {
5539
assert.ok(ber);
@@ -74,7 +58,6 @@ AddRequest.prototype._parse = function (ber) {
7458
return true;
7559
};
7660

77-
7861
AddRequest.prototype._toBer = function (ber) {
7962
assert.ok(ber);
8063

@@ -88,7 +71,6 @@ AddRequest.prototype._toBer = function (ber) {
8871
return ber;
8972
};
9073

91-
9274
AddRequest.prototype._json = function (j) {
9375
assert.ok(j);
9476

@@ -102,7 +84,6 @@ AddRequest.prototype._json = function (j) {
10284
return j;
10385
};
10486

105-
10687
AddRequest.prototype.indexOf = function (attr) {
10788
if (!attr || typeof (attr) !== 'string')
10889
throw new TypeError('attr (string) required');
@@ -115,7 +96,6 @@ AddRequest.prototype.indexOf = function (attr) {
11596
return -1;
11697
};
11798

118-
11999
AddRequest.prototype.attributeNames = function () {
120100
var attrs = [];
121101

@@ -125,7 +105,6 @@ AddRequest.prototype.attributeNames = function () {
125105
return attrs;
126106
};
127107

128-
129108
AddRequest.prototype.getAttribute = function (name) {
130109
if (!name || typeof (name) !== 'string')
131110
throw new TypeError('attribute name (string) required');
@@ -140,15 +119,13 @@ AddRequest.prototype.getAttribute = function (name) {
140119
return null;
141120
};
142121

143-
144122
AddRequest.prototype.addAttribute = function (attr) {
145123
if (!(attr instanceof Attribute))
146124
throw new TypeError('attribute (Attribute) required');
147125

148126
return this.attributes.push(attr);
149127
};
150128

151-
152129
/**
153130
* Returns a "pure" JS representation of this object.
154131
*
@@ -187,3 +164,8 @@ AddRequest.prototype.toObject = function () {
187164

188165
return obj;
189166
};
167+
168+
169+
///--- Exports
170+
171+
module.exports = AddRequest;

lib/messages/add_response.js

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

3+
var assert = require('assert-plus');
34
var util = require('util');
45

56
var LDAPResult = require('./result');
67
var Protocol = require('../protocol');
78

89

9-
1010
///--- API
1111

1212
function AddResponse(options) {
13-
if (options) {
14-
if (typeof (options) !== 'object')
15-
throw new TypeError('options must be an object');
16-
} else {
17-
options = {};
18-
}
13+
options = options || {};
14+
assert.object(options);
1915

2016
options.protocolOp = Protocol.LDAP_REP_ADD;
2117
LDAPResult.call(this, options);
2218
}
2319
util.inherits(AddResponse, LDAPResult);
20+
21+
22+
///--- Exports
23+
2424
module.exports = AddResponse;

0 commit comments

Comments
 (0)