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) {
|
async function handleRequest(request) {
|
||||||
const url = new URL(request.url);
|
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 baseHostname = 'YOURWORKER.YOURUSER.workers.dev';
|
||||||
const baseMappingPath = '/examplebase/';
|
|
||||||
const testFilesPath = '/testfiles/';
|
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
|
// Check if the path matches any of the desired formats
|
||||||
// Add ifs as needed
|
if (url.hostname === baseHostname && (url.pathname.startsWith(v10MappingPath) || url.pathname.startsWith(testFilesPath) || url.pathname.startsWith(v11MappingPath) || url.pathname.startsWith(v12MappingPath) || url.pathname.startsWith(v13MappingPath))) {
|
||||||
if (url.hostname === baseHostname && (url.pathname.startsWith(baseMappingPath) || url.pathname.startsWith(testFilesPath))) {
|
let subpath = '';
|
||||||
const subpath = url.pathname.startsWith(baseMappingPath)
|
|
||||||
? url.pathname.substring(baseMappingPath.length) // Extract the subpath for mappings
|
if (url.pathname.startsWith(v10MappingPath)) {
|
||||||
: url.pathname.substring(testFilesPath.length); // Extract the subpath for test files
|
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
|
// Define the mapping of subpaths to redirect URLs
|
||||||
const baseTargetUrls = {
|
const v10TargetUrls = {
|
||||||
'x86_64.iso': 'https://example.com/x86_64.iso',
|
'v1.0.zip': 'https://foo.bar/v1.0.zip',
|
||||||
'i686.iso': 'https://example.com/i686.iso',
|
'v1.0-i686.zip': 'https://foo.bar/v1.0.i686.zip',
|
||||||
// Add more mappings here if needed
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Check if the subpath exists in the mapping
|
const v11TargetUrls = {
|
||||||
if (baseTargetUrls[subpath]) {
|
'v1.1.zip': 'https://foo.bar/v1.1.zip',
|
||||||
const targetUrl = baseTargetUrls[subpath];
|
'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);
|
return fetch(targetUrl, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the subpath exists in test files
|
|
||||||
const testFileUrls = {
|
const testFileUrls = {
|
||||||
'example': 'https://example.com'
|
'example.png': 'https://foo.bar/example.png',
|
||||||
// Add more test file mappings here if needed
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (testFileUrls[subpath]) {
|
if (testFileUrls[subpath]) {
|
||||||
|
|
Reference in New Issue