Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #195 from bookchin/master
Browse files Browse the repository at this point in the history
v1.2.8
  • Loading branch information
bookchin committed Jun 20, 2016
2 parents 90197d7 + 9655ab0 commit 2392650
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 27 deletions.
14 changes: 4 additions & 10 deletions bin/storj.js
Expand Up @@ -68,7 +68,8 @@ function makeTempDir(callback) {
// 0700.
mode: 448,
// require manual cleanup.
keep: true
keep: true,
unsafeCleanup: true
};

tmp.dir(opts, function(err, path, cleanupCallback) {
Expand Down Expand Up @@ -372,10 +373,6 @@ var ACTIONS = {
var secret = new storj.DataCipherKeyIv();
var encrypter = new storj.EncryptStream(secret);

function cleanup() {
log('info', 'Cleaning up...');
}

getKeyRing(function(keyring) {
log('info', 'Generating encryption key...');
log('info', 'Encrypting file "%s"', [filepath]);
Expand All @@ -389,12 +386,8 @@ var ACTIONS = {

function cleanup() {
log('info', 'Cleaning up...');
try {
fs.unlinkSync(tmppath);
} catch (err) {
// NOOP
}
tmpCleanup();
log('info', 'Finished cleaning!');
}

fs.createReadStream(filepath)
Expand Down Expand Up @@ -429,6 +422,7 @@ var ACTIONS = {
'Name: %s, Type: %s, Size: %s bytes, ID: %s',
[file.filename, file.mimetype, file.size, file.id]
);
process.exit();
}
);
});
Expand Down
38 changes: 33 additions & 5 deletions lib/bridgeclient.js
Expand Up @@ -258,10 +258,35 @@ BridgeClient.prototype.destroyFileStagingFrameById = function(id, callback) {
* Creates a file staging frame
* @param {String} id - Unique frame ID
* @param {Object} shard - The shard metadata
* @param {Object} options
* @param {Number} options.retry - Retry the request this many times if failed
* @param {Function} callback
*/
BridgeClient.prototype.addShardToFileStagingFrame = function(id, shard, cb) {
return this._request('PUT', '/frames/' + id, shard, cb);
BridgeClient.prototype.addShardToFileStagingFrame = function(f, s, opt, cb) {
var self = this;
var retries = 0;

if (typeof arguments[2] === 'function') {
cb = opt;
opt = { retry: 3 };
}

function _addShard() {
return self._request('PUT', '/frames/' + f, s, function(err, result) {
if (err) {
if (opt.retry > retries) {
retries++;
return _addShard();
}

return cb(err);
}

cb(null, result);
});
}

return _addShard();
};

/**
Expand Down Expand Up @@ -364,11 +389,14 @@ BridgeClient.prototype.storeFileInBucket = function(id, token, file, callback) {
if (completed === numShards && !failedToUploadShard) {
cleanup();

// NB: use the original filename if called from cli
var origFileName = path.basename(file).split('.crypt')[0];

self._logger.info('Transfer finished, creating entry...');
self._request('POST', '/buckets/' + id + '/files', {
frame: frame.id,
mimetype: mime.lookup(file),
filename: path.basename(file).split('.crypt')[0]
mimetype: mime.lookup(origFileName),
filename: origFileName
}, callback);
}
});
Expand Down Expand Up @@ -422,7 +450,7 @@ BridgeClient.prototype.resolveFileFromPointers = function(pointers, callback) {
var opened = 0;
var size = pointers.reduce(function(a, b) {
return { size: a.size + b.size };
}).size;
}, { size: 0 }).size;
var muxer = new FileMuxer({
shards: pointers.length,
length: size
Expand Down
6 changes: 4 additions & 2 deletions lib/constants.js
Expand Up @@ -10,7 +10,9 @@ module.exports = {
/** @constant {Number} PREFIX - NodeID prefix (same as bitcoin) */
PREFIX: 0x00,
/** @constant {Number} NONCE_EXPIRE - Time to honor a signed message */
NONCE_EXPIRE: 10000,
NONCE_EXPIRE: 15000,
/** @constant {Number} RPC_TIMEOUT - Max wait time for a RPC response */
RPC_TIMEOUT: 15000,
/** @constant {Number} AUDIT_BYTES - Number of bytes for audit challenge */
AUDIT_BYTES: 32,
/** @constant {Number} CLEAN_INTERVAL - Interval for reaping stale shards */
Expand All @@ -36,7 +38,7 @@ module.exports = {
/** @constant {Number} OPCODE_DEG_HIGH - Opcode for medium criteria degree */
OPCODE_DEG_HIGH: 0x03,
/** @constant {Number} MAX_CONCURRENT_OFFERS - Number of concurrent offers */
MAX_CONCURRENT_OFFERS: 6,
MAX_CONCURRENT_OFFERS: 3,
/** @constant {Number} MAX_CONCURRENT_AUDITS - Number of concurrent audits */
MAX_CONCURRENT_AUDITS: 3,
/** @constant MAX_FIND_TUNNEL_RELAYS - Max times to relay FIND_TUNNEL */
Expand Down
4 changes: 1 addition & 3 deletions lib/manager.js
Expand Up @@ -167,9 +167,7 @@ Manager.prototype._initShardReaper = function() {
var self = this;

if (!this._options.disableReaper) {
this.clean(function() {
setTimeout(self._initShardReaper.bind(self), constants.CLEAN_INTERVAL);
});
setTimeout(self._initShardReaper.bind(self), constants.CLEAN_INTERVAL);
}
};

Expand Down
4 changes: 2 additions & 2 deletions lib/patches.js
@@ -1,11 +1,11 @@
'use strict';

var ms = require('ms');
var kad = require('kad');
var constants = require('./constants');

module.exports = function() {

// NB: Increase response timeout for RPC calls
kad.constants.T_RESPONSETIMEOUT = ms('10s');
kad.constants.T_RESPONSETIMEOUT = constants.RPC_TIMEOUT;

};
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "storj",
"version": "1.2.7",
"version": "1.2.8",
"description": "implementation of the storj protocol for node.js and the browser",
"main": "index.js",
"directories": {
Expand Down Expand Up @@ -62,7 +62,7 @@
"ip": "^1.1.2",
"jsen": "^0.6.0",
"json-stable-stringify": "^1.0.1",
"kad": "^1.5.13",
"kad": "^1.5.14",
"kad-quasar": "^0.1.3",
"leveldown": "^1.4.6",
"levelup": "^1.3.1",
Expand Down
36 changes: 36 additions & 0 deletions test/bridgeclient.unit.js
Expand Up @@ -778,6 +778,42 @@ describe('BridgeClient', function() {
});
});

it('should retry if the request fails', function(done) {
var _request = sinon.stub(
BridgeClient.prototype,
'_request'
).callsArgWith(
3,
new Error('Request failed')
);
var client = new BridgeClient();
client.addShardToFileStagingFrame('myframe', {
meta: 'data'
}, function() {
_request.restore();
expect(_request.callCount).to.equal(4);
done();
});
});

it('should retry the defined number of retries', function(done) {
var _request = sinon.stub(
BridgeClient.prototype,
'_request'
).callsArgWith(
3,
new Error('Request failed')
);
var client = new BridgeClient();
client.addShardToFileStagingFrame('myframe', {
meta: 'data'
}, { retry: 6 }, function() {
_request.restore();
expect(_request.callCount).to.equal(7);
done();
});
});

});

});
Expand Down
6 changes: 3 additions & 3 deletions test/manager.unit.js
Expand Up @@ -135,7 +135,7 @@ describe('Manager', function() {
_size.restore();
expect(err.message).to.equal('Failed');
done();
});
})._checkCapacity();
});

it('should emit locked if the status changes', function(done) {
Expand All @@ -145,7 +145,7 @@ describe('Manager', function() {
man.on('locked', function() {
_size.restore();
done();
});
})._checkCapacity();
});

it('should emit unlocked if the status changes', function(done) {
Expand All @@ -156,7 +156,7 @@ describe('Manager', function() {
man.on('unlocked', function() {
_size.restore();
done();
});
})._checkCapacity();
});

});
Expand Down
11 changes: 11 additions & 0 deletions test/storage/adapter.unit.js
Expand Up @@ -127,6 +127,17 @@ describe('StorageAdapter', function() {
}).read();
});

it('should end the stream when all keys are read', function(done) {
var a = new StorageAdapter();
var s = a.createReadStream();
a._keys = function(callback) {
callback(null, []);
};
s.on('end', function() {
done();
}).read();
});

});

});
19 changes: 19 additions & 0 deletions test/storage/adapters/level.unit.js
Expand Up @@ -339,4 +339,23 @@ describe('LevelDBFileStore', function() {

});

describe('#reset', function() {

it('should delete the item if it exists', function(done) {
var fs = new LevelDBFileStore(store);
var _get = sinon.stub(fs._db, 'get').callsArg(1);
var _del = sinon.stub(fs._db, 'del', function(k, cb) {
_get.restore();
cb();
});
fs.reset('key', function() {
_del.restore();
expect(_del.called).to.equal(true);
done();
});

});

});

});

0 comments on commit 2392650

Please sign in to comment.