can1357/oh-my-pi · error · ArchiveError

Archive is too large to read safely

Error message

Archive is too large to read safely

What it means

`fileByteSource` refuses to back a ByteSource with a file whose size is not a JS safe integer (i.e. larger than 2^53-1 bytes). Such sizes cannot be handled with exact integer range arithmetic, so the library fails fast instead of silently producing wrong offsets.

Source

Thrown at packages/utils/src/ar/source.ts:45

	return buffer.subarray(start, end);
}

/** Wrap borrowed bytes as a {@link ByteSource}. */
export function memoryByteSource(buffer: Uint8Array): ByteSource {
	return {
		size: buffer.byteLength,
		async read(start, end) {
			return readMemoryRange(buffer, start, end);
		},
	};
}

/** Lazily read ranges of a file on disk as a {@link ByteSource}. */
export function fileByteSource(filePath: string): ByteSource {
	const file = Bun.file(filePath);
	const size = file.size;
	if (!Number.isSafeInteger(size)) {
		throw new ArchiveError("Archive is too large to read safely");
	}
	return {
		size,
		async read(start, end) {
			assertValidRange(start, end);
			const bytes = await file.slice(start, end).bytes();
			if (bytes.byteLength !== end - start) {
				throw new ArchiveError("Invalid archive: truncated data");
			}
			return bytes;
		},
	};
}

/** Materialize an entire {@link ByteSource}; use only under a limits check. */
export async function readAllBytes(source: ByteSource): Promise<Uint8Array> {
	return source.read(0, source.size);
}

View on GitHub (pinned to 9690622007)

Solutions

  1. Check the file's real size with `Bun.file(path).size` before opening and reject if not a safe integer
  2. If on a FUSE/virtual mount, verify the size reporting is correct at the mount layer
  3. For test setups, ensure mocked file objects expose a numeric, safe-integer `size`

Example fix

// before: opening directly
const src = fileByteSource(mountPath);
// after: pre-check the reported size
const f = Bun.file(mountPath);
if (!Number.isSafeInteger(f.size)) throw new Error(`unusable size for ${mountPath}: ${f.size}`);
const src = fileByteSource(mountPath);
Defensive patterns

Strategy: validation

Validate before calling

const f = Bun.file(path);
if (!Number.isSafeInteger(f.size)) throw new Error(`file size not representable: ${f.size}`);

Type guard

function hasSafeSize(size: unknown): size is number {
	return typeof size === "number" && Number.isSafeInteger(size);
}

Try / catch

try {
	const src = fileByteSource(path);
} catch (err) {
	if (err instanceof ArchiveError && err.message.includes("too large to read safely")) {
		throw new Error(`cannot open ${path}: size exceeds safe integer range`);
	}
	throw err;
}

Prevention

When it happens

Trigger: Calling `fileByteSource` (or `resolveSource` with a file path) on a file whose reported size exceeds Number.MAX_SAFE_INTEGER — ~9 petabytes. Practically impossible for real disks; only reachable via exotic filesystems reporting bogus sizes.

Common situations: Virtual/synthetic filesystems or FUSE mounts reporting overflowed sizes; corrupted stat results; tests mocking Bun.file with string/NaN sizes that fail the safe-integer check.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/7122d14916fca3b3. Report an issue: GitHub.