mastra-ai/mastra · error · Error

buildHdrl internal error: wrote ${p} bytes, expected ${hdrlP

Error message

buildHdrl internal error: wrote ${p} bytes, expected ${hdrlPayloadSize}

What it means

buildHdrl writes the AVI main + stream header ('hdrl') list and verifies the byte cursor matches the precomputed payload size. A mismatch is a pure internal bug in the writer's size accounting, not user input — it means the header builder and its size math diverged.

Source

Thrown at packages/core/src/browser/recording/mjpeg-avi.ts:338

  p += 2;
  // biCompression = "MJPG"
  FOURCC_MJPG.copy(buf, p);
  p += 4;
  // biSizeImage
  buf.writeUInt32LE(sizeImage, p);
  p += 4;
  // biXPelsPerMeter, biYPelsPerMeter, biClrUsed, biClrImportant
  buf.writeInt32LE(0, p);
  p += 4;
  buf.writeInt32LE(0, p);
  p += 4;
  buf.writeUInt32LE(0, p);
  p += 4;
  buf.writeUInt32LE(0, p);
  p += 4;

  if (p !== hdrlPayloadSize) {
    throw new Error(`buildHdrl internal error: wrote ${p} bytes, expected ${hdrlPayloadSize}`);
  }
  return buf;
}

function paddedLength(n: number): number {
  return n + (n & 1);
}

function assertJpegFrame(bytes: Uint8Array, index: number): void {
  if (bytes.length < 2 || bytes[0] !== 0xff || bytes[1] !== 0xd8) {
    throw new Error(`writeMjpegAviFile: frame ${index} is not a JPEG frame (missing SOI marker)`);
  }
}

function assertU32(value: number, field: string): void {
  if (!Number.isSafeInteger(value) || value < 0 || value > MAX_U32) {
    throw new Error(`writeMjpegAviFile: ${field} exceeds 32-bit AVI limit (${value})`);
  }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. File a bug against the mjpeg-avi muxer — this is an internal invariant violation
  2. If you modified buildHdrl, recompute hdrlPayloadSize to match every writeUInt* call in the builder
  3. Add/refresh unit tests that mux a minimal recording so the invariant is exercised in CI

Example fix

// before (added an extra field but kept the old size)
buf.writeUInt32LE(flags, p); p += 4; // hdrlPayloadSize not updated
// after
hdrlPayloadSize += 4; // keep size constant in sync with writes
Defensive patterns

Strategy: try-catch

Validate before calling

null

Try / catch

try {
  const buf = buildHdrl(...);
} catch (e) {
  if (e.message.startsWith('buildHdrl internal error')) {
    throw new Error('AVI muxer bug: header size accounting mismatch — report to library maintainers');
  }
  throw e;
}

Prevention

When it happens

Trigger: Only reachable when the module's own hdrl layout code and hdrlPayloadSize constant disagree — typically after someone edited buildHdrl (added/removed fields) without updating hdrlPayloadSize.

Common situations: Encountered by contributors modifying the AVI muxer; end users hit it only as fallout of an untested fork/patch or corrupted build of mjpeg-avi.ts.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/e1271753ec91a1ec. Report an issue: GitHub.