-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Caches satnav data in cloudflare's cache.
- Loading branch information
Alan Shaw
authored
Jul 13, 2023
1 parent
fb85a04
commit 759d117
Showing
7 changed files
with
132 additions
and
18 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const MAX_CAR_BYTES_IN_MEMORY = 1024 * 1024 * 5 | ||
export const CAR_CODE = 0x0202 |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* eslint-env worker */ | ||
|
||
const MAX_AGE = 86400 // 1 day | ||
|
||
/** | ||
* @typedef {import('../bindings').SimpleBucket} SimpleBucket | ||
* @implements {SimpleBucket} | ||
*/ | ||
export class CachingBucket { | ||
#source | ||
#cache | ||
#ctx | ||
|
||
/** | ||
* @param {SimpleBucket} source | ||
* @param {Cache} cache | ||
* @param {Pick<import('@cloudflare/workers-types').ExecutionContext, 'waitUntil'>} ctx | ||
*/ | ||
constructor (source, cache, ctx) { | ||
this.#source = source | ||
this.#cache = cache | ||
this.#ctx = ctx | ||
} | ||
|
||
/** @type {import('../bindings').SimpleBucket['get']} */ | ||
async get (key) { | ||
const cacheKey = new URL(key, 'http://localhost') | ||
const res = await this.#cache.match(cacheKey) | ||
if (res && res.body) return { key, body: res.body } | ||
const obj = await this.#source.get(key) | ||
if (!obj) return null | ||
const [body0, body1] = obj.body.tee() | ||
this.#ctx.waitUntil(this.#cache.put(cacheKey, new Response(body1, { | ||
headers: { 'Cache-Control': `max-age=${MAX_AGE}` } | ||
}))) | ||
return { key, body: body0 } | ||
} | ||
} | ||
|
||
/** | ||
* Cast an R2 bucket as a simple bucket. | ||
* | ||
* @param {import('@cloudflare/workers-types').R2Bucket} r2 | ||
* @returns {SimpleBucket} | ||
*/ | ||
// @ts-expect-error R2Bucket.get is overloaded with a non-optional options which | ||
// means it does not overlap with our SimpleBucket interface :( | ||
export const asSimpleBucket = r2 => r2 |
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