Skip to content

Commit

Permalink
feat: allow multiple origins in config (#231)
Browse files Browse the repository at this point in the history
* feat: allow multiple origins in config

* PubNub SDK v4.35.0 release.

Co-authored-by: Serhii Mamontov <parfeon@me.com>
Co-authored-by: Client Engineering Bot <60980775+Client Engineering Bot@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 6, 2021
1 parent d4180ec commit 5945f98
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 89 deletions.
11 changes: 8 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
---
changelog:
- date: 2021-12-02
version: v4.35.0
changes:
- type: feature
text: "Allows to specify multiple origins in the config, which enables domain sharding for custom origins."
- date: 2021-12-01
version: v4.34.2
changes:
Expand Down Expand Up @@ -1076,7 +1081,7 @@ supported-platforms:
- "Ubuntu 14.04 and up"
- "Windows 7 and up"
version: "Pubnub Javascript for Node"
version: "4.34.2"
version: "4.35.0"
sdks:
-
full-name: PubNub Javascript SDK
Expand All @@ -1095,7 +1100,7 @@ sdks:
distribution-type: source
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/archive/refs/tags/v4.34.2.zip
location: https://github.com/pubnub/javascript/archive/refs/tags/v4.35.0.zip
requires:
-
name: "agentkeepalive"
Expand Down Expand Up @@ -1859,7 +1864,7 @@ sdks:
distribution-type: library
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/releases/download/v4.34.2/pubnub.4.34.2.js
location: https://github.com/pubnub/javascript/releases/download/v4.35.0/pubnub.4.35.0.js
requires:
-
name: "agentkeepalive"
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v4.35.0
December 02 2021

#### Added
- Allows to specify multiple origins in the config, which enables domain sharding for custom origins.

## v4.34.2
December 01 2021

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ You will need the publish and subscribe keys to authenticate your app. Get your
npm install pubnub
```
* or download one of our builds from our CDN:
* https://cdn.pubnub.com/sdk/javascript/pubnub.4.34.2.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.4.34.2.min.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.4.35.0.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.4.35.0.min.js

2. Configure your keys:

Expand Down
53 changes: 35 additions & 18 deletions dist/titanium/pubnub.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! 4.34.2 / Consumer */
/*! 4.35.0 / Consumer */
exports["PubNub"] =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
Expand Down Expand Up @@ -366,6 +366,14 @@ var _flow_interfaces = __webpack_require__(2);
var PRESENCE_TIMEOUT_MINIMUM = 20;
var PRESENCE_TIMEOUT_DEFAULT = 300;

var makeDefaultOrigins = function makeDefaultOrigins() {
return Array.from({
length: 20
}, function (_, i) {
return "ps".concat(i + 1, ".pndsn.com");
});
};

var _default = function () {
function _default(_ref) {
var _setup$fileUploadPubl, _setup$useRandomIVs;
Expand Down Expand Up @@ -423,7 +431,12 @@ var _default = function () {
this.setAuthKey(setup.authKey);
this.setCipherKey(setup.cipherKey);
this.setFilterExpression(setup.filterExpression);
this.origin = setup.origin || 'ps.pndsn.com';

if (typeof setup.origin !== 'string' && !Array.isArray(setup.origin) && setup.origin !== undefined) {
throw new Error('Origin must be either undefined, a string or a list of strings.');
}

this.origin = setup.origin || makeDefaultOrigins();
this.secure = setup.ssl || false;
this.restore = setup.restore || false;
this.proxy = setup.proxy;
Expand Down Expand Up @@ -575,7 +588,7 @@ var _default = function () {
}, {
key: "getVersion",
value: function getVersion() {
return '4.34.2';
return '4.35.0';
}
}, {
key: "_addPnsdkSuffix",
Expand Down Expand Up @@ -8638,7 +8651,8 @@ var _default = function _default(modules, _ref) {
var channel = _ref.channel,
id = _ref.id,
name = _ref.name;
var config = modules.config;
var config = modules.config,
networking = modules.networking;

if (!channel) {
throw new _endpoint.PubNubError('Validation failed, check status for details', (0, _endpoint.createValidationError)("channel can't be empty"));
Expand Down Expand Up @@ -8674,10 +8688,10 @@ var _default = function _default(modules, _ref) {
}).join('&');

if (queryParams !== '') {
return "https://".concat(config.origin).concat(url, "?").concat(queryParams);
return "".concat(networking.getStandardOrigin()).concat(url, "?").concat(queryParams);
}

return "https://".concat(config.origin).concat(url);
return "".concat(networking.getStandardOrigin()).concat(url);
};

exports["default"] = _default;
Expand Down Expand Up @@ -13265,11 +13279,9 @@ var _default = function () {
(0, _classCallCheck2["default"])(this, _default);
(0, _defineProperty2["default"])(this, "_modules", void 0);
(0, _defineProperty2["default"])(this, "_config", void 0);
(0, _defineProperty2["default"])(this, "_maxSubDomain", void 0);
(0, _defineProperty2["default"])(this, "_currentSubDomain", void 0);
(0, _defineProperty2["default"])(this, "_standardOrigin", void 0);
(0, _defineProperty2["default"])(this, "_subscribeOrigin", void 0);
(0, _defineProperty2["default"])(this, "_providedFQDN", void 0);
(0, _defineProperty2["default"])(this, "_requestTimeout", void 0);
(0, _defineProperty2["default"])(this, "_coreParams", void 0);
this._modules = {};
Expand All @@ -13282,28 +13294,33 @@ var _default = function () {
key: "init",
value: function init(config) {
this._config = config;
this._maxSubDomain = 20;
this._currentSubDomain = Math.floor(Math.random() * this._maxSubDomain);
this._providedFQDN = (this._config.secure ? 'https://' : 'http://') + this._config.origin;

if (Array.isArray(this._config.origin)) {
this._currentSubDomain = Math.floor(Math.random() * this._config.origin.length);
} else {
this._currentSubDomain = 0;
}

this._coreParams = {};
this.shiftStandardOrigin();
}
}, {
key: "nextOrigin",
value: function nextOrigin() {
if (!this._providedFQDN.match(/ps\.pndsn\.com$/i)) {
return this._providedFQDN;
var protocol = this._config.secure ? 'https://' : 'http://';

if (typeof this._config.origin === 'string') {
return "".concat(protocol).concat(this._config.origin);
}

var newSubDomain;
this._currentSubDomain += 1;

if (this._currentSubDomain >= this._maxSubDomain) {
this._currentSubDomain = 1;
if (this._currentSubDomain >= this._config.origin.length) {
this._currentSubDomain = 0;
}

newSubDomain = this._currentSubDomain.toString();
return this._providedFQDN.replace('ps.pndsn.com', "ps".concat(newSubDomain, ".pndsn.com"));
var origin = this._config.origin[this._currentSubDomain];
return "".concat(protocol).concat(origin);
}
}, {
key: "hasModule",
Expand Down
4 changes: 2 additions & 2 deletions dist/titanium/pubnub.min.js

Large diffs are not rendered by default.

53 changes: 35 additions & 18 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! 4.34.2 / Consumer */
/*! 4.35.0 / Consumer */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
Expand Down Expand Up @@ -376,6 +376,14 @@ var _flow_interfaces = __webpack_require__(2);
var PRESENCE_TIMEOUT_MINIMUM = 20;
var PRESENCE_TIMEOUT_DEFAULT = 300;

var makeDefaultOrigins = function makeDefaultOrigins() {
return Array.from({
length: 20
}, function (_, i) {
return "ps".concat(i + 1, ".pndsn.com");
});
};

var _default = function () {
function _default(_ref) {
var _setup$fileUploadPubl, _setup$useRandomIVs;
Expand Down Expand Up @@ -433,7 +441,12 @@ var _default = function () {
this.setAuthKey(setup.authKey);
this.setCipherKey(setup.cipherKey);
this.setFilterExpression(setup.filterExpression);
this.origin = setup.origin || 'ps.pndsn.com';

if (typeof setup.origin !== 'string' && !Array.isArray(setup.origin) && setup.origin !== undefined) {
throw new Error('Origin must be either undefined, a string or a list of strings.');
}

this.origin = setup.origin || makeDefaultOrigins();
this.secure = setup.ssl || false;
this.restore = setup.restore || false;
this.proxy = setup.proxy;
Expand Down Expand Up @@ -585,7 +598,7 @@ var _default = function () {
}, {
key: "getVersion",
value: function getVersion() {
return '4.34.2';
return '4.35.0';
}
}, {
key: "_addPnsdkSuffix",
Expand Down Expand Up @@ -11467,7 +11480,8 @@ var _default = function _default(modules, _ref) {
var channel = _ref.channel,
id = _ref.id,
name = _ref.name;
var config = modules.config;
var config = modules.config,
networking = modules.networking;

if (!channel) {
throw new _endpoint.PubNubError('Validation failed, check status for details', (0, _endpoint.createValidationError)("channel can't be empty"));
Expand Down Expand Up @@ -11503,10 +11517,10 @@ var _default = function _default(modules, _ref) {
}).join('&');

if (queryParams !== '') {
return "https://".concat(config.origin).concat(url, "?").concat(queryParams);
return "".concat(networking.getStandardOrigin()).concat(url, "?").concat(queryParams);
}

return "https://".concat(config.origin).concat(url);
return "".concat(networking.getStandardOrigin()).concat(url);
};

exports["default"] = _default;
Expand Down Expand Up @@ -16094,11 +16108,9 @@ var _default = function () {
(0, _classCallCheck2["default"])(this, _default);
(0, _defineProperty2["default"])(this, "_modules", void 0);
(0, _defineProperty2["default"])(this, "_config", void 0);
(0, _defineProperty2["default"])(this, "_maxSubDomain", void 0);
(0, _defineProperty2["default"])(this, "_currentSubDomain", void 0);
(0, _defineProperty2["default"])(this, "_standardOrigin", void 0);
(0, _defineProperty2["default"])(this, "_subscribeOrigin", void 0);
(0, _defineProperty2["default"])(this, "_providedFQDN", void 0);
(0, _defineProperty2["default"])(this, "_requestTimeout", void 0);
(0, _defineProperty2["default"])(this, "_coreParams", void 0);
this._modules = {};
Expand All @@ -16111,28 +16123,33 @@ var _default = function () {
key: "init",
value: function init(config) {
this._config = config;
this._maxSubDomain = 20;
this._currentSubDomain = Math.floor(Math.random() * this._maxSubDomain);
this._providedFQDN = (this._config.secure ? 'https://' : 'http://') + this._config.origin;

if (Array.isArray(this._config.origin)) {
this._currentSubDomain = Math.floor(Math.random() * this._config.origin.length);
} else {
this._currentSubDomain = 0;
}

this._coreParams = {};
this.shiftStandardOrigin();
}
}, {
key: "nextOrigin",
value: function nextOrigin() {
if (!this._providedFQDN.match(/ps\.pndsn\.com$/i)) {
return this._providedFQDN;
var protocol = this._config.secure ? 'https://' : 'http://';

if (typeof this._config.origin === 'string') {
return "".concat(protocol).concat(this._config.origin);
}

var newSubDomain;
this._currentSubDomain += 1;

if (this._currentSubDomain >= this._maxSubDomain) {
this._currentSubDomain = 1;
if (this._currentSubDomain >= this._config.origin.length) {
this._currentSubDomain = 0;
}

newSubDomain = this._currentSubDomain.toString();
return this._providedFQDN.replace('ps.pndsn.com', "ps".concat(newSubDomain, ".pndsn.com"));
var origin = this._config.origin[this._currentSubDomain];
return "".concat(protocol).concat(origin);
}
}, {
key: "hasModule",
Expand Down
6 changes: 3 additions & 3 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions lib/core/components/config.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5945f98

Please sign in to comment.