Skip to content

Commit e498159

Browse files
committed
Fix cache
1 parent a5d194e commit e498159

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/express-http-server.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,40 @@ class ExpressHTTPServer {
8787
}
8888

8989
interceptResponseCompletion(path, res) {
90-
let send = res.send.bind(res);
90+
let prevWrite = res.write;
91+
let prevEnd = res.end;
92+
let chunks = [];
93+
let cache = this.cache;
94+
let ui = this.ui;
95+
96+
let pushChunk = (chunk) => {
97+
if (!chunk) return;
98+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
99+
};
100+
101+
res.write = function(chunk) {
102+
pushChunk(chunk);
103+
prevWrite.apply(res, arguments);
104+
};
105+
106+
res.end = function(chunk) {
107+
pushChunk(chunk);
91108

92-
res.send = (body) => {
93-
let ret = send(body);
109+
let body = Buffer.concat(chunks).toString();
94110

95-
this.cache.put(path, body, res)
111+
cache.put(path, body, res)
96112
.then(() => {
97-
this.ui.writeLine(`stored in cache; path=${path}`);
113+
ui.writeLine(`stored in cache; path=${path}`);
98114
})
99115
.catch(() => {
100116
let truncatedBody = body.replace(/\n/g).substr(0, 200);
101-
this.ui.writeLine(`error storing cache; path=${path}; body=${truncatedBody}...`);
117+
ui.writeLine(`error storing cache; path=${path}; body=${truncatedBody}...`);
102118
});
103119

104-
res.send = send;
120+
res.write = prevWrite;
121+
res.end = prevEnd;
105122

106-
return ret;
123+
prevEnd.apply(res, arguments);
107124
};
108125
}
109126
}

0 commit comments

Comments
 (0)