File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -299,13 +299,10 @@ export class FFmpeg {
299299 public terminate = ( ) : void => this . #terminate( ERROR_TERMINATED ) ;
300300
301301 #terminate = ( reason : Error ) : void => {
302- const ids = Object . keys ( this . #rejects) ;
303302 // rejects all incomplete Promises.
304- for ( const id of ids ) {
305- this . #rejects[ id ] ( reason ) ;
306- delete this . #rejects[ id ] ;
307- delete this . #resolves[ id ] ;
308- }
303+ Object . values ( this . #rejects) . forEach ( ( reject ) => reject ( reason ) ) ;
304+ this . #rejects = { } ;
305+ this . #resolves = { } ;
309306
310307 if ( this . #worker) {
311308 this . #worker. terminate ( ) ;
Original file line number Diff line number Diff line change @@ -164,9 +164,7 @@ export type CallbackData =
164164 | FSNode [ ]
165165 | undefined ;
166166
167- export interface Callbacks {
168- [ id : number | string ] : ( data : CallbackData ) => void ;
169- }
167+ export type Callbacks = Record < number , ( data : CallbackData ) => void > ;
170168
171169export type LogEventCallback = ( event : LogEvent ) => void ;
172170export type ProgressEventCallback = ( event : ProgressEvent ) => void ;
Original file line number Diff line number Diff line change @@ -47,7 +47,9 @@ const load = async ({
4747 coreURL : _coreURL ,
4848 wasmURL : _wasmURL ,
4949} : FFMessageLoadConfig ) : Promise < IsFirst > => {
50- const first = ! ffmpeg ;
50+ // A second core would leak the first one's memory and threads (#494);
51+ // terminate() and load() again to switch cores.
52+ if ( ffmpeg ) return false ;
5153
5254 // ffmpeg-core runs FFmpeg's threads on a SharedArrayBuffer.
5355 if ( typeof SharedArrayBuffer === "undefined" ) throw ERROR_NOT_ISOLATED ;
@@ -75,9 +77,15 @@ const load = async ({
7577
7678 // the hash tells FFmpeg's threads where the wasm is (see _locateFile in bind.js)
7779 const mainScriptUrlOrBlob = `${ coreURL } #${ btoa ( JSON . stringify ( { wasmURL } ) ) } ` ;
78- ffmpeg = await ( self as WorkerGlobalScope ) . createFFmpegCore (
79- coreURL . startsWith ( "file:" ) ? { } : { mainScriptUrlOrBlob }
80- ) ;
80+ try {
81+ ffmpeg = await ( self as WorkerGlobalScope ) . createFFmpegCore (
82+ coreURL . startsWith ( "file:" ) ? { } : { mainScriptUrlOrBlob }
83+ ) ;
84+ } catch ( e ) {
85+ // e.g. an HTML 404 page or SPA fallback served instead of the wasm (#609)
86+ if ( String ( e ) . includes ( "magic word" ) ) throw new Error ( `${ wasmURL } is not a WebAssembly file (${ e } )` ) ;
87+ throw e ;
88+ }
8189 ffmpeg . setLogger ( ( data ) =>
8290 self . postMessage ( { type : FFMessageType . LOG , data } )
8391 ) ;
@@ -87,7 +95,7 @@ const load = async ({
8795 data,
8896 } )
8997 ) ;
90- return first ;
98+ return true ;
9199} ;
92100
93101const exec = ( { args, timeout = - 1 } : FFMessageExecData ) : ExitCode => {
Original file line number Diff line number Diff line change @@ -78,7 +78,8 @@ function setLogger(logger) {
7878 Module [ "logger" ] = logger ;
7979}
8080
81- function setTimeout ( timeout ) {
81+ // Not named setTimeout: that would shadow the global inside the core (#611).
82+ function setExecTimeout ( timeout ) {
8283 Module [ "timeout" ] = timeout ;
8384}
8485
@@ -129,7 +130,7 @@ Module["locateFile"] = _locateFile;
129130Module [ "exec" ] = exec ;
130131Module [ "ffprobe" ] = ffprobe ;
131132Module [ "setLogger" ] = setLogger ;
132- Module [ "setTimeout" ] = setTimeout ;
133+ Module [ "setTimeout" ] = setExecTimeout ;
133134Module [ "setProgress" ] = setProgress ;
134135Module [ "reset" ] = reset ;
135136Module [ "terminateThreads" ] = terminateThreads ;
Original file line number Diff line number Diff line change 44import { expect } from "vitest" ;
55import { test , coreURL } from "../../helpers/ffmpeg.js" ;
66
7- test . fails ( "load() again keeps the loaded core (#494) " , { timeout : 60000 } , async ( { ffmpeg } ) => {
7+ test ( "load() again keeps the loaded core" , { timeout : 60000 } , async ( { ffmpeg } ) => {
88 await ffmpeg . writeFile ( "/kept.txt" , "hello" ) ;
99 const isFirst = await ffmpeg . load ( { coreURL : coreURL ( ) } ) ;
1010 const kept = await ffmpeg . readFile ( "/kept.txt" , "utf8" ) ;
Original file line number Diff line number Diff line change 55import { expect } from "vitest" ;
66import { test , coreURL } from "../../helpers/ffmpeg.js" ;
77
8- test . fails ( "names the URL when the wasm file is not WebAssembly (#609) " , async ( { unloaded } ) => {
8+ test ( "names the URL when the wasm file is not WebAssembly" , async ( { unloaded } ) => {
99 const wasmURL = `${ location . origin } /tests/README.md` ;
1010 const loading = unloaded . load ( { coreURL : coreURL ( ) , wasmURL } ) ;
1111 await expect ( loading ) . rejects . toThrow ( wasmURL ) ;
You can’t perform that action at this time.
0 commit comments