@cloud-cli/dns 1.0.2 → 1.2.0
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 +13 -5
- package/dist/index.js +24 -14
- package/dist/index.spec.js +23 -3
- package/package.json +2 -1
- package/src/index.spec.ts +32 -5
- package/src/index.ts +36 -19
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
target: any;
|
|
5
|
-
};
|
|
1
|
+
import { init } from '@cloud-cli/cli';
|
|
2
|
+
interface DNSConfig {
|
|
3
|
+
defaultTarget?: string;
|
|
6
4
|
}
|
|
7
5
|
interface DomainAndTarget {
|
|
8
6
|
domain: string;
|
|
@@ -11,11 +9,21 @@ interface DomainAndTarget {
|
|
|
11
9
|
declare function add(input: DomainAndTarget): boolean;
|
|
12
10
|
declare function remove(input: DomainAndTarget): boolean;
|
|
13
11
|
declare function list(): DomainAndTarget[];
|
|
12
|
+
declare function get(options: {
|
|
13
|
+
domain: string;
|
|
14
|
+
}): DomainAndTarget | null;
|
|
14
15
|
declare function reload(): Promise<true>;
|
|
16
|
+
declare function addDnsConfig(options: DNSConfig): void;
|
|
17
|
+
export declare function parseDNSLine(line: string): {
|
|
18
|
+
domain: string;
|
|
19
|
+
target: string;
|
|
20
|
+
};
|
|
15
21
|
declare const _default: {
|
|
16
22
|
add: typeof add;
|
|
17
23
|
remove: typeof remove;
|
|
18
24
|
list: typeof list;
|
|
19
25
|
reload: typeof reload;
|
|
26
|
+
get: typeof get;
|
|
27
|
+
[init]: typeof addDnsConfig;
|
|
20
28
|
};
|
|
21
29
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import { join } from 'path';
|
|
3
3
|
import { exec } from '@cloud-cli/exec';
|
|
4
|
+
import { init } from '@cloud-cli/cli';
|
|
4
5
|
const lineParse = /^address=\/(.+)\/(.+)$/;
|
|
5
|
-
const filePath = join(process.cwd(), 'configuration
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return { domain: parts[1], target: parts[2] };
|
|
10
|
-
}
|
|
11
|
-
}
|
|
6
|
+
const filePath = join(process.cwd(), 'configuration', 'apps.conf');
|
|
7
|
+
const dnsConfig = {
|
|
8
|
+
defaultTarget: '127.0.0.1'
|
|
9
|
+
};
|
|
12
10
|
function add(input) {
|
|
13
11
|
let current = list();
|
|
14
12
|
if (!input.target) {
|
|
15
|
-
input.target =
|
|
13
|
+
input.target = dnsConfig.defaultTarget;
|
|
16
14
|
}
|
|
17
15
|
current = current.filter(item => item.domain !== input.domain);
|
|
18
16
|
current.push(input);
|
|
@@ -25,10 +23,6 @@ function remove(input) {
|
|
|
25
23
|
save(newList);
|
|
26
24
|
return true;
|
|
27
25
|
}
|
|
28
|
-
function save(list) {
|
|
29
|
-
const txt = list.map(line => 'address=/' + line.domain + '/' + line.target);
|
|
30
|
-
fs.writeFileSync(filePath, txt.join('\n'));
|
|
31
|
-
}
|
|
32
26
|
function list() {
|
|
33
27
|
if (!fs.existsSync(filePath)) {
|
|
34
28
|
return [];
|
|
@@ -38,11 +32,27 @@ function list() {
|
|
|
38
32
|
.trim()
|
|
39
33
|
.split('\n')
|
|
40
34
|
.filter(Boolean)
|
|
41
|
-
.map(
|
|
35
|
+
.map(parseDNSLine);
|
|
42
36
|
return entries;
|
|
43
37
|
}
|
|
38
|
+
function get(options) {
|
|
39
|
+
return list().filter(d => d.domain === options.domain)[0] || null;
|
|
40
|
+
}
|
|
44
41
|
async function reload() {
|
|
45
42
|
const cmd = await exec('systemctl', ['restart', 'dnsmasq']);
|
|
46
43
|
return cmd.ok || Promise.reject(new Error('Failed to reload'));
|
|
47
44
|
}
|
|
48
|
-
|
|
45
|
+
function addDnsConfig(options) {
|
|
46
|
+
if (options && options.defaultTarget) {
|
|
47
|
+
dnsConfig.defaultTarget = options.defaultTarget;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function save(list) {
|
|
51
|
+
const txt = list.map(line => `address=/${line.domain}/${line.target}`);
|
|
52
|
+
fs.writeFileSync(filePath, txt.join('\n'));
|
|
53
|
+
}
|
|
54
|
+
export function parseDNSLine(line) {
|
|
55
|
+
const parts = line.match(lineParse);
|
|
56
|
+
return { domain: parts[1], target: parts[2] };
|
|
57
|
+
}
|
|
58
|
+
export default { add, remove, list, reload, get, [init]: addDnsConfig };
|
package/dist/index.spec.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import dns from './index';
|
|
1
|
+
import dns, { parseDNSLine } from './index';
|
|
3
2
|
import fs from 'fs';
|
|
4
3
|
import * as exec from '@cloud-cli/exec';
|
|
4
|
+
import { init } from '@cloud-cli/cli';
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
jest.spyOn(fs, 'writeFileSync').mockImplementation();
|
|
7
|
+
jest.spyOn(fs, 'readFileSync').mockImplementation(() => '');
|
|
8
|
+
});
|
|
5
9
|
describe('dns', () => {
|
|
6
10
|
it('should parse a DNS configuration line', () => {
|
|
7
11
|
const line = 'address=/foo/1.2.3.4';
|
|
8
|
-
const output =
|
|
12
|
+
const output = parseDNSLine(line);
|
|
9
13
|
expect(output).toEqual({ target: '1.2.3.4', domain: 'foo' });
|
|
10
14
|
});
|
|
11
15
|
it('should load entries from file', () => {
|
|
@@ -21,6 +25,15 @@ describe('dns', () => {
|
|
|
21
25
|
{ domain: 'foo', target: '5.6.7.8' },
|
|
22
26
|
]);
|
|
23
27
|
});
|
|
28
|
+
it('should get a DNS entry by domain', () => {
|
|
29
|
+
let fileExists = false;
|
|
30
|
+
const buffer = `address=/test.com/1.2.3.4\naddress=/foo.com/5.6.7.8`;
|
|
31
|
+
jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
|
|
32
|
+
jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
|
|
33
|
+
expect(dns.get({ domain: 'foo.com' })).toBe(null);
|
|
34
|
+
fileExists = true;
|
|
35
|
+
expect(dns.get({ domain: 'foo.com' })).toEqual({ domain: 'foo.com', target: '5.6.7.8' });
|
|
36
|
+
});
|
|
24
37
|
it('should add and remove local DNS entries', () => {
|
|
25
38
|
let text = '';
|
|
26
39
|
jest.spyOn(fs, 'writeFileSync').mockImplementation((_file, value) => { text = value; });
|
|
@@ -44,4 +57,11 @@ describe('dns', () => {
|
|
|
44
57
|
await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
|
|
45
58
|
});
|
|
46
59
|
});
|
|
60
|
+
describe('dns configuration', () => {
|
|
61
|
+
it('should configure the default target', () => {
|
|
62
|
+
dns[init]({ defaultTarget: '1.1.2.2' });
|
|
63
|
+
dns.add({ domain: 'bar' });
|
|
64
|
+
expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), 'address=/bar/1.1.2.2');
|
|
65
|
+
});
|
|
66
|
+
});
|
|
47
67
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloud-cli/dns",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"@cloud-cli/typescript-config": "^1.0.0"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"@cloud-cli/cli": "^1.6.0",
|
|
26
27
|
"@cloud-cli/exec": "^1.0.4"
|
|
27
28
|
}
|
|
28
29
|
}
|
package/src/index.spec.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import dns from './index';
|
|
1
|
+
import dns, { parseDNSLine } from './index';
|
|
3
2
|
import fs from 'fs';
|
|
4
3
|
import * as exec from '@cloud-cli/exec';
|
|
4
|
+
import { init } from '@cloud-cli/cli';
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
jest.spyOn(fs, 'writeFileSync').mockImplementation();
|
|
8
|
+
jest.spyOn(fs, 'readFileSync').mockImplementation(() => '');
|
|
9
|
+
});
|
|
5
10
|
|
|
6
11
|
describe('dns', () => {
|
|
7
12
|
it('should parse a DNS configuration line', () => {
|
|
8
13
|
const line = 'address=/foo/1.2.3.4';
|
|
9
|
-
const output =
|
|
14
|
+
const output = parseDNSLine(line);
|
|
15
|
+
|
|
10
16
|
expect(output).toEqual({ target: '1.2.3.4', domain: 'foo' });
|
|
11
17
|
});
|
|
12
18
|
|
|
@@ -15,9 +21,9 @@ describe('dns', () => {
|
|
|
15
21
|
const buffer = `address=/test/1.2.3.4\naddress=/foo/5.6.7.8`;
|
|
16
22
|
jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
|
|
17
23
|
jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
|
|
18
|
-
|
|
24
|
+
|
|
19
25
|
expect(dns.list()).toEqual([]);
|
|
20
|
-
|
|
26
|
+
|
|
21
27
|
fileExists = true;
|
|
22
28
|
const list = dns.list();
|
|
23
29
|
expect(list).toEqual([
|
|
@@ -26,6 +32,18 @@ describe('dns', () => {
|
|
|
26
32
|
])
|
|
27
33
|
});
|
|
28
34
|
|
|
35
|
+
it('should get a DNS entry by domain', () => {
|
|
36
|
+
let fileExists = false;
|
|
37
|
+
const buffer = `address=/test.com/1.2.3.4\naddress=/foo.com/5.6.7.8`;
|
|
38
|
+
jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
|
|
39
|
+
jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
|
|
40
|
+
|
|
41
|
+
expect(dns.get({ domain: 'foo.com' })).toBe(null);
|
|
42
|
+
|
|
43
|
+
fileExists = true;
|
|
44
|
+
expect(dns.get({ domain: 'foo.com' })).toEqual({ domain: 'foo.com', target: '5.6.7.8' });
|
|
45
|
+
});
|
|
46
|
+
|
|
29
47
|
it('should add and remove local DNS entries', () => {
|
|
30
48
|
let text = '';
|
|
31
49
|
jest.spyOn(fs, 'writeFileSync').mockImplementation((_file, value: string) => { text = value; });
|
|
@@ -57,4 +75,13 @@ describe('dns', () => {
|
|
|
57
75
|
await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
|
|
58
76
|
});
|
|
59
77
|
});
|
|
78
|
+
|
|
79
|
+
describe('dns configuration', () => {
|
|
80
|
+
it('should configure the default target', () => {
|
|
81
|
+
dns[init]({ defaultTarget: '1.1.2.2' });
|
|
82
|
+
dns.add({ domain: 'bar' });
|
|
83
|
+
|
|
84
|
+
expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), 'address=/bar/1.1.2.2');
|
|
85
|
+
});
|
|
86
|
+
})
|
|
60
87
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import { join } from 'path';
|
|
3
3
|
import { exec } from '@cloud-cli/exec';
|
|
4
|
+
import { init } from '@cloud-cli/cli';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export class DNS {
|
|
9
|
-
static parse(line) {
|
|
10
|
-
const parts = line.match(lineParse);
|
|
11
|
-
return { domain: parts[1], target: parts[2] }
|
|
12
|
-
}
|
|
6
|
+
interface DNSConfig {
|
|
7
|
+
defaultTarget?: string;
|
|
13
8
|
}
|
|
14
9
|
|
|
10
|
+
const lineParse = /^address=\/(.+)\/(.+)$/;
|
|
11
|
+
const filePath = join(process.cwd(), 'configuration', 'apps.conf');
|
|
12
|
+
const dnsConfig: DNSConfig = {
|
|
13
|
+
defaultTarget: '127.0.0.1'
|
|
14
|
+
};
|
|
15
|
+
|
|
15
16
|
interface DomainAndTarget {
|
|
16
17
|
domain: string;
|
|
17
18
|
target?: string;
|
|
@@ -21,7 +22,7 @@ function add(input: DomainAndTarget) {
|
|
|
21
22
|
let current = list();
|
|
22
23
|
|
|
23
24
|
if (!input.target) {
|
|
24
|
-
input.target =
|
|
25
|
+
input.target = dnsConfig.defaultTarget;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
current = current.filter(item => item.domain !== input.domain);
|
|
@@ -33,33 +34,49 @@ function add(input: DomainAndTarget) {
|
|
|
33
34
|
|
|
34
35
|
function remove(input: DomainAndTarget) {
|
|
35
36
|
const current = list();
|
|
36
|
-
const newList = current.filter(item => item.domain !== input.domain)
|
|
37
|
+
const newList = current.filter(item => item.domain !== input.domain);
|
|
37
38
|
save(newList);
|
|
38
|
-
|
|
39
39
|
return true;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
function save(list: DomainAndTarget[]) {
|
|
43
|
-
const txt = list.map(line => 'address=/' + line.domain + '/' + line.target);
|
|
44
|
-
fs.writeFileSync(filePath, txt.join('\n'));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
42
|
function list(): DomainAndTarget[] {
|
|
48
|
-
if (!fs.existsSync(filePath)) {
|
|
43
|
+
if (!fs.existsSync(filePath)) {
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
49
46
|
|
|
50
47
|
const input = fs.readFileSync(filePath, 'utf8');
|
|
51
48
|
const entries = input
|
|
52
49
|
.trim()
|
|
53
50
|
.split('\n')
|
|
54
51
|
.filter(Boolean)
|
|
55
|
-
.map(
|
|
52
|
+
.map(parseDNSLine)
|
|
56
53
|
|
|
57
54
|
return entries;
|
|
58
55
|
}
|
|
59
56
|
|
|
57
|
+
function get(options: { domain: string; }): DomainAndTarget | null {
|
|
58
|
+
return list().filter(d => d.domain === options.domain)[0] || null;
|
|
59
|
+
}
|
|
60
|
+
|
|
60
61
|
async function reload() {
|
|
61
62
|
const cmd = await exec('systemctl', ['restart', 'dnsmasq']);
|
|
62
63
|
return cmd.ok || Promise.reject(new Error('Failed to reload'));
|
|
63
64
|
}
|
|
64
65
|
|
|
65
|
-
|
|
66
|
+
function addDnsConfig(options: DNSConfig) {
|
|
67
|
+
if (options && options.defaultTarget) {
|
|
68
|
+
dnsConfig.defaultTarget = options.defaultTarget;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function save(list: DomainAndTarget[]) {
|
|
73
|
+
const txt = list.map(line => `address=/${line.domain}/${line.target}`);
|
|
74
|
+
fs.writeFileSync(filePath, txt.join('\n'));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function parseDNSLine(line: string) {
|
|
78
|
+
const parts = line.match(lineParse);
|
|
79
|
+
return { domain: parts[1], target: parts[2] }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export default { add, remove, list, reload, get, [init]: addDnsConfig }
|