Skip to content

Commit

Permalink
Merge pull request #7 from aarranz/feature/nginx-as-frontend
Browse files Browse the repository at this point in the history
Minor fixes to allow to use Nginx as front-end
  • Loading branch information
aarranz authored Apr 2, 2018
2 parents cdc6ed5 + a0fa104 commit 28c23c6
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,27 @@ var build_absolute_url = function build_absolute_url(req, url) {
};

exports.options_eventsource = function options_eventsource(req, res) {
res.header('Connection', 'keep-alive');
var origin = req.header('Origin');
if (origin != null) {
res.header('Access-Control-Allow-Origin', origin);
res.header('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
res.header('Access-Control-Expose-Headers', 'Location');
}
res.header('Connection', 'keep-alive');
res.header('Content-Length', '0');
res.sendStatus(204);
};

exports.options_eventsource_entry = function options_eventsource_entry(req, res) {
res.header('Connection', 'keep-alive');
var origin = req.header('Origin');
if (origin != null) {
res.header('Access-Control-Allow-Origin', origin);
res.header('Access-Control-Allow-Methods', 'GET, DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
}
res.header('Connection', 'keep-alive');
res.header('Content-Length', '0');
res.sendStatus(204);
};

Expand Down Expand Up @@ -193,6 +195,8 @@ exports.eventsource = function eventsource(req, res) {

res.header('Cache-Control', 'no-cache');
res.header('Connection', 'keep-alive');
// Forbid Nginx buffering
res.header('X-Accel-Buffering', 'no');
req.socket.setTimeout(0);

if (connection.response != null) {
Expand Down Expand Up @@ -220,6 +224,8 @@ exports.eventsource = function eventsource(req, res) {
url: build_absolute_url(req, '/eventsource/' + connection.id)
}).toString('utf8') + '\n\n');

// Force sending init event
res.flush();
res.on('close', connection.close_listener);
};

Expand Down Expand Up @@ -253,19 +259,21 @@ exports.delete_eventsource = function delete_eventsource(req, res) {
delete callbacks[callback_id];
}

res.header('Content-Length', '0');
res.sendStatus(204);
};

exports.options_callbacks = function options_callbacks(req, res) {
res.header('Cache-Control', 'no-cache');
res.header('Connection', 'keep-alive');
var origin = req.header('Origin');
if (origin != null) {
res.header('Access-Control-Allow-Origin', origin);
res.header('Access-Control-Allow-Methods', 'OPTIONS, POST');
res.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With');
res.header('Access-Control-Expose-Headers', 'Location');
}
res.header('Cache-Control', 'no-cache');
res.header('Connection', 'keep-alive');
res.header('Content-Length', '0');
res.sendStatus(204);
};

Expand All @@ -274,6 +282,7 @@ exports.create_callback = function create_callback(req, res) {

res.header('Cache-Control', 'no-cache');
res.header('Connection', 'keep-alive');
res.header('Content-Length', '0');
origin = req.header('Origin');
if (origin != null) {
res.header('Access-Control-Allow-Origin', origin);
Expand Down Expand Up @@ -341,24 +350,28 @@ exports.process_callback = function process_callback(req, res) {
}).toString('utf8');
eventsource.write('event: notification\n');
eventsource.write('data: ' + data + '\n\n');
// Send this event
eventsource.flush();
} else {
console.log('Ignoring notification as the client is not connected');
}

res.header('Content-Length', '0');
res.sendStatus(204);
callbacks[req.params.id].notification_counter++;
});
};

exports.options_callback_entry = function options_callback_entry(req, res) {
res.header('Cache-Control', 'no-cache');
res.header('Connection', 'keep-alive');
var origin = req.header('Origin');
if (origin != null) {
res.header('Access-Control-Allow-Origin', origin);
res.header('Access-Control-Allow-Methods', 'DELETE, OPTIONS, POST');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
}
res.header('Cache-Control', 'no-cache');
res.header('Connection', 'keep-alive');
res.header('Content-Length', '0');
res.sendStatus(204);
};

Expand All @@ -377,5 +390,8 @@ exports.delete_callback = function delete_callback(req, res) {
}

removeCallback(req.params.id);
res.header('Cache-Control', 'no-cache');
res.header('Connection', 'keep-alive');
res.header('Content-Length', '0');
res.sendStatus(204);
};

0 comments on commit 28c23c6

Please sign in to comment.