11const express = require ( "express" ) ;
2- const mf2 = require ( "microformat-node " ) ;
2+ const { mf2 } = require ( "microformats-parser " ) ;
33const undici = require ( "undici" ) ;
44const querystring = require ( "querystring" ) ;
55const pkg = require ( "./package.json" ) ;
66const app = express ( ) ;
77const port = process . env . PORT || 9000 ;
88
9+ function getDependencyVersion ( dependencyName ) {
10+ const fs = require ( 'fs' ) ;
11+ const lockfile = require ( '@yarnpkg/lockfile' ) ;
12+ const parsed = lockfile . parse ( fs . readFileSync ( "./yarn.lock" , "utf-8" ) ) ;
13+ if ( parsed . type !== "success" ) return "unknown" ;
14+ const dependency = parsed . object [ `${ dependencyName } @${ pkg . dependencies [ dependencyName ] } ` ] ;
15+ if ( dependency === undefined ) return "unknown" ;
16+ return dependency . version ;
17+ }
18+ const mf2version = getDependencyVersion ( "microformats-parser" ) ;
19+
920function htmlToMf2 ( url , html , res ) {
10- mf2 . get ( { baseUrl : url , html } , ( err , data ) => {
11- const body = err || data ;
12- res
13- . header ( "content-type" , "application/json; charset=UTF-8" )
14- . send ( JSON . stringify ( body , null , 2 ) ) ;
15- } ) ;
21+ const body = mf2 ( html , { baseUrl : url } ) ;
22+ res
23+ . header ( "content-type" , "application/json; charset=UTF-8" )
24+ . send ( JSON . stringify ( body , null , 2 ) ) ;
1625}
1726
1827app . set ( "view engine" , "ejs" ) ;
@@ -31,7 +40,7 @@ app.get("/", async (req, res) => {
3140 htmlToMf2 ( url , text , res ) ;
3241 } else {
3342 res . render ( "index.html.ejs" , {
34- version : `${ pkg . version } (lib: ${ mf2 . version } )` ,
43+ version : `${ pkg . version } (lib: ${ mf2version } )` ,
3544 } ) ;
3645 }
3746} ) ;
0 commit comments