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

Commit c10a1a6

Browse files
committed
Clean up client handling of end/error for searches
1 parent acc1ca8 commit c10a1a6

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

lib/client/client.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ Client.prototype._sendSocket = function _sendSocket(message,
13751375
var timer = false;
13761376
var sentEmitter = false;
13771377

1378-
function _done(event, obj) {
1378+
function sendResult(event, obj) {
13791379
if (event === 'error' && self.listeners('resultError')) {
13801380
self.emit('resultError', obj);
13811381
}
@@ -1385,19 +1385,15 @@ Client.prototype._sendSocket = function _sendSocket(message,
13851385
// Execute callback with the error instead.
13861386
if (!sentEmitter)
13871387
return callback(obj);
1388-
emitter.removeAllListeners('end');
13891388
}
1390-
if (event === 'end')
1391-
emitter.removeAllListeners('error');
1392-
13931389
return emitter.emit(event, obj);
13941390
}
13951391

13961392
if (event === 'error')
13971393
return callback(obj);
13981394

13991395
return callback(null, obj);
1400-
} // end function _done(event, obj)
1396+
}
14011397

14021398
function messageCallback(msg) {
14031399
if (timer)
@@ -1407,29 +1403,29 @@ Client.prototype._sendSocket = function _sendSocket(message,
14071403
log.trace({msg: msg ? msg.json : null}, 'response received');
14081404

14091405
if (expect === 'abandon')
1410-
return _done('end', null);
1406+
return sendResult('end', null);
14111407

14121408
if (msg instanceof SearchEntry || msg instanceof SearchReference) {
14131409
var event = msg.constructor.name;
14141410
event = event[0].toLowerCase() + event.slice(1);
1415-
return _done(event, msg);
1411+
return sendResult(event, msg);
14161412
} else {
14171413
tracker.remove(message.messageID);
14181414
// Potentially mark client as idle
14191415
self._updateIdle();
14201416

14211417
if (msg instanceof LDAPResult) {
1422-
if (expect.indexOf(msg.status) === -1)
1423-
return _done('error', errors.getError(msg));
1424-
1425-
return _done('end', msg);
1418+
if (expect.indexOf(msg.status) === -1) {
1419+
return sendResult('error', errors.getError(msg));
1420+
}
1421+
return sendResult('end', msg);
14261422
} else if (msg instanceof Error) {
1427-
return _done('error', msg);
1423+
return sendResult('error', msg);
14281424
} else {
1429-
return _done('error', new errors.ProtocolError(msg.type));
1425+
return sendResult('error', new errors.ProtocolError(msg.type));
14301426
}
14311427
}
1432-
} // end function messageCallback(msg)
1428+
}
14331429

14341430
function onRequestTimeout() {
14351431
self.emit('timeout', message);
@@ -1438,7 +1434,7 @@ Client.prototype._sendSocket = function _sendSocket(message,
14381434
//FIXME: the timed-out request should be abandoned
14391435
cb(new errors.TimeoutError('request timeout (client interrupt)'));
14401436
}
1441-
} // end function onRequestTimeout()
1437+
}
14421438

14431439
function writeCallback() {
14441440
if (expect === 'abandon') {
@@ -1461,7 +1457,7 @@ Client.prototype._sendSocket = function _sendSocket(message,
14611457
return callback(null, emitter);
14621458
}
14631459
return false;
1464-
} // end writeCallback()
1460+
}
14651461

14661462
// Start actually doing something...
14671463
tracker.track(message, messageCallback);

test/client.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,37 @@ test('setup', function (t) {
215215
}
216216
});
217217

218+
server.search('cn=pagederr', function (req, res, next) {
219+
var cookie = null;
220+
req.controls.forEach(function (control) {
221+
if (control.type === ldap.PagedResultsControl.OID) {
222+
cookie = control.value.cookie;
223+
}
224+
});
225+
if (cookie && Buffer.isBuffer(cookie) && cookie.length === 0) {
226+
// send first "page"
227+
res.send({
228+
dn: util.format('o=result, cn=pagederr'),
229+
attributes: {
230+
o: 'result',
231+
objectclass: ['pagedResult']
232+
}
233+
});
234+
res.controls.push(new ldap.PagedResultsControl({
235+
value: {
236+
size: 2,
237+
cookie: new Buffer('a')
238+
}
239+
}));
240+
res.end();
241+
return next();
242+
} else {
243+
// send error instead of second page
244+
res.end(ldap.LDAP_SIZE_LIMIT_EXCEEDED);
245+
return next();
246+
}
247+
});
248+
218249
server.search('dc=empty', function (req, res, next) {
219250
res.send({
220251
dn: 'dc=empty',
@@ -694,6 +725,30 @@ test('search paged', function (t) {
694725
}
695726
});
696727

728+
t.test('paged - handle later error', function (t2) {
729+
var countEntries = 0;
730+
var countPages = 0;
731+
client.search('cn=pagederr', {
732+
paged: { pageSize: 1 }
733+
}, function (err, res) {
734+
t2.ifError(err);
735+
res.on('searchEntry', function () {
736+
t2.ok(++countEntries);
737+
});
738+
res.on('page', function () {
739+
t2.ok(++countPages);
740+
});
741+
res.on('error', function (error) {
742+
t2.equal(countEntries, 1);
743+
t2.equal(countPages, 1);
744+
t2.end();
745+
});
746+
res.on('end', function () {
747+
t2.fail('should not be reached');
748+
});
749+
});
750+
});
751+
697752
t.end();
698753
});
699754

0 commit comments

Comments
 (0)