Skip to content

Commit 607726d

Browse files
authored
Merge 3511995 into abf2b4b
2 parents abf2b4b + 3511995 commit 607726d

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

packages/client/lib/client/index.spec.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ describe('Client', () => {
215215

216216
testUtils.testWithClient('client.hGetAll should return object', async client => {
217217
await client.v4.hSet('key', 'field', 'value');
218-
218+
219219
assert.deepEqual(
220220
await promisify(client.hGetAll).call(client, 'key'),
221221
Object.create(null, {
@@ -317,7 +317,7 @@ describe('Client', () => {
317317
}
318318
});
319319

320-
testUtils.testWithClient('client.multi.hGetAll should return object', async client => {
320+
testUtils.testWithClient('client.multi.hGetAll should return object', async client => {
321321
assert.deepEqual(
322322
await multiExecAsync(
323323
client.multi()
@@ -607,24 +607,48 @@ describe('Client', () => {
607607
return client.executeIsolated(isolated => killClient(isolated, client));
608608
}, GLOBAL.SERVERS.OPEN);
609609

610-
testUtils.testWithClient('scanIterator', async client => {
611-
const promises = [],
612-
keys = new Set();
613-
for (let i = 0; i < 100; i++) {
614-
const key = i.toString();
615-
keys.add(key);
616-
promises.push(client.set(key, ''));
617-
}
610+
describe('scanIterator', () => {
611+
testUtils.testWithClient('strings', async client => {
612+
const args: Array<string> = [],
613+
keys = new Set<string>();
614+
for (let i = 0; i < 100; i++) {
615+
const key = i.toString();
616+
args.push(key, '');
617+
keys.add(key);
618+
}
618619

619-
await Promise.all(promises);
620+
await client.mSet(args);
620621

621-
const results = new Set();
622-
for await (const key of client.scanIterator()) {
623-
results.add(key);
624-
}
622+
const results = new Set<string>();
623+
for await (const key of client.scanIterator()) {
624+
results.add(key);
625+
}
625626

626-
assert.deepEqual(keys, results);
627-
}, GLOBAL.SERVERS.OPEN);
627+
assert.deepEqual(keys, results);
628+
}, GLOBAL.SERVERS.OPEN);
629+
630+
testUtils.testWithClient('buffers', async client => {
631+
const args: Array<string | Buffer> = [],
632+
keys = new Set<Buffer>();
633+
for (let i = 0; i < 100; i++) {
634+
const key = Buffer.from([i]);
635+
args.push(key, '');
636+
keys.add(key);
637+
}
638+
639+
await client.mSet(args);
640+
641+
const results = new Set<Buffer>(),
642+
iteartor = client.scanIterator(
643+
client.commandOptions({ returnBuffers: true })
644+
);
645+
for await (const key of iteartor) {
646+
results.add(key);
647+
}
648+
649+
assert.deepEqual(keys, results);
650+
}, GLOBAL.SERVERS.OPEN);
651+
});
628652

629653
testUtils.testWithClient('hScanIterator', async client => {
630654
const hash: Record<string, string> = {};

packages/client/lib/client/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ export default class RedisClient<
415415
);
416416
} else if (!this.#socket.isReady && this.#options?.disableOfflineQueue) {
417417
return Promise.reject(new ClientOfflineError());
418-
}
418+
}
419419

420420
const promise = this.#queue.addCommand<T>(args, options);
421421
this.#tick();
@@ -656,10 +656,29 @@ export default class RedisClient<
656656
return results;
657657
}
658658

659-
async* scanIterator(options?: ScanCommandOptions): AsyncIterable<string> {
659+
scanIterator<T extends CommandOptions<ClientCommandOptions>>(
660+
commandOptions: T,
661+
options?: ScanCommandOptions
662+
): AsyncIterable<T['returnBuffers'] extends true ? Buffer : string>;
663+
scanIterator(
664+
options?: ScanCommandOptions
665+
): AsyncIterable<string>;
666+
async* scanIterator<T extends CommandOptions<ClientCommandOptions>>(
667+
commandOptions?: T | ScanCommandOptions,
668+
options?: ScanCommandOptions
669+
): AsyncIterable<T['returnBuffers'] extends true ? Buffer : string> {
670+
if (!isCommandOptions(commandOptions)) {
671+
options = commandOptions;
672+
commandOptions = undefined;
673+
}
674+
675+
const scan = commandOptions ?
676+
(...args: Array<unknown>) => (this as any).scan(commandOptions, ...args) :
677+
(...args: Array<unknown>) => (this as any).scan(...args);
678+
660679
let cursor = 0;
661680
do {
662-
const reply = await (this as any).scan(cursor, options);
681+
const reply = await scan(cursor, options);
663682
cursor = reply.cursor;
664683
for (const key of reply.keys) {
665684
yield key;

0 commit comments

Comments
 (0)