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/rspack.d.ts CHANGED
@@ -1,9 +1,8 @@
1
- import * as _unplugin from 'unplugin';
2
1
  import { Options, RspackSetupMiddlewares } from './types.js';
3
2
  import 'webpack-dev-server';
4
3
  import 'node:http';
5
4
  import 'vite-plugin-vconsole';
6
5
 
7
- declare const _default: (options: Options) => readonly [RspackSetupMiddlewares, _unplugin.RspackPluginInstance];
6
+ declare const _default: (options: Options) => readonly [RspackSetupMiddlewares, _rspack_core_dist_config_types.RspackPluginInstance];
8
7
 
9
8
  export { _default as default };
package/dist/rspack.js CHANGED
@@ -1,13 +1,12 @@
1
1
  import {
2
2
  fetch,
3
3
  resovedInfo,
4
+ startVueDevtools,
4
5
  unpluginFactory
5
- } from "./chunk-RDTWBIQD.js";
6
- import "./chunk-PMOTCIQR.js";
6
+ } from "./chunk-QU7HLXVD.js";
7
+ import "./chunk-ZEHXGFFH.js";
7
8
 
8
9
  // src/rspack.ts
9
- import { exec } from "child_process";
10
- import process from "process";
11
10
  import { createRspackPlugin } from "unplugin";
12
11
  import cookie from "cookie";
13
12
  import c from "picocolors";
@@ -18,14 +17,15 @@ var rspack_default = (options) => {
18
17
  }
19
18
  }
20
19
  const {
21
- chii: enableChii = true
20
+ chii
22
21
  } = options || {};
22
+ const enableChii = (chii == null ? void 0 : chii.enable) !== false;
23
23
  const injectSetupMiddlewares = (middlewares, _devServer) => {
24
24
  if (options.debugCookies && options.debugCookies.length > 0) {
25
25
  middlewares.unshift((req, res, next) => {
26
26
  const cookies = cookie.parse(req.headers.cookie || "");
27
27
  for (const [name, value] of Object.entries(cookies)) {
28
- if (options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
28
+ if (value && options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
29
29
  const serializedCookie = cookie.serialize(name, value, {
30
30
  httpOnly: false
31
31
  });
@@ -48,6 +48,9 @@ var rspack_default = (options) => {
48
48
  const devToolsUrl = `http://localhost:${availablePort}/front_end/chii_app.html?ws=localhost:${availablePort}/client/${Math.random().toString(20).substring(2, 8)}?target=${data.targets[0].id}&rtc=false`;
49
49
  res.writeHead(302, { Location: devToolsUrl });
50
50
  res.end();
51
+ } else {
52
+ res.writeHead(404);
53
+ res.end();
51
54
  }
52
55
  } catch (error) {
53
56
  debug(`${error}`);
@@ -66,18 +69,7 @@ var rspack_default = (options) => {
66
69
  res.writeHead(302, {
67
70
  Location: `dingtalk://dingtalkclient/page/link?url=${encodeURIComponent(targetURL.toString())}`
68
71
  });
69
- if (((_a = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a.enable) && !resovedInfo.devtoolsInstance) {
70
- resovedInfo.devtoolsInstance = exec("npx vue-devtools");
71
- console.log(` ${c.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
72
- resovedInfo.devtoolsInstance.on("exit", () => {
73
- resovedInfo.devtoolsInstance = void 0;
74
- });
75
- process.on("exit", () => {
76
- if (resovedInfo.devtoolsInstance) {
77
- resovedInfo.devtoolsInstance.kill();
78
- }
79
- });
80
- }
72
+ startVueDevtools((_a = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a.enable);
81
73
  res.end();
82
74
  });
83
75
  return middlewares;
package/dist/types.d.cts CHANGED
@@ -5,16 +5,27 @@ import { viteVConsoleOptions } from 'vite-plugin-vconsole';
5
5
  interface Options {
6
6
  enable?: boolean;
7
7
  targetUrl?: string;
8
+ /** 是否启动dns服务器 */
9
+ dns?: {
10
+ host: string;
11
+ ip: string;
12
+ };
8
13
  corpId?: string;
9
14
  debug?: boolean;
10
15
  /** only support vite */
11
16
  vconsole?: viteVConsoleOptions;
12
- /** default `true` */
13
- chii?: boolean;
17
+ chii?: {
18
+ /** default true */
19
+ enable?: boolean;
20
+ /** default 127.0.0.1 */
21
+ host: string;
22
+ };
14
23
  debugCookies?: string[];
15
24
  vueDevtools?: {
16
25
  enable?: boolean;
26
+ /** default 127.0.0.1 */
17
27
  host?: string;
28
+ /** default 8098 */
18
29
  port?: number;
19
30
  };
20
31
  }
package/dist/types.d.ts CHANGED
@@ -5,16 +5,27 @@ import { viteVConsoleOptions } from 'vite-plugin-vconsole';
5
5
  interface Options {
6
6
  enable?: boolean;
7
7
  targetUrl?: string;
8
+ /** 是否启动dns服务器 */
9
+ dns?: {
10
+ host: string;
11
+ ip: string;
12
+ };
8
13
  corpId?: string;
9
14
  debug?: boolean;
10
15
  /** only support vite */
11
16
  vconsole?: viteVConsoleOptions;
12
- /** default `true` */
13
- chii?: boolean;
17
+ chii?: {
18
+ /** default true */
19
+ enable?: boolean;
20
+ /** default 127.0.0.1 */
21
+ host: string;
22
+ };
14
23
  debugCookies?: string[];
15
24
  vueDevtools?: {
16
25
  enable?: boolean;
26
+ /** default 127.0.0.1 */
17
27
  host?: string;
28
+ /** default 8098 */
18
29
  port?: number;
19
30
  };
20
31
  }
package/dist/vite.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) {
@@ -5217,6 +5220,8 @@ var import_vite_plugin_vconsole = __toESM(require("vite-plugin-vconsole"), 1);
5217
5220
  // src/index.ts
5218
5221
  var import_node_child_process = require("child_process");
5219
5222
  var import_node_process = __toESM(require("process"), 1);
5223
+ var import_node_http3 = require("http");
5224
+ var import_node_net2 = require("net");
5220
5225
 
5221
5226
  // node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/index.js
5222
5227
  var import_node_http2 = __toESM(require("http"), 1);
@@ -6514,20 +6519,94 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) {
6514
6519
  var import_unplugin = require("unplugin");
6515
6520
  var import_picocolors = __toESM(require("picocolors"), 1);
6516
6521
  var import_cookie = __toESM(require("cookie"), 1);
6517
- var import_chii = require("chii");
6522
+ var import_z_chii = require("z-chii");
6523
+ var import_dns2 = __toESM(require("dns2"), 1);
6518
6524
  var import_get_port_please = require("get-port-please");
6525
+ var import_http_proxy = __toESM(require("http-proxy"), 1);
6519
6526
  var cwd = import_node_process.default.cwd();
6520
6527
  var config;
6521
6528
  var colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
6522
6529
  var resovedInfo = {
6523
6530
  devtoolsInstance: void 0,
6531
+ dnsServerInstence: void 0,
6524
6532
  availablePort: void 0,
6525
6533
  targetURL: void 0
6526
6534
  };
6535
+ function startVueDevtools(enable) {
6536
+ if (enable && !resovedInfo.devtoolsInstance) {
6537
+ resovedInfo.devtoolsInstance = (0, import_node_child_process.exec)("npx vue-devtools");
6538
+ console.log(` ${import_picocolors.default.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
6539
+ resovedInfo.devtoolsInstance.on("exit", () => {
6540
+ resovedInfo.devtoolsInstance = void 0;
6541
+ });
6542
+ import_node_process.default.on("exit", () => {
6543
+ if (resovedInfo.devtoolsInstance) {
6544
+ resovedInfo.devtoolsInstance.kill();
6545
+ }
6546
+ });
6547
+ }
6548
+ }
6549
+ function startDnsServer(options) {
6550
+ function debug(...args) {
6551
+ if (options == null ? void 0 : options.debug) {
6552
+ console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
6553
+ }
6554
+ }
6555
+ if ((options == null ? void 0 : options.enable) && (options == null ? void 0 : options.dns) && !resovedInfo.dnsServerInstence) {
6556
+ const { Packet } = import_dns2.default;
6557
+ resovedInfo.dnsServerInstence = import_dns2.default.createServer({
6558
+ tcp: true,
6559
+ udp: true,
6560
+ handle: (request, send, rinfo) => {
6561
+ var _a4;
6562
+ const response = Packet.createResponseFromRequest(request);
6563
+ const [question] = request.questions;
6564
+ const { name } = question;
6565
+ if (name === options.dns.host) {
6566
+ response.answers.push({
6567
+ name,
6568
+ type: Packet.TYPE.A,
6569
+ class: Packet.CLASS.IN,
6570
+ ttl: 300,
6571
+ address: (_a4 = options.dns) == null ? void 0 : _a4.ip
6572
+ });
6573
+ debug(`dns request from ${rinfo.address}:${rinfo.port} for ${name} => ${options.dns.ip}`);
6574
+ }
6575
+ send(response);
6576
+ }
6577
+ });
6578
+ resovedInfo.dnsServerInstence.listen({
6579
+ udp: { address: "0.0.0.0", port: 53 },
6580
+ tcp: { address: "0.0.0.0", port: 53 }
6581
+ }).then(() => {
6582
+ debug("DNS server is running.");
6583
+ const dns = new import_dns2.default({
6584
+ port: 53,
6585
+ nameServers: ["127.0.0.1", "8.8.8.8"]
6586
+ });
6587
+ dns.resolveA(options.dns.host).then((addresses) => {
6588
+ var _a4;
6589
+ if (((_a4 = addresses.answers[0]) == null ? void 0 : _a4.address) === options.dns.ip) {
6590
+ startDnsServer(options);
6591
+ 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}`)}`);
6592
+ } else {
6593
+ debug(addresses.answers[0].address);
6594
+ throw new Error("DNS server started failed");
6595
+ }
6596
+ }).catch((e2) => {
6597
+ throw e2;
6598
+ });
6599
+ }).catch((e2) => {
6600
+ debug(e2);
6601
+ console.log(` ${import_picocolors.default.red("\u279C")} ${import_picocolors.default.bold((e2 == null ? void 0 : e2.message) || e2)}`);
6602
+ });
6603
+ }
6604
+ }
6527
6605
  var unpluginFactory = (options) => {
6528
6606
  const {
6529
- chii: enableChii = true
6607
+ chii
6530
6608
  } = options || {};
6609
+ const enableChii = (chii == null ? void 0 : chii.enable) !== false;
6531
6610
  function debug(...args) {
6532
6611
  if (options == null ? void 0 : options.debug) {
6533
6612
  console.log(` ${import_picocolors.default.yellow("DEBUG")} `, ...args);
@@ -6543,22 +6622,16 @@ var unpluginFactory = (options) => {
6543
6622
  var _a4, _b, _c;
6544
6623
  if ((options == null ? void 0 : options.enable) && enableChii && !resovedInfo.availablePort) {
6545
6624
  resovedInfo.availablePort = await (0, import_get_port_please.getRandomPort)();
6546
- debug(`chii server port: ${JSON.stringify({ availablePort: resovedInfo.availablePort })}`);
6547
- (0, import_chii.start)({
6548
- host: "0.0.0.0",
6625
+ (0, import_z_chii.start)({
6549
6626
  port: resovedInfo.availablePort
6550
6627
  });
6628
+ debug(`chii server port: ${resovedInfo.availablePort}`);
6551
6629
  }
6552
6630
  if (options == null ? void 0 : options.enable) {
6553
6631
  const codes = [
6554
6632
  "/* eslint-disable */;",
6555
6633
  ((_a4 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a4.enable) ? `import { devtools } from '@vue/devtools'
6556
- 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});` : "",
6557
- (options == null ? void 0 : options.enable) && enableChii ? `(() => {
6558
- const script = document.createElement('script');
6559
- script.src="http://localhost:${resovedInfo.availablePort}/target.js";
6560
- document.body.appendChild(script);
6561
- })()` : "",
6634
+ 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});` : "",
6562
6635
  "/* eslint-enable */",
6563
6636
  `${_source};`
6564
6637
  ];
@@ -6578,6 +6651,22 @@ var unpluginFactory = (options) => {
6578
6651
  configResolved(_config) {
6579
6652
  config = _config;
6580
6653
  },
6654
+ transformIndexHtml(html) {
6655
+ if (enableChii) {
6656
+ return html.replace(
6657
+ "</body>",
6658
+ `</body>
6659
+ <script>
6660
+ (() => {
6661
+ const script = document.createElement('script');
6662
+ script.src="./__chii_proxy/target.js";
6663
+ document.body.appendChild(script);
6664
+ })()
6665
+ </script>
6666
+ `
6667
+ );
6668
+ }
6669
+ },
6581
6670
  async configureServer(server) {
6582
6671
  var _a4, _b;
6583
6672
  if (!(options == null ? void 0 : options.enable)) {
@@ -6613,7 +6702,7 @@ var unpluginFactory = (options) => {
6613
6702
  server.middlewares.use((req, res, next) => {
6614
6703
  const cookies = import_cookie.default.parse(req.headers.cookie || "");
6615
6704
  for (const [name, value] of Object.entries(cookies)) {
6616
- if (options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
6705
+ if (value && options.debugCookies && options.debugCookies.length > 0 && options.debugCookies.includes(name)) {
6617
6706
  const serializedCookie = import_cookie.default.serialize(name, value, {
6618
6707
  httpOnly: false
6619
6708
  });
@@ -6624,6 +6713,51 @@ var unpluginFactory = (options) => {
6624
6713
  });
6625
6714
  }
6626
6715
  if (enableChii) {
6716
+ let createProxyMiddleware2 = function() {
6717
+ let proxy = null;
6718
+ const handleUpgrade = (req, socket, head) => {
6719
+ var _a5;
6720
+ if (proxy && ((_a5 = req.url) == null ? void 0 : _a5.startsWith("/__chii_proxy"))) {
6721
+ debug("WS upgrade:", req.url);
6722
+ req.url = req.url.replace("/__chii_proxy", "");
6723
+ proxy.ws(req, socket, head);
6724
+ }
6725
+ };
6726
+ return (resolvedInfo) => {
6727
+ return (req, res, next) => {
6728
+ var _a5;
6729
+ if (!proxy && resolvedInfo.availablePort) {
6730
+ proxy = import_http_proxy.default.createProxyServer({
6731
+ target: `http://localhost:${resolvedInfo.availablePort}`,
6732
+ ws: true
6733
+ // changeOrigin: true, // Consider if you need this
6734
+ });
6735
+ proxy.on("error", (err, req2, res2) => {
6736
+ console.error("Proxy error:", err);
6737
+ if (res2 instanceof import_node_http3.ServerResponse) {
6738
+ if (!res2.headersSent) {
6739
+ res2.writeHead(500, { "Content-Type": "text/plain" });
6740
+ }
6741
+ res2.end(`Proxy error: ${err.message}`);
6742
+ } else if (res2 instanceof import_node_net2.Socket) {
6743
+ res2.destroy();
6744
+ }
6745
+ });
6746
+ if (req.socket.server) {
6747
+ req.socket.server.on("upgrade", handleUpgrade);
6748
+ }
6749
+ }
6750
+ if (proxy && ((_a5 = req.url) == null ? void 0 : _a5.startsWith("/__chii_proxy"))) {
6751
+ debug(req.url);
6752
+ req.url = req.url.replace("/__chii_proxy", "");
6753
+ proxy.web(req, res);
6754
+ } else {
6755
+ next();
6756
+ }
6757
+ };
6758
+ };
6759
+ };
6760
+ var createProxyMiddleware = createProxyMiddleware2;
6627
6761
  server.middlewares.use("/__chrome_devtools", async (_req, res) => {
6628
6762
  try {
6629
6763
  const raw = await fetch(`http://localhost:${resovedInfo.availablePort}/targets`);
@@ -6632,6 +6766,9 @@ var unpluginFactory = (options) => {
6632
6766
  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`;
6633
6767
  res.writeHead(302, { Location: devToolsUrl });
6634
6768
  res.end();
6769
+ } else {
6770
+ res.writeHead(404);
6771
+ res.end();
6635
6772
  }
6636
6773
  } catch (error) {
6637
6774
  debug(`${error}`);
@@ -6639,6 +6776,8 @@ var unpluginFactory = (options) => {
6639
6776
  res.end();
6640
6777
  }
6641
6778
  });
6779
+ const proxyMiddleware = createProxyMiddleware2();
6780
+ server.middlewares.use(proxyMiddleware(resovedInfo));
6642
6781
  }
6643
6782
  server.middlewares.use("/open-dingtalk", (req, res) => {
6644
6783
  var _a5;
@@ -6646,20 +6785,10 @@ var unpluginFactory = (options) => {
6646
6785
  res.writeHead(302, {
6647
6786
  Location: `dingtalk://dingtalkclient/page/link?url=${encodeURIComponent(targetURL.toString())}`
6648
6787
  });
6649
- if (((_a5 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a5.enable) && !resovedInfo.devtoolsInstance) {
6650
- resovedInfo.devtoolsInstance = (0, import_node_child_process.exec)("npx vue-devtools");
6651
- console.log(` ${import_picocolors.default.green("\u279C")} vue-devtools is running. If the devtools has no data, please refresh the page in dingtalk.`);
6652
- resovedInfo.devtoolsInstance.on("exit", () => {
6653
- resovedInfo.devtoolsInstance = void 0;
6654
- });
6655
- import_node_process.default.on("exit", () => {
6656
- if (resovedInfo.devtoolsInstance) {
6657
- resovedInfo.devtoolsInstance.kill();
6658
- }
6659
- });
6660
- }
6788
+ startVueDevtools((_a5 = options == null ? void 0 : options.vueDevtools) == null ? void 0 : _a5.enable);
6661
6789
  res.end();
6662
6790
  });
6791
+ startDnsServer(options);
6663
6792
  }
6664
6793
  },
6665
6794
  webpack(compiler) {
@@ -6690,6 +6819,7 @@ var unpluginFactory = (options) => {
6690
6819
  if (options == null ? void 0 : options.corpId) {
6691
6820
  resovedInfo.targetURL.searchParams.append("corpId", options.corpId);
6692
6821
  }
6822
+ startDnsServer(options);
6693
6823
  },
6694
6824
  async rspack(compiler) {
6695
6825
  var _a4, _b, _c, _d2, _e;
@@ -6718,6 +6848,7 @@ var unpluginFactory = (options) => {
6718
6848
  "Click to open chrome devtools"
6719
6849
  )}: ${colorUrl(`http://${source}${base}__chrome_devtools`)}`);
6720
6850
  }
6851
+ startDnsServer(options);
6721
6852
  }
6722
6853
  };
6723
6854
  return unpluginDing;
package/dist/vite.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  vite_default
3
- } from "./chunk-2OB7UBRW.js";
4
- import "./chunk-RDTWBIQD.js";
5
- import "./chunk-PMOTCIQR.js";
3
+ } from "./chunk-VQXY6FXR.js";
4
+ import "./chunk-QU7HLXVD.js";
5
+ import "./chunk-ZEHXGFFH.js";
6
6
  export {
7
7
  vite_default as default
8
8
  };