@theholocron/holocron-plugin-cloudflare 5.0.0-alpha.510 → 5.0.0-alpha.511

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 { AuthError, DeployScriptConfig, DeployScriptResult, Deployment, DeploymentProject, DeploymentProjectSettings, DeploymentRecord, DeploymentTarget, DeploymentTrigger, Dns, DnsRecord, DnsRecordRequest, EnsuredTunnel, PluginToken, ResolveTokenInput, TunnelRoute, Tunnels, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess, WikiProxyConfig, WorkerDeploymentState, WorkerScriptState, Workers } from "@theholocron/cli";
1
+ import { AuthError, DeployScriptConfig, DeployScriptResult, Deployment, DeploymentProject, DeploymentProjectSettings, DeploymentRecord, DeploymentTarget, DeploymentTrigger, Dns, DnsRecord, DnsRecordRequest, EnsureRelaySourceInput, EnsureRelaySourceResult, EnsuredTunnel, InspectRelaySourceInput, PluginToken, RelaySourceState, ResolveTokenInput, TunnelRoute, Tunnels, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess, WikiProxyConfig, WorkerDeploymentState, WorkerScriptState, Workers } from "@theholocron/cli";
2
2
  import { CloudflareClient, CloudflareClient as CloudflareClient$1, CloudflareClientOptions, createCloudflareClient } from "@theholocron/cloudflare-client";
3
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
4
  export declare const resolveToken: import("@theholocron/cli").TokenResolver;
@@ -46,12 +46,18 @@ export declare class CloudflareDeployment implements Deployment {
46
46
  /** The Cloudflare implementation of the `dns` capability; finds a domain's zone by walking from the domain up to its apex. */
47
47
  export declare class CloudflareDns implements Dns {
48
48
  private readonly client;
49
+ private readonly accountId?;
49
50
  /** The capability this class provides, always `"dns"`. */
50
51
  readonly key: "dns";
51
52
  /** The provider name, `"cloudflare"`. */
52
53
  readonly providerName = "cloudflare";
53
54
  private readonly zoneCache;
54
- 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;
55
61
  /** Lists the DNS records in the zone that holds the domain. */
56
62
  listRecords(domain: string): Promise<DnsRecord[]>;
57
63
  /** Updates the first record of the same type and name, or creates the record when there is none; returns the stored record. */
package/dist/index.mjs CHANGED
@@ -9,7 +9,8 @@ const EVERYTHING = {
9
9
  routes: "required",
10
10
  scripts: "required",
11
11
  pages: "required",
12
- tunnels: "required"
12
+ tunnels: "required",
13
+ notifications: "required"
13
14
  };
14
15
  /**
15
16
  * What each layer's Cloudflare token is meant to carry, the same as "What each Cloudflare token needs" in
@@ -32,7 +33,8 @@ const EXPECTED = {
32
33
  routes: "absent",
33
34
  scripts: "absent",
34
35
  pages: "required",
35
- tunnels: "absent"
36
+ tunnels: "absent",
37
+ notifications: "absent"
36
38
  }
37
39
  }
38
40
  };
@@ -176,17 +178,123 @@ function normalizeStatus(status) {
176
178
  default: return "error";
177
179
  }
178
180
  }
181
+ // src/capabilities/relay-source.ts
182
+ /** The name of the policy that sends one alert type to the relay: the source's name, then the alert type. */
183
+ function policyName(name, alertType) {
184
+ return `${name}: ${alertType}`;
185
+ }
186
+ /** The webhook targets of a policy with the destination in them, once. */
187
+ function withDestination(webhooks = [], id) {
188
+ return webhooks.some((w) => w.id === id) ? webhooks : [...webhooks, { id }];
189
+ }
190
+ /** The policies that belong to a source, found by name, one per alert type of its layout. */
191
+ function ownPolicies(policies, name) {
192
+ const prefix = `${name}: `;
193
+ return policies.filter((p) => p.name.startsWith(prefix));
194
+ }
195
+ /**
196
+ * Makes the Cloudflare notification webhook for a relay source (spec `tech-relay-sources`). A destination alone sends
197
+ * nothing, so each alert type of `events` gets a policy of its own that names the destination. The destination is
198
+ * found by URL and each policy by name; nothing is deleted. The secret the caller offers is set only when the
199
+ * destination is made (Cloudflare never returns it), so it is returned only then.
200
+ */
201
+ async function ensureRelaySource(client, accountId, input) {
202
+ const updated = [];
203
+ let found = (await client.alerting.webhooks.list(accountId)).find((d) => d.url === input.url);
204
+ let created = false;
205
+ let secret;
206
+ if (!found) {
207
+ found = {
208
+ id: (await client.alerting.webhooks.create(accountId, {
209
+ name: input.name,
210
+ url: input.url,
211
+ ...input.secret ? { secret: input.secret } : {}
212
+ })).id,
213
+ name: input.name,
214
+ url: input.url
215
+ };
216
+ created = true;
217
+ if (input.secret) secret = input.secret;
218
+ }
219
+ const destination = found;
220
+ const events = input.events ?? [];
221
+ if (events.length === 0) {
222
+ const todo = [`List the Cloudflare alert types to send in the source's \`events\` (GET /accounts/<id>/alerting/v3/available_alerts names them); a webhook with no policy sends nothing.`];
223
+ return {
224
+ created,
225
+ updated,
226
+ ...secret ? { secret } : {},
227
+ todo
228
+ };
229
+ }
230
+ const policies = ownPolicies(await client.alerting.policies.list(accountId), input.name);
231
+ for (const alertType of events) {
232
+ const name = policyName(input.name, alertType);
233
+ const body = (base) => ({
234
+ name,
235
+ alert_type: alertType,
236
+ enabled: true,
237
+ mechanisms: {
238
+ ...base?.mechanisms,
239
+ webhooks: withDestination(base?.mechanisms.webhooks, destination.id)
240
+ },
241
+ ...base?.description !== void 0 ? { description: base.description } : {},
242
+ ...base?.filters !== void 0 ? { filters: base.filters } : {},
243
+ ...base?.alert_interval !== void 0 ? { alert_interval: base.alert_interval } : {}
244
+ });
245
+ const existing = policies.find((p) => p.name === name);
246
+ if (!existing) {
247
+ await client.alerting.policies.create(accountId, body());
248
+ updated.push(`policy ${alertType}`);
249
+ continue;
250
+ }
251
+ const attached = existing.mechanisms.webhooks?.some((w) => w.id === destination.id) ?? false;
252
+ if (existing.enabled && attached) continue;
253
+ await client.alerting.policies.update(accountId, existing.id, body(existing));
254
+ updated.push(`policy ${alertType}`);
255
+ }
256
+ return {
257
+ created,
258
+ updated: created ? [] : updated,
259
+ ...secret ? { secret } : {}
260
+ };
261
+ }
262
+ /** Reads the webhook back without changing anything: whether the destination exists and which alert types it is sent for. */
263
+ async function inspectRelaySource(client, accountId, input) {
264
+ const destination = (await client.alerting.webhooks.list(accountId)).find((d) => d.url === input.url);
265
+ if (!destination) return { exists: false };
266
+ const policies = ownPolicies(await client.alerting.policies.list(accountId), input.name).filter((p) => p.mechanisms.webhooks?.some((w) => w.id === destination.id));
267
+ return {
268
+ exists: true,
269
+ enabled: policies.length > 0 && policies.every((p) => p.enabled),
270
+ events: policies.map((p) => p.alert_type)
271
+ };
272
+ }
179
273
  // src/capabilities/dns.ts
180
274
  /** The Cloudflare implementation of the `dns` capability; finds a domain's zone by walking from the domain up to its apex. */
181
275
  var CloudflareDns = class {
182
276
  client;
277
+ accountId;
183
278
  /** The capability this class provides, always `"dns"`. */
184
279
  key = "dns";
185
280
  /** The provider name, `"cloudflare"`. */
186
281
  providerName = "cloudflare";
187
282
  zoneCache = /* @__PURE__ */ new Map();
188
- constructor(client) {
283
+ constructor(client, accountId) {
189
284
  this.client = client;
285
+ this.accountId = accountId;
286
+ }
287
+ /** Makes the Cloudflare notification webhook and the policies that send to it for a relay source; needs the account id. */
288
+ async ensureRelaySource(input) {
289
+ return ensureRelaySource(this.client(), this.account(), input);
290
+ }
291
+ /** Reads the relay source's notification webhook and its policies back, changing nothing. */
292
+ async inspectRelaySource(input) {
293
+ return inspectRelaySource(this.client(), this.account(), input);
294
+ }
295
+ account() {
296
+ if (!this.accountId) throw new ProviderApiError("Cloudflare accountId is required for notification webhooks: set accountId in the plugin options or CLOUDFLARE_ACCOUNT_ID", 0, void 0);
297
+ return this.accountId;
190
298
  }
191
299
  /** Lists the DNS records in the zone that holds the domain. */
192
300
  async listRecords(domain) {
@@ -618,6 +726,11 @@ function tokenProbe(ctx) {
618
726
  permission: "tunnels",
619
727
  outcome: await reach(() => client.tunnels.list(account), "tunnels")
620
728
  });
729
+ tried.push({
730
+ label: "Notifications: Edit",
731
+ permission: "notifications",
732
+ outcome: await reach(async () => (await client.alerting.webhooks.list(account)).map((w) => ({ name: w.name })), "notification webhooks")
733
+ });
621
734
  } else probe.say({
622
735
  status: rules ? "fail" : "info",
623
736
  label: "account permissions",
@@ -697,7 +810,7 @@ function createContext(options = {}) {
697
810
  }
698
811
  /** Builds the `dns` capability for a context. */
699
812
  function dns(ctx) {
700
- return new CloudflareDns(ctx.client);
813
+ return new CloudflareDns(ctx.client, ctx.options.accountId);
701
814
  }
702
815
  /** Builds the `deployment` capability for a context; throws when no account id is set. */
703
816
  function deployment(ctx) {
@@ -743,5 +856,5 @@ function createPlugin(options = {}) {
743
856
  };
744
857
  }
745
858
  /** One-line hint printed by `holocron new token cloudflare` when no token is supplied or the supplied one is rejected. */
746
- 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), Workers Routes:Edit (zone-level) and, for tunnels, Cloudflare Tunnel:Edit (account-level) permissions, then run: holocron new token cloudflare <TOKEN>";
859
+ 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), Workers Routes:Edit (zone-level) and, for tunnels, Cloudflare Tunnel:Edit and, for relay sources, Notifications:Edit (account-level) permissions, then run: holocron new token cloudflare <TOKEN>";
747
860
  export { AUTH_HINT, AuthError, CloudflareDeployment, CloudflareDns, CloudflareTunnels, CloudflareWorkers, createCloudflareClient, createContext, createPlugin, deployment, dns, resolveToken, tokens, tunnelsCapability, 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.510",
3
+ "version": "5.0.0-alpha.511",
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",
@@ -44,11 +44,11 @@
44
44
  "tsx": "4.23.15",
45
45
  "typescript": "^5.9.3",
46
46
  "vitest": "^5.1.0",
47
- "@theholocron/cloudflare-client": "^1.58.0-alpha.48",
48
- "@theholocron/cli": "5.0.0-alpha.510",
47
+ "@theholocron/cli": "5.0.0-alpha.511",
48
+ "@theholocron/cloudflare-client": "^1.58.0-alpha.49",
49
+ "@theholocron/http-client": "^1.58.0-alpha.49",
49
50
  "@theholocron/eslint-config": "^8.9.0-alpha.21",
50
- "@theholocron/http-client": "^1.58.0-alpha.48",
51
- "@theholocron/kyber": "5.0.0-alpha.510",
51
+ "@theholocron/kyber": "5.0.0-alpha.511",
52
52
  "@theholocron/misc-utils": "^1.17.0-alpha.5",
53
53
  "@theholocron/tsconfig": "^8.9.0-alpha.21",
54
54
  "@theholocron/tsdown-config": "^8.9.0-alpha.21",
@@ -57,7 +57,7 @@
57
57
  "peerDependencies": {
58
58
  "@theholocron/cloudflare-client": "^1.53.1",
59
59
  "@theholocron/misc-utils": "^1.16.0",
60
- "@theholocron/cli": "5.0.0-alpha.510"
60
+ "@theholocron/cli": "5.0.0-alpha.511"
61
61
  },
62
62
  "peerDependenciesMeta": {
63
63
  "@theholocron/cloudflare-client": {}