unplugin-https-reverse-proxy 0.0.23 → 1.0.0-beta.0

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/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, once } from './utils'
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((stop) => {
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,41 +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
- try {
169
- _stop && await _stop(() => {
170
- _close && _close(callback)
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
126
  // webpack 4 not support yet
181
127
  options.https = false
182
- registerExit(async () => {
183
- try {
184
- _stop && await _stop()
185
- }
186
- catch (e) {
187
- consola.error(e)
188
- }
189
- })
190
128
  }
191
129
 
192
130
  caddy = new CaddyInstant()
193
131
 
194
132
  caddy.run(source, target, {
195
133
  ...options,
196
- }).then((stop) => {
134
+ }).then(() => {
197
135
  const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
198
136
 
199
137
  compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
200
138
  consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
201
-
202
- !_stop && (_stop = once(stop))
203
139
  })
204
140
  }).catch((e) => {
205
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 {