Skip to content

Commit

Permalink
lib and dist
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Jan 8, 2024
1 parent c253e96 commit f7b92ad
Show file tree
Hide file tree
Showing 32 changed files with 1,674 additions and 199 deletions.
748 changes: 655 additions & 93 deletions dist/web/pubnub.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

38 changes: 20 additions & 18 deletions lib/core/components/config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
"use strict";
/* */
/* global location */
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Expand All @@ -23,7 +12,7 @@ var makeDefaultOrigins = function () { return Array.from({ length: 20 }, functio
var default_1 = /** @class */ (function () {
function default_1(_a) {
var setup = _a.setup;
var _b, _c, _d;
var _b, _c, _d, _e;
this._PNSDKSuffix = {};
this.instanceId = "pn-".concat(uuid_1.default.createUUID());
this.secretKey = setup.secretKey || setup.secret_key;
Expand Down Expand Up @@ -51,10 +40,8 @@ var default_1 = /** @class */ (function () {
this.customDecrypt = setup.customDecrypt;
this.fileUploadPublishRetryLimit = (_b = setup.fileUploadPublishRetryLimit) !== null && _b !== void 0 ? _b : 5;
this.useRandomIVs = (_c = setup.useRandomIVs) !== null && _c !== void 0 ? _c : true;
// flag for beta subscribe feature enablement
this.enableSubscribeBeta = (_d = setup.enableSubscribeBeta) !== null && _d !== void 0 ? _d : false;
// reconnection configuration settings to apply reconnection settings in subscription
this.reconnectionConfiguration = setup.reconnectionConfiguration || { reconnectionPolicy: 'None' };
this.enableEventEngine = (_d = setup.enableEventEngine) !== null && _d !== void 0 ? _d : false;
this.maintainPresenceState = (_e = setup.maintainPresenceState) !== null && _e !== void 0 ? _e : true;
// if location config exist and we are in https, force secure to true.
if (typeof location !== 'undefined' && location.protocol === 'https:') {
this.secure = true;
Expand All @@ -66,6 +53,9 @@ var default_1 = /** @class */ (function () {
this.useInstanceId = setup.useInstanceId || false;
this.useRequestId = setup.useRequestId || false;
this.requestMessageCountThreshold = setup.requestMessageCountThreshold;
if (setup.retryConfiguration) {
this.setRetryConfiguration(setup.retryConfiguration);
}
// set timeout to how long a transaction request will wait for the server (default 15 seconds)
this.setTransactionTimeout(setup.transactionalRequestTimeout || 15 * 1000);
// set timeout to how long a subscribe event loop will run (default 310 seconds)
Expand Down Expand Up @@ -184,8 +174,20 @@ var default_1 = /** @class */ (function () {
default_1.prototype.getVersion = function () {
return '7.2.3';
};
default_1.prototype.setReconnectionConfiguration = function (reconnectionPolicy, maximumReconnectionRetries) {
this.reconnectionConfiguration = __assign(__assign({}, config.reconnectionConfiguration), { reconnectionPolicy: reconnectionPolicy, maximumReconnectionRetries: maximumReconnectionRetries });
default_1.prototype.setRetryConfiguration = function (configuration) {
if (configuration.minimumdelay < 2) {
throw new Error('Minimum delay can not be set less than 2 seconds for retry');
}
if (configuration.maximumDelay > 150) {
throw new Error('Maximum delay can not be set more than 150 seconds for retry');
}
if (configuration.maximumDelay && maximumRetry > 6) {
throw new Error('Maximum retry for exponential retry policy can not be more than 6');
}
else if (configuration.maximumRetry > 10) {
throw new Error('Maximum retry for linear retry policy can not be more than 10');
}
this.retryConfiguration = configuration;
};
default_1.prototype._addPnsdkSuffix = function (name, suffix) {
this._PNSDKSuffix[name] = suffix;
Expand Down
2 changes: 2 additions & 0 deletions lib/core/constants/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ exports.default = {
PNConnectedCategory: 'PNConnectedCategory',
PNRequestMessageCountExceededCategory: 'PNRequestMessageCountExceededCategory',
PNDisconnectedCategory: 'PNDisconnectedCategory',
PNConnectionErrorCategory: 'PNConnectionErrorCategory',
PNDisconnectedUnexpectedlyCategory: 'PNDisconnectedUnexpectedlyCategory',
};
6 changes: 4 additions & 2 deletions lib/core/endpoints/presence/heartbeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ function getRequestTimeout(_a) {
}
exports.getRequestTimeout = getRequestTimeout;
function prepareParams(modules, incomingParams) {
var _a = incomingParams.channelGroups, channelGroups = _a === void 0 ? [] : _a, _b = incomingParams.state, state = _b === void 0 ? {} : _b;
var _a = incomingParams.channelGroups, channelGroups = _a === void 0 ? [] : _a, state = incomingParams.state;
var config = modules.config;
var params = {};
if (channelGroups.length > 0) {
params['channel-group'] = channelGroups.join(',');
}
params.state = JSON.stringify(state);
if (state) {
params.state = JSON.stringify(state);
}
params.heartbeat = config.getPresenceTimeout();
return params;
}
Expand Down
7 changes: 7 additions & 0 deletions lib/core/endpoints/subscriptionUtils/handshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ var endpoint = {
outParams['channel-group'] = params.channelGroups.join(',');
}
outParams.tt = 0;
if (params.state) {
outParams.state = JSON.stringify(params.state);
}
if (params.filterExpression && params.filterExpression.length > 0) {
outParams['filter-expr'] = params.filterExpression;
}
outParams.ee = '';
return outParams;
},
handleResponse: function (_, response) { return ({
Expand Down
4 changes: 4 additions & 0 deletions lib/core/endpoints/subscriptionUtils/receiveMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ var endpoint = {
if (params.channelGroups && params.channelGroups.length > 0) {
outParams['channel-group'] = params.channelGroups.join(',');
}
if (params.filterExpression && params.filterExpression.length > 0) {
outParams['filter-expr'] = params.filterExpression;
}
outParams.tt = params.timetoken;
outParams.tr = params.region;
outParams.ee = '';
return outParams;
},
handleResponse: function (_, response) {
Expand Down
46 changes: 39 additions & 7 deletions lib/core/pubnub-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ var operations_1 = __importDefault(require("./constants/operations"));
var categories_1 = __importDefault(require("./constants/categories"));
var uuid_1 = __importDefault(require("./components/uuid"));
var event_engine_1 = require("../event-engine");
var reconnectionDelay_1 = require("../event-engine/core/reconnectionDelay");
var presence_1 = require("../event-engine/presence/presence");
var retryPolicy_1 = require("../event-engine/core/retryPolicy");
var default_1 = /** @class */ (function () {
//
function default_1(setup) {
var _this = this;
var _a;
var networking = setup.networking, cbor = setup.cbor;
var config = new config_1.default({ setup: setup });
this._config = config;
Expand Down Expand Up @@ -183,15 +183,45 @@ var default_1 = /** @class */ (function () {
this.setPresenceState = endpoint_1.default.bind(this, modules, presenceSetStateConfig);
this.handshake = endpoint_1.default.bind(this, modules, handshake_1.default);
this.receiveMessages = endpoint_1.default.bind(this, modules, receiveMessages_1.default);
if (config.enableSubscribeBeta === true) {
var policy_1 = modules.config.reconnectionConfiguration.reconnectionPolicy;
var maxRetries_1 = (_a = modules.config.reconnectionConfiguration.maximumReconnectionRetries) !== null && _a !== void 0 ? _a : 0;
if (config.enableEventEngine === true) {
if (config.maintainPresenceState) {
this.presenceState = {};
this.setState = function (args) {
var _a, _b;
(_a = args.channels) === null || _a === void 0 ? void 0 : _a.forEach(function (channel) { return (_this.presenceState[channel] = args.state); });
(_b = args.channelGroups) === null || _b === void 0 ? void 0 : _b.forEach(function (group) { return (_this.presenceState[group] = args.state); });
return _this.setPresenceState({
channels: args.channels,
channelGroups: args.channelGroups,
state: _this.presenceState,
});
};
}
if (config.getHeartbeatInterval()) {
var presenceEventEngine = new presence_1.PresenceEventEngine({
heartbeat: this.iAmHere,
leave: this.iAmAway,
heartbeatDelay: function () {
return new Promise(function (resolve) { return setTimeout(resolve, modules.config.getHeartbeatInterval() * 1000); });
},
retryDelay: function (amount) { return new Promise(function (resolve) { return setTimeout(resolve, amount); }); },
config: modules.config,
presenceState: this.presenceState,
});
this.presenceEventEngine = presenceEventEngine;
this.join = this.presenceEventEngine.join.bind(presenceEventEngine);
this.leave = this.presenceEventEngine.leave.bind(presenceEventEngine);
this.leaveAll = this.presenceEventEngine.leaveAll.bind(presenceEventEngine);
}
var eventEngine = new event_engine_1.EventEngine({
handshake: this.handshake,
receiveEvents: this.receiveMessages,
getRetryDelay: function (attempts) { return reconnectionDelay_1.ReconnectionDelay.getDelay(policy_1, attempts); },
delay: function (amount) { return new Promise(function (resolve) { return setTimeout(resolve, amount); }); },
shouldRetry: function (_, attempts) { return maxRetries_1 > attempts && policy_1 && policy_1 != 'None'; },
join: this.join,
leave: this.leave,
leaveAll: this.leaveAll,
presenceState: this.presenceState,
config: modules.config,
emitEvents: function (events) {
var e_1, _a;
try {
Expand Down Expand Up @@ -565,6 +595,8 @@ var default_1 = /** @class */ (function () {
};
default_1.OPERATIONS = operations_1.default;
default_1.CATEGORIES = categories_1.default;
default_1.LinearRetryPolicy = retryPolicy_1.RetryPolicy.LinearRetryPolicy;
default_1.ExponentialRetryPolicy = retryPolicy_1.RetryPolicy.ExponentialRetryPolicy;
return default_1;
}());
exports.default = default_1;
9 changes: 8 additions & 1 deletion lib/event-engine/core/reconnectionDelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ var ReconnectionDelay = /** @class */ (function () {
var backoffValue = backoff !== null && backoff !== void 0 ? backoff : 5;
switch (policy.toUpperCase()) {
case 'LINEAR':
return attempts * backoffValue + Math.random() * 1000;
return attempts * backoffValue + 200;
case 'EXPONENTIAL':
return Math.trunc(Math.pow(2, attempts - 1)) * 1000 + Math.random() * 1000;
default:
throw new Error('invalid policy');
}
};
ReconnectionDelay.shouldRetry = function (maxRetries, attempts, policy) {
// maxRetries > attempts && policy && policy != 'None';
if (policy && policy !== 'None') {
return maxRetries > attempts;
}
return false;
};
return ReconnectionDelay;
}());
exports.ReconnectionDelay = ReconnectionDelay;
48 changes: 48 additions & 0 deletions lib/event-engine/core/retryPolicy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RetryPolicy = void 0;
var RetryPolicy = /** @class */ (function () {
function RetryPolicy() {
}
RetryPolicy.LinearRetryPolicy = function (configuration) {
return {
delay: configuration.delay,
maximumRetry: configuration.maximumRetry,
shouldRetry: function (error, attempt) {
var _a;
if (((_a = error === null || error === void 0 ? void 0 : error.status) === null || _a === void 0 ? void 0 : _a.statusCode) === 403) {
return false;
}
return this.maximumRetry > attempt;
},
getDelay: function (_) {
return this.delay * 1000;
},
};
};
RetryPolicy.ExponentialRetryPolicy = function (configuration) {
return {
minimumDelay: configuration.minimumDelay,
maximumDelay: configuration.maximumDelay,
maximumRetry: configuration.maximumRetry,
shouldRetry: function (error, attempt) {
var _a;
if (((_a = error === null || error === void 0 ? void 0 : error.status) === null || _a === void 0 ? void 0 : _a.statusCode) === 403) {
return false;
}
return this.maximumRetry > attempt;
},
getDelay: function (attempt) {
var calculatedDelay = Math.trunc(Math.pow(2, attempt)) * 1000 + Math.random() * 1000;
if (calculatedDelay > 150000) {
return 150000;
}
else {
return calculatedDelay;
}
},
};
};
return RetryPolicy;
}());
exports.RetryPolicy = RetryPolicy;
Loading

0 comments on commit f7b92ad

Please sign in to comment.