forked from Tencent/nohost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (80 loc) · 2.81 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
/**
* Tencent is pleased to support the open source community by making nohost-环境配置与抓包调试平台 available.
* Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. The below software in
* this distribution may have been modified by THL A29 Limited ("Tencent Modifications").
* All Tencent Modifications are Copyright (C) THL A29 Limited.
* nohost-环境配置与抓包调试平台 is licensed under the MIT License except for the third-party components listed below.
*/
// 避免第三方模块没处理好异常导致程序crash
require('whistle/lib/util/patch');
const fse = require('fs-extra');
const fs = require('fs');
const path = require('path');
const { getWhistlePath } = require('whistle/lib/config');
const pkg = require('./package.json');
const initConfig = require('./lib/config');
const PURE_URL_RE = /^(#?(?:https?:)?\/\/[\w.-]+[^?#]*)/;
// 设置存储路径
const WHISTLE_PATH = process.env.NOHOST_PATH || getWhistlePath();
process.env.WHISTLE_PATH = WHISTLE_PATH;
fse.ensureDirSync(process.env.WHISTLE_PATH); // eslint-disable-line
const getPureUrl = (url) => {
if (!url || !PURE_URL_RE.test(url)) {
return;
}
return RegExp.$1.replace(/\/+$/, '');
};
const getErrorStack = (err) => {
if (!err) {
return '';
}
let stack;
try {
stack = err.stack;
} catch (e) {}
stack = stack || err.message || err;
const result = [
`From: nohost@${pkg.version}`,
`Node: ${process.version}`,
`Date: ${new Date().toLocaleString()}`,
stack];
return result.join('\r\n');
};
const handleUncaughtException = (err) => {
if (!err || err.code !== 'ERR_IPC_CHANNEL_CLOSED') {
const stack = getErrorStack(err);
fs.writeFileSync(path.join(WHISTLE_PATH, 'nohost.log'), `\r\n${stack}\r\n`, { flag: 'a' }); // eslint-disable-line
console.error(stack); // eslint-disable-line
}
process.exit(1);
};
process.on('unhandledRejection', handleUncaughtException);
process.on('uncaughtException', handleUncaughtException);
module.exports = (options, cb) => {
if (typeof options === 'function') {
cb = options;
options = {};
} else if (!options) {
options = {};
}
if (options.__maxHttpHeaderSize > 0) {
process.env.PFORK_MAX_HTTP_HEADER_SIZE = options.__maxHttpHeaderSize;
}
if (options.debugMode) {
const mode = typeof options.mode === 'string' ? options.mode.trim().split(/\s*[|,&]\s*/) : [];
if (mode.includes('prod') || mode.includes('production')) {
options.debugMode = false;
} else {
process.env.PFORK_MODE = 'bind';
}
}
const redirect = getPureUrl(options.redirect);
if (redirect && redirect[0] === '#') {
options.redirect = redirect.substring(1);
options.deprecated = true;
} else {
options.redirect = redirect;
}
initConfig(options);
require('./lib')(options, cb); // eslint-disable-line
};