Skip to content

Commit cb766a7

Browse files
committed
refactor: wip
1 parent b6f2bb6 commit cb766a7

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

packages/utils/src/lib/wal.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export class WriteAheadLogFile<T> implements AppendableSink<T> {
158158
if (this.#fd) {
159159
return;
160160
}
161-
fs.mkdirSync(path.dirname(this.#file), { recursive: true });
161+
ensureDirectoryExistsSync(path.dirname(this.#file));
162162
this.#fd = fs.openSync(this.#file, 'a');
163163
};
164164

@@ -229,7 +229,7 @@ export class WriteAheadLogFile<T> implements AppendableSink<T> {
229229
const recordsToWrite = hasInvalidEntries
230230
? (r.records as T[])
231231
: filterValidRecords(r.records);
232-
fs.mkdirSync(path.dirname(out), { recursive: true });
232+
ensureDirectoryExistsSync(path.dirname(out));
233233
fs.writeFileSync(out, `${recordsToWrite.map(this.#encode).join('\n')}\n`);
234234
}
235235

@@ -410,6 +410,16 @@ export function sortableReadableDateString(timestampMs: string): string {
410410
return `${yyyy}${mm}${dd}-${hh}${min}${ss}-${ms}`;
411411
}
412412

413+
/**
414+
* Ensures a directory exists, creating it recursively if necessary using sync methods.
415+
* @param dirPath - The directory path to ensure exists
416+
*/
417+
function ensureDirectoryExistsSync(dirPath: string): void {
418+
if (!fs.existsSync(dirPath)) {
419+
fs.mkdirSync(dirPath, { recursive: true });
420+
}
421+
}
422+
413423
/**
414424
* Generates a path to a shard file using human-readable IDs.
415425
* Both groupId and shardId are already in readable date format.
@@ -497,6 +507,9 @@ export class ShardedWal<T extends object | string = object> {
497507
groupId: this.groupId,
498508
}),
499509
);
510+
// create dir if not existing
511+
ensureDirectoryExistsSync(groupIdDir);
512+
500513
return fs
501514
.readdirSync(groupIdDir)
502515
.filter(entry => entry.endsWith(this.#format.walExtension))
@@ -533,9 +546,7 @@ export class ShardedWal<T extends object | string = object> {
533546
format: this.#format,
534547
groupId: this.groupId,
535548
});
536-
fs.mkdirSync(path.dirname(out), {
537-
recursive: true,
538-
});
549+
ensureDirectoryExistsSync(path.dirname(out));
539550
fs.writeFileSync(out, this.#format.finalizer(recordsToFinalize, opt));
540551
}
541552

0 commit comments

Comments
 (0)