index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. var semver = require('semver')
  2. function getNextTarget (runtime, targets) {
  3. if (targets == null) targets = allTargets
  4. var latest = targets.filter(function (t) { return t.runtime === runtime }).slice(-1)[0]
  5. var increment = runtime === 'electron' ? 'minor' : 'major'
  6. var next = semver.inc(latest.target, increment)
  7. // Electron releases appear in the registry in their beta form, sometimes there is
  8. // no active beta line. During this time we need to double bump
  9. if (runtime === 'electron' && semver.parse(latest.target).prerelease.length) {
  10. next = semver.inc(next, 'major')
  11. }
  12. return next
  13. }
  14. function getAbi (target, runtime) {
  15. if (target === String(Number(target))) return target
  16. if (target) target = target.replace(/^v/, '')
  17. if (!runtime) runtime = 'node'
  18. if (runtime === 'node') {
  19. if (!target) return process.versions.modules
  20. if (target === process.versions.node) return process.versions.modules
  21. }
  22. var abi
  23. var lastTarget
  24. for (var i = 0; i < allTargets.length; i++) {
  25. var t = allTargets[i]
  26. if (t.runtime !== runtime) continue
  27. if (semver.lte(t.target, target) && (!lastTarget || semver.gte(t.target, lastTarget))) {
  28. abi = t.abi
  29. lastTarget = t.target
  30. }
  31. }
  32. if (abi && semver.lt(target, getNextTarget(runtime))) return abi
  33. throw new Error('Could not detect abi for version ' + target + ' and runtime ' + runtime + '. Updating "node-abi" might help solve this issue if it is a new release of ' + runtime)
  34. }
  35. function getTarget (abi, runtime) {
  36. if (abi && abi !== String(Number(abi))) return abi
  37. if (!runtime) runtime = 'node'
  38. if (runtime === 'node' && !abi) return process.versions.node
  39. var match = allTargets
  40. .filter(function (t) {
  41. return t.abi === abi && t.runtime === runtime
  42. })
  43. .map(function (t) {
  44. return t.target
  45. })
  46. if (match.length) {
  47. var betaSeparatorIndex = match[0].indexOf("-")
  48. return betaSeparatorIndex > -1
  49. ? match[0].substring(0, betaSeparatorIndex)
  50. : match[0]
  51. }
  52. throw new Error('Could not detect target for abi ' + abi + ' and runtime ' + runtime)
  53. }
  54. function sortByTargetFn (a, b) {
  55. var abiComp = Number(a.abi) - Number(b.abi)
  56. if (abiComp !== 0) return abiComp
  57. if (a.target < b.target) return -1
  58. if (a.target > b.target) return 1
  59. return 0
  60. }
  61. function loadGeneratedTargets () {
  62. var registry = require('./abi_registry.json')
  63. var targets = {
  64. supported: [],
  65. additional: [],
  66. future: []
  67. }
  68. registry.forEach(function (item) {
  69. var target = {
  70. runtime: item.runtime,
  71. target: item.target,
  72. abi: item.abi
  73. }
  74. if (item.lts) {
  75. var startDate = new Date(Date.parse(item.lts[0]))
  76. var endDate = new Date(Date.parse(item.lts[1]))
  77. var currentDate = new Date()
  78. target.lts = startDate < currentDate && currentDate < endDate
  79. } else {
  80. target.lts = false
  81. }
  82. if (target.runtime === 'node-webkit') {
  83. targets.additional.push(target)
  84. } else if (item.future) {
  85. targets.future.push(target)
  86. } else {
  87. targets.supported.push(target)
  88. }
  89. })
  90. targets.supported.sort(sortByTargetFn)
  91. targets.additional.sort(sortByTargetFn)
  92. targets.future.sort(sortByTargetFn)
  93. return targets
  94. }
  95. var generatedTargets = loadGeneratedTargets()
  96. var supportedTargets = [
  97. {runtime: 'node', target: '5.0.0', abi: '47', lts: false},
  98. {runtime: 'node', target: '6.0.0', abi: '48', lts: false},
  99. {runtime: 'node', target: '7.0.0', abi: '51', lts: false},
  100. {runtime: 'node', target: '8.0.0', abi: '57', lts: false},
  101. {runtime: 'node', target: '9.0.0', abi: '59', lts: false},
  102. {runtime: 'node', target: '10.0.0', abi: '64', lts: new Date(2018, 10, 1) < new Date() && new Date() < new Date(2020, 4, 31)},
  103. {runtime: 'electron', target: '0.36.0', abi: '47', lts: false},
  104. {runtime: 'electron', target: '1.1.0', abi: '48', lts: false},
  105. {runtime: 'electron', target: '1.3.0', abi: '49', lts: false},
  106. {runtime: 'electron', target: '1.4.0', abi: '50', lts: false},
  107. {runtime: 'electron', target: '1.5.0', abi: '51', lts: false},
  108. {runtime: 'electron', target: '1.6.0', abi: '53', lts: false},
  109. {runtime: 'electron', target: '1.7.0', abi: '54', lts: false},
  110. {runtime: 'electron', target: '1.8.0', abi: '57', lts: false},
  111. {runtime: 'electron', target: '2.0.0', abi: '57', lts: false},
  112. {runtime: 'electron', target: '3.0.0', abi: '64', lts: false},
  113. {runtime: 'electron', target: '4.0.0', abi: '64', lts: false},
  114. {runtime: 'electron', target: '4.0.4', abi: '69', lts: false}
  115. ]
  116. supportedTargets.push.apply(supportedTargets, generatedTargets.supported)
  117. var additionalTargets = [
  118. {runtime: 'node-webkit', target: '0.13.0', abi: '47', lts: false},
  119. {runtime: 'node-webkit', target: '0.15.0', abi: '48', lts: false},
  120. {runtime: 'node-webkit', target: '0.18.3', abi: '51', lts: false},
  121. {runtime: 'node-webkit', target: '0.23.0', abi: '57', lts: false},
  122. {runtime: 'node-webkit', target: '0.26.5', abi: '59', lts: false}
  123. ]
  124. additionalTargets.push.apply(additionalTargets, generatedTargets.additional)
  125. var deprecatedTargets = [
  126. {runtime: 'node', target: '0.2.0', abi: '1', lts: false},
  127. {runtime: 'node', target: '0.9.1', abi: '0x000A', lts: false},
  128. {runtime: 'node', target: '0.9.9', abi: '0x000B', lts: false},
  129. {runtime: 'node', target: '0.10.4', abi: '11', lts: false},
  130. {runtime: 'node', target: '0.11.0', abi: '0x000C', lts: false},
  131. {runtime: 'node', target: '0.11.8', abi: '13', lts: false},
  132. {runtime: 'node', target: '0.11.11', abi: '14', lts: false},
  133. {runtime: 'node', target: '1.0.0', abi: '42', lts: false},
  134. {runtime: 'node', target: '1.1.0', abi: '43', lts: false},
  135. {runtime: 'node', target: '2.0.0', abi: '44', lts: false},
  136. {runtime: 'node', target: '3.0.0', abi: '45', lts: false},
  137. {runtime: 'node', target: '4.0.0', abi: '46', lts: false},
  138. {runtime: 'electron', target: '0.30.0', abi: '44', lts: false},
  139. {runtime: 'electron', target: '0.31.0', abi: '45', lts: false},
  140. {runtime: 'electron', target: '0.33.0', abi: '46', lts: false}
  141. ]
  142. var futureTargets = generatedTargets.future
  143. var allTargets = deprecatedTargets
  144. .concat(supportedTargets)
  145. .concat(additionalTargets)
  146. .concat(futureTargets)
  147. exports.getAbi = getAbi
  148. exports.getTarget = getTarget
  149. exports.deprecatedTargets = deprecatedTargets
  150. exports.supportedTargets = supportedTargets
  151. exports.additionalTargets = additionalTargets
  152. exports.futureTargets = futureTargets
  153. exports.allTargets = allTargets
  154. exports._getNextTarget = getNextTarget