@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.
@@ -22,6 +22,9 @@ var writers_exports = {};
22
22
  __export(writers_exports, {
23
23
  DnsWriteError: () => DnsWriteError,
24
24
  cloudflare: () => cloudflare,
25
+ digitalocean: () => digitalocean,
26
+ namecom: () => namecom,
27
+ porkbun: () => porkbun,
25
28
  writers: () => writers
26
29
  });
27
30
  module.exports = __toCommonJS(writers_exports);
@@ -73,10 +76,35 @@ var MESSAGES = {
73
76
  zone_read_missing: () => "That token can't read your domains",
74
77
  dns_edit_missing: () => "That token can't change DNS records",
75
78
  zone_not_found: (p) => `No zone holds ${p.hostname ?? "that name"}`,
76
- conflict: (p) => `${p.hostname ?? "That name"} already has a record (type ${p.type ?? "?"})`,
79
+ conflict: (p) => `${p.hostname ?? "That name"} already has a record${p.type ? ` (type ${p.type})` : ""}`,
77
80
  unavailable: () => "Couldn't reach the DNS host"
78
81
  };
79
82
 
83
+ // src/writers/policy.ts
84
+ function zoneCandidates(hostname) {
85
+ const labels = hostname.split(".");
86
+ const names = [];
87
+ for (let start = 0; start < labels.length - 1; start++) names.push(labels.slice(start).join("."));
88
+ return names;
89
+ }
90
+ var unquoted = (content) => content.replace(/"\s+"/g, "").replace(/^"|"$/g, "");
91
+ var sameValue = (record, existing) => record.type === "CNAME" ? normalizeDnsName(existing.content) === normalizeDnsName(record.value) : unquoted(existing.content) === record.value;
92
+ var isSpf = (text) => /^v=spf1(?:\s|$)/i.test(unquoted(text).trim());
93
+ function blocks(record, existing) {
94
+ if (record.type === "CNAME" || existing.type === "CNAME") return true;
95
+ return existing.type === "TXT" && isSpf(record.value) && isSpf(existing.content);
96
+ }
97
+ function decide(record, existing, hostname) {
98
+ const ours = existing.find((r) => r.type === record.type && sameValue(record, r));
99
+ if (ours) {
100
+ if (record.type === "CNAME" && ours.proxied === true) return { kind: "unproxy", id: ours.id };
101
+ return { kind: "unchanged" };
102
+ }
103
+ const blocker = existing.find((r) => blocks(record, r));
104
+ if (blocker) throw new DnsWriteError("conflict", { hostname, type: blocker.type });
105
+ return { kind: "create" };
106
+ }
107
+
80
108
  // src/writers/cloudflare.ts
81
109
  var API = "https://api.cloudflare.com/client/v4";
82
110
  var TIMEOUT_MS = 1e4;
@@ -162,19 +190,6 @@ function recordsOf(result) {
162
190
  };
163
191
  });
164
192
  }
165
- function zoneCandidates(hostname) {
166
- const labels = hostname.split(".");
167
- const names = [];
168
- for (let start = 0; start < labels.length - 1; start++) names.push(labels.slice(start).join("."));
169
- return names;
170
- }
171
- var unquoted = (content) => content.replace(/"\s+"/g, "").replace(/^"|"$/g, "");
172
- var sameValue = (record, existing) => record.type === "CNAME" ? normalizeDnsName(existing.content) === normalizeDnsName(record.value) : unquoted(existing.content) === record.value;
173
- var isSpf = (text) => /^v=spf1(?:\s|$)/i.test(unquoted(text).trim());
174
- function blocks(record, existing) {
175
- if (record.type === "CNAME" || existing.type === "CNAME") return true;
176
- return existing.type === "TXT" && isSpf(record.value) && isSpf(existing.content);
177
- }
178
193
  var cloudflare = {
179
194
  host: "cloudflare",
180
195
  /** Add the record (DNS only) to the Cloudflare zone that holds its name. An identical record
@@ -203,11 +218,11 @@ var cloudflare = {
203
218
  subject
204
219
  )
205
220
  );
206
- const ours = existing.find((r) => r.type === record.type && sameValue(record, r));
207
- if (ours) {
208
- if (record.type !== "CNAME" || ours.proxied !== true) return "unchanged";
221
+ const decision = decide(record, existing, hostname);
222
+ if (decision.kind === "unchanged") return "unchanged";
223
+ if (decision.kind === "unproxy") {
209
224
  resultOf(
210
- await call(doFetch, token, `/zones/${zoneId}/dns_records/${ours.id}`, {
225
+ await call(doFetch, token, `/zones/${zoneId}/dns_records/${decision.id}`, {
211
226
  method: "PATCH",
212
227
  body: { proxied: false }
213
228
  }),
@@ -216,8 +231,6 @@ var cloudflare = {
216
231
  );
217
232
  return "unproxied";
218
233
  }
219
- const blocker = existing.find((r) => blocks(record, r));
220
- if (blocker) throw new DnsWriteError("conflict", { hostname, type: blocker.type });
221
234
  resultOf(
222
235
  await call(doFetch, token, `/zones/${zoneId}/dns_records`, {
223
236
  method: "POST",
@@ -239,11 +252,304 @@ var cloudflare = {
239
252
  }
240
253
  };
241
254
 
255
+ // src/writers/digitalocean.ts
256
+ var API2 = "https://api.digitalocean.com/v2";
257
+ var TIMEOUT_MS2 = 1e4;
258
+ var MAX_BODY_BYTES2 = 512 * 1024;
259
+ var RECORD_TYPE2 = /^[A-Z0-9]{1,12}$/;
260
+ async function call2(doFetch, token, path, send) {
261
+ const res = await fetchBounded(
262
+ doFetch,
263
+ `${API2}${path}`,
264
+ {
265
+ method: send ? "POST" : "GET",
266
+ headers: {
267
+ Authorization: `Bearer ${token}`,
268
+ Accept: "application/json",
269
+ ...send ? { "Content-Type": "application/json" } : {}
270
+ },
271
+ body: send ? JSON.stringify(send.body) : void 0
272
+ },
273
+ { timeoutMs: TIMEOUT_MS2, maxBytes: MAX_BODY_BYTES2 }
274
+ );
275
+ if (!res) return { status: 0, body: null };
276
+ let json = null;
277
+ try {
278
+ json = JSON.parse(res.text);
279
+ } catch {
280
+ }
281
+ return { status: res.status, body: isObject(json) ? json : null };
282
+ }
283
+ function refusal2(answer, stage, hostname) {
284
+ if (answer.status === 401) return new DnsWriteError("token_invalid");
285
+ if (answer.status === 403)
286
+ return new DnsWriteError(stage === "reading" ? "zone_read_missing" : "dns_edit_missing");
287
+ if (answer.status === 422 && stage === "writing")
288
+ return new DnsWriteError("conflict", { hostname });
289
+ return new DnsWriteError("unavailable");
290
+ }
291
+ function recordsOf2(body) {
292
+ const rows = body?.domain_records;
293
+ if (!Array.isArray(rows)) throw new DnsWriteError("unavailable");
294
+ return rows.map((row) => {
295
+ if (!isObject(row) || typeof row.id !== "number" || typeof row.type !== "string" || typeof row.data !== "string" || !RECORD_TYPE2.test(row.type))
296
+ throw new DnsWriteError("unavailable");
297
+ return { id: String(row.id), type: row.type, content: row.data };
298
+ });
299
+ }
300
+ var digitalocean = {
301
+ host: "digitalocean",
302
+ /** Add the record to the DigitalOcean domain that holds its name. An identical record is left
303
+ * as it is. DigitalOcean has no proxy, so the outcome is `created` or `unchanged`. */
304
+ async addRecord({ token, record, fetch: doFetch = fetch }) {
305
+ const hostname = normalizeDnsName(record.name);
306
+ let zone;
307
+ let existing = [];
308
+ for (const candidate of zoneCandidates(hostname)) {
309
+ const query = new URLSearchParams({ name: hostname, per_page: "200" });
310
+ const answer = await call2(
311
+ doFetch,
312
+ token,
313
+ `/domains/${encodeURIComponent(candidate)}/records?${query}`
314
+ );
315
+ if (answer.status === 200) {
316
+ zone = candidate;
317
+ existing = recordsOf2(answer.body);
318
+ break;
319
+ }
320
+ if (answer.status !== 404) throw refusal2(answer, "reading", hostname);
321
+ }
322
+ if (!zone) throw new DnsWriteError("zone_not_found", { hostname });
323
+ const decision = decide(record, existing, hostname);
324
+ if (decision.kind === "unchanged") return "unchanged";
325
+ const name = hostname === zone ? "@" : hostname.slice(0, -(zone.length + 1));
326
+ const created = await call2(doFetch, token, `/domains/${encodeURIComponent(zone)}/records`, {
327
+ body: {
328
+ type: record.type,
329
+ name,
330
+ data: record.type === "CNAME" ? `${normalizeDnsName(record.value)}.` : record.value,
331
+ // The API lists these as required for a TXT; they mean nothing for one.
332
+ ...record.type === "TXT" ? { flags: null, tag: null } : {}
333
+ }
334
+ });
335
+ if (created.status !== 201) throw refusal2(created, "writing", hostname);
336
+ return "created";
337
+ }
338
+ };
339
+
340
+ // src/writers/namecom.ts
341
+ var API3 = "https://api.name.com/core/v1";
342
+ var TIMEOUT_MS3 = 1e4;
343
+ var MAX_BODY_BYTES3 = 512 * 1024;
344
+ var PER_PAGE = 500;
345
+ var MAX_PAGES = 10;
346
+ var TTL = 300;
347
+ var RECORD_TYPE3 = /^[A-Z0-9]{1,12}$/;
348
+ function basicOf(token) {
349
+ const at = token.indexOf(":");
350
+ return at > 0 && at < token.length - 1 && !/\s/.test(token) ? btoa(token) : null;
351
+ }
352
+ async function call3(doFetch, basic, path, send) {
353
+ const res = await fetchBounded(
354
+ doFetch,
355
+ `${API3}${path}`,
356
+ {
357
+ method: send ? "POST" : "GET",
358
+ headers: {
359
+ Authorization: `Basic ${basic}`,
360
+ Accept: "application/json",
361
+ ...send ? { "Content-Type": "application/json" } : {}
362
+ },
363
+ body: send ? JSON.stringify(send.body) : void 0
364
+ },
365
+ { timeoutMs: TIMEOUT_MS3, maxBytes: MAX_BODY_BYTES3 }
366
+ );
367
+ if (!res) return { status: 0, body: null };
368
+ let json = null;
369
+ try {
370
+ json = JSON.parse(res.text);
371
+ } catch {
372
+ }
373
+ return { status: res.status, body: isObject(json) ? json : null };
374
+ }
375
+ function refusal3(answer, stage, hostname) {
376
+ if (answer.status === 401) return new DnsWriteError("token_invalid");
377
+ if (answer.status === 403)
378
+ return new DnsWriteError(stage === "reading" ? "zone_read_missing" : "dns_edit_missing");
379
+ if (answer.status === 409) return new DnsWriteError("conflict", { hostname });
380
+ return new DnsWriteError("unavailable");
381
+ }
382
+ function pageOf(body) {
383
+ const rows = body?.records;
384
+ if (!Array.isArray(rows)) throw new DnsWriteError("unavailable");
385
+ const records = rows.map((row) => {
386
+ if (!isObject(row) || typeof row.id !== "number" || typeof row.type !== "string" || typeof row.answer !== "string" || typeof row.fqdn !== "string" || !RECORD_TYPE3.test(row.type))
387
+ throw new DnsWriteError("unavailable");
388
+ return { id: String(row.id), type: row.type, content: row.answer, name: row.fqdn };
389
+ });
390
+ return { records, more: typeof body?.nextPage === "number" };
391
+ }
392
+ var namecom = {
393
+ host: "name-com",
394
+ /** Add the record to the Name.com domain that holds its name. An identical record is left as
395
+ * it is. Name.com has no proxy, so the outcome is `created` or `unchanged`. */
396
+ async addRecord({ token, record, fetch: doFetch = fetch }) {
397
+ const hostname = normalizeDnsName(record.name);
398
+ const basic = basicOf(token);
399
+ if (!basic) throw new DnsWriteError("token_invalid");
400
+ let zone;
401
+ let existing = [];
402
+ for (const candidate of zoneCandidates(hostname)) {
403
+ const path = `/domains/${encodeURIComponent(candidate)}/records`;
404
+ const first = await call3(doFetch, basic, `${path}?perPage=${PER_PAGE}`);
405
+ if (first.status === 404) continue;
406
+ if (first.status !== 200) throw refusal3(first, "reading", hostname);
407
+ zone = candidate;
408
+ let page = pageOf(first.body);
409
+ existing = page.records;
410
+ for (let next = 2; page.more && next <= MAX_PAGES; next++) {
411
+ const answer = await call3(doFetch, basic, `${path}?perPage=${PER_PAGE}&page=${next}`);
412
+ if (answer.status !== 200) throw refusal3(answer, "reading", hostname);
413
+ page = pageOf(answer.body);
414
+ existing = existing.concat(page.records);
415
+ }
416
+ if (page.more) throw new DnsWriteError("unavailable");
417
+ break;
418
+ }
419
+ if (!zone) throw new DnsWriteError("zone_not_found", { hostname });
420
+ const here = existing.filter(
421
+ (r) => r.name !== void 0 && normalizeDnsName(r.name) === hostname
422
+ );
423
+ const decision = decide(record, here, hostname);
424
+ if (decision.kind === "unchanged") return "unchanged";
425
+ const host = hostname === zone ? "@" : hostname.slice(0, -(zone.length + 1));
426
+ const created = await call3(doFetch, basic, `/domains/${encodeURIComponent(zone)}/records`, {
427
+ body: { host, type: record.type, answer: record.value, ttl: TTL }
428
+ });
429
+ if (created.status !== 200) throw refusal3(created, "writing", hostname);
430
+ const stored = created.body?.answer;
431
+ if (typeof stored !== "string" || !sameValue(record, { content: stored }))
432
+ throw new DnsWriteError("unavailable");
433
+ return "created";
434
+ }
435
+ };
436
+
437
+ // src/writers/porkbun.ts
438
+ var API4 = "https://api.porkbun.com/api/json/v3";
439
+ var TIMEOUT_MS4 = 1e4;
440
+ var MAX_BODY_BYTES4 = 256 * 1024;
441
+ var KEY_CODES = /* @__PURE__ */ new Set([
442
+ "API_KEY_REQUIRED",
443
+ "INVALID_API_KEYS_001",
444
+ "INVALID_TOKEN",
445
+ "INVALID_USER"
446
+ ]);
447
+ var ACCESS_CODES = /* @__PURE__ */ new Set(["API_ACCESS_DISABLED", "IP_NOT_ALLOWED", "DOMAIN_NOT_ALLOWED"]);
448
+ var NOT_A_ZONE_CODES = /* @__PURE__ */ new Set(["INVALID_DOMAIN", "DOMAIN_NOT_FOUND"]);
449
+ var ID2 = /^[0-9]{1,20}$/;
450
+ var RECORD_TYPE4 = /^[A-Z0-9]{1,12}$/;
451
+ function keysOf(token) {
452
+ const at = token.indexOf(":");
453
+ const key = token.slice(0, at);
454
+ const secret = token.slice(at + 1);
455
+ return at > 0 && secret && !/\s/.test(token) ? { key, secret } : null;
456
+ }
457
+ async function call4(doFetch, keys, path, send) {
458
+ const res = await fetchBounded(
459
+ doFetch,
460
+ `${API4}${path}`,
461
+ {
462
+ method: send ? "POST" : "GET",
463
+ headers: {
464
+ "X-API-Key": keys.key,
465
+ "X-Secret-API-Key": keys.secret,
466
+ Accept: "application/json",
467
+ ...send ? { "Content-Type": "application/json" } : {}
468
+ },
469
+ body: send ? JSON.stringify(send.body) : void 0
470
+ },
471
+ { timeoutMs: TIMEOUT_MS4, maxBytes: MAX_BODY_BYTES4 }
472
+ );
473
+ if (!res) return { code: "", ok: false, body: null };
474
+ let json = null;
475
+ try {
476
+ json = JSON.parse(res.text);
477
+ } catch {
478
+ }
479
+ const body = isObject(json) ? json : null;
480
+ return {
481
+ code: typeof body?.code === "string" ? body.code : "",
482
+ ok: res.status === 200 && body?.status === "SUCCESS",
483
+ body
484
+ };
485
+ }
486
+ function refusal4(answer, hostname) {
487
+ if (KEY_CODES.has(answer.code)) return new DnsWriteError("token_invalid");
488
+ if (ACCESS_CODES.has(answer.code)) return new DnsWriteError("dns_edit_missing");
489
+ if (answer.code === "RECORD_CONFLICT") return new DnsWriteError("conflict", { hostname });
490
+ return new DnsWriteError("unavailable");
491
+ }
492
+ function recordsOf3(body) {
493
+ const rows = body?.records;
494
+ if (!Array.isArray(rows)) throw new DnsWriteError("unavailable");
495
+ return rows.map((row) => {
496
+ 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))
497
+ throw new DnsWriteError("unavailable");
498
+ return { id: row.id, type: row.type, content: row.content, name: row.name };
499
+ });
500
+ }
501
+ var porkbun = {
502
+ host: "porkbun",
503
+ /** Add the record to the Porkbun domain that holds its name. An identical record is left as
504
+ * it is. Porkbun has no proxy, so the outcome is `created` or `unchanged`. */
505
+ async addRecord({ token, record, comment, fetch: doFetch = fetch }) {
506
+ const hostname = normalizeDnsName(record.name);
507
+ const keys = keysOf(token);
508
+ if (!keys) throw new DnsWriteError("token_invalid");
509
+ let zone;
510
+ let existing = [];
511
+ for (const candidate of zoneCandidates(hostname)) {
512
+ const answer = await call4(doFetch, keys, `/dns/retrieve/${encodeURIComponent(candidate)}`);
513
+ if (answer.ok) {
514
+ zone = candidate;
515
+ existing = recordsOf3(answer.body).filter(
516
+ (r) => r.name !== void 0 && normalizeDnsName(r.name) === hostname
517
+ );
518
+ break;
519
+ }
520
+ if (!NOT_A_ZONE_CODES.has(answer.code)) throw refusal4(answer, hostname);
521
+ }
522
+ if (!zone) throw new DnsWriteError("zone_not_found", { hostname });
523
+ const decision = decide(record, existing, hostname);
524
+ if (decision.kind === "unchanged") return "unchanged";
525
+ const subdomain = hostname === zone ? "" : hostname.slice(0, -(zone.length + 1));
526
+ const created = await call4(doFetch, keys, `/dns/create/${encodeURIComponent(zone)}`, {
527
+ body: {
528
+ name: subdomain,
529
+ type: record.type,
530
+ content: record.value,
531
+ ...comment ? { notes: comment } : {}
532
+ }
533
+ });
534
+ if (created.code === "DUPLICATE_RECORD") return "unchanged";
535
+ if (!created.ok) throw refusal4(created, hostname);
536
+ return "created";
537
+ }
538
+ };
539
+
242
540
  // src/writers/index.ts
243
- var writers = { cloudflare };
541
+ var writers = {
542
+ cloudflare,
543
+ digitalocean,
544
+ "name-com": namecom,
545
+ porkbun
546
+ };
244
547
  // Annotate the CommonJS export names for ESM import in node:
245
548
  0 && (module.exports = {
246
549
  DnsWriteError,
247
550
  cloudflare,
551
+ digitalocean,
552
+ namecom,
553
+ porkbun,
248
554
  writers
249
555
  });
@@ -1,4 +1,5 @@
1
1
  import { D as DnsHostId } from '../hosts-B0_V4Ned.cjs';
2
+ import { F as FetchFn } from '../bounded-fetch-nozW2egX.cjs';
2
3
 
3
4
  interface DnsWriteRecord {
4
5
  type: "CNAME" | "TXT";
@@ -45,13 +46,19 @@ interface DnsWriter {
45
46
  record: DnsWriteRecord;
46
47
  /** Shown beside the record in the host's panel, where the host has such a field. */
47
48
  comment?: string;
48
- fetch?: typeof fetch;
49
+ fetch?: FetchFn;
49
50
  }): Promise<DnsWriteOutcome>;
50
51
  }
51
52
 
52
53
  declare const cloudflare: DnsWriter;
53
54
 
55
+ declare const digitalocean: DnsWriter;
56
+
57
+ declare const namecom: DnsWriter;
58
+
59
+ declare const porkbun: DnsWriter;
60
+
54
61
  /** The hosts that take a pasted token, by host id: `writers[host]` is undefined for the rest. */
55
62
  declare const writers: Partial<Record<DnsHostId, DnsWriter>>;
56
63
 
57
- export { DnsWriteError, type DnsWriteErrorCode, type DnsWriteOutcome, type DnsWriteRecord, type DnsWriter, cloudflare, writers };
64
+ export { DnsWriteError, type DnsWriteErrorCode, type DnsWriteOutcome, type DnsWriteRecord, type DnsWriter, FetchFn, cloudflare, digitalocean, namecom, porkbun, writers };
@@ -1,4 +1,5 @@
1
1
  import { D as DnsHostId } from '../hosts-B0_V4Ned.js';
2
+ import { F as FetchFn } from '../bounded-fetch-nozW2egX.js';
2
3
 
3
4
  interface DnsWriteRecord {
4
5
  type: "CNAME" | "TXT";
@@ -45,13 +46,19 @@ interface DnsWriter {
45
46
  record: DnsWriteRecord;
46
47
  /** Shown beside the record in the host's panel, where the host has such a field. */
47
48
  comment?: string;
48
- fetch?: typeof fetch;
49
+ fetch?: FetchFn;
49
50
  }): Promise<DnsWriteOutcome>;
50
51
  }
51
52
 
52
53
  declare const cloudflare: DnsWriter;
53
54
 
55
+ declare const digitalocean: DnsWriter;
56
+
57
+ declare const namecom: DnsWriter;
58
+
59
+ declare const porkbun: DnsWriter;
60
+
54
61
  /** The hosts that take a pasted token, by host id: `writers[host]` is undefined for the rest. */
55
62
  declare const writers: Partial<Record<DnsHostId, DnsWriter>>;
56
63
 
57
- export { DnsWriteError, type DnsWriteErrorCode, type DnsWriteOutcome, type DnsWriteRecord, type DnsWriter, cloudflare, writers };
64
+ export { DnsWriteError, type DnsWriteErrorCode, type DnsWriteOutcome, type DnsWriteRecord, type DnsWriter, FetchFn, cloudflare, digitalocean, namecom, porkbun, writers };
@@ -1,11 +1,17 @@
1
1
  import {
2
2
  DnsWriteError,
3
3
  cloudflare,
4
+ digitalocean,
5
+ namecom,
6
+ porkbun,
4
7
  writers
5
- } from "../chunk-FGM7S7GQ.js";
8
+ } from "../chunk-56ZUD4EQ.js";
6
9
  import "../chunk-P2O7INDB.js";
7
10
  export {
8
11
  DnsWriteError,
9
12
  cloudflare,
13
+ digitalocean,
14
+ namecom,
15
+ porkbun,
10
16
  writers
11
17
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gusnips/one-click-dns",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "packageManager": "bun@1.4.2",
5
5
  "description": "Put DNS records in a customer's zone: find their DNS host, send them to a signed Domain Connect link, write with an API token, or show the steps for that host",
6
6
  "type": "module",
@@ -79,7 +79,7 @@
79
79
  "access": "public"
80
80
  },
81
81
  "peerDependencies": {
82
- "@gusnips/server": ">=0.15.0"
82
+ "@gusnips/server": ">=0.14.3"
83
83
  },
84
84
  "devDependencies": {
85
85
  "@eslint/js": "^10.0.1",