-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathasgi.py
More file actions
315 lines (254 loc) · 9.26 KB
/
Copy pathasgi.py
File metadata and controls
315 lines (254 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import logging
from asyncio import Event, Future, Queue, create_task, ensure_future, sleep
from collections.abc import Awaitable
from contextlib import contextmanager
from inspect import isawaitable
from typing import Any
import js
from workers import Context, Request
ASGI = {"spec_version": "2.0", "version": "3.0"}
logger = logging.getLogger("asgi")
background_tasks = set()
def run_in_background(coro: Awaitable[Any]) -> None:
fut = ensure_future(coro)
background_tasks.add(fut)
def _on_done(f):
background_tasks.discard(f)
exc = f.exception() if not f.cancelled() else None
if exc is not None:
logger.error("Unhandled exception in background task", exc_info=exc)
fut.add_done_callback(_on_done)
@contextmanager
def acquire_js_buffer(pybuffer):
from pyodide.ffi import create_proxy
px = create_proxy(pybuffer)
buf = px.getBuffer()
px.destroy()
try:
yield buf.data
finally:
buf.release()
def request_to_scope(req, env, ws=False):
from js import URL
# @app.get("/example")
# async def example(request: Request):
# request.headers.get("content-type")
# - this will error if header is not "bytes" as in ASGI spec.
# Support both JS and Python http.client.HTTPMessage headers.
req_headers = req.headers.items() if isinstance(req, Request) else req.headers
headers = [(k.lower().encode(), v.encode()) for k, v in req_headers]
url = URL.new(req.url)
assert url.protocol[-1] == ":"
scheme = url.protocol[:-1]
path = url.pathname
assert "?".startswith(url.search[0:1])
query_string = url.search[1:].encode()
if ws:
ty = "websocket"
else:
ty = "http"
return {
"asgi": ASGI,
"headers": headers,
"http_version": "1.1",
"method": req.method,
"scheme": scheme,
"path": path,
"query_string": query_string,
"type": ty,
"env": env,
}
async def start_application(app):
shutdown_future = Future()
async def shutdown():
shutdown_future.set_result(None)
await sleep(0)
it = iter([{"type": "lifespan.startup"}, Future()])
async def receive():
res = next(it)
if isawaitable(res):
await res
return res
ready = Future()
async def send(got):
if got["type"] == "lifespan.startup.complete":
ready.set_result(None)
return
if got["type"] == "lifespan.shutdown.complete":
return
raise RuntimeError(f"Unexpected lifespan event {got['type']}")
run_in_background(
app(
{
"asgi": ASGI,
"state": {},
"type": "lifespan",
},
receive,
send,
)
)
await ready
return shutdown
async def process_request(
app: Any,
req: "Request | js.Request",
env: Any,
# added for waitUntil, but not used anymore
# TODO(later): remove this parameter after unvendoring Python SDK from workerd
ctx: Context | None,
) -> js.Response:
from js import Object, Response, TransformStream
from pyodide.ffi import create_proxy
status = None
headers = None
result = Future()
finished_response = Event()
# Streaming state — initialized lazily on first body chunk with more_body=True.
writer = None
receive_queue = Queue()
if req.body:
async for data in req.body:
await receive_queue.put(
{
"body": data.to_bytes(),
"more_body": True,
"type": "http.request",
}
)
await receive_queue.put({"body": b"", "more_body": False, "type": "http.request"})
async def receive():
message = None
if not receive_queue.empty():
message = await receive_queue.get()
else:
await finished_response.wait()
message = {"type": "http.disconnect"}
return message
async def send(got):
nonlocal status
nonlocal headers
nonlocal writer
if got["type"] == "http.response.start":
status = got["status"]
# Like above, we need to convert byte-pairs into string explicitly.
headers = [(k.decode(), v.decode()) for k, v in got["headers"]]
elif got["type"] == "http.response.body":
body = got["body"]
more_body = got.get("more_body", False)
if writer is not None:
# Already in streaming mode — write chunk to the stream.
with acquire_js_buffer(body) as jsbytes:
await writer.write(jsbytes.slice())
if not more_body:
await writer.close()
finished_response.set()
elif more_body:
# First body chunk with more data coming — switch to streaming.
# Create a TransformStream so the runtime can start consuming
# body chunks as they are written.
transform_stream = TransformStream.new()
readable = transform_stream.readable
writer = transform_stream.writable.getWriter()
resp = Response.new(
readable, headers=Object.fromEntries(headers), status=status
)
result.set_result(resp)
with acquire_js_buffer(body) as jsbytes:
await writer.write(jsbytes.slice())
else:
# Complete body in a single chunk
px = create_proxy(body)
buf = px.getBuffer()
px.destroy()
resp = Response.new(
buf.data, headers=Object.fromEntries(headers), status=status
)
result.set_result(resp)
finished_response.set()
# Run the application in the background
async def run_app():
try:
await app(request_to_scope(req, env), receive, send)
# If we get here and no response has been set yet, the app didn't generate a response
if not result.done():
raise RuntimeError("The application did not generate a response") # noqa: TRY301
except Exception as e:
if not result.done():
result.set_exception(e)
if writer is not None:
await writer.close()
finished_response.set()
else:
# Response already sent — exception can't be propagated to the
# client, so log it to avoid silently swallowing errors.
logger.exception("Exception in ASGI application after response started")
# Create task to run the application in the background
app_task = create_proxy(create_task(run_app()))
from workers import wait_until
wait_until(app_task)
try:
return await result
finally:
app_task.destroy()
async def process_websocket(app: Any, req: "Request | js.Request") -> js.Response:
from js import Response, WebSocketPair
client, server = WebSocketPair.new().object_values()
server.accept()
queue = Queue()
def onopen(evt):
msg = {"type": "websocket.connect"}
queue.put_nowait(msg)
# onopen doesn't seem to get called. WS lifecycle events are a bit messed up
# here.
onopen(1)
def onclose(evt):
msg = {"type": "websocket.close", "code": evt.code, "reason": evt.reason}
queue.put_nowait(msg)
def onmessage(evt):
msg = {"type": "websocket.receive", "text": evt.data}
queue.put_nowait(msg)
server.onopen = onopen
server.onopen = onclose
server.onmessage = onmessage
async def ws_send(got):
if got["type"] == "websocket.send":
b = got.get("bytes", None)
s = got.get("text", None)
if b:
with acquire_js_buffer(b) as jsbytes:
# Unlike the `Response` constructor, server.send seems to
# eagerly copy the source buffer
server.send(jsbytes)
if s:
server.send(s)
else:
logger.warning(" == Not implemented %s", got["type"])
async def ws_receive():
received = await queue.get()
return received
env = {}
run_in_background(app(request_to_scope(req, env, ws=True), ws_receive, ws_send))
return Response.new(None, status=101, webSocket=client)
async def fetch(
app: Any, req: "Request | js.Request", env: Any, ctx: Context | None = None
) -> js.Response:
logger.debug("ASGI request: %s %s", req.method, req.url)
shutdown = await start_application(app)
try:
result = await process_request(app, req, env, ctx)
except Exception:
logger.exception("ASGI request failed")
raise
await shutdown()
return result
async def websocket(app: Any, req: "Request | js.Request") -> js.Response:
return await process_websocket(app, req)
def __getattr__(name):
if name == "env":
from fastapi import Depends, Request
@Depends
async def env(request: Request):
return request.scope["env"]
return env
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")