@cloud-cli/dns 1.0.0 → 1.0.2
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 +2 -2
- package/dist/index.js +11 -8
- package/dist/index.spec.js +5 -1
- package/package.json +4 -1
- package/src/index.spec.ts +8 -3
- package/src/index.ts +11 -8
package/dist/index.d.ts
CHANGED
|
@@ -10,12 +10,12 @@ interface DomainAndTarget {
|
|
|
10
10
|
}
|
|
11
11
|
declare function add(input: DomainAndTarget): boolean;
|
|
12
12
|
declare function remove(input: DomainAndTarget): boolean;
|
|
13
|
-
declare function
|
|
13
|
+
declare function list(): DomainAndTarget[];
|
|
14
14
|
declare function reload(): Promise<true>;
|
|
15
15
|
declare const _default: {
|
|
16
16
|
add: typeof add;
|
|
17
17
|
remove: typeof remove;
|
|
18
|
-
|
|
18
|
+
list: typeof list;
|
|
19
19
|
reload: typeof reload;
|
|
20
20
|
};
|
|
21
21
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -10,18 +10,18 @@ export class DNS {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
function add(input) {
|
|
13
|
-
let
|
|
13
|
+
let current = list();
|
|
14
14
|
if (!input.target) {
|
|
15
15
|
input.target = '127.0.0.1';
|
|
16
16
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
save(
|
|
17
|
+
current = current.filter(item => item.domain !== input.domain);
|
|
18
|
+
current.push(input);
|
|
19
|
+
save(current);
|
|
20
20
|
return true;
|
|
21
21
|
}
|
|
22
22
|
function remove(input) {
|
|
23
|
-
const
|
|
24
|
-
const newList =
|
|
23
|
+
const current = list();
|
|
24
|
+
const newList = current.filter(item => item.domain !== input.domain);
|
|
25
25
|
save(newList);
|
|
26
26
|
return true;
|
|
27
27
|
}
|
|
@@ -29,7 +29,10 @@ function save(list) {
|
|
|
29
29
|
const txt = list.map(line => 'address=/' + line.domain + '/' + line.target);
|
|
30
30
|
fs.writeFileSync(filePath, txt.join('\n'));
|
|
31
31
|
}
|
|
32
|
-
function
|
|
32
|
+
function list() {
|
|
33
|
+
if (!fs.existsSync(filePath)) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
33
36
|
const input = fs.readFileSync(filePath, 'utf8');
|
|
34
37
|
const entries = input
|
|
35
38
|
.trim()
|
|
@@ -42,4 +45,4 @@ async function reload() {
|
|
|
42
45
|
const cmd = await exec('systemctl', ['restart', 'dnsmasq']);
|
|
43
46
|
return cmd.ok || Promise.reject(new Error('Failed to reload'));
|
|
44
47
|
}
|
|
45
|
-
export default { add, remove,
|
|
48
|
+
export default { add, remove, list, reload };
|
package/dist/index.spec.js
CHANGED
|
@@ -9,9 +9,13 @@ describe('dns', () => {
|
|
|
9
9
|
expect(output).toEqual({ target: '1.2.3.4', domain: 'foo' });
|
|
10
10
|
});
|
|
11
11
|
it('should load entries from file', () => {
|
|
12
|
+
let fileExists = false;
|
|
12
13
|
const buffer = `address=/test/1.2.3.4\naddress=/foo/5.6.7.8`;
|
|
14
|
+
jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
|
|
13
15
|
jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
|
|
14
|
-
|
|
16
|
+
expect(dns.list()).toEqual([]);
|
|
17
|
+
fileExists = true;
|
|
18
|
+
const list = dns.list();
|
|
15
19
|
expect(list).toEqual([
|
|
16
20
|
{ domain: 'test', target: '1.2.3.4' },
|
|
17
21
|
{ domain: 'foo', target: '5.6.7.8' },
|
package/package.json
CHANGED
package/src/index.spec.ts
CHANGED
|
@@ -11,10 +11,15 @@ describe('dns', () => {
|
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
it('should load entries from file', () => {
|
|
14
|
+
let fileExists = false;
|
|
14
15
|
const buffer = `address=/test/1.2.3.4\naddress=/foo/5.6.7.8`;
|
|
16
|
+
jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
|
|
15
17
|
jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
|
|
19
|
+
expect(dns.list()).toEqual([]);
|
|
20
|
+
|
|
21
|
+
fileExists = true;
|
|
22
|
+
const list = dns.list();
|
|
18
23
|
expect(list).toEqual([
|
|
19
24
|
{ domain: 'test', target: '1.2.3.4' },
|
|
20
25
|
{ domain: 'foo', target: '5.6.7.8' },
|
|
@@ -52,4 +57,4 @@ describe('dns', () => {
|
|
|
52
57
|
await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
|
|
53
58
|
});
|
|
54
59
|
});
|
|
55
|
-
});
|
|
60
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -11,28 +11,29 @@ export class DNS {
|
|
|
11
11
|
return { domain: parts[1], target: parts[2] }
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
+
|
|
14
15
|
interface DomainAndTarget {
|
|
15
16
|
domain: string;
|
|
16
17
|
target?: string;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
function add(input: DomainAndTarget) {
|
|
20
|
-
let
|
|
21
|
+
let current = list();
|
|
21
22
|
|
|
22
23
|
if (!input.target) {
|
|
23
24
|
input.target = '127.0.0.1';
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
save(
|
|
27
|
+
current = current.filter(item => item.domain !== input.domain);
|
|
28
|
+
current.push(input);
|
|
29
|
+
save(current);
|
|
29
30
|
|
|
30
31
|
return true;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
function remove(input: DomainAndTarget) {
|
|
34
|
-
const
|
|
35
|
-
const newList =
|
|
35
|
+
const current = list();
|
|
36
|
+
const newList = current.filter(item => item.domain !== input.domain)
|
|
36
37
|
save(newList);
|
|
37
38
|
|
|
38
39
|
return true;
|
|
@@ -43,7 +44,9 @@ function save(list: DomainAndTarget[]) {
|
|
|
43
44
|
fs.writeFileSync(filePath, txt.join('\n'));
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
function
|
|
47
|
+
function list(): DomainAndTarget[] {
|
|
48
|
+
if (!fs.existsSync(filePath)) { return []; }
|
|
49
|
+
|
|
47
50
|
const input = fs.readFileSync(filePath, 'utf8');
|
|
48
51
|
const entries = input
|
|
49
52
|
.trim()
|
|
@@ -59,4 +62,4 @@ async function reload() {
|
|
|
59
62
|
return cmd.ok || Promise.reject(new Error('Failed to reload'));
|
|
60
63
|
}
|
|
61
64
|
|
|
62
|
-
export default { add, remove,
|
|
65
|
+
export default { add, remove, list, reload }
|