Skip to content

Instantly share code, notes, and snippets.

@andreban
Created July 4, 2018 12:23
Show Gist options
  • Save andreban/c2f24f93fef4e7554902bd8d2fb0ecfd to your computer and use it in GitHub Desktop.
Save andreban/c2f24f93fef4e7554902bd8d2fb0ecfd to your computer and use it in GitHub Desktop.
Retrieves RuntimeVersion from v0.js
/**
* Copyright 2017 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.
*/
'use strict';
const {OneBehindFetch} = require('amp-toolbox-core');
const V0_URL = 'https://cdn.ampproject.org/v0.js';
const CONFIG_REGEX = /self.AMP_CONFIG=({.+?})/;
/**
* Queries cdn.ampproject.org for the lastest AMP runtime version. Uses a
* stale-while-revalidate caching strategy to avoid refreshing the version.
*/
class RuntimeVersionV0 {
constructor(request = OneBehindFetch.create()) {
this.request_ = request;
}
/**
* Returns the version of the current AMP runtime release.
*
* @returns {Promise<string>} a promise containing the current version
*/
async currentVersion() {
const data = await this.request_.get(V0_URL);
const result = CONFIG_REGEX.exec(data);
if (!result) {
throw new Error('Unable to fidn self.AMP_CONFIG section.');
}
const ampConfig = JSON.parse(result[1]);
return ampConfig.v;
}
}
module.exports = RuntimeVersionV0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment