diff --git a/.github/workflows/deploy-prod.yml b/.github/workflows/deploy-prod.yml deleted file mode 100644 index d2d7d6e..0000000 --- a/.github/workflows/deploy-prod.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: Deploy worker (prod) - -on: - workflow_dispatch: - -jobs: - deploy-prod: - runs-on: ubuntu-latest - steps: - - name: Code checkout - uses: actions/checkout@v2 - - name: Deploy - uses: cloudflare/wrangler-action@v3 - with: - apiToken: ${{ secrets.CF_API_TOKEN }} - command: deploy --env prod diff --git a/.github/workflows/deploy-staging.yml b/.github/workflows/deploy.yml similarity index 100% rename from .github/workflows/deploy-staging.yml rename to .github/workflows/deploy.yml diff --git a/src/worker.ts b/src/worker.ts index beecb2a..803728e 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -20,81 +20,94 @@ interface Worker { const cloudflareWorker: Worker = { fetch: async (request, env, ctx) => { const cache = caches.default; - - const shouldServeCache = isCacheEnabled(env); - - if (['GET', 'HEAD'].includes(request.method) === false) { - // This endpoint is called from the sync script to purge - // directories that are commonly updated so we don't need to - // wait for the cache to expire - if ( - shouldServeCache && - request.method === 'POST' && - request.url === '/_cf/cache-purge' - ) { - return cachePurgeHandler(request, cache, env); - } - - if (request.method === 'OPTIONS') { + switch (request.method) { + case 'HEAD': + case 'GET': + return getHandler(request, env, ctx, cache); + case 'POST': + return postHandler(request, env, cache); + case 'OPTIONS': return new Response(undefined, { headers: { Allow: 'GET, HEAD, OPTIONS', }, }); - } - - return responses.METHOD_NOT_ALLOWED; + default: + return responses.METHOD_NOT_ALLOWED; } + }, +}; - if (shouldServeCache) { - // Caching is enabled, let's see if the request is cached - const response = await cache.match(request); - - if (typeof response !== 'undefined') { - response.headers.append('x-cache-status', 'hit'); - - return response; - } +async function getHandler( + request: Request, + env: Env, + ctx: ExecutionContext, + cache: Cache +): Promise { + const shouldServeCache = isCacheEnabled(env); + if (shouldServeCache) { + // Caching is enabled, let's see if the request is cached + const response = await cache.match(request); + + if (typeof response !== 'undefined') { + response.headers.append('x-cache-status', 'hit'); + + return response; } + } - let url: URL; - try { - url = new URL(request.url); - } catch (e) { - return new Response(undefined, { status: 400 }); - } + let url: URL; + try { + url = new URL(request.url); + } catch (e) { + return new Response(undefined, { status: 400 }); + } - const bucketPath = mapUrlPathToBucketPath(url, env); + const bucketPath = mapUrlPathToBucketPath(url, env); - if (typeof bucketPath === 'undefined') { - // Directory listing is restricted and we're not on - // a supported path, block request - return new Response('Unauthorized', { status: 401 }); - } + if (typeof bucketPath === 'undefined') { + // Directory listing is restricted and we're not on + // a supported path, block request + return new Response('Unauthorized', { status: 401 }); + } - const isPathADirectory = isDirectoryPath(bucketPath); + const isPathADirectory = isDirectoryPath(bucketPath); - if (isPathADirectory && env.DIRECTORY_LISTING === 'off') { - // File not found since we should only be allowing - // file paths if directory listing is off - return responses.FILE_NOT_FOUND(request); - } + if (isPathADirectory && env.DIRECTORY_LISTING === 'off') { + // File not found since we should only be allowing + // file paths if directory listing is off + return responses.FILE_NOT_FOUND(request); + } - const response: Response = isPathADirectory - ? // Directory requested, try listing it - await directoryHandler(url, request, bucketPath, env) - : // File requested, try to serve it - await fileHandler(url, request, bucketPath, env); + const response: Response = isPathADirectory + ? // Directory requested, try listing it + await directoryHandler(url, request, bucketPath, env) + : // File requested, try to serve it + await fileHandler(url, request, bucketPath, env); - // Cache response if cache is enabled - if (shouldServeCache && response.status !== 304) { - ctx.waitUntil(cache.put(request, response.clone())); - } + // Cache response if cache is enabled + if (shouldServeCache && response.status !== 304) { + ctx.waitUntil(cache.put(request, response.clone())); + } - response.headers.append('x-cache-status', 'miss'); + response.headers.append('x-cache-status', 'miss'); - return response; - }, -}; + return response; +} + +async function postHandler( + request: Request, + env: Env, + cache: Cache +): Promise { + // This endpoint is called from the sync script to purge + // directories that are commonly updated so we don't need to + // wait for the cache to expire + if (isCacheEnabled(env) && request.url === '/_cf/cache-purge') { + return cachePurgeHandler(request, cache, env); + } + + return new Response(undefined, { status: 404 }); +} export default cloudflareWorker;