@theholocron/holocron-plugin-cloudflare 5.0.0-alpha.28 → 5.0.0-alpha.281
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 +2 -2
- package/dist/index.d.mts +66 -36
- package/dist/index.mjs +95 -17
- package/package.json +21 -19
package/README.md
CHANGED
|
@@ -79,7 +79,7 @@ Zone ids are cached per plugin instance for the lifetime of the process.
|
|
|
79
79
|
|
|
80
80
|
## `deployment` capability
|
|
81
81
|
|
|
82
|
-
Manages [Cloudflare Pages](https://developers.cloudflare.com/pages/) projects. Used by `holocron setup` to provision per-PR preview deployments.
|
|
82
|
+
Manages [Cloudflare Pages](https://developers.cloudflare.com/pages/) projects. Used by `holocron repo setup` to provision per-PR preview deployments.
|
|
83
83
|
|
|
84
84
|
| Method | What it does |
|
|
85
85
|
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------- |
|
|
@@ -106,7 +106,7 @@ cache, separate from `dns`'s.
|
|
|
106
106
|
|
|
107
107
|
### Preview deployment setup
|
|
108
108
|
|
|
109
|
-
When `preview: true` (or `preview: { project, domain }`) is set in a repo's deploy config, `holocron setup` calls:
|
|
109
|
+
When `preview: true` (or `preview: { project, domain }`) is set in a repo's deploy config, `holocron repo setup` calls:
|
|
110
110
|
|
|
111
111
|
1. `ensureProject` — creates `<org>-preview` if it doesn't exist
|
|
112
112
|
2. `ensureCustomDomain` — attaches `*.<domain>` to the project
|
package/dist/index.d.mts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
import { AuthError, DeployScriptConfig, DeployScriptResult, Deployment, DeploymentProject, DeploymentProjectSettings, DeploymentRecord, DeploymentTarget, DeploymentTrigger, Dns, DnsRecord, DnsRecordRequest, ResolveTokenInput, WikiProxyConfig, Workers } from "@theholocron/cli";
|
|
1
|
+
import { AuthError, DeployScriptConfig, DeployScriptResult, Deployment, DeploymentProject, DeploymentProjectSettings, DeploymentRecord, DeploymentTarget, DeploymentTrigger, Dns, DnsRecord, DnsRecordRequest, PluginToken, ResolveTokenInput, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess, WikiProxyConfig, WorkerScriptState, Workers } from "@theholocron/cli";
|
|
2
2
|
import { CloudflareClient, CloudflareClient as CloudflareClient$1, CloudflareClientOptions, createCloudflareClient } from "@theholocron/cloudflare-client";
|
|
3
|
-
|
|
4
|
-
declare const resolveToken: (
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 tokens`. */
|
|
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,44 @@ 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
39
|
ensureCustomDomain(projectId: string, hostname: string): Promise<DnsRecordRequest | null>;
|
|
32
40
|
private getProjectByName;
|
|
33
41
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
declare class CloudflareDns implements Dns {
|
|
42
|
+
/** The Cloudflare implementation of the `dns` capability; finds a domain's zone by walking from the domain up to its apex. */
|
|
43
|
+
export declare class CloudflareDns implements Dns {
|
|
37
44
|
private readonly client;
|
|
45
|
+
/** The capability this class provides, always `"dns"`. */
|
|
38
46
|
readonly key: "dns";
|
|
47
|
+
/** The provider name, `"cloudflare"`. */
|
|
39
48
|
readonly providerName = "cloudflare";
|
|
40
49
|
private readonly zoneCache;
|
|
41
50
|
constructor(client: () => CloudflareClient);
|
|
51
|
+
/** Lists the DNS records in the zone that holds the domain. */
|
|
42
52
|
listRecords(domain: string): Promise<DnsRecord[]>;
|
|
53
|
+
/** Updates the first record of the same type and name, or creates the record when there is none; returns the stored record. */
|
|
43
54
|
upsertRecord(domain: string, record: DnsRecord): Promise<DnsRecord>;
|
|
55
|
+
/** Deletes the DNS record with this id from the zone that holds the domain. */
|
|
44
56
|
deleteRecord(domain: string, id: string): Promise<void>;
|
|
57
|
+
/** `holocron doctor` has no specific domain to check — lists every zone the token can see instead. */
|
|
58
|
+
whoami(): Promise<{
|
|
59
|
+
ok: boolean;
|
|
60
|
+
zones: number;
|
|
61
|
+
}>;
|
|
45
62
|
/**
|
|
46
63
|
* Walk from full domain up to apex to find the Cloudflare zone.
|
|
47
64
|
* E.g. "api.staging.example.com" tries:
|
|
@@ -51,40 +68,42 @@ declare class CloudflareDns implements Dns {
|
|
|
51
68
|
*/
|
|
52
69
|
private resolveZone;
|
|
53
70
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
declare class CloudflareWorkers implements Workers {
|
|
71
|
+
/** The Cloudflare implementation of the `workers` capability, backed by Workers scripts and routes in one account. */
|
|
72
|
+
export declare class CloudflareWorkers implements Workers {
|
|
57
73
|
private readonly client;
|
|
58
74
|
private readonly accountId;
|
|
75
|
+
/** The capability this class provides, always `"workers"`. */
|
|
59
76
|
readonly key: "workers";
|
|
77
|
+
/** The provider name, `"cloudflare"`. */
|
|
60
78
|
readonly providerName = "cloudflare";
|
|
61
79
|
private readonly zoneCache;
|
|
62
80
|
constructor(client: () => Pick<CloudflareClient$1, "workers" | "zones">, accountId: string);
|
|
63
81
|
upsertProxy(hostname: string, config: WikiProxyConfig): Promise<void>;
|
|
82
|
+
/** `holocron doctor` has no specific script to check — lists every script the token can see instead. */
|
|
83
|
+
whoami(): Promise<{
|
|
84
|
+
ok: boolean;
|
|
85
|
+
scripts: number;
|
|
86
|
+
}>;
|
|
64
87
|
deployScript(name: string, config: DeployScriptConfig): Promise<DeployScriptResult>;
|
|
88
|
+
setSecret(script: string, name: string, value: string): Promise<void>;
|
|
89
|
+
secretNames(script: string): Promise<string[]>;
|
|
90
|
+
/**
|
|
91
|
+
* Whether the script exists, which routes in the zone of `host` point at it, and the names of its secrets. Reads
|
|
92
|
+
* only, and asks Cloudflare for names, so no value is ever fetched.
|
|
93
|
+
*/
|
|
94
|
+
inspectScript(name: string, host: string): Promise<WorkerScriptState>;
|
|
65
95
|
/** Idempotent create-or-update for one route pattern — shared by both public methods. */
|
|
66
96
|
private upsertRoute;
|
|
67
97
|
private resolveZone;
|
|
68
98
|
}
|
|
69
|
-
//#endregion
|
|
70
|
-
//#region src/verify-token.d.ts
|
|
71
|
-
interface VerifyTokenSuccess {
|
|
72
|
-
ok: true;
|
|
73
|
-
subject: string;
|
|
74
|
-
}
|
|
75
|
-
interface VerifyTokenFailure {
|
|
76
|
-
ok: false;
|
|
77
|
-
message: string;
|
|
78
|
-
}
|
|
79
|
-
type VerifyTokenResult = VerifyTokenSuccess | VerifyTokenFailure;
|
|
80
99
|
interface VerifyTokenOptions {
|
|
81
100
|
baseUrl?: string;
|
|
82
101
|
fetch?: typeof fetch;
|
|
83
102
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
interface CloudflarePluginOptions extends ResolveTokenInput {
|
|
103
|
+
/** 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. */
|
|
104
|
+
export declare function verifyToken(token: string, opts?: VerifyTokenOptions): Promise<VerifyTokenResult$1>;
|
|
105
|
+
/** Options for the Cloudflare plugin: how the API token is resolved, the account, and test overrides. */
|
|
106
|
+
export interface CloudflarePluginOptions extends ResolveTokenInput {
|
|
88
107
|
/**
|
|
89
108
|
* Cloudflare account ID. Required for Pages (deployment) operations;
|
|
90
109
|
* optional for DNS-only use.
|
|
@@ -92,27 +111,38 @@ interface CloudflarePluginOptions extends ResolveTokenInput {
|
|
|
92
111
|
accountId?: string;
|
|
93
112
|
/** Override base URL for tests. */
|
|
94
113
|
baseUrl?: string;
|
|
114
|
+
/** The `fetch` to use instead of the global one, for tests. */
|
|
95
115
|
fetch?: typeof fetch;
|
|
96
116
|
}
|
|
97
|
-
|
|
117
|
+
/** The shared state the Cloudflare plugin's capabilities and probes are built from. */
|
|
118
|
+
export interface PluginContext {
|
|
119
|
+
/** The options the plugin was created with, with `accountId` falling back to `CLOUDFLARE_ACCOUNT_ID`. */
|
|
98
120
|
options: CloudflarePluginOptions;
|
|
99
121
|
/** Memoized client — the token is resolved on first use, not at plugin load. */
|
|
100
122
|
client: () => CloudflareClient;
|
|
101
123
|
/** Memoized token resolver — shared by the client and the Workers capability. */
|
|
102
124
|
token: () => string;
|
|
103
125
|
}
|
|
104
|
-
|
|
105
|
-
declare function
|
|
106
|
-
|
|
107
|
-
declare function
|
|
108
|
-
|
|
126
|
+
/** Builds the plugin context from its options; the API token is resolved lazily on the first client use. */
|
|
127
|
+
export declare function createContext(options?: CloudflarePluginOptions): PluginContext;
|
|
128
|
+
/** Builds the `dns` capability for a context. */
|
|
129
|
+
export declare function dns(ctx: PluginContext): Dns;
|
|
130
|
+
/** Builds the `deployment` capability for a context; throws when no account id is set. */
|
|
131
|
+
export declare function deployment(ctx: PluginContext): Deployment;
|
|
132
|
+
/** Builds the `workers` capability for a context; throws when no account id is set. */
|
|
133
|
+
export declare function workersCapability(ctx: PluginContext): Workers;
|
|
134
|
+
/** Creates the Cloudflare plugin: `dns` always, plus `deployment` and `workers` when an account id is set, and a `connection` probe. */
|
|
135
|
+
export declare function createPlugin(options?: CloudflarePluginOptions): {
|
|
109
136
|
name: string;
|
|
137
|
+
probes: {
|
|
138
|
+
connection: import("@theholocron/cli").Probe;
|
|
139
|
+
};
|
|
110
140
|
capabilities: {
|
|
111
141
|
deployment?: (() => Deployment) | undefined;
|
|
112
142
|
workers?: (() => Workers) | undefined;
|
|
113
143
|
dns: () => Dns;
|
|
114
144
|
};
|
|
115
145
|
};
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
export {
|
|
146
|
+
/** One-line hint printed by `holocron auth set cloudflare` when no token is supplied or the supplied one is rejected. */
|
|
147
|
+
export declare const AUTH_HINT: string;
|
|
148
|
+
export { AuthError, type CloudflareClient, type CloudflareClientOptions, type ResolveTokenInput, type VerifyTokenFailure, type VerifyTokenResult, type VerifyTokenSuccess, createCloudflareClient };
|
package/dist/index.mjs
CHANGED
|
@@ -1,23 +1,30 @@
|
|
|
1
|
-
import { AuthError, ProviderApiError, createResolveToken } from "@theholocron/cli";
|
|
1
|
+
import { AuthError, ProviderApiError, connectionProbe, createResolveToken } from "@theholocron/cli";
|
|
2
2
|
import { createCloudflareClient } from "@theholocron/cloudflare-client";
|
|
3
|
-
|
|
3
|
+
import { errorMessage } from "@theholocron/misc-utils";
|
|
4
|
+
// src/auth.ts
|
|
5
|
+
/** 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
6
|
const resolveToken = createResolveToken({
|
|
5
7
|
envName: "HOLOCRON_CLOUDFLARE_TOKEN",
|
|
6
8
|
vendorEnvName: "CLOUDFLARE_API_TOKEN",
|
|
7
9
|
keyringService: "cloudflare",
|
|
8
10
|
errorMessage: "no Cloudflare API token found. Pass --token <TOKEN>, set HOLOCRON_CLOUDFLARE_TOKEN / CLOUDFLARE_API_TOKEN, or run: holocron auth set cloudflare <TOKEN>"
|
|
9
11
|
});
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
/** Every token this plugin takes, for `holocron doctor tokens`. */
|
|
13
|
+
const tokens = [resolveToken.token];
|
|
14
|
+
// src/capabilities/deployment.ts
|
|
15
|
+
/** The Cloudflare implementation of the `deployment` capability, backed by Cloudflare Pages projects in one account. */
|
|
12
16
|
var CloudflareDeployment = class {
|
|
13
17
|
client;
|
|
14
18
|
accountId;
|
|
19
|
+
/** The capability this class provides, always `"deployment"`. */
|
|
15
20
|
key = "deployment";
|
|
21
|
+
/** The provider name, `"cloudflare"`. */
|
|
16
22
|
providerName = "cloudflare";
|
|
17
23
|
constructor(client, accountId) {
|
|
18
24
|
this.client = client;
|
|
19
25
|
this.accountId = accountId;
|
|
20
26
|
}
|
|
27
|
+
/** Lists the account's Pages projects. */
|
|
21
28
|
async listProjects() {
|
|
22
29
|
return (await this.client().pages.listProjects(this.accountId)).map(mapProject);
|
|
23
30
|
}
|
|
@@ -29,14 +36,17 @@ var CloudflareDeployment = class {
|
|
|
29
36
|
production_branch: "main"
|
|
30
37
|
}));
|
|
31
38
|
}
|
|
39
|
+
/** Returns the project as it is now; Pages has no REST setting for the requested options, so `settings` is ignored. */
|
|
32
40
|
async updateProjectSettings(projectId, settings) {
|
|
33
41
|
return mapProject(await this.client().pages.getProject(this.accountId, projectId));
|
|
34
42
|
}
|
|
43
|
+
/** Lists the names of the environment variables set for the production or preview target. */
|
|
35
44
|
async listEnvVars(projectId, target) {
|
|
36
45
|
const project = await this.client().pages.getProject(this.accountId, projectId);
|
|
37
46
|
const envConfig = target === "production" ? project.deployment_configs.production : project.deployment_configs.preview;
|
|
38
47
|
return Object.keys(envConfig.env_vars);
|
|
39
48
|
}
|
|
49
|
+
/** Sets a plain-text environment variable on the production or preview target, keeping the others. */
|
|
40
50
|
async setEnvVar(projectId, target, name, value) {
|
|
41
51
|
const cfg = (await this.client().pages.getProject(this.accountId, projectId)).deployment_configs;
|
|
42
52
|
const scope = target === "production" ? "production" : "preview";
|
|
@@ -55,6 +65,7 @@ var CloudflareDeployment = class {
|
|
|
55
65
|
async triggerDeployment(input) {
|
|
56
66
|
return mapDeployment(await this.client().pages.createDeployment(this.accountId, input.projectId, input.branch), input.projectId, input.branch, input.target);
|
|
57
67
|
}
|
|
68
|
+
/** Fetches a deployment by an id of the form `projectName:deploymentId`; throws a 400 `ProviderApiError` for any other form. */
|
|
58
69
|
async getDeployment(deploymentId) {
|
|
59
70
|
const sep = deploymentId.indexOf(":");
|
|
60
71
|
if (sep === -1) throw new ProviderApiError(`Invalid Cloudflare Pages deployment id "${deploymentId}" — expected "projectName:deploymentId"`, 400, void 0);
|
|
@@ -114,20 +125,24 @@ function normalizeStatus(status) {
|
|
|
114
125
|
default: return "error";
|
|
115
126
|
}
|
|
116
127
|
}
|
|
117
|
-
|
|
118
|
-
|
|
128
|
+
// src/capabilities/dns.ts
|
|
129
|
+
/** The Cloudflare implementation of the `dns` capability; finds a domain's zone by walking from the domain up to its apex. */
|
|
119
130
|
var CloudflareDns = class {
|
|
120
131
|
client;
|
|
132
|
+
/** The capability this class provides, always `"dns"`. */
|
|
121
133
|
key = "dns";
|
|
134
|
+
/** The provider name, `"cloudflare"`. */
|
|
122
135
|
providerName = "cloudflare";
|
|
123
136
|
zoneCache = /* @__PURE__ */ new Map();
|
|
124
137
|
constructor(client) {
|
|
125
138
|
this.client = client;
|
|
126
139
|
}
|
|
140
|
+
/** Lists the DNS records in the zone that holds the domain. */
|
|
127
141
|
async listRecords(domain) {
|
|
128
142
|
const zoneId = await this.resolveZone(domain);
|
|
129
143
|
return (await this.client().dns.list(zoneId)).map(mapRecord);
|
|
130
144
|
}
|
|
145
|
+
/** Updates the first record of the same type and name, or creates the record when there is none; returns the stored record. */
|
|
131
146
|
async upsertRecord(domain, record) {
|
|
132
147
|
const client = this.client();
|
|
133
148
|
const zoneId = await this.resolveZone(domain);
|
|
@@ -150,10 +165,18 @@ var CloudflareDns = class {
|
|
|
150
165
|
...record.proxied !== void 0 ? { proxied: record.proxied } : {}
|
|
151
166
|
}));
|
|
152
167
|
}
|
|
168
|
+
/** Deletes the DNS record with this id from the zone that holds the domain. */
|
|
153
169
|
async deleteRecord(domain, id) {
|
|
154
170
|
const zoneId = await this.resolveZone(domain);
|
|
155
171
|
await this.client().dns.delete(zoneId, id);
|
|
156
172
|
}
|
|
173
|
+
/** `holocron doctor` has no specific domain to check — lists every zone the token can see instead. */
|
|
174
|
+
async whoami() {
|
|
175
|
+
return {
|
|
176
|
+
ok: true,
|
|
177
|
+
zones: (await this.client().zones.list()).length
|
|
178
|
+
};
|
|
179
|
+
}
|
|
157
180
|
/**
|
|
158
181
|
* Walk from full domain up to apex to find the Cloudflare zone.
|
|
159
182
|
* E.g. "api.staging.example.com" tries:
|
|
@@ -182,15 +205,18 @@ function mapRecord(raw) {
|
|
|
182
205
|
type: raw.type,
|
|
183
206
|
name: raw.name,
|
|
184
207
|
content: raw.content,
|
|
185
|
-
ttl: raw.ttl
|
|
208
|
+
ttl: raw.ttl,
|
|
209
|
+
...raw.proxied !== void 0 ? { proxied: raw.proxied } : {}
|
|
186
210
|
};
|
|
187
211
|
}
|
|
188
|
-
|
|
189
|
-
|
|
212
|
+
// src/capabilities/workers.ts
|
|
213
|
+
/** The Cloudflare implementation of the `workers` capability, backed by Workers scripts and routes in one account. */
|
|
190
214
|
var CloudflareWorkers = class {
|
|
191
215
|
client;
|
|
192
216
|
accountId;
|
|
217
|
+
/** The capability this class provides, always `"workers"`. */
|
|
193
218
|
key = "workers";
|
|
219
|
+
/** The provider name, `"cloudflare"`. */
|
|
194
220
|
providerName = "cloudflare";
|
|
195
221
|
zoneCache = /* @__PURE__ */ new Map();
|
|
196
222
|
constructor(client, accountId) {
|
|
@@ -203,12 +229,45 @@ var CloudflareWorkers = class {
|
|
|
203
229
|
await this.client().workers.putScript(this.accountId, scriptName, script);
|
|
204
230
|
await this.upsertRoute(`${hostname}/*`, scriptName);
|
|
205
231
|
}
|
|
232
|
+
/** `holocron doctor` has no specific script to check — lists every script the token can see instead. */
|
|
233
|
+
async whoami() {
|
|
234
|
+
return {
|
|
235
|
+
ok: true,
|
|
236
|
+
scripts: (await this.client().workers.listScripts(this.accountId)).length
|
|
237
|
+
};
|
|
238
|
+
}
|
|
206
239
|
async deployScript(name, config) {
|
|
207
240
|
await this.client().workers.putScript(this.accountId, name, config.code);
|
|
208
241
|
for (const [secretName, value] of Object.entries(config.secrets ?? {})) await this.client().workers.putSecret(this.accountId, name, secretName, value);
|
|
209
242
|
for (const pattern of config.routes ?? []) await this.upsertRoute(pattern, name);
|
|
210
243
|
return { scriptName: name };
|
|
211
244
|
}
|
|
245
|
+
async setSecret(script, name, value) {
|
|
246
|
+
await this.client().workers.putSecret(this.accountId, script, name, value);
|
|
247
|
+
}
|
|
248
|
+
async secretNames(script) {
|
|
249
|
+
return (await this.client().workers.listSecrets(this.accountId, script)).map((secret) => secret.name);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Whether the script exists, which routes in the zone of `host` point at it, and the names of its secrets. Reads
|
|
253
|
+
* only, and asks Cloudflare for names, so no value is ever fetched.
|
|
254
|
+
*/
|
|
255
|
+
async inspectScript(name, host) {
|
|
256
|
+
const client = this.client();
|
|
257
|
+
if (!(await client.workers.listScripts(this.accountId)).some((s) => s.id === name)) return {
|
|
258
|
+
exists: false,
|
|
259
|
+
routes: [],
|
|
260
|
+
bindingNames: []
|
|
261
|
+
};
|
|
262
|
+
const zoneId = await this.resolveZone(host);
|
|
263
|
+
const routes = await client.workers.listRoutes(zoneId);
|
|
264
|
+
const secrets = await client.workers.listSecrets(this.accountId, name);
|
|
265
|
+
return {
|
|
266
|
+
exists: true,
|
|
267
|
+
routes: routes.filter((r) => r.script === name).map((r) => r.pattern),
|
|
268
|
+
bindingNames: secrets.map((secret) => secret.name)
|
|
269
|
+
};
|
|
270
|
+
}
|
|
212
271
|
/** Idempotent create-or-update for one route pattern — shared by both public methods. */
|
|
213
272
|
async upsertRoute(pattern, scriptName) {
|
|
214
273
|
const hostname = pattern.split("/")[0];
|
|
@@ -251,8 +310,8 @@ function generateProxyScript(config) {
|
|
|
251
310
|
`};`
|
|
252
311
|
].join("\n");
|
|
253
312
|
}
|
|
254
|
-
|
|
255
|
-
|
|
313
|
+
// src/verify-token.ts
|
|
314
|
+
/** 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. */
|
|
256
315
|
async function verifyToken(token, opts = {}) {
|
|
257
316
|
const client = createCloudflareClient({
|
|
258
317
|
token,
|
|
@@ -267,19 +326,24 @@ async function verifyToken(token, opts = {}) {
|
|
|
267
326
|
};
|
|
268
327
|
} catch {}
|
|
269
328
|
try {
|
|
329
|
+
const names = (await client.zones.list()).map((z) => z.name);
|
|
330
|
+
if (names.length === 0) return {
|
|
331
|
+
ok: true,
|
|
332
|
+
subject: "no zones visible"
|
|
333
|
+
};
|
|
270
334
|
return {
|
|
271
335
|
ok: true,
|
|
272
|
-
subject: `
|
|
336
|
+
subject: `zones: ${names.length <= 5 ? names.join(", ") : `${names.slice(0, 5).join(", ")} (+${names.length - 5} more)`}`
|
|
273
337
|
};
|
|
274
338
|
} catch (err) {
|
|
275
339
|
return {
|
|
276
340
|
ok: false,
|
|
277
|
-
message:
|
|
341
|
+
message: errorMessage(err)
|
|
278
342
|
};
|
|
279
343
|
}
|
|
280
344
|
}
|
|
281
|
-
|
|
282
|
-
|
|
345
|
+
// src/index.ts
|
|
346
|
+
/** Builds the plugin context from its options; the API token is resolved lazily on the first client use. */
|
|
283
347
|
function createContext(options = {}) {
|
|
284
348
|
const accountId = options.accountId ?? process.env.CLOUDFLARE_ACCOUNT_ID;
|
|
285
349
|
const opts = {
|
|
@@ -299,21 +363,35 @@ function createContext(options = {}) {
|
|
|
299
363
|
})
|
|
300
364
|
};
|
|
301
365
|
}
|
|
366
|
+
/** Builds the `dns` capability for a context. */
|
|
302
367
|
function dns(ctx) {
|
|
303
368
|
return new CloudflareDns(ctx.client);
|
|
304
369
|
}
|
|
370
|
+
/** Builds the `deployment` capability for a context; throws when no account id is set. */
|
|
305
371
|
function deployment(ctx) {
|
|
306
372
|
if (!ctx.options.accountId) throw new Error("Cloudflare accountId is required for Pages deployments — set accountId in the plugin options or CLOUDFLARE_ACCOUNT_ID env var");
|
|
307
373
|
return new CloudflareDeployment(ctx.client, ctx.options.accountId);
|
|
308
374
|
}
|
|
375
|
+
/** Builds the `workers` capability for a context; throws when no account id is set. */
|
|
309
376
|
function workersCapability(ctx) {
|
|
310
377
|
if (!ctx.options.accountId) throw new Error("Cloudflare accountId is required for Workers — set accountId in the plugin options or CLOUDFLARE_ACCOUNT_ID env var");
|
|
311
378
|
return new CloudflareWorkers(ctx.client, ctx.options.accountId);
|
|
312
379
|
}
|
|
380
|
+
/** Creates the Cloudflare plugin: `dns` always, plus `deployment` and `workers` when an account id is set, and a `connection` probe. */
|
|
313
381
|
function createPlugin(options = {}) {
|
|
314
382
|
const ctx = createContext(options);
|
|
315
383
|
return {
|
|
316
384
|
name: "@theholocron/holocron-plugin-cloudflare",
|
|
385
|
+
probes: { connection: connectionProbe({
|
|
386
|
+
provider: "Cloudflare",
|
|
387
|
+
url: options.baseUrl ?? "https://api.cloudflare.com",
|
|
388
|
+
fetch: options.fetch,
|
|
389
|
+
token: () => resolveToken(options),
|
|
390
|
+
verify: (token) => verifyToken(token, {
|
|
391
|
+
baseUrl: options.baseUrl,
|
|
392
|
+
fetch: options.fetch
|
|
393
|
+
})
|
|
394
|
+
}) },
|
|
317
395
|
capabilities: {
|
|
318
396
|
dns: () => dns(ctx),
|
|
319
397
|
...ctx.options.accountId ? {
|
|
@@ -323,6 +401,6 @@ function createPlugin(options = {}) {
|
|
|
323
401
|
}
|
|
324
402
|
};
|
|
325
403
|
}
|
|
404
|
+
/** One-line hint printed by `holocron auth set cloudflare` when no token is supplied or the supplied one is rejected. */
|
|
326
405
|
const AUTH_HINT = "create an API token at https://dash.cloudflare.com/profile/api-tokens with Zone:Read, DNS:Edit, Cloudflare Pages:Edit, Workers Scripts:Edit (account-level), and Workers Routes:Edit (zone-level) permissions, then run: holocron auth set cloudflare <TOKEN>";
|
|
327
|
-
|
|
328
|
-
export { AUTH_HINT, AuthError, CloudflareDeployment, CloudflareDns, CloudflareWorkers, createCloudflareClient, createContext, createPlugin, deployment, dns, resolveToken, verifyToken, workersCapability };
|
|
406
|
+
export { AUTH_HINT, AuthError, CloudflareDeployment, CloudflareDns, CloudflareWorkers, createCloudflareClient, createContext, createPlugin, deployment, dns, resolveToken, tokens, verifyToken, workersCapability };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theholocron/holocron-plugin-cloudflare",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.281",
|
|
4
4
|
"description": "Holocron plugin for Cloudflare. Implements the dns capability against Cloudflare's REST API — zone resolution, DNS record management.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
"holocron",
|
|
9
9
|
"plugin"
|
|
10
10
|
],
|
|
11
|
-
"homepage": "https://
|
|
11
|
+
"homepage": "https://docs.theholocron.dev/holocron/plugins/cloudflare/",
|
|
12
12
|
"bugs": "https://github.com/theholocron/holocron/issues",
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "git",
|
|
15
15
|
"url": "git+https://github.com/theholocron/holocron.git",
|
|
16
|
-
"directory": "
|
|
16
|
+
"directory": "plugins/holocron-plugin-cloudflare"
|
|
17
17
|
},
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"author": "Newton Koumantzelis",
|
|
@@ -30,28 +30,30 @@
|
|
|
30
30
|
"dist"
|
|
31
31
|
],
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@theholocron/cloudflare-client": "^1.23.0",
|
|
34
|
-
"@theholocron/eslint-config": "^8.0.0",
|
|
35
|
-
"@theholocron/http-client": "^1.23.0",
|
|
36
|
-
"@theholocron/tsconfig": "^8.0.0",
|
|
37
|
-
"@theholocron/tsdown-config": "^8.0.0",
|
|
38
|
-
"@theholocron/vitest-config": "^8.0.0",
|
|
39
33
|
"@types/node": "^26",
|
|
40
|
-
"@vitest/coverage-v8": "^
|
|
34
|
+
"@vitest/coverage-v8": "^5.0.5",
|
|
41
35
|
"@vitest/eslint-plugin": "^1.6.27",
|
|
42
36
|
"chalk": "^6.0.0",
|
|
43
37
|
"eslint": "^10.8.1",
|
|
44
38
|
"eslint-plugin-n": "^18.3.0",
|
|
45
39
|
"globals": "^17.11.0",
|
|
46
|
-
"tsdown": "^0.
|
|
47
|
-
"tsx": "4.23.
|
|
40
|
+
"tsdown": "^0.23.3",
|
|
41
|
+
"tsx": "4.23.15",
|
|
48
42
|
"typescript": "^5.9.3",
|
|
49
|
-
"vitest": "^
|
|
50
|
-
"@theholocron/cli": "5.0.0-alpha.
|
|
43
|
+
"vitest": "^5.0.5",
|
|
44
|
+
"@theholocron/cli": "5.0.0-alpha.281",
|
|
45
|
+
"@theholocron/cloudflare-client": "^1.58.0-alpha.3",
|
|
46
|
+
"@theholocron/misc-utils": "^1.16.1-alpha.3",
|
|
47
|
+
"@theholocron/tsconfig": "^8.9.0-alpha.5",
|
|
48
|
+
"@theholocron/tsdown-config": "^8.9.0-alpha.5",
|
|
49
|
+
"@theholocron/vitest-config": "^8.9.0-alpha.5",
|
|
50
|
+
"@theholocron/http-client": "^1.58.0-alpha.3",
|
|
51
|
+
"@theholocron/eslint-config": "^8.9.0-alpha.5"
|
|
51
52
|
},
|
|
52
53
|
"peerDependencies": {
|
|
53
|
-
"@theholocron/cloudflare-client": "^1.
|
|
54
|
-
"@theholocron/
|
|
54
|
+
"@theholocron/cloudflare-client": "^1.53.1",
|
|
55
|
+
"@theholocron/misc-utils": "^1.16.0",
|
|
56
|
+
"@theholocron/cli": "5.0.0-alpha.281"
|
|
55
57
|
},
|
|
56
58
|
"peerDependenciesMeta": {
|
|
57
59
|
"@theholocron/cloudflare-client": {}
|
|
@@ -64,10 +66,10 @@
|
|
|
64
66
|
},
|
|
65
67
|
"scripts": {
|
|
66
68
|
"delivery.build": "tsdown",
|
|
67
|
-
"sourceQuality.staticAnalysis": "
|
|
69
|
+
"sourceQuality.staticAnalysis": "eslint .",
|
|
68
70
|
"test:watch": "vitest",
|
|
69
71
|
"validate": "tsx scripts/validate.mjs",
|
|
70
|
-
"verification.typeSafety": "
|
|
71
|
-
"verification.unitTests": "
|
|
72
|
+
"verification.typeSafety": "tsc --noEmit",
|
|
73
|
+
"verification.unitTests": "vitest run"
|
|
72
74
|
}
|
|
73
75
|
}
|