From 20d60cf318f08ab7018ce9ec4c9aaf51e3346358 Mon Sep 17 00:00:00 2001 From: Florent Date: Wed, 31 Jul 2024 15:33:09 +0200 Subject: [PATCH] use function overloading for better types --- src/compat/eme/eme-api-implementation.ts | 9 ++++++--- src/compat/event_listeners.ts | 20 ++++++++++++++++++++ src/main_thread/decrypt/content_decryptor.ts | 3 +-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/compat/eme/eme-api-implementation.ts b/src/compat/eme/eme-api-implementation.ts index 0fc9e924f1..5a8c2833d4 100644 --- a/src/compat/eme/eme-api-implementation.ts +++ b/src/compat/eme/eme-api-implementation.ts @@ -20,7 +20,10 @@ import getMozMediaKeysCallbacks, { import getOldKitWebKitMediaKeyCallbacks, { isOldWebkitMediaElement, } from "./custom_media_keys/old_webkit_media_keys"; -import type { ICustomMediaKeys } from "./custom_media_keys/types"; +import type { + ICustomMediaEncryptedEvent, + ICustomMediaKeys, +} from "./custom_media_keys/types"; import getWebKitMediaKeysCallbacks from "./custom_media_keys/webkit_media_keys"; import { WebKitMediaKeysConstructor } from "./custom_media_keys/webkit_media_keys_constructor"; @@ -64,7 +67,7 @@ export interface IEmeApiImplementation { */ onEncrypted: ( target: IEventTargetLike, - listener: (evt: unknown) => void, + listener: (evt: ICustomMediaEncryptedEvent) => void, cancelSignal: CancellationSignal, ) => void; @@ -286,7 +289,7 @@ function createOnEncryptedForWebkit(): IEmeApiImplementation["onEncrypted"] { ); const onEncrypted = ( target: IEventTargetLike, - listener: (event?: Event) => void, + listener: (event: ICustomMediaEncryptedEvent) => void, cancelSignal: CancellationSignal, ) => { compatibleEventListener( diff --git a/src/compat/event_listeners.ts b/src/compat/event_listeners.ts index 073bd8545d..9500fb9a3c 100644 --- a/src/compat/event_listeners.ts +++ b/src/compat/event_listeners.ts @@ -30,6 +30,7 @@ import type { IEventTarget, IMediaElement, } from "./browser_compatibility_types"; +import type { ICustomMediaEncryptedEvent } from "./eme/custom_media_keys/types"; const BROWSER_PREFIXES = ["", "webkit", "moz", "ms"]; @@ -105,6 +106,16 @@ export type IEventTargetLike = HTMLElement | IEventEmitterLike | IEventEmitter, + prefixes?: string[], +): ( + element: IEventTargetLike, + listener: (event: ICustomMediaEncryptedEvent) => void, + cancelSignal: CancellationSignal, +) => void; + function createCompatibleEventListener( eventNames: string[], prefixes?: string[], @@ -112,6 +123,15 @@ function createCompatibleEventListener( element: IEventTargetLike, listener: (event?: Event) => void, cancelSignal: CancellationSignal, +) => void; + +function createCompatibleEventListener( + eventNames: string[] | Array<"needkey" | "encrypted">, + prefixes?: string[], +): ( + element: IEventTargetLike, + listener: (event?: Event | MediaEncryptedEvent) => void, + cancelSignal: CancellationSignal, ) => void { let mem: string | undefined; const prefixedEvents = eventPrefixed(eventNames, prefixes); diff --git a/src/main_thread/decrypt/content_decryptor.ts b/src/main_thread/decrypt/content_decryptor.ts index 53c034da06..07726b706a 100644 --- a/src/main_thread/decrypt/content_decryptor.ts +++ b/src/main_thread/decrypt/content_decryptor.ts @@ -17,7 +17,6 @@ import type { IMediaElement } from "../../compat/browser_compatibility_types"; import type { ICustomMediaKeys, ICustomMediaKeySystemAccess } from "../../compat/eme"; import eme, { getInitData } from "../../compat/eme"; -import type { ICustomMediaEncryptedEvent } from "../../compat/eme/custom_media_keys/types"; import config from "../../config"; import { EncryptedMediaError, OtherError } from "../../errors"; import log from "../../log"; @@ -178,7 +177,7 @@ export default class ContentDecryptor extends EventEmitter { log.debug("DRM: Encrypted event received from media element."); - const initData = getInitData(evt as ICustomMediaEncryptedEvent); + const initData = getInitData(evt); if (initData !== null) { this.onInitializationData(initData); }