@@ -62,24 +62,47 @@ async function fetchWithTimeout(url: string, options: RequestInit = {}, timeoutM
62
62
* @returns Phishing configuration object
63
63
*/
64
64
export const fetchPhishingList = async ( forceRefresh = false ) : Promise < any > => {
65
+ let cachedData : any = null ;
66
+ let useCache = false ;
67
+
65
68
// If not forcing refresh, try to use cache first
66
69
if ( ! forceRefresh ) {
67
70
try {
68
- const cachedData = await getFromLocalCache ( ) ;
71
+ cachedData = await getFromLocalCache ( ) ;
69
72
if ( cachedData && cachedData . lastFetchTime ) {
70
73
const cacheAge = Date . now ( ) - cachedData . lastFetchTime ;
71
74
72
- // If cache is fresh enough, use it directly
75
+ // If cache is fresh enough, use it directly (but still fetch UNISAT)
73
76
if ( cacheAge < MIN_CACHE_AGE ) {
74
77
console . log ( '[Phishing] Using recent cache, age:' , Math . round ( cacheAge / 60000 ) , 'minutes' ) ;
75
- return cachedData ;
78
+ useCache = true ;
76
79
}
77
80
}
78
81
} catch ( error ) {
79
82
console . error ( '[Phishing] Cache check failed:' , error ) ;
80
83
}
81
84
}
82
85
86
+ // If using cache and not forcing refresh, return early for most sources
87
+ // But always fetch UNISAT source
88
+ if ( useCache && ! forceRefresh ) {
89
+ // Create a working copy of cached data for UNISAT updates
90
+ const mergedData = JSON . parse ( JSON . stringify ( cachedData ) ) ;
91
+
92
+ // Always fetch UNISAT source
93
+ try {
94
+ await fetchAndMergeUnisat ( mergedData ) ;
95
+
96
+ // Update the cache with the UNISAT updates
97
+ await saveToLocalCache ( mergedData ) ;
98
+ return mergedData ;
99
+ } catch ( error ) {
100
+ console . error ( '[Phishing] UNISAT source fetch failed, using cache only:' , error ) ;
101
+ return cachedData ;
102
+ }
103
+ }
104
+
105
+ // If not using cache, fetch from all sources
83
106
const fetchOptions : RequestInit = {
84
107
cache : 'no-cache' ,
85
108
headers : {
@@ -128,16 +151,10 @@ export const fetchPhishingList = async (forceRefresh = false): Promise<any> => {
128
151
console . error ( '[Phishing] Backup source fetch failed:' , error ) ;
129
152
}
130
153
154
+ // Always try to fetch UNISAT source
131
155
try {
132
- const response = await fetchWithTimeout ( PHISHING_SOURCES . UNISAT , fetchOptions ) ;
133
-
134
- if ( response . ok ) {
135
- const data = await response . json ( ) ;
136
- mergePhishingData ( mergedData , data ) ;
137
- mergedData . sources . push ( 'UNISAT' ) ;
138
- hasAnySourceSucceeded = true ;
139
- console . log ( '[Phishing] Successfully fetched from UNISAT source' ) ;
140
- }
156
+ await fetchAndMergeUnisat ( mergedData ) ;
157
+ hasAnySourceSucceeded = true ;
141
158
} catch ( error ) {
142
159
console . error ( '[Phishing] Unisat source fetch failed:' , error ) ;
143
160
}
@@ -150,7 +167,7 @@ export const fetchPhishingList = async (forceRefresh = false): Promise<any> => {
150
167
151
168
// All remote sources failed, try using cache (regardless of age)
152
169
try {
153
- const cachedData = await getFromLocalCache ( ) ;
170
+ cachedData = await getFromLocalCache ( ) ;
154
171
if ( cachedData ) {
155
172
console . warn ( '[Phishing] Using cached data as all remote sources failed' ) ;
156
173
return cachedData ;
@@ -163,6 +180,38 @@ export const fetchPhishingList = async (forceRefresh = false): Promise<any> => {
163
180
throw new Error ( 'Failed to fetch phishing list from all available sources' ) ;
164
181
} ;
165
182
183
+ /**
184
+ * Fetches UNISAT phishing list and merges it into target data
185
+ * This function is separated to allow always fetching UNISAT data
186
+ * @param targetData The data object to merge UNISAT data into
187
+ * @returns Promise<boolean> indicating success
188
+ */
189
+ async function fetchAndMergeUnisat ( targetData : any ) : Promise < boolean > {
190
+ const fetchOptions : RequestInit = {
191
+ cache : 'no-cache' ,
192
+ headers : {
193
+ Accept : 'application/json'
194
+ }
195
+ } ;
196
+
197
+ const response = await fetchWithTimeout ( PHISHING_SOURCES . UNISAT , fetchOptions ) ;
198
+
199
+ if ( response . ok ) {
200
+ const data = await response . json ( ) ;
201
+ mergePhishingData ( targetData , data ) ;
202
+
203
+ // Only add to sources if not already there
204
+ if ( ! targetData . sources . includes ( 'UNISAT' ) ) {
205
+ targetData . sources . push ( 'UNISAT' ) ;
206
+ }
207
+
208
+ console . log ( '[Phishing] Successfully fetched from UNISAT source' ) ;
209
+ return true ;
210
+ }
211
+
212
+ throw new Error ( 'Failed to fetch from UNISAT source' ) ;
213
+ }
214
+
166
215
function mergePhishingData ( target : any , source : any ) : void {
167
216
if ( Array . isArray ( source . blacklist ) ) {
168
217
target . blacklist = [ ...new Set ( [ ...target . blacklist , ...source . blacklist ] ) ] ;
0 commit comments