@@ -34,6 +34,8 @@ const runBonjour = require('./utils/runBonjour');
34
34
const routes = require ( './utils/routes' ) ;
35
35
const getSocketServerImplementation = require ( './utils/getSocketServerImplementation' ) ;
36
36
const handleStdin = require ( './utils/handleStdin' ) ;
37
+ const tryParseInt = require ( './utils/tryParseInt' ) ;
38
+ const startUnixSocket = require ( './utils/startUnixSocket' ) ;
37
39
const schema = require ( './options.json' ) ;
38
40
39
41
// Workaround for node ^8.6.0, ^9.0.0
@@ -726,23 +728,54 @@ class Server {
726
728
listen ( port , hostname , fn ) {
727
729
this . hostname = hostname ;
728
730
729
- return this . listeningApp . listen ( port , hostname , ( err ) => {
731
+ const setupCallback = ( ) => {
730
732
this . createSocketServer ( ) ;
731
733
732
734
if ( this . options . bonjour ) {
733
735
runBonjour ( this . options ) ;
734
736
}
735
737
736
738
this . showStatus ( ) ;
739
+ } ;
737
740
738
- if ( fn ) {
741
+ // between setupCallback and userCallback should be any other needed handling,
742
+ // specifically so that things are done in the right order to prevent
743
+ // backwards compatability issues
744
+ let userCallbackCalled = false ;
745
+ const userCallback = ( err ) => {
746
+ if ( fn && ! userCallbackCalled ) {
747
+ userCallbackCalled = true ;
739
748
fn . call ( this . listeningApp , err ) ;
740
749
}
750
+ } ;
741
751
752
+ const onListeningCallback = ( ) => {
742
753
if ( typeof this . options . onListening === 'function' ) {
743
754
this . options . onListening ( this ) ;
744
755
}
745
- } ) ;
756
+ } ;
757
+
758
+ const fullCallback = ( err ) => {
759
+ setupCallback ( ) ;
760
+ userCallback ( err ) ;
761
+ onListeningCallback ( ) ;
762
+ } ;
763
+
764
+ // try to follow the Node standard in terms of deciding
765
+ // whether this is a socket or a port that we will listen on:
766
+ // https://github.com/nodejs/node/blob/64219741218aa87e259cf8257596073b8e747f0a/lib/net.js#L196
767
+ if ( typeof port === 'string' && tryParseInt ( port ) === null ) {
768
+ // in this case the "port" argument is actually a socket path
769
+ const socket = port ;
770
+ // set this so that status helper can identify how the project is being run correctly
771
+ this . options . socket = socket ;
772
+
773
+ startUnixSocket ( this . listeningApp , socket , fullCallback ) ;
774
+ } else {
775
+ this . listeningApp . listen ( port , hostname , fullCallback ) ;
776
+ }
777
+
778
+ return this . listeningApp ;
746
779
}
747
780
748
781
close ( cb ) {
0 commit comments