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

Commit 05445cb

Browse files
chrisbroomepfmooney
authored andcommitted
Move Attribute getters/setters into prototype
1 parent aab28a2 commit 05445cb

File tree

3 files changed

+121
-49
lines changed

3 files changed

+121
-49
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ coverage
66
*.tar.*
77
*.tgz
88
.DS_Store
9+
.idea

lib/attribute.js

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ var Protocol = require('./protocol');
99

1010
///--- API
1111

12+
/**
13+
* @constructor
14+
* @param [options={}]
15+
*/
1216
function Attribute(options) {
1317
if (options) {
1418
if (typeof (options) !== 'object')
@@ -19,63 +23,63 @@ function Attribute(options) {
1923
options = {};
2024
}
2125

22-
var self = this;
23-
2426
this.type = options.type || '';
2527
this._vals = [];
2628

27-
this.__defineGetter__('vals', function () {
28-
var _vals = [];
29-
30-
self._vals.forEach(function (v) {
31-
/* JSSTYLED */
32-
if (/;binary$/.test(self.type)) {
33-
_vals.push(v.toString('base64'));
34-
} else {
35-
_vals.push(v.toString('utf8'));
36-
}
37-
});
38-
39-
return _vals;
40-
});
41-
42-
this.__defineSetter__('vals', function (vals) {
43-
if (Array.isArray(vals)) {
44-
vals.forEach(function (v) {
45-
self.addValue(v);
46-
});
47-
} else {
48-
self.addValue(vals);
49-
}
50-
});
51-
52-
this.__defineGetter__('buffers', function () {
53-
return self._vals;
54-
});
55-
56-
this.__defineGetter__('json', function () {
57-
return {
58-
type: self.type,
59-
vals: self.vals
60-
};
61-
});
62-
6329
if (options.vals !== undefined && options.vals !== null)
6430
this.vals = options.vals;
65-
6631
}
32+
6733
module.exports = Attribute;
6834

69-
Attribute.prototype.addValue = function (val) {
70-
if (Buffer.isBuffer(val)) {
71-
this._vals.push(val);
72-
} else {
73-
var encoding = 'utf8';
74-
/* JSSTYLED */
75-
if (/;binary$/.test(this.type))
76-
encoding = 'base64';
77-
this._vals.push(new Buffer(val + '', encoding));
35+
Object.defineProperties(Attribute.prototype, {
36+
37+
buffers: {
38+
get: function () {
39+
var self = this;
40+
return self._vals;
41+
},
42+
configurable: false
43+
},
44+
45+
json: {
46+
get: function() {
47+
var self = this;
48+
return {
49+
type: self.type,
50+
vals: self.vals
51+
};
52+
},
53+
configurable: false
54+
},
55+
56+
vals: {
57+
get: function() {
58+
var self = this;
59+
var type = self.type;
60+
var _vals = self._vals;
61+
return _vals.map(function(v) {
62+
return v.toString(_getEncodingFromType(type));
63+
});
64+
},
65+
set: function(vals) {
66+
var self = this;
67+
this._vals = [];
68+
if (Array.isArray(vals)) {
69+
vals.forEach(function(v) {
70+
self.addValue(v);
71+
});
72+
} else {
73+
self.addValue(vals);
74+
}
75+
},
76+
configurable: false
7877
}
78+
79+
});
80+
81+
Attribute.prototype.addValue = function (val) {
82+
this._vals.push(_valueToBuffer(val, this.type));
7983
};
8084

8185

@@ -169,3 +173,27 @@ Attribute.isAttribute = function (attr) {
169173
Attribute.prototype.toString = function () {
170174
return JSON.stringify(this.json);
171175
};
176+
177+
/**
178+
* Gets the encoding to use based on the type of the attribute
179+
* @param {string} type
180+
* @returns {string}
181+
* @private
182+
*/
183+
function _getEncodingFromType(type) {
184+
/* JSSTYLED */
185+
return /;binary$/.test(type) ? 'base64' : 'utf8';
186+
}
187+
188+
/**
189+
* Converts a value to a buffer based on the given type
190+
* @param {*} val
191+
* @param {string} type
192+
* @returns {Buffer}
193+
* @private
194+
*/
195+
function _valueToBuffer(val, type) {
196+
return Buffer.isBuffer(val) ?
197+
val :
198+
new Buffer(val + '', _getEncodingFromType(type));
199+
}

test/attribute.test.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var test = require('tape').test;
44

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

7-
87
///--- Globals
98

109
var BerReader = asn1.BerReader;
@@ -34,6 +33,15 @@ test('new with args', function (t) {
3433
});
3534
t.ok(attr);
3635
attr.addValue('baz');
36+
t.throws(function() {
37+
new Attribute('not an object');
38+
});
39+
t.throws(function() {
40+
var typeThatIsNotAString = 1;
41+
new Attribute({
42+
type: typeThatIsNotAString
43+
})
44+
});
3745
t.equal(attr.type, 'cn');
3846
t.equal(attr.vals.length, 3);
3947
t.equal(attr.vals[0], 'foo');
@@ -81,6 +89,32 @@ test('parse', function (t) {
8189
t.end();
8290
});
8391

92+
test('parse - without 0x31', function(t) {
93+
var ber = new BerWriter;
94+
ber.startSequence();
95+
ber.writeString('sn');
96+
ber.endSequence();
97+
98+
var attr = new Attribute;
99+
t.ok(attr);
100+
t.ok(attr.parse(new BerReader(ber.buffer)));
101+
102+
t.equal(attr.type, 'sn');
103+
t.equal(attr.vals.length, 0);
104+
105+
t.end();
106+
});
107+
108+
test('toString', function(t) {
109+
var attr = new Attribute({
110+
type: 'foobar',
111+
vals: ['asdf']
112+
});
113+
var expected = attr.toString();
114+
var actual = JSON.stringify(attr.json);
115+
t.equal(actual, expected);
116+
t.end();
117+
});
84118

85119
test('isAttribute', function (t) {
86120
var isA = Attribute.isAttribute;
@@ -118,6 +152,15 @@ test('compare', function (t) {
118152
type: 'foo',
119153
vals: ['bar']
120154
});
155+
var notAnAttribute = 'this is not an attribute';
156+
157+
t.throws(function() {
158+
comp(a, notAnAttribute);
159+
});
160+
t.throws(function() {
161+
comp(notAnAttribute, b)
162+
});
163+
121164
t.equal(comp(a, b), 0);
122165

123166
// Different types

0 commit comments

Comments
 (0)