-
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.
feat(kaltura player): export setup method (#2)
- Loading branch information
Showing
10 changed files
with
2,291 additions
and
1,118 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
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 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,63 @@ | ||
//@flow | ||
/** | ||
* @return {string} - GUID | ||
* @private | ||
*/ | ||
function _generateGUID(): string { | ||
let S4 = () => { | ||
return (((1+Math.random())*0x10000)|0).toString(16).substring(1); | ||
}; | ||
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()); | ||
} | ||
|
||
/** | ||
* @param {Object} selectedSource - selected source | ||
* @param {Player} player - player | ||
* @return {void} | ||
* @private | ||
*/ | ||
function _addSessionId(selectedSource: Object, player: Player): void { | ||
let delimiter = selectedSource.url.indexOf('?') === -1 ? '?' : '&'; | ||
let primaryGUID = _generateGUID(); | ||
let secondGUID = _generateGUID(); | ||
let sessionId = primaryGUID + ':' + secondGUID; | ||
selectedSource.url += delimiter + 'playSessionId=' + sessionId; | ||
player.sessionId = sessionId; | ||
} | ||
|
||
/** | ||
* @param {Object} selectedSource - selected source | ||
* @param {string} sessionId - session id | ||
* @param {Player} player - player | ||
* @return {void} | ||
* @private | ||
*/ | ||
function _replaceSecondGUID(selectedSource: Object, sessionId: string, player: Player): void { | ||
let secondGUIDRegex = /:((?:[a-z0-9]|-)*)/i; | ||
let secondGUID = secondGUIDRegex.exec(sessionId); | ||
if (secondGUID && secondGUID[1]) { | ||
let newSessionId = sessionId.replace(secondGUID[1], _generateGUID()); | ||
selectedSource.url = selectedSource.url.replace(sessionId, newSessionId); | ||
player.sessionId = newSessionId; | ||
} | ||
} | ||
|
||
/** | ||
* @param {Object} selectedSource - selected source | ||
* @param {Player} player - player | ||
* @return {void} | ||
* @public | ||
*/ | ||
function handleSessionId(selectedSource: Object = {}, player: Player): void { | ||
if (typeof selectedSource.url === 'string' && selectedSource.url.toLowerCase().indexOf('playmanifest/') !== -1) { | ||
let sessionIdRegex = /playSessionId=((?:[a-z0-9]|-|:)*)/i; | ||
let sessionId = sessionIdRegex.exec(selectedSource.url); | ||
if (sessionId && sessionId[1]) { | ||
_replaceSecondGUID(selectedSource, sessionId[1], player); | ||
} else { | ||
_addSessionId(selectedSource, player); | ||
} | ||
} | ||
} | ||
|
||
export default handleSessionId |
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,28 @@ | ||
//@flow | ||
import * as Playkit from 'playkit-js'; | ||
import OvpProvider from 'playkit-js-providers/dist/ovpProvider'; | ||
import handleSessionId from './session-id' | ||
|
||
/** | ||
* | ||
* @param {Object} config - contains partnerId and entryID | ||
* @return {Promise<*>} - player promise | ||
*/ | ||
export default function setup(config: Object): Promise<*> { | ||
let player = Playkit.playkit(); | ||
player.addEventListener(player.Event.SOURCE_SELECTED, (event) => { | ||
handleSessionId(event.payload.selectedSource, player); | ||
}); | ||
if (config) { | ||
let provider = new OvpProvider(config.partnerId); | ||
return provider.getConfig(config.entryID) | ||
.then(data => { | ||
player.configure(data); | ||
return Promise.resolve(player); | ||
}).catch(error => { | ||
return Promise.reject(error); | ||
}); | ||
} else { | ||
return Promise.resolve(player); | ||
} | ||
} |
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,53 @@ | ||
import handleSessionId from '../../src/session-id' | ||
|
||
describe('handleSessionId', function () { | ||
|
||
it('should add session id for playManifest source', function () { | ||
let selectedSource = {url: 'a/b/c/playmanifest/source'}; | ||
let player = {}; | ||
handleSessionId(selectedSource, player); | ||
selectedSource.url.should.be.equal('a/b/c/playmanifest/source?playSessionId=' + player.sessionId); | ||
}); | ||
|
||
it('should add session id for playManifest source with query param', function () { | ||
let selectedSource = {url: 'a/b/c/playmanifest/source?a'}; | ||
let player = {}; | ||
handleSessionId(selectedSource, player); | ||
selectedSource.url.should.be.equal('a/b/c/playmanifest/source?a&playSessionId=' + player.sessionId); | ||
}); | ||
|
||
it('should add session id for PLAYMANIFEST source', function () { | ||
let selectedSource = {url: 'a/b/c/PLAYMANIFEST/source'}; | ||
let player = {}; | ||
handleSessionId(selectedSource, player); | ||
selectedSource.url.should.be.equal('a/b/c/PLAYMANIFEST/source?playSessionId=' + player.sessionId); | ||
}); | ||
|
||
it('should add session id for PLAYMANIFEST source with query param', function () { | ||
let selectedSource = {url: 'a/b/c/PLAYMANIFEST/source?a'}; | ||
let player = {}; | ||
handleSessionId(selectedSource, player); | ||
selectedSource.url.should.be.equal('a/b/c/PLAYMANIFEST/source?a&playSessionId=' + player.sessionId); | ||
}); | ||
|
||
it('should not add session id for no playManifest source', function () { | ||
let selectedSource = {url: 'a/b/c'}; | ||
let player = {}; | ||
handleSessionId(selectedSource, player); | ||
selectedSource.url.should.be.equal('a/b/c'); | ||
}); | ||
|
||
it('should replace the second GUID for existing session id as first query param', function () { | ||
let selectedSource = {url: 'a/b/c/playmanifest/source?playSessionId=8a18888e-4110-d61b-5285-c601c51b70e3:b892a45b-23dc-7f3b-0ca1-5381a88e0c81&a'}; | ||
let player = {}; | ||
handleSessionId(selectedSource, player); | ||
selectedSource.url.should.be.equal('a/b/c/playmanifest/source?playSessionId=8a18888e-4110-d61b-5285-c601c51b70e3' + player.sessionId.substr(player.sessionId.indexOf(':')) + '&a'); | ||
}); | ||
|
||
it('should replace the second GUID for existing session id as second query param', function () { | ||
let selectedSource = {url: 'a/b/c/playmanifest/source?a&playSessionId=8a18888e-4110-d61b-5285-c601c51b70e3:b892a45b-23dc-7f3b-0ca1-5381a88e0c81'}; | ||
let player = {}; | ||
handleSessionId(selectedSource, player); | ||
selectedSource.url.should.be.equal('a/b/c/playmanifest/source?a&playSessionId=8a18888e-4110-d61b-5285-c601c51b70e3' + player.sessionId.substr(player.sessionId.indexOf(':'))); | ||
}); | ||
}); |
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,46 @@ | ||
import setup from '../../src/setup' | ||
import * as TestUtils from 'playkit-js/test/src/utils/test-utils' | ||
|
||
describe('setup', function () { | ||
|
||
let config, player; | ||
|
||
beforeEach(function () { | ||
config = { | ||
partnerId: 2196781, entryID: '1_h14v9eug' | ||
}; | ||
}); | ||
|
||
afterEach(function () { | ||
player.destroy(); | ||
TestUtils.removeVideoElementsFromTestPage(); | ||
}); | ||
|
||
it('should create a full player', function (done) { | ||
setup(config).then(p => { | ||
player = p; | ||
player.config.id.should.equal(config.entryID); | ||
player.config.session.partnerID.should.equal(config.partnerId); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should create an empty player', function (done) { | ||
setup().then(p => { | ||
player = p; | ||
(!player.config.id).should.be.true; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should decorate the selected source by session id', function (done) { | ||
setup(config).then(p => { | ||
player = p; | ||
player.load(); | ||
let sessionIdRegex = /playSessionId=((?:[a-z0-9]|-|:)*)/i; | ||
player.config.session.id.should.exist; | ||
sessionIdRegex.exec(player.src)[1].should.equal(player.config.session.id); | ||
done(); | ||
}); | ||
}); | ||
}); |