@fabrix/spool-realtime
Version:
Spool: Realtime, Synchronize the client and server via WebSockets using Primus
179 lines • 7.74 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const extension_1 = require("@fabrix/fabrix/dist/common/spools/extension");
const errors_1 = require("@fabrix/fabrix/dist/errors");
const api = __importStar(require("./api/index"));
const config = __importStar(require("./config/index"));
const pkg = __importStar(require("../package.json"));
const Validator = __importStar(require("./validator"));
const primus_1 = __importDefault(require("primus"));
const primusDefaults = {
transformer: 'engine.io'
};
class RealtimeSpool extends extension_1.ExtensionSpool {
constructor(app) {
super(app, {
config: config,
pkg: pkg,
api: api
});
this.extensions = {
sockets: {
get: () => {
return this.sockets;
},
set: (newValue) => {
throw new Error('sockets can not be set through FabrixApp, check spool-realtime instead');
},
enumerable: true,
configurable: true
}
};
return this;
}
get sockets() {
return this._sockets;
}
validate() {
return __awaiter(this, void 0, void 0, function* () {
const requiredSpools = [
'router'
];
const spools = Object.keys(this.app.spools);
if (!spools.some(v => requiredSpools.indexOf(v) === -1)) {
return Promise.reject(new Error(`spool-realtime requires spools: ${requiredSpools.join(', ')}!`));
}
return Promise.all([
Validator.validateConfig.config(this.app.config.get('realtime')),
]);
});
}
initialize() {
return __awaiter(this, void 0, void 0, function* () {
const isExpress = this.app.config.get('web.server') === 'express';
const listener = isExpress
? 'webserver:http'
: 'webserver:http:ready';
const pathname = this.app.config.get('realtime.prefix')
? `${this.app.config.get('realtime.prefix')}/primus`
: 'primus';
this.app.log.debug('Realtime: Primus Socket Path:', `/${pathname}`);
const path = this.app.config.get('realtime.path')
|| this.app.config.get('main.paths.www')
|| __dirname;
this.app.log.debug('Realtime: Primus JS Path:', `/${path}/primus.js`);
const primusConfig = Object.assign({}, { pathname: pathname }, this.app.config.get('realtime.primus'));
const plugins = this.app.config.get('realtime.plugins') || {};
this.app.emit('sockets:starting');
return new Promise((resolve, reject) => {
this.app.once(listener, (httpServer) => {
if (Array.isArray(httpServer)) {
httpServer = httpServer[0];
}
try {
this._sockets = primus_1.default(httpServer, Object.assign({}, primusConfig.options, primusDefaults));
}
catch (err) {
this.app.log.error('Primus failed to create', err);
reject(err);
}
try {
Object.keys(plugins).forEach(k => {
this._sockets.plugin(k, plugins[k]);
});
}
catch (err) {
reject(err);
}
Object.keys(this.app.sparks || {}).forEach(k => {
this._sockets.on('connection', this.app.sparks[k].connection);
if (this.app.sparks[k].data) {
this._sockets.on('incoming::data', this.app.sparks[k].data);
}
if (this.app.sparks[k].initialised) {
this._sockets.on('initialised', this.app.sparks[k].initialised);
}
if (this.app.sparks[k].plugin) {
this._sockets.on('plugin', this.app.sparks[k].plugin);
}
if (this.app.sparks[k].plugout) {
this._sockets.on('plugout', this.app.sparks[k].plugout);
}
if (this.app.sparks[k].log) {
this._sockets.on('log', this.app.sparks[k].log);
}
if (this.app.sparks[k].end) {
this._sockets.on('end', this.app.sparks[k].end);
}
if (this.app.sparks[k].close) {
this._sockets.on('close', this.app.sparks[k].close);
}
this._sockets.on('disconnection', this.app.sparks[k].disconnection);
});
this._sockets.save(path + '/primus.js', function save(err) {
if (err) {
return reject(err);
}
return resolve();
});
});
})
.then(() => {
this.app.emit('sockets:ready', this._sockets);
return;
});
});
}
unload() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
this.app.emit('sockets:stopping', this._sockets);
this.sockets.on('destroy', function () {
this.app.log.debug('primus destroyed');
});
this.sockets.destroy(this.app.config.get('realtime.destroy'));
return resolve();
});
});
}
sanity() {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve()
.then(() => {
if (!this._sockets) {
throw new errors_1.SanityError('Sockets does not exist');
}
return;
})
.then(() => {
if (!this._sockets.Spark) {
throw new errors_1.SanityError('Socket Spark does not exist');
}
return;
})
.catch(err => {
return Promise.reject(err);
});
});
}
}
exports.RealtimeSpool = RealtimeSpool;
//# sourceMappingURL=RealtimeSpool.js.map