├── .gitignore ├── .travis.yml ├── Gruntfile.js ├── LICENSE ├── README.md ├── TESTS.md ├── angleman.png ├── cli.js ├── package.json ├── test └── _general.js └── wget.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "0.12" 5 | - "4.1" 6 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt){ 2 | 3 | grunt.loadNpmTasks('grunt-bump'); 4 | 5 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 angleman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wgetjs [![NPM version](https://badge.fury.io/js/wgetjs.png?branch=master)](http://badge.fury.io/js/wgetjs) [![Build Status](https://travis-ci.org/angleman/wgetjs.png?branch=master)](https://travis-ci.org/angleman/wgetjs) [![Dependency Status](https://gemnasium.com/angleman/wgetjs.png?branch=master)](https://gemnasium.com/angleman/wgetjs) [![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](#licensemit) 2 | Ultra simple async retrieval of remote files over http or https inspired by wgetjs 3 | 4 | ## Install 5 | 6 | ``` 7 | npm install node-wget 8 | ``` 9 | 10 | ## Usage 11 | 12 | ```javascript 13 | var wget = require('node-wget'); 14 | 15 | wget(url); 16 | 17 | wget(url, callback); 18 | 19 | wget({url: url, dest: destination_folder_or_filename}, callback); 20 | 21 | wget({url: url, dry: true}); // dry run, nothing loaded, callback passing parsed options as data 22 | ``` 23 | 24 | ## Examples 25 | 26 | ```javascript 27 | var wget = require('node-wget'); 28 | 29 | wget('https://raw.github.com/angleman/wgetjs/master/angleman.png'); // angleman.png saved to current folder 30 | 31 | wget({ 32 | url: 'https://raw.github.com/angleman/wgetjs/master/package.json', 33 | dest: '/tmp/', // destination path or path with filenname, default is ./ 34 | timeout: 2000 // duration to wait for request fulfillment in milliseconds, default is 2 seconds 35 | }, 36 | function (error, response, body) { 37 | if (error) { 38 | console.log('--- error:'); 39 | console.log(error); // error encountered 40 | } else { 41 | console.log('--- headers:'); 42 | console.log(response.headers); // response headers 43 | console.log('--- body:'); 44 | console.log(body); // content of package 45 | } 46 | } 47 | ); 48 | 49 | // dry run 50 | wget({ 51 | url: 'https://raw.github.com/angleman/wgetjs/master/package.json', 52 | dest: '/tmp/', 53 | dry: true 54 | }, function(err, data) { // data: { headers:{...}, filepath:'...' } 55 | console.log('--- dry run data:'); 56 | console.log(data); // '/tmp/package.json' 57 | } 58 | ); 59 | ``` 60 | 61 | ## CLI 62 | 63 | Install: 64 | 65 | ```bash 66 | $ npm install -g node-wget 67 | ``` 68 | 69 | Use: 70 | 71 | ```text 72 | Usage: wget [options] 73 | 74 | Ultra simple async retrieval of remote files over http or https 75 | 76 | Options: 77 | 78 | -h, --help output usage information 79 | -v, --version output version number 80 | -d, --destination specify download destination 81 | 82 | Usage: 83 | 84 | # Download file 85 | $ wget https://github.com/NodeOS/NodeOS/archive/master.zip 86 | 87 | # Download file to location 88 | $ wget https://github.com/NodeOS/NodeOS/archive/master.zip -d path/to/here/ 89 | ``` 90 | 91 | ## License: MIT 92 | -------------------------------------------------------------------------------- /TESTS.md: -------------------------------------------------------------------------------- 1 | # TOC 2 | - [wgetjs](#wgetjs) 3 | 4 | 5 | 6 | # wgetjs 7 | 8 | ## should 9 | load. 10 | 11 | ```js 12 | wget = require('../wget.js'); 13 | should.exist(wget); 14 | ``` 15 | 16 | return relative filepath: ./angleman.png. 17 | 18 | ```js 19 | var testpath = wget({dry: true, url: src_url}); 20 | should.exist(testpath); 21 | testpath.should.equal(rel_path); 22 | ``` 23 | 24 | return absolute filepath: /tmp/angleman.png. 25 | 26 | ```js 27 | var testpath = wget({dry: true, dest: dst_dir, url: src_url}); 28 | should.exist(testpath); 29 | testpath.should.equal(dst_path); 30 | ``` 31 | 32 | load /tmp/angleman.png from https://raw.github.com/angleman/wgetjs/master/angleman.png. 33 | 34 | ```js 35 | should.not.exist(holderr); 36 | should.exist(holddata); 37 | should.exist(holddata.filepath); 38 | holddata.filepath.should.equal(dst_path) 39 | ``` 40 | 41 | -------------------------------------------------------------------------------- /angleman.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TylerLeonhardt/wgetjs/d3b57102cf4a3711fb41f41eb647b68fc6e297f9/angleman.png -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | 4 | /* eslint-disable no-process-exit */ 5 | 6 | /* 7 | * Dependencies. 8 | */ 9 | 10 | var wget = require('./'); 11 | var pack = require('./package.json'); 12 | 13 | /* 14 | * Arguments. 15 | */ 16 | 17 | var argv = process.argv.slice(2); 18 | 19 | /* 20 | * Command. 21 | */ 22 | 23 | var command = Object.keys(pack.bin)[0]; 24 | 25 | /** 26 | * Help. 27 | * 28 | * @return {string} 29 | */ 30 | function help() { 31 | return [ 32 | '', 33 | 'Usage: ' + command + ' [options] [url]...', 34 | '', 35 | pack.description, 36 | '', 37 | 'Options:', 38 | '', 39 | ' -h, --help output usage information', 40 | ' -v, --version output version number', 41 | '', 42 | 'Usage:', 43 | '', 44 | '# Download file', 45 | '$ ' + command + ' https://github.com/NodeOS/NodeOS/archive/master.zip', 46 | '' 47 | ].join('\n ') + '\n'; 48 | } 49 | 50 | /* 51 | * Program. 52 | */ 53 | 54 | if ( 55 | argv.indexOf('--help') !== -1 || 56 | argv.indexOf('-h') !== -1 57 | ) { 58 | console.log(help()); 59 | } else if ( 60 | argv.indexOf('--version') !== -1 || 61 | argv.indexOf('-v') !== -1 62 | ) { 63 | console.log(pack.version); 64 | } else if (argv.length) { 65 | var destinationIndex = argv.indexOf('--destination') + argv.indexOf('-d') + 2; 66 | 67 | var args = {}; 68 | if(destinationIndex){ 69 | args.dest = argv[destinationIndex]; 70 | argv.splice(destinationIndex-1,2); 71 | } 72 | args.url = firstNonFlag(argv); 73 | if(args.url.length > 0){ 74 | console.log("Downloading..."); 75 | wget(args, callback); 76 | }else{ 77 | console.log(help()); 78 | } 79 | } else { 80 | console.log(help()); 81 | } 82 | 83 | function callback(error, response, body){ 84 | if(error){ 85 | console.log('--- error:'); 86 | console.log(error); 87 | }else{ 88 | console.log('Done!'); 89 | } 90 | } 91 | 92 | function firstNonFlag(args){ 93 | for(var i = 0; i < args.length; i++){ 94 | if(args[i].charAt(0) != '-'){ 95 | return args[i]; 96 | } 97 | } 98 | return ""; 99 | } 100 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-wget", 3 | "version": "0.4.3", 4 | "description": "Ultra simple async retrieval of remote files over http or https", 5 | "main": "wget.js", 6 | "scripts": { 7 | "test": "mocha -R list test/*.js", 8 | "TESTS.md": "mocha -R markdown test/*.js > TESTS.md" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/tylerl0706/wgetjs.git" 13 | }, 14 | "keywords": [ 15 | "curl", 16 | "wget", 17 | "download", 18 | "file", 19 | "http", 20 | "https" 21 | ], 22 | "author": "tylerl0706", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/tylerl0706/wgetjs/issues" 26 | }, 27 | "engines": { 28 | "node": ">=0.8.6" 29 | }, 30 | "bin": { 31 | "wget": "cli.js" 32 | }, 33 | "dependencies": { 34 | "request": "~2.85.0" 35 | }, 36 | "devDependencies": { 37 | "license-md": "~0.3.6", 38 | "grunt": "~1.0.2", 39 | "grunt-bump": "~0.8.0", 40 | "grunt-license": "~0.1.5", 41 | "mocha": "~5.0.4", 42 | "should": "~13.2.1" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test/_general.js: -------------------------------------------------------------------------------- 1 | var should = require('should') 2 | , fs = require('fs') 3 | , dst_dir = '/tmp/' 4 | , filename = 'angleman.png' 5 | , dst_path = dst_dir + filename 6 | , rel_path = './' + filename 7 | , src_path = 'https://raw.github.com/angleman/wgetjs/master/' 8 | // , src_path = '~/projects/' 9 | , src_url = src_path + filename 10 | , wget 11 | ; 12 | 13 | 14 | 15 | describe('wgetjs', function() { 16 | describe('should', function() { 17 | 18 | it("load", function(){ 19 | wget = require('../wget.js'); 20 | should.exist(wget); 21 | }); 22 | 23 | wget = require('../wget.js'); 24 | 25 | it("return relative filepath: " + rel_path, function(){ 26 | var testpath = wget({dry: true, url: src_url}); 27 | should.exist(testpath); 28 | testpath.should.equal(rel_path); 29 | }); 30 | 31 | it("return absolute filepath: " + dst_path, function(){ 32 | var testpath = wget({dry: true, dest: dst_dir, url: src_url}); 33 | should.exist(testpath); 34 | testpath.should.equal(dst_path); 35 | }); 36 | 37 | var flag = false; 38 | var url = src_url; 39 | var holderr, holdres, holddata = undefined; 40 | 41 | beforeEach(function(done){ 42 | this.timeout(15 * 60 * 1000); // give it 15 seconds instead of 2 43 | 44 | wget({url: src_url, dest: dst_path}, function(err, data) { 45 | holderr = err; 46 | holddata = data; 47 | done(); // complete the async beforeEach 48 | }); 49 | 50 | }); 51 | 52 | it("load " + dst_path + " from " + src_url, function(){ 53 | should.not.exist(holderr); 54 | should.exist(holddata); 55 | should.exist(holddata.filepath); 56 | holddata.filepath.should.equal(dst_path) 57 | }); 58 | 59 | }); 60 | 61 | }); -------------------------------------------------------------------------------- /wget.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'), 2 | request = require('request') // mikeal/request 3 | ; 4 | 5 | 6 | function wget(options, callback) { 7 | if (typeof options === 'string') { 8 | options = { url: options }; 9 | } 10 | options = options || {}; 11 | callback = callback || function (){}; 12 | var src = options.url || options.uri || options.src, 13 | parts = src.split('/'), 14 | file = parts[parts.length-1] 15 | ; 16 | parts = file.split('?'); 17 | file = parts[0]; 18 | parts = file.split('#'); 19 | file = parts[0]; 20 | options.dest = options.dest || './'; 21 | if (options.dest.substr(options.dest.length-1,1) == '/') { 22 | options.dest = options.dest + file; 23 | } 24 | 25 | 26 | function handle_request_callback(err, res, body) { 27 | if (err) { 28 | callback(err); 29 | } else { 30 | var data = { 31 | filepath: options.dest 32 | }; 33 | if (res && res.headers) { 34 | data.headers = res.headers; 35 | } 36 | callback(err, data, body); 37 | } 38 | } 39 | 40 | 41 | if (options.dry) { 42 | handle_request_callback(null, {}, { filepath: options.dest }); 43 | return options.dest; 44 | } else { 45 | try { 46 | request(options, handle_request_callback).pipe(fs.createWriteStream(options.dest)); 47 | } catch (err) { 48 | callback(err); 49 | } 50 | } 51 | } 52 | 53 | module.exports = wget; 54 | --------------------------------------------------------------------------------