Skip to content

Commit

Permalink
Experimental streaming of response for video content
Browse files Browse the repository at this point in the history
  • Loading branch information
betschki committed Aug 31, 2024
1 parent 69b0f8c commit 01a6875
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,19 @@ proxy.on('proxyReq', function (proxyReq, req, res, options) {
}
});

proxy.on('proxyRes', async (proxyRes, req, res) => {
proxy.on('proxyRes', (proxyRes, req, res) => {
if (DEBUG) {
console.log('Proxying response:', proxyRes.statusCode, req.method, req.url);
console.log('Headers:', proxyRes.headers);
}

let body = Buffer.alloc(0);
// Ensure that we set the headers before streaming the response
res.writeHead(proxyRes.statusCode || 200, proxyRes.headers);

proxyRes.on('data', (data) => {
body = Buffer.concat([body, data]);
});
// Stream the response data directly to the client to avoid buffering large files in memory
proxyRes.pipe(res);

proxyRes.on('end', () => {
res.writeHead(proxyRes.statusCode || 500, proxyRes.headers);
res.end(body);

if (proxyRes.headers['x-cache-invalidate']) {
console.log(
'Detected x-cache-invalidate header, scheduling cache purge...'
Expand Down

0 comments on commit 01a6875

Please sign in to comment.