sinch-rtc
Version:
RTC JavaScript/Web SDK
88 lines • 2.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PushPayload = void 0;
exports.getFlags = getFlags;
exports.generatePushPayload = generatePushPayload;
const PushType_1 = require("./PushType");
const OcraPushService_1 = require("../push/OcraPushService");
const api_1 = require("../ocra/api");
function getFlags(call) {
let flags = 0;
if (call.requestedVideo) {
flags |= OcraPushService_1.REQUEST_VIDEO_FLAG;
}
return flags;
}
function generatePushPayload(call) {
let payload;
const message = new PushPayload(OcraPushService_1.PUSH_VERSION, PushType_1.PushType.Call, call.id, call.createdAt, call.localPeer.userId, getFlags(call), api_1.Domain.Mxp, call.headers);
if (!message.tryEncode((encoded) => {
payload = encoded;
}) ||
!payload) {
throw new Error("Unable to create push payload");
}
return payload;
}
class PushPayload {
constructor(version, type, sessionId, timestamp, userId, flags, domain, publicHeaders) {
this.version = version;
this.type = type;
this.sessionId = sessionId;
this.timestamp = timestamp;
this.userId = userId;
this.flags = flags;
this.domain = domain;
this.publicHeaders = publicHeaders;
this.supportedVersions = new Map([[4, this.getPayloadV4]]);
}
static isValid(json) {
return (json.version &&
json.type &&
json.session_id &&
json.timestamp &&
json.user_id);
}
static tryDecode(payload, onMessage) {
try {
const json = JSON.parse(payload);
if (PushPayload.isValid(json)) {
onMessage(new PushPayload(json.version, json.type, json.session_id, new Date(json.timestamp * 1000), json.user_id, json.flags, json.domain, json.headers));
return true;
}
else {
return false;
}
}
catch (error) {
return false;
}
}
tryEncode(onPayload) {
const message = this.getPayload();
if (message) {
onPayload(JSON.stringify(message));
return true;
}
console.log(`Unsupported push version ${this.version}`);
return false;
}
getPayload() {
var _a;
return (_a = this.supportedVersions.get(this.version)) === null || _a === void 0 ? void 0 : _a.call(this);
}
getPayloadV4() {
return {
version: this.version,
type: this.type,
session_id: this.sessionId, // eslint-disable-line
timestamp: Math.round(this.timestamp.getTime() / 1000),
user_id: this.userId, // eslint-disable-line
flags: this.flags,
domain: this.domain,
public_headers: this.publicHeaders, // eslint-disable-line
};
}
}
exports.PushPayload = PushPayload;
//# sourceMappingURL=PushPayload.js.map