@wordbricks/cva 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/install.js +6 -4
- package/lib/install.test.js +54 -0
- package/package.json +1 -1
package/lib/install.js
CHANGED
|
@@ -16,7 +16,7 @@ async function downloadFile(url, destination) {
|
|
|
16
16
|
const request = https.get(url, (response) => {
|
|
17
17
|
if (response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) {
|
|
18
18
|
response.resume();
|
|
19
|
-
downloadFile(response.headers.location, destination)
|
|
19
|
+
resolve(downloadFile(response.headers.location, destination));
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
22
|
if (response.statusCode !== 200) {
|
|
@@ -27,13 +27,14 @@ async function downloadFile(url, destination) {
|
|
|
27
27
|
|
|
28
28
|
const output = fs.createWriteStream(tempFile, { mode: 0o755 });
|
|
29
29
|
pipeline(response, output)
|
|
30
|
-
.then(
|
|
30
|
+
.then(() => {
|
|
31
|
+
fs.renameSync(tempFile, destination);
|
|
32
|
+
resolve();
|
|
33
|
+
})
|
|
31
34
|
.catch(reject);
|
|
32
35
|
});
|
|
33
36
|
request.on("error", reject);
|
|
34
37
|
});
|
|
35
|
-
|
|
36
|
-
fs.renameSync(tempFile, destination);
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
async function ensureBinary(options = {}) {
|
|
@@ -74,5 +75,6 @@ if (require.main === module) {
|
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
module.exports = {
|
|
78
|
+
downloadFile,
|
|
77
79
|
ensureBinary
|
|
78
80
|
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const test = require("node:test");
|
|
2
|
+
const assert = require("node:assert/strict");
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const os = require("node:os");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const { PassThrough } = require("node:stream");
|
|
7
|
+
const https = require("node:https");
|
|
8
|
+
|
|
9
|
+
const { downloadFile } = require("./install");
|
|
10
|
+
|
|
11
|
+
test("downloadFile follows redirects without double-renaming the temp file", async () => {
|
|
12
|
+
const originalGet = https.get;
|
|
13
|
+
const calls = [];
|
|
14
|
+
|
|
15
|
+
https.get = (url, callback) => {
|
|
16
|
+
calls.push(url);
|
|
17
|
+
const response = new PassThrough();
|
|
18
|
+
if (calls.length === 1) {
|
|
19
|
+
response.statusCode = 302;
|
|
20
|
+
response.headers = { location: "https://example.com/final" };
|
|
21
|
+
process.nextTick(() => {
|
|
22
|
+
callback(response);
|
|
23
|
+
response.end();
|
|
24
|
+
});
|
|
25
|
+
} else {
|
|
26
|
+
response.statusCode = 200;
|
|
27
|
+
response.headers = {};
|
|
28
|
+
process.nextTick(() => {
|
|
29
|
+
callback(response);
|
|
30
|
+
response.end("binary-data");
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
on() {
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "cva-install-test-"));
|
|
41
|
+
const destination = path.join(tempDir, "cva");
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
await downloadFile("https://example.com/redirect", destination);
|
|
45
|
+
assert.equal(fs.readFileSync(destination, "utf8"), "binary-data");
|
|
46
|
+
assert.deepEqual(calls, [
|
|
47
|
+
"https://example.com/redirect",
|
|
48
|
+
"https://example.com/final"
|
|
49
|
+
]);
|
|
50
|
+
} finally {
|
|
51
|
+
https.get = originalGet;
|
|
52
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
53
|
+
}
|
|
54
|
+
});
|
package/package.json
CHANGED