http.response.start goes AFTER http.response.body

This commit is contained in:
Yusur 2025-08-10 11:18:12 +02:00
parent 9cd2345c80
commit b035c86b31

View file

@ -11,7 +11,7 @@ from sass import CompileError
from sassutils.builder import Manifest from sassutils.builder import Manifest
from importlib.metadata import version as _get_version from importlib.metadata import version as _get_version
from .codecs import quote_css_string from .codecs import quote_css_string, want_bytes
from .validators import must_be from .validators import must_be
from .asgi import _MiddlewareFactory, ASGIApp, ASGIReceive, ASGIScope, ASGISend from .asgi import _MiddlewareFactory, ASGIApp, ASGIReceive, ASGIScope, ASGISend
from . import __version__ as _suou_version from . import __version__ as _suou_version
@ -83,13 +83,6 @@ class SassAsyncMiddleware(_MiddlewareFactory):
break break
except CompileError as e: except CompileError as e:
logger.error(str(e)) logger.error(str(e))
await send({
'type': 'http.response.start',
'status': self.error_status,
'headers': [
(b'Content-Type', b'text/css; charset=utf-8'),
]
})
await send({ await send({
'type': 'http.response.body', 'type': 'http.response.body',
'body': '\n'.join([ 'body': '\n'.join([
@ -110,6 +103,13 @@ class SassAsyncMiddleware(_MiddlewareFactory):
'}' '}'
]).encode('utf-8') ]).encode('utf-8')
}) })
await send({
'type': 'http.response.start',
'status': self.error_status,
'headers': [
(b'Content-Type', b'text/css; charset=utf-8'),
]
})
async def _read_file(path): async def _read_file(path):
with open(path, 'rb') as f: with open(path, 'rb') as f:
@ -120,22 +120,19 @@ class SassAsyncMiddleware(_MiddlewareFactory):
else: else:
break break
resp_body = b''
async for chunk in _read_file(os.path.join(package_dir, result)): async for chunk in _read_file(os.path.join(package_dir, result)):
resp_body += chunk await send({
'type': 'http.response.body',
'body': chunk
})
await send({ await send({
'type': 'http.response.start', 'type': 'http.response.start',
'status': 200, 'status': 200,
'headers': [ 'headers': [
(b'Content-Type', b'text/css; charset=utf-8'), (b'Content-Type', b'text/css; charset=utf-8')
] ]
}) })
await send({
'type': 'http.response.body',
'body': resp_body
})
await self.app(scope, receive, send) await self.app(scope, receive, send)