unplugin-dingtalk 0.4.2 → 0.5.2

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/dist/astro.cjs CHANGED
@@ -5069,6 +5069,7 @@ var init_multipart_parser = __esm({
5069
5069
  state = S.HEADER_FIELD;
5070
5070
  mark("onHeaderField");
5071
5071
  index = 0;
5072
+ // falls through
5072
5073
  case S.HEADER_FIELD:
5073
5074
  if (c2 === CR) {
5074
5075
  clear("onHeaderField");
@@ -5098,6 +5099,7 @@ var init_multipart_parser = __esm({
5098
5099
  }
5099
5100
  mark("onHeaderValue");
5100
5101
  state = S.HEADER_VALUE;
5102
+ // falls through
5101
5103
  case S.HEADER_VALUE:
5102
5104
  if (c2 === CR) {
5103
5105
  dataCallback("onHeaderValue", true);
@@ -5121,6 +5123,7 @@ var init_multipart_parser = __esm({
5121
5123
  case S.PART_DATA_START:
5122
5124
  state = S.PART_DATA;
5123
5125
  mark("onPartData");
5126
+ // falls through
5124
5127
  case S.PART_DATA:
5125
5128
  previousIndex = index;
5126
5129
  if (index === 0) {
@@ -5215,6 +5218,8 @@ module.exports = __toCommonJS(astro_exports);
5215
5218
  // src/index.ts
5216
5219
  var import_node_child_process = require("child_process");
5217
5220
  var import_node_process = __toESM(require("process"), 1);
5221
+ var import_node_http3 = require("http");
5222
+ var import_node_net2 = require("net");
5218
5223
 
5219
5224
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/index.js
5220
5225
  var import_node_http2 = __toESM(require("http"), 1);
@@ -6512,20 +6517,94 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) {
6512
6517
  var import_unplugin = require("unplugin");
6513
6518
  var import_picocolors = __toESM(require("picocolors"), 1);
6514
6519
  var import_cookie = __toESM(require("cookie"), 1);
6515
- var import_chii = require("chii");
6520
+ var import_z_chii = require("z-chii");
6521
+ var import_dns2 = __toESM(require("dns2"), 1);
6516
6522
  var import_get_port_please = require("get-port-please");
6523
+ var import_http_proxy = __toESM(require("http-proxy"), 1);
6517
6524
  var cwd = import_node_process.default.cwd();
6518
6525
  var config;
6519
6526
  var colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
6520
6527
  var resovedInfo = {
6521
6528
  devtoolsInstance: void 0,
6529
+ dnsServerInstence: void 0,
6522
6530
  availablePort: void 0,
6523
6531
  targetURL: void 0
6524
6532
  };
6533
+ function startVueDevtools(enable) {
6534
+ if (enable && !resovedInfo.devtoolsInstance) {
6535
+ resovedInfo.devtoolsInstance = (0, import_node_child_process.exec)("npx vue-devtools");
6536
+ console.log(` ${import_picocolors.default.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
6537
+ resovedInfo.devtoolsInstance.on("exit", () => {
6538
+ resovedInfo.devtoolsInstance = void 0;
6539
+ });
6540
+ import_node_process.default.on("exit", () => {
6541
+ if (resovedInfo.devtoolsInstance) {
6542
+ resovedInfo.devtoolsInstance.kill();
6543
+ }
6544
+ });
6545
+ }
6546
+ }
6547
+ function startDnsServer(options) {
6548
+ function debug(...args) {
6549
+ if (options == null ? void 0 : options.debug) {
6550
+ console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
6551
+ }
6552
+ }
6553
+ if ((options == null ? void 0 : options.enable) && (options == null ? void 0 : options.dns) && !resovedInfo.dnsServerInstence) {
6554
+ const { Packet } = import_dns2.default;
6555
+ resovedInfo.dnsServerInstence = import_dns2.default.createServer({
6556
+ tcp: true,
6557
+ udp: true,
6558
+ handle: (request, send, rinfo) => {
6559
+ var _a4;
6560
+ const response = Packet.createResponseFromRequest(request);
6561
+ const [question] = request.questions;
6562
+ const { name } = question;
6563
+ if (name === options.dns.host) {
6564
+ response.answers.push({
6565
+ name,
6566
+ type: Packet.TYPE.A,
6567
+ class: Packet.CLASS.IN,
6568
+ ttl: 300,
6569
+ address: (_a4 = options.dns) == null ? void 0 : _a4.ip
6570
+ });
6571
+ debug(`dns request from ${rinfo.address}:${rinfo.port} for ${name} => ${options.dns.ip}`);
6572
+ }
6573
+ send(response);
6574
+ }
6575
+ });
6576
+ resovedInfo.dnsServerInstence.listen({
6577
+ udp: { address: "0.0.0.0", port: 53 },
6578
+ tcp: { address: "0.0.0.0", port: 53 }
6579
+ }).then(() => {
6580
+ debug("DNS server is running.");
6581
+ const dns = new import_dns2.default({
6582
+ port: 53,
6583
+ nameServers: ["127.0.0.1", "8.8.8.8"]
6584
+ });
6585
+ dns.resolveA(options.dns.host).then((addresses) => {
6586
+ var _a4;
6587
+ if (((_a4 = addresses.answers[0]) == null ? void 0 : _a4.address) === options.dns.ip) {
6588
+ startDnsServer(options);
6589
+ console.log(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold(`DNS server is started, please modify the DNS of the remote device to ${options.dns.ip}`)}`);
6590
+ } else {
6591
+ debug(addresses.answers[0].address);
6592
+ throw new Error("DNS server started failed");
6593
+ }
6594
+ }).catch((e2) => {
6595
+ throw e2;
6596
+ });
6597
+ }).catch((e2) => {
6598
+ debug(e2);
6599
+ console.log(` ${import_picocolors.default.red("\u279C")} ${import_picocolors.default.bold((e2 == null ? void 0 : e2.message) || e2)}`);
6600
+ });
6601
+ }
6602
+ }
6525
6603
  var unpluginFactory = (options) => {
6526
6604
  const {
6527
- chii: enableChii = true
6605
+ chii
6528
6606
  } = options || {};
6607
+ const enableChii = (chii == null ? void 0 : chii.enable) !== false;
6529
6608
  function debug(...args) {
6530
6609
  if (options == null ? void 0 : options.debug) {
6531
6610
  console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
@@ -6541,22 +6620,16 @@ var unpluginFactory = (options) => {
6541
6620
  var _a4, _b, _c;
6542
6621
  if ((options == null ? void 0 : options.enable) && enableChii && !resovedInfo.availablePort) {
6543
6622
  resovedInfo.availablePort = await (0, import_get_port_please.getRandomPort)();
6544
- debug(`chii server port: ${JSON.stringify({ availablePort: resovedInfo.availablePort })}`);
6545
- (0, import_chii.start)({
6546
- host: "0.0.0.0",
6623
+ (0, import_z_chii.start)({
6547
6624
  port: resovedInfo.availablePort
6548
6625
  });
6626
+ debug(`chii server port: ${resovedInfo.availablePort}`);
6549
6627
  }
6550
6628
  if (options == null ? void 0 : options.enable) {
6551
6629
  const codes = [
6552
6630
  "/* eslint-disable */;",
6553
6631
  ((_a4 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a4.enable) ? `import { devtools } from '@vue/devtools'
6554
- devtools.connect(${(_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host}, ${(_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port});` : "",
6555
- (options == null ? void 0 : options.enable) && enableChii ? `(() => {
6556
- const script = document.createElement('script');
6557
- script.src="http://localhost:${resovedInfo.availablePort}/target.js";
6558
- document.body.appendChild(script);
6559
- })()` : "",
6632
+ devtools.connect(${((_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host) ? `"${options.vueDevtools.host}"` : void 0}, ${((_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port) ? `${options.vueDevtools.port}` : void 0});` : "",
6560
6633
  "/* eslint-enable */",
6561
6634
  `${_source};`
6562
6635
  ];
@@ -6576,6 +6649,22 @@ var unpluginFactory = (options) => {
6576
6649
  configResolved(_config) {
6577
6650
  config = _config;
6578
6651
  },
6652
+ transformIndexHtml(html) {
6653
+ if (enableChii) {
6654
+ return html.replace(
6655
+ "</body>",
6656
+ `</body>
6657
+ <script>
6658
+ (() => {
6659
+ const script = document.createElement('script');
6660
+ script.src="./__chii_proxy/target.js";
6661
+ document.body.appendChild(script);
6662
+ })()
6663
+ </script>
6664
+ `
6665
+ );
6666
+ }
6667
+ },
6579
6668
  async configureServer(server) {
6580
6669
  var _a4, _b;
6581
6670
  if (!(options == null ? void 0 : options.enable)) {
@@ -6611,7 +6700,7 @@ var unpluginFactory = (options) => {
6611
6700
  server.middlewares.use((req, res, next) => {
6612
6701
  const cookies = import_cookie.default.parse(req.headers.cookie || "");
6613
6702
  for (const [name, value] of Object.entries(cookies)) {
6614
- if (options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
6703
+ if (value && options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
6615
6704
  const serializedCookie = import_cookie.default.serialize(name, value, {
6616
6705
  httpOnly: false
6617
6706
  });
@@ -6622,6 +6711,51 @@ var unpluginFactory = (options) => {
6622
6711
  });
6623
6712
  }
6624
6713
  if (enableChii) {
6714
+ let createProxyMiddleware2 = function() {
6715
+ let proxy = null;
6716
+ const handleUpgrade = (req, socket, head) => {
6717
+ var _a5;
6718
+ if (proxy && ((_a5 = req.url) == null ? void 0 : _a5.startsWith("/__chii_proxy"))) {
6719
+ debug("WS upgrade:", req.url);
6720
+ req.url = req.url.replace("/__chii_proxy", "");
6721
+ proxy.ws(req, socket, head);
6722
+ }
6723
+ };
6724
+ return (resolvedInfo) => {
6725
+ return (req, res, next) => {
6726
+ var _a5;
6727
+ if (!proxy && resolvedInfo.availablePort) {
6728
+ proxy = import_http_proxy.default.createProxyServer({
6729
+ target: `http://localhost:${resolvedInfo.availablePort}`,
6730
+ ws: true
6731
+ // changeOrigin: true, // Consider if you need this
6732
+ });
6733
+ proxy.on("error", (err, req2, res2) => {
6734
+ console.error("Proxy error:", err);
6735
+ if (res2 instanceof import_node_http3.ServerResponse) {
6736
+ if (!res2.headersSent) {
6737
+ res2.writeHead(500, { "Content-Type": "text/plain" });
6738
+ }
6739
+ res2.end(`Proxy error: ${err.message}`);
6740
+ } else if (res2 instanceof import_node_net2.Socket) {
6741
+ res2.destroy();
6742
+ }
6743
+ });
6744
+ if (req.socket.server) {
6745
+ req.socket.server.on("upgrade", handleUpgrade);
6746
+ }
6747
+ }
6748
+ if (proxy && ((_a5 = req.url) == null ? void 0 : _a5.startsWith("/__chii_proxy"))) {
6749
+ debug(req.url);
6750
+ req.url = req.url.replace("/__chii_proxy", "");
6751
+ proxy.web(req, res);
6752
+ } else {
6753
+ next();
6754
+ }
6755
+ };
6756
+ };
6757
+ };
6758
+ var createProxyMiddleware = createProxyMiddleware2;
6625
6759
  server.middlewares.use("/__chrome_devtools", async (_req, res) => {
6626
6760
  try {
6627
6761
  const raw = await fetch(`http://localhost:${resovedInfo.availablePort}/targets`);
@@ -6630,6 +6764,9 @@ var unpluginFactory = (options) => {
6630
6764
  const devToolsUrl = `http://localhost:${resovedInfo.availablePort}/front_end/chii_app.html?ws=localhost:${resovedInfo.availablePort}/client/${Math.random().toString(20).substring(2, 8)}?target=${data.targets[0].id}&rtc=false`;
6631
6765
  res.writeHead(302, { Location: devToolsUrl });
6632
6766
  res.end();
6767
+ } else {
6768
+ res.writeHead(404);
6769
+ res.end();
6633
6770
  }
6634
6771
  } catch (error) {
6635
6772
  debug(`${error}`);
@@ -6637,6 +6774,8 @@ var unpluginFactory = (options) => {
6637
6774
  res.end();
6638
6775
  }
6639
6776
  });
6777
+ const proxyMiddleware = createProxyMiddleware2();
6778
+ server.middlewares.use(proxyMiddleware(resovedInfo));
6640
6779
  }
6641
6780
  server.middlewares.use("/open-dingtalk", (req, res) => {
6642
6781
  var _a5;
@@ -6644,20 +6783,10 @@ var unpluginFactory = (options) => {
6644
6783
  res.writeHead(302, {
6645
6784
  Location: `dingtalk://dingtalkclient/page/link?url=${encodeURIComponent(targetURL.toString())}`
6646
6785
  });
6647
- if (((_a5 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a5.enable) && !resovedInfo.devtoolsInstance) {
6648
- resovedInfo.devtoolsInstance = (0, import_node_child_process.exec)("npx vue-devtools");
6649
- console.log(` ${import_picocolors.default.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
6650
- resovedInfo.devtoolsInstance.on("exit", () => {
6651
- resovedInfo.devtoolsInstance = void 0;
6652
- });
6653
- import_node_process.default.on("exit", () => {
6654
- if (resovedInfo.devtoolsInstance) {
6655
- resovedInfo.devtoolsInstance.kill();
6656
- }
6657
- });
6658
- }
6786
+ startVueDevtools((_a5 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a5.enable);
6659
6787
  res.end();
6660
6788
  });
6789
+ startDnsServer(options);
6661
6790
  }
6662
6791
  },
6663
6792
  webpack(compiler) {
@@ -6688,6 +6817,7 @@ var unpluginFactory = (options) => {
6688
6817
  if (options == null ? void 0 : options.corpId) {
6689
6818
  resovedInfo.targetURL.searchParams.append("corpId", options.corpId);
6690
6819
  }
6820
+ startDnsServer(options);
6691
6821
  },
6692
6822
  async rspack(compiler) {
6693
6823
  var _a4, _b, _c, _d2, _e;
@@ -6716,12 +6846,13 @@ var unpluginFactory = (options) => {
6716
6846
  "Click to open chrome devtools"
6717
6847
  )}: ${colorUrl(`http://${source}${base}__chrome_devtools`)}`);
6718
6848
  }
6849
+ startDnsServer(options);
6719
6850
  }
6720
6851
  };
6721
6852
  return unpluginDing;
6722
6853
  };
6723
6854
  var unplugin = /* @__PURE__ */ (0, import_unplugin.createUnplugin)(unpluginFactory);
6724
- var src_default = unplugin;
6855
+ var index_default = unplugin;
6725
6856
 
6726
6857
  // src/astro.ts
6727
6858
  var astro_default = (options) => ({
@@ -6730,7 +6861,7 @@ var astro_default = (options) => ({
6730
6861
  "astro:config:setup": async (astro) => {
6731
6862
  var _a4;
6732
6863
  (_a4 = astro.config.vite).plugins || (_a4.plugins = []);
6733
- astro.config.vite.plugins.push(src_default.vite(options));
6864
+ astro.config.vite.plugins.push(index_default.vite(options));
6734
6865
  }
6735
6866
  }
6736
6867
  });
package/dist/astro.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
- src_default
3
- } from "./chunk-RDTWBIQD.js";
4
- import "./chunk-PMOTCIQR.js";
2
+ index_default
3
+ } from "./chunk-QU7HLXVD.js";
4
+ import "./chunk-ZEHXGFFH.js";
5
5
 
6
6
  // src/astro.ts
7
7
  var astro_default = (options) => ({
@@ -10,7 +10,7 @@ var astro_default = (options) => ({
10
10
  "astro:config:setup": async (astro) => {
11
11
  var _a;
12
12
  (_a = astro.config.vite).plugins || (_a.plugins = []);
13
- astro.config.vite.plugins.push(src_default.vite(options));
13
+ astro.config.vite.plugins.push(index_default.vite(options));
14
14
  }
15
15
  }
16
16
  });
@@ -5,18 +5,20 @@ import {
5
5
  __spreadValues,
6
6
  fetch_blob_default,
7
7
  formDataToBlob
8
- } from "./chunk-PMOTCIQR.js";
8
+ } from "./chunk-ZEHXGFFH.js";
9
9
 
10
10
  // src/index.ts
11
- import { exec } from "child_process";
12
- import process2 from "process";
11
+ import { exec } from "node:child_process";
12
+ import process2 from "node:process";
13
+ import { ServerResponse } from "node:http";
14
+ import { Socket } from "node:net";
13
15
 
14
16
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/index.js
15
- import http2 from "http";
16
- import https from "https";
17
- import zlib from "zlib";
18
- import Stream2, { PassThrough as PassThrough2, pipeline as pump } from "stream";
19
- import { Buffer as Buffer3 } from "buffer";
17
+ import http2 from "node:http";
18
+ import https from "node:https";
19
+ import zlib from "node:zlib";
20
+ import Stream2, { PassThrough as PassThrough2, pipeline as pump } from "node:stream";
21
+ import { Buffer as Buffer3 } from "node:buffer";
20
22
 
21
23
  // node_modules/.pnpm/data-uri-to-buffer@4.0.1/node_modules/data-uri-to-buffer/dist/index.js
22
24
  function dataUriToBuffer(uri) {
@@ -58,9 +60,9 @@ function dataUriToBuffer(uri) {
58
60
  var dist_default = dataUriToBuffer;
59
61
 
60
62
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/body.js
61
- import Stream, { PassThrough } from "stream";
62
- import { types, deprecate, promisify } from "util";
63
- import { Buffer as Buffer2 } from "buffer";
63
+ import Stream, { PassThrough } from "node:stream";
64
+ import { types, deprecate, promisify } from "node:util";
65
+ import { Buffer as Buffer2 } from "node:buffer";
64
66
 
65
67
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/errors/base.js
66
68
  var FetchBaseError = class extends Error {
@@ -186,7 +188,7 @@ var Body = class {
186
188
  }
187
189
  return formData;
188
190
  }
189
- const { toFormData } = await import("./multipart-parser-HI4LNJC5.js");
191
+ const { toFormData } = await import("./multipart-parser-VO5TIRQZ.js");
190
192
  return toFormData(this.body, ct);
191
193
  }
192
194
  /**
@@ -373,8 +375,8 @@ var writeToStream = async (dest, { body }) => {
373
375
  };
374
376
 
375
377
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/headers.js
376
- import { types as types2 } from "util";
377
- import http from "http";
378
+ import { types as types2 } from "node:util";
379
+ import http from "node:http";
378
380
  var validateHeaderName = typeof http.validateHeaderName === "function" ? http.validateHeaderName : (name) => {
379
381
  if (!/^[\^`\-\w!#$%&'*+.|~]+$/.test(name)) {
380
382
  const error = new TypeError(`Header name must be a valid HTTP token [${name}]`);
@@ -682,8 +684,8 @@ Object.defineProperties(Response.prototype, {
682
684
  });
683
685
 
684
686
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/request.js
685
- import { format as formatUrl } from "url";
686
- import { deprecate as deprecate2 } from "util";
687
+ import { format as formatUrl } from "node:url";
688
+ import { deprecate as deprecate2 } from "node:util";
687
689
 
688
690
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/utils/get-search.js
689
691
  var getSearch = (parsedURL) => {
@@ -696,7 +698,7 @@ var getSearch = (parsedURL) => {
696
698
  };
697
699
 
698
700
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/utils/referrer.js
699
- import { isIP } from "net";
701
+ import { isIP } from "node:net";
700
702
  function stripURLForUseAsAReferrer(url, originOnly = false) {
701
703
  if (url == null) {
702
704
  return "no-referrer";
@@ -1303,20 +1305,94 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) {
1303
1305
  import { createUnplugin } from "unplugin";
1304
1306
  import c from "picocolors";
1305
1307
  import cookie from "cookie";
1306
- import { start } from "chii";
1308
+ import { start } from "z-chii";
1309
+ import dns2 from "dns2";
1307
1310
  import { getRandomPort } from "get-port-please";
1311
+ import httpProxy from "http-proxy";
1308
1312
  var cwd = process2.cwd();
1309
1313
  var config;
1310
1314
  var colorUrl = (url) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`));
1311
1315
  var resovedInfo = {
1312
1316
  devtoolsInstance: void 0,
1317
+ dnsServerInstence: void 0,
1313
1318
  availablePort: void 0,
1314
1319
  targetURL: void 0
1315
1320
  };
1321
+ function startVueDevtools(enable) {
1322
+ if (enable && !resovedInfo.devtoolsInstance) {
1323
+ resovedInfo.devtoolsInstance = exec("npx vue-devtools");
1324
+ console.log(` ${c.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
1325
+ resovedInfo.devtoolsInstance.on("exit", () => {
1326
+ resovedInfo.devtoolsInstance = void 0;
1327
+ });
1328
+ process2.on("exit", () => {
1329
+ if (resovedInfo.devtoolsInstance) {
1330
+ resovedInfo.devtoolsInstance.kill();
1331
+ }
1332
+ });
1333
+ }
1334
+ }
1335
+ function startDnsServer(options) {
1336
+ function debug(...args) {
1337
+ if (options == null ? void 0 : options.debug) {
1338
+ console.log(` ${c.yellow("DEBUG")} `, ...args);
1339
+ }
1340
+ }
1341
+ if ((options == null ? void 0 : options.enable) && (options == null ? void 0 : options.dns) && !resovedInfo.dnsServerInstence) {
1342
+ const { Packet } = dns2;
1343
+ resovedInfo.dnsServerInstence = dns2.createServer({
1344
+ tcp: true,
1345
+ udp: true,
1346
+ handle: (request, send, rinfo) => {
1347
+ var _a;
1348
+ const response = Packet.createResponseFromRequest(request);
1349
+ const [question] = request.questions;
1350
+ const { name } = question;
1351
+ if (name === options.dns.host) {
1352
+ response.answers.push({
1353
+ name,
1354
+ type: Packet.TYPE.A,
1355
+ class: Packet.CLASS.IN,
1356
+ ttl: 300,
1357
+ address: (_a = options.dns) == null ? void 0 : _a.ip
1358
+ });
1359
+ debug(`dns request from ${rinfo.address}:${rinfo.port} for ${name} => ${options.dns.ip}`);
1360
+ }
1361
+ send(response);
1362
+ }
1363
+ });
1364
+ resovedInfo.dnsServerInstence.listen({
1365
+ udp: { address: "0.0.0.0", port: 53 },
1366
+ tcp: { address: "0.0.0.0", port: 53 }
1367
+ }).then(() => {
1368
+ debug("DNS server is running.");
1369
+ const dns = new dns2({
1370
+ port: 53,
1371
+ nameServers: ["127.0.0.1", "8.8.8.8"]
1372
+ });
1373
+ dns.resolveA(options.dns.host).then((addresses) => {
1374
+ var _a;
1375
+ if (((_a = addresses.answers[0]) == null ? void 0 : _a.address) === options.dns.ip) {
1376
+ startDnsServer(options);
1377
+ console.log(` ${c.green("\u279C")} ${c.bold(`DNS server is started, please modify the DNS of the remote device to ${options.dns.ip}`)}`);
1378
+ } else {
1379
+ debug(addresses.answers[0].address);
1380
+ throw new Error("DNS server started failed");
1381
+ }
1382
+ }).catch((e) => {
1383
+ throw e;
1384
+ });
1385
+ }).catch((e) => {
1386
+ debug(e);
1387
+ console.log(` ${c.red("\u279C")} ${c.bold((e == null ? void 0 : e.message) || e)}`);
1388
+ });
1389
+ }
1390
+ }
1316
1391
  var unpluginFactory = (options) => {
1317
1392
  const {
1318
- chii: enableChii = true
1393
+ chii
1319
1394
  } = options || {};
1395
+ const enableChii = (chii == null ? void 0 : chii.enable) !== false;
1320
1396
  function debug(...args) {
1321
1397
  if (options == null ? void 0 : options.debug) {
1322
1398
  console.log(` ${c.yellow("DEBUG")} `, ...args);
@@ -1332,22 +1408,16 @@ var unpluginFactory = (options) => {
1332
1408
  var _a, _b, _c;
1333
1409
  if ((options == null ? void 0 : options.enable) && enableChii && !resovedInfo.availablePort) {
1334
1410
  resovedInfo.availablePort = await getRandomPort();
1335
- debug(`chii server port: ${JSON.stringify({ availablePort: resovedInfo.availablePort })}`);
1336
1411
  start({
1337
- host: "0.0.0.0",
1338
1412
  port: resovedInfo.availablePort
1339
1413
  });
1414
+ debug(`chii server port: ${resovedInfo.availablePort}`);
1340
1415
  }
1341
1416
  if (options == null ? void 0 : options.enable) {
1342
1417
  const codes = [
1343
1418
  "/* eslint-disable */;",
1344
1419
  ((_a = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a.enable) ? `import { devtools } from '@vue/devtools'
1345
- devtools.connect(${(_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host}, ${(_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port});` : "",
1346
- (options == null ? void 0 : options.enable) && enableChii ? `(() => {
1347
- const script = document.createElement('script');
1348
- script.src="http://localhost:${resovedInfo.availablePort}/target.js";
1349
- document.body.appendChild(script);
1350
- })()` : "",
1420
+ devtools.connect(${((_b = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _b.host) ? `"${options.vueDevtools.host}"` : void 0}, ${((_c = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _c.port) ? `${options.vueDevtools.port}` : void 0});` : "",
1351
1421
  "/* eslint-enable */",
1352
1422
  `${_source};`
1353
1423
  ];
@@ -1367,6 +1437,22 @@ var unpluginFactory = (options) => {
1367
1437
  configResolved(_config) {
1368
1438
  config = _config;
1369
1439
  },
1440
+ transformIndexHtml(html) {
1441
+ if (enableChii) {
1442
+ return html.replace(
1443
+ "</body>",
1444
+ `</body>
1445
+ <script>
1446
+ (() => {
1447
+ const script = document.createElement('script');
1448
+ script.src="./__chii_proxy/target.js";
1449
+ document.body.appendChild(script);
1450
+ })()
1451
+ </script>
1452
+ `
1453
+ );
1454
+ }
1455
+ },
1370
1456
  async configureServer(server) {
1371
1457
  var _a, _b;
1372
1458
  if (!(options == null ? void 0 : options.enable)) {
@@ -1402,7 +1488,7 @@ var unpluginFactory = (options) => {
1402
1488
  server.middlewares.use((req, res, next) => {
1403
1489
  const cookies = cookie.parse(req.headers.cookie || "");
1404
1490
  for (const [name, value] of Object.entries(cookies)) {
1405
- if (options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
1491
+ if (value && options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
1406
1492
  const serializedCookie = cookie.serialize(name, value, {
1407
1493
  httpOnly: false
1408
1494
  });
@@ -1413,6 +1499,51 @@ var unpluginFactory = (options) => {
1413
1499
  });
1414
1500
  }
1415
1501
  if (enableChii) {
1502
+ let createProxyMiddleware2 = function() {
1503
+ let proxy = null;
1504
+ const handleUpgrade = (req, socket, head) => {
1505
+ var _a2;
1506
+ if (proxy && ((_a2 = req.url) == null ? void 0 : _a2.startsWith("/__chii_proxy"))) {
1507
+ debug("WS upgrade:", req.url);
1508
+ req.url = req.url.replace("/__chii_proxy", "");
1509
+ proxy.ws(req, socket, head);
1510
+ }
1511
+ };
1512
+ return (resolvedInfo) => {
1513
+ return (req, res, next) => {
1514
+ var _a2;
1515
+ if (!proxy && resolvedInfo.availablePort) {
1516
+ proxy = httpProxy.createProxyServer({
1517
+ target: `http://localhost:${resolvedInfo.availablePort}`,
1518
+ ws: true
1519
+ // changeOrigin: true, // Consider if you need this
1520
+ });
1521
+ proxy.on("error", (err, req2, res2) => {
1522
+ console.error("Proxy error:", err);
1523
+ if (res2 instanceof ServerResponse) {
1524
+ if (!res2.headersSent) {
1525
+ res2.writeHead(500, { "Content-Type": "text/plain" });
1526
+ }
1527
+ res2.end(`Proxy error: ${err.message}`);
1528
+ } else if (res2 instanceof Socket) {
1529
+ res2.destroy();
1530
+ }
1531
+ });
1532
+ if (req.socket.server) {
1533
+ req.socket.server.on("upgrade", handleUpgrade);
1534
+ }
1535
+ }
1536
+ if (proxy && ((_a2 = req.url) == null ? void 0 : _a2.startsWith("/__chii_proxy"))) {
1537
+ debug(req.url);
1538
+ req.url = req.url.replace("/__chii_proxy", "");
1539
+ proxy.web(req, res);
1540
+ } else {
1541
+ next();
1542
+ }
1543
+ };
1544
+ };
1545
+ };
1546
+ var createProxyMiddleware = createProxyMiddleware2;
1416
1547
  server.middlewares.use("/__chrome_devtools", async (_req, res) => {
1417
1548
  try {
1418
1549
  const raw = await fetch(`http://localhost:${resovedInfo.availablePort}/targets`);
@@ -1421,6 +1552,9 @@ var unpluginFactory = (options) => {
1421
1552
  const devToolsUrl = `http://localhost:${resovedInfo.availablePort}/front_end/chii_app.html?ws=localhost:${resovedInfo.availablePort}/client/${Math.random().toString(20).substring(2, 8)}?target=${data.targets[0].id}&rtc=false`;
1422
1553
  res.writeHead(302, { Location: devToolsUrl });
1423
1554
  res.end();
1555
+ } else {
1556
+ res.writeHead(404);
1557
+ res.end();
1424
1558
  }
1425
1559
  } catch (error) {
1426
1560
  debug(`${error}`);
@@ -1428,6 +1562,8 @@ var unpluginFactory = (options) => {
1428
1562
  res.end();
1429
1563
  }
1430
1564
  });
1565
+ const proxyMiddleware = createProxyMiddleware2();
1566
+ server.middlewares.use(proxyMiddleware(resovedInfo));
1431
1567
  }
1432
1568
  server.middlewares.use("/open-dingtalk", (req, res) => {
1433
1569
  var _a2;
@@ -1435,20 +1571,10 @@ var unpluginFactory = (options) => {
1435
1571
  res.writeHead(302, {
1436
1572
  Location: `dingtalk://dingtalkclient/page/link?url=${encodeURIComponent(targetURL.toString())}`
1437
1573
  });
1438
- if (((_a2 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a2.enable) && !resovedInfo.devtoolsInstance) {
1439
- resovedInfo.devtoolsInstance = exec("npx vue-devtools");
1440
- console.log(` ${c.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
1441
- resovedInfo.devtoolsInstance.on("exit", () => {
1442
- resovedInfo.devtoolsInstance = void 0;
1443
- });
1444
- process2.on("exit", () => {
1445
- if (resovedInfo.devtoolsInstance) {
1446
- resovedInfo.devtoolsInstance.kill();
1447
- }
1448
- });
1449
- }
1574
+ startVueDevtools((_a2 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a2.enable);
1450
1575
  res.end();
1451
1576
  });
1577
+ startDnsServer(options);
1452
1578
  }
1453
1579
  },
1454
1580
  webpack(compiler) {
@@ -1479,6 +1605,7 @@ var unpluginFactory = (options) => {
1479
1605
  if (options == null ? void 0 : options.corpId) {
1480
1606
  resovedInfo.targetURL.searchParams.append("corpId", options.corpId);
1481
1607
  }
1608
+ startDnsServer(options);
1482
1609
  },
1483
1610
  async rspack(compiler) {
1484
1611
  var _a, _b, _c, _d, _e;
@@ -1507,17 +1634,20 @@ var unpluginFactory = (options) => {
1507
1634
  "Click to open chrome devtools"
1508
1635
  )}: ${colorUrl(`http://${source}${base}__chrome_devtools`)}`);
1509
1636
  }
1637
+ startDnsServer(options);
1510
1638
  }
1511
1639
  };
1512
1640
  return unpluginDing;
1513
1641
  };
1514
1642
  var unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
1515
- var src_default = unplugin;
1643
+ var index_default = unplugin;
1516
1644
 
1517
1645
  export {
1518
1646
  fetch,
1519
1647
  resovedInfo,
1648
+ startVueDevtools,
1649
+ startDnsServer,
1520
1650
  unpluginFactory,
1521
1651
  unplugin,
1522
- src_default
1652
+ index_default
1523
1653
  };