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