|
1 | 1 | import {EventEmitter} from 'events';
|
2 |
| -import {request, GaxiosResponse} from 'gaxios'; |
| 2 | +import {URL} from 'url'; |
3 | 3 | import * as http from 'http';
|
4 |
| -import enableDestroy = require('server-destroy'); |
5 |
| -import finalhandler = require('finalhandler'); |
6 |
| -import serveStatic = require('serve-static'); |
7 | 4 | import * as fs from 'fs';
|
8 | 5 | import * as util from 'util';
|
9 | 6 | import * as path from 'path';
|
10 |
| -import * as marked from 'marked'; |
| 7 | + |
| 8 | +import {request, GaxiosResponse} from 'gaxios'; |
11 | 9 | import PQueue, {DefaultAddOptions} from 'p-queue';
|
| 10 | +import PriorityQueue from 'p-queue/dist/priority-queue'; |
12 | 11 | import * as globby from 'glob';
|
13 | 12 |
|
14 | 13 | import {getLinks} from './links';
|
15 |
| -import {URL} from 'url'; |
16 |
| -import PriorityQueue from 'p-queue/dist/priority-queue'; |
| 14 | +import {startWebServer} from './server'; |
17 | 15 |
|
18 | 16 | const stat = util.promisify(fs.stat);
|
19 |
| -const readFile = util.promisify(fs.readFile); |
20 | 17 | const glob = util.promisify(globby);
|
21 | 18 |
|
22 | 19 | export interface CheckOptions {
|
@@ -85,12 +82,11 @@ export class LinkChecker extends EventEmitter {
|
85 | 82 | const hasHttpPaths = options.path.find(x => x.startsWith('http'));
|
86 | 83 | if (!hasHttpPaths) {
|
87 | 84 | const port = options.port || 5000 + Math.round(Math.random() * 1000);
|
88 |
| - server = await this.startWebServer( |
| 85 | + server = await startWebServer( |
89 | 86 | options.serverRoot!,
|
90 | 87 | port,
|
91 | 88 | options.markdown
|
92 | 89 | );
|
93 |
| - enableDestroy(server); |
94 | 90 | for (let i = 0; i < options.path.length; i++) {
|
95 | 91 | if (options.path[i].startsWith('/')) {
|
96 | 92 | options.path[i] = options.path[i].slice(1);
|
@@ -244,44 +240,9 @@ export class LinkChecker extends EventEmitter {
|
244 | 240 | }
|
245 | 241 | }
|
246 | 242 | }
|
247 |
| - |
248 | 243 | return options;
|
249 | 244 | }
|
250 | 245 |
|
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 |
| - |
285 | 246 | /**
|
286 | 247 | * Crawl a given url with the provided options.
|
287 | 248 | * @pram opts List of options used to do the crawl
|
|
0 commit comments