使用大善人加速 npm registry

大善人默认分配的域名没法用,国内基本访问不了。需要使用自己的域名托管的cf,
然后修改 CUSTOM_DOMAIN 为你自己的域名即可,部署完成之后访问出现这样的内容则为成功:

不放心也可以访问一下 axios的包看看相应内容

github 地址, 里面主要就是下面js的内容

/**
 * Welcome to Cloudflare Workers! This is your first worker.
 *
 * - Run `npm run dev` in your terminal to start a development server
 * - Open a browser tab at http://localhost:8787/ to see your worker in action
 * - Run `npm run deploy` to publish your worker
 *
 * Learn more at https://developers.cloudflare.com/workers/
 */

// https://registry.npmjs.org/axios/0.2.0
const CUSTOM_DOMAIN = 'https://npmproxy.wxhome.onflashdrive.app';
const NPM_JS = 'https://registry.npmjs.org';

const json = (data = {}, code = 200, headers = {}) => {
	return new Response(JSON.stringify(data), {
		status: code,
		headers: {
			'Content-Type': 'application/json',
			'Access-Control-Allow-Origin': '*',
			'X-Powered-By': 'X-Powered-By cloudflare npm proxy',
			...headers,
		},
	});
};

// Export a default object containing event handlers
export default {
	// The fetch handler is invoked when this worker receives a HTTP(S) request
	// and should return a Response (optionally wrapped in a Promise)
	async fetch(request, env, ctx) {
		// You'll find it helpful to parse the request.url string into a URL object. Learn more at https://developer.mozilla.org/en-US/docs/Web/API/URL
		const url = new URL(request.url);
		if ('GET' !== request.method) {
			return Response.redirect(`${NPM_JS}${url.pathname}`, 302);
		}
		// console.log('xxx: ', url);
		if ('/' === url.pathname) {
			return new Response(`Hello ${CUSTOM_DOMAIN}`);
		}

		if (url.pathname.endsWith('.tgz')) {
			let response = await fetch(`${NPM_JS}${url.pathname}`);
			const contentType = response.headers.get('content-type');
			if (contentType && contentType.indexOf('application/json') !== -1) {
				return json({ error: `NPM fetch failed: ${(await response.json()).message}` }, 500);
			} else {
				return response;
			}
		}

		let data = await (await fetch(`${NPM_JS}${url.pathname}`)).json();
		if (data.error) {
			return json(data, 500);
		}

		if (data && data.dist && data.dist.tarball) {
			data.dist.tarball = data.dist.tarball.replace(NPM_JS, CUSTOM_DOMAIN);
		}
		return json(data);
	},
};
11 个赞

淘宝不香吗?

3 个赞

前几天淘宝出现过一次https ssl 过期,不知道是不是我本地代理的问题还是??!

3 个赞

淘宝镜像换域名了,换成npmmirror了

3 个赞

我只能说淘宝那个一言难尽,有时候会有各种奇葩问题,还有不是有所得包都会及时同步

5 个赞

或者用清华的?

3 个赞

学习了,谢谢

3 个赞

感谢分享

2 个赞

清华的有npm镜像嘛?

2 个赞

常规话题软件分享

感谢

1 个赞