Skip to content

Commit

Permalink
add disable-compres-response-body cli args; add compress middleware; (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
KarryCharon authored Feb 2, 2025
1 parent 9e1d301 commit 24d6871
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions comfy/cli_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ def is_valid_directory(path: Optional[str]) -> Optional[str]:

parser.add_argument("--user-directory", type=is_valid_directory, default=None, help="Set the ComfyUI user directory with an absolute path. Overrides --base-directory.")

parser.add_argument("--disable-compres-response-body", action="store_true", help="Disable compressing response body.")

if comfy.options.args_parsing:
args = parser.parse_args()
else:
Expand Down
18 changes: 17 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ async def cache_control(request: web.Request, handler):
response.headers.setdefault('Cache-Control', 'no-cache')
return response


@web.middleware
async def compress_body(request: web.Request, handler):
accept_encoding = request.headers.get("Accept-Encoding", "")
response: web.Response = await handler(request)
if args.disable_compres_response_body:
return response
if not isinstance(response, web.Response):
return response
if response.content_type not in ["application/json", "text/plain"]:
return response
if response.body and "gzip" in accept_encoding:
response.enable_compression()
return response


def create_cors_middleware(allowed_origin: str):
@web.middleware
async def cors_middleware(request: web.Request, handler):
Expand Down Expand Up @@ -149,7 +165,7 @@ def __init__(self, loop):
self.client_session:Optional[aiohttp.ClientSession] = None
self.number = 0

middlewares = [cache_control]
middlewares = [cache_control, compress_body]
if args.enable_cors_header:
middlewares.append(create_cors_middleware(args.enable_cors_header))
else:
Expand Down

0 comments on commit 24d6871

Please sign in to comment.