Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit e74ba91

Browse files
author
zhaoyunfeng
committed
feat: now search callback response will emit SearchRequest after every search request is sent
1 parent 010b472 commit e74ba91

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

docs/client.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ containing the following fields:
276276
|timeLimit |the maximum amount of time the server should take in responding, in seconds. Defaults to 10. Lots of servers will ignore this.|
277277
|paged |enable and/or configure automatic result paging|
278278

279-
Responses from the `search` method are an `EventEmitter` where you will get a
280-
notification for each `searchEntry` that comes back from the server. You will
281-
additionally be able to listen for a `searchReference`, `error` and `end` event.
282-
Note that the `error` event will only be for client/TCP errors, not LDAP error
283-
codes like the other APIs. You'll want to check the LDAP status code
284-
(likely for `0`) on the `end` event to assert success. LDAP search results
285-
can give you a lot of status codes, such as time or size exceeded, busy,
286-
inappropriate matching, etc., which is why this method doesn't try to wrap up
287-
the code matching.
279+
Responses inside callback of the `search` method are an `EventEmitter` where you will get a notification for
280+
each `searchEntry` that comes back from the server. You will additionally be able to listen for a `searchRequest`
281+
, `searchReference`, `error` and `end` event.
282+
`searchRequest` is emitted immediately after every `SearchRequest` is sent with a `SearchRequest` param. You can do op
283+
like `client.abandon` with `searchRequest.messageID` to abandon this search request. Note that the `error` event will
284+
only be for client/TCP errors, not LDAP error codes like the other APIs. You'll want to check the LDAP status code
285+
(likely for `0`) on the `end` event to assert success. LDAP search results can give you a lot of status codes, such as
286+
time or size exceeded, busy, inappropriate matching, etc., which is why this method doesn't try to wrap up the code
287+
matching.
288288

289289
Example:
290290

@@ -298,6 +298,9 @@ const opts = {
298298
client.search('o=example', opts, (err, res) => {
299299
assert.ifError(err);
300300

301+
res.on('searchRequest', (searchRequest) => {
302+
console.log('searchRequest: ', searchRequest.messageID);
303+
});
301304
res.on('searchEntry', (entry) => {
302305
console.log('entry: ' + JSON.stringify(entry.object));
303306
});

lib/client/client.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,9 @@ Client.prototype._sendSocket = function _sendSocket (message,
12511251
conn.end()
12521252
} else if (emitter) {
12531253
sentEmitter = true
1254-
return callback(null, emitter)
1254+
callback(null, emitter)
1255+
emitter.emit('searchRequest', message)
1256+
return
12551257
}
12561258
return false
12571259
}

lib/client/search_pager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function SearchPager (opts) {
5555
this.started = false
5656

5757
const emitter = new EventEmitter()
58+
emitter.on('searchRequest', this.emit.bind(this, 'searchRequest'))
5859
emitter.on('searchEntry', this.emit.bind(this, 'searchEntry'))
5960
emitter.on('end', this._onEnd.bind(this))
6061
emitter.on('error', this._onError.bind(this))

test/client.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,14 +777,23 @@ tap.test('search paged', { timeout: 10000 }, function (t) {
777777
t.test('paged - no pauses', function (t2) {
778778
let countEntries = 0
779779
let countPages = 0
780+
let currentSearchRequest = null
780781
t.context.client.search('cn=paged', { paged: { pageSize: 100 } }, function (err, res) {
781782
t2.error(err)
782783
res.on('searchEntry', entryListener)
784+
res.on('searchRequest', (searchRequest) => {
785+
t2.ok(searchRequest instanceof ldap.SearchRequest)
786+
if (currentSearchRequest === null) {
787+
t2.equal(countPages, 0)
788+
}
789+
currentSearchRequest = searchRequest
790+
})
783791
res.on('page', pageListener)
784792
res.on('error', (err) => t2.error(err))
785-
res.on('end', function () {
793+
res.on('end', function (result) {
786794
t2.equal(countEntries, 1000)
787795
t2.equal(countPages, 10)
796+
t2.equal(result.messageID, currentSearchRequest.messageID)
788797
t2.end()
789798
})
790799

@@ -797,8 +806,11 @@ tap.test('search paged', { timeout: 10000 }, function (t) {
797806
countEntries += 1
798807
}
799808

800-
function pageListener () {
809+
function pageListener(result) {
801810
countPages += 1
811+
if (countPages < 10) {
812+
t2.equal(result.messageID, currentSearchRequest.messageID)
813+
}
802814
}
803815
})
804816
})

0 commit comments

Comments
 (0)