Skip to content

Commit

Permalink
Merge pull request #37 from kuzzleio/indexMandatory
Browse files Browse the repository at this point in the history
[Breaking] Index as a mandatory argument
  • Loading branch information
scottinet committed Dec 24, 2015
2 parents 2d49128 + ade3f95 commit 0b45d8a
Show file tree
Hide file tree
Showing 20 changed files with 250 additions and 161 deletions.
139 changes: 89 additions & 50 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 @@ -263,15 +288,16 @@ var
/**
* Kuzzle object constructor.
* @param url - URL to the Kuzzle instance
* @param index - Database index
* @param [options] - Connection options
* @param {responseCallback} [cb] - Handles connection response
* @constructor
*/
module.exports = Kuzzle = function (url, options, cb) {
module.exports = Kuzzle = function (url, index, options, cb) {
var self = this;

if (!(this instanceof Kuzzle)) {
return new Kuzzle(url, options, cb);
return new Kuzzle(url, index, options, cb);
}

if (!cb && typeof options === 'function') {
Expand All @@ -280,7 +306,11 @@ module.exports = Kuzzle = function (url, options, cb) {
}

if (!url || url === '') {
throw new Error('URL to Kuzzle can\'t be empty');
throw new Error('URL argument missing');
}

if (!index || index === '') {
throw new Error('Index argument missing');
}

Object.defineProperties(this, {
Expand Down Expand Up @@ -344,7 +374,7 @@ module.exports = Kuzzle = function (url, options, cb) {
enumerable: true
},
index: {
value: (options && typeof options.index === 'string') ? options.index : 'mainindex',
value: index,
enumerable: true
},
reconnectionDelay: {
Expand Down Expand Up @@ -1797,7 +1827,15 @@ KuzzleDataMapping.prototype.refresh = function (options, cb) {
return cb ? cb(err) : false;
}

self.mapping = res.mainindex.mappings[self.collection].properties;
if (res[self.kuzzle.index]) {
if (res[self.kuzzle.index].mappings[self.collection]) {
self.mapping = res[self.kuzzle.index].mappings[self.collection].properties;
} else {
return cb ? cb(new Error('No mapping found for collection ' + self.collection)) : false;
}
} else {
return cb ? cb(new Error('No mapping found for index ' + self.kuzzle.index)) : false;
}

if (cb) {
cb(null, self);
Expand All @@ -1814,6 +1852,7 @@ KuzzleDataMapping.prototype.refresh = function (options, cb) {
* Changes made by this function won’t be applied until you call the apply method
*
* @param {string} field - Name of the field from which the mapping is to be added or updated
* @param {object} mapping - corresponding field mapping
* @returns {KuzzleDataMapping}
*/
KuzzleDataMapping.prototype.set = function (field, mapping) {
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.1.1",
"version": "1.2.0",
"description": "Official Javascript SDK for Kuzzle",
"author": "The Kuzzle Team <support@kuzzle.io>",
"repository": {
Expand Down
13 changes: 9 additions & 4 deletions src/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ var
/**
* Kuzzle object constructor.
* @param url - URL to the Kuzzle instance
* @param index - Database index
* @param [options] - Connection options
* @param {responseCallback} [cb] - Handles connection response
* @constructor
*/
module.exports = Kuzzle = function (url, options, cb) {
module.exports = Kuzzle = function (url, index, options, cb) {
var self = this;

if (!(this instanceof Kuzzle)) {
return new Kuzzle(url, options, cb);
return new Kuzzle(url, index, options, cb);
}

if (!cb && typeof options === 'function') {
Expand All @@ -30,7 +31,11 @@ module.exports = Kuzzle = function (url, options, cb) {
}

if (!url || url === '') {
throw new Error('URL to Kuzzle can\'t be empty');
throw new Error('URL argument missing');
}

if (!index || index === '') {
throw new Error('Index argument missing');
}

Object.defineProperties(this, {
Expand Down Expand Up @@ -94,7 +99,7 @@ module.exports = Kuzzle = function (url, options, cb) {
enumerable: true
},
index: {
value: (options && typeof options.index === 'string') ? options.index : 'mainindex',
value: index,
enumerable: true
},
reconnectionDelay: {
Expand Down
11 changes: 10 additions & 1 deletion src/kuzzleDataMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,15 @@ KuzzleDataMapping.prototype.refresh = function (options, cb) {
return cb ? cb(err) : false;
}

self.mapping = res.mainindex.mappings[self.collection].properties;
if (res[self.kuzzle.index]) {
if (res[self.kuzzle.index].mappings[self.collection]) {
self.mapping = res[self.kuzzle.index].mappings[self.collection].properties;
} else {
return cb ? cb(new Error('No mapping found for collection ' + self.collection)) : false;
}
} else {
return cb ? cb(new Error('No mapping found for index ' + self.kuzzle.index)) : false;
}

if (cb) {
cb(null, self);
Expand All @@ -129,6 +137,7 @@ KuzzleDataMapping.prototype.refresh = function (options, cb) {
* Changes made by this function won’t be applied until you call the apply method
*
* @param {string} field - Name of the field from which the mapping is to be added or updated
* @param {object} mapping - corresponding field mapping
* @returns {KuzzleDataMapping}
*/
KuzzleDataMapping.prototype.set = function (field, mapping) {
Expand Down
Loading

0 comments on commit 0b45d8a

Please sign in to comment.