@@ -4,9 +4,11 @@ import { markdownStaticHandler as md } from '../packages/markdown/src'
44import { logger } from '../packages/logger/src'
55import { createReadStream } from 'fs'
66import { transformMWPageStream , transformPageIndexStream } from './streams'
7- import unfetch from 'isomorphic-unfetch '
7+ import fetchCache from 'node-fetch-cache '
88import hljs from 'highlight.js'
99
10+ const fetch = fetchCache ( `${ __dirname } /.cache` )
11+
1012const app = new App ( {
1113 settings : {
1214 networkExtensions : true ,
@@ -15,7 +17,7 @@ const app = new App({
1517
1618const 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
2022app
2123 . use (
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 ) ) )
5850 }
5951
6052 readStream . pipe ( transformer ) . pipe ( res )
53+ } catch ( e ) {
54+ next ( e )
6155 }
6256 } )
6357 . get ( '/mw/:mw' , async ( req , res , next ) => {
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 )
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