Create some code
This commit is contained in:
1 parent
5b1e7c8725
commit
66d2461caf
12 files changed
+3762
No files matched your search
@@ -0,0 +1,12 @@
|
|||||||
|
# http://editorconfig.org
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_style = tab
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[*.yml]
|
||||||
|
indent_style = space
|
||||||
+172
@@ -0,0 +1,172 @@
|
|||||||
|
# Logs
|
||||||
|
|
||||||
|
logs
|
||||||
|
_.log
|
||||||
|
npm-debug.log_
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
.pnpm-debug.log*
|
||||||
|
|
||||||
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
|
|
||||||
|
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
|
||||||
|
pids
|
||||||
|
_.pid
|
||||||
|
_.seed
|
||||||
|
\*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
|
||||||
|
coverage
|
||||||
|
\*.lcov
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# Snowpack dependency directory (https://snowpack.dev/)
|
||||||
|
|
||||||
|
web_modules/
|
||||||
|
|
||||||
|
# TypeScript cache
|
||||||
|
|
||||||
|
\*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional stylelint cache
|
||||||
|
|
||||||
|
.stylelintcache
|
||||||
|
|
||||||
|
# Microbundle cache
|
||||||
|
|
||||||
|
.rpt2_cache/
|
||||||
|
.rts2_cache_cjs/
|
||||||
|
.rts2_cache_es/
|
||||||
|
.rts2_cache_umd/
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
|
||||||
|
\*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variable files
|
||||||
|
|
||||||
|
.env
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
.env.local
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
|
||||||
|
.cache
|
||||||
|
.parcel-cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
|
||||||
|
.next
|
||||||
|
out
|
||||||
|
|
||||||
|
# Nuxt.js build / generate output
|
||||||
|
|
||||||
|
.nuxt
|
||||||
|
dist
|
||||||
|
|
||||||
|
# Gatsby files
|
||||||
|
|
||||||
|
.cache/
|
||||||
|
|
||||||
|
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||||
|
|
||||||
|
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||||
|
|
||||||
|
# public
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# vuepress v2.x temp and cache directory
|
||||||
|
|
||||||
|
.temp
|
||||||
|
.cache
|
||||||
|
|
||||||
|
# Docusaurus cache and generated files
|
||||||
|
|
||||||
|
.docusaurus
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
|
||||||
|
.vscode-test
|
||||||
|
|
||||||
|
# yarn v2
|
||||||
|
|
||||||
|
.yarn/cache
|
||||||
|
.yarn/unplugged
|
||||||
|
.yarn/build-state.yml
|
||||||
|
.yarn/install-state.gz
|
||||||
|
.pnp.\*
|
||||||
|
|
||||||
|
# wrangler project
|
||||||
|
|
||||||
|
.dev.vars
|
||||||
|
.wrangler/
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"printWidth": 140,
|
||||||
|
"singleQuote": true,
|
||||||
|
"semi": true,
|
||||||
|
"useTabs": true
|
||||||
|
}
|
||||||
+132
@@ -0,0 +1,132 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Xget - Fast File Download Proxy</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
||||||
|
line-height: 1.6;
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 20px;
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
color: #2563eb;
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 40px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.platform-box {
|
||||||
|
background: #f8fafc;
|
||||||
|
border: 1px solid #e2e8f0;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 20px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
code {
|
||||||
|
background: #e2e8f0;
|
||||||
|
padding: 2px 6px;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-family: 'Courier New', Courier, monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
.example {
|
||||||
|
background: #334155;
|
||||||
|
color: #fff;
|
||||||
|
padding: 15px;
|
||||||
|
border-radius: 8px;
|
||||||
|
overflow-x: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.features {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
|
||||||
|
gap: 20px;
|
||||||
|
margin: 30px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.feature-card {
|
||||||
|
background: #fff;
|
||||||
|
border: 1px solid #e2e8f0;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.feature-card h3 {
|
||||||
|
color: #2563eb;
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>🚀 Xget</h1>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Xget is a fast and reliable file download proxy service powered by Cloudflare Workers. It provides accelerated access to files from
|
||||||
|
various platforms including GitHub, GitLab, HuggingFace, and Kaggle.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="features">
|
||||||
|
<div class="feature-card">
|
||||||
|
<h3>⚡ Fast</h3>
|
||||||
|
<p>Powered by Cloudflare's global network for optimal performance</p>
|
||||||
|
</div>
|
||||||
|
<div class="feature-card">
|
||||||
|
<h3>🛡️ Secure</h3>
|
||||||
|
<p>Built-in security headers and file type filtering</p>
|
||||||
|
</div>
|
||||||
|
<div class="feature-card">
|
||||||
|
<h3>🔄 Cached</h3>
|
||||||
|
<p>Automatic caching for faster repeated access</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2>How to Use</h2>
|
||||||
|
<p>Simply prefix your file URL with our service URL and the appropriate platform identifier:</p>
|
||||||
|
|
||||||
|
<div class="platform-box">
|
||||||
|
<h3>Supported Platforms:</h3>
|
||||||
|
<ul>
|
||||||
|
<li><code>/gh/</code> - GitHub Raw Content</li>
|
||||||
|
<li><code>/gl/</code> - GitLab Files</li>
|
||||||
|
<li><code>/hf/</code> - HuggingFace Files</li>
|
||||||
|
<li><code>/kg/</code> - Kaggle Datasets</li>
|
||||||
|
<li><code>/url/</code> - Direct URLs</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2>Examples</h2>
|
||||||
|
|
||||||
|
<h3>GitHub Example:</h3>
|
||||||
|
<div class="example">
|
||||||
|
<code>https://your-worker.workers.dev/gh/username/repo/branch/path/to/file</code>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3>GitLab Example:</h3>
|
||||||
|
<div class="example">
|
||||||
|
<code>https://your-worker.workers.dev/gl/username/repo/-/blob/branch/path/to/file</code>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3>Direct URL Example:</h3>
|
||||||
|
<div class="example">
|
||||||
|
<code>https://your-worker.workers.dev/url/https://example.com/file.zip</code>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h2>Limitations</h2>
|
||||||
|
<ul>
|
||||||
|
<li>Maximum path length: 2048 characters</li>
|
||||||
|
<li>Supported HTTP methods: GET, HEAD</li>
|
||||||
|
<li>Blocked file types: .php, .exe, .dll, .bat, .cmd, .sh</li>
|
||||||
|
<li>Cache duration: 24 hours</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<footer style="text-align: center; margin-top: 40px; padding: 20px; border-top: 1px solid #e2e8f0">
|
||||||
|
<p>Powered by Cloudflare Workers</p>
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Generated
+3085
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "xget",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"deploy": "wrangler deploy",
|
||||||
|
"dev": "wrangler dev",
|
||||||
|
"start": "wrangler dev",
|
||||||
|
"test": "vitest"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@cloudflare/vitest-pool-workers": "^0.5.2",
|
||||||
|
"wrangler": "^3.60.3",
|
||||||
|
"vitest": "2.0.5"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { PLATFORMS } from './platforms';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} SecurityConfig
|
||||||
|
* @property {string[]} ALLOWED_METHODS - List of allowed HTTP methods
|
||||||
|
* @property {string[]} ALLOWED_ORIGINS - List of allowed CORS origins
|
||||||
|
* @property {number} MAX_PATH_LENGTH - Maximum allowed URL path length
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} ApplicationConfig
|
||||||
|
* @property {number} TIMEOUT_SECONDS - Request timeout in seconds
|
||||||
|
* @property {number} MAX_RETRIES - Maximum number of retry attempts
|
||||||
|
* @property {number} RETRY_DELAY_MS - Delay between retries in milliseconds
|
||||||
|
* @property {number} CACHE_DURATION - Cache duration in seconds
|
||||||
|
* @property {SecurityConfig} SECURITY - Security-related configurations
|
||||||
|
* @property {Object} PLATFORMS - Platform-specific configurations
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @type {ApplicationConfig} */
|
||||||
|
export const CONFIG = {
|
||||||
|
TIMEOUT_SECONDS: 30,
|
||||||
|
MAX_RETRIES: 3,
|
||||||
|
RETRY_DELAY_MS: 1000,
|
||||||
|
CACHE_DURATION: 1800, // 30 minutes
|
||||||
|
SECURITY: {
|
||||||
|
ALLOWED_METHODS: ['GET', 'HEAD'],
|
||||||
|
ALLOWED_ORIGINS: ['*'],
|
||||||
|
MAX_PATH_LENGTH: 2048,
|
||||||
|
},
|
||||||
|
PLATFORMS,
|
||||||
|
};
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/**
|
||||||
|
* Configuration object for different platform URLs and path transformations
|
||||||
|
* @type {Object.<string, {base: string, transform: function(string): string}>}
|
||||||
|
*/
|
||||||
|
export const PLATFORMS = {
|
||||||
|
/** @type {{base: string, transform: function(string): string}} GitHub configuration */
|
||||||
|
gh: {
|
||||||
|
base: 'https://raw.githubusercontent.com',
|
||||||
|
transform: (path) => path.replace(/^\/gh\//, '/'),
|
||||||
|
},
|
||||||
|
/** @type {{base: string, transform: function(string): string}} GitLab configuration */
|
||||||
|
gl: {
|
||||||
|
base: 'https://gitlab.com',
|
||||||
|
transform: (path) => path.replace(/^\/gl\//, '/') + '/raw',
|
||||||
|
},
|
||||||
|
/** @type {{base: string, transform: function(string): string}} HuggingFace configuration */
|
||||||
|
hf: {
|
||||||
|
base: 'https://huggingface.co',
|
||||||
|
transform: (path) => path.replace(/^\/hf\//, '/'),
|
||||||
|
},
|
||||||
|
/** @type {{base: string, transform: function(string): string}} Kaggle configuration */
|
||||||
|
kg: {
|
||||||
|
base: 'https://www.kaggle.com',
|
||||||
|
transform: (path) => path.replace(/^\/kg\//, '/'),
|
||||||
|
},
|
||||||
|
};
|
||||||
+242
@@ -0,0 +1,242 @@
|
|||||||
|
import { CONFIG } from './config';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Monitors performance metrics during request processing
|
||||||
|
*/
|
||||||
|
class PerformanceMonitor {
|
||||||
|
/**
|
||||||
|
* Initializes a new performance monitor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
this.startTime = Date.now();
|
||||||
|
this.marks = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marks a timing point with the given name
|
||||||
|
* @param {string} name - The name of the timing mark
|
||||||
|
*/
|
||||||
|
mark(name) {
|
||||||
|
if (this.marks.has(name)) {
|
||||||
|
console.warn(`Mark with name ${name} already exists.`);
|
||||||
|
}
|
||||||
|
this.marks.set(name, Date.now() - this.startTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all collected metrics
|
||||||
|
* @returns {Object.<string, number>} Object containing name-timestamp pairs
|
||||||
|
*/
|
||||||
|
getMetrics() {
|
||||||
|
return Object.fromEntries(this.marks.entries());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates incoming requests against security rules
|
||||||
|
* @param {Request} request - The incoming request object
|
||||||
|
* @param {URL} url - Parsed URL object
|
||||||
|
* @returns {{valid: boolean, error?: string, status?: number}} Validation result
|
||||||
|
*/
|
||||||
|
function validateRequest(request, url) {
|
||||||
|
if (!CONFIG.SECURITY.ALLOWED_METHODS.includes(request.method)) {
|
||||||
|
return { valid: false, error: 'Method not allowed', status: 405 };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.pathname.length > CONFIG.SECURITY.MAX_PATH_LENGTH) {
|
||||||
|
return { valid: false, error: 'Path too long', status: 414 };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { valid: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds security headers to the response
|
||||||
|
* @param {Headers} headers - Headers object to modify
|
||||||
|
* @returns {Headers} Modified headers object
|
||||||
|
*/
|
||||||
|
function addSecurityHeaders(headers) {
|
||||||
|
headers.set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload');
|
||||||
|
headers.set('X-Frame-Options', 'DENY');
|
||||||
|
headers.set('X-XSS-Protection', '1; mode=block');
|
||||||
|
headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
|
||||||
|
headers.set('Content-Security-Policy', "default-src 'none'; img-src 'self'; script-src 'none'");
|
||||||
|
headers.set('Permissions-Policy', 'interest-cohort=()');
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles incoming requests with caching, retries, and security measures
|
||||||
|
* @param {Request} request - The incoming request
|
||||||
|
* @param {Object} env - Environment variables
|
||||||
|
* @param {ExecutionContext} ctx - Cloudflare Workers execution context
|
||||||
|
* @returns {Promise<Response>} The response object
|
||||||
|
*/
|
||||||
|
async function handleRequest(request, env, ctx) {
|
||||||
|
try {
|
||||||
|
const url = new URL(request.url);
|
||||||
|
|
||||||
|
// Serve homepage for root path
|
||||||
|
if (url.pathname === '/' || url.pathname === '') {
|
||||||
|
const headers = new Headers();
|
||||||
|
headers.set('Location', 'https://xget.us.kg');
|
||||||
|
addSecurityHeaders(headers);
|
||||||
|
return new Response(null, {
|
||||||
|
status: 301,
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const monitor = new PerformanceMonitor();
|
||||||
|
|
||||||
|
const validation = validateRequest(request, url);
|
||||||
|
if (!validation.valid) {
|
||||||
|
return new Response(validation.error, {
|
||||||
|
status: validation.status,
|
||||||
|
headers: addSecurityHeaders(new Headers()),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse platform and path
|
||||||
|
const [_, platform, ...pathParts] = url.pathname.split('/');
|
||||||
|
if (!platform || !CONFIG.PLATFORMS[platform]) {
|
||||||
|
return new Response('Invalid or missing platform', {
|
||||||
|
status: 400,
|
||||||
|
headers: addSecurityHeaders(new Headers()),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transform URL based on platform
|
||||||
|
const targetPath = CONFIG.PLATFORMS[platform].transform(url.pathname);
|
||||||
|
const targetUrl = `${CONFIG.PLATFORMS[platform].base}${targetPath}`;
|
||||||
|
|
||||||
|
// Check cache first
|
||||||
|
const cache = caches.default;
|
||||||
|
const cacheKey = new Request(targetUrl, request);
|
||||||
|
let response = await cache.match(cacheKey);
|
||||||
|
|
||||||
|
if (response) {
|
||||||
|
monitor.mark('cache_hit');
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchOptions = {
|
||||||
|
cf: {
|
||||||
|
http3: true,
|
||||||
|
cacheTtl: CONFIG.CACHE_DURATION,
|
||||||
|
cacheEverything: true,
|
||||||
|
minify: {
|
||||||
|
javascript: true,
|
||||||
|
css: true,
|
||||||
|
html: true,
|
||||||
|
},
|
||||||
|
preconnect: true,
|
||||||
|
},
|
||||||
|
headers: {
|
||||||
|
'Accept-Encoding': 'gzip, deflate, br',
|
||||||
|
Connection: 'keep-alive',
|
||||||
|
'User-Agent': 'Wget/1.21.3',
|
||||||
|
Origin: request.headers.get('Origin') || '*',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Handle range requests
|
||||||
|
const rangeHeader = request.headers.get('Range');
|
||||||
|
if (rangeHeader) {
|
||||||
|
fetchOptions.headers['Range'] = rangeHeader;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implement retry mechanism
|
||||||
|
let attempts = 0;
|
||||||
|
while (attempts < CONFIG.MAX_RETRIES) {
|
||||||
|
try {
|
||||||
|
monitor.mark('attempt_' + attempts);
|
||||||
|
|
||||||
|
// Fetch with timeout
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeoutId = setTimeout(() => controller.abort(), CONFIG.TIMEOUT_SECONDS * 1000);
|
||||||
|
|
||||||
|
response = await fetch(targetUrl, {
|
||||||
|
...fetchOptions,
|
||||||
|
signal: controller.signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
|
if (response.ok || response.status === 206) {
|
||||||
|
monitor.mark('success');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
attempts++;
|
||||||
|
if (attempts < CONFIG.MAX_RETRIES) {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, CONFIG.RETRY_DELAY_MS * attempts));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
attempts++;
|
||||||
|
if (error.name === 'AbortError') {
|
||||||
|
return new Response('Request timeout', { status: 408 });
|
||||||
|
}
|
||||||
|
if (attempts >= CONFIG.MAX_RETRIES) {
|
||||||
|
return new Response(`Failed after ${CONFIG.MAX_RETRIES} attempts: ${error.message}`, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare response headers
|
||||||
|
const headers = new Headers(response.headers);
|
||||||
|
headers.set('Cache-Control', `public, max-age=${CONFIG.CACHE_DURATION}`);
|
||||||
|
headers.set('X-Content-Type-Options', 'nosniff');
|
||||||
|
headers.set('Accept-Ranges', 'bytes');
|
||||||
|
addSecurityHeaders(headers);
|
||||||
|
|
||||||
|
// Create final response
|
||||||
|
const finalResponse = new Response(response.body, {
|
||||||
|
status: response.status,
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Cache successful responses
|
||||||
|
if (response.ok || response.status === 206) {
|
||||||
|
ctx.waitUntil(cache.put(cacheKey, finalResponse.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
monitor.mark('complete');
|
||||||
|
return addPerformanceHeaders(finalResponse, monitor);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error handling request:', error);
|
||||||
|
return new Response(`Internal Server Error: ${error.message}`, {
|
||||||
|
status: 500,
|
||||||
|
headers: addSecurityHeaders(new Headers()),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds performance metrics to response headers
|
||||||
|
* @param {Response} response - The response object
|
||||||
|
* @param {PerformanceMonitor} monitor - Performance monitor instance
|
||||||
|
* @returns {Response} New response with performance headers
|
||||||
|
*/
|
||||||
|
function addPerformanceHeaders(response, monitor) {
|
||||||
|
const headers = new Headers(response.headers);
|
||||||
|
headers.set('X-Performance-Metrics', JSON.stringify(monitor.getMetrics()));
|
||||||
|
addSecurityHeaders(headers);
|
||||||
|
return new Response(response.body, {
|
||||||
|
status: response.status,
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
/**
|
||||||
|
* Main entry point for the Cloudflare Worker
|
||||||
|
* @param {Request} request - The incoming request
|
||||||
|
* @param {Object} env - Environment variables
|
||||||
|
* @param {ExecutionContext} ctx - Cloudflare Workers execution context
|
||||||
|
* @returns {Promise<Response>} The response object
|
||||||
|
*/
|
||||||
|
fetch(request, env, ctx) {
|
||||||
|
return handleRequest(request, env, ctx);
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { createExecutionContext, env, SELF, waitOnExecutionContext } from 'cloudflare:test';
|
||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import worker from '../src';
|
||||||
|
|
||||||
|
describe('Hello World worker', () => {
|
||||||
|
it('responds with Hello World! (unit style)', async () => {
|
||||||
|
const request = new Request('http://example.com');
|
||||||
|
// Create an empty context to pass to `worker.fetch()`.
|
||||||
|
const ctx = createExecutionContext();
|
||||||
|
const response = await worker.fetch(request, env, ctx);
|
||||||
|
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
|
||||||
|
await waitOnExecutionContext(ctx);
|
||||||
|
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('responds with Hello World! (integration style)', async () => {
|
||||||
|
const response = await SELF.fetch(request, env, ctx);
|
||||||
|
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config';
|
||||||
|
|
||||||
|
export default defineWorkersConfig({
|
||||||
|
test: {
|
||||||
|
poolOptions: {
|
||||||
|
workers: {
|
||||||
|
wrangler: { configPath: './wrangler.toml' },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#:schema node_modules/wrangler/config-schema.json
|
||||||
|
name = "xget"
|
||||||
|
main = "src/index.js"
|
||||||
|
compatibility_date = "2024-10-22"
|
||||||
|
compatibility_flags = ["nodejs_compat"]
|
||||||
|
[observability]
|
||||||
|
enabled = true
|
||||||
|
head_sampling_rate = 1
|
||||||
Reference in new issue
Block a user