-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
20,332 additions
and
26,261 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//@flow | ||
import {Utils, VERSION} from 'playkit-js' | ||
|
||
const PLAY_MANIFEST = 'playmanifest/'; | ||
const PLAY_SESSION_ID = 'playSessionId='; | ||
const REFERRER = 'referrer='; | ||
const CLIENT_TAG = 'clientTag=html5:v'; | ||
|
||
/** | ||
* @param {Player} player - player | ||
* @return {void} | ||
* @public | ||
*/ | ||
function handleSessionId(player: Player): void { | ||
if (player.config.session && player.config.session.id) { // on change media | ||
updateSessionId(player); | ||
} else { // on first playback | ||
addSessionId(player); | ||
} | ||
} | ||
|
||
/** | ||
* @param {Player} player - player | ||
* @return {void} | ||
* @private | ||
*/ | ||
function addSessionId(player: Player): void { | ||
let primaryGUID = Utils.Generator.guid(); | ||
let secondGUID = Utils.Generator.guid(); | ||
player.sessionId = primaryGUID + ':' + secondGUID; | ||
} | ||
|
||
/** | ||
* @param {Player} player - player | ||
* @return {void} | ||
* @private | ||
*/ | ||
function updateSessionId(player: Player): void { | ||
let secondGuidInSessionIdRegex = /:((?:[a-z0-9]|-)*)/i; | ||
let secondGuidInSessionId = secondGuidInSessionIdRegex.exec(player.config.session.id); | ||
if (secondGuidInSessionId && secondGuidInSessionId[1]) { | ||
player.sessionId = player.config.session.id.replace(secondGuidInSessionId[1], Utils.Generator.guid()); | ||
} | ||
} | ||
|
||
/** | ||
* @param {Object} source - source | ||
* @param {string} sessionId - session id | ||
* @return {void} | ||
* @private | ||
*/ | ||
function updateSessionIdInUrl(source: Object = {}, sessionId: string): void { | ||
let sessionIdInUrlRegex = new RegExp(PLAY_SESSION_ID + '((?:[a-z0-9]|-)*:(?:[a-z0-9]|-)*)', 'i'); | ||
let sessionIdInUrl = sessionIdInUrlRegex.exec(source.url); | ||
if (sessionIdInUrl && sessionIdInUrl[1]) { // this url has session id (has already been played) | ||
source.url = source.url.replace(sessionIdInUrl[1], sessionId); | ||
} else { | ||
let delimiter = source.url.indexOf('?') === -1 ? '?' : '&'; | ||
source.url += delimiter + PLAY_SESSION_ID + sessionId; | ||
} | ||
} | ||
|
||
/** | ||
* @param {Object} source - source | ||
* @return {void} | ||
* @private | ||
*/ | ||
function addReferrer(source: Object) { | ||
if (source.url.indexOf(REFERRER) === -1) { | ||
let delimiter = source.url.indexOf('?') === -1 ? '?' : '&'; | ||
source.url += delimiter + REFERRER + btoa(document.referrer || document.URL); | ||
} | ||
} | ||
|
||
/** | ||
* @param {Object} source - source | ||
* @return {void} | ||
* @private | ||
*/ | ||
function addClientTag(source: Object) { | ||
if (source.url.indexOf(CLIENT_TAG) === -1) { | ||
let delimiter = source.url.indexOf('?') === -1 ? '?' : '&'; | ||
source.url += delimiter + CLIENT_TAG + VERSION; | ||
} | ||
} | ||
|
||
/** | ||
* @param {Array<Object>} sources - player sources | ||
* @param {Player} player - player | ||
* @return {void} | ||
* @private | ||
*/ | ||
function addKalturaParams(sources: Array<Object> = [], player: Player): void { | ||
handleSessionId(player); | ||
for (let source of sources) { | ||
if (typeof source.url === 'string' && source.url.toLowerCase().indexOf(PLAY_MANIFEST) > -1) { | ||
updateSessionIdInUrl(source, player.config.session.id); | ||
addReferrer(source); | ||
addClientTag(source); | ||
} | ||
} | ||
} | ||
|
||
export {addKalturaParams, handleSessionId, updateSessionIdInUrl, addReferrer, addClientTag} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import {addKalturaParams, handleSessionId, updateSessionIdInUrl, addReferrer, addClientTag} from '../../src/kaltura-params' | ||
import {VERSION} from 'playkit-js' | ||
|
||
class Player { | ||
set sessionId(s){ | ||
this.config.session.id = s; | ||
} | ||
} | ||
|
||
let player = new Player(); | ||
|
||
describe('addKalturaParams', function () { | ||
|
||
it('should add session id, referrer and client tag for playManifest source', function () { | ||
let source1 = {url: 'a/b/c/playmanifest/source'}; | ||
let source2 = {url: 'd/e/f/playmanifest/source?a'}; | ||
player.config = {session: {}, sources: {progressive: [source1, source2]}}; | ||
addKalturaParams(player.config.sources.progressive, player); | ||
source1.url.should.be.equal('a/b/c/playmanifest/source?playSessionId=' + player.config.session.id + '&referrer=' + btoa(document.referrer) + '&clientTag=html5:v' + VERSION); | ||
source2.url.should.be.equal('d/e/f/playmanifest/source?a&playSessionId=' + player.config.session.id + '&referrer=' + btoa(document.referrer) + '&clientTag=html5:v' + VERSION); | ||
}); | ||
|
||
it('should add session id, referrer and client tag for PLAYMANIFEST source', function () { | ||
let source1 = {url: 'a/b/c/PLAYMANIFEST/source'}; | ||
let source2 = {url: 'd/e/f/PLAYMANIFEST/source?a'}; | ||
player.config = {session: {}, sources: {progressive: [source1, source2]}}; | ||
addKalturaParams(player.config.sources.progressive, player); | ||
source1.url.should.be.equal('a/b/c/PLAYMANIFEST/source?playSessionId=' + player.config.session.id + '&referrer=' + btoa(document.referrer) + '&clientTag=html5:v' + VERSION); | ||
source2.url.should.be.equal('d/e/f/PLAYMANIFEST/source?a&playSessionId=' + player.config.session.id + '&referrer=' + btoa(document.referrer) + '&clientTag=html5:v' + VERSION); | ||
}); | ||
|
||
it('should add nothing for no playManifest source', function () { | ||
let source1 = {url: 'a/b/c'}; | ||
addKalturaParams([source1], player); | ||
player.config.session.id.should.be.exist; | ||
source1.url.should.be.equal('a/b/c'); | ||
}); | ||
}); | ||
|
||
describe('handleSessionId', function () { | ||
let sessionIdRegex = /(?:[a-z0-9]|-)*:(?:[a-z0-9]|-)*/i; | ||
it('should add the player session id', function () { | ||
player.config = {session: {}}; | ||
handleSessionId(player); | ||
sessionIdRegex.test(player.config.session.id).should.be.true; | ||
}); | ||
|
||
it('should update the player session id', function () { | ||
player.config = {session: {id: "5cc03aa6-c58f-3220-b548-2a698aa54830:33e6d80e-63b3-108a-091d-ccc15998f85b"}}; | ||
handleSessionId(player); | ||
sessionIdRegex.test(player.config.session.id).should.be.true; | ||
(player.config.session.id.indexOf("5cc03aa6-c58f-3220-b548-2a698aa54830:") > -1).should.be.true; | ||
(player.config.session.id.indexOf("33e6d80e-63b3-108a-091d-ccc15998f85b") > -1).should.be.false; | ||
}); | ||
|
||
}); | ||
|
||
describe('updateSessionIdInUrl', function () { | ||
|
||
it('should add session id to URL as first param', function () { | ||
let source = {url: 'a/b/c/playmanifest/source'}; | ||
player.config = {session: {id: "5cc03aa6-c58f-3220-b548-2a698aa54830:33e6d80e-63b3-108a-091d-ccc15998f85b"}}; | ||
updateSessionIdInUrl(source, player.config.session.id); | ||
source.url.should.be.equal('a/b/c/playmanifest/source?playSessionId=' + player.config.session.id); | ||
}); | ||
|
||
it('should add session id to URL as second param', function () { | ||
let source = {url: 'a/b/c/playmanifest/source?a'}; | ||
player.config = {session: {id: "5cc03aa6-c58f-3220-b548-2a698aa54830:33e6d80e-63b3-108a-091d-ccc15998f85b"}}; | ||
updateSessionIdInUrl(source, player.config.session.id); | ||
source.url.should.be.equal('a/b/c/playmanifest/source?a&playSessionId=' + player.config.session.id); | ||
}); | ||
|
||
it('should update session id in URL as first param', function () { | ||
let source = {url: 'a/b/c/playmanifest/source?playSessionId=5cc03aa6-c58f-3220-b548-2a698aa54830:33e6d80e-63b3-108a-091d-ccc15998f85b'}; | ||
player.config = {session: {id: "5cc03aa6-c58f-3220-b548-2a698aa54830:33e6d80e-63b3-108a-091d-ccc15998f85b"}}; | ||
updateSessionIdInUrl(source, player.config.session.id); | ||
source.url.should.be.equal('a/b/c/playmanifest/source?playSessionId=' + player.config.session.id); | ||
}); | ||
|
||
it('should update session id in URL as second param', function () { | ||
let source = {url: 'a/b/c/playmanifest/source?a&playSessionId=5cc03aa6-c58f-3220-b548-2a698aa54830:b5391ed8-be5d-3a71-e157-f23a1b434121'}; | ||
player.config = {session: {id: "5cc03aa6-c58f-3220-b548-2a698aa54830:33e6d80e-63b3-108a-091d-ccc15998f85b"}}; | ||
updateSessionIdInUrl(source, player.config.session.id); | ||
source.url.should.be.equal('a/b/c/playmanifest/source?a&playSessionId=' + player.config.session.id); | ||
}); | ||
}); | ||
|
||
describe('addReferrer', function () { | ||
|
||
it('should add referrer as first param', function () { | ||
let source = {url: 'a/b/c/playmanifest/source'}; | ||
player.config = {session: {}}; | ||
addReferrer(source, player); | ||
source.url.should.be.equal('a/b/c/playmanifest/source?referrer=' + btoa(document.referrer)); | ||
}); | ||
|
||
it('should add referrer as second param', function () { | ||
let source = {url: 'a/b/c/playmanifest/source?a'}; | ||
player.config = {session: {}}; | ||
addReferrer(source, player); | ||
source.url.should.be.equal('a/b/c/playmanifest/source?a&referrer=' + btoa(document.referrer)); | ||
}); | ||
}); | ||
|
||
describe('addClientTag', function () { | ||
|
||
it('should add client tag as first param', function () { | ||
let source = {url: 'a/b/c/playmanifest/source'}; | ||
player.config = {session: {}}; | ||
addClientTag(source, player); | ||
source.url.should.be.equal('a/b/c/playmanifest/source?clientTag=html5:v' + VERSION); | ||
}); | ||
|
||
it('should add client tag as second param', function () { | ||
let source = {url: 'a/b/c/playmanifest/source?a'}; | ||
player.config = {session: {}}; | ||
addClientTag(source, player); | ||
source.url.should.be.equal('a/b/c/playmanifest/source?a&clientTag=html5:v' + VERSION); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.