-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinstall.ts
135 lines (115 loc) · 4.8 KB
/
install.ts
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { Package, Installation, StowageNativeBottle } from "../types.ts"
import useOffLicense from "../hooks/useOffLicense.ts"
import useDownload from "../hooks/useDownload.ts"
import { flock } from "../utils/flock.ts"
import useConfig from "../hooks/useConfig.ts"
import useCellar from "../hooks/useCellar.ts"
import useCache from "../hooks/useCache.ts"
import useFetch from "../hooks/useFetch.ts"
import { createHash } from "node:crypto"
import Path from "../utils/Path.ts"
export default async function install(pkg: Package, logger?: Logger): Promise<Installation> {
const { project, version } = pkg
const cellar = useCellar()
const { prefix: PKGX_DIR, options: { compression } } = useConfig()
const stowage = StowageNativeBottle({ pkg: { project, version }, compression })
const url = useOffLicense('s3').url(stowage)
const tarball = useCache().path(stowage)
const shelf = PKGX_DIR.join(pkg.project)
logger?.locking?.(pkg)
const unflock = await flock(shelf.mkdir('p'))
try {
const already_installed = await cellar.has(pkg)
if (already_installed) {
// some other pkgx instance installed us while we were waiting for the lock
// or potentially we were already installed and the caller is naughty
logger?.installed?.(already_installed)
return already_installed
}
logger?.downloading?.({pkg})
const PATH = Deno.build.os == 'windows' ? "C:\\windows\\system32" : "/usr/bin:/bin"
const tmpdir = Path.mktemp({
//TODO dir should not be here ofc
dir: PKGX_DIR.join(".local/tmp").join(pkg.project),
prefix: `v${pkg.version}.`
//NOTE ^^ inside pkgx prefix to avoid TMPDIR is on a different volume problems
})
const tar_args = compression == 'xz' ? 'xJf' : 'xzf' // laughably confusing
const untar = new Deno.Command("tar", {
args: [tar_args, "-", "--strip-components", (pkg.project.split("/").length + 1).toString()],
stdin: 'piped', stdout: "inherit", stderr: "inherit",
cwd: tmpdir.string,
/// hard coding path to ensure we don’t deadlock trying to use ourselves to untar ourselves
env: { PATH }
}).spawn()
const hasher = createHash("sha256")
const remote_SHA_promise = remote_SHA(new URL(`${url}.sha256sum`))
const writer = untar.stdin.getWriter()
let total: number | undefined
let n = 0
await useDownload().download({
src: url,
dst: tarball,
logger: info => {
logger?.downloading?.({ pkg, ...info })
total ??= info.total
}
}, blob => {
n += blob.length
hasher.update(blob)
logger?.installing?.({ pkg, progress: total ? n / total : total })
return writer.write(blob)
})
writer.close()
const untar_exit_status = await untar.status
if (!untar_exit_status.success) {
throw new Error(`tar exited with status ${untar_exit_status.code}`)
}
const computed_hash_value = hasher.digest("hex")
const checksum = await remote_SHA_promise
if (computed_hash_value != checksum) {
tarball.rm()
console.error("pkgx: we deleted the invalid tarball. try again?")
throw new Error(`sha: expected: ${checksum}, got: ${computed_hash_value}`)
}
const path = tmpdir.mv({ to: shelf.join(`v${pkg.version}`) }).chmod(0o755)
const install = { pkg, path }
logger?.installed?.(install)
return install
} catch (err) {
tarball.rm() //FIXME resumable downloads!
throw err
} finally {
logger?.unlocking?.(pkg)
await unflock()
}
}
async function remote_SHA(url: URL) {
const rsp = await useFetch(url)
if (!rsp.ok) throw rsp
const txt = await rsp.text()
return txt.split(' ')[0]
}
export interface Logger {
locking?(pkg: Package): void
/// raw http info
downloading?(info: {pkg: Package, src?: URL, dst?: Path, rcvd?: number, total?: number}): void
/// we are simultaneously downloading and untarring the bottle
/// the install progress here is proper and tied to download progress
/// progress is a either a fraction between 0 and 1 or the number of bytes that have been untarred
/// we try to give you the fraction as soon as possible, but you will need to deal with both formats
installing?(info: {pkg: Package, progress: number | undefined}): void
unlocking?(pkg: Package): void
installed?(installation: Installation): void
}
// deno-lint-ignore no-explicit-any
export function ConsoleLogger(prefix?: any): Logger {
prefix = prefix ? `${prefix}: ` : ""
return {
locking: function() { console.error(`${prefix}locking`, ...arguments) },
downloading: function() { console.error(`${prefix}downloading`, ...arguments) },
installing: function() { console.error(`${prefix}installing`, ...arguments) },
unlocking: function() { console.error(`${prefix}unlocking`, ...arguments) },
installed: function() { console.error(`${prefix}installed`, ...arguments) }
}
}