@@ -11,18 +11,11 @@ function Token(config) {
11
11
this . config = config ;
12
12
this . expirationRedLine = 15 * 1000 ;
13
13
this . session = { } ;
14
- this . readable = false ;
15
14
16
15
process . nextTick ( function ( ) {
17
- this . bake ( function ( err , res ) {
16
+ this . bake ( function ( err ) {
18
17
if ( err ) return this . emit ( 'error' , err ) ;
19
-
20
- this . session = res ;
21
18
this . schedule ( ) ;
22
-
23
- this . readable = true ;
24
- debug ( 'emit readable!' ) ;
25
- this . emit ( 'readable' ) ;
26
19
} . bind ( this ) ) ;
27
20
} . bind ( this ) ) ;
28
21
}
@@ -47,21 +40,22 @@ Token.prototype.close = function(fn) {
47
40
clearTimeout ( this . refreshTimeout ) ;
48
41
debug ( 'closing token' ) ;
49
42
request . del ( this . config . scout + '/api/v1/token' )
50
- . set ( 'Accept' , 'application/json' )
51
- . set ( 'Authorization' , 'Bearer ' + this . session . token )
52
- . end ( function ( err , res ) {
53
- debug ( 'response from token close' ) ;
54
- fn ( err , res ) ;
55
- } ) ;
43
+ . set ( 'Accept' , 'application/json' )
44
+ . set ( 'Authorization' , 'Bearer ' + this . session . token )
45
+ . end ( function ( err , res ) {
46
+ debug ( 'response from token close' ) ;
47
+ fn ( err , res ) ;
48
+ } ) ;
56
49
} ;
57
50
58
51
Token . prototype . bake = function ( done ) {
59
52
var payload = {
60
53
seed : this . config . seed
61
54
} ;
62
55
63
- if ( this . config . timeout )
56
+ if ( this . config . timeout ) {
64
57
payload . timeout = this . config . timeout ;
58
+ }
65
59
66
60
if ( this . config . auth ) {
67
61
Object . keys ( this . config . auth ) . map ( function ( name ) {
@@ -73,36 +67,43 @@ Token.prototype.bake = function(done) {
73
67
}
74
68
debug ( 'getting token for' , this . config . seed , payload ) ;
75
69
request . post ( this . config . scout + '/api/v1/token' )
76
- . send ( payload )
77
- . set ( 'Accept' , 'application/json' )
78
- . end ( function ( err , res ) {
79
- if ( err ) return done ( err ) ;
80
-
81
- if ( ! err && res . status >= 400 ) {
82
- err = new Error ( res . body ? res . body . message : res . text ) ;
83
- err . code = res . status ;
84
- Error . captureStackTrace ( err , Token . prototype . bake ) ;
85
- return done ( err ) ;
86
- }
87
-
88
- if ( ! res . body . expires_at || ! res . body . created_at ) {
89
- return done ( new Error ( 'Malformed response. Missing expires_at or created_at' ) ) ;
90
- }
91
-
92
- if ( new Date ( res . body . expires_at ) - Date . now ( ) < ( 1 * 60 * 1000 ) ) {
93
- return done ( new Error ( 'Got an expires that is less than a minute from now.' ) ) ;
94
- }
95
-
96
- done ( null , res . body ) ;
97
- } . bind ( this ) ) ;
70
+ . send ( payload )
71
+ . set ( 'Accept' , 'application/json' )
72
+ . end ( function ( err , res ) {
73
+ if ( err ) {
74
+ if ( res && res . body ) {
75
+ err . message += ': ' + res . body . message ;
76
+ }
77
+ console . error ( 'Error getting token:' , err ) ;
78
+ return done ( err ) ;
79
+ }
80
+
81
+ if ( ! err && res . status >= 400 ) {
82
+ err = new Error ( res . body ? res . body . message : res . text ) ;
83
+ err . code = res . status ;
84
+ Error . captureStackTrace ( err , Token . prototype . bake ) ;
85
+ return done ( err ) ;
86
+ }
87
+
88
+ if ( ! res . body . expires_at || ! res . body . created_at ) {
89
+ return done ( new Error ( 'Malformed response. Missing expires_at or created_at' ) ) ;
90
+ }
91
+
92
+ if ( new Date ( res . body . expires_at ) - Date . now ( ) < ( 1 * 60 * 1000 ) ) {
93
+ return done ( new Error ( 'Got an expires that is less than a minute from now.' ) ) ;
94
+ }
95
+
96
+ this . session = res . body ;
97
+ this . emit ( 'data' , this . session ) ;
98
+
99
+ done ( null , res . body ) ;
100
+ } . bind ( this ) ) ;
98
101
} ;
99
102
100
103
Token . prototype . refresh = function ( ) {
101
- this . bake ( function ( err , res ) {
104
+ this . bake ( function ( err ) {
102
105
if ( err ) return this . emit ( 'error' , err ) ;
103
- if ( ! res ) return this . emit ( 'error' , new Error ( 'Empty response with no error' ) ) ;
104
106
105
- this . session = res ;
106
107
debug ( 'token refreshed successfully' ) ;
107
108
return this . schedule ( ) ;
108
109
} . bind ( this ) ) ;
@@ -114,5 +115,6 @@ Token.prototype.schedule = function() {
114
115
return this ;
115
116
}
116
117
var ms = ( new Date ( this . session . expires_at ) - Date . now ( ) ) - this . expirationRedLine ;
118
+ debug ( 'scheduling token refresh %dms from now' , ms ) ;
117
119
this . refreshTimeout = setTimeout ( this . refresh . bind ( this ) , ms ) ;
118
120
} ;
0 commit comments