Skip to content
This repository was archived by the owner on Oct 27, 2020. It is now read-only.

Commit a7f3449

Browse files
evilebottnawimichael-ciniawsky
authored andcommitted
perf: throw early if file doesn't exists (#5)
1 parent 89bab1f commit a7f3449

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

src/index.js

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -66,50 +66,44 @@ function pitch(remainingRequest, prevRequest, dataInput) {
6666
const hash = digest(remainingRequest);
6767
const cacheFile = path.join(cacheDirectory, `${hash}.json`);
6868
data.cacheFile = cacheFile;
69-
fs.exists(cacheFile, (exist) => {
70-
if (!exist) {
69+
fs.readFile(cacheFile, 'utf-8', (readFileErr, content) => {
70+
if (readFileErr) {
7171
callback();
7272
return;
7373
}
74-
fs.readFile(cacheFile, 'utf-8', (readFileErr, content) => {
75-
if (readFileErr) {
76-
callback();
77-
return;
78-
}
79-
data.fileExists = true;
80-
let cacheData;
81-
try {
82-
cacheData = JSON.parse(content);
83-
} catch (e) {
84-
callback();
85-
return;
86-
}
87-
if (cacheData.remainingRequest !== remainingRequest) {
88-
// in case of a hash conflict
89-
callback();
90-
return;
91-
}
92-
async.each(cacheData.dependencies.concat(cacheData.contextDependencies), (dep, eachCallback) => {
93-
fs.stat(dep.path, (statErr, stats) => {
94-
if (statErr) {
95-
eachCallback(statErr);
96-
return;
97-
}
98-
if (stats.mtime.getTime() !== dep.mtime) {
99-
eachCallback(true);
100-
return;
101-
}
102-
eachCallback();
103-
});
104-
}, (err) => {
105-
if (err) {
106-
callback();
74+
data.fileExists = true;
75+
let cacheData;
76+
try {
77+
cacheData = JSON.parse(content);
78+
} catch (e) {
79+
callback();
80+
return;
81+
}
82+
if (cacheData.remainingRequest !== remainingRequest) {
83+
// in case of a hash conflict
84+
callback();
85+
return;
86+
}
87+
async.each(cacheData.dependencies.concat(cacheData.contextDependencies), (dep, eachCallback) => {
88+
fs.stat(dep.path, (statErr, stats) => {
89+
if (statErr) {
90+
eachCallback(statErr);
91+
return;
92+
}
93+
if (stats.mtime.getTime() !== dep.mtime) {
94+
eachCallback(true);
10795
return;
10896
}
109-
cacheData.dependencies.forEach(dep => this.addDependency(dep.path));
110-
cacheData.contextDependencies.forEach(dep => this.addContextDependency(dep.path));
111-
callback(null, ...cacheData.result);
97+
eachCallback();
11298
});
99+
}, (err) => {
100+
if (err) {
101+
callback();
102+
return;
103+
}
104+
cacheData.dependencies.forEach(dep => this.addDependency(dep.path));
105+
cacheData.contextDependencies.forEach(dep => this.addContextDependency(dep.path));
106+
callback(null, ...cacheData.result);
113107
});
114108
});
115109
}

0 commit comments

Comments
 (0)