Skip to content

Commit

Permalink
Merge pull request #420 from pusher/bundle-ts-types
Browse files Browse the repository at this point in the history
Bundle ts types
  • Loading branch information
James Lees authored Feb 11, 2020
2 parents fcfb007 + 308314e commit 3e49245
Show file tree
Hide file tree
Showing 127 changed files with 1,302 additions and 15 deletions.
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export {
AuthInfo,
Authorizer,
AuthOptions,
AuthorizerGenerator,
AuthorizerCallback,
} from './types/src/core/auth/options';
export { Options } from './types/src/core/options'

export {default as Channel} from './types/src/core/channels/channel';
export {default as PresenceChannel} from './types/src/core/channels/presence_channel';
export {default as Members} from './types/src/core/channels/members';
export {default as Runtime} from './types/src/runtimes/interface'
export {default as ConnectionManager} from './types/src/core/connection/connection_manager'

export {default} from './types/src/core/pusher'
2 changes: 1 addition & 1 deletion spec/javascripts/unit/core/channels/channel_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("Channel", function() {
it("should call back with false, {} immediately", function() {
var callback = jasmine.createSpy("callback");
channel.authorize("1.1", callback);
expect(callback).toHaveBeenCalledWith(false, {});
expect(callback).toHaveBeenCalledWith(false, {auth: ''});
});
});

Expand Down
11 changes: 10 additions & 1 deletion src/core/auth/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ export interface AuthOptions {
headers: any;
}

export interface AuthData {
auth: string;
channel_data?: string;
}

export type AuthInfo = AuthData | string;

export type AuthorizerCallback = (error: boolean, authInfo: AuthInfo) => void;

export interface Authorizer {
authorize(socketId: string, callback: Function);
authorize(socketId: string, callback: AuthorizerCallback): void;
}

export interface AuthorizerGenerator {
Expand Down
11 changes: 8 additions & 3 deletions src/core/auth/pusher_authorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import Channel from '../channels/channel';
import Factory from '../utils/factory';
import Runtime from 'runtime';
import { AuthTransports } from './auth_transports';
import { AuthOptions, AuthorizerOptions, Authorizer } from './options';
import {
AuthOptions,
AuthorizerOptions,
Authorizer,
AuthorizerCallback
} from './options';

export default class PusherAuthorizer implements Authorizer {
static authorizers: AuthTransports;
Expand Down Expand Up @@ -45,10 +50,10 @@ export default class PusherAuthorizer implements Authorizer {
return query;
}

authorize(socketId: string, callback: Function): any {
authorize(socketId: string, callback: AuthorizerCallback): void {
PusherAuthorizer.authorizers =
PusherAuthorizer.authorizers || Runtime.getAuthorizers();
return PusherAuthorizer.authorizers[this.type].call(
PusherAuthorizer.authorizers[this.type].call(
this,
Runtime,
socketId,
Expand Down
6 changes: 4 additions & 2 deletions src/core/channels/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Pusher from '../pusher';
import { PusherEvent } from '../connection/protocol/message-types';
import Metadata from './metadata';
import UrlStore from '../utils/url_store';
import { AuthData, AuthorizerCallback } from '../auth/options';

/** Provides base public channel interface with an event emitter.
*
Expand Down Expand Up @@ -38,8 +39,8 @@ export default class Channel extends EventsDispatcher {
*
* @param {Function} callback
*/
authorize(socketId: string, callback: Function) {
return callback(false, {});
authorize(socketId: string, callback: AuthorizerCallback) {
return callback(false, { auth: '' });
}

/** Triggers an event */
Expand Down Expand Up @@ -104,6 +105,7 @@ export default class Channel extends EventsDispatcher {
Logger.error(data);
this.emit('pusher:subscription_error', data);
} else {
data = data as AuthData;
this.pusher.send_event('pusher:subscribe', {
auth: data.auth,
channel_data: data.channel_data,
Expand Down
3 changes: 2 additions & 1 deletion src/core/channels/encrypted_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { secretbox } from 'tweetnacl';
import { encodeUTF8, decodeBase64 } from 'tweetnacl-util';
import Dispatcher from '../events/dispatcher';
import { PusherEvent } from '../connection/protocol/message-types';
import { AuthorizerCallback } from '../auth/options';

/** Extends private channels to provide encrypted channel interface.
*
Expand All @@ -19,7 +20,7 @@ export default class EncryptedChannel extends PrivateChannel {
* @param {String} socketId
* @param {Function} callback
*/
authorize(socketId: string, callback: Function) {
authorize(socketId: string, callback: AuthorizerCallback) {
super.authorize(socketId, (error, authData) => {
if (error) {
callback(true, authData);
Expand Down
2 changes: 2 additions & 0 deletions src/core/channels/presence_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Pusher from '../pusher';
import UrlStore from 'core/utils/url_store';
import { PusherEvent } from '../connection/protocol/message-types';
import Metadata from './metadata';
import { AuthData } from '../auth/options';

export default class PresenceChannel extends PrivateChannel {
members: Members;
Expand All @@ -27,6 +28,7 @@ export default class PresenceChannel extends PrivateChannel {
authorize(socketId: string, callback: Function) {
super.authorize(socketId, (error, authData) => {
if (!error) {
authData = authData as AuthData;
if (authData.channel_data === undefined) {
let suffix = UrlStore.buildLogSuffix('authenticationEndpoint');
Logger.error(
Expand Down
3 changes: 2 additions & 1 deletion src/core/channels/private_channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Factory from '../utils/factory';
import Channel from './channel';
import { AuthorizerCallback } from '../auth/options';

/** Extends public channels to provide private channel interface.
*
Expand All @@ -12,7 +13,7 @@ export default class PrivateChannel extends Channel {
* @param {String} socketId
* @param {Function} callback
*/
authorize(socketId: string, callback: Function) {
authorize(socketId: string, callback: AuthorizerCallback) {
var authorizer = Factory.createAuthorizer(this, this.pusher.config);
return authorizer.authorize(socketId, callback);
}
Expand Down
32 changes: 30 additions & 2 deletions src/core/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ConnectionManager from './connection/connection_manager';
import { AuthOptions, AuthorizerGenerator } from './auth/options';

interface PusherOptions {
export interface PusherOptions {
cluster: string;
disableStats: boolean;
enableStats: boolean;
Expand All @@ -17,4 +17,32 @@ interface PusherOptions {
authorizer: AuthorizerGenerator;
}

export default PusherOptions;
type Transport = 'ws' | 'wss' | 'xhr_streaming' | 'xhr_polling' | 'sockjs';
type AuthTransport = 'ajax' | 'jsonp';

export interface Options {
activityTimeout?: number;
enableStats?: boolean;
disableStats?: boolean; // deprecated
authEndpoint?: string;
auth?: AuthOptions;
authTransport?: AuthTransport;
authorizer?: AuthorizerGenerator;
disabledTransports?: Transport[];
enabledTransports?: Transport[];
encrypted?: boolean;
forceTLS?: boolean;
ignoreNullOrigin?: boolean;
pongTimeout?: number;
statsHost?: string;
timelineParams?: any;
unavailable_timeout?: number;
cluster?: string;
wsHost?: string;
httpHost?: string;
wsPath?: string;
wsPort?: number;
wssPort?: number;
httpPort?: number;
httpsPort?: number;
}
4 changes: 2 additions & 2 deletions src/core/pusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Defaults from './defaults';
import * as DefaultConfig from './config';
import Logger from './logger';
import Factory from './utils/factory';
import PusherOptions from './options';
import { PusherOptions, Options } from './options';
import UrlStore from 'core/utils/url_store';

export default class Pusher {
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class Pusher {
connection: ConnectionManager;
timelineSenderTimer: PeriodicTimer;

constructor(app_key: string, options: any) {
constructor(app_key: string, options?: Options) {
checkAppKey(app_key);
options = options || {};
if (!options.cluster && !(options.wsHost || options.httpHost)) {
Expand Down
7 changes: 6 additions & 1 deletion src/runtimes/web/auth/jsonp_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import Logger from 'core/logger';
import JSONPRequest from '../dom/jsonp_request';
import { ScriptReceivers } from '../dom/script_receiver_factory';
import { AuthTransport } from 'core/auth/auth_transports';
import { AuthorizerCallback } from 'core/auth/options';

var jsonp: AuthTransport = function(context: Browser, socketId, callback) {
var jsonp: AuthTransport = function(
context: Browser,
socketId: string,
callback: AuthorizerCallback
) {
if (this.authOptions.headers !== undefined) {
Logger.warn(
'To send headers with the auth request, you must use AJAX, rather than JSONP.'
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"compilerOptions": {
"sourceMap": true,
"module": "es6",
"declaration": false,
"declaration": true,
"declarationDir": "types",
"target": "es3",
"removeComments": true,
"moduleResolution": "node",
Expand Down
15 changes: 15 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Pusher from './src/core/pusher'
import { Authorizer, AuthOptions, AuthorizerGenerator } from './src/core/auth/options';
import { Options } from './src/core/options'
import Channel from './src/core/channels/channel';
import Runtime from './src/runtimes/interface'

export {
Options,
AuthOptions,
AuthorizerGenerator,
Authorizer,
Channel,
Runtime,
}
export default Pusher
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare var Dependencies: {};
4 changes: 4 additions & 0 deletions types/spec/javascripts/helpers/pusher_integration_class.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Pusher from '../../../src/core/pusher';
export default class PusherIntegration extends Pusher {
static Integration: any;
}
8 changes: 8 additions & 0 deletions types/src/core/auth/auth_transports.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import AbstractRuntime from 'runtimes/interface';
interface AuthTransport {
(context: AbstractRuntime, socketId: string, callback: Function): void;
}
interface AuthTransports {
[index: string]: AuthTransport;
}
export { AuthTransport, AuthTransports };
22 changes: 22 additions & 0 deletions types/src/core/auth/options.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Channel from '../channels/channel';
export interface AuthOptions {
params: any;
headers: any;
}
export interface AuthData {
auth: string;
channel_data?: string;
}
export declare type AuthInfo = AuthData | string;
export declare type AuthorizerCallback = (error: boolean, authInfo: AuthInfo) => void;
export interface Authorizer {
authorize(socketId: string, callback: AuthorizerCallback): void;
}
export interface AuthorizerGenerator {
(channel: Channel, options: AuthorizerOptions): Authorizer;
}
export interface AuthorizerOptions {
authTransport: 'ajax' | 'jsonp';
auth: AuthOptions;
authorizer: AuthorizerGenerator;
}
13 changes: 13 additions & 0 deletions types/src/core/auth/pusher_authorizer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Channel from '../channels/channel';
import { AuthTransports } from './auth_transports';
import { AuthOptions, AuthorizerOptions, Authorizer, AuthorizerCallback } from './options';
export default class PusherAuthorizer implements Authorizer {
static authorizers: AuthTransports;
channel: Channel;
type: string;
options: AuthorizerOptions;
authOptions: AuthOptions;
constructor(channel: Channel, options: AuthorizerOptions);
composeQuery(socketId: string): string;
authorize(socketId: string, callback: AuthorizerCallback): void;
}
1 change: 1 addition & 0 deletions types/src/core/base64.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function encode(s: any): string;
21 changes: 21 additions & 0 deletions types/src/core/channels/channel.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { default as EventsDispatcher } from '../events/dispatcher';
import Pusher from '../pusher';
import { PusherEvent } from '../connection/protocol/message-types';
import { AuthorizerCallback } from '../auth/options';
export default class Channel extends EventsDispatcher {
name: string;
pusher: Pusher;
subscribed: boolean;
subscriptionPending: boolean;
subscriptionCancelled: boolean;
constructor(name: string, pusher: Pusher);
authorize(socketId: string, callback: AuthorizerCallback): void;
trigger(event: string, data: any): boolean;
disconnect(): void;
handleEvent(event: PusherEvent): void;
handleSubscriptionSucceededEvent(event: PusherEvent): void;
subscribe(): void;
unsubscribe(): void;
cancelSubscription(): void;
reinstateSubscription(): void;
}
5 changes: 5 additions & 0 deletions types/src/core/channels/channel_table.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Channel from './channel';
interface ChannelTable {
[index: string]: Channel;
}
export default ChannelTable;
12 changes: 12 additions & 0 deletions types/src/core/channels/channels.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Channel from './channel';
import ChannelTable from './channel_table';
import Pusher from '../pusher';
export default class Channels {
channels: ChannelTable;
constructor();
add(name: string, pusher: Pusher): Channel;
all(): Channel[];
find(name: string): Channel;
remove(name: string): Channel;
disconnect(): void;
}
12 changes: 12 additions & 0 deletions types/src/core/channels/encrypted_channel.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import PrivateChannel from './private_channel';
import Dispatcher from '../events/dispatcher';
import { PusherEvent } from '../connection/protocol/message-types';
import { AuthorizerCallback } from '../auth/options';
export default class EncryptedChannel extends PrivateChannel {
key: Uint8Array;
authorize(socketId: string, callback: AuthorizerCallback): void;
trigger(event: string, data: any): boolean;
handleEvent(event: PusherEvent): void;
private handleEncryptedEvent;
emitJSON(eventName: string, data?: any): Dispatcher;
}
14 changes: 14 additions & 0 deletions types/src/core/channels/members.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default class Members {
members: any;
count: number;
myID: any;
me: any;
constructor();
get(id: string): any;
each(callback: Function): void;
setMyID(id: string): void;
onSubscription(subscriptionData: any): void;
addMember(memberData: any): any;
removeMember(memberData: any): any;
reset(): void;
}
4 changes: 4 additions & 0 deletions types/src/core/channels/metadata.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface Metadata {
user_id?: string;
}
export default Metadata;
13 changes: 13 additions & 0 deletions types/src/core/channels/presence_channel.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import PrivateChannel from './private_channel';
import Members from './members';
import Pusher from '../pusher';
import { PusherEvent } from '../connection/protocol/message-types';
export default class PresenceChannel extends PrivateChannel {
members: Members;
constructor(name: string, pusher: Pusher);
authorize(socketId: string, callback: Function): void;
handleEvent(event: PusherEvent): void;
handleInternalEvent(event: PusherEvent): void;
handleSubscriptionSucceededEvent(event: PusherEvent): void;
disconnect(): void;
}
5 changes: 5 additions & 0 deletions types/src/core/channels/private_channel.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Channel from './channel';
import { AuthorizerCallback } from '../auth/options';
export default class PrivateChannel extends Channel {
authorize(socketId: string, callback: AuthorizerCallback): void;
}
Loading

0 comments on commit 3e49245

Please sign in to comment.