@skyway-sdk/rtc-api-client
Version:
The official Next Generation JavaScript SDK for SkyWay
750 lines • 34.7 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RtcApiImpl = void 0;
const common_1 = require("@skyway-sdk/common");
const token_1 = require("@skyway-sdk/token");
const errors_1 = require("../errors");
const util_1 = require("../util");
const log = new common_1.Logger('packages/rtc-api-client/src/infrastructure/api.ts');
class RtcApiImpl {
constructor(_client) {
this._client = _client;
this.closed = false;
this.onClose = new common_1.Event();
this.onFatalError = new common_1.Event();
token_1.SkyWayAuthToken.Decode(this._client.token);
_client.onClose.once(() => {
this.close();
});
_client.onFatalError.add((e) => {
this.onFatalError.emit(e);
});
}
/** @throws {@link SkyWayError} */
connect() {
return __awaiter(this, void 0, void 0, function* () {
yield this._client.connect();
});
}
updateAuthToken(token) {
return __awaiter(this, void 0, void 0, function* () {
token_1.SkyWayAuthToken.Decode(token);
yield this._client.updateToken(token).catch((e) => {
var _a, _b, _c, _d, _e;
const { info } = e;
if (((_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.code) === 429001) {
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updateAuthToken',
path: log.prefix,
info: errors_1.errors.projectUsageLimitExceeded,
error: e,
});
}
const error = this._commonError('RtcApiImpl.updateAuthToken', (_d = (_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) !== null && _d !== void 0 ? _d : -1, e);
if (error) {
throw error;
}
switch ((_e = info === null || info === void 0 ? void 0 : info.error) === null || _e === void 0 ? void 0 : _e.code) {
case 401:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updateAuthToken',
path: log.prefix,
info: errors_1.errors.invalidAuthToken,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updateAuthToken',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
close() {
if (this.closed) {
return;
}
this.closed = true;
log.debug('closed');
this._client.close();
this.onClose.emit();
this.onClose.removeAllListeners();
}
_commonError(method, code, detail) {
switch (code) {
case -32602:
return (0, util_1.createError)({
operationName: method,
info: errors_1.errors.invalidRequestParameter,
path: log.prefix,
error: detail,
});
case -32603:
return (0, util_1.createError)({
operationName: method,
info: errors_1.errors.internalError,
path: log.prefix,
error: detail,
});
case 403:
case 4030:
return (0, util_1.createError)({
operationName: method,
info: errors_1.errors.insufficientPermissions,
path: log.prefix,
error: detail,
});
case 429:
return (0, util_1.createError)({
operationName: method,
info: errors_1.errors.rateLimitExceeded,
path: log.prefix,
error: detail,
});
}
}
/**@throws {@link SkyWayError} */
createChannel(appId, channelInit) {
return __awaiter(this, void 0, void 0, function* () {
const { id } = yield this._client
.createChannel({
appId,
name: channelInit.name,
metadata: channelInit.metadata,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.createChannel', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.createChannel',
path: log.prefix,
info: errors_1.errors.channelNotFound,
error: e,
});
case 409:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.createChannel',
path: log.prefix,
info: errors_1.errors.channelNameDuplicated,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.createChannel',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
const res = yield this.getChannel(appId, { id });
return res;
});
}
getChannel(appId, { name, id }) {
return __awaiter(this, void 0, void 0, function* () {
if (id) {
return yield this._client.getChannel({ appId, id }).catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.getChannel', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.getChannel',
path: log.prefix,
info: errors_1.errors.channelNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.getChannel',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
}
if (name) {
return yield this._client.getChannelByName({ appId, name }).catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.getChannel', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'getChannel',
path: log.prefix,
info: errors_1.errors.channelNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'getChannel',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
}
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.getChannel',
path: log.prefix,
info: errors_1.errors.invalidRequestParameter,
});
});
}
findOrCreateChannel(appId, query) {
return __awaiter(this, void 0, void 0, function* () {
return this._client.findOrCreateChannel(Object.assign(Object.assign({}, query), { appId })).catch((e) => {
var _a, _b, _c, _d;
const { info } = e;
const error = this._commonError('RtcApiImpl.findOrCreateChannel', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
if (query.name && ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) === 409) {
return this.getChannel(appId, { name: query.name });
}
switch ((_d = info === null || info === void 0 ? void 0 : info.error) === null || _d === void 0 ? void 0 : _d.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.findOrCreateChannel',
path: log.prefix,
info: errors_1.errors.channelNotFound,
error: e,
});
case 409:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.findOrCreateChannel',
path: log.prefix,
info: errors_1.errors.channelNameDuplicated,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.findOrCreateChannel',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
deleteChannel(appId, id) {
return __awaiter(this, void 0, void 0, function* () {
yield this._client.deleteChannel({ appId, id }).catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.deleteChannel', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.deleteChannel',
path: log.prefix,
info: errors_1.errors.channelNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.deleteChannel',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
updateChannelMetadata(appId, id, metadata) {
return __awaiter(this, void 0, void 0, function* () {
yield this._client
.updateChannelMetadata({ appId, id, metadata })
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.updateChannelMetadata', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updateChannelMetadata',
path: log.prefix,
info: errors_1.errors.channelNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updateChannelMetadata',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
join(appId, channelId, memberInit) {
return __awaiter(this, void 0, void 0, function* () {
const { memberId } = yield this._client
.addMember({
appId,
channelId,
name: memberInit.name,
metadata: memberInit.metadata,
ttlSec: memberInit.ttlSec,
type: memberInit.type,
subtype: memberInit.subtype,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.addMember', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.addMember',
path: log.prefix,
info: errors_1.errors.channelNotFound,
error: e,
});
case 409:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.addMember',
path: log.prefix,
info: errors_1.errors.memberNameDuplicated,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.addMember',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
const member = {
id: memberId,
name: memberInit.name,
type: memberInit.type,
subtype: memberInit.subtype,
metadata: memberInit.metadata,
};
return member;
});
}
updateMemberTtl(appId, channelId, memberId, ttlSec) {
return __awaiter(this, void 0, void 0, function* () {
yield this._client
.updateMemberTtl({
appId,
channelId,
memberId,
ttlSec,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.updateMemberTtl', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updateMemberTtl',
path: log.prefix,
info: errors_1.errors.memberNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updateMemberTtl',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
/**
* @returns Date.now()
*/
getServerUnixtime(appId) {
return __awaiter(this, void 0, void 0, function* () {
return yield this._client
.getServerUnixtime({
appId,
})
.catch((e) => {
var _a, _b;
const { info } = e;
const error = this._commonError('RtcApiImpl.getServerUnixtime', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.getServerUnixtime',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
});
});
}
updateMemberMetadata(appId, channelId, memberId, metadata) {
return __awaiter(this, void 0, void 0, function* () {
yield this._client
.updateMemberMetadata({
appId,
channelId,
memberId,
metadata,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.updateMemberMetadata', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updateMemberMetadata',
path: log.prefix,
info: errors_1.errors.memberNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updateMemberMetadata',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
leave(appId, channelId, memberId) {
return __awaiter(this, void 0, void 0, function* () {
yield this._client
.leaveChannel({
channelId,
id: memberId,
appId,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.leaveChannel', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.leaveChannel',
path: log.prefix,
info: errors_1.errors.memberNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.leaveChannel',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
/**@throws {@link SkyWayError} */
publish(appId, init) {
return __awaiter(this, void 0, void 0, function* () {
const { publicationId } = yield this._client
.publishStream({
channelId: init.channel,
publisherId: init.publisher,
contentType: init.contentType,
metadata: init.metadata,
origin: init.origin,
codecCapabilities: init.codecCapabilities,
encodings: init.encodings,
isEnabled: init.isEnabled,
type: init.type,
appId,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.publish', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.publish',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
return publicationId;
});
}
updatePublicationMetadata(appId, channelId, publicationId, metadata) {
return __awaiter(this, void 0, void 0, function* () {
yield this._client
.updatePublicationMetadata({
channelId,
publicationId,
metadata,
appId,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.updatePublicationMetadata', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updatePublicationMetadata',
path: log.prefix,
info: errors_1.errors.publicationNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.updatePublicationMetadata',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
disablePublication(appId, channelId, publicationId) {
return __awaiter(this, void 0, void 0, function* () {
yield this._client
.disablePublication({
channelId,
publicationId,
appId,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.disablePublication', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.disablePublication',
path: log.prefix,
info: errors_1.errors.publicationNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.disablePublication',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
enablePublication(appId, channelId, publicationId) {
return __awaiter(this, void 0, void 0, function* () {
yield this._client
.enablePublication({
channelId,
publicationId,
appId,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.enablePublication', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.enablePublication',
path: log.prefix,
info: errors_1.errors.publicationNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.enablePublication',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
unpublish(appId, channelId, publicationId) {
return __awaiter(this, void 0, void 0, function* () {
yield this._client
.unpublishStream({ channelId, publicationId, appId })
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.unpublishStream', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.unpublishStream',
path: log.prefix,
info: errors_1.errors.publicationNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.unpublishStream',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
/**@throws {@link SkyWayError} */
subscribe(appId, init) {
return __awaiter(this, void 0, void 0, function* () {
const { subscriptionId } = yield this._client
.subscribeStream({
channelId: init.channel.id,
subscriberId: init.subscriber.id,
publicationId: init.publication.id,
appId,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.subscribeStream', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.subscribeStream',
path: log.prefix,
info: errors_1.errors.publicationNotFound,
error: e,
});
case 409:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.subscribeStream',
path: log.prefix,
info: errors_1.errors.subscriptionAlreadyExists,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.subscribeStream',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
return subscriptionId;
});
}
unsubscribe(appId, channelId, subscriptionId) {
return __awaiter(this, void 0, void 0, function* () {
yield this._client
.unsubscribeStream({
appId,
channelId,
subscriptionId,
})
.catch((e) => {
var _a, _b, _c;
const { info } = e;
const error = this._commonError('RtcApiImpl.unsubscribeStream', (_b = (_a = info === null || info === void 0 ? void 0 : info.error) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : -1, e);
if (error) {
throw error;
}
switch ((_c = info === null || info === void 0 ? void 0 : info.error) === null || _c === void 0 ? void 0 : _c.code) {
case 404:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.unsubscribeStream',
path: log.prefix,
info: errors_1.errors.publicationNotFound,
error: e,
});
default:
throw (0, util_1.createError)({
operationName: 'RtcApiImpl.unsubscribeStream',
path: log.prefix,
info: errors_1.errors.internalError,
error: e,
});
}
});
});
}
}
exports.RtcApiImpl = RtcApiImpl;
//# sourceMappingURL=api.js.map