Comparing version 0.6.6 to 0.7.0
{ | ||
"name": "sonic-js", | ||
"version": "0.6.6", | ||
"version": "0.7.0", | ||
"description": "ws client library for the Sonic protocol", | ||
@@ -10,3 +10,3 @@ "main": "src/lib.js", | ||
"scripts": { | ||
"test": "node_modules/mocha/bin/mocha -r chai spec/ws.js" | ||
"test": "node_modules/mocha/bin/mocha -r chai spec/close.js spec/ws.js" | ||
}, | ||
@@ -13,0 +13,0 @@ "dependencies": { |
@@ -108,3 +108,3 @@ /* eslint-env node, mocha */ | ||
describe('Sonicd ws', function() { | ||
describe('Sonic ws', function() { | ||
@@ -130,3 +130,3 @@ var client = new Client(sonicEndpoint); | ||
describe('Sonicd ws auth', function() { | ||
describe('Sonic ws auth', function() { | ||
@@ -155,3 +155,3 @@ it('should throw an error if api key is invalid', function(done) { | ||
describe('Sonicd ws with authentication', function() { | ||
describe('Sonic ws with authentication', function() { | ||
@@ -171,2 +171,7 @@ var authenticated = new Client(sonicEndpoint); | ||
after(function(done) { | ||
authenticated.close(); | ||
done(); | ||
}); | ||
// client is authenticated | ||
@@ -218,3 +223,2 @@ runSpecTests(authenticated, 'authenticated'); | ||
util.expectError(authenticated, query, done); | ||
authenticated.close(); | ||
}, 'only_from_ip'); // check server's reference.conf | ||
@@ -221,0 +225,0 @@ }); |
@@ -17,4 +17,15 @@ 'use strict'; | ||
function cancel(ws) { | ||
if (ws.readyState === WebSocket.OPEN) { | ||
ws.send(JSON.stringify({ e: 'C' })); | ||
} else { | ||
ws.on('open', function() { | ||
ws.send(JSON.stringify({ e: 'C' })); | ||
}); | ||
} | ||
} | ||
function Client(sonicAddress) { | ||
this.url = sonicAddress; | ||
this.ws = []; | ||
} | ||
@@ -34,2 +45,3 @@ | ||
var isError = false; | ||
var self = this; | ||
@@ -41,3 +53,12 @@ return function(message, ws) { | ||
function done(err, id) { | ||
ws.close(); | ||
var idx; | ||
ws.close(1000, 'completed'); | ||
if ((idx = self.ws.indexOf(ws)) < 0) { | ||
throw new Error('ws not found'); | ||
} | ||
self.ws.splice(idx, 1); | ||
doneCb(err, id); | ||
@@ -133,2 +154,6 @@ } | ||
}); | ||
this.ws.push(ws); | ||
return ws; | ||
}; | ||
@@ -139,2 +164,3 @@ | ||
var queryMsg = utils.toMsg(query); | ||
var ws; | ||
@@ -166,4 +192,8 @@ function done(err) { | ||
this.exec(queryMsg, done, output, progress, metadata, started); | ||
ws = this.exec(queryMsg, done, output, progress, metadata, started); | ||
emitter.cancel = function() { | ||
cancel(ws); | ||
}; | ||
return emitter; | ||
@@ -176,2 +206,3 @@ }; | ||
var queryMsg = utils.toMsg(query); | ||
var ws; | ||
@@ -190,3 +221,9 @@ function done(err) { | ||
this.exec(queryMsg, done, output); | ||
ws = this.exec(queryMsg, done, output); | ||
return { | ||
cancel: function() { | ||
cancel(ws); | ||
} | ||
}; | ||
}; | ||
@@ -221,7 +258,5 @@ | ||
Client.prototype.close = function() { | ||
if (this.ws) { | ||
this.ws.close(); | ||
} | ||
this.ws.forEach(cancel); | ||
}; | ||
module.exports.Client = Client; |
42963
16
679
4