unplugin-https-reverse-proxy 0.0.22 → 0.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -2
- package/dist/{chunk-NNO3ZUSU.js → chunk-3RTK3FSZ.js} +0 -10
- package/dist/{chunk-5MGNE7J4.js → chunk-Q7WB6WMX.js} +1 -1
- package/dist/{chunk-BTWCTT26.js → chunk-TVCN6ESH.js} +112 -149
- package/dist/{chunk-Q7NSASV3.js → chunk-W4RHTMNZ.js} +1 -1
- package/dist/esbuild.cjs +115 -160
- package/dist/esbuild.js +2 -2
- package/dist/index.cjs +115 -160
- package/dist/index.js +2 -2
- package/dist/nuxt.cjs +115 -160
- package/dist/nuxt.js +4 -4
- package/dist/rollup.cjs +115 -160
- package/dist/rollup.js +2 -2
- package/dist/utils.cjs +2 -13
- package/dist/utils.d.cts +1 -2
- package/dist/utils.d.ts +1 -2
- package/dist/utils.js +3 -5
- package/dist/vite.cjs +115 -160
- package/dist/vite.js +3 -3
- package/dist/webpack.cjs +115 -160
- package/dist/webpack.js +3 -3
- package/package.json +1 -1
- package/src/caddy/index.ts +53 -78
- package/src/host/index.ts +54 -0
- package/src/index.ts +8 -70
- package/src/utils.ts +0 -10
package/src/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { UnpluginFactory } from 'unplugin'
|
|
|
4
4
|
import { createUnplugin } from 'unplugin'
|
|
5
5
|
import type { ResolvedConfig, ViteDevServer } from 'vite'
|
|
6
6
|
import c from 'picocolors'
|
|
7
|
-
import { consola, isAdmin
|
|
7
|
+
import { consola, isAdmin } from './utils'
|
|
8
8
|
import type { Options } from './types'
|
|
9
9
|
import { CaddyInstant } from './caddy'
|
|
10
10
|
|
|
@@ -12,61 +12,14 @@ let config: ResolvedConfig
|
|
|
12
12
|
|
|
13
13
|
let caddy: CaddyInstant
|
|
14
14
|
|
|
15
|
-
let _stop: (callback?: () => any) => Promise<any>
|
|
16
15
|
let _servce: ViteDevServer
|
|
17
16
|
|
|
18
|
-
function registerExit(clear: (() => Promise<any>) | (() => any)) {
|
|
19
|
-
process.on('beforeExit', async (_code) => {
|
|
20
|
-
await clear()
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
process.on('uncaughtException', async (err) => {
|
|
24
|
-
console.error('An uncaught error occurred!', err.stack)
|
|
25
|
-
await clear()
|
|
26
|
-
process.nextTick(() => {
|
|
27
|
-
process.exit(1)
|
|
28
|
-
})
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
process.on('unhandledRejection', async (reason, promise) => {
|
|
32
|
-
console.error('Unhandled Rejection at:', promise, 'reason:', reason)
|
|
33
|
-
await clear()
|
|
34
|
-
process.nextTick(() => {
|
|
35
|
-
process.exit(1)
|
|
36
|
-
})
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
process.once('SIGINT', async () => {
|
|
40
|
-
await clear()
|
|
41
|
-
process.exit(0)
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
process.once('SIGTERM', async () => {
|
|
45
|
-
await clear()
|
|
46
|
-
process.exit(0)
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
|
|
50
17
|
export const unpluginFactory: UnpluginFactory<Options> = options => ({
|
|
51
18
|
name: 'unplugin-https-reverse-proxy',
|
|
52
19
|
enforce: 'pre',
|
|
53
20
|
vite: {
|
|
54
21
|
configResolved(_config) {
|
|
55
22
|
config = _config
|
|
56
|
-
registerExit(async () => {
|
|
57
|
-
try {
|
|
58
|
-
if (_servce) {
|
|
59
|
-
_servce.httpServer?.close(async () => {
|
|
60
|
-
_stop && await _stop()
|
|
61
|
-
})
|
|
62
|
-
_servce.close()
|
|
63
|
-
}
|
|
64
|
-
_stop && await _stop()
|
|
65
|
-
}
|
|
66
|
-
catch (e) {
|
|
67
|
-
consola.error(e)
|
|
68
|
-
}
|
|
69
|
-
})
|
|
70
23
|
},
|
|
71
24
|
configureServer(server) {
|
|
72
25
|
if (!isAdmin()) {
|
|
@@ -114,10 +67,9 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
|
|
|
114
67
|
caddy.run(source, target, {
|
|
115
68
|
base,
|
|
116
69
|
...options,
|
|
117
|
-
}).then((
|
|
70
|
+
}).then(() => {
|
|
118
71
|
const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
|
|
119
72
|
consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
|
|
120
|
-
!_stop && (_stop = once(stop))
|
|
121
73
|
}).catch((e) => {
|
|
122
74
|
throw e
|
|
123
75
|
})
|
|
@@ -165,39 +117,25 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
|
|
|
165
117
|
const _close = compiler.close?.bind(compiler)
|
|
166
118
|
if (_close) {
|
|
167
119
|
compiler.close = async (callback: Parameters<typeof _close>[0]) => {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
})
|
|
172
|
-
}
|
|
173
|
-
catch (e) {
|
|
174
|
-
consola.error(e)
|
|
175
|
-
process.exit(1)
|
|
176
|
-
}
|
|
120
|
+
process.on('exit', () => {
|
|
121
|
+
_close && _close(callback)
|
|
122
|
+
})
|
|
177
123
|
}
|
|
178
124
|
}
|
|
179
125
|
else {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
_stop && await _stop()
|
|
183
|
-
}
|
|
184
|
-
catch (e) {
|
|
185
|
-
consola.error(e)
|
|
186
|
-
}
|
|
187
|
-
})
|
|
126
|
+
// webpack 4 not support yet
|
|
127
|
+
options.https = false
|
|
188
128
|
}
|
|
189
129
|
|
|
190
130
|
caddy = new CaddyInstant()
|
|
191
131
|
|
|
192
132
|
caddy.run(source, target, {
|
|
193
133
|
...options,
|
|
194
|
-
}).then((
|
|
134
|
+
}).then(() => {
|
|
195
135
|
const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
|
|
196
136
|
|
|
197
137
|
compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
|
|
198
138
|
consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
|
|
199
|
-
|
|
200
|
-
!_stop && (_stop = once(stop))
|
|
201
139
|
})
|
|
202
140
|
}).catch((e) => {
|
|
203
141
|
throw e
|
package/src/utils.ts
CHANGED
|
@@ -6,16 +6,6 @@ export const consola = createConsola({
|
|
|
6
6
|
level: 3,
|
|
7
7
|
}).withTag('reverse-proxy')
|
|
8
8
|
|
|
9
|
-
export function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> {
|
|
10
|
-
let called = false
|
|
11
|
-
return (...args: any[]) => {
|
|
12
|
-
if (called)
|
|
13
|
-
return
|
|
14
|
-
called = true
|
|
15
|
-
return fn(...args)
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
9
|
export function isAdmin() {
|
|
20
10
|
if (process.platform === 'win32') {
|
|
21
11
|
try {
|