Skip to content

Commit

Permalink
fix: Fix missing fields in message events
Browse files Browse the repository at this point in the history
Closes #27

Change-Id: I6691c8678555efb090266b7e58ef4c1856434c24
  • Loading branch information
joeyparrish committed Dec 14, 2021
1 parent 0825a51 commit 63e6d5f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
44 changes: 38 additions & 6 deletions eme-trace-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ function emeLogger(log) {
log.eventName == 'message') {
// You can't (and shouldn't, to avoid breaking the app) modify the event
// object here. So clone it, and replace the message field of that.
log.event = {
// The spread operator reads the fields and values (shallow clone) of the
// object named here.
...log.event,
// This overrides on of those fields with a new value.
log.event = cloneEvent(log.event, {
// This overrides the message field with a new value from the formatter.
message: formatEmeRequest(log.event.message),
};
});
} else if (log.type == TraceAnything.LogTypes.Method &&
log.className == 'MediaKeys' &&
log.methodName == 'setServerCertificate' &&
Expand All @@ -62,6 +59,41 @@ function emeLogger(log) {
window.postMessage({type: 'emeTraceLog', log: prepLogForMessage(log)}, '*');
}

/**
* Clone an event. The clone will have the same type and values, except for the
* values overridden by the overrides parameter.
*
* The spread operator and Object.assign can only read "own" properties of an
* object, so this custom cloning method must be used for Events instead.
*
* @param {!Event} original
* @param {Object=} overrides
* @return {!Event}
*/
function cloneEvent(original, overrides) {
const clone = {};

// When reading the original, we must use a for-in loop to see all fields.
// Static fields from Event and methods are skipped, but everything else is
// copied into a plain object.
for (const key in original) {
if (!(key in Event) && (typeof original[key] != 'function')) {
clone[key] = original[key];
}
}

// Overrides are applied all at once using Object.assign.
if (overrides) {
Object.assign(clone, overrides);
}

// Setting the prototype of the clone gives the clone the same type as the
// original.
Object.setPrototypeOf(clone, original.constructor.prototype);

return clone;
}

/**
* @param {BufferSource} response
* @return {(string|!Object|BufferSource)}
Expand Down
36 changes: 32 additions & 4 deletions spec/eme-trace-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

describe('EME tracing', () => {
beforeEach(() => {
spyOn(window, 'emeLogger').and.callFake((log) => {
spyOn(window, 'emeLogger').and.callThrough();

spyOn(window, 'postMessage').and.callFake((log) => {
expect(log.type).toEqual('emeTraceLog');

// Validate that the logs can always be serialized. We don't care about
// the output at this level.
delete log.instance;

try {
JSON.stringify(prepLogForMessage(log));
JSON.stringify(log);
} catch (exception) {
fail(exception);
}
Expand Down Expand Up @@ -262,6 +264,32 @@ describe('EME tracing', () => {
'value': session.keyStatuses,
}));
});

// Regression test for https://github.com/google/eme_logger/issues/27
it('Message events with all fields', async () => {
await session.generateRequest('cenc', initData);
// Wait for the message event to come through, with a generous timeout.
await delay(2);

// Cloning of Events happens at the emeLogger level, so we have to
// validate the results of that at the level of postMessage instead.
// Both message and messageType fields should be present.
const expectedMessage = jasmine.objectContaining({
log: jasmine.objectContaining({
type: TraceAnything.LogTypes.Event,
event: {
__type__: 'message Event',
__fields__: jasmine.objectContaining({
message: jasmine.objectContaining({
__type__: 'Uint8Array',
}),
messageType: 'license-request',
}),
},
}),
});
expect(postMessage).toHaveBeenCalledWith(expectedMessage, '*');
});
});

describe('logs HTML media elements', () => {
Expand Down

0 comments on commit 63e6d5f

Please sign in to comment.