node-ldapjs/lib/messages/ext_request.js

121 lines
3.0 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-plus');
2011-08-04 20:32:01 +00:00
var util = require('util');
var LDAPMessage = require('./message');
var Protocol = require('../protocol');
///--- API
function ExtendedRequest(options) {
options = options || {};
assert.object(options);
assert.optionalString(options.requestName);
if (options.requestValue &&
!(Buffer.isBuffer(options.requestValue) ||
typeof (options.requestValue) === 'string')) {
throw new TypeError('options.requestValue must be a buffer or a string');
2011-08-04 20:32:01 +00:00
}
options.protocolOp = Protocol.LDAP_REQ_EXTENSION;
LDAPMessage.call(this, options);
this.requestName = options.requestName || '';
2014-01-17 17:52:26 +00:00
this.requestValue = options.requestValue;
2019-08-27 12:04:45 +00:00
if(Buffer.isBuffer(this.requestValue)) {
this.requestValueBuffer = this.requestValue;
} else {
this.requestValueBuffer = Buffer.from(this.requestValue || '', 'utf8');
2019-08-27 12:04:45 +00:00
}
2011-08-04 20:32:01 +00:00
}
util.inherits(ExtendedRequest, LDAPMessage);
Object.defineProperties(ExtendedRequest.prototype, {
type: {
get: function getType() { return 'ExtendedRequest'; },
configurable: false
},
_dn: {
get: function getDN() { return this.requestName; },
configurable: false
},
name: {
get: function getName() { return this.requestName; },
set: function setName(val) {
assert.string(val);
this.requestName = val;
},
configurable: false
},
value: {
get: function getValue() { return this.requestValue; },
set: function setValue(val) {
if (!(Buffer.isBuffer(val) || typeof (val) === 'string'))
throw new TypeError('value must be a buffer or a string');
2019-08-27 12:04:45 +00:00
if(Buffer.isBuffer(val)) {
this.requestValueBuffer = val;
} else {
this.requestValueBuffer = Buffer.from(val, 'utf8');
2019-08-27 12:04:45 +00:00
}
this.requestValue = val;
},
configurable: false
2019-08-27 12:04:45 +00:00
},
valueBuffer: {
get: function getValueBuffer() {
return this.requestValueBuffer;
},
set: function setValueBuffer(val) {
if(!Buffer.isBuffer(val))
throw new TypeError('valueBuffer must be a buffer');
this.value = val;
},
configurable: false
}
});
2011-08-04 20:32:01 +00:00
ExtendedRequest.prototype._parse = function (ber) {
2011-08-04 20:32:01 +00:00
assert.ok(ber);
this.requestName = ber.readString(0x80);
2019-08-27 12:04:45 +00:00
if (ber.peek() === 0x81) {
this.requestValueBuffer = ber.readString(0x81, true);
this.requestValue = this.requestValueBuffer.toString('utf8');
}
2011-08-04 20:32:01 +00:00
return true;
};
ExtendedRequest.prototype._toBer = function (ber) {
2011-08-04 20:32:01 +00:00
assert.ok(ber);
ber.writeString(this.requestName, 0x80);
2015-04-26 03:36:36 +00:00
if (Buffer.isBuffer(this.requestValue)) {
ber.writeBuffer(this.requestValue, 0x81);
2015-04-26 03:36:36 +00:00
} else if (typeof (this.requestValue) === 'string') {
2011-08-04 20:32:01 +00:00
ber.writeString(this.requestValue, 0x81);
2014-01-17 17:52:26 +00:00
}
2011-08-04 20:32:01 +00:00
return ber;
};
ExtendedRequest.prototype._json = function (j) {
2011-08-04 20:32:01 +00:00
assert.ok(j);
j.requestName = this.requestName;
2014-01-17 17:52:26 +00:00
j.requestValue = (Buffer.isBuffer(this.requestValue)) ?
this.requestValue.toString('hex') : this.requestValue;
2011-08-04 20:32:01 +00:00
return j;
};
///--- Exports
module.exports = ExtendedRequest;