{"record":{"id":"e4f25fec6882ce71","repo":"can1357/oh-my-pi","slug":"asar-archive-is-too-large-to-encode-in-memory","errorCode":null,"errorMessage":"ASAR archive is too large to encode in memory","messagePattern":"ASAR archive is too large to encode in memory","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/asar.ts","lineNumber":463,"sourceCode":"\t\t}\n\n\t\tconst jsonBytes = ASAR_HEADER_ENCODER.encode(JSON.stringify(root));\n\t\tconst paddedJsonSize = alignAsarPayload(jsonBytes.byteLength);\n\t\tconst innerPayloadSize = 4 + paddedJsonSize;\n\t\tconst headerSize = 4 + innerPayloadSize;\n\t\tif (jsonBytes.byteLength > 0xffffffff || headerSize > 0xffffffff) {\n\t\t\tthrow new ArchiveError(\"ASAR header is too large to encode\");\n\t\t}\n\t\tconst dataOffset = ASAR_PICKLE_PREFIX_SIZE + headerSize;\n\t\tconst archiveSize = dataOffset + payloadSize;\n\t\tif (!Number.isSafeInteger(archiveSize)) {\n\t\t\tthrow new ArchiveError(\"ASAR archive is too large to encode safely\");\n\t\t}\n\t\tlet output: Uint8Array;\n\t\ttry {\n\t\t\toutput = new Uint8Array(archiveSize);\n\t\t} catch {\n\t\t\tthrow new ArchiveError(\"ASAR archive is too large to encode in memory\");\n\t\t}\n\t\twriteUInt32LE(output, 0, 4);\n\t\twriteUInt32LE(output, 4, headerSize);\n\t\twriteUInt32LE(output, 8, innerPayloadSize);\n\t\twriteUInt32LE(output, 12, jsonBytes.byteLength);\n\t\toutput.set(jsonBytes, ASAR_JSON_OFFSET);\n\t\tlet offset = dataOffset;\n\t\tfor (const payload of payloads) {\n\t\t\toutput.set(payload, offset);\n\t\t\toffset += payload.byteLength;\n\t\t}\n\t\treturn output;\n\t} catch (error) {\n\t\tif (error instanceof ArchiveError) throw error;\n\t\tthrow new ArchiveError(`Failed to encode ASAR archive: ${describeError(error)}`);\n\t}\n}\n","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/asar.ts#L445-L481","documentation":"encodeAsar builds the entire ASAR archive as a single in-memory Uint8Array. Before allocating, it validates the total archive size is a safe integer, then attempts `new Uint8Array(archiveSize)`. If that allocation fails (RangeError from the JS engine), the library wraps the failure as ArchiveError(\"ASAR archive is too large to encode in memory\"), because the encoder API is memory-buffer based and cannot stream output.","triggerScenarios":"Calling encodeAsar (directly or via encodeArchive/bytes) with a member set whose combined payload size plus header exceeds available memory — e.g. many multi-GB files, or a total size near the safe-integer limit where a contiguous ArrayBuffer cannot be allocated.","commonSituations":"Packaging huge Electron app directories where the sum of file sizes exceeds the process heap/ArrayBuffer limits; running in memory-constrained environments (containers, low-RAM CI runners); accidentally passing the same large file many times or building member lists in a loop without deduplication.","solutions":["Reduce the total payload size: exclude large binaries/assets from the archive and ship them separately.","Split the content into multiple smaller ASAR archives.","Increase the Node/Bun memory headroom for the process (e.g. larger heap, containers with more RAM) if the size is legitimate.","Use a streaming or file-backed archiver instead of this in-memory encoder for very large payloads."],"exampleFix":"// before\nconst archive = encodeAsar(members); // members include node_modules with multi-GB artifacts\n// after\nconst smallMembers = new Map([...members].filter(([path]) => !path.startsWith(\"assets/videos/\")));\nconst archive = encodeAsar(smallMembers); // exclude bulk assets, ship them separately","handlingStrategy":"validation","validationCode":"const totalBytes = [...members.values()].reduce((n, b) => n + b.byteLength, 0);\nif (!Number.isSafeInteger(totalBytes) || totalBytes > MAX_IN_MEMORY_ARCHIVE_BYTES) {\n  throw new Error(`archive too large for in-memory encode: ${totalBytes} bytes`);\n}","typeGuard":"null","tryCatchPattern":"try {\n  const archive = encodeAsar(members);\n} catch (err) {\n  if (err instanceof ArchiveError && err.message.includes(\"too large to encode in memory\")) {\n    // fall back to streaming/file-based packaging\n  } else throw err;\n}","preventionTips":["Sum member byte lengths before encoding and enforce a size budget.","Exclude bulk assets (videos, prebuilt binaries) from ASAR and ship them outside.","Monitor process memory; run packaging in a high-RAM environment for large apps.","Prefer a streaming archiver for anything near the GB scale."],"tags":["memory","asar","archive","allocation"],"backgroundTag":"asar-encode-out-of-memory","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}