Skip to content

Commit 41a126b

Browse files
committed
Merge branch 'fix/file_emit_persistent_cache' of https://github.com/martinheidegger/module-deps into martinheidegger-fix/file_emit_persistent_cache
2 parents 9c3cfbc + f67af91 commit 41a126b

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var fs = require('fs');
22
var path = require('path');
3-
var relativePath = require('cached-path-relative')
3+
var relativePath = require('cached-path-relative');
44

55
var browserResolve = require('browser-resolve');
66
var nodeResolve = require('resolve');
@@ -206,8 +206,6 @@ Deps.prototype.readFile = function (file, id, pkg) {
206206
var rs = fs.createReadStream(file, {
207207
encoding: 'utf8'
208208
});
209-
rs.on('error', function (err) { self.emit('error', err) });
210-
this.emit('file', file, id);
211209
return rs;
212210
};
213211

@@ -235,7 +233,9 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
235233

236234
for (var i = 0; i < transforms.length; i++) (function (i) {
237235
makeTransform(transforms[i], function (err, trs) {
238-
if (err) return self.emit('error', err)
236+
if (err) {
237+
return dup.emit('error', err);
238+
}
239239
streams[i] = trs;
240240
if (-- pending === 0) done();
241241
});
@@ -247,7 +247,7 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
247247
middle.on('error', function (err) {
248248
err.message += ' while parsing file: ' + file;
249249
if (!err.filename) err.filename = file;
250-
self.emit('error', err);
250+
dup.emit('error', err);
251251
});
252252
input.pipe(middle).pipe(output);
253253
}
@@ -283,7 +283,7 @@ Deps.prototype.getTransforms = function (file, pkg, opts) {
283283
if (err) {
284284
params.basedir = pkg.__dirname;
285285
return nodeResolve(id, params, function (e, r) {
286-
nr(e, r, true)
286+
nr(e, r, true);
287287
});
288288
}
289289

@@ -349,6 +349,9 @@ Deps.prototype.walk = function (id, parent, cb) {
349349
file = rec.file;
350350

351351
var ts = self.getTransforms(file, pkg);
352+
ts.on('error', function (err) {
353+
self.emit('error', err);
354+
});
352355
ts.pipe(concat(function (body) {
353356
rec.source = body.toString('utf8');
354357
fromSource(file, rec.source, pkg);
@@ -371,6 +374,9 @@ Deps.prototype.walk = function (id, parent, cb) {
371374

372375
if (rec.source) {
373376
var ts = self.getTransforms(file, pkg);
377+
ts.on('error', function (err) {
378+
self.emit('error', err);
379+
});
374380
ts.pipe(concat(function (body) {
375381
rec.source = body.toString('utf8');
376382
fromSource(file, rec.source, pkg);
@@ -382,6 +388,7 @@ Deps.prototype.walk = function (id, parent, cb) {
382388
if (c) return fromDeps(file, c.source, c.package, fakePath, Object.keys(c.deps));
383389

384390
self.persistentCache(file, id, pkg, persistentCacheFallback, function (err, c) {
391+
self.emit('file', file, id);
385392
if (err) {
386393
self.emit('error', err);
387394
return;
@@ -390,12 +397,13 @@ Deps.prototype.walk = function (id, parent, cb) {
390397
});
391398

392399
function persistentCacheFallback (dataAsString, cb) {
393-
var stream = dataAsString ? toStream(dataAsString) : self.readFile(file, id, pkg);
400+
var stream = dataAsString ? toStream(dataAsString) : self.readFile(file, id, pkg).on('error', cb);
394401
stream
395402
.pipe(self.getTransforms(fakePath || file, pkg, {
396403
builtin: builtin,
397404
inNodeModules: parent.inNodeModules
398405
}))
406+
.on('error', cb)
399407
.pipe(concat(function (body) {
400408
var src = body.toString('utf8');
401409
var deps = getDeps(file, src);
@@ -524,7 +532,7 @@ Deps.prototype.lookupPackage = function (file, cb) {
524532
catch (err) {
525533
return onpkg(new Error([
526534
err + ' while parsing json file ' + pkgfile
527-
].join('')))
535+
].join('')));
528536
}
529537
pkg.__dirname = dir;
530538

@@ -539,8 +547,8 @@ Deps.prototype.lookupPackage = function (file, cb) {
539547
delete self.pkgFileCachePending[pkgfile];
540548
fns.forEach(function (f) { f(err, pkg) });
541549
}
542-
if (err) cb(err)
543-
else if (pkg && typeof pkg === 'object') cb(null, pkg)
550+
if (err) cb(err);
551+
else if (pkg && typeof pkg === 'object') cb(null, pkg);
544552
else {
545553
self.pkgCache[pkgfile] = false;
546554
next();

test/cache_persistent.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ test('uses persistent cache', function (t) {
1313
var p = parser({
1414
persistentCache: function (file, id, pkg, fallback, cb) {
1515
if (file === files.bar) {
16-
return fallback(null, cb)
16+
return fallback(null, cb);
1717
}
1818
cb(null, {
1919
source: 'file at ' + file + '@' + id,
2020
package: pkg,
2121
deps: { './bar': files.bar }
22-
})
22+
});
2323
}
2424
});
2525
p.end({ id: 'foo', file: files.foo, entry: false });
@@ -48,7 +48,7 @@ test('passes persistent cache error through', function (t) {
4848
t.plan(1);
4949
var p = parser({
5050
persistentCache: function (file, id, pkg, fallback, cb) {
51-
cb(new Error('foo'))
51+
cb(new Error('foo'));
5252
}
5353
});
5454
p.end({ id: 'foo', file: files.foo, entry: false });
@@ -59,7 +59,7 @@ test('allow passing of the raw source as string', function (t) {
5959
t.plan(1);
6060
var p = parser({
6161
persistentCache: function (file, id, pkg, fallback, cb) {
62-
fallback(fs.readFileSync(files.bar, 'utf8'), cb)
62+
fallback(fs.readFileSync(files.bar, 'utf8'), cb);
6363
}
6464
});
6565
p.end({ id: 'foo', file: files.foo, entry: false });
@@ -78,4 +78,45 @@ test('allow passing of the raw source as string', function (t) {
7878
});
7979
});
8080

81+
test('send file event with persistent cache', function (t) {
82+
t.plan(2);
83+
var p = parser({
84+
persistentCache: function (file, id, pkg, fallback, cb) {
85+
cb(null, {
86+
source: 'file at ' + file + '@' + id,
87+
package: pkg,
88+
deps: {}
89+
});
90+
}
91+
});
92+
p.end({ id: 'foo', file: files.foo, entry: false });
93+
p.on('file', function (file, id) {
94+
t.same(file, path.resolve(files.foo));
95+
t.same(id, path.resolve(files.foo));
96+
});
97+
});
98+
99+
test('errors of transforms occur in the correct order with a persistent cache', function (t) {
100+
t.plan(3);
101+
var p = parser({
102+
transform: [
103+
path.join(__dirname, 'cache_persistent', 'error_transform')
104+
],
105+
persistentCache: function (file, id, pkg, fallback, cb) {
106+
fallback(fs.readFileSync(files.foo, 'utf8'), cb);
107+
}
108+
});
109+
p.end({ id: 'foo', file: files.foo, entry: false });
110+
var order = 0;
111+
p.on('file', function (file, id) {
112+
t.same(order, 0);
113+
order += 1;
114+
});
115+
p.on('error', function (err) {
116+
t.same(order, 1);
117+
t.same(err.message, 'rawr while parsing file: ' + path.resolve(files.foo));
118+
});
119+
});
120+
121+
81122
function cmp (a, b) { return a.id < b.id ? -1 : 1 }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var through = require('through2');
2+
module.exports = function (file) {
3+
return through(function (chunk, enc, callback) {
4+
callback(new Error('rawr'));
5+
});
6+
};

0 commit comments

Comments
 (0)