Skip to content

Commit 55ee6df

Browse files
committed
refactor(test): use createWriteStream instead of dynamic import
Replace await import('node:fs/promises') with createWriteStream for building large test files. This adheres to the project rule against dynamic imports and provides better memory efficiency through proper backpressure handling. Addresses CodeRabbit review feedback.
1 parent 8abf17b commit 55ee6df

1 file changed

Lines changed: 15 additions & 15 deletions

File tree

‎apps/ccusage/src/data-loader.ts‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
SortOrder,
1919
Version,
2020
} from './_types.ts';
21-
import { createReadStream } from 'node:fs';
21+
import { createReadStream, createWriteStream } from 'node:fs';
2222
import { readFile } from 'node:fs/promises';
2323
import path from 'node:path';
2424
import process from 'node:process';
@@ -661,23 +661,23 @@ if (import.meta.vitest != null) {
661661
await using fixture = await createFixture({});
662662
const filePath = path.join(fixture.path, 'large.jsonl');
663663

664-
// Write file in chunks to avoid Node.js string length limit (~512MB)
664+
// Write file using streaming to avoid Node.js string length limit (~512MB)
665665
// Creating a 600MB string directly would cause "RangeError: Invalid string length"
666-
// even in test code, so we write in smaller chunks instead
667-
const { writeFile } = await import('node:fs/promises');
668-
const chunkSize = 10000; // lines per chunk
669-
let chunk = '';
670-
for (let i = 0; i < chunkSize; i++) {
671-
chunk += sampleEntry;
666+
const writeStream = createWriteStream(filePath);
667+
668+
// Write lines and handle backpressure
669+
for (let i = 0; i < lineCount; i++) {
670+
const canContinue = writeStream.write(sampleEntry);
671+
// Respect backpressure by waiting for drain event
672+
if (!canContinue) {
673+
await new Promise(resolve => writeStream.once('drain', resolve));
674+
}
672675
}
673676

674-
for (let i = 0; i < lineCount; i += chunkSize) {
675-
const currentChunkSize = Math.min(chunkSize, lineCount - i);
676-
const currentChunk = currentChunkSize === chunkSize
677-
? chunk
678-
: sampleEntry.repeat(currentChunkSize);
679-
await writeFile(filePath, currentChunk, { flag: i === 0 ? 'w' : 'a' });
680-
}
677+
// Ensure all data is flushed
678+
await new Promise((resolve, reject) => {
679+
writeStream.end((err?: Error | null) => err ? reject(err) : resolve(undefined));
680+
});
681681

682682
// Test streaming processing
683683
let processedCount = 0;

0 commit comments

Comments
 (0)