1 |
|
2 |
|
3 | const assert = require('assert-plus')
|
4 | const util = require('util')
|
5 |
|
6 |
|
7 |
|
8 | const LDAPMessage = require('./message')
|
9 | const Protocol = require('../protocol')
|
10 | const dn = require('../dn')
|
11 | const url = require('../url')
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const parseURL = url.parse
|
17 |
|
18 |
|
19 |
|
20 | function SearchReference (options) {
|
21 | options = options || {}
|
22 | assert.object(options)
|
23 |
|
24 | options.protocolOp = Protocol.LDAP_REP_SEARCH_REF
|
25 | LDAPMessage.call(this, options)
|
26 |
|
27 | this.uris = options.uris || []
|
28 | }
|
29 | util.inherits(SearchReference, LDAPMessage)
|
30 | Object.defineProperties(SearchReference.prototype, {
|
31 | type: {
|
32 | get: function getType () { return 'SearchReference' },
|
33 | configurable: false
|
34 | },
|
35 | _dn: {
|
36 | get: function getDN () { return new dn.DN('') },
|
37 | configurable: false
|
38 | },
|
39 | object: {
|
40 | get: function getObject () {
|
41 | return {
|
42 | dn: this.dn.toString(),
|
43 | uris: this.uris.slice()
|
44 | }
|
45 | },
|
46 | configurable: false
|
47 | },
|
48 | urls: {
|
49 | get: function getUrls () { return this.uris },
|
50 | set: function setUrls (val) {
|
51 | assert.ok(val)
|
52 | assert.ok(Array.isArray(val))
|
53 | this.uris = val.slice()
|
54 | },
|
55 | configurable: false
|
56 | }
|
57 | })
|
58 |
|
59 | SearchReference.prototype.toObject = function () {
|
60 | return this.object
|
61 | }
|
62 |
|
63 | SearchReference.prototype.fromObject = function (obj) {
|
64 | if (typeof (obj) !== 'object') { throw new TypeError('object required') }
|
65 |
|
66 | this.uris = obj.uris ? obj.uris.slice() : []
|
67 |
|
68 | return true
|
69 | }
|
70 |
|
71 | SearchReference.prototype._json = function (j) {
|
72 | assert.ok(j)
|
73 | j.uris = this.uris.slice()
|
74 | return j
|
75 | }
|
76 |
|
77 | SearchReference.prototype._parse = function (ber, length) {
|
78 | assert.ok(ber)
|
79 |
|
80 | while (ber.offset < length) {
|
81 | const _url = ber.readString()
|
82 | parseURL(_url)
|
83 | this.uris.push(_url)
|
84 | }
|
85 |
|
86 | return true
|
87 | }
|
88 |
|
89 | SearchReference.prototype._toBer = function (ber) {
|
90 | assert.ok(ber)
|
91 |
|
92 | this.uris.forEach(function (u) {
|
93 | ber.writeString(u.href || u)
|
94 | })
|
95 |
|
96 | return ber
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 | module.exports = SearchReference
|