Skip to content

Commit

Permalink
feat(file-share): add file upload and download timeout configuration
Browse files Browse the repository at this point in the history
Add `fileRequestTimeout` client configuration option which is specific only for requests which
upload and download files.

fix: fix `instanceId` query parameter

Fix issue with `instanceId` set to `undefined` for requests with `useInstanceId` configuration
flag set to `true`.
  • Loading branch information
parfeon committed Jan 15, 2025
1 parent 1a2ddeb commit 67e72fe
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 36 deletions.
44 changes: 29 additions & 15 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3644,6 +3644,10 @@
* Subscription request timeout.
*/
const SUBSCRIBE_REQUEST_TIMEOUT = 310;
/**
* File upload / download request timeout.
*/
const FILE_REQUEST_TIMEOUT = 300;
/**
* Default user presence timeout.
*/
Expand All @@ -3660,27 +3664,28 @@
* @internal
*/
const setDefaults$1 = (configuration) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
// Copy configuration.
const configurationCopy = Object.assign({}, configuration);
(_a = configurationCopy.logVerbosity) !== null && _a !== void 0 ? _a : (configurationCopy.logVerbosity = USE_VERBOSE_LOGGING);
(_b = configurationCopy.ssl) !== null && _b !== void 0 ? _b : (configurationCopy.ssl = USE_SSL);
(_c = configurationCopy.transactionalRequestTimeout) !== null && _c !== void 0 ? _c : (configurationCopy.transactionalRequestTimeout = TRANSACTIONAL_REQUEST_TIMEOUT);
(_d = configurationCopy.subscribeRequestTimeout) !== null && _d !== void 0 ? _d : (configurationCopy.subscribeRequestTimeout = SUBSCRIBE_REQUEST_TIMEOUT);
(_e = configurationCopy.restore) !== null && _e !== void 0 ? _e : (configurationCopy.restore = RESTORE);
(_f = configurationCopy.useInstanceId) !== null && _f !== void 0 ? _f : (configurationCopy.useInstanceId = USE_INSTANCE_ID);
(_g = configurationCopy.suppressLeaveEvents) !== null && _g !== void 0 ? _g : (configurationCopy.suppressLeaveEvents = SUPPRESS_LEAVE_EVENTS);
(_h = configurationCopy.requestMessageCountThreshold) !== null && _h !== void 0 ? _h : (configurationCopy.requestMessageCountThreshold = DEDUPE_CACHE_SIZE);
(_j = configurationCopy.autoNetworkDetection) !== null && _j !== void 0 ? _j : (configurationCopy.autoNetworkDetection = AUTO_NETWORK_DETECTION);
(_k = configurationCopy.enableEventEngine) !== null && _k !== void 0 ? _k : (configurationCopy.enableEventEngine = ENABLE_EVENT_ENGINE);
(_l = configurationCopy.maintainPresenceState) !== null && _l !== void 0 ? _l : (configurationCopy.maintainPresenceState = MAINTAIN_PRESENCE_STATE);
(_m = configurationCopy.keepAlive) !== null && _m !== void 0 ? _m : (configurationCopy.keepAlive = KEEP_ALIVE$1);
(_e = configurationCopy.fileRequestTimeout) !== null && _e !== void 0 ? _e : (configurationCopy.fileRequestTimeout = FILE_REQUEST_TIMEOUT);
(_f = configurationCopy.restore) !== null && _f !== void 0 ? _f : (configurationCopy.restore = RESTORE);
(_g = configurationCopy.useInstanceId) !== null && _g !== void 0 ? _g : (configurationCopy.useInstanceId = USE_INSTANCE_ID);
(_h = configurationCopy.suppressLeaveEvents) !== null && _h !== void 0 ? _h : (configurationCopy.suppressLeaveEvents = SUPPRESS_LEAVE_EVENTS);
(_j = configurationCopy.requestMessageCountThreshold) !== null && _j !== void 0 ? _j : (configurationCopy.requestMessageCountThreshold = DEDUPE_CACHE_SIZE);
(_k = configurationCopy.autoNetworkDetection) !== null && _k !== void 0 ? _k : (configurationCopy.autoNetworkDetection = AUTO_NETWORK_DETECTION);
(_l = configurationCopy.enableEventEngine) !== null && _l !== void 0 ? _l : (configurationCopy.enableEventEngine = ENABLE_EVENT_ENGINE);
(_m = configurationCopy.maintainPresenceState) !== null && _m !== void 0 ? _m : (configurationCopy.maintainPresenceState = MAINTAIN_PRESENCE_STATE);
(_o = configurationCopy.keepAlive) !== null && _o !== void 0 ? _o : (configurationCopy.keepAlive = KEEP_ALIVE$1);
if (configurationCopy.userId && configurationCopy.uuid)
throw new PubNubError("PubNub client configuration error: use only 'userId'");
(_o = configurationCopy.userId) !== null && _o !== void 0 ? _o : (configurationCopy.userId = configurationCopy.uuid);
(_p = configurationCopy.userId) !== null && _p !== void 0 ? _p : (configurationCopy.userId = configurationCopy.uuid);
if (!configurationCopy.userId)
throw new PubNubError("PubNub client configuration error: 'userId' not set");
else if (((_p = configurationCopy.userId) === null || _p === void 0 ? void 0 : _p.trim().length) === 0)
else if (((_q = configurationCopy.userId) === null || _q === void 0 ? void 0 : _q.trim().length) === 0)
throw new PubNubError("PubNub client configuration error: 'userId' is empty");
// Generate default origin subdomains.
if (!configurationCopy.origin)
Expand Down Expand Up @@ -3894,6 +3899,11 @@
return this._instanceId;
return undefined;
},
getInstanceId() {
if (this.useInstanceId)
return this._instanceId;
return undefined;
},
getUserId() {
return this.userId;
},
Expand Down Expand Up @@ -3957,6 +3967,9 @@
getSubscribeTimeout() {
return this.subscribeRequestTimeout;
},
getFileTimeout() {
return this.fileRequestTimeout;
},
get PubNubFile() {
return base.PubNubFile;
},
Expand Down Expand Up @@ -4270,7 +4283,7 @@
req.queryParameters = {};
// Modify request with required information.
if (clientConfiguration.useInstanceId)
req.queryParameters['instanceid'] = clientConfiguration.instanceId;
req.queryParameters['instanceid'] = clientConfiguration.getInstanceId();
if (!req.queryParameters['uuid'])
req.queryParameters['uuid'] = clientConfiguration.userId;
if (clientConfiguration.useRequestId)
Expand Down Expand Up @@ -13037,9 +13050,10 @@
}
// Complete request configuration.
const transportRequest = request.request();
if (transportRequest.formData && transportRequest.formData.length > 0) {
// Set 300 seconds file upload request delay.
transportRequest.timeout = 300;
if ((transportRequest.formData && transportRequest.formData.length > 0) ||
request.operation() === RequestOperation$1.PNDownloadFileOperation) {
// Set file upload / download request delay.
transportRequest.timeout = this._configuration.getFileTimeout();
}
else {
if (request.operation() === RequestOperation$1.PNSubscribeOperation)
Expand Down
4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions lib/core/components/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ const makeConfiguration = (base, setupCryptoModule) => {
return this._instanceId;
return undefined;
},
getInstanceId() {
if (this.useInstanceId)
return this._instanceId;
return undefined;
},
getUserId() {
return this.userId;
},
Expand Down Expand Up @@ -108,6 +113,9 @@ const makeConfiguration = (base, setupCryptoModule) => {
getSubscribeTimeout() {
return this.subscribeRequestTimeout;
},
getFileTimeout() {
return this.fileRequestTimeout;
},
get PubNubFile() {
return base.PubNubFile;
},
Expand Down
27 changes: 16 additions & 11 deletions lib/core/interfaces/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ const TRANSACTIONAL_REQUEST_TIMEOUT = 15;
* Subscription request timeout.
*/
const SUBSCRIBE_REQUEST_TIMEOUT = 310;
/**
* File upload / download request timeout.
*/
const FILE_REQUEST_TIMEOUT = 300;
/**
* Default user presence timeout.
*/
Expand All @@ -94,27 +98,28 @@ const PRESENCE_TIMEOUT_MINIMUM = 20;
* @internal
*/
const setDefaults = (configuration) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
// Copy configuration.
const configurationCopy = Object.assign({}, configuration);
(_a = configurationCopy.logVerbosity) !== null && _a !== void 0 ? _a : (configurationCopy.logVerbosity = USE_VERBOSE_LOGGING);
(_b = configurationCopy.ssl) !== null && _b !== void 0 ? _b : (configurationCopy.ssl = USE_SSL);
(_c = configurationCopy.transactionalRequestTimeout) !== null && _c !== void 0 ? _c : (configurationCopy.transactionalRequestTimeout = TRANSACTIONAL_REQUEST_TIMEOUT);
(_d = configurationCopy.subscribeRequestTimeout) !== null && _d !== void 0 ? _d : (configurationCopy.subscribeRequestTimeout = SUBSCRIBE_REQUEST_TIMEOUT);
(_e = configurationCopy.restore) !== null && _e !== void 0 ? _e : (configurationCopy.restore = RESTORE);
(_f = configurationCopy.useInstanceId) !== null && _f !== void 0 ? _f : (configurationCopy.useInstanceId = USE_INSTANCE_ID);
(_g = configurationCopy.suppressLeaveEvents) !== null && _g !== void 0 ? _g : (configurationCopy.suppressLeaveEvents = SUPPRESS_LEAVE_EVENTS);
(_h = configurationCopy.requestMessageCountThreshold) !== null && _h !== void 0 ? _h : (configurationCopy.requestMessageCountThreshold = DEDUPE_CACHE_SIZE);
(_j = configurationCopy.autoNetworkDetection) !== null && _j !== void 0 ? _j : (configurationCopy.autoNetworkDetection = AUTO_NETWORK_DETECTION);
(_k = configurationCopy.enableEventEngine) !== null && _k !== void 0 ? _k : (configurationCopy.enableEventEngine = ENABLE_EVENT_ENGINE);
(_l = configurationCopy.maintainPresenceState) !== null && _l !== void 0 ? _l : (configurationCopy.maintainPresenceState = MAINTAIN_PRESENCE_STATE);
(_m = configurationCopy.keepAlive) !== null && _m !== void 0 ? _m : (configurationCopy.keepAlive = KEEP_ALIVE);
(_e = configurationCopy.fileRequestTimeout) !== null && _e !== void 0 ? _e : (configurationCopy.fileRequestTimeout = FILE_REQUEST_TIMEOUT);
(_f = configurationCopy.restore) !== null && _f !== void 0 ? _f : (configurationCopy.restore = RESTORE);
(_g = configurationCopy.useInstanceId) !== null && _g !== void 0 ? _g : (configurationCopy.useInstanceId = USE_INSTANCE_ID);
(_h = configurationCopy.suppressLeaveEvents) !== null && _h !== void 0 ? _h : (configurationCopy.suppressLeaveEvents = SUPPRESS_LEAVE_EVENTS);
(_j = configurationCopy.requestMessageCountThreshold) !== null && _j !== void 0 ? _j : (configurationCopy.requestMessageCountThreshold = DEDUPE_CACHE_SIZE);
(_k = configurationCopy.autoNetworkDetection) !== null && _k !== void 0 ? _k : (configurationCopy.autoNetworkDetection = AUTO_NETWORK_DETECTION);
(_l = configurationCopy.enableEventEngine) !== null && _l !== void 0 ? _l : (configurationCopy.enableEventEngine = ENABLE_EVENT_ENGINE);
(_m = configurationCopy.maintainPresenceState) !== null && _m !== void 0 ? _m : (configurationCopy.maintainPresenceState = MAINTAIN_PRESENCE_STATE);
(_o = configurationCopy.keepAlive) !== null && _o !== void 0 ? _o : (configurationCopy.keepAlive = KEEP_ALIVE);
if (configurationCopy.userId && configurationCopy.uuid)
throw new pubnub_error_1.PubNubError("PubNub client configuration error: use only 'userId'");
(_o = configurationCopy.userId) !== null && _o !== void 0 ? _o : (configurationCopy.userId = configurationCopy.uuid);
(_p = configurationCopy.userId) !== null && _p !== void 0 ? _p : (configurationCopy.userId = configurationCopy.uuid);
if (!configurationCopy.userId)
throw new pubnub_error_1.PubNubError("PubNub client configuration error: 'userId' not set");
else if (((_p = configurationCopy.userId) === null || _p === void 0 ? void 0 : _p.trim().length) === 0)
else if (((_q = configurationCopy.userId) === null || _q === void 0 ? void 0 : _q.trim().length) === 0)
throw new pubnub_error_1.PubNubError("PubNub client configuration error: 'userId' is empty");
// Generate default origin subdomains.
if (!configurationCopy.origin)
Expand Down
7 changes: 4 additions & 3 deletions lib/core/pubnub-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,10 @@ class PubNubCore {
}
// Complete request configuration.
const transportRequest = request.request();
if (transportRequest.formData && transportRequest.formData.length > 0) {
// Set 300 seconds file upload request delay.
transportRequest.timeout = 300;
if ((transportRequest.formData && transportRequest.formData.length > 0) ||
request.operation() === operations_1.default.PNDownloadFileOperation) {
// Set file upload / download request delay.
transportRequest.timeout = this._configuration.getFileTimeout();
}
else {
if (request.operation() === operations_1.default.PNSubscribeOperation)
Expand Down
2 changes: 1 addition & 1 deletion lib/transport/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class PubNubMiddleware {
req.queryParameters = {};
// Modify request with required information.
if (clientConfiguration.useInstanceId)
req.queryParameters['instanceid'] = clientConfiguration.instanceId;
req.queryParameters['instanceid'] = clientConfiguration.getInstanceId();
if (!req.queryParameters['uuid'])
req.queryParameters['uuid'] = clientConfiguration.userId;
if (clientConfiguration.useRequestId)
Expand Down
9 changes: 9 additions & 0 deletions lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +2497,15 @@ declare namespace PubNub {
* @default `310` seconds
*/
subscribeRequestTimeout?: number;
/**
* File upload / download request timeout in milliseconds.
*
* Maximum duration for which PubNub client should wait for file upload / download request
* completion.
*
* @default `300` seconds
*/
fileRequestTimeout?: number;
/**
* `true` to allow catch up on the front-end applications.
*
Expand Down
7 changes: 7 additions & 0 deletions src/core/components/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export const makeConfiguration = (
if (this.useInstanceId) return this._instanceId;
return undefined;
},
getInstanceId(): string | undefined {
if (this.useInstanceId) return this._instanceId;
return undefined;
},
getUserId() {
return this.userId!;
},
Expand Down Expand Up @@ -167,6 +171,9 @@ export const makeConfiguration = (
getSubscribeTimeout(): number {
return this.subscribeRequestTimeout!;
},
getFileTimeout(): number {
return this.fileRequestTimeout!;
},
get PubNubFile(): PubNubFileConstructor<PubNubFileInterface, unknown> | undefined {
return base.PubNubFile;
},
Expand Down
31 changes: 31 additions & 0 deletions src/core/interfaces/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ const TRANSACTIONAL_REQUEST_TIMEOUT = 15;
*/
const SUBSCRIBE_REQUEST_TIMEOUT = 310;

/**
* File upload / download request timeout.
*/
const FILE_REQUEST_TIMEOUT = 300;

/**
* Default user presence timeout.
*/
Expand Down Expand Up @@ -218,6 +223,16 @@ export type UserConfiguration = {
*/
subscribeRequestTimeout?: number;

/**
* File upload / download request timeout in milliseconds.
*
* Maximum duration for which PubNub client should wait for file upload / download request
* completion.
*
* @default `300` seconds
*/
fileRequestTimeout?: number;

/**
* `true` to allow catch up on the front-end applications.
*
Expand Down Expand Up @@ -617,6 +632,14 @@ export interface PrivateClientConfiguration
*/
getSubscribeTimeout(): number;

/**
* File requests timeout.
*
* @returns Maximum duration in milliseconds for which PubNub client should wait for
* file upload / download request completion.
*/
getFileTimeout(): number;

/**
* PubNub file object constructor.
*/
Expand All @@ -629,6 +652,13 @@ export interface PrivateClientConfiguration
*/
get instanceId(): string | undefined;

/**
* Get PubNub client instance identifier.
*
* @returns Current PubNub client instance identifier.
*/
getInstanceId(): string | undefined;

/**
* Get SDK family identifier.
*
Expand Down Expand Up @@ -698,6 +728,7 @@ export const setDefaults = (configuration: UserConfiguration): ExtendedConfigura
configurationCopy.ssl ??= USE_SSL;
configurationCopy.transactionalRequestTimeout ??= TRANSACTIONAL_REQUEST_TIMEOUT;
configurationCopy.subscribeRequestTimeout ??= SUBSCRIBE_REQUEST_TIMEOUT;
configurationCopy.fileRequestTimeout ??= FILE_REQUEST_TIMEOUT;
configurationCopy.restore ??= RESTORE;
configurationCopy.useInstanceId ??= USE_INSTANCE_ID;
configurationCopy.suppressLeaveEvents ??= SUPPRESS_LEAVE_EVENTS;
Expand Down
9 changes: 6 additions & 3 deletions src/core/pubnub-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -806,9 +806,12 @@ export class PubNubCore<

// Complete request configuration.
const transportRequest = request.request();
if (transportRequest.formData && transportRequest.formData.length > 0) {
// Set 300 seconds file upload request delay.
transportRequest.timeout = 300;
if (
(transportRequest.formData && transportRequest.formData.length > 0) ||
request.operation() === RequestOperation.PNDownloadFileOperation
) {
// Set file upload / download request delay.
transportRequest.timeout = this._configuration.getFileTimeout();
} else {
if (request.operation() === RequestOperation.PNSubscribeOperation)
transportRequest.timeout = this._configuration.getSubscribeTimeout();
Expand Down
2 changes: 1 addition & 1 deletion src/transport/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class PubNubMiddleware implements Transport {
if (!req.queryParameters) req.queryParameters = {};

// Modify request with required information.
if (clientConfiguration.useInstanceId) req.queryParameters['instanceid'] = clientConfiguration.instanceId!;
if (clientConfiguration.useInstanceId) req.queryParameters['instanceid'] = clientConfiguration.getInstanceId()!;
if (!req.queryParameters['uuid']) req.queryParameters['uuid'] = clientConfiguration.userId!;
if (clientConfiguration.useRequestId) req.queryParameters['requestid'] = req.identifier;
req.queryParameters['pnsdk'] = this.generatePNSDK();
Expand Down

0 comments on commit 67e72fe

Please sign in to comment.