@@ -8,6 +8,7 @@ const AKITA_DATA_TYPE = {
8
8
'TIME_SPENT' : 2 ,
9
9
'ORIGIN_FAVICON' : 3
10
10
} ;
11
+ const VALID_AKITA_DATA_TYPE_VALUES = Object . values ( AKITA_DATA_TYPE ) ;
11
12
12
13
const ORIGIN_NAME_LIST_KEY = 'originList' ;
13
14
const ORIGIN_STATS_KEY = 'originStats' ;
@@ -83,7 +84,9 @@ async function storeDataIntoAkitaFormatNonMonetized(data, typeOfData) {
83
84
* @param {Boolean } isMonetizedData Whether or not the data being stored is for a monetized origin.
84
85
*/
85
86
async function updateAkitaData ( originData , data , typeOfData , isMonetizedData ) {
86
- // TODO: ensure typeOfData is one of AKITA_DATA_TYPE
87
+ if ( ! VALID_AKITA_DATA_TYPE_VALUES . includes ( typeOfData ) ) {
88
+ throw "invalid typeOfData passed to updateAkitaData" ;
89
+ }
87
90
88
91
// Get existing originStats or create it if it doesn't already exist
89
92
let originStats = await loadOriginStats ( ) ;
@@ -107,16 +110,48 @@ async function updateAkitaData(originData, data, typeOfData, isMonetizedData) {
107
110
updateTimeSpent ( originData , originStats , data , isMonetizedData ) ;
108
111
break ;
109
112
case AKITA_DATA_TYPE . ORIGIN_FAVICON : // Only for a monetized origin
110
- updateOriginFavicon ( originData , data ) ;
113
+ storeOriginFavicon ( originData . origin , data ) ;
111
114
break ;
112
- default :
113
- // console.log("invalid data type provided");
114
115
}
115
116
116
117
// Overwrite or create origin stats in storage
117
118
await storeOriginStats ( originStats ) ;
118
119
}
119
120
121
+ /**
122
+ * Given an origin and an absolute or relative path to a favicon, this function:
123
+ * - fetches the favicon to check if it is a valid favicon,
124
+ * - if the fetch is successful, the favicon is valid, so the origin's orginData favicon is set
125
+ * to the given favicon path,
126
+ * - if the fetch is unsuccessful, the origin's originData favicon is set to "",
127
+ * - finally, the updated originData is stored.
128
+ *
129
+ * Note: this function's implementation assumes that the specified origin's data has been created
130
+ * and stored prior to the critical section.
131
+ *
132
+ * @param {String } origin The origin to set a favicon for.
133
+ * @param {String } faviconPath The absolute or relative path to the origin's favicon.
134
+ */
135
+ async function storeOriginFavicon ( origin , faviconPath ) {
136
+ const absoluteFaviconPath = pathToAbsolutePath ( faviconPath , origin ) ;
137
+ const isFaviconValid = await isFetchStatusOk ( absoluteFaviconPath ) ;
138
+
139
+ // Once/If the favicon resolves, store the originData with the favicon included
140
+ const resolveLock = await acquireStoreLock ( ) ;
141
+
142
+ // Get and update existing data for this origin
143
+ let originData = await loadOriginData ( origin ) ;
144
+ if ( isFaviconValid ) {
145
+ originData . setOriginFavicon ( absoluteFaviconPath ) ;
146
+ } else {
147
+ originData . setOriginFavicon ( "" ) ;
148
+ }
149
+
150
+ // Update the data for this origin in storage
151
+ await storeOriginData ( origin , originData ) ;
152
+ resolveLock ( ) ;
153
+ }
154
+
120
155
121
156
/***********************************************************
122
157
* Locking Functions
@@ -197,46 +232,44 @@ function updateTimeSpent(originData, originStats, recentTimeSpent, isMonetizedTi
197
232
}
198
233
199
234
/**
200
- * Store the path to the origin's favicon in the origin data. If a relative path
201
- * is provided, construct the absolute path. Attempt to fetch the favicon to check
202
- * if it is a valid path. If fetch response is 200 OK, the path is valid, so store
203
- * the url of the favicon.
235
+ * Makes an absolute path from an input path which is either relative or absolute. If the input path
236
+ * is absolute, the input path is returned unchanged. If the input path is relative, an absolute
237
+ * path on the given origin is made with the input path.
204
238
*
205
- * @param {AkitaOriginData } originData The origin data to update.
206
- * @param {String } faviconData The absolute or relative path to the site's favicon.
239
+ * @param {String } path A path to convert to an absolute path, or check that it is already an absolute path.
240
+ * @param {String } origin If the path is relative, this param is he origin which the path is
241
+ * relative to. Not used if the path is absolute.
242
+ * @returns {String } The input path as an absolute path.
207
243
*/
208
- async function updateOriginFavicon ( originData , faviconData ) {
209
- let faviconPath = null ;
210
- let origin = originData . origin ;
211
-
244
+ function pathToAbsolutePath ( path , origin ) {
212
245
// Regex pattern for the start of the path
213
246
const pathStartPattern = / ^ ( h t t p s ? : \/ \/ ) ( w w w \. ) ? / i; // i = ignore case (case insensitive)
214
247
215
- if ( pathStartPattern . test ( faviconData ) ) {
248
+ if ( pathStartPattern . test ( path ) ) {
216
249
// The favicon path is an absolute path
217
- faviconPath = faviconData ;
250
+ return path ;
218
251
} else {
219
252
// The favicon path is relative to the origin
220
253
if ( ( origin . charAt ( origin . length ) !== '/' )
221
- && ( faviconData . charAt ( 0 ) !== '/' )
254
+ && ( path . charAt ( 0 ) !== '/' )
222
255
) {
223
- faviconPath = origin + "/" + faviconData ;
256
+ return origin + "/" + path ;
224
257
} else {
225
- faviconPath = origin + faviconData ;
258
+ return origin + path ;
226
259
}
227
260
}
261
+ }
228
262
229
- if ( faviconPath ) {
230
- let response = await fetch ( faviconPath ) ;
263
+ /**
264
+ * Fetches a url and resolves to true if the response has status 200 OK.
265
+ *
266
+ * @param {String } url The url to be requested by the browser.
267
+ * @returns {Promise<Boolean> } true if the request response has status 200 OK, false otherwise.
268
+ */
269
+ async function isFetchStatusOk ( url ) {
270
+ let response = await fetch ( url ) ;
231
271
232
- if ( response ) {
233
- if ( 200 === response . status ) {
234
- originData . storeOriginFavicon ( faviconPath ) ;
235
- } else {
236
- originData . storeOriginFavicon ( "" ) ;
237
- }
238
- }
239
- }
272
+ return ( 200 === response ?. status ) ;
240
273
}
241
274
242
275
/***********************************************************
0 commit comments