Skip to content

Commit

Permalink
Revert back to using our own file streaming
Browse files Browse the repository at this point in the history
The outage was caused by a race condition caused by the deployment
scripts copying files from staging to production. During this time the
server could read a partial file, which would then be cached immutably
for 1 year. Currently we're fixing that by having the deployment scripts
stop the backend service during the copy.
  • Loading branch information
GarboMuffin committed Aug 20, 2024
1 parent c003ec7 commit 57d4d94
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,17 @@ app.get('/*', asyncHandler(async (req, res, next) => {
}
const varyAcceptEncoding = fileEncodings.length > 0;

const sendFileHeaders = () => {
stats.handleServedFile(pathName);
res.setHeader('Content-Type', fileType.type);
if (contentEncoding !== null) {
res.setHeader('Content-Encoding', contentEncoding);
}
if (varyAcceptEncoding) {
res.setHeader('Vary', 'Accept-Encoding');
}
};

if (requiresSpecialRewriting) {
let fileContents = await readFile(filePath, 'utf-8');

Expand Down Expand Up @@ -376,23 +387,25 @@ app.get('/*', asyncHandler(async (req, res, next) => {
fileContents = fileContents.replace('</head>', newHead + '</head>');
}

res.setHeader('Content-Type', fileType.type);
if (varyAcceptEncoding) {
res.setHeader('Vary', 'Accept-Encoding');
}
sendFileHeaders();
res.send(fileContents);
} else {
const headers = {};
headers['Content-Type'] = fileType.type;
if (contentEncoding !== null) {
headers['Content-Encoding'] = contentEncoding;
}
if (varyAcceptEncoding) {
headers['Vary'] = 'Accept-Encoding';
}

res.sendFile(filePath, {
headers
const readStream = fs.createReadStream(filePath, {
highWaterMark: 1024 * 64
});
readStream.on('open', () => {
sendFileHeaders();
res.setHeader('Content-Length', fileStat.size);
readStream.pipe(res);
res.on('close', () => {
readStream.destroy();
});
res.on('error', (err) => {
readStream.destroy(err);
});
});
readStream.on('error', (err) => {
next(err);
});
}
}));
Expand Down

0 comments on commit 57d4d94

Please sign in to comment.