zgrzyt 1.6.0 → 2.0.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/Readme.md CHANGED
@@ -84,8 +84,8 @@ MIT © [Damian Krzeminski](https://pirxpilot.me)
84
84
  [rc]: https://www.npmjs.com/package/rc
85
85
  [Cloudflare DNS]: https://www.cloudflare.com/dns/
86
86
 
87
- [npm-image]: https://img.shields.io/npm/v/zgrzyt.svg
87
+ [npm-image]: https://img.shields.io/npm/v/zgrzyt
88
88
  [npm-url]: https://npmjs.org/package/zgrzyt
89
89
 
90
90
  [build-url]: https://github.com/pirxpilot/zgrzyt/actions/workflows/check.yaml
91
- [build-image]: https://img.shields.io/github/workflow/status/pirxpilot/zgrzyt/check
91
+ [build-image]: https://img.shields.io/github/actions/workflow/status/pirxpilot/zgrzyt/check.yaml?branch=main
package/bin/zgrzyt.cjs ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ import('../index.js');
package/index.js CHANGED
@@ -1,9 +1,10 @@
1
- const conf = require('rc')('zgrzyt');
2
- const prepareConfig = require('./lib/config');
3
- const zgrzyt = require('./lib/zgrzyt');
4
- const report = require('./lib/report');
5
- const { onExit } = require('./lib/state');
1
+ import rc from 'rc';
2
+ import prepareConfig from './lib/config.js';
3
+ import { zgrzyt } from './lib/zgrzyt.js';
4
+ import { report } from './lib/report.js';
5
+ import { onExit } from './lib/state.js';
6
6
 
7
+ const conf = rc('zgrzyt');
7
8
  const apis = prepareConfig(conf);
8
9
 
9
10
  if (!apis) {
@@ -17,7 +18,7 @@ main(apis).catch(e => {
17
18
 
18
19
  async function main(apis) {
19
20
  const results = await Promise.all(apis.map(zgrzyt));
20
- const { exitCode, lines } = report(results);
21
+ const { exitCode, lines } = report(results);
21
22
  console.log(lines.join('\n'));
22
23
  await onExit();
23
24
  process.exit(exitCode);
package/lib/check.js CHANGED
@@ -1,26 +1,29 @@
1
- const http = require('http');
2
- const https = require('https');
3
- const debug = require('debug')('zgrzyt:check');
1
+ import { readFileSync } from 'node:fs';
2
+ import http from 'node:http';
3
+ import https from 'node:https';
4
+ import makeDebug from 'debug';
4
5
 
5
- const { resolve } = require('./dns');
6
- const { updateHealth } = require('./state');
6
+ const debug = makeDebug('zgrzyt:check');
7
+
8
+ import { resolve } from './dns.js';
9
+ import { updateHealth } from './state.js';
7
10
 
8
11
  const {
9
12
  name,
10
13
  version,
11
14
  homepage = 'https://github.com/pirxpilot/zgrzyt'
12
- } = require('../package.json');
15
+ } = JSON.parse(readFileSync(new URL('../package.json', import.meta.url)));
13
16
 
14
17
  const USER_AGENT = `${name}/${version} (${homepage})`;
15
18
 
16
19
  /* global URL */
17
20
 
18
- module.exports = {
21
+ export {
19
22
  checkServices
20
23
  };
21
24
 
22
25
  async function checkService(server, api) {
23
- const address = await resolve(server) ;
26
+ const address = await resolve(server);
24
27
  debug('Resolved %s to %s', server, address);
25
28
  const ok = await checkApi(api, address);
26
29
  const health = await updateHealth(api, server, ok);
@@ -49,7 +52,7 @@ async function checkApi({ url, timeout, method = 'HEAD', headers = {}, retry = 2
49
52
  break;
50
53
  }
51
54
  await waitRandom(timeout);
52
- } while(--retry);
55
+ } while (--retry);
53
56
 
54
57
  return result;
55
58
 
@@ -63,13 +66,13 @@ async function checkApi({ url, timeout, method = 'HEAD', headers = {}, retry = 2
63
66
  ...headers
64
67
  }
65
68
  })
66
- .on('timeout', function () {
67
- debug('Timeout for %s on %s', url, address);
68
- this.destroy(new Error('Request timeout.'));
69
- })
70
- .on('response', res => resolve(isOk(res)))
71
- .on('error', () => resolve(false))
72
- .end());
69
+ .on('timeout', function () {
70
+ debug('Timeout for %s on %s', url, address);
71
+ this.destroy(new Error('Request timeout.'));
72
+ })
73
+ .on('response', res => resolve(isOk(res)))
74
+ .on('error', () => resolve(false))
75
+ .end());
73
76
 
74
77
  function isOk({ statusCode }) {
75
78
  const ok = statusCode >= 200 && statusCode < 300;
@@ -86,6 +89,9 @@ async function checkApi({ url, timeout, method = 'HEAD', headers = {}, retry = 2
86
89
  async function checkServices(servers, selected, api, { force, repair }) {
87
90
  const items = await Promise.all(servers.map(s => checkService(s, api)));
88
91
  const okItems = items.filter(item => item.ok);
92
+ if (okItems.length === 0) {
93
+ return;
94
+ }
89
95
  if (force) {
90
96
  return okItems[0];
91
97
  }
package/lib/cloudflare.js CHANGED
@@ -1,8 +1,9 @@
1
- const got = require('got');
2
- const debug = require('debug')('zgrzyt:cloudflare');
1
+ import got from 'got';
2
+ import makeDebug from 'debug';
3
3
 
4
+ const debug = makeDebug('zgrzyt:cloudflare');
4
5
 
5
- module.exports = client;
6
+ export default client;
6
7
 
7
8
  class CloudflareError extends Error {
8
9
  constructor(errors) {
package/lib/config.js CHANGED
@@ -1,7 +1,7 @@
1
- const { parseDomain, fromUrl } = require('parse-domain');
2
- const makeClient = require('./cloudflare');
1
+ import { parseDomain, fromUrl } from 'parse-domain';
2
+ import makeClient from './cloudflare.js';
3
3
 
4
- module.exports = prepareConfig;
4
+ export default prepareConfig;
5
5
 
6
6
  function getDomainAndZone({ url, domain: apiDomain }) {
7
7
  if (!apiDomain) {
@@ -55,10 +55,10 @@ function prepareConfig(config) {
55
55
  }
56
56
 
57
57
  if (api.header && !Array.isArray(api.header)) {
58
- api.header = [ api.header ];
58
+ api.header = [api.header];
59
59
  }
60
60
  const headers = (api.header || []).reduce((headers, str) => {
61
- const [ name, value ] = str.split('=', 2);
61
+ const [name, value] = str.split('=', 2);
62
62
  headers[name] = value;
63
63
  return headers;
64
64
  }, {});
@@ -80,6 +80,4 @@ function prepareConfig(config) {
80
80
  };
81
81
 
82
82
  }
83
-
84
-
85
83
  }
package/lib/dns.js CHANGED
@@ -1,15 +1,12 @@
1
- const { Resolver } = require('dns').promises;
2
- const resolver = new Resolver();
3
-
4
- const debug = require('debug')('dns:zgrzyt');
1
+ import { Resolver } from 'node:dns/promises';
2
+ import makeDebug from 'debug';
5
3
 
6
- module.exports = {
7
- resolve
8
- };
4
+ const resolver = new Resolver();
5
+ const debug = makeDebug('dns:zgrzyt');
9
6
 
10
7
  const cache = Object.create(null);
11
8
 
12
- function resolve(domain) {
9
+ export function resolve(domain) {
13
10
  let p = cache[domain];
14
11
  if (!p) {
15
12
  cache[domain] = p = doResolve(domain);
package/lib/report.js CHANGED
@@ -1,8 +1,6 @@
1
- const sprintf = require('sprintfjs');
1
+ import sprintf from 'sprintfjs';
2
2
 
3
- module.exports = report;
4
-
5
- function report(results) {
3
+ export function report(results) {
6
4
  const collected = results.reduce(collect, {
7
5
  missing: [],
8
6
  switched: [],
package/lib/state.js CHANGED
@@ -1,9 +1,6 @@
1
- const {
2
- readFile,
3
- writeFile,
4
- } = require('fs').promises;
1
+ import { readFile, writeFile, } from 'node:fs/promises';
5
2
 
6
- module.exports = {
3
+ export {
7
4
  updateHealth,
8
5
  onExit
9
6
  };
package/lib/zgrzyt.js CHANGED
@@ -1,10 +1,7 @@
1
1
 
2
- const { checkServices } = require('./check');
2
+ import { checkServices } from './check.js';
3
3
 
4
- module.exports = zgrzyt;
5
-
6
-
7
- async function zgrzyt({ servers, api, client, force, repair }) {
4
+ export async function zgrzyt({ servers, api, client, force, repair }) {
8
5
  const { domain, zone } = api;
9
6
  const record = await client.listRecords(zone, domain);
10
7
  const good = await checkServices(servers, record.content, api, { force, repair });
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "zgrzyt",
3
- "version": "1.6.0",
3
+ "version": "2.0.0",
4
4
  "description": "Poor man's load balancing DNS switcher.",
5
+ "type": "module",
5
6
  "author": {
6
7
  "name": "Damian Krzeminski",
7
8
  "email": "pirxpilot@furkot.com",
8
9
  "url": "https://pirxpilot.me"
9
10
  },
10
11
  "repository": "pirxpilot/zgrzyt",
11
- "bin": "./bin/zgrzyt",
12
+ "bin": "./bin/zgrzyt.cjs",
12
13
  "license": "MIT",
13
14
  "keywords": [
14
15
  "zgrzyt",
@@ -16,17 +17,18 @@
16
17
  "load balancing",
17
18
  "monitor"
18
19
  ],
20
+ "engines": {
21
+ "node": ">= 16"
22
+ },
19
23
  "dependencies": {
20
24
  "debug": "~4",
21
- "got": "^11.8.0",
22
- "parse-domain": "~4",
25
+ "got": "~12",
26
+ "parse-domain": "~7",
23
27
  "rc": "^1.2.8",
24
28
  "sprintfjs": "^1.2.16"
25
29
  },
26
30
  "devDependencies": {
27
- "jshint": "^2.10.2",
28
- "tap-diff": "^0.1.1",
29
- "tape": "~5"
31
+ "jshint": "^2.10.2"
30
32
  },
31
33
  "scripts": {
32
34
  "test": "make check"
package/bin/zgrzyt DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- require('..');