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