UNPKG

5.21 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright The OpenTelemetry Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.isCompatible = exports._makeCompatibilityCheck = void 0;
19var version_1 = require("../version");
20var re = /^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;
21/**
22 * Create a function to test an API version to see if it is compatible with the provided ownVersion.
23 *
24 * The returned function has the following semantics:
25 * - Exact match is always compatible
26 * - Major versions must match exactly
27 * - 1.x package cannot use global 2.x package
28 * - 2.x package cannot use global 1.x package
29 * - The minor version of the API module requesting access to the global API must be less than or equal to the minor version of this API
30 * - 1.3 package may use 1.4 global because the later global contains all functions 1.3 expects
31 * - 1.4 package may NOT use 1.3 global because it may try to call functions which don't exist on 1.3
32 * - If the major version is 0, the minor version is treated as the major and the patch is treated as the minor
33 * - Patch and build tag differences are not considered at this time
34 *
35 * @param ownVersion version which should be checked against
36 */
37function _makeCompatibilityCheck(ownVersion) {
38 var acceptedVersions = new Set([ownVersion]);
39 var rejectedVersions = new Set();
40 var myVersionMatch = ownVersion.match(re);
41 if (!myVersionMatch) {
42 // we cannot guarantee compatibility so we always return noop
43 return function () { return false; };
44 }
45 var ownVersionParsed = {
46 major: +myVersionMatch[1],
47 minor: +myVersionMatch[2],
48 patch: +myVersionMatch[3],
49 prerelease: myVersionMatch[4],
50 };
51 // if ownVersion has a prerelease tag, versions must match exactly
52 if (ownVersionParsed.prerelease != null) {
53 return function isExactmatch(globalVersion) {
54 return globalVersion === ownVersion;
55 };
56 }
57 function _reject(v) {
58 rejectedVersions.add(v);
59 return false;
60 }
61 function _accept(v) {
62 acceptedVersions.add(v);
63 return true;
64 }
65 return function isCompatible(globalVersion) {
66 if (acceptedVersions.has(globalVersion)) {
67 return true;
68 }
69 if (rejectedVersions.has(globalVersion)) {
70 return false;
71 }
72 var globalVersionMatch = globalVersion.match(re);
73 if (!globalVersionMatch) {
74 // cannot parse other version
75 // we cannot guarantee compatibility so we always noop
76 return _reject(globalVersion);
77 }
78 var globalVersionParsed = {
79 major: +globalVersionMatch[1],
80 minor: +globalVersionMatch[2],
81 patch: +globalVersionMatch[3],
82 prerelease: globalVersionMatch[4],
83 };
84 // if globalVersion has a prerelease tag, versions must match exactly
85 if (globalVersionParsed.prerelease != null) {
86 return _reject(globalVersion);
87 }
88 // major versions must match
89 if (ownVersionParsed.major !== globalVersionParsed.major) {
90 return _reject(globalVersion);
91 }
92 if (ownVersionParsed.major === 0) {
93 if (ownVersionParsed.minor === globalVersionParsed.minor &&
94 ownVersionParsed.patch <= globalVersionParsed.patch) {
95 return _accept(globalVersion);
96 }
97 return _reject(globalVersion);
98 }
99 if (ownVersionParsed.minor <= globalVersionParsed.minor) {
100 return _accept(globalVersion);
101 }
102 return _reject(globalVersion);
103 };
104}
105exports._makeCompatibilityCheck = _makeCompatibilityCheck;
106/**
107 * Test an API version to see if it is compatible with this API.
108 *
109 * - Exact match is always compatible
110 * - Major versions must match exactly
111 * - 1.x package cannot use global 2.x package
112 * - 2.x package cannot use global 1.x package
113 * - The minor version of the API module requesting access to the global API must be less than or equal to the minor version of this API
114 * - 1.3 package may use 1.4 global because the later global contains all functions 1.3 expects
115 * - 1.4 package may NOT use 1.3 global because it may try to call functions which don't exist on 1.3
116 * - If the major version is 0, the minor version is treated as the major and the patch is treated as the minor
117 * - Patch and build tag differences are not considered at this time
118 *
119 * @param version version of the API requesting an instance of the global API
120 */
121exports.isCompatible = _makeCompatibilityCheck(version_1.VERSION);
122//# sourceMappingURL=semver.js.map
\No newline at end of file