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..dbc761f7498 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) as unknown as string) 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 {