semver.js 4.9 KB

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