Skip to content

Commit 6cd8272

Browse files
committed
feat: add cache to check endpoint
1 parent d8ffb09 commit 6cd8272

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

packages/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"uint8arrays": "^3.0.0"
3434
},
3535
"devDependencies": {
36-
"@cloudflare/workers-types": "^3.1.1",
36+
"@cloudflare/workers-types": "^3.3.1",
3737
"@sentry/cli": "^1.71.0",
3838
"@sentry/webpack-plugin": "^1.16.0",
3939
"@types/debug": "^4.1.5",

packages/api/src/routes/nfts-check.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,50 @@ import { DBClient } from '../utils/db-client'
55
import { parseCid } from '../utils/utils.js'
66
import { toCheckNftResponse } from '../utils/db-transforms.js'
77

8+
const CACHE_MAX_AGE_NO_DEAL_YET = 10 * 60 // in seconds (10 minutes)
89
const db = new DBClient(database.url, secrets.database)
910

1011
/** @type {import('../bindings').Handler} */
1112
export const nftCheck = async (event, { params }) => {
13+
const cache = await caches.open('nft:check')
14+
// @ts-ignore match function not found in type https://developer.mozilla.org/en-US/docs/Web/API/Cache
15+
let res = await caches.match(event.request.url)
16+
17+
if (res) {
18+
return res
19+
}
20+
1221
const cid = parseCid(params.cid)
1322
const content = await db.getContent(cid.contentCid)
1423

15-
if (content) {
16-
return new JSONResponse({
17-
ok: true,
18-
value: toCheckNftResponse(cid.sourceCid, content),
19-
})
20-
} else {
24+
if (!content) {
2125
throw new HTTPError('NFT not found', 404)
2226
}
27+
28+
const nftCheckResponseValue = toCheckNftResponse(cid.sourceCid, content)
29+
const headers = !!nftCheckResponseValue.deals.find(
30+
(d) => d.status === 'active'
31+
)
32+
? {
33+
// cache status response with max age
34+
'Cache-Control': `public, max-age=${CACHE_MAX_AGE_NO_DEAL_YET}`,
35+
}
36+
: undefined // cache default
37+
38+
res = new JSONResponse(
39+
{
40+
ok: true,
41+
value: nftCheckResponseValue,
42+
},
43+
{
44+
headers,
45+
}
46+
)
47+
48+
// Cache if pin status is pinned
49+
if (nftCheckResponseValue.pin.status === 'pinned') {
50+
event.waitUntil(cache.put(event.request, res.clone()))
51+
}
52+
53+
return res
2354
}

packages/api/src/utils/json-response.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ export class JSONResponse extends Response {
55
* @param {ResponseInitializerDict} [init]
66
*/
77
constructor(body, init = {}) {
8-
const headers = {
8+
super(JSON.stringify(body), {
9+
...init,
910
headers: {
1011
'content-type': 'application/json;charset=UTF-8',
12+
...init.headers,
1113
},
12-
}
13-
super(JSON.stringify(body), { ...init, ...headers })
14+
})
1415
}
1516
}

yarn.lock

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@
10571057
resolved "https://registry.yarnpkg.com/@cfworker/json-schema/-/json-schema-1.12.1.tgz#8b151da771678d37bf77c5eda87b1137b2b009c8"
10581058
integrity sha512-ZVhk1DKw19sBJgO7OHBsvNAzDfHMzpFI0DCkOLTZHb0dccmS+EM8rwwAYh5eaNmD5t8/nCbZ2ZeGzbDQdOzokQ==
10591059

1060-
"@cloudflare/workers-types@^3.1.1":
1060+
"@cloudflare/workers-types@^3.3.1":
10611061
version "3.3.1"
10621062
resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-3.3.1.tgz#8847543bda320472252708c29aaf7bac3374e814"
10631063
integrity sha512-GJFDgWd8ZHlr/m+Q2mp4xUl0/FIPpR6kf0Ix2C78E9HeJyUCW0c0w2EKCvgEDFFKB2rkzIX3eBqZCnLsbsw8zQ==
@@ -10056,6 +10056,23 @@ nft.storage@^3.3.0:
1005610056
p-retry "^4.6.1"
1005710057
streaming-iterables "^6.0.0"
1005810058

10059+
nft.storage@^5.1.0:
10060+
version "5.2.5"
10061+
resolved "https://registry.yarnpkg.com/nft.storage/-/nft.storage-5.2.5.tgz#55c9f1b3cd40654cfeae006c3a4abb2da238049f"
10062+
integrity sha512-ITP9ETleKIZvSRsjpHi6JFFE/YVcp9jwwyi+Q1GtdNmFc5Xyu1g7it+yrk7m5+iSEMcOjSJGPQrktnUTe0qtAg==
10063+
dependencies:
10064+
"@ipld/car" "^3.2.3"
10065+
"@ipld/dag-cbor" "^6.0.13"
10066+
"@web-std/blob" "^3.0.1"
10067+
"@web-std/fetch" "^3.0.3"
10068+
"@web-std/file" "^3.0.0"
10069+
"@web-std/form-data" "^3.0.0"
10070+
carbites "^1.0.6"
10071+
ipfs-car "^0.6.2"
10072+
multiformats "^9.6.3"
10073+
p-retry "^4.6.1"
10074+
streaming-iterables "^6.0.0"
10075+
1005910076
nice-try@^1.0.4:
1006010077
version "1.0.5"
1006110078
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
@@ -10088,7 +10105,6 @@ node-fetch@^1.0.1:
1008810105

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

1009410110
node-forge@^1.2.0:

0 commit comments

Comments
 (0)