A reverse proxy to mirror HuggingFace models and datasets on Cloudflare Workers.
- Fork this repository.
- Get an API key from Cloudflare and set it as a secret named
CLOUDFLARE_API_TOKENin your repository. - Change name in the
wrangler.tomlfile to use your own name for the worker. - [Optional] Add custom domain(s) to the workers.
This repo provides two ways of proxy:
- Basic (Default): Hostname that ends with
.hf.cois excluded. All redirection response to*.hf.cowill be returned as is. - Proxy-All: All requests are proxied.
- Traditional: Follow 302 Location to redirect LFS.
- CAS-XET: Use CAS-XET reconstruction service to proxy LFS.
Caution
Support of CAS-XET is experimental and likely to be troublesome.
PROXY_ALL_HOST: Switch between Basic and Proxy-All mode. Requiring custom domainhf-proxy.<domain>.USE_CAS_XET: When in Proxy-All mode, switch between Traditional and CAS-XET method for LFS proxy. Requiring custom domaincas-xet.<domain>.
See worker-configuration.d.ts for detailed descriptions.
Tip
For example, if your custom domain is https://your.domain,
the Proxy-All mode requires https://hf-proxy.your.domain,
and the CAS-XET method requires https://cas-xet.your.domain.
Just set environment variable HF_ENDPOINT to your domain, such as https://your.domain.
Huggingface clients expect Content-Length headers for all responses, including HEAD requests.
However, there is no way to set Content-Length headers in Cloudflare Workers as far as I know.
Nevertheless, the clients treat X-Linked-Size the same as Content-Length, so this project sets X-Linked-Size headers instead.
The other problem is the Content-Length header from upstream response.
It seems that Cloudflare Workers could not receive Content-Length headers from upstream responses.
This issue is particularly weird that it only happens in deployments, but not in online debugging environment.
As a workaround, this project receives the whole response body to calculate the content length, which may introduce performance overhead (but these files are usually very small).
For HEAD requests, the project makes a separate GET request to get the content length.
Copyright 2024-2025 BrandonStudio.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.