{"record":{"id":"0fbda2ca9fbd3c2f","repo":"can1357/oh-my-pi","slug":"asar-archive-is-too-large-to-encode-safely","errorCode":null,"errorMessage":"ASAR archive is too large to encode safely","messagePattern":"ASAR archive is too large to encode safely","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/asar.ts","lineNumber":457,"sourceCode":"\t\t\tif (!Number.isSafeInteger(member[1].byteLength) || !Number.isSafeInteger(payloadSize + member[1].byteLength)) {\n\t\t\t\tthrow new ArchiveError(\"ASAR payload is too large to encode safely\");\n\t\t\t}\n\t\t\tdirectory.files[name] = { size: member[1].byteLength, offset: String(payloadSize) };\n\t\t\tpayloads.push(member[1]);\n\t\t\tpayloadSize += member[1].byteLength;\n\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;","sourceCodeStart":439,"sourceCodeEnd":475,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/asar.ts#L439-L475","documentation":"After computing dataOffset (Pickle prefix + header) plus the accumulated payload size, encodeAsar checks that the total archive size is a safe integer (< 2^53). If dataOffset + payloadSize exceeds Number.MAX_SAFE_INTEGER the offsets written into the JSON header could not be addressed reliably, so it throws instead of producing a corrupt archive.","triggerScenarios":"Calling encodeAsar/encodeArchive/bytes where the combined byteLength of all member payloads plus the header offset exceeds Number.MAX_SAFE_INTEGER (2^53-1, ~9 PiB). Practically only reachable with synthetic/unsized Uint8Array-like inputs.","commonSituations":"Encoding an unbounded iterable of huge members in tests or data pipelines; a member object with an inflated byteLength; attempts to build a single multi-petabyte archive instead of splitting it.","solutions":["Split the content into multiple archives, each with a payload well under the safe-integer bound.","Validate that members are genuine Uint8Arrays with plausible byteLength before encoding.","Stream to disk incrementally instead of one monolithic encodeAsar call for very large datasets.","Catch ArchiveError and report the attempted archiveSize for diagnostics."],"exampleFix":"// before\nawait encodeAsar(unboundedHugeIterable); // archiveSize > 2^53\n// after\nawait Promise.all(partition(unboundedHugeIterable, MAX_BYTES).map(encodeAsar));","handlingStrategy":"validation","validationCode":"function assertSafeArchiveSize(members, headerReserve = 1 << 20) {\n  let payloadSize = 0;\n  for (const [, data] of members) {\n    payloadSize += data.byteLength;\n    if (!Number.isSafeInteger(payloadSize + headerReserve)) {\n      throw new Error('Archive would exceed safe integer size; split it');\n    }\n  }\n}\nassertSafeArchiveSize(members);","typeGuard":null,"tryCatchPattern":"try {\n  const archive = await encodeAsar(members);\n} catch (err) {\n  if (err instanceof ArchiveError && err.message.includes('archive is too large to encode safely')) {\n    throw new Error('Total archive size exceeds Number.MAX_SAFE_INTEGER; split content into multiple archives');\n  }\n  throw err;\n}","preventionTips":["Sum member byteLengths before encoding and split archives that approach unsafe sizes.","Validate member objects are genuine Uint8Arrays to avoid inflated byteLength values.","For very large datasets, write incrementally to disk rather than a single encode call.","Keep per-archive payload budgets far below 2^53 bytes."],"tags":["asar","archive","size-limit","safe-integer"],"backgroundTag":"archive-size-limit-exceeded","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}