Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Fix DataCloneError in failed-upload persistence by stripping reactive proxies from tags/asrOptions before IndexedDB write
Murtaza Nasirdfc25dc70deb
diff --git a/static/js/modules/db/failed-uploads.js b/static/js/modules/db/failed-uploads.jsindex 76a33856..b40d188d 100644--- a/static/js/modules/db/failed-uploads.js+++ b/static/js/modules/db/failed-uploads.js@@ -49,6 +49,20 @@ export const initDB = () => {/*** Store a failed upload for later retry*/+// IndexedDB's structured clone cannot serialize Vue reactive proxies (tags /+// asrOptions arrive as reactive state), so objectStore.add() throws a+// DataCloneError ("Proxy object could not be cloned") and the whole record is+// lost to the download fallback. Round-trip through JSON to get plain,+// clone-safe values. fileData (an ArrayBuffer) is already cloneable as-is.+const toCloneable = (value, fallback) => {+ if (value == null) return fallback;+ try {+ return JSON.parse(JSON.stringify(value));+ } catch (_) {+ return fallback;+ }+};+export const storeFailedUpload = async (uploadData) => {try {const db = await initDB();@@ -58,9 +72,9 @@ export const storeFailedUpload = async (uploadData) => {clientId: uploadData.clientId || `client-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`,fileName: uploadData.file?.name || uploadData.fileName || 'unknown',fileSize: uploadData.file?.size || uploadData.fileSize || 0,- notes: uploadData.notes || '',- tags: uploadData.tags || [],- asrOptions: uploadData.asrOptions || {},+ notes: typeof uploadData.notes === 'string' ? uploadData.notes : toCloneable(uploadData.notes, ''),+ tags: toCloneable(uploadData.tags, []),+ asrOptions: toCloneable(uploadData.asrOptions, {}),retryCount: uploadData.retryCount || 0,lastError: uploadData.error || '',fileData: uploadData.fileData || null, // ArrayBuffer of filediff --git a/static/js/modules/db/failed-uploads.test.js b/static/js/modules/db/failed-uploads.test.jsindex 9504634e..f1281495 100644--- a/static/js/modules/db/failed-uploads.test.js+++ b/static/js/modules/db/failed-uploads.test.js@@ -228,3 +228,72 @@ describe('IndexedDB transaction lifetime', () => {expect(store.has(999)).toBe(false);});});++describe('storeFailedUpload structured-clone safety (Proxy metadata)', () => {+ // A mock whose add() runs structuredClone(value) first — exactly what a real+ // browser does. The previous mocks skipped this, which is why the+ // "Proxy object could not be cloned" DataCloneError slipped through: tags /+ // asrOptions arrive as Vue reactive proxies and IndexedDB rejects them.+ let store;+ let originalIndexedDB;+ let storeFailedUpload;++ beforeEach(async () => {+ vi.resetModules();+ store = new Map();+ let nextId = 1;+ const makeRequest = (op) => {+ const request = {};+ queueMicrotask(() => {+ try { request.result = op(); request.onsuccess?.(); }+ catch (err) { request.error = err; request.onerror?.(); }+ });+ return request;+ };+ const db = {+ transaction() {+ const objectStore = {+ add: (value) => makeRequest(() => {+ // Throws DataCloneError on a Proxy, like a real engine.+ const cloned = structuredClone(value);+ const id = nextId++;+ store.set(id, { ...cloned, id });+ return id;+ }),+ };+ return { objectStore: () => objectStore };+ },+ objectStoreNames: { contains: () => true },+ };+ originalIndexedDB = global.indexedDB;+ global.indexedDB = {+ open: vi.fn(() => {+ const request = {};+ queueMicrotask(() => { request.result = db; request.onsuccess?.(); });+ return request;+ }),+ };+ ({ storeFailedUpload } = await import('./failed-uploads.js'));+ });++ afterEach(() => { global.indexedDB = originalIndexedDB; });++ it('persists even when tags/asrOptions are reactive proxies', async () => {+ const file = {+ name: 'r.webm', size: 8, type: 'audio/webm',+ arrayBuffer: async () => new ArrayBuffer(8),+ };+ // Simulate Vue reactive() state: a Proxy that structuredClone rejects.+ const reactiveTags = new Proxy([{ id: 1, name: 'Work' }], {});+ const reactiveAsr = new Proxy({ language: 'en', diarize: true }, {});++ const id = await storeFailedUpload({+ file, clientId: 'c', tags: reactiveTags, asrOptions: reactiveAsr,+ });++ expect(id).toBeTruthy();+ const stored = store.get(id);+ expect(stored.tags).toEqual([{ id: 1, name: 'Work' }]);+ expect(stored.asrOptions).toEqual({ language: 'en', diarize: true });+ });+});