Permalink
Browse files

# workaround to use xml2js 0.1.10 \n # added more services for queues

  • Loading branch information...
1 parent 81b580b commit 27379cd9d1c86a8e5c8de0fc8794bf0c5df512bf Juan Pablo Garcia Dalolla committed Sep 11, 2011
@@ -77,7 +77,7 @@ Container.prototype.upload = function(name, stream, contentType, metadata, block
var blockNumber = 0;
var identifiers = [];
- stream.addListener('data', function(data) {
+ stream.on('data', function(data) {
stream.pause();
var identifier = new Buffer(_s.pad(blockNumber, 64, '0')).toString('base64');
@@ -98,7 +98,7 @@ Container.prototype.upload = function(name, stream, contentType, metadata, block
});
});
- stream.addListener("close", function(){
+ stream.on("close", function(){
serviceInstance.putBlockList(path, identifiers, contentType, metadata, function(err, data){
var blob = null;
@@ -111,7 +111,7 @@ Container.prototype.upload = function(name, stream, contentType, metadata, block
});
});
- stream.addListener("error", function(err){
+ stream.on("error", function(err){
blockUploaded(err);
});
};
@@ -20,37 +20,38 @@ Service.prototype.listContainers = function(args, callback){
callback({ message: 'container `' + name + '` not found' }, null);
return;
}
-
- var parser = new xml2js.Parser();
- parser.addListener('end', function(result) {
+
+ new xml2js.Parser().on('end', function(result) {
var containers = [];
if (result.Containers.Container)
containers = [result.Containers.Container].flatten().map(function(c){ return { name: c.Name, url: c.Url, lastModified: c.LastModified }; });
callback(null, containers);
- });
+ }).on('error', function(e){
+ // Don't know why but without this it throws an error
+ // Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar
+ }).parseString(response.body);
- var result = parser.parseString(response.body);
});
};
Service.prototype.listBlobs = function(containerName, callback){
var options = {restype: 'container', comp: 'list'};
this.execute('get', containerName, options, {'x-ms-version': '2009-09-19'}, null, function(err, response) {
- var parser = new xml2js.Parser();
-
- parser.addListener('end', function(result) {
+
+ new xml2js.Parser().on('end', function(result) {
var blobs = [];
if (result.Blobs.Blob)
blobs = [result.Blobs.Blob].flatten().map(function(c){ return { name: c.Name, url: c.Url, contentType: c.Properties['Content-Type'] }; });
callback(null, blobs);
- });
-
- var result = parser.parseString(response.body);
+ }).on('error', function(e){
+ // Don't know why but without adding this callback it throws an error
+ // Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar
+ }).parseString(' '+response.body);
});
};
@@ -248,15 +249,13 @@ Service.prototype.snapshotBlob = function(path, callback){
}
Service.prototype.putBlock = function(path, identifier, payload, callback){
- this.execute('put', path, { comp : 'block', blockid: identifier }, {'Content-Type': "application/octet-stream"}, payload, function(err, response) {
- var error = null, data = null
+ var service = this;
+
+ service.execute('put', path, { comp : 'block', blockid: identifier }, {'Content-Type': "application/octet-stream"}, payload, function(err, response) {
+ if (service.parseError(response, callback))
+ return;
- if (err == null && response.statusCode == 201)
- data = response.headers;
- else
- error = err;
-
- callback(error, data);
+ callback(null, response.headers);
});
}
@@ -267,15 +266,12 @@ Service.prototype.putBlockList = function(path, blockList, contentType, metadata
<BlockList>' +
_.map(blockList, function(id) { return "<Latest>" + id + "</Latest>"}).join('') +
'</BlockList>';
+ var service = this;
+
+ this.execute('put', path, { comp : 'blocklist' }, headers , payload, function(response) {
+ if (service.parseError(response, callback))
+ return;
- this.execute('put', path, { comp : 'blocklist' }, headers , payload, function(err, response) {
- var error = null, data = null
-
- if (err == null && response.statusCode == 201)
- data = response.headers;
- else
- error = err;
-
- callback(error, data);
+ callback(null, response.headers);
});
};
@@ -21,7 +21,10 @@ Service.prototype.list = function(options, callback){
return;
new xml2js.Parser().on('end', function(result) {
- callback(null, result);
+ callback(null, result.merge(response.headers));
+ }).on('error', function(e){
+ // Don't know why but without adding this callback it throws an error
+ // Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar
}).parseString(response.body);
});
};
@@ -70,15 +73,36 @@ Service.prototype.putMetadata = function(name, metadata, callback){
});
};
-Service.prototype.putMessage = function(name, message, messagettl, callback){
+Service.prototype.putMessage = function(name, message, options, callback){
var service = this;
var payload = '<QueueMessage><MessageText>' + message + '</MessageText></QueueMessage>';
- service.execute('put', name + '/messages', {messagettl: messagettl}, {'x-ms-version': '2009-09-19'}, payload, function(response) {
+ service.execute('put', name + '/messages', options, {'x-ms-version': '2009-09-19'}, payload, function(response) {
if (service.parseError(response, callback))
return;
callback(null, response.headers);
});
+};
+
+Service.prototype.getMessages = function(name, options, callback){
+ var service = this;
+
+ service.execute('get', name + '/messages', options, {'x-ms-version': '2009-09-19'}, null, function(response) {
+ if (service.parseError(response, callback))
+ return;
+
+ new xml2js.Parser({explicitRoot: true}).on('end', function(result) {
+ var data = { messages: [] };
+
+ if (result.QueueMessagesList.QueueMessage)
+ data.messages = [result.QueueMessagesList.QueueMessage].flatten();
+
+ callback(null, data.merge(response.headers));
+ }).on('error', function(e){
+ // Don't know why but without adding this callback it throws an error
+ // Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar
+ }).parseString(response.body);
+ });
};
@@ -174,15 +174,15 @@ CoreService.prototype.execute = function(verb, path, query, headers, payload, ca
response.setEncoding('utf8');
var body = [];
- response.addListener('data', function (chunk) {
+ response.on('data', function (chunk) {
body.push(chunk);
});
- response.addListener('end', function () {
- var response = reponse.merge({body: body.join("")});
+ response.on('end', function () {
+ var result = response.merge({body: body.join('')});
// TODO: remove on parameter previously used to return err as the first parameter
- callback(response, response);
+ callback(result, result);
});
});
View
@@ -10,7 +10,7 @@
"engines": { "node": ">= 0.2.0" },
"dependencies": {
"querystring": ">= 0.0.1",
- "xml2js": "0.1.9",
+ "xml2js": "0.1.10",
"underscore.string": ">= 1.1.6"
},
"devDependencies": {
@@ -17,23 +17,23 @@ module.exports = {
<Metadata> \
<Color>red</Color> \
<SomeMetadataName>SomeMetadataValue</SomeMetadataName> \
- <Metadata> \
+ </Metadata> \
</Queue> \
<Queue> \
<Name>q2</Name> \
<Url>http://myaccount.queue.core.windows.net/q2</Url> \
<Metadata> \
<Color>blue</Color> \
<SomeMetadataName>SomeMetadataValue</SomeMetadataName> \
- <Metadata> \
+ </Metadata> \
</Queue> \
<Queue> \
<Name>q3</Name> \
<Url>http://myaccount.queue.core.windows.net/q3</Url> \
<Metadata> \
<Color>yellow</Color> \
<SomeMetadataName>SomeMetadataValue</SomeMetadataName> \
- <Metadata> \
+ </Metadata> \
</Queue> \
</Queues> \
<NextMarker>q4</NextMarker> \
@@ -203,18 +203,20 @@ module.exports = {
var mock = sinon.mock(service);
var expectedPayload = '<QueueMessage><MessageText>message-content</MessageText></QueueMessage>'
-
- mock.expects("execute").withArgs('put', 'queue1/messages', {messagettl: 10}, { 'x-ms-version': '2009-09-19', }, expectedPayload)
+
+ var options = {messagettl: 10};
+
+ mock.expects("execute").withArgs('put', 'queue1/messages', options, { 'x-ms-version': '2009-09-19', }, expectedPayload)
.yields({statusCode: 201, headers: { 'x-ms-version': '2009-09-19', 'x-ms-request-id': 'id'}})
.once();
- service.putMessage('queue1', 'message-content', 10, function(err, data){
+ service.putMessage('queue1', 'message-content', options, function(err, data){
assert.equal(err, null);
assert.equal(data['x-ms-request-id'], 'id');
});
mock.verify();
- },
+ },
'should return an error when adding a new message': function(){
var service = new Service({});
@@ -228,4 +230,128 @@ module.exports = {
mock.verify();
},
+
+ 'should get messages from a queue': function(){
+ var service = new Service({});
+ var mock = sinon.mock(service);
+
+ var mockResponseBody = '<QueueMessagesList> \
+ <QueueMessage> \
+ <MessageId>string-message-id</MessageId> \
+ <InsertionTime>insertion-time</InsertionTime> \
+ <ExpirationTime>expiration-time</ExpirationTime> \
+ <PopReceipt>opaque-string-receipt-data</PopReceipt> \
+ <TimeNextVisible>time-next-visible</TimeNextVisible> \
+ <DequeueCount>20</DequeueCount> \
+ <MessageText>message-body</MessageText> \
+ </QueueMessage> \
+ </QueueMessagesList>'
+
+ var options = { numberofmessages: 10, visibilitytimeout: 60 };
+
+ mock.expects("execute").withArgs('get', 'queue1/messages', options, { 'x-ms-version': '2009-09-19', }, null)
+ .yields({statusCode: 201, headers: { 'x-ms-version': '2009-09-19', 'x-ms-request-id': 'id'}, body: mockResponseBody})
+ .once();
+
+ service.getMessages('queue1', options, function(err, data){
+ assert.equal(err, null);
+
+ assert.equal(data['x-ms-version'], '2009-09-19');
+ assert.equal(data['x-ms-request-id'], 'id');
+
+ assert.equal(data.messages.length, 1);
+ assert.equal(data.messages[0]['MessageId'], 'string-message-id');
+ assert.equal(data.messages[0]['InsertionTime'], 'insertion-time');
+ assert.equal(data.messages[0]['ExpirationTime'], 'expiration-time');
+ assert.equal(data.messages[0]['PopReceipt'], 'opaque-string-receipt-data');
+ assert.equal(data.messages[0]['TimeNextVisible'], 'time-next-visible');
+ assert.equal(data.messages[0]['DequeueCount'], 20);
+ assert.equal(data.messages[0]['MessageText'], 'message-body');
+ });
+
+ mock.verify();
+ },
+
+ 'should get messages from a queue when there are more than one message': function(){
+ var service = new Service({});
+ var mock = sinon.mock(service);
+
+ var mockResponseBody = '<QueueMessagesList> \
+ <QueueMessage> \
+ <MessageId>string-message-id</MessageId> \
+ <InsertionTime>insertion-time</InsertionTime> \
+ <ExpirationTime>expiration-time</ExpirationTime> \
+ <PopReceipt>opaque-string-receipt-data</PopReceipt> \
+ <TimeNextVisible>time-next-visible</TimeNextVisible> \
+ <DequeueCount>20</DequeueCount> \
+ <MessageText>message-body</MessageText> \
+ </QueueMessage> \
+ <QueueMessage> \
+ <MessageId>string-message-id2</MessageId> \
+ <InsertionTime>insertion-time2</InsertionTime> \
+ <ExpirationTime>expiration-time2</ExpirationTime> \
+ <PopReceipt>opaque-string-receipt-data2</PopReceipt> \
+ <TimeNextVisible>time-next-visible2</TimeNextVisible> \
+ <DequeueCount>202</DequeueCount> \
+ <MessageText>message-body2</MessageText> \
+ </QueueMessage> \
+ </QueueMessagesList>'
+
+ var options = { numberofmessages: 10, visibilitytimeout: 60 };
+
+ mock.expects("execute").withArgs('get', 'queue1/messages', options, { 'x-ms-version': '2009-09-19', }, null)
+ .yields({statusCode: 201, headers: { 'x-ms-version': '2009-09-19', 'x-ms-request-id': 'id'}, body: mockResponseBody})
+ .once();
+
+ service.getMessages('queue1', options, function(err, data){
+ assert.equal(err, null);
+
+ assert.equal(data['x-ms-version'], '2009-09-19');
+ assert.equal(data['x-ms-request-id'], 'id');
+
+ assert.equal(data.messages.length, 2);
+ assert.equal(data.messages[0]['MessageId'], 'string-message-id');
+ assert.equal(data.messages[0]['InsertionTime'], 'insertion-time');
+ assert.equal(data.messages[0]['ExpirationTime'], 'expiration-time');
+ assert.equal(data.messages[0]['PopReceipt'], 'opaque-string-receipt-data');
+ assert.equal(data.messages[0]['TimeNextVisible'], 'time-next-visible');
+ assert.equal(data.messages[0]['DequeueCount'], 20);
+ assert.equal(data.messages[0]['MessageText'], 'message-body');
+
+ assert.equal(data.messages[1]['MessageId'], 'string-message-id2');
+ assert.equal(data.messages[1]['InsertionTime'], 'insertion-time2');
+ assert.equal(data.messages[1]['ExpirationTime'], 'expiration-time2');
+ assert.equal(data.messages[1]['PopReceipt'], 'opaque-string-receipt-data2');
+ assert.equal(data.messages[1]['TimeNextVisible'], 'time-next-visible2');
+ assert.equal(data.messages[1]['DequeueCount'], 202);
+ assert.equal(data.messages[1]['MessageText'], 'message-body2');
+ });
+
+ mock.verify();
+ },
+
+ 'should return an empty array of messages': function(){
+ var service = new Service({});
+ var mock = sinon.mock(service);
+
+ var mockResponseBody = '<QueueMessagesList> \
+ </QueueMessagesList>'
+
+ var options = { numberofmessages: 10, visibilitytimeout: 60 };
+
+ mock.expects("execute").withArgs('get', 'queue1/messages', options, { 'x-ms-version': '2009-09-19', }, null)
+ .yields({statusCode: 201, headers: { 'x-ms-version': '2009-09-19', 'x-ms-request-id': 'id'}, body: mockResponseBody})
+ .once();
+
+ service.getMessages('queue1', options, function(err, data){
+ assert.equal(err, null);
+
+ assert.equal(data['x-ms-version'], '2009-09-19');
+ assert.equal(data['x-ms-request-id'], 'id');
+
+ assert.equal(data.messages.length, 0);
+ });
+
+ mock.verify();
+ },
}

0 comments on commit 27379cd

Please sign in to comment.