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