@intuitionrobotics/thunderstorm
Version:
98 lines • 5.44 kB
JavaScript
;
/*
* Thunderstorm is a full web app framework!
*
* Typescript & Express backend infrastructure that natively runs on firebase function
* Typescript & React frontend infrastructure
*
* Copyright (C) 2020 Intuition Robotics
*
* 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.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ForceUpgrade = void 0;
const ts_common_1 = require("@intuitionrobotics/ts-common");
const HttpServer_1 = require("./server/HttpServer");
const exceptions_1 = require("../exceptions");
const force_upgrade_1 = require("../../shared/force-upgrade");
const Header_PlatformVersion = new HttpServer_1.HeaderKey(force_upgrade_1.HeaderKey_PlatformVersion);
const Header_PlatformName = new HttpServer_1.HeaderKey(force_upgrade_1.HeaderKey_PlatformName);
// const DefaultRegexps: { [k in PlatformName]: string } = {
// chrome: "Chrome/([0-9\.]+)"
// };
class ForceUpgrade_Class extends ts_common_1.Module {
constructor() {
super("ForceUpgrade");
}
compareVersion(request) {
const platformVersion = Header_PlatformVersion.get(request);
const platformName = Header_PlatformName.get(request);
if (!platformName)
throw new exceptions_1.ApiException(500, `Platform name was not specified`);
if (!platformVersion)
throw new exceptions_1.ApiException(500, `Platform version was not specified`);
const platformNameConfig = this.config[platformName];
if (!platformNameConfig || !platformNameConfig.regexp)
return {}; // no info about this platformName
const regex = new RegExp(platformNameConfig.regexp);
const match = platformVersion.match(regex);
if (!match)
throw new ts_common_1.BadImplementationException(`Error extracting version.. \nVersion: '${platformVersion}'\n config: '${(0, ts_common_1.__stringify)(this.config)}'`);
const minimumValidVersion = platformNameConfig.minimumValidVersion;
if (!minimumValidVersion)
return {
upgradeRequired: false
};
const matchGroups = match.groups;
if (!matchGroups)
throw new ts_common_1.BadImplementationException(`If minimumValidVersion is provided ${platformNameConfig.minimumValidVersion}, then groups in regex have to be defined ${(0, ts_common_1.__stringify)(match)}. i.e. "(?<first>[0-9]+).(?<second>[0-9]+).(?<third>[0-9]+)"`);
const minimumVersionMatch = minimumValidVersion.match(regex);
if (!minimumVersionMatch)
throw new ts_common_1.BadImplementationException(`Error extracting minimum valid version. \nVersion: '${minimumValidVersion}'\n config: '${(0, ts_common_1.__stringify)(this.config)}'`);
const versionsGroups = minimumVersionMatch.groups;
if (!versionsGroups)
throw new ts_common_1.BadImplementationException(`If minimumValidVersion is provided ${platformNameConfig.minimumValidVersion}, then groups in regex have to be defined ${(0, ts_common_1.__stringify)(minimumVersionMatch)}. i.e. "(?<first>[0-9]+).(?<second>[0-9]+).(?<third>[0-9]+)"`);
const versions = Object.values(matchGroups);
const minimumVersions = Object.values(versionsGroups);
for (let i = 0; i < versions.length; i++) {
const v = versions[i];
const minV = minimumVersions[i];
if (+v < +minV)
return { upgradeRequired: true };
if (+v > +minV)
return { upgradeRequired: false };
}
return { upgradeRequired: false };
}
assertVersion(request) {
return __awaiter(this, void 0, void 0, function* () {
const upgradeRequired = this.compareVersion(request);
if (upgradeRequired.upgradeRequired)
throw new exceptions_1.ApiException(426, "require upgrade..").setErrorBody({ type: "upgrade-required", body: upgradeRequired });
});
}
}
_a = ForceUpgrade_Class;
ForceUpgrade_Class.Middleware = (request) => __awaiter(void 0, void 0, void 0, function* () { return exports.ForceUpgrade.assertVersion(request); });
exports.ForceUpgrade = new ForceUpgrade_Class();
//# sourceMappingURL=ForceUpgrade.js.map