Skip to content

Commit

Permalink
feat: add cache to check endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Feb 17, 2022
1 parent d8ffb09 commit e361d46
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.1.1",
"@cloudflare/workers-types": "^3.3.1",
"@sentry/cli": "^1.71.0",
"@sentry/webpack-plugin": "^1.16.0",
"@types/debug": "^4.1.5",
Expand Down
43 changes: 37 additions & 6 deletions packages/api/src/routes/nfts-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 4 additions & 3 deletions packages/api/src/utils/json-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})
}
}
20 changes: 18 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@
resolved "https://registry.yarnpkg.com/@cfworker/json-schema/-/json-schema-1.12.1.tgz#8b151da771678d37bf77c5eda87b1137b2b009c8"
integrity sha512-ZVhk1DKw19sBJgO7OHBsvNAzDfHMzpFI0DCkOLTZHb0dccmS+EM8rwwAYh5eaNmD5t8/nCbZ2ZeGzbDQdOzokQ==

"@cloudflare/workers-types@^3.1.1":
"@cloudflare/workers-types@^3.3.1":
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==
Expand Down Expand Up @@ -10056,6 +10056,23 @@ nft.storage@^3.3.0:
p-retry "^4.6.1"
streaming-iterables "^6.0.0"

nft.storage@^5.1.0:
version "5.2.5"
resolved "https://registry.yarnpkg.com/nft.storage/-/nft.storage-5.2.5.tgz#55c9f1b3cd40654cfeae006c3a4abb2da238049f"
integrity sha512-ITP9ETleKIZvSRsjpHi6JFFE/YVcp9jwwyi+Q1GtdNmFc5Xyu1g7it+yrk7m5+iSEMcOjSJGPQrktnUTe0qtAg==
dependencies:
"@ipld/car" "^3.2.3"
"@ipld/dag-cbor" "^6.0.13"
"@web-std/blob" "^3.0.1"
"@web-std/fetch" "^3.0.3"
"@web-std/file" "^3.0.0"
"@web-std/form-data" "^3.0.0"
carbites "^1.0.6"
ipfs-car "^0.6.2"
multiformats "^9.6.3"
p-retry "^4.6.1"
streaming-iterables "^6.0.0"

nice-try@^1.0.4:
version "1.0.5"
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
Expand Down Expand Up @@ -10088,7 +10105,6 @@ node-fetch@^1.0.1:

"node-fetch@https://registry.npmjs.org/@achingbrain/node-fetch/-/node-fetch-2.6.7.tgz":
version "2.6.7"
uid "1b5d62978f2ed07b99444f64f0df39f960a6d34d"
resolved "https://registry.npmjs.org/@achingbrain/node-fetch/-/node-fetch-2.6.7.tgz#1b5d62978f2ed07b99444f64f0df39f960a6d34d"

node-forge@^1.2.0:
Expand Down

0 comments on commit e361d46

Please sign in to comment.