From 776e61ad47df2eee0452dbeccd7cc6c177fa0e28 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Wed, 22 Oct 2025 22:23:48 +0700 Subject: [PATCH] fix(lib/storage): unintended behaviors - Fixed Storage#delete() always returning false regardless of success - Clone deep default option to prevent unintended behavior after deleting storage --- lib/storage/src/index.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/storage/src/index.ts b/lib/storage/src/index.ts index 826dc27..4fa891c 100644 --- a/lib/storage/src/index.ts +++ b/lib/storage/src/index.ts @@ -1,6 +1,6 @@ import { FileModule } from '@revenge-mod/discord/native' import { getErrorStack } from '@revenge-mod/utils/error' -import { mergeDeep } from '@revenge-mod/utils/object' +import { cloneDeep, mergeDeep } from '@revenge-mod/utils/object' import type { AnyObject, DeepPartial, If } from '@revenge-mod/utils/types' export type StorageSubscription = ( @@ -37,10 +37,14 @@ export function Storage( const subscriptions = new Set>() this.loaded = false - this.cache = options?.default this.exists = () => FileModule.fileExists(fullPath) - this.delete = () => FileModule.removeFile(directory, path) + this.delete = async function () { + await FileModule.removeFile(directory, path) + const success = !(await this.exists()) + if (success) await this.get() + return success + } this.subscribe = callback => { subscriptions.add(callback) @@ -61,7 +65,7 @@ export function Storage( this.get = async function () { if (!(await this.exists())) { - this.cache = options?.default ?? {} + this.cache = cloneDeep(options?.default ?? {}) await write(this) this.loaded = true return this.cache @@ -149,7 +153,7 @@ export interface Storage { */ loaded: boolean /** - * The cached storage object. Set once `get()` is called, or `options.default` is set, and updated on `set()`. + * The cached storage object. Set once `get()` is called, and updated on `set()`. * You should not modify this directly. */ cache?: T | AnyObject