-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b057f5
commit 2846d45
Showing
24 changed files
with
431 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import assert from 'node:assert/strict'; | ||
import npmrc from './npmrc.js'; | ||
|
||
// 从网络读取 npm 包 binary-mirror-config 的 package.json | ||
// 为防止网络抽风,同时从两个 URL 获取, | ||
// 一个是饿了么的 unpak 服务, | ||
// 另一个是 npm registry ,默认从 npm 提供的环境变量读取, | ||
// 如果用户用的是 yarn 之类,会读不到,那使用阿里镜像 | ||
async function getAliNpmMirrorCfg () { | ||
const controller = new AbortController(); | ||
const pkg = await Promise.any( | ||
[ | ||
'https://npm.elemecdn.com/binary-mirror-config/package.json', | ||
(process.env.npm_config_registry || 'https://registry.npmmirror.com') + '/binary-mirror-config/latest', | ||
].map(async url => { | ||
const res = await fetch( | ||
url, | ||
{ signal: controller.signal }, | ||
); | ||
assert.ok(res.ok); | ||
return res.json(); | ||
}), | ||
).catch(() => require('binary-mirror-config/package.json')).catch(() => {}); | ||
// 并发的http请求,任意一个成功后,终止其他 | ||
controller.abort(); | ||
// 为了防止他们未来修改相关代码,做容错处理 | ||
return pkg?.mirrors?.china; | ||
} | ||
|
||
// 查找阿里镜像的最新地址,防止他们将来再换域名 | ||
// 从阿里团队开发的npm包,binary-mirror-config 的 package.json 读取镜像配置,取 url 最短的那个(最短的大概率是镜像的根目录) | ||
function getAliNpmBinMirrorUrl (aliNpmBinMirrorCfg) { | ||
return aliNpmBinMirrorCfg && Object.keys(aliNpmBinMirrorCfg).map( | ||
pkg => aliNpmBinMirrorCfg[pkg], | ||
).map( | ||
pkg => pkg.remote_path && pkg.host, | ||
).filter(Boolean).reduce( | ||
(result, host) => result.length < host.length ? result : host, | ||
); | ||
} | ||
|
||
let baseUrl = 'https://cdn.npmmirror.com/binaries'; | ||
|
||
async function load () { | ||
const aliNpmBinMirrorCfg = await getAliNpmMirrorCfg(); | ||
baseUrl = getAliNpmBinMirrorUrl(aliNpmBinMirrorCfg); | ||
npmrc.registry = aliNpmBinMirrorCfg?.ENVS?.COREPACK_NPM_REGISTRY || npmrc.registry; | ||
for (const key in aliNpmBinMirrorCfg?.ENVS) { | ||
const value = aliNpmBinMirrorCfg.ENVS[key]; | ||
if (/^https?:\/\//.test(value) && !/\w+_ORG_MIRROR$/i.test(key) && !/_REGISTRY$/i.test(key)) { | ||
npmrc[key.toLowerCase().replace(/^npm_config_/, '').replaceAll('_', '-')] = value.replace(baseUrl, '{bin-mirror}'); | ||
} | ||
} | ||
return npmrc; | ||
}; | ||
|
||
await load(); | ||
|
||
export { | ||
baseUrl, | ||
npmrc, | ||
}; | ||
export default { | ||
baseUrl, | ||
npmrc, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const npmrc = { | ||
'registry': 'https://registry.npmmirror.com', | ||
'corepack-npm-registry': 'https://registry.npmmirror.com', | ||
|
||
'disturl': '{bin-mirror}/node', | ||
|
||
/* eslint no-template-curly-in-string: "off" */ | ||
'better-sqlite3-binary-host': '{bin-mirror}/better-sqlite3', | ||
'canvas-binary-host-mirror': '{bin-mirror}/node-canvas-prebuilt/v{version}', | ||
'canvas-prebuilt-binary-host-mirror': '{bin-mirror}/node-canvas-prebuilt/v{version}', | ||
'chromedriver-cdnurl': '{bin-mirror}/chromedriver', | ||
'couchbase-binary-host-mirror': '{bin-mirror}/couchbase/v{version}', | ||
'cypress-download-path-template': '{bin-mirror}/cypress/${version}/${platform}-${arch}/cypress.zip', | ||
'debug-binary-host-mirror': '{bin-mirror}/node-inspector', | ||
'electron-builder-binaries-mirror': '{bin-mirror}/electron-builder-binaries/', | ||
'electron-mirror': '{bin-mirror}/electron/', | ||
'flow-bin-binary-host-mirror': '{bin-mirror}/flow/v', | ||
'fse-binary-host-mirror': '{bin-mirror}/fsevents', | ||
'fuse-bindings-binary-host-mirror': '{bin-mirror}/fuse-bindings/v{version}', | ||
'git4win-mirror': '{bin-mirror}/git-for-windows', | ||
'gl-binary-host-mirror': '{bin-mirror}/gl/v{version}', | ||
'grpc-node-binary-host-mirror': '{bin-mirror}', | ||
'hackrf-binary-host-mirror': '{bin-mirror}/hackrf/v{version}', | ||
'keytar-binary-host': '{bin-mirror}/keytar', | ||
'leveldown-binary-host-mirror': '{bin-mirror}/leveldown/v{version}', | ||
'leveldown-hyper-binary-host-mirror': '{bin-mirror}/leveldown-hyper/v{version}', | ||
'mknod-binary-host-mirror': '{bin-mirror}/mknod/v{version}', | ||
'node-sqlite3-binary-host-mirror': '{bin-mirror}', | ||
'node-tk5-binary-host-mirror': '{bin-mirror}/node-tk5/v{version}', | ||
'nodegit-binary-host-mirror': '{bin-mirror}/nodegit/v{version}/', | ||
'nwjs-urlbase': '{bin-mirror}/nwjs/v', | ||
'operadriver-cdnurl': '{bin-mirror}/operadriver', | ||
'phantomjs-cdnurl': '{bin-mirror}/phantomjs', | ||
'playwright-download-host': '{bin-mirror}/playwright', | ||
'profiler-binary-host-mirror': '{bin-mirror}/node-inspector/', | ||
// https://github.com/puppeteer/puppeteer/commit/9758cae029f90908c4b5340561d9c51c26aa2f21 | ||
'puppeteer-download-base-url': '{bin-mirror}/chrome-for-testing', | ||
'python-mirror': '{bin-mirror}/python', | ||
'rabin-binary-host-mirror': '{bin-mirror}/rabin/v{version}', | ||
're2-download-mirror': '{bin-mirror}/node-re2', | ||
'robotjs-binary-host': '{bin-mirror}/robotjs', | ||
'sass-binary-site': '{bin-mirror}/node-sass', | ||
'saucectl-install-binary-mirror': '{bin-mirror}/saucectl', | ||
'sentrycli-cdnurl': '{bin-mirror}/sentry-cli', | ||
'sharp-binary-host': '{bin-mirror}/sharp', | ||
'sharp-libvips-binary-host': '{bin-mirror}/sharp-libvips', | ||
'sodium-prebuilt-binary-host-mirror': '{bin-mirror}/sodium-prebuilt/v{version}', | ||
'sqlite3-binary-site': '{bin-mirror}/sqlite3', | ||
'swc-binary-site': '{bin-mirror}/node-swc', | ||
'utf-8-validate-binary-host-mirror': '{bin-mirror}/utf-8-validate/v{version}', | ||
'utp-native-binary-host-mirror': '{bin-mirror}/utp-native/v{version}', | ||
'zmq-prebuilt-binary-host-mirror': '{bin-mirror}/zmq-prebuilt/v{version}', | ||
}; | ||
|
||
export { | ||
npmrc, | ||
}; | ||
export default npmrc; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.