From 7d3f9465e7f3fae9e767436063cd4ca3bb94d062 Mon Sep 17 00:00:00 2001 From: Adam Waldron Date: Mon, 13 May 2024 22:00:59 -0700 Subject: [PATCH] fix: tests and unsafe triggers on event bus --- src/eme.js | 6 +++--- src/fairplay.js | 10 ++++++---- src/ms-prefixed.js | 17 +++++++++-------- test/eme.test.js | 23 ++++++++++++++++++++--- test/fairplay.test.js | 10 ++++++++-- test/ms-prefixed.test.js | 17 ++++++++++++++++- 6 files changed, 62 insertions(+), 21 deletions(-) diff --git a/src/eme.js b/src/eme.js index 77f9341..117c1b5 100644 --- a/src/eme.js +++ b/src/eme.js @@ -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}); }; /** @@ -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); diff --git a/src/fairplay.js b/src/fairplay.js index 477f46f..240148b 100644 --- a/src/fairplay.js +++ b/src/fairplay.js @@ -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'; @@ -85,7 +86,7 @@ const addKey = ({video, contentId, initData, cert, options, getLicense, eventBus return; } - eventBus.trigger({ + safeTriggerOnEventBus(eventBus, { type: 'keysessioncreated', keySession }); @@ -93,13 +94,14 @@ const addKey = ({video, contentId, initData, cert, options, getLicense, eventBus 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 = { @@ -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 }); diff --git a/src/ms-prefixed.js b/src/ms-prefixed.js index b2d67c8..ab95bfd 100644 --- a/src/ms-prefixed.js +++ b/src/ms-prefixed.js @@ -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'; @@ -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' @@ -35,7 +36,7 @@ export const addKeyToSession = (options, session, event, eventBus, emeError) => session.update(key); - eventBus.trigger({ + safeTriggerOnEventBus(eventBus, { type: 'keysessionupdated', keySession: session }); @@ -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) { @@ -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' @@ -98,7 +99,7 @@ export const createSession = (video, initData, options, eventBus, emeError) => { throw error; } - eventBus.trigger({ + safeTriggerOnEventBus(eventBus, { type: 'keysessioncreated', keySession: session }); @@ -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 }); @@ -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, @@ -136,7 +137,7 @@ export const createSession = (video, initData, options, eventBus, emeError) => { }); session.addEventListener('mskeyadded', () => { - eventBus.trigger({ + safeTriggerOnEventBus(eventBus, { target: session, type: 'mskeyadded' }); diff --git a/test/eme.test.js b/test/eme.test.js index 8effbe8..1b407e5 100644 --- a/test/eme.test.js +++ b/test/eme.test.js @@ -525,6 +525,9 @@ QUnit.test('5 July 2016 lifecycle', function(assert) { if (name === 'keysessionupdated') { callCounts.keysessionUpdatedEvent++; } + }, + isDisposed: () => { + return false; } }; @@ -1302,6 +1305,9 @@ QUnit.test('makeNewRequest triggers keysessioncreated', function(assert) { assert.ok(true, 'got a keysessioncreated event'); done(); } + }, + isDisposed: () => { + return false; } } }); @@ -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'); @@ -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'); @@ -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, diff --git a/test/fairplay.test.js b/test/fairplay.test.js index 15a8eee..82cea5e 100644 --- a/test/fairplay.test.js +++ b/test/fairplay.test.js @@ -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; } }; @@ -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; } }; diff --git a/test/ms-prefixed.test.js b/test/ms-prefixed.test.js index 702eaf1..e6b45c5 100644 --- a/test/ms-prefixed.test.js +++ b/test/ms-prefixed.test.js @@ -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 @@ -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 @@ -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 @@ -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 @@ -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