Permalink
Browse files
Added backpressure support to `staticCache()`
- Loading branch information...
Showing
with
14 additions
and
4 deletions.
-
+14
−4
lib/middleware/staticCache.js
|
@@ -75,11 +75,21 @@ module.exports = function staticCache(options){ |
|
|
|
|
|
// respond with cache
|
|
|
res.writeHead(200, header);
|
|
|
- for (var i = 1, len = hit.length; i < len; ++i) {
|
|
|
- res.write(hit[i]);
|
|
|
+
|
|
|
+ // backpressure
|
|
|
+ function write(i) {
|
|
|
+ var buf = hit[i];
|
|
|
+ if (!buf) return res.end();
|
|
|
+ if (false === res.write(buf)) {
|
|
|
+ res.once('drain', function(){
|
|
|
+ write(++i);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ write(++i);
|
|
|
+ }
|
|
|
}
|
|
|
- res.end();
|
|
|
- return;
|
|
|
+
|
|
|
+ return write(1);
|
|
|
}
|
|
|
|
|
|
// cache static
|
|
|
0 comments on commit
4123b0d