Skip to content

Commit be77d9e

Browse files
committed
fix: 前面那一坨大概就是说把CommonJS改成ES Module了
1 parent 03c1d7c commit be77d9e

File tree

4 files changed

+79
-54
lines changed

4 files changed

+79
-54
lines changed

_script/append-js-ext-transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Append .js to relative import/export/source literal specifiers that lack extension.
33
* Skips node: and absolute/package imports and json/vue/css etc.
44
*/
5-
module.exports = function (fileInfo, api) {
5+
export default function (fileInfo, api) {
66
const j = api.jscodeshift
77
const root = j(fileInfo.source)
88

_script/cjs-to-esm-transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Note: this is a best-effort codemod and will need manual review.
99
*/
1010

11-
module.exports = function (fileInfo, api) {
11+
export default function (fileInfo, api) {
1212
const j = api.jscodeshift
1313
const root = j(fileInfo.source)
1414

packages/mitmproxy/src/index.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
const mitmproxy = require('./lib/proxy')
2-
const proxyConfig = require('./lib/proxy/common/config')
3-
const speedTest = require('./lib/speed/index.js')
4-
const ProxyOptions = require('./options')
5-
const log = require('./utils/util.log.server')
6-
const { fireError, fireStatus } = require('./utils/util.process')
1+
import proxyLib from './lib/proxy/index.js'
2+
import proxyConfig, { setDefaultCABasePath } from './lib/proxy/common/config.js'
3+
import speedTest from './lib/speed/index.js'
4+
import ProxyOptions from './options.js'
5+
import log from './utils/util.log.server.js'
6+
import utilProcess from './utils/util.process.js'
7+
8+
const { createProxy } = proxyLib
9+
const { action } = speedTest
10+
const { fireError, fireStatus } = utilProcess
11+
const { info, error, warn } = log
12+
// const mitmproxy = require('./lib/proxy')
13+
// const proxyConfig = require('./lib/proxy/common/config')
14+
// const speedTest = require('./lib/speed/index.js')
15+
// const ProxyOptions = require('./options')
16+
// const log = require('./utils/util.log.server')
17+
// const { fireError, fireStatus } = require('./utils/util.process')
718

819
let servers = []
920

@@ -13,7 +24,7 @@ const api = {
1324
const setting = config.setting
1425
if (setting) {
1526
if (setting.userBasePath) {
16-
proxyConfig.setDefaultCABasePath(setting.userBasePath)
27+
setDefaultCABasePath(setting.userBasePath)
1728
}
1829
}
1930

@@ -23,7 +34,7 @@ const api = {
2334
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'
2435
}
2536
// log.info('启动代理服务时的配置:', JSON.stringify(proxyOptions, null, '\t'))
26-
const newServers = mitmproxy.createProxy(proxyOptions, (server, port, host, ssl) => {
37+
const newServers = createProxy(proxyOptions, (server, port, host, ssl) => {
2738
fireStatus(true)
2839
log.info(`代理服务已启动:${host}:${port}, ssl: ${ssl}`)
2940
})
@@ -54,22 +65,22 @@ const api = {
5465
server.close((err) => {
5566
if (err && err.code !== 'ERR_SERVER_NOT_RUNNING') {
5667
if (err.code === 'ERR_SERVER_NOT_RUNNING') {
57-
log.info('代理服务未运行,无需关闭')
68+
info('代理服务未运行,无需关闭')
5869
resolve()
5970
} else {
60-
log.error('代理服务关闭失败:', err)
71+
error('代理服务关闭失败:', err)
6172
reject(err)
6273
}
6374
return
6475
}
6576

66-
log.info('代理服务关闭成功')
77+
info('代理服务关闭成功')
6778
resolve()
6879
})
6980
}
7081
servers = []
7182
} else {
72-
log.info('server is null, no need to close.')
83+
info('server is null, no need to close.')
7384
fireStatus(false)
7485
resolve()
7586
}
@@ -79,16 +90,16 @@ const api = {
7990

8091
function registerProcessListener () {
8192
process.on('message', (msg) => {
82-
log.info('child get msg:', JSON.stringify(msg))
93+
info('child get msg:', JSON.stringify(msg))
8394
if (msg.type === 'action') {
8495
api[msg.event.key](msg.event.params)
8596
} else if (msg.type === 'speed') {
86-
speedTest.action(msg.event)
97+
action(msg.event)
8798
}
8899
})
89100

90101
process.on('SIGINT', () => {
91-
log.info('on sigint : closed ')
102+
info('on sigint : closed ')
92103
process.exit(0)
93104
})
94105

@@ -98,29 +109,29 @@ function registerProcessListener () {
98109
// log.error(err.errno)
99110
return
100111
}
101-
log.error('Process uncaughtException:', err)
112+
error('Process uncaughtException:', err)
102113
})
103114

104115
process.on('unhandledRejection', (err, p) => {
105-
log.info('Process unhandledRejection at: Promise', p, 'err:', err)
116+
info('Process unhandledRejection at: Promise', p, 'err:', err)
106117
// application specific logging, throwing an error, or other logic here
107118
})
108119
process.on('uncaughtExceptionMonitor', (err, origin) => {
109-
log.info('Process uncaughtExceptionMonitor:', err, origin)
120+
info('Process uncaughtExceptionMonitor:', err, origin)
110121
})
111122
process.on('exit', (code, signal) => {
112-
log.info('代理服务进程被关闭:', code, signal)
123+
info('代理服务进程被关闭:', code, signal)
113124
})
114125
// Aviod keeping print log before exit
115126
// process.on('beforeExit', (code, signal) => {
116127
// log.info('Process beforeExit event with code: ', code, signal)
117128
// })
118129
process.on('SIGPIPE', (code, signal) => {
119-
log.warn('sub Process SIGPIPE', code, signal)
130+
warn('sub Process SIGPIPE', code, signal)
120131
})
121132
}
122133

123-
module.exports = {
134+
export default {
124135
...api,
125136
config: proxyConfig,
126137
log,

packages/mitmproxy/src/json.js

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,56 @@
1-
const logOrConsole = require('@docmirror/dev-sidecar/src/utils/util.log-or-console.js')
2-
let JSON5 = require('json5')
3-
if (JSON5.default) {
4-
JSON5 = JSON5.default
5-
}
1+
import logOrConsole from '@docmirror/dev-sidecar/src/utils/util.log-or-console.js'
2+
import JSON5 from 'json5'
63

7-
module.exports = {
8-
parse (str, defaultValue) {
9-
if (str == null || str.length < 2) {
10-
return defaultValue || {}
11-
}
4+
// const logOrConsole = require('@docmirror/dev-sidecar/src/utils/util.log-or-console.js')
5+
// let JSON5 = require('json5')
6+
// if (JSON5.default) {
7+
// JSON5 = JSON5.default
8+
// }
129

13-
str = str.toString()
10+
export function parse (str, defaultValue) {
11+
if (str == null || str.length < 2) {
12+
return defaultValue || {}
13+
}
1414

15-
if (defaultValue != null) {
15+
str = str.toString()
16+
17+
if (defaultValue != null) {
18+
try {
19+
return JSON5.parse(str)
20+
} catch (e) {
21+
logOrConsole.error(`JSON5解析失败: ${e.message},JSON内容:\r\n`, str)
22+
return defaultValue
23+
}
24+
} else {
25+
try {
26+
return JSON.parse(str)
27+
} catch (e1) {
1628
try {
1729
return JSON5.parse(str)
18-
} catch (e) {
19-
logOrConsole.error(`JSON5解析失败: ${e.message},JSON内容:\r\n`, str)
20-
return defaultValue
30+
} catch (e2) {
31+
logOrConsole.error(`JSON解析失败: ${e2.message},JSON内容:\r\n`, str)
32+
return {}
2133
}
22-
} else {
23-
return JSON5.parse(str)
2434
}
25-
},
26-
stringify (obj) {
27-
return JSON.stringify(obj, null, '\t')
28-
},
29-
30-
// 仅用于记录日志时使用
31-
stringify2 (obj) {
35+
}
36+
}
37+
export function stringify (obj) {
38+
return JSON.stringify(obj, null, '\t')
39+
}
40+
export function stringify2 (obj) {
41+
try {
42+
return JSON.stringify(obj)
43+
} catch {
3244
try {
33-
return JSON.stringify(obj)
45+
return JSON5.stringify(obj)
3446
} catch {
35-
try {
36-
return JSON5.stringify(obj)
37-
} catch {
38-
return obj
39-
}
47+
return obj
4048
}
41-
},
49+
}
50+
}
51+
52+
export default {
53+
parse,
54+
stringify,
55+
stringify2,
4256
}

0 commit comments

Comments
 (0)