From 3fc566ea7d0c65acbcef6781b52079e519248d62 Mon Sep 17 00:00:00 2001 From: Kamal Bennani Date: Wed, 29 Nov 2023 23:00:12 +0100 Subject: [PATCH 1/2] fix: persist previous options --- spec/javascripts/unit/core/pusher_spec.js | 17 +++++++++++++++++ src/core/pusher.ts | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/spec/javascripts/unit/core/pusher_spec.js b/spec/javascripts/unit/core/pusher_spec.js index 28cfd73f..6993243a 100644 --- a/spec/javascripts/unit/core/pusher_spec.js +++ b/spec/javascripts/unit/core/pusher_spec.js @@ -346,6 +346,23 @@ describe("Pusher", function() { "channel" ); }); + + it('should keep the persist the previous options', () => { + var authorizeSpy = jasmine.createSpy("authorizeSpy"); + const options = { + cluster: "mt1", + enableStats: true, + channelAuthorization: { + customHandler: authorizeSpy + } + }; + + var pusher = new Pusher("foo", options); + pusher.connect(); + pusher.switchCluster({ appKey: 'bar', cluster: 'us3' }); + + expect(pusher.options).toEqual({ ...options, cluster: 'us3' }); + }) }) describe("#unsubscribe", function() { diff --git a/src/core/pusher.ts b/src/core/pusher.ts index b1ffc8ed..19d48ef9 100644 --- a/src/core/pusher.ts +++ b/src/core/pusher.ts @@ -63,7 +63,8 @@ export default class Pusher { checkAppKey(app_key); validateOptions(options); this.key = app_key; - this.config = getConfig(options, this); + this.options = options; + this.config = getConfig(this.options, this); this.channels = Factory.createChannels(); this.global_emitter = new EventsDispatcher(); From f59dac48c29c7db0a1f855aaa9bed9cecd40c553 Mon Sep 17 00:00:00 2001 From: Kamal Bennani Date: Wed, 29 Nov 2023 23:05:41 +0100 Subject: [PATCH 2/2] fix(typing): fix misleading authorizer typing --- src/core/auth/options.ts | 18 ++++++---------- src/core/config.ts | 37 +++++++++++++++++++------------- types/src/core/auth/options.d.ts | 15 +++++-------- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/src/core/auth/options.ts b/src/core/auth/options.ts index 26bf62c8..da0eaf29 100644 --- a/src/core/auth/options.ts +++ b/src/core/auth/options.ts @@ -51,28 +51,24 @@ export type AuthTransportCallback = | ChannelAuthorizationCallback | UserAuthenticationCallback; -export interface AuthOptionsT { +export interface InternalAuthOptions { transport: 'ajax' | 'jsonp'; endpoint: string; params?: any; headers?: any; paramsProvider?: () => any; headersProvider?: () => any; - customHandler?: AuthHandler; } +export type CustomAuthOptions = { + customHandler: AuthHandler; +} + +export type AuthOptionsT = InternalAuthOptions | CustomAuthOptions + export declare type UserAuthenticationOptions = AuthOptionsT< UserAuthenticationHandler >; export declare type ChannelAuthorizationOptions = AuthOptionsT< ChannelAuthorizationHandler >; - -export interface InternalAuthOptions { - transport: 'ajax' | 'jsonp'; - endpoint: string; - params?: any; - headers?: any; - paramsProvider?: () => any; - headersProvider?: () => any; -} diff --git a/src/core/config.ts b/src/core/config.ts index e5860f3c..acfa5df8 100644 --- a/src/core/config.ts +++ b/src/core/config.ts @@ -3,7 +3,9 @@ import Defaults from './defaults'; import { ChannelAuthorizationHandler, UserAuthenticationHandler, - ChannelAuthorizationOptions + ChannelAuthorizationOptions, + AuthOptionsT, + CustomAuthOptions } from './auth/options'; import UserAuthenticator from './auth/user_authenticator'; import ChannelAuthorizer from './auth/channel_authorizer'; @@ -131,15 +133,17 @@ function getEnableStatsConfig(opts: Options): boolean { return false; } +const hasCustomHandler = (auth: AuthOptionsT): auth is CustomAuthOptions => { + return 'customHandler' in auth && auth['customHandler'] != null; +} + function buildUserAuthenticator(opts: Options): UserAuthenticationHandler { const userAuthentication = { ...Defaults.userAuthentication, ...opts.userAuthentication }; - if ( - 'customHandler' in userAuthentication && - userAuthentication['customHandler'] != null - ) { + + if (hasCustomHandler(userAuthentication)) { return userAuthentication['customHandler']; } @@ -158,17 +162,22 @@ function buildChannelAuth(opts: Options, pusher): ChannelAuthorizationOptions { transport: opts.authTransport || Defaults.authTransport, endpoint: opts.authEndpoint || Defaults.authEndpoint }; + if ('auth' in opts) { if ('params' in opts.auth) channelAuthorization.params = opts.auth.params; if ('headers' in opts.auth) channelAuthorization.headers = opts.auth.headers; } - if ('authorizer' in opts) - channelAuthorization.customHandler = ChannelAuthorizerProxy( - pusher, - channelAuthorization, - opts.authorizer - ); + + if ('authorizer' in opts) { + return { + customHandler: ChannelAuthorizerProxy( + pusher, + channelAuthorization, + opts.authorizer + ) + } + } } return channelAuthorization; } @@ -178,10 +187,8 @@ function buildChannelAuthorizer( pusher ): ChannelAuthorizationHandler { const channelAuthorization = buildChannelAuth(opts, pusher); - if ( - 'customHandler' in channelAuthorization && - channelAuthorization['customHandler'] != null - ) { + + if (hasCustomHandler(channelAuthorization)) { return channelAuthorization['customHandler']; } diff --git a/types/src/core/auth/options.d.ts b/types/src/core/auth/options.d.ts index 5ff6deef..a82e6bf8 100644 --- a/types/src/core/auth/options.d.ts +++ b/types/src/core/auth/options.d.ts @@ -27,22 +27,17 @@ export interface UserAuthenticationHandler { (params: UserAuthenticationRequestParams, callback: UserAuthenticationCallback): void; } export type AuthTransportCallback = ChannelAuthorizationCallback | UserAuthenticationCallback; -export interface AuthOptionsT { +export interface InternalAuthOptions { transport: 'ajax' | 'jsonp'; endpoint: string; params?: any; headers?: any; paramsProvider?: () => any; headersProvider?: () => any; - customHandler?: AuthHandler; } +export type CustomAuthOptions = { + customHandler: AuthHandler; +}; +export type AuthOptionsT = InternalAuthOptions | CustomAuthOptions; export declare type UserAuthenticationOptions = AuthOptionsT; export declare type ChannelAuthorizationOptions = AuthOptionsT; -export interface InternalAuthOptions { - transport: 'ajax' | 'jsonp'; - endpoint: string; - params?: any; - headers?: any; - paramsProvider?: () => any; - headersProvider?: () => any; -}