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.
- package/dist/{api.js → api.mjs} +28 -28
- package/dist/{esbuild.d.ts → esbuild.d.mts} +1 -1
- package/dist/{esbuild.js → esbuild.mjs} +6 -1
- package/dist/{farm.d.ts → farm.d.mts} +1 -1
- package/dist/{farm.js → farm.mjs} +6 -1
- package/dist/{index-BK_mUExr.d.ts → index.d.mts} +12 -2
- package/dist/{src-CVRdCQUT.js → index.mjs} +80 -49
- package/dist/{rolldown.d.ts → rolldown.d.mts} +1 -1
- package/dist/{rolldown.js → rolldown.mjs} +6 -1
- package/dist/{rollup.d.ts → rollup.d.mts} +1 -1
- package/dist/{rollup.js → rollup.mjs} +6 -1
- package/dist/{rspack.d.ts → rspack.d.mts} +1 -1
- package/dist/{rspack.js → rspack.mjs} +6 -1
- package/dist/schemas-CwcXCIyR.mjs +941 -0
- package/dist/{vite.d.ts → vite.d.mts} +1 -1
- package/dist/{vite.js → vite.mjs} +6 -1
- package/dist/{webpack.d.ts → webpack.d.mts} +1 -1
- package/dist/{webpack.js → webpack.mjs} +6 -1
- package/package.json +36 -32
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -3
- /package/dist/{api.d.ts → api.d.mts} +0 -0
- /package/dist/{astro.d.ts → astro.d.mts} +0 -0
- /package/dist/{astro.js → astro.mjs} +0 -0
- /package/dist/{virtual.d.ts → virtual.d.mts} +0 -0
package/dist/{api.js → api.mjs}
RENAMED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import
|
|
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 =
|
|
7
|
-
code:
|
|
8
|
-
message:
|
|
6
|
+
const CloudflareErrorSchema = object({
|
|
7
|
+
code: number(),
|
|
8
|
+
message: string()
|
|
9
9
|
});
|
|
10
|
-
const CloudflareApiResponseSchema =
|
|
11
|
-
success:
|
|
12
|
-
errors:
|
|
13
|
-
messages:
|
|
14
|
-
result:
|
|
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 =
|
|
17
|
-
id:
|
|
18
|
-
name:
|
|
16
|
+
const AccountSchema = object({
|
|
17
|
+
id: string(),
|
|
18
|
+
name: string()
|
|
19
19
|
});
|
|
20
|
-
const ZoneSchema =
|
|
21
|
-
id:
|
|
22
|
-
name:
|
|
20
|
+
const ZoneSchema = object({
|
|
21
|
+
id: string(),
|
|
22
|
+
name: string()
|
|
23
23
|
});
|
|
24
|
-
const TunnelSchema =
|
|
25
|
-
id:
|
|
26
|
-
name:
|
|
27
|
-
account_tag:
|
|
28
|
-
created_at:
|
|
29
|
-
connections:
|
|
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 =
|
|
32
|
-
id:
|
|
33
|
-
type:
|
|
34
|
-
name:
|
|
35
|
-
content:
|
|
36
|
-
proxied:
|
|
37
|
-
comment:
|
|
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 {
|
|
@@ -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 {
|
|
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 =
|
|
44
|
-
code:
|
|
45
|
-
message:
|
|
54
|
+
const CloudflareErrorSchema = object({
|
|
55
|
+
code: number(),
|
|
56
|
+
message: string()
|
|
46
57
|
});
|
|
47
|
-
const CloudflareApiResponseSchema =
|
|
48
|
-
success:
|
|
49
|
-
errors:
|
|
50
|
-
messages:
|
|
51
|
-
result:
|
|
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 =
|
|
54
|
-
id:
|
|
55
|
-
name:
|
|
64
|
+
const AccountSchema = object({
|
|
65
|
+
id: string(),
|
|
66
|
+
name: string()
|
|
56
67
|
});
|
|
57
|
-
const ZoneSchema =
|
|
58
|
-
id:
|
|
59
|
-
name:
|
|
68
|
+
const ZoneSchema = object({
|
|
69
|
+
id: string(),
|
|
70
|
+
name: string()
|
|
60
71
|
});
|
|
61
|
-
const TunnelSchema =
|
|
62
|
-
id:
|
|
63
|
-
name:
|
|
64
|
-
account_tag:
|
|
65
|
-
created_at:
|
|
66
|
-
connections:
|
|
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 =
|
|
69
|
-
id:
|
|
70
|
-
type:
|
|
71
|
-
name:
|
|
72
|
-
content:
|
|
73
|
-
proxied:
|
|
74
|
-
comment:
|
|
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
|
|
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 ===
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
644
|
-
|
|
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:
|
|
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,
|
|
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,
|
|
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,
|
|
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
|
|
941
|
+
resolveId(id) {
|
|
916
942
|
if (id === VIRTUAL_MODULE_ID) {
|
|
917
943
|
debugLog("resolveId called for", id);
|
|
918
|
-
return
|
|
944
|
+
return id;
|
|
919
945
|
}
|
|
920
946
|
},
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
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 {
|
|
1018
|
+
export { CloudflareTunnel, src_default as default };
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
import {
|
|
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
|