CheeseAPI 2.0.8b2__tar.gz → 2.0.8b4__tar.gz

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.
@@ -150,7 +150,7 @@ class AppRoute(Route):
150
150
  self._routes: dict[str, dict[HTTP_METHOD_TYPE, RouteDict]] = {}
151
151
  self._patterns: list[Pattern] = [
152
152
  {
153
- 'pattern': re.compile(r'-?(0|[1-9]\d*)'),
153
+ 'pattern': re.compile(r'-?(?:0|[1-9]\d*)'),
154
154
  'weight': 5,
155
155
  'type': int,
156
156
  'key': 'int'
@@ -300,7 +300,7 @@ class SchedulerProxy:
300
300
 
301
301
  def add(self, interval_time: float, fn: Callable | None = None, *, first_run_timer: datetime.datetime | None = None, expected_run_num: int | None = None, key: str | None = None, run_type: Literal['THREAD', 'PROCESS'] = 'THREAD', args: tuple = (), kwargs: dict = {}, auto_remove: bool = False, timeout: float | None = None) -> Callable | Task:
302
302
  if fn:
303
- if not self._pubsub_ready:
303
+ if self.app.sync_server_url and not self._pubsub_ready:
304
304
  self._pubsub_ready = True
305
305
  threading.Thread(target = self._start_pubsub, args = (self.app,), daemon = True).start()
306
306
  coro = self._async_start_pubsub(self.app)
@@ -329,7 +329,7 @@ class SchedulerProxy:
329
329
 
330
330
  async def async_add(self, interval_time: float | None = None, fn: Callable | None = None, *, first_run_timer: datetime.datetime | None = None, expected_run_num: int | None = None, key: str | None = None, args: tuple = (), kwargs: dict = {}, auto_remove: bool = False, timeout: float | None = None) -> Callable | Task:
331
331
  if fn is not None:
332
- if not self._pubsub_ready:
332
+ if self.app.sync_server_url and not self._pubsub_ready:
333
333
  self._pubsub_ready = True
334
334
  threading.Thread(target = self._start_pubsub, args = (self.app,), daemon = True).start()
335
335
  coro = self._async_start_pubsub(self.app)
@@ -112,11 +112,17 @@ class WebsocketProxy:
112
112
  @staticmethod
113
113
  def _static_send(path: str, data: bytes | list | str | dict, *, websocket_key_or_keys: str | list[str] | None = None):
114
114
  if static.websocket_sync_servers is not None:
115
- data = json.dumps({
116
- 'data': data,
117
- 'key': websocket_key_or_keys,
118
- 'event': 'send'
119
- }).encode()
115
+ if isinstance(data, bytes):
116
+ data = b'\x01' + json.dumps({
117
+ 'key': websocket_key_or_keys,
118
+ 'event': 'send'
119
+ }).encode() + b'\x00' + data
120
+ else:
121
+ data = b'\x00' + json.dumps({
122
+ 'data': data,
123
+ 'key': websocket_key_or_keys,
124
+ 'event': 'send'
125
+ }).encode()
120
126
  if static.websocket_data_encode is not None:
121
127
  data = static.websocket_data_encode(data)
122
128
  redis.Redis(connection_pool = static.websocket_sync_servers[0]).publish(path, data)
@@ -137,11 +143,18 @@ class WebsocketProxy:
137
143
  @staticmethod
138
144
  async def async_send(path: str, data: bytes | list | str | dict, *, websocket_key_or_keys: str | list[str] | None = None):
139
145
  if static.websocket_sync_servers is not None:
140
- data = json.dumps({
141
- 'data': data,
142
- 'key': websocket_key_or_keys,
143
- 'event': 'send'
144
- }).encode()
146
+ if isinstance(data, bytes):
147
+ header = json.dumps({
148
+ 'key': websocket_key_or_keys,
149
+ 'event': 'send'
150
+ }).encode()
151
+ data = b'\x01' + header + b'\x00' + data
152
+ else:
153
+ data = b'\x00' + json.dumps({
154
+ 'data': data,
155
+ 'key': websocket_key_or_keys,
156
+ 'event': 'send'
157
+ }).encode()
145
158
  if static.websocket_data_encode is not None:
146
159
  data = static.websocket_data_encode(data)
147
160
  await redis.asyncio.Redis(connection_pool = static.websocket_sync_servers[1]).publish(path, data)
@@ -157,7 +170,7 @@ class WebsocketProxy:
157
170
  @staticmethod
158
171
  def _static_close(path: str, code: int = 1000, message: str = '', *, websocket_key_or_keys: str | list[str] | None = None):
159
172
  if static.websocket_sync_servers is not None:
160
- data = json.dumps({
173
+ data = b'\x00' + json.dumps({
161
174
  'key': websocket_key_or_keys,
162
175
  'event': 'close'
163
176
  }).encode()
@@ -181,7 +194,7 @@ class WebsocketProxy:
181
194
  @staticmethod
182
195
  async def async_close(path: str, code: int = 1000, message: str = '', *, websocket_key_or_keys: str | list[str] | None = None):
183
196
  if static.websocket_sync_servers is not None:
184
- data = json.dumps({
197
+ data = b'\x00' + json.dumps({
185
198
  'key': websocket_key_or_keys,
186
199
  'event': 'close'
187
200
  }).encode()
@@ -263,7 +276,14 @@ class WebsocketProxy:
263
276
  message = message['data']
264
277
  if static.websocket_data_decode is not None:
265
278
  message = static.websocket_data_decode(message)
266
- message = json.loads(message.decode())
279
+ msg_type = message[0]
280
+ if msg_type == 0:
281
+ message = json.loads(message[1:].decode())
282
+ _data = message.get('data')
283
+ else:
284
+ sep = message.index(b'\x00', 1)
285
+ _data = message[sep + 1:]
286
+ message = json.loads(message[1:sep].decode())
267
287
  if isinstance(message['key'], list):
268
288
  connectors = [connector for connector in connectors if connector.key in message['key']]
269
289
  elif isinstance(message['key'], str):
@@ -274,7 +294,7 @@ class WebsocketProxy:
274
294
  if connectors:
275
295
  if message['event'] == 'send':
276
296
  for connector in connectors:
277
- asyncio.create_task(connector.send(message['data']))
297
+ asyncio.create_task(connector.send(_data))
278
298
  elif message['event'] == 'close':
279
299
  for connector in connectors:
280
300
  asyncio.create_task(connector.close())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: CheeseAPI
3
- Version: 2.0.8b2
3
+ Version: 2.0.8b4
4
4
  Summary: 一款web协程框架
5
5
  Project-URL: Source, https://github.com/CheeseUnknown/CheeseAPI
6
6
  Author-email: Cheese Unknown <cheese@cheese.ren>
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "CheeseAPI"
7
- version = "2.0.8-beta.2"
7
+ version = "2.0.8-beta.4"
8
8
  description = "一款web协程框架"
9
9
  readme = "README.md"
10
10
  license-files = { paths = [ "LICENSE" ] }
File without changes
File without changes
File without changes