UNPKG

2.08 kBJavaScriptView Raw
1// Copyright 2011 Mark Cavage, Inc. All rights reserved.
2
3const assert = require('assert-plus')
4const util = require('util')
5
6// var asn1 = require('asn1')
7
8const LDAPMessage = require('./message')
9const Protocol = require('../protocol')
10const dn = require('../dn')
11const url = require('../url')
12
13/// --- Globals
14
15// var BerWriter = asn1.BerWriter
16const parseURL = url.parse
17
18/// --- API
19
20function 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}
29util.inherits(SearchReference, LDAPMessage)
30Object.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
59SearchReference.prototype.toObject = function () {
60 return this.object
61}
62
63SearchReference.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
71SearchReference.prototype._json = function (j) {
72 assert.ok(j)
73 j.uris = this.uris.slice()
74 return j
75}
76
77SearchReference.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
89SearchReference.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/// --- Exports
100
101module.exports = SearchReference