@flash-tunnel/adapter-cloudflare 0.2.0 → 0.4.0

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/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { DnsRecordType, DnsRecordSpec, DnsAdapter, TunnelAdapter } from '@flash-tunnel/types';
1
+ import { TunnelAdapter } from '@flash-tunnel/types';
2
2
 
3
3
  interface CloudflareApiOptions {
4
4
  apiToken: string;
@@ -9,11 +9,6 @@ interface CloudflareAccount {
9
9
  id: string;
10
10
  name: string;
11
11
  }
12
- interface CloudflareZone {
13
- id: string;
14
- name: string;
15
- status: string;
16
- }
17
12
  interface CloudflareTunnel {
18
13
  id: string;
19
14
  name: string;
@@ -23,14 +18,6 @@ interface CloudflareTunnel {
23
18
  }>;
24
19
  token?: string;
25
20
  }
26
- interface CloudflareDnsRecord {
27
- id: string;
28
- name: string;
29
- type: DnsRecordType;
30
- content: string;
31
- proxied: boolean;
32
- comment?: string;
33
- }
34
21
  declare class CloudflareApi {
35
22
  private readonly apiToken;
36
23
  private readonly apiBase;
@@ -40,29 +27,13 @@ declare class CloudflareApi {
40
27
  verifyToken(): Promise<void>;
41
28
  listAccounts(): Promise<CloudflareAccount[]>;
42
29
  resolveAccountId(preferred?: string): Promise<string>;
43
- listZones(name?: string): Promise<CloudflareZone[]>;
44
- resolveZone(zone: string): Promise<CloudflareZone>;
45
30
  createTunnel(accountId: string, name: string): Promise<CloudflareTunnel>;
46
31
  getTunnel(accountId: string, tunnelId: string): Promise<CloudflareTunnel>;
47
32
  getTunnelToken(accountId: string, tunnelId: string): Promise<string>;
48
33
  putIngress(accountId: string, tunnelId: string, ingress: unknown[]): Promise<void>;
49
34
  cleanupConnections(accountId: string, tunnelId: string): Promise<void>;
50
35
  deleteTunnel(accountId: string, tunnelId: string): Promise<void>;
51
- listDnsRecords(zoneId: string, query?: {
52
- name?: string;
53
- comment?: string;
54
- }): Promise<CloudflareDnsRecord[]>;
55
- createDnsRecord(zoneId: string, spec: DnsRecordSpec): Promise<CloudflareDnsRecord>;
56
- deleteDnsRecord(zoneId: string, recordId: string): Promise<void>;
57
- }
58
-
59
- interface CloudflareDnsOptions {
60
- apiToken: string;
61
- zoneId?: string;
62
- apiBase?: string;
63
- fetcher?: typeof fetch;
64
36
  }
65
- declare function createCloudflareDns(options: CloudflareDnsOptions): DnsAdapter;
66
37
 
67
38
  interface CloudflareTunnelOptions {
68
39
  cloudflaredPath?: string;
@@ -78,9 +49,8 @@ interface CloudflareTunnelOptions {
78
49
  declare function createCloudflareTunnel(options?: CloudflareTunnelOptions): TunnelAdapter;
79
50
  declare const cloudflareTunnel: TunnelAdapter;
80
51
 
81
- declare const CLOUDFLARE_DNS_ADAPTER_ID = "cloudflare";
82
52
  declare const CLOUDFLARE_TUNNEL_ADAPTER_ID = "cloudflare";
83
53
  declare const CLOUDFLARE_API_BASE = "https://api.cloudflare.com/client/v4";
84
54
 
85
- export { CLOUDFLARE_API_BASE, CLOUDFLARE_DNS_ADAPTER_ID, CLOUDFLARE_TUNNEL_ADAPTER_ID, CloudflareApi, cloudflareTunnel, createCloudflareDns, createCloudflareTunnel };
86
- export type { CloudflareAccount, CloudflareDnsOptions, CloudflareDnsRecord, CloudflareTunnel, CloudflareTunnelOptions, CloudflareZone };
55
+ export { CLOUDFLARE_API_BASE, CLOUDFLARE_TUNNEL_ADAPTER_ID, CloudflareApi, cloudflareTunnel, createCloudflareTunnel };
56
+ export type { CloudflareAccount, CloudflareTunnel, CloudflareTunnelOptions };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { DnsRecordType, DnsRecordSpec, DnsAdapter, TunnelAdapter } from '@flash-tunnel/types';
1
+ import { TunnelAdapter } from '@flash-tunnel/types';
2
2
 
3
3
  interface CloudflareApiOptions {
4
4
  apiToken: string;
@@ -9,11 +9,6 @@ interface CloudflareAccount {
9
9
  id: string;
10
10
  name: string;
11
11
  }
12
- interface CloudflareZone {
13
- id: string;
14
- name: string;
15
- status: string;
16
- }
17
12
  interface CloudflareTunnel {
18
13
  id: string;
19
14
  name: string;
@@ -23,14 +18,6 @@ interface CloudflareTunnel {
23
18
  }>;
24
19
  token?: string;
25
20
  }
26
- interface CloudflareDnsRecord {
27
- id: string;
28
- name: string;
29
- type: DnsRecordType;
30
- content: string;
31
- proxied: boolean;
32
- comment?: string;
33
- }
34
21
  declare class CloudflareApi {
35
22
  private readonly apiToken;
36
23
  private readonly apiBase;
@@ -40,29 +27,13 @@ declare class CloudflareApi {
40
27
  verifyToken(): Promise<void>;
41
28
  listAccounts(): Promise<CloudflareAccount[]>;
42
29
  resolveAccountId(preferred?: string): Promise<string>;
43
- listZones(name?: string): Promise<CloudflareZone[]>;
44
- resolveZone(zone: string): Promise<CloudflareZone>;
45
30
  createTunnel(accountId: string, name: string): Promise<CloudflareTunnel>;
46
31
  getTunnel(accountId: string, tunnelId: string): Promise<CloudflareTunnel>;
47
32
  getTunnelToken(accountId: string, tunnelId: string): Promise<string>;
48
33
  putIngress(accountId: string, tunnelId: string, ingress: unknown[]): Promise<void>;
49
34
  cleanupConnections(accountId: string, tunnelId: string): Promise<void>;
50
35
  deleteTunnel(accountId: string, tunnelId: string): Promise<void>;
51
- listDnsRecords(zoneId: string, query?: {
52
- name?: string;
53
- comment?: string;
54
- }): Promise<CloudflareDnsRecord[]>;
55
- createDnsRecord(zoneId: string, spec: DnsRecordSpec): Promise<CloudflareDnsRecord>;
56
- deleteDnsRecord(zoneId: string, recordId: string): Promise<void>;
57
- }
58
-
59
- interface CloudflareDnsOptions {
60
- apiToken: string;
61
- zoneId?: string;
62
- apiBase?: string;
63
- fetcher?: typeof fetch;
64
36
  }
65
- declare function createCloudflareDns(options: CloudflareDnsOptions): DnsAdapter;
66
37
 
67
38
  interface CloudflareTunnelOptions {
68
39
  cloudflaredPath?: string;
@@ -78,9 +49,8 @@ interface CloudflareTunnelOptions {
78
49
  declare function createCloudflareTunnel(options?: CloudflareTunnelOptions): TunnelAdapter;
79
50
  declare const cloudflareTunnel: TunnelAdapter;
80
51
 
81
- declare const CLOUDFLARE_DNS_ADAPTER_ID = "cloudflare";
82
52
  declare const CLOUDFLARE_TUNNEL_ADAPTER_ID = "cloudflare";
83
53
  declare const CLOUDFLARE_API_BASE = "https://api.cloudflare.com/client/v4";
84
54
 
85
- export { CLOUDFLARE_API_BASE, CLOUDFLARE_DNS_ADAPTER_ID, CLOUDFLARE_TUNNEL_ADAPTER_ID, CloudflareApi, cloudflareTunnel, createCloudflareDns, createCloudflareTunnel };
86
- export type { CloudflareAccount, CloudflareDnsOptions, CloudflareDnsRecord, CloudflareTunnel, CloudflareTunnelOptions, CloudflareZone };
55
+ export { CLOUDFLARE_API_BASE, CLOUDFLARE_TUNNEL_ADAPTER_ID, CloudflareApi, cloudflareTunnel, createCloudflareTunnel };
56
+ export type { CloudflareAccount, CloudflareTunnel, CloudflareTunnelOptions };
package/dist/index.mjs CHANGED
@@ -72,7 +72,7 @@ class CloudflareApi {
72
72
  if (result?.status !== "active") {
73
73
  throw new AdapterError("Cloudflare API token is not active", {
74
74
  code: "AUTH_INVALID",
75
- hint: "Create a token with Account Cloudflare Tunnel Edit and Zone DNS Edit"
75
+ hint: "Create a token with Account Cloudflare Tunnel Edit"
76
76
  });
77
77
  }
78
78
  }
@@ -90,33 +90,11 @@ class CloudflareApi {
90
90
  throw new AdapterError("Cannot determine the Cloudflare account id", {
91
91
  code: "ACCOUNT_NOT_FOUND",
92
92
  hint: "Set CLOUDFLARE_ACCOUNT_ID or providers.cloudflare.accountId",
93
- details: { accounts: accounts.map((account) => account.name) }
93
+ details: {
94
+ accounts: accounts.map((account) => account.name)
95
+ }
94
96
  });
95
97
  }
96
- async listZones(name) {
97
- const query = new URLSearchParams({ per_page: "50" });
98
- if (name) {
99
- query.set("name", name);
100
- }
101
- return this.request("GET", `/zones?${query.toString()}`);
102
- }
103
- async resolveZone(zone) {
104
- const zones = await this.listZones(zone);
105
- const match = zones.find((item) => item.name === zone);
106
- if (!match) {
107
- throw new AdapterError(`Zone "${zone}" was not found`, {
108
- code: "ZONE_NOT_FOUND",
109
- hint: "Check domain.zone, or set CLOUDFLARE_ZONE_ID"
110
- });
111
- }
112
- if (match.status !== "active") {
113
- throw new AdapterError(`Zone "${zone}" is ${match.status}`, {
114
- code: "ZONE_NOT_ACTIVE",
115
- hint: "Wait for the zone to become active in Cloudflare"
116
- });
117
- }
118
- return match;
119
- }
120
98
  async createTunnel(accountId, name) {
121
99
  return this.request("POST", `/accounts/${accountId}/cfd_tunnel`, {
122
100
  name,
@@ -152,132 +130,6 @@ class CloudflareApi {
152
130
  async deleteTunnel(accountId, tunnelId) {
153
131
  await this.request("DELETE", `/accounts/${accountId}/cfd_tunnel/${tunnelId}`);
154
132
  }
155
- async listDnsRecords(zoneId, query = {}) {
156
- const params = new URLSearchParams({ per_page: "100" });
157
- if (query.name) {
158
- params.set("name", query.name);
159
- }
160
- if (query.comment) {
161
- params.set("comment", query.comment);
162
- }
163
- return this.request("GET", `/zones/${zoneId}/dns_records?${params.toString()}`);
164
- }
165
- async createDnsRecord(zoneId, spec) {
166
- return this.request("POST", `/zones/${zoneId}/dns_records`, {
167
- type: spec.type,
168
- name: spec.name,
169
- content: spec.content,
170
- proxied: spec.proxied ?? false,
171
- ttl: spec.ttl ?? 1,
172
- comment: spec.comment
173
- });
174
- }
175
- async deleteDnsRecord(zoneId, recordId) {
176
- await this.request("DELETE", `/zones/${zoneId}/dns_records/${recordId}`);
177
- }
178
- }
179
-
180
- const OWNED_COMMENT_PREFIX = "flash-tunnel:";
181
- function isOwned(comment) {
182
- return comment?.startsWith(OWNED_COMMENT_PREFIX) ?? false;
183
- }
184
- function toRef(provider, zoneId, record) {
185
- return {
186
- provider,
187
- zoneId,
188
- recordId: record.id,
189
- name: record.name,
190
- type: record.type,
191
- content: record.content,
192
- proxied: record.proxied
193
- };
194
- }
195
- function createCloudflareDns(options) {
196
- const api = new CloudflareApi(options);
197
- const zoneIds = /* @__PURE__ */ new Map();
198
- async function zoneIdFor(zone) {
199
- if (options.zoneId) {
200
- return options.zoneId;
201
- }
202
- const cached = zoneIds.get(zone);
203
- if (cached) {
204
- return cached;
205
- }
206
- const resolved = await api.resolveZone(zone);
207
- zoneIds.set(zone, resolved.id);
208
- return resolved.id;
209
- }
210
- return {
211
- kind: "dns",
212
- id: "cloudflare",
213
- apiVersion: 1,
214
- capabilities: {
215
- recordTypes: ["CNAME", "A", "AAAA", "TXT"],
216
- proxied: true,
217
- ttl: true,
218
- comment: true,
219
- lookupByName: true
220
- },
221
- async preflight(_ctx, target) {
222
- await api.verifyToken();
223
- const zoneId = await zoneIdFor(target.zone);
224
- const existing = await api.listDnsRecords(zoneId, { name: target.name });
225
- const conflicts = existing.filter((record) => !isOwned(record.comment));
226
- if (conflicts.length > 0 && !target.overwrite) {
227
- throw new AdapterError(`DNS record "${target.name}" already exists`, {
228
- code: "RECORD_CONFLICT",
229
- hint: "Use a different subdomain, or pass --overwrite to replace a non-flash-tunnel record",
230
- details: { records: conflicts.map((record) => record.id) }
231
- });
232
- }
233
- },
234
- async ensure(_ctx, spec) {
235
- const zoneId = await zoneIdFor(spec.zone);
236
- const existing = await api.listDnsRecords(zoneId, { name: spec.name });
237
- for (const record of existing) {
238
- if (spec.overwrite || isOwned(record.comment)) {
239
- await api.deleteDnsRecord(zoneId, record.id).catch(() => {
240
- });
241
- }
242
- }
243
- try {
244
- const record = await api.createDnsRecord(zoneId, spec);
245
- return toRef("cloudflare", zoneId, record);
246
- } catch (error) {
247
- if (error instanceof AdapterError && error.code === "RECORD_CONFLICT") {
248
- const current = await api.listDnsRecords(zoneId, { name: spec.name });
249
- const owned = current.filter((record2) => isOwned(record2.comment));
250
- if (owned.length === 0) {
251
- throw error;
252
- }
253
- for (const record2 of owned) {
254
- await api.deleteDnsRecord(zoneId, record2.id);
255
- }
256
- const record = await api.createDnsRecord(zoneId, spec);
257
- return toRef("cloudflare", zoneId, record);
258
- }
259
- throw error;
260
- }
261
- },
262
- async remove(_ctx, ref) {
263
- try {
264
- await api.deleteDnsRecord(ref.zoneId, ref.recordId);
265
- } catch (error) {
266
- if (error instanceof AdapterError && error.code === "NOT_FOUND") {
267
- return;
268
- }
269
- throw error;
270
- }
271
- },
272
- async find(_ctx, query) {
273
- const zoneId = await zoneIdFor(query.zone);
274
- const records = await api.listDnsRecords(zoneId, {
275
- name: query.name,
276
- comment: query.comment
277
- });
278
- return records.map((record) => toRef("cloudflare", zoneId, record));
279
- }
280
- };
281
133
  }
282
134
 
283
135
  const ADAPTER_ID = "cloudflare";
@@ -384,13 +236,6 @@ function createCloudflareTunnel(options = {}) {
384
236
  capabilities: {
385
237
  modes: ["named", "quick"],
386
238
  publicHostname: "both",
387
- requiresDns: false,
388
- dns: {
389
- type: "CNAME",
390
- targetFrom: "handle",
391
- proxied: true,
392
- providerAffinity: ["cloudflare"]
393
- },
394
239
  concurrent: true
395
240
  },
396
241
  async preflight(ctx, { mode }) {
@@ -423,7 +268,7 @@ function createCloudflareTunnel(options = {}) {
423
268
  if (spec.hostname === void 0) {
424
269
  throw new AdapterError("Named tunnel requires a hostname", {
425
270
  code: "TUNNEL_CREATE_FAILED",
426
- hint: "Set domain.zone (and optionally domain.subdomain) in flash-tunnel.ts"
271
+ hint: "Set tunnel.hostname in flash-nexus.config.ts or pass --hostname"
427
272
  });
428
273
  }
429
274
  const client = requireApi();
@@ -450,7 +295,7 @@ function createCloudflareTunnel(options = {}) {
450
295
  tunnelId: tunnel.id,
451
296
  name: spec.name,
452
297
  publicUrl: `https://${spec.hostname}`,
453
- dns: {
298
+ target: {
454
299
  type: "CNAME",
455
300
  target: `${tunnel.id}.cfargotunnel.com`,
456
301
  proxied: true
@@ -479,7 +324,12 @@ function createCloudflareTunnel(options = {}) {
479
324
  code: "PROCESS_SPAWN_FAILED"
480
325
  });
481
326
  }
482
- const args = named ? ["--no-autoupdate", "tunnel", "run"] : ["--no-autoupdate", "tunnel", "--url", localUrl];
327
+ const args = named ? ["--no-autoupdate", "tunnel", "run"] : [
328
+ "--no-autoupdate",
329
+ "tunnel",
330
+ "--url",
331
+ localUrl
332
+ ];
483
333
  const invocation = shellInvocation(cloudflaredPath, args);
484
334
  const child = spawn(invocation.command, invocation.args, {
485
335
  shell: invocation.shell,
@@ -490,7 +340,10 @@ function createCloudflareTunnel(options = {}) {
490
340
  });
491
341
  childProcess = child;
492
342
  if (child.pid) {
493
- handle.meta = { ...handle.meta, processPid: String(child.pid) };
343
+ handle.meta = {
344
+ ...handle.meta,
345
+ processPid: String(child.pid)
346
+ };
494
347
  }
495
348
  child.stderr?.on("data", (chunk) => capture(chunk, hooks));
496
349
  child.stdout?.on("data", (chunk) => capture(chunk, hooks));
@@ -555,7 +408,12 @@ ${output.slice(-10).join("\n")}` : ""}`,
555
408
  return;
556
409
  }
557
410
  if (process.platform === "win32") {
558
- spawnSync("taskkill", ["/PID", String(child.pid), "/T", "/F"], {
411
+ spawnSync("taskkill", [
412
+ "/PID",
413
+ String(child.pid),
414
+ "/T",
415
+ "/F"
416
+ ], {
559
417
  stdio: "ignore",
560
418
  windowsHide: true
561
419
  });
@@ -623,8 +481,7 @@ ${output.slice(-10).join("\n")}` : ""}`,
623
481
  }
624
482
  const cloudflareTunnel = createCloudflareTunnel();
625
483
 
626
- const CLOUDFLARE_DNS_ADAPTER_ID = "cloudflare";
627
484
  const CLOUDFLARE_TUNNEL_ADAPTER_ID = "cloudflare";
628
485
  const CLOUDFLARE_API_BASE = "https://api.cloudflare.com/client/v4";
629
486
 
630
- export { CLOUDFLARE_API_BASE, CLOUDFLARE_DNS_ADAPTER_ID, CLOUDFLARE_TUNNEL_ADAPTER_ID, CloudflareApi, cloudflareTunnel, createCloudflareDns, createCloudflareTunnel };
487
+ export { CLOUDFLARE_API_BASE, CLOUDFLARE_TUNNEL_ADAPTER_ID, CloudflareApi, cloudflareTunnel, createCloudflareTunnel };
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@flash-tunnel/adapter-cloudflare",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.4.0",
5
5
  "license": "MIT",
6
- "homepage": "https://github.com/flash-tunnel/flash-tunnel#readme",
6
+ "homepage": "https://github.com/flash-nexus/flash-tunnel#readme",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/flash-tunnel/flash-tunnel.git"
9
+ "url": "git+https://github.com/flash-nexus/flash-tunnel.git"
10
10
  },
11
11
  "bugs": {
12
- "url": "https://github.com/flash-tunnel/flash-tunnel/issues"
12
+ "url": "https://github.com/flash-nexus/flash-tunnel/issues"
13
13
  },
14
14
  "exports": {
15
15
  ".": {
@@ -30,8 +30,8 @@
30
30
  "access": "public"
31
31
  },
32
32
  "dependencies": {
33
- "@flash-tunnel/types": "0.2.0",
34
- "@flash-tunnel/utils": "0.2.0"
33
+ "@flash-tunnel/types": "0.4.0",
34
+ "@flash-tunnel/utils": "0.4.0"
35
35
  },
36
36
  "scripts": {
37
37
  "stub": "unbuild --stub",