@theholocron/holocron-plugin-cloudflare 5.0.0-alpha.538 → 5.0.0-alpha.539
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.mjs +66 -16
- package/package.json +10 -10
package/dist/index.mjs
CHANGED
|
@@ -178,29 +178,71 @@ function normalizeStatus(status) {
|
|
|
178
178
|
default: return "error";
|
|
179
179
|
}
|
|
180
180
|
}
|
|
181
|
+
// src/capabilities/destination-matches.ts
|
|
182
|
+
/** The origin of a URL, or `undefined` when it is not one. */
|
|
183
|
+
function originOf(url) {
|
|
184
|
+
try {
|
|
185
|
+
return new URL(url).origin;
|
|
186
|
+
} catch {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Whether a Cloudflare notification destination belongs to a relay source. The URL equal to the wanted one always
|
|
192
|
+
* matches. Cloudflare otherwise returns a destination's URL masked, the whole path replaced (`https://host/...`), so
|
|
193
|
+
* the path cannot be compared: a masked URL matches when it is exactly that shape on the wanted URL's origin and the
|
|
194
|
+
* name is equal (ignoring case). A real URL that merely ends in `...` is not a masked one.
|
|
195
|
+
*/
|
|
196
|
+
function destinationMatches(destination, wanted) {
|
|
197
|
+
if (destination.url === wanted.url) return true;
|
|
198
|
+
if (destination.name.toLowerCase() !== wanted.name.toLowerCase()) return false;
|
|
199
|
+
const origin = originOf(wanted.url);
|
|
200
|
+
return origin !== void 0 && destination.url === `${origin}/...`;
|
|
201
|
+
}
|
|
202
|
+
// src/capabilities/newest-destination.ts
|
|
203
|
+
/** The instant of an RFC 3339 time, so `+02:00` and `Z` spellings of times compare as the moments they are; `-Infinity` when absent or unreadable. */
|
|
204
|
+
function instant(createdAt) {
|
|
205
|
+
const time = createdAt === void 0 ? NaN : Date.parse(createdAt);
|
|
206
|
+
return Number.isNaN(time) ? Number.NEGATIVE_INFINITY : time;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* The destination to use among several that match: the one created last (by the instant of `created_at`, not the
|
|
210
|
+
* text), else the last in list order (a destination with no usable date never beats one with a date; ties go to the
|
|
211
|
+
* later one in the list).
|
|
212
|
+
*/
|
|
213
|
+
function newestDestination(destinations) {
|
|
214
|
+
let best;
|
|
215
|
+
for (const d of destinations) if (!best || instant(d.created_at) >= instant(best.created_at)) best = d;
|
|
216
|
+
return best;
|
|
217
|
+
}
|
|
181
218
|
// src/capabilities/relay-source.ts
|
|
182
219
|
/** The name of the policy that sends one alert type to the relay: the source's name, then the alert type. */
|
|
183
220
|
function policyName(name, alertType) {
|
|
184
221
|
return `${name}: ${alertType}`;
|
|
185
222
|
}
|
|
186
|
-
/**
|
|
187
|
-
|
|
188
|
-
|
|
223
|
+
/**
|
|
224
|
+
* The webhook targets of a policy with the destination in them, once, and without the other destinations that match
|
|
225
|
+
* the same source (`duplicates`): a policy that still names an older duplicate would send every alert twice.
|
|
226
|
+
*/
|
|
227
|
+
function withDestination(webhooks = [], id, duplicates) {
|
|
228
|
+
const kept = webhooks.filter((w) => w.id === id || !duplicates.has(w.id));
|
|
229
|
+
return kept.some((w) => w.id === id) ? kept : [...kept, { id }];
|
|
189
230
|
}
|
|
190
|
-
/** The policies that belong to a source, found by name, one per alert type of its layout. */
|
|
231
|
+
/** The policies that belong to a source, found by name (ignoring case, as the destination is), one per alert type of its layout. */
|
|
191
232
|
function ownPolicies(policies, name) {
|
|
192
|
-
const prefix = `${name}:
|
|
193
|
-
return policies.filter((p) => p.name.startsWith(prefix));
|
|
233
|
+
const prefix = `${name}: `.toLowerCase();
|
|
234
|
+
return policies.filter((p) => p.name.toLowerCase().startsWith(prefix));
|
|
194
235
|
}
|
|
195
236
|
/**
|
|
196
237
|
* Makes the Cloudflare notification webhook for a relay source (spec `tech-relay-sources`). A destination alone sends
|
|
197
238
|
* 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
|
|
239
|
+
* found by name and (masked) URL and each policy by name; nothing is deleted. The secret the caller offers is set only when the
|
|
199
240
|
* destination is made (Cloudflare never returns it), so it is returned only then.
|
|
200
241
|
*/
|
|
201
242
|
async function ensureRelaySource(client, accountId, input) {
|
|
202
243
|
const updated = [];
|
|
203
|
-
|
|
244
|
+
const matches = (await client.alerting.webhooks.list(accountId)).filter((d) => destinationMatches(d, input));
|
|
245
|
+
let found = newestDestination(matches);
|
|
204
246
|
let created = false;
|
|
205
247
|
let secret;
|
|
206
248
|
if (!found) {
|
|
@@ -217,9 +259,15 @@ async function ensureRelaySource(client, accountId, input) {
|
|
|
217
259
|
if (input.secret) secret = input.secret;
|
|
218
260
|
}
|
|
219
261
|
const destination = found;
|
|
262
|
+
const duplicates = new Set(matches.map((d) => d.id));
|
|
263
|
+
const todo = [];
|
|
264
|
+
if (matches.length > 1) {
|
|
265
|
+
const others = matches.filter((d) => d.id !== destination.id).map((d) => d.id);
|
|
266
|
+
todo.push(`Cloudflare has ${matches.length} destinations named ${input.name} for ${input.url} (${matches.map((d) => d.id).join(", ")}). Setup attaches the policies to the newest (${destination.id}) and takes the others off them. Cloudflare does not show a destination's secret, so which one the Worker's secret belongs to is not checked. Send a test alert to confirm it is accepted, then delete the unused ones (${others.join(", ")}) in the Cloudflare dashboard (Notifications, Destinations).`);
|
|
267
|
+
}
|
|
220
268
|
const events = input.events ?? [];
|
|
221
269
|
if (events.length === 0) {
|
|
222
|
-
|
|
270
|
+
todo.push(`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
271
|
return {
|
|
224
272
|
created,
|
|
225
273
|
updated,
|
|
@@ -236,19 +284,20 @@ async function ensureRelaySource(client, accountId, input) {
|
|
|
236
284
|
enabled: true,
|
|
237
285
|
mechanisms: {
|
|
238
286
|
...base?.mechanisms,
|
|
239
|
-
webhooks: withDestination(base?.mechanisms.webhooks, destination.id)
|
|
287
|
+
webhooks: withDestination(base?.mechanisms.webhooks, destination.id, duplicates)
|
|
240
288
|
},
|
|
241
289
|
...base?.description !== void 0 ? { description: base.description } : {},
|
|
242
290
|
...base?.filters !== void 0 ? { filters: base.filters } : {},
|
|
243
291
|
...base?.alert_interval !== void 0 ? { alert_interval: base.alert_interval } : {}
|
|
244
292
|
});
|
|
245
|
-
const existing = policies.find((p) => p.name === name);
|
|
293
|
+
const existing = policies.find((p) => p.name.toLowerCase() === name.toLowerCase());
|
|
246
294
|
if (!existing) {
|
|
247
295
|
await client.alerting.policies.create(accountId, body());
|
|
248
296
|
updated.push(`policy ${alertType}`);
|
|
249
297
|
continue;
|
|
250
298
|
}
|
|
251
|
-
const
|
|
299
|
+
const targets = existing.mechanisms.webhooks ?? [];
|
|
300
|
+
const attached = targets.some((w) => w.id === destination.id) && targets.every((w) => w.id === destination.id || !duplicates.has(w.id));
|
|
252
301
|
if (existing.enabled && attached) continue;
|
|
253
302
|
await client.alerting.policies.update(accountId, existing.id, body(existing));
|
|
254
303
|
updated.push(`policy ${alertType}`);
|
|
@@ -256,14 +305,15 @@ async function ensureRelaySource(client, accountId, input) {
|
|
|
256
305
|
return {
|
|
257
306
|
created,
|
|
258
307
|
updated: created ? [] : updated,
|
|
259
|
-
...secret ? { secret } : {}
|
|
308
|
+
...secret ? { secret } : {},
|
|
309
|
+
...todo.length > 0 ? { todo } : {}
|
|
260
310
|
};
|
|
261
311
|
}
|
|
262
312
|
/** Reads the webhook back without changing anything: whether the destination exists and which alert types it is sent for. */
|
|
263
313
|
async function inspectRelaySource(client, accountId, input) {
|
|
264
|
-
const
|
|
265
|
-
if (
|
|
266
|
-
const policies = ownPolicies(await client.alerting.policies.list(accountId), input.name).filter((p) => p.mechanisms.webhooks?.some((w) => w.id
|
|
314
|
+
const ids = new Set((await client.alerting.webhooks.list(accountId)).filter((d) => destinationMatches(d, input)).map((d) => d.id));
|
|
315
|
+
if (ids.size === 0) return { exists: false };
|
|
316
|
+
const policies = ownPolicies(await client.alerting.policies.list(accountId), input.name).filter((p) => p.mechanisms.webhooks?.some((w) => ids.has(w.id)));
|
|
267
317
|
return {
|
|
268
318
|
exists: true,
|
|
269
319
|
enabled: policies.length > 0 && policies.every((p) => p.enabled),
|
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.539",
|
|
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",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"dist"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@theholocron/env-utils": "^1.17.0-alpha.
|
|
33
|
+
"@theholocron/env-utils": "^1.17.0-alpha.6"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/node": "^26",
|
|
@@ -44,20 +44,20 @@
|
|
|
44
44
|
"tsx": "4.23.15",
|
|
45
45
|
"typescript": "^6.0.3",
|
|
46
46
|
"vitest": "^5.1.0",
|
|
47
|
-
"@theholocron/
|
|
48
|
-
"@theholocron/
|
|
47
|
+
"@theholocron/cloudflare-client": "^1.58.0-alpha.57",
|
|
48
|
+
"@theholocron/cli": "5.0.0-alpha.539",
|
|
49
49
|
"@theholocron/eslint-config": "^8.9.0-alpha.22",
|
|
50
|
-
"@theholocron/http-client": "^1.58.0-alpha.
|
|
51
|
-
"@theholocron/kyber": "5.0.0-alpha.
|
|
52
|
-
"@theholocron/misc-utils": "^1.17.0-alpha.
|
|
53
|
-
"@theholocron/
|
|
50
|
+
"@theholocron/http-client": "^1.58.0-alpha.57",
|
|
51
|
+
"@theholocron/kyber": "5.0.0-alpha.539",
|
|
52
|
+
"@theholocron/misc-utils": "^1.17.0-alpha.6",
|
|
53
|
+
"@theholocron/tsconfig": "^8.9.0-alpha.22",
|
|
54
54
|
"@theholocron/vitest-config": "^8.9.0-alpha.22",
|
|
55
|
-
"@theholocron/
|
|
55
|
+
"@theholocron/tsdown-config": "^8.9.0-alpha.22"
|
|
56
56
|
},
|
|
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.
|
|
60
|
+
"@theholocron/cli": "5.0.0-alpha.539"
|
|
61
61
|
},
|
|
62
62
|
"peerDependenciesMeta": {
|
|
63
63
|
"@theholocron/cloudflare-client": {}
|