Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR extracts fetching and configuration of runtime parameters into a separate function outside the transformers. This externalizes and centralizes runtime parameter handling enabling, removing the responsibility of handling runtime configuration inside transformers (with the potential for duplication) and enabling future optimizations of file system based caching of runtime artifacts (see #650).
- Loading branch information
1 parent
22ce4af
commit c931749
Showing
18 changed files
with
136 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'mode strict'; | ||
|
||
const validatorRules = require('@ampproject/toolbox-validator-rules'); | ||
const {AMP_CACHE_HOST, AMP_RUNTIME_CSS_PATH, appendRuntimeVersion} = require('./AmpConstants.js'); | ||
|
||
/** | ||
* Creates the runtime parameters used by the transformers. Runtime parameters might | ||
* change at runtime with new AMP releases. | ||
* | ||
* @param {Object} config - the AMP Optimizer config | ||
* @param {Object} customRuntimeParameters - user defined runtime parameters | ||
*/ | ||
async function fetchRuntimeParameters(config, customRuntimeParameters) { | ||
const runtimeParameters = Object.assign({}, customRuntimeParameters); | ||
// Configure the log level | ||
runtimeParameters.verbose = customRuntimeParameters.verbose || config.verbose || false; | ||
// Copy lts and rtv runtime flag from custom parameters or the static config. Both are disabled by default. | ||
runtimeParameters.lts = customRuntimeParameters.lts || config.lts || false; | ||
runtimeParameters.rtv = customRuntimeParameters.rtv || config.rtv || false; | ||
// Fetch the validator rules | ||
runtimeParameters.validatorRules = config.validatorRules || (await validatorRules.fetch()); | ||
let {ampUrlPrefix, ampRuntimeVersion, ampRuntimeStyles, lts} = runtimeParameters; | ||
// Use existing runtime version or fetch lts or latest | ||
runtimeParameters.ampRuntimeVersion = | ||
ampRuntimeVersion || (await config.runtimeVersion.currentVersion({ampUrlPrefix, lts})); | ||
// Fetch runtime styles based on the runtime version | ||
runtimeParameters.ampRuntimeStyles = | ||
ampRuntimeStyles || | ||
(await fetchAmpRuntimeStyles_(config, ampUrlPrefix, runtimeParameters.ampRuntimeVersion)); | ||
return runtimeParameters; | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
async function fetchAmpRuntimeStyles_(config, ampUrlPrefix, ampRuntimeVersion) { | ||
if (ampUrlPrefix && !_isAbsoluteUrl(ampUrlPrefix)) { | ||
config.log.warn( | ||
`AMP runtime styles cannot be fetched from relative ampUrlPrefix, please use the 'ampRuntimeStyles' parameter to provide the correct runtime style.` | ||
); | ||
// Gracefully fallback to latest runtime version | ||
ampUrlPrefix = AMP_CACHE_HOST; | ||
ampRuntimeVersion = ampRuntimeVersion || (await config.runtimeVersion.currentVersion()); | ||
} | ||
// Construct the AMP runtime CSS download URL, the default is: https://cdn.ampproject.org/rtv/${ampRuntimeVersion}/v0.css | ||
const runtimeCssUrl = | ||
appendRuntimeVersion(ampUrlPrefix || AMP_CACHE_HOST, ampRuntimeVersion) + AMP_RUNTIME_CSS_PATH; | ||
// Fetch runtime styles | ||
const response = await config.fetch(runtimeCssUrl); | ||
if (!response.ok) { | ||
config.log.error( | ||
`Could not fetch ${runtimeCssUrl}, failed with status ` + `${response.status}.` | ||
); | ||
if (ampUrlPrefix || ampRuntimeVersion) { | ||
// Try to download latest from cdn.ampproject.org instead | ||
return fetchAmpRuntimeStyles_(AMP_CACHE_HOST, await config.runtimeVersion.currentVersion()); | ||
} else { | ||
return ''; | ||
} | ||
} | ||
return response.text(); | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
function _isAbsoluteUrl(url) { | ||
try { | ||
new URL(url); | ||
return true; | ||
} catch (ex) {} | ||
|
||
return false; | ||
} | ||
|
||
module.exports = fetchRuntimeParameters; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
...sformers/experimental/AmpBoilerplateTransformer/supports_custom_host/expected_output.html
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
.../spec/transformers/experimental/AmpBoilerplateTransformer/supports_custom_host/input.html
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
...pec/transformers/experimental/AmpBoilerplateTransformer/supports_lts/expected_output.html
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
...ptimizer/spec/transformers/experimental/AmpBoilerplateTransformer/supports_lts/input.html
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
...rs/experimental/AmpBoilerplateTransformer/uses_given_runtime_version/expected_output.html
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
...transformers/experimental/AmpBoilerplateTransformer/uses_given_runtime_version/input.html
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...s/optimizer/spec/transformers/experimental/RewriteAmpUrls/adds_runtime_version/input.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<!-- | ||
{ | ||
"rtv": true, | ||
"ampRuntimeVersion": "001515617716922" | ||
} | ||
--> | ||
|
1 change: 1 addition & 0 deletions
1
...r/spec/transformers/experimental/RewriteAmpUrls/rewrites_host_and_adds_version/input.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
<!-- | ||
{ | ||
"rtv": true, | ||
"ampUrlPrefix": "/amp", | ||
"ampRuntimeVersion": "001515617716922" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.