From 13f2e56dc1f183e23ac95ae52b6fd885ba03a061 Mon Sep 17 00:00:00 2001 From: Lucas Segurado Date: Tue, 11 Mar 2025 13:57:07 +0100 Subject: [PATCH 1/2] Added support for trusted types --- packages/ffmpeg/package.json | 1 + packages/ffmpeg/src/classes.ts | 9 +++++---- packages/ffmpeg/src/types.ts | 5 +++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/ffmpeg/package.json b/packages/ffmpeg/package.json index bf68fe14988..dfc6cc9edd9 100644 --- a/packages/ffmpeg/package.json +++ b/packages/ffmpeg/package.json @@ -63,6 +63,7 @@ "webpack-cli": "^5.1.4" }, "dependencies": { + "@types/trusted-types": "^2.0.7", "@ffmpeg/types": "^0.12.4" } } diff --git a/packages/ffmpeg/src/classes.ts b/packages/ffmpeg/src/classes.ts index bec0354fb35..c02edd019ff 100644 --- a/packages/ffmpeg/src/classes.ts +++ b/packages/ffmpeg/src/classes.ts @@ -185,17 +185,18 @@ export class FFmpeg { * @returns `true` if ffmpeg core is loaded for the first time. */ public load = ( - { classWorkerURL, ...config }: FFMessageLoadConfig = {}, + { classWorkerURL, trustedTypePolicy, ...config }: FFMessageLoadConfig = { }, { signal }: FFMessageOptions = {} ): Promise => { + const createScriptURL = ((url: string) => (trustedTypePolicy ?? window.trustedTypes?.defaultPolicy)?.createScriptURL?.(url) ?? url) if (!this.#worker) { this.#worker = classWorkerURL ? - new Worker(new URL(classWorkerURL, import.meta.url), { + new Worker(createScriptURL(new URL(classWorkerURL, import.meta.url).toString()), { type: "module", }) : // We need to duplicated the code here to enable webpack // to bundle worekr.js here. - new Worker(new URL("./worker.js", import.meta.url), { + new Worker(createScriptURL(new URL("./worker.js", import.meta.url).toString()), { type: "module", }); this.#registerHandlers(); @@ -340,7 +341,7 @@ export class FFmpeg { ) as Promise; }; - public mount = (fsType: FFFSType, options: FFFSMountOptions, mountPoint: FFFSPath, ): Promise => { + public mount = (fsType: FFFSType, options: FFFSMountOptions, mountPoint: FFFSPath): Promise => { const trans: Transferable[] = []; return this.#send( { diff --git a/packages/ffmpeg/src/types.ts b/packages/ffmpeg/src/types.ts index 6082a6d908d..3fca59f6110 100644 --- a/packages/ffmpeg/src/types.ts +++ b/packages/ffmpeg/src/types.ts @@ -30,6 +30,11 @@ export interface FFMessageLoadConfig { * @defaultValue `./worker.js` */ classWorkerURL?: string; + + /** + * Trusted type policy to use on workers + */ + trustedTypePolicy?: TrustedTypePolicy } export interface FFMessageExecData { From afdc9e367e39aae697c2512fbba90781ebe2dd7e Mon Sep 17 00:00:00 2001 From: Lucas Segurado Date: Wed, 12 Mar 2025 21:21:01 +0100 Subject: [PATCH 2/2] Small type bugfix --- packages/ffmpeg/src/classes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ffmpeg/src/classes.ts b/packages/ffmpeg/src/classes.ts index c02edd019ff..dbc761f7498 100644 --- a/packages/ffmpeg/src/classes.ts +++ b/packages/ffmpeg/src/classes.ts @@ -188,7 +188,7 @@ export class FFmpeg { { classWorkerURL, trustedTypePolicy, ...config }: FFMessageLoadConfig = { }, { signal }: FFMessageOptions = {} ): Promise => { - const createScriptURL = ((url: string) => (trustedTypePolicy ?? window.trustedTypes?.defaultPolicy)?.createScriptURL?.(url) ?? url) + const createScriptURL = ((url: string) => ((trustedTypePolicy ?? window.trustedTypes?.defaultPolicy)?.createScriptURL?.(url) ?? url) as unknown as string) if (!this.#worker) { this.#worker = classWorkerURL ? new Worker(createScriptURL(new URL(classWorkerURL, import.meta.url).toString()), {