UNPKG

@api.video/private-video-session

Version:
196 lines (187 loc) 9.12 kB
import md5 from 'md5'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol */ function __awaiter(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()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; const isAssetUrlOptions = (options) => { return options && "assetUrl" in options; }; class PrivateVideoSession { constructor(options) { this.domains = options.domains || {}; const videoIdPrivateToken = isAssetUrlOptions(options) ? this.initFromAssetUrl(options.assetUrl) : this.initFromToken(options.token, options.videoId); this.videoId = videoIdPrivateToken.videoId; this.privateToken = videoIdPrivateToken.privateToken; this.environment = videoIdPrivateToken.environment || "production"; this.videoType = videoIdPrivateToken.videoType || "vod"; if (Object.keys(this.domains).length > 0 && !this.domains[this.videoType]) { throw new Error(`The ${this.videoType} domain is required when using ${this.videoType} assets with custom domains.`); } } getHlsUrl() { return __awaiter(this, void 0, void 0, function* () { return this.videoType === "vod" ? `https://${this.getMediaHost()}/vod/${this.videoId}/token/${this.privateToken}/hls/manifest.m3u8?avh=${yield this.getSessionToken()}` : `https://${this.getMediaHost()}/private/${this.privateToken}/${this.videoId}.m3u8?avh=${yield this.getSessionToken()}`; }); } getPlayerUrl() { return __awaiter(this, void 0, void 0, function* () { return `https://${this.getEmbedHost()}/${this.videoType}/${this.videoId}?token=${this.privateToken}&avh=${yield this.getSessionToken()}`; }); } getMp4Url() { return __awaiter(this, void 0, void 0, function* () { if (this.videoType === "live") { throw new Error("The mp4 url is not available for live streams."); } return `https://${this.getMediaHost()}/vod/${this.videoId}/token/${this.privateToken}/mp4/source.mp4?avh=${yield this.getSessionToken()}`; }); } getThumbnailUrl() { return __awaiter(this, void 0, void 0, function* () { return this.videoType === "vod" ? `https://${this.getMediaHost()}/vod/${this.videoId}/token/${this.privateToken}/thumbnail.jpg?avh=${yield this.getSessionToken()}` : `https://${this.getMediaHost()}/private/${this.privateToken}/${this.videoId}/thumbnail.jpg?avh=${yield this.getSessionToken()}`; }); } getSessionToken() { return __awaiter(this, void 0, void 0, function* () { if (this.sessionToken) { return this.sessionToken; } const sessionStorageKey = md5(`xTokenSession_${this.privateToken}`); const storageSessionToken = sessionStorage.getItem(sessionStorageKey); if (storageSessionToken) { this.sessionToken = storageSessionToken; return storageSessionToken; } const newSessionToken = yield this.fetchSessionToken(); if (newSessionToken) { sessionStorage.setItem(sessionStorageKey, newSessionToken); this.sessionToken = newSessionToken; } return newSessionToken; }); } getEmbedHost() { if (this.domains.embed) { return this.domains.embed; } return this.environment === "production" ? "embed.api.video" : "embed.staging.api.video"; } getMediaHost() { if (this.domains[this.videoType]) { return this.domains[this.videoType]; } return this.environment === "production" ? `${this.videoType}.api.video` : `${this.videoType}.staging.api.video`; } initFromAssetUrl(privateAssetUrl) { var _a; const urls = [ { re: new RegExp(`https:\/\/${this.domains.vod || "vod.api.video"}\/vod\/(?<videoId>[^\/]*)\/token\/(?<privateToken>[^\/]*)\/.*`, "gm"), type: "vod", env: "production", }, { re: new RegExp(`https:\/\/${this.domains.vod || "vod.staging.api.video"}\/vod\/(?<videoId>[^\/]*)\/token\/(?<privateToken>[^\/]*)\/.*`, "gm"), type: "vod", env: "staging", }, { re: new RegExp(`https:\/\/${this.domains.embed || "embed.api.video"}\/vod\/(?<videoId>[^\/]*)\\?token=(?<privateToken>[^\/]*)`, "gm"), type: "vod", env: "production", }, { re: new RegExp(`https:\/\/${this.domains.embed || "embed.staging.api.video"}\/vod\/(?<videoId>[^\/]*)\\?token=(?<privateToken>[^\/]*)`, "gm"), type: "vod", env: "staging", }, { re: new RegExp(`https:\/\/${this.domains.live || "live.api.video"}\/private\/(?<privateToken>[^\/]*)\/(?<videoId>[^\.]*)\..*`, "gm"), type: "live", env: "production", }, { re: new RegExp(`https:\/\/${this.domains.live || "live.staging.api.video"}\/private\/(?<privateToken>[^\/]*)\/(?<videoId>[^\.]*)\..*`, "gm"), type: "live", env: "staging", }, { re: new RegExp(`https:\/\/${this.domains.embed || "embed.api.video"}\/live\/(?<videoId>[^\/]*)\\?token=(?<privateToken>[^\/]*)`, "gm"), type: "live", env: "production", }, { re: new RegExp(`https:\/\/${this.domains.embed || "embed.staging.api.video"}\/live\/(?<videoId>[^\/]*)\\?token=(?<privateToken>[^\/]*)`, "gm"), type: "live", env: "staging", }, ]; for (const url of urls) { const parsed = url.re.exec(privateAssetUrl); if (((_a = parsed === null || parsed === void 0 ? void 0 : parsed.groups) === null || _a === void 0 ? void 0 : _a.videoId) && parsed.groups.privateToken) { return { videoId: parsed.groups.videoId, privateToken: parsed.groups.privateToken, environment: url.env, videoType: url.type, }; } } throw new Error("The provided URL doesn't look like an api.video private asset URL."); } initFromToken(privateToken, videoId) { if (!privateToken || !videoId) { throw new Error("The provided token and videoId are invalid."); } return { videoId, privateToken, }; } fetchSessionToken() { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(this.videoType === "vod" ? `https://${this.getMediaHost()}/vod/${this.videoId}/token/${this.privateToken}/session` : `https://${this.getMediaHost()}/private/${this.privateToken}/${this.videoId}/session`); if (response.status !== 200) { throw new Error("Can't retrieve a session token. The private token has probably already been used."); } const json = yield response.json(); return json.session_token; }); } } export { PrivateVideoSession }; //# sourceMappingURL=index.es.js.map