Made easier
This commit is contained in:
parent
a94b464615
commit
33fed434f5
74
worker.js
74
worker.js
|
@ -5,35 +5,73 @@ addEventListener('fetch', event => {
|
|||
async function handleRequest(request) {
|
||||
const url = new URL(request.url);
|
||||
|
||||
// Define the base hostname and path for the mappings
|
||||
// Define the base hostname and paths for the mappings
|
||||
const baseHostname = 'YOURWORKER.YOURUSER.workers.dev';
|
||||
const baseMappingPath = '/examplebase/';
|
||||
const testFilesPath = '/testfiles/';
|
||||
const v10MappingPath = '/v1.0/';
|
||||
const v11MappingPath = '/v1.1/'; // New mapping path
|
||||
const v12MappingPath = '/v1.2/'; // New mapping path
|
||||
const v13MappingPath = '/v1.3/'; // New mapping path
|
||||
|
||||
// Check if the path matches the desired format
|
||||
// Add ifs as needed
|
||||
if (url.hostname === baseHostname && (url.pathname.startsWith(baseMappingPath) || url.pathname.startsWith(testFilesPath))) {
|
||||
const subpath = url.pathname.startsWith(baseMappingPath)
|
||||
? url.pathname.substring(baseMappingPath.length) // Extract the subpath for mappings
|
||||
: url.pathname.substring(testFilesPath.length); // Extract the subpath for test files
|
||||
// Check if the path matches any of the desired formats
|
||||
if (url.hostname === baseHostname && (url.pathname.startsWith(v10MappingPath) || url.pathname.startsWith(testFilesPath) || url.pathname.startsWith(v11MappingPath) || url.pathname.startsWith(v12MappingPath) || url.pathname.startsWith(v13MappingPath))) {
|
||||
let subpath = '';
|
||||
|
||||
if (url.pathname.startsWith(v10MappingPath)) {
|
||||
subpath = url.pathname.substring(v10MappingPath.length);
|
||||
} else if (url.pathname.startsWith(testFilesPath)) {
|
||||
subpath = url.pathname.substring(testFilesPath.length);
|
||||
} else if (url.pathname.startsWith(v11MappingPath)) {
|
||||
subpath = url.pathname.substring(v11MappingPath.length);
|
||||
} else if (url.pathname.startsWith(v12MappingPath)) {
|
||||
subpath = url.pathname.substring(v12MappingPath.length);
|
||||
} else if (url.pathname.startsWith(v13MappingPath)) {
|
||||
subpath = url.pathname.substring(v13MappingPath.length);
|
||||
}
|
||||
|
||||
// Define the mapping of subpaths to redirect URLs
|
||||
const baseTargetUrls = {
|
||||
'x86_64.iso': 'https://example.com/x86_64.iso',
|
||||
'i686.iso': 'https://example.com/i686.iso',
|
||||
// Add more mappings here if needed
|
||||
const v10TargetUrls = {
|
||||
'v1.0.zip': 'https://foo.bar/v1.0.zip',
|
||||
'v1.0-i686.zip': 'https://foo.bar/v1.0.i686.zip',
|
||||
};
|
||||
|
||||
// Check if the subpath exists in the mapping
|
||||
if (baseTargetUrls[subpath]) {
|
||||
const targetUrl = baseTargetUrls[subpath];
|
||||
const v11TargetUrls = {
|
||||
'v1.1.zip': 'https://foo.bar/v1.1.zip',
|
||||
'v1.1-i686.zip': 'https://foo.bar/v1.1.i686.zip',
|
||||
};
|
||||
|
||||
const v12TargetUrls = {
|
||||
'v1.2.zip': 'https://foo.bar/v1.2.zip',
|
||||
'v1.2-i686.zip': 'https://foo.bar/v1.2.i686.zip',
|
||||
};
|
||||
|
||||
const v13TargetUrls = {
|
||||
'v1.3.zip': 'https://foo.bar/v1.3.zip',
|
||||
'v1.3-i686.zip': 'https://foo.bar/v1.3.i686.zip',
|
||||
};
|
||||
|
||||
if (v10TargetUrls[subpath]) {
|
||||
const targetUrl = v10TargetUrls[subpath];
|
||||
return fetch(targetUrl, request);
|
||||
}
|
||||
|
||||
if (v11TargetUrls[subpath]) {
|
||||
const targetUrl = v11TargetUrls[subpath];
|
||||
return fetch(targetUrl, request);
|
||||
}
|
||||
|
||||
if (v12TargetUrls[subpath]) {
|
||||
const targetUrl = v12TargetUrls[subpath];
|
||||
return fetch(targetUrl, request);
|
||||
}
|
||||
|
||||
if (v13TargetUrls[subpath]) {
|
||||
const targetUrl = v13TargetUrls[subpath];
|
||||
return fetch(targetUrl, request);
|
||||
}
|
||||
|
||||
// Check if the subpath exists in test files
|
||||
const testFileUrls = {
|
||||
'example': 'https://example.com'
|
||||
// Add more test file mappings here if needed
|
||||
'example.png': 'https://foo.bar/example.png',
|
||||
};
|
||||
|
||||
if (testFileUrls[subpath]) {
|
||||
|
|
Reference in New Issue