Skip to content

Commit

Permalink
Merge pull request #783 from kamalbennani/fix/persist-initial-options
Browse files Browse the repository at this point in the history
Persist initial Pusher options
  • Loading branch information
MeenaAlfons authored Dec 1, 2023
2 parents de37543 + f59dac4 commit 6fe1ce6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 37 deletions.
17 changes: 17 additions & 0 deletions spec/javascripts/unit/core/pusher_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 7 additions & 11 deletions src/core/auth/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,24 @@ export type AuthTransportCallback =
| ChannelAuthorizationCallback
| UserAuthenticationCallback;

export interface AuthOptionsT<AuthHandler> {
export interface InternalAuthOptions {
transport: 'ajax' | 'jsonp';
endpoint: string;
params?: any;
headers?: any;
paramsProvider?: () => any;
headersProvider?: () => any;
customHandler?: AuthHandler;
}

export type CustomAuthOptions<AuthHandler> = {
customHandler: AuthHandler;
}

export type AuthOptionsT<AuthHandler> = InternalAuthOptions | CustomAuthOptions<AuthHandler>

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;
}
37 changes: 22 additions & 15 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -131,15 +133,17 @@ function getEnableStatsConfig(opts: Options): boolean {
return false;
}

const hasCustomHandler = <T>(auth: AuthOptionsT<T>): auth is CustomAuthOptions<T> => {
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'];
}

Expand All @@ -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;
}
Expand All @@ -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'];
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/pusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 5 additions & 10 deletions types/src/core/auth/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,17 @@ export interface UserAuthenticationHandler {
(params: UserAuthenticationRequestParams, callback: UserAuthenticationCallback): void;
}
export type AuthTransportCallback = ChannelAuthorizationCallback | UserAuthenticationCallback;
export interface AuthOptionsT<AuthHandler> {
export interface InternalAuthOptions {
transport: 'ajax' | 'jsonp';
endpoint: string;
params?: any;
headers?: any;
paramsProvider?: () => any;
headersProvider?: () => any;
customHandler?: AuthHandler;
}
export type CustomAuthOptions<AuthHandler> = {
customHandler: AuthHandler;
};
export type AuthOptionsT<AuthHandler> = InternalAuthOptions | CustomAuthOptions<AuthHandler>;
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;
}

0 comments on commit 6fe1ce6

Please sign in to comment.