Skip to content

Commit

Permalink
feat(shared-worker): migrate to shared worker
Browse files Browse the repository at this point in the history
Use `SharedWorker` instead of `Service Worker` for better PubNub client instances feedback.

feat(shared-worker): add worker log verbosity flag

Add configuration option to enable debug log output from the subscription `SharedWorker`.

refactor: add declarations generation

Create types declaration files.
  • Loading branch information
parfeon committed May 15, 2024
1 parent 3fff075 commit 5c14f33
Show file tree
Hide file tree
Showing 243 changed files with 5,035 additions and 1,168 deletions.
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"parser" : "typescript",
"semi": true,
"printWidth": 120,
"singleQuote": true,
Expand Down
1,608 changes: 909 additions & 699 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.

452 changes: 305 additions & 147 deletions dist/web/pubnub.worker.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/core/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.queryStringFromObject = exports.findUniqueCommonElements = exports.removeSingleOccurance = exports.encodeNames = exports.encodeString = void 0;
exports.queryStringFromObject = exports.findUniqueCommonElements = exports.removeSingleOccurrence = exports.encodeNames = exports.encodeString = void 0;
const encodeString = (input) => {
return encodeURIComponent(input).replace(/[!~*'()]/g, (x) => `%${x.charCodeAt(0).toString(16).toUpperCase()}`);
};
Expand All @@ -10,7 +10,7 @@ const encodeNames = (names, defaultString) => {
return encodedNames.length ? encodedNames.join(',') : defaultString !== null && defaultString !== void 0 ? defaultString : '';
};
exports.encodeNames = encodeNames;
const removeSingleOccurance = (source, elementsToRemove) => {
const removeSingleOccurrence = (source, elementsToRemove) => {
const removed = Object.fromEntries(elementsToRemove.map((prop) => [prop, false]));
return source.filter((e) => {
if (elementsToRemove.includes(e) && !removed[e]) {
Expand All @@ -20,7 +20,7 @@ const removeSingleOccurance = (source, elementsToRemove) => {
return true;
});
};
exports.removeSingleOccurance = removeSingleOccurance;
exports.removeSingleOccurrence = removeSingleOccurrence;
const findUniqueCommonElements = (a, b) => {
return [...a].filter((value) => b.includes(value) && a.indexOf(value) === a.lastIndexOf(value) && b.indexOf(value) === b.lastIndexOf(value));
};
Expand Down
4 changes: 2 additions & 2 deletions lib/event-engine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ class EventEngine {
}
}
unsubscribe({ channels = [], channelGroups = [] }) {
const filteredChannels = utils.removeSingleOccurance(this.channels, [
const filteredChannels = utils.removeSingleOccurrence(this.channels, [
...channels,
...channels.map((c) => `${c}-pnpres`),
]);
const filteredGroups = utils.removeSingleOccurance(this.groups, [
const filteredGroups = utils.removeSingleOccurrence(this.groups, [
...channelGroups,
...channelGroups.map((c) => `${c}-pnpres`),
]);
Expand Down
93 changes: 46 additions & 47 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
const cbor_sync_1 = __importDefault(require("cbor-sync"));
const buffer_1 = require("buffer");
const nodeCryptoModule_1 = require("../crypto/modules/NodeCryptoModule/nodeCryptoModule");
Expand All @@ -18,50 +17,50 @@ const cryptography_1 = __importDefault(require("../core/components/cryptography"
const pubnub_error_1 = require("../errors/pubnub-error");
const pubnub_common_1 = require("../core/pubnub-common");
const common_1 = __importDefault(require("../cbor/common"));
module.exports = (_a = 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 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 })],
});
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 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 })],
});
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));
const crypto = new cryptography_1.default({
secretKey: clientConfiguration.secretKey,
cipherKey: clientConfiguration.getCipherKey(),
useRandomIVs: clientConfiguration.getUseRandomIVs(),
customEncrypt: clientConfiguration.getCustomEncrypt(),
customDecrypt: clientConfiguration.getCustomDecrypt(),
});
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),
});
super({
configuration: clientConfiguration,
transport: transportMiddleware,
cryptography: new node_2.default(),
tokenManager,
crypto,
});
this.File = node_1.default;
this.nodeTransport = transport;
}
setProxy(configuration) {
var _b;
if (configuration && ((_b = this._configuration.keepAlive) !== null && _b !== void 0 ? _b : false))
throw new pubnub_error_1.PubNubError("Can't set 'proxy' because already configured for 'keepAlive'");
this.nodeTransport.setProxy(configuration);
this.reconnect();
}
},
_a.CryptoModule = nodeCryptoModule_1.CryptoModule,
_a);
});
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));
const crypto = new cryptography_1.default({
secretKey: clientConfiguration.secretKey,
cipherKey: clientConfiguration.getCipherKey(),
useRandomIVs: clientConfiguration.getUseRandomIVs(),
customEncrypt: clientConfiguration.getCustomEncrypt(),
customDecrypt: clientConfiguration.getCustomDecrypt(),
});
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),
});
super({
configuration: clientConfiguration,
transport: transportMiddleware,
cryptography: new node_2.default(),
tokenManager,
crypto,
});
this.File = node_1.default;
this.nodeTransport = transport;
}
setProxy(configuration) {
var _a;
if (configuration && ((_a = this._configuration.keepAlive) !== null && _a !== void 0 ? _a : false))
throw new pubnub_error_1.PubNubError("Can't set 'proxy' because already configured for 'keepAlive'");
this.nodeTransport.setProxy(configuration);
this.reconnect();
}
}
PubNub.CryptoModule = nodeCryptoModule_1.CryptoModule;
module.exports = PubNub;
1 change: 1 addition & 0 deletions lib/types/cbor/common.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
4 changes: 4 additions & 0 deletions lib/types/core/components/abort_signal.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare class AbortError extends Error {
name: string;
constructor();
}
1 change: 1 addition & 0 deletions lib/types/core/components/base64_codec.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions lib/types/core/components/configuration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
3 changes: 3 additions & 0 deletions lib/types/core/components/cryptography/hmac-sha256.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export namespace mode {
let ECB: any;
}
30 changes: 30 additions & 0 deletions lib/types/core/components/cryptography/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { CryptorConfiguration } from '../../interfaces/crypto-module';
import { Payload } from '../../types/api';
type CryptoConfiguration = {
encryptKey?: boolean;
keyEncoding?: 'hex' | 'utf8' | 'base64' | 'binary';
keyLength?: 128 | 256;
mode?: 'ecb' | 'cbc';
};
export default class {
private readonly configuration;
private iv;
private allowedKeyEncodings;
private allowedKeyLengths;
private allowedModes;
private readonly defaultOptions;
constructor(configuration: CryptorConfiguration);
HMACSHA256(data: string): string;
SHA256(data: string): string;
encrypt(data: string | Payload, customCipherKey?: string, options?: CryptoConfiguration): string;
decrypt(data: string, customCipherKey?: string, options?: CryptoConfiguration): Payload | null;
private pnEncrypt;
private pnDecrypt;
private parseOptions;
private decodeKey;
private getPaddedKey;
private getMode;
private getIV;
private getRandomIV;
}
export {};
11 changes: 11 additions & 0 deletions lib/types/core/components/deduping_manager.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default class _default {
constructor({ config }: {
config: any;
});
_config: any;
hashHistory: any[];
getKey(message: any): string;
isDuplicate(message: any): boolean;
addEntry(message: any): void;
clearHistory(): void;
}
1 change: 1 addition & 0 deletions lib/types/core/components/eventEmitter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
14 changes: 14 additions & 0 deletions lib/types/core/components/listener_manager.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Subscription from '../types/api/subscription';
import { Status, StatusEvent } from '../types/api';
export type Listener = {
message?: (message: Subscription.Message) => void;
signal?: (signal: Subscription.Signal) => void;
presence?: (presence: Subscription.Presence) => void;
objects?: (object: Subscription.AppContextObject) => void;
messageAction?: (action: Subscription.MessageAction) => void;
file?: (file: Subscription.File) => void;
status?: (status: Status | StatusEvent) => void;
user?: (user: Subscription.UserAppContextObject) => void;
space?: (space: Subscription.SpaceAppContextObject) => void;
membership?: (membership: Subscription.VSPMembershipAppContextObject) => void;
};
156 changes: 156 additions & 0 deletions lib/types/core/components/push_payload.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
type APNSPayload = {
aps: {
alert?: {
title?: string;
subtitle?: string;
body?: string;
};
badge?: number | null;
sound?: string;
'content-available'?: 1;
};
pn_push: PubNubAPNS2Configuration[];
};
type APNS2Configuration = {
collapseId?: string;
expirationDate?: Date;
targets: APNS2Target[];
};
type PubNubAPNS2Configuration = {
auth_method: 'token';
targets: PubNubAPNS2Target[];
collapse_id?: string;
expiration?: string;
version: 'v2';
};
type APNS2Target = {
topic: string;
environment?: 'development' | 'production';
excludedDevices?: string[];
};
type PubNubAPNS2Target = Omit<APNS2Target, 'excludedDevices'> & {
excluded_devices?: string[];
};
type FCMPayload = {
notification?: {
title?: string;
body?: string;
icon?: string;
sound?: string;
tag?: string;
};
data?: {
notification?: FCMPayload['notification'];
};
};
declare class BaseNotificationPayload {
protected _title?: string;
protected _subtitle?: string;
protected _sound?: string;
protected _badge?: number | null;
protected _body?: string;
protected _payload: unknown;
constructor(payload: unknown, title?: string, body?: string);
get payload(): unknown;
set title(value: string | undefined);
set subtitle(value: string | undefined);
set body(value: string | undefined);
set badge(value: number | null | undefined);
set sound(value: string | undefined);
protected setDefaultPayloadStructure(): void;
toObject(): unknown;
}
export declare class APNSNotificationPayload extends BaseNotificationPayload {
private _configurations?;
private _apnsPushType;
private _isSilent;
get payload(): APNSPayload;
set configurations(value: APNS2Configuration[]);
get notification(): {
alert?: {
title?: string | undefined;
subtitle?: string | undefined;
body?: string | undefined;
} | undefined;
badge?: number | null | undefined;
sound?: string | undefined;
'content-available'?: 1 | undefined;
};
get title(): string | undefined;
set title(value: string | undefined);
get subtitle(): string | undefined;
set subtitle(value: string | undefined);
get body(): string | undefined;
set body(value: string | undefined);
get badge(): number | null | undefined;
set badge(value: number | null | undefined);
get sound(): string | undefined;
set sound(value: string | undefined);
set silent(value: boolean);
protected setDefaultPayloadStructure(): void;
toObject(): APNSPayload | null;
private objectFromAPNS2Configuration;
private objectFromAPNSTarget;
}
export declare class FCMNotificationPayload extends BaseNotificationPayload {
private _isSilent?;
private _icon?;
private _tag?;
get payload(): FCMPayload;
get notification(): {
title?: string | undefined;
body?: string | undefined;
icon?: string | undefined;
sound?: string | undefined;
tag?: string | undefined;
} | undefined;
get data(): {
notification?: {
title?: string | undefined;
body?: string | undefined;
icon?: string | undefined;
sound?: string | undefined;
tag?: string | undefined;
} | undefined;
} | undefined;
get title(): string | undefined;
set title(value: string | undefined);
get body(): string | undefined;
set body(value: string | undefined);
get sound(): string | undefined;
set sound(value: string | undefined);
get icon(): string | undefined;
set icon(value: string | undefined);
get tag(): string | undefined;
set tag(value: string | undefined);
set silent(value: boolean);
protected setDefaultPayloadStructure(): void;
toObject(): FCMPayload | null;
}
declare class NotificationsPayload {
private readonly _payload;
private _debugging?;
private readonly _title;
private _subtitle?;
private readonly _body;
private _badge?;
private _sound?;
apns: APNSNotificationPayload;
fcm: FCMNotificationPayload;
constructor(title: string, body: string);
set debugging(value: boolean);
get title(): string;
get subtitle(): string | undefined;
set subtitle(value: string | undefined);
get body(): string;
get badge(): number | undefined;
set badge(value: number | undefined);
get sound(): string | undefined;
set sound(value: string | undefined);
buildPayload(platforms: string[]): {
pn_apns?: APNSPayload | undefined;
pn_gcm?: FCMPayload | undefined;
pn_debug?: boolean | undefined;
};
}
export default NotificationsPayload;
1 change: 1 addition & 0 deletions lib/types/core/components/reconnection_manager.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions lib/types/core/components/request.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions lib/types/core/components/stringify_buffer_keys.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions lib/types/core/components/subject.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions lib/types/core/components/subscription-manager.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
1 change: 1 addition & 0 deletions lib/types/core/components/token_manager.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
4 changes: 4 additions & 0 deletions lib/types/core/components/uuid.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare const _default: {
createUUID(): any;
};
export default _default;
Loading

0 comments on commit 5c14f33

Please sign in to comment.