Skip to content

Commit 5dceb0c

Browse files
author
v1rtl
committed
Add caching to requests in the website
1 parent 9bdce29 commit 5dceb0c

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ coverage
55
examples/*/.env
66
pnpm-lock.yaml
77
.next
8+
.cache

site/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"@tinyhttp/markdown": "workspace:*",
99
"@tinyhttp/static": "workspace:*",
1010
"highlight.js": "^10.1.2",
11-
"isomorphic-unfetch": "^3.0.0",
1211
"marked": "^1.1.1",
12+
"node-fetch-cache": "^1.0.4",
1313
"sirv": "^1.0.6"
1414
},
1515
"scripts": {

site/server.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { markdownStaticHandler as md } from '../packages/markdown/src'
44
import { logger } from '../packages/logger/src'
55
import { createReadStream } from 'fs'
66
import { transformMWPageStream, transformPageIndexStream } from './streams'
7-
import unfetch from 'isomorphic-unfetch'
7+
import fetchCache from 'node-fetch-cache'
88
import hljs from 'highlight.js'
99

10+
const fetch = fetchCache(`${__dirname}/.cache`)
11+
1012
const app = new App({
1113
settings: {
1214
networkExtensions: true,
@@ -15,7 +17,7 @@ const app = new App({
1517

1618
const HTML_PATH = `${process.cwd()}/pages/html`
1719

18-
const NON_MW_PKGS: string[] = ['app', 'etag', 'cookie', 'cookie-signature', 'dotenv']
20+
const NON_MW_PKGS: string[] = ['app', 'etag', 'cookie', 'cookie-signature', 'dotenv', 'send', 'router', 'req', 'res']
1921

2022
app
2123
.use(
@@ -29,21 +31,11 @@ app
2931
})
3032
)
3133
.get('/mw', async (req, res, next) => {
32-
let json: any, status: number, msg: string
33-
3434
try {
35-
const res = await unfetch('https://api.github.com/repos/talentlessguy/tinyhttp/contents/packages')
35+
const request = await fetch('https://api.github.com/repos/talentlessguy/tinyhttp/contents/packages')
3636

37-
status = res.status
38-
msg = res.statusText
39-
json = await res.json()
40-
} catch (e) {
41-
next(e)
42-
}
37+
const json = await request.json()
4338

44-
if (status !== 200) {
45-
next(msg)
46-
} else {
4739
const readStream = createReadStream(`${HTML_PATH}/search.html`)
4840

4941
let transformer = transformPageIndexStream(json.filter((e) => !NON_MW_PKGS.includes(e.name)))
@@ -58,6 +50,8 @@ app
5850
}
5951

6052
readStream.pipe(transformer).pipe(res)
53+
} catch (e) {
54+
next(e)
6155
}
6256
})
6357
.get('/mw/:mw', async (req, res, next) => {
@@ -67,17 +61,16 @@ app
6761
let json: any, status: number
6862

6963
try {
70-
const res = await unfetch(`https://registry.npmjs.org/@tinyhttp/${req.params.mw}`)
64+
const res = await fetch(`https://registry.npmjs.org/@tinyhttp/${req.params.mw}`)
7165

7266
status = res.status
7367
json = await res.json()
7468
} catch (e) {
7569
next(e)
7670
}
7771

78-
if (status === 404) {
79-
res.sendStatus(status)
80-
} else {
72+
if (status === 404) res.sendStatus(status)
73+
else {
8174
const readStream = createReadStream(`${HTML_PATH}/mw.html`)
8275

8376
readStream.pipe(transformMWPageStream(json)).pipe(res)
@@ -93,7 +86,7 @@ app
9386
},
9487
],
9588
markedOptions: {
96-
highlight: function (code, lang) {
89+
highlight: (code, lang) => {
9790
if (!lang) lang = 'txt'
9891

9992
return hljs.highlight(lang, code).value

0 commit comments

Comments
 (0)