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