§
    ®	Êj8K  ã                  ó   — d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZmZ ddlmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZmZ ddlm Z m!Z! ddlm"Z# ddl$m%Z%m&Z&  G d„ dej'        ¦  «        Z(ddl)m*Z*m+Z+m,Z, g d¢Z-dS )uA   Async Playwright faÃ§ade - mirrors sync_api but with async/await.é    )ÚannotationsN)ÚPath)ÚAnyÚDictÚOptionalÚUnion)ÚBrowserÚBrowserContextÚ
PlaywrightÚasync_playwright)Úpage_number_reservedé   )Ú_session)Úresolve_cursor_engine)ÚProfileÚgenerate_profile)ÚSessionLocaleÚpersona_cookiesÚprepare_session_geo)Úassert_wire_versionÚresolve_executable)Úconfigure_proxy)ÚSessionTokenÚ	guard_forc                  ój   — e Zd ZdZ	 d.dddddddddddddœd/d!„Zd0d#„Zd$Zd%Zd1d&„Zd2d)„Z	d3d,„Z
d1d-„ZdS )4ÚInvisiblePlaywrightzZAsync context manager - see invisible_playwright.InvisiblePlaywright for the sync variant.NFTÚautoÚ )ÚpinÚheadlessÚproxyÚ
extra_argsÚhumanizeÚlocaleÚtimezoneÚextra_prefsÚbinary_pathÚprofile_dirÚprep_recaptchaÚshow_cursorÚseedúOptional[int]r   úOptional[Dict[str, Any]]r    Úboolr!   úOptional[Dict[str, str]]r"   úOptional[list[str]]r#   úUnion[bool, float]r$   Ústrr%   r&   r'   úOptional[str]r(   úOptional[Union[str, Path]]r)   r*   úOptional[bool]ÚreturnÚNonec               óÂ  — |�t          |¦  «        nt          j        d¦  «        | _        || _        || _        || _        t          |pg ¦  «        | _        || _	        t          |¦  «        | _        |€d nt          |¦  «        | _        || _        d | _        || _        |	| _        |
| _        |rt'          |¦  «        nd | _        t          |¦  «        o| j        d u | _        t-          | j        | j        ¬¦  «        | _        d | _        d | _        d | _        d | _        t9          ¦   «         | _        t=          ¦   «         | _        d | _         d | _!        d| _"        d| _#        d S )Né   )r   g        r   )$ÚintÚsecretsÚrandbitsr+   Ú_pinÚ	_headlessÚ_proxyÚlistÚ_extra_argsÚ	_humanizer   Ú_cursor_enginer.   Ú_show_cursorÚ_locale_requestedÚ_localeÚ	_timezoneÚ_extra_prefsÚ_binary_pathr   Ú_profile_dirÚ_prep_recaptchar   Ú_profileÚ_pwÚ_browserÚ_persistent_contextÚ_virtual_displayr   Ú_session_tokenr   Ú_lifetime_guardÚ_webrtc_egress_ipÚ_srflx_declaredÚ_ultimo_controllo_uscitaÚ_uscite_non_misurabili)Úselfr+   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   s                 úNC:\Projects\Muse2API\.venv\Lib\site-packages\invisible_playwright/async_api.pyÚ__init__zInvisiblePlaywright.__init__   se  € ð$ '+Ð&6�˜T™œ˜½GÔ<LÈRÑ<PÔ<PˆŒ	ØˆŒ	Ø!ˆŒØˆŒÝ 
Ð 0¨bÑ1Ô1ˆÔØ!ˆŒõ 4°HÑ=Ô=ˆÔð &1Ð%8˜T˜TÝ"& {Ñ"3Ô"3ð 	Ôð
 "(ˆÔØ04ˆŒØ!ˆŒØ'ˆÔØ'ˆÔØALÐ,V­D°Ñ,=Ô,=Ð,=ÐRVˆÔå# NÑ3Ô3ÐQ¸Ô8IÈTÐ8QˆÔõ "2°$´)ÀÄÐ!KÑ!KÔ!KˆŒØ)-ˆŒØ+/ˆŒØ=AˆÔ Ø%)ˆÔõ +™nœnˆÔÝ(™{œ{ˆÔà04ˆÔð
 /3ˆÔð 03ˆÔ%ð ,-ˆÔ#Ð#Ð#ó    úUnion[Browser, BrowserContext]c              ƒ  óD  K  — t          j        t          | j        | j        | j        ¦  «        ƒ d {V —†}|j        | _        |j        | _        |j	        | _
        |                     ¦   «         | _        t          | j        ¦  «        }|                      ¦   «         }|                      ¦   «         }t#          | j        |¦  «        }t%          j        ¦   «         | _        |                      |¦  «        }	 t-          ¦   «                              ¦   «         ƒ d {V —†| _        | j        �±| j                             dd¬¦  «          | j        j        j        dt;          | j        ¦  «        t;          |¦  «        |||| j        |dœ|                      ¦   «         ¤Žƒ d {V —†| _         |  !                    ¦   «          |  "                    | j         ¦  «         | j         S | j        j         #                    t;          |¦  «        |||| j        |¬¦  «        ƒ d {V —†| _$        tK          | j$        ¦  «         |  !                    ¦   «          n)# tL          $ r |  '                    ¦   «         ƒ d {V —† ‚ w xY w|  (                    | j$        ¦  «         |  "                    | j$        ¦  «         | j$        S )NT)ÚparentsÚexist_ok)Úuser_data_dirÚexecutable_pathr    Úfirefox_user_prefsr!   ÚargsÚenv)r`   r    ra   r!   rb   rc   © ))ÚasyncioÚ	to_threadr   rG   r?   rE   r%   r$   rF   Ú	egress_iprS   Úsrflx_to_declarerT   r   rI   Ú_resolve_headlessÚ_build_prefsÚ_configure_proxy_sharedr   ÚmintrQ   Ú
_build_envr   ÚstartrM   rJ   ÚmkdirÚfirefoxÚlaunch_persistent_contextr2   rA   Ú_default_context_kwargsrO   Ú_bind_process_treeÚ_arm_cursor_engineÚlaunchrN   r   ÚBaseExceptionÚ	_teardownÚ_patch_new_context_defaults)rW   Ú_geoÚ
executableÚpw_headlessÚprefsÚplaywright_proxyrc   s          rX   Ú
__aenter__zInvisiblePlaywright.__aenter__h   sì  è è € õ Ô&Ý ¤°´¸dÔ>Tñ
ô 
ð 
ð 
ð 
ð 
ð 
ð 
ˆð œˆŒØ”{ˆŒØ!%¤ˆÔð  $×4Ò4Ñ6Ô6ˆÔõ (¨Ô(9Ñ:Ô:ˆ
ð ×,Ò,Ñ.Ô.ˆØ×!Ò!Ñ#Ô#ˆÝ2°4´;ÀÑFÔFÐÝ*Ô/Ñ1Ô1ˆÔØ�oŠo˜eÑ$Ô$ˆð#	Ý-Ñ/Ô/×5Ò5Ñ7Ô7Ð7Ð7Ð7Ð7Ð7Ð7ˆDŒHØÔ Ð,àÔ!×'Ò'°¸tÐ'ÑDÔDÐDð 2\°´Ô1AÔ1[ð 	2Ý"% dÔ&7Ñ"8Ô"8Ý$'¨
¡O¤OØ(Ø',Ø*ØÔ)Øð	2ð 	2ð ×2Ò2Ñ4Ô4ð	2ð 	2ð 	,ð 	,ð 	,ð 	,ð 	,ð 	,�Ô(ð ×'Ò'Ñ)Ô)Ð)Ø×'Ò'¨Ô(@ÑAÔAÐAØÔ/Ð/Ø"&¤(Ô"2×"9Ò"9Ý # J¡¤Ø$Ø#(Ø&ØÔ%Øð #:ñ #ô #ð ð ð ð ð ð ˆDŒMõ   ¤Ñ.Ô.Ð.Ø×#Ò#Ñ%Ô%Ð%Ð%øÝð 	ð 	ð 	Ø—.’.Ñ"Ô"Ð"Ð"Ð"Ð"Ð"Ð"Ð"Øð	øøøð 	×(Ò(¨¬Ñ7Ô7Ð7Ø×Ò ¤Ñ.Ô.Ð.ØŒ}Ðs   Ã0C"H> ÇA*H> È>&I$g      ^@é   c              ƒ  ó  K  — | j         r| j        sdS t          j        ¦   «         }|| j        z
  | j        k     rdS || _        t          j        ¦   «                              dt          j
        | j         | j        ¦  «        ƒ d{V —†\  }}|t          j        k    r t          j        d| j        ›d|›d�¦  «        ‚|t          j        k    r>| xj        dz  c_        | j        | j        k    rt          j        d| j        z  ¦  «        ‚dS d| _        dS )z„Rejects if the egress IP has changed since launch. See the
        sync twin in `launcher.py` for why it does not update on the fly.NzCthe proxy's egress IP changed during the session: at launch it was z, now it is a-  . The WebRTC srflx candidate still declares the first one, so from this moment the page exits from one address and WebRTC announces another - the disagreement detectors look for. This proxy does not hold the session sticky for the required duration: use one that guarantees it, or shorten the session.r   a0  the egress IP could not be verified for %d checks in a row. It is not drift - the probe did not respond at all - but it is not agreement either: from here on the engine would be declaring an address on every page that nobody confirms anymore. Check that the proxy is reachable, then relaunch the session.r   )r?   rS   ÚtimeÚ	monotonicrU   Ú_INTERVALLO_CONTROLLO_USCITA_Sre   Úget_running_loopÚrun_in_executorr   Úegress_ancora_validoÚUSCITA_DERIVATAÚProxyEgressDriftedÚUSCITA_NON_MISURABILErV   Ú_MAX_USCITE_NON_MISURABILIÚProxyEgressNonVerificabile)rW   ÚnowÚoutcomeÚcurrents       rX   Ú_assert_uscita_invariataz,InvisiblePlaywright._assert_uscita_invariataÂ   sY  è è € ð Œ{ð 	 $Ô"8ð 	ØˆFÝŒnÑÔˆØ�Ô.Ñ.°Ô1TÒTÐTØˆFØ(+ˆÔ%å!(Ô!9Ñ!;Ô!;×!KÒ!KØ•(Ô/°´ØÔ"ñ"$ô "$ð $ð $ð $ð $ð $ð $Ñˆ�ð •hÔ.Ò.Ð.ÝÔ-Ð-ð Ô)Ð)Ð)¨7¨7¨7ð4ñ	5ô 	5ð 	5ð •hÔ4Ò4Ð4ð Ð'Ô'¨1Ñ,Ð'Ô'ØÔ*¨dÔ.MÒMÐMÝÔ9ðEð Ô1ñ2ñ3ô 3ð 3ð ˆFØ&'ˆÔ#Ð#Ð#rZ   Úbrowserr	   c                óÔ   ‡ ‡‡‡‡‡‡‡	‡
— ‰j         Š‰                      ¦   «         Š‰ j        Š‰ j        Š	‰ j        Šˆˆ	fd„Š
ˆˆˆˆ
ˆ fd„}|‰_         ‰j        Šˆˆˆˆˆ
ˆ fd„}|‰_        dS )a´  Both entry points, for the reason spelled out in the sync launcher:
        Playwright's `Browser.new_page` forwards to the IMPLEMENTATION object,
        whose own `new_page` calls `self.new_context` - itself - so a wrapper
        installed on the api object is never consulted, and
        `await browser.new_page()` opened a page with the stock viewport and
        colour scheme against a fingerprint claiming the profile's screen.c              “  ó‚   •K  — 	 |                       t          ‰‰¦  «        ¦  «        ƒ d {V —† d S # t          $ r Y d S w xY w©N)Úadd_cookiesr   Ú	Exception)ÚctxÚlocÚprofiles    €€rX   Úseed_cookieszEInvisiblePlaywright._patch_new_context_defaults.<locals>.seed_cookiesú   sb   øè è € ðØ—o’o¥o°g¸sÑ&CÔ&CÑDÔDÐDÐDÐDÐDÐDÐDÐDÐDÐDøÝð ð ð Ø��ðøøøs   …)0 °
>½>c               ›  óø   •‡‡K  — ‰	                      ¦   «         ƒ d {V —† t          ‰¦  «        }|                     | ¦  «          ‰di |¤Žƒ d {V —†Š‰r ‰‰¦  «        ƒ d {V —† ‰j        Šˆˆˆ	fd„}|‰_        ‰S )Nc               ›  ó¾   •K  — t          ‰j        j        ¦  «        5  ‰                     ¦   «         ƒ d {V —†  ‰di | ¤Žƒ d {V —†cd d d ¦  «         S # 1 swxY w Y   d S ©Nrd   )r   Ú	_impl_objrN   r�   )Úkw2Ú_new_page_ctxr–   rW   s    €€€rX   Ú_new_page_guardedz[InvisiblePlaywright._patch_new_context_defaults.<locals>.patched.<locals>._new_page_guarded  sÉ   øè è € õ *¨#¬-Ô*@ÑAÔAð 6ð 6Ø×7Ò7Ñ9Ô9Ð9Ð9Ð9Ð9Ð9Ð9Ð9Ø!. Ð!5Ð!5°Ð!5Ð!5Ð5Ð5Ð5Ð5Ð5Ð5ð6ð 6ð 6ð 6ñ 6ô 6ð 6ð 6ð 6ð 6ð 6ð 6øøøð 6ð 6ð 6ð 6ð 6ð 6s   �(AÁAÁArd   )r�   ÚdictÚupdateÚnew_page)
ÚkwÚmergedr    rŸ   r–   ÚdefaultsÚoriginalÚprepr™   rW   s
      @@€€€€€rX   Úpatchedz@InvisiblePlaywright._patch_new_context_defaults.<locals>.patched  sÕ   øøøè è € Ø×/Ò/Ñ1Ô1Ð1Ð1Ð1Ð1Ð1Ð1Ð1Ý˜(‘^”^ˆFØ�MŠM˜"ÑÔÐØ ˜Ð*Ð* 6Ð*Ð*Ð*Ð*Ð*Ð*Ð*Ð*ˆCØð (Ø"�l 3Ñ'Ô'Ð'Ð'Ð'Ð'Ð'Ð'Ð'ð  œLˆMð6ð 6ð 6ð 6ð 6ð 6ð 6ð -ˆCŒLØˆJrZ   c               ›  ó0  •K  — t          ‰j        ¦  «        5  ‰	                     ¦   «         ƒ d {V —† t          ‰¦  «        }|                     | ¦  «          ‰di |¤Žƒ d {V —†}d d d ¦  «         n# 1 swxY w Y   |j        }‰r ‰|¦  «        ƒ d {V —† |S rœ   )r   r�   r�   r¡   r¢   Úcontext)
r¤   r¥   Úpager–   r�   r¦   Úoriginal_pager¨   r™   rW   s
       €€€€€€rX   Úpatched_pagezEInvisiblePlaywright._patch_new_context_defaults.<locals>.patched_page'  s  øè è € å% gÔ&7Ñ8Ô8ð 5ð 5Ø×3Ò3Ñ5Ô5Ð5Ð5Ð5Ð5Ð5Ð5Ð5Ý˜h™œ�Ø—’˜bÑ!Ô!Ð!Ø*˜]Ð4Ð4¨VÐ4Ð4Ð4Ð4Ð4Ð4Ð4Ð4�ð	5ð 5ð 5ñ 5ô 5ð 5ð 5ð 5ð 5ð 5ð 5øøøð 5ð 5ð 5ð 5ð
 ”,ˆCØð (Ø"�l 3Ñ'Ô'Ð'Ð'Ð'Ð'Ð'Ð'Ð'ØˆKs   ˜AA1Á1A5Á8A5N)Únew_contextrr   rK   rL   rF   r£   )rW   r�   r©   r®   r¦   r—   r§   r­   r¨   r˜   r™   s   ``  @@@@@@@rX   rx   z/InvisiblePlaywright._patch_new_context_defaultsí   sÜ   øøøøøøøøø€ ð Ô&ˆØ×/Ò/Ñ1Ô1ˆØÔ#ˆØ”-ˆØŒlˆð	ð 	ð 	ð 	ð 	ð 	ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð@ &ˆÔàÔ(ˆð
	ð 
	ð 
	ð 
	ð 
	ð 
	ð 
	ð 
	ð 
	ð 
	ð (ˆÔÐÐrZ   Úexcr   c              ‡  ó>   K  — |                       ¦   «         ƒ d {V —† d S r“   )rw   )rW   r°   s     rX   Ú	__aexit__zInvisiblePlaywright.__aexit__6  s.   è è € Ø�nŠnÑÔÐÐÐÐÐÐÐÐÐrZ   c              ƒ  ó  ‡K  — dŠdˆfd„}	 | j         �/ || j                              ¦   «         ¦  «        ƒ d{V —† d| _         | j        �/ || j                             ¦   «         ¦  «        ƒ d{V —† d| _        | j        �/ || j                             ¦   «         ¦  «        ƒ d{V —† d| _        | j        �2	 | j                             ¦   «          n# t          $ r Y nw xY wd| _        | j        rD	 | j         	                    | j        ¦  «         n# t          $ r Y nw xY wt          ¦   «         | _        nP# | j        rD	 | j         	                    | j        ¦  «         n# t          $ r Y nw xY wt          ¦   «         | _        w xY w‰�‰‚dS )uÒ  Shut everything down, and finish doing it even while being cancelled.

        â›” `except Exception` DOES NOT CATCH A CANCELLATION, and that is the
        whole reason this is not four plain try/excepts any more. When the
        task running this is cancelled, the `await` below raises
        `asyncio.CancelledError`, which inherits from `BaseException` and not
        from `Exception` - so it escapes, teardown stops at whichever step it
        had reached, and everything after it is skipped. What survives is a
        browser nobody closes: the exact orphan this project has already
        measured twice, once as 88 stray firefox processes and once as 7,308
        leftover profile directories.

        So a cancellation is CAUGHT, kept, and re-raised at the end - the
        cancellation still propagates, as asyncio requires, but only after
        every step has had its turn. Reported by DatGuy1 as #104 against the
        published `main` and fixed here in the same shape.

        â›” AND THE REAPING IS IN A `finally`, not last in the body: it is the
        one step that must run even if the loop above dies in a way this does
        not model. Nothing carrying this session's token may outlive it.
        NÚstepr   r6   r7   c              “  ót   •K  — 	 | ƒ d {V —† d S # t           j        $ r}|ŠY d }~d S d }~wt          $ r Y d S w xY wr“   )re   ÚCancelledErrorr•   )r´   ÚstopÚ	cancelleds     €rX   Úclosez,InvisiblePlaywright._teardown.<locals>.closeQ  sm   øè è € ðØ�
�
�
�
�
�
�
�
�
øÝÔ)ð !ð !ð !Ø �	�	�	�	�	�	�	øøøøÝð ð ð Ø��ðøøøs   … �7ž&¦7¶7)r´   r   r6   r7   )rO   r¹   rN   rM   r·   rP   r•   rQ   rR   Úreapr   )rW   r¹   r¸   s     @rX   rw   zInvisiblePlaywright._teardown9  s  øè è € ð, .2ˆ	ð	ð 	ð 	ð 	ð 	ð 	ð	5ØÔ'Ð3Ø�e˜DÔ4×:Ò:Ñ<Ô<Ñ=Ô=Ð=Ð=Ð=Ð=Ð=Ð=Ð=Ø+/�Ô(ØŒ}Ð(Ø�e˜DœM×/Ò/Ñ1Ô1Ñ2Ô2Ð2Ð2Ð2Ð2Ð2Ð2Ð2Ø $�”ØŒxÐ#Ø�e˜DœHŸMšM™OœOÑ,Ô,Ð,Ð,Ð,Ð,Ð,Ð,Ð,Ø�”ØÔ$Ð0ðØÔ)×.Ò.Ñ0Ô0Ð0Ð0øÝ ð ð ð Ø�Dðøøøà(,�Ô%àÔ"ð 5ðØÔ(×-Ò-¨dÔ.AÑBÔBÐBÐBøÝ ð ð ð Ø�Dðøøøå&2¡n¤n�Ô#øøð Ô"ð 5ðØÔ(×-Ò-¨dÔ.AÑBÔBÐBÐBøÝ ð ð ð Ø�Dðøøøå&2¡n¤n�Ô#Ð4Ð4Ð4Ð4àÐ ØˆOð !Ð sf   �B)D4 Â7C ÃD4 Ã
CÃD4 ÃCÃ
D4 Ã0D Ä
DÄDÄ4FÄ=EÅFÅ
E*Å'FÅ)E*Å*Fr“   )r+   r,   r   r-   r    r.   r!   r/   r"   r0   r#   r1   r$   r2   r%   r2   r&   r-   r'   r3   r(   r4   r)   r.   r*   r5   r6   r7   )r6   r[   )r6   r7   )r�   r	   r6   r7   )r°   r   r6   r7   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rY   r~   rƒ   rŠ   r�   rx   r²   rw   rd   rZ   rX   r   r      sï   € € € € € ØdÐdð #ðM-ð )-ØØ*.Ø*.Ø'+ØØØ04Ø%)Ø26Ø$Ø&*ðM-ð M-ð M-ð M-ð M-ð M-ð^Jð Jð Jð Jðb &+Ð"ð "#Ðð)(ð )(ð )(ð )(ðVF(ð F(ð F(ð F(ðRð ð ð ð<ð <ð <ð <ð <ð <rZ   r   )ÚErrorÚTargetClosedErrorÚTimeoutError)r   r¿   rÀ   rÁ   ).r¾   Ú
__future__r   re   r;   r�   Úpathlibr   Útypingr   r   r   r   Ú"invisible_playwright._pw.async_apir	   r
   r   r   Ú/invisible_playwright._pw._impl._browser_contextr   r   r   Ú_cursorr   Úinvisible_core._fpforger   r   Úinvisible_corer   r   r   Ú_enginer   r   r   rk   Ú_reaperr   r   ÚCommonLaunchr   Ú&invisible_playwright._pw._impl._errorsr¿   rÀ   rÁ   Ú__all__rd   rZ   rX   ú<module>rÏ      sº  ðØ GÐ GØ "Ð "Ð "Ð "Ð "Ð "à €€€Ø €€€Ø €€€Ø Ð Ð Ð Ð Ð Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -à dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dØ PÐ PÐ PÐ PÐ PÐ Pà Ð Ð Ð Ð Ð Ø *Ð *Ð *Ð *Ð *Ð *Ø =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ø NÐ NÐ NÐ NÐ NÐ NÐ NÐ NÐ NÐ NØ <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ø EÐ EÐ EÐ EÐ EÐ EØ ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,ð_ð _ð _ð _ð _˜(Ô/ñ _ô _ð _ðV,ð ,ð ,ð ,ð ,ð ,ð ,ð ,ð ,ð ,ð PÐ
OÐ
O€€€rZ   