Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist initial Pusher options #783

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}