diff --git a/src/browser/defaultOptions.js b/src/browser/defaultOptions.js index 491a8a8..ed51481 100644 --- a/src/browser/defaultOptions.js +++ b/src/browser/defaultOptions.js @@ -1,4 +1,3 @@ -const resolveURL = require('resolve-url'); const { devDependencies } = require('../../package.json'); /* @@ -6,6 +5,6 @@ const { devDependencies } = require('../../package.json'); */ module.exports = { corePath: typeof process !== 'undefined' && process.env.NODE_ENV === 'development' - ? resolveURL('/node_modules/@ffmpeg/core/dist/ffmpeg-core.js') + ? new URL('/node_modules/@ffmpeg/core/dist/ffmpeg-core.js', import.meta.url).href : `https://unpkg.com/@ffmpeg/core@${devDependencies['@ffmpeg/core'].substring(1)}/dist/ffmpeg-core.js`, }; diff --git a/src/browser/fetchFile.js b/src/browser/fetchFile.js index 74dcb7d..f977c6c 100644 --- a/src/browser/fetchFile.js +++ b/src/browser/fetchFile.js @@ -1,5 +1,3 @@ -const resolveURL = require('resolve-url'); - const readFromBlobOrFile = (blob) => ( new Promise((resolve, reject) => { const fileReader = new FileReader(); @@ -27,7 +25,7 @@ module.exports = async (_data) => { .map((c) => c.charCodeAt(0)); /* From remote server/URL */ } else { - const res = await fetch(resolveURL(_data)); + const res = await fetch(new URL(_data, import.meta.url).href); data = await res.arrayBuffer(); } /* From Blob or File */ diff --git a/src/browser/getCreateFFmpegCore.js b/src/browser/getCreateFFmpegCore.js index f31167e..762bc0d 100644 --- a/src/browser/getCreateFFmpegCore.js +++ b/src/browser/getCreateFFmpegCore.js @@ -1,5 +1,4 @@ /* eslint-disable no-undef */ -const resolveURL = require('resolve-url'); const { log } = require('../utils/log'); const { CREATE_FFMPEG_CORE_IS_NOT_DEFINED, @@ -19,31 +18,31 @@ const toBlobURL = async (url, mimeType) => { return blobURL; }; -module.exports = async ({ corePath: _corePath }) => { - if (typeof _corePath !== 'string') { - throw Error('corePath should be a string!'); - } - const coreRemotePath = resolveURL(_corePath); - const corePath = await toBlobURL( - coreRemotePath, - 'application/javascript', - ); - const wasmPath = await toBlobURL( - coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.wasm'), - 'application/wasm', - ); - const workerPath = await toBlobURL( - coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.worker.js'), - 'application/javascript', - ); - if (typeof createFFmpegCore === 'undefined') { - return new Promise((resolve) => { - const script = document.createElement('script'); - const eventHandler = () => { - script.removeEventListener('load', eventHandler); +if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) { + // in Web Worker context + module.exports = async ({ corePath: _corePath }) => { + if (typeof _corePath !== 'string') { + throw Error('corePath should be a string!'); + } + const coreRemotePath = new URL(_corePath, import.meta.url).href; + const corePath = await toBlobURL( + coreRemotePath, + 'application/javascript', + ); + const wasmPath = await toBlobURL( + coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.wasm'), + 'application/wasm', + ); + const workerPath = await toBlobURL( + coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.worker.js'), + 'application/javascript', + ); + if (typeof createFFmpegCore === 'undefined') { + return new Promise((resolve) => { if (typeof createFFmpegCore === 'undefined') { throw Error(CREATE_FFMPEG_CORE_IS_NOT_DEFINED(coreRemotePath)); } + importScripts(corePath); log('info', 'ffmpeg-core.js script loaded'); resolve({ createFFmpegCore, @@ -51,18 +50,63 @@ module.exports = async ({ corePath: _corePath }) => { wasmPath, workerPath, }); - }; - script.src = corePath; - script.type = 'text/javascript'; - script.addEventListener('load', eventHandler); - document.getElementsByTagName('head')[0].appendChild(script); + }); + } + log('info', 'ffmpeg-core.js script is loaded already'); + return Promise.resolve({ + createFFmpegCore, + corePath, + wasmPath, + workerPath, }); - } - log('info', 'ffmpeg-core.js script is loaded already'); - return Promise.resolve({ - createFFmpegCore, - corePath, - wasmPath, - workerPath, - }); -}; + }; +} else { + module.exports = async ({ corePath: _corePath }) => { + if (typeof _corePath !== 'string') { + throw Error('corePath should be a string!'); + } + const coreRemotePath = new URL(_corePath, import.meta.url).href; + const corePath = await toBlobURL( + coreRemotePath, + 'application/javascript', + ); + const wasmPath = await toBlobURL( + coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.wasm'), + 'application/wasm', + ); + const workerPath = await toBlobURL( + coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.worker.js'), + 'application/javascript', + ); + if (typeof createFFmpegCore === 'undefined') { + return new Promise((resolve) => { + const script = document.createElement('script'); + const eventHandler = () => { + script.removeEventListener('load', eventHandler); + if (typeof createFFmpegCore === 'undefined') { + throw Error(CREATE_FFMPEG_CORE_IS_NOT_DEFINED(coreRemotePath)); + } + log('info', 'ffmpeg-core.js script loaded'); + resolve({ + createFFmpegCore, + corePath, + wasmPath, + workerPath, + }); + }; + script.src = corePath; + script.type = 'text/javascript'; + script.addEventListener('load', eventHandler); + document.getElementsByTagName('head')[0].appendChild(script); + }); + } + log('info', 'ffmpeg-core.js script is loaded already'); + return Promise.resolve({ + createFFmpegCore, + corePath, + wasmPath, + workerPath, + }); + }; +} +