fix(ldap): search stream does not await callbacks

Signed-off-by: Antonio Musolino <antoniomusolino007@gmail.com>
This commit is contained in:
Antonio Musolino
2022-07-20 10:17:25 +02:00
parent 1b479cb5cb
commit c54fcea9af
3 changed files with 12 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend-module-ldap': patch
---
Now the `searchStream` method in LDAP client awaits the callbacks
@@ -90,7 +90,7 @@ export class LdapClient {
searchStreaming(
dn: string,
options: SearchOptions,
f: (entry: SearchEntry) => void,
f: (entry: SearchEntry) => Promise<void> | void,
): Promise<void>;
}
@@ -147,9 +147,10 @@ export class LdapClient {
async searchStreaming(
dn: string,
options: SearchOptions,
f: (entry: SearchEntry) => void,
f: (entry: SearchEntry) => Promise<void> | void,
): Promise<void> {
try {
const awaitList: Array<Promise<void> | void> = [];
return await new Promise<void>((resolve, reject) => {
// Note that we clone the (frozen) options, since ldapjs rudely tries to
// overwrite parts of them
@@ -163,7 +164,7 @@ export class LdapClient {
});
res.on('searchEntry', entry => {
f(entry);
awaitList.push(f(entry));
});
res.on('error', e => {
@@ -176,7 +177,9 @@ export class LdapClient {
} else if (r.status !== 0) {
throw new Error(`Got status ${r.status}: ${r.errorMessage}`);
} else {
resolve();
Promise.all(awaitList)
.then(() => resolve())
.catch(reject);
}
});
});