Skip to content

Commit

Permalink
fix: tests and unsafe triggers on event bus
Browse files Browse the repository at this point in the history
  • Loading branch information
adrums86 committed May 14, 2024
1 parent 24c1f86 commit 7d3f946
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/eme.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const isFairplayKeySystem = (str) => str.startsWith('com.apple.fps');
* @param {Component} eventBus
* @param {...} args
*/
const safeTriggerOnEventBus = (eventBus, ...args) => {
export const safeTriggerOnEventBus = (eventBus, args) => {
if (eventBus.isDisposed()) {
return;
}

eventBus.trigger(...args);
eventBus.trigger({...args});
};

/**
Expand Down Expand Up @@ -438,7 +438,7 @@ const promisifyGetLicense = (keySystem, getLicenseFn, eventBus) => {
return new Promise((resolve, reject) => {
const callback = function(err, license) {
if (eventBus) {
safeTriggerOnEventBus(eventBus, 'licenserequestattempted');
safeTriggerOnEventBus(eventBus, { type: 'licenserequestattempted' });
}
if (err) {
reject(err);
Expand Down
10 changes: 6 additions & 4 deletions src/fairplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import window from 'global/window';
import {stringToUint16Array, uint16ArrayToString, getHostnameFromUri, mergeAndRemoveNull} from './utils';
import {httpResponseHandler} from './http-handler.js';
import EmeError from './consts/errors';
import { safeTriggerOnEventBus } from './eme.js';

export const LEGACY_FAIRPLAY_KEY_SYSTEM = 'com.apple.fps.1_0';

Expand Down Expand Up @@ -85,21 +86,22 @@ const addKey = ({video, contentId, initData, cert, options, getLicense, eventBus
return;
}

eventBus.trigger({
safeTriggerOnEventBus(eventBus, {
type: 'keysessioncreated',
keySession
});

keySession.contentId = contentId;

keySession.addEventListener('webkitkeymessage', (event) => {
eventBus.trigger({
safeTriggerOnEventBus(eventBus, {
type: 'keymessage',
messageEvent: event
});
getLicense(options, contentId, event.message, (err, license) => {
if (eventBus) {
eventBus.trigger('licenserequestattempted');

safeTriggerOnEventBus(eventBus, { type: 'licenserequestattempted' });
}
if (err) {
const metadata = {
Expand All @@ -114,7 +116,7 @@ const addKey = ({video, contentId, initData, cert, options, getLicense, eventBus

keySession.update(new Uint8Array(license));

eventBus.trigger({
safeTriggerOnEventBus(eventBus, {
type: 'keysessionupdated',
keySession
});
Expand Down
17 changes: 9 additions & 8 deletions src/ms-prefixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import window from 'global/window';
import { requestPlayreadyLicense } from './playready';
import { getMediaKeySystemConfigurations } from './utils';
import EmeError from './consts/errors';
import { safeTriggerOnEventBus } from './eme';

export const PLAYREADY_KEY_SYSTEM = 'com.microsoft.playready';

Expand All @@ -25,7 +26,7 @@ export const addKeyToSession = (options, session, event, eventBus, emeError) =>
};

emeError(err, metadata);
eventBus.trigger({
safeTriggerOnEventBus(eventBus, {
message: 'Unable to get key: ' + err,
target: session,
type: 'mskeyerror'
Expand All @@ -35,7 +36,7 @@ export const addKeyToSession = (options, session, event, eventBus, emeError) =>

session.update(key);

eventBus.trigger({
safeTriggerOnEventBus(eventBus, {
type: 'keysessionupdated',
keySession: session
});
Expand All @@ -55,7 +56,7 @@ export const addKeyToSession = (options, session, event, eventBus, emeError) =>

const callback = (err, responseBody) => {
if (eventBus) {
eventBus.trigger('licenserequestattempted');
safeTriggerOnEventBus(eventBus, { type: 'licenserequestattempted' });
}

if (err) {
Expand All @@ -65,7 +66,7 @@ export const addKeyToSession = (options, session, event, eventBus, emeError) =>
};

emeError(err, metadata);
eventBus.trigger({
safeTriggerOnEventBus(eventBus, {
message: 'Unable to request key from url: ' + playreadyOptions.url,
target: session,
type: 'mskeyerror'
Expand Down Expand Up @@ -98,7 +99,7 @@ export const createSession = (video, initData, options, eventBus, emeError) => {
throw error;
}

eventBus.trigger({
safeTriggerOnEventBus(eventBus, {
type: 'keysessioncreated',
keySession: session
});
Expand All @@ -113,7 +114,7 @@ export const createSession = (video, initData, options, eventBus, emeError) => {
// eslint-disable-next-line max-len
// @see [PlayReady License Acquisition]{@link https://msdn.microsoft.com/en-us/library/dn468979.aspx}
session.addEventListener('mskeymessage', (event) => {
eventBus.trigger({
safeTriggerOnEventBus(eventBus, {
type: 'keymessage',
messageEvent: event
});
Expand All @@ -127,7 +128,7 @@ export const createSession = (video, initData, options, eventBus, emeError) => {
};

emeError(session.error, metadata);
eventBus.trigger({
safeTriggerOnEventBus(eventBus, {
message: 'Unexpected key error from key session with ' +
`code: ${session.error.code} and systemCode: ${session.error.systemCode}`,
target: session,
Expand All @@ -136,7 +137,7 @@ export const createSession = (video, initData, options, eventBus, emeError) => {
});

session.addEventListener('mskeyadded', () => {
eventBus.trigger({
safeTriggerOnEventBus(eventBus, {
target: session,
type: 'mskeyadded'
});
Expand Down
23 changes: 20 additions & 3 deletions test/eme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,9 @@ QUnit.test('5 July 2016 lifecycle', function(assert) {
if (name === 'keysessionupdated') {
callCounts.keysessionUpdatedEvent++;
}
},
isDisposed: () => {
return false;
}
};

Expand Down Expand Up @@ -1302,6 +1305,9 @@ QUnit.test('makeNewRequest triggers keysessioncreated', function(assert) {
assert.ok(true, 'got a keysessioncreated event');
done();
}
},
isDisposed: () => {
return false;
}
}
});
Expand Down Expand Up @@ -1348,7 +1354,10 @@ QUnit.test('emeError is called when keySession.close fails', function(assert) {
createSession: () => mockSession
},
eventBus: {
trigger: () => {}
trigger: () => {},
isDisposed: () => {
return false;
}
},
emeError: (error, metadata) => {
assert.equal(error, expectedErrorMessage, 'expected eme error message');
Expand All @@ -1372,7 +1381,10 @@ QUnit.test('emeError called when session.generateRequest fails', function(assert
createSession: () => mockSession
},
eventBus: {
trigger: () => {}
trigger: () => {},
isDisposed: () => {
return false;
}
},
emeError: (error, metadata) => {
assert.equal(error, expectedErrorMessage, 'expected eme error message');
Expand Down Expand Up @@ -1472,7 +1484,12 @@ QUnit.test('addPendingSessions reuses saved options', function(assert) {
options,
getLicense,
removeSession: () => '',
eventBus: { trigger: () => {} }
eventBus: {
trigger: () => {},
isDisposed: () => {
return false;
}
}
}];
const video = {
pendingSessionData,
Expand Down
10 changes: 8 additions & 2 deletions test/fairplay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ QUnit.test('lifecycle', function(assert) {
};

const eventBus = {
trigger: (name) => {
if (name === 'licenserequestattempted') {
trigger: (event) => {
if (event.type === 'licenserequestattempted') {
callCounts.licenseRequestAttempts++;
}
},
isDisposed: () => {
return false;
}
};

Expand Down Expand Up @@ -356,6 +359,9 @@ QUnit.test('keysessioncreated fired on key session created', function(assert) {
assert.deepEqual(event.keySession, { addEventListener }, 'keySession payload passed with event');
done();
}
},
isDisposed: () => {
return false;
}
};

Expand Down
17 changes: 16 additions & 1 deletion test/ms-prefixed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ QUnit.test('throws error on keysession mskeyerror event', function(assert) {
eventBus: {
trigger: (event) => {
errorMessage = typeof event === 'string' ? event : event.message;
},
isDisposed: () => {
return false;
}
},
emeError
Expand Down Expand Up @@ -188,6 +191,9 @@ QUnit.test('calls getKey when provided on key message', function(assert) {
eventBus: {
trigger: (event) => {
errorMessage = typeof event === 'string' ? event : event.message;
},
isDisposed: () => {
return false;
}
},
emeError
Expand Down Expand Up @@ -254,6 +260,9 @@ QUnit.test('makes request when nothing provided on key message', function(assert
if (typeof event === 'object' && event.type === 'mskeyerror') {
errorMessage = event.message;
}
},
isDisposed: () => {
return false;
}
},
emeError
Expand Down Expand Up @@ -389,6 +398,9 @@ QUnit.test('makes request with provided url string on key message', function(ass
if (typeof event === 'object' && event.type === 'mskeyerror') {
errorMessage = event.message;
}
},
isDisposed: () => {
return false;
}
},
emeError
Expand Down Expand Up @@ -480,11 +492,14 @@ QUnit.test('makes request with provided url on key message', function(assert) {
},
eventBus: {
trigger: (event) => {
if (event === 'licenserequestattempted') {
if (event.type === 'licenserequestattempted') {
callCounts.licenseRequestAttempts++;
} else if (typeof event === 'object' && event.type === 'mskeyerror') {
errorMessage = event.message;
}
},
isDisposed: () => {
return false;
}
},
emeError
Expand Down

0 comments on commit 7d3f946

Please sign in to comment.