Repository navigation
Expand file tree
/
Copy pathcompression.ts
More file actions
110 lines (101 loc) · 2.65 KB
/
Copy pathcompression.ts
File metadata and controls
110 lines (101 loc) · 2.65 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
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
import { toText } from "@std/streams";
const ALGORITHMS = [
"none",
"gzip",
"bzip2",
"zstd",
"xz",
] as const;
export type CompressionAlgorithm = typeof ALGORITHMS[number];
export function isCompressionAlgorithm(
value: string,
): value is CompressionAlgorithm {
return (ALGORITHMS as readonly string[]).includes(value);
}
function spawn(command: string): TransformStream<Uint8Array, Uint8Array> {
// warning! this function is horrible. i've tried like 10 different ways to
// make it work, and this is the only one that works. i'm sorry.
// also it leaks ops, but not if you pass --trace-leaks
const proc = new Deno.Command(command, {
stdin: "piped",
stdout: "piped",
stderr: "piped",
args: ["-vv"],
}).spawn();
const stdin = proc.stdin.getWriter();
let writer: Promise<void>;
return new TransformStream({
start(controller) {
writer = proc.stdout.pipeTo(
new WritableStream({
write(chunk) {
try {
controller.enqueue(chunk);
} catch {
// intentionally ignore because we're already terminating
}
},
close() {
controller.terminate();
},
abort(reason) {
controller.error(reason);
},
}),
);
proc.status.then(
async (status) => {
if (status.success) {
await proc.stderr.cancel();
} else {
const text = await toText(proc.stderr);
controller.error(new Error(text));
}
},
(err) => {
controller.error(err);
},
);
},
async transform(chunk) {
await stdin.write(chunk);
},
async flush() {
await stdin.close();
await writer;
},
async cancel(reason) {
await stdin.abort(reason);
proc.kill();
},
});
}
export function createDecompressionStream(
algorithm: CompressionAlgorithm,
): TransformStream<Uint8Array, Uint8Array> {
switch (algorithm) {
case "none":
return new TransformStream();
case "gzip":
return new DecompressionStream("gzip");
case "bzip2":
return spawn("bzcat");
case "zstd":
return spawn("zstdcat");
case "xz":
return spawn("xzcat");
default:
throw new Error(
`Unsupported decompression algorithm: ${algorithm satisfies never}`,
);
}
}
export function getCompressionAlgorithmFromExtension(
name: string,
): CompressionAlgorithm {
if (name.endsWith("gz")) return "gzip";
if (name.endsWith("bz2")) return "bzip2";
if (name.endsWith("zst")) return "zstd";
if (name.endsWith("xz")) return "xz";
return "none";
}