unplugin-https-reverse-proxy 0.0.12 → 0.0.14

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.
@@ -1,4 +1,8 @@
1
1
  import { createServer } from 'node:net'
2
+ import ora from 'ora'
3
+ import { consola } from '../utils'
4
+
5
+ let spinner: ora.Ora
2
6
 
3
7
  /**
4
8
  * @returns whether the port is in use
@@ -23,3 +27,40 @@ export function tryPort(port: number) {
23
27
  }
24
28
  })
25
29
  }
30
+
31
+ function getNumber(number: number) {
32
+ if (number <= 0)
33
+ return '0%'
34
+ else if (number < 100)
35
+ return `${number.toFixed(2)}%`
36
+ else
37
+ return '100%'
38
+ }
39
+
40
+ export function logProgress(percent: number | string) {
41
+ if (!spinner) {
42
+ // empty a line before start
43
+ consola.log('')
44
+ spinner = ora({
45
+ text: 'Start Download Caddy: 0%',
46
+ indent: 2,
47
+ })
48
+ spinner.start()
49
+ }
50
+ else { spinner.text = `Download Caddy: ${getNumber(Number(percent) * 100)}` }
51
+ }
52
+
53
+ export function logProgressOver() {
54
+ if (!spinner) {
55
+ spinner = ora({
56
+ text: 'Start Download Caddy: 100%',
57
+ indent: 2,
58
+ })
59
+ spinner.start()
60
+ }
61
+ else {
62
+ spinner.succeed()
63
+ // empty a line after succeed
64
+ consola.log('')
65
+ }
66
+ }
package/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import process from 'node:process'
2
2
  import type { UnpluginFactory } from 'unplugin'
3
3
  import { createUnplugin } from 'unplugin'
4
- import type { ResolvedConfig } from 'vite'
4
+ import type { ResolvedConfig, ViteDevServer } from 'vite'
5
5
  import c from 'picocolors'
6
- import { consola } from './utils'
6
+ import { consola, once } from './utils'
7
7
  import type { Options } from './types'
8
8
  import { CaddyInstant } from './caddy'
9
9
 
@@ -11,12 +11,59 @@ let config: ResolvedConfig
11
11
 
12
12
  let caddy: CaddyInstant
13
13
 
14
+ let _stop: (callback?: () => any) => Promise<any>
15
+ let _servce: ViteDevServer
16
+
17
+ function registerExit(clear: (() => Promise<any>) | (() => any)) {
18
+ process.on('beforeExit', async (_code) => {
19
+ await clear()
20
+ })
21
+
22
+ process.on('uncaughtException', async (err) => {
23
+ console.error('An uncaught error occurred!', err.stack)
24
+ await clear()
25
+ process.nextTick(() => {
26
+ process.exit(1)
27
+ })
28
+ })
29
+
30
+ process.on('unhandledRejection', async (reason, promise) => {
31
+ console.error('Unhandled Rejection at:', promise, 'reason:', reason)
32
+ await clear()
33
+ process.nextTick(() => {
34
+ process.exit(1)
35
+ })
36
+ })
37
+
38
+ process.once('SIGINT', async () => {
39
+ await clear()
40
+ process.emit('SIGINT')
41
+ })
42
+
43
+ process.once('SIGTERM', async () => {
44
+ await clear()
45
+ process.emit('SIGTERM')
46
+ })
47
+ }
48
+
14
49
  export const unpluginFactory: UnpluginFactory<Options> = options => ({
15
50
  name: 'unplugin-https-reverse-proxy',
16
- enforce: 'post',
51
+ enforce: 'pre',
17
52
  vite: {
18
53
  configResolved(_config) {
19
54
  config = _config
55
+ registerExit(async () => {
56
+ try {
57
+ _servce && _servce.close()
58
+ _servce.httpServer?.close(async () => {
59
+ _stop && await _stop()
60
+ })
61
+ _stop && await _stop()
62
+ }
63
+ catch (e) {
64
+ consola.error(e)
65
+ }
66
+ })
20
67
  },
21
68
  configureServer(server) {
22
69
  const {
@@ -29,6 +76,9 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
29
76
  consola.fail('please provide target')
30
77
  return
31
78
  }
79
+
80
+ _servce = server
81
+
32
82
  const _printUrls = server.printUrls
33
83
  server.printUrls = () => {
34
84
  _printUrls()
@@ -40,19 +90,16 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
40
90
  }
41
91
  const base = server.config.base || '/'
42
92
  try {
43
- const caddy = new CaddyInstant()
93
+ if (caddy)
94
+ return
95
+ caddy = new CaddyInstant()
44
96
  caddy.run(source, target, {
45
97
  base,
46
98
  showCaddyLog: options.showCaddyLog,
47
99
  }).then((stop) => {
48
100
  const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
49
101
  consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`https://${target}${base}`)}`)
50
-
51
- process.on('SIGINT', async () => {
52
- await stop()
53
- server.close()
54
- process.exit()
55
- })
102
+ !_stop && (_stop = once(stop))
56
103
  }).catch((e) => {
57
104
  throw e
58
105
  })
@@ -74,14 +121,43 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
74
121
  consola.fail('please provide target')
75
122
  return
76
123
  }
124
+
77
125
  try {
78
- // @ts-expect-error vuecli
79
- const devServer = compiler.options.devServer || process.VUE_CLI_SERVICE.projectOptions.devServer
80
- const source = `${devServer?.host || '127.0.0.1'}:${devServer?.port || '8080'}`
126
+ // registerExit(async () => {
127
+ // try {
128
+ // _stop && await _stop()
129
+ // }
130
+ // catch (e) {
131
+ // consola.error(e)
132
+ // }
133
+ // })
134
+
135
+ const devServer = {
136
+ host: '127.0.0.1',
137
+ port: 8080,
138
+ ...compiler.options.devServer,
139
+ // @ts-expect-error vuecli
140
+ ...process.VUE_CLI_SERVICE.projectOptions.devServer,
141
+ }
142
+ const source = `${devServer.host}:${devServer.port}`
81
143
 
82
144
  if (caddy)
83
145
  return
84
146
 
147
+ // https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
148
+ const _close = compiler.close.bind(compiler)
149
+ compiler.close = async (callback: Parameters<typeof _close>[0]) => {
150
+ try {
151
+ _stop && await _stop(() => {
152
+ _close && _close(callback)
153
+ })
154
+ }
155
+ catch (e) {
156
+ consola.error(e)
157
+ process.exit(1)
158
+ }
159
+ }
160
+
85
161
  caddy = new CaddyInstant()
86
162
 
87
163
  caddy.run(source, target, {
@@ -89,16 +165,10 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
89
165
  }).then((stop) => {
90
166
  const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
91
167
 
92
- compiler.hooks.done.tap('unplugin-https-reverse-proxy', (stats) => {
93
- if (stats.hasErrors())
94
- return
95
-
168
+ compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
96
169
  consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`https://${target}`)}`)
97
- })
98
170
 
99
- process.on('SIGINT', async () => {
100
- await stop()
101
- process.exit()
171
+ !_stop && (_stop = once(stop))
102
172
  })
103
173
  }).catch((e) => {
104
174
  throw e
package/src/utils.ts CHANGED
@@ -3,3 +3,13 @@ import { createConsola } from 'consola'
3
3
  export const consola = createConsola({
4
4
  level: 3,
5
5
  }).withTag('reverse-proxy')
6
+
7
+ export function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> {
8
+ let called = false
9
+ return (...args: any[]) => {
10
+ if (called)
11
+ return
12
+ called = true
13
+ return fn(...args)
14
+ }
15
+ }
@@ -1,9 +0,0 @@
1
- // src/utils.ts
2
- import { createConsola } from "consola";
3
- var consola = createConsola({
4
- level: 3
5
- }).withTag("reverse-proxy");
6
-
7
- export {
8
- consola
9
- };