-
Notifications
You must be signed in to change notification settings - Fork 18
/
archive.js
65 lines (51 loc) · 2.02 KB
/
archive.js
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
export default normalizeArchive;
import { createGzip } from "zlib";
import intoStream from "into-stream";
import { getStreamAsBuffer } from "get-stream";
import { extract as _extract, pack as _pack } from "tar-stream";
import gunzip from "gunzip-maybe";
import { regex } from "../temporary-repository.js";
async function normalizeArchive(scenarioState, response, fixture) {
fixture.headers["content-disposition"] = fixture.headers[
"content-disposition"
]
// normalize folder name in file name
.replace(regex, "$1")
// zero sha
.replace(/archive-\w{7}/, "archive-0000000");
const extract = _extract();
const pack = _pack();
const readStream = intoStream(Buffer.from(response, "hex"));
// The response is the Repository folder with the README.md file inside. The
// folder name is always different, based on the repository name when recorded.
// That's why we have to untar/zip the response, change the folder name and
// retar/zip it again.
extract.on("entry", function (header, stream, callback) {
header.name = header.name
// normalize folder name in path
.replace(regex, "$1")
// zero sha in path
.replace(/-(\w){7}\//, "-0000000/");
// normalize mtime
header.mtime = {
getTime: () => 1507651200000,
};
// write the new entry to the pack stream
stream.pipe(pack.entry(header, callback));
});
extract.on("finish", function () {
// all entries done - lets finalize it
pack.finalize();
});
// pipe the old tarball to the extractor
readStream.pipe(gunzip()).pipe(extract);
// pipe the new tarball the another stream
const writeStream = pack.pipe(createGzip());
const result = await getStreamAsBuffer(writeStream).catch(console.log);
fixture.response = result.toString("hex");
// normalize across operating systems / extra flags
// see http://www.zlib.org/rfc-gzip.html#header-trailer
const normalizedHeader = "1f8b0800000000000003";
fixture.response =
normalizedHeader + fixture.response.substr(normalizedHeader.length);
}