node-ldapjs/lib/messages/ext_request.js

117 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')
var util = require('util')
2011-08-04 20:32:01 +00:00
var LDAPMessage = require('./message')
var Protocol = require('../protocol')
2011-08-04 20:32:01 +00:00
/// --- API
2011-08-04 20:32:01 +00:00
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)
2011-08-04 20:32:01 +00:00
this.requestName = options.requestName || ''
this.requestValue = options.requestValue
2019-08-27 12:04:45 +00:00
if (Buffer.isBuffer(this.requestValue)) {
this.requestValueBuffer = this.requestValue
2019-08-27 12:04:45 +00:00
} 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') }
if (Buffer.isBuffer(val)) {
this.requestValueBuffer = val
2019-08-27 12:04:45 +00:00
} 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
2019-08-27 12:04:45 +00:00
},
set: function setValueBuffer (val) {
if (!Buffer.isBuffer(val)) { throw new TypeError('valueBuffer must be a buffer') }
2019-08-27 12:04:45 +00:00
this.value = val
2019-08-27 12:04:45 +00:00
},
configurable: false
}
})
2011-08-04 20:32:01 +00:00
ExtendedRequest.prototype._parse = function (ber) {
assert.ok(ber)
2011-08-04 20:32:01 +00:00
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')
2019-08-27 12:04:45 +00:00
}
2011-08-04 20:32:01 +00:00
return true
}
2011-08-04 20:32:01 +00:00
ExtendedRequest.prototype._toBer = function (ber) {
assert.ok(ber)
2011-08-04 20:32:01 +00:00
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') {
ber.writeString(this.requestValue, 0x81)
2014-01-17 17:52:26 +00:00
}
2011-08-04 20:32:01 +00:00
return ber
}
2011-08-04 20:32:01 +00:00
ExtendedRequest.prototype._json = function (j) {
assert.ok(j)
2011-08-04 20:32:01 +00:00
j.requestName = this.requestName
j.requestValue = (Buffer.isBuffer(this.requestValue))
? this.requestValue.toString('hex') : this.requestValue
return j
}
/// --- Exports
module.exports = ExtendedRequest