Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor([issue-1532]): unify ZIP per-member cap into one shared constant

[._.]/ Adam Eivy6187a42e4202
diff --git a/server/lib/zipStream.js b/server/lib/zipStream.js
index 08c9fe22ba..6d57200bca 100644
--- a/server/lib/zipStream.js
+++ b/server/lib/zipStream.js
@@ -200,21 +200,26 @@ export function parseZip() {
return sink;
}
+// Default per-member ceiling when buffering one ZIP entry into memory. Guards
+// against a pathological member exhausting RAM; consumers buffering arbitrary
+// archive members (chatgptZipImport, appleHealth clinical records) share it.
+export const MAX_ZIP_MEMBER_BYTES = 100 * 1024 * 1024;
+
/**
* Collect a single `parseZip()` entry's decompressed bytes into one Buffer.
*
* `parseZip()` entries are NOT readable streams — they expose only `.pipe(dest)`
* / `.autodrain()`, so the EventEmitter idiom `entry.on('data'/'end')` throws
* `entry.on is not a function`. Pipe the entry into a collecting Writable and
- * resolve with the concatenated buffer instead. When `maxBytes` is finite the
- * collect rejects once the member exceeds it, rather than buffering an unbounded
- * amount into memory.
+ * resolve with the concatenated buffer instead. The collect rejects once the
+ * member exceeds `maxBytes` (defaults to `MAX_ZIP_MEMBER_BYTES`), rather than
+ * buffering an unbounded amount into memory.
*
* Call this synchronously inside the `entry` handler (entries auto-drain on the
* next tick if nothing attaches a pipe/drain), and gather the returned promises
* so the parser's `close` handler can `Promise.all` them before settling.
*/
-export function collectZipEntry(entry, maxBytes = Infinity) {
+export function collectZipEntry(entry, maxBytes = MAX_ZIP_MEMBER_BYTES) {
return new Promise((resolve, reject) => {
const chunks = [];
let size = 0;
diff --git a/server/routes/appleHealth.js b/server/routes/appleHealth.js
index 463641461e..ce0e26bf4a 100644
--- a/server/routes/appleHealth.js
+++ b/server/routes/appleHealth.js
@@ -23,11 +23,6 @@ import {
const XML_WRITE_POLL_INTERVAL_MS = 50;
const XML_WRITE_TIMEOUT_MS = 5000;
-// Per-member cap when buffering a clinical-record JSON into memory. Real records
-// are ~1-5KB each; the generous ceiling just guards against a pathological
-// member exhausting memory (mirrors chatgptZipImport's MAX_MEMBER_BYTES).
-const MAX_CLINICAL_RECORD_BYTES = 100 * 1024 * 1024;
-
const isZip = (file) =>
file.mimetype === 'application/zip' ||
file.mimetype === 'application/x-zip-compressed' ||
@@ -143,10 +138,10 @@ router.post('/import/xml', uploadXml, asyncHandler(async (req, res) => {
} else if (entry.path.includes('clinical_records/') && entry.path.endsWith('.json')) {
// Buffer clinical record JSON files (~1-5KB each). parseZip() entries
// aren't EventEmitters (no entry.on('data'/'end') — that throws), so
- // pipe into a collecting Writable via the shared helper and track the
- // read so 'close' can await it.
+ // pipe into a collecting Writable via the shared helper (which applies
+ // MAX_ZIP_MEMBER_BYTES) and track the read so 'close' can await it.
clinicalReads.push(
- collectZipEntry(entry, MAX_CLINICAL_RECORD_BYTES)
+ collectZipEntry(entry)
.then((buf) => { clinicalJsons.push(buf.toString('utf-8')); })
.catch(settle(reject))
);
diff --git a/server/services/chatgptZipImport.js b/server/services/chatgptZipImport.js
index 01a49a6a3d..7f8e266eda 100644
--- a/server/services/chatgptZipImport.js
+++ b/server/services/chatgptZipImport.js
@@ -33,7 +33,7 @@ import { createReadStream, createWriteStream } from 'fs';
import { mkdir, rename, unlink } from 'fs/promises';
import { Writable } from 'stream';
import { PATHS, getMimeType } from '../lib/fileUtils.js';
-import { parseZip, collectZipEntry } from '../lib/zipStream.js';
+import { parseZip, collectZipEntry, MAX_ZIP_MEMBER_BYTES } from '../lib/zipStream.js';
import { parseExport, importConversations, assetPointerId } from './chatgptImport.js';
import { ServerError } from '../lib/errorHandler.js';
@@ -43,7 +43,7 @@ import { ServerError } from '../lib/errorHandler.js';
// malicious zip claiming one member is enormous. Assets stream straight to disk
// (peak RAM is one chunk), so there is no aggregate in-memory ceiling to keep —
// a legitimately large multi-GB export imports without an arbitrary budget.
-const MAX_MEMBER_BYTES = 100 * 1024 * 1024;
+const MAX_MEMBER_BYTES = MAX_ZIP_MEMBER_BYTES;
// Leading bytes captured from each streamed asset to sniff its magic-byte type
// (the widest sniff — WAV/WebP — reads bytes 8–11). Everything past this streams
@@ -104,11 +104,6 @@ const IS_ASSET_NAME_MAP = (path) => /(?:^|\/)conversation_asset_file_names\.json
// of which `assetPointerId()` reduces to that same bare id.
const datAssetId = (path) => path.replace(/^.*\//, '').replace(/\.dat$/i, '');
-// `parseZip` entries aren't readable streams — they expose `.pipe(dest)` /
-// `.autodrain()`, so buffering one into memory goes through the shared,
-// size-capped `collectZipEntry` helper (see lib/zipStream.js).
-const collect = collectZipEntry;
-
// Stream a `.dat` entry straight to `filePath`, holding only the leading
// `SNIFF_BYTES` so the asset's real extension can be sniffed without buffering
// the whole file (peak RAM is one chunk). Enforces the per-member size cap.
@@ -199,11 +194,11 @@ export async function extractChatgptZip(zipPath, { assetDir = PATHS.brainImportA
.on('entry', (entry) => {
const { path } = entry;
if (IS_ASSET_NAME_MAP(path)) {
- inFlight.push(collect(entry, MAX_MEMBER_BYTES)
+ inFlight.push(collectZipEntry(entry, MAX_MEMBER_BYTES)
.then((buf) => { assetNameMap = JSON.parse(buf.toString('utf8')); })
.catch(onErr));
} else if (IS_CONVO_JSON(path)) {
- inFlight.push(collect(entry, MAX_MEMBER_BYTES)
+ inFlight.push(collectZipEntry(entry, MAX_MEMBER_BYTES)
.then((buf) => { convoBuffers.push({ path, buffer: buf }); })
.catch(onErr));
} else if (IS_DAT(path)) {