1
1
import * as fs from "fs" ;
2
2
import { createClient , type RedisClientType } from "redis" ;
3
3
4
- // Storage constantsf
4
+ // Storage constants
5
5
export const WALLET_KEY_PREFIX = "wallet_data:" ;
6
- export const WALLET_STORAGE_DIR = ".data/wallet_data" ;
7
- export const XMTP_STORAGE_DIR = ".data/xmtp" ;
6
+ export const LOCAL_STORAGE_DIR = ".data/wallet_data" ;
8
7
export let redisClient : RedisClientType | null = null ;
9
8
9
+ if ( ! process . env . REDIS_URL ) {
10
+ console . warn (
11
+ "Warning: REDIS_URL not set, using local file storage for wallet data" ,
12
+ ) ;
13
+ }
10
14
/**
11
15
* Initialize Redis client and handle fallback to local storage
12
16
*/
13
17
export async function initializeStorage ( ) {
14
18
if ( process . env . REDIS_URL ) {
15
- redisClient = createClient ( {
16
- url : process . env . REDIS_URL ,
17
- } ) ;
19
+ try {
20
+ redisClient = createClient ( {
21
+ url : process . env . REDIS_URL ,
22
+ } ) ;
18
23
19
- await redisClient . connect ( ) ;
20
- console . log ( "Connected to Redis" ) ;
24
+ await redisClient . connect ( ) ;
25
+ console . log ( "Connected to Redis" ) ;
26
+ } catch ( error : unknown ) {
27
+ console . error ( "Failed to connect to Redis:" , error ) ;
28
+ console . log ( "Falling back to local file storage" ) ;
29
+ redisClient = null ;
30
+ ensureLocalStorage ( ) ;
31
+ }
21
32
} else {
22
33
console . log ( "Using local file storage for wallet data" ) ;
23
34
ensureLocalStorage ( ) ;
@@ -28,8 +39,8 @@ export async function initializeStorage() {
28
39
* Ensure local storage directory exists
29
40
*/
30
41
export function ensureLocalStorage ( ) {
31
- if ( ! fs . existsSync ( WALLET_STORAGE_DIR ) ) {
32
- fs . mkdirSync ( WALLET_STORAGE_DIR , { recursive : true } ) ;
42
+ if ( ! fs . existsSync ( LOCAL_STORAGE_DIR ) ) {
43
+ fs . mkdirSync ( LOCAL_STORAGE_DIR , { recursive : true } ) ;
33
44
}
34
45
}
35
46
@@ -48,7 +59,7 @@ export async function saveWalletData(
48
59
} else {
49
60
// Save to local file
50
61
try {
51
- fs . writeFileSync ( WALLET_STORAGE_DIR + "/" + key + ".json" , walletData ) ;
62
+ fs . writeFileSync ( LOCAL_STORAGE_DIR + "/" + key + ".json" , walletData ) ;
52
63
} catch ( error : unknown ) {
53
64
const errorMessage =
54
65
error instanceof Error ? error . message : String ( error ) ;
@@ -70,8 +81,11 @@ export async function getWalletData(
70
81
return await redisClient . get ( userKey ) ;
71
82
} else {
72
83
try {
73
- if ( fs . existsSync ( WALLET_STORAGE_DIR + "/" + userKey ) ) {
74
- return fs . readFileSync ( WALLET_STORAGE_DIR + "/" + userKey , "utf8" ) ;
84
+ if ( fs . existsSync ( LOCAL_STORAGE_DIR + "/" + userKey + ".json" ) ) {
85
+ return fs . readFileSync (
86
+ LOCAL_STORAGE_DIR + "/" + userKey + ".json" ,
87
+ "utf8" ,
88
+ ) ;
75
89
}
76
90
} catch ( error : unknown ) {
77
91
const errorMessage =
0 commit comments