-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.py
More file actions
34 lines (25 loc) · 1.05 KB
/
serve.py
File metadata and controls
34 lines (25 loc) · 1.05 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
"""
Minimal HTTP server with Cross-Origin Isolation headers.
These headers enable SharedArrayBuffer, which Pyodide needs for efficient
WASM memory management. Without them, Pyodide uses a fallback that roughly
doubles memory usage, often causing STATUS_ACCESS_VIOLATION crashes.
Usage:
python serve.py [port]
# Default: http://localhost:8080
"""
import sys
from functools import partial
from http.server import HTTPServer
from http.server import SimpleHTTPRequestHandler
class COOPCOEPHandler(SimpleHTTPRequestHandler):
"""HTTP request handler with COOP and COEP headers.
Args:
SimpleHTTPRequestHandler (_type_): _description_
"""
def end_headers(self):
"""Add COOP and COEP headers to enable cross-origin isolation."""
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "credentialless")
super().end_headers()
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8080
HTTPServer(("", port), partial(COOPCOEPHandler, directory=".")).serve_forever()