@cloud-cli/dns 2.0.1 → 2.0.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/dist/index.d.ts CHANGED
@@ -1,7 +1,4 @@
1
1
  import { init } from '@cloud-cli/cli';
2
- interface DNSConfig {
3
- defaultTarget?: string;
4
- }
5
2
  interface DomainAndTarget {
6
3
  domain: string;
7
4
  target?: string;
@@ -13,7 +10,7 @@ declare function get(options: {
13
10
  domain: string;
14
11
  }): DomainAndTarget | null;
15
12
  declare function reload(): Promise<true>;
16
- declare function addDnsConfig(options: DNSConfig): void;
13
+ declare function addDnsConfig(): void;
17
14
  export declare function parseDNSLine(line: string): DomainAndTarget[];
18
15
  declare const _default: {
19
16
  add: typeof add;
package/dist/index.js CHANGED
@@ -1,24 +1,24 @@
1
1
  import fs from 'node:fs';
2
2
  import { join } from 'node:path';
3
3
  import { exec } from '@cloud-cli/exec';
4
- import { init } from '@cloud-cli/cli';
4
+ import { getConfig, init } from '@cloud-cli/cli';
5
5
  const filePath = join(process.cwd(), 'configuration', 'hosts.conf');
6
6
  const dnsConfig = {
7
- defaultTarget: '127.0.0.1'
7
+ defaultTarget: '127.0.0.1',
8
8
  };
9
9
  function add(input) {
10
10
  let current = list();
11
11
  if (!input.target) {
12
12
  input.target = dnsConfig.defaultTarget;
13
13
  }
14
- current = current.filter(item => item.domain !== input.domain);
14
+ current = current.filter((item) => item.domain !== input.domain);
15
15
  current.push(input);
16
16
  save(current);
17
17
  return true;
18
18
  }
19
19
  function remove(input) {
20
20
  const current = list();
21
- const newList = current.filter(item => item.domain !== input.domain);
21
+ const newList = current.filter((item) => item.domain !== input.domain);
22
22
  save(newList);
23
23
  return true;
24
24
  }
@@ -27,38 +27,26 @@ function list() {
27
27
  return [];
28
28
  }
29
29
  const input = fs.readFileSync(filePath, 'utf8');
30
- const entries = input
31
- .trim()
32
- .split('\n')
33
- .filter(Boolean)
34
- .flatMap(parseDNSLine);
30
+ const entries = input.trim().split('\n').filter(Boolean).flatMap(parseDNSLine);
35
31
  return entries;
36
32
  }
37
33
  function get(options) {
38
- return list().filter(d => d.domain === options.domain)[0] || null;
34
+ return list().filter((d) => d.domain === options.domain)[0] || null;
39
35
  }
40
36
  async function reload() {
41
37
  const getPid = await exec('pidof', ['dnsmasq']);
42
38
  const cmd = await exec('kill', ['-s', 'HUP', getPid.stdout.trim()]);
43
39
  return cmd.ok || Promise.reject(new Error('Failed to reload'));
44
40
  }
45
- function addDnsConfig(options) {
46
- if (options && options.defaultTarget) {
47
- dnsConfig.defaultTarget = options.defaultTarget;
48
- }
41
+ function addDnsConfig() {
42
+ Object.assign(dnsConfig, getConfig('dns'));
49
43
  }
50
44
  function save(list) {
51
- const targets = list.reduce((all, next) => {
52
- var _a;
53
- all[_a = next.target] || (all[_a] = []);
54
- all[next.target].push(next.domain);
55
- return all;
56
- }, {});
57
- const txt = Object.entries(targets).map(([ip, domains]) => `${ip} ${domains.join(' ')}`);
58
- fs.writeFileSync(filePath, txt.join('\n').trim());
45
+ let lines = list.map(next => next.target + ' ' + next.domain);
46
+ fs.writeFileSync(filePath, lines.join('\n').trim());
59
47
  }
60
48
  export function parseDNSLine(line) {
61
49
  const [ip, ...domains] = line.split(/\s+/);
62
- return domains.map(d => ({ domain: d, target: ip }));
50
+ return domains.map((d) => ({ domain: d, target: ip }));
63
51
  }
64
52
  export default { add, remove, list, reload, get, [init]: addDnsConfig };
@@ -4,9 +4,17 @@ import { init } from '@cloud-cli/cli';
4
4
  import { describe, it, expect, vi, beforeEach } from 'vitest';
5
5
  const execMocks = vi.hoisted(() => ({
6
6
  exec: vi.fn(),
7
+ getConfig: vi.fn(),
7
8
  }));
8
9
  vi.mock('get-port', () => ({ default: vi.fn().mockReturnValue(1234) }));
9
10
  vi.mock('@cloud-cli/exec', () => ({ exec: execMocks.exec }));
11
+ vi.mock('@cloud-cli/cli', async (original) => {
12
+ const mod = await original();
13
+ return {
14
+ ...mod,
15
+ getConfig: execMocks.getConfig,
16
+ };
17
+ });
10
18
  beforeEach(() => {
11
19
  vi.spyOn(fs, 'writeFileSync').mockImplementation(() => { });
12
20
  vi.spyOn(fs, 'readFileSync').mockImplementation(() => '');
@@ -51,12 +59,11 @@ describe('dns', () => {
51
59
  dns.add({ domain: 'foo', target: '2.3.4.5' });
52
60
  dns.add({ domain: 'bar', target: '2.3.4.5' });
53
61
  dns.add({ domain: 'baz' });
54
- const lines = ['2.3.4.5 foo bar', '127.0.0.1 baz'];
55
62
  expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo');
56
- expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo bar');
57
- expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), lines.join('\n'));
63
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo\n2.3.4.5 bar');
64
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo\n2.3.4.5 bar\n127.0.0.1 baz');
58
65
  dns.remove({ domain: 'baz' });
59
- expect(text).toBe(lines[0]);
66
+ expect(text).toBe('2.3.4.5 foo\n2.3.4.5 bar');
60
67
  });
61
68
  describe('reload', () => {
62
69
  it('should reload the DNS service', async () => {
@@ -74,7 +81,8 @@ describe('dns', () => {
74
81
  });
75
82
  describe('dns configuration', () => {
76
83
  it('should configure the default target', () => {
77
- dns[init]({ defaultTarget: '1.1.2.2' });
84
+ execMocks.getConfig.mockReturnValue({ defaultTarget: '1.1.2.2' });
85
+ dns[init]();
78
86
  dns.add({ domain: 'bar' });
79
87
  expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '1.1.2.2 bar');
80
88
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dns",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
package/src/index.spec.ts CHANGED
@@ -5,10 +5,18 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
5
5
 
6
6
  const execMocks = vi.hoisted(() => ({
7
7
  exec: vi.fn(),
8
+ getConfig: vi.fn(),
8
9
  }));
9
10
 
10
11
  vi.mock('get-port', () => ({ default: vi.fn().mockReturnValue(1234) }));
11
12
  vi.mock('@cloud-cli/exec', () => ({ exec: execMocks.exec }));
13
+ vi.mock('@cloud-cli/cli', async (original) => {
14
+ const mod: any = await original();
15
+ return {
16
+ ...mod,
17
+ getConfig: execMocks.getConfig,
18
+ };
19
+ });
12
20
 
13
21
  beforeEach(() => {
14
22
  vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
@@ -65,14 +73,12 @@ describe('dns', () => {
65
73
  dns.add({ domain: 'bar', target: '2.3.4.5' });
66
74
  dns.add({ domain: 'baz' });
67
75
 
68
- const lines = ['2.3.4.5 foo bar', '127.0.0.1 baz'];
69
-
70
76
  expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo');
71
- expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo bar');
72
- expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), lines.join('\n'));
77
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo\n2.3.4.5 bar');
78
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '2.3.4.5 foo\n2.3.4.5 bar\n127.0.0.1 baz');
73
79
 
74
80
  dns.remove({ domain: 'baz' });
75
- expect(text).toBe(lines[0]);
81
+ expect(text).toBe('2.3.4.5 foo\n2.3.4.5 bar');
76
82
  });
77
83
 
78
84
  describe('reload', () => {
@@ -95,7 +101,8 @@ describe('dns', () => {
95
101
 
96
102
  describe('dns configuration', () => {
97
103
  it('should configure the default target', () => {
98
- dns[init]({ defaultTarget: '1.1.2.2' });
104
+ execMocks.getConfig.mockReturnValue({ defaultTarget: '1.1.2.2' });
105
+ dns[init]();
99
106
  dns.add({ domain: 'bar' });
100
107
 
101
108
  expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '1.1.2.2 bar');
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from 'node:fs';
2
2
  import { join } from 'node:path';
3
3
  import { exec } from '@cloud-cli/exec';
4
- import { init } from '@cloud-cli/cli';
4
+ import { getConfig, init } from '@cloud-cli/cli';
5
5
 
6
6
  interface DNSConfig {
7
7
  defaultTarget?: string;
@@ -9,7 +9,7 @@ interface DNSConfig {
9
9
 
10
10
  const filePath = join(process.cwd(), 'configuration', 'hosts.conf');
11
11
  const dnsConfig: DNSConfig = {
12
- defaultTarget: '127.0.0.1'
12
+ defaultTarget: '127.0.0.1',
13
13
  };
14
14
 
15
15
  interface DomainAndTarget {
@@ -24,7 +24,7 @@ function add(input: DomainAndTarget) {
24
24
  input.target = dnsConfig.defaultTarget;
25
25
  }
26
26
 
27
- current = current.filter(item => item.domain !== input.domain);
27
+ current = current.filter((item) => item.domain !== input.domain);
28
28
  current.push(input);
29
29
  save(current);
30
30
 
@@ -33,7 +33,7 @@ function add(input: DomainAndTarget) {
33
33
 
34
34
  function remove(input: DomainAndTarget) {
35
35
  const current = list();
36
- const newList = current.filter(item => item.domain !== input.domain);
36
+ const newList = current.filter((item) => item.domain !== input.domain);
37
37
  save(newList);
38
38
  return true;
39
39
  }
@@ -44,17 +44,13 @@ function list(): DomainAndTarget[] {
44
44
  }
45
45
 
46
46
  const input = fs.readFileSync(filePath, 'utf8');
47
- const entries = input
48
- .trim()
49
- .split('\n')
50
- .filter(Boolean)
51
- .flatMap(parseDNSLine)
47
+ const entries = input.trim().split('\n').filter(Boolean).flatMap(parseDNSLine);
52
48
 
53
49
  return entries;
54
50
  }
55
51
 
56
- function get(options: { domain: string; }): DomainAndTarget | null {
57
- return list().filter(d => d.domain === options.domain)[0] || null;
52
+ function get(options: { domain: string }): DomainAndTarget | null {
53
+ return list().filter((d) => d.domain === options.domain)[0] || null;
58
54
  }
59
55
 
60
56
  async function reload() {
@@ -63,27 +59,18 @@ async function reload() {
63
59
  return cmd.ok || Promise.reject(new Error('Failed to reload'));
64
60
  }
65
61
 
66
- function addDnsConfig(options: DNSConfig) {
67
- if (options && options.defaultTarget) {
68
- dnsConfig.defaultTarget = options.defaultTarget;
69
- }
62
+ function addDnsConfig() {
63
+ Object.assign(dnsConfig, getConfig('dns'));
70
64
  }
71
65
 
72
66
  function save(list: DomainAndTarget[]) {
73
- const targets: Record<string, string[]> = list.reduce((all, next) => {
74
- all[next.target] ||= [];
75
- all[next.target].push(next.domain);
76
- return all;
77
- }, {});
78
-
79
- const txt = Object.entries(targets).map(([ip, domains]) => `${ip} ${domains.join(' ')}`);
80
-
81
- fs.writeFileSync(filePath, txt.join('\n').trim());
67
+ let lines = list.map(next => next.target + ' ' + next.domain);
68
+ fs.writeFileSync(filePath, lines.join('\n').trim());
82
69
  }
83
70
 
84
71
  export function parseDNSLine(line: string): DomainAndTarget[] {
85
72
  const [ip, ...domains] = line.split(/\s+/);
86
- return domains.map(d => ({ domain: d, target: ip }));
73
+ return domains.map((d) => ({ domain: d, target: ip }));
87
74
  }
88
75
 
89
- export default { add, remove, list, reload, get, [init]: addDnsConfig }
76
+ export default { add, remove, list, reload, get, [init]: addDnsConfig };