From eeb1c5bde3ce1ddece517ac22abbab84edef31da Mon Sep 17 00:00:00 2001 From: Henrik Fehlauer Date: Thu, 6 Jul 2023 09:46:47 +0000 Subject: [PATCH] Emit console warning when using an unsupported BRouter version abrensch/brouter@c9ae7c8681 changed indexing of voice hint ids, because some new hints were inserted in the middle instead of strictly at the end, changing the numbering of existing ids. For example, now id `12` was sent to indicate a right u-turn, while we still assume the old meaning of `12`, i.e. "off route". This clearly was an API break. This leads us to abort exporting with the Gpsies turn instructions style, since `OFFR` has an `undefined` symbol assigned, as well as emitting wrong voice hints for ids after `9`. Another unwelcome side effect is showing negative exit numbers for roundabouts. This breakage in the GeoJSON HTTP API has been shipping in BRouter 1.7.0 and 1.7.1 and finally got fixed with abrensch/brouter@82fecf9 contained in BRouter 1.7.2 or later. Earlier releases like 1.6.3 are also unaffected. To avoid emitting incorrect voice hints in BRouter-Web, running with broken versions of BRouter should be avoided. By checking the "Creator" field after receiving the first response from BRouter, we can now emit a warning if the version of BRouter used is unsupported. The warning mostly targets administrators and power users, i.e. those responsible for choosing the software versions used, and it is also only shown once per session. Note that the version check is compatible with the common "SemVer" scheme, so the check should continue working and even support more complex version compatibility scenarios as long as BRouter stays SemVer-compliant. Ref #751 Test Plan: - Run with BRouter 1.6.3 and 1.7.2, no warnings shown. - Run with BRouter 1.7.0 and 1.7.1, warnings shown only for the first segment. --- README.md | 2 ++ js/router/BRouter.js | 27 +++++++++++++++++++++++++++ package.json | 6 ++++++ yarn.lock | 5 +++++ 4 files changed, 40 insertions(+) diff --git a/README.md b/README.md index fe49bf0..987f37b 100644 --- a/README.md +++ b/README.md @@ -148,3 +148,5 @@ Copyright (c) 2018 Norbert Renner and [contributors](https://github.com/nrenner/ Copyright (c) 2012 davidshimjs [The MIT License](https://github.com/llyys/qrcodejs/blob/master/LICENSE) - [Bootstrap Icons](https://github.com/twbs/icons) Copyright (c) 2019-2023 The Bootstrap Authors [The MIT License](https://github.com/twbs/icons/blob/main/LICENSE) +- [compare-versions](https://github.com/omichelsen/compare-versions) + Copyright (c) 2015-2021 Ole Michelsen [The MIT License](https://github.com/omichelsen/compare-versions/blob/main/LICENSE) diff --git a/js/router/BRouter.js b/js/router/BRouter.js index 7b191f4..ccdf2be 100644 --- a/js/router/BRouter.js +++ b/js/router/BRouter.js @@ -10,6 +10,7 @@ L.BRouter = L.Class.extend({ GROUP_SEPARATOR: '|', ABORTED_ERROR: 'aborted', CUSTOM_PREFIX: 'custom_', + SUPPORTED_BROUTER_VERSIONS: '< 1.7.0 || >=1.7.2', // compatibility string should be in npm package versioning format isCustomProfile: function (profileName) { return profileName && profileName.substring(0, 7) === L.BRouter.CUSTOM_PREFIX; }, @@ -178,6 +179,7 @@ L.BRouter = L.Class.extend({ try { geojson = JSON.parse(xhr.responseText); layer = this._assignFeatures(L.geoJSON(geojson).getLayers()[0]); + this.checkBRouterVersion(layer.feature.properties.creator); return cb(null, layer); } catch (e) { @@ -189,6 +191,31 @@ L.BRouter = L.Class.extend({ } }, + versionCheckDone: false, + checkBRouterVersion: function (creator) { + if (this.versionCheckDone) { + return; + } + this.versionCheckDone = true; + + try { + const actualBRouterVersion = creator.replace(/^BRouter-/, ''); + if (!compareVersions.satisfies(actualBRouterVersion, L.BRouter.SUPPORTED_BROUTER_VERSIONS)) { + console.warn( + 'BRouter-Web ' + + BR.version + + ' requires BRouter versions ' + + L.BRouter.SUPPORTED_BROUTER_VERSIONS + + ', but only ' + + creator + + ' was found.' + ); + } + } catch (e) { + console.error(e); + } + }, + getRouteSegment: function (l1, l2, cb) { this.queue.push({ segment: [l1, l2] }, cb); }, diff --git a/package.json b/package.json index f61a047..2383ab6 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "bootstrap-select": "1.13.18", "bootstrap-slider": "11.0.2", "codemirror": "5.65.13", + "compare-versions": "^6.0.0", "core-js-bundle": "3.29.1", "datatables": "1.10.18", "fit-file-writer": "tbsmark86/fit-file-writer#3eebe13", @@ -309,6 +310,11 @@ "index.js" ] }, + "compare-versions": { + "main": [ + "lib/umd/index.js" + ] + }, "@mapbox/maki": { "main": [ "icons/art-gallery.svg", diff --git a/yarn.lock b/yarn.lock index 564d8f6..13d0316 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4197,6 +4197,11 @@ commander@^9.0.0: resolved "https://registry.yarnpkg.com/commander/-/commander-9.5.0.tgz#bc08d1eb5cedf7ccb797a96199d41c7bc3e60d30" integrity sha512-KRs7WVDKg86PWiuAqhDrAQnTXZKraVcCc6vFdL14qrZ/DcWwuRo7VoiYXalXO7S5GKpqYiVEwCbgFDfxNHKJBQ== +compare-versions@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-6.0.0.tgz#a3edb527e4487bfab9a8b62ffe70cebc9b87675b" + integrity sha512-s2MzYxfRsE9f/ow8hjn7ysa7pod1xhHdQMsgiJtKx6XSNf4x2N1KG4fjrkUmXcP/e9Y2ZX4zB6sHIso0Lm6evQ== + component-emitter@^1.2.1: version "1.3.0" resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0"