
    +j                        d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ er	  e ej        dd	                    Z e ej        d
d                    Z ej        d ej        dd                    Z	 	 	 	 	 	 	 d2d3dZ ej        d          duZ ee          j                            dd d!          Z	 	 	 	 d4d5d&Zd6d*Zd7d8d.Z d9d1Z!dS ):    )annotationsN)TimeoutError)closing)HTTPConnection)Path)TYPE_CHECKINGGRADIO_SERVER_PORT7860GRADIO_NODE_NUM_PORTS100GRADIO_NODE_SERVER_NAMEGRADIO_SERVER_NAME	127.0.0.1Fserver_name
str | Noneserver_port
int | None	node_pathpython_portpython_hoststatic_worker_portslist[int] | Nonedebugboolreturn=tuple[str | None, subprocess.Popen[bytes] | None, int | None]c           	     <   | pt           } |                     d          r |                     d          r| dd         }n| }||gn!t          t          t          t
          z             }t          |pt          j        d          ||||pd|pg |          \  }	}
| |	|
fS )	a  Launches the Node SSR server as a front proxy.

    Parameters:
        server_name: to make app accessible on local network, set this to "0.0.0.0". Can be set by environment variable GRADIO_SERVER_NAME.
        server_port: will start gradio app on this port (if available). Can be set by environment variable GRADIO_SERVER_PORT.
        node_path: the path to the node executable. Can be set by environment variable GRADIO_NODE_PATH.
        python_port: the port of the main Python (FastAPI) server that Node will proxy to.
        python_host: the host of the main Python server (default 127.0.0.1).
        static_worker_ports: ports of static file worker processes for round-robin proxying.

    Returns:
        server_name: the name of the server (default is "localhost")
        node_process: the node process that is running the SSR app
        node_port: the port the node server is running on
    []   NGRADIO_NODE_PATHr   )r   r   server_portsr   r   r   r   )	LOCALHOST_NAME
startswithendswithrangeINITIAL_PORT_VALUETRY_NUM_PORTSstart_node_processosgetenv)r   r   r   r   r   r   r   hostr#   node_process	node_ports              V/home/wildlama/visual-decline/.venv/lib/python3.11/site-packages/gradio/node_server.pystart_node_serverr1      s    2 /K
 c"" {';';C'@'@ 1R4  " 
%'9M'IJJ  1<ry);<<!.;/52  L) i//    GRADIO_LOCAL_DEV_MODE	templatesnodebuildstrr#   list[int] | range1tuple[subprocess.Popen[bytes] | None, int | None]c                4   t           rdS | sdS d |D ]C}	 t          j                    }|                    t          j        t          j        d           |                    ||f           |                                 t          j        }	t          |          |	d<   ||	d<   t           rd|	d<   |t          |          |	d<   ||	d	<   |r"d

                    d |D                       |	d<   t          t          t                    j                            dd                    }
t          j        dk    rd|
z   }
t#          j        | d|
t&          g|	|st"          j        nd |st"          j        nd           t+          ||d          }|rJt-          j        t,          j        fd           t-          j        t,          j        fd           |fc S                                                      d           d # t6          $ r Y t8          $ r;}t;          j        d| d           r                                 d Y d }~=d }~ww xY wt?          d|d          d|d          d           t?          d           t?          d            dS )!N)Ni&  )NNr    PORTHOST1r3   GRADIO_PYTHON_PORTGRADIO_PYTHON_HOST,c              3  4   K   | ]}t          |          V  d S N)r7   ).0ps     r0   	<genexpr>z%start_node_process.<locals>.<genexpr>~   s9       = = CFF= = = = = =r2   GRADIO_STATIC_WORKER_PORTSr4   zregister.mjswin32zfile://z--import)envstdoutstderr   timeoutc                "    t                    S rB   handle_sigterm___r.   s     r0   <lambda>z$start_node_process.<locals>.<lambda>   s    .2N2N r2   c                "    t                    S rB   rO   rQ   s     r0   rT   z$start_node_process.<locals>.<lambda>   s    >,;W;W r2      z-Unexpected error while starting Node server: z. Trying next port...z2Cannot start Node server on any port in the range r   -r!   .zwPlease install Node 20 or higher and set the environment variable GRADIO_NODE_PATH to the path of your Node executable.zWYou can explicitly specify a port by setting the environment variable GRADIO_NODE_PORT.) r3   socket
setsockopt
SOL_SOCKETSO_REUSEADDRbindcloser+   environr7   joinr   __file__parentjoinpathsysplatform
subprocessPopenSSR_APP_PATHDEVNULLverify_server_startupsignalSIGTERMSIGINT	terminatewaitOSError	Exceptionwarningswarnprint)r   r   r#   r   r   r   r   portsrH   register_file
is_workinger.   s                @r0   r*   r*   V   sC     z zL H HG	 ALL*F,?CCC FFK&'''GGIII *Cd))CK%CK$ 3/2+, &,/,<,<(),7()" 47HH = =$7= = = 5 501  X%..{NKK M |w&& )M 9%+J|D16@z))D16@z))D	  L /{D"MMMJ $N$N$N$N$N   fm-W-W-W-WXXX#T)))) &&(((!!!!,,,# 	 	 	H 	 	 	MXXXX    $&&(((#HHHH	 
b\!_bb|\^O_bbb   
 	B   
a   :s$   F<H,H
I	I0IIr-   ru   intc                    	 t          t          j        | |fd                    5  	 ddd           dS # 1 swxY w Y   dS # t          t          f$ r Y dS t
          $ r Y dS w xY w)z+Attempts a single connection to the server.r    rL   NTF)r   rY   create_connectionr   ConnectionRefusedErrorrq   )r-   ru   s     r0   attempt_connectionr~      s    V-tTlAFFFGG 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	01   uu   uus2   %A 6A :A :A A$	A$#A$      .@rM   floatc                   t          j                     }t          j                     |z
  |k     r	 t          | |d          }|                    dd           |                                }|                                 |j        dk     rdS n# t          $ r Y nw xY wt          j        d           t          j                     |z
  |k     dS )	zPolls ``HEAD /`` until the server returns a non-5xx status (or the
    timeout elapses), confirming it can actually serve requests rather than
    just that its TCP port is open.rV   rL   HEAD/i  Tg?F)timer   requestgetresponser^   statusrq   sleep)r-   ru   rM   
start_timeconnresps         r0   rj   rj      s     J
)++

"W
,
,	!$a888DLL%%%##%%DJJLLL{S  t ! 	 	 	D	
3 )++

"W
,
, 5s   AB 
BBr.   subprocess.Popen[bytes] | Nonec                    | Mt          d           |                                  |                                  t          j        d           d S d S )Nz
Stopping Node.js server...r   )rt   rn   ro   rd   exit)r.   s    r0   rP   rP      sT    ,---   	  r2   )NNNNNNF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )Nr   NF)r   r   r   r7   r#   r8   r   r   r   r7   r   r   r   r   r   r9   )r-   r7   ru   rz   r   r   )r   )r-   r7   ru   rz   rM   r   r   r   )r.   r   )"
__future__r   r+   rk   rY   rf   rd   r   rr   concurrent.futuresr   
contextlibr   http.clientr   pathlibr   typingr   rz   r,   r(   r)   r$   r1   r3   ra   rb   rc   rh   r*   r~   rj   rP    r2   r0   <module>r      s	   " " " " " " 				       



   + + + + + +       & & & & & &                   	 S#7@@AA IBI5u==>>yry)={KK  #" "",030 30 30 30 30l "	"9::$F tH~~$--k67KK #",0e e e e eP       &     r2   