@theholocron/holocron-plugin-cloudflare 3.24.3
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/LICENSE +21 -0
- package/README.md +50 -0
- package/dist/index.d.mts +67 -0
- package/dist/index.mjs +123 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Newton Koumantzelis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
<!-- editorconfig-checker-disable-file -->
|
|
2
|
+
|
|
3
|
+
# `@theholocron/holocron-plugin-cloudflare`
|
|
4
|
+
|
|
5
|
+
Cloudflare plugin for [Holocron](../cli). Implements the `dns`
|
|
6
|
+
capability against the [Cloudflare v4 API](https://developers.cloudflare.com/api/).
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
<!-- prettier-ignore -->
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add -D @theholocron/holocron-plugin-cloudflare
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Auth
|
|
17
|
+
|
|
18
|
+
Token resolution order:
|
|
19
|
+
|
|
20
|
+
1. `--token <TOKEN>` flag on the holocron invocation
|
|
21
|
+
2. `HOLOCRON_CLOUDFLARE_TOKEN` env var
|
|
22
|
+
3. `CLOUDFLARE_API_TOKEN` env var (the standard Cloudflare variable name)
|
|
23
|
+
|
|
24
|
+
Generate a scoped API token at **dash.cloudflare.com/profile/api-tokens**
|
|
25
|
+
with `Zone:Read` and `DNS:Edit` permissions.
|
|
26
|
+
|
|
27
|
+
## Config
|
|
28
|
+
|
|
29
|
+
<!-- prettier-ignore -->
|
|
30
|
+
```jsonc
|
|
31
|
+
{
|
|
32
|
+
"providers": {
|
|
33
|
+
"dns": ["cloudflare", { "accountId": "optional-account-id" }],
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
- `accountId` (optional) — Cloudflare account id. Not required for DNS
|
|
40
|
+
operations; needed only if you extend the plugin to tunnel management.
|
|
41
|
+
|
|
42
|
+
## What's implemented
|
|
43
|
+
|
|
44
|
+
| Method | What it does |
|
|
45
|
+
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
46
|
+
| `listRecords` | Lists all DNS records in the zone that contains the given domain. Resolves the zone by walking label-by-label from the full domain to the apex. |
|
|
47
|
+
| `upsertRecord` | Creates a record if none matching `type + name` exists; patches the first match otherwise. When multiple same-type records exist, only the first is updated — use explicit list/delete/create for multi-TXT scenarios (SPF + DKIM). |
|
|
48
|
+
| `deleteRecord` | Deletes a record by id within the zone that contains the given domain. |
|
|
49
|
+
|
|
50
|
+
Zone ids are cached per plugin instance for the lifetime of the process.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { AuthError, Dns, DnsRecord, ResolveTokenInput } from "@theholocron/cli";
|
|
2
|
+
import { CloudflareClient, CloudflareClientOptions, createCloudflareClient } from "@theholocron/cloudflare-client";
|
|
3
|
+
//#region src/auth.d.ts
|
|
4
|
+
declare const resolveToken: (input?: ResolveTokenInput) => string;
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/capabilities/dns.d.ts
|
|
7
|
+
declare class CloudflareDns implements Dns {
|
|
8
|
+
private readonly client;
|
|
9
|
+
readonly key: "dns";
|
|
10
|
+
readonly providerName = "cloudflare";
|
|
11
|
+
private readonly zoneCache;
|
|
12
|
+
constructor(client: CloudflareClient);
|
|
13
|
+
listRecords(domain: string): Promise<DnsRecord[]>;
|
|
14
|
+
upsertRecord(domain: string, record: DnsRecord): Promise<DnsRecord>;
|
|
15
|
+
deleteRecord(domain: string, id: string): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Walk from full domain up to apex to find the Cloudflare zone.
|
|
18
|
+
* E.g. "api.staging.example.com" tries:
|
|
19
|
+
* 1. "api.staging.example.com"
|
|
20
|
+
* 2. "staging.example.com"
|
|
21
|
+
* 3. "example.com"
|
|
22
|
+
*/
|
|
23
|
+
private resolveZone;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/verify-token.d.ts
|
|
27
|
+
interface VerifyTokenSuccess {
|
|
28
|
+
ok: true;
|
|
29
|
+
subject: string;
|
|
30
|
+
}
|
|
31
|
+
interface VerifyTokenFailure {
|
|
32
|
+
ok: false;
|
|
33
|
+
message: string;
|
|
34
|
+
}
|
|
35
|
+
type VerifyTokenResult = VerifyTokenSuccess | VerifyTokenFailure;
|
|
36
|
+
interface VerifyTokenOptions {
|
|
37
|
+
baseUrl?: string;
|
|
38
|
+
fetch?: typeof fetch;
|
|
39
|
+
}
|
|
40
|
+
declare function verifyToken(token: string, opts?: VerifyTokenOptions): Promise<VerifyTokenResult>;
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/index.d.ts
|
|
43
|
+
interface CloudflarePluginOptions extends ResolveTokenInput {
|
|
44
|
+
/**
|
|
45
|
+
* Cloudflare account id. Optional for DNS operations; required only
|
|
46
|
+
* for account-scoped endpoints (tunnels, custom nameservers).
|
|
47
|
+
*/
|
|
48
|
+
accountId?: string;
|
|
49
|
+
/** Override base URL for tests. */
|
|
50
|
+
baseUrl?: string;
|
|
51
|
+
fetch?: typeof fetch;
|
|
52
|
+
}
|
|
53
|
+
interface PluginContext {
|
|
54
|
+
options: CloudflarePluginOptions;
|
|
55
|
+
client: CloudflareClient;
|
|
56
|
+
}
|
|
57
|
+
declare function createContext(options?: CloudflarePluginOptions): PluginContext;
|
|
58
|
+
declare function dns(ctx: PluginContext): Dns;
|
|
59
|
+
declare function createPlugin(options?: CloudflarePluginOptions): {
|
|
60
|
+
name: string;
|
|
61
|
+
capabilities: {
|
|
62
|
+
dns: () => Dns;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
declare const AUTH_HINT: string;
|
|
66
|
+
//#endregion
|
|
67
|
+
export { AUTH_HINT, AuthError, type CloudflareClient, type CloudflareClientOptions, CloudflareDns, CloudflarePluginOptions, PluginContext, type ResolveTokenInput, type VerifyTokenFailure, type VerifyTokenResult, type VerifyTokenSuccess, createCloudflareClient, createContext, createPlugin, dns, resolveToken, verifyToken };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { AuthError, ProviderApiError, createResolveToken } from "@theholocron/cli";
|
|
2
|
+
import { createCloudflareClient } from "@theholocron/cloudflare-client";
|
|
3
|
+
//#region src/auth.ts
|
|
4
|
+
const resolveToken = createResolveToken({
|
|
5
|
+
envName: "HOLOCRON_CLOUDFLARE_TOKEN",
|
|
6
|
+
vendorEnvName: "CLOUDFLARE_API_TOKEN",
|
|
7
|
+
keyringService: "cloudflare",
|
|
8
|
+
errorMessage: "no Cloudflare API token found. Pass --token <TOKEN>, set HOLOCRON_CLOUDFLARE_TOKEN / CLOUDFLARE_API_TOKEN, or run: holocron auth set cloudflare <TOKEN>"
|
|
9
|
+
});
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/capabilities/dns.ts
|
|
12
|
+
var CloudflareDns = class {
|
|
13
|
+
client;
|
|
14
|
+
key = "dns";
|
|
15
|
+
providerName = "cloudflare";
|
|
16
|
+
zoneCache = /* @__PURE__ */ new Map();
|
|
17
|
+
constructor(client) {
|
|
18
|
+
this.client = client;
|
|
19
|
+
}
|
|
20
|
+
async listRecords(domain) {
|
|
21
|
+
const zoneId = await this.resolveZone(domain);
|
|
22
|
+
return (await this.client.dns.list(zoneId)).map(mapRecord);
|
|
23
|
+
}
|
|
24
|
+
async upsertRecord(domain, record) {
|
|
25
|
+
const zoneId = await this.resolveZone(domain);
|
|
26
|
+
const existing = await this.client.dns.list(zoneId, {
|
|
27
|
+
type: record.type,
|
|
28
|
+
name: record.name
|
|
29
|
+
});
|
|
30
|
+
if (existing.length > 0) return mapRecord(await this.client.dns.update(zoneId, existing[0].id, {
|
|
31
|
+
type: record.type,
|
|
32
|
+
name: record.name,
|
|
33
|
+
content: record.content,
|
|
34
|
+
...record.ttl !== void 0 ? { ttl: record.ttl } : {}
|
|
35
|
+
}));
|
|
36
|
+
return mapRecord(await this.client.dns.create(zoneId, {
|
|
37
|
+
type: record.type,
|
|
38
|
+
name: record.name,
|
|
39
|
+
content: record.content,
|
|
40
|
+
...record.ttl !== void 0 ? { ttl: record.ttl } : {}
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
async deleteRecord(domain, id) {
|
|
44
|
+
const zoneId = await this.resolveZone(domain);
|
|
45
|
+
await this.client.dns.delete(zoneId, id);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Walk from full domain up to apex to find the Cloudflare zone.
|
|
49
|
+
* E.g. "api.staging.example.com" tries:
|
|
50
|
+
* 1. "api.staging.example.com"
|
|
51
|
+
* 2. "staging.example.com"
|
|
52
|
+
* 3. "example.com"
|
|
53
|
+
*/
|
|
54
|
+
async resolveZone(domain) {
|
|
55
|
+
const cached = this.zoneCache.get(domain);
|
|
56
|
+
if (cached) return cached;
|
|
57
|
+
const parts = domain.split(".");
|
|
58
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
59
|
+
const candidate = parts.slice(i).join(".");
|
|
60
|
+
const zones = await this.client.zones.list({ name: candidate });
|
|
61
|
+
if (zones.length > 0) {
|
|
62
|
+
this.zoneCache.set(domain, zones[0].id);
|
|
63
|
+
return zones[0].id;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
throw new ProviderApiError(`No Cloudflare zone found for domain: ${domain}`, 404, void 0);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
function mapRecord(raw) {
|
|
70
|
+
return {
|
|
71
|
+
id: raw.id,
|
|
72
|
+
type: raw.type,
|
|
73
|
+
name: raw.name,
|
|
74
|
+
content: raw.content,
|
|
75
|
+
ttl: raw.ttl
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/verify-token.ts
|
|
80
|
+
async function verifyToken(token, opts = {}) {
|
|
81
|
+
const client = createCloudflareClient({
|
|
82
|
+
token,
|
|
83
|
+
baseUrl: opts.baseUrl,
|
|
84
|
+
fetch: opts.fetch
|
|
85
|
+
});
|
|
86
|
+
try {
|
|
87
|
+
const result = await client.tokens.verify();
|
|
88
|
+
return {
|
|
89
|
+
ok: true,
|
|
90
|
+
subject: `token ${result.id} (${result.status})`
|
|
91
|
+
};
|
|
92
|
+
} catch (err) {
|
|
93
|
+
return {
|
|
94
|
+
ok: false,
|
|
95
|
+
message: err instanceof Error ? err.message : String(err)
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/index.ts
|
|
101
|
+
function createContext(options = {}) {
|
|
102
|
+
return {
|
|
103
|
+
options,
|
|
104
|
+
client: createCloudflareClient({
|
|
105
|
+
token: resolveToken(options),
|
|
106
|
+
baseUrl: options.baseUrl,
|
|
107
|
+
fetch: options.fetch
|
|
108
|
+
})
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function dns(ctx) {
|
|
112
|
+
return new CloudflareDns(ctx.client);
|
|
113
|
+
}
|
|
114
|
+
function createPlugin(options = {}) {
|
|
115
|
+
const ctx = createContext(options);
|
|
116
|
+
return {
|
|
117
|
+
name: "@theholocron/holocron-plugin-cloudflare",
|
|
118
|
+
capabilities: { dns: () => dns(ctx) }
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const AUTH_HINT = "create an API token at https://dash.cloudflare.com/profile/api-tokens with Zone:Read and DNS:Edit permissions, then run: holocron auth set cloudflare <TOKEN>";
|
|
122
|
+
//#endregion
|
|
123
|
+
export { AUTH_HINT, AuthError, CloudflareDns, createCloudflareClient, createContext, createPlugin, dns, resolveToken, verifyToken };
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@theholocron/holocron-plugin-cloudflare",
|
|
3
|
+
"version": "3.24.3",
|
|
4
|
+
"description": "Holocron plugin for Cloudflare. Implements the dns capability against Cloudflare's REST API — zone resolution, DNS record management.",
|
|
5
|
+
"homepage": "https://github.com/theholocron/holocron/tree/main/packages/holocron-plugin-cloudflare#readme",
|
|
6
|
+
"bugs": "https://github.com/theholocron/holocron/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/theholocron/holocron.git",
|
|
10
|
+
"directory": "packages/holocron-plugin-cloudflare"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Newton Koumantzelis",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.mjs",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"default": "./dist/index.mjs"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@theholocron/cloudflare-client": "^1.7.1",
|
|
25
|
+
"@theholocron/cli": "3.24.4"
|
|
26
|
+
},
|
|
27
|
+
"peerDependenciesMeta": {
|
|
28
|
+
"@theholocron/cloudflare-client": {
|
|
29
|
+
"optional": false
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@theholocron/cloudflare-client": "^1.7.1",
|
|
34
|
+
"@theholocron/eslint-config": "^7.19.1",
|
|
35
|
+
"@theholocron/tsconfig": "^7.19.1",
|
|
36
|
+
"@theholocron/tsdown-config": "^7.19.1",
|
|
37
|
+
"@theholocron/vitest-config": "^7.19.1",
|
|
38
|
+
"@types/node": "^26",
|
|
39
|
+
"chalk": "^6.0.0",
|
|
40
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
41
|
+
"@vitest/eslint-plugin": "^1.6.27",
|
|
42
|
+
"eslint": "^10.8.1",
|
|
43
|
+
"eslint-plugin-n": "^18.3.0",
|
|
44
|
+
"globals": "^17.9.0",
|
|
45
|
+
"tsdown": "^0.22.14",
|
|
46
|
+
"tsx": "4.22.4",
|
|
47
|
+
"typescript": "^5.9.3",
|
|
48
|
+
"vitest": "^4.1.10",
|
|
49
|
+
"@theholocron/cli": "3.24.4"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"files": [
|
|
55
|
+
"dist",
|
|
56
|
+
"README.md"
|
|
57
|
+
],
|
|
58
|
+
"types": "./dist/index.d.mts",
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsdown",
|
|
61
|
+
"lint": "eslint .",
|
|
62
|
+
"typecheck": "tsc --noEmit",
|
|
63
|
+
"test": "vitest run",
|
|
64
|
+
"test:watch": "vitest",
|
|
65
|
+
"test:coverage": "vitest run --coverage",
|
|
66
|
+
"validate": "tsx scripts/validate.mjs"
|
|
67
|
+
}
|
|
68
|
+
}
|