-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
146 lines (127 loc) · 4.33 KB
/
index.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
136
137
138
139
140
141
142
143
144
145
146
import tarStream from 'tar-stream'
import { createGunzip } from 'zlib'
import { pipeline } from 'stream/promises'
import { StringDecoder } from 'string_decoder'
import { Readable } from 'stream'
import type { ReadableStream } from 'stream/web'
export interface AlpineRepository {
name: string
content: string
description: string
}
export interface AlpinePackage {
deps: string[]
version: string
}
export interface AlpinePackageMap {
[name: string]: AlpinePackage
}
async function downloadRepo(repo: string, version: string, arch: string): Promise<AlpineRepository> {
const url = `http://dl-cdn.alpinelinux.org/alpine/${version}/${repo}/${arch}/APKINDEX.tar.gz`
const response = await fetch(url);
if (response.status !== 200 || response.body === null) throw new Error(`Failed to download ${url}`);
const unzip = createGunzip();
const tar = tarStream.extract();
const fileData = {
APKINDEX: '',
DESCRIPTION: ''
};
tar.on('entry', (header, stream, next) => {
if (header.name in fileData) {
const stringDec = new StringDecoder();
stream.on('data', (chunk: Buffer) => {
fileData[header.name as keyof typeof fileData] += stringDec.write(chunk);
});
stream.on('end', () => {
fileData[header.name as keyof typeof fileData] += stringDec.end();
next();
});
} else {
stream.on('end', next);
}
stream.resume();
});
await pipeline(
Readable.fromWeb(response.body as ReadableStream),
unzip,
tar
);
if (!fileData.APKINDEX || !fileData.DESCRIPTION) throw new Error(`Failed to download ${url}`);
return {
name: repo,
content: fileData.APKINDEX,
description: fileData.DESCRIPTION
};
}
function getDependencyTreeInternal(
name: string,
pkg: AlpinePackage,
seen: Set<AlpinePackage>,
packages: AlpinePackageMap
) {
let output = name + '@' + pkg.version + ',';
for (const dep of pkg.deps) {
const dPkg = packages[dep];
if (dPkg === undefined) {
continue;
}
if (!seen.has(dPkg)) {
seen.add(dPkg);
output += getDependencyTreeInternal(dep, dPkg, seen, packages);
}
}
return output;
}
export class AlpineApk {
async update(version = 'latest-stable', arch = 'x86_64', repos = ['main', 'community']) {
const rawRepos = await Promise.all(repos.map(r => downloadRepo(r, version, arch)));
this.setRepositories(rawRepos);
return rawRepos;
}
pkgs: AlpinePackageMap = {};
pkgNames: AlpinePackageMap = {};
setRepositories(repositories: AlpineRepository[]) {
for (const repo of repositories) {
for (const pkgLines of repo.content.split('\n\n')) {
const pkg: AlpinePackage = {
version: '',
deps: []
};
for (const line of pkgLines.split('\n')) {
const c = line[0];
const rest = line.substr(2);
if (c === 'P') {
this.pkgs[rest] = this.pkgNames[rest] = pkg;
} else if (c === 'D') {
pkg.deps = rest.split(' ').map(d => {
if (d.startsWith('!')) d = d.substr(1);
[d] = d.split(/[=<>~]/);
return d;
});
} else if (c === 'p') {
for (let p of rest.split(' ')) {
[p] = p.split('=');
this.pkgs[p] = pkg;
}
} else if (c === 'V') {
pkg.version = rest;
}
}
}
}
}
get(name: string): AlpinePackage | undefined {
return this.pkgNames[name] ?? this.pkgs[name];
}
getDependencyTree(...names: string[]) {
const seen = new Set<AlpinePackage>();
let tree = '';
for (const name of names) {
const pkg = this.get(name)!;
if (pkg === undefined) continue;
tree += getDependencyTreeInternal(name, pkg, seen, this.pkgs);
}
return tree;
}
}
export default AlpineApk;