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.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-RDTWBIQD.js";
3
+ } from "./chunk-QU7HLXVD.js";
4
4
 
5
5
  // src/vite.ts
6
6
  import { createVitePlugin } from "unplugin";
@@ -4386,12 +4386,12 @@ var require_streams = __commonJS({
4386
4386
  var POOL_SIZE2 = 65536;
4387
4387
  if (!globalThis.ReadableStream) {
4388
4388
  try {
4389
- const process = __require("process");
4389
+ const process = __require("node:process");
4390
4390
  const { emitWarning } = process;
4391
4391
  try {
4392
4392
  process.emitWarning = () => {
4393
4393
  };
4394
- Object.assign(globalThis, __require("stream/web"));
4394
+ Object.assign(globalThis, __require("node:stream/web"));
4395
4395
  process.emitWarning = emitWarning;
4396
4396
  } catch (error) {
4397
4397
  process.emitWarning = emitWarning;
@@ -4799,7 +4799,7 @@ Content-Type: ${v.type || "application/octet-stream"}\r
4799
4799
  }
4800
4800
 
4801
4801
  // node_modules/.pnpm/fetch-blob@3.2.0/node_modules/fetch-blob/from.js
4802
- import { statSync, createReadStream, promises as fs } from "fs";
4802
+ import { statSync, createReadStream, promises as fs } from "node:fs";
4803
4803
  var import_node_domexception = __toESM(require_node_domexception(), 1);
4804
4804
  var { stat } = fs;
4805
4805
 
package/dist/esbuild.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) {
@@ -5216,6 +5219,8 @@ var import_unplugin2 = require("unplugin");
5216
5219
  // src/index.ts
5217
5220
  var import_node_child_process = require("child_process");
5218
5221
  var import_node_process = __toESM(require("process"), 1);
5222
+ var import_node_http3 = require("http");
5223
+ var import_node_net2 = require("net");
5219
5224
 
5220
5225
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/index.js
5221
5226
  var import_node_http2 = __toESM(require("http"), 1);
@@ -6513,20 +6518,94 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) {
6513
6518
  var import_unplugin = require("unplugin");
6514
6519
  var import_picocolors = __toESM(require("picocolors"), 1);
6515
6520
  var import_cookie = __toESM(require("cookie"), 1);
6516
- var import_chii = require("chii");
6521
+ var import_z_chii = require("z-chii");
6522
+ var import_dns2 = __toESM(require("dns2"), 1);
6517
6523
  var import_get_port_please = require("get-port-please");
6524
+ var import_http_proxy = __toESM(require("http-proxy"), 1);
6518
6525
  var cwd = import_node_process.default.cwd();
6519
6526
  var config;
6520
6527
  var colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
6521
6528
  var resovedInfo = {
6522
6529
  devtoolsInstance: void 0,
6530
+ dnsServerInstence: void 0,
6523
6531
  availablePort: void 0,
6524
6532
  targetURL: void 0
6525
6533
  };
6534
+ function startVueDevtools(enable) {
6535
+ if (enable && !resovedInfo.devtoolsInstance) {
6536
+ resovedInfo.devtoolsInstance = (0, import_node_child_process.exec)("npx vue-devtools");
6537
+ console.log(` ${import_picocolors.default.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
6538
+ resovedInfo.devtoolsInstance.on("exit", () => {
6539
+ resovedInfo.devtoolsInstance = void 0;
6540
+ });
6541
+ import_node_process.default.on("exit", () => {
6542
+ if (resovedInfo.devtoolsInstance) {
6543
+ resovedInfo.devtoolsInstance.kill();
6544
+ }
6545
+ });
6546
+ }
6547
+ }
6548
+ function startDnsServer(options) {
6549
+ function debug(...args) {
6550
+ if (options == null ? void 0 : options.debug) {
6551
+ console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
6552
+ }
6553
+ }
6554
+ if ((options == null ? void 0 : options.enable) && (options == null ? void 0 : options.dns) && !resovedInfo.dnsServerInstence) {
6555
+ const { Packet } = import_dns2.default;
6556
+ resovedInfo.dnsServerInstence = import_dns2.default.createServer({
6557
+ tcp: true,
6558
+ udp: true,
6559
+ handle: (request, send, rinfo) => {
6560
+ var _a4;
6561
+ const response = Packet.createResponseFromRequest(request);
6562
+ const [question] = request.questions;
6563
+ const { name } = question;
6564
+ if (name === options.dns.host) {
6565
+ response.answers.push({
6566
+ name,
6567
+ type: Packet.TYPE.A,
6568
+ class: Packet.CLASS.IN,
6569
+ ttl: 300,
6570
+ address: (_a4 = options.dns) == null ? void 0 : _a4.ip
6571
+ });
6572
+ debug(`dns request from ${rinfo.address}:${rinfo.port} for ${name} => ${options.dns.ip}`);
6573
+ }
6574
+ send(response);
6575
+ }
6576
+ });
6577
+ resovedInfo.dnsServerInstence.listen({
6578
+ udp: { address: "0.0.0.0", port: 53 },
6579
+ tcp: { address: "0.0.0.0", port: 53 }
6580
+ }).then(() => {
6581
+ debug("DNS server is running.");
6582
+ const dns = new import_dns2.default({
6583
+ port: 53,
6584
+ nameServers: ["127.0.0.1", "8.8.8.8"]
6585
+ });
6586
+ dns.resolveA(options.dns.host).then((addresses) => {
6587
+ var _a4;
6588
+ if (((_a4 = addresses.answers[0]) == null ? void 0 : _a4.address) === options.dns.ip) {
6589
+ startDnsServer(options);
6590
+ 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}`)}`);
6591
+ } else {
6592
+ debug(addresses.answers[0].address);
6593
+ throw new Error("DNS server started failed");
6594
+ }
6595
+ }).catch((e2) => {
6596
+ throw e2;
6597
+ });
6598
+ }).catch((e2) => {
6599
+ debug(e2);
6600
+ console.log(` ${import_picocolors.default.red("\u279C")} ${import_picocolors.default.bold((e2 == null ? void 0 : e2.message) || e2)}`);
6601
+ });
6602
+ }
6603
+ }
6526
6604
  var unpluginFactory = (options) => {
6527
6605
  const {
6528
- chii: enableChii = true
6606
+ chii
6529
6607
  } = options || {};
6608
+ const enableChii = (chii == null ? void 0 : chii.enable) !== false;
6530
6609
  function debug(...args) {
6531
6610
  if (options == null ? void 0 : options.debug) {
6532
6611
  console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
@@ -6542,22 +6621,16 @@ var unpluginFactory = (options) => {
6542
6621
  var _a4, _b, _c;
6543
6622
  if ((options == null ? void 0 : options.enable) && enableChii && !resovedInfo.availablePort) {
6544
6623
  resovedInfo.availablePort = await (0, import_get_port_please.getRandomPort)();
6545
- debug(`chii server port: ${JSON.stringify({ availablePort: resovedInfo.availablePort })}`);
6546
- (0, import_chii.start)({
6547
- host: "0.0.0.0",
6624
+ (0, import_z_chii.start)({
6548
6625
  port: resovedInfo.availablePort
6549
6626
  });
6627
+ debug(`chii server port: ${resovedInfo.availablePort}`);
6550
6628
  }
6551
6629
  if (options == null ? void 0 : options.enable) {
6552
6630
  const codes = [
6553
6631
  "/* eslint-disable */;",
6554
6632
  ((_a4 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a4.enable) ? `import { devtools } from '@vue/devtools'
6555
- 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});` : "",
6556
- (options == null ? void 0 : options.enable) && enableChii ? `(() => {
6557
- const script = document.createElement('script');
6558
- script.src="http://localhost:${resovedInfo.availablePort}/target.js";
6559
- document.body.appendChild(script);
6560
- })()` : "",
6633
+ 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});` : "",
6561
6634
  "/* eslint-enable */",
6562
6635
  `${_source};`
6563
6636
  ];
@@ -6577,6 +6650,22 @@ var unpluginFactory = (options) => {
6577
6650
  configResolved(_config) {
6578
6651
  config = _config;
6579
6652
  },
6653
+ transformIndexHtml(html) {
6654
+ if (enableChii) {
6655
+ return html.replace(
6656
+ "</body>",
6657
+ `</body>
6658
+ <script>
6659
+ (() => {
6660
+ const script = document.createElement('script');
6661
+ script.src="./__chii_proxy/target.js";
6662
+ document.body.appendChild(script);
6663
+ })()
6664
+ </script>
6665
+ `
6666
+ );
6667
+ }
6668
+ },
6580
6669
  async configureServer(server) {
6581
6670
  var _a4, _b;
6582
6671
  if (!(options == null ? void 0 : options.enable)) {
@@ -6612,7 +6701,7 @@ var unpluginFactory = (options) => {
6612
6701
  server.middlewares.use((req, res, next) => {
6613
6702
  const cookies = import_cookie.default.parse(req.headers.cookie || "");
6614
6703
  for (const [name, value] of Object.entries(cookies)) {
6615
- if (options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
6704
+ if (value && options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
6616
6705
  const serializedCookie = import_cookie.default.serialize(name, value, {
6617
6706
  httpOnly: false
6618
6707
  });
@@ -6623,6 +6712,51 @@ var unpluginFactory = (options) => {
6623
6712
  });
6624
6713
  }
6625
6714
  if (enableChii) {
6715
+ let createProxyMiddleware2 = function() {
6716
+ let proxy = null;
6717
+ const handleUpgrade = (req, socket, head) => {
6718
+ var _a5;
6719
+ if (proxy && ((_a5 = req.url) == null ? void 0 : _a5.startsWith("/__chii_proxy"))) {
6720
+ debug("WS upgrade:", req.url);
6721
+ req.url = req.url.replace("/__chii_proxy", "");
6722
+ proxy.ws(req, socket, head);
6723
+ }
6724
+ };
6725
+ return (resolvedInfo) => {
6726
+ return (req, res, next) => {
6727
+ var _a5;
6728
+ if (!proxy && resolvedInfo.availablePort) {
6729
+ proxy = import_http_proxy.default.createProxyServer({
6730
+ target: `http://localhost:${resolvedInfo.availablePort}`,
6731
+ ws: true
6732
+ // changeOrigin: true, // Consider if you need this
6733
+ });
6734
+ proxy.on("error", (err, req2, res2) => {
6735
+ console.error("Proxy error:", err);
6736
+ if (res2 instanceof import_node_http3.ServerResponse) {
6737
+ if (!res2.headersSent) {
6738
+ res2.writeHead(500, { "Content-Type": "text/plain" });
6739
+ }
6740
+ res2.end(`Proxy error: ${err.message}`);
6741
+ } else if (res2 instanceof import_node_net2.Socket) {
6742
+ res2.destroy();
6743
+ }
6744
+ });
6745
+ if (req.socket.server) {
6746
+ req.socket.server.on("upgrade", handleUpgrade);
6747
+ }
6748
+ }
6749
+ if (proxy && ((_a5 = req.url) == null ? void 0 : _a5.startsWith("/__chii_proxy"))) {
6750
+ debug(req.url);
6751
+ req.url = req.url.replace("/__chii_proxy", "");
6752
+ proxy.web(req, res);
6753
+ } else {
6754
+ next();
6755
+ }
6756
+ };
6757
+ };
6758
+ };
6759
+ var createProxyMiddleware = createProxyMiddleware2;
6626
6760
  server.middlewares.use("/__chrome_devtools", async (_req, res) => {
6627
6761
  try {
6628
6762
  const raw = await fetch(`http://localhost:${resovedInfo.availablePort}/targets`);
@@ -6631,6 +6765,9 @@ var unpluginFactory = (options) => {
6631
6765
  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`;
6632
6766
  res.writeHead(302, { Location: devToolsUrl });
6633
6767
  res.end();
6768
+ } else {
6769
+ res.writeHead(404);
6770
+ res.end();
6634
6771
  }
6635
6772
  } catch (error) {
6636
6773
  debug(`${error}`);
@@ -6638,6 +6775,8 @@ var unpluginFactory = (options) => {
6638
6775
  res.end();
6639
6776
  }
6640
6777
  });
6778
+ const proxyMiddleware = createProxyMiddleware2();
6779
+ server.middlewares.use(proxyMiddleware(resovedInfo));
6641
6780
  }
6642
6781
  server.middlewares.use("/open-dingtalk", (req, res) => {
6643
6782
  var _a5;
@@ -6645,20 +6784,10 @@ var unpluginFactory = (options) => {
6645
6784
  res.writeHead(302, {
6646
6785
  Location: `dingtalk://dingtalkclient/page/link?url=${encodeURIComponent(targetURL.toString())}`
6647
6786
  });
6648
- if (((_a5 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a5.enable) && !resovedInfo.devtoolsInstance) {
6649
- resovedInfo.devtoolsInstance = (0, import_node_child_process.exec)("npx vue-devtools");
6650
- console.log(` ${import_picocolors.default.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
6651
- resovedInfo.devtoolsInstance.on("exit", () => {
6652
- resovedInfo.devtoolsInstance = void 0;
6653
- });
6654
- import_node_process.default.on("exit", () => {
6655
- if (resovedInfo.devtoolsInstance) {
6656
- resovedInfo.devtoolsInstance.kill();
6657
- }
6658
- });
6659
- }
6787
+ startVueDevtools((_a5 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a5.enable);
6660
6788
  res.end();
6661
6789
  });
6790
+ startDnsServer(options);
6662
6791
  }
6663
6792
  },
6664
6793
  webpack(compiler) {
@@ -6689,6 +6818,7 @@ var unpluginFactory = (options) => {
6689
6818
  if (options == null ? void 0 : options.corpId) {
6690
6819
  resovedInfo.targetURL.searchParams.append("corpId", options.corpId);
6691
6820
  }
6821
+ startDnsServer(options);
6692
6822
  },
6693
6823
  async rspack(compiler) {
6694
6824
  var _a4, _b, _c, _d2, _e;
@@ -6717,6 +6847,7 @@ var unpluginFactory = (options) => {
6717
6847
  "Click to open chrome devtools"
6718
6848
  )}: ${colorUrl(`http://${source}${base}__chrome_devtools`)}`);
6719
6849
  }
6850
+ startDnsServer(options);
6720
6851
  }
6721
6852
  };
6722
6853
  return unpluginDing;
package/dist/esbuild.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-RDTWBIQD.js";
4
- import "./chunk-PMOTCIQR.js";
3
+ } from "./chunk-QU7HLXVD.js";
4
+ import "./chunk-ZEHXGFFH.js";
5
5
 
6
6
  // src/esbuild.ts
7
7
  import { createEsbuildPlugin } from "unplugin";
package/dist/index.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) {
@@ -5206,16 +5209,20 @@ var init_multipart_parser = __esm({
5206
5209
  });
5207
5210
 
5208
5211
  // src/index.ts
5209
- var src_exports = {};
5210
- __export(src_exports, {
5211
- default: () => src_default,
5212
+ var index_exports = {};
5213
+ __export(index_exports, {
5214
+ default: () => index_default,
5212
5215
  resovedInfo: () => resovedInfo,
5216
+ startDnsServer: () => startDnsServer,
5217
+ startVueDevtools: () => startVueDevtools,
5213
5218
  unplugin: () => unplugin,
5214
5219
  unpluginFactory: () => unpluginFactory
5215
5220
  });
5216
- module.exports = __toCommonJS(src_exports);
5221
+ module.exports = __toCommonJS(index_exports);
5217
5222
  var import_node_child_process = require("child_process");
5218
5223
  var import_node_process = __toESM(require("process"), 1);
5224
+ var import_node_http3 = require("http");
5225
+ var import_node_net2 = require("net");
5219
5226
 
5220
5227
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/index.js
5221
5228
  var import_node_http2 = __toESM(require("http"), 1);
@@ -6513,20 +6520,94 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) {
6513
6520
  var import_unplugin = require("unplugin");
6514
6521
  var import_picocolors = __toESM(require("picocolors"), 1);
6515
6522
  var import_cookie = __toESM(require("cookie"), 1);
6516
- var import_chii = require("chii");
6523
+ var import_z_chii = require("z-chii");
6524
+ var import_dns2 = __toESM(require("dns2"), 1);
6517
6525
  var import_get_port_please = require("get-port-please");
6526
+ var import_http_proxy = __toESM(require("http-proxy"), 1);
6518
6527
  var cwd = import_node_process.default.cwd();
6519
6528
  var config;
6520
6529
  var colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
6521
6530
  var resovedInfo = {
6522
6531
  devtoolsInstance: void 0,
6532
+ dnsServerInstence: void 0,
6523
6533
  availablePort: void 0,
6524
6534
  targetURL: void 0
6525
6535
  };
6536
+ function startVueDevtools(enable) {
6537
+ if (enable && !resovedInfo.devtoolsInstance) {
6538
+ resovedInfo.devtoolsInstance = (0, import_node_child_process.exec)("npx vue-devtools");
6539
+ console.log(` ${import_picocolors.default.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
6540
+ resovedInfo.devtoolsInstance.on("exit", () => {
6541
+ resovedInfo.devtoolsInstance = void 0;
6542
+ });
6543
+ import_node_process.default.on("exit", () => {
6544
+ if (resovedInfo.devtoolsInstance) {
6545
+ resovedInfo.devtoolsInstance.kill();
6546
+ }
6547
+ });
6548
+ }
6549
+ }
6550
+ function startDnsServer(options) {
6551
+ function debug(...args) {
6552
+ if (options == null ? void 0 : options.debug) {
6553
+ console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
6554
+ }
6555
+ }
6556
+ if ((options == null ? void 0 : options.enable) && (options == null ? void 0 : options.dns) && !resovedInfo.dnsServerInstence) {
6557
+ const { Packet } = import_dns2.default;
6558
+ resovedInfo.dnsServerInstence = import_dns2.default.createServer({
6559
+ tcp: true,
6560
+ udp: true,
6561
+ handle: (request, send, rinfo) => {
6562
+ var _a4;
6563
+ const response = Packet.createResponseFromRequest(request);
6564
+ const [question] = request.questions;
6565
+ const { name } = question;
6566
+ if (name === options.dns.host) {
6567
+ response.answers.push({
6568
+ name,
6569
+ type: Packet.TYPE.A,
6570
+ class: Packet.CLASS.IN,
6571
+ ttl: 300,
6572
+ address: (_a4 = options.dns) == null ? void 0 : _a4.ip
6573
+ });
6574
+ debug(`dns request from ${rinfo.address}:${rinfo.port} for ${name} => ${options.dns.ip}`);
6575
+ }
6576
+ send(response);
6577
+ }
6578
+ });
6579
+ resovedInfo.dnsServerInstence.listen({
6580
+ udp: { address: "0.0.0.0", port: 53 },
6581
+ tcp: { address: "0.0.0.0", port: 53 }
6582
+ }).then(() => {
6583
+ debug("DNS server is running.");
6584
+ const dns = new import_dns2.default({
6585
+ port: 53,
6586
+ nameServers: ["127.0.0.1", "8.8.8.8"]
6587
+ });
6588
+ dns.resolveA(options.dns.host).then((addresses) => {
6589
+ var _a4;
6590
+ if (((_a4 = addresses.answers[0]) == null ? void 0 : _a4.address) === options.dns.ip) {
6591
+ startDnsServer(options);
6592
+ 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}`)}`);
6593
+ } else {
6594
+ debug(addresses.answers[0].address);
6595
+ throw new Error("DNS server started failed");
6596
+ }
6597
+ }).catch((e2) => {
6598
+ throw e2;
6599
+ });
6600
+ }).catch((e2) => {
6601
+ debug(e2);
6602
+ console.log(` ${import_picocolors.default.red("\u279C")} ${import_picocolors.default.bold((e2 == null ? void 0 : e2.message) || e2)}`);
6603
+ });
6604
+ }
6605
+ }
6526
6606
  var unpluginFactory = (options) => {
6527
6607
  const {
6528
- chii: enableChii = true
6608
+ chii
6529
6609
  } = options || {};
6610
+ const enableChii = (chii == null ? void 0 : chii.enable) !== false;
6530
6611
  function debug(...args) {
6531
6612
  if (options == null ? void 0 : options.debug) {
6532
6613
  console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
@@ -6542,22 +6623,16 @@ var unpluginFactory = (options) => {
6542
6623
  var _a4, _b, _c;
6543
6624
  if ((options == null ? void 0 : options.enable) && enableChii && !resovedInfo.availablePort) {
6544
6625
  resovedInfo.availablePort = await (0, import_get_port_please.getRandomPort)();
6545
- debug(`chii server port: ${JSON.stringify({ availablePort: resovedInfo.availablePort })}`);
6546
- (0, import_chii.start)({
6547
- host: "0.0.0.0",
6626
+ (0, import_z_chii.start)({
6548
6627
  port: resovedInfo.availablePort
6549
6628
  });
6629
+ debug(`chii server port: ${resovedInfo.availablePort}`);
6550
6630
  }
6551
6631
  if (options == null ? void 0 : options.enable) {
6552
6632
  const codes = [
6553
6633
  "/* eslint-disable */;",
6554
6634
  ((_a4 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a4.enable) ? `import { devtools } from '@vue/devtools'
6555
- 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});` : "",
6556
- (options == null ? void 0 : options.enable) && enableChii ? `(() => {
6557
- const script = document.createElement('script');
6558
- script.src="http://localhost:${resovedInfo.availablePort}/target.js";
6559
- document.body.appendChild(script);
6560
- })()` : "",
6635
+ 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});` : "",
6561
6636
  "/* eslint-enable */",
6562
6637
  `${_source};`
6563
6638
  ];
@@ -6577,6 +6652,22 @@ var unpluginFactory = (options) => {
6577
6652
  configResolved(_config) {
6578
6653
  config = _config;
6579
6654
  },
6655
+ transformIndexHtml(html) {
6656
+ if (enableChii) {
6657
+ return html.replace(
6658
+ "</body>",
6659
+ `</body>
6660
+ <script>
6661
+ (() => {
6662
+ const script = document.createElement('script');
6663
+ script.src="./__chii_proxy/target.js";
6664
+ document.body.appendChild(script);
6665
+ })()
6666
+ </script>
6667
+ `
6668
+ );
6669
+ }
6670
+ },
6580
6671
  async configureServer(server) {
6581
6672
  var _a4, _b;
6582
6673
  if (!(options == null ? void 0 : options.enable)) {
@@ -6612,7 +6703,7 @@ var unpluginFactory = (options) => {
6612
6703
  server.middlewares.use((req, res, next) => {
6613
6704
  const cookies = import_cookie.default.parse(req.headers.cookie || "");
6614
6705
  for (const [name, value] of Object.entries(cookies)) {
6615
- if (options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
6706
+ if (value && options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
6616
6707
  const serializedCookie = import_cookie.default.serialize(name, value, {
6617
6708
  httpOnly: false
6618
6709
  });
@@ -6623,6 +6714,51 @@ var unpluginFactory = (options) => {
6623
6714
  });
6624
6715
  }
6625
6716
  if (enableChii) {
6717
+ let createProxyMiddleware2 = function() {
6718
+ let proxy = null;
6719
+ const handleUpgrade = (req, socket, head) => {
6720
+ var _a5;
6721
+ if (proxy && ((_a5 = req.url) == null ? void 0 : _a5.startsWith("/__chii_proxy"))) {
6722
+ debug("WS upgrade:", req.url);
6723
+ req.url = req.url.replace("/__chii_proxy", "");
6724
+ proxy.ws(req, socket, head);
6725
+ }
6726
+ };
6727
+ return (resolvedInfo) => {
6728
+ return (req, res, next) => {
6729
+ var _a5;
6730
+ if (!proxy && resolvedInfo.availablePort) {
6731
+ proxy = import_http_proxy.default.createProxyServer({
6732
+ target: `http://localhost:${resolvedInfo.availablePort}`,
6733
+ ws: true
6734
+ // changeOrigin: true, // Consider if you need this
6735
+ });
6736
+ proxy.on("error", (err, req2, res2) => {
6737
+ console.error("Proxy error:", err);
6738
+ if (res2 instanceof import_node_http3.ServerResponse) {
6739
+ if (!res2.headersSent) {
6740
+ res2.writeHead(500, { "Content-Type": "text/plain" });
6741
+ }
6742
+ res2.end(`Proxy error: ${err.message}`);
6743
+ } else if (res2 instanceof import_node_net2.Socket) {
6744
+ res2.destroy();
6745
+ }
6746
+ });
6747
+ if (req.socket.server) {
6748
+ req.socket.server.on("upgrade", handleUpgrade);
6749
+ }
6750
+ }
6751
+ if (proxy && ((_a5 = req.url) == null ? void 0 : _a5.startsWith("/__chii_proxy"))) {
6752
+ debug(req.url);
6753
+ req.url = req.url.replace("/__chii_proxy", "");
6754
+ proxy.web(req, res);
6755
+ } else {
6756
+ next();
6757
+ }
6758
+ };
6759
+ };
6760
+ };
6761
+ var createProxyMiddleware = createProxyMiddleware2;
6626
6762
  server.middlewares.use("/__chrome_devtools", async (_req, res) => {
6627
6763
  try {
6628
6764
  const raw = await fetch(`http://localhost:${resovedInfo.availablePort}/targets`);
@@ -6631,6 +6767,9 @@ var unpluginFactory = (options) => {
6631
6767
  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`;
6632
6768
  res.writeHead(302, { Location: devToolsUrl });
6633
6769
  res.end();
6770
+ } else {
6771
+ res.writeHead(404);
6772
+ res.end();
6634
6773
  }
6635
6774
  } catch (error) {
6636
6775
  debug(`${error}`);
@@ -6638,6 +6777,8 @@ var unpluginFactory = (options) => {
6638
6777
  res.end();
6639
6778
  }
6640
6779
  });
6780
+ const proxyMiddleware = createProxyMiddleware2();
6781
+ server.middlewares.use(proxyMiddleware(resovedInfo));
6641
6782
  }
6642
6783
  server.middlewares.use("/open-dingtalk", (req, res) => {
6643
6784
  var _a5;
@@ -6645,20 +6786,10 @@ var unpluginFactory = (options) => {
6645
6786
  res.writeHead(302, {
6646
6787
  Location: `dingtalk://dingtalkclient/page/link?url=${encodeURIComponent(targetURL.toString())}`
6647
6788
  });
6648
- if (((_a5 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a5.enable) && !resovedInfo.devtoolsInstance) {
6649
- resovedInfo.devtoolsInstance = (0, import_node_child_process.exec)("npx vue-devtools");
6650
- console.log(` ${import_picocolors.default.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
6651
- resovedInfo.devtoolsInstance.on("exit", () => {
6652
- resovedInfo.devtoolsInstance = void 0;
6653
- });
6654
- import_node_process.default.on("exit", () => {
6655
- if (resovedInfo.devtoolsInstance) {
6656
- resovedInfo.devtoolsInstance.kill();
6657
- }
6658
- });
6659
- }
6789
+ startVueDevtools((_a5 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a5.enable);
6660
6790
  res.end();
6661
6791
  });
6792
+ startDnsServer(options);
6662
6793
  }
6663
6794
  },
6664
6795
  webpack(compiler) {
@@ -6689,6 +6820,7 @@ var unpluginFactory = (options) => {
6689
6820
  if (options == null ? void 0 : options.corpId) {
6690
6821
  resovedInfo.targetURL.searchParams.append("corpId", options.corpId);
6691
6822
  }
6823
+ startDnsServer(options);
6692
6824
  },
6693
6825
  async rspack(compiler) {
6694
6826
  var _a4, _b, _c, _d2, _e;
@@ -6717,15 +6849,18 @@ var unpluginFactory = (options) => {
6717
6849
  "Click to open chrome devtools"
6718
6850
  )}: ${colorUrl(`http://${source}${base}__chrome_devtools`)}`);
6719
6851
  }
6852
+ startDnsServer(options);
6720
6853
  }
6721
6854
  };
6722
6855
  return unpluginDing;
6723
6856
  };
6724
6857
  var unplugin = /* @__PURE__ */ (0, import_unplugin.createUnplugin)(unpluginFactory);
6725
- var src_default = unplugin;
6858
+ var index_default = unplugin;
6726
6859
  // Annotate the CommonJS export names for ESM import in node:
6727
6860
  0 && (module.exports = {
6728
6861
  resovedInfo,
6862
+ startDnsServer,
6863
+ startVueDevtools,
6729
6864
  unplugin,
6730
6865
  unpluginFactory
6731
6866
  });