cryptpad/lib/api.js

52 lines
2 KiB
JavaScript
Raw Normal View History

/* jshint esversion: 6 */
const WebSocketServer = require('ws').Server;
const NetfluxSrv = require('chainpad-server');
module.exports.create = function (config) {
2020-02-07 23:58:57 +00:00
// asynchronously create a historyKeeper and RPC together
require('./historyKeeper.js').create(config, function (err, historyKeeper) {
if (err) { throw err; }
2020-02-07 23:58:57 +00:00
var log = config.log;
var noop = function () {};
var special_errors = {};
['EPIPE', 'ECONNRESET'].forEach(function (k) { special_errors[k] = noop; });
special_errors.NF_ENOENT = function (error, label, info) {
log.error(label, {
info: info,
});
};
2020-02-07 23:58:57 +00:00
// spawn ws server and attach netflux event handlers
NetfluxSrv.create(new WebSocketServer({ server: config.httpServer}))
.on('channelClose', historyKeeper.channelClose)
.on('channelMessage', historyKeeper.channelMessage)
.on('channelOpen', historyKeeper.channelOpen)
.on('sessionClose', historyKeeper.sessionClose)
2020-02-07 23:58:57 +00:00
.on('error', function (error, label, info) {
if (!error) { return; }
if (error && error.code) {
/* EPIPE,ECONNERESET, NF_ENOENT */
if (typeof(special_errors[error.code]) === 'function') {
return void special_errors[error.code](error, label, info);
}
}
2020-02-07 23:58:57 +00:00
/* labels:
SEND_MESSAGE_FAIL, SEND_MESSAGE_FAIL_2, FAIL_TO_DISCONNECT,
FAIL_TO_TERMINATE, HANDLE_CHANNEL_LEAVE, NETFLUX_BAD_MESSAGE,
NETFLUX_WEBSOCKET_ERROR
2020-02-07 23:58:57 +00:00
*/
log.error(label, {
code: error.code,
message: error.message,
stack: error.stack,
info: info,
});
})
.register(historyKeeper.id, historyKeeper.directMessage);
});
};