@signiant/media-shuttle-sdk-base
Version:
The base parent sdk behind other media shuttle sdks (e.g. media-shuttle-sdk)
109 lines (108 loc) • 5.32 kB
JavaScript
/* eslint-disable import/first */
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import ChannelProviderFactory from './ChannelProviderFactory';
import { v4 as uuid } from 'uuid';
import { cloneDeep } from 'lodash';
import AggregateChannelProvider from '../AggregateChannelProvider';
import SizeMessageChannelDecorator from '../decorators/SizeMessageChannelDecorator';
import EncryptionMessageChannelDecorator from '../decorators/EncryptionMessageChannelDecorator';
import * as constants from '../../constants';
import WebsocketAggregateChannelProvider from '../websocket/WebsocketAggregateChannelProvider';
import { LogManager } from '../../../system';
import PubNubDualChannelProvider from '../pubnub/PubNubDualChannelProvider';
import { CodedError, ErrorCode } from '../../../../external';
import ErrorMessages from '../../../../external/common/ErrorMessages';
var DualChannelProviderFactory = /** @class */ (function (_super) {
__extends(DualChannelProviderFactory, _super);
function DualChannelProviderFactory(params) {
var _this = _super.call(this, params) || this;
_this._providerInfoList = [];
_this._sessionId = uuid();
return _this;
}
DualChannelProviderFactory.prototype.getProvider = function (messagingConfig) {
var _this = this;
this.parseProvidersFromConfig(messagingConfig);
var provider = new AggregateChannelProvider(messagingConfig, this._encryptionInfo);
provider.sessionId = this._sessionId;
if (this._legacyPubNubInfo) {
provider.addProvider(this.buildChannel(this._legacyPubNubInfo));
}
else if (this._providerInfoList.length > 0) {
this._providerInfoList.forEach(function (providerInfoObj) {
//MessageChannel should never be undefined because we have already filtered to
//known types.
var messageChannel = _this.buildChannel(providerInfoObj);
LogManager.debug("Provider Info: ".concat(messageChannel.getProviderInfo()));
provider.addProvider(messageChannel);
});
}
else {
throw new CodedError({
code: ErrorCode.NO_PROVIDER_ERROR,
message: ErrorMessages.generatedErrorMessages[ErrorCode.NO_PROVIDER_ERROR](JSON.stringify(messagingConfig)),
});
}
return provider;
};
DualChannelProviderFactory.prototype.parseProvidersFromConfig = function (providerInfo) {
var _this = this;
if (providerInfo) {
if (Array.isArray(providerInfo.providers)) {
// new messaging service
providerInfo.providers.forEach(function (providerInfoObj) {
if (_this.isKnownProvider(providerInfoObj)) {
_this._providerInfoList.push(cloneDeep(providerInfoObj));
}
});
}
else {
//legacy messaging service
if (this.isPubNub(providerInfo)) {
this._legacyPubNubInfo = cloneDeep(providerInfo);
}
}
}
else {
throw new CodedError({
code: ErrorCode.NO_PROVIDER_ERROR,
message: ErrorMessages.generatedErrorMessages[ErrorCode.NO_PROVIDER_ERROR](JSON.stringify(providerInfo)),
});
}
};
DualChannelProviderFactory.prototype.buildDecorator = function (provider) {
var sizeDecorator = new SizeMessageChannelDecorator(provider);
var encryptDecorator = new EncryptionMessageChannelDecorator(sizeDecorator, this._encryptionInfo.symmetricKey, constants.preferredEncryptionMode);
encryptDecorator.updateSettings({ encrypt: false });
return encryptDecorator;
};
DualChannelProviderFactory.prototype.buildChannel = function (providerConfig) {
var channel;
LogManager.debug('DualChannelProviderFactory', providerConfig);
if (this.isPubNub(providerConfig)) {
channel = new PubNubDualChannelProvider(providerConfig);
// provider.legacyParam = JSON.parse(JSON.stringify(channel.getChannelParams()));
}
else if (this.isWebsocket(providerConfig)) {
var websocketAggregate = new WebsocketAggregateChannelProvider(providerConfig, this._sessionId, this._encryptionInfo);
channel = this.buildDecorator(websocketAggregate);
}
return channel;
};
return DualChannelProviderFactory;
}(ChannelProviderFactory));
export default DualChannelProviderFactory;