zgrzyt 2.2.0 → 2.2.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.
- package/lib/cloudflare.js +48 -16
- package/package.json +2 -3
package/lib/cloudflare.js
CHANGED
|
@@ -1,29 +1,66 @@
|
|
|
1
|
-
import got from 'got';
|
|
2
1
|
import makeDebug from 'debug';
|
|
3
2
|
|
|
4
3
|
const debug = makeDebug('zgrzyt:cloudflare');
|
|
5
4
|
|
|
6
5
|
export default client;
|
|
7
6
|
|
|
7
|
+
/* global AbortController, fetch, URLSearchParams */
|
|
8
|
+
|
|
8
9
|
class CloudflareError extends Error {
|
|
9
10
|
constructor(errors) {
|
|
10
11
|
super(`Cloudflare API error: ${errors[0].message}`);
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
function
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
function makeFetch({ token, timeout, retry }) {
|
|
16
|
+
const authorization = `Bearer ${token}`;
|
|
17
|
+
return {
|
|
18
|
+
put: (...args) => retryFetch('PUT', ...args),
|
|
19
|
+
get: (...args) => retryFetch('GET', ...args)
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
async function retryFetch(...args) {
|
|
23
|
+
while (true) {
|
|
24
|
+
try {
|
|
25
|
+
return await doFetch(...args);
|
|
26
|
+
} catch (err) {
|
|
27
|
+
if (--retry <= 0) {
|
|
28
|
+
throw err;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function doFetch(method, command, { searchParams, json }) {
|
|
35
|
+
const controller = new AbortController();
|
|
36
|
+
const id = setTimeout(() => controller.abort(), timeout);
|
|
37
|
+
const url = new URL(command, 'https://api.cloudflare.com/client/v4/');
|
|
38
|
+
if (searchParams) {
|
|
39
|
+
url.search = new URLSearchParams(searchParams);
|
|
40
|
+
}
|
|
41
|
+
const options = {
|
|
42
|
+
method,
|
|
43
|
+
headers: { authorization },
|
|
44
|
+
signal: controller.signal
|
|
45
|
+
};
|
|
46
|
+
if (json) {
|
|
47
|
+
options.headers['Content-Type'] = 'application/json';
|
|
48
|
+
options.body = JSON.stringify(json);
|
|
23
49
|
}
|
|
24
|
-
|
|
50
|
+
const res = await fetch(url, options);
|
|
51
|
+
clearTimeout(id);
|
|
52
|
+
return res.json();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function client({ token, timeout = 4000, retry = 2 }) {
|
|
57
|
+
const cf = makeFetch({ token, timeout, retry });
|
|
25
58
|
const cacheZones = Object.create(null);
|
|
26
59
|
let pZones;
|
|
60
|
+
return {
|
|
61
|
+
switchToService,
|
|
62
|
+
listRecords
|
|
63
|
+
};
|
|
27
64
|
|
|
28
65
|
async function switchToService(zoneName, domain, good) {
|
|
29
66
|
const zone = await getZone(zoneName);
|
|
@@ -128,9 +165,4 @@ function client({ token, timeout = 4000, retry = 2 }) {
|
|
|
128
165
|
|
|
129
166
|
return success;
|
|
130
167
|
}
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
switchToService,
|
|
134
|
-
listRecords
|
|
135
|
-
};
|
|
136
168
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zgrzyt",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "Poor man's load balancing DNS switcher.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"debug": "~4",
|
|
30
|
-
"got": "~14",
|
|
31
30
|
"parse-domain": "~8",
|
|
32
31
|
"rc": "^1.2.8",
|
|
33
32
|
"sprintfjs": "^1.2.16"
|
|
@@ -43,4 +42,4 @@
|
|
|
43
42
|
"lib",
|
|
44
43
|
"bin"
|
|
45
44
|
]
|
|
46
|
-
}
|
|
45
|
+
}
|