@kaynooo/cli-probe 0.4.4 → 0.4.6

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.
Files changed (3) hide show
  1. package/README.md +3 -3
  2. package/dist/probe.js +49 -21
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -22,9 +22,9 @@ bun add -g @kaynooo/cli-probe
22
22
 
23
23
  ## JSON output
24
24
 
25
- One target prints one object, `{target, url, version, status, chain, timings, addresses, tls, certificate, headers, cdn, offers, httpRedirect, warnings}`, unchanged by the other options (`--json` is always the full report). Several targets print an array of those in the order given, an unreachable one being `{target, error}`.
25
+ One target prints one object, `{target, url, version, status, chain, timings, addresses, tls, certificate, headers, cdn, offers, httpRedirect, failedHop, warnings}`, unchanged by the other options (`--json` is always the full report). Several targets print an array of those in the order given, an unreachable one being `{target, error}`.
26
26
 
27
- Redirects are followed hop by hop, at most 10. The HTTP version and timings come from `curl --write-out '%{json}'`, the certificate from `node:tls`, which Bun's fetch exposes neither of. HTTP/3 is spoken only when `curl --version` lists it, otherwise h3 is reported from `alt-svc` alone.
27
+ Redirects are followed hop by hop, at most 10; a redirect to a target that does not answer is kept as `failedHop`, `{url, error}`, with a warning. The HTTP version and timings come from `curl --write-out '%{json}'`, the certificate from `node:tls`, which Bun's fetch exposes neither of. HTTP/3 is spoken only when `curl --version` lists it, otherwise h3 is reported from `alt-svc` alone.
28
28
 
29
29
  Warnings: a certificate with fewer than 14 days left, expired or not trusted, an `http://` twin that does not redirect to https (checked for a bare domain or an https URL on the default port), more than 10 redirects, a site not served over https.
30
30
 
@@ -40,4 +40,4 @@ Warnings: the system resolver behind the zone (`sudo resolvectl flush-caches` dr
40
40
 
41
41
  `--cors <origin>` sends `OPTIONS` with `Origin`, `Access-Control-Request-Method` and `Access-Control-Request-Headers`, the way a browser does, then applies the Fetch standard's checks: a 2xx status, `access-control-allow-origin` being `*` or the origin, `access-control-allow-methods` listing a method other than GET, HEAD or POST (or `*`), `access-control-allow-headers` covering every header (`*` covers all but `authorization`). The method is uppercased when it is one of the six fetch normalizes (DELETE, GET, HEAD, OPTIONS, POST, PUT) and otherwise sent and matched as typed, `patch` needing `patch` listed. `accept`, `accept-language` and `content-language` are left out of the preflight, a browser letting them through unlisted. Warnings: credentials allowed alongside a `*`, which a credentialed request does not get, and the request's origin allowed without `vary: origin`. `--json` is `{target, url, status, request, allowed, reasons, warnings, headers}`. `-H`, `--resolve`, `-k` and `-t` apply to the preflight too.
42
42
 
43
- Exits 0 when every site answered, 1 when one could not be reached, a CORS preflight was refused or no resolver knows a name, 2 on a usage error (a bad URL, header, origin or `--resolve` is refused before any request is made).
43
+ Exits 0 when every site answered, 1 when one could not be reached or one of its redirects led nowhere, a CORS preflight was refused or no resolver knows a name, 2 on a usage error (a bad URL, header, origin or `--resolve` is refused before any request is made).
package/dist/probe.js CHANGED
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env -S bun --no-env-file --config=/dev/null
2
- // @bun
1
+ #!/bin/sh
2
+ // @bun 2>/dev/null; exec bun --no-env-file --config=/dev/null "$0" "$@"
3
3
  var __esm = (fn, res, err) => () => {
4
4
  if (fn)
5
5
  try {
@@ -142,11 +142,14 @@ var init_config = __esm(() => {
142
142
 
143
143
  // ../kit/src/user-config.ts
144
144
  import { homedir } from "os";
145
- import { join as join2 } from "path";
145
+ import { isAbsolute, join as join2 } from "path";
146
146
  import process4 from "process";
147
+ function xdgFolder(value, fallback) {
148
+ return value !== undefined && isAbsolute(value) ? value : fallback;
149
+ }
147
150
  function configBase(where = {}) {
148
151
  const xdg = where.xdg ?? (where.home === undefined ? process4.env.XDG_CONFIG_HOME : undefined);
149
- return xdg !== undefined && xdg !== "" ? xdg : join2(where.home ?? homedir(), ".config");
152
+ return xdgFolder(xdg, join2(where.home ?? homedir(), ".config"));
150
153
  }
151
154
  function userConfigPath(bin, where = {}) {
152
155
  return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
@@ -572,20 +575,31 @@ function closest(name, names) {
572
575
  var init_text2 = () => {};
573
576
 
574
577
  // ../kit/src/input.ts
578
+ function durationMs(text, unit) {
579
+ if (BARE_DURATION.test(text))
580
+ return Number(text) * UNIT_MS[unit];
581
+ if (!DURATION.test(text))
582
+ return 0;
583
+ let ms = 0;
584
+ for (const [, amount, part] of text.matchAll(DURATION_PART))
585
+ ms += Number(amount) * UNIT_MS[part];
586
+ return ms;
587
+ }
575
588
  function duration(value, name, fallback, unit = "s") {
576
589
  if (value === undefined)
577
590
  return fallback;
578
- const match = DURATION.exec(value.trim());
579
- const ms = match === null ? 0 : Number(match[1]) * UNIT_MS[match[2] ?? unit];
591
+ const ms = durationMs(value.trim(), unit);
580
592
  if (ms < 1)
581
593
  badUsage(`--${name} takes a duration such as 30s, 5m, 1h or 7d, not ${value}`);
582
594
  return ms;
583
595
  }
584
- var DURATION, UNIT_MS;
596
+ var BARE_DURATION, DURATION, DURATION_PART, UNIT_MS;
585
597
  var init_input = __esm(() => {
586
598
  init_args();
587
- DURATION = /^(\d+(?:\.\d+)?)(ms|[smhd])?$/;
588
- UNIT_MS = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 };
599
+ BARE_DURATION = /^\d+(?:\.\d+)?$/;
600
+ DURATION = /^(?:\d+(?:\.\d+)?(?:ms|min|[smhd]))+$/;
601
+ DURATION_PART = /(\d+(?:\.\d+)?)(ms|min|[smhd])/g;
602
+ UNIT_MS = { ms: 1, s: 1000, m: 60000, min: 60000, h: 3600000, d: 86400000 };
589
603
  });
590
604
 
591
605
  // ../kit/src/args.ts
@@ -870,6 +884,11 @@ init_format();
870
884
 
871
885
  // ../kit/src/progress.ts
872
886
  init_format();
887
+
888
+ // ../kit/src/process.ts
889
+ var GROUP_SIGNALS = new Set(["SIGINT", "SIGHUP"]);
890
+
891
+ // ../kit/src/progress.ts
873
892
  init_style();
874
893
  init_theme();
875
894
  var ESC = String.fromCharCode(27);
@@ -1014,14 +1033,16 @@ init_text2();
1014
1033
  // ../kit/src/usage.ts
1015
1034
  import { appendFileSync, mkdirSync } from "fs";
1016
1035
  import { homedir as homedir3 } from "os";
1017
- import { dirname as dirname2, isAbsolute, join as join4 } from "path";
1036
+ import { dirname as dirname2, join as join4 } from "path";
1018
1037
  import process13 from "process";
1019
1038
 
1020
1039
  // ../kit/src/logs.ts
1040
+ init_user_config();
1021
1041
  var DAY_MS = 24 * 60 * 60 * 1000;
1022
1042
  var CLI_FOLDER = "kaynooo-cli";
1023
1043
 
1024
1044
  // ../kit/src/usage.ts
1045
+ init_user_config();
1025
1046
  var UNKNOWN_OPTION2 = /unknown option '(--[a-z][a-z0-9-]*)[=']/;
1026
1047
  var FLAG_NAME = /^(?:--[a-z][a-z0-9-]*|-[a-zA-Z])$/;
1027
1048
  var LONGEST_NAME = 40;
@@ -1030,8 +1051,7 @@ function noteUsageError(message) {
1030
1051
  usageError = message;
1031
1052
  }
1032
1053
  function stateHome(env = process13.env) {
1033
- const dir = env.XDG_STATE_HOME;
1034
- return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
1054
+ return xdgFolder(env.XDG_STATE_HOME, join4(homedir3(), ".local", "state"));
1035
1055
  }
1036
1056
  function usagePath(env = process13.env) {
1037
1057
  return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
@@ -1234,6 +1254,7 @@ init_style();
1234
1254
  init_text2();
1235
1255
  // ../kit/src/secrets.ts
1236
1256
  var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
1257
+ var NOT_PASSWORDS = new Set(["compass", "encompass", "bypass", "overpass", "underpass", "surpass", "trespass"]);
1237
1258
  // ../kit/src/sessions.ts
1238
1259
  var {Glob } = globalThis.Bun;
1239
1260
  var SESSION_FILES = new Glob("*.json");
@@ -1761,7 +1782,7 @@ A bare domain is tried over https first, then http, and redirects are followed h
1761
1782
  Warns on a certificate with fewer than 14 days left or not trusted, and on an http:// twin that does not redirect to https.
1762
1783
  --dns makes no HTTP request and compares the records the system resolver, public resolvers and the zone's nameservers give.
1763
1784
  --cors <origin> sends the preflight a browser on that origin would send and says whether it would go on, with each reason when not.`;
1764
- var EXITS = [[0, "every site answered, warnings or not"], [1, "a site could not be reached, a CORS preflight was refused, or no resolver knows a name"], [2, "a usage error"]];
1785
+ var EXITS = [[0, "every site answered, warnings or not"], [1, "a site could not be reached or redirects where nothing answers, a CORS preflight was refused, or no resolver knows a name"], [2, "a usage error"]];
1765
1786
  var OPTIONS = {
1766
1787
  insecure: { type: "boolean", short: "k", description: "let curl go on with a certificate it does not trust" },
1767
1788
  header: { type: "string", short: "H", multiple: true, description: "a `Name: value` header sent with every request, repeatable" },
@@ -1772,17 +1793,17 @@ var OPTIONS = {
1772
1793
  cors: { type: "string", description: "the origin a browser would call from: whether its CORS preflight lets the request through", conflicts: ["dns", "verbose"] },
1773
1794
  method: { type: "string", description: "with --cors, the method of the request the preflight asks for (POST)" },
1774
1795
  "cors-headers": { type: "string", description: "with --cors, the headers it carries, comma separated, empty for none (content-type)" },
1775
- json: jsonOption("one object per target {target, url, version, status, chain, timings, addresses, tls, certificate, headers, cdn, offers, httpRedirect, warnings}, an array of them for several targets (an unreachable one is {target, error}); with --dns {target, zone, answers, agree, warnings}, with --cors {target, url, status, request, allowed, reasons, warnings, headers}")
1796
+ json: jsonOption("one object per target {target, url, version, status, chain, timings, addresses, tls, certificate, headers, cdn, offers, httpRedirect, failedHop: {url, error} or null, warnings}, an array of them for several targets (an unreachable one is {target, error}); with --dns {target, zone, answers, agree, warnings}, with --cors {target, url, status, request, allowed, reasons, warnings, headers}")
1776
1797
  };
1777
1798
  var BRACKETS2 = /^\[|\]$/g;
1778
1799
  var HEADER_LINE = /^[\w!#$%&'*+.^`|~-]+:/;
1779
1800
  async function follow(start, { insecure, wire }, http3) {
1780
1801
  const chain = [];
1781
1802
  let url = start;
1782
- while (chain.length < MAX_HOPS) {
1803
+ while (chain.length <= MAX_HOPS) {
1783
1804
  const { hop, errormsg } = await curlHop(url, { ...wire, http3: http3 && url.startsWith("https:"), insecure, get: false });
1784
1805
  if (hop === undefined)
1785
- return { chain, error: errormsg ?? "no response", tooMany: false };
1806
+ return { chain, failed: { url, error: errormsg ?? "no response" }, tooMany: false };
1786
1807
  chain.push(hop);
1787
1808
  const next = hop.location === undefined ? undefined : nextUrl(hop.url, hop.location);
1788
1809
  if (!isRedirect(hop.status) || next === undefined)
@@ -1795,16 +1816,19 @@ async function probe(target, input, settings) {
1795
1816
  const { wire } = settings;
1796
1817
  const http3 = await curlHasHttp3();
1797
1818
  let followed = await follow(target.url.href, settings, http3);
1819
+ let httpsError;
1798
1820
  if (followed.chain.length === 0 && target.bare) {
1799
1821
  const plain = new URL(target.url.href);
1800
1822
  plain.protocol = "http:";
1801
1823
  const second = await follow(plain.href, settings, http3);
1802
- if (second.chain.length > 0)
1824
+ if (second.chain.length > 0) {
1825
+ httpsError = followed.failed?.error;
1803
1826
  followed = second;
1827
+ }
1804
1828
  }
1805
1829
  const final = followed.chain.at(-1);
1806
1830
  if (final === undefined)
1807
- return { target: input, error: `could not reach ${target.url.host}: ${followed.error ?? "no response"}` };
1831
+ return { target: input, error: `could not reach ${target.url.host}: ${followed.failed?.error ?? "no response"}` };
1808
1832
  const finalUrl = new URL(final.url);
1809
1833
  const secure = finalUrl.protocol === "https:";
1810
1834
  const [addresses, tls, twin] = await Promise.all([
@@ -1815,8 +1839,10 @@ async function probe(target, input, settings) {
1815
1839
  const httpRedirect = httpRedirectOf(twin);
1816
1840
  const offers = offersOf({ alpn: tls?.alpn, version: final.version, altSvc: final.headers["alt-svc"] });
1817
1841
  const warnings = warningsOf({ cert: tls?.cert, authorizationError: tls?.authorizationError, httpRedirect, tooManyRedirects: followed.tooMany });
1842
+ if (followed.failed !== undefined)
1843
+ warnings.unshift(`the redirect to ${followed.failed.url} got no answer: ${followed.failed.error}`);
1818
1844
  if (!secure)
1819
- warnings.push("the site is not served over https");
1845
+ warnings.push(httpsError === undefined ? "the site is not served over https" : `the site is not served over https: ${httpsError}`);
1820
1846
  return {
1821
1847
  target: input,
1822
1848
  url: final.url,
@@ -1831,9 +1857,11 @@ async function probe(target, input, settings) {
1831
1857
  cdn: cdnHint(final.headers) ?? null,
1832
1858
  offers,
1833
1859
  httpRedirect: httpRedirect ?? null,
1860
+ failedHop: followed.failed ?? null,
1834
1861
  warnings
1835
1862
  };
1836
1863
  }
1864
+ var unreached = (report) => ("error" in report) || report.failedHop !== null;
1837
1865
  async function twinOf(url, { insecure, wire }) {
1838
1866
  const plain = new URL(url.href);
1839
1867
  plain.protocol = "http:";
@@ -2009,7 +2037,7 @@ async function main(args) {
2009
2037
  fail(only.error, 1);
2010
2038
  if (values.json === true) {
2011
2039
  printJson(reports.length === 1 ? only : reports);
2012
- return reports.some((report) => ("error" in report)) ? 1 : 0;
2040
+ return reports.some(unreached) ? 1 : 0;
2013
2041
  }
2014
2042
  let first = true;
2015
2043
  for (const report of reports) {
@@ -2026,6 +2054,6 @@ async function main(args) {
2026
2054
  }
2027
2055
  first = false;
2028
2056
  }
2029
- return reports.some((report) => ("error" in report)) ? 1 : 0;
2057
+ return reports.some(unreached) ? 1 : 0;
2030
2058
  }
2031
2059
  process16.exitCode = await runMain({ name: "probe", run: main });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-probe",
3
3
  "type": "module",
4
- "version": "0.4.4",
4
+ "version": "0.4.6",
5
5
  "description": "probe: a site's HTTP version, redirect chain, timings, addresses, TLS certificate and notable headers in one call, with warnings.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -31,10 +31,10 @@
31
31
  "registry": "https://git.kaynooo.fr/api/packages/kaynooo/npm/"
32
32
  },
33
33
  "scripts": {
34
- "build": "$npm_execpath build src/probe.ts --target bun --outdir dist",
34
+ "build": "$npm_execpath build src/probe.ts --target bun --outdir dist && $npm_execpath ../../scripts/shell-shebang.ts",
35
35
  "probe": "$npm_execpath src/probe.ts"
36
36
  },
37
37
  "devDependencies": {
38
- "@kaynooo/cli-kit": "0.46.0"
38
+ "@kaynooo/cli-kit": "0.46.4"
39
39
  }
40
40
  }