Skip to content

Commit

Permalink
fix: Re-add atomic fs
Browse files Browse the repository at this point in the history
Signed-off-by: Reece Dunham <[email protected]>
  • Loading branch information
RDIL committed Oct 13, 2024
1 parent 8730d59 commit 6fdc048
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
15 changes: 10 additions & 5 deletions components/databaseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ import type { ContractSession, GameVersion, UserProfile } from "./types/types"
import { deserializeSession, serializeSession } from "./contracts/sessions"
import { castUserProfile } from "./utils"
import { log, LogLevel } from "./loggingInterop"
import { mkdir, readdir, readFile, unlink, writeFile } from "fs/promises"
import { mkdir, readdir, unlink } from "fs/promises"
import type * as nodeFs from "node:fs/promises"
import * as atomically from "atomically"
import { existsSync } from "fs"

// unlink, mkdir, readdir from node:fs/promises
type NodeUnlinkMkdirReaddir = Pick<
typeof nodeFs,
"unlink" | "mkdir" | "readdir" | "writeFile" | "readFile"
"unlink" | "mkdir" | "readdir"
>

type AtomicReadWrite = Pick<typeof atomically, "readFile" | "writeFile">

// custom exists function because node doesn't have an async version of existsSync
type ExistsPromise = {
exists: (path: string) => Promise<boolean>
Expand All @@ -39,7 +42,9 @@ type ExistsPromise = {
/**
* The fs implementation that this system uses.
*/
export type DataStorageFs = NodeUnlinkMkdirReaddir & ExistsPromise
export type DataStorageFs = NodeUnlinkMkdirReaddir &
ExistsPromise &
AtomicReadWrite

/**
* Handles the dispatching of user data in a way that avoids FS operations unless absolutely needed.
Expand All @@ -65,8 +70,8 @@ class AsyncUserDataGuard {
*/
getFs(): DataStorageFs {
return {
writeFile,
readFile,
writeFile: atomically.writeFile,
readFile: atomically.readFile,
unlink,
mkdir,
readdir,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@peacockproject/statemachine-parser": "^6.1.0",
"@yarnpkg/fslib": "^3.1.0",
"@yarnpkg/libzip": "^3.1.0",
"atomically": "^2.0.3",
"axios": "^1.7.4",
"body-parser": "*",
"clipanion": "^4.0.0-rc.3",
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ __metadata:
"@typescript-eslint/parser": "npm:^7.13.0"
"@yarnpkg/fslib": "npm:^3.1.0"
"@yarnpkg/libzip": "npm:^3.1.0"
atomically: "npm:^2.0.3"
axios: "npm:^1.7.4"
body-parser: "npm:*"
clipanion: "npm:^4.0.0-rc.3"
Expand Down Expand Up @@ -1701,6 +1702,16 @@ __metadata:
languageName: node
linkType: hard

"atomically@npm:^2.0.3":
version: 2.0.3
resolution: "atomically@npm:2.0.3"
dependencies:
stubborn-fs: "npm:^1.2.5"
when-exit: "npm:^2.1.1"
checksum: 10/c71cd27688a99199bfb441930ebdea8a289d86f210f59351d872aaaff39f4e29b7dfaf47ab593e444ef1b0e29ee5ea870285cc25a43078040c4f6cfc50bbf19a
languageName: node
linkType: hard

"axios@npm:^1.7.4":
version: 1.7.5
resolution: "axios@npm:1.7.5"
Expand Down Expand Up @@ -5184,6 +5195,13 @@ __metadata:
languageName: node
linkType: hard

"stubborn-fs@npm:^1.2.5":
version: 1.2.5
resolution: "stubborn-fs@npm:1.2.5"
checksum: 10/bd811a7a33f6c7aa2656f41167affd033c8d686eccdd998e8b3b53c0bce0dc78b0e03af97b7fe426196825cd5bc0c649cc3bcc7ef4675b770f3ba47c67463a2e
languageName: node
linkType: hard

"supports-color@npm:^7.1.0":
version: 7.2.0
resolution: "supports-color@npm:7.2.0"
Expand Down Expand Up @@ -5651,6 +5669,13 @@ __metadata:
languageName: node
linkType: hard

"when-exit@npm:^2.1.1":
version: 2.1.3
resolution: "when-exit@npm:2.1.3"
checksum: 10/d4242a15148df89e08e518b4c372580516d45a6ab527f14643a3789c6edb9ccfc788b1d717728c1a33c85dc53badefb511a3ee704ea61c4e86f693f3bf7666f2
languageName: node
linkType: hard

"which@npm:^2.0.1":
version: 2.0.2
resolution: "which@npm:2.0.2"
Expand Down

0 comments on commit 6fdc048

Please sign in to comment.