raqibo 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +67 -2
- package/bin/raqibo.js +11 -0
- package/package.json +29 -4
- package/src/cli.js +370 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The Raqibo authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,68 @@
|
|
|
1
|
-
#
|
|
1
|
+
# raqibo
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Website checks from a server, for terminals and CI pipelines: uptime, redirects, TLS expiry,
|
|
4
|
+
security headers, domain expiry, email security, broken links and robots.txt - and your
|
|
5
|
+
[Raqibo](https://raqibo.com) monitors as a file you keep in version control.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npx raqibo down example.com
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Node 20 or newer. No dependencies.
|
|
12
|
+
|
|
13
|
+
## Checks
|
|
14
|
+
|
|
15
|
+
No account needed. Each exits **0** when it passes, **1** when it fails, and **2** when it could
|
|
16
|
+
not check at all (a bad argument, the API out of reach, a rate limit), so a pipeline can tell
|
|
17
|
+
"the site is wrong" from "the check did not happen".
|
|
18
|
+
|
|
19
|
+
| Command | Fails when |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| `raqibo down <url>` | the site does not answer, or answers with an error |
|
|
22
|
+
| `raqibo trace <url> [--max-redirects n] [--expect url]` | too many redirects, the wrong end, or a drop from https to http |
|
|
23
|
+
| `raqibo ssl <host> [--min-days 14]` | the certificate is untrusted, for another name, or expires sooner |
|
|
24
|
+
| `raqibo headers <url> [--min-grade C]` | the security headers grade is below the minimum |
|
|
25
|
+
| `raqibo domain <domain> [--min-days 30]` | the registration expires sooner, or is at risk |
|
|
26
|
+
| `raqibo email <domain> [--min-grade C]` | the SPF/DKIM/DMARC/DNSSEC/CAA grade is below the minimum |
|
|
27
|
+
| `raqibo links <url>` | a link or image on the page is broken |
|
|
28
|
+
| `raqibo robots <url>` | robots.txt or a sitemap has a real problem |
|
|
29
|
+
|
|
30
|
+
`--json` prints the API's full answer. `--api <url>` (or `RAQIBO_API_URL`) points at another
|
|
31
|
+
API address.
|
|
32
|
+
|
|
33
|
+
## Monitors as code
|
|
34
|
+
|
|
35
|
+
With an API key from the dashboard (Settings, API keys) in `RAQIBO_API_KEY`:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
raqibo monitors export -o monitors.yaml # every monitor, as YAML
|
|
39
|
+
raqibo monitors apply monitors.yaml --dry-run # what would change
|
|
40
|
+
raqibo monitors apply monitors.yaml # apply it, all or nothing
|
|
41
|
+
raqibo monitors apply monitors.yaml --prune # and delete monitors the file does not list
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The file holds alert destinations, so keep it private. Credentials for the sites checked are
|
|
45
|
+
never in it.
|
|
46
|
+
|
|
47
|
+
## Development
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
cd apps/cli && npm test # node --test, against a fake API
|
|
51
|
+
node bin/raqibo.js --help
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Publishing (the owner)
|
|
55
|
+
|
|
56
|
+
MIT licensed (`LICENSE`). From a clean, up-to-date `main`:
|
|
57
|
+
|
|
58
|
+
```sh
|
|
59
|
+
cd apps/cli
|
|
60
|
+
npm test
|
|
61
|
+
npm login
|
|
62
|
+
npm publish --dry-run # the files listed must be bin/, src/, README.md, LICENSE, package.json
|
|
63
|
+
npm publish
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
After the first publish, set `CLI_PUBLISHED = true` in `apps/web/lib/cli.ts`, which turns on
|
|
67
|
+
`raqibo.com/docs/cli`. For a later release, raise `version` in `package.json` and `VERSION` in
|
|
68
|
+
`src/cli.js` together (a test checks they match), then publish again.
|
package/bin/raqibo.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { main } from "../src/cli.js";
|
|
3
|
+
|
|
4
|
+
const code = await main(process.argv.slice(2), {
|
|
5
|
+
fetch: globalThis.fetch,
|
|
6
|
+
api: process.env.RAQIBO_API_URL,
|
|
7
|
+
key: process.env.RAQIBO_API_KEY?.trim() || undefined,
|
|
8
|
+
out: (text) => process.stdout.write(`${text}\n`),
|
|
9
|
+
err: (text) => process.stderr.write(`${text}\n`),
|
|
10
|
+
});
|
|
11
|
+
process.exitCode = code;
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "raqibo",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Website checks from a server, for terminals and CI: uptime, redirects, TLS, headers, domain, email security, broken links, robots.txt - and monitors as a file.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"raqibo": "bin/raqibo.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"src",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "node --test"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://raqibo.com/docs/cli",
|
|
22
|
+
"keywords": [
|
|
23
|
+
"monitoring",
|
|
24
|
+
"uptime",
|
|
25
|
+
"ssl",
|
|
26
|
+
"security-headers",
|
|
27
|
+
"broken-links",
|
|
28
|
+
"ci"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT"
|
|
31
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// The Raqibo command line: the free checks in a terminal or a CI pipeline, and monitors as a file.
|
|
2
|
+
//
|
|
3
|
+
// Every check exits 0 when it passes, 1 when it fails, and 2 when it could not be run (a bad
|
|
4
|
+
// argument, a refusal, the API out of reach). A pipeline can therefore tell "the site is wrong"
|
|
5
|
+
// from "the check did not happen", and must never mistake the second for the first. The checks
|
|
6
|
+
// need no account; `monitors` needs an API key in RAQIBO_API_KEY.
|
|
7
|
+
//
|
|
8
|
+
// No dependencies: Node's own fetch, argument parsing and test runner.
|
|
9
|
+
|
|
10
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
11
|
+
import { parseArgs } from "node:util";
|
|
12
|
+
|
|
13
|
+
export const VERSION = "0.1.1";
|
|
14
|
+
export const DEFAULT_API = "https://api.raqibo.com";
|
|
15
|
+
|
|
16
|
+
export const PASS = 0;
|
|
17
|
+
export const FAIL = 1;
|
|
18
|
+
export const ERROR = 2;
|
|
19
|
+
|
|
20
|
+
const GRADES = ["A", "B", "C", "D", "F"];
|
|
21
|
+
|
|
22
|
+
export class UsageError extends Error {}
|
|
23
|
+
|
|
24
|
+
/** A refusal or failure from the API, with its own code and message. */
|
|
25
|
+
export class ApiError extends Error {
|
|
26
|
+
constructor(status, code, message, retryAfter) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.status = status;
|
|
29
|
+
this.code = code;
|
|
30
|
+
this.retryAfter = retryAfter;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const HELP = `raqibo ${VERSION} - website checks from a server, for terminals and CI
|
|
35
|
+
|
|
36
|
+
Usage: raqibo <command> [target] [options]
|
|
37
|
+
|
|
38
|
+
Checks (no account needed; exit 0 pass, 1 fail, 2 could not check):
|
|
39
|
+
down <url> Fails when the site does not answer, or answers with an error
|
|
40
|
+
trace <url> Every redirect hop
|
|
41
|
+
--max-redirects <n> Fail on more redirects than this
|
|
42
|
+
--expect <url> Fail unless the chain ends at this address
|
|
43
|
+
headers <url> Security headers grade
|
|
44
|
+
--min-grade <A-F> Fail below this grade (default: C)
|
|
45
|
+
ssl <host> TLS certificate
|
|
46
|
+
--min-days <n> Fail when it expires sooner (default: 14)
|
|
47
|
+
domain <domain> Domain registration
|
|
48
|
+
--min-days <n> Fail when it expires sooner (default: 30)
|
|
49
|
+
email <domain> SPF, DKIM, DMARC, DNSSEC, CAA - one grade
|
|
50
|
+
--min-grade <A-F> Fail below this grade (default: C)
|
|
51
|
+
links <url> Broken links and images on one page
|
|
52
|
+
robots <url> robots.txt and sitemaps
|
|
53
|
+
|
|
54
|
+
Monitors as a file (needs RAQIBO_API_KEY):
|
|
55
|
+
monitors export [-o <file>] Every monitor as YAML (stdout unless -o)
|
|
56
|
+
monitors apply <file> Apply a monitors file, all or nothing
|
|
57
|
+
--dry-run Show what would change, change nothing
|
|
58
|
+
--prune Delete monitors the file does not list
|
|
59
|
+
|
|
60
|
+
Options:
|
|
61
|
+
--json Print the API's answer as JSON
|
|
62
|
+
--api <url> API address (default: ${DEFAULT_API}, or RAQIBO_API_URL)
|
|
63
|
+
-h, --help This help
|
|
64
|
+
-v, --version The version
|
|
65
|
+
`;
|
|
66
|
+
|
|
67
|
+
/** An API key goes only over https, or to this machine: never in clear text to a network. */
|
|
68
|
+
export function safeForKey(api) {
|
|
69
|
+
try {
|
|
70
|
+
const url = new URL(api);
|
|
71
|
+
return url.protocol === "https:" || ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Text from a checked site, made safe to print: a page's own address or a server's message can
|
|
79
|
+
* carry terminal control sequences, which in a build log could hide a failure or fake an "OK".
|
|
80
|
+
*/
|
|
81
|
+
export function printable(text) {
|
|
82
|
+
// eslint-disable-next-line no-control-regex
|
|
83
|
+
return String(text).replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g, "");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** POST/GET to the API; resolves to the parsed body or rejects with ApiError. */
|
|
87
|
+
export async function call(env, path, { method = "GET", body, key, text = false } = {}) {
|
|
88
|
+
const headers = { "User-Agent": `raqibo-cli/${VERSION}`, Accept: text ? "application/yaml" : "application/json" };
|
|
89
|
+
if (body !== undefined) headers["Content-Type"] = "application/json";
|
|
90
|
+
if (key) headers["X-Api-Key"] = key;
|
|
91
|
+
if (key && !safeForKey(env.api)) {
|
|
92
|
+
throw new ApiError(0, "insecure_api", `Refusing to send the API key to ${env.api}: use an https address.`);
|
|
93
|
+
}
|
|
94
|
+
let response;
|
|
95
|
+
try {
|
|
96
|
+
response = await env.fetch(`${env.api}${path}`, {
|
|
97
|
+
method,
|
|
98
|
+
headers,
|
|
99
|
+
// With a key, a redirect is an error: the key must not follow one to another host.
|
|
100
|
+
redirect: key ? "error" : "follow",
|
|
101
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
102
|
+
});
|
|
103
|
+
} catch (cause) {
|
|
104
|
+
throw new ApiError(0, "network_error", `Could not reach ${env.api}: ${cause?.message ?? cause}`);
|
|
105
|
+
}
|
|
106
|
+
if (!response.ok) {
|
|
107
|
+
let payload = null;
|
|
108
|
+
try {
|
|
109
|
+
payload = await response.json();
|
|
110
|
+
} catch {
|
|
111
|
+
// not JSON
|
|
112
|
+
}
|
|
113
|
+
const error = payload?.error ?? payload?.detail?.error;
|
|
114
|
+
let message = error?.message ?? `The API answered HTTP ${response.status}.`;
|
|
115
|
+
if (response.status === 401) message = "The API key was not accepted. Set RAQIBO_API_KEY to a key from the dashboard.";
|
|
116
|
+
if (error?.code === "human_check") {
|
|
117
|
+
message = "This address has run many checks; the API wants a person to confirm on raqibo.com. Try again in an hour.";
|
|
118
|
+
}
|
|
119
|
+
const retry = response.headers.get("retry-after");
|
|
120
|
+
throw new ApiError(response.status, error?.code ?? "http_error", message, retry ? Number(retry) : undefined);
|
|
121
|
+
}
|
|
122
|
+
return text ? response.text() : response.json();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Whether two addresses are the same once written the standard way (a bare host gets its "/"). */
|
|
126
|
+
function sameUrl(a, b) {
|
|
127
|
+
try {
|
|
128
|
+
return new URL(a).href === new URL(b).href;
|
|
129
|
+
} catch {
|
|
130
|
+
return a === b;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function gradeAtLeast(grade, minimum) {
|
|
135
|
+
return GRADES.indexOf(grade) <= GRADES.indexOf(minimum);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function checkGrade(value) {
|
|
139
|
+
const grade = String(value).toUpperCase();
|
|
140
|
+
if (!GRADES.includes(grade)) throw new UsageError(`--min-grade must be one of ${GRADES.join(", ")}.`);
|
|
141
|
+
return grade;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function checkNumber(value, name) {
|
|
145
|
+
const n = Number(value);
|
|
146
|
+
if (!Number.isInteger(n) || n < 0) throw new UsageError(`${name} must be a whole number of 0 or more.`);
|
|
147
|
+
return n;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function need(target, what) {
|
|
151
|
+
if (!target) throw new UsageError(`Give ${what}. See raqibo --help.`);
|
|
152
|
+
return target;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// ------------------------------------------------------------------------------------ checks
|
|
156
|
+
// Each returns { code, lines, answer }: the exit code, what to print, the API's answer.
|
|
157
|
+
|
|
158
|
+
const CHECKS = {
|
|
159
|
+
async down(env, target) {
|
|
160
|
+
const answer = await call(env, "/v1/tools/down-check", { method: "POST", body: { url: need(target, "a website") } });
|
|
161
|
+
const up = answer.verdict === "up";
|
|
162
|
+
const lines = [
|
|
163
|
+
`${up ? "UP" : "DOWN"} ${answer.url}`,
|
|
164
|
+
` ${answer.summary}`,
|
|
165
|
+
answer.status !== null ? ` HTTP ${answer.status}${answer.response_ms !== null ? `, ${Math.round(answer.response_ms)} ms` : ""}` : null,
|
|
166
|
+
];
|
|
167
|
+
return { code: up ? PASS : FAIL, lines, answer };
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
async trace(env, target, options) {
|
|
171
|
+
const answer = await call(env, "/v1/tools/redirect-trace", { method: "POST", body: { url: need(target, "a URL") } });
|
|
172
|
+
const lines = answer.hops.map((hop) => ` ${hop.status} ${hop.url}`);
|
|
173
|
+
const failures = [];
|
|
174
|
+
if (options["max-redirects"] !== undefined) {
|
|
175
|
+
const most = checkNumber(options["max-redirects"], "--max-redirects");
|
|
176
|
+
if (answer.redirect_count > most) failures.push(`${answer.redirect_count} redirects; at most ${most} allowed.`);
|
|
177
|
+
}
|
|
178
|
+
if (options.expect !== undefined && sameUrl(answer.final_url, options.expect) === false) {
|
|
179
|
+
failures.push(`Ends at ${answer.final_url}, not ${options.expect}.`);
|
|
180
|
+
}
|
|
181
|
+
if (answer.downgrades_to_http) failures.push("The chain drops from https to http on the way.");
|
|
182
|
+
lines.unshift(`${failures.length ? "FAIL" : "OK"} ${answer.redirect_count} redirect${answer.redirect_count === 1 ? "" : "s"}, ends at ${answer.final_url} (HTTP ${answer.final_status})`);
|
|
183
|
+
lines.push(...failures.map((f) => ` ! ${f}`));
|
|
184
|
+
return { code: failures.length ? FAIL : PASS, lines, answer };
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
async headers(env, target, options) {
|
|
188
|
+
const minimum = checkGrade(options["min-grade"] ?? "C");
|
|
189
|
+
const answer = await call(env, "/v1/tools/security-headers", { method: "POST", body: { url: need(target, "a URL") } });
|
|
190
|
+
const ok = gradeAtLeast(answer.grade, minimum);
|
|
191
|
+
const lines = [
|
|
192
|
+
`${ok ? "OK" : "FAIL"} grade ${answer.grade} (at least ${minimum} wanted) ${answer.final_url}`,
|
|
193
|
+
...answer.findings.filter((f) => f.status === "fail" || f.status === "warn").map((f) => ` - ${f.title}: ${f.fix ?? f.explanation}`),
|
|
194
|
+
` ${answer.scope_note}`,
|
|
195
|
+
];
|
|
196
|
+
return { code: ok ? PASS : FAIL, lines, answer };
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
async ssl(env, target, options) {
|
|
200
|
+
const minimum = checkNumber(options["min-days"] ?? 14, "--min-days");
|
|
201
|
+
const answer = await call(env, "/v1/tools/tls-inspect", { method: "POST", body: { host: need(target, "a hostname") } });
|
|
202
|
+
const problems = [];
|
|
203
|
+
if (!answer.trusted) problems.push(`Not trusted: ${answer.trust_error ?? "unknown reason"}`);
|
|
204
|
+
if (!answer.hostname_matches) problems.push("The certificate is not for this name.");
|
|
205
|
+
if (answer.expired) problems.push("Expired.");
|
|
206
|
+
else if (answer.days_until_expiry < minimum) problems.push(`Expires in ${answer.days_until_expiry} days; at least ${minimum} wanted.`);
|
|
207
|
+
const lines = [
|
|
208
|
+
`${problems.length ? "FAIL" : "OK"} ${answer.host}:${answer.port} expires in ${answer.days_until_expiry} days`,
|
|
209
|
+
...problems.map((p) => ` ! ${p}`),
|
|
210
|
+
];
|
|
211
|
+
return { code: problems.length ? FAIL : PASS, lines, answer };
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
async domain(env, target, options) {
|
|
215
|
+
const minimum = checkNumber(options["min-days"] ?? 30, "--min-days");
|
|
216
|
+
const answer = await call(env, "/v1/tools/domain-expiry", { method: "POST", body: { domain: need(target, "a domain") } });
|
|
217
|
+
if (answer.verdict === "unsupported" || answer.verdict === "unknown") {
|
|
218
|
+
return { code: ERROR, lines: [`UNKNOWN ${answer.summary}`], answer };
|
|
219
|
+
}
|
|
220
|
+
const days = answer.days_until_expiry;
|
|
221
|
+
const failed =
|
|
222
|
+
["expired", "at_risk", "not_registered"].includes(answer.verdict) || (days !== null && days < minimum);
|
|
223
|
+
const lines = [
|
|
224
|
+
`${failed ? "FAIL" : "OK"} ${answer.domain ?? target}${days !== null ? ` expires in ${days} days` : ""}`,
|
|
225
|
+
` ${answer.summary}`,
|
|
226
|
+
];
|
|
227
|
+
return { code: failed ? FAIL : PASS, lines, answer };
|
|
228
|
+
},
|
|
229
|
+
|
|
230
|
+
async email(env, target, options) {
|
|
231
|
+
const minimum = checkGrade(options["min-grade"] ?? "C");
|
|
232
|
+
const answer = await call(env, "/v1/tools/email-dns", { method: "POST", body: { domain: need(target, "a domain") } });
|
|
233
|
+
if (!answer.grade) return { code: ERROR, lines: [`UNKNOWN ${answer.domain}: no grade (${answer.grade_reason ?? "no answer"})`], answer };
|
|
234
|
+
const ok = gradeAtLeast(answer.grade, minimum);
|
|
235
|
+
const lines = [
|
|
236
|
+
`${ok ? "OK" : "FAIL"} ${answer.domain} grade ${answer.grade} (at least ${minimum} wanted)`,
|
|
237
|
+
answer.grade_reason ? ` ${answer.grade_reason}` : null,
|
|
238
|
+
];
|
|
239
|
+
return { code: ok ? PASS : FAIL, lines, answer };
|
|
240
|
+
},
|
|
241
|
+
|
|
242
|
+
async links(env, target) {
|
|
243
|
+
const answer = await call(env, "/v1/tools/broken-links", { method: "POST", body: { url: need(target, "a page") } });
|
|
244
|
+
if (answer.page_status < 200 || answer.page_status >= 300) {
|
|
245
|
+
// The page did not load, so there were no links to check: that is a failure, not a pass.
|
|
246
|
+
return {
|
|
247
|
+
code: FAIL,
|
|
248
|
+
lines: [`FAIL the page itself answered HTTP ${answer.page_status} ${answer.page_url}`],
|
|
249
|
+
answer,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
const bad = answer.links.filter((l) => l.state === "broken" || l.state === "unreachable");
|
|
253
|
+
const lines = [
|
|
254
|
+
`${bad.length ? "FAIL" : "OK"} ${bad.length} broken of ${answer.checked} checked (${answer.found} found) ${answer.page_url}`,
|
|
255
|
+
...bad.map((l) => ` ${l.status ?? "---"} ${l.url}${l.reason ? ` (${l.reason})` : ""}`),
|
|
256
|
+
];
|
|
257
|
+
if (answer.counts.not_checked) lines.push(` ${answer.counts.not_checked} not checked (limits per check).`);
|
|
258
|
+
return { code: bad.length ? FAIL : PASS, lines, answer };
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
async robots(env, target) {
|
|
262
|
+
const answer = await call(env, "/v1/tools/robots-sitemap", { method: "POST", body: { url: need(target, "a website") } });
|
|
263
|
+
const problems = answer.findings.filter((f) => f.level === "problem");
|
|
264
|
+
const lines = [
|
|
265
|
+
`${problems.length ? "FAIL" : "OK"} ${answer.site} robots.txt ${answer.robots.state}, ${answer.sitemaps.length} sitemap${answer.sitemaps.length === 1 ? "" : "s"} read`,
|
|
266
|
+
...answer.findings.map((f) => ` ${f.level === "good" ? "+" : f.level === "warning" ? "-" : "!"} ${f.text}`),
|
|
267
|
+
];
|
|
268
|
+
return { code: problems.length ? FAIL : PASS, lines, answer };
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
// ---------------------------------------------------------------------------------- monitors
|
|
273
|
+
|
|
274
|
+
async function monitors(env, args, options) {
|
|
275
|
+
const key = env.key;
|
|
276
|
+
if (!key) throw new UsageError("Set RAQIBO_API_KEY to an API key from the dashboard (Settings, API keys).");
|
|
277
|
+
const [action, file] = args;
|
|
278
|
+
if (action === "export") {
|
|
279
|
+
const text = await call(env, "/v1/monitors/export", { key, text: true });
|
|
280
|
+
if (options.output) {
|
|
281
|
+
await (env.writeFile ?? writeFile)(options.output, text);
|
|
282
|
+
return { code: PASS, lines: [`Wrote ${options.output}.`] };
|
|
283
|
+
}
|
|
284
|
+
return { code: PASS, lines: [text.replace(/\n$/, "")] };
|
|
285
|
+
}
|
|
286
|
+
if (action === "apply") {
|
|
287
|
+
const text = await (env.readFile ?? readFile)(need(file, "a monitors file"), "utf8");
|
|
288
|
+
const preview = Boolean(options["dry-run"]);
|
|
289
|
+
const answer = await call(env, "/v1/monitors/apply", {
|
|
290
|
+
method: "POST",
|
|
291
|
+
key,
|
|
292
|
+
body: { text, preview, prune: Boolean(options.prune) },
|
|
293
|
+
});
|
|
294
|
+
const rows = answer.rows.filter((row) => row.action !== "unchanged");
|
|
295
|
+
const lines = [
|
|
296
|
+
answer.refused > 0
|
|
297
|
+
? `REFUSED ${answer.refused} entr${answer.refused === 1 ? "y" : "ies"} cannot be applied, so nothing was.`
|
|
298
|
+
: `${preview ? "WOULD" : "DONE"} ${answer.created} added, ${answer.updated} changed, ${answer.deleted} deleted`,
|
|
299
|
+
...rows.map((row) => ` ${row.action.padEnd(9)} ${row.name || "-"}${row.problem ? ` (${row.problem})` : ""}`),
|
|
300
|
+
];
|
|
301
|
+
return { code: answer.refused > 0 ? FAIL : PASS, lines, answer };
|
|
302
|
+
}
|
|
303
|
+
throw new UsageError("Use: raqibo monitors export [-o file] | raqibo monitors apply <file> [--dry-run] [--prune]");
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// -------------------------------------------------------------------------------------- main
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Run with `argv` (no node/script prefix). `env` carries fetch, the API address, the key and
|
|
310
|
+
* the output streams, so tests can run it without a network. Resolves to the exit code.
|
|
311
|
+
*/
|
|
312
|
+
export async function main(argv, env) {
|
|
313
|
+
let parsed;
|
|
314
|
+
try {
|
|
315
|
+
parsed = parseArgs({
|
|
316
|
+
args: argv,
|
|
317
|
+
allowPositionals: true,
|
|
318
|
+
options: {
|
|
319
|
+
json: { type: "boolean" },
|
|
320
|
+
api: { type: "string" },
|
|
321
|
+
help: { type: "boolean", short: "h" },
|
|
322
|
+
version: { type: "boolean", short: "v" },
|
|
323
|
+
"min-grade": { type: "string" },
|
|
324
|
+
"min-days": { type: "string" },
|
|
325
|
+
"max-redirects": { type: "string" },
|
|
326
|
+
expect: { type: "string" },
|
|
327
|
+
output: { type: "string", short: "o" },
|
|
328
|
+
"dry-run": { type: "boolean" },
|
|
329
|
+
prune: { type: "boolean" },
|
|
330
|
+
},
|
|
331
|
+
});
|
|
332
|
+
} catch (error) {
|
|
333
|
+
env.err(`${error.message}\nSee raqibo --help.`);
|
|
334
|
+
return ERROR;
|
|
335
|
+
}
|
|
336
|
+
const { values: options, positionals } = parsed;
|
|
337
|
+
if (options.version) {
|
|
338
|
+
env.out(VERSION);
|
|
339
|
+
return PASS;
|
|
340
|
+
}
|
|
341
|
+
const [command, ...rest] = positionals;
|
|
342
|
+
if (options.help || !command) {
|
|
343
|
+
env.out(HELP);
|
|
344
|
+
return command || options.help ? PASS : ERROR;
|
|
345
|
+
}
|
|
346
|
+
const run = { ...env, api: (options.api ?? env.api ?? DEFAULT_API).replace(/\/+$/, "") };
|
|
347
|
+
try {
|
|
348
|
+
let result;
|
|
349
|
+
if (command === "monitors") result = await monitors(run, rest, options);
|
|
350
|
+
else if (Object.hasOwn(CHECKS, command)) result = await CHECKS[command](run, rest[0], options);
|
|
351
|
+
else throw new UsageError(`Unknown command "${command}". See raqibo --help.`);
|
|
352
|
+
if (options.json && result.answer !== undefined) env.out(JSON.stringify(result.answer, null, 2));
|
|
353
|
+
else env.out(printable(result.lines.filter((line) => line !== null).join("\n")));
|
|
354
|
+
return result.code;
|
|
355
|
+
} catch (error) {
|
|
356
|
+
if (error instanceof UsageError) {
|
|
357
|
+
env.err(error.message);
|
|
358
|
+
return ERROR;
|
|
359
|
+
}
|
|
360
|
+
if (error instanceof ApiError) {
|
|
361
|
+
const wait = error.retryAfter ? ` Try again in ${error.retryAfter} s.` : "";
|
|
362
|
+
env.err(printable(`Could not check: ${error.message}${wait}`));
|
|
363
|
+
return ERROR;
|
|
364
|
+
}
|
|
365
|
+
// Anything else - a file that is not there, an answer that is not JSON - is also "could
|
|
366
|
+
// not check", never the 1 that means the site failed.
|
|
367
|
+
env.err(printable(`Could not run: ${error?.message ?? error}`));
|
|
368
|
+
return ERROR;
|
|
369
|
+
}
|
|
370
|
+
}
|