DEV Community

MT
MT

Posted on • Originally published at chi.miantiao.me on

Cloudflare PyPI Mirror

Pyodide is a library that runs Python in WebAssembly, using Micropip to install packages from PyPI. Due to WebAssembly's requirements for CORS and PEP 691 when running in browsers, and the fact that Tsinghua's TUNA mirror doesn't support CORS, this creates some challenges.

PyPI is not directly accessible in mainland China, but there are many mirrors available. Institutions like Tsinghua University, Alibaba Cloud, Tencent Cloud, and Huawei Cloud provide mirror services. However, except for Tsinghua's TUNA mirror, none of them support the JSON-based Simple API for Python (PEP 691).

Since WebAssembly requires both CORS support and PEP 691 compliance when running in browsers, and Tsinghua's TUNA mirror doesn't support CORS, there might not be any suitable PyPI mirrors available in mainland China for Micropip.

Given this situation, I've set up a Cloudflare-based mirror that supports both PEP 691 and CORS.

You can build this using either Workers or Snippets, each with their own advantages and disadvantages:

Workers

Pros: Available with the free plan.

Cons: Generates many Worker requests, which might exceed free plan limits and require payment or become unusable.

Snippets

Pros: Doesn't generate Worker requests, supports high usage volumes.
Cons: Currently only available for Pro plans and above, not available on Free tier.

Code

The corresponding code has been open-sourced and is available at:

https://github.com/ccbikai/cloudflare-pypi-mirror

Cloudflare PyPI Mirror

stat

Top comments (0)