Initial commit.

This commit is contained in:
Pierre Bourdon 2015-06-20 23:48:14 +02:00
commit 08f27b1b08
5 changed files with 104 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

27
README.md Normal file
View File

@ -0,0 +1,27 @@
# ep_http_hook
Pushes pad updates to a remote location through HTTP POST requests. Batches
updates to avoid overloading the remote location.
## Settings
```
"ep_http_hook": {
"url": "https://...",
"hmac_key": "secret"
}
```
## POST content
The updates are sent as POST requests with the following body:
* HMAC-SHA256 of the payload (in hex encoding).
* A space character.
* The payload data as JSON.
The payload is a list of events which can be of the following types (identified
by their `type` field):
* `pad_update`: A pad was updated. The pad id is contained in the `id` field,
and its raw contents in the `text` field.

10
ep.json Normal file
View File

@ -0,0 +1,10 @@
{
"parts": [
{
"name": "main",
"hooks": {
"padUpdate": "ep_http_hook/main"
}
}
]
}

47
main.js Normal file
View File

@ -0,0 +1,47 @@
var crypto = require("crypto");
var request = require("request");
var settings = require('ep_etherpad-lite/node/utils/Settings');
// Constants
var BATCHING_TIME_MS = 2000;
// Globals (boo).
var pending_events = [];
var pending_events_sender = null;
function computeHmac(data) {
var key = settings.ep_http_hook.hmac_key || '';
return crypto.createHmac('sha256', key).update(data).digest('hex');
}
function sendSerializedEvents(data) {
request.post({ url: settings.ep_http_hook.url, body: data });
}
function sendEvent(data) {
if (!settings.ep_http_hook || !settings.ep_http_hook.url) {
return;
}
if (pending_events_sender) {
clearTimeout(pending_events_sender);
}
pending_events.push(data);
pending_events_sender = setTimeout(function () {
pending_events_sender = null;
var serialized_data = JSON.stringify(pending_events);
var data_to_send = computeHmac(serialized_data) + ' ' + serialized_data;
sendSerializedEvents(data_to_send);
pending_events = [];
}, BATCHING_TIME_MS);
};
exports.padUpdate = function (hook_name, context) {
var pad = context.pad;
var hook_data = {
type: "pad_update",
id: pad.id,
text: pad.atext.text
};
sendEvent(hook_data);
};

19
package.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "ep_http_hook",
"version": "0.0.1",
"description": "Adds webhooks support to Etherpad lite.",
"homepage": "https://github.com/delroth/ep_http_hook",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/delroth/ep_http_hook"
},
"author": {
"name": "Pierre Bourdon",
"email": "delroth@gmail.com"
},
"dependencies": {
"crypto": "0.0.3",
"request": "^2.58.0"
}
}