Remove wget to make Filer work in node.js

This commit is contained in:
Ali Al Dallal 2014-09-09 09:25:20 -04:00
parent a9551d8a56
commit adf039943d
7 changed files with 0 additions and 317 deletions

View File

@ -1167,9 +1167,6 @@ var sh = fs.Shell();
* [sh.rm(path, [options], callback)](#rm) * [sh.rm(path, [options], callback)](#rm)
* [sh.tempDir(callback)](#tempDir) * [sh.tempDir(callback)](#tempDir)
* [sh.mkdirp(path, callback)](#mkdirp) * [sh.mkdirp(path, callback)](#mkdirp)
* [sh.wget(path, [options], callback)](#wget)
* [sh.zip(zipfile, paths, [options], callback)](#zip)
* [sh.unzip(zipfile, [options], callback)](#unzip)
#### sh.cd(path, callback)<a name="cd"></a> #### sh.cd(path, callback)<a name="cd"></a>
@ -1376,85 +1373,3 @@ sh.mkdirp('/test/mkdirp', function(err) {
// the root '/' now contains a directory 'test' containing the directory 'mkdirp' // the root '/' now contains a directory 'test' containing the directory 'mkdirp'
}); });
``` ```
#### sh.wget(url, [options], callback)<a name="wget"></a>
Downloads the file at `url` and saves it to the filesystem.
The file is saved to a file named with the filename portion of the url
unless the `options.filename` is present, in which case that
filename is used instead. The callback receives `(error, path)`,
where `path` is the full path to the downloaded file.
Example:
```javascript
// Download the file at /files/file.json
sh.wget('/files/file.json', function(err, path) {
if(err) throw err;
// /file.json is now saved to the fs
});
// Download the file at /files/file.json, specifying a filename
sh.wget('/data?id=17', {filename: 'file.json'}, function(err, path) {
if(err) throw err;
// /file.json is now saved to the fs
});
```
#### sh.zip(zipfile, paths, [options], callback)]<a name="zip"></a>
Creates a zip archive named `zipfile` using the paths (files, dirs) listed in `paths`.
Valid options include `recursive=true`, which when set, causes directories to be followed
deeply. The `paths` argument must either be a single path (String) or a list of paths
(Array of String). The zip archive file, named in `zipfile`, must not exist or an error
will be returned on the callback. The callback receives `(error)`.
Examples:
```javascript
// Compress a single file
sh.zip('/data.zip', '/data.txt', function(err) {
if(err) throw err;
// /data.zip is now the compressed archive of /data.txt
});
// Compress multiple files
sh.zip('/data.zip', ['/data.txt', '/data2.txt'], function(err) {
if(err) throw err;
// /data.zip is now the compressed archive of /data.txt and /data2.txt
});
// Compress the entire filesystem, starting at the root /
sh.zip('/fs-backup.zip', '/', { recursive: true }, function(err) {
if(err) throw err;
// /fs-backup.zip now contains the entire filesystem
});
```
#### sh.unzip(zipfile, [options], callback)]<a name="zip"></a>
Extracts files and directories from a zip archive named `zipfile`. If `zipfile` does not
exist, an error is returned on the callback. Valid options include `destination`, which
is the path to use when extracting the files (defaults to the shell's current working directory).
If an optional `destination` path is specified, it must first exist. The callback receives `(error)`.
Examples:
```javascript
// Extract files in /backup.zip to the current directory
sh.unzip('/backup.zip', function(err) {
if(err) throw err;
// The current working directory now contains all files archived in backup.zip
});
// Extract files in /backup.zip to the /backup directory
sh.mkdirp('/backup', function(err) {
if(err) throw err;
sh.unzip('/backup.zip', { destination: '/backup' }, function(err) {
if(err) throw err;
// The current working directory now contains all files archived in backup.zip
});
});
```

View File

@ -24,13 +24,8 @@
"type": "git", "type": "git",
"url": "https://github.com/js-platform/filer.git" "url": "https://github.com/js-platform/filer.git"
}, },
"browser": {
"request": "browser-request"
},
"dependencies": { "dependencies": {
"bower": "~1.3.8", "bower": "~1.3.8",
"request": "^2.36.0",
"browser-request": "git://github.com/humphd/browser-request.git#959ea95bf200d64939ed76897d3b06bb684f3a0d",
"base64-arraybuffer": "^0.1.2" "base64-arraybuffer": "^0.1.2"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,22 +0,0 @@
var request = require('request');
module.exports.download = function(uri, callback) {
request({
url: uri,
method: 'GET',
encoding: null
}, function(err, msg, body) {
var statusCode;
var error;
msg = msg || null;
statusCode = msg && msg.statusCode;
error = statusCode !== 200 ? { message: err || 'Not found!', code: statusCode } : null;
if (error) {
callback(error, null);
return;
}
callback(null, body);
});
};

View File

@ -2,7 +2,6 @@ var Path = require('../path.js');
var Errors = require('../errors.js'); var Errors = require('../errors.js');
var Environment = require('./environment.js'); var Environment = require('./environment.js');
var async = require('../../lib/async.js'); var async = require('../../lib/async.js');
var Network = require('./network.js');
var Encoding = require('../encoding.js'); var Encoding = require('../encoding.js');
function Shell(fs, options) { function Shell(fs, options) {
@ -427,52 +426,4 @@ Shell.prototype.mkdirp = function(path, callback) {
_mkdirp(path, callback); _mkdirp(path, callback);
}; };
/**
* Downloads the file at `url` and saves it to the filesystem.
* The file is saved to a file named with the current date/time
* unless the `options.filename` is present, in which case that
* filename is used instead. The callback receives (error, path).
*/
Shell.prototype.wget = function(url, options, callback) {
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
if(!url) {
callback(new Errors.EINVAL('Missing url argument'));
return;
}
// Grab whatever is after the last / (assuming there is one). Like the real
// wget, we leave query string or hash portions in tact. This assumes a
// properly encoded URL.
// i.e. instead of "/foo?bar/" we would expect "/foo?bar%2F"
var path = options.filename || url.split('/').pop();
path = Path.resolve(sh.pwd(), path);
function onerror() {
callback(new Error('unable to get resource'));
}
Network.download(url, function(err, data) {
if (err || !data) {
return onerror();
}
fs.writeFile(path, data, function(err) {
if(err) {
callback(err);
} else {
callback(null, path);
}
});
});
};
module.exports = Shell; module.exports = Shell;

View File

@ -54,8 +54,6 @@ require("./spec/shell/ls.spec");
require("./spec/shell/rm.spec"); require("./spec/shell/rm.spec");
require("./spec/shell/env.spec"); require("./spec/shell/env.spec");
require("./spec/shell/mkdirp.spec"); require("./spec/shell/mkdirp.spec");
require("./spec/shell/wget.spec");
require("./spec/shell/network.spec");
// Ported node.js tests (filenames match names in https://github.com/joyent/node/tree/master/test) // Ported node.js tests (filenames match names in https://github.com/joyent/node/tree/master/test)
require("./spec/node-js/simple/test-fs-mkdir"); require("./spec/node-js/simple/test-fs-mkdir");

View File

@ -1,59 +0,0 @@
var network = require('../../../src/shell/network.js');
var expect = require('chai').expect;
describe('Network module', function() {
var uri;
if (typeof XMLHttpRequest === 'undefined') {
// Node context
uri = {
valid: 'http://localhost:1234/package.json',
invalid: 'booyah!',
notFound: 'http://localhost:1234/this-isnt-real'
}
} else {
// Browser context
uri = {
valid: '../package.json',
invalid: 'asdf://booyah!',
notFound: 'this-isnt-real'
};
}
it('should get an error when a non-existent path is specified', function(done) {
network.download(uri.notFound, function(error, data) {
expect(error).to.exist;
expect(error.code).to.eql(404);
expect(data).to.be.eql(null);
done();
});
});
if (typeof XMLHttpRequest === 'undefined') {
it('in nodejs, should get an error when an invalid URI is specified', function(done) {
network.download(uri.invalid, function(error, data) {
expect(error).to.exist;
expect(error.code).to.eql(null);
expect(error.message).to.exist;
expect(data).to.be.eql(null);
done();
});
});
} else {
it('in a browser, should throw an error when an invalid URI is specified', function(done) {
expect(function(){
network.download(uri.invalid, function() {});
}).to.throwError;
done();
});
}
it('should download a resource from the server', function(done) {
network.download(uri.valid, function(error, data) {
expect(error).not.to.exist;
expect(data).to.exist;
expect(data).to.have.length.above(0);
done();
});
});
});

View File

@ -1,95 +0,0 @@
var Filer = require('../../..');
var util = require('../../lib/test-utils.js');
var expect = require('chai').expect;
describe('FileSystemShell.wget', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should be a function', function() {
var shell = util.shell();
expect(shell.wget).to.be.a('function');
});
it('should fail when url argument is absent', function(done) {
var fs = util.fs();
var shell = fs.Shell();
shell.wget(null, function(err, data) {
expect(err).to.exist;
expect(data).not.to.exist;
done();
});
});
it('should fail when the url does not exist (404)', function(done) {
var fs = util.fs();
var shell = fs.Shell();
shell.wget("no-such-url", function(err, data) {
expect(err).to.exist;
expect(data).not.to.exist;
done();
});
});
it('should download the contents of a file from a url to default filename', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-file.txt" : "/tests/test-file.txt";
var contents = "This is a test file used in some of the tests.\n";
shell.wget(url, function(err, path) {
if(err) throw err;
expect(path).to.equal('/test-file.txt');
fs.readFile(path, 'utf8', function(err, data) {
if(err) throw err;
expect(data).to.equal(contents);
done();
});
});
});
it('should download the contents of a file from a url to specified filename', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-file.txt" : "/tests/test-file.txt";
var contents = "This is a test file used in some of the tests.\n";
shell.wget(url, { filename: 'test-file.txt' }, function(err, path) {
if(err) throw err;
expect(path).to.equal('/test-file.txt');
fs.readFile(path, 'utf8', function(err, data) {
if(err) throw err;
expect(data).to.equal(contents);
done();
});
});
});
it('should download the contents of a file from a url with query string', function(done) {
var fs = util.fs();
var shell = fs.Shell();
var url = typeof XMLHttpRequest === "undefined" ? "http://localhost:1234/tests/test-file.txt?foo" : "/tests/test-file.txt?foo";
var contents = "This is a test file used in some of the tests.\n";
shell.wget(url, function(err, path) {
if(err) throw err;
expect(path).to.equal('/test-file.txt?foo');
fs.readFile(path, 'utf8', function(err, data) {
if(err) throw err;
expect(data).to.equal(contents);
done();
});
});
});
});