Skip to content

Commit

Permalink
replace error emitting on methods with callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
ctalkington committed Jan 11, 2013
1 parent 505dbb6 commit 51a7dbf
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
v0.3.0:
date: 2013-01-30
changes:
- (in development)
- replace error emitting on methods with callback.
- add generic create method.
- make tar recordSize configurable for advanced users.
v0.2.2:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ You can also use `npm install https://github.com/ctalkington/node-archiver/archi

Creates an Archiver instance based on the type (ie zip/tar) passed.

#### addFile(inputStream, data, callback)
#### addFile(inputStream, data, callback(err))

Adds a file to the Archiver stream.

#### finalize(callback(written))
#### finalize(callback(err, written))

Finalizes the Archiver stream. When everything is done, callback is called with the total number of bytes in the archive.

Expand Down
19 changes: 13 additions & 6 deletions examples/pack-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ var async = require('async');
var out = fs.createWriteStream('out.zip');
var archive = archiver.createZip();

archive.pipe(out);

archive.on('error', function(err) {
console.log(err);
// then handle exit process or such
});

archive.pipe(out);

async.forEachSeries(['file1.js', 'file2.js'], function(file, next) {
archive.addFile(fs.createReadStream(file), { name: file }, function() {
next();
archive.addFile(fs.createReadStream(file), { name: file }, function(err) {
next(err);

This comment has been minimized.

Copy link
@danmilon

danmilon Jan 11, 2013

Contributor

You can directly pass next.

archive.addFile(fs.createReadStream(file), { name: file }, next)

This comment has been minimized.

Copy link
@ctalkington

ctalkington Jan 11, 2013

Author Member

good point. will be fixed in upcoming commit.

});
}, function(err) {
archive.finalize(function(written) {
if (err) {
throw err;
}

archive.finalize(function(err, written) {
if (err) {
throw err;
}

console.log(written + ' total bytes written');
});
});
22 changes: 19 additions & 3 deletions examples/pack-tar-gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@ var out = fs.createWriteStream('out.tar.gz');
var gzipper = zlib.createGzip();
var archive = archiver.createTar();

archive.on('error', function(err) {
console.log(err);
});

archive.pipe(gzipper).pipe(out);

archive.addFile(fs.createReadStream('file1.js'), {name: 'file1.js'}, function() {
archive.addFile(fs.createReadStream('file2.js'), {name: 'file2.js'}, function() {
archive.finalize(function(written) {
archive.addFile(fs.createReadStream('file1.js'), {name: 'file1.js'}, function(err) {
if (err) {
throw err;
}

archive.addFile(fs.createReadStream('file2.js'), {name: 'file2.js'}, function(err) {
if (err) {
throw err;
}

archive.finalize(function(err, written) {
if (err) {
throw err;
}

This comment has been minimized.

Copy link
@danmilon

danmilon Jan 11, 2013

Contributor

Too nested. can be:

async.forEach(['file1.js', 'file2.js'], function (file, cb) {
  archive.addFile(fs.createReadStream(file), { name: file }, cb)
}, function (err) {
  if (err) {
    throw err
  }

  archive.finalize(function (err, written) {
    if (err) {
      throw err;
    }

    console.log(written + ' total bytes written');
  })
)

This comment has been minimized.

Copy link
@ctalkington

ctalkington Jan 11, 2013

Author Member

yah i'm undecided if i should use async module in all examples. thats why its like it currently is.

This comment has been minimized.

Copy link
@danmilon

danmilon Jan 11, 2013

Contributor

I see. Well, with the need to wait an addFile to finish before calling it again, it feels really uncomfortable to do this without a control flow library. So, my guess is that everyone will straight or eventually use async (or other) along with archiver, thus having examples with it shouldn't be an issue.

But yeah, I see your concern. Another option is to simplify the examples, so they don't need to be that nested.

console.log(written + ' total bytes written'); // this wont be accurate since gzip happens after tar
});
});
Expand Down
22 changes: 19 additions & 3 deletions examples/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,27 @@ var archiver = require('archiver');
var out = fs.createWriteStream('out.zip'); // or out.tar
var archive = archiver.createZip(); // or createTar

archive.on('error', function(err) {
console.log(err);
});

archive.pipe(out);

archive.addFile(fs.createReadStream('file1.js'), {name: 'file1.js'}, function() {
archive.addFile(fs.createReadStream('file2.js'), {name: 'file2.js'}, function() {
archive.finalize(function(written) {
archive.addFile(fs.createReadStream('file1.js'), {name: 'file1.js'}, function(err) {
if (err) {
throw err;
}

archive.addFile(fs.createReadStream('file2.js'), {name: 'file2.js'}, function(err) {
if (err) {
throw err;
}

archive.finalize(function(err, written) {
if (err) {
throw err;
}

console.log(written + ' total bytes written');
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/archiver/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Archiver.prototype._read = function() {
self.readable = false;

if (utils.lo.isFunction(self.callback)) {
self.callback(self.fileptr);
self.callback(null, self.fileptr);
}
}

Expand Down
14 changes: 11 additions & 3 deletions lib/archiver/tar.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ tarArchiver.prototype._writeData = function(file, sourceBuffer) {
tarArchiver.prototype.addFile = function(source, data, callback) {
var self = this;

if (utils.lo.isFunction(callback) === false) {
callback = utils.fallCall;
}

if (self.busy) {
self.emit('error', 'previous file not finished');
callback(new Error('Previous file not finished'));
return;
}

Expand All @@ -147,7 +151,7 @@ tarArchiver.prototype.addFile = function(source, data, callback) {
});

if (utils.lo.isEmpty(file.name) || utils.lo.isString(file.name) === false) {
self.emit('error', 'name must be a valid string value');
callback(new Error('Name must be a valid string value'));
return;
}

Expand Down Expand Up @@ -215,8 +219,12 @@ tarArchiver.prototype.addFile = function(source, data, callback) {
tarArchiver.prototype.finalize = function(callback) {
var self = this;

if (utils.lo.isFunction(callback) === false) {
callback = utils.fallCall;
}

if (self.files.length === 0) {
self.emit('error', 'no files in tar');
callback(new Error('No files in archive'));
return;
}

Expand Down
14 changes: 11 additions & 3 deletions lib/archiver/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,12 @@ zipArchiver.prototype._pushCentralDirectory = function() {
zipArchiver.prototype.addFile = function(source, data, callback) {
var self = this;

if (utils.lo.isFunction(callback) === false) {
callback = utils.fallCall;
}

if (self.busy) {
self.emit('error', 'previous file not finished');
callback(new Error('Previous file not finished'));
return;
}

Expand All @@ -285,7 +289,7 @@ zipArchiver.prototype.addFile = function(source, data, callback) {
});

if (utils.lo.isEmpty(file.name) || utils.lo.isString(file.name) === false) {
self.emit('error', 'name must be a valid string value');
callback(new Error('Name must be a valid string value'));
return;
}

Expand Down Expand Up @@ -388,8 +392,12 @@ zipArchiver.prototype.addFile = function(source, data, callback) {
zipArchiver.prototype.finalize = function(callback) {
var self = this;

if (utils.lo.isFunction(callback) === false) {
callback = utils.fallCall;
}

if (self.files.length === 0) {
self.emit('error', 'no files in zip');
callback(new Error('No files in archive'));
return;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,12 @@ utils.unixifyPath = function(filepath) {
} else {
return filepath;
}
};

utils.fallCall = function(err, data) {
if (err) {
throw err;
}

return data;
};

0 comments on commit 51a7dbf

Please sign in to comment.