{"record":{"id":"c65d13d468d6d2e1","repo":"jackwener/OpenCLI","slug":"midjourney-media-transfer-size-mismatch-expected","errorCode":null,"errorMessage":"Midjourney media transfer size mismatch: expected ${size}, received ${buffer.length}","messagePattern":"Midjourney media transfer size mismatch: expected (.+?), received (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/midjourney/utils.js","lineNumber":590,"sourceCode":"      const base64 = unwrapEvaluateResult(await page.evaluate((key, start, length) => {\n        const bytes = window[key];\n        if (!(bytes instanceof Uint8Array)) return null;\n        const chunk = bytes.subarray(start, Math.min(bytes.length, start + length));\n        let binary = '';\n        const binaryChunkSize = 0x8000;\n        for (let index = 0; index < chunk.length; index += binaryChunkSize) {\n          binary += String.fromCharCode(...chunk.subarray(index, index + binaryChunkSize));\n        }\n        return btoa(binary);\n      }, transferKey, offset, chunkSize));\n      if (typeof base64 !== 'string' || !base64) {\n        throw new CommandExecutionError(`Midjourney media transfer lost its browser buffer at byte ${offset}`);\n      }\n      parts.push(Buffer.from(base64, 'base64'));\n    }\n    const buffer = Buffer.concat(parts);\n    if (buffer.length !== size) {\n      throw new CommandExecutionError(`Midjourney media transfer size mismatch: expected ${size}, received ${buffer.length}`);\n    }\n    return { buffer, mime: String(payload.type || '').split(';', 1)[0].toLowerCase() };\n  } finally {\n    await page.evaluate((key) => {\n      delete window[key];\n      return true;\n    }, transferKey).catch(() => {});\n  }\n}\n\nfunction sniffMediaMime(buffer) {\n  if (!Buffer.isBuffer(buffer) || buffer.length < 4) return null;\n  if (buffer.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return 'image/png';\n  if (buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) return 'image/jpeg';\n  if (buffer.subarray(0, 4).toString('ascii') === 'RIFF' && buffer.subarray(8, 12).toString('ascii') === 'WEBP') return 'image/webp';\n  if (/^GIF8[79]a$/.test(buffer.subarray(0, 6).toString('ascii'))) return 'image/gif';\n  if (buffer.subarray(4, 8).toString('ascii') === 'ftyp') return 'video/mp4';\n  return null;","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/midjourney/utils.js#L572-L608","documentation":"After concatenating all base64 chunks, the byte length did not match the size the initial payload reported. This indicates a partial or corrupted transfer (some chunks were missed or truncated), so the library refuses to return incomplete media bytes.","triggerScenarios":"A chunk read silently returned short/incorrect data; the page mutated between reads dropping bytes; mismatch between the declared payload.size and the bytes actually stored in the browser buffer.","commonSituations":"Unstable long-running browser sessions transferring large videos; page memory pressure; race conditions with concurrent evaluate calls on the same page.","solutions":["Retry the download; a fresh transfer usually reassembles correctly.","Ensure no concurrent operations run on the same page during the transfer.","Keep the page alive and idle for the full transfer duration.","If reproducible for large files, split transfers or update the library."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  const { buffer } = await downloadMedia(page, url);\n} catch (err) {\n  if (/size mismatch/.test(err.message)) {\n    await sleep(2000);\n    await downloadMedia(page, url); // retry; verify byte length after\n  } else throw err;\n}","preventionTips":["Serialize downloads; one chunked transfer per page at a time.","Avoid heavy concurrent page.evaluate work during large transfers.","For very large videos, ensure the browser session is stable and has adequate memory.","Always let the library's integrity check decide; never trust a partial file."],"tags":["integrity","chunked-transfer","data-corruption","midjourney"],"backgroundTag":"transfer-size-mismatch","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}