Skip to content

Commit

Permalink
feat(tree-shaking): add environment flags to disable modules
Browse files Browse the repository at this point in the history
Add environment flags processing to opt-out feature modules from built bundles.
  • Loading branch information
parfeon committed May 17, 2024
1 parent 5607747 commit 6e7d05f
Show file tree
Hide file tree
Showing 24 changed files with 1,934 additions and 1,349 deletions.
1,031 changes: 568 additions & 463 deletions dist/web/pubnub.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

898 changes: 568 additions & 330 deletions lib/core/pubnub-common.js

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions lib/entities/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ class Channel {
this.name = channelName;
}
subscription(subscriptionOptions) {
return new Subscription_1.Subscription({
channels: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
});
if (process.env.SUBSCRIBE_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: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
});
}
else
throw new Error('Subscription error: subscription module disabled');
}
}
exports.Channel = Channel;
18 changes: 11 additions & 7 deletions lib/entities/ChannelGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ class ChannelGroup {
this.name = channelGroup;
}
subscription(subscriptionOptions) {
return new Subscription_1.Subscription({
channels: [],
channelGroups: (subscriptionOptions === null || subscriptionOptions === void 0 ? void 0 : subscriptionOptions.receivePresenceEvents) ? [this.name, `${this.name}-pnpres`] : [this.name],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
});
if (process.env.SUBSCRIBE_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],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
});
}
else
throw new Error('Subscription error: subscription module disabled');
}
}
exports.ChannelGroup = ChannelGroup;
18 changes: 11 additions & 7 deletions lib/entities/ChannelMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ class ChannelMetadata {
this.pubnub = pubnub;
}
subscription(subscriptionOptions) {
return new Subscription_1.Subscription({
channels: [this.id],
channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
});
if (process.env.SUBSCRIBE_MODULE !== 'disabled') {
return new Subscription_1.Subscription({
channels: [this.id],
channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
});
}
else
throw new Error('Subscription error: subscription module disabled');
}
}
exports.ChannelMetadata = ChannelMetadata;
18 changes: 11 additions & 7 deletions lib/entities/UserMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ class UserMetadata {
this.pubnub = pubnub;
}
subscription(subscriptionOptions) {
return new Subscription_1.Subscription({
channels: [this.id],
channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
});
if (process.env.SUBSCRIBE_MODULE !== 'disabled') {
return new Subscription_1.Subscription({
channels: [this.id],
channelGroups: [],
subscriptionOptions: subscriptionOptions,
eventEmitter: this.eventEmitter,
pubnub: this.pubnub,
});
}
else
throw new Error('Subscription error: subscription module disabled');
}
}
exports.UserMetadata = UserMetadata;
48 changes: 32 additions & 16 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,53 @@ const common_1 = __importDefault(require("../cbor/common"));
class PubNub extends pubnub_common_1.PubNubCore {
constructor(configuration) {
const configurationCopy = (0, configuration_2.setDefaults)(configuration);
const platformConfiguration = Object.assign(Object.assign({}, configurationCopy), { sdkFamily: 'Nodejs', PubNubFile: node_1.default });
const platformConfiguration = Object.assign(Object.assign({}, configurationCopy), { sdkFamily: 'Nodejs' });
if (process.env.FILE_SHARING_MODULE !== 'disabled')
platformConfiguration.PubNubFile = node_1.default;
// Prepare full client configuration.
const clientConfiguration = (0, configuration_1.makeConfiguration)(platformConfiguration, (cryptoConfiguration) => {
if (!cryptoConfiguration.cipherKey)
return undefined;
return new nodeCryptoModule_1.CryptoModule({
default: new nodeCryptoModule_1.LegacyCryptor(Object.assign({}, cryptoConfiguration)),
cryptors: [new nodeCryptoModule_1.AesCbcCryptor({ cipherKey: cryptoConfiguration.cipherKey })],
});
if (process.env.CRYPTO_MODULE !== 'disabled') {
return new nodeCryptoModule_1.CryptoModule({
default: new nodeCryptoModule_1.LegacyCryptor(Object.assign({}, cryptoConfiguration)),
cryptors: [new nodeCryptoModule_1.AesCbcCryptor({ cipherKey: cryptoConfiguration.cipherKey })],
});
}
else
return undefined;
});
// Prepare Token manager.
const tokenManager = new token_manager_1.TokenManager(new common_1.default((buffer) => cbor_sync_1.default.decode(buffer_1.Buffer.from(buffer)), base64_codec_1.decode));
let tokenManager;
if (process.env.CRYPTO_MODULE !== 'disabled') {
tokenManager = new token_manager_1.TokenManager(new common_1.default((buffer) => cbor_sync_1.default.decode(buffer_1.Buffer.from(buffer)), base64_codec_1.decode));
}
// Legacy crypto (legacy data encryption / decryption and request signature support).
const crypto = new cryptography_1.default({
secretKey: clientConfiguration.secretKey,
cipherKey: clientConfiguration.getCipherKey(),
useRandomIVs: clientConfiguration.getUseRandomIVs(),
customEncrypt: clientConfiguration.getCustomEncrypt(),
customDecrypt: clientConfiguration.getCustomDecrypt(),
});
let crypto;
if (process.env.CRYPTO_MODULE !== 'disabled') {
crypto = new cryptography_1.default({
secretKey: clientConfiguration.secretKey,
cipherKey: clientConfiguration.getCipherKey(),
useRandomIVs: clientConfiguration.getUseRandomIVs(),
customEncrypt: clientConfiguration.getCustomEncrypt(),
customDecrypt: clientConfiguration.getCustomDecrypt(),
});
}
let cryptography;
if (process.env.CRYPTO_MODULE !== 'disabled')
cryptography = new node_2.default();
// Setup transport provider.
const transport = new node_transport_1.NodeTransport(configuration.keepAlive, configuration.keepAliveSettings);
const transportMiddleware = new middleware_1.PubNubMiddleware({
clientConfiguration,
tokenManager,
transport,
shaHMAC: crypto === null || crypto === void 0 ? void 0 : crypto.HMACSHA256.bind(crypto),
shaHMAC: process.env.CRYPTO_MODULE !== 'disabled' ? crypto === null || crypto === void 0 ? void 0 : crypto.HMACSHA256.bind(crypto) : undefined,
});
super({
configuration: clientConfiguration,
transport: transportMiddleware,
cryptography: new node_2.default(),
cryptography,
tokenManager,
crypto,
});
Expand Down Expand Up @@ -83,5 +98,6 @@ class PubNub extends pubnub_common_1.PubNubCore {
/**
* Data encryption / decryption module constructor.
*/
PubNub.CryptoModule = nodeCryptoModule_1.CryptoModule;
// @ts-expect-error Allowed to simplify interface when module can be disabled.
PubNub.CryptoModule = process.env.CRYPTO_MODULE !== 'disabled' ? nodeCryptoModule_1.CryptoModule : undefined;
module.exports = PubNub;
27 changes: 17 additions & 10 deletions lib/react_native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,28 @@ global.Buffer = global.Buffer || buffer_1.Buffer;
class PubNub extends pubnub_common_1.PubNubCore {
constructor(configuration) {
const configurationCopy = (0, configuration_2.setDefaults)(configuration);
const platformConfiguration = Object.assign(Object.assign({}, configurationCopy), { sdkFamily: 'ReactNative', PubNubFile: react_native_1.default });
const platformConfiguration = Object.assign(Object.assign({}, configurationCopy), { sdkFamily: 'ReactNative' });
if (process.env.FILE_SHARING_MODULE !== 'disabled')
platformConfiguration.PubNubFile = react_native_1.default;
// Prepare full client configuration.
const clientConfiguration = (0, configuration_1.makeConfiguration)(platformConfiguration);
// Prepare Token manager.
const tokenManager = new token_manager_1.TokenManager(new common_1.default((arrayBuffer) => (0, stringify_buffer_keys_1.stringifyBufferKeys)(cbor_js_1.default.decode(arrayBuffer)), base64_codec_1.decode));
let tokenManager;
if (process.env.CRYPTO_MODULE !== 'disabled') {
tokenManager = new token_manager_1.TokenManager(new common_1.default((arrayBuffer) => (0, stringify_buffer_keys_1.stringifyBufferKeys)(cbor_js_1.default.decode(arrayBuffer)), base64_codec_1.decode));
}
// Legacy crypto (legacy data encryption / decryption and request signature support).
let crypto;
if (clientConfiguration.getCipherKey() || clientConfiguration.secretKey) {
crypto = new cryptography_1.default({
secretKey: clientConfiguration.secretKey,
cipherKey: clientConfiguration.getCipherKey(),
useRandomIVs: clientConfiguration.getUseRandomIVs(),
customEncrypt: clientConfiguration.getCustomEncrypt(),
customDecrypt: clientConfiguration.getCustomDecrypt(),
});
if (process.env.CRYPTO_MODULE !== 'disabled') {
if (clientConfiguration.getCipherKey() || clientConfiguration.secretKey) {
crypto = new cryptography_1.default({
secretKey: clientConfiguration.secretKey,
cipherKey: clientConfiguration.getCipherKey(),
useRandomIVs: clientConfiguration.getUseRandomIVs(),
customEncrypt: clientConfiguration.getCustomEncrypt(),
customDecrypt: clientConfiguration.getCustomDecrypt(),
});
}
}
// Setup transport layer.
const transportMiddleware = new middleware_1.PubNubMiddleware({
Expand Down
2 changes: 1 addition & 1 deletion lib/transport/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class PubNubMiddleware {
if (req.path.startsWith('/v2/auth/') || req.path.startsWith('/v3/pam/') || req.path.startsWith('/time'))
return;
const { clientConfiguration, tokenManager } = this.configuration;
const accessKey = (_a = tokenManager.getToken()) !== null && _a !== void 0 ? _a : clientConfiguration.authKey;
const accessKey = (_a = (tokenManager && tokenManager.getToken())) !== null && _a !== void 0 ? _a : clientConfiguration.authKey;
if (accessKey)
req.queryParameters['auth'] = accessKey;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/types/core/pubnub-common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ClientInstanceConfiguration<CryptographyTypes> = {
/**
* REST API endpoints access tokens manager.
*/
tokenManager: TokenManager;
tokenManager?: TokenManager;
/**
* Legacy crypto module implementation.
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/types/node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { ProxyAgentOptions } from 'proxy-agent';
import { Readable } from 'stream';
import { Buffer } from 'buffer';
import { CryptoModule } from '../crypto/modules/NodeCryptoModule/nodeCryptoModule';
import type { CryptoModule as CryptoModuleType } from '../crypto/modules/NodeCryptoModule/nodeCryptoModule';
import PubNubFile, { PubNubFileParameters } from '../file/modules/node';
import { PubNubConfiguration } from './configuration';
import { PubNubFileConstructor } from '../core/types/file';
Expand All @@ -15,7 +15,7 @@ declare class PubNub extends PubNubCore<string | ArrayBuffer | Buffer | Readable
/**
* Data encryption / decryption module constructor.
*/
static CryptoModule: typeof CryptoModule;
static CryptoModule: typeof CryptoModuleType;
/**
* PubNub File constructor.
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/types/transport/middleware.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type PubNubMiddlewareConfiguration = {
/**
* REST API endpoints access tokens manager.
*/
tokenManager: TokenManager;
tokenManager?: TokenManager;
/**
* HMAC-SHA256 hash generator from provided `data`.
*/
Expand Down
Loading

0 comments on commit 6e7d05f

Please sign in to comment.