Mercurial > code > home > repos > patchablegraph
annotate patch_cyclone.py @ 23:8d6ba6d372c8
add CORS support to graph responses
author | drewp@bigasterisk.com |
---|---|
date | Sat, 23 Apr 2022 23:45:45 -0700 |
parents | 1b6718a54c00 |
children |
rev | line source |
---|---|
9
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
1 |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
2 |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
3 def patch_sse(): |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
4 from cyclone import escape |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
5 import cyclone.sse |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
6 def new_sendEvent(self, message, event=None, eid=None, retry=None): |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
7 if isinstance(message, dict): |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
8 message = escape.json_encode(message) |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
9 if isinstance(message, str): |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
10 message = message.encode("utf-8") |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
11 assert isinstance(message, bytes) |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
12 if eid: |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
13 self.transport.write(b"id: " + eid.encode("utf-8") + b"\n") |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
14 if event: |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
15 self.transport.write(b"event: " + event + b"\n") |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
16 if retry: |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
17 self.transport.write(b"retry: " + retry.encode("utf-8") + b"\n") |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
18 self.transport.write(b"data: " + message + b"\n\n") |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
19 |
1b6718a54c00
add a workaround for a str/bytes bug in cyclone SSE
drewp@bigasterisk.com
parents:
diff
changeset
|
20 cyclone.sse.SSEHandler.sendEvent = new_sendEvent |