offline-protocol-sdk 0.28.0__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- offline_protocol_sdk/__init__.py +22 -0
- offline_protocol_sdk/_callback_reentrancy.py +64 -0
- offline_protocol_sdk/address_declaration.py +207 -0
- offline_protocol_sdk/ble_manager.py +595 -0
- offline_protocol_sdk/ble_peripheral.py +673 -0
- offline_protocol_sdk/dnssd_bridge.py +614 -0
- offline_protocol_sdk/gateway_attach_policy.py +437 -0
- offline_protocol_sdk/gateway_manager.py +1217 -0
- offline_protocol_sdk/gateway_verdict_tracker.py +97 -0
- offline_protocol_sdk/internet_manager.py +1444 -0
- offline_protocol_sdk/legacy_store_adoption.py +184 -0
- offline_protocol_sdk/local_api/__init__.py +34 -0
- offline_protocol_sdk/local_api/authz.py +180 -0
- offline_protocol_sdk/local_api/cli.py +171 -0
- offline_protocol_sdk/local_api/codec.py +210 -0
- offline_protocol_sdk/local_api/dispatch.py +537 -0
- offline_protocol_sdk/local_api/mux.py +239 -0
- offline_protocol_sdk/local_api/server.py +546 -0
- offline_protocol_sdk/local_api/session.py +86 -0
- offline_protocol_sdk/local_api/table.py +849 -0
- offline_protocol_sdk/offline_protocol.py +13508 -0
- offline_protocol_sdk/peer_stream_manager.py +1303 -0
- offline_protocol_sdk/presence_watch_policy.py +102 -0
- offline_protocol_sdk/protocol_manager.py +1108 -0
- offline_protocol_sdk/relay_answer_prefixes.py +61 -0
- offline_protocol_sdk/secure_storage.py +551 -0
- offline_protocol_sdk/services.py +195 -0
- offline_protocol_sdk/state_storage.py +452 -0
- offline_protocol_sdk/storage_namespace.py +33 -0
- offline_protocol_sdk/transport_manager.py +138 -0
- offline_protocol_sdk/uniffi.dll +0 -0
- offline_protocol_sdk-0.28.0.dist-info/METADATA +519 -0
- offline_protocol_sdk-0.28.0.dist-info/RECORD +40 -0
- offline_protocol_sdk-0.28.0.dist-info/WHEEL +5 -0
- offline_protocol_sdk-0.28.0.dist-info/entry_points.txt +2 -0
- offline_protocol_sdk-0.28.0.dist-info/licenses/EXPORT.md +68 -0
- offline_protocol_sdk-0.28.0.dist-info/licenses/LICENSE +661 -0
- offline_protocol_sdk-0.28.0.dist-info/licenses/LICENSE-COMMERCIAL.md +89 -0
- offline_protocol_sdk-0.28.0.dist-info/licenses/THIRD-PARTY-NOTICES.md +6645 -0
- offline_protocol_sdk-0.28.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"""The one mapping between the interface definition's types and JSON.
|
|
2
|
+
|
|
3
|
+
The chapter's encoding table, applied in both directions from the generated
|
|
4
|
+
table rather than from a hand-written list per method: a parameter is decoded
|
|
5
|
+
by its declared type, a result is encoded by its declared type, and a
|
|
6
|
+
dictionary's fields are decoded by the dictionary's declaration. Nothing here
|
|
7
|
+
knows a method by name.
|
|
8
|
+
|
|
9
|
+
Errors are the engine's own taxonomy. A ``ProtocolError`` becomes a JSON-RPC
|
|
10
|
+
error whose ``code`` is ``-32000 - position`` in the definition's error enum
|
|
11
|
+
and whose ``data.variant`` is the variant name; the session refusals the
|
|
12
|
+
chapter defines reuse the same variants, so no error exists only on this
|
|
13
|
+
wire.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import base64
|
|
19
|
+
import binascii
|
|
20
|
+
import enum
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
from .. import offline_protocol as generated
|
|
24
|
+
from .table import TABLE
|
|
25
|
+
|
|
26
|
+
PARSE_ERROR = -32700
|
|
27
|
+
INVALID_REQUEST = -32600
|
|
28
|
+
METHOD_NOT_FOUND = -32601
|
|
29
|
+
INVALID_PARAMS = -32602
|
|
30
|
+
INTERNAL_ERROR = -32603
|
|
31
|
+
|
|
32
|
+
#: The base of the taxonomy's range: variant ``n`` is ``TAXONOMY_BASE - n``.
|
|
33
|
+
TAXONOMY_BASE = -32000
|
|
34
|
+
|
|
35
|
+
_INT_RANGES = {
|
|
36
|
+
"u8": (0, 2**8 - 1),
|
|
37
|
+
"u16": (0, 2**16 - 1),
|
|
38
|
+
"u32": (0, 2**32 - 1),
|
|
39
|
+
"u64": (0, 2**64 - 1),
|
|
40
|
+
"i16": (-(2**15), 2**15 - 1),
|
|
41
|
+
"i32": (-(2**31), 2**31 - 1),
|
|
42
|
+
"i64": (-(2**63), 2**63 - 1),
|
|
43
|
+
}
|
|
44
|
+
_FLOATS = ("f32", "double")
|
|
45
|
+
|
|
46
|
+
ERROR_VARIANTS: tuple[str, ...] = TABLE["errors"]["ProtocolError"]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class RpcError(Exception):
|
|
50
|
+
"""A JSON-RPC error object, raised to end a call."""
|
|
51
|
+
|
|
52
|
+
def __init__(self, code: int, message: str, data: dict[str, Any] | None = None) -> None:
|
|
53
|
+
super().__init__(message)
|
|
54
|
+
self.code = code
|
|
55
|
+
self.message = message
|
|
56
|
+
self.data = data
|
|
57
|
+
|
|
58
|
+
def to_json(self) -> dict[str, Any]:
|
|
59
|
+
error: dict[str, Any] = {"code": self.code, "message": self.message}
|
|
60
|
+
if self.data is not None:
|
|
61
|
+
error["data"] = self.data
|
|
62
|
+
return error
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def taxonomy_error(variant: str, message: str) -> RpcError:
|
|
66
|
+
"""A refusal spelled with one of the engine's variants."""
|
|
67
|
+
position = ERROR_VARIANTS.index(variant)
|
|
68
|
+
return RpcError(TAXONOMY_BASE - position, message, {"variant": variant})
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def error_from_protocol(exc: BaseException) -> RpcError:
|
|
72
|
+
"""The engine's exception as the wire's error, variant and all."""
|
|
73
|
+
variant = type(exc).__name__
|
|
74
|
+
if variant not in ERROR_VARIANTS:
|
|
75
|
+
# A generated error class the table does not know: the table is
|
|
76
|
+
# stale against the library, which a test refuses before this can
|
|
77
|
+
# happen in a checked-in tree.
|
|
78
|
+
return RpcError(INTERNAL_ERROR, f"unmapped engine error {variant}: {exc}")
|
|
79
|
+
return taxonomy_error(variant, str(exc))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def invalid_params(message: str) -> RpcError:
|
|
83
|
+
return RpcError(INVALID_PARAMS, message)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
# -- decoding (JSON -> the generated binding's values) ------------------------
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _b64decode(value: Any, where: str) -> bytes:
|
|
90
|
+
if not isinstance(value, str):
|
|
91
|
+
raise invalid_params(f"{where}: bytes are a base64 string")
|
|
92
|
+
try:
|
|
93
|
+
return base64.b64decode(value, validate=True)
|
|
94
|
+
except (binascii.Error, ValueError) as exc:
|
|
95
|
+
raise invalid_params(f"{where}: not valid base64 ({exc})") from None
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def decode(type_name: str, value: Any, where: str) -> Any:
|
|
99
|
+
"""``value`` (parsed JSON) as the Python value the binding takes for
|
|
100
|
+
``type_name`` (a type as the definition spells it, spaces removed)."""
|
|
101
|
+
if type_name.endswith("?"):
|
|
102
|
+
if value is None:
|
|
103
|
+
return None
|
|
104
|
+
return decode(type_name[:-1], value, where)
|
|
105
|
+
if type_name == "string":
|
|
106
|
+
if not isinstance(value, str):
|
|
107
|
+
raise invalid_params(f"{where}: expected a string")
|
|
108
|
+
return value
|
|
109
|
+
if type_name == "boolean":
|
|
110
|
+
if not isinstance(value, bool):
|
|
111
|
+
raise invalid_params(f"{where}: expected true or false")
|
|
112
|
+
return value
|
|
113
|
+
if type_name in _INT_RANGES:
|
|
114
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
115
|
+
raise invalid_params(f"{where}: expected an integer")
|
|
116
|
+
low, high = _INT_RANGES[type_name]
|
|
117
|
+
if not low <= value <= high:
|
|
118
|
+
raise invalid_params(f"{where}: {value} is outside {type_name}")
|
|
119
|
+
return value
|
|
120
|
+
if type_name in _FLOATS:
|
|
121
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
122
|
+
raise invalid_params(f"{where}: expected a number")
|
|
123
|
+
try:
|
|
124
|
+
return float(value)
|
|
125
|
+
except OverflowError:
|
|
126
|
+
# An integer JSON can spell but a double cannot hold: a refusal,
|
|
127
|
+
# not an exception that closes the connection.
|
|
128
|
+
raise invalid_params(f"{where}: {value!r} is outside {type_name}") from None
|
|
129
|
+
if type_name == "bytes":
|
|
130
|
+
return _b64decode(value, where)
|
|
131
|
+
if type_name == "sequence<u8>":
|
|
132
|
+
return list(_b64decode(value, where))
|
|
133
|
+
if type_name.startswith("sequence<"):
|
|
134
|
+
inner = type_name[len("sequence<") : -1]
|
|
135
|
+
if not isinstance(value, list):
|
|
136
|
+
raise invalid_params(f"{where}: expected an array")
|
|
137
|
+
return [decode(inner, item, f"{where}[{i}]") for i, item in enumerate(value)]
|
|
138
|
+
if type_name.startswith("record<DOMString,"):
|
|
139
|
+
inner = type_name[len("record<DOMString,") : -1]
|
|
140
|
+
if not isinstance(value, dict):
|
|
141
|
+
raise invalid_params(f"{where}: expected an object")
|
|
142
|
+
return {key: decode(inner, item, f"{where}.{key}") for key, item in value.items()}
|
|
143
|
+
if type_name in TABLE["enums"]:
|
|
144
|
+
variants = TABLE["enums"][type_name]
|
|
145
|
+
if not isinstance(value, str) or value not in variants:
|
|
146
|
+
raise invalid_params(
|
|
147
|
+
f"{where}: expected one of {', '.join(variants)} for {type_name}"
|
|
148
|
+
)
|
|
149
|
+
return getattr(generated, type_name)(variants.index(value))
|
|
150
|
+
if type_name in TABLE["records"]:
|
|
151
|
+
return _decode_record(type_name, value, where)
|
|
152
|
+
raise invalid_params(f"{where}: a value of type {type_name} has no JSON form")
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _decode_record(type_name: str, value: Any, where: str) -> Any:
|
|
156
|
+
if not isinstance(value, dict):
|
|
157
|
+
raise invalid_params(f"{where}: expected an object shaped as {type_name}")
|
|
158
|
+
fields = TABLE["records"][type_name]
|
|
159
|
+
known = {name for name, _, _ in fields}
|
|
160
|
+
unknown = sorted(set(value) - known)
|
|
161
|
+
if unknown:
|
|
162
|
+
raise invalid_params(f"{where}: {type_name} has no field {unknown[0]!r}")
|
|
163
|
+
kwargs: dict[str, Any] = {}
|
|
164
|
+
for name, field_type, has_default in fields:
|
|
165
|
+
if name in value:
|
|
166
|
+
kwargs[name] = decode(field_type, value[name], f"{where}.{name}")
|
|
167
|
+
elif has_default:
|
|
168
|
+
# Left to the definition's own default (C6): the server never
|
|
169
|
+
# substitutes a literal for a field the client did not send.
|
|
170
|
+
continue
|
|
171
|
+
elif field_type.endswith("?"):
|
|
172
|
+
kwargs[name] = None
|
|
173
|
+
else:
|
|
174
|
+
raise invalid_params(f"{where}: {type_name} requires {name}")
|
|
175
|
+
return getattr(generated, type_name)(**kwargs)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
# -- encoding (the binding's values -> JSON) ----------------------------------
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def encode(type_name: str, value: Any) -> Any:
|
|
182
|
+
"""``value`` as returned by the binding for ``type_name``, as JSON."""
|
|
183
|
+
if value is None:
|
|
184
|
+
return None
|
|
185
|
+
if type_name.endswith("?"):
|
|
186
|
+
return encode(type_name[:-1], value)
|
|
187
|
+
if type_name == "void":
|
|
188
|
+
return None
|
|
189
|
+
if type_name in ("string", "boolean") or type_name in _INT_RANGES or type_name in _FLOATS:
|
|
190
|
+
return value
|
|
191
|
+
if type_name == "bytes":
|
|
192
|
+
return base64.b64encode(bytes(value)).decode("ascii")
|
|
193
|
+
if type_name == "sequence<u8>":
|
|
194
|
+
return base64.b64encode(bytes(value)).decode("ascii")
|
|
195
|
+
if type_name.startswith("sequence<"):
|
|
196
|
+
inner = type_name[len("sequence<") : -1]
|
|
197
|
+
return [encode(inner, item) for item in value]
|
|
198
|
+
if type_name.startswith("record<DOMString,"):
|
|
199
|
+
inner = type_name[len("record<DOMString,") : -1]
|
|
200
|
+
return {key: encode(inner, item) for key, item in value.items()}
|
|
201
|
+
if type_name in TABLE["enums"]:
|
|
202
|
+
if isinstance(value, enum.Enum):
|
|
203
|
+
return TABLE["enums"][type_name][value.value]
|
|
204
|
+
return TABLE["enums"][type_name][int(value)]
|
|
205
|
+
if type_name in TABLE["records"]:
|
|
206
|
+
return {
|
|
207
|
+
name: encode(field_type, getattr(value, name))
|
|
208
|
+
for name, field_type, _ in TABLE["records"][type_name]
|
|
209
|
+
}
|
|
210
|
+
raise RpcError(INTERNAL_ERROR, f"a result of type {type_name} has no JSON form")
|