Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Sep 2, 2023
1 parent bc3736f commit 59bfc05
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 75 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/deploy-prod.yml

This file was deleted.

File renamed without changes.
131 changes: 72 additions & 59 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> {
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<Response> {
// 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;

0 comments on commit 59bfc05

Please sign in to comment.