11import 'core-js/full/array/from-async' ;
22
3- import { Context , Middleware } from 'koa' ;
3+ import { JsonWebTokenError , sign } from 'jsonwebtoken' ;
4+ import { Context , Middleware , ParameterizedContext } from 'koa' ;
5+ import JWT from 'koa-jwt' ;
46import { HTTPError } from 'koajax' ;
57import { Content } from 'mobx-github' ;
68import { DataObject } from 'mobx-restful' ;
79import { KoaOption , withKoa } from 'next-ssr-middleware' ;
8- import Path from 'path' ;
910import { ProxyAgent , setGlobalDispatcher } from 'undici' ;
10- import YAML from 'yaml' ;
11+ import { parse } from 'yaml' ;
12+
13+ import { LarkAppMeta } from '../../models/configuration' ;
1114
1215const { HTTP_PROXY } = process . env ;
1316
1417if ( HTTP_PROXY ) setGlobalDispatcher ( new ProxyAgent ( HTTP_PROXY ) ) ;
1518
19+ export type JWTContext = ParameterizedContext <
20+ { jwtOriginalError : JsonWebTokenError } | { user : DataObject }
21+ > ;
22+
23+ export const parseJWT = JWT ( {
24+ secret : LarkAppMeta . secret ,
25+ cookie : 'token' ,
26+ passthrough : true ,
27+ } ) ;
28+
29+ export const verifyJWT = JWT ( { secret : LarkAppMeta . secret , cookie : 'token' } ) ;
30+
31+ const RobotToken = sign ( { id : 0 , name : 'Robot' } , LarkAppMeta . secret ) ;
32+
33+ console . table ( { RobotToken } ) ;
34+
1635export const safeAPI : Middleware < any , any > = async ( context : Context , next ) => {
1736 try {
1837 return await next ( ) ;
@@ -64,7 +83,7 @@ export function splitFrontMatter(raw: string) {
6483 if ( ! frontMatter ) return { markdown : raw } ;
6584
6685 try {
67- const meta = YAML . parse ( frontMatter ) as DataObject ;
86+ const meta = parse ( frontMatter ) as DataObject ;
6887
6988 return { markdown, meta } ;
7089 } catch ( error ) {
@@ -80,34 +99,31 @@ export async function* pageListOf(path: string, prefix = 'pages'): AsyncGenerato
8099 const list = await readdir ( prefix + path , { withFileTypes : true } ) ;
81100
82101 for ( const node of list ) {
83- let { name, path } = node ;
102+ let { name, parentPath } = node ;
84103
85104 if ( name . startsWith ( '.' ) ) continue ;
86105
87106 const isMDX = MDX_pattern . test ( name ) ;
88107
89- ( { name } = Path . parse ( name ) ) ;
90- path = `${ path } /${ name } ` . replace ( new RegExp ( `^${ prefix } ` ) , '' ) ;
108+ name = name . replace ( MDX_pattern , '' ) ;
109+ const path = `${ parentPath } /${ name } ` . replace ( new RegExp ( `^${ prefix } ` ) , '' ) ;
91110
92- if ( node . isFile ( ) ) {
111+ if ( node . isFile ( ) && isMDX ) {
93112 const article : ArticleMeta = { name, path, subs : [ ] } ;
94113
95- if ( isMDX )
96- try {
97- const rawFile = await readFile ( `${ node . path } /${ node . name } ` , { encoding : 'utf-8' } ) ;
114+ const file = await readFile ( `${ parentPath } /${ node . name } ` , 'utf-8' ) ;
98115
99- const { meta } = splitFrontMatter ( rawFile ) ;
116+ const { meta } = splitFrontMatter ( file ) ;
100117
101- if ( meta ) article . meta = meta ;
102- } catch ( error ) {
103- console . error ( `Error reading front matter for ${ node . path } /${ node . name } :` , error ) ;
104- }
105- yield article ;
106- } else if ( node . isDirectory ( ) ) {
107- const subs = await Array . fromAsync ( pageListOf ( path , prefix ) ) ;
118+ if ( meta ) article . meta = meta ;
108119
109- if ( subs [ 0 ] ) yield { name , subs } ;
120+ yield article ;
110121 }
122+ if ( ! node . isDirectory ( ) ) continue ;
123+
124+ const subs = await Array . fromAsync ( pageListOf ( path , prefix ) ) ;
125+
126+ if ( subs [ 0 ] ) yield { name, subs } ;
111127 }
112128}
113129
0 commit comments