[Pkg-javascript-commits] [node-module-deps] 429/444: - 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.
Bastien Roucariès
rouca at moszumanska.debian.org
Fri Dec 15 09:48:20 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-module-deps.
commit 37ca0bb572f9217892225c3728cb039cb4089d5c
Author: Martin Heidegger <martin.heidegger at gmail.com>
Date: Fri Jan 27 08:59:45 2017 +0900
- 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.
---
index.js | 38 +++++++++++++++++++++++++-------------
readme.markdown | 30 +++++++++++++++++++++---------
test/cache_persistent.js | 2 +-
3 files changed, 47 insertions(+), 23 deletions(-)
diff --git a/index.js b/index.js
index 90c8994..99b7ab7 100644
--- a/index.js
+++ b/index.js
@@ -201,10 +201,7 @@ Deps.prototype.resolve = function (id, parent, cb) {
Deps.prototype.readFile = function (file, id, pkg) {
var self = this;
if (xhas(this.fileCache, file)) {
- var tr = through();
- tr.push(this.fileCache[file]);
- tr.push(null);
- return tr;
+ return toStream(this.fileCache[file]);
}
var rs = fs.createReadStream(file, {
encoding: 'utf8'
@@ -381,8 +378,22 @@ Deps.prototype.walk = function (id, parent, cb) {
var c = self.cache && self.cache[file];
if (c) return fromDeps(file, c.source, c.package, fakePath, Object.keys(c.deps));
- self.persistentCache(file, id, pkg, function fallback (stream, cb) {
- (stream || self.readFile(file, id, pkg))
+ self.persistentCache(file, id, pkg, persistentCacheFallback, function (err, c) {
+ if (err) {
+ self.emit('error', err);
+ return;
+ }
+ fromDeps(file, c.source, c.package, fakePath, Object.keys(c.deps));
+ });
+
+ function persistentCacheFallback (dataAsString, cb) {
+ var stream
+ if (dataAsString) {
+ stream = toStream(dataAsString)
+ } else {
+ stream = self.readFile(file, id, pkg)
+ }
+ stream
.pipe(self.getTransforms(fakePath || file, pkg, {
builtin: builtin,
inNodeModules: parent.inNodeModules
@@ -401,13 +412,7 @@ Deps.prototype.walk = function (id, parent, cb) {
});
}
}));
- }, function (err, c) {
- if (err) {
- self.emit('error', err);
- return;
- }
- fromDeps(file, c.source, c.package, fakePath, Object.keys(c.deps));
- });
+ }
});
function getDeps (file, src) {
@@ -577,6 +582,13 @@ function xhas (obj) {
return true;
}
+function toStream (dataAsString) {
+ var tr = through();
+ tr.push(dataAsString);
+ tr.push(null);
+ return tr;
+}
+
function has (obj, key) {
return obj && Object.prototype.hasOwnProperty.call(obj, key);
}
diff --git a/readme.markdown b/readme.markdown
index 1d18d0f..c6b3e6f 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -106,9 +106,13 @@ from disk.
if (hasError()) {
return cb(error) // Pass any error to the callback
}
- if (isInCache()) {
+
+ var fileData = fs.readFileSync(file)
+ var key = keyFromFile(file, fileData)
+
+ if (db.has(key)) {
return cb(null, {
- source: fs.readFileSync(file, 'utf8'), // String of the fully processed file
+ source: db.get(key).toString(),
package: pkg, // The package for housekeeping
deps: {
'id': // id that is used to reference a required file
@@ -116,13 +120,21 @@ from disk.
}
})
}
- // optional (can be null) stream that provides the data from
- // the hard disk, can be provided in case the file data is used
- // to evaluate the cache identifier
- stream = fs.createReadStream(file)
-
- // fallback to the default reading
- fallback(stream, cb)
+ //
+ // The fallback will process the file in case the file is not
+ // in cache.
+ //
+ // Note that if your implementation doesn't need the file data
+ // then you can pass `null` instead of the source and the fallback will
+ // fetch the data by itself.
+ //
+ fallback(fileData, function (error, cacheableEntry) {
+ if (error) {
+ return cb(error)
+ }
+ db.addToCache(key, cacheableEntry)
+ cb(null, cacheableEntry)
+ })
}
```
diff --git a/test/cache_persistent.js b/test/cache_persistent.js
index b90f447..30a79f1 100644
--- a/test/cache_persistent.js
+++ b/test/cache_persistent.js
@@ -59,7 +59,7 @@ test('allow passing of a different stream', function (t) {
t.plan(1);
var p = parser({
persistentCache: function (file, id, pkg, fallback, cb) {
- fallback(fs.createReadStream(files.bar), cb)
+ fallback(fs.readFileSync(files.bar, 'utf8'), cb)
}
});
p.end({ id: 'foo', file: files.foo, entry: false });
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-module-deps.git
More information about the Pkg-javascript-commits
mailing list