@gusnips/one-click-dns 0.1.0 → 0.1.1

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.
@@ -801,10 +801,35 @@ var MESSAGES = {
801
801
  zone_read_missing: () => "That token can't read your domains",
802
802
  dns_edit_missing: () => "That token can't change DNS records",
803
803
  zone_not_found: (p) => `No zone holds ${p.hostname ?? "that name"}`,
804
- conflict: (p) => `${p.hostname ?? "That name"} already has a record (type ${p.type ?? "?"})`,
804
+ conflict: (p) => `${p.hostname ?? "That name"} already has a record${p.type ? ` (type ${p.type})` : ""}`,
805
805
  unavailable: () => "Couldn't reach the DNS host"
806
806
  };
807
807
 
808
+ // src/writers/policy.ts
809
+ function zoneCandidates(hostname) {
810
+ const labels = hostname.split(".");
811
+ const names = [];
812
+ for (let start = 0; start < labels.length - 1; start++) names.push(labels.slice(start).join("."));
813
+ return names;
814
+ }
815
+ var unquoted = (content) => content.replace(/"\s+"/g, "").replace(/^"|"$/g, "");
816
+ var sameValue = (record, existing) => record.type === "CNAME" ? normalizeDnsName(existing.content) === normalizeDnsName(record.value) : unquoted(existing.content) === record.value;
817
+ var isSpf = (text2) => /^v=spf1(?:\s|$)/i.test(unquoted(text2).trim());
818
+ function blocks(record, existing) {
819
+ if (record.type === "CNAME" || existing.type === "CNAME") return true;
820
+ return existing.type === "TXT" && isSpf(record.value) && isSpf(existing.content);
821
+ }
822
+ function decide(record, existing, hostname) {
823
+ const ours = existing.find((r) => r.type === record.type && sameValue(record, r));
824
+ if (ours) {
825
+ if (record.type === "CNAME" && ours.proxied === true) return { kind: "unproxy", id: ours.id };
826
+ return { kind: "unchanged" };
827
+ }
828
+ const blocker = existing.find((r) => blocks(record, r));
829
+ if (blocker) throw new DnsWriteError("conflict", { hostname, type: blocker.type });
830
+ return { kind: "create" };
831
+ }
832
+
808
833
  // src/writers/cloudflare.ts
809
834
  var API = "https://api.cloudflare.com/client/v4";
810
835
  var TIMEOUT_MS = 1e4;
@@ -890,19 +915,6 @@ function recordsOf(result) {
890
915
  };
891
916
  });
892
917
  }
893
- function zoneCandidates(hostname) {
894
- const labels = hostname.split(".");
895
- const names = [];
896
- for (let start = 0; start < labels.length - 1; start++) names.push(labels.slice(start).join("."));
897
- return names;
898
- }
899
- var unquoted = (content) => content.replace(/"\s+"/g, "").replace(/^"|"$/g, "");
900
- var sameValue = (record, existing) => record.type === "CNAME" ? normalizeDnsName(existing.content) === normalizeDnsName(record.value) : unquoted(existing.content) === record.value;
901
- var isSpf = (text2) => /^v=spf1(?:\s|$)/i.test(unquoted(text2).trim());
902
- function blocks(record, existing) {
903
- if (record.type === "CNAME" || existing.type === "CNAME") return true;
904
- return existing.type === "TXT" && isSpf(record.value) && isSpf(existing.content);
905
- }
906
918
  var cloudflare = {
907
919
  host: "cloudflare",
908
920
  /** Add the record (DNS only) to the Cloudflare zone that holds its name. An identical record
@@ -931,11 +943,11 @@ var cloudflare = {
931
943
  subject
932
944
  )
933
945
  );
934
- const ours = existing.find((r) => r.type === record.type && sameValue(record, r));
935
- if (ours) {
936
- if (record.type !== "CNAME" || ours.proxied !== true) return "unchanged";
946
+ const decision = decide(record, existing, hostname);
947
+ if (decision.kind === "unchanged") return "unchanged";
948
+ if (decision.kind === "unproxy") {
937
949
  resultOf(
938
- await call(doFetch, token, `/zones/${zoneId}/dns_records/${ours.id}`, {
950
+ await call(doFetch, token, `/zones/${zoneId}/dns_records/${decision.id}`, {
939
951
  method: "PATCH",
940
952
  body: { proxied: false }
941
953
  }),
@@ -944,8 +956,6 @@ var cloudflare = {
944
956
  );
945
957
  return "unproxied";
946
958
  }
947
- const blocker = existing.find((r) => blocks(record, r));
948
- if (blocker) throw new DnsWriteError("conflict", { hostname, type: blocker.type });
949
959
  resultOf(
950
960
  await call(doFetch, token, `/zones/${zoneId}/dns_records`, {
951
961
  method: "POST",
@@ -967,8 +977,298 @@ var cloudflare = {
967
977
  }
968
978
  };
969
979
 
980
+ // src/writers/digitalocean.ts
981
+ var API2 = "https://api.digitalocean.com/v2";
982
+ var TIMEOUT_MS2 = 1e4;
983
+ var MAX_BODY_BYTES2 = 512 * 1024;
984
+ var RECORD_TYPE2 = /^[A-Z0-9]{1,12}$/;
985
+ async function call2(doFetch, token, path, send) {
986
+ const res = await fetchBounded(
987
+ doFetch,
988
+ `${API2}${path}`,
989
+ {
990
+ method: send ? "POST" : "GET",
991
+ headers: {
992
+ Authorization: `Bearer ${token}`,
993
+ Accept: "application/json",
994
+ ...send ? { "Content-Type": "application/json" } : {}
995
+ },
996
+ body: send ? JSON.stringify(send.body) : void 0
997
+ },
998
+ { timeoutMs: TIMEOUT_MS2, maxBytes: MAX_BODY_BYTES2 }
999
+ );
1000
+ if (!res) return { status: 0, body: null };
1001
+ let json = null;
1002
+ try {
1003
+ json = JSON.parse(res.text);
1004
+ } catch {
1005
+ }
1006
+ return { status: res.status, body: isObject(json) ? json : null };
1007
+ }
1008
+ function refusal2(answer, stage, hostname) {
1009
+ if (answer.status === 401) return new DnsWriteError("token_invalid");
1010
+ if (answer.status === 403)
1011
+ return new DnsWriteError(stage === "reading" ? "zone_read_missing" : "dns_edit_missing");
1012
+ if (answer.status === 422 && stage === "writing")
1013
+ return new DnsWriteError("conflict", { hostname });
1014
+ return new DnsWriteError("unavailable");
1015
+ }
1016
+ function recordsOf2(body) {
1017
+ const rows = body?.domain_records;
1018
+ if (!Array.isArray(rows)) throw new DnsWriteError("unavailable");
1019
+ return rows.map((row) => {
1020
+ if (!isObject(row) || typeof row.id !== "number" || typeof row.type !== "string" || typeof row.data !== "string" || !RECORD_TYPE2.test(row.type))
1021
+ throw new DnsWriteError("unavailable");
1022
+ return { id: String(row.id), type: row.type, content: row.data };
1023
+ });
1024
+ }
1025
+ var digitalocean = {
1026
+ host: "digitalocean",
1027
+ /** Add the record to the DigitalOcean domain that holds its name. An identical record is left
1028
+ * as it is. DigitalOcean has no proxy, so the outcome is `created` or `unchanged`. */
1029
+ async addRecord({ token, record, fetch: doFetch = fetch }) {
1030
+ const hostname = normalizeDnsName(record.name);
1031
+ let zone;
1032
+ let existing = [];
1033
+ for (const candidate of zoneCandidates(hostname)) {
1034
+ const query = new URLSearchParams({ name: hostname, per_page: "200" });
1035
+ const answer = await call2(
1036
+ doFetch,
1037
+ token,
1038
+ `/domains/${encodeURIComponent(candidate)}/records?${query}`
1039
+ );
1040
+ if (answer.status === 200) {
1041
+ zone = candidate;
1042
+ existing = recordsOf2(answer.body);
1043
+ break;
1044
+ }
1045
+ if (answer.status !== 404) throw refusal2(answer, "reading", hostname);
1046
+ }
1047
+ if (!zone) throw new DnsWriteError("zone_not_found", { hostname });
1048
+ const decision = decide(record, existing, hostname);
1049
+ if (decision.kind === "unchanged") return "unchanged";
1050
+ const name = hostname === zone ? "@" : hostname.slice(0, -(zone.length + 1));
1051
+ const created = await call2(doFetch, token, `/domains/${encodeURIComponent(zone)}/records`, {
1052
+ body: {
1053
+ type: record.type,
1054
+ name,
1055
+ data: record.type === "CNAME" ? `${normalizeDnsName(record.value)}.` : record.value,
1056
+ // The API lists these as required for a TXT; they mean nothing for one.
1057
+ ...record.type === "TXT" ? { flags: null, tag: null } : {}
1058
+ }
1059
+ });
1060
+ if (created.status !== 201) throw refusal2(created, "writing", hostname);
1061
+ return "created";
1062
+ }
1063
+ };
1064
+
1065
+ // src/writers/namecom.ts
1066
+ var API3 = "https://api.name.com/core/v1";
1067
+ var TIMEOUT_MS3 = 1e4;
1068
+ var MAX_BODY_BYTES3 = 512 * 1024;
1069
+ var PER_PAGE = 500;
1070
+ var MAX_PAGES = 10;
1071
+ var TTL = 300;
1072
+ var RECORD_TYPE3 = /^[A-Z0-9]{1,12}$/;
1073
+ function basicOf(token) {
1074
+ const at = token.indexOf(":");
1075
+ return at > 0 && at < token.length - 1 && !/\s/.test(token) ? btoa(token) : null;
1076
+ }
1077
+ async function call3(doFetch, basic, path, send) {
1078
+ const res = await fetchBounded(
1079
+ doFetch,
1080
+ `${API3}${path}`,
1081
+ {
1082
+ method: send ? "POST" : "GET",
1083
+ headers: {
1084
+ Authorization: `Basic ${basic}`,
1085
+ Accept: "application/json",
1086
+ ...send ? { "Content-Type": "application/json" } : {}
1087
+ },
1088
+ body: send ? JSON.stringify(send.body) : void 0
1089
+ },
1090
+ { timeoutMs: TIMEOUT_MS3, maxBytes: MAX_BODY_BYTES3 }
1091
+ );
1092
+ if (!res) return { status: 0, body: null };
1093
+ let json = null;
1094
+ try {
1095
+ json = JSON.parse(res.text);
1096
+ } catch {
1097
+ }
1098
+ return { status: res.status, body: isObject(json) ? json : null };
1099
+ }
1100
+ function refusal3(answer, stage, hostname) {
1101
+ if (answer.status === 401) return new DnsWriteError("token_invalid");
1102
+ if (answer.status === 403)
1103
+ return new DnsWriteError(stage === "reading" ? "zone_read_missing" : "dns_edit_missing");
1104
+ if (answer.status === 409) return new DnsWriteError("conflict", { hostname });
1105
+ return new DnsWriteError("unavailable");
1106
+ }
1107
+ function pageOf(body) {
1108
+ const rows = body?.records;
1109
+ if (!Array.isArray(rows)) throw new DnsWriteError("unavailable");
1110
+ const records = rows.map((row) => {
1111
+ if (!isObject(row) || typeof row.id !== "number" || typeof row.type !== "string" || typeof row.answer !== "string" || typeof row.fqdn !== "string" || !RECORD_TYPE3.test(row.type))
1112
+ throw new DnsWriteError("unavailable");
1113
+ return { id: String(row.id), type: row.type, content: row.answer, name: row.fqdn };
1114
+ });
1115
+ return { records, more: typeof body?.nextPage === "number" };
1116
+ }
1117
+ var namecom = {
1118
+ host: "name-com",
1119
+ /** Add the record to the Name.com domain that holds its name. An identical record is left as
1120
+ * it is. Name.com has no proxy, so the outcome is `created` or `unchanged`. */
1121
+ async addRecord({ token, record, fetch: doFetch = fetch }) {
1122
+ const hostname = normalizeDnsName(record.name);
1123
+ const basic = basicOf(token);
1124
+ if (!basic) throw new DnsWriteError("token_invalid");
1125
+ let zone;
1126
+ let existing = [];
1127
+ for (const candidate of zoneCandidates(hostname)) {
1128
+ const path = `/domains/${encodeURIComponent(candidate)}/records`;
1129
+ const first = await call3(doFetch, basic, `${path}?perPage=${PER_PAGE}`);
1130
+ if (first.status === 404) continue;
1131
+ if (first.status !== 200) throw refusal3(first, "reading", hostname);
1132
+ zone = candidate;
1133
+ let page = pageOf(first.body);
1134
+ existing = page.records;
1135
+ for (let next = 2; page.more && next <= MAX_PAGES; next++) {
1136
+ const answer = await call3(doFetch, basic, `${path}?perPage=${PER_PAGE}&page=${next}`);
1137
+ if (answer.status !== 200) throw refusal3(answer, "reading", hostname);
1138
+ page = pageOf(answer.body);
1139
+ existing = existing.concat(page.records);
1140
+ }
1141
+ if (page.more) throw new DnsWriteError("unavailable");
1142
+ break;
1143
+ }
1144
+ if (!zone) throw new DnsWriteError("zone_not_found", { hostname });
1145
+ const here = existing.filter(
1146
+ (r) => r.name !== void 0 && normalizeDnsName(r.name) === hostname
1147
+ );
1148
+ const decision = decide(record, here, hostname);
1149
+ if (decision.kind === "unchanged") return "unchanged";
1150
+ const host = hostname === zone ? "@" : hostname.slice(0, -(zone.length + 1));
1151
+ const created = await call3(doFetch, basic, `/domains/${encodeURIComponent(zone)}/records`, {
1152
+ body: { host, type: record.type, answer: record.value, ttl: TTL }
1153
+ });
1154
+ if (created.status !== 200) throw refusal3(created, "writing", hostname);
1155
+ const stored = created.body?.answer;
1156
+ if (typeof stored !== "string" || !sameValue(record, { content: stored }))
1157
+ throw new DnsWriteError("unavailable");
1158
+ return "created";
1159
+ }
1160
+ };
1161
+
1162
+ // src/writers/porkbun.ts
1163
+ var API4 = "https://api.porkbun.com/api/json/v3";
1164
+ var TIMEOUT_MS4 = 1e4;
1165
+ var MAX_BODY_BYTES4 = 256 * 1024;
1166
+ var KEY_CODES = /* @__PURE__ */ new Set([
1167
+ "API_KEY_REQUIRED",
1168
+ "INVALID_API_KEYS_001",
1169
+ "INVALID_TOKEN",
1170
+ "INVALID_USER"
1171
+ ]);
1172
+ var ACCESS_CODES = /* @__PURE__ */ new Set(["API_ACCESS_DISABLED", "IP_NOT_ALLOWED", "DOMAIN_NOT_ALLOWED"]);
1173
+ var NOT_A_ZONE_CODES = /* @__PURE__ */ new Set(["INVALID_DOMAIN", "DOMAIN_NOT_FOUND"]);
1174
+ var ID2 = /^[0-9]{1,20}$/;
1175
+ var RECORD_TYPE4 = /^[A-Z0-9]{1,12}$/;
1176
+ function keysOf(token) {
1177
+ const at = token.indexOf(":");
1178
+ const key = token.slice(0, at);
1179
+ const secret = token.slice(at + 1);
1180
+ return at > 0 && secret && !/\s/.test(token) ? { key, secret } : null;
1181
+ }
1182
+ async function call4(doFetch, keys, path, send) {
1183
+ const res = await fetchBounded(
1184
+ doFetch,
1185
+ `${API4}${path}`,
1186
+ {
1187
+ method: send ? "POST" : "GET",
1188
+ headers: {
1189
+ "X-API-Key": keys.key,
1190
+ "X-Secret-API-Key": keys.secret,
1191
+ Accept: "application/json",
1192
+ ...send ? { "Content-Type": "application/json" } : {}
1193
+ },
1194
+ body: send ? JSON.stringify(send.body) : void 0
1195
+ },
1196
+ { timeoutMs: TIMEOUT_MS4, maxBytes: MAX_BODY_BYTES4 }
1197
+ );
1198
+ if (!res) return { code: "", ok: false, body: null };
1199
+ let json = null;
1200
+ try {
1201
+ json = JSON.parse(res.text);
1202
+ } catch {
1203
+ }
1204
+ const body = isObject(json) ? json : null;
1205
+ return {
1206
+ code: typeof body?.code === "string" ? body.code : "",
1207
+ ok: res.status === 200 && body?.status === "SUCCESS",
1208
+ body
1209
+ };
1210
+ }
1211
+ function refusal4(answer, hostname) {
1212
+ if (KEY_CODES.has(answer.code)) return new DnsWriteError("token_invalid");
1213
+ if (ACCESS_CODES.has(answer.code)) return new DnsWriteError("dns_edit_missing");
1214
+ if (answer.code === "RECORD_CONFLICT") return new DnsWriteError("conflict", { hostname });
1215
+ return new DnsWriteError("unavailable");
1216
+ }
1217
+ function recordsOf3(body) {
1218
+ const rows = body?.records;
1219
+ if (!Array.isArray(rows)) throw new DnsWriteError("unavailable");
1220
+ return rows.map((row) => {
1221
+ if (!isObject(row) || typeof row.id !== "string" || typeof row.name !== "string" || typeof row.type !== "string" || typeof row.content !== "string" || !ID2.test(row.id) || !RECORD_TYPE4.test(row.type))
1222
+ throw new DnsWriteError("unavailable");
1223
+ return { id: row.id, type: row.type, content: row.content, name: row.name };
1224
+ });
1225
+ }
1226
+ var porkbun = {
1227
+ host: "porkbun",
1228
+ /** Add the record to the Porkbun domain that holds its name. An identical record is left as
1229
+ * it is. Porkbun has no proxy, so the outcome is `created` or `unchanged`. */
1230
+ async addRecord({ token, record, comment, fetch: doFetch = fetch }) {
1231
+ const hostname = normalizeDnsName(record.name);
1232
+ const keys = keysOf(token);
1233
+ if (!keys) throw new DnsWriteError("token_invalid");
1234
+ let zone;
1235
+ let existing = [];
1236
+ for (const candidate of zoneCandidates(hostname)) {
1237
+ const answer = await call4(doFetch, keys, `/dns/retrieve/${encodeURIComponent(candidate)}`);
1238
+ if (answer.ok) {
1239
+ zone = candidate;
1240
+ existing = recordsOf3(answer.body).filter(
1241
+ (r) => r.name !== void 0 && normalizeDnsName(r.name) === hostname
1242
+ );
1243
+ break;
1244
+ }
1245
+ if (!NOT_A_ZONE_CODES.has(answer.code)) throw refusal4(answer, hostname);
1246
+ }
1247
+ if (!zone) throw new DnsWriteError("zone_not_found", { hostname });
1248
+ const decision = decide(record, existing, hostname);
1249
+ if (decision.kind === "unchanged") return "unchanged";
1250
+ const subdomain = hostname === zone ? "" : hostname.slice(0, -(zone.length + 1));
1251
+ const created = await call4(doFetch, keys, `/dns/create/${encodeURIComponent(zone)}`, {
1252
+ body: {
1253
+ name: subdomain,
1254
+ type: record.type,
1255
+ content: record.value,
1256
+ ...comment ? { notes: comment } : {}
1257
+ }
1258
+ });
1259
+ if (created.code === "DUPLICATE_RECORD") return "unchanged";
1260
+ if (!created.ok) throw refusal4(created, hostname);
1261
+ return "created";
1262
+ }
1263
+ };
1264
+
970
1265
  // src/writers/index.ts
971
- var writers = { cloudflare };
1266
+ var writers = {
1267
+ cloudflare,
1268
+ digitalocean,
1269
+ "name-com": namecom,
1270
+ porkbun
1271
+ };
972
1272
 
973
1273
  // src/server/plan.ts
974
1274
  async function planConnection(input, io) {
@@ -1,4 +1,5 @@
1
1
  import { Resolver } from 'node:dns/promises';
2
+ import { F as FetchFn } from '../bounded-fetch-nozW2egX.cjs';
2
3
  import { h as DnsZone, e as DnsHostReading, D as DnsHostId } from '../hosts-B0_V4Ned.cjs';
3
4
 
4
5
  /** What one question came back with. `absent` is DNS answering that nothing is there; `failed`
@@ -56,7 +57,7 @@ declare const CONNECT_PROVIDERS: readonly ConnectProvider[];
56
57
 
57
58
  interface DiscoveryIo {
58
59
  txt: DnsLookups["txt"];
59
- fetch?: typeof fetch;
60
+ fetch?: FetchFn;
60
61
  }
61
62
  type Discovery = {
62
63
  found: true;
@@ -141,4 +142,4 @@ interface PlanInput {
141
142
  }
142
143
  declare function planConnection(input: PlanInput, io: DiscoveryIo & ZoneLookups): Promise<ConnectPlan>;
143
144
 
144
- export { CONNECT_PROVIDERS, type ConnectMethod, type ConnectPlan, type ConnectProvider, type Discovery, type DiscoveryIo, type DnsAnswer, type DnsAsk, type DnsLookups, type DomainConnectIdentity, type PlanInput, type ZoneLookups, discoverConnect, dnsHostOf, planConnection, readDnsZone, signedApplyUrl, systemDnsLookups, systemResolver };
145
+ export { CONNECT_PROVIDERS, type ConnectMethod, type ConnectPlan, type ConnectProvider, type Discovery, type DiscoveryIo, type DnsAnswer, type DnsAsk, type DnsLookups, type DomainConnectIdentity, FetchFn, type PlanInput, type ZoneLookups, discoverConnect, dnsHostOf, planConnection, readDnsZone, signedApplyUrl, systemDnsLookups, systemResolver };
@@ -1,4 +1,5 @@
1
1
  import { Resolver } from 'node:dns/promises';
2
+ import { F as FetchFn } from '../bounded-fetch-nozW2egX.js';
2
3
  import { h as DnsZone, e as DnsHostReading, D as DnsHostId } from '../hosts-B0_V4Ned.js';
3
4
 
4
5
  /** What one question came back with. `absent` is DNS answering that nothing is there; `failed`
@@ -56,7 +57,7 @@ declare const CONNECT_PROVIDERS: readonly ConnectProvider[];
56
57
 
57
58
  interface DiscoveryIo {
58
59
  txt: DnsLookups["txt"];
59
- fetch?: typeof fetch;
60
+ fetch?: FetchFn;
60
61
  }
61
62
  type Discovery = {
62
63
  found: true;
@@ -141,4 +142,4 @@ interface PlanInput {
141
142
  }
142
143
  declare function planConnection(input: PlanInput, io: DiscoveryIo & ZoneLookups): Promise<ConnectPlan>;
143
144
 
144
- export { CONNECT_PROVIDERS, type ConnectMethod, type ConnectPlan, type ConnectProvider, type Discovery, type DiscoveryIo, type DnsAnswer, type DnsAsk, type DnsLookups, type DomainConnectIdentity, type PlanInput, type ZoneLookups, discoverConnect, dnsHostOf, planConnection, readDnsZone, signedApplyUrl, systemDnsLookups, systemResolver };
145
+ export { CONNECT_PROVIDERS, type ConnectMethod, type ConnectPlan, type ConnectProvider, type Discovery, type DiscoveryIo, type DnsAnswer, type DnsAsk, type DnsLookups, type DomainConnectIdentity, FetchFn, type PlanInput, type ZoneLookups, discoverConnect, dnsHostOf, planConnection, readDnsZone, signedApplyUrl, systemDnsLookups, systemResolver };
@@ -5,7 +5,7 @@ import {
5
5
  fetchBounded,
6
6
  isObject,
7
7
  writers
8
- } from "../chunk-FGM7S7GQ.js";
8
+ } from "../chunk-56ZUD4EQ.js";
9
9
  import {
10
10
  normalizeDnsName
11
11
  } from "../chunk-P2O7INDB.js";