-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (115 loc) · 3.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// @ts-check
const Logger = require('3h-log'),
{ join, normalize } = require('path'),
{ createServer } = require('http'),
lib = require('./lib'),
DEFAULT_TYPES = require('./types');
exports.lib = lib;
exports.DEFAULT_TYPES = DEFAULT_TYPES;
/**
* @typedef ServeOptions
* @property {string} root
* @property {number} port
* @property {string} timeFormat
* @property {boolean} cache
* @property {boolean} zip
* @property {'info' | 'log' | 'debug'} logLevel
* @property {boolean} silent
* @property {string | null} separator
* @property {Map<string, string>} types
* @property {RegExp | null} forbiddenPattern
* @property {RegExp | null} serverlessPattern
* @property {string | null} serverlessExtension
* @property {string | null} spaPage
* @property {string | null} defaultPage
* @property {string | null} defaultExtension
* @property {string | null} fallbackPage
*/
/** @type {ServeOptions} */
const DEFAULT_SERVE_OPTIONS = {
root: '.',
port: 8080,
timeFormat: '[YYYY-MM-DD HH:mm:SS.sss]',
cache: true,
zip: true,
logLevel: 'info',
silent: false,
separator: '-'.repeat(10),
types: DEFAULT_TYPES,
forbiddenPattern: null,
serverlessPattern: null,
serverlessExtension: '.js',
spaPage: '200.html',
defaultPage: 'index.html',
defaultExtension: '.html',
fallbackPage: '404.html',
};
exports.DEFAULT_SERVE_OPTIONS = DEFAULT_SERVE_OPTIONS;
/** @param {Partial<ServeOptions>} options */
exports.serve = options => {
options = lib.merge(DEFAULT_SERVE_OPTIONS, options || Object.create(null));
const logger = new Logger({
timeFormat: options.timeFormat,
});
logger.setLevel(options.logLevel);
if (options.silent) {
logger.disableAll();
}
const server = createServer((request, response) => {
if (options.separator) {
logger.log(options.separator);
}
logger.log(request.method + ' ' + request.url);
const url = request.url.split('?')[0],
path = normalize(join(options.root, url));
if (
!path.startsWith(options.root) ||
(options.forbiddenPattern && options.forbiddenPattern.test(url))
) {
return lib.endWithCode(response, 403, logger);
}
try {
lib.solve(request, response, path, {
root: options.root,
cache: options.cache,
zip: options.zip,
types: options.types,
spaPage: options.spaPage,
defaultPage: options.defaultPage,
defaultExtension: options.defaultExtension,
forbiddenPattern: options.forbiddenPattern,
serverlessPattern: options.serverlessPattern,
serverlessExtension: options.serverlessExtension,
fallbackPage: options.fallbackPage,
logger,
});
} catch (error) {
logger.error(error);
if (!response.finished) {
lib.endWithCode(response, 500, logger);
}
}
}).on('error', error => {
logger.error(String(error));
process.exit(1);
}).on('close', () => {
if (options.separator) {
logger.log(options.separator);
}
logger.info('Server closed');
});
process.on('SIGINT', () => {
server.close(error => {
if (error) {
logger.error(String(error));
process.exit(1);
} else {
process.exit(0);
}
});
});
return server.listen(options.port, () => {
logger.info(`Server started. (port: ${options.port}; pid: ${process.pid})`);
logger.info('Press Ctrl+C to stop the server.');
});
};