Skip to content

Commit 082bc29

Browse files
authored
Merge pull request #201 from webpack/bugfix/purge-readdir
fix purging of filesystem for readdir
2 parents f026f32 + 9748fca commit 082bc29

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/CachedInputFileSystem.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
*/
55
"use strict";
66

7+
const dirname = path => {
8+
let idx = path.length - 1;
9+
while (idx >= 0) {
10+
const c = path.charCodeAt(idx);
11+
// slash or backslash
12+
if (c === 47 || c === 92) break;
13+
idx--;
14+
}
15+
if (idx < 0) return "";
16+
return path.slice(0, idx);
17+
};
18+
719
class Storage {
820
constructor(duration) {
921
this.duration = duration;
@@ -175,6 +187,27 @@ class Storage {
175187
}
176188
}
177189
}
190+
191+
purgeParent(what) {
192+
if (!what) {
193+
this.count = 0;
194+
clearInterval(this.interval);
195+
this.nextTick = null;
196+
this.data.clear();
197+
this.levels.forEach(level => {
198+
level.clear();
199+
});
200+
} else if (typeof what === "string") {
201+
what = dirname(what);
202+
for (let key of this.data.keys()) {
203+
if (key.startsWith(what)) this.data.delete(key);
204+
}
205+
} else {
206+
for (let i = what.length - 1; i >= 0; i--) {
207+
this.purgeParent(what[i]);
208+
}
209+
}
210+
}
178211
}
179212

180213
module.exports = class CachedInputFileSystem {
@@ -299,7 +332,7 @@ module.exports = class CachedInputFileSystem {
299332

300333
purge(what) {
301334
this._statStorage.purge(what);
302-
this._readdirStorage.purge(what);
335+
this._readdirStorage.purgeParent(what);
303336
this._readFileStorage.purge(what);
304337
this._readlinkStorage.purge(what);
305338
this._readJsonStorage.purge(what);

test/CachedInputFileSystem.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ describe("CachedInputFileSystem", function() {
66
var fs;
77

88
beforeEach(function() {
9+
let counter = 0;
910
fs = new CachedInputFileSystem(
1011
{
1112
stat: function(path, callback) {
@@ -15,6 +16,9 @@ describe("CachedInputFileSystem", function() {
1516
}),
1617
100
1718
);
19+
},
20+
readdir: function(path, callback) {
21+
callback(null, [`${counter++}`]);
1822
}
1923
},
2024
1000
@@ -114,4 +118,23 @@ describe("CachedInputFileSystem", function() {
114118
done();
115119
});
116120
});
121+
122+
it("should purge readdir correctly", function(done) {
123+
fs.readdir("/test/path", (err, r) => {
124+
r[0].should.be.eql("0");
125+
fs.purge(["/test/path/sub/path"]);
126+
fs.readdir("/test/path", (err, r) => {
127+
r[0].should.be.eql("0");
128+
fs.purge(["/test/path/sub"]);
129+
fs.readdir("/test/path", (err, r) => {
130+
r[0].should.be.eql("1");
131+
fs.purge(["/test/path"]);
132+
fs.readdir("/test/path", (err, r) => {
133+
r[0].should.be.eql("2");
134+
done();
135+
});
136+
});
137+
});
138+
});
139+
});
117140
});

0 commit comments

Comments
 (0)