Skip to content

Commit

Permalink
[VIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
ss-o committed Jul 24, 2022
1 parent 5f42776 commit 98546cc
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 50 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/checksum.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Main

on:
push:
tags:
- "v*.*.*"
paths:
- "lib/sh/**"
- "lib/zsh/**"
workflow_dispatch: {}

jobs:
release:
name: Create release
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Download releaseartifacts
uses: actions/download-artifact@v2
with:
name: release-artifacts
path: lib
- name: Generate checksum
uses: jmgilman/actions-generate-checksum@57c13a1f01a90b2a8bb449ec29bc23728b96c1cd
with:
patterns: |
lib/zsh/init.zsh
- name: Create release
uses: softprops/action-gh-release@v1
with:
files: |
lib/zsh/checksum.txt
lib/zsh/init.zsh
4 changes: 2 additions & 2 deletions workers/cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"devDependencies": {
"@cloudflare/workers-types": "3.14.1",
"prettier": "2.7.1",
"wrangler": "2.0.22",
"typescript": "4.7.4"
"typescript": "4.7.4",
"wrangler": "2.0.22"
},
"engines": {
"node": ">=16"
Expand Down
51 changes: 24 additions & 27 deletions workers/cache/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
async function handleRequest(event) {
const request = event.request;
const cacheUrl = new URL(request.url);
const cacheKey = new Request(cacheUrl.toString(), request);
const cache = caches.default;
export default {
async fetch(request, env, context) {
try {
const cacheUrl = new URL(request.url);
const cacheKey = new Request(cacheUrl.toString(), request);
const cache = caches.default;

let response = await cache.match(cacheKey);
let response = await cache.match(cacheKey);

if (!response) {
console.log(
`Response for request url: ${request.url} not present in cache. Fetching and caching request.`
);
response = await fetch(request);
response = new Response(response.body, response);
response.headers.append("Cache-Control", "s-maxage=360");
if (!response) {
console.log(
`Response for request url: ${request.url} not present in cache. Fetching and caching request.`
);
response = await fetch(request);
response = new Response(response.body, response);
response.headers.append("Cache-Control", "s-maxage=60");

event.waitUntil(cache.put(cacheKey, response.clone()));
} else {
console.log(`Cache hit for: ${request.url}.`);
}
return response;
}

addEventListener("fetch", (event) => {
try {
return event.respondWith(handleRequest(event));
} catch (e) {
return event.respondWith(new Response("Error thrown " + e.message));
}
});
context.waitUntil(cache.put(cacheKey, response.clone()));
} else {
console.log(`Cache hit for: ${request.url}.`);
}
return response;
} catch (e) {
return new Response("Error thrown " + e.message);
}
},
};
11 changes: 1 addition & 10 deletions workers/cache/src/cache.ts → workers/cache/src/r2_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,13 @@ export default {
const headers = new Headers();
object.writeHttpMetadata(headers);
headers.set("etag", object.httpEtag);
headers.set("Cache-Control", "s-maxage=84000");
headers.set("Cache-Control", "max-age=14400");
headers.append("Cache-Control", "stale-if-error=400");

headers.append("Cloudflare-CDN-Cache-Control", "stale-if-error=60");
headers.append("CDN-Cache-Control", "stale-if-error=200");

headers.append("Cloudflare-CDN-Cache-Control", "max-age=24400");
headers.append("CDN-Cache-Control", "max-age=18000");
headers.append("Cache-Control", "s-maxage=10");

response = new Response(object.body, {
headers,
});

context.waitUntil(cache.put(cacheKey, response.clone()));

return response;
} catch (e) {
return new Response("Error thrown " + e.message);
Expand Down
2 changes: 1 addition & 1 deletion workers/cache/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ main = "src/index.ts"
compatibility_date = "2022-07-19"
workers_dev = false
account_id = "2db2112b269ed3616ed3a863a222f3e8"
route = { pattern = "wiki.zshell.dev/*", zone_id = "88404a09f4a582bb65186110a35d1151" }
route = { pattern = "https://wiki.zshell.dev/*", zone_id = "88404a09f4a582bb65186110a35d1151" }
r2_buckets = [
{ binding = "R2_STORE", bucket_name = "r2-store", preview_bucket_name = "" },
]
Expand Down
24 changes: 15 additions & 9 deletions workers/r2-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function parseRange(

function objectNotFound(objectName: string): Response {
return new Response(
`<html><body>R2 object "<b>${objectName}</b>" not found</body></html>`,
`<html><body style="background-color:black; color:white"><h3 style="text-align:center">R2 object "<b>${objectName}</b>" not found</h3></body></html>`,
{
status: 404,
headers: {
Expand Down Expand Up @@ -51,7 +51,7 @@ export default {
};
console.log(JSON.stringify(options));

const listing = await env.R2_BUCKET.list(options);
const listing = await env.R2_STORE.list(options);
return new Response(JSON.stringify(listing), {
headers: {
"content-type": "application/json; charset=UTF-8",
Expand All @@ -61,7 +61,7 @@ export default {

if (request.method === "GET") {
const range = parseRange(request.headers.get("range"));
const object = await env.R2_BUCKET.get(objectName, {
const object = await env.R2_STORE.get(objectName, {
range,
onlyIf: request.headers,
});
Expand All @@ -73,14 +73,20 @@ export default {
const headers = new Headers();
object.writeHttpMetadata(headers);
headers.set("etag", object.httpEtag);
/** headers.append("Access-Control-Allow-Headers", "Content-Type, Set-Cookie, Cache-Control");
headers.append("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, OPTIONS");
headers.append("Access-Control-Allow-Origin", "htt");
headers.append("Access-Control-Max-Age", "600");
headers.append("Vary", "Origin, Accept-Encoding"); */

const status = object.body ? (range ? 206 : 200) : 304;
return new Response(object.body, {
headers,
status,
});
}

const object = await env.R2_BUCKET.head(objectName);
const object = await env.R2_STORE.head(objectName);

if (object === null) {
return objectNotFound(objectName);
Expand All @@ -89,14 +95,14 @@ export default {
const headers = new Headers();
object.writeHttpMetadata(headers);
headers.set("etag", object.httpEtag);
headers.append("Access-Control-Allow-Origin", "https://wiki.zshell.dev");

return new Response(null, {
headers,
});
}

/**
if (request.method === "PUT" || request.method == "POST") {
const object = await env.R2_BUCKET.put(objectName, request.body, {
const object = await env.R2_STORE.put(objectName, request.body, {
httpMetadata: request.headers,
});
return new Response(null, {
Expand All @@ -106,10 +112,10 @@ export default {
});
}
if (request.method === "DELETE") {
await env.R2_BUCKET.delete(url.pathname.slice(1));
await env.R2_STORE.delete(url.pathname.slice(1));
return new Response();
}

*/
return new Response(`Unsupported method`, {
status: 400,
});
Expand Down
6 changes: 5 additions & 1 deletion workers/r2-store/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ main = "src/index.ts"
compatibility_date = "2022-07-11"
workers_dev = false
account_id = "2db2112b269ed3616ed3a863a222f3e8"
routes = [
{ pattern = "r2.zshell.dev", custom_domain = true },
{ pattern = "https://*.zshell.dev/r2/*", zone_id = "88404a09f4a582bb65186110a35d1151" },
]

[[r2_buckets]]
binding = "R2_BUCKET"
binding = "R2_STORE"
bucket_name = "r2-store"

0 comments on commit 98546cc

Please sign in to comment.