@@ -111,15 +111,46 @@ define(function (require, exports, module) {
111111 */
112112 StaticServer . prototype . readyToServe = function ( ) {
113113 var self = this ;
114- var port = _prefs . get ( "port" ) ;
114+ var deferred = new $ . Deferred ( ) ;
115115
116- return this . _nodeDomain . exec ( "getServer" , self . _root , port )
116+ function sanitizePort ( port ) {
117+ port = parseInt ( port , 10 ) ;
118+ port = ( port && ! isNaN ( port ) && port > 0 && port < 65536 ) ? port : 0 ;
119+ return port ;
120+ }
121+
122+ function onSuccess ( address ) {
123+ self . _baseUrl = "http://" + address . address + ":" + address . port + "/" ;
124+ deferred . resolve ( ) ;
125+ }
126+
127+ function onFailure ( ) {
128+ self . _baseUrl = "" ;
129+ deferred . resolve ( ) ;
130+ }
131+
132+ var port = sanitizePort ( _prefs . get ( "port" ) ) ;
133+
134+ this . _nodeDomain . exec ( "getServer" , self . _root , port )
117135 . done ( function ( address ) {
118- self . _baseUrl = "http://" + address . address + ":" + address . port + "/" ;
136+
137+ // If the port returned wasn't what was requested, then the preference has
138+ // changed. Close the current server, and open a new one with the new port.
139+ if ( address . port !== port && port > 0 ) {
140+ return self . _nodeDomain . exec ( "closeServer" , self . _root )
141+ . done ( function ( ) {
142+ return self . _nodeDomain . exec ( "getServer" , self . _root , port )
143+ . done ( onSuccess )
144+ . fail ( onFailure ) ;
145+ } )
146+ . fail ( onFailure ) ;
147+ }
148+
149+ onSuccess ( address ) ;
119150 } )
120- . fail ( function ( ) {
121- self . _baseUrl = "" ;
122- } ) ;
151+ . fail ( onFailure ) ;
152+
153+ return deferred . promise ( ) ;
123154 } ;
124155
125156 /**
0 commit comments