-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(FileAdapter): Remove need for atomically. With RWLock we have al…
…l we need. One less dependency for the library.
- Loading branch information
Showing
6 changed files
with
58 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,6 @@ | |
] | ||
}, | ||
"dependencies": { | ||
"atomically": "^1.7.0", | ||
"rwlock": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {IFileAdapter} from "../IAdapter"; | ||
import {readFile, open, FileHandle, mkdir} from "fs/promises"; | ||
import * as path from "path"; | ||
|
||
export class FileAdapter implements IFileAdapter<string> { | ||
public readonly filename: string; | ||
private readonly fsync: boolean; | ||
|
||
constructor(filename: string, fsync: boolean) { | ||
this.filename = filename; | ||
this.fsync = fsync; | ||
} | ||
|
||
async readAsync(): Promise<string | null> { | ||
try { | ||
return await readFile(this.filename, { | ||
encoding: 'utf-8' | ||
}) | ||
} catch (e) { | ||
if ((e as NodeJS.ErrnoException).code === 'ENOENT') { | ||
return null; | ||
} | ||
throw e | ||
} | ||
} | ||
|
||
async writeAsync(data: string): Promise<void> { | ||
let fd: FileHandle | null = null; | ||
try { | ||
fd = await open(this.filename, 'w') | ||
} catch (e) { | ||
if ((e as NodeJS.ErrnoException).code !== 'ENOENT') { | ||
throw e; | ||
} | ||
const basepath = path.dirname(this.filename); | ||
await mkdir(basepath, {recursive: true}); | ||
fd = await open(this.filename, 'w'); | ||
} | ||
try { | ||
await fd.writeFile(data, { | ||
encoding: 'utf-8' | ||
}) | ||
if (this.fsync) { | ||
await fd.sync() | ||
} | ||
} finally { | ||
await fd.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters