|
| 1 | +const semver = require('semver') |
| 2 | + |
| 3 | +const checkEngine = (target, npmVer, nodeVer, force = false) => { |
| 4 | + const nodev = force ? null : nodeVer |
| 5 | + const eng = target.engines |
| 6 | + const opt = { includePrerelease: true } |
| 7 | + if (!eng) { |
| 8 | + return |
| 9 | + } |
| 10 | + |
| 11 | + const nodeFail = nodev && eng.node && !semver.satisfies(nodev, eng.node, opt) |
| 12 | + const npmFail = npmVer && eng.npm && !semver.satisfies(npmVer, eng.npm, opt) |
| 13 | + if (nodeFail || npmFail) { |
| 14 | + throw Object.assign(new Error('Unsupported engine'), { |
| 15 | + pkgid: target._id, |
| 16 | + current: { node: nodeVer, npm: npmVer }, |
| 17 | + required: eng, |
| 18 | + code: 'EBADENGINE', |
| 19 | + }) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +const isMusl = (file) => file.includes('libc.musl-') || file.includes('ld-musl-') |
| 24 | + |
| 25 | +const checkPlatform = (target, force = false, environment = {}) => { |
| 26 | + if (force) { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + const platform = environment.os || process.platform |
| 31 | + const arch = environment.cpu || process.arch |
| 32 | + const osOk = target.os ? checkList(platform, target.os) : true |
| 33 | + const cpuOk = target.cpu ? checkList(arch, target.cpu) : true |
| 34 | + |
| 35 | + let libcOk = true |
| 36 | + let libcFamily = null |
| 37 | + if (target.libc) { |
| 38 | + // libc checks only work in linux, any value is a failure if we aren't |
| 39 | + if (environment.libc) { |
| 40 | + libcOk = checkList(environment.libc, target.libc) |
| 41 | + } else if (platform !== 'linux') { |
| 42 | + libcOk = false |
| 43 | + } else { |
| 44 | + const report = process.report.getReport() |
| 45 | + if (report.header?.glibcVersionRuntime) { |
| 46 | + libcFamily = 'glibc' |
| 47 | + } else if (Array.isArray(report.sharedObjects) && report.sharedObjects.some(isMusl)) { |
| 48 | + libcFamily = 'musl' |
| 49 | + } |
| 50 | + libcOk = libcFamily ? checkList(libcFamily, target.libc) : false |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (!osOk || !cpuOk || !libcOk) { |
| 55 | + throw Object.assign(new Error('Unsupported platform'), { |
| 56 | + pkgid: target._id, |
| 57 | + current: { |
| 58 | + os: platform, |
| 59 | + cpu: arch, |
| 60 | + libc: libcFamily, |
| 61 | + }, |
| 62 | + required: { |
| 63 | + os: target.os, |
| 64 | + cpu: target.cpu, |
| 65 | + libc: target.libc, |
| 66 | + }, |
| 67 | + code: 'EBADPLATFORM', |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +const checkList = (value, list) => { |
| 73 | + if (typeof list === 'string') { |
| 74 | + list = [list] |
| 75 | + } |
| 76 | + if (list.length === 1 && list[0] === 'any') { |
| 77 | + return true |
| 78 | + } |
| 79 | + // match none of the negated values, and at least one of the |
| 80 | + // non-negated values, if any are present. |
| 81 | + let negated = 0 |
| 82 | + let match = false |
| 83 | + for (const entry of list) { |
| 84 | + const negate = entry.charAt(0) === '!' |
| 85 | + const test = negate ? entry.slice(1) : entry |
| 86 | + if (negate) { |
| 87 | + negated++ |
| 88 | + if (value === test) { |
| 89 | + return false |
| 90 | + } |
| 91 | + } else { |
| 92 | + match = match || value === test |
| 93 | + } |
| 94 | + } |
| 95 | + return match || negated === list.length |
| 96 | +} |
| 97 | + |
| 98 | +module.exports = { |
| 99 | + checkEngine, |
| 100 | + checkPlatform, |
| 101 | +} |
0 commit comments