@@ -48,106 +48,117 @@ const cache = {
4848 * @async
4949 * @function checkAndUpdateCache
5050 *
51- * @param {Object } highchartsOptions - Object containing `highcharts` options.
52- * @param {Object } serverProxyOptions - Object containing `server.proxy`
53- * options.
51+ * @param {Object } highchartsOptions - The configuration object containing
52+ * `highcharts` options.
53+ * @param {Object } serverProxyOptions- The configuration object containing
54+ * `server.proxy` options.
5455 */
5556export async function checkAndUpdateCache (
5657 highchartsOptions ,
5758 serverProxyOptions
5859) {
59- let fetchedModules ;
60-
61- // Get the cache path
62- const cachePath = getCachePath ( ) ;
63-
64- // Prepare paths to manifest and sources from the cache folder
65- const manifestPath = join ( cachePath , 'manifest.json' ) ;
66- const sourcePath = join ( cachePath , 'sources.js' ) ;
67-
68- // Create the cache destination if it doesn't exist already
69- ! existsSync ( cachePath ) && mkdirSync ( cachePath , { recursive : true } ) ;
70-
71- // Fetch all the scripts either if the `manifest.json` does not exist
72- // or if the `forceFetch` option is enabled
73- if ( ! existsSync ( manifestPath ) || highchartsOptions . forceFetch ) {
74- log ( 3 , '[cache] Fetching and caching Highcharts dependencies.' ) ;
75- fetchedModules = await _updateCache (
76- highchartsOptions ,
77- serverProxyOptions ,
78- sourcePath
79- ) ;
80- } else {
81- let requestUpdate = false ;
60+ try {
61+ let fetchedModules ;
8262
83- // Read the manifest JSON
84- const manifest = JSON . parse ( readFileSync ( manifestPath ) ) ;
63+ // Get the cache path
64+ const cachePath = getCachePath ( ) ;
8565
86- // Check if the modules is an array, if so, we rewrite it to a map to make
87- // it easier to resolve modules.
88- if ( manifest . modules && Array . isArray ( manifest . modules ) ) {
89- const moduleMap = { } ;
90- manifest . modules . forEach ( ( m ) => ( moduleMap [ m ] = 1 ) ) ;
91- manifest . modules = moduleMap ;
92- }
66+ // Prepare paths to manifest and sources from the cache folder
67+ const manifestPath = join ( cachePath , 'manifest.json' ) ;
68+ const sourcePath = join ( cachePath , 'sources.js' ) ;
9369
94- // Get the actual number of scripts to be fetched
95- const { coreScripts, moduleScripts, indicatorScripts } = highchartsOptions ;
96- const numberOfModules =
97- coreScripts . length + moduleScripts . length + indicatorScripts . length ;
98-
99- // Compare the loaded highcharts config with the contents in cache.
100- // If there are changes, fetch requested modules and products,
101- // and bake them into a giant blob. Save the blob.
102- if ( manifest . version !== highchartsOptions . version ) {
103- log (
104- 2 ,
105- '[cache] A Highcharts version mismatch in the cache, need to re-fetch.'
106- ) ;
107- requestUpdate = true ;
108- } else if ( Object . keys ( manifest . modules || { } ) . length !== numberOfModules ) {
109- log (
110- 2 ,
111- '[cache] The cache and the requested modules do not match, need to re-fetch.'
112- ) ;
113- requestUpdate = true ;
114- } else {
115- // Check each module, if anything is missing refetch everything
116- requestUpdate = ( moduleScripts || [ ] ) . some ( ( moduleName ) => {
117- if ( ! manifest . modules [ moduleName ] ) {
118- log (
119- 2 ,
120- `[cache] The ${ moduleName } is missing in the cache, need to re-fetch.`
121- ) ;
122- return true ;
123- }
124- } ) ;
125- }
70+ // Create the cache destination if it doesn't exist already
71+ ! existsSync ( cachePath ) && mkdirSync ( cachePath , { recursive : true } ) ;
12672
127- // Update cache if needed
128- if ( requestUpdate ) {
73+ // Fetch all the scripts either if the `manifest.json` does not exist
74+ // or if the `forceFetch` option is enabled
75+ if ( ! existsSync ( manifestPath ) || highchartsOptions . forceFetch ) {
76+ log ( 3 , '[cache] Fetching and caching Highcharts dependencies.' ) ;
12977 fetchedModules = await _updateCache (
13078 highchartsOptions ,
13179 serverProxyOptions ,
13280 sourcePath
13381 ) ;
13482 } else {
135- log ( 3 , '[cache] Dependency cache is up to date, proceeding.' ) ;
83+ let requestUpdate = false ;
13684
137- // Load the sources
138- cache . sources = readFileSync ( sourcePath , 'utf8' ) ;
85+ // Read the manifest JSON
86+ const manifest = JSON . parse ( readFileSync ( manifestPath ) , 'utf8' ) ;
13987
140- // Get current modules map
141- fetchedModules = manifest . modules ;
88+ // Check if the modules is an array, if so, we rewrite it to a map to make
89+ // it easier to resolve modules.
90+ if ( manifest . modules && Array . isArray ( manifest . modules ) ) {
91+ const moduleMap = { } ;
92+ manifest . modules . forEach ( ( m ) => ( moduleMap [ m ] = 1 ) ) ;
93+ manifest . modules = moduleMap ;
94+ }
95+
96+ // Get the actual number of scripts to be fetched
97+ const { coreScripts, moduleScripts, indicatorScripts } =
98+ highchartsOptions ;
99+ const numberOfModules =
100+ coreScripts . length + moduleScripts . length + indicatorScripts . length ;
101+
102+ // Compare the loaded highcharts config with the contents in cache.
103+ // If there are changes, fetch requested modules and products,
104+ // and bake them into a giant blob. Save the blob.
105+ if ( manifest . version !== highchartsOptions . version ) {
106+ log (
107+ 2 ,
108+ '[cache] A Highcharts version mismatch in the cache, need to re-fetch.'
109+ ) ;
110+ requestUpdate = true ;
111+ } else if (
112+ Object . keys ( manifest . modules || { } ) . length !== numberOfModules
113+ ) {
114+ log (
115+ 2 ,
116+ '[cache] The cache and the requested modules do not match, need to re-fetch.'
117+ ) ;
118+ requestUpdate = true ;
119+ } else {
120+ // Check each module, if anything is missing refetch everything
121+ requestUpdate = ( moduleScripts || [ ] ) . some ( ( moduleName ) => {
122+ if ( ! manifest . modules [ moduleName ] ) {
123+ log (
124+ 2 ,
125+ `[cache] The ${ moduleName } is missing in the cache, need to re-fetch.`
126+ ) ;
127+ return true ;
128+ }
129+ } ) ;
130+ }
131+
132+ // Update cache if needed
133+ if ( requestUpdate ) {
134+ fetchedModules = await _updateCache (
135+ highchartsOptions ,
136+ serverProxyOptions ,
137+ sourcePath
138+ ) ;
139+ } else {
140+ log ( 3 , '[cache] Dependency cache is up to date, proceeding.' ) ;
142141
143- // Extract and save version of currently used Highcharts
144- cache . hcVersion = extractVersion ( cache . sources ) ;
142+ // Load the sources
143+ cache . sources = readFileSync ( sourcePath , 'utf8' ) ;
144+
145+ // Get current modules map
146+ fetchedModules = manifest . modules ;
147+
148+ // Extract and save version of currently used Highcharts
149+ cache . hcVersion = extractVersion ( cache . sources ) ;
150+ }
145151 }
146- }
147152
148- // Finally, save the new manifest, which is basically our current config
149- // in a slightly different format
150- await _saveConfigToManifest ( highchartsOptions , fetchedModules ) ;
153+ // Finally, save the new manifest, which is basically our current config
154+ // in a slightly different format
155+ await _saveConfigToManifest ( highchartsOptions , fetchedModules ) ;
156+ } catch ( error ) {
157+ throw new ExportError (
158+ '[cache] Could not configure cache and create or update the config manifest.' ,
159+ 500
160+ ) . setError ( error ) ;
161+ }
151162}
152163
153164/**
@@ -235,7 +246,7 @@ export function getCache() {
235246 * @returns {string } The absolute path to the cache directory for Highcharts.
236247 */
237248export function getCachePath ( ) {
238- return getAbsolutePath ( getOptions ( ) . highcharts . cachePath ) ; // #562
249+ return getAbsolutePath ( getOptions ( ) . highcharts . cachePath , 'utf8' ) ; // #562
239250}
240251
241252/**
@@ -251,7 +262,7 @@ export function getCachePath() {
251262 * modules have been fetched.
252263 * @param {boolean } [shouldThrowError=false] - A flag to indicate if the error
253264 * should be thrown. This should be used only for the core scripts. The default
254- * value is false.
265+ * value is ` false` .
255266 *
256267 * @returns {Promise<string> } A Promise that resolves to the text representation
257268 * of the fetched script.
@@ -304,7 +315,8 @@ async function _fetchAndProcessScript(
304315 * @async
305316 * @function _saveConfigToManifest
306317 *
307- * @param {Object } highchartsOptions - Object containing `highcharts` options.
318+ * @param {Object } highchartsOptions - The configuration object containing
319+ * `highcharts` options.
308320 * @param {Object } [fetchedModules={}] - An object which tracks which Highcharts
309321 * modules have been fetched. The default value is an empty object.
310322 *
@@ -345,8 +357,8 @@ async function _saveConfigToManifest(highchartsOptions, fetchedModules = {}) {
345357 * @param {Array.<string> } moduleScripts - Highcharts modules to fetch.
346358 * @param {Array.<string> } customScripts - Custom script paths to fetch (full
347359 * URLs).
348- * @param {Object } serverProxyOptions - Object containing `server.proxy`
349- * options.
360+ * @param {Object } serverProxyOptions - The configuration object containing
361+ * `server.proxy` options.
350362 * @param {Object } fetchedModules - An object which tracks which Highcharts
351363 * modules have been fetched.
352364 *
@@ -413,9 +425,10 @@ async function _fetchScripts(
413425 * @async
414426 * @function _updateCache
415427 *
416- * @param {Object } highchartsOptions - Object containing `highcharts` options.
417- * @param {Object } serverProxyOptions - Object containing `server.proxy`
418- * options.
428+ * @param {Object } highchartsOptions - The configuration object containing
429+ * `highcharts` options.
430+ * @param {Object } serverProxyOptions - The configuration object containing
431+ * `server.proxy` options.
419432 * @param {string } sourcePath - The path to the source file in the cache.
420433 *
421434 * @returns {Promise<Object> } A Promise that resolves to an object representing
0 commit comments