Skip to content

Commit 6f8d65a

Browse files
authoredDec 29, 2020
refactor: split server into its own file (#224)
1 parent 6e49545 commit 6f8d65a

File tree

2 files changed

+55
-45
lines changed

2 files changed

+55
-45
lines changed
 

‎src/index.ts

+6-45
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import {EventEmitter} from 'events';
2-
import {request, GaxiosResponse} from 'gaxios';
2+
import {URL} from 'url';
33
import * as http from 'http';
4-
import enableDestroy = require('server-destroy');
5-
import finalhandler = require('finalhandler');
6-
import serveStatic = require('serve-static');
74
import * as fs from 'fs';
85
import * as util from 'util';
96
import * as path from 'path';
10-
import * as marked from 'marked';
7+
8+
import {request, GaxiosResponse} from 'gaxios';
119
import PQueue, {DefaultAddOptions} from 'p-queue';
10+
import PriorityQueue from 'p-queue/dist/priority-queue';
1211
import * as globby from 'glob';
1312

1413
import {getLinks} from './links';
15-
import {URL} from 'url';
16-
import PriorityQueue from 'p-queue/dist/priority-queue';
14+
import {startWebServer} from './server';
1715

1816
const stat = util.promisify(fs.stat);
19-
const readFile = util.promisify(fs.readFile);
2017
const glob = util.promisify(globby);
2118

2219
export interface CheckOptions {
@@ -85,12 +82,11 @@ export class LinkChecker extends EventEmitter {
8582
const hasHttpPaths = options.path.find(x => x.startsWith('http'));
8683
if (!hasHttpPaths) {
8784
const port = options.port || 5000 + Math.round(Math.random() * 1000);
88-
server = await this.startWebServer(
85+
server = await startWebServer(
8986
options.serverRoot!,
9087
port,
9188
options.markdown
9289
);
93-
enableDestroy(server);
9490
for (let i = 0; i < options.path.length; i++) {
9591
if (options.path[i].startsWith('/')) {
9692
options.path[i] = options.path[i].slice(1);
@@ -244,44 +240,9 @@ export class LinkChecker extends EventEmitter {
244240
}
245241
}
246242
}
247-
248243
return options;
249244
}
250245

251-
/**
252-
* Spin up a local HTTP server to serve static requests from disk
253-
* @param root The local path that should be mounted as a static web server
254-
* @param port The port on which to start the local web server
255-
* @param markdown If markdown should be automatically compiled and served
256-
* @private
257-
* @returns Promise that resolves with the instance of the HTTP server
258-
*/
259-
private async startWebServer(root: string, port: number, markdown?: boolean) {
260-
return new Promise<http.Server>((resolve, reject) => {
261-
const serve = serveStatic(root);
262-
const server = http
263-
.createServer(async (req, res) => {
264-
const pathParts = req.url!.split('/').filter(x => !!x);
265-
if (pathParts.length > 0) {
266-
const ext = path.extname(pathParts[pathParts.length - 1]);
267-
if (markdown && ext.toLowerCase() === '.md') {
268-
const filePath = path.join(path.resolve(root), req.url!);
269-
const data = await readFile(filePath, {encoding: 'utf-8'});
270-
const result = marked(data, {gfm: true});
271-
res.writeHead(200, {
272-
'content-type': 'text/html',
273-
});
274-
res.end(result);
275-
return;
276-
}
277-
}
278-
return serve(req, res, finalhandler(req, res) as () => void);
279-
})
280-
.listen(port, () => resolve(server))
281-
.on('error', reject);
282-
});
283-
}
284-
285246
/**
286247
* Crawl a given url with the provided options.
287248
* @pram opts List of options used to do the crawl

‎src/server.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as http from 'http';
2+
import * as path from 'path';
3+
import * as util from 'util';
4+
import * as fs from 'fs';
5+
import * as marked from 'marked';
6+
import finalhandler = require('finalhandler');
7+
import serveStatic = require('serve-static');
8+
import enableDestroy = require('server-destroy');
9+
10+
const readFile = util.promisify(fs.readFile);
11+
12+
/**
13+
* Spin up a local HTTP server to serve static requests from disk
14+
* @param root The local path that should be mounted as a static web server
15+
* @param port The port on which to start the local web server
16+
* @param markdown If markdown should be automatically compiled and served
17+
* @private
18+
* @returns Promise that resolves with the instance of the HTTP server
19+
*/
20+
export async function startWebServer(
21+
root: string,
22+
port: number,
23+
markdown?: boolean
24+
) {
25+
return new Promise<http.Server>((resolve, reject) => {
26+
const serve = serveStatic(root);
27+
const server = http
28+
.createServer(async (req, res) => {
29+
const pathParts = req.url!.split('/').filter(x => !!x);
30+
if (pathParts.length > 0) {
31+
const ext = path.extname(pathParts[pathParts.length - 1]);
32+
if (markdown && ext.toLowerCase() === '.md') {
33+
const filePath = path.join(path.resolve(root), req.url!);
34+
const data = await readFile(filePath, {encoding: 'utf-8'});
35+
const result = marked(data, {gfm: true});
36+
res.writeHead(200, {
37+
'content-type': 'text/html',
38+
});
39+
res.end(result);
40+
return;
41+
}
42+
}
43+
return serve(req, res, finalhandler(req, res) as () => void);
44+
})
45+
.listen(port, () => resolve(server))
46+
.on('error', reject);
47+
enableDestroy(server);
48+
});
49+
}

0 commit comments

Comments
 (0)
Please sign in to comment.