|
| 1 | +import socket |
| 2 | +import threading |
| 3 | +from _typeshed import FileDescriptorLike |
| 4 | +from _typeshed.wsgi import WSGIEnvironment |
| 5 | +from collections.abc import Callable |
| 6 | +from selectors import SelectorKey, _EventMask |
| 7 | +from ssl import SSLContext |
| 8 | +from typing import Any, Protocol, type_check_only |
| 9 | + |
| 10 | +from wsproto import ConnectionType, WSConnection |
| 11 | +from wsproto.events import Request |
| 12 | +from wsproto.frame_protocol import CloseReason |
| 13 | + |
| 14 | +@type_check_only |
| 15 | +class _ThreadClassProtocol(Protocol): |
| 16 | + name: str |
| 17 | + # this accepts any callable as the target, like `threading.Thread` |
| 18 | + def __init__(self, target: Callable[..., Any]) -> None: ... |
| 19 | + def start(self) -> None: ... |
| 20 | + |
| 21 | +@type_check_only |
| 22 | +class _EventClassProtocol(Protocol): |
| 23 | + def clear(self) -> None: ... |
| 24 | + def set(self) -> None: ... |
| 25 | + def wait(self, timeout: float | None = None) -> bool: ... |
| 26 | + |
| 27 | +@type_check_only |
| 28 | +class _SelectorClassProtocol(Protocol): |
| 29 | + # the signature of `register` here is the same as `selectors._BaseSelectorImpl` from the stdlib |
| 30 | + def register(self, fileobj: FileDescriptorLike, events: _EventMask, data: Any = None) -> SelectorKey: ... |
| 31 | + # the signature of `select` here is the same as `selectors.DefaultSelector` from the stdlib |
| 32 | + def select(self, timeout: float | None = None) -> list[tuple[SelectorKey, _EventMask]]: ... |
| 33 | + def close(self) -> None: ... |
| 34 | + |
| 35 | +class Base: |
| 36 | + subprotocol: str | None |
| 37 | + sock: socket.socket | None |
| 38 | + receive_bytes: int |
| 39 | + ping_interval: float | None |
| 40 | + max_message_size: int | None |
| 41 | + pong_received: bool |
| 42 | + input_buffer: list[bytes | str] |
| 43 | + incoming_message: bytes | str | None |
| 44 | + incoming_message_len: int |
| 45 | + connected: bool |
| 46 | + is_server: bool |
| 47 | + close_reason: CloseReason |
| 48 | + close_message: str | None |
| 49 | + selector_class: type[_SelectorClassProtocol] |
| 50 | + event: _EventClassProtocol | threading.Event |
| 51 | + ws: WSConnection |
| 52 | + thread: _ThreadClassProtocol | threading.Thread |
| 53 | + def __init__( |
| 54 | + self, |
| 55 | + sock: socket.socket | None = None, |
| 56 | + connection_type: ConnectionType | None = None, |
| 57 | + receive_bytes: int = 4096, |
| 58 | + ping_interval: float | None = None, |
| 59 | + max_message_size: int | None = None, |
| 60 | + thread_class: type[_ThreadClassProtocol] | None = None, |
| 61 | + event_class: type[_EventClassProtocol] | None = None, |
| 62 | + selector_class: type[_SelectorClassProtocol] | None = None, |
| 63 | + ) -> None: ... |
| 64 | + def handshake(self) -> None: ... |
| 65 | + # data can be antyhing. a special case is made for `bytes`, anything else is converted to `str`. |
| 66 | + def send(self, data: bytes | Any) -> None: ... |
| 67 | + def receive(self, timeout: float | None = None) -> bytes | str | None: ... |
| 68 | + def close(self, reason: CloseReason | None = None, message: str | None = None) -> None: ... |
| 69 | + def choose_subprotocol(self, request: Request) -> str | None: ... |
| 70 | + |
| 71 | +class Server(Base): |
| 72 | + environ: WSGIEnvironment |
| 73 | + subprotocols: list[str] |
| 74 | + mode: str |
| 75 | + connected: bool |
| 76 | + def __init__( |
| 77 | + self, |
| 78 | + environ: WSGIEnvironment, |
| 79 | + subprotocols: list[str] | None = None, |
| 80 | + receive_bytes: int = 4096, |
| 81 | + ping_interval: float | None = None, |
| 82 | + max_message_size: int | None = None, |
| 83 | + thread_class: type[_ThreadClassProtocol] | None = None, |
| 84 | + event_class: type[_EventClassProtocol] | None = None, |
| 85 | + selector_class: type[_SelectorClassProtocol] | None = None, |
| 86 | + ) -> None: ... |
| 87 | + @classmethod |
| 88 | + def accept( |
| 89 | + cls, |
| 90 | + environ: WSGIEnvironment, |
| 91 | + subprotocols: list[str] | None = None, |
| 92 | + receive_bytes: int = 4096, |
| 93 | + ping_interval: float | None = None, |
| 94 | + max_message_size: int | None = None, |
| 95 | + thread_class: type[_ThreadClassProtocol] | None = None, |
| 96 | + event_class: type[_EventClassProtocol] | None = None, |
| 97 | + selector_class: type[_SelectorClassProtocol] | None = None, |
| 98 | + ) -> Server: ... |
| 99 | + def handshake(self) -> None: ... |
| 100 | + def choose_subprotocol(self, request: Request) -> str | None: ... |
| 101 | + |
| 102 | +class Client(Base): |
| 103 | + host: str |
| 104 | + port: int |
| 105 | + path: str |
| 106 | + subprotocols: list[str] |
| 107 | + extra_headeers: list[tuple[bytes, bytes]] |
| 108 | + subprotocol: str | None |
| 109 | + connected: bool |
| 110 | + def __init__( |
| 111 | + self, |
| 112 | + url: str, |
| 113 | + subprotocols: list[str] | None = None, |
| 114 | + headers: dict[bytes, bytes] | list[tuple[bytes, bytes]] | None = None, |
| 115 | + receive_bytes: int = 4096, |
| 116 | + ping_interval: float | None = None, |
| 117 | + max_message_size: int | None = None, |
| 118 | + ssl_context: SSLContext | None = None, |
| 119 | + thread_class: type[_ThreadClassProtocol] | None = None, |
| 120 | + event_class: type[_EventClassProtocol] | None = None, |
| 121 | + ) -> None: ... |
| 122 | + @classmethod |
| 123 | + def connect( |
| 124 | + cls, |
| 125 | + url: str, |
| 126 | + subprotocols: list[str] | None = None, |
| 127 | + headers: dict[bytes, bytes] | list[tuple[bytes, bytes]] | None = None, |
| 128 | + receive_bytes: int = 4096, |
| 129 | + ping_interval: float | None = None, |
| 130 | + max_message_size: int | None = None, |
| 131 | + ssl_context: SSLContext | None = None, |
| 132 | + thread_class: type[_ThreadClassProtocol] | None = None, |
| 133 | + event_class: type[_EventClassProtocol] | None = None, |
| 134 | + ) -> Client: ... |
| 135 | + def handshake(self) -> None: ... |
| 136 | + def close(self, reason: CloseReason | None = None, message: str | None = None) -> None: ... |
0 commit comments