/
getFilePathInCafs.ts
43 lines (37 loc) · 1.1 KB
/
getFilePathInCafs.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
import path from 'path'
import ssri, { type IntegrityLike } from 'ssri'
export const modeIsExecutable = (mode: number) => (mode & 0o111) === 0o111
export type FileType = 'exec' | 'nonexec' | 'index'
export function getFilePathByModeInCafs (
cafsDir: string,
integrity: string | IntegrityLike,
mode: number
) {
const fileType = modeIsExecutable(mode) ? 'exec' : 'nonexec'
return path.join(cafsDir, contentPathFromIntegrity(integrity, fileType))
}
export function getFilePathInCafs (
cafsDir: string,
integrity: string | IntegrityLike,
fileType: FileType
) {
return path.join(cafsDir, contentPathFromIntegrity(integrity, fileType))
}
function contentPathFromIntegrity (
integrity: string | IntegrityLike,
fileType: FileType
) {
const sri = ssri.parse(integrity, { single: true })
return contentPathFromHex(fileType, sri.hexDigest())
}
export function contentPathFromHex (fileType: FileType, hex: string) {
const p = path.join(hex.slice(0, 2), hex.slice(2))
switch (fileType) {
case 'exec':
return `${p}-exec`
case 'nonexec':
return p
case 'index':
return `${p}-index.json`
}
}