Skip to content

Commit

Permalink
build(tree-shaking): more fine grained control over subscribe
Browse files Browse the repository at this point in the history
Add two separate flags to exclude legacy or new event engine mechanism from the final bundle.
  • Loading branch information
parfeon committed May 19, 2024
1 parent 7aff924 commit e30f827
Show file tree
Hide file tree
Showing 16 changed files with 345 additions and 594 deletions.
493 changes: 64 additions & 429 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.

124 changes: 68 additions & 56 deletions lib/core/pubnub-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ class PubNubCore {
* @returns Pre-formatted message payload which will trigger push notification.
*/
static notificationPayload(title, body) {
return new push_payload_1.default(title, body);
if (process.env.PUBLISH_MODULE !== 'disabled') {
return new push_payload_1.default(title, body);
}
else
throw new Error('Notification Payload error: publish module disabled');
}
/**
* Generate unique identifier.
Expand Down Expand Up @@ -157,55 +161,63 @@ class PubNubCore {
this.listenerManager = new listener_manager_1.ListenerManager();
this.eventEmitter = new eventEmitter_1.default(this.listenerManager);
if (this._configuration.enableEventEngine) {
let heartbeatInterval = this._configuration.getHeartbeatInterval();
this.presenceState = {};
if (process.env.PRESENCE_MODULE !== 'disabled') {
if (heartbeatInterval) {
this.presenceEventEngine = new presence_1.PresenceEventEngine({
heartbeat: this.heartbeat.bind(this),
leave: (parameters) => this.makeUnsubscribe(parameters, () => { }),
heartbeatDelay: () => new Promise((resolve, reject) => {
heartbeatInterval = this._configuration.getHeartbeatInterval();
if (!heartbeatInterval)
reject(new pubnub_error_1.PubNubError('Heartbeat interval has been reset.'));
else
setTimeout(resolve, heartbeatInterval * 1000);
}),
retryDelay: (amount) => new Promise((resolve) => setTimeout(resolve, amount)),
emitStatus: (status) => this.listenerManager.announceStatus(status),
config: this._configuration,
presenceState: this.presenceState,
});
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
let heartbeatInterval = this._configuration.getHeartbeatInterval();
this.presenceState = {};
if (process.env.PRESENCE_MODULE !== 'disabled') {
if (heartbeatInterval) {
this.presenceEventEngine = new presence_1.PresenceEventEngine({
heartbeat: this.heartbeat.bind(this),
leave: (parameters) => this.makeUnsubscribe(parameters, () => { }),
heartbeatDelay: () => new Promise((resolve, reject) => {
heartbeatInterval = this._configuration.getHeartbeatInterval();
if (!heartbeatInterval)
reject(new pubnub_error_1.PubNubError('Heartbeat interval has been reset.'));
else
setTimeout(resolve, heartbeatInterval * 1000);
}),
retryDelay: (amount) => new Promise((resolve) => setTimeout(resolve, amount)),
emitStatus: (status) => this.listenerManager.announceStatus(status),
config: this._configuration,
presenceState: this.presenceState,
});
}
}
this.eventEngine = new event_engine_1.EventEngine({
handshake: this.subscribeHandshake.bind(this),
receiveMessages: this.subscribeReceiveMessages.bind(this),
delay: (amount) => new Promise((resolve) => setTimeout(resolve, amount)),
join: this.join.bind(this),
leave: this.leave.bind(this),
leaveAll: this.leaveAll.bind(this),
presenceState: this.presenceState,
config: this._configuration,
emitMessages: (events) => {
try {
events.forEach((event) => this.eventEmitter.emitEvent(event));
}
catch (e) {
const errorStatus = {
error: true,
category: categories_1.default.PNUnknownCategory,
errorData: e,
statusCode: 0,
};
this.listenerManager.announceStatus(errorStatus);
}
},
emitStatus: (status) => this.listenerManager.announceStatus(status),
});
}
this.eventEngine = new event_engine_1.EventEngine({
handshake: this.subscribeHandshake.bind(this),
receiveMessages: this.subscribeReceiveMessages.bind(this),
delay: (amount) => new Promise((resolve) => setTimeout(resolve, amount)),
join: this.join.bind(this),
leave: this.leave.bind(this),
leaveAll: this.leaveAll.bind(this),
presenceState: this.presenceState,
config: this._configuration,
emitMessages: (events) => {
try {
events.forEach((event) => this.eventEmitter.emitEvent(event));
}
catch (e) {
const errorStatus = {
error: true,
category: categories_1.default.PNUnknownCategory,
errorData: e,
statusCode: 0,
};
this.listenerManager.announceStatus(errorStatus);
}
},
emitStatus: (status) => this.listenerManager.announceStatus(status),
});
else
throw new Error('Event Engine error: subscription event engine module disabled');
}
else {
this.subscriptionManager = new subscription_manager_1.SubscriptionManager(this._configuration, this.listenerManager, this.eventEmitter, this.makeSubscribe.bind(this), this.heartbeat.bind(this), this.makeUnsubscribe.bind(this), this.time.bind(this));
if (process.env.SUBSCRIBE_MANAGER_MODULE !== 'disabled') {
this.subscriptionManager = new subscription_manager_1.SubscriptionManager(this._configuration, this.listenerManager, this.eventEmitter, this.makeSubscribe.bind(this), this.heartbeat.bind(this), this.makeUnsubscribe.bind(this), this.time.bind(this));
}
else
throw new Error('Subscription Manager error: subscription manager module disabled');
}
}
}
Expand Down Expand Up @@ -496,11 +508,11 @@ class PubNubCore {
* @param parameters - Subscriptions set configuration parameters.
*/
subscriptionSet(parameters) {
if (process.env.SUBSCRIBE_MODULE !== 'disabled') {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
return new SubscriptionSet_1.SubscriptionSet(Object.assign(Object.assign({}, parameters), { eventEmitter: this.eventEmitter, pubnub: this }));
}
else
throw new Error('Subscription error: subscription module disabled');
throw new Error('Subscription error: subscription event engine module disabled');
}
/**
* Schedule request execution.
Expand Down Expand Up @@ -754,7 +766,7 @@ class PubNubCore {
* @param callback - Request completion handler callback.
*/
makeSubscribe(parameters, callback) {
if (process.env.SUBSCRIBE_MODULE !== 'disabled') {
if (process.env.SUBSCRIBE_MANAGER_MODULE !== 'disabled') {
const request = new subscribe_1.SubscribeRequest(Object.assign(Object.assign({}, parameters), { keySet: this._configuration.keySet, crypto: this._configuration.getCryptoModule(), getFileUrl: this.getFileUrl.bind(this) }));
this.sendRequest(request, (status, result) => {
var _a;
Expand All @@ -776,7 +788,7 @@ class PubNubCore {
}
}
else
throw new Error('Subscription error: subscription module disabled');
throw new Error('Subscription error: subscription manager module disabled');
}
/**
* Unsubscribe from specified channels and groups real-time events.
Expand Down Expand Up @@ -857,7 +869,7 @@ class PubNubCore {
*/
subscribeHandshake(parameters) {
return __awaiter(this, void 0, void 0, function* () {
if (process.env.SUBSCRIBE_MODULE !== 'disabled') {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
const request = new handshake_1.HandshakeSubscribeRequest(Object.assign(Object.assign({}, parameters), { keySet: this._configuration.keySet, crypto: this._configuration.getCryptoModule(), getFileUrl: this.getFileUrl.bind(this) }));
const abortUnsubscribe = parameters.abortSignal.subscribe((err) => {
request.abort();
Expand All @@ -875,7 +887,7 @@ class PubNubCore {
});
}
else
throw new Error('Subscription error: subscription module disabled');
throw new Error('Subscription error: subscription event engine module disabled');
});
}
/**
Expand All @@ -885,7 +897,7 @@ class PubNubCore {
*/
subscribeReceiveMessages(parameters) {
return __awaiter(this, void 0, void 0, function* () {
if (process.env.SUBSCRIBE_MODULE !== 'disabled') {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
const request = new receiveMessages_1.ReceiveMessagesSubscribeRequest(Object.assign(Object.assign({}, parameters), { keySet: this._configuration.keySet, crypto: this._configuration.getCryptoModule(), getFileUrl: this.getFileUrl.bind(this) }));
const abortUnsubscribe = parameters.abortSignal.subscribe((err) => {
request.abort();
Expand All @@ -903,7 +915,7 @@ class PubNubCore {
});
}
else
throw new Error('Subscription error: subscription module disabled');
throw new Error('Subscription error: subscription event engine module disabled');
});
}
/**
Expand Down Expand Up @@ -1167,10 +1179,10 @@ class PubNubCore {
*/
presence(parameters) {
var _a;
if (process.env.SUBSCRIBE_MODULE !== 'disabled')
if (process.env.SUBSCRIBE_MANAGER_MODULE !== 'disabled')
(_a = this.subscriptionManager) === null || _a === void 0 ? void 0 : _a.changePresence(parameters);
else
throw new Error('Change UUID presence error: subscription module disabled');
throw new Error('Change UUID presence error: subscription manager module disabled');
}
// endregion
// region Heartbeat
Expand Down
4 changes: 2 additions & 2 deletions lib/entities/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Channel {
this.name = channelName;
}
subscription(subscriptionOptions) {
if (process.env.SUBSCRIBE_MODULE !== 'disabled') {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
return new Subscription_1.Subscription({
channels: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
channelGroups: [],
Expand All @@ -19,7 +19,7 @@ class Channel {
});
}
else
throw new Error('Subscription error: subscription module disabled');
throw new Error('Subscription error: subscription event engine module disabled');
}
}
exports.Channel = Channel;
4 changes: 2 additions & 2 deletions lib/entities/ChannelGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ChannelGroup {
this.name = channelGroup;
}
subscription(subscriptionOptions) {
if (process.env.SUBSCRIBE_MODULE !== 'disabled') {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
return new Subscription_1.Subscription({
channels: [],
channelGroups: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
Expand All @@ -19,7 +19,7 @@ class ChannelGroup {
});
}
else
throw new Error('Subscription error: subscription module disabled');
throw new Error('Subscription error: subscription event engine module disabled');
}
}
exports.ChannelGroup = ChannelGroup;
2 changes: 1 addition & 1 deletion lib/entities/ChannelMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ChannelMetadata {
this.pubnub = pubnub;
}
subscription(subscriptionOptions) {
if (process.env.SUBSCRIBE_MODULE !== 'disabled') {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
return new Subscription_1.Subscription({
channels: [this.id],
channelGroups: [],
Expand Down
4 changes: 2 additions & 2 deletions lib/entities/UserMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UserMetadata {
this.pubnub = pubnub;
}
subscription(subscriptionOptions) {
if (process.env.SUBSCRIBE_MODULE !== 'disabled') {
if (process.env.SUBSCRIBE_EVENT_ENGINE_MODULE !== 'disabled') {
return new Subscription_1.Subscription({
channels: [this.id],
channelGroups: [],
Expand All @@ -19,7 +19,7 @@ class UserMetadata {
});
}
else
throw new Error('Subscription error: subscription module disabled');
throw new Error('Subscription error: subscription event engine module disabled');
}
}
exports.UserMetadata = UserMetadata;
14 changes: 10 additions & 4 deletions lib/transport/middleware.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PubNubMiddleware = exports.RequestSignature = void 0;
exports.PubNubMiddleware = void 0;
const transport_request_1 = require("../core/types/transport-request");
const utils_1 = require("../core/utils");
/**
* Request signature generator.
*
* @internal
*/
class RequestSignature {
constructor(publishKey, secretKey, hasher) {
this.publishKey = publishKey;
Expand Down Expand Up @@ -56,14 +61,15 @@ class RequestSignature {
.join('&');
}
}
exports.RequestSignature = RequestSignature;
RequestSignature.textDecoder = new TextDecoder('utf-8');
class PubNubMiddleware {
constructor(configuration) {
this.configuration = configuration;
const { clientConfiguration: { keySet }, shaHMAC, } = configuration;
if (keySet.secretKey && shaHMAC)
this.signatureGenerator = new RequestSignature(keySet.publishKey, keySet.secretKey, shaHMAC);
if (process.env.CRYPTO_MODULE !== 'disabled') {
if (keySet.secretKey && shaHMAC)
this.signatureGenerator = new RequestSignature(keySet.publishKey, keySet.secretKey, shaHMAC);
}
}
makeSendable(req) {
return this.configuration.transport.makeSendable(this.request(req));
Expand Down
21 changes: 0 additions & 21 deletions lib/types/transport/middleware.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,6 @@ type PubNubMiddlewareConfiguration = {
*/
transport: Transport;
};
export declare class RequestSignature {
private publishKey;
private secretKey;
private hasher;
private static textDecoder;
constructor(publishKey: string, secretKey: string, hasher: (input: string, secret: string) => string);
/**
* Compute request signature.
*
* @param req - Request which will be used to compute signature.
* @returns {string} `v2` request signature.
*/
signature(req: TransportRequest): string;
/**
* Prepare request query parameters for signature.
*
* @param query - Key / value pair of the request query parameters.
* @private
*/
private queryParameters;
}
export declare class PubNubMiddleware implements Transport {
private configuration;
/**
Expand Down
Loading

0 comments on commit e30f827

Please sign in to comment.