Skip to content

Instantly share code, notes, and snippets.

@johnrees
Last active September 12, 2021 19:31
Show Gist options
  • Save johnrees/6dcf1e0931298f137452ef27e0c83769 to your computer and use it in GitHub Desktop.
Save johnrees/6dcf1e0931298f137452ef27e0c83769 to your computer and use it in GitHub Desktop.
caching test
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'
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