Files
chsrc/doc/11-如何设置换源链接与测速链接.md
2026-01-21 18:12:38 +08:00

3.6 KiB
Raw Blame History

新增/修改 "换源链接"

_prelude() 函数中,在 def_sources_begin() 的位置的第二列,新增/修改你想要替换的链接。

注: 以下代码仅起解释作用JavaScript换源的真正实现可能不同

def_sources_begin()
{&UpstreamProvider, "https://registry.npmjs.org/",                     FeedByPrelude},
{&NpmMirror,        "https://registry.npmmirror.com",                  FeedByPrelude},
{&Huawei,           "https://mirrors.huaweicloud.com/repository/npm/", FeedByPrelude},
{&Tencent,          "https://mirrors.cloud.tencent.com/npm/",          FeedByPrelude},
{&某新镜像站,       "某镜像站提供的换源链接"                         FeedByPrelude}
def_sources_end()

//

// 调整上述某一个镜像站的所提供源的 "换源链接"
chef_set_repoURL (this, &UpstreamProvider, "新的换源链接");

设置/修改 "测速链接"

_prelude() 函数中,在 def_sources_begin() 的位置的第三列,设置/修改你想要替换的链接。

  1. 在下面代码的第一行中,我们直接给了一个链接,这就是精准测速链接

  2. 在下面代码的第二行和第三行中,我们都设置了一个宏 DelegateToMirror,这意味着我们没有直接提供精准测速链接,而是让 chsrc 去测试其对应镜像站定义的测速链接

    • NpmMirror 是专用镜像站,所以其测速链接被设置为了 ACCURATE,即精准测速
    • Huawei 是通用镜像站,所以其测速链接被设置为了 ROUGH ,即模糊测速
  3. 在下面代码的第四行中,我们设置了一个宏 FeedByPrelude,这意味着我们将在 _prelude() 函数的后续动态计算 URL

注: 以下代码仅起解释作用JavaScript换源的真正实现可能不同

def_sources_begin()
{&UpstreamProvider, "https://registry.npmjs.org/",                     "https://registry.npmjs.org/BigFile.tar.gz"},
{&NpmMirror,        "https://registry.npmmirror.com",                  DelegateToMirror},
{&Huawei,           "https://mirrors.huaweicloud.com/repository/npm/", DelegateToMirror},
{&Tencent,          "https://mirrors.cloud.tencent.com/npm/",          FeedByPrelude},
def_sources_end()

//

// 调整/设置上述某一个镜像站的所提供源的 "精准测速链接"
chef_set_smURL (this, &UpstreamProvider, "新的测速链接");
// 设置上述某一个镜像站的所提供源的 "精准测速链接" 设置为 "换源链接" + postfix
chef_set_smURL_with_postfix (this, &UpstreamProvider, "/dir/BigFile.tar.gz");
// 基于 "换源链接" 做更自定义的操作
chef_set_smURL_with_func (this, &UpstreamProvider, func, data);

// 把所有上述源中还未设置 "精准测速链接" 的源进行设置
chef_set_rest_smURL_with_postfix (this, "/dir/BigFile.tar.gz");
chef_set_rest_smURL_with_func (this, func, data);

// 调整某一个镜像站(Provider)的 "测速链接"
chef_set_provider_smURL (&Tencent, "https://mirrors.cloud.tencent.com/npm/BigFile.tar.gz");
// 调整某一个镜像站(Provider)的 "测速精度"
chef_set_provider_sm_accuracy (&UpstreamProvider, ROUGH);