-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from karldreher/develop
1.0
- Loading branch information
Showing
6 changed files
with
94 additions
and
58 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,68 @@ | ||
addEventListener('fetch', event => { | ||
event.respondWith(handleRequest(event)) | ||
}) | ||
addEventListener("fetch", (event) => { | ||
event.respondWith(handleRequest(event)); | ||
}); | ||
|
||
async function getValueFromKV(key) { | ||
let value = await kv_namespace.get(key); | ||
return value; | ||
} | ||
|
||
async function authorizeAccount() { | ||
authValue = await getValueFromKV("AUTH_HEADER"); | ||
authHeader = { | ||
Authorization: authValue, | ||
}; | ||
const account = await fetch( | ||
"https://api.backblazeb2.com/b2api/v2/b2_authorize_account", | ||
{ | ||
headers: authHeader, | ||
} | ||
).then((res) => res.json()); | ||
//https://www.backblaze.com/b2/docs/b2_authorize_account.html | ||
//Note, the auth will be valid for 24 hours. right now this is called every time, on next update should be stored in KV and ideally cron'd | ||
return account; | ||
} | ||
|
||
async function serveAsset(event) { | ||
const url = new URL(event.request.url) | ||
const cache = caches.default | ||
let response = await cache.match(event.request) | ||
const url = new URL(event.request.url); | ||
|
||
//First determine if the cache has the object already | ||
const cache = caches.default; | ||
let response = await cache.match(event.request); | ||
|
||
if (!response) { | ||
//get api url and auth from b2 | ||
auth = await authorizeAccount(); | ||
|
||
//Set target URL | ||
baseURL = await getValueFromKV('ENDPOINT') | ||
const url = new URL(event.request.url); | ||
requestURL = baseURL + url.pathname | ||
|
||
response = await fetch(requestURL) | ||
console.log(new Map(response.headers)) | ||
//Bucket name comes from KV | ||
bucketName = await getValueFromKV("BUCKET_NAME"); | ||
//Request URL to B2 must contain the download URL returned from authorize account, then the path /file/{bucketname}/{pathname} | ||
requestURL = new URL( | ||
auth.downloadUrl + "/file/" + bucketName + url.pathname | ||
); | ||
params = { b2CacheControl: "public,max-age=86400" }; | ||
requestURL.search = new URLSearchParams(params).toString(); | ||
requestHeaders = { | ||
Authorization: auth.authorizationToken, | ||
}; | ||
response = await fetch(requestURL, { | ||
headers: requestHeaders, | ||
}); | ||
|
||
response = new Response(response.body, response) | ||
response.headers.set("Cache-Control", "public,max-age=86400") | ||
response = new Response(response.body, response); | ||
response.headers.set("Cache-Control", "public,max-age=86400"); | ||
|
||
event.waitUntil(cache.put(event.request, response.clone())) | ||
event.waitUntil(cache.put(event.request, response.clone())); | ||
} | ||
return response | ||
return response; | ||
} | ||
|
||
|
||
async function handleRequest(event) { | ||
let response = await serveAsset(event) | ||
let response = await serveAsset(event); | ||
if (response.status > 399) { | ||
response = new Response(response.statusText, { status: response.status }) | ||
response = new Response(response.statusText, { status: response.status }); | ||
} | ||
return response | ||
return response; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,8 @@ | |
"description": "Cloudflare worker for caching a private Backblaze B2 bucket.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"format": "prettier --write '**/*.{js,css,json,md}'" | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Karl Dreher <[email protected]>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"prettier": "^1.18.2" | ||
} | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
"value": "placeholder" | ||
}, | ||
{ | ||
"key": "ENDPOINT", | ||
"key": "BUCKET_NAME", | ||
"value": "placeholder" | ||
} | ||
] | ||
] |