unplugin-https-reverse-proxy 1.0.0-beta.16 → 1.0.0-beta.17

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unplugin-https-reverse-proxy",
3
3
  "type": "module",
4
- "version": "1.0.0-beta.16",
4
+ "version": "1.0.0-beta.17",
5
5
  "description": "",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/zcf0508/unplugin-https-reverse-proxy#readme",
@@ -119,10 +119,9 @@
119
119
  "consola": "^3.2.3",
120
120
  "dotenv-flow": "^4.1.0",
121
121
  "got-cjs": "^12.5.4",
122
- "hosts-so-easy": "^1.2.9",
122
+ "hostile": "^1.4.0",
123
123
  "http-proxy-agent": "^7.0.2",
124
124
  "https-proxy-agent": "^7.0.4",
125
- "kill-port": "^2.0.1",
126
125
  "ora": "5",
127
126
  "picocolors": "^1.0.0",
128
127
  "tsx": "^4.7.1",
@@ -134,12 +133,14 @@
134
133
  "@nuxt/schema": "^3.10.2",
135
134
  "@rsbuild/core": "^0.4.3",
136
135
  "@rspack/core": "^0.5.4",
136
+ "@types/hostile": "^1.3.5",
137
137
  "@types/kill-port": "^2.0.3",
138
138
  "@types/node": "^20.11.19",
139
139
  "@types/proper-lockfile": "^4.1.4",
140
140
  "@vue/cli": "^5.0.8",
141
141
  "bumpp": "^9.3.0",
142
142
  "chalk": "^5.3.0",
143
+ "cross-env": "^7.0.3",
143
144
  "eslint": "^8.56.0",
144
145
  "fast-glob": "^3.3.2",
145
146
  "nodemon": "^3.0.3",
@@ -165,6 +166,6 @@
165
166
  "play:vuecli4": "npm -C examples/vuecli4 run serve",
166
167
  "release": "bumpp",
167
168
  "start": "tsx src/index.ts",
168
- "test": "UHRP_CADDY_PATH='./test/.uhrp' vitest"
169
+ "test": "cross-env UHRP_CADDY_PATH='./test/.uhrp' vitest"
169
170
  }
170
171
  }
@@ -49,27 +49,21 @@ function once(fn) {
49
49
  var import_node_os2 = require("os");
50
50
  var import_node_process4 = __toESM(require("process"), 1);
51
51
  var import_node_fs2 = require("fs");
52
- var import_node_child_process3 = require("child_process");
52
+ var import_node_child_process4 = require("child_process");
53
53
  var import_promises2 = require("fs/promises");
54
54
  var import_got_cjs = require("got-cjs");
55
55
  var import_http_proxy_agent = require("http-proxy-agent");
56
56
  var import_https_proxy_agent = require("https-proxy-agent");
57
- var import_kill_port = __toESM(require("kill-port"), 1);
58
57
  var lockfile = __toESM(require("proper-lockfile"), 1);
59
58
 
60
59
  // src/host/index.ts
61
60
  var import_node_process2 = __toESM(require("process"), 1);
62
61
  var import_node_child_process2 = require("child_process");
63
62
  var import_node_fs = require("fs");
64
- var import_hosts_so_easy = __toESM(require("hosts-so-easy"), 1);
65
- var Hosts = import_hosts_so_easy.default.default || import_hosts_so_easy.default;
66
- var hostInstance = new Hosts({
67
- debounceTime: 50,
68
- hostsFile: getPathOfSystemHostsPath()
69
- });
70
- function getPathOfSystemHostsPath() {
71
- return import_node_process2.default.platform === "win32" ? `${import_node_process2.default.env.windir || "C:\\WINDOWS"}\\system32\\drivers\\etc\\hosts` : "/etc/hosts";
72
- }
63
+ var import_node_util = require("util");
64
+ var import_hostile = __toESM(require("hostile"), 1);
65
+ var _setHost = (0, import_node_util.promisify)(import_hostile.default.set);
66
+ var _removeHost = (0, import_node_util.promisify)(import_hostile.default.remove);
73
67
 
74
68
  // src/caddy/constants.ts
75
69
  var import_node_path2 = require("path");
@@ -201,6 +195,7 @@ var supportList = [
201
195
 
202
196
  // src/caddy/utils.ts
203
197
  var import_node_net = require("net");
198
+ var import_node_child_process3 = require("child_process");
204
199
  var import_ora = __toESM(require("ora"), 1);
205
200
  var spinner;
206
201
  function getNumber(number) {
@@ -286,7 +281,7 @@ function testCaddy() {
286
281
  if (!(0, import_node_fs2.existsSync)(caddyPath))
287
282
  return resolve(false);
288
283
  (0, import_node_fs2.chmodSync)(caddyPath, 511);
289
- const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process3.spawn)(caddyPath, []) : (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath]);
284
+ const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process4.spawn)(caddyPath, []) : (0, import_node_child_process4.spawn)("sudo", ["-E", caddyPath]);
290
285
  child.on("close", () => {
291
286
  return resolve(false);
292
287
  });
@@ -6,12 +6,11 @@ import { readFile, unlink, writeFile } from 'node:fs/promises'
6
6
  import { got } from 'got-cjs'
7
7
  import { HttpProxyAgent } from 'http-proxy-agent'
8
8
  import { HttpsProxyAgent } from 'https-proxy-agent'
9
- import kill from 'kill-port'
10
9
  import * as lockfile from 'proper-lockfile'
11
10
  import { chmodRecursive, consola, once } from '../utils'
12
11
  import { addHost, removeHost } from '../host'
13
12
  import { TEMP_DIR, caddyFilePath, caddyLockFilePath, caddyPath, supportList } from './constants'
14
- import { logProgress, logProgressOver, tryPort } from './utils'
13
+ import { kill, logProgress, logProgressOver, tryPort } from './utils'
15
14
 
16
15
  export async function download() {
17
16
  if (await testCaddy())
@@ -109,6 +108,8 @@ export class CaddyInstant {
109
108
  private locked = false
110
109
  private caddyfile: string | undefined
111
110
 
111
+ private caddyChild: ReturnType<typeof spawn> | undefined
112
+
112
113
  constructor() {
113
114
  testCaddy().then(async () => {
114
115
  await this.getCaddyfile()
@@ -197,9 +198,9 @@ ${target.split(':')[0]}${https ? '' : ':80'} {
197
198
 
198
199
  if (!this.locked) {
199
200
  if (await tryPort(443))
200
- await kill(443, 'tcp')
201
+ await kill(443)
201
202
  if ((process.platform === 'win32' || !https) && await tryPort(80))
202
- await kill(80, 'tcp')
203
+ await kill(80)
203
204
  }
204
205
 
205
206
  return new Promise<void>((resolve, reject) => {
@@ -235,7 +236,7 @@ ${target.split(':')[0]}${https ? '' : ':80'} {
235
236
  process.exit = async (code?: number) => {
236
237
  const port = Number(source.split(':')[1])
237
238
  if (!Number.isNaN(port) && await tryPort(port))
238
- await kill(port, 'tcp')
239
+ await kill(port)
239
240
 
240
241
  // fix `Error: EPERM: operation not permitted`
241
242
  const pwd = process.cwd()
@@ -275,17 +276,17 @@ ${target.split(':')[0]}${https ? '' : ':80'} {
275
276
  }
276
277
 
277
278
  if (!this.locked) {
278
- const child = process.platform !== 'win32'
279
+ this.caddyChild = process.platform !== 'win32'
279
280
  ? spawn('sudo', ['-E', caddyPath, 'run', '--config', caddyFilePath, '--watch'])
280
281
  : spawn(caddyPath, ['run', '--config', caddyFilePath, '--watch'])
281
282
 
282
283
  lockfile.lockSync(caddyLockFilePath)
283
284
 
284
- child.on('error', (err) => {
285
+ this.caddyChild!.on('error', (err) => {
285
286
  return reject(err)
286
287
  })
287
288
 
288
- child.stderr.on('data', (data) => {
289
+ this.caddyChild!.stderr?.on('data', (data) => {
289
290
  const lines = (data.toString() as string).split('\n').map(line => line.trim())
290
291
  for (const line of lines) {
291
292
  // caddy log
@@ -304,7 +305,7 @@ ${target.split(':')[0]}${https ? '' : ':80'} {
304
305
  }
305
306
  })
306
307
 
307
- child.stdout.on('data', (_data) => {
308
+ this.caddyChild!.stdout?.on('data', (_data) => {
308
309
  consola.info(_data.toString())
309
310
  resolve()
310
311
  })
@@ -323,6 +324,7 @@ ${target.split(':')[0]}${https ? '' : ':80'} {
323
324
 
324
325
  async baseCleanup() {
325
326
  if (!this.locked) {
327
+ this.caddyChild?.kill()
326
328
  try {
327
329
  await lockfile.unlock(caddyLockFilePath)
328
330
  await unlink(caddyFilePath)
@@ -1,4 +1,5 @@
1
1
  import { createServer } from 'node:net'
2
+ import { exec } from 'node:child_process'
2
3
  import ora from 'ora'
3
4
  import { consola } from '../utils'
4
5
 
@@ -28,6 +29,23 @@ export function tryPort(port: number) {
28
29
  })
29
30
  }
30
31
 
32
+ /**
33
+ * npx free-port :port
34
+ */
35
+ export async function kill(port: number) {
36
+ return new Promise<void>((resolve, reject) => {
37
+ exec(`npx @maxbo/free-port ${port} -s`, (err, stdout, stderr) => {
38
+ if (err) {
39
+ reject(err)
40
+ }
41
+ else {
42
+ consola.log(stdout)
43
+ resolve()
44
+ }
45
+ })
46
+ })
47
+ }
48
+
31
49
  function getNumber(number: number) {
32
50
  if (number <= 0)
33
51
  return '0%'
package/src/host/index.ts CHANGED
@@ -1,17 +1,13 @@
1
1
  import process from 'node:process'
2
2
  import { exec } from 'node:child_process'
3
3
  import { chmodSync } from 'node:fs'
4
+ import { promisify } from 'node:util'
4
5
 
5
- // @ts-expect-error no type
6
- import _Hosts from 'hosts-so-easy'
6
+ import hostile from 'hostile'
7
7
  import { consola } from '../utils'
8
8
 
9
- const Hosts = _Hosts.default || _Hosts
10
-
11
- const hostInstance = new Hosts({
12
- debounceTime: 50,
13
- hostsFile: getPathOfSystemHostsPath(),
14
- })
9
+ const _setHost = promisify(hostile.set)
10
+ const _removeHost = promisify(hostile.remove)
15
11
 
16
12
  function getPathOfSystemHostsPath() {
17
13
  return process.platform === 'win32'
@@ -38,20 +34,18 @@ async function syncHost() {
38
34
  export async function addHost(ip: string, host: string) {
39
35
  if (process.platform !== 'win32')
40
36
  chmodSync(getPathOfSystemHostsPath(), 0o777)
41
- hostInstance.add('127.0.0.1', 'localhost')
42
- hostInstance.add(ip, host)
37
+ await _setHost('127.0.0.1', 'localhost')
38
+ await _setHost(ip, host)
43
39
  if (ip === 'localhost')
44
- hostInstance.add('127.0.0.1', host)
45
- await hostInstance.updateFinish()
40
+ await _setHost('127.0.0.1', host)
46
41
  return await syncHost()
47
42
  }
48
43
 
49
44
  export async function removeHost(ip: string, host: string) {
50
45
  if (process.platform !== 'win32')
51
46
  chmodSync(getPathOfSystemHostsPath(), 0o777)
52
- hostInstance.remove(ip, host)
47
+ await _removeHost(ip, host)
53
48
  if (ip === 'localhost')
54
- hostInstance.remove('127.0.0.1', host)
55
- await hostInstance.updateFinish()
49
+ await _removeHost('127.0.0.1', host)
56
50
  return await syncHost()
57
51
  }