@theholocron/holocron-plugin-cloudflare 5.0.0-alpha.6 → 5.0.0-alpha.600

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/README.md CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  # `@theholocron/holocron-plugin-cloudflare`
4
4
 
5
- Cloudflare plugin for [Holocron](../cli). Implements the `dns` and `deployment`
6
- capabilities against the [Cloudflare v4 API](https://developers.cloudflare.com/api/).
5
+ Cloudflare plugin for [Holocron](../cli). Implements the `dns`, `deployment`,
6
+ `workers` and `tunnels` capabilities against the
7
+ [Cloudflare v4 API](https://developers.cloudflare.com/api/).
7
8
 
8
9
  ## Install
9
10
 
@@ -21,7 +22,7 @@ Token resolution order:
21
22
  2. `HOLOCRON_CLOUDFLARE_TOKEN` env var
22
23
  3. `CLOUDFLARE_API_TOKEN` env var (the standard Cloudflare variable name)
23
24
  4. Keyring `cloudflare.<org>` — tried first when an org is active via `--org`, `HOLOCRON_ORG`, or `org` in `holocron.config.ts`
24
- 5. Keyring `cloudflare` — unnamespaced fallback; set via `holocron auth set cloudflare <token>`
25
+ 5. Keyring `cloudflare` — unnamespaced fallback; set via `holocron new token cloudflare <token>`
25
26
 
26
27
  Generate a scoped API token at **dash.cloudflare.com/profile/api-tokens**.
27
28
 
@@ -40,12 +41,15 @@ Generate a scoped API token at **dash.cloudflare.com/profile/api-tokens**.
40
41
  // Cloudflare Pages deployments (requires accountId)
41
42
  // accountId falls back to CLOUDFLARE_ACCOUNT_ID env var when omitted
42
43
  "deployment": "cloudflare",
44
+
45
+ // Workers deployment (also requires accountId)
46
+ "workers": "cloudflare",
43
47
  },
44
48
  }
45
49
 
46
50
  ```
47
51
 
48
- Both capabilities can be enabled together:
52
+ Any combination of capabilities can be enabled together:
49
53
 
50
54
  <!-- prettier-ignore -->
51
55
  ```jsonc
@@ -53,12 +57,16 @@ Both capabilities can be enabled together:
53
57
  "providers": {
54
58
  "dns": "cloudflare",
55
59
  "deployment": "cloudflare",
60
+ "workers": "cloudflare",
56
61
  },
57
62
  }
58
63
 
59
64
  ```
60
65
 
61
- The `deployment` capability is only exposed when `accountId` is resolvable — either passed explicitly in options or set via the `CLOUDFLARE_ACCOUNT_ID` env var.
66
+ The `deployment`, `workers` and `tunnels` capabilities are only exposed when `accountId`
67
+ is resolvable — either passed explicitly in options or set as the
68
+ `HOLOCRON_CLOUDFLARE_ACCOUNT_ID` (or `CLOUDFLARE_ACCOUNT_ID`) env var or in a `credentials` provider's Environment (the option wins, then the
69
+ environment, then the provider).
62
70
 
63
71
  ## `dns` capability
64
72
 
@@ -83,6 +91,35 @@ Manages [Cloudflare Pages](https://developers.cloudflare.com/pages/) projects. U
83
91
  | `triggerDeployment` | Triggers a new Pages deployment from the latest production branch commit. |
84
92
  | `updateProjectSettings` | Updates project-level settings (currently a no-op; CF Pages REST API has no direct settings endpoint for these fields). |
85
93
 
94
+ ## `workers` capability
95
+
96
+ Deploys and manages [Workers](https://developers.cloudflare.com/workers/) scripts.
97
+
98
+ | Method | What it does |
99
+ | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
100
+ | `upsertProxy` | Deploys a generated reverse-proxy Worker forwarding `hostname/*` to `config.target`, injecting `config.headers`. Used by `setup` to wire wiki custom-domain proxies when the wiki provider requires one in addition to the CNAME. |
101
+ | `deployScript` | Deploys (or updates) an arbitrary Worker script — the caller's own code, not a generated proxy — with optional secret bindings and route patterns. For a consumer that isn't a reverse-proxy, e.g. a webhook receiver. |
102
+
103
+ Both methods are idempotent: re-running with the same inputs makes no
104
+ route-write call when nothing changed. Zone ids are cached per plugin
105
+ instance for the lifetime of the process — the `workers` capability's own
106
+ cache, separate from `dns`'s.
107
+
108
+ ## `tunnels` capability
109
+
110
+ Named [Cloudflare Tunnels](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/): a stable
111
+ public hostname that reaches a port on this machine, for a webhook that has to find a development instance. It needs a
112
+ token with **Cloudflare Tunnel: Edit** (account).
113
+
114
+ | Method | What it does |
115
+ | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
116
+ | `ensureTunnel` | Finds the tunnel by name or makes it, and makes its configuration serve `hostname` at `service`. Other hostnames' rules are kept and the catch-all (`http_status:404`) stays last. Returns the tunnel id and the `CNAME` target (`<id>.cfargotunnel.com`). |
117
+ | `tunnelToken` | The token `cloudflared` runs the tunnel with. A credential: never printed or stored. |
118
+ | `whoami` | How many tunnels the token can see, for `holocron doctor`. |
119
+
120
+ `ensureTunnel` is idempotent: a tunnel that already serves the hostname is not rewritten. The DNS record for the
121
+ hostname is the `dns` provider's job.
122
+
86
123
  ### Preview deployment setup
87
124
 
88
125
  When `preview: true` (or `preview: { project, domain }`) is set in a repo's deploy config, `holocron setup` calls:
package/dist/index.d.mts CHANGED
@@ -1,15 +1,19 @@
1
- import { AuthError, Deployment, DeploymentProject, DeploymentProjectSettings, DeploymentRecord, DeploymentTarget, DeploymentTrigger, Dns, DnsRecord, ResolveTokenInput, WikiProxyConfig, Workers } from "@theholocron/cli";
2
- import { CloudflareClient, CloudflareClientOptions, CloudflareClientOptions as CloudflareClientOptions$1, createCloudflareClient } from "@theholocron/cloudflare-client";
3
- //#region src/auth.d.ts
4
- declare const resolveToken: (input?: ResolveTokenInput) => string;
5
- //#endregion
6
- //#region src/capabilities/deployment.d.ts
7
- declare class CloudflareDeployment implements Deployment {
1
+ import { AuthError, DeployScriptConfig, DeployScriptResult, Deployment, DeploymentProject, DeploymentProjectSettings, DeploymentRecord, DeploymentTarget, DeploymentTrigger, Dns, DnsRecord, DnsRecordRequest, EnsureRelaySourceInput, EnsureRelaySourceResult, EnsuredTunnel, InspectRelaySourceInput, MintTokenInput, PluginToken, RelaySourceState, ResolveTokenInput, RevokeTokenInput, TunnelRoute, Tunnels, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess, WikiProxyConfig, WorkerDeploymentState, WorkerScriptState, Workers } from "@theholocron/cli";
2
+ import { CloudflareClient, CloudflareClient as CloudflareClient$1, CloudflareClientOptions, createCloudflareClient } from "@theholocron/cloudflare-client";
3
+ /** Resolves the Cloudflare API token from `--token`, `HOLOCRON_CLOUDFLARE_TOKEN`, `CLOUDFLARE_API_TOKEN` or the keyring, and throws an `AuthError` when none is set. */
4
+ export declare const resolveToken: import("@theholocron/cli").TokenResolver;
5
+ /** Every token this plugin takes, for `holocron doctor secrets --verify`, with what each layer's token must and must not be able to do. */
6
+ export declare const tokens: PluginToken[];
7
+ /** The Cloudflare implementation of the `deployment` capability, backed by Cloudflare Pages projects in one account. */
8
+ export declare class CloudflareDeployment implements Deployment {
8
9
  private readonly client;
9
10
  private readonly accountId;
11
+ /** The capability this class provides, always `"deployment"`. */
10
12
  readonly key: "deployment";
13
+ /** The provider name, `"cloudflare"`. */
11
14
  readonly providerName = "cloudflare";
12
15
  constructor(client: () => CloudflareClient, accountId: string);
16
+ /** Lists the account's Pages projects. */
13
17
  listProjects(): Promise<DeploymentProject[]>;
14
18
  ensureProject(input: {
15
19
  name: string;
@@ -17,31 +21,54 @@ declare class CloudflareDeployment implements Deployment {
17
21
  repo?: string;
18
22
  rootDirectory?: string;
19
23
  }): Promise<DeploymentProject>;
24
+ /** Returns the project as it is now; Pages has no REST setting for the requested options, so `settings` is ignored. */
20
25
  updateProjectSettings(projectId: string, settings: DeploymentProjectSettings): Promise<DeploymentProject>;
26
+ /** Lists the names of the environment variables set for the production or preview target. */
21
27
  listEnvVars(projectId: string, target: DeploymentTarget): Promise<string[]>;
28
+ /** Sets a plain-text environment variable on the production or preview target, keeping the others. */
22
29
  setEnvVar(projectId: string, target: DeploymentTarget, name: string, value: string): Promise<void>;
23
30
  triggerDeployment(input: {
24
31
  projectId: string;
25
32
  branch: string;
26
33
  target?: DeploymentTrigger;
27
34
  }): Promise<DeploymentRecord>;
35
+ /** Fetches a deployment by an id of the form `projectName:deploymentId`; throws a 400 `ProviderApiError` for any other form. */
28
36
  getDeployment(deploymentId: string): Promise<DeploymentRecord>;
29
37
  listPreviewDeployments(projectId: string, branch: string): Promise<DeploymentRecord[]>;
30
38
  deletePreviewDeployments(projectId: string, deploymentIds: string[]): Promise<number>;
31
- ensureCustomDomain(projectId: string, hostname: string): Promise<void>;
39
+ ensureCustomDomain(projectId: string, hostname: string): Promise<DnsRecordRequest | null>;
40
+ /** The hostnames attached to the Pages project. Reads only. */
41
+ listDomains(projectId: string): Promise<string[]>;
42
+ /** The newest production deployment of the project, or null. Reads only. */
43
+ latestDeployment(projectId: string): Promise<DeploymentRecord | null>;
32
44
  private getProjectByName;
33
45
  }
34
- //#endregion
35
- //#region src/capabilities/dns.d.ts
36
- declare class CloudflareDns implements Dns {
46
+ /** The Cloudflare implementation of the `dns` capability; finds a domain's zone by walking from the domain up to its apex. */
47
+ export declare class CloudflareDns implements Dns {
37
48
  private readonly client;
49
+ private readonly accountId?;
50
+ /** The capability this class provides, always `"dns"`. */
38
51
  readonly key: "dns";
52
+ /** The provider name, `"cloudflare"`. */
39
53
  readonly providerName = "cloudflare";
40
54
  private readonly zoneCache;
41
- constructor(client: () => CloudflareClient);
55
+ constructor(client: () => CloudflareClient, accountId?: string | undefined);
56
+ /** Makes the Cloudflare notification webhook and the policies that send to it for a relay source; needs the account id. */
57
+ ensureRelaySource(input: EnsureRelaySourceInput): Promise<EnsureRelaySourceResult>;
58
+ /** Reads the relay source's notification webhook and its policies back, changing nothing. */
59
+ inspectRelaySource(input: InspectRelaySourceInput): Promise<RelaySourceState>;
60
+ private account;
61
+ /** Lists the DNS records in the zone that holds the domain. */
42
62
  listRecords(domain: string): Promise<DnsRecord[]>;
63
+ /** Updates the first record of the same type and name, or creates the record when there is none; returns the stored record. */
43
64
  upsertRecord(domain: string, record: DnsRecord): Promise<DnsRecord>;
65
+ /** Deletes the DNS record with this id from the zone that holds the domain. */
44
66
  deleteRecord(domain: string, id: string): Promise<void>;
67
+ /** `holocron doctor` has no specific domain to check — lists every zone the token can see instead. */
68
+ whoami(): Promise<{
69
+ ok: boolean;
70
+ zones: number;
71
+ }>;
45
72
  /**
46
73
  * Walk from full domain up to apex to find the Cloudflare zone.
47
74
  * E.g. "api.staging.example.com" tries:
@@ -51,84 +78,116 @@ declare class CloudflareDns implements Dns {
51
78
  */
52
79
  private resolveZone;
53
80
  }
54
- //#endregion
55
- //#region src/capabilities/workers.d.ts
56
- interface ZonesClient {
57
- list(query?: {
58
- name?: string;
59
- per_page?: number;
60
- }): Promise<Array<{
61
- id: string;
62
- }>>;
81
+ /** The Cloudflare implementation of the `tunnels` capability, backed by Cloudflare Tunnel in one account. */
82
+ export declare class CloudflareTunnels implements Tunnels {
83
+ private readonly client;
84
+ private readonly accountId;
85
+ /** The capability this class provides, always `"tunnels"`. */
86
+ readonly key: "tunnels";
87
+ /** The provider name, `"cloudflare"`. */
88
+ readonly providerName = "cloudflare";
89
+ constructor(client: () => Pick<CloudflareClient$1, "tunnels">, accountId: string);
90
+ /** Finds the tunnel by name or makes it, then rewrites its routes only when they do not already serve the hostname. */
91
+ ensureTunnel(route: TunnelRoute): Promise<EnsuredTunnel>;
92
+ /** The token `cloudflared` runs the tunnel with. */
93
+ tunnelToken(tunnelId: string): Promise<string>;
94
+ /** `holocron doctor` has no specific tunnel to check: lists the ones the token can see. */
95
+ whoami(): Promise<{
96
+ ok: boolean;
97
+ tunnels: number;
98
+ }>;
63
99
  }
64
- declare class CloudflareWorkers implements Workers {
65
- private readonly zones;
100
+ /** The Cloudflare implementation of the `workers` capability, backed by Workers scripts and routes in one account. */
101
+ export declare class CloudflareWorkers implements Workers {
102
+ private readonly client;
66
103
  private readonly accountId;
104
+ /** The capability this class provides, always `"workers"`. */
67
105
  readonly key: "workers";
106
+ /** The provider name, `"cloudflare"`. */
68
107
  readonly providerName = "cloudflare";
69
108
  private readonly zoneCache;
70
- private readonly baseUrl;
71
- private readonly fetchImpl;
72
- /** Memoized token thunk — resolved on the first authenticated request. */
73
- private readonly token;
74
- constructor(zones: () => ZonesClient, accountId: string, opts: {
75
- token: () => string;
76
- } & Pick<CloudflareClientOptions$1, "baseUrl" | "fetch">);
109
+ constructor(client: () => Pick<CloudflareClient$1, "workers" | "zones">, accountId: string);
77
110
  upsertProxy(hostname: string, config: WikiProxyConfig): Promise<void>;
78
- private putScript;
79
- private listRoutes;
80
- private createRoute;
81
- private updateRoute;
82
- private cfRequest;
111
+ /** `holocron doctor` has no specific script to check — lists every script the token can see instead. */
112
+ whoami(): Promise<{
113
+ ok: boolean;
114
+ scripts: number;
115
+ }>;
116
+ deployScript(name: string, config: DeployScriptConfig): Promise<DeployScriptResult>;
117
+ setSecret(script: string, name: string, value: string): Promise<void>;
118
+ secretNames(script: string): Promise<string[]>;
119
+ /**
120
+ * Whether the script exists, which routes in the zone of `host` point at it, and the names of its secrets. Reads
121
+ * only, and asks Cloudflare for names, so no value is ever fetched.
122
+ */
123
+ inspectScript(name: string, host: string): Promise<WorkerScriptState>;
124
+ /** The newest deployment of the script, and whether it serves all traffic. Reads only. */
125
+ latestDeployment(script: string): Promise<WorkerDeploymentState | null>;
126
+ proxyScriptName(hostname: string): string;
127
+ /** Idempotent create-or-update for one route pattern — shared by both public methods. */
128
+ private upsertRoute;
83
129
  private resolveZone;
84
130
  }
85
- //#endregion
86
- //#region src/verify-token.d.ts
87
- interface VerifyTokenSuccess {
88
- ok: true;
89
- subject: string;
90
- }
91
- interface VerifyTokenFailure {
92
- ok: false;
93
- message: string;
94
- }
95
- type VerifyTokenResult = VerifyTokenSuccess | VerifyTokenFailure;
96
131
  interface VerifyTokenOptions {
97
132
  baseUrl?: string;
98
133
  fetch?: typeof fetch;
99
134
  }
100
- declare function verifyToken(token: string, opts?: VerifyTokenOptions): Promise<VerifyTokenResult>;
101
- //#endregion
102
- //#region src/index.d.ts
103
- interface CloudflarePluginOptions extends ResolveTokenInput {
135
+ /** Checks a Cloudflare API token, first by the token-verify endpoint and otherwise by listing zones; resolves to a description of what it found, or the failure message. */
136
+ export declare function verifyToken(token: string, opts?: VerifyTokenOptions): Promise<VerifyTokenResult$1>;
137
+ /** Options for the Cloudflare plugin: how the API token is resolved, the account, and test overrides. */
138
+ export interface CloudflarePluginOptions extends ResolveTokenInput {
104
139
  /**
105
140
  * Cloudflare account ID. Required for Pages (deployment) operations;
106
141
  * optional for DNS-only use.
107
142
  */
108
143
  accountId?: string;
144
+ /**
145
+ * The zone the organization's own DNS lives in. The `token` probe reads this one zone when no zone is named on the
146
+ * command line; without it the probe reads the first few zones of the account.
147
+ */
148
+ zone?: string;
109
149
  /** Override base URL for tests. */
110
150
  baseUrl?: string;
151
+ /** The `fetch` to use instead of the global one, for tests. */
111
152
  fetch?: typeof fetch;
112
153
  }
113
- interface PluginContext {
154
+ /** The shared state the Cloudflare plugin's capabilities and probes are built from. */
155
+ export interface PluginContext {
156
+ /** The options the plugin was created with, with `accountId` falling back to `CLOUDFLARE_ACCOUNT_ID` in the environment, then in the `credentials` providers' variables. */
114
157
  options: CloudflarePluginOptions;
115
158
  /** Memoized client — the token is resolved on first use, not at plugin load. */
116
159
  client: () => CloudflareClient;
117
160
  /** Memoized token resolver — shared by the client and the Workers capability. */
118
161
  token: () => string;
119
162
  }
120
- declare function createContext(options?: CloudflarePluginOptions): PluginContext;
121
- declare function dns(ctx: PluginContext): Dns;
122
- declare function deployment(ctx: PluginContext): Deployment;
123
- declare function workersCapability(ctx: PluginContext): Workers;
124
- declare function createPlugin(options?: CloudflarePluginOptions): {
163
+ /** Builds the plugin context from its options; the API token is resolved lazily on the first client use. */
164
+ export declare function createContext(options?: CloudflarePluginOptions): PluginContext;
165
+ /** Builds the `dns` capability for a context. */
166
+ export declare function dns(ctx: PluginContext): Dns;
167
+ /** Builds the `deployment` capability for a context; throws when no account id is set. */
168
+ export declare function deployment(ctx: PluginContext): Deployment;
169
+ /** Builds the `workers` capability for a context; throws when no account id is set. */
170
+ export declare function workersCapability(ctx: PluginContext): Workers;
171
+ /** Builds the `tunnels` capability for a context; throws when no account id is set. */
172
+ export declare function tunnelsCapability(ctx: PluginContext): Tunnels;
173
+ /** Creates the Cloudflare plugin: `dns` always, plus `deployment`, `workers` and `tunnels` when an account id is set, and the `connection` and `token` probes. */
174
+ export declare function createPlugin(options?: CloudflarePluginOptions): {
125
175
  name: string;
176
+ probes: {
177
+ token: import("@theholocron/cli").Probe;
178
+ connection: import("@theholocron/cli").Probe;
179
+ };
180
+ whoami: import("@theholocron/cli").ProviderAction;
181
+ mintToken: (input: MintTokenInput) => Promise<import("@theholocron/cli").MintTokenResult>;
182
+ revokeToken: (input: RevokeTokenInput) => Promise<import("@theholocron/cli").RevokeTokenResult>;
183
+ readTokenId: () => Promise<string | undefined>;
126
184
  capabilities: {
127
185
  deployment?: (() => Deployment) | undefined;
128
186
  workers?: (() => Workers) | undefined;
187
+ tunnels?: (() => Tunnels) | undefined;
129
188
  dns: () => Dns;
130
189
  };
131
190
  };
132
- declare const AUTH_HINT: string;
133
- //#endregion
134
- export { AUTH_HINT, AuthError, type CloudflareClient, type CloudflareClientOptions, CloudflareDeployment, CloudflareDns, CloudflarePluginOptions, CloudflareWorkers, PluginContext, type ResolveTokenInput, type VerifyTokenFailure, type VerifyTokenResult, type VerifyTokenSuccess, createCloudflareClient, createContext, createPlugin, deployment, dns, resolveToken, verifyToken, workersCapability };
191
+ /** One-line hint printed by `holocron new token cloudflare` when no token is supplied or the supplied one is rejected. */
192
+ export declare const AUTH_HINT: string;
193
+ export { AuthError, type CloudflareClient, type CloudflareClientOptions, type ResolveTokenInput, type VerifyTokenFailure, type VerifyTokenResult, type VerifyTokenSuccess, createCloudflareClient };