Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cache #89

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions src/express-http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,29 @@ class ExpressHTTPServer {

this.beforeMiddleware(app);

if (this.cache) {
app.get('/*', this.buildCacheMiddleware());
}

if (this.gzip) {
this.app.use(require('compression')());
app.use(require('compression')());
}

if (this.cache) {
app.use(function(req, res, next) {
if (res.body) {
res.send(res.body);
} else {
next();
}
});
}

if (username !== undefined || password !== undefined) {
this.ui.writeLine(`adding basic auth; username=${username}; password=${password}`);
app.use(basicAuth(username, password));
}

if (this.cache) {
app.get('/*', this.buildCacheMiddleware());
}

if (this.distPath) {
app.get('/', fastbootMiddleware);
app.use(express.static(this.distPath));
Expand Down Expand Up @@ -75,35 +85,52 @@ class ExpressHTTPServer {
.then(response => {
if (response) {
this.ui.writeLine(`cache hit; path=${path}`);
res.send(response);
res.body = response;
} else {
this.ui.writeLine(`cache miss; path=${path}`);
this.interceptResponseCompletion(path, res);
next();
}
next();
})
.catch(() => next());
};
}

interceptResponseCompletion(path, res) {
let send = res.send.bind(res);
let prevWrite = res.write;
let prevEnd = res.end;
let chunks = [];
let cache = this.cache;
let ui = this.ui;

let pushChunk = (chunk) => {
if (!chunk) return;
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
};

res.write = function(chunk) {
pushChunk(chunk);
prevWrite.apply(res, arguments);
};

res.end = function(chunk) {
pushChunk(chunk);

res.send = (body) => {
let ret = send(body);
let body = Buffer.concat(chunks).toString();

this.cache.put(path, body, res)
cache.put(path, body, res)
.then(() => {
this.ui.writeLine(`stored in cache; path=${path}`);
ui.writeLine(`stored in cache; path=${path}`);
})
.catch(() => {
let truncatedBody = body.replace(/\n/g).substr(0, 200);
this.ui.writeLine(`error storing cache; path=${path}; body=${truncatedBody}...`);
ui.writeLine(`error storing cache; path=${path}; body=${truncatedBody}...`);
});

res.send = send;
res.write = prevWrite;
res.end = prevEnd;

return ret;
prevEnd.apply(res, arguments);
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Worker {
}

if (!this.httpServer.cache) { this.httpServer.cache = this.cache; }
if (!this.httpServer.gzip) { this.httpServer.gzip = this.gzip; }
if (!this.httpServer.distPath) { this.httpServer.distPath = this.distPath; }
if (!this.httpServer.ui) { this.httpServer.ui = this.ui; }
}
Expand Down