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

fix: preserve dump bytes (latin1) and cap diagnostic capture in importDumpFile (#2516)

Adam Eivy38f9f82e12ba
diff --git a/server/routes/database.js b/server/routes/database.js
index 14ac30435c..bbaa218b7e 100644
--- a/server/routes/database.js
+++ b/server/routes/database.js
@@ -336,11 +336,20 @@ export function importDumpFile(dumpFile, port, env, timeout = 120_000) {
// can tear them down. If psql exits early (ON_ERROR_STOP rollback) or the
// timeout SIGKILLs it, the `drain` we may be waiting on never fires, so we
// must destroy the source ourselves or its file descriptor leaks.
- const src = createReadStream(dumpFile);
+ //
+ // Read/write as latin1 (a lossless 1:1 byte↔char mapping): the dump's bytes
+ // pass through untouched regardless of the database's client_encoding — a
+ // utf8 decode→re-encode round-trip would corrupt non-utf8 dumps. This keeps
+ // the byte-transparency the old `sed | psql` pipe had.
+ const src = createReadStream(dumpFile, { encoding: 'latin1' });
const rl = createInterface({ input: src, crlfDelay: Infinity });
+ // Cap diagnostic capture so a pathologically chatty psql (verbose \echo,
+ // giant error text) can't exhaust the heap. psql is near-silent on success.
+ const CAPTURE_LIMIT = 256 * 1024;
let stdout = '';
let stderr = '';
+ const capture = (buf, chunk) => (buf.length >= CAPTURE_LIMIT ? buf : buf + chunk);
let finished = false;
const finish = (exitCode, extraDetail) => {
if (finished) return;
@@ -361,8 +370,8 @@ export function importDumpFile(dumpFile, port, env, timeout = 120_000) {
finish(1, `import timed out after ${timeout}ms`);
}, timeout);
- psql.stdout.on('data', (d) => { stdout += d.toString(); });
- psql.stderr.on('data', (d) => { stderr += d.toString(); });
+ psql.stdout.on('data', (d) => { stdout = capture(stdout, d.toString()); });
+ psql.stderr.on('data', (d) => { stderr = capture(stderr, d.toString()); });
psql.on('error', (err) => finish(1, err.message));
psql.on('close', (code) => finish(typeof code === 'number' ? code : 1));
// psql may exit early (e.g. ON_ERROR_STOP) while we're still writing — the
@@ -377,7 +386,7 @@ export function importDumpFile(dumpFile, port, env, timeout = 120_000) {
rl.on('line', (line) => {
if (isPg17OnlyDirective(line) || !psql.stdin.writable) return;
// Honor backpressure so a large dump doesn't buffer unboundedly in memory.
- if (!psql.stdin.write(line + '\n')) {
+ if (!psql.stdin.write(line + '\n', 'latin1')) {
rl.pause();
psql.stdin.once('drain', () => rl.resume());
}
diff --git a/server/routes/database.test.js b/server/routes/database.test.js
index 8f0e6fc5f5..53a212de01 100644
--- a/server/routes/database.test.js
+++ b/server/routes/database.test.js
@@ -106,13 +106,15 @@ describe('importDumpFile (no-shell streaming import)', () => {
child.stderr = new PassThrough();
const writes = [];
child.stdin = new PassThrough();
- child.stdin.on('data', (chunk) => writes.push(chunk.toString()));
+ child.stdin.on('data', (chunk) => writes.push(Buffer.from(chunk)));
child.stdin.on('finish', () => {
// Emulate psql exiting cleanly once stdin closes.
child.emit('close', 0);
});
child.kill = vi.fn();
- child.__writes = writes;
+ // Raw bytes piped to psql stdin, concatenated.
+ child.__pipedBuffer = () => Buffer.concat(writes);
+ child.__piped = (encoding = 'latin1') => Buffer.concat(writes).toString(encoding);
return child;
}
@@ -145,7 +147,7 @@ describe('importDumpFile (no-shell streaming import)', () => {
expect(args.join(' ')).not.toMatch(/sed|\|/);
// The pg17-only directives were stripped; real SQL survived.
- const piped = child.__writes.join('');
+ const piped = child.__piped();
expect(piped).not.toMatch(/\\restrict/);
expect(piped).not.toMatch(/\\unrestrict/);
expect(piped).not.toMatch(/SET transaction_timeout/);
@@ -153,6 +155,30 @@ describe('importDumpFile (no-shell streaming import)', () => {
expect(piped).toMatch(/INSERT INTO foo VALUES \(1\);/);
});
+ it('preserves raw bytes (non-utf8-safe) instead of round-tripping through utf8', async () => {
+ const child = makeFakePsql();
+ spawn.mockReturnValue(child);
+
+ const dir = mkdtempSync(pathJoin(tmpdir(), 'portos-dump-'));
+ const dumpPath = pathJoin(dir, 'dump.sql');
+ // A lone 0xE9 byte (LATIN1 'é') is NOT valid standalone UTF-8; a utf8
+ // decode→re-encode would replace it with 0xEFBFBD. Plus a valid multibyte
+ // UTF-8 sequence (emoji) that must also survive unchanged.
+ const body = Buffer.concat([
+ Buffer.from('INSERT INTO t VALUES (', 'utf8'),
+ Buffer.from([0xe9]), // raw LATIN1 byte
+ Buffer.from(' -- 😀\n', 'utf8'), // valid UTF-8 multibyte
+ ]);
+ writeFileSync(dumpPath, body);
+
+ const result = await importDumpFile(dumpPath, '5561', {});
+ expect(result.exitCode).toBe(0);
+
+ // Bytes piped to psql must equal the input bytes exactly (line kept; the
+ // '\n' terminator is re-emitted by the line filter).
+ expect(child.__pipedBuffer().equals(body)).toBe(true);
+ });
+
it('resolves with a non-zero exitCode when the dump file cannot be read (no throw)', async () => {
const child = makeFakePsql();
spawn.mockReturnValue(child);