|
| 1 | +--- |
| 2 | +template: pyodide.html |
| 3 | +--- |
| 4 | + |
| 5 | +# Emscripten Support |
| 6 | + |
| 7 | +httpx2 has support for running on WebAssembly / Emscripten using |
| 8 | +[Pyodide](https://github.com/pyodide/pyodide/). |
| 9 | + |
| 10 | +Asynchronous requests always use `fetch`. Synchronous requests use the following |
| 11 | +methods: |
| 12 | +1. If [Javascript Promise Integration](https://github.com/WebAssembly/js-promise-integration/blob/main/proposals/js-promise-integration/Overview.md) |
| 13 | + (JSPI) is supported by the JavaScript runtime, the request will be made with |
| 14 | + `fetch` and stack switching. |
| 15 | +2. Otherwise, if in a browser, the request will be made using a synchronous |
| 16 | + `XMLHttpRequest`. |
| 17 | +3. Otherwise, if in Node, the request will fail. Synchronous requests in Node |
| 18 | + require JSPI. |
| 19 | + |
| 20 | +In Emscripten, all network connections are handled by the enclosing Javascript |
| 21 | +runtime. As such, there is limited control over various features. In particular: |
| 22 | + |
| 23 | +- Proxy servers are handled by the runtime, so httpx2 cannot control them. |
| 24 | +- httpx2 has no control over connection pooling. |
| 25 | +- Certificate handling is done by the browser, so httpx2 cannot modify it. |
| 26 | +- Requests are constrained by cross-origin isolation settings in the same way as |
| 27 | + any request that is originated by Javascript code. |
| 28 | +- Timeouts will not work in the main browser thread unless the browser supports |
| 29 | + JSPI because main thread synchronous `XMLHttpRequest` does not support |
| 30 | + timeouts. |
| 31 | + |
| 32 | +Setting any of the transport options that depend on these features (`verify`, |
| 33 | +`cert`, `http2`, `limits`, `proxy`, `uds`, `local_address`, `retries`, or |
| 34 | +`socket_options`) will emit a `UserWarning` and the option will be silently |
| 35 | +ignored. |
| 36 | + |
| 37 | +## Try it in your browser |
| 38 | + |
| 39 | +Use the following live example to test httpx2 in your web browser. You can |
| 40 | +change the code below and hit run again to test different features or web |
| 41 | +addresses. |
| 42 | + |
| 43 | +<div id="pyodide_editor">import httpx2 |
| 44 | +print("Sending response using httpx2 in the browser:") |
| 45 | +print("--------------------------------------------") |
| 46 | +r = httpx2.get("http://www.example.com") |
| 47 | +print("Status = ", r.status_code) |
| 48 | +print("Response = ", r.text[:50], "...")</div> |
| 49 | + |
| 50 | +<div id="pyodide_output"></div> |
| 51 | + |
| 52 | +<div id="pyodide_buttons"></div> |
| 53 | + |
| 54 | +## Build it |
| 55 | + |
| 56 | +Because `httpx2` is a pure python module, building is the same as ever |
| 57 | +(`python -m build`), or use the built wheel from PyPI. |
| 58 | + |
| 59 | +## Testing Custom Builds of httpx2 in Emscripten |
| 60 | + |
| 61 | +Once you have a wheel you can test it in your browser. You can do this using the |
| 62 | +[Pyodide console](https://pyodide.org/en/stable/console.html), or by hosting |
| 63 | +your own web page. You will need version 0.26.2 or later of Pyodide. |
| 64 | + |
| 65 | +1. To test in Pyodide console, serve the wheel file via http (e.g. by calling |
| 66 | + python -m `http.server` in the dist directory.) Then in the [Pyodide |
| 67 | + console](https://pyodide.org/en/stable/console.html), type the following, |
| 68 | + replacing the URL of the locally served wheel. |
| 69 | + |
| 70 | + ```python |
| 71 | + import pyodide_js as pjs |
| 72 | + import ssl, certifi, idna |
| 73 | + pjs.loadPackage("<URL_OF_THE_WHEEL>") |
| 74 | + import httpx2 |
| 75 | + # Now httpx2 should work |
| 76 | + ``` |
| 77 | + |
| 78 | +2. To test a custom-built wheel in your own web page, create a page which loads |
| 79 | + the Pyodide JavaScript (see the |
| 80 | + [instructions](https://pyodide.org/en/stable/usage/index.html) on the |
| 81 | + Pyodide website). After starting the Pyodide runtime, run the following code |
| 82 | + to load httpx2 and its dependencies: |
| 83 | + ```js |
| 84 | + await pyodide.loadPackage([httpx2_wheel_url, "ssl", "certifi", "idna"]) |
| 85 | + ``` |
| 86 | + |
| 87 | +3. To test in Node.js, run `npm i pyodide` or download a Pyodide distribution |
| 88 | + download to a known folder, then load Pyodide following the instructions on |
| 89 | + the Pyodide website (https://pyodide.org/en/stable/usage/index.html). After |
| 90 | + starting the Pyodide runtime, run the following code to load httpx2 and its |
| 91 | + dependencies: |
| 92 | + ```js |
| 93 | + await pyodide.loadPackage([httpx2_wheel_url, "ssl", "certifi", "idna"]) |
| 94 | + ``` |
0 commit comments