Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Заменил использование Request на Axios #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 41 additions & 25 deletions cli/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,50 @@ module.exports = function (context) {
let totalBytes = 0;
let receivedBytes = 0;

context.request(this.makeDownloadParams(url))
.on('response', (data) => {
totalBytes = parseInt(data.headers['content-length']);
if (data.statusCode === 404)
//const source = context.axios.CancelToken.source();

context.axios({
method: 'get',
url: this.makeDownloadParams(url),
responseType: 'stream',
//cancelToken: source.token
})
.then((response) => {
totalBytes = parseInt(response.headers['content-length']);
if (response.status === 404)
reject(`Package unavailable on URL ${url}`);
if ((data.statusCode < 200) || data.statusCode > 300)
reject(`Error of downloading package from [${url}]. Response with code ${data.statusCode}.`);
if ((response.status < 200) || response.status > 300)
reject(`Error of downloading package from [${url}]. Response with code ${response.status}.`);
log.progressBegin();

const stream = response.data;
stream.on('data', (chunk) => {
receivedBytes += chunk.length;
log.progress(receivedBytes, totalBytes);
});
stream.on('end', () => {
log.progressEnd();
log.end('Done.');
});
stream.on('error', reject);

stream.pipe(zlib.createGunzip())
.pipe(tar.x({
strip: 1,
C: tryFolder
}))
.on('error', reject)
.on('close', () => {
fs.renameSync(tryFolder, tmpFolder);
success(tmpFolder);
});
})
.on('data', (chunk) => {
receivedBytes += chunk.length;
log.progress(receivedBytes, totalBytes);
})
.on('end', (chunk) => {
log.progressEnd();
log.end('Done.');
})
.on('error', reject)
//.pipe(unzipper.Extract({ path: tmpFolder }))
.pipe(zlib.createGunzip())
.pipe(tar.x({
strip: 1,
C: tryFolder
}))
.on('error', reject)
.on('close', () => {
fs.renameSync(tryFolder, tmpFolder);
success(tmpFolder);
.catch((error) => {
if (context.axios.isCancel(error)) {
log.end('Canceled.');
} else {
reject(error);
}
});
});
}
Expand Down
6 changes: 5 additions & 1 deletion cli/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const path = require('path');
const fs = require('fs');
const os = require('os');

const { default: axios } = require('axios');
const https = require('https');
const agent = new https.Agent({ rejectUnauthorized: false });

const cwd = process.cwd();
const locationCWD = path.resolve(cwd, '_metamodel_');

Expand All @@ -23,7 +27,7 @@ const context = {
downloadCert: // Ссылка на SSL сертификат для скачивания
process.env.ARCHPKG_DOWNLOAD_CERT || null
},
request: require('request'), // Реализация web-запросов
axios: axios.create({httpsAgent:agent}), // Реализация web-запросов
log: require('./log')(), // Реализация системы логирования
path, // Работа с путями
fs, // Функции файловой системы
Expand Down
3 changes: 2 additions & 1 deletion cli/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = function (context) {
const os = context.os;
const fs = context.fs;
const repoAPI = context.repo;
const request = context.request;
//const request = context.request;
const axios = context.axios;

const SEP = path.sep;

Expand Down
35 changes: 17 additions & 18 deletions cli/repo.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
const semver = require('semver');
const { re } = require('semver/internal/re');

module.exports = function (context) {
const log = context.log;

const doRequest = function (url) {
return new Promise(function (resolve, reject) {
try {
context.request(url, function (error, response, body) {
if (!error && (response.statusCode >= 200) && response.statusCode < 300) {
resolve({
statusCode: response.statusCode,
response,
body
});
} else {
reject(error || `Request to ${url} failed with code ${response.statusCode} and body [${body}]`);
}
});
} catch (error) {
reject(error || `Request to ${url} failed.`);
return context.axios.get(url)
.then((response) => {
return {
statusCode: response.status,
response,
body: response.data
};
})
.catch((error) => {
if (error.response) {
throw new Error(`Request to ${url} failed with code ${error.response.status} and body [${error.response.data}]`);
} else {
throw error;
}
});
}
Expand All @@ -42,13 +41,13 @@ module.exports = function (context) {
if (!this.env.token) {
log.begin('Try to get access to repo...');
const response = await doRequest(
this.makeURL(this.routes.access.guestToken).toString()
this.makeURL(this.routes.access.guestToken).toString()
);
const code = response && response.statusCode;
if (response && code !== 201) {
throw new Error(`Error server response with code ${code} and body [${response.body}]`);
}
const content = JSON.parse(response.body);
const content = response.body; //JSON.parse(response.body);
this.env.token = content.token;
log.end(`Access token provided: ${content.token}`);
}
Expand All @@ -64,7 +63,7 @@ module.exports = function (context) {
});
if (response.statusCode !== 200)
throw new Error(`Error of resolve the download link of package ${package}. Response code ${response.statusCode} with body [${response.body}]`);
const content = JSON.parse(response.body);
const content = response.body; //JSON.parse(response.body);
if (content.type === 'built-in') {
log.end(`The package is built-in.`);
return {
Expand Down
Loading