-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinflate_response.ts
75 lines (62 loc) · 2.35 KB
/
inflate_response.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
import { resolve as resolvePath } from "@std/path";
import { UntarStream } from "@std/tar/untar-stream";
import { ensureFile } from "@std/fs/ensure_file";
import { ensureDir } from "@std/fs/ensure_dir";
import * as path from "@std/path";
const supportedCompressionFormats = ["deflate", "gzip", "deflate-raw"] as const;
type CompressionFormat = (typeof supportedCompressionFormats)[number];
type InflateResponseOptions = {
compressionFormat?: CompressionFormat;
doUntar?: boolean;
};
export async function inflateResponse(
response: Response,
inflateDestination: string,
options?: InflateResponseOptions,
) {
const { compressionFormat = "gzip", doUntar = false } = options || {};
if (!supportedCompressionFormats.includes(compressionFormat)) {
throw new Error(`Unsupported compression format: "${compressionFormat}"`);
}
const decompressionStream = new DecompressionStream(compressionFormat);
if (!response.body) {
throw new Error("Response is not readable");
}
if (!response.ok) {
throw new Error(
`Response ${response.status} not ok: ${response.statusText}`,
);
}
if (doUntar) { // multiple files in tarball
const tarballPath = resolvePath(inflateDestination + ".tar");
using inflatedTarball = await Deno.open(resolvePath(tarballPath), {
create: true,
write: true,
});
const decompressedStream = response.body.pipeThrough(decompressionStream);
await decompressedStream.pipeTo(inflatedTarball.writable);
using inflatedTarFile = await Deno.open(tarballPath, { read: true });
const entries = inflatedTarFile.readable.pipeThrough(new UntarStream());
for await (const entry of entries) {
if (entry.readable === undefined) {
await ensureDir(entry.path);
continue;
}
await ensureDir(inflateDestination);
await ensureFile(path.join(inflateDestination, entry.path));
using file = await Deno.open(
path.join(inflateDestination, entry.path),
{ write: true },
);
// <entry> is a reader.
await entry.readable.pipeTo(file.writable);
}
} else { // single file
using inflatedFile = await Deno.open(resolvePath(inflateDestination), {
create: true,
write: true,
});
const decompressedStream = response.body.pipeThrough(decompressionStream);
await decompressedStream.pipeTo(inflatedFile.writable);
}
}