@preventive/upstream 1.5.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ // Gem::Platform's reading and writing of a platform, RubyGems 3 and 4 each
2
+ // their own way, ported from @preventive/lockfile.
3
+
4
+ // Ruby's String#split on `-`, which drops the empty strings at the end.
5
+ function split(text) {
6
+ const parts = text.split('-')
7
+ while (parts.at(-1) === '') parts.pop()
8
+ return parts
9
+ }
10
+
11
+ const none = () => undefined
12
+
13
+ // Gem::Platform#initialize's OS patterns, in its order and as unanchored.
14
+ // RubyGems 4 reads all after the CPU as the OS, and a `-` before its version.
15
+ const OSES = {
16
+ 3: [
17
+ [/aix(\d+)?/u, 'aix'],
18
+ [/cygwin/u, 'cygwin', none],
19
+ [/darwin(\d+)?/u, 'darwin'],
20
+ [/^macruby$/u, 'macruby', none],
21
+ [/freebsd(\d+)?/u, 'freebsd'],
22
+ [/^(?:java|jruby)$/u, 'java', none],
23
+ [/^java([\d.]*)/u, 'java'],
24
+ [/^dalvik(\d+)?$/u, 'dalvik'],
25
+ [/^dotnet$/u, 'dotnet', none],
26
+ [/^dotnet([\d.]*)/u, 'dotnet'],
27
+ [/linux-?(\w+)?/u, 'linux'],
28
+ [/mingw32/u, 'mingw32', none],
29
+ [/mingw-?(\w+)?/u, 'mingw'],
30
+ [/(mswin\d+)(_(\d+))?/u, undefined, (m) => m[3]],
31
+ [/netbsdelf/u, 'netbsdelf', none],
32
+ [/openbsd(\d+\.\d+)?/u, 'openbsd'],
33
+ [/solaris(\d+\.\d+)?/u, 'solaris'],
34
+ [/wasi/u, 'wasi', none],
35
+ [/^(\w+_platform)(\d+)?/u, undefined, (m) => m[2]],
36
+ ],
37
+ 4: [
38
+ [/aix-?(\d+)?/u, 'aix'],
39
+ [/cygwin/u, 'cygwin', none],
40
+ [/darwin-?(\d+)?/u, 'darwin'],
41
+ [/^macruby$/u, 'macruby', none],
42
+ [/^macruby-?(\d+(?:\.\d+)*)?/u, 'macruby'],
43
+ [/freebsd-?(\d+)?/u, 'freebsd'],
44
+ [/^(?:java|jruby)$/u, 'java', none],
45
+ [/^java-?(\d+(?:\.\d+)*)?/u, 'java'],
46
+ [/^dalvik-?(\d+)?$/u, 'dalvik'],
47
+ [/^dotnet$/u, 'dotnet', none],
48
+ [/^dotnet-?(\d+(?:\.\d+)*)?/u, 'dotnet'],
49
+ [/linux-?(\w+)?/u, 'linux'],
50
+ [/mingw32/u, 'mingw32', none],
51
+ [/mingw-?(\w+)?/u, 'mingw'],
52
+ [/(mswin\d+)(?:[_-](\d+))?/u, undefined, (m) => m[2]],
53
+ [/netbsdelf/u, 'netbsdelf', none],
54
+ [/openbsd-?(\d+\.\d+)?/u, 'openbsd'],
55
+ [/solaris-?(\d+\.\d+)?/u, 'solaris'],
56
+ [/wasi/u, 'wasi', none],
57
+ [/^(\w+_platform)-?(\d+)?/u, undefined, (m) => m[2]],
58
+ ],
59
+ }
60
+
61
+ // [os, version] as `rubygems` reads them, and the CPU a lone mswin32 is of.
62
+ function readOs(os, rubygems) {
63
+ if (os === undefined) return ['unknown', undefined, undefined]
64
+ for (const [pattern, name, version = (m) => m[1]] of OSES[rubygems]) {
65
+ const match = pattern.exec(os)
66
+ if (match === null) continue
67
+ const named = name ?? match[1]
68
+ return [named, version(match), /^mswin\d*32$/u.test(named) ? 'x86' : undefined]
69
+ }
70
+ return ['unknown', undefined, undefined]
71
+ }
72
+
73
+ // RubyGems 3: x86_64-linux-gnu's libc is part of the OS, darwin-23's
74
+ // version apart.
75
+ function split3(text) {
76
+ const parts = split(text)
77
+ // RubyGems' /\d+(\.\d+)?$/, without a regex retried from every digit.
78
+ if (parts.length > 2 && !/\d$/u.test(parts.at(-1))) {
79
+ const extra = parts.pop()
80
+ parts[parts.length - 1] += `-${extra}`
81
+ }
82
+ const cpu = parts.shift()
83
+ if (parts.length === 2 && /^\d+(?:\.\d+)?$/u.test(parts[1])) return { cpu, os: parts[0], version: parts[1] }
84
+ return { cpu, os: parts[0] }
85
+ }
86
+
87
+ // RubyGems 4: the CPU, and all after it, less any `-` at the end.
88
+ function split4(text) {
89
+ let end = text.length
90
+ while (text[end - 1] === '-') end--
91
+ const trimmed = text.slice(0, end)
92
+ const sep = trimmed.indexOf('-')
93
+ return sep === -1 ? { cpu: trimmed || undefined } : { cpu: trimmed.slice(0, sep), os: trimmed.slice(sep + 1) }
94
+ }
95
+
96
+ // [cpu, os, version] as Gem::Platform.new reads them, undefined where absent.
97
+ function platformParts(text, rubygems) {
98
+ const { cpu, os, version } = rubygems === 3 ? split3(text) : split4(text)
99
+ const x86 = cpu !== undefined && /i\d86/u.test(cpu) ? 'x86' : cpu
100
+ if (version !== undefined) return [x86, os, version]
101
+ // A lone part is the OS, as `java` is.
102
+ const [name, osVersion, lone] = readOs(os ?? cpu, rubygems)
103
+ return [os === undefined ? lone : x86, name, osVersion]
104
+ }
105
+
106
+ // Gem::Platform#to_s: RubyGems 4 puts no `-` in a platform without a CPU.
107
+ export function platformOf(text, rubygems) {
108
+ const parts = platformParts(text, rubygems)
109
+ return parts.filter((part) => part !== undefined).join(rubygems === 4 && parts[0] === undefined ? '' : '-')
110
+ }
111
+
112
+ // RubyGems keeps the CPU as written, so `..-linux` would round-trip: each
113
+ // part starts with a letter or digit, the first with a letter, and no `..`
114
+ // (cap ours).
115
+ const SHAPE = /^(?=.{1,128}$)(?!.*\.\.)[A-Za-z][\w.]*(?:-[\dA-Za-z][\w.]*)*$/u
116
+
117
+ // What RubyGems 3 and 4 both read back as written: the platform a gem's
118
+ // file is named by. `ruby` and `current` mean no platform and the host's.
119
+ export const isGemPlatform = (text) => typeof text === 'string' && SHAPE.test(text) && text !== 'ruby' && text !== 'current'
120
+ && platformOf(text, 3) === text && platformOf(text, 4) === text
@@ -0,0 +1,124 @@
1
+ // Gem::Version and Gem::Requirement as RubyGems 3.5 has them, ported from
2
+ // @preventive/lockfile. Numbers are bigints: Ruby's are unbounded.
3
+
4
+ // Gem::Version::VERSION_PATTERN: a gem's own version has no `-`, but a
5
+ // range may.
6
+ const VERSION = String.raw`\d+(?:\.[\dA-Za-z]+)*(?:-[\dA-Za-z-]+(?:\.[\dA-Za-z-]+)*)?`
7
+ const SPACE = String.raw`[\t\n\v\f\r ]` // Ruby's \s, ASCII alone.
8
+ // Gem::Requirement::PATTERN, operators longest first as Ruby's backtracking
9
+ // tries them.
10
+ const REQUIREMENT = new RegExp(`^${SPACE}*(?:(>=|<=|!=|~>|=|>|<)${SPACE}*)?(${VERSION})${SPACE}*$`, 'u')
11
+ const ANCHORED = new RegExp(`^${VERSION}$`, 'u')
12
+
13
+ // 256 at most, before any regex runs.
14
+ export const isVersion = (text) => typeof text === 'string' && text.length <= 256 && ANCHORED.test(text)
15
+
16
+ // Gem::Version reads `-` as `.pre.`.
17
+ const read = (version) => version.replaceAll('-', '.pre.')
18
+
19
+ const isPrerelease = (version) => /[A-Za-z]/u.test(version)
20
+
21
+ // Gem::Version#partition_segments: numbers and runs of letters.
22
+ const partition = (version) => (version.match(/\d+|[A-Za-z]+/gu) ?? []).map((part) => (/^\d/u.test(part) ? BigInt(part) : part))
23
+
24
+ // Runs of `.` and `0` as [start, end), in one pass.
25
+ const zeroRuns = (text) => Array.from(text.matchAll(/[.0]+/gu), (m) => [m.index, m.index + m[0].length])
26
+
27
+ function startIn(text, [from, to], follows) {
28
+ for (let at = from; at < to; at++) if (follows(text[at - 1])) return at
29
+ return undefined
30
+ }
31
+
32
+ // Gem::Version#canonical_segments: trailing zeros dropped, then of a
33
+ // prerelease the first run of zeros a letter follows.
34
+ function canonical(version) {
35
+ let from = version.length
36
+ while (version[from - 1] === '.' || version[from - 1] === '0') from--
37
+ const end = from < version.length ? startIn(version, [from, version.length], (char) => char !== undefined && /[A-Za-z.]/u.test(char)) : undefined
38
+ const text = version.slice(0, end)
39
+ if (!isPrerelease(version)) return partition(text)
40
+ for (const run of zeroRuns(text)) {
41
+ const start = /[A-Za-z]/u.test(text[run[1]] ?? '') ? startIn(text, run, (char) => char === undefined || char === '.') : undefined
42
+ if (start !== undefined) return partition(`${text.slice(0, start)}${text.slice(run[1])}`)
43
+ }
44
+ return partition(text)
45
+ }
46
+
47
+ // Ruby's String#<=>, by code unit, which for ASCII is by byte.
48
+ const order = (a, b) => (a < b ? -1 : a > b ? 1 : 0)
49
+
50
+ // Gem::Version#<=>: a missing segment is 0, and letters come before a number.
51
+ function compareSegments(left, right) {
52
+ for (let i = 0; i < Math.max(left.length, right.length); i++) {
53
+ const [lhs, rhs] = [left[i] ?? 0n, right[i] ?? 0n]
54
+ if (lhs === rhs) continue
55
+ if (typeof lhs !== typeof rhs) return typeof lhs === 'string' ? -1 : 1
56
+ return order(lhs, rhs)
57
+ }
58
+ return 0
59
+ }
60
+
61
+ export function compareVersions(a, b) {
62
+ const [left, right] = [read(a), read(b)]
63
+ return left === right ? 0 : compareSegments(canonical(left), canonical(right))
64
+ }
65
+
66
+ // The segments before the first letters, as written, not canonical.
67
+ function numeric(version) {
68
+ const segments = partition(version)
69
+ const letters = segments.findIndex((segment) => typeof segment === 'string')
70
+ return letters === -1 ? segments : segments.slice(0, letters)
71
+ }
72
+
73
+ // Gem::Version#release
74
+ const release = (version) => (isPrerelease(version) ? numeric(version).join('.') : version)
75
+
76
+ // Gem::Version#bump: the last segment dropped, the one before raised.
77
+ function bump(version) {
78
+ const segments = numeric(version)
79
+ if (segments.length > 1) segments.pop()
80
+ segments[segments.length - 1] += 1n
81
+ return segments.join('.')
82
+ }
83
+
84
+ const OPERATORS = {
85
+ __proto__: null,
86
+ '=': (cmp) => cmp === 0,
87
+ '!=': (cmp) => cmp !== 0,
88
+ '>': (cmp) => cmp > 0,
89
+ '<': (cmp) => cmp < 0,
90
+ '>=': (cmp) => cmp >= 0,
91
+ '<=': (cmp) => cmp <= 0,
92
+ }
93
+
94
+ // Undefined where a part isn't a requirement, or the range is over 1,024
95
+ // characters.
96
+ function requirementsOf(text) {
97
+ if (text.length > 1024) return undefined
98
+ const parts = text.split(',').map((part) => REQUIREMENT.exec(part))
99
+ if (parts.some((match) => match === null || match[2].length > 256)) return undefined
100
+ return parts.map(([, operator = '=', against]) => [operator, against])
101
+ }
102
+
103
+ // A test of a range, each version read once. A prerelease meets one as any
104
+ // version does: only Gem::Dependency sets them apart.
105
+ export function readRange(text) {
106
+ const parts = requirementsOf(text)
107
+ if (parts === undefined) return undefined
108
+ const requirements = parts.map(([operator, against]) => ({ operator, at: canonical(read(against)), below: operator === '~>' ? canonical(bump(read(against))) : undefined }))
109
+ return (version) => {
110
+ const [own, released] = [canonical(read(version)), canonical(release(read(version)))]
111
+ return requirements.every(({ operator, at, below }) => (operator === '~>'
112
+ ? compareSegments(own, at) >= 0 && compareSegments(released, below) < 0
113
+ : OPERATORS[operator](compareSegments(own, at))))
114
+ }
115
+ }
116
+
117
+ // Lower bounds alone end below the one patched version above them; any
118
+ // other range is as given.
119
+ export function boundRange(range, patched) {
120
+ if (!isVersion(patched)) return range
121
+ if (range.trim() === '') return `< ${patched}`
122
+ const open = requirementsOf(range)?.every(([operator, against]) => operator === '!=' || ((operator === '>=' || operator === '>') && compareVersions(against, patched) < 0))
123
+ return open ? `${range.trim()}, < ${patched}` : range
124
+ }
package/src/tree.js CHANGED
@@ -42,9 +42,10 @@ function emptyDirs(dir, prefix = '') {
42
42
 
43
43
  // Git sorts a subtree as if its name ended in `/`.
44
44
  function treeId(dir) {
45
- const entries = [...dir].map(([name, entry]) => (entry instanceof Map ? { mode: '40000', name, id: treeId(entry) } : { ...entry, name }))
46
- const keyed = entries.map((entry) => [entry.mode === '40000' ? `${entry.name}/` : entry.name, entry])
47
- keyed.sort(([a], [b]) => (a < b ? -1 : Number(a > b)))
45
+ const keyed = [...dir].map(([name, entry]) => {
46
+ const own = entry instanceof Map ? { mode: '40000', name, id: treeId(entry) } : { ...entry, name }
47
+ return [own.mode === '40000' ? `${name}/` : name, own]
48
+ }).sort(([a], [b]) => (a < b ? -1 : Number(a > b)))
48
49
  return objectId('tree', Buffer.concat(keyed.flatMap(([, { mode, name, id }]) => [Buffer.from(`${mode} ${name}\0`, 'latin1'), id])))
49
50
  }
50
51