[PR #8943] [MERGED] fix(server): stream native export images as base64 slices #8898

Closed
opened 2026-11-07 20:43:06 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8943
Author: @SpicyMarinara
Created: 11/7/2026
Status: ✅ Merged
Merged: 11/7/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/export-memory-headroom


📝 Commits (1)

  • 52c909b fix(server): stream native export images as base64 slices

📊 Changes

3 files changed (+93 additions, -18 deletions)

View changed files

📝 packages/server/src/routes/characters.routes.ts (+11 -10)
📝 packages/server/src/utils/export-stream.ts (+52 -4)
📝 scripts/regressions/native-export-memory.regression.ts (+30 -4)

📄 Description

Linked issue

Closes #8917

Why this change

scripts/regressions/native-export-memory.regression.ts flaked: the export server ran out of memory at its 128 MB heap limit. That came from the export code, not the test. The export read one image at a time, but each image still turned into several full-size strings on the JavaScript heap:

  • readImageAsDataUrl built a ~5.3 MB base64 data URL for a 4 MB file;
  • streamJsonWithSlot copied it with JSON.stringify, again with replaceAll for indented ZIP entries, and again when the stream turned it into bytes.

Strings that big land in V8's large-object space, which counts against --max-old-space-size. The old generation peaked at 114–119 MB of 128, so whether a run survived depended on when garbage collection ran. A real export of a character with a large gallery carries the same memory cost.

What changed

  • packages/server/src/utils/export-stream.ts:
    • A DataUrl holds the image's type and bytes.
    • A jsonPieces generator writes exactly the same JSON as before, but emits each image's base64 in 48 KB slices (a multiple of 3, so the slices join cleanly).
    • The output is byte for byte unchanged, and no image-sized string is ever created.
    • The avatar, gallery and sprite items all use it, in the character, persona and bulk ZIP exports.
  • packages/server/src/routes/characters.routes.ts: readImageAsDataUrl, readAvatarDataUrl and spriteExportEntries return DataUrl. The PNG card's sprite reader calls .toString() and keeps its old behaviour.
  • The regression:
    • The 128 MB limit stays. The export now peaks at ~98–104 MB of old generation, while the buffered control needs ~170 MB and still runs out.
    • It logs the old generation separately.
    • A new assertion, independent of GC timing: large-object space grows by less than one image.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Run by the agents (not a substitute for the boxes above):

  • pnpm check: exit 0.
Runs Result Old generation peak Large-object growth
Staging 8 4 died with "Reached heap limit" — —
Staging code + new test 8 all failed 113–119 MB 32–43 MB
This branch 18 (two separate sets) all passed 98–104 MB 0 MB
  • The streamed output equals JSON.stringify at space 0 and 2, with images of 0, 1, 2, 3, 49151, 49152, 49153, 98305 and 300001 bytes, nested and repeated.
  • Export regressions pass: bulk-export-zips, export-file-save, large-native-json-import, persona-api-normalization, sprite-list-security, card-field-alternatives and others.

Still to verify by hand:

  • Manually verify that a native .marinara.json export of a character with an avatar, sprites and a large gallery downloads and re-imports with every image.
  • Manually verify that bulk character and persona ZIP exports open and re-import, and that the PNG card export still embeds sprites.

Docs and release impact

  • No docs changes needed

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/8943 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 11/7/2026 **Status:** ✅ Merged **Merged:** 11/7/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/export-memory-headroom` --- ### 📝 Commits (1) - [`52c909b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/52c909bdfe5eedf5d5f0b6e189aebad9f9175f56) fix(server): stream native export images as base64 slices ### 📊 Changes **3 files changed** (+93 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/characters.routes.ts` (+11 -10) 📝 `packages/server/src/utils/export-stream.ts` (+52 -4) 📝 `scripts/regressions/native-export-memory.regression.ts` (+30 -4) </details> ### 📄 Description ## Linked issue Closes #8917 ## Why this change `scripts/regressions/native-export-memory.regression.ts` flaked: the export server ran out of memory at its 128 MB heap limit. That came from the export code, not the test. The export read one image at a time, but each image still turned into several full-size strings on the JavaScript heap: - `readImageAsDataUrl` built a ~5.3 MB base64 data URL for a 4 MB file; - `streamJsonWithSlot` copied it with `JSON.stringify`, again with `replaceAll` for indented ZIP entries, and again when the stream turned it into bytes. Strings that big land in V8's large-object space, which counts against `--max-old-space-size`. The old generation peaked at 114–119 MB of 128, so whether a run survived depended on when garbage collection ran. A real export of a character with a large gallery carries the same memory cost. ## What changed - `packages/server/src/utils/export-stream.ts`: - A `DataUrl` holds the image's type and bytes. - A `jsonPieces` generator writes exactly the same JSON as before, but emits each image's base64 in 48 KB slices (a multiple of 3, so the slices join cleanly). - The output is byte for byte unchanged, and no image-sized string is ever created. - The avatar, gallery and sprite items all use it, in the character, persona and bulk ZIP exports. - `packages/server/src/routes/characters.routes.ts`: `readImageAsDataUrl`, `readAvatarDataUrl` and `spriteExportEntries` return `DataUrl`. The PNG card's sprite reader calls `.toString()` and keeps its old behaviour. - The regression: - The 128 MB limit stays. The export now peaks at ~98–104 MB of old generation, while the buffered control needs ~170 MB and still runs out. - It logs the old generation separately. - A new assertion, independent of GC timing: large-object space grows by less than one image. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes Run by the agents (not a substitute for the boxes above): - `pnpm check`: exit 0. | | Runs | Result | Old generation peak | Large-object growth | |---|---|---|---|---| | Staging | 8 | 4 died with "Reached heap limit" | — | — | | Staging code + new test | 8 | all failed | 113–119 MB | 32–43 MB | | This branch | 18 (two separate sets) | all passed | 98–104 MB | 0 MB | - The streamed output equals `JSON.stringify` at space 0 and 2, with images of 0, 1, 2, 3, 49151, 49152, 49153, 98305 and 300001 bytes, nested and repeated. - Export regressions pass: bulk-export-zips, export-file-save, large-native-json-import, persona-api-normalization, sprite-list-security, card-field-alternatives and others. Still to verify by hand: - Manually verify that a native `.marinara.json` export of a character with an avatar, sprites and a large gallery downloads and re-imports with every image. - Manually verify that bulk character and persona ZIP exports open and re-import, and that the PNG card export still embeds sprites. ## Docs and release impact - [x] No docs changes needed 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi changed title from [PR #8943] fix(server): stream native export images as base64 slices to [PR #8943] [MERGED] fix(server): stream native export images as base64 slices 2026-11-07 21:50:02 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#8898
No description provided.