@minimaltech/node-infra
Version:
Minimal Technology NodeJS Infrastructure - Loopback 4 Framework
107 lines • 5.25 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var SocketIOComponent_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SocketIOComponent = void 0;
const applications_1 = require("../../base/applications");
const base_component_1 = require("../../base/base.component");
const helpers_1 = require("../../helpers");
const utilities_1 = require("../../utilities");
const core_1 = require("@loopback/core");
const common_1 = require("./common");
const helpers_2 = require("./helpers");
let SocketIOComponent = SocketIOComponent_1 = class SocketIOComponent extends base_component_1.BaseComponent {
constructor(application) {
super({ scope: SocketIOComponent_1.name });
this.application = application;
this.bindings = [
core_1.Binding.bind(common_1.SocketIOKeys.IDENTIFIER).to('SOCKET_IO_SERVER'),
core_1.Binding.bind(common_1.SocketIOKeys.SERVER_OPTIONS).to({
path: '/io',
}),
core_1.Binding.bind(common_1.SocketIOKeys.REDIS_CONNECTION).to(null),
];
this.binding();
}
binding() {
if (!this.application) {
throw (0, utilities_1.getError)({
statusCode: 500,
message: '[binding] Invalid application to bind AuthenticateComponent',
});
}
this.logger.info('[binding] Binding authenticate for application...');
const identifier = this.application.getSync(common_1.SocketIOKeys.IDENTIFIER);
let serverOptions = {
path: '/io',
cors: {
origin: '*',
methods: ['GET', 'POST'],
preflightContinue: false,
optionsSuccessStatus: 204,
credentials: true,
},
perMessageDeflate: {
threshold: 4096,
zlibDeflateOptions: {
chunkSize: 10 * 1024,
},
zlibInflateOptions: {
windowBits: 12,
memLevel: 8,
},
clientNoContextTakeover: true,
serverNoContextTakeover: true,
serverMaxWindowBits: 10,
concurrencyLimit: 20,
},
};
if (this.application.isBound(common_1.SocketIOKeys.SERVER_OPTIONS)) {
const extraServerOptions = this.application.getSync(common_1.SocketIOKeys.SERVER_OPTIONS);
serverOptions = Object.assign({}, serverOptions, extraServerOptions);
}
const redisConnection = this.application.getSync(common_1.SocketIOKeys.REDIS_CONNECTION);
if (!(redisConnection instanceof helpers_1.DefaultRedisHelper)) {
throw (0, utilities_1.getError)({
message: '[SocketIOComponent][binding] Invaid instance of redisConnection | Please init connection with RedisHelper for single redis connection or RedisClusterHelper for redis cluster mode!',
});
}
const authenticateFn = this.application.getSync(common_1.SocketIOKeys.AUTHENTICATE_HANDLER);
let clientConnectedFn = null;
if (this.application.isBound(common_1.SocketIOKeys.CLIENT_CONNECTED_HANDLER)) {
clientConnectedFn = this.application.getSync(common_1.SocketIOKeys.CLIENT_CONNECTED_HANDLER);
}
const restServer = this.application.restServer;
const httpServer = restServer.httpServer;
if (!httpServer) {
throw (0, utilities_1.getError)({
message: '[DANGER][SocketIOComponent] Invalid http server to setup io socket server!',
});
}
this.application.bind(common_1.SocketIOKeys.SOCKET_IO_INSTANCE).to(new helpers_2.SocketIOServerHelper({
identifier: identifier !== null && identifier !== void 0 ? identifier : `SOCKET_IO_SERVER`,
server: httpServer.server,
serverOptions,
redisConnection,
authenticateFn,
clientConnectedFn,
}));
}
};
exports.SocketIOComponent = SocketIOComponent;
exports.SocketIOComponent = SocketIOComponent = SocketIOComponent_1 = __decorate([
__param(0, (0, core_1.inject)(core_1.CoreBindings.APPLICATION_INSTANCE)),
__metadata("design:paramtypes", [applications_1.BaseApplication])
], SocketIOComponent);
//# sourceMappingURL=component.js.map