Skip to content

Commit

Permalink
refactor: split server into its own file (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Dec 29, 2020
1 parent 6e49545 commit 6f8d65a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 45 deletions.
51 changes: 6 additions & 45 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import {EventEmitter} from 'events';
import {request, GaxiosResponse} from 'gaxios';
import {URL} from 'url';
import * as http from 'http';
import enableDestroy = require('server-destroy');
import finalhandler = require('finalhandler');
import serveStatic = require('serve-static');
import * as fs from 'fs';
import * as util from 'util';
import * as path from 'path';
import * as marked from 'marked';

import {request, GaxiosResponse} from 'gaxios';
import PQueue, {DefaultAddOptions} from 'p-queue';
import PriorityQueue from 'p-queue/dist/priority-queue';
import * as globby from 'glob';

import {getLinks} from './links';
import {URL} from 'url';
import PriorityQueue from 'p-queue/dist/priority-queue';
import {startWebServer} from './server';

const stat = util.promisify(fs.stat);
const readFile = util.promisify(fs.readFile);
const glob = util.promisify(globby);

export interface CheckOptions {
Expand Down Expand Up @@ -85,12 +82,11 @@ export class LinkChecker extends EventEmitter {
const hasHttpPaths = options.path.find(x => x.startsWith('http'));
if (!hasHttpPaths) {
const port = options.port || 5000 + Math.round(Math.random() * 1000);
server = await this.startWebServer(
server = await startWebServer(
options.serverRoot!,
port,
options.markdown
);
enableDestroy(server);
for (let i = 0; i < options.path.length; i++) {
if (options.path[i].startsWith('/')) {
options.path[i] = options.path[i].slice(1);
Expand Down Expand Up @@ -244,44 +240,9 @@ export class LinkChecker extends EventEmitter {
}
}
}

return options;
}

/**
* Spin up a local HTTP server to serve static requests from disk
* @param root The local path that should be mounted as a static web server
* @param port The port on which to start the local web server
* @param markdown If markdown should be automatically compiled and served
* @private
* @returns Promise that resolves with the instance of the HTTP server
*/
private async startWebServer(root: string, port: number, markdown?: boolean) {
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'});
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);
});
}

/**
* Crawl a given url with the provided options.
* @pram opts List of options used to do the crawl
Expand Down
49 changes: 49 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as http from 'http';
import * as path from 'path';
import * as util from 'util';
import * as fs from 'fs';
import * as marked from 'marked';
import finalhandler = require('finalhandler');
import serveStatic = require('serve-static');
import enableDestroy = require('server-destroy');

const readFile = util.promisify(fs.readFile);

/**
* Spin up a local HTTP server to serve static requests from disk
* @param root The local path that should be mounted as a static web server
* @param port The port on which to start the local web server
* @param markdown If markdown should be automatically compiled and served
* @private
* @returns Promise that resolves with the instance of the HTTP server
*/
export async function startWebServer(
root: string,
port: number,
markdown?: boolean
) {
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'});
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);
enableDestroy(server);
});
}

0 comments on commit 6f8d65a

Please sign in to comment.