From 29504d0cbe146b417f76919858da79474664bcdb Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Tue, 1 Feb 2022 19:10:43 +0100 Subject: [PATCH] feat: add cache to check endpoint --- packages/api/src/routes/nfts-check.js | 43 +++++++++++++++++++++---- packages/api/src/utils/json-response.js | 7 ++-- yarn.lock | 6 ++-- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/packages/api/src/routes/nfts-check.js b/packages/api/src/routes/nfts-check.js index 9b64a599e0..6e9fc04547 100644 --- a/packages/api/src/routes/nfts-check.js +++ b/packages/api/src/routes/nfts-check.js @@ -5,19 +5,50 @@ import { DBClient } from '../utils/db-client' import { parseCid } from '../utils/utils.js' import { toCheckNftResponse } from '../utils/db-transforms.js' +const CACHE_MAX_AGE_NO_DEAL_YET = 10 * 60 // in seconds (10 minutes) const db = new DBClient(database.url, secrets.database) /** @type {import('../bindings').Handler} */ export const nftCheck = async (event, { params }) => { + const cache = await caches.open('nft:check') + // @ts-ignore match function not found in type https://developer.mozilla.org/en-US/docs/Web/API/Cache + let res = await caches.match(event.request.url) + + if (res) { + return res + } + const cid = parseCid(params.cid) const content = await db.getContent(cid.contentCid) - if (content) { - return new JSONResponse({ - ok: true, - value: toCheckNftResponse(cid.sourceCid, content), - }) - } else { + if (!content) { throw new HTTPError('NFT not found', 404) } + + const nftCheckResponseValue = toCheckNftResponse(cid.sourceCid, content) + const headers = + !!nftCheckResponseValue.deals.find((d) => d.status === 'active') && + nftCheckResponseValue.pin.status === 'pinned' + ? { + // cache status response with max age + 'Cache-Control': `public, max-age=${CACHE_MAX_AGE_NO_DEAL_YET}`, + } + : undefined // cache default + + res = new JSONResponse( + { + ok: true, + value: nftCheckResponseValue, + }, + { + headers, + } + ) + + // Cache if pin status is pinned + if (nftCheckResponseValue.pin.status === 'pinned') { + event.waitUntil(cache.put(event.request, res.clone())) + } + + return res } diff --git a/packages/api/src/utils/json-response.js b/packages/api/src/utils/json-response.js index 53652dbd22..464101c77f 100644 --- a/packages/api/src/utils/json-response.js +++ b/packages/api/src/utils/json-response.js @@ -5,11 +5,12 @@ export class JSONResponse extends Response { * @param {ResponseInitializerDict} [init] */ constructor(body, init = {}) { - const headers = { + super(JSON.stringify(body), { + ...init, headers: { 'content-type': 'application/json;charset=UTF-8', + ...init.headers, }, - } - super(JSON.stringify(body), { ...init, ...headers }) + }) } } diff --git a/yarn.lock b/yarn.lock index 1dad86633a..977be7f4d2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1058,9 +1058,9 @@ integrity sha512-hh7qzfT0+1rkKiZrZnttRZxjZLzcHHZNQ7XmzA8De0YJxhg/tEovmczM1AjuGZJr8sr69gfOFtfZgqz2s1/p5Q== "@cloudflare/workers-types@^3.3.1": - version "3.4.0" - resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-3.4.0.tgz#80311e14df2f7f8c2cfcdce945b4f4ad8f9b03b1" - integrity sha512-i/3czUrt6YqbOWl44OtIqd0cSZvEVXp/1oD/DZylC4PHZL3q/BhbamdEVeVhc/HPk4iD/7MZ2HGaIMO4Z4b12A== + version "3.3.1" + resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-3.3.1.tgz#8847543bda320472252708c29aaf7bac3374e814" + integrity sha512-GJFDgWd8ZHlr/m+Q2mp4xUl0/FIPpR6kf0Ix2C78E9HeJyUCW0c0w2EKCvgEDFFKB2rkzIX3eBqZCnLsbsw8zQ== "@concordance/react@^2.0.0": version "2.0.0"