20
20
*/
21
21
22
22
/*jslint regexp: true */
23
- /*global Phoenix */
23
+ /*global path, logger */
24
24
/*unittests: ExtensionManager*/
25
25
26
26
/**
@@ -44,6 +44,7 @@ define(function (require, exports, module) {
44
44
ExtensionLoader = require ( "utils/ExtensionLoader" ) ,
45
45
ExtensionUtils = require ( "utils/ExtensionUtils" ) ,
46
46
FileSystem = require ( "filesystem/FileSystem" ) ,
47
+ FileUtils = require ( "file/FileUtils" ) ,
47
48
PreferencesManager = require ( "preferences/PreferencesManager" ) ,
48
49
Strings = require ( "strings" ) ,
49
50
StringUtils = require ( "utils/StringUtils" ) ,
@@ -54,6 +55,40 @@ define(function (require, exports, module) {
54
55
"test_extension_registry" : "extension_registry" ,
55
56
EXTENSION_REGISTRY_LOCAL_STORAGE_VERSION_KEY = Phoenix . isTestWindow ?
56
57
"test_extension_registry_version" : "extension_registry_version" ;
58
+
59
+ // earlier, we used to cache the full uncompressed registry in ls which has a usual size limit of 5mb, and the
60
+ // registry takes a few MB. So we moved this storage and this will clear local storage on any existing installs on
61
+ // next update. This migration code can be removed after July 2025(6 Months).
62
+ localStorage . removeItem ( EXTENSION_REGISTRY_LOCAL_STORAGE_KEY ) ;
63
+
64
+ const REGISTRY_CACHE_PATH = path . normalize (
65
+ Phoenix . app . getExtensionsDirectory ( ) + "/" + "registry_cache.json" ) ;
66
+ function _getCachedRegistry ( ) {
67
+ // never rejects
68
+ return new Promise ( ( resolve ) => {
69
+ const registryFile = FileSystem . getFileForPath ( REGISTRY_CACHE_PATH ) ;
70
+ FileUtils . readAsText ( registryFile )
71
+ . done ( resolve )
72
+ . fail ( function ( err ) {
73
+ console . error ( `Registry cache not found ${ REGISTRY_CACHE_PATH } ` , err ) ;
74
+ resolve ( null ) ;
75
+ } ) ;
76
+ } ) ;
77
+ }
78
+
79
+ function _putCachedRegistry ( registryFileText ) {
80
+ // never rejects
81
+ return new Promise ( ( resolve ) => {
82
+ const registryFile = FileSystem . getFileForPath ( REGISTRY_CACHE_PATH ) ;
83
+ FileUtils . writeText ( registryFile , registryFileText )
84
+ . done ( resolve )
85
+ . fail ( function ( err ) {
86
+ logger . reportError ( err , `Registry cache write error ${ REGISTRY_CACHE_PATH } ` ) ;
87
+ resolve ( ) ;
88
+ } ) ;
89
+ } ) ;
90
+ }
91
+
57
92
// semver.browser is an AMD-compatible module
58
93
var semver = require ( "thirdparty/semver.browser" ) ;
59
94
@@ -223,38 +258,42 @@ define(function (require, exports, module) {
223
258
if ( registryVersion . version !== parseInt ( currentRegistryVersion ) ) {
224
259
resolve ( registryVersion . version ) ;
225
260
} else {
226
- const registryJson = localStorage . getItem ( EXTENSION_REGISTRY_LOCAL_STORAGE_KEY ) ;
227
- if ( ! registryJson ) {
228
- resolve ( registryVersion . version ) ;
229
- // if we dont have anything, best to atlest try to fetch the registry now.
230
- return ;
231
- }
232
- reject ( ) ;
261
+ _getCachedRegistry ( ) // never rejects
262
+ . then ( registryJson => {
263
+ if ( ! registryJson ) {
264
+ resolve ( registryVersion . version ) ;
265
+ // if we dont have anything, best to atlest try to fetch the registry now.
266
+ return ;
267
+ }
268
+ reject ( ) ;
269
+ } ) ;
233
270
}
234
271
} )
235
272
. fail ( function ( err ) {
236
273
console . error ( "error Fetching Extension Registry version" , err ) ;
237
- const registryJson = localStorage . getItem ( EXTENSION_REGISTRY_LOCAL_STORAGE_KEY ) ;
238
- if ( ! registryJson ) {
239
- resolve ( 1 ) ; // if we dont have anything, best to atlest try to fetch the registry now.
240
- return ;
241
- }
242
- reject ( ) ;
274
+ _getCachedRegistry ( ) // never rejects
275
+ . then ( registryJson => {
276
+ if ( ! registryJson ) {
277
+ resolve ( 1 ) ; // if we dont have anything, best to atlest try to fetch the registry now.
278
+ return ;
279
+ }
280
+ reject ( ) ;
281
+ } ) ;
243
282
} ) ;
244
283
} ) ;
245
284
}
246
285
247
- function _patchDownloadCounts ( ) {
248
- let registryJson = localStorage . getItem ( EXTENSION_REGISTRY_LOCAL_STORAGE_KEY ) ;
286
+ async function _patchDownloadCounts ( ) {
287
+ let registryJson = await _getCachedRegistry ( ) ;
249
288
if ( ! registryJson ) {
250
289
return ;
251
290
}
252
291
$ . ajax ( {
253
292
url : brackets . config . extension_registry_popularity ,
254
293
dataType : "json" ,
255
294
cache : false
256
- } ) . done ( function ( popularity ) {
257
- registryJson = localStorage . getItem ( EXTENSION_REGISTRY_LOCAL_STORAGE_KEY ) ;
295
+ } ) . done ( async function ( popularity ) {
296
+ registryJson = await _getCachedRegistry ( ) ;
258
297
let registry = JSON . parse ( registryJson ) ;
259
298
for ( let key of Object . keys ( popularity ) ) {
260
299
if ( registry [ key ] ) {
@@ -264,7 +303,7 @@ define(function (require, exports, module) {
264
303
|| null ;
265
304
}
266
305
}
267
- localStorage . setItem ( EXTENSION_REGISTRY_LOCAL_STORAGE_KEY , JSON . stringify ( registry ) ) ;
306
+ _putCachedRegistry ( JSON . stringify ( registry ) ) ;
268
307
} ) ;
269
308
}
270
309
@@ -308,6 +347,7 @@ define(function (require, exports, module) {
308
347
pendingDownloadRegistry = new $ . Deferred ( ) ;
309
348
310
349
function _updateRegistry ( newVersion ) {
350
+ console . log ( "downloading extension registry: " , newVersion , brackets . config . extension_registry ) ;
311
351
$ . ajax ( {
312
352
url : brackets . config . extension_registry ,
313
353
dataType : "json" ,
@@ -316,20 +356,20 @@ define(function (require, exports, module) {
316
356
. done ( function ( registry ) {
317
357
registry = _filterIncompatibleEntries ( registry ) ;
318
358
localStorage . setItem ( EXTENSION_REGISTRY_LOCAL_STORAGE_VERSION_KEY , newVersion ) ;
319
- localStorage . setItem ( EXTENSION_REGISTRY_LOCAL_STORAGE_KEY , JSON . stringify ( registry ) ) ;
320
- if ( ! pendingDownloadRegistry . alreadyResolvedFromCache ) {
321
- _populateExtensions ( registry ) ;
322
- pendingDownloadRegistry . resolve ( ) ;
323
- }
359
+ _putCachedRegistry ( JSON . stringify ( registry ) ) . then ( ( ) => {
360
+ if ( ! pendingDownloadRegistry . alreadyResolvedFromCache ) {
361
+ _populateExtensions ( registry ) ;
362
+ pendingDownloadRegistry . resolve ( ) ;
363
+ }
364
+ } ) . finally ( ( ) => {
365
+ pendingDownloadRegistry = null ;
366
+ } ) ;
324
367
} )
325
368
. fail ( function ( err ) {
326
369
console . error ( "error Fetching Extension Registry" , err ) ;
327
370
if ( ! pendingDownloadRegistry . alreadyResolvedFromCache ) {
328
371
pendingDownloadRegistry . reject ( ) ;
329
372
}
330
- } )
331
- . always ( function ( ) {
332
- // Make sure to clean up the pending registry so that new requests can be made.
333
373
pendingDownloadRegistry = null ;
334
374
} ) ;
335
375
}
@@ -339,26 +379,28 @@ define(function (require, exports, module) {
339
379
return pendingDownloadRegistry . promise ( ) ;
340
380
}
341
381
342
- const registryJson = localStorage . getItem ( EXTENSION_REGISTRY_LOCAL_STORAGE_KEY ) ;
343
- if ( registryJson ) {
344
- // we always immediately but after the promise chain is setup after function return (some bug sigh)
345
- // resolve for ui responsiveness and then check for updates.
346
- setTimeout ( ( ) => {
347
- Metrics . countEvent ( Metrics . EVENT_TYPE . EXTENSIONS , "registry" , "cachedUse" ) ;
348
- let registry = JSON . parse ( registryJson ) ;
349
- registry = _filterIncompatibleEntries ( registry ) ;
350
- _populateExtensions ( registry ) ;
351
- pendingDownloadRegistry . resolve ( ) ;
352
- } , 0 ) ;
353
- pendingDownloadRegistry . alreadyResolvedFromCache = true ;
354
- }
355
- // check for latest updates even if we have cache
356
- _shouldUpdateExtensionRegistry ( )
357
- . then ( _updateRegistry )
358
- . catch ( ( ) => {
359
- pendingDownloadRegistry = null ;
382
+ _getCachedRegistry ( ) // never rejects
383
+ . then ( registryJson => {
384
+ if ( registryJson ) {
385
+ // we always immediately but after the promise chain is setup after function return (some bug sigh)
386
+ // resolve for ui responsiveness and then check for updates.
387
+ setTimeout ( ( ) => {
388
+ Metrics . countEvent ( Metrics . EVENT_TYPE . EXTENSIONS , "registry" , "cachedUse" ) ;
389
+ let registry = JSON . parse ( registryJson ) ;
390
+ registry = _filterIncompatibleEntries ( registry ) ;
391
+ _populateExtensions ( registry ) ;
392
+ pendingDownloadRegistry . resolve ( ) ;
393
+ } , 0 ) ;
394
+ pendingDownloadRegistry . alreadyResolvedFromCache = true ;
395
+ }
396
+ // check for latest updates even if we have cache
397
+ _shouldUpdateExtensionRegistry ( )
398
+ . then ( _updateRegistry )
399
+ . catch ( ( ) => {
400
+ console . log ( "Registry update skipped" ) ;
401
+ } ) ;
402
+ _patchDownloadCounts ( ) ;
360
403
} ) ;
361
- _patchDownloadCounts ( ) ;
362
404
363
405
return pendingDownloadRegistry . promise ( ) ;
364
406
}
0 commit comments