unplugin-cloudflare-tunnel 0.0.1 → 0.0.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,40 +1,40 @@
1
- import * as z from "zod/mini";
1
+ import { a as number, c as string, i as nullish, l as unknown, n as array, o as object, r as boolean, s as optional } from "./schemas-CwcXCIyR.mjs";
2
2
  import NodeFS from "node:fs/promises";
3
3
  import { install } from "cloudflared";
4
4
 
5
5
  //#region src/api.ts
6
- const CloudflareErrorSchema = z.object({
7
- code: z.number(),
8
- message: z.string()
6
+ const CloudflareErrorSchema = object({
7
+ code: number(),
8
+ message: string()
9
9
  });
10
- const CloudflareApiResponseSchema = z.object({
11
- success: z.boolean(),
12
- errors: z.optional(z.array(CloudflareErrorSchema)),
13
- messages: z.optional(z.array(z.string())),
14
- result: z.unknown()
10
+ const CloudflareApiResponseSchema = object({
11
+ success: boolean(),
12
+ errors: optional(array(CloudflareErrorSchema)),
13
+ messages: optional(array(string())),
14
+ result: unknown()
15
15
  });
16
- const AccountSchema = z.object({
17
- id: z.string(),
18
- name: z.string()
16
+ const AccountSchema = object({
17
+ id: string(),
18
+ name: string()
19
19
  });
20
- const ZoneSchema = z.object({
21
- id: z.string(),
22
- name: z.string()
20
+ const ZoneSchema = object({
21
+ id: string(),
22
+ name: string()
23
23
  });
24
- const TunnelSchema = z.object({
25
- id: z.string(),
26
- name: z.string(),
27
- account_tag: z.string(),
28
- created_at: z.string(),
29
- connections: z.optional(z.array(z.unknown()))
24
+ const TunnelSchema = object({
25
+ id: string(),
26
+ name: string(),
27
+ account_tag: string(),
28
+ created_at: string(),
29
+ connections: optional(array(unknown()))
30
30
  });
31
- const DNSRecordSchema = z.object({
32
- id: z.string(),
33
- type: z.string(),
34
- name: z.string(),
35
- content: z.string(),
36
- proxied: z.boolean(),
37
- comment: z.nullish(z.string())
31
+ const DNSRecordSchema = object({
32
+ id: string(),
33
+ type: string(),
34
+ name: string(),
35
+ content: string(),
36
+ proxied: boolean(),
37
+ comment: nullish(string())
38
38
  });
39
39
  function normalizeAddress(address) {
40
40
  if (address && typeof address === "object") return {
@@ -1,4 +1,4 @@
1
- import { r as CloudflareTunnel } from "./index-BK_mUExr.js";
1
+ import { CloudflareTunnel } from "./index.mjs";
2
2
 
3
3
  //#region src/esbuild.d.ts
4
4
 
@@ -1,7 +1,12 @@
1
- import { t as CloudflareTunnel } from "./src-CVRdCQUT.js";
1
+ import { CloudflareTunnel } from "./index.mjs";
2
2
 
3
3
  //#region src/esbuild.ts
4
4
  /**
5
+ * This entry file is for esbuild plugin.
6
+ *
7
+ * @module
8
+ */
9
+ /**
5
10
  * Esbuild plugin
6
11
  *
7
12
  * @example
@@ -1,4 +1,4 @@
1
- import { r as CloudflareTunnel } from "./index-BK_mUExr.js";
1
+ import { CloudflareTunnel } from "./index.mjs";
2
2
 
3
3
  //#region src/farm.d.ts
4
4
 
@@ -1,7 +1,12 @@
1
- import { t as CloudflareTunnel } from "./src-CVRdCQUT.js";
1
+ import { CloudflareTunnel } from "./index.mjs";
2
2
 
3
3
  //#region src/farm.ts
4
4
  /**
5
+ * This entry file is for Farm plugin.
6
+ *
7
+ * @module
8
+ */
9
+ /**
5
10
  * Farm plugin
6
11
  *
7
12
  * @example
@@ -2,7 +2,17 @@ import { UnpluginInstance } from "unplugin";
2
2
  import * as z from "zod/mini";
3
3
 
4
4
  //#region src/index.d.ts
5
-
5
+ /**
6
+ * @fileoverview Cloudflare Tunnel Unplugin
7
+ *
8
+ * A cross-bundler plugin that automatically creates and manages
9
+ * Cloudflare tunnels for local development, providing instant HTTPS access
10
+ * to your local dev server from anywhere on the internet.
11
+ *
12
+ * @author Cloudflare Tunnel Plugin Contributors
13
+ * @version 1.0.0
14
+ * @license MIT
15
+ */
6
16
  declare const CloudflareApiResponseSchema: z.ZodMiniObject<{
7
17
  success: z.ZodMiniBoolean<boolean>;
8
18
  errors: z.ZodMiniOptional<z.ZodMiniArray<z.ZodMiniObject<{
@@ -154,4 +164,4 @@ interface QuickTunnelOptions extends BaseTunnelOptions {}
154
164
  type CloudflareTunnelOptions = NamedTunnelOptions | QuickTunnelOptions;
155
165
  declare const CloudflareTunnel: UnpluginInstance<CloudflareTunnelOptions | undefined, false>;
156
166
  //#endregion
157
- export { DNSRecord as a, CloudflareTunnelOptions as i, CloudflareApiResponse as n, Tunnel as o, CloudflareTunnel as r, Zone as s, Account as t };
167
+ export { Account, CloudflareApiResponse, CloudflareTunnel, CloudflareTunnel as default, CloudflareTunnelOptions, DNSRecord, Tunnel, Zone };
@@ -1,10 +1,21 @@
1
+ import { a as number, c as string, i as nullish, l as unknown, n as array, o as object, r as boolean, s as optional, t as any } from "./schemas-CwcXCIyR.mjs";
1
2
  import { createUnplugin } from "unplugin";
2
- import * as z from "zod/mini";
3
3
  import NodeFS from "node:fs/promises";
4
4
  import { bin, install } from "cloudflared";
5
5
  import * as NodeChildProcess from "node:child_process";
6
6
 
7
7
  //#region src/index.ts
8
+ /**
9
+ * @fileoverview Cloudflare Tunnel Unplugin
10
+ *
11
+ * A cross-bundler plugin that automatically creates and manages
12
+ * Cloudflare tunnels for local development, providing instant HTTPS access
13
+ * to your local dev server from anywhere on the internet.
14
+ *
15
+ * @author Cloudflare Tunnel Plugin Contributors
16
+ * @version 1.0.0
17
+ * @license MIT
18
+ */
8
19
  const PLUGIN_NAME = "unplugin-cloudflare-tunnel";
9
20
  const INFO_LOG_REGEX = /^.*Z INF .*/;
10
21
  const LOG_LEVEL_RANK = {
@@ -40,38 +51,38 @@ function colorize(text, ansi) {
40
51
  if (!supportsColor()) return text;
41
52
  return `${ansi}${text}${ANSI.reset}`;
42
53
  }
43
- const CloudflareErrorSchema = z.object({
44
- code: z.number(),
45
- message: z.string()
54
+ const CloudflareErrorSchema = object({
55
+ code: number(),
56
+ message: string()
46
57
  });
47
- const CloudflareApiResponseSchema = z.object({
48
- success: z.boolean(),
49
- errors: z.optional(z.array(CloudflareErrorSchema)),
50
- messages: z.optional(z.array(z.string())),
51
- result: z.unknown()
58
+ const CloudflareApiResponseSchema = object({
59
+ success: boolean(),
60
+ errors: optional(array(CloudflareErrorSchema)),
61
+ messages: optional(array(string())),
62
+ result: unknown()
52
63
  });
53
- const AccountSchema = z.object({
54
- id: z.string(),
55
- name: z.string()
64
+ const AccountSchema = object({
65
+ id: string(),
66
+ name: string()
56
67
  });
57
- const ZoneSchema = z.object({
58
- id: z.string(),
59
- name: z.string()
68
+ const ZoneSchema = object({
69
+ id: string(),
70
+ name: string()
60
71
  });
61
- const TunnelSchema = z.object({
62
- id: z.string(),
63
- name: z.string(),
64
- account_tag: z.string(),
65
- created_at: z.string(),
66
- connections: z.optional(z.array(z.unknown()))
72
+ const TunnelSchema = object({
73
+ id: string(),
74
+ name: string(),
75
+ account_tag: string(),
76
+ created_at: string(),
77
+ connections: optional(array(unknown()))
67
78
  });
68
- const DNSRecordSchema = z.object({
69
- id: z.string(),
70
- type: z.string(),
71
- name: z.string(),
72
- content: z.string(),
73
- proxied: z.boolean(),
74
- comment: z.nullish(z.string())
79
+ const DNSRecordSchema = object({
80
+ id: string(),
81
+ type: string(),
82
+ name: string(),
83
+ content: string(),
84
+ proxied: boolean(),
85
+ comment: nullish(string())
75
86
  });
76
87
  const unpluginFactory = (options = {}) => {
77
88
  const { enabled = true } = options;
@@ -81,10 +92,13 @@ const unpluginFactory = (options = {}) => {
81
92
  name: PLUGIN_NAME,
82
93
  enforce: "pre",
83
94
  resolveId(id) {
84
- if (id === VIRTUAL_MODULE_ID_STUB) return "\0" + VIRTUAL_MODULE_ID_STUB;
95
+ if (id === VIRTUAL_MODULE_ID_STUB) return id;
96
+ },
97
+ loadInclude(id) {
98
+ return id === VIRTUAL_MODULE_ID_STUB;
85
99
  },
86
100
  load(id) {
87
- if (id === "\0" + VIRTUAL_MODULE_ID_STUB) return "export function getTunnelUrl() { return \"\"; }";
101
+ if (id === VIRTUAL_MODULE_ID_STUB) return "export function getTunnelUrl() { return \"\"; }";
88
102
  }
89
103
  };
90
104
  }
@@ -235,7 +249,7 @@ const unpluginFactory = (options = {}) => {
235
249
  };
236
250
  const findMismatchedSslCertificates = async (apiToken, zoneId, currentTunnelName, currentHostname) => {
237
251
  try {
238
- const certPacks = await cf(apiToken, "GET", `/zones/${zoneId}/ssl/certificate_packs?status=all`, void 0, z.any());
252
+ const certPacks = await cf(apiToken, "GET", `/zones/${zoneId}/ssl/certificate_packs?status=all`, void 0, any());
239
253
  const currentTunnelCerts = (Array.isArray(certPacks) ? certPacks : certPacks.result || []).filter((cert) => {
240
254
  return (cert.hostnames || cert.hosts || []).some((host) => host.startsWith(`cf-tunnel-plugin-${currentTunnelName}--`));
241
255
  });
@@ -259,7 +273,7 @@ const unpluginFactory = (options = {}) => {
259
273
  };
260
274
  const cleanupMismatchedDnsRecords = async (apiToken, zoneId, dnsComment, currentHostname, tunnelId) => {
261
275
  try {
262
- const pluginDnsRecords = await cf(apiToken, "GET", `/zones/${zoneId}/dns_records?comment=${dnsComment}&match=all`, void 0, z.array(DNSRecordSchema));
276
+ const pluginDnsRecords = await cf(apiToken, "GET", `/zones/${zoneId}/dns_records?comment=${dnsComment}&match=all`, void 0, array(DNSRecordSchema));
263
277
  debugLog(`Found ${pluginDnsRecords.length} DNS records for current tunnel: ${dnsComment}`);
264
278
  const expectedCnameContent = `${tunnelId}.cfargotunnel.com`;
265
279
  const mismatchedRecords = pluginDnsRecords.filter((record) => {
@@ -576,7 +590,7 @@ const unpluginFactory = (options = {}) => {
576
590
  if (!apiToken) throw new Error("[unplugin-cloudflare-tunnel] API token is required. Provide it via 'apiToken' option or set the CLOUDFLARE_API_KEY environment variable. Get your token at: https://dash.cloudflare.com/profile/api-tokens");
577
591
  debugLog(`[unplugin-cloudflare-tunnel] Using port ${port}${userProvidedPort === port ? " (user-provided)" : " (from bundler config)"}`);
578
592
  await ensureCloudflaredBinary(bin);
579
- const accounts = await cf(apiToken, "GET", "/accounts", void 0, z.array(AccountSchema));
593
+ const accounts = await cf(apiToken, "GET", "/accounts", void 0, array(AccountSchema));
580
594
  const accountId = forcedAccount || accounts[0]?.id;
581
595
  if (!accountId) throw new Error("Unable to determine Cloudflare account ID");
582
596
  const apexDomain = hostname.split(".").slice(-2).join(".");
@@ -587,16 +601,16 @@ const unpluginFactory = (options = {}) => {
587
601
  if (!zoneId) {
588
602
  let zones = [];
589
603
  try {
590
- zones = await cf(apiToken, "GET", `/zones?name=${parentDomain}`, void 0, z.array(ZoneSchema));
604
+ zones = await cf(apiToken, "GET", `/zones?name=${parentDomain}`, void 0, array(ZoneSchema));
591
605
  } catch (error) {
592
606
  debugLog("← Error fetching zone for parent domain", error);
593
607
  }
594
- if (zones.length === 0) zones = await cf(apiToken, "GET", `/zones?name=${apexDomain}`, void 0, z.array(ZoneSchema));
608
+ if (zones.length === 0) zones = await cf(apiToken, "GET", `/zones?name=${apexDomain}`, void 0, array(ZoneSchema));
595
609
  zoneId = zones[0]?.id;
596
610
  }
597
611
  if (!zoneId) throw new Error(`Zone ${apexDomain} not found in account ${accountId}`);
598
612
  const { autoCleanup = true } = cleanupConfig;
599
- let tunnel = (await cf(apiToken, "GET", `/accounts/${accountId}/cfd_tunnel?name=${tunnelName}`, void 0, z.array(TunnelSchema)))[0];
613
+ let tunnel = (await cf(apiToken, "GET", `/accounts/${accountId}/cfd_tunnel?name=${tunnelName}`, void 0, array(TunnelSchema)))[0];
600
614
  if (!tunnel) {
601
615
  pluginLog.info(`Creating tunnel '${tunnelName}'...`);
602
616
  tunnel = await cf(apiToken, "POST", `/accounts/${accountId}/cfd_tunnel`, {
@@ -626,7 +640,7 @@ const unpluginFactory = (options = {}) => {
626
640
  };
627
641
  if (dnsOption) {
628
642
  const ensureDnsRecord = async (type, content) => {
629
- if ((await cf(apiToken, "GET", `/zones/${zoneId}/dns_records?type=${type}&name=${encodeURIComponent(dnsOption)}`, void 0, z.array(DNSRecordSchema))).length === 0) {
643
+ if ((await cf(apiToken, "GET", `/zones/${zoneId}/dns_records?type=${type}&name=${encodeURIComponent(dnsOption)}`, void 0, array(DNSRecordSchema))).length === 0) {
630
644
  console.log(`[unplugin-cloudflare-tunnel] Creating ${type} record for ${dnsOption}...`);
631
645
  await cf(apiToken, "POST", `/zones/${zoneId}/dns_records`, {
632
646
  type,
@@ -640,22 +654,34 @@ const unpluginFactory = (options = {}) => {
640
654
  await ensureDnsRecord("CNAME", `${tunnelId}.cfargotunnel.com`);
641
655
  } else {
642
656
  const wildcardDns = `*.${parentDomain}`;
643
- if ((await cf(apiToken, "GET", `/zones/${zoneId}/dns_records?type=CNAME&name=${wildcardDns}`, void 0, z.array(DNSRecordSchema))).length === 0) {
644
- if (!((await cf(apiToken, "GET", `/zones/${zoneId}/dns_records?type=CNAME&name=${hostname}`, void 0, z.array(DNSRecordSchema))).length > 0)) {
657
+ if ((await cf(apiToken, "GET", `/zones/${zoneId}/dns_records?type=CNAME&name=${wildcardDns}`, void 0, array(DNSRecordSchema))).length === 0) {
658
+ const existingRecord = (await cf(apiToken, "GET", `/zones/${zoneId}/dns_records?type=CNAME&name=${hostname}`, void 0, array(DNSRecordSchema)))[0];
659
+ const expectedContent = `${tunnelId}.cfargotunnel.com`;
660
+ if (!existingRecord) {
645
661
  console.log(`[unplugin-cloudflare-tunnel] Creating DNS record for ${hostname}...`);
646
662
  await cf(apiToken, "POST", `/zones/${zoneId}/dns_records`, {
647
663
  type: "CNAME",
648
664
  name: hostname,
649
- content: `${tunnelId}.cfargotunnel.com`,
665
+ content: expectedContent,
666
+ proxied: true,
667
+ comment: generateDnsComment()
668
+ }, DNSRecordSchema);
669
+ } else if (existingRecord.content !== expectedContent) {
670
+ debugLog(`← DNS record for ${hostname} points to different tunnel, updating...`);
671
+ pluginLog.info(`Updating DNS record for ${hostname} to point to tunnel '${tunnelName}'...`);
672
+ await cf(apiToken, "PUT", `/zones/${zoneId}/dns_records/${existingRecord.id}`, {
673
+ type: "CNAME",
674
+ name: hostname,
675
+ content: expectedContent,
650
676
  proxied: true,
651
677
  comment: generateDnsComment()
652
678
  }, DNSRecordSchema);
653
679
  }
654
680
  }
655
681
  }
656
- const token = await cf(apiToken, "GET", `/accounts/${accountId}/cfd_tunnel/${tunnelId}/token`, void 0, z.string());
682
+ const token = await cf(apiToken, "GET", `/accounts/${accountId}/cfd_tunnel/${tunnelId}/token`, void 0, string());
657
683
  try {
658
- const certListRaw = await cf(apiToken, "GET", `/zones/${zoneId}/ssl/certificate_packs?status=all`, void 0, z.any());
684
+ const certListRaw = await cf(apiToken, "GET", `/zones/${zoneId}/ssl/certificate_packs?status=all`, void 0, any());
659
685
  const certPacks = Array.isArray(certListRaw) ? certListRaw : certListRaw.result || [];
660
686
  const certContainingHost = (host) => certPacks.filter((c) => (c.hostnames || c.hosts || []).includes(host))?.[0];
661
687
  if (sslOption) {
@@ -681,7 +707,7 @@ const unpluginFactory = (options = {}) => {
681
707
  const wildcardDomain = `*.${parentDomain}`;
682
708
  const wildcardExists = certContainingHost(wildcardDomain);
683
709
  if (!wildcardExists) {
684
- const totalTls = await cf(apiToken, "GET", `/zones/${zoneId}/acm/total_tls`, void 0, z.object({ status: z.string() }));
710
+ const totalTls = await cf(apiToken, "GET", `/zones/${zoneId}/acm/total_tls`, void 0, object({ status: string() }));
685
711
  debugLog("← Total TLS", totalTls);
686
712
  const existingHostnameCert = certContainingHost(hostname);
687
713
  if (totalTls.status !== "on" && !existingHostnameCert) {
@@ -912,15 +938,20 @@ const unpluginFactory = (options = {}) => {
912
938
  return {
913
939
  name: PLUGIN_NAME,
914
940
  enforce: "pre",
915
- resolveId: (id) => {
941
+ resolveId(id) {
916
942
  if (id === VIRTUAL_MODULE_ID) {
917
943
  debugLog("resolveId called for", id);
918
- return "\0" + VIRTUAL_MODULE_ID;
944
+ return id;
919
945
  }
920
946
  },
921
- load: async (id) => {
922
- const url = await globalState.tunnelUrl;
923
- if (id === "\0" + VIRTUAL_MODULE_ID) return `export function getTunnelUrl() { return ${JSON.stringify(url || "")}; }`;
947
+ loadInclude(id) {
948
+ return id === VIRTUAL_MODULE_ID;
949
+ },
950
+ async load(id) {
951
+ if (id === VIRTUAL_MODULE_ID) {
952
+ const url = await globalState.tunnelUrl;
953
+ return `export function getTunnelUrl() { return ${JSON.stringify(url || "")}; }`;
954
+ }
924
955
  },
925
956
  vite: {
926
957
  config: (config) => {
@@ -984,4 +1015,4 @@ const CloudflareTunnel = createUnplugin(unpluginFactory);
984
1015
  var src_default = CloudflareTunnel;
985
1016
 
986
1017
  //#endregion
987
- export { src_default as n, CloudflareTunnel as t };
1018
+ export { CloudflareTunnel, src_default as default };
@@ -1,4 +1,4 @@
1
- import { r as CloudflareTunnel } from "./index-BK_mUExr.js";
1
+ import { CloudflareTunnel } from "./index.mjs";
2
2
 
3
3
  //#region src/rolldown.d.ts
4
4
 
@@ -1,7 +1,12 @@
1
- import { t as CloudflareTunnel } from "./src-CVRdCQUT.js";
1
+ import { CloudflareTunnel } from "./index.mjs";
2
2
 
3
3
  //#region src/rolldown.ts
4
4
  /**
5
+ * This entry file is for Rolldown plugin.
6
+ *
7
+ * @module
8
+ */
9
+ /**
5
10
  * Rolldown plugin
6
11
  *
7
12
  * @example
@@ -1,4 +1,4 @@
1
- import { r as CloudflareTunnel } from "./index-BK_mUExr.js";
1
+ import { CloudflareTunnel } from "./index.mjs";
2
2
 
3
3
  //#region src/rollup.d.ts
4
4
 
@@ -1,7 +1,12 @@
1
- import { t as CloudflareTunnel } from "./src-CVRdCQUT.js";
1
+ import { CloudflareTunnel } from "./index.mjs";
2
2
 
3
3
  //#region src/rollup.ts
4
4
  /**
5
+ * This entry file is for Rollup plugin.
6
+ *
7
+ * @module
8
+ */
9
+ /**
5
10
  * Rollup plugin
6
11
  *
7
12
  * @example
@@ -1,4 +1,4 @@
1
- import { r as CloudflareTunnel } from "./index-BK_mUExr.js";
1
+ import { CloudflareTunnel } from "./index.mjs";
2
2
 
3
3
  //#region src/rspack.d.ts
4
4
 
@@ -1,7 +1,12 @@
1
- import { t as CloudflareTunnel } from "./src-CVRdCQUT.js";
1
+ import { CloudflareTunnel } from "./index.mjs";
2
2
 
3
3
  //#region src/rspack.ts
4
4
  /**
5
+ * This entry file is for Rspack plugin.
6
+ *
7
+ * @module
8
+ */
9
+ /**
5
10
  * Rspack plugin
6
11
  *
7
12
  * @example