node-ldapjs/lib/attribute.js

200 lines
4.1 KiB
JavaScript
Raw Normal View History

2011-08-04 20:32:01 +00:00
// Copyright 2011 Mark Cavage, Inc. All rights reserved.
var assert = require('assert');
2011-10-11 20:56:16 +00:00
var asn1 = require('asn1');
2011-08-04 20:32:01 +00:00
var Protocol = require('./protocol');
///--- API
/**
* @constructor
* @param [options={}]
*/
2011-08-04 20:32:01 +00:00
function Attribute(options) {
if (options) {
if (typeof (options) !== 'object')
2011-08-04 20:32:01 +00:00
throw new TypeError('options must be an object');
if (options.type && typeof (options.type) !== 'string')
2011-08-04 20:32:01 +00:00
throw new TypeError('options.type must be a string');
} else {
options = {};
}
this.type = options.type || '';
2011-10-11 20:56:16 +00:00
this._vals = [];
if (options.vals !== undefined && options.vals !== null)
2011-10-11 20:56:16 +00:00
this.vals = options.vals;
2011-08-04 20:32:01 +00:00
}
2011-08-04 20:32:01 +00:00
module.exports = Attribute;
Object.defineProperties(Attribute.prototype, {
buffers: {
get: function () {
var self = this;
return self._vals;
},
configurable: false
},
json: {
get: function() {
var self = this;
return {
type: self.type,
vals: self.vals
};
},
configurable: false
},
vals: {
get: function() {
var self = this;
var type = self.type;
var _vals = self._vals;
return _vals.map(function(v) {
return v.toString(_getEncodingFromType(type));
});
},
set: function(vals) {
var self = this;
this._vals = [];
if (Array.isArray(vals)) {
vals.forEach(function(v) {
self.addValue(v);
});
} else {
self.addValue(vals);
}
},
configurable: false
2011-10-11 20:56:16 +00:00
}
});
Attribute.prototype.addValue = function (val) {
this._vals.push(_valueToBuffer(val, this.type));
2011-10-11 20:56:16 +00:00
};
/* BEGIN JSSTYLED */
Attribute.compare = function compare(a, b) {
if (!(Attribute.isAttribute(a)) || !(Attribute.isAttribute(b))) {
throw new TypeError('can only compare Attributes');
}
if (a.type < b.type) return -1;
if (a.type > b.type) return 1;
if (a.vals.length < b.vals.length) return -1;
if (a.vals.length > b.vals.length) return 1;
for (var i = 0; i < a.vals.length; i++) {
if (a.vals[i] < b.vals[i]) return -1;
if (a.vals[i] > b.vals[i]) return 1;
}
return 0;
};
/* END JSSTYLED */
2011-08-04 20:32:01 +00:00
Attribute.prototype.parse = function (ber) {
2011-08-04 20:32:01 +00:00
assert.ok(ber);
ber.readSequence();
this.type = ber.readString();
2011-08-04 20:32:01 +00:00
if (ber.peek() === Protocol.LBER_SET) {
if (ber.readSequence(Protocol.LBER_SET)) {
var end = ber.offset + ber.length;
while (ber.offset < end)
2011-10-11 20:56:16 +00:00
this._vals.push(ber.readString(asn1.Ber.OctetString, true));
}
2011-08-04 20:32:01 +00:00
}
return true;
};
Attribute.prototype.toBer = function (ber) {
2011-08-04 20:32:01 +00:00
assert.ok(ber);
ber.startSequence();
ber.writeString(this.type);
ber.startSequence(Protocol.LBER_SET);
if (this._vals.length) {
this._vals.forEach(function (b) {
ber.writeByte(asn1.Ber.OctetString);
ber.writeLength(b.length);
for (var i = 0; i < b.length; i++)
ber.writeByte(b[i]);
});
} else {
ber.writeStringArray([]);
}
ber.endSequence();
2011-08-04 20:32:01 +00:00
ber.endSequence();
return ber;
};
Attribute.toBer = function (attr, ber) {
2011-08-04 20:32:01 +00:00
return Attribute.prototype.toBer.call(attr, ber);
};
Attribute.isAttribute = function (attr) {
if (!attr || typeof (attr) !== 'object') {
return false;
2012-01-24 17:43:46 +00:00
}
if (attr instanceof Attribute) {
return true;
}
if ((typeof (attr.toBer) === 'function') &&
(typeof (attr.type) === 'string') &&
(Array.isArray(attr.vals)) &&
(attr.vals.filter(function (item) {
return (typeof (item) === 'string' ||
Buffer.isBuffer(item));
}).length === attr.vals.length)) {
return true;
}
return false;
2011-08-04 20:32:01 +00:00
};
Attribute.prototype.toString = function () {
2011-08-04 20:32:01 +00:00
return JSON.stringify(this.json);
};
/**
* Gets the encoding to use based on the type of the attribute
* @param {string} type
* @returns {string}
* @private
*/
function _getEncodingFromType(type) {
/* JSSTYLED */
return /;binary$/.test(type) ? 'base64' : 'utf8';
}
/**
* Converts a value to a buffer based on the given type
* @param {*} val
* @param {string} type
* @returns {Buffer}
* @private
*/
function _valueToBuffer(val, type) {
return Buffer.isBuffer(val) ?
val :
new Buffer(val + '', _getEncodingFromType(type));
}