wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict'
 
const { BerReader, BerWriter } = require('@ldapjs/asn1')
const isObject = require('../is-object')
const hasOwn = require('../has-own')
const Control = require('../control')
 
/**
 * @typedef {object} EntryChangeNotificationControlValue
 * @property {number} changeType One of 1 (add), 2 (delete), 4 (modify),
 * or 8 (modifyDN).
 * @property {string} previousDN Only set when operation is a modifyDN op.
 * @property {number} changeNumber
 */
 
/**
 * Implements:
 * https://datatracker.ietf.org/doc/html/draft-ietf-ldapext-psearch-03.txt#section-5
 *
 * @extends Control
 */
class EntryChangeNotificationControl extends Control {
  static OID = '2.16.840.1.113730.3.4.7'
 
  /**
   * @typedef {ControlParams} EntryChangeNotificationParams
   * @property {EntryChangeNotificationControlValue | Buffer} [value]
   */
 
  /**
   * Creates a new persistent search control.
   *
   * @param {EntryChangeNotificationParams} [options]
   */
  constructor (options = {}) {
    options.type = EntryChangeNotificationControl.OID
    super(options)
 
    this._value = {
      changeType: 4
    }
 
    if (hasOwn(options, 'value') === false) {
      return
    }
 
    if (Buffer.isBuffer(options.value)) {
      this.#parse(options.value)
    } else if (isObject(options.value)) {
      this._value = options.value
    } else {
      throw new TypeError('options.value must be a Buffer or Object')
    }
  }
 
  get value () {
    return this._value
  }
 
  set value (obj) {
    this._value = Object.assign({}, this._value, obj)
  }
 
  /**
   * Given a BER buffer that represents a
   * {@link EntryChangeNotificationControlValue}, read that buffer into the
   * current instance.
   */
  #parse (buffer) {
    const ber = new BerReader(buffer)
    /* istanbul ignore else */
    if (ber.readSequence()) {
      this._value = {
        changeType: ber.readInt()
      }
 
      /* istanbul ignore else */
      if (this._value.changeType === 8) {
        // If the operation was moddn, then parse the optional previousDN attr.
        this._value.previousDN = ber.readString()
      }
 
      this._value.changeNumber = ber.readInt()
    }
  }
 
  _toBer (ber) {
    const writer = new BerWriter()
    writer.startSequence()
    writer.writeInt(this._value.changeType)
    if (this._value.previousDN) { writer.writeString(this._value.previousDN) }
 
    if (Object.prototype.hasOwnProperty.call(this._value, 'changeNumber')) {
      writer.writeInt(parseInt(this._value.changeNumber, 10))
    }
    writer.endSequence()
 
    ber.writeBuffer(writer.buffer, 0x04)
    return ber
  }
 
  _updatePlainObject (obj) {
    obj.controlValue = this.value
    return obj
  }
}
module.exports = EntryChangeNotificationControl