New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sonic-js

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sonic-js - npm Package Compare versions

Comparing version 0.6.3 to 0.6.4

84

examples/example.js

@@ -1,36 +0,29 @@

#!/usr/bin/env node
/* eslint no-console: [0]*/
//var Client = require('sonic-js').Client;
// var Client = require('sonic-js').Client;
var Client = require('../src/lib.js').Client;
var assert = require('assert');
var host = process.env.SONIC_HOST || 'wss://0.0.0.0:443';
var API_KEY = '1234';
var USER = 'serrallonga';
var client = new Client('wss://0.0.0.0:443');
var client = new Client(host + '/v1/query');
var query = {
query: '5',
query: '10',
config: {
"class" : "SyntheticSource",
"seed" : 1000,
"progress-delay" : 10
class: 'SyntheticSource',
seed: 1000,
'progress-delay': 10
}
};
/* UNAUTHENTICATED Client.prototype.run */
var query2 = {
query: '5',
config: 'secured_test'
};
client.run(query, function(err, res) {
if (err) {
console.log(err);
return;
}
var done = 0;
res.forEach(function(e) {
console.log(e);
});
client.close();
console.log('exec is done!');
});
/* UNAUTHENTICATED Client.prototype.stream */

@@ -40,4 +33,2 @@

var done = 0;
stream.on('data', function(data) {

@@ -49,3 +40,3 @@ console.log(data);

done += p.progress;
console.log('running.. ' + done + "/" + p.total + " "+ p.units);
console.log('running.. ' + done + '/' + p.total + ' ' + p.units);
});

@@ -69,15 +60,24 @@

/* UNAUTHENTICATED Client.prototype.run */
client.run(query, function(err, res) {
if (err) {
console.log(err);
return;
}
var query2 = {
query: '5',
config: 'secured_test',
};
res.forEach(function(e) {
console.log(e);
});
console.log('exec is done!');
});
/* AUTHENTICATED Client.prototype.run */
//`secured_test` source can be accessed without
//an auth token that grants
//authorization equal or higher than 3.
client.run(query2, function(err, res) {
// `secured_test` source can be accessed without
// an auth token that grants
// authorization equal or higher than 3.
client.run(query2, function(err) {
assert.throws(function () {

@@ -88,9 +88,5 @@ if (err) {

});
})
});
var API_KEY = '1234';
var USER = 'serrallonga';
//first we need to authenticate
// first we need to authenticate
client.authenticate(USER, API_KEY, function(err, token) {

@@ -105,4 +101,3 @@ if (err) {

if (err) {
console.log(err);
return;
throw err;
}

@@ -114,7 +109,8 @@

console.log('secured exec is done!');
// close ws
client.close();
console.log('secured exec is done!');
});
})
});
{
"name": "sonic-js",
"version": "0.6.3",
"version": "0.6.4",
"description": "ws client library for the Sonic protocol",

@@ -5,0 +5,0 @@ "main": "src/lib.js",

var assert = require('chai').assert;
module.exports.testHappyPathSingle = function (client, query, n, done) {
client.run(query, function(err, data, traceId) {
assert(typeof traceId !== 'undefined', 'traceId is undefined in run method on `testHappyPathSingle`');
module.exports.testHappyPathSingle = function(client, query, n, done) {
client.run(query, function(err, data) {
if (err) {

@@ -16,11 +15,9 @@ done(err);

});
}
};
module.exports.testHappyPath = function (client, query, n, done) {
var stream;
module.exports.testHappyPath = function(client, query, n, done) {
var _done = 0;
var traceId;
var stream, traceId;
client.run(query, function(err, data, traceId) {
assert(typeof traceId !== 'undefined', 'traceId is undefined in run done callback on `testHappyPath` test');
client.run(query, function(err, data) {
if (err) {

@@ -40,3 +37,3 @@ done(err);

stream.on('trace', function(id) {
stream.on('started', function(id) {
traceId = id;

@@ -63,3 +60,3 @@ });

});
}
};

@@ -69,6 +66,4 @@ module.exports.expectError = function(client, query, done) {

var stream;
var traceId;
client.run(query, function(err, traceId) {
assert(typeof traceId !== 'undefined', 'traceId is undefined in run done callback on `expectError` test');
client.run(query, function(err) {
if (err) {

@@ -89,4 +84,4 @@ if (done) {

stream.on('trace', function(id) {
traceId = id;
stream.on('done', function() {
done(new Error('stream emitted `done` but `error` expected'));
});

@@ -97,13 +92,2 @@

if (done) {
done();
}
} else {
done(new Error('expected error but no error returned'));
}
});
stream.on('done', function(err) {
assert(typeof traceId !== 'undefined', 'traceId is undefined in stream done callback on `expectError` test');
if (err) {
if (done) {
if (_done === 1) {

@@ -119,7 +103,7 @@ done();

});
}
};
module.exports.doAuthenticate = function (client, done, apiKeyMaybe) {
module.exports.doAuthenticate = function(client, done, apiKeyMaybe) {
var apiKey = apiKeyMaybe || '1234';
client.authenticate('spec_tests', apiKey, function(err) {
client.authenticate('spec_tests', apiKey, function(err, token) {
if (err) {

@@ -130,4 +114,4 @@ done(new Error('failed to authenticate'));

if (client.token) {
done();
if (token) {
done(null, token);
} else {

@@ -137,3 +121,3 @@ done(new Error('protocol error: no token received from server'));

});
}
};

@@ -6,4 +6,5 @@ /* eslint-env node, mocha */

var process = require('process');
var sonicdHost = process.env.SONICD_HOST || 'wss://0.0.0.0:443';
var sonicEndpoint = (process.env.SONIC_HOST || 'wss://0.0.0.0:443') + '/v1/query';
var util = require('./util');
var token;

@@ -14,2 +15,3 @@ function runSpecTests(client, id) {

query: '5',
auth: token,
config: {

@@ -28,2 +30,3 @@ class: 'SyntheticSource',

query: '1',
auth: token,
config: {

@@ -40,2 +43,3 @@ class: 'UnknownClass'

query: null,
auth: token,
config: {

@@ -52,2 +56,3 @@ class: 'SyntheticSource'

query: '1',
auth: token,
config: null

@@ -63,2 +68,3 @@ };

query: '28',
auth: token,
config: {

@@ -76,2 +82,3 @@ class: 'SyntheticSource'

query: '-1',
auth: token,
config: {

@@ -93,5 +100,6 @@ class: 'SyntheticSource'

}
var query = {
query: q,
auth: token,
config: {

@@ -110,3 +118,3 @@ class: 'SyntheticSource',

var client = new Client(sonicdHost);
var client = new Client(sonicEndpoint);

@@ -143,3 +151,9 @@ runSpecTests(client, 'unauthenticated');

it('should authenticate user', function(done) {
util.doAuthenticate(client, done);
util.doAuthenticate(client, function(err, token) {
if (err) {
done(err);
return;
}
done();
});
});

@@ -151,6 +165,13 @@ });

var authenticated = new Client(sonicdHost);
var authenticated = new Client(sonicEndpoint);
beforeEach(function(done) {
util.doAuthenticate(authenticated, done);
before(function(done) {
util.doAuthenticate(authenticated, function(err, t) {
if (err) {
done(err);
return;
}
token = t;
done();
});
});

@@ -164,2 +185,3 @@

query: '5',
auth: token,
config: {

@@ -177,2 +199,3 @@ class: 'SyntheticSource',

query: '5',
auth: token,
config: {

@@ -188,16 +211,22 @@ class: 'SyntheticSource',

it('should return error if an authenticated and authorized user from not a whitelisted IP tries to run a query on a secured source', function(done) {
var query = {
query: '5',
config: {
class: 'SyntheticSource',
security: 1,
util.doAuthenticate(authenticated, function(err, token) {
if (err) {
done(err);
return;
}
};
util.doAuthenticate(authenticated, done, 'only_from_ip'); // check server's reference.conf
util.expectError(authenticated, query, done);
authenticated.close();
var query = {
query: '5',
auth: token,
config: {
class: 'SyntheticSource',
security: 1
}
};
util.expectError(authenticated, query, done);
authenticated.close();
}, 'only_from_ip'); // check server's reference.conf
});
});
});

@@ -17,15 +17,13 @@ 'use strict';

function Client(sonicdAddress, token) {
this.address = sonicdAddress;
this.token = token;
this.connections = [];
function Client(sonicAddress) {
this.url = sonicAddress;
}
function SonicdEmitter() {
function SonicEmitter() {
EventEmitter.call(this);
}
util.inherits(SonicdEmitter, EventEmitter);
util.inherits(SonicEmitter, EventEmitter);
Client.prototype.exec = function(address, command, done, outputCb, progressCb, metadataCb, startedCb) {
Client.prototype.send = function(doneCb, outputCb, progressCb, metadataCb, startedCb) {
var output = outputCb || (function() {});

@@ -36,99 +34,98 @@ var progress = progressCb || (function() {});

var isError = false;
var uri = address + '/v1/query';
var ws = new WebSocket(uri);
var traceId;
var self = this;
function closedUnexp() {
done(new Error('connection closed unexpectedly'), traceId);
}
return function(message, ws) {
ws.on('close', function(ev) {
var idx = self.connections.indexOf(ws);
ws.send(message);
// browser
if (BrowserWebSocket) {
if (isError) {
done(new Error('WebSocket close code: ' + ev.code + '; reason: ' + ev.reason), traceId);
} else if (ev.code !== 1000 && !isDone) {
function done(err, id) {
ws.close();
doneCb(err, id);
}
function closedUnexp() {
done(new Error('connection closed unexpectedly'));
}
ws.on('close', function(ev) {
// browser
if (BrowserWebSocket) {
if (isError) {
done(new Error('WebSocket close code: ' + ev.code + '; reason: ' + ev.reason));
} else if (ev.code !== 1000 && !isDone) {
closedUnexp();
}
// ws
} else if (!isDone && ev !== 1000) {
closedUnexp();
}
});
// ws
} else if (!isDone && ev !== 1000) {
closedUnexp();
}
ws.on('error', function(ev) {
// ev is defined with `ws`, but not with the
// browser's WebSocket API
if (BrowserWebSocket) {
isError = true;
} else {
isDone = true;
done(ev);
}
});
if (idx > 0) {
self.connections.splice(idx, 1);
}
});
ws.on('message', function(message) {
var msg = BrowserWebSocket ? JSON.parse(message.data) : JSON.parse(message.toString('utf-8'));
ws.on('error', function(ev) {
// ev is defined with `ws`, but not with the
// browser's WebSocket API
if (BrowserWebSocket) {
isError = true;
} else {
isDone = true;
done(ev, traceId);
}
});
switch (msg.e) {
case 'P':
progress(utils.toProgress(msg.p));
break;
ws.on('message', function(message) {
var msg = BrowserWebSocket ? JSON.parse(message.data) : JSON.parse(message.toString('utf-8'));
case 'D':
isDone = true;
ws.send(JSON.stringify({ e: 'A' }));
switch (msg.e) {
case 'P':
progress(utils.toProgress(msg.p));
break;
if (msg.v) {
done(new Error('Query with trace_id `' + msg.p.trace_id + '` failed: ' + msg.v));
} else {
done(null);
}
break;
case 'D':
isDone = true;
// send ack
ws.send(JSON.stringify({ e: 'A' }));
if (msg.v) {
done(new Error('Query with trace_id `' + msg.p.trace_id + '` failed: ' + msg.v), traceId);
} else {
done(null, traceId);
}
break;
case 'T':
metadata(msg.p.map(function(elem) {
return [elem[0], typeof elem[1]];
}));
break;
case 'T':
metadata(msg.p.map(function(elem) {
return [elem[0], typeof elem[1]];
}));
break;
case 'S':
if (typeof startedCb !== 'undefined') {
startedCb(msg.v);
}
break;
case 'S':
traceId = msg.v;
if (typeof startedCb !== 'undefined') {
startedCb(traceId);
}
break;
case 'O':
output(msg.p);
break;
case 'O':
output(msg.p);
break;
default:
// ignore to improve forwards compatibility
break;
}
});
};
};
default:
// ignore to improve forwards compatibility
break;
}
});
Client.prototype.exec = function(message, doneCb, outputCb, progressCb, metadataCb, startedCb) {
ws.on('error', function(err) {
done(err, traceId);
});
var ws = new WebSocket(this.url);
var doExec = this.send(doneCb, outputCb, progressCb, metadataCb, startedCb);
ws.on('open', function() {
ws.send(JSON.stringify(command));
doExec(JSON.stringify(message), ws);
});
return ws;
};
Client.prototype.stream = function(query) {
var emitter = new SonicdEmitter();
var queryMsg = utils.toMsg(query, this.token);
var emitter = new SonicEmitter();
var queryMsg = utils.toMsg(query);

@@ -157,6 +154,6 @@ function done(err) {

function started(traceId) {
emitter.emit('trace', traceId);
emitter.emit('started', traceId);
}
this.connections.push(this.exec(this.address, queryMsg, done, output, progress, metadata, started));
this.exec(queryMsg, done, output, progress, metadata, started);

@@ -168,10 +165,10 @@ return emitter;

var buffer = [];
var queryMsg = utils.toMsg(query, this.token);
var data = [];
var queryMsg = utils.toMsg(query);
function done(err, traceId) {
function done(err) {
if (err) {
doneCb(err, null, traceId);
doneCb(err, null);
} else {
doneCb(null, buffer, traceId);
doneCb(null, data);
}

@@ -181,10 +178,10 @@ }

function output(elems) {
buffer.push(elems);
data.push(elems);
}
this.connections.push(this.exec(this.address, queryMsg, done, output));
this.exec(queryMsg, done, output);
};
Client.prototype.authenticate = function(user, apiKey, doneCb, traceId) {
var self = this;
var token;
var authMsg = {

@@ -199,7 +196,7 @@ e: 'H',

function done(err, traceId) {
function done(err) {
if (err) {
doneCb(err, null, traceId);
doneCb(err, null);
} else {
doneCb(null, self.token, traceId);
doneCb(null, token);
}

@@ -209,20 +206,14 @@ }

function output(elems) {
self.token = elems[0];
token = elems[0];
}
this.connections.push(this.exec(this.address, authMsg, done, output));
this.exec(authMsg, done, output);
};
Client.prototype.close = function() {
if (!this.client) {
return;
if (this.ws) {
this.ws.close();
}
this.connections.forEach(function(conn) {
conn.close(1000, 'user closed');
});
};
module.exports.Client = Client;
'use strict';
function toMsg(query, tokenMaybe) {
function toMsg(query) {
var traceId = query.trace_id || query.traceId;
var token = query.token || query.auth || tokenMaybe;
var token = query.token || query.auth;
return {

@@ -7,0 +7,0 @@ e: 'Q',

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc