From e07656b233548426e8eded2c86f8cc2144401e02 Mon Sep 17 00:00:00 2001 From: Philippe Seewer Date: Fri, 7 May 2021 14:04:37 +0200 Subject: [PATCH 1/2] SearchPager: Implement queueing events until a listener appears MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes the same problems for paged searches as does https://github.com/ldapjs/node-ldapjs/pull/610 for unpaged searches. By passing an EventEmitter via callback there exist cases when events are emitted before listeners are registered, resulting in missed events. The Change turns SearchPager into a CorkedEmitter which is already used as a solution for non paged searches. Doing so requires the internal 'search' event to be dropped. This change adapts a test case originally by László Szűcs (@ifroz). Signed-off-by: Philippe Seewer --- lib/client/client.js | 4 ++-- lib/client/search_pager.js | 12 +++++++----- test/client.test.js | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/client/client.js b/lib/client/client.js index bdff844..f54cf6a 100644 --- a/lib/client/client.js +++ b/lib/client/client.js @@ -630,9 +630,9 @@ Client.prototype.search = function search (base, callback: callback, controls: controls, pageSize: size, - pagePause: pageOpts.pagePause + pagePause: pageOpts.pagePause, + sendRequest: sendRequest }) - pager.on('search', sendRequest) pager.begin() } else { sendRequest(controls, new CorkedEmitter(), callback) diff --git a/lib/client/search_pager.js b/lib/client/search_pager.js index d44f318..83a77e9 100644 --- a/lib/client/search_pager.js +++ b/lib/client/search_pager.js @@ -10,6 +10,8 @@ const assert = require('assert-plus') // var Protocol = require('../protocol') const PagedControl = require('../controls/paged_results_control.js') +const CorkedEmitter = require('../corked_emitter.js') + /// --- API /** @@ -29,19 +31,20 @@ const PagedControl = require('../controls/paged_results_control.js') * will be emitted (and 'end' will not be). By listening to * 'pageError', a successful search that lacks paging will be * able to emit 'end'. - * 3. search - Emitted as an internal event to trigger another client search. */ function SearchPager (opts) { assert.object(opts) assert.func(opts.callback) assert.number(opts.pageSize) + assert.func(opts.sendRequest) - EventEmitter.call(this, {}) + CorkedEmitter.call(this, {}) this.callback = opts.callback this.controls = opts.controls this.pageSize = opts.pageSize this.pagePause = opts.pagePause + this.sendRequest = opts.sendRequest this.controls.forEach(function (control) { if (control.type === PagedControl.OID) { @@ -60,7 +63,7 @@ function SearchPager (opts) { emitter.on('error', this._onError.bind(this)) this.childEmitter = emitter } -util.inherits(SearchPager, EventEmitter) +util.inherits(SearchPager, CorkedEmitter) module.exports = SearchPager /** @@ -143,8 +146,7 @@ SearchPager.prototype._nextPage = function _nextPage (cookie) { } })) - this.emit('search', controls, this.childEmitter, - this._sendCallback.bind(this)) + this.sendRequest(controls, this.childEmitter, this._sendCallback.bind(this)) } /** diff --git a/test/client.test.js b/test/client.test.js index 8fe6194..f95e4f7 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -899,6 +899,27 @@ tap.test('search paged', { timeout: 10000 }, function (t) { }) }) + tap.test('paged - search with delayed event listener binding', function (t) { + t.context.client.search('cn=paged', { filter: '(objectclass=*)', paged : true }, function (err, res) { + t.error(err) + setTimeout(() => { + let gotEntry = 0 + console.log('registering searchEntry'); + + res.on('searchEntry', function () { + gotEntry++ + }) + res.on('error', function (err) { + t.fail(err) + }) + res.on('end', function () { + t.equal(gotEntry, 1000) + t.end() + }) + }, 100) + }) + }) + t.end() }) From d064b359d32759cd15dec715ffb1778f3145f3dd Mon Sep 17 00:00:00 2001 From: Philippe Seewer Date: Fri, 7 May 2021 15:50:02 +0200 Subject: [PATCH 2/2] Run lint. Signed-off-by: Philippe Seewer --- test/client.test.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/client.test.js b/test/client.test.js index f95e4f7..1b69454 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -900,16 +900,14 @@ tap.test('search paged', { timeout: 10000 }, function (t) { }) tap.test('paged - search with delayed event listener binding', function (t) { - t.context.client.search('cn=paged', { filter: '(objectclass=*)', paged : true }, function (err, res) { + t.context.client.search('cn=paged', { filter: '(objectclass=*)', paged: true }, function (err, res) { t.error(err) setTimeout(() => { - let gotEntry = 0 - console.log('registering searchEntry'); - + let gotEntry = 0 res.on('searchEntry', function () { gotEntry++ }) - res.on('error', function (err) { + res.on('error', function (err) { t.fail(err) }) res.on('end', function () {