You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2013/07/12 04:08:48 UTC

git commit: [CB-4182] Delay in lazy load module. Simplified with a stream implementation.

Updated Branches:
  refs/heads/master 062849f94 -> 6f99515bc


[CB-4182] Delay in lazy load module. Simplified with a stream implementation.


Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/6f99515b
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/6f99515b
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/6f99515b

Branch: refs/heads/master
Commit: 6f99515bccf64e176bd97b60a7e120162e81ad6c
Parents: 062849f
Author: Fil Maj <ma...@gmail.com>
Authored: Thu Jul 11 19:08:01 2013 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Thu Jul 11 19:08:01 2013 -0700

----------------------------------------------------------------------
 spec/lazy_load.spec.js | 57 +++++++++-------------------
 src/lazy_load.js       | 91 ++++++++++++---------------------------------
 2 files changed, 42 insertions(+), 106 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6f99515b/spec/lazy_load.spec.js
----------------------------------------------------------------------
diff --git a/spec/lazy_load.spec.js b/spec/lazy_load.spec.js
index 95bab3f..1605240 100644
--- a/spec/lazy_load.spec.js
+++ b/spec/lazy_load.spec.js
@@ -4,7 +4,7 @@ var lazy_load = require('../src/lazy_load'),
     shell = require('shelljs'),
     path = require('path'),
     hooker = require('../src/hooker'),
-    https = require('follow-redirects').https,
+    request = require('request'),
     fs = require('fs'),
     platforms = require('../platforms');
 
@@ -54,50 +54,29 @@ describe('lazy_load module', function() {
             expect(fire).toHaveBeenCalledWith('before_library_download', {platform:'platform X', url:'some url', id:'some id', version:'three point five'}, jasmine.any(Function));
         });
 
-        describe('remove URLs for libraries', function() {
-            var req, write_stream, http_on;
-            beforeEach(function() {
-                write_spy = jasmine.createSpy('write stream write');
-                write_stream = spyOn(fs, 'createWriteStream').andReturn({
-                    write:write_spy
+        describe('remote URLs for libraries', function() {
+            var req,
+                p1 = jasmine.createSpy().andReturn({
+                    on:function() {
+                        return {
+                            on:function(){}
+                        }
+                    }
                 });
-                http_on = jasmine.createSpy('https result on');
-                // TODO: jasmien does not support chaning both andCallFake + andReturn...
-                req = spyOn(https, 'request').andCallFake(function(opts, cb) {
-                    cb({
-                        on:http_on
-                    });
-                }).andReturn({
-                    on:function(){},
-                   end:function(){}
+            var p2 = jasmine.createSpy().andReturn({pipe:p1});
+            beforeEach(function() {
+                req = spyOn(request, 'get').andReturn({
+                    pipe:p2
                 });
             });
 
-            it('should call into https request with appopriate url params', function() {
-                lazy_load.custom('https://github.com/apache/someplugin', 'random', 'android', '1.0');
+            it('should call request with appopriate url params', function() {
+                var url = 'https://github.com/apache/someplugin';
+                lazy_load.custom(url, 'random', 'android', '1.0');
                 expect(req).toHaveBeenCalledWith({
-                    hostname:'github.com',
-                    path:'/apache/someplugin'
+                    uri:url
                 }, jasmine.any(Function));
             });
-            // TODO: jasmine does not support chaning andCallFake andReturn. Cannot test the below.
-            xit('should fire download events as the https request receives data events, and write to a file stream', function() {
-                http_on.andCallFake(function(evt, cb) {
-                    console.log(evt);
-                    if (evt == 'data') {
-                        cb('chunk');
-                    }
-                });
-                lazy_load.custom('https://github.com/apache/someplugin', 'random', 'android', '1.0');
-                expect(fire).toHaveBeenCalledWith('library_download', {
-                    platform:'android',
-                    url:'https://github.com/apache/someplugin',
-                    id:'random',
-                    version:'1.0',
-                    chunk:'chunk'
-                });
-                expect(write_spy).toHaveBeenCalledWith('chunk', 'binary');
-            });
         });
 
         describe('local paths for libraries', function() {
@@ -107,7 +86,7 @@ describe('lazy_load module', function() {
             });
             it('should fire after hook once done', function() {
                 lazy_load.custom('/some/random/lib', 'id', 'X', 'three point five')
-                expect(fire).toHaveBeenCalledWith('after_library_download', {platform:'X',url:'/some/random/lib',id:'id',version:'three point five',path:path.join(util.libDirectory, 'X', 'id', 'three point five')}, jasmine.any(Function));
+                expect(fire).toHaveBeenCalledWith('after_library_download', {platform:'X',url:'/some/random/lib',id:'id',version:'three point five',path:path.join(util.libDirectory, 'X', 'id', 'three point five'), symlink:true}, jasmine.any(Function));
             });
         });
     });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/6f99515b/src/lazy_load.js
----------------------------------------------------------------------
diff --git a/src/lazy_load.js b/src/lazy_load.js
index d49cef0..3ca566e 100644
--- a/src/lazy_load.js
+++ b/src/lazy_load.js
@@ -21,6 +21,7 @@ var path          = require('path'),
     shell         = require('shelljs'),
     platforms     = require('../platforms'),
     events        = require('./events'),
+    request       = require('request'),
     config        = require('./config'),
     hooker        = require('./hooker'),
     https         = require('follow-redirects').https,
@@ -63,79 +64,34 @@ module.exports = {
         }, function() {
             var uri = URL.parse(url);
             if (uri.protocol) {
-                shell.mkdir(download_dir);
-                // assuming its remote
-                var filename = path.join(download_dir, id+'-'+platform+'-'+version+'.tar.gz');
-                if (fs.existsSync(filename)) {
-                    shell.rm(filename);
-                }
-                var req_opts = {
-                    hostname: uri.hostname,
-                    path: uri.path
-                };
+                shell.mkdir('-p', download_dir);
                 events.emit('log', 'Requesting ' + url + '...');
-                // TODO: may not be an https request..
-                var req = https.request(req_opts, function(res) {
-                    var downloadfile = fs.createWriteStream(filename, {'flags': 'a'});
-
-                    res.on('data', function(chunk){
-                        downloadfile.write(chunk, 'binary');
-                        hooker.fire('library_download', {
+                var size = 0;
+                request.get({uri:url}, function(err, req, body) { size = body.length; })
+                    .pipe(zlib.createUnzip())
+                    .pipe(tar.Extract({path:download_dir}))
+                    .on('error', function(err) {
+                        if (callback) callback(err);
+                        else throw err;
+                    })
+                    .on('end', function() {
+                        events.emit('log', 'Downloaded, unzipped and extracted ' + size + ' byte response.');
+                        var entries = fs.readdirSync(download_dir);
+                        var entry = path.join(download_dir, entries[0]);
+                        shell.mv('-f', path.join(entry, (platform=='blackberry'?'blackberry10':''), '*'), download_dir);
+                        shell.rm('-rf', entry);
+                        hooker.fire('after_library_download', {
                             platform:platform,
                             url:url,
                             id:id,
                             version:version,
-                            chunk:chunk
-                        });
-                    });
-
-                    res.on('end', function(){
-                        downloadfile.end();
-                        var payload_size = fs.statSync(filename).size;
-                        events.emit('log', 'Download complete. Extracting...');
-                        var tar_path = path.join(download_dir, id+'-'+platform+'-'+version+'.tar');
-                        var tarfile = fs.createWriteStream(tar_path);
-                        tarfile.on('error', function(err) {
-                            if (callback) callback(err);
-                            else throw err;
+                            path:download_dir,
+                            size:size,
+                            symlink:false
+                        }, function() {
+                            if (callback) callback();
                         });
-                        tarfile.on('finish', function() {
-                            shell.rm(filename);
-                            fs.createReadStream(tar_path)
-                                .pipe(tar.Extract({ path: download_dir }))
-                                .on("error", function (err) {
-                                    if (callback) callback(err);
-                                    else throw err;
-                                })
-                                .on("end", function () {
-                                    shell.rm(tar_path);
-                                    // move contents out of extracted dir
-                                    var entries = fs.readdirSync(download_dir);
-                                    var entry = path.join(download_dir, entries[0]);
-                                    shell.mv('-f', path.join(entry, (platform=='blackberry'?'blackberry10':''), '*'), download_dir);
-                                    shell.rm('-rf', entry);
-                                    hooker.fire('after_library_download', {
-                                        platform:platform,
-                                        url:url,
-                                        id:id,
-                                        version:version,
-                                        path:download_dir,
-                                        size:payload_size
-                                    }, function() {
-                                        if (callback) callback();
-                                    });
-                                });
-                        });
-                        fs.createReadStream(filename)
-                            .pipe(zlib.createUnzip())
-                            .pipe(tarfile);
-                    });
-                });
-                req.on('error', function(err) {
-                    if (callback) return callback(err);
-                    else throw err;
                 });
-                req.end();
             } else {
                 // local path
                 // symlink instead of copying
@@ -145,7 +101,8 @@ module.exports = {
                     url:url,
                     id:id,
                     version:version,
-                    path:download_dir
+                    path:download_dir,
+                    symlink:true
                 }, function() {
                     if (callback) callback();
                 });