Skip to content

Commit 76b1e11

Browse files
authored
Merge pull request #205 from Chocobozzz/patch-1
Fix broken stream on normal flow error
2 parents 4ac77b2 + e65f1d2 commit 76b1e11

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

index.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,7 @@ export class ReaddirpStream extends Readable {
185185
const slice = fil.splice(0, batch).map((dirent) => this._formatEntry(dirent, path));
186186
const awaited = await Promise.all(slice);
187187
for (const entry of awaited) {
188-
if (!entry) {
189-
batch--;
190-
return;
191-
}
188+
if (!entry) continue;
192189
if (this.destroyed) return;
193190

194191
const entryType = await this._getEntryType(entry);

test/index.test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ describe('readdirp', () => {
330330
entry.should.containSubset(formatEntry(created[index], currPath))
331331
);
332332
});
333-
it('should emit warning for missing file', async () => {
333+
it('should emit warning for missing directory', async () => {
334334
await beforeEach();
335335
// readdirp() is initialized on some big root directory
336336
// readdirp() receives path a/b/c to its queue
@@ -354,6 +354,33 @@ describe('readdirp', () => {
354354
await Promise.race([waitForEnd(stream), delay(2000)]);
355355
isWarningCalled.should.equals(true);
356356
}); //.timeout(4000);
357+
it('should emit warning for missing file', async () => {
358+
await beforeEach();
359+
// readdirp() is initialized on some big root directory
360+
// readdirp() receives files f1, f2, f3 to its queue
361+
// readdirp is reading something else
362+
// f1 gets deleted, so stat()-ting f1 would now emit enoent
363+
// We should emit warnings for this case and properly process f2 and f3
364+
// this.timeout(4000);
365+
await touch(['f1', 'f2', 'f3']);
366+
let isWarningCalled = false;
367+
const stream = readdirp(currPath, { type: 'all', highWaterMark: 1, alwaysStat: true });
368+
stream.on('warn', (warning) => {
369+
warning.should.be.an.instanceof(Error);
370+
warning.code.should.equals('ENOENT');
371+
isWarningCalled = true;
372+
});
373+
await delay(1000);
374+
await rm(sysPath.join(currPath, 'f1'), { recursive: true });
375+
const detected = [];
376+
stream.on('data', (file) => {
377+
detected.push(file.path);
378+
});
379+
await Promise.race([waitForEnd(stream), delay(2000)]);
380+
381+
chai.expect(detected).to.deep.equal(['f2', 'f3']);
382+
isWarningCalled.should.equals(true);
383+
});
357384
it('should emit warning for file with strict permission', async () => {
358385
// Windows doesn't throw permission error if you access permitted directory
359386
if (isWindows) {

0 commit comments

Comments
 (0)