-
-
Notifications
You must be signed in to change notification settings - Fork 40
fix(snapshot): use request module for http requests #428
Changes from 1 commit
4d092ed
011ce3c
9b8b922
0733d8e
b6239e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
const { chmodSync, createWriteStream, existsSync } = require("fs"); | ||
const { get: httpsGet } = require("https"); | ||
const { tmpdir } = require("os"); | ||
const { dirname, join } = require("path"); | ||
|
||
const { mkdir } = require("shelljs"); | ||
const { get } = require("request"); | ||
|
||
const CONSTANTS = { | ||
SNAPSHOT_TMP_DIR: join(tmpdir(), "snapshot-tools"), | ||
|
@@ -12,49 +13,34 @@ const createDirectory = dir => mkdir('-p', dir); | |
|
||
const downloadFile = (url, destinationFilePath) => | ||
new Promise((resolve, reject) => { | ||
const request = httpsGet(url, response => { | ||
switch (response.statusCode) { | ||
case 200: | ||
const file = createWriteStream(destinationFilePath, {autoClose: true}); | ||
file.on('error', function (error) { | ||
return reject(error); | ||
}); | ||
file.on("finish", function() { | ||
chmodSync(destinationFilePath, 0755); | ||
return resolve(destinationFilePath); | ||
}); | ||
response.pipe(file); | ||
break; | ||
case 301: | ||
case 302: | ||
case 303: | ||
const redirectUrl = response.headers.location; | ||
return this.downloadExecFile(redirectUrl, destinationFilePath); | ||
default: | ||
return reject(new Error("Unable to download file at " + url + ". Status code: " + response.statusCode)); | ||
} | ||
}); | ||
|
||
request.end(); | ||
|
||
request.on('error', function(err) { | ||
return reject(err); | ||
}); | ||
get(url) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any tests for this functionality are preferable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left it without tests, because I thought that the tests for the |
||
.on("error", reject) | ||
.pipe(createWriteStream(destinationFilePath, { autoClose: true })) | ||
.on("finish", _ => { | ||
chmodSync(destinationFilePath, 0755); | ||
resolve(destinationFilePath); | ||
}); | ||
}); | ||
|
||
const getJsonFile = url => | ||
new Promise((resolve, reject) => { | ||
httpsGet(url, res => { | ||
let body = ""; | ||
res.on("data", chunk => { | ||
body += chunk; | ||
}) | ||
get(url, (error, response, body) => { | ||
if (error) { | ||
reject(error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
const { statusCode } = response; | ||
if (statusCode !== 200) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should accept all 2xx codes as success. Also you may receive 3xx (redirect). You may handle this by passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2xx - Correct me if I'm wrong but I'm sure that all the 2xx codes will denote success in this case. For example, status code 204 ('No content') doesn't seem to be ok when we are expecting to fetch a file or receive a json. |
||
reject(`Couldn't fetch ${url}! Response status code: ${statusCode}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case it is a good idea to show the real response. For example when you have proxy that has blocked access to this resource, the response may contain information that the admins of the user have blocked the access and who is responsible for enabling the access. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
res.on("end", () => { | ||
try { | ||
const data = JSON.parse(body); | ||
return resolve(data); | ||
}); | ||
}).on("error", reject); | ||
resolve(data); | ||
} catch (e) { | ||
reject(`Couldn't parse json data! Original error:\n${e}`); | ||
} | ||
}); | ||
}); | ||
|
||
module.exports = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should use strict versions in package.json. This minimizes the risk of being broken by release of another package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll pin the versions.