Last active
September 12, 2021 19:31
-
-
Save johnrees/6dcf1e0931298f137452ef27e0c83769 to your computer and use it in GitHub Desktop.
caching test
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
curl https://rpc.goo.tools -X POST -H "Content-Type: application/json" -d ' | |
{ | |
"jsonrpc": "2.0", | |
"id": 1, | |
"method": "getProgramAccounts", | |
"params": [ | |
"cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ", | |
{ | |
"dataSlice": { "offset": 0, "length": 0 } | |
} | |
] | |
}' | jq -r '.result[].pubkey' |
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
const url = "https://api.mainnet-beta.solana.com"; | |
async function sha256(message) { | |
const msgBuffer = new TextEncoder().encode(message); | |
const hashBuffer = await crypto.subtle.digest("SHA-256", msgBuffer); | |
const hashArray = Array.from(new Uint8Array(hashBuffer)); | |
const hashHex = hashArray | |
.map((b) => ("00" + b.toString(16)).slice(-2)) | |
.join(""); | |
return hashHex; | |
} | |
async function handleRequest(event) { | |
const body = await event.request.text(); | |
const hash = await sha256(body); | |
let value = await RPC.get(hash); | |
if (value === null) { | |
const res = await fetch(url, { | |
body, | |
method: "POST", | |
headers: { | |
"content-type": "application/json", | |
}, | |
}); | |
value = await res.text(); | |
await RPC.put(hash, value, { expirationTtl: 60 }); | |
} | |
return new Response(value, { | |
headers: { | |
"content-type": "application/json", | |
}, | |
}); | |
} | |
addEventListener("fetch", (event) => { | |
return event.respondWith(handleRequest(event)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment