New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github-downloader

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github-downloader - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

209

index.js

@@ -10,7 +10,108 @@ var fs = require('fs'),

/**
* function used to output stuff to console.
* if debug function is called in module.exports,
* then this is changed to console.log
*/
var log = function () {};
/**
* User agent used in requests to github
* @type {string}
*/
var userAgent = "";
var count = 0;
/**
* Downloads a github repository and extracts
* @param {object} options
* @param {function} next - callback
* @constructor
*/
var GithubDownloader = function (options, next) {
var self = this;
count = count + 1;
self.id = count;
if(!(this instanceof GithubDownloader)){
log('creating instance');
return new GithubDownloader(options, next);
}
if(!next){
next = function(){};
}
options = options || {};
log(options);
function download(url, next){
log(url);
// check required fields for options
if(typeof options.username !== "string"){
return next(new Error('github user name needs to be a string'));
}
if(typeof options.repo !== "string"){
return next(new Error('githubRepo needs to be a string'));
}
if(typeof options.output !== "string"){
return next(new Error('outputPath needs to be a string'));
}
version = options.version;
if(typeof version !== "undefined"){
if(version === "latest"){
// download latest version
var url = "https://api.github.com/repos/";
url += options.username;
url += '/';
url += options.repo;
url += '/releases/latest';
log(url);
var requestOpts = {
uri : url,
headers: {
'User-Agent': userAgent
}
};
// get url for zip file
request(requestOpts, function (err, data){
if(err) {
log('err ', err);
return next(err);
}
var body = JSON.parse(data.body);
download(body.zipball_url, self.id, function (err) {
if(err) {
return next(err);
}
extract(self.id, options.output, function (err) {
log(err);
log('finished unzipping');
next(err);
});
});
});
} else {
// TODO download version specified
console.log('we only support "latest" for version');
}
} else {
var url = "https://api.github.com/repos/";
url += options.username;
url += '/';
url += options.repo;
url += '/zipball';
log(url);
download(url, self.id, function (err) {
if(err){
return next(err);
}
extract(self.id, options.output, function (err) {
log(err);
log('finished unzipping');
next(err);
});
});
}
};
function download(url, zipName, next){
console.log(url);
var options = {

@@ -22,3 +123,3 @@ url: url,

}
log('temzip=' + __dirname + path.sep + 'test.zip')
log('temzip=' + __dirname + path.sep + zipName + '.zip')
request(options).on('error', function (err){

@@ -30,23 +131,19 @@ log(err);

next();
}).pipe(fs.createWriteStream(__dirname + path.sep + 'test.zip'));
}).pipe(fs.createWriteStream(__dirname + path.sep + zipName + '.zip'));
}
function extract(extractTo, next){
function extract(zipName, extractTo, next){
log('extracting');
try{
yauzl.open(__dirname + path.sep + 'test.zip', function (err, zipfile) {
yauzl.open(__dirname + path.sep + zipName + '.zip', function (err, zipfile) {
if (err) {
send(err);
return;
return next(err);
}
zipfile.once('end', function () {
fs.unlink(__dirname + path.sep + 'test.zip', function (err) {
if (err) {
return err;
}
log('unzipped');
})
fs.unlink(__dirname + path.sep + zipName + '.zip', function (err) {
log('unzipped');
return next(err);
});
});

@@ -57,3 +154,3 @@ // save files and folders

if (/\/$/.test(entry.fileName)) {
// directory file names end with '/'
// directory file names end with '/'. ignore those.
return;

@@ -75,4 +172,3 @@ }

if (err) {
send(err);
return;
return err;
}

@@ -82,3 +178,3 @@

if (err) {
send(err);
return err;
}

@@ -91,3 +187,3 @@

readStream.pipe(fs.createWriteStream(dest).on("error", function (err) {
send(err)
return next(err)
}))

@@ -103,77 +199,8 @@ });

log(e);
return next(e);
}
}
module.exports = function (options, next) {
if(!next){
next = function(){};
}
options = options || {};
console.dir(options);
if(typeof options.username !== "string"){
return next(new Error('github user name needs to be a string'));
}
if(typeof options.repo !== "string"){
return next(new Error('githubRepo needs to be a string'));
}
if(typeof options.output !== "string"){
return next(new Error('outputPath needs to be a string'));
}
version = options.version || "";
if(version !== ""){
if(version === "latest"){
// download latest version
var url = "https://api.github.com/repos/";
url += options.username;
url += '/';
url += options.repo;
url += '/releases/latest';
log(url);
var requestOpts = {
uri : url,
headers: {
'User-Agent': userAgent
}
};
request(requestOpts, function (err, data){
log('err ', err);
var body = JSON.parse(data.body);
download(body.zipball_url, function (err) {
if(err) {
next(err);
return err;
}
extract(options.output, function (err) {
log(err);
log('finished unzipping');
next();
});
});
});
} else {
// TODO download version specified
console.log('we only support "latest" for version');
}
} else {
var url = "https://api.github.com/repos/";
url += options.username;
url += '/';
url += options.repo;
url += '/zipball';
log(url);
download(url, function (err) {
if(err){
next(err);
return err;
}
extract(options.output, function (err) {
log(err);
log('finished unzipping');
next();
});
});
}
}
module.exports = GithubDownloader;
module.exports.setUserAgent = function (_userAgent) {

@@ -180,0 +207,0 @@ userAgent = _userAgent;

{
"name": "github-downloader",
"version": "0.0.2",
"version": "0.0.3",
"description": "Downloads and extracts a github repository",

@@ -5,0 +5,0 @@ "main": "index.js",

var download = require('../index.js');
download.setUserAgent('silk-gui');
download.debug();
function clean (next) {
//download.debug();
function clean (path, next) {
var exec = require('child_process').exec;
exec('rm -r ./tests/output', function(err, data){
console.log(err);
next()
exec('rm -r ./tests/output/' + path, function(err, data){
console.log('rm err ', err);
});

@@ -17,7 +17,7 @@ }

version: 'latest',
output: './tests/output'
output: './tests/output/1'
}, function (err) {
console.log(err);
console.log('err1', err);
console.log('finished 1');
clean(download2);
clean('1');
});

@@ -27,8 +27,11 @@

function download2 () {
download('Silk-GUI', 'Silk', './tests/output');
console.log('finished tests');
clean();
download({username: 'Silk-GUI', repo: 'Silk', output: './tests/output/2'}, function (err) {
console.log('err2', err);
console.log('finished 2');
clean('2');
});
}
download1();
download2();

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc