1717import fnmatch
1818import re
1919
20- from typing import Any , Callable , Dict , List , Optional , Tuple , Union , TYPE_CHECKING
20+ from typing import Any , Callable , Dict , List , Optional , Tuple , Union , TYPE_CHECKING , Pattern , cast
2121
2222import sys
2323
3737class FilePayload (TypedDict ):
3838 name : str
3939 mimeType : str
40- buffer : bytes
40+ buffer : Union [ bytes , str ]
4141class FrameMatch (TypedDict ):
4242 url : URLMatch
4343 name : str
@@ -49,16 +49,35 @@ class ConsoleMessageLocation(TypedDict):
4949 url : Optional [str ]
5050 lineNumber : Optional [int ]
5151 columnNumber : Optional [int ]
52- class ErrorPayload (TypedDict ):
52+ class ErrorPayload (TypedDict , total = False ):
5353 message : str
5454 name : str
5555 stack : str
5656 value : Any
5757
58+ class ContinueParameters (TypedDict , total = False ):
59+ method : str
60+ headers : Dict [str , str ]
61+ postData : str
62+
63+ class ParsedMessageParams (TypedDict ):
64+ type : str
65+ guid : str
66+ initializer : Dict
67+
68+ class ParsedMessagePayload (TypedDict , total = False ):
69+ id : int
70+ guid : str
71+ method : str
72+ params : ParsedMessageParams
73+ result : Any
74+ error : ErrorPayload
75+
76+
5877class URLMatcher :
5978 def __init__ (self , match : URLMatch ):
60- self ._callback = None
61- self ._regex_obj = None
79+ self ._callback : Optional [ Callable [[ str ], bool ]] = None
80+ self ._regex_obj : Optional [ Pattern ] = None
6281 if isinstance (match , str ):
6382 regex = '(?:http://|https://)' + fnmatch .translate (match )
6483 self ._regex_obj = re .compile (regex )
@@ -69,7 +88,9 @@ def __init__(self, match: URLMatch):
6988 def matches (self , url : str ) -> bool :
7089 if self ._callback :
7190 return self ._callback (url )
72- return self ._regex_obj .match (url )
91+ if self ._regex_obj :
92+ return cast (bool , self ._regex_obj .match (url ))
93+ return False
7394
7495
7596class TimeoutSettings :
@@ -79,16 +100,22 @@ def __init__(self, parent: Optional['TimeoutSettings']) -> None:
79100 def set_default_timeout (self , timeout ):
80101 self .timeout = timeout
81102
82- class Error (BaseException ):
103+ class Error (Exception ):
83104 def __init__ (self , message : str , stack : str = None ) -> None :
84105 self .message = message
85106 self .stack = stack
86107
87- def serialize_error (ex : BaseException ) -> ErrorPayload :
108+ class TimeoutError (Error ):
109+ pass
110+
111+ def serialize_error (ex : Exception ) -> ErrorPayload :
88112 return dict (message = str (ex ))
89113
90114def parse_error (error : ErrorPayload ):
91- return Error (error ['message' ], error ['stack' ])
115+ base_error_class = Error
116+ if error .get ("name" ) == "TimeoutError" :
117+ base_error_class = TimeoutError
118+ return base_error_class (error ['message' ], error ['stack' ])
92119
93120def is_function_body (expression : str ) -> bool :
94121 expression = expression .strip ()
0 commit comments