1
- var request = require ( 'request' ) ;
1
+ let request = require ( 'request' ) ;
2
+ let url = require ( 'url' ) ;
2
3
import { Channel } from './channel' ;
3
4
import { Log } from './../log' ;
4
- var url = require ( 'url' ) ;
5
5
6
6
export class PrivateChannel {
7
7
/**
8
8
* Request client.
9
9
*
10
- * @type {object }
10
+ * @type {any }
11
11
*/
12
12
private request : any ;
13
13
14
14
/**
15
15
* Create a new private channel instance.
16
+ *
17
+ * @param {any } options
16
18
*/
17
- constructor ( private options ) {
19
+ constructor ( private options : any ) {
18
20
this . request = request ;
19
21
}
20
22
21
23
/**
22
24
* Send authentication request to application server.
23
25
*
24
- * @param {object } socket
25
- * @param {object } data
26
+ * @param {any } socket
27
+ * @param {any } data
26
28
* @return {Promise<any> }
27
29
*/
28
30
authenticate ( socket : any , data : any ) : Promise < any > {
@@ -37,37 +39,51 @@ export class PrivateChannel {
37
39
}
38
40
39
41
/**
40
- * Get the auth endpoint .
42
+ * Get the auth host based on the Socket .
41
43
*
44
+ * @param {any } socket
42
45
* @return {string }
43
46
*/
44
47
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 ) ?
48
51
this . options . authHost : this . options . host ;
49
-
50
- if ( typeof authHosts === "string" )
52
+
53
+ if ( typeof authHosts === "string" ) {
51
54
authHosts = [ authHosts ] ;
52
-
53
- for ( let authHost of authHosts )
54
- {
55
+ }
56
+
57
+ for ( let authHost of authHosts ) {
55
58
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 } ` ;
59
62
break ;
60
63
}
61
- }
64
+ } ;
62
65
63
66
return authHostSelected ;
64
67
}
65
68
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
+
66
82
/**
67
83
* Send a request to the server.
68
84
*
69
- * @param {object } socket
70
- * @param {object } options
85
+ * @param {any } socket
86
+ * @param {any } options
71
87
* @return {Promise<any> }
72
88
*/
73
89
protected serverRequest ( socket : any , options : any ) : Promise < any > {
@@ -111,7 +127,8 @@ export class PrivateChannel {
111
127
/**
112
128
* Prepare headers for request to app server.
113
129
*
114
- * @param {object } options
130
+ * @param {any } socket
131
+ * @param {any } options
115
132
* @return {any }
116
133
*/
117
134
protected prepareHeaders ( socket : any , options : any ) : any {
0 commit comments