I'm trying to send spaces at specific interval to avoid heroku
timeout(30s), but I want to support the gzip encoding. So I'm trying something similar as below:
const express = require('express')
const zlib = require('zlib')
const app = express()
const gzipped_space = zlib.gzipSync(' ');
app.get('/json-chunked', function (req, res) {
let interval = setInterval(() => {
res.write(' ');
}, 1000);
setTimeout(() => {
res.write('{"hello":"world"}');
clearInterval(interval);
res.end();
}, 5500);
});
app.get('/gzip-chunked', function (req, res) {
res.writeHead(200, {
'Content-Encoding': 'gzip' // setting the encoding to gzip
});
let interval = setInterval(() => {
res.write(gzipped_space);
}, 1000);
setTimeout(() => {
res.write(zlib.gzipSync('{"hello":"world"}'));
clearInterval(interval);
res.end();
}, 5500);
});
app.listen(3000, () => {
console.log('listening on 3000');
})
The http://localhost:3000/json-chunked works correctly in the browser and whole json response is received with spaces in the start. But for http://localhost:3000/gzip-chunked the browser seems to receive only the first space and the request is terminated. However the same request from the postman
works correctly and whole response is received and decoded there.
Does browser expects the whole response to be one gzip body divided in chunks and not the smaller gzipped chunks?(It feels very odd that browser doesn't support the separately gzipped chunks :( ) Are there any other options with which I can send back the empty space the keep the connection alive?
EDIT: Are there any special characters in gzip that are ignored while decompressing?