Skip to content

Commit 37ca0bb

Browse files
- Switched from passing on a stream to passing on a string.
- Used database instead of filesystem in the cache example. - Moved the fallback handler to a method one scope out.
1 parent ef4532a commit 37ca0bb

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

index.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,7 @@ Deps.prototype.resolve = function (id, parent, cb) {
201201
Deps.prototype.readFile = function (file, id, pkg) {
202202
var self = this;
203203
if (xhas(this.fileCache, file)) {
204-
var tr = through();
205-
tr.push(this.fileCache[file]);
206-
tr.push(null);
207-
return tr;
204+
return toStream(this.fileCache[file]);
208205
}
209206
var rs = fs.createReadStream(file, {
210207
encoding: 'utf8'
@@ -381,8 +378,22 @@ Deps.prototype.walk = function (id, parent, cb) {
381378
var c = self.cache && self.cache[file];
382379
if (c) return fromDeps(file, c.source, c.package, fakePath, Object.keys(c.deps));
383380

384-
self.persistentCache(file, id, pkg, function fallback (stream, cb) {
385-
(stream || self.readFile(file, id, pkg))
381+
self.persistentCache(file, id, pkg, persistentCacheFallback, function (err, c) {
382+
if (err) {
383+
self.emit('error', err);
384+
return;
385+
}
386+
fromDeps(file, c.source, c.package, fakePath, Object.keys(c.deps));
387+
});
388+
389+
function persistentCacheFallback (dataAsString, cb) {
390+
var stream
391+
if (dataAsString) {
392+
stream = toStream(dataAsString)
393+
} else {
394+
stream = self.readFile(file, id, pkg)
395+
}
396+
stream
386397
.pipe(self.getTransforms(fakePath || file, pkg, {
387398
builtin: builtin,
388399
inNodeModules: parent.inNodeModules
@@ -401,13 +412,7 @@ Deps.prototype.walk = function (id, parent, cb) {
401412
});
402413
}
403414
}));
404-
}, function (err, c) {
405-
if (err) {
406-
self.emit('error', err);
407-
return;
408-
}
409-
fromDeps(file, c.source, c.package, fakePath, Object.keys(c.deps));
410-
});
415+
}
411416
});
412417

413418
function getDeps (file, src) {
@@ -577,6 +582,13 @@ function xhas (obj) {
577582
return true;
578583
}
579584

585+
function toStream (dataAsString) {
586+
var tr = through();
587+
tr.push(dataAsString);
588+
tr.push(null);
589+
return tr;
590+
}
591+
580592
function has (obj, key) {
581593
return obj && Object.prototype.hasOwnProperty.call(obj, key);
582594
}

readme.markdown

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,35 @@ from disk.
106106
if (hasError()) {
107107
return cb(error) // Pass any error to the callback
108108
}
109-
if (isInCache()) {
109+
110+
var fileData = fs.readFileSync(file)
111+
var key = keyFromFile(file, fileData)
112+
113+
if (db.has(key)) {
110114
return cb(null, {
111-
source: fs.readFileSync(file, 'utf8'), // String of the fully processed file
115+
source: db.get(key).toString(),
112116
package: pkg, // The package for housekeeping
113117
deps: {
114118
'id': // id that is used to reference a required file
115119
'file' // file path to the required file
116120
}
117121
})
118122
}
119-
// optional (can be null) stream that provides the data from
120-
// the hard disk, can be provided in case the file data is used
121-
// to evaluate the cache identifier
122-
stream = fs.createReadStream(file)
123-
124-
// fallback to the default reading
125-
fallback(stream, cb)
123+
//
124+
// The fallback will process the file in case the file is not
125+
// in cache.
126+
//
127+
// Note that if your implementation doesn't need the file data
128+
// then you can pass `null` instead of the source and the fallback will
129+
// fetch the data by itself.
130+
//
131+
fallback(fileData, function (error, cacheableEntry) {
132+
if (error) {
133+
return cb(error)
134+
}
135+
db.addToCache(key, cacheableEntry)
136+
cb(null, cacheableEntry)
137+
})
126138
}
127139
```
128140

test/cache_persistent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ test('allow passing of a different stream', function (t) {
5959
t.plan(1);
6060
var p = parser({
6161
persistentCache: function (file, id, pkg, fallback, cb) {
62-
fallback(fs.createReadStream(files.bar), cb)
62+
fallback(fs.readFileSync(files.bar, 'utf8'), cb)
6363
}
6464
});
6565
p.end({ id: 'foo', file: files.foo, entry: false });

0 commit comments

Comments
 (0)