-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
61 lines (51 loc) · 1.5 KB
/
utils.ts
File metadata and controls
61 lines (51 loc) · 1.5 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
54
55
56
57
58
59
60
61
export {
canParse,
format,
maxSatisfying,
parse,
parseRange,
} from "jsr:@std/semver@1.0.7";
import { UntarStream } from "jsr:@std/tar@0.1.9/untar-stream";
import { globToRegExp } from "jsr:@std/path@1.1.4/glob-to-regexp";
export async function* untar(
blob: Blob,
pattern: string,
): AsyncGenerator<File> {
const untar = blob
.stream()
.pipeThrough(new DecompressionStream("gzip"))
.pipeThrough(new UntarStream());
const regexp = globToRegExp(pattern, { globstar: true, extended: true });
const basePath = pattern.includes("*") ? pattern.split("/*")[0] : undefined;
for await (const entry of untar) {
const stream = entry.readable;
if (!stream) {
continue;
}
const content = await getContent(stream);
let path = removeFirstDirectory(entry.path);
if (!path || regexp.test(path) === false || path === "/") {
continue;
}
if (basePath) {
path = path.slice(basePath.length);
}
yield new File([content], path, {
type: "application/octet-stream",
});
}
}
async function getContent(stream: ReadableStream<Uint8Array>): Promise<Blob> {
const reader = stream.getReader();
const chunks: Uint8Array<ArrayBuffer>[] = [];
let result = await reader.read();
while (!result.done) {
chunks.push(result.value as Uint8Array<ArrayBuffer>);
result = await reader.read();
}
return new Blob(chunks);
}
function removeFirstDirectory(path: string): string {
const [, ...parts] = path.split("/");
return `/${parts.join("/")}`;
}