Skip to content

Commit

Permalink
Merge pull request #33 from kuzzleio/KUZ-335_list_realtime_collections
Browse files Browse the repository at this point in the history
implemented new listCollections arguments
  • Loading branch information
scottinet committed Dec 18, 2015
2 parents 341a64a + 1bb4596 commit 0f0cd89
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 85 deletions.
147 changes: 90 additions & 57 deletions dist/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,74 @@
// Copyright (c) 2010-2012 Robert Kieffer
// MIT License - http://opensource.org/licenses/mit-license.php

(function() {
var _global = this;
/*global window, require, define */
(function(_window) {
'use strict';

// Unique ID creation requires a high quality random # generator. We feature
// detect to determine the best RNG source, normalizing to a function that
// returns 128-bits of randomness, since that's what's usually required
var _rng;
var _rng, _mathRNG, _nodeRNG, _whatwgRNG, _previousRoot;

function setupBrowser() {
// Allow for MSIE11 msCrypto
var _crypto = _window.crypto || _window.msCrypto;

if (!_rng && _crypto && _crypto.getRandomValues) {
// WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
//
// Moderately fast, high quality
try {
var _rnds8 = new Uint8Array(16);
_whatwgRNG = _rng = function whatwgRNG() {
_crypto.getRandomValues(_rnds8);
return _rnds8;
};
_rng();
} catch(e) {}
}

// Node.js crypto-based RNG - http://nodejs.org/docs/v0.6.2/api/crypto.html
//
// Moderately fast, high quality
if (typeof(_global.require) == 'function') {
try {
var _rb = _global.require('crypto').randomBytes;
_rng = _rb && function() {return _rb(16);};
} catch(e) {}
if (!_rng) {
// Math.random()-based (RNG)
//
// If all else fails, use Math.random(). It's fast, but is of unspecified
// quality.
var _rnds = new Array(16);
_mathRNG = _rng = function() {
for (var i = 0, r; i < 16; i++) {
if ((i & 0x03) === 0) { r = Math.random() * 0x100000000; }
_rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
}

return _rnds;
};
if ('undefined' !== typeof console && console.warn) {
console.warn("[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()");
}
}
}

if (!_rng && _global.crypto && crypto.getRandomValues) {
// WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
function setupNode() {
// Node.js crypto-based RNG - http://nodejs.org/docs/v0.6.2/api/crypto.html
//
// Moderately fast, high quality
var _rnds8 = new Uint8Array(16);
_rng = function whatwgRNG() {
crypto.getRandomValues(_rnds8);
return _rnds8;
};
if ('function' === typeof require) {
try {
var _rb = require('crypto').randomBytes;
_nodeRNG = _rng = _rb && function() {return _rb(16);};
_rng();
} catch(e) {}
}
}

if (!_rng) {
// Math.random()-based (RNG)
//
// If all else fails, use Math.random(). It's fast, but is of unspecified
// quality.
var _rnds = new Array(16);
_rng = function() {
for (var i = 0, r; i < 16; i++) {
if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
_rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
}

return _rnds;
};
if (_window) {
setupBrowser();
} else {
setupNode();
}

// Buffer class to use
var BufferClass = typeof(_global.Buffer) == 'function' ? _global.Buffer : Array;
var BufferClass = ('function' === typeof Buffer) ? Buffer : Array;

// Maps for number <-> hex string conversion
var _byteToHex = [];
Expand Down Expand Up @@ -119,17 +140,17 @@

options = options || {};

var clockseq = options.clockseq != null ? options.clockseq : _clockseq;
var clockseq = (options.clockseq != null) ? options.clockseq : _clockseq;

// UUID timestamps are 100 nano-second units since the Gregorian epoch,
// (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
// time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
// (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
var msecs = options.msecs != null ? options.msecs : new Date().getTime();
var msecs = (options.msecs != null) ? options.msecs : new Date().getTime();

// Per 4.2.1.2, use count of uuid's generated during the current clock
// cycle to simulate higher resolution clock
var nsecs = options.nsecs != null ? options.nsecs : _lastNSecs + 1;
var nsecs = (options.nsecs != null) ? options.nsecs : _lastNSecs + 1;

// Time since last uuid creation (in msecs)
var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
Expand Down Expand Up @@ -195,8 +216,8 @@
// Deprecated - 'format' argument, as supported in v1.2
var i = buf && offset || 0;

if (typeof(options) == 'string') {
buf = options == 'binary' ? new BufferClass(16) : null;
if (typeof(options) === 'string') {
buf = (options === 'binary') ? new BufferClass(16) : null;
options = null;
}
options = options || {};
Expand Down Expand Up @@ -224,28 +245,32 @@
uuid.parse = parse;
uuid.unparse = unparse;
uuid.BufferClass = BufferClass;
uuid._rng = _rng;
uuid._mathRNG = _mathRNG;
uuid._nodeRNG = _nodeRNG;
uuid._whatwgRNG = _whatwgRNG;

if (typeof(module) != 'undefined' && module.exports) {
if (('undefined' !== typeof module) && module.exports) {
// Publish as node.js module
module.exports = uuid;
} else if (typeof define === 'function' && define.amd) {
} else if (typeof define === 'function' && define.amd) {
// Publish as AMD module
define(function() {return uuid;});


} else {
// Publish as global (in browsers)
var _previousRoot = _global.uuid;
_previousRoot = _window.uuid;

// **`noConflict()` - (browser only) to reset global 'uuid' var**
uuid.noConflict = function() {
_global.uuid = _previousRoot;
_window.uuid = _previousRoot;
return uuid;
};

_global.uuid = uuid;
_window.uuid = uuid;
}
}).call(this);
})('undefined' !== typeof window ? window : null);

},{}],2:[function(require,module,exports){
var
Expand All @@ -264,7 +289,7 @@ var
* Kuzzle object constructor.
* @param url - URL to the Kuzzle instance
* @param [options] - Connection options
* @param {Kuzzle~constructorCallback} [cb] - Handles connection response
* @param {responseCallback} [cb] - Handles connection response
* @constructor
*/
module.exports = Kuzzle = function (url, options, cb) {
Expand All @@ -289,6 +314,9 @@ module.exports = Kuzzle = function (url, options, cb) {
value: {},
writable: true
},
connectCB: {
value: cb
},
eventListeners: {
value: {
connected: [],
Expand Down Expand Up @@ -469,7 +497,7 @@ module.exports = Kuzzle = function (url, options, cb) {


if (!options || !options.connect || options.connect === 'auto') {
this.connect(cb);
this.connect();
} else {
this.state = 'ready';
}
Expand All @@ -478,7 +506,7 @@ module.exports = Kuzzle = function (url, options, cb) {
return this.bluebird.promisifyAll(this, {
suffix: 'Promise',
filter: function (name, func, target, passes) {
var whitelist = ['connect', 'getAllStatistics', 'getStatistics', 'listCollections', 'now', 'query'];
var whitelist = ['getAllStatistics', 'getStatistics', 'listCollections', 'now', 'query'];

return passes && whitelist.indexOf(name) !== -1;
}
Expand All @@ -491,15 +519,14 @@ module.exports = Kuzzle = function (url, options, cb) {

/**
* Connects to a Kuzzle instance using the provided URL.
* @param {responseCallback} [cb]
* @returns {Object} this
*/
Kuzzle.prototype.connect = function (cb) {
Kuzzle.prototype.connect = function () {
var self = this;

if (['initializing', 'ready', 'loggedOff', 'error', 'offline'].indexOf(this.state) === -1) {
if (cb) {
cb(null, self);
if (self.connectCB) {
self.connectCB(null, self);
}
return self;
}
Expand Down Expand Up @@ -528,8 +555,8 @@ Kuzzle.prototype.connect = function (cb) {
listener.fn();
});

if (cb) {
cb(null, self);
if (self.connectCB) {
self.connectCB(null, self);
}
});

Expand All @@ -540,8 +567,8 @@ Kuzzle.prototype.connect = function (cb) {
listener.fn();
});

if (cb) {
cb(error);
if (self.connectCB) {
self.connectCB(error);
}
});

Expand Down Expand Up @@ -810,14 +837,20 @@ Kuzzle.prototype.flushQueue = function () {
* @returns {object} this
*/
Kuzzle.prototype.listCollections = function (options, cb) {
var collectionType = 'all';

if (!cb && typeof options === 'function') {
cb = options;
options = null;
}

this.callbackRequired('Kuzzle.listCollections', cb);

this.query(null, 'read', 'listCollections', {}, options, function (err, res) {
if (options && options.type) {
collectionType = options.type;
}

this.query(null, 'read', 'listCollections', {body: {type: collectionType}}, options, function (err, res) {
if (err) {
return cb(err);
}
Expand Down
4 changes: 2 additions & 2 deletions dist/kuzzle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kuzzle.min.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kuzzle-sdk",
"version": "1.0.9",
"version": "1.1.0",
"description": "Official Javascript SDK for Kuzzle",
"author": "The Kuzzle Team <support@kuzzle.io>",
"repository": {
Expand Down
32 changes: 20 additions & 12 deletions src/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var
* Kuzzle object constructor.
* @param url - URL to the Kuzzle instance
* @param [options] - Connection options
* @param {Kuzzle~constructorCallback} [cb] - Handles connection response
* @param {responseCallback} [cb] - Handles connection response
* @constructor
*/
module.exports = Kuzzle = function (url, options, cb) {
Expand All @@ -39,6 +39,9 @@ module.exports = Kuzzle = function (url, options, cb) {
value: {},
writable: true
},
connectCB: {
value: cb
},
eventListeners: {
value: {
connected: [],
Expand Down Expand Up @@ -219,7 +222,7 @@ module.exports = Kuzzle = function (url, options, cb) {


if (!options || !options.connect || options.connect === 'auto') {
this.connect(cb);
this.connect();
} else {
this.state = 'ready';
}
Expand All @@ -228,7 +231,7 @@ module.exports = Kuzzle = function (url, options, cb) {
return this.bluebird.promisifyAll(this, {
suffix: 'Promise',
filter: function (name, func, target, passes) {
var whitelist = ['connect', 'getAllStatistics', 'getStatistics', 'listCollections', 'now', 'query'];
var whitelist = ['getAllStatistics', 'getStatistics', 'listCollections', 'now', 'query'];

return passes && whitelist.indexOf(name) !== -1;
}
Expand All @@ -241,15 +244,14 @@ module.exports = Kuzzle = function (url, options, cb) {

/**
* Connects to a Kuzzle instance using the provided URL.
* @param {responseCallback} [cb]
* @returns {Object} this
*/
Kuzzle.prototype.connect = function (cb) {
Kuzzle.prototype.connect = function () {
var self = this;

if (['initializing', 'ready', 'loggedOff', 'error', 'offline'].indexOf(this.state) === -1) {
if (cb) {
cb(null, self);
if (self.connectCB) {
self.connectCB(null, self);
}
return self;
}
Expand Down Expand Up @@ -278,8 +280,8 @@ Kuzzle.prototype.connect = function (cb) {
listener.fn();
});

if (cb) {
cb(null, self);
if (self.connectCB) {
self.connectCB(null, self);
}
});

Expand All @@ -290,8 +292,8 @@ Kuzzle.prototype.connect = function (cb) {
listener.fn();
});

if (cb) {
cb(error);
if (self.connectCB) {
self.connectCB(error);
}
});

Expand Down Expand Up @@ -560,14 +562,20 @@ Kuzzle.prototype.flushQueue = function () {
* @returns {object} this
*/
Kuzzle.prototype.listCollections = function (options, cb) {
var collectionType = 'all';

if (!cb && typeof options === 'function') {
cb = options;
options = null;
}

this.callbackRequired('Kuzzle.listCollections', cb);

this.query(null, 'read', 'listCollections', {}, options, function (err, res) {
if (options && options.type) {
collectionType = options.type;
}

this.query(null, 'read', 'listCollections', {body: {type: collectionType}}, options, function (err, res) {
if (err) {
return cb(err);
}
Expand Down
Loading

0 comments on commit 0f0cd89

Please sign in to comment.