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
108
109
/// --- Globals
 
// var parseDN = require('./dn').parse
 
const EntryChangeNotificationControl =
  require('./controls').EntryChangeNotificationControl
 
/// --- API
 
// Cache used to store connected persistent search clients
function PersistentSearch () {
  this.clientList = []
}
 
PersistentSearch.prototype.addClient = function (req, res, callback) {
  if (typeof (req) !== 'object') { throw new TypeError('req must be an object') }
  if (typeof (res) !== 'object') { throw new TypeError('res must be an object') }
  if (callback && typeof (callback) !== 'function') { throw new TypeError('callback must be a function') }
 
  const log = req.log
 
  const client = {}
  client.req = req
  client.res = res
 
  log.debug('%s storing client', req.logId)
 
  this.clientList.push(client)
 
  log.debug('%s stored client', req.logId)
  log.debug('%s total number of clients %s',
    req.logId, this.clientList.length)
  if (callback) { callback(client) }
}
 
PersistentSearch.prototype.removeClient = function (req, res, callback) {
  if (typeof (req) !== 'object') { throw new TypeError('req must be an object') }
  if (typeof (res) !== 'object') { throw new TypeError('res must be an object') }
  if (callback && typeof (callback) !== 'function') { throw new TypeError('callback must be a function') }
 
  const log = req.log
  log.debug('%s removing client', req.logId)
  const client = {}
  client.req = req
  client.res = res
 
  // remove the client if it exists
  this.clientList.forEach(function (element, index, array) {
    if (element.req === client.req) {
      log.debug('%s removing client from list', req.logId)
      array.splice(index, 1)
    }
  })
 
  log.debug('%s number of persistent search clients %s',
    req.logId, this.clientList.length)
  if (callback) { callback(client) }
}
 
function getOperationType (requestType) {
  switch (requestType) {
    case 'AddRequest':
    case 'add':
      return 1
    case 'DeleteRequest':
    case 'delete':
      return 2
    case 'ModifyRequest':
    case 'modify':
      return 4
    case 'ModifyDNRequest':
    case 'modrdn':
      return 8
    default:
      throw new TypeError('requestType %s, is an invalid request type',
        requestType)
  }
}
 
function getEntryChangeNotificationControl (req, obj) {
  // if we want to return a ECNC
  if (req.persistentSearch.value.returnECs) {
    const attrs = obj.attributes
    const value = {}
    value.changeType = getOperationType(attrs.changetype)
    // if it's a modDN request, fill in the previous DN
    if (value.changeType === 8 && attrs.previousDN) {
      value.previousDN = attrs.previousDN
    }
 
    value.changeNumber = attrs.changenumber
    return new EntryChangeNotificationControl({ value })
  } else {
    return false
  }
}
 
function checkChangeType (req, requestType) {
  return (req.persistentSearch.value.changeTypes &
          getOperationType(requestType))
}
 
/// --- Exports
 
module.exports = {
  PersistentSearchCache: PersistentSearch,
  checkChangeType,
  getEntryChangeNotificationControl
}