Last active
October 28, 2020 01:23
-
-
Save timc1/53f03f4b6f0a0ebea7fedda44d7306ef to your computer and use it in GitHub Desktop.
simple s3 cache for time sensitive bucket access
This file contains hidden or 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
type Cache = { | |
[k: string]: string; | |
}; | |
function cache() { | |
const _cache: Cache = {}; | |
const cache = { | |
get: (key: string) => { | |
const cachedUrl = _cache[key]; | |
if (cachedUrl) { | |
try { | |
// grab created time from s3 url. why is the param Expires? 🤔 | |
const parsedUrl = new URL(cachedUrl); | |
const created = Number(parsedUrl.searchParams.get("Expires")); | |
// calculate the expiration time (1 hour in our case) | |
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-creating-signed-url-canned-policy.html | |
const dt = new Date(created); | |
dt.setTime(dt.getTime() + 1); | |
const expires = parseInt((new Date().getTime() / 1000).toFixed(0)); | |
const expired = expires - created > 0; | |
if (expired) { | |
delete _cache[key]; | |
return undefined; | |
} | |
return cachedUrl; | |
} catch { | |
delete _cache[key]; | |
return undefined; | |
} | |
} | |
return undefined; | |
}, | |
set: (key: string, value: string) => (_cache[key] = value), | |
}; | |
return cache; | |
} | |
export default cache(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment