@@ -34,6 +34,8 @@ const runBonjour = require('./utils/runBonjour');
3434const routes = require ( './utils/routes' ) ;
3535const getSocketServerImplementation = require ( './utils/getSocketServerImplementation' ) ;
3636const handleStdin = require ( './utils/handleStdin' ) ;
37+ const tryParseInt = require ( './utils/tryParseInt' ) ;
38+ const startUnixSocket = require ( './utils/startUnixSocket' ) ;
3739const schema = require ( './options.json' ) ;
3840
3941// Workaround for node ^8.6.0, ^9.0.0
@@ -728,23 +730,54 @@ class Server {
728730 listen ( port , hostname , fn ) {
729731 this . hostname = hostname ;
730732
731- return this . listeningApp . listen ( port , hostname , ( err ) => {
733+ const setupCallback = ( ) => {
732734 this . createSocketServer ( ) ;
733735
734736 if ( this . options . bonjour ) {
735737 runBonjour ( this . options ) ;
736738 }
737739
738740 this . showStatus ( ) ;
741+ } ;
739742
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 ;
741750 fn . call ( this . listeningApp , err ) ;
742751 }
752+ } ;
743753
754+ const onListeningCallback = ( ) => {
744755 if ( typeof this . options . onListening === 'function' ) {
745756 this . options . onListening ( this ) ;
746757 }
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 ;
748781 }
749782
750783 close ( cb ) {
0 commit comments