protocol
protocol
Wire protocol shared by the Nyx bridge server and its clients.
Standard library only, for Python 3.11+: clients living in other applications (DCCs) install this package, so it must have no dependencies.
Messages are JSON objects with a type key, sent over TCP with a 4-byte big-endian
length prefix.
Attributes
ADDRESS_ENV_VAR = 'NYX_BRIDGE_ADDRESS'
module-attribute
ADDRESS_FILE_ENV_VAR = 'NYX_BRIDGE_ADDRESS_FILE'
module-attribute
Address = collections.namedtuple('Address', ['host', 'port', 'token'])
module-attribute
DEFAULT_ADDRESS_FILE_NAME = 'bridge.json'
module-attribute
DEFAULT_HOST = '127.0.0.1'
module-attribute
DEFAULT_PORT = 47770
module-attribute
EVAL = 'eval'
module-attribute
EXEC = 'exec'
module-attribute
MAX_MESSAGE_SIZE = 64 * 1024 * 1024
module-attribute
PROTOCOL_VERSION = 1
module-attribute
TOKEN_ENV_VAR = 'NYX_BRIDGE_TOKEN'
module-attribute
Classes
ConnectionClosed
Bases: ProtocolError
The peer closed the connection (or it broke) while reading.
HandshakeError
Bases: ProtocolError
The server refused the hello message.
MessageType
Values of the type key of messages.
Attributes
ACK = 'ack'
class-attribute
instance-attribute
ERROR = 'error'
class-attribute
instance-attribute
EXECUTE = 'execute'
class-attribute
instance-attribute
HELLO = 'hello'
class-attribute
instance-attribute
PING = 'ping'
class-attribute
instance-attribute
PONG = 'pong'
class-attribute
instance-attribute
RESULT = 'result'
class-attribute
instance-attribute
SHUTDOWN = 'shutdown'
class-attribute
instance-attribute
ProtocolError
Bases: Exception
Malformed message or a peer breaking the protocol.
Functions:
default_address_file_path()
Where Nyx writes its address file unless told otherwise.
The folder is the per-user data folder of the Nyx application, so a client on the same machine finds a running Nyx without any configuration.
Returns:
| Type | Description |
|---|---|
Path
|
pathlib.Path: path of the file, which may not exist. |
Source code in src/nyx_bridge/protocol.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
encode_message(message)
Serialize a message and prefix it with its length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
dict
|
JSON serializable message. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
message can't be serialized to JSON. |
ProtocolError
|
message is larger than |
Returns:
| Name | Type | Description |
|---|---|---|
bytes |
bytes
|
frame to send. |
Source code in src/nyx_bridge/protocol.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
find_server_address(address=None, token=None)
Work out where the server is.
Looked up in order: the arguments, NYX_BRIDGE_ADDRESS (host:port) with
NYX_BRIDGE_TOKEN, then the file named by NYX_BRIDGE_ADDRESS_FILE, then the
default address file of a running Nyx (see default_address_file_path).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
address
|
str | tuple[str, int] | None
|
|
None
|
token
|
str | None
|
token, taken from the environment if omitted. |
None
|
Returns:
| Type | Description |
|---|---|
Address | None
|
Address | None: None when nothing says where the server is. |
Source code in src/nyx_bridge/protocol.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
make_ack(client_id, server_name)
Build the server's answer to an accepted hello.
Source code in src/nyx_bridge/protocol.py
166 167 168 169 170 171 172 173 | |
make_error(message, request_id=None)
Build an error message, optionally answering a request.
Source code in src/nyx_bridge/protocol.py
176 177 178 179 180 181 | |
make_execute(request_id, code, mode=EXEC, args=None)
Build a request to run code in the client.
Source code in src/nyx_bridge/protocol.py
184 185 186 187 188 189 190 191 192 193 194 | |
make_hello(token, client_name, client_kind, client_version)
Build the message a client opens a connection with.
Source code in src/nyx_bridge/protocol.py
152 153 154 155 156 157 158 159 160 161 162 163 | |
make_ping(request_id)
Build a ping, answered by a pong with the same id.
Source code in src/nyx_bridge/protocol.py
220 221 222 | |
make_pong(request_id)
Build the answer to a ping.
Source code in src/nyx_bridge/protocol.py
225 226 227 | |
make_result(request_id, ok, value=None, error=None, stdout='', stderr='')
Build the answer to an execute request.
On failure error is {"type", "message", "traceback"}.
Source code in src/nyx_bridge/protocol.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
make_shutdown()
Build the message asking a client to disconnect and stop serving.
Source code in src/nyx_bridge/protocol.py
230 231 232 | |
parse_address(text)
Split host:port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
address, a bare port means the default host. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
text is not an address. |
Returns:
| Type | Description |
|---|---|
tuple[str, int]
|
tuple[str, int]: host and port. |
Source code in src/nyx_bridge/protocol.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
read_address_file(path)
Read a file written by write_address_file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike
|
address file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Address |
Address
|
host, port and token. |
Source code in src/nyx_bridge/protocol.py
303 304 305 306 307 308 309 310 311 312 313 314 | |
recv_message(sock)
Block until a whole message is read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sock
|
socket
|
connected socket. |
required |
Raises:
| Type | Description |
|---|---|
ConnectionClosed
|
connection closed or broken. |
ProtocolError
|
invalid frame, size or payload. |
TimeoutError
|
socket timeout expired. |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
message. |
Source code in src/nyx_bridge/protocol.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
send_message(sock, message)
Send a message. Callers sharing a socket between threads have to lock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sock
|
socket
|
connected socket. |
required |
message
|
dict
|
JSON serializable message. |
required |
Source code in src/nyx_bridge/protocol.py
85 86 87 88 89 90 91 92 | |
to_jsonable(value, _depth=0)
Convert a value to something JSON can carry.
Containers are converted item by item. Anything else that JSON can't carry, like
an object of the application, becomes its repr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
value to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
JSON serializable value. |
Source code in src/nyx_bridge/protocol.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
write_address_file(path, host, port, token)
Write where a server listens, so clients on the machine can find it.
The file holds the token, so it is made readable by the owner only where possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike
|
file to write. |
required |
host
|
str
|
server host. |
required |
port
|
int
|
server port. |
required |
token
|
str
|
server token. |
required |
Source code in src/nyx_bridge/protocol.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |