-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuseInventory.ts
More file actions
53 lines (42 loc) · 1.32 KB
/
useInventory.ts
File metadata and controls
53 lines (42 loc) · 1.32 KB
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
import { Package, PackageRequirement } from "../types.ts"
import { DownloadError } from "./useDownload.ts"
import SemVer from "../utils/semver.ts"
import useFetch from "./useFetch.ts"
import host from "../utils/host.ts"
import "../utils/misc.ts"
export interface Inventory {
[project: string]: {
[platform: string]: {
[arch: string]: string[]
}
}
}
const select = async (rq: PackageRequirement | Package) => {
const versions = await _internals.get(rq)
if ("constraint" in rq) {
return rq.constraint.max(versions)
} else if (versions.find(x => x.eq(rq.version))) {
return rq.version
}
}
const get = async (rq: PackageRequirement | Package) => {
const { platform, arch } = host()
const url = new URL(`https://dist.pkgx.dev/${rq.project}/${platform}/${arch}/versions.txt`)
const rsp = await useFetch(url)
if (!rsp.ok) {
throw new DownloadError(rsp.status, {src: url})
}
const releases = await rsp.text()
let versions = releases.split("\n").compact(x => new SemVer(x))
if (versions.length < 1) throw new Error()
if (rq.project == 'openssl.org') {
// workaround our previous sins
const v = new SemVer("1.1.118")
versions = versions.filter(x => x.neq(v))
}
return versions
}
export default function useInventory() {
return { select, get }
}
export const _internals = { get }