Removed references to resolveURL

This commit is contained in:
Nathan Johnson 2022-04-04 21:37:57 -05:00
parent 8d45d585b2
commit 6e99e5f96f
3 changed files with 83 additions and 42 deletions

View File

@ -1,4 +1,3 @@
const resolveURL = require('resolve-url');
const { devDependencies } = require('../../package.json'); const { devDependencies } = require('../../package.json');
/* /*
@ -6,6 +5,6 @@ const { devDependencies } = require('../../package.json');
*/ */
module.exports = { module.exports = {
corePath: typeof process !== 'undefined' && process.env.NODE_ENV === 'development' 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`, : `https://unpkg.com/@ffmpeg/core@${devDependencies['@ffmpeg/core'].substring(1)}/dist/ffmpeg-core.js`,
}; };

View File

@ -1,5 +1,3 @@
const resolveURL = require('resolve-url');
const readFromBlobOrFile = (blob) => ( const readFromBlobOrFile = (blob) => (
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const fileReader = new FileReader(); const fileReader = new FileReader();
@ -27,7 +25,7 @@ module.exports = async (_data) => {
.map((c) => c.charCodeAt(0)); .map((c) => c.charCodeAt(0));
/* From remote server/URL */ /* From remote server/URL */
} else { } else {
const res = await fetch(resolveURL(_data)); const res = await fetch(new URL(_data, import.meta.url).href);
data = await res.arrayBuffer(); data = await res.arrayBuffer();
} }
/* From Blob or File */ /* From Blob or File */

View File

@ -1,5 +1,4 @@
/* eslint-disable no-undef */ /* eslint-disable no-undef */
const resolveURL = require('resolve-url');
const { log } = require('../utils/log'); const { log } = require('../utils/log');
const { const {
CREATE_FFMPEG_CORE_IS_NOT_DEFINED, CREATE_FFMPEG_CORE_IS_NOT_DEFINED,
@ -19,31 +18,31 @@ const toBlobURL = async (url, mimeType) => {
return blobURL; return blobURL;
}; };
module.exports = async ({ corePath: _corePath }) => { if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
if (typeof _corePath !== 'string') { // in Web Worker context
throw Error('corePath should be a string!'); module.exports = async ({ corePath: _corePath }) => {
} if (typeof _corePath !== 'string') {
const coreRemotePath = resolveURL(_corePath); throw Error('corePath should be a string!');
const corePath = await toBlobURL( }
coreRemotePath, const coreRemotePath = new URL(_corePath, import.meta.url).href;
'application/javascript', const corePath = await toBlobURL(
); coreRemotePath,
const wasmPath = await toBlobURL( 'application/javascript',
coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.wasm'), );
'application/wasm', const wasmPath = await toBlobURL(
); coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.wasm'),
const workerPath = await toBlobURL( 'application/wasm',
coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.worker.js'), );
'application/javascript', const workerPath = await toBlobURL(
); coreRemotePath.replace('ffmpeg-core.js', 'ffmpeg-core.worker.js'),
if (typeof createFFmpegCore === 'undefined') { 'application/javascript',
return new Promise((resolve) => { );
const script = document.createElement('script'); if (typeof createFFmpegCore === 'undefined') {
const eventHandler = () => { return new Promise((resolve) => {
script.removeEventListener('load', eventHandler);
if (typeof createFFmpegCore === 'undefined') { if (typeof createFFmpegCore === 'undefined') {
throw Error(CREATE_FFMPEG_CORE_IS_NOT_DEFINED(coreRemotePath)); throw Error(CREATE_FFMPEG_CORE_IS_NOT_DEFINED(coreRemotePath));
} }
importScripts(corePath);
log('info', 'ffmpeg-core.js script loaded'); log('info', 'ffmpeg-core.js script loaded');
resolve({ resolve({
createFFmpegCore, createFFmpegCore,
@ -51,18 +50,63 @@ module.exports = async ({ corePath: _corePath }) => {
wasmPath, wasmPath,
workerPath, workerPath,
}); });
}; });
script.src = corePath; }
script.type = 'text/javascript'; log('info', 'ffmpeg-core.js script is loaded already');
script.addEventListener('load', eventHandler); return Promise.resolve({
document.getElementsByTagName('head')[0].appendChild(script); createFFmpegCore,
corePath,
wasmPath,
workerPath,
}); });
} };
log('info', 'ffmpeg-core.js script is loaded already'); } else {
return Promise.resolve({ module.exports = async ({ corePath: _corePath }) => {
createFFmpegCore, if (typeof _corePath !== 'string') {
corePath, throw Error('corePath should be a string!');
wasmPath, }
workerPath, 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,
});
};
}