refactor: 重构
This commit is contained in:
2
.npmrc
Normal file
2
.npmrc
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
registry=http://localhost:8787/_debug
|
||||||
|
shamefully-hoist=true
|
2272
_registry-json/@vue%2Fbabel-plugin-jsx.json
Normal file
2272
_registry-json/@vue%2Fbabel-plugin-jsx.json
Normal file
File diff suppressed because one or more lines are too long
@ -10,4 +10,4 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"wrangler": "^3.60.3"
|
"wrangler": "^3.60.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
9
request.http
Normal file
9
request.http
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
###
|
||||||
|
GET http://localhost:8787/_debug
|
||||||
|
|
||||||
|
|
||||||
|
###
|
||||||
|
GET http://localhost:8787/_debug/@vue%2Fbabel-plugin-jsx
|
||||||
|
|
||||||
|
###
|
||||||
|
GET http://localhost:8787/_debug/@vue/babel-plugin-jsx/-/babel-plugin-jsx-1.0.0-rc.2.tgz
|
13
src/package-lock.json
generated
13
src/package-lock.json
generated
@ -1,13 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "npmproxy",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"lockfileVersion": 3,
|
|
||||||
"requires": true,
|
|
||||||
"packages": {
|
|
||||||
"": {
|
|
||||||
"name": "npmproxy",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"license": "ISC"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "npmproxy",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"license": "ISC"
|
|
||||||
}
|
|
49
src/worker copy.js
Normal file
49
src/worker copy.js
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
const CUSTOM_DOMAIN = "https://npm-cf-proxy.oo1.dev";
|
||||||
|
const NPM_JS = "https://registry.npmjs.org";
|
||||||
|
// https://github.com/yaming116/npm-registry-worker
|
||||||
|
|
||||||
|
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 ${url.origin} 'pnpm config set registry ${url.origin}' `
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.pathname.endsWith(".tgz") || url.pathname.split("/").length === 2) {
|
||||||
|
let response = await fetch(`${NPM_JS}${url.pathname}`);
|
||||||
|
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);
|
||||||
|
},
|
||||||
|
};
|
@ -1,60 +1,40 @@
|
|||||||
/**
|
let NPM_JS = "https://registry.npmjs.org";
|
||||||
* 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://npm-cf-proxy.oo1.dev";
|
|
||||||
const NPM_JS = "https://registry.npmjs.org";
|
|
||||||
// https://github.com/yaming116/npm-registry-worker
|
|
||||||
|
|
||||||
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 {
|
export default {
|
||||||
// The fetch handler is invoked when this worker receives a HTTP(S) request
|
async fetch(request, env, ctx) {
|
||||||
// and should return a Response (optionally wrapped in a Promise)
|
const url = new URL(request.url);
|
||||||
async fetch(request, env, ctx) {
|
// console.log('url :>> ', url);
|
||||||
// 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 ${url.origin} 'pnpm config set registry ${url.origin}' `
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (url.pathname.endsWith(".tgz") || url.pathname.split("/").length === 2) {
|
let origin = url.origin;
|
||||||
let response = await fetch(`${NPM_JS}${url.pathname}`);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
let data = await (await fetch(`${NPM_JS}${url.pathname}`)).json();
|
// registry=http://localhost:8787/_debug
|
||||||
if (data.error) {
|
if (url.pathname.startsWith('/_debug')) {
|
||||||
return json(data, 500);
|
url.pathname = url.pathname.replace('/_debug', '');
|
||||||
}
|
NPM_JS = "https://registry.npmmirror.com";
|
||||||
|
origin = `http://localhost:8787/_debug`;
|
||||||
|
}
|
||||||
|
|
||||||
if (data && data.dist && data.dist.tarball) {
|
if ("/" === url.pathname) {
|
||||||
data.dist.tarball = data.dist.tarball.replace(NPM_JS, CUSTOM_DOMAIN);
|
return new Response(`Hello ${url.origin} 'pnpm config set registry ${url.origin}' `);
|
||||||
}
|
}
|
||||||
return json(data);
|
if ("GET" !== request.method) {
|
||||||
},
|
return new Response('Method not allowed', { status: 405, });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.pathname.endsWith(".tgz")) {
|
||||||
|
return fetch(`${NPM_JS}${url.pathname}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('url.pathname :>> ', url.pathname); // >> /@vue%2Fbabel-plugin-jsx
|
||||||
|
// https://registry.npmjs.org/@vue%2Fbabel-plugin-jsx
|
||||||
|
// https://registry.npmjs.org/@vue/babel-plugin-jsx/-/babel-plugin-jsx-1.0.0-rc.2.tgz
|
||||||
|
|
||||||
|
let response = await fetch(`${NPM_JS}${url.pathname}`);
|
||||||
|
let text = await response.text()
|
||||||
|
text = text.replace(new RegExp(`${NPM_JS}(.*?).tgz`, 'g'), `${origin}$1.tgz`);
|
||||||
|
// console.log('text :>> ', text);
|
||||||
|
return new Response(text, response);
|
||||||
|
|
||||||
|
return new Response(`This is a worker for ${url.origin}`, { status: 500, });
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
name = "npmproxy"
|
|
||||||
main = "worker.js"
|
|
||||||
compatibility_date = "2023-08-23"
|
|
Reference in New Issue
Block a user