requester-lb 1.0.1 → 1.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/README.md CHANGED
@@ -1,3 +1,24 @@
1
+ A dependency-free Node.js CLI for making HTTP requests and DNS lookups, runnable via `npx`.
2
+
3
+ ## Flags
4
+
5
+ | Flag | Description | Default |
6
+ |--------------|---------------------------------------------------|---------|
7
+ | `--domain` | Target host (required) | — |
8
+ | `--path` | URL path | `/` |
9
+ | `--method` | HTTP method | `GET` |
10
+ | `--port` | Port number | 443 (TLS) / 80 (plain) |
11
+ | `--no-tls` | Use plain HTTP instead of HTTPS | off |
12
+ | `--insecure` | Skip TLS certificate verification | off |
13
+ | `--header` | Request header, `"Key: Value"` (repeatable) | — |
14
+ | `--body` | Raw request body | — |
15
+ | `--timeout` | Request timeout in ms | `5000` |
16
+ | `--record` | DNS record type: `A`, `AAAA`, `CNAME`, `TXT`, `MX`, `NS`, `SOA` | `A` |
17
+ | `--resolver` | DNS server IP to query directly | system resolver |
18
+ | `--dns-only` | Resolve DNS and exit, skip the HTTP request | off |
19
+
20
+ ---
21
+
1
22
  Package for authors use only.
2
23
 
3
24
  Package purpose
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "requester-lb",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": ".",
5
5
  "type": "commonjs",
6
6
  "main": "index.js",
package/requester.js CHANGED
@@ -2,6 +2,7 @@
2
2
  const { parseArgs } = require('node:util');
3
3
  const http = require('node:http');
4
4
  const https = require('node:https');
5
+ const dns = require('node:dns');
5
6
 
6
7
  const { values } = parseArgs({
7
8
  options: {
@@ -14,48 +15,91 @@ const { values } = parseArgs({
14
15
  header: { type: 'string', multiple: true, default: [] },
15
16
  body: { type: 'string' },
16
17
  timeout: { type: 'string', default: '5000' },
18
+ record: { type: 'string', default: 'A' },
19
+ resolver: { type: 'string' },
20
+ 'dns-only': { type: 'boolean', default: false },
17
21
  },
18
22
  });
19
23
 
20
24
  if (!values.domain) {
21
- console.error('Usage: request --domain <host> [--path /] [--method GET] [--port 443] [--no-tls] [--insecure] [--header "K: V"] [--body "data"] [--timeout 5000]');
25
+ console.error(
26
+ 'Usage: request --domain <host> [--path /] [--method GET] [--port 443] [--no-tls] [--insecure]\n' +
27
+ ' [--header "K: V"] [--body "data"] [--timeout 5000]\n' +
28
+ ' [--record A|AAAA|CNAME|TXT|MX|NS|SOA] [--resolver <ip>] [--dns-only]'
29
+ );
22
30
  process.exit(1);
23
31
  }
24
32
 
25
- const lib = values.tls ? https : http;
26
- const port = values.port ? Number(values.port) : (values.tls ? 443 : 80);
33
+ function resolveDns(domain, recordType, resolverIp) {
34
+ return new Promise((resolve, reject) => {
35
+ const resolverObj = resolverIp ? new dns.Resolver() : dns;
36
+ if (resolverIp) resolverObj.setServers([resolverIp]);
27
37
 
28
- const headers = {};
29
- for (const h of values.header) {
30
- const idx = h.indexOf(':');
31
- if (idx === -1) continue;
32
- headers[h.slice(0, idx).trim()] = h.slice(idx + 1).trim();
38
+ resolverObj.resolve(domain, recordType, (err, records) => {
39
+ if (err) return reject(err);
40
+ resolve(records);
41
+ });
42
+ });
33
43
  }
34
44
 
35
- const options = {
36
- hostname: values.domain,
37
- port,
38
- path: values.path,
39
- method: values.method.toUpperCase(),
40
- headers,
41
- timeout: Number(values.timeout),
42
- ...(values.insecure ? { rejectUnauthorized: false } : {}),
43
- };
44
-
45
- const req = lib.request(options, (res) => {
46
- console.log(`Status: ${res.statusCode} ${res.statusMessage}`);
47
- console.log('Headers:', res.headers);
48
- let data = '';
49
- res.on('data', (chunk) => (data += chunk));
50
- res.on('end', () => console.log('Body:', data));
51
- });
45
+ async function runDns() {
46
+ console.log(`Resolving ${values.domain} (${values.record})${values.resolver ? ` via ${values.resolver}` : ''}...`);
47
+ try {
48
+ const records = await resolveDns(values.domain, values.record, values.resolver);
49
+ console.log('Records:', records);
50
+ return true;
51
+ } catch (err) {
52
+ console.error('DNS error:', err.message);
53
+ return false;
54
+ }
55
+ }
52
56
 
53
- req.on('timeout', () => {
54
- console.error('Request timed out');
55
- req.destroy();
56
- });
57
+ function runHttp() {
58
+ const lib = values.tls ? https : http;
59
+ const port = values.port ? Number(values.port) : (values.tls ? 443 : 80);
60
+
61
+ const headers = {};
62
+ for (const h of values.header) {
63
+ const idx = h.indexOf(':');
64
+ if (idx === -1) continue;
65
+ headers[h.slice(0, idx).trim()] = h.slice(idx + 1).trim();
66
+ }
67
+
68
+ const options = {
69
+ hostname: values.domain,
70
+ port,
71
+ path: values.path,
72
+ method: values.method.toUpperCase(),
73
+ headers,
74
+ timeout: Number(values.timeout),
75
+ ...(values.insecure ? { rejectUnauthorized: false } : {}),
76
+ };
57
77
 
58
- req.on('error', (err) => console.error('Error:', err.message));
78
+ const req = lib.request(options, (res) => {
79
+ console.log(`Status: ${res.statusCode} ${res.statusMessage}`);
80
+ console.log('Headers:', res.headers);
81
+ let data = '';
82
+ res.on('data', (chunk) => (data += chunk));
83
+ res.on('end', () => console.log('Body:', data));
84
+ });
85
+
86
+ req.on('timeout', () => {
87
+ console.error('Request timed out');
88
+ req.destroy();
89
+ });
90
+
91
+ req.on('error', (err) => console.error('Error:', err.message));
92
+
93
+ if (values.body) req.write(values.body);
94
+ req.end();
95
+ }
59
96
 
60
- if (values.body) req.write(values.body);
61
- req.end();
97
+ (async () => {
98
+ const dnsOk = await runDns();
99
+ if (values['dns-only']) return;
100
+ if (!dnsOk) {
101
+ console.error('Skipping HTTP request due to DNS failure.');
102
+ return;
103
+ }
104
+ runHttp();
105
+ })();
package/test.sh ADDED
@@ -0,0 +1 @@
1
+ echo 'test';