Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/196 test middleware #199

Merged
merged 7 commits into from
Mar 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 68 additions & 63 deletions src/middleware/blob/Artifact.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*globals define*/
/*jshint browser: true, node:true*/

/*
* Copyright (C) 2014 Vanderbilt University, All rights reserved.
*
* Author: Zsolt Lattmann
* @author lattmann / https://github.com/lattmann
*/

define(['blob/BlobMetadata', 'blob/BlobConfig', 'core/tasync'], function (BlobMetadata, BlobConfig, tasync) {

'use strict';
/**
* Creates a new instance of artifact, i.e. complex object, in memory. This object can be saved in the storage.
* @param {string} name Artifact's name without extension
Expand Down Expand Up @@ -67,6 +68,69 @@ define(['blob/BlobMetadata', 'blob/BlobConfig', 'core/tasync'], function (BlobMe
});
};

/**
* Adds a hash to the artifact using the given file path.
* @param {string} name Path to the file in the artifact. Note: 'a/b/c.txt'
* @param {string} hash Metadata hash that has to be added.
* @param callback
*/
Artifact.prototype.addObjectHash = function (name, hash, callback) {
var self = this;

if (BlobConfig.hashRegex.test(hash) === false) {
callback('Blob hash is invalid');
return;
}

self.blobClientGetMetadata.call(self.blobClient, hash, function (err, metadata) {
if (err) {
callback(err);
return;
}

if (self.descriptor.content.hasOwnProperty(name)) {
callback('Another content with the same name was already added. ' + JSON.stringify(self.descriptor.content[name]));

} else {
self.descriptor.size += metadata.size;

self.descriptor.content[name] = {
content: metadata.content,
contentType: BlobMetadata.CONTENT_TYPES.OBJECT
};
callback(null, hash);
}
});
};

Artifact.prototype.addMetadataHash = function (name, hash, callback) {
var self = this;

if (BlobConfig.hashRegex.test(hash) === false) {
callback('Blob hash is invalid');
return;
}
self.blobClientGetMetadata.call(self.blobClient, hash, function (err, metadata) {
if (err) {
callback(err);
return;
}

if (self.descriptor.content.hasOwnProperty(name)) {
callback('Another content with the same name was already added. ' + JSON.stringify(self.descriptor.content[name]));

} else {
self.descriptor.size += metadata.size;

self.descriptor.content[name] = {
content: hash,
contentType: BlobMetadata.CONTENT_TYPES.SOFT_LINK
};
callback(null, hash);
}
});
};

/**
* Adds multiple files.
* @param {Object.<string, Blob>} files files to add
Expand Down Expand Up @@ -101,7 +165,6 @@ define(['blob/BlobMetadata', 'blob/BlobConfig', 'core/tasync'], function (BlobMe
}
};


/**
* Adds multiple files as soft-links.
* @param {Object.<string, Blob>} files files to add
Expand Down Expand Up @@ -136,36 +199,6 @@ define(['blob/BlobMetadata', 'blob/BlobConfig', 'core/tasync'], function (BlobMe
}
};

/**
* Adds a hash to the artifact using the given file path.
* @param {string} name Path to the file in the artifact. Note: 'a/b/c.txt'
* @param {string} hash Metadata hash that has to be added.
* @param callback
*/
Artifact.prototype.addObjectHash = function (name, hash, callback) {
var self = this;

self.blobClientGetMetadata.call(self.blobClient, hash, function (err, metadata) {
if (err) {
callback(err);
return;
}

if (self.descriptor.content.hasOwnProperty(name)) {
callback('Another content with the same name was already added. ' + JSON.stringify(self.descriptor.content[name]));

} else {
self.descriptor.size += metadata.size;

self.descriptor.content[name] = {
content: metadata.content,
contentType: BlobMetadata.CONTENT_TYPES.OBJECT
};
callback(null, hash);
}
});
};

/**
* Adds hashes to the artifact using the given file paths.
* @param {object.<string, string>} objectHashes - Keys are file paths and values object hashes.
Expand Down Expand Up @@ -200,34 +233,6 @@ define(['blob/BlobMetadata', 'blob/BlobConfig', 'core/tasync'], function (BlobMe
}
};

Artifact.prototype.addMetadataHash = function (name, hash, callback) {
var self = this;

if (BlobConfig.hashRegex.test(hash) === false) {
callback("Blob hash is invalid");
return;
}
self.blobClientGetMetadata.call(self.blobClient, hash, function (err, metadata) {
if (err) {
callback(err);
return;
}

if (self.descriptor.content.hasOwnProperty(name)) {
callback('Another content with the same name was already added. ' + JSON.stringify(self.descriptor.content[name]));

} else {
self.descriptor.size += metadata.size;

self.descriptor.content[name] = {
content: hash,
contentType: BlobMetadata.CONTENT_TYPES.SOFT_LINK
};
callback(null, hash);
}
});
};

/**
* Adds hashes to the artifact using the given file paths.
* @param {object.<string, string>} objectHashes - Keys are file paths and values object hashes.
Expand Down
107 changes: 57 additions & 50 deletions src/middleware/blob/BlobClient.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*globals define*/
/*jshint browser: true, node:true*/
/*
* Copyright (C) 2014 Vanderbilt University, All rights reserved.
*
* Author: Zsolt Lattmann
* @author lattmann / https://github.com/lattmann
* @author ksmyth / https://github.com/ksmyth
*/

define(['./Artifact', 'blob/BlobMetadata', 'superagent'], function (Artifact, BlobMetadata, superagent) {
'use strict';

var BlobClient = function (parameters) {
this.artifacts = [];
Expand Down Expand Up @@ -56,8 +58,8 @@ define(['./Artifact', 'blob/BlobMetadata', 'superagent'], function (Artifact, Bl
}
};


BlobClient.prototype.putFile = function (name, data, callback) {
var contentLength;
function toArrayBuffer(buffer) {
var ab = new ArrayBuffer(buffer.length);
var view = new Uint8Array(ab);
Expand All @@ -76,9 +78,10 @@ define(['./Artifact', 'blob/BlobMetadata', 'superagent'], function (Artifact, Bl
data = '';
}
}
contentLength = data.hasOwnProperty('length') ? data.length : data.byteLength;
superagent.post(this.getCreateURL(name))
.set('Content-Type', 'application/octet-stream')
.set('Content-Length', data.length)
.set('Content-Length', contentLength)
.send(data)
.end(function (err, res) {
if (err || res.status > 399) {
Expand Down Expand Up @@ -125,38 +128,39 @@ define(['./Artifact', 'blob/BlobMetadata', 'superagent'], function (Artifact, Bl
};

BlobClient.prototype.putFiles = function (o, callback) {
var self = this;

var filenames = Object.keys(o);
var remaining = filenames.length;

var hashes = {};

for (var j = 0; j < filenames.length; j += 1) {
(function(filename, data) {

self.putFile(filename, data, function (err, hash) {
remaining -= 1;
var self = this,
error = '',
filenames = Object.keys(o),
remaining = filenames.length,
hashes = {},
putFile;
if (remaining === 0) {
callback(null, hashes);
}
putFile = function(filename, data) {
self.putFile(filename, data, function (err, hash) {
remaining -= 1;

hashes[filename] = hash;
hashes[filename] = hash;

if (err) {
// TODO: log/handle error
return;
}
if (err) {
error += 'putFile error: ' + err.toString();
}

if (remaining === 0) {
callback(null, hashes);
}
});
if (remaining === 0) {
callback(error, hashes);
}
});
};

})(filenames[j], o[filenames[j]]);
for (var j = 0; j < filenames.length; j += 1) {
putFile(filenames[j], o[filenames[j]]);
}
};

BlobClient.prototype.getSubObject = function (hash, subpath, callback) {
return this.getObject(hash, callback, subpath);
}
};

BlobClient.prototype.getObject = function (hash, callback, subpath) {
superagent.parse['application/zip'] = function (obj, parseCallback) {
Expand All @@ -165,21 +169,21 @@ define(['./Artifact', 'blob/BlobMetadata', 'superagent'], function (Artifact, Bl
} else {
return obj;
}
}
};
//superagent.parse['application/json'] = superagent.parse['application/zip'];

var req = superagent.get(this.getViewURL(hash, subpath));
if (req.pipe) {
// running on node
var Writable = require('stream').Writable;
require('util').inherits(BuffersWritable, Writable);

function BuffersWritable(options) {
var BuffersWritable = function (options) {
Writable.call(this, options);

var self = this;
self.buffers = [];
}
};
require('util').inherits(BuffersWritable, Writable);

BuffersWritable.prototype._write = function(chunk, encoding, callback) {
this.buffers.push(chunk);
callback();
Expand Down Expand Up @@ -207,10 +211,10 @@ define(['./Artifact', 'blob/BlobMetadata', 'superagent'], function (Artifact, Bl
} else {
var contentType = req.xhr.getResponseHeader('content-type');
var response = req.xhr.response; // response is an arraybuffer
if (contentType == 'application/json') {
function utf8ArrayToString(uintArray) {
if (contentType === 'application/json') {
var utf8ArrayToString = function (uintArray) {
return decodeURIComponent(escape(String.fromCharCode.apply(null, uintArray)));
}
};
response = JSON.parse(utf8ArrayToString(new Uint8Array(response)));
}
callback(null, response);
Expand Down Expand Up @@ -259,28 +263,31 @@ define(['./Artifact', 'blob/BlobMetadata', 'superagent'], function (Artifact, Bl
};

BlobClient.prototype.saveAllArtifacts = function (callback) {
var remaining = this.artifacts.length;
var hashes = [];
var remaining = this.artifacts.length,
hashes = [],
error = '',
saveCallback;

if (remaining === 0) {
callback(null, hashes);
}

for (var i = 0; i < this.artifacts.length; i += 1) {
saveCallback = function(err, hash) {
remaining -= 1;

this.artifacts[i].save(function(err, hash) {
remaining -= 1;
hashes.push(hash);

hashes.push(hash);
if (err) {
error += 'artifact.save err: ' + err.toString();
}
if (remaining === 0) {
callback(error, hashes);
}
};

if (err) {
// TODO: log/handle errors
return;
}
if (remaining === 0) {
callback(null, hashes);
}
});
for (var i = 0; i < this.artifacts.length; i += 1) {

this.artifacts[i].save(saveCallback);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/middleware/blob/BlobServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ define(['logManager',
// give more precise description about the error type and message. Resource if not available etc.
res.send(500);
} else {
//res.send(200);
//res.status(200);
}
});
}
Expand Down
Loading