-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from yepcord/add-support-for-zstd-ws-compressor
Add support for zstd ws compressor
- Loading branch information
Showing
10 changed files
with
364 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
YEPCord: Free open source selfhostable fully discord-compatible chat | ||
Copyright (C) 2022-2024 RuslanUC | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
import zlib | ||
|
||
import zstandard | ||
|
||
|
||
class WsCompressor(ABC): | ||
CLSs = {} | ||
|
||
@abstractmethod | ||
def __call__(self, data: bytes) -> bytes: ... | ||
|
||
@classmethod | ||
def create_compressor(cls, name: str) -> WsCompressor | None: | ||
if name in cls.CLSs: | ||
return cls.CLSs[name]() | ||
|
||
|
||
class ZlibCompressor(WsCompressor): | ||
__slots__ = ("_obj",) | ||
|
||
def __init__(self): | ||
self._obj = zlib.compressobj() | ||
|
||
def __call__(self, data: bytes) -> bytes: | ||
return self._obj.compress(data) + self._obj.flush(zlib.Z_FULL_FLUSH) | ||
|
||
|
||
class ZstdCompressor(WsCompressor): | ||
__slots__ = ("_obj",) | ||
|
||
def __init__(self): | ||
self._obj = zstandard.ZstdCompressor().compressobj() | ||
|
||
def __call__(self, data: bytes) -> bytes: | ||
return self._obj.compress(data)# + self._obj.flush(zlib.Z_FULL_FLUSH) | ||
|
||
|
||
WsCompressor.CLSs["zlib-stream"] = ZlibCompressor | ||
WsCompressor.CLSs["zstd-stream"] = ZstdCompressor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters