Skip to content

Commit

Permalink
handle stat at a later point when it won't need complicated queue han…
Browse files Browse the repository at this point in the history
…dling.
  • Loading branch information
ctalkington committed Jul 4, 2014
1 parent 45d826b commit c98c9b3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
90 changes: 54 additions & 36 deletions lib/modules/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Licensed under the MIT license.
* https://github.com/ctalkington/node-archiver/blob/master/LICENSE-MIT
*/
var fs = require('fs');
var inherits = require('util').inherits;
var Transform = require('readable-stream').Transform;

Expand Down Expand Up @@ -35,7 +36,7 @@ var Archiver = module.exports = function(options) {

inherits(Archiver, Transform);

Archiver.prototype._append = function(filepath, data, stats) {
Archiver.prototype._append = function(filepath, data) {
data = data || {};

if (!data.name) {
Expand All @@ -44,32 +45,19 @@ Archiver.prototype._append = function(filepath, data, stats) {

data.sourcePath = filepath;

var source;

if (stats.isFile()) {
data.type = 'file';
data.sourceType = 'stream';

source = util.lazyReadStream(filepath);
} else if (stats.isDirectory() && this._moduleSupports('directory')) {
data.name = util.trailingSlashIt(data.name);
data.type = 'directory';
data.sourcePath = util.trailingSlashIt(filepath);
data.sourceType = 'buffer';

source = new Buffer(0);
} else {
this.emit('error', new Error('unsupported filepath: ' + filepath));
return;
}

this._queue.push({
data: this._normalizeEntryData(data, stats),
source: source
data: data,
source: false,
deferredStat: true,
filepath: filepath
});
};

Archiver.prototype._finalizeModule = function() {
Archiver.prototype._moduleAppend = function(source, data, callback) {
this._module.append(source, data, callback);
};

Archiver.prototype._moduleFinalize = function() {
this._state.finalized = true;

if (typeof this._module.finalize === 'function') {
Expand Down Expand Up @@ -137,14 +125,15 @@ Archiver.prototype._onModuleError = function(err) {

Archiver.prototype._onQueueDrain = function() {
if (this._state.finalize && !this._state.finalized && this._queue.idle()) {
this._finalizeModule();
this._moduleFinalize();
}
};

Archiver.prototype._onQueueTask = function(task, callback) {
this._module.append(task.source, task.data, function(err, file) {
var afterAppend = function(err, file) {
if (err) {
this.emit('error', err);
callback();
return;
}

Expand All @@ -154,7 +143,43 @@ Archiver.prototype._onQueueTask = function(task, callback) {
this._files.push(file);

callback();
}.bind(this));
}.bind(this);

var afterStat = function(err, stats) {
if (err) {
this.emit('error', err);
callback();
return;
}

task = this._updateQueueTaskWithStats(task, task.filepath, stats);
this._moduleAppend(task.source, task.data, afterAppend);
}.bind(this);

if (task.deferredStat) {
fs.stat(task.filepath, afterStat);
} else {
this._moduleAppend(task.source, task.data, afterAppend);
}
};

Archiver.prototype._updateQueueTaskWithStats = function(task, filepath, stats) {
if (stats.isFile()) {
task.data.type = 'file';
task.data.sourceType = 'stream';
task.source = util.lazyReadStream(filepath);
} else if (stats.isDirectory() && this._moduleSupports('directory')) {
task.data.name = util.trailingSlashIt(task.data.name);
task.data.type = 'directory';
task.data.sourcePath = util.trailingSlashIt(filepath);
task.data.sourceType = 'buffer';
task.source = new Buffer(0);
} else {
this.emit('error', new Error('unsupported filepath: ' + filepath));
}

task.data = this._normalizeEntryData(task.data, stats);
return task;
};

Archiver.prototype._pipeModuleOutput = function() {
Expand Down Expand Up @@ -238,14 +263,8 @@ Archiver.prototype.bulk = function(mappings) {
return;
}

var stats = util.stat(filepath);

if (!stats) {
return;
}

data.name = name;
self._append(filepath, data, stats);
self._append(filepath, data);
});
});

Expand All @@ -263,8 +282,7 @@ Archiver.prototype.file = function(filepath, data) {
return this;
}

var stats = util.stat(filepath);
this._append(filepath, data, stats);
this._append(filepath, data);

return this;
};
Expand All @@ -273,7 +291,7 @@ Archiver.prototype.finalize = function() {
this._state.finalize = true;

if (this._queue.idle()) {
this._finalizeModule();
this._moduleFinalize();
}

return this;
Expand Down
5 changes: 0 additions & 5 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ util.sanitizePath = function() {
return filepath.replace(/\\/g, '/').replace(/:/g, '').replace(/^\/+/, '');
};

util.stat = function() {
var filepath = path.join.apply(path, arguments);
return util.file.exists(filepath) ? fs.statSync(filepath) : false;
};

util.trailingSlashIt = function(str) {
return str.slice(-1) !== '/' ? str + '/' : str;
};
Expand Down

0 comments on commit c98c9b3

Please sign in to comment.