Skip to content

Commit

Permalink
fix: file writer
Browse files Browse the repository at this point in the history
  • Loading branch information
shahradelahi committed May 21, 2024
1 parent 643c24a commit 2ae88a2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storage-box",
"version": "1.0.0-canary.0",
"version": "1.0.0-canary.2",
"description": "A memory-based key–value storage for javascript.",
"type": "module",
"main": "dist/index.cjs",
Expand Down
12 changes: 8 additions & 4 deletions src/utils/file-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { rename, writeFile } from 'node:fs/promises';
import { basename, dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

import { access } from '@/utils/fs-extra';

// Returns a temporary file
// Example: for /some/file will return /some/.file.tmp
function getTempFilename(file: PathLike): string {
Expand Down Expand Up @@ -44,9 +46,9 @@ interface FileWriterOptions {
}

export class FileWriter {
#filename: PathLike;
#tempFilename: PathLike;
#encoding: BufferEncoding = 'utf-8';
readonly #filename: PathLike;
readonly #tempFilename: PathLike;
readonly #encoding: BufferEncoding = 'utf-8';
#locked = false;
#prev: [Resolve, Reject] | null = null;
#next: [Resolve, Reject] | null = null;
Expand Down Expand Up @@ -78,7 +80,9 @@ export class FileWriter {
await writeFile(this.#tempFilename, data, this.#encoding);
await retryAsyncOperation(
async () => {
await rename(this.#tempFilename, this.#filename);
if (await access(this.#tempFilename)) {
await rename(this.#tempFilename, this.#filename);
}
},
10,
100
Expand Down

0 comments on commit 2ae88a2

Please sign in to comment.