@ampproject/toolbox-optimizer
Advanced tools
Comparing version 2.8.0-canary.18 to 2.8.0-canary.19
@@ -18,2 +18,3 @@ /** | ||
const URL_BENTO_COMPONENT_INFO = 'https://amp.dev/static/bento-components.json'; | ||
const validatorRulesProvider = require('@ampproject/toolbox-validator-rules'); | ||
@@ -30,3 +31,3 @@ const {MaxAge} = require('@ampproject/toolbox-core'); | ||
const AMP_RUNTIME_MAX_AGE = 10 * 60; // 10 min | ||
const cache = require('./cache.js'); | ||
let cacheErrorLogged = false; | ||
@@ -77,4 +78,33 @@ /** | ||
} | ||
try { | ||
runtimeParameters.bentoComponentInfo = | ||
customRuntimeParameters.bentoComponentInfo || | ||
config.bentoComponentInfo || | ||
(await fetchBentoComponentInfoFromCache_(config)); | ||
} catch (error) { | ||
config.log.error('Could not fetch bento component info'); | ||
config.log.verbose(error); | ||
runtimeParameters.bentoComponentInfo = []; | ||
} | ||
} | ||
async function fetchBentoComponentInfoFromCache_(config) { | ||
let bentoComponentInfo = await readFromCache_(config, 'bento-component-info'); | ||
if (!bentoComponentInfo) { | ||
bentoComponentInfo = await fetchBentoComponentInfo_(config); | ||
writeToCache_(config, 'bento-component-info', bentoComponentInfo); | ||
} | ||
return bentoComponentInfo; | ||
} | ||
async function fetchBentoComponentInfo_(config) { | ||
const response = await config.fetch(URL_BENTO_COMPONENT_INFO); | ||
if (!response.ok) { | ||
config.log.error( | ||
`Failed fetching bento component info from ${URL_BENTO_COMPONENT_INFO} with status: ${response.status}` | ||
); | ||
return []; | ||
} | ||
return response.json(); | ||
} | ||
/** | ||
@@ -84,6 +114,3 @@ * @private | ||
async function fetchValidatorRulesFromCache_(config) { | ||
if (config.cache === false) { | ||
return fetchValidatorRules_(config); | ||
} | ||
let rawRules = await cache.get('validator-rules'); | ||
let rawRules = await readFromCache_(config, 'validator-rules'); | ||
let validatorRules; | ||
@@ -94,3 +121,3 @@ if (!rawRules) { | ||
// We save the raw rules to make the validation rules JSON serializable | ||
cache.set(KEY_VALIDATOR_RULES, validatorRules.raw); | ||
writeToCache_(config, KEY_VALIDATOR_RULES, validatorRules.raw); | ||
} else { | ||
@@ -157,7 +184,4 @@ validatorRules = await validatorRulesProvider.fetch({rules: rawRules}); | ||
async function fetchAmpRuntimeVersion_(context) { | ||
if (context.config.cache === false) { | ||
return (await fetchLatestRuntimeData_(context)).version; | ||
} | ||
const versionKey = context.ampUrlPrefix + '-' + context.lts; | ||
let ampRuntimeData = await cache.get(versionKey); | ||
const versionKey = `version-${context.ampUrlPrefix}-${context.lts}`; | ||
let ampRuntimeData = await readFromCache_(context.config, versionKey); | ||
if (!ampRuntimeData) { | ||
@@ -191,3 +215,3 @@ ampRuntimeData = await fetchLatestRuntimeData_(context, versionKey); | ||
} else if (ampRuntimeData.version && versionKey) { | ||
cache.set(versionKey, ampRuntimeData); | ||
writeToCache_(config, versionKey, ampRuntimeData); | ||
} | ||
@@ -236,3 +260,3 @@ return ampRuntimeData; | ||
if (config.cache !== false) { | ||
styles = await cache.get(runtimeCssUrl); | ||
styles = await readFromCache_(config, runtimeCssUrl); | ||
} | ||
@@ -252,3 +276,3 @@ if (!styles) { | ||
if (config.cache !== false) { | ||
cache.set(runtimeCssUrl, styles); | ||
writeToCache_(config, runtimeCssUrl, styles); | ||
} | ||
@@ -271,2 +295,35 @@ } | ||
/** | ||
* @private | ||
*/ | ||
function readFromCache_(config, key) { | ||
if (config.cache === false) { | ||
return null; | ||
} | ||
try { | ||
return config.cache.get(key); | ||
} catch (e) { | ||
if (!cacheErrorLogged) { | ||
config.log.warn('Could not read from cache', e); | ||
cacheErrorLogged = true; | ||
} | ||
} | ||
} | ||
/** | ||
* @private | ||
*/ | ||
function writeToCache_(config, key, value) { | ||
if (config.cache === false) { | ||
return; | ||
} | ||
try { | ||
config.cache.set(key, value); | ||
} catch (e) { | ||
if (!cacheErrorLogged) { | ||
config.log.warn('Could not write to cache', e); | ||
cacheErrorLogged = true; | ||
} | ||
} | ||
} | ||
module.exports = fetchRuntimeParameters; |
@@ -144,2 +144,8 @@ /** | ||
} | ||
if (!this.bentoComponentInfo) { | ||
this.bentoComponentInfo = {}; | ||
for (const bentoComponent of params.bentoComponentInfo || []) { | ||
this.bentoComponentInfo[bentoComponent.name] = bentoComponent; | ||
} | ||
} | ||
if (!this.extensionSpec_) { | ||
@@ -191,9 +197,3 @@ this.extensionSpec_ = this.createExtensionsSpec(params); | ||
// Use the latest version by default | ||
let version = extension.version[extension.version.length - 1]; | ||
const customVersion = this.extensionVersions[extensionName]; | ||
// Let user override default | ||
if (customVersion) { | ||
this.log_.debug('using custom version for', extensionName, customVersion); | ||
version = customVersion; | ||
} | ||
let version = this.calculateVersion(extension, extensionName); | ||
const extensionImportAttribs = { | ||
@@ -210,2 +210,29 @@ async: '', | ||
calculateVersion(extension, extensionName) { | ||
const customVersion = this.extensionVersions[extensionName]; | ||
// Let user override default | ||
if (customVersion) { | ||
this.log_.debug('using custom version for', extensionName, customVersion); | ||
return customVersion; | ||
} | ||
// Get latest version as specified by validation rules | ||
let version = extension.version[extension.version.length - 1]; | ||
// Get bento component info as these might still be experimental | ||
const bentoComponent = this.bentoComponentInfo[extensionName]; | ||
if (!bentoComponent) { | ||
// No bento component | ||
return version; | ||
} | ||
// Bento component is not experimental, nothing to worry about | ||
if (!bentoComponent.experimental) { | ||
return version; | ||
} | ||
// Bento component version is not yet known to the validator | ||
if (version != bentoComponent.version) { | ||
return version; | ||
} | ||
// Bento component version is not yet valid, pick the previous one | ||
return extension.version[extension.version.length - 2] || '0.1'; | ||
} | ||
/** | ||
@@ -212,0 +239,0 @@ * @private |
{ | ||
"name": "@ampproject/toolbox-optimizer", | ||
"version": "2.8.0-canary.18", | ||
"version": "2.8.0-canary.19", | ||
"description": "Server-side rendering for AMPs.", | ||
@@ -51,3 +51,3 @@ "main": "index.js", | ||
"homepage": "https://github.com/ampproject/amp-toolbox/tree/main/packages/optimizer", | ||
"gitHead": "5b7f8de1e96cc2eba0c9fd1073d4ce1fe3ab5243" | ||
"gitHead": "2c0197ab2ec605ec5046dbfe6f9840d41e38a604" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
234325
49
5594