Harden large app state UTF-8 writes (#4587)
* fix: write stats file in chunks to avoid Electron UTF-8 abort
orca-stats.json gains an event on every agent start/stop. After about a
month of use mine had grown to ~3.6k events / ~608 KB, and the app started
hard-crashing a few seconds after every launch (SIGTRAP, no catchable JS
stack):
Assertion failed: (length + 1) <= (capacity())
node::MaybeStackBuffer<char>::SetLengthAndZeroTerminate <- node::Utf8Value
The crash is in StatsCollector.writeToDiskSync(), which saves the whole
file in one writeFileSync(JSON.stringify(data)). Electron 42.3.2's bundled
Node aborts when encoding a string that large to UTF-8 in a single write;
stock Node 24 handles the same file fine and the data is well-formed, so
it's an Electron/Node encoding limit, not bad data. The save runs on a
debounce after agent_start, which restored agents fire on launch -- so it
crashed right after opening.
Write the JSON in 64 KB slices through one fd instead (never splitting a
surrogate pair), and lower MAX_EVENTS 10k -> 1k so the file can't grow back
this large. Lifetime aggregates are unaffected.
Verified by reproducing the abort standalone with the real 608 KB file
under ELECTRON_RUN_AS_NODE, confirming the chunked writer round-trips it
byte-for-byte with no crash, and running a patched build that loads the
file without crashing. The underlying encode abort is an Electron/Node bug
to report upstream.
* fix: harden stats JSON writes
* fix: chunk app state UTF-8 writes
* fix: stabilize status and terminal polling
---------
Co-authored-by: thiagomsoares <5190162+thiagomsoares@users.noreply.github.com>
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
// Orca restart so retained dashboard rows reappear on relaunch
|
||||
import { createServer, type IncomingMessage, type ServerResponse } from 'http'
|
||||
import { createHash, randomBytes, randomUUID } from 'crypto'
|
||||
import { chmodSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'fs'
|
||||
import { chmodSync, mkdirSync, readFileSync, renameSync, unlinkSync } from 'fs'
|
||||
import { join } from 'path'
|
||||
|
||||
import { track } from '../telemetry/client'
|
||||
@@ -53,6 +53,7 @@ import {
|
||||
type AgentInterruptInferenceRequest
|
||||
} from '../../shared/agent-interrupt-intent'
|
||||
import { parseLegacyNumericPaneKey, parsePaneKey } from '../../shared/stable-pane-id'
|
||||
import { writeUtf8FileInChunksSync } from '../../shared/utf8-file-writer'
|
||||
import type { LegacyPaneKeyAliasEntry } from '../../shared/types'
|
||||
|
||||
export type { AgentHookSource }
|
||||
@@ -1411,7 +1412,7 @@ export class AgentHookServer {
|
||||
// best-effort
|
||||
}
|
||||
}
|
||||
writeFileSync(tmpPath, json, { mode: 0o600 })
|
||||
writeUtf8FileInChunksSync(tmpPath, json, { mode: 0o600 })
|
||||
tmpWritten = true
|
||||
renameSync(tmpPath, this.lastStatusFilePath)
|
||||
this.lastWrittenJson = json
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* eslint-disable max-lines -- Why: this store is the single main-process owner for Claude usage persistence, scan gating, and query semantics. Keeping those policy decisions together avoids split-brain range/scope logic across multiple files. */
|
||||
import { app } from 'electron'
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'fs'
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync } from 'fs'
|
||||
import { dirname, join } from 'path'
|
||||
import type {
|
||||
ClaudeUsageBreakdownKind,
|
||||
@@ -15,6 +15,7 @@ import type {
|
||||
} from '../../shared/claude-usage-types'
|
||||
import type { AutomationRunUsage } from '../../shared/automations-types'
|
||||
import type { Store } from '../persistence'
|
||||
import { writeUtf8FileInChunksSync } from '../../shared/utf8-file-writer'
|
||||
import { loadKnownUsageWorktreesByRepo, type UsageWorktreeRef } from '../usage-worktree-metadata'
|
||||
import type { ClaudeUsagePersistedState } from './types'
|
||||
import { createWorktreeRefs, getSessionProjectLabel, scanClaudeUsageFiles } from './scanner'
|
||||
@@ -346,7 +347,7 @@ export class ClaudeUsageStore {
|
||||
// atomic temp-file pattern as the main store so a crash or concurrent write
|
||||
// cannot leave a truncated analytics file as the common failure mode.
|
||||
const tmpFile = `${usageFile}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`
|
||||
writeFileSync(tmpFile, JSON.stringify(this.state, null, 2), 'utf-8')
|
||||
writeUtf8FileInChunksSync(tmpFile, JSON.stringify(this.state, null, 2))
|
||||
renameSync(tmpFile, usageFile)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/* eslint-disable max-lines -- Why: this store owns Codex analytics persistence, scan policy, and renderer query semantics. Keeping them together prevents the Codex range/scope rules from drifting away from the scanner’s event model. */
|
||||
import { app } from 'electron'
|
||||
import { dirname, join } from 'path'
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'fs'
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync } from 'fs'
|
||||
import type {
|
||||
CodexUsageBreakdownKind,
|
||||
CodexUsageBreakdownRow,
|
||||
@@ -15,6 +15,7 @@ import type {
|
||||
} from '../../shared/codex-usage-types'
|
||||
import type { AutomationRunUsage } from '../../shared/automations-types'
|
||||
import type { Store } from '../persistence'
|
||||
import { writeUtf8FileInChunksSync } from '../../shared/utf8-file-writer'
|
||||
import { loadKnownUsageWorktreesByRepo, type UsageWorktreeRef } from '../usage-worktree-metadata'
|
||||
import type { CodexUsagePersistedState } from './types'
|
||||
import { createWorktreeRefs, scanCodexUsageFiles } from './scanner'
|
||||
@@ -358,7 +359,7 @@ export class CodexUsageStore {
|
||||
mkdirSync(dir, { recursive: true })
|
||||
}
|
||||
const tmpFile = `${usageFile}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`
|
||||
writeFileSync(tmpFile, JSON.stringify(this.state, null, 2), 'utf-8')
|
||||
writeUtf8FileInChunksSync(tmpFile, JSON.stringify(this.state, null, 2))
|
||||
renameSync(tmpFile, usageFile)
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,8 @@
|
||||
//
|
||||
// 1. Synchronous writes by default. The error-tracking lane has to be
|
||||
// durable on crash — if the renderer or main process is about to die,
|
||||
// a buffered async flush is exactly what we don't want. We use the
|
||||
// `appendFileSync` path (cheap on modern fs at this volume) and
|
||||
// explicitly do a final `flush()` on shutdown.
|
||||
// a buffered async flush is exactly what we don't want. We write through
|
||||
// an owned fd and explicitly do a final `flush()` on shutdown.
|
||||
//
|
||||
// 2. Buffered batches with a flush threshold. Batches of up to
|
||||
// `FLUSH_BUFFER_THRESHOLD` lines are coalesced into one syscall to
|
||||
@@ -31,10 +30,10 @@ import {
|
||||
openSync,
|
||||
renameSync,
|
||||
statSync,
|
||||
unlinkSync,
|
||||
writeSync
|
||||
unlinkSync
|
||||
} from 'node:fs'
|
||||
import { dirname } from 'node:path'
|
||||
import { writeUtf8StringToFdInChunksSync } from '../../shared/utf8-file-writer'
|
||||
|
||||
const DEFAULT_FLUSH_BUFFER_THRESHOLD = 32
|
||||
export const DEFAULT_MAX_BYTES = 10 * 1024 * 1024 // 10 MB
|
||||
@@ -173,7 +172,7 @@ export function createLocalFileSink(opts: LocalFileSinkOptions): LocalFileSink {
|
||||
}
|
||||
const chunk = chunkLines.join('')
|
||||
try {
|
||||
writeSync(fd, chunk)
|
||||
writeUtf8StringToFdInChunksSync(fd, chunk)
|
||||
currentBytes += chunkBytes
|
||||
} catch {
|
||||
// Reopen and retry once. If the second write also fails, drop this
|
||||
@@ -186,7 +185,7 @@ export function createLocalFileSink(opts: LocalFileSinkOptions): LocalFileSink {
|
||||
/* swallow — best effort */
|
||||
}
|
||||
fd = openAppend(filePath)
|
||||
writeSync(fd, chunk)
|
||||
writeUtf8StringToFdInChunksSync(fd, chunk)
|
||||
currentBytes = safeFstatSize(fd)
|
||||
} catch {
|
||||
/* swallow — telemetry must never crash main */
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/* eslint-disable max-lines -- Why: this store owns OpenCode analytics persistence, scan policy, and renderer query semantics. Keeping range/scope queries next to scan persistence prevents UI totals from drifting from the SQLite projection. */
|
||||
import { app } from 'electron'
|
||||
import { dirname, join } from 'path'
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'fs'
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync } from 'fs'
|
||||
import type {
|
||||
OpenCodeUsageBreakdownKind,
|
||||
OpenCodeUsageBreakdownRow,
|
||||
@@ -14,6 +14,7 @@ import type {
|
||||
OpenCodeUsageSummary
|
||||
} from '../../shared/opencode-usage-types'
|
||||
import type { Store } from '../persistence'
|
||||
import { writeUtf8FileInChunksSync } from '../../shared/utf8-file-writer'
|
||||
import { loadKnownUsageWorktreesByRepo, type UsageWorktreeRef } from '../usage-worktree-metadata'
|
||||
import type { OpenCodeUsageDailyAggregate, OpenCodeUsagePersistedState } from './types'
|
||||
import { createWorktreeRefs, scanOpenCodeUsageDatabases } from './scanner'
|
||||
@@ -184,7 +185,7 @@ export class OpenCodeUsageStore {
|
||||
mkdirSync(dir, { recursive: true })
|
||||
}
|
||||
const tmpFile = `${usageFile}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`
|
||||
writeFileSync(tmpFile, JSON.stringify(this.state, null, 2), 'utf-8')
|
||||
writeUtf8FileInChunksSync(tmpFile, JSON.stringify(this.state, null, 2))
|
||||
renameSync(tmpFile, usageFile)
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ as a unit instead of being scattered across modules. */
|
||||
import { app, safeStorage } from 'electron'
|
||||
import {
|
||||
readFileSync,
|
||||
writeFileSync,
|
||||
mkdirSync,
|
||||
existsSync,
|
||||
renameSync,
|
||||
@@ -13,7 +12,7 @@ import {
|
||||
statSync,
|
||||
realpathSync
|
||||
} from 'fs'
|
||||
import { writeFile, rename, mkdir, rm, copyFile } from 'fs/promises'
|
||||
import { rename, mkdir, rm, copyFile } from 'fs/promises'
|
||||
import { join, dirname, isAbsolute, resolve, sep } from 'path'
|
||||
import { homedir } from 'os'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
@@ -70,6 +69,7 @@ import {
|
||||
ONBOARDING_FINAL_STEP
|
||||
} from '../shared/constants'
|
||||
import { parseWorkspaceSession } from '../shared/workspace-session-schema'
|
||||
import { writeUtf8FileInChunks, writeUtf8FileInChunksSync } from '../shared/utf8-file-writer'
|
||||
import { toRelaySshPtyId } from './providers/ssh-pty-id'
|
||||
import {
|
||||
isTerminalLeafId,
|
||||
@@ -1585,7 +1585,7 @@ export class Store {
|
||||
const raw = readFileSync(path, 'utf-8')
|
||||
JSON.parse(raw)
|
||||
mkdirSync(dirname(dataFile), { recursive: true })
|
||||
writeFileSync(dataFile, raw, 'utf-8')
|
||||
writeUtf8FileInChunksSync(dataFile, raw)
|
||||
console.warn(`[persistence] Recovered state from backup slot ${i}: ${path}`)
|
||||
return true
|
||||
} catch (err) {
|
||||
@@ -2187,7 +2187,7 @@ export class Store {
|
||||
// multi-megabyte orphan behind. Successful rename consumes the tmp file.
|
||||
let renamed = false
|
||||
try {
|
||||
await writeFile(tmpFile, JSON.stringify(stateToSave, null, 2), 'utf-8')
|
||||
await writeUtf8FileInChunks(tmpFile, JSON.stringify(stateToSave, null, 2))
|
||||
// Why: if flush() ran while this async write was in-flight, it bumped
|
||||
// writeGeneration and already wrote the latest state synchronously.
|
||||
// Renaming this stale tmp file would overwrite the fresh data.
|
||||
@@ -2237,12 +2237,12 @@ export class Store {
|
||||
}
|
||||
}
|
||||
|
||||
// Why: mirror the async path — on any failure between writeFileSync and
|
||||
// Why: mirror the async path — on any failure between writing and
|
||||
// renameSync, remove the tmp file so crashes during shutdown don't leak
|
||||
// orphans into userData.
|
||||
let renamed = false
|
||||
try {
|
||||
writeFileSync(tmpFile, JSON.stringify(stateToSave, null, 2), 'utf-8')
|
||||
writeUtf8FileInChunksSync(tmpFile, JSON.stringify(stateToSave, null, 2))
|
||||
renameSync(tmpFile, dataFile)
|
||||
renamed = true
|
||||
} finally {
|
||||
|
||||
@@ -2,14 +2,14 @@ import { app } from 'electron'
|
||||
import { readFileSync, mkdirSync, existsSync, renameSync } from 'fs'
|
||||
import { join, dirname } from 'path'
|
||||
import type { StatsSummary } from '../../shared/types'
|
||||
import { writeJsonInChunks } from './stats-json-writer'
|
||||
import { writeUtf8FileInChunksSync } from '../../shared/utf8-file-writer'
|
||||
import type { StatsEvent, StatsAggregates, StatsFile } from './types'
|
||||
|
||||
const STATS_SCHEMA_VERSION = 1
|
||||
// Why 1000 (was 10000): the events array is a bounded "fun stats" log. A smaller
|
||||
// cap bounds the JSON written on each save which, together with the chunked write
|
||||
// in writeToDiskSync, keeps Orca clear of an Electron/Node abort when encoding a
|
||||
// very large string to UTF-8 (see writeJsonInChunks).
|
||||
// very large string to UTF-8 (see writeUtf8FileInChunksSync).
|
||||
const MAX_EVENTS = 1_000
|
||||
// Why: countedPRs is a deduplication registry that grows with every PR created
|
||||
// through Orca. Without a cap, a heavily-used instance accumulates thousands of
|
||||
@@ -273,7 +273,7 @@ export class StatsCollector {
|
||||
// Why unique temp file: same race-safe pattern as persistence.ts:120 —
|
||||
// synchronous flushes can race the debounced writer during shutdown.
|
||||
const tmpFile = `${statsFile}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`
|
||||
writeJsonInChunks(tmpFile, JSON.stringify(data))
|
||||
writeUtf8FileInChunksSync(tmpFile, JSON.stringify(data))
|
||||
renameSync(tmpFile, statsFile)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
import { mkdtempSync, readFileSync, rmSync } from 'fs'
|
||||
import { tmpdir } from 'os'
|
||||
import { join } from 'path'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { writeJsonInChunks } from './stats-json-writer'
|
||||
|
||||
let tempDirs: string[] = []
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of tempDirs) {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
tempDirs = []
|
||||
})
|
||||
|
||||
function getTempFile(name: string): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'orca-stats-json-writer-'))
|
||||
tempDirs.push(dir)
|
||||
return join(dir, name)
|
||||
}
|
||||
|
||||
function expectChunkedWriteToPreserveBytes(name: string, json: string): void {
|
||||
const file = getTempFile(name)
|
||||
writeJsonInChunks(file, json)
|
||||
expect(readFileSync(file)).toEqual(Buffer.from(json, 'utf8'))
|
||||
}
|
||||
|
||||
describe('writeJsonInChunks', () => {
|
||||
it('writes large JSON byte-identically', () => {
|
||||
const json = JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
events: Array.from({ length: 2000 }, (_, index) => ({
|
||||
type: index % 2 === 0 ? 'agent_start' : 'agent_stop',
|
||||
at: 1760000000000 + index,
|
||||
meta: {
|
||||
ptyId: `pty-${index}`,
|
||||
note: 'plain stats payload '.repeat(8)
|
||||
}
|
||||
})),
|
||||
aggregates: {
|
||||
totalAgentsSpawned: 1000,
|
||||
totalPRsCreated: 0,
|
||||
totalAgentTimeMs: 0,
|
||||
countedPRs: [],
|
||||
firstEventAt: 1760000000000
|
||||
}
|
||||
})
|
||||
|
||||
expect(Buffer.byteLength(json, 'utf8')).toBeGreaterThan(200_000)
|
||||
expectChunkedWriteToPreserveBytes('large.json', json)
|
||||
})
|
||||
|
||||
it('preserves multibyte content and surrogate pairs at chunk boundaries', () => {
|
||||
const boundaryAstral = `${'a'.repeat(16_383)}😀${'b'.repeat(512)}`
|
||||
const multibyteJson = JSON.stringify({
|
||||
note: `${boundaryAstral}${'é漢字'.repeat(20_000)}`
|
||||
})
|
||||
|
||||
expectChunkedWriteToPreserveBytes('multibyte.json', multibyteJson)
|
||||
})
|
||||
})
|
||||
@@ -1,47 +0,0 @@
|
||||
import { closeSync, openSync, writeSync } from 'fs'
|
||||
|
||||
// Why 16K code units: JS string slicing is code-unit based. This keeps each
|
||||
// UTF-8 Buffer comfortably below 64KiB even for non-ASCII JSON content.
|
||||
const JSON_WRITE_CHUNK_CODE_UNITS = 16_384
|
||||
|
||||
/**
|
||||
* Write `json` to `path` in small slices via a single file descriptor.
|
||||
*
|
||||
* Why not writeFileSync(path, json): Electron's bundled Node can abort the
|
||||
* process when encoding a large string to UTF-8 in one filesystem write. Each
|
||||
* slice is encoded to a small Buffer first, so fs.writeSync never receives a
|
||||
* large string and surrogate pairs stay intact.
|
||||
*/
|
||||
export function writeJsonInChunks(path: string, json: string): void {
|
||||
const fd = openSync(path, 'w')
|
||||
try {
|
||||
let index = 0
|
||||
while (index < json.length) {
|
||||
const end = getNextChunkEnd(json, index)
|
||||
writeBufferFully(fd, Buffer.from(json.slice(index, end), 'utf8'))
|
||||
index = end
|
||||
}
|
||||
} finally {
|
||||
closeSync(fd)
|
||||
}
|
||||
}
|
||||
|
||||
function getNextChunkEnd(json: string, index: number): number {
|
||||
let end = Math.min(index + JSON_WRITE_CHUNK_CODE_UNITS, json.length)
|
||||
const lastUnit = json.charCodeAt(end - 1)
|
||||
if (end < json.length && lastUnit >= 0xd800 && lastUnit <= 0xdbff) {
|
||||
end -= 1
|
||||
}
|
||||
return end
|
||||
}
|
||||
|
||||
function writeBufferFully(fd: number, buffer: Buffer): void {
|
||||
let offset = 0
|
||||
while (offset < buffer.byteLength) {
|
||||
const bytesWritten = writeSync(fd, buffer, offset, buffer.byteLength - offset)
|
||||
if (bytesWritten === 0) {
|
||||
throw new Error('Failed to write stats JSON chunk')
|
||||
}
|
||||
offset += bytesWritten
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'fs'
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync } from 'fs'
|
||||
import { homedir } from 'os'
|
||||
import { dirname, join } from 'path'
|
||||
import { writeUtf8FileInChunksSync } from '../shared/utf8-file-writer'
|
||||
import type { RelayDispatcher } from './dispatcher'
|
||||
|
||||
type RemoteWorkspaceSnapshot = {
|
||||
@@ -116,7 +117,7 @@ export class WorkspaceSessionHandler {
|
||||
const path = this.snapshotPath(snapshot.namespace)
|
||||
mkdirSync(dirname(path), { recursive: true, mode: 0o700 })
|
||||
const tmpPath = `${path}.tmp`
|
||||
writeFileSync(tmpPath, JSON.stringify(snapshot, null, 2), { mode: 0o600 })
|
||||
writeUtf8FileInChunksSync(tmpPath, JSON.stringify(snapshot, null, 2), { mode: 0o600 })
|
||||
renameSync(tmpPath, path)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
import { mkdtempSync, readFileSync, rmSync, statSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { writeUtf8FileInChunks, writeUtf8FileInChunksSync } from './utf8-file-writer'
|
||||
|
||||
let tempDirs: string[] = []
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of tempDirs) {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
tempDirs = []
|
||||
})
|
||||
|
||||
function getTempFile(name: string): string {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'orca-utf8-file-writer-'))
|
||||
tempDirs.push(dir)
|
||||
return join(dir, name)
|
||||
}
|
||||
|
||||
function buildLargeJson(): string {
|
||||
return JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
events: Array.from({ length: 2000 }, (_, index) => ({
|
||||
type: index % 2 === 0 ? 'agent_start' : 'agent_stop',
|
||||
at: 1760000000000 + index,
|
||||
meta: {
|
||||
ptyId: `pty-${index}`,
|
||||
note: 'plain stats payload '.repeat(8)
|
||||
}
|
||||
})),
|
||||
aggregates: {
|
||||
totalAgentsSpawned: 1000,
|
||||
totalPRsCreated: 0,
|
||||
totalAgentTimeMs: 0,
|
||||
countedPRs: [],
|
||||
firstEventAt: 1760000000000
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function buildBoundaryJson(): string {
|
||||
const boundaryAstral = `${'a'.repeat(16_383)}😀${'b'.repeat(512)}`
|
||||
return JSON.stringify({
|
||||
note: `${boundaryAstral}${'é漢字'.repeat(20_000)}`
|
||||
})
|
||||
}
|
||||
|
||||
describe('writeUtf8FileInChunksSync', () => {
|
||||
it('writes large JSON byte-identically', () => {
|
||||
const json = buildLargeJson()
|
||||
const file = getTempFile('large.json')
|
||||
|
||||
expect(Buffer.byteLength(json, 'utf8')).toBeGreaterThan(200_000)
|
||||
writeUtf8FileInChunksSync(file, json)
|
||||
|
||||
expect(readFileSync(file)).toEqual(Buffer.from(json, 'utf8'))
|
||||
})
|
||||
|
||||
it('preserves multibyte content and surrogate pairs at chunk boundaries', () => {
|
||||
const json = buildBoundaryJson()
|
||||
const file = getTempFile('multibyte.json')
|
||||
|
||||
writeUtf8FileInChunksSync(file, json)
|
||||
|
||||
expect(readFileSync(file)).toEqual(Buffer.from(json, 'utf8'))
|
||||
})
|
||||
|
||||
it('honors private file modes', () => {
|
||||
if (process.platform === 'win32') {
|
||||
return
|
||||
}
|
||||
const file = getTempFile('private.json')
|
||||
|
||||
writeUtf8FileInChunksSync(file, '{}', { mode: 0o600 })
|
||||
|
||||
expect(statSync(file).mode & 0o777).toBe(0o600)
|
||||
})
|
||||
})
|
||||
|
||||
describe('writeUtf8FileInChunks', () => {
|
||||
it('writes large JSON byte-identically', async () => {
|
||||
const json = buildLargeJson()
|
||||
const file = getTempFile('large-async.json')
|
||||
|
||||
await writeUtf8FileInChunks(file, json)
|
||||
|
||||
expect(readFileSync(file)).toEqual(Buffer.from(json, 'utf8'))
|
||||
})
|
||||
|
||||
it('preserves multibyte content and surrogate pairs at chunk boundaries', async () => {
|
||||
const json = buildBoundaryJson()
|
||||
const file = getTempFile('multibyte-async.json')
|
||||
|
||||
await writeUtf8FileInChunks(file, json)
|
||||
|
||||
expect(readFileSync(file)).toEqual(Buffer.from(json, 'utf8'))
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,107 @@
|
||||
import { closeSync, openSync, writeSync } from 'node:fs'
|
||||
import { open, type FileHandle } from 'node:fs/promises'
|
||||
|
||||
// Why 16K code units: JS string slicing is code-unit based. This keeps each
|
||||
// UTF-8 Buffer comfortably below 64KiB even for non-ASCII JSON content.
|
||||
const UTF8_WRITE_CHUNK_CODE_UNITS = 16_384
|
||||
|
||||
type Utf8FileWriteOptions = {
|
||||
readonly mode?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Write `contents` to `path` in small UTF-8 chunks.
|
||||
*
|
||||
* Why not writeFile/writeFileSync(path, contents): Electron's bundled Node can
|
||||
* abort the process when encoding a large string to UTF-8 in one filesystem
|
||||
* write. Each slice is encoded to a small Buffer first, so fs never receives a
|
||||
* large string and surrogate pairs stay intact.
|
||||
*/
|
||||
export async function writeUtf8FileInChunks(
|
||||
path: string,
|
||||
contents: string,
|
||||
options: Utf8FileWriteOptions = {}
|
||||
): Promise<void> {
|
||||
const handle = await openPathForWrite(path, options)
|
||||
try {
|
||||
await writeUtf8StringToHandleInChunks(handle, contents)
|
||||
} finally {
|
||||
await handle.close()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronous variant for shutdown and other paths that cannot wait on async IO.
|
||||
*/
|
||||
export function writeUtf8FileInChunksSync(
|
||||
path: string,
|
||||
contents: string,
|
||||
options: Utf8FileWriteOptions = {}
|
||||
): void {
|
||||
const fd = openPathForWriteSync(path, options)
|
||||
try {
|
||||
writeUtf8StringToFdInChunksSync(fd, contents)
|
||||
} finally {
|
||||
closeSync(fd)
|
||||
}
|
||||
}
|
||||
|
||||
export function writeUtf8StringToFdInChunksSync(fd: number, contents: string): void {
|
||||
let index = 0
|
||||
while (index < contents.length) {
|
||||
const end = getNextChunkEnd(contents, index)
|
||||
writeBufferFullySync(fd, Buffer.from(contents.slice(index, end), 'utf8'))
|
||||
index = end
|
||||
}
|
||||
}
|
||||
|
||||
async function writeUtf8StringToHandleInChunks(
|
||||
handle: FileHandle,
|
||||
contents: string
|
||||
): Promise<void> {
|
||||
let index = 0
|
||||
while (index < contents.length) {
|
||||
const end = getNextChunkEnd(contents, index)
|
||||
await writeBufferFully(handle, Buffer.from(contents.slice(index, end), 'utf8'))
|
||||
index = end
|
||||
}
|
||||
}
|
||||
|
||||
function openPathForWrite(path: string, options: Utf8FileWriteOptions): Promise<FileHandle> {
|
||||
return options.mode === undefined ? open(path, 'w') : open(path, 'w', options.mode)
|
||||
}
|
||||
|
||||
function openPathForWriteSync(path: string, options: Utf8FileWriteOptions): number {
|
||||
return options.mode === undefined ? openSync(path, 'w') : openSync(path, 'w', options.mode)
|
||||
}
|
||||
|
||||
function getNextChunkEnd(contents: string, index: number): number {
|
||||
let end = Math.min(index + UTF8_WRITE_CHUNK_CODE_UNITS, contents.length)
|
||||
const lastUnit = contents.charCodeAt(end - 1)
|
||||
if (end < contents.length && lastUnit >= 0xd800 && lastUnit <= 0xdbff) {
|
||||
end -= 1
|
||||
}
|
||||
return end
|
||||
}
|
||||
|
||||
function writeBufferFullySync(fd: number, buffer: Buffer): void {
|
||||
let offset = 0
|
||||
while (offset < buffer.byteLength) {
|
||||
const bytesWritten = writeSync(fd, buffer, offset, buffer.byteLength - offset)
|
||||
if (bytesWritten === 0) {
|
||||
throw new Error('Failed to write UTF-8 file chunk')
|
||||
}
|
||||
offset += bytesWritten
|
||||
}
|
||||
}
|
||||
|
||||
async function writeBufferFully(handle: FileHandle, buffer: Buffer): Promise<void> {
|
||||
let offset = 0
|
||||
while (offset < buffer.byteLength) {
|
||||
const { bytesWritten } = await handle.write(buffer, offset, buffer.byteLength - offset)
|
||||
if (bytesWritten === 0) {
|
||||
throw new Error('Failed to write UTF-8 file chunk')
|
||||
}
|
||||
offset += bytesWritten
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user