1
1
/* eslint-disable no-undef */
2
- const resolveURL = require ( 'resolve-url' ) ;
3
2
const { log } = require ( '../utils/log' ) ;
4
3
const {
5
4
CREATE_FFMPEG_CORE_IS_NOT_DEFINED ,
@@ -19,50 +18,95 @@ const toBlobURL = async (url, mimeType) => {
19
18
return blobURL ;
20
19
} ;
21
20
22
- module . exports = async ( { corePath : _corePath } ) => {
23
- if ( typeof _corePath !== 'string' ) {
24
- throw Error ( 'corePath should be a string!' ) ;
25
- }
26
- const coreRemotePath = resolveURL ( _corePath ) ;
27
- const corePath = await toBlobURL (
28
- coreRemotePath ,
29
- 'application/javascript' ,
30
- ) ;
31
- const wasmPath = await toBlobURL (
32
- coreRemotePath . replace ( 'ffmpeg-core.js' , 'ffmpeg-core.wasm' ) ,
33
- 'application/wasm' ,
34
- ) ;
35
- const workerPath = await toBlobURL (
36
- coreRemotePath . replace ( 'ffmpeg-core.js' , 'ffmpeg-core.worker.js' ) ,
37
- 'application/javascript' ,
38
- ) ;
39
- if ( typeof createFFmpegCore === 'undefined' ) {
40
- return new Promise ( ( resolve ) => {
41
- const script = document . createElement ( 'script' ) ;
42
- const eventHandler = ( ) => {
43
- script . removeEventListener ( 'load' , eventHandler ) ;
21
+ if ( typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope ) {
22
+ // in Web Worker context
23
+ module . exports = async ( { corePath : _corePath } ) => {
24
+ if ( typeof _corePath !== 'string' ) {
25
+ throw Error ( 'corePath should be a string!' ) ;
26
+ }
27
+ const coreRemotePath = new URL ( _corePath , import . meta. url ) . href ;
28
+ const corePath = await toBlobURL (
29
+ coreRemotePath ,
30
+ 'application/javascript' ,
31
+ ) ;
32
+ const wasmPath = await toBlobURL (
33
+ coreRemotePath . replace ( 'ffmpeg-core.js' , 'ffmpeg-core.wasm' ) ,
34
+ 'application/wasm' ,
35
+ ) ;
36
+ const workerPath = await toBlobURL (
37
+ coreRemotePath . replace ( 'ffmpeg-core.js' , 'ffmpeg-core.worker.js' ) ,
38
+ 'application/javascript' ,
39
+ ) ;
40
+ if ( typeof createFFmpegCore === 'undefined' ) {
41
+ return new Promise ( ( resolve ) => {
44
42
if ( typeof createFFmpegCore === 'undefined' ) {
45
43
throw Error ( CREATE_FFMPEG_CORE_IS_NOT_DEFINED ( coreRemotePath ) ) ;
46
44
}
45
+ importScripts ( corePath ) ;
47
46
log ( 'info' , 'ffmpeg-core.js script loaded' ) ;
48
47
resolve ( {
49
48
createFFmpegCore,
50
49
corePath,
51
50
wasmPath,
52
51
workerPath,
53
52
} ) ;
54
- } ;
55
- script . src = corePath ;
56
- script . type = 'text/javascript' ;
57
- script . addEventListener ( 'load' , eventHandler ) ;
58
- document . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( script ) ;
53
+ } ) ;
54
+ }
55
+ log ( 'info' , 'ffmpeg-core.js script is loaded already' ) ;
56
+ return Promise . resolve ( {
57
+ createFFmpegCore,
58
+ corePath,
59
+ wasmPath,
60
+ workerPath,
59
61
} ) ;
60
- }
61
- log ( 'info' , 'ffmpeg-core.js script is loaded already' ) ;
62
- return Promise . resolve ( {
63
- createFFmpegCore,
64
- corePath,
65
- wasmPath,
66
- workerPath,
67
- } ) ;
68
- } ;
62
+ } ;
63
+ } else {
64
+ module . exports = async ( { corePath : _corePath } ) => {
65
+ if ( typeof _corePath !== 'string' ) {
66
+ throw Error ( 'corePath should be a string!' ) ;
67
+ }
68
+ const coreRemotePath = new URL ( _corePath , import . meta. url ) . href ;
69
+ const corePath = await toBlobURL (
70
+ coreRemotePath ,
71
+ 'application/javascript' ,
72
+ ) ;
73
+ const wasmPath = await toBlobURL (
74
+ coreRemotePath . replace ( 'ffmpeg-core.js' , 'ffmpeg-core.wasm' ) ,
75
+ 'application/wasm' ,
76
+ ) ;
77
+ const workerPath = await toBlobURL (
78
+ coreRemotePath . replace ( 'ffmpeg-core.js' , 'ffmpeg-core.worker.js' ) ,
79
+ 'application/javascript' ,
80
+ ) ;
81
+ if ( typeof createFFmpegCore === 'undefined' ) {
82
+ return new Promise ( ( resolve ) => {
83
+ const script = document . createElement ( 'script' ) ;
84
+ const eventHandler = ( ) => {
85
+ script . removeEventListener ( 'load' , eventHandler ) ;
86
+ if ( typeof createFFmpegCore === 'undefined' ) {
87
+ throw Error ( CREATE_FFMPEG_CORE_IS_NOT_DEFINED ( coreRemotePath ) ) ;
88
+ }
89
+ log ( 'info' , 'ffmpeg-core.js script loaded' ) ;
90
+ resolve ( {
91
+ createFFmpegCore,
92
+ corePath,
93
+ wasmPath,
94
+ workerPath,
95
+ } ) ;
96
+ } ;
97
+ script . src = corePath ;
98
+ script . type = 'text/javascript' ;
99
+ script . addEventListener ( 'load' , eventHandler ) ;
100
+ document . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( script ) ;
101
+ } ) ;
102
+ }
103
+ log ( 'info' , 'ffmpeg-core.js script is loaded already' ) ;
104
+ return Promise . resolve ( {
105
+ createFFmpegCore,
106
+ corePath,
107
+ wasmPath,
108
+ workerPath,
109
+ } ) ;
110
+ } ;
111
+ }
112
+
0 commit comments