persistent_search.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /// --- Globals
  2. // var parseDN = require('./dn').parse
  3. const EntryChangeNotificationControl =
  4. require('./controls').EntryChangeNotificationControl
  5. /// --- API
  6. // Cache used to store connected persistent search clients
  7. function PersistentSearch () {
  8. this.clientList = []
  9. }
  10. PersistentSearch.prototype.addClient = function (req, res, callback) {
  11. if (typeof (req) !== 'object') { throw new TypeError('req must be an object') }
  12. if (typeof (res) !== 'object') { throw new TypeError('res must be an object') }
  13. if (callback && typeof (callback) !== 'function') { throw new TypeError('callback must be a function') }
  14. const log = req.log
  15. const client = {}
  16. client.req = req
  17. client.res = res
  18. log.debug('%s storing client', req.logId)
  19. this.clientList.push(client)
  20. log.debug('%s stored client', req.logId)
  21. log.debug('%s total number of clients %s',
  22. req.logId, this.clientList.length)
  23. if (callback) { callback(client) }
  24. }
  25. PersistentSearch.prototype.removeClient = function (req, res, callback) {
  26. if (typeof (req) !== 'object') { throw new TypeError('req must be an object') }
  27. if (typeof (res) !== 'object') { throw new TypeError('res must be an object') }
  28. if (callback && typeof (callback) !== 'function') { throw new TypeError('callback must be a function') }
  29. const log = req.log
  30. log.debug('%s removing client', req.logId)
  31. const client = {}
  32. client.req = req
  33. client.res = res
  34. // remove the client if it exists
  35. this.clientList.forEach(function (element, index, array) {
  36. if (element.req === client.req) {
  37. log.debug('%s removing client from list', req.logId)
  38. array.splice(index, 1)
  39. }
  40. })
  41. log.debug('%s number of persistent search clients %s',
  42. req.logId, this.clientList.length)
  43. if (callback) { callback(client) }
  44. }
  45. function getOperationType (requestType) {
  46. switch (requestType) {
  47. case 'AddRequest':
  48. case 'add':
  49. return 1
  50. case 'DeleteRequest':
  51. case 'delete':
  52. return 2
  53. case 'ModifyRequest':
  54. case 'modify':
  55. return 4
  56. case 'ModifyDNRequest':
  57. case 'modrdn':
  58. return 8
  59. default:
  60. throw new TypeError('requestType %s, is an invalid request type',
  61. requestType)
  62. }
  63. }
  64. function getEntryChangeNotificationControl (req, obj) {
  65. // if we want to return a ECNC
  66. if (req.persistentSearch.value.returnECs) {
  67. const attrs = obj.attributes
  68. const value = {}
  69. value.changeType = getOperationType(attrs.changetype)
  70. // if it's a modDN request, fill in the previous DN
  71. if (value.changeType === 8 && attrs.previousDN) {
  72. value.previousDN = attrs.previousDN
  73. }
  74. value.changeNumber = attrs.changenumber
  75. return new EntryChangeNotificationControl({ value })
  76. } else {
  77. return false
  78. }
  79. }
  80. function checkChangeType (req, requestType) {
  81. return (req.persistentSearch.value.changeTypes &
  82. getOperationType(requestType))
  83. }
  84. /// --- Exports
  85. module.exports = {
  86. PersistentSearchCache: PersistentSearch,
  87. checkChangeType,
  88. getEntryChangeNotificationControl
  89. }