Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript

fix(tts): add request cancellation and fix proxy CORS for localhost

lvalicsbf4a99f26035
diff --git a/apps/web/functions/api/proxy/[[catchall]].ts b/apps/web/functions/api/proxy/[[catchall]].ts
index 66ae0182..03d12bfb 100644
--- a/apps/web/functions/api/proxy/[[catchall]].ts
+++ b/apps/web/functions/api/proxy/[[catchall]].ts
@@ -43,6 +43,8 @@ const SERVICE_CONFIG: Record<string, ServiceConfig> = {
const ALLOWED_ORIGINS = [
"https://openreel.pages.dev",
"https://openreel-preview.pages.dev",
+ "http://localhost:5173",
+ "http://localhost:4173",
];
const MAX_REQUEST_BODY_BYTES = 1_048_576; // 1 MB
diff --git a/apps/web/src/components/editor/inspector/hooks/useElevenLabsApi.ts b/apps/web/src/components/editor/inspector/hooks/useElevenLabsApi.ts
index 24986663..a770eaae 100644
--- a/apps/web/src/components/editor/inspector/hooks/useElevenLabsApi.ts
+++ b/apps/web/src/components/editor/inspector/hooks/useElevenLabsApi.ts
@@ -20,9 +20,9 @@ interface UseElevenLabsApiReturn {
allModels: ElevenLabsModel[];
isLoadingVoices: boolean;
isLoadingModels: boolean;
- generateWithElevenLabs: (text: string, voiceId: string) => Promise<Blob>;
- generateWithPiper: (text: string, voice: string, speed: number) => Promise<Blob>;
- enhanceViaLlm: (text: string) => Promise<string>;
+ generateWithElevenLabs: (text: string, voiceId: string, signal?: AbortSignal) => Promise<Blob>;
+ generateWithPiper: (text: string, voice: string, speed: number, signal?: AbortSignal) => Promise<Blob>;
+ enhanceViaLlm: (text: string, signal?: AbortSignal) => Promise<string>;
}
export function useElevenLabsApi(options: UseElevenLabsApiOptions): UseElevenLabsApiReturn {
@@ -149,13 +149,12 @@ export function useElevenLabsApi(options: UseElevenLabsApiOptions): UseElevenLab
}
}, [provider]);
- const generateWithPiper = useCallback(async (inputText: string, voice: string, spd: number): Promise<Blob> => {
- const controller = new AbortController();
+ const generateWithPiper = useCallback(async (inputText: string, voice: string, spd: number, signal?: AbortSignal): Promise<Blob> => {
const response = await fetch(`${OPENREEL_TTS_URL}/tts`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: inputText, voice, speed: spd }),
- signal: controller.signal,
+ signal,
});
if (!response.ok) {
@@ -171,7 +170,7 @@ export function useElevenLabsApi(options: UseElevenLabsApiOptions): UseElevenLab
return response.blob();
}, []);
- const generateWithElevenLabs = useCallback(async (inputText: string, voiceId: string): Promise<Blob> => {
+ const generateWithElevenLabs = useCallback(async (inputText: string, voiceId: string, signal?: AbortSignal): Promise<Blob> => {
if (!isSessionUnlocked()) {
throw new Error("Session locked. Unlock in Settings > API Keys first.");
}
@@ -193,6 +192,7 @@ export function useElevenLabsApi(options: UseElevenLabsApiOptions): UseElevenLab
model_id: elevenLabsModel,
voice_settings: { stability: 0.5, similarity_boost: 0.75 },
}),
+ signal,
},
);
@@ -207,7 +207,7 @@ export function useElevenLabsApi(options: UseElevenLabsApiOptions): UseElevenLab
return response.blob();
}, [elevenLabsModel]);
- const enhanceViaLlm = useCallback(async (inputText: string): Promise<string> => {
+ const enhanceViaLlm = useCallback(async (inputText: string, signal?: AbortSignal): Promise<string> => {
const llmProvider = defaultLlmProvider;
if (!isSessionUnlocked()) {
@@ -229,6 +229,7 @@ export function useElevenLabsApi(options: UseElevenLabsApiOptions): UseElevenLab
system: ENHANCE_SYSTEM_PROMPT,
messages: [{ role: "user", content: inputText }],
}),
+ signal,
});
if (!response.ok) {
@@ -254,6 +255,7 @@ export function useElevenLabsApi(options: UseElevenLabsApiOptions): UseElevenLab
],
max_tokens: 2048,
}),
+ signal,
});
if (!response.ok) {
diff --git a/apps/web/src/components/editor/inspector/hooks/useTtsActions.ts b/apps/web/src/components/editor/inspector/hooks/useTtsActions.ts
index ffb98840..2cc76a68 100644
--- a/apps/web/src/components/editor/inspector/hooks/useTtsActions.ts
+++ b/apps/web/src/components/editor/inspector/hooks/useTtsActions.ts
@@ -14,9 +14,9 @@ interface UseTtsActionsOptions {
enhancedPreview: string | null;
allVoices: ElevenLabsVoice[];
favoriteVoices: Array<{ voiceId: string; name: string; previewUrl?: string }>;
- generateWithElevenLabs: (text: string, voiceId: string) => Promise<Blob>;
- generateWithPiper: (text: string, voice: string, speed: number) => Promise<Blob>;
- enhanceViaLlm: (text: string) => Promise<string>;
+ generateWithElevenLabs: (text: string, voiceId: string, signal?: AbortSignal) => Promise<Blob>;
+ generateWithPiper: (text: string, voice: string, speed: number, signal?: AbortSignal) => Promise<Blob>;
+ enhanceViaLlm: (text: string, signal?: AbortSignal) => Promise<string>;
setText: (text: string) => void;
setError: (error: string | null) => void;
setEnhancedPreview: (preview: string | null) => void;
@@ -76,6 +76,7 @@ export function useTtsActions(options: UseTtsActionsOptions): UseTtsActionsRetur
const [successMsg, setSuccessMsg] = useState<string | null>(null);
const audioRef = useRef<HTMLAudioElement | null>(null);
+ const generateAbortRef = useRef<AbortController | null>(null);
const hasUnsavedAudio = generatedAudio !== null && !isAudioSaved;
@@ -98,12 +99,15 @@ export function useTtsActions(options: UseTtsActionsOptions): UseTtsActionsRetur
}
}, [audioUrl]);
- // Pause audio on unmount (but don't revoke URL — store owns it)
+ // Pause audio and abort in-flight requests on unmount
useEffect(() => {
return () => {
if (audioRef.current) {
audioRef.current.pause();
}
+ if (generateAbortRef.current) {
+ generateAbortRef.current.abort();
+ }
};
}, []);
@@ -132,13 +136,18 @@ export function useTtsActions(options: UseTtsActionsOptions): UseTtsActionsRetur
return;
}
+ generateAbortRef.current?.abort();
+ const controller = new AbortController();
+ generateAbortRef.current = controller;
+
setIsEnhancing(true);
setError(null);
try {
- const result = await enhanceViaLlm(text.trim());
+ const result = await enhanceViaLlm(text.trim(), controller.signal);
setEnhancedPreview(result);
} catch (err) {
+ if (err instanceof DOMException && err.name === "AbortError") return;
setError(err instanceof Error ? err.message : "Failed to enhance text");
} finally {
setIsEnhancing(false);
@@ -151,6 +160,10 @@ export function useTtsActions(options: UseTtsActionsOptions): UseTtsActionsRetur
return;
}
+ generateAbortRef.current?.abort();
+ const controller = new AbortController();
+ generateAbortRef.current = controller;
+
setIsGenerating(true);
setError(null);
@@ -158,8 +171,8 @@ export function useTtsActions(options: UseTtsActionsOptions): UseTtsActionsRetur
const finalText = (enhanceText && enhancedPreview) ? enhancedPreview : text.trim();
const blob = provider === "elevenlabs"
- ? await generateWithElevenLabs(finalText, selectedVoice)
- : await generateWithPiper(finalText, selectedVoice, speed);
+ ? await generateWithElevenLabs(finalText, selectedVoice, controller.signal)
+ : await generateWithPiper(finalText, selectedVoice, speed, controller.signal);
storeSetAudio(blob);
@@ -168,6 +181,7 @@ export function useTtsActions(options: UseTtsActionsOptions): UseTtsActionsRetur
if (url) audioRef.current.src = url;
}
} catch (err) {
+ if (err instanceof DOMException && err.name === "AbortError") return;
setError(err instanceof Error ? err.message : "Failed to generate speech");
} finally {
setIsGenerating(false);