fix(downloader): code splitting (happy-dom)

This commit is contained in:
JellyBrick
2025-07-13 17:04:38 +09:00
parent 33a09cc8e1
commit 4dad68d255

View File

@ -7,11 +7,9 @@ import { Innertube, UniversalCache, Utils, YTNodes } from 'youtubei.js';
import is from 'electron-is'; import is from 'electron-is';
import filenamify from 'filenamify'; import filenamify from 'filenamify';
import { Mutex } from 'async-mutex'; import { Mutex } from 'async-mutex';
import { createFFmpeg } from '@ffmpeg.wasm/main';
import NodeID3, { TagConstants } from 'node-id3'; import NodeID3, { TagConstants } from 'node-id3';
import { Window } from 'happy-dom';
import { BG, type BgConfig } from 'bgutils-js'; import { BG, type BgConfig } from 'bgutils-js';
import { lazy } from 'lazy-var';
import { import {
cropMaxWidth, cropMaxWidth,
@ -44,11 +42,13 @@ import type {
type CustomSongInfo = SongInfo & { trackId?: string }; type CustomSongInfo = SongInfo & { trackId?: string };
const ffmpeg = createFFmpeg({ const ffmpeg = lazy(async () =>
log: false, (await import('@ffmpeg.wasm/main')).createFFmpeg({
logger() {}, // Console.log, log: false,
progress() {}, // Console.log, logger() {}, // Console.log,
}); progress() {}, // Console.log,
}),
);
const ffmpegMutex = new Mutex(); const ffmpegMutex = new Mutex();
let yt: Innertube; let yt: Innertube;
@ -142,7 +142,7 @@ export const onMainLoad = async ({
try { try {
const [width, height] = win.getSize(); const [width, height] = win.getSize();
// emulate jsdom using linkedom // emulate jsdom using linkedom
const window = new Window({ const window = new (await import('happy-dom')).Window({
width, width,
height, height,
console, console,
@ -505,12 +505,13 @@ async function iterableStreamToProcessedUint8Array(
return await ffmpegMutex.runExclusive(async () => { return await ffmpegMutex.runExclusive(async () => {
try { try {
if (!ffmpeg.isLoaded()) { const ffmpegInstance = await ffmpeg.get();
await ffmpeg.load(); if (!ffmpegInstance.isLoaded()) {
await ffmpegInstance.load();
} }
sendFeedback(t('plugins.downloader.backend.feedback.preparing-file')); sendFeedback(t('plugins.downloader.backend.feedback.preparing-file'));
ffmpeg.FS( ffmpegInstance.FS(
'writeFile', 'writeFile',
safeVideoName, safeVideoName,
Buffer.concat( Buffer.concat(
@ -525,7 +526,7 @@ async function iterableStreamToProcessedUint8Array(
sendFeedback(t('plugins.downloader.backend.feedback.converting')); sendFeedback(t('plugins.downloader.backend.feedback.converting'));
ffmpeg.setProgress(({ ratio }) => { ffmpegInstance.setProgress(({ ratio }) => {
sendFeedback( sendFeedback(
t('plugins.downloader.backend.feedback.conversion-progress', { t('plugins.downloader.backend.feedback.conversion-progress', {
percent: Math.floor(ratio * 100), percent: Math.floor(ratio * 100),
@ -537,7 +538,7 @@ async function iterableStreamToProcessedUint8Array(
const safeVideoNameWithExtension = `${safeVideoName}.${extension}`; const safeVideoNameWithExtension = `${safeVideoName}.${extension}`;
try { try {
await ffmpeg.run( await ffmpegInstance.run(
'-i', '-i',
safeVideoName, safeVideoName,
...presetFfmpegArgs, ...presetFfmpegArgs,
@ -545,15 +546,15 @@ async function iterableStreamToProcessedUint8Array(
safeVideoNameWithExtension, safeVideoNameWithExtension,
); );
} finally { } finally {
ffmpeg.FS('unlink', safeVideoName); ffmpegInstance.FS('unlink', safeVideoName);
} }
sendFeedback(t('plugins.downloader.backend.feedback.saving')); sendFeedback(t('plugins.downloader.backend.feedback.saving'));
try { try {
return ffmpeg.FS('readFile', safeVideoNameWithExtension); return ffmpegInstance.FS('readFile', safeVideoNameWithExtension);
} finally { } finally {
ffmpeg.FS('unlink', safeVideoNameWithExtension); ffmpegInstance.FS('unlink', safeVideoNameWithExtension);
} }
} catch (error: unknown) { } catch (error: unknown) {
sendError(error as Error, safeVideoName); sendError(error as Error, safeVideoName);