Skip to content

Commit

Permalink
fix: remove express dependency (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Dec 2, 2020
1 parent 8913f87 commit 429b325
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -21,23 +21,25 @@
"dependencies": {
"chalk": "^4.0.0",
"cheerio": "^1.0.0-rc.2",
"express": "^4.17.1",
"finalhandler": "^1.1.2",
"gaxios": "^4.0.0",
"jsonexport": "^3.0.0",
"marked": "^1.2.5",
"meow": "^8.0.0",
"p-queue": "^6.2.1",
"serve-static": "^1.14.1",
"server-destroy": "^1.0.1",
"update-notifier": "^5.0.0"
},
"devDependencies": {
"@types/chai": "^4.2.7",
"@types/cheerio": "^0.22.10",
"@types/express": "^4.17.9",
"@types/finalhandler": "^1.1.0",
"@types/marked": "^1.2.0",
"@types/meow": "^5.0.0",
"@types/mocha": "^8.0.0",
"@types/node": "^12.7.12",
"@types/serve-static": "^1.13.8",
"@types/server-destroy": "^1.0.0",
"@types/sinon": "^9.0.0",
"@types/update-notifier": "^5.0.0",
Expand Down
50 changes: 26 additions & 24 deletions src/index.ts
Expand Up @@ -2,7 +2,8 @@ import {EventEmitter} from 'events';
import * as gaxios from 'gaxios';
import * as http from 'http';
import enableDestroy = require('server-destroy');
import * as express from 'express';
import finalhandler = require('finalhandler');
import serveStatic = require('serve-static');
import * as fs from 'fs';
import * as util from 'util';
import * as path from 'path';
Expand Down Expand Up @@ -179,9 +180,8 @@ export class LinkChecker extends EventEmitter {
if (s.isFile()) {
const pathParts = options.path[0].split(path.sep);
options.path = [path.sep + pathParts[pathParts.length - 1]];
options.serverRoot = pathParts
.slice(0, pathParts.length - 1)
.join(path.sep);
options.serverRoot =
pathParts.slice(0, pathParts.length - 1).join(path.sep) || '.';
} else {
options.serverRoot = options.path[0];
options.path = '/';
Expand All @@ -201,27 +201,29 @@ export class LinkChecker extends EventEmitter {
* @returns Promise that resolves with the instance of the HTTP server
*/
private async startWebServer(root: string, port: number, markdown?: boolean) {
const app = express()
.use(async (req, res, next) => {
if (!markdown) {
return next();
}
const pathParts = req.path.split('/').filter(x => !!x);
const ext = path.extname(pathParts[pathParts.length - 1]);
if (ext.toLowerCase() === '.md') {
const filePath = path.join(path.resolve(root), req.path);
const data = await readFile(filePath, {encoding: 'utf-8'});
const result = marked(data, {gfm: true});
res.send(result).end();
return;
}
return next();
})
.use(express.static(path.resolve(root)));
const server = await new Promise<http.Server>(resolve => {
const s = app.listen(port, () => resolve(s));
return new Promise<http.Server>((resolve, reject) => {
const serve = serveStatic(root);
const server = http
.createServer(async (req, res) => {
const pathParts = req.url!.split('/').filter(x => !!x);
if (pathParts.length > 0) {
const ext = path.extname(pathParts[pathParts.length - 1]);
if (markdown && ext.toLowerCase() === '.md') {
const filePath = path.join(path.resolve(root), req.url!);
const data = await readFile(filePath, {encoding: 'utf-8'});

This comment has been minimized.

Copy link
@XhmikosR

This comment has been minimized.

Copy link
@JustinBeckwith

JustinBeckwith Jan 10, 2021

Author Owner

Interesting. It's unclear why this is an actual problem though? What's happening here - if you run linkinator against a directory with markdown in it, I will read the file, convert it to markdown, then serve it. The server-root helps with identifying where on disk the file is stored. Any ideas on why it's complaining?

This comment has been minimized.

Copy link
@XhmikosR

XhmikosR Jan 10, 2021

Contributor

BTW now with CodeQL you should see the warning in the security repo tab too.

Not sure how exactly you should proceed, lgtm has some help https://lgtm.com/rules/1971530250/ and CodeQL should too if you hover/click on the CWE tags.

I guess there should be some kind of validation of what's allowed here to be scanned?

This comment has been minimized.

const result = marked(data, {gfm: true});
res.writeHead(200, {
'content-type': 'text/html',
});
res.end(result);
return;
}
}
return serve(req, res, finalhandler(req, res) as () => void);
})
.listen(port, () => resolve(server))
.on('error', reject);
});
return server;
}

/**
Expand Down

0 comments on commit 429b325

Please sign in to comment.