1- var request = require ( 'request' ) ;
1+ let request = require ( 'request' ) ;
2+ let url = require ( 'url' ) ;
23import { Channel } from './channel' ;
34import { Log } from './../log' ;
4- var url = require ( 'url' ) ;
55
66export class PrivateChannel {
77 /**
88 * Request client.
99 *
10- * @type {object }
10+ * @type {any }
1111 */
1212 private request : any ;
1313
1414 /**
1515 * Create a new private channel instance.
16+ *
17+ * @param {any } options
1618 */
17- constructor ( private options ) {
19+ constructor ( private options : any ) {
1820 this . request = request ;
1921 }
2022
2123 /**
2224 * Send authentication request to application server.
2325 *
24- * @param {object } socket
25- * @param {object } data
26+ * @param {any } socket
27+ * @param {any } data
2628 * @return {Promise<any> }
2729 */
2830 authenticate ( socket : any , data : any ) : Promise < any > {
@@ -37,37 +39,51 @@ export class PrivateChannel {
3739 }
3840
3941 /**
40- * Get the auth endpoint .
42+ * Get the auth host based on the Socket .
4143 *
44+ * @param {any } socket
4245 * @return {string }
4346 */
4447 protected authHost ( socket : any ) : string {
45- let referer : Object = url . parse ( socket . request . headers . referer ) ;
46- let authHostSelected : string = 'http://localhost' ;
47- let authHosts : any = ( this . options . authHost ) ?
48+ let referer = url . parse ( socket . request . headers . referer ) ;
49+ let authHostSelected = 'http://localhost' ;
50+ let authHosts = ( this . options . authHost ) ?
4851 this . options . authHost : this . options . host ;
49-
50- if ( typeof authHosts === "string" )
52+
53+ if ( typeof authHosts === "string" ) {
5154 authHosts = [ authHosts ] ;
52-
53- for ( let authHost of authHosts )
54- {
55+ }
56+
57+ for ( let authHost of authHosts ) {
5558 authHostSelected = authHost ;
56- if ( referer . hostname . substr ( referer . hostname . indexOf ( '.' ) ) === authHostSelected || referer . protocol + "//" + referer . host === authHostSelected || referer . host === authHostSelected )
57- {
58- authHostSelected = referer . protocol + "//" + referer . host ;
59+
60+ if ( this . hasMatchingHost ( referer , authHost ) ) {
61+ authHostSelected = ` ${ referer . protocol } // ${ referer . host } ` ;
5962 break ;
6063 }
61- }
64+ } ;
6265
6366 return authHostSelected ;
6467 }
6568
69+ /**
70+ * Check if there is a matching auth host.
71+ *
72+ * @param {any } referer
73+ * @param {any } host
74+ * @return {boolean }
75+ */
76+ protected hasMatchingHost ( referer : any , host : any ) : boolean {
77+ return referer . hostname . substr ( referer . hostname . indexOf ( '.' ) ) === host ||
78+ `${ referer . protocol } //${ referer . host } ` === host ||
79+ referer . host === host ;
80+ }
81+
6682 /**
6783 * Send a request to the server.
6884 *
69- * @param {object } socket
70- * @param {object } options
85+ * @param {any } socket
86+ * @param {any } options
7187 * @return {Promise<any> }
7288 */
7389 protected serverRequest ( socket : any , options : any ) : Promise < any > {
@@ -111,7 +127,8 @@ export class PrivateChannel {
111127 /**
112128 * Prepare headers for request to app server.
113129 *
114- * @param {object } options
130+ * @param {any } socket
131+ * @param {any } options
115132 * @return {any }
116133 */
117134 protected prepareHeaders ( socket : any , options : any ) : any {
0 commit comments