@fedify/cli 2.1.30 → 2.1.32

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/dist/config.js CHANGED
@@ -71,7 +71,8 @@ const nodeinfoSchema = object$1({
71
71
  raw: optional$1(boolean()),
72
72
  bestEffort: optional$1(boolean()),
73
73
  showFavicon: optional$1(boolean()),
74
- showMetadata: optional$1(boolean())
74
+ showMetadata: optional$1(boolean()),
75
+ timeout: optional$1(number())
75
76
  });
76
77
  /**
77
78
  * Config context for use with bindConfig().
package/dist/deno.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "@js-temporal/polyfill";
2
2
  //#region deno.json
3
- var version = "2.1.30";
3
+ var version = "2.1.32";
4
4
  //#endregion
5
5
  export { version };
package/dist/nodeinfo.js CHANGED
@@ -2,7 +2,7 @@ import "@js-temporal/polyfill";
2
2
  import { configContext } from "./config.js";
3
3
  import { userAgentOption } from "./options.js";
4
4
  import { colors, formatObject } from "./utils.js";
5
- import { argument, command, constant, flag, group, merge, message, object, string, text } from "@optique/core";
5
+ import { argument, command, constant, flag, float, group, merge, message, object, option, optional, string, text } from "@optique/core";
6
6
  import { print, printError } from "@optique/run";
7
7
  import os from "node:os";
8
8
  import process from "node:process";
@@ -10,7 +10,7 @@ import { bindConfig } from "@optique/config";
10
10
  import { getNodeInfo } from "@fedify/fedify";
11
11
  import { getLogger } from "@logtape/logtape";
12
12
  import ora from "ora";
13
- import { getUserAgent } from "@fedify/vocab-runtime";
13
+ import { getUserAgent, validatePublicUrl } from "@fedify/vocab-runtime";
14
14
  import { createJimp } from "@jimp/core";
15
15
  import webp from "@jimp/wasm-webp";
16
16
  import { isICO, parseICO } from "icojs";
@@ -21,6 +21,76 @@ const logger = getLogger([
21
21
  "cli",
22
22
  "nodeinfo"
23
23
  ]);
24
+ const MAX_FAVICON_REDIRECTS = 5;
25
+ const DEFAULT_NODEINFO_TIMEOUT_SECONDS = 10;
26
+ const MAX_TIMER_DELAY = 2147483647;
27
+ /**
28
+ * Uses a CLI-owned signal so a timeout can be distinguished from the other
29
+ * lookup failures that `getNodeInfo()` reports as `undefined`.
30
+ * @internal
31
+ */
32
+ async function withNodeInfoTimeout(timeoutSeconds, lookup) {
33
+ const seconds = timeoutSeconds ?? DEFAULT_NODEINFO_TIMEOUT_SECONDS;
34
+ const milliseconds = Math.ceil(seconds * 1e3);
35
+ if (!Number.isFinite(seconds) || seconds <= 0 || milliseconds > MAX_TIMER_DELAY) throw new RangeError("The NodeInfo timeout must be a positive, finite number of seconds within the timer range.");
36
+ const controller = new AbortController();
37
+ let timer;
38
+ const deadline = new Promise((resolve) => {
39
+ timer = setTimeout(() => {
40
+ resolve({ timedOut: true });
41
+ controller.abort();
42
+ }, milliseconds);
43
+ });
44
+ try {
45
+ const loading = lookup(controller.signal).then((value) => ({
46
+ timedOut: false,
47
+ value
48
+ }));
49
+ loading.catch(() => {});
50
+ const outcome = await Promise.race([loading, deadline]);
51
+ return outcome.timedOut ? {
52
+ value: void 0,
53
+ timedOut: true,
54
+ timeoutSeconds: seconds
55
+ } : {
56
+ value: outcome.value,
57
+ timedOut: false,
58
+ timeoutSeconds: seconds
59
+ };
60
+ } finally {
61
+ clearTimeout(timer);
62
+ }
63
+ }
64
+ /**
65
+ * Describes a lookup failure without treating a timeout as a missing document.
66
+ * @internal
67
+ */
68
+ function describeNodeInfoFailure(timedOut, raw, timeoutSeconds) {
69
+ if (timedOut) return `NodeInfo request timed out after ${timeoutSeconds} seconds.`;
70
+ return raw ? "No NodeInfo document found." : "No NodeInfo document found or it is invalid.";
71
+ }
72
+ async function fetchFaviconResource(url, userAgent) {
73
+ let current = typeof url === "string" ? new URL(url) : url;
74
+ for (let redirects = 0;; redirects++) {
75
+ await validatePublicUrl(current.href);
76
+ const response = await fetch(current, {
77
+ headers: { "User-Agent": userAgent == null ? getUserAgent() : userAgent },
78
+ redirect: "manual"
79
+ });
80
+ if (response.status !== 301 && response.status !== 302 && response.status !== 303 && response.status !== 307 && response.status !== 308) return {
81
+ response,
82
+ url: current
83
+ };
84
+ const location = response.headers.get("Location");
85
+ if (location == null) return {
86
+ response,
87
+ url: current
88
+ };
89
+ await response.body?.cancel();
90
+ if (redirects >= MAX_FAVICON_REDIRECTS) throw new Error("Too many favicon redirects");
91
+ current = new URL(location, current);
92
+ }
93
+ }
24
94
  const Jimp = createJimp({
25
95
  formats: [...defaultFormats, webp],
26
96
  plugins: defaultPlugins
@@ -49,7 +119,14 @@ const nodeInfoOption = merge(object("Display options", {
49
119
  const nodeInfoCommand = command("nodeinfo", merge(object("Arguments", {
50
120
  command: constant("nodeinfo"),
51
121
  host: argument(string({ metavar: "HOST" }), { description: message`Bare hostname or a full URL of the instance` })
52
- }), nodeInfoOption, group("Network options", userAgentOption)), {
122
+ }), nodeInfoOption, group("Network options", merge(userAgentOption, object({ timeout: bindConfig(optional(option("-T", "--timeout", float({
123
+ min: Number.MIN_VALUE,
124
+ metavar: "SECONDS"
125
+ }), { description: message`Set the NodeInfo lookup timeout in seconds (default: 10).` })), {
126
+ context: configContext,
127
+ key: (config) => config.nodeinfo?.timeout,
128
+ default: DEFAULT_NODEINFO_TIMEOUT_SECONDS
129
+ }) })))), {
53
130
  brief: message`Get information about a remote node using the NodeInfo protocol`,
54
131
  description: message`Get information about a remote node using the NodeInfo protocol.
55
132
 
@@ -62,28 +139,36 @@ async function runNodeInfo(command) {
62
139
  }).start();
63
140
  const url = new URL(URL.canParse(command.host) ? command.host : `https://${command.host}`);
64
141
  if (command.raw) {
65
- const nodeInfo = await getNodeInfo(url, {
142
+ const { value: nodeInfo, timedOut, timeoutSeconds } = await withNodeInfoTimeout(command.timeout, (signal) => getNodeInfo(url, {
66
143
  parse: "none",
67
- userAgent: command.userAgent
68
- });
144
+ userAgent: command.userAgent,
145
+ timeout: null,
146
+ signal
147
+ }));
69
148
  if (nodeInfo === void 0) {
70
- spinner.fail("No NodeInfo document found.");
71
- printError(message`No NodeInfo document found.`);
149
+ const failure = describeNodeInfoFailure(timedOut, true, timeoutSeconds);
150
+ spinner.fail(failure);
151
+ printError(message`${text(failure)}`);
152
+ if (timedOut) printError(message`Try increasing the timeout with -T/--timeout or check network connectivity.`);
72
153
  process.exit(1);
73
154
  }
74
155
  spinner.succeed("NodeInfo document fetched.");
75
156
  print(message`${text(formatObject(nodeInfo, void 0, true))}`);
76
157
  return;
77
158
  }
78
- const nodeInfo = await getNodeInfo(url, {
159
+ const { value: nodeInfo, timedOut, timeoutSeconds } = await withNodeInfoTimeout(command.timeout, (signal) => getNodeInfo(url, {
79
160
  parse: command.bestEffort ? "best-effort" : "strict",
80
- userAgent: command.userAgent
81
- });
161
+ userAgent: command.userAgent,
162
+ timeout: null,
163
+ signal
164
+ }));
82
165
  logger.debug("NodeInfo document: {nodeInfo}", { nodeInfo });
83
166
  if (nodeInfo == void 0) {
84
- spinner.fail("No NodeInfo document found or it is invalid.");
85
- printError(message`No NodeInfo document found or it is invalid.`);
86
- if (!command.bestEffort) printError(message`Use the -b/--best-effort option to try to parse the document anyway.`);
167
+ const failure = describeNodeInfoFailure(timedOut, false, timeoutSeconds);
168
+ spinner.fail(failure);
169
+ printError(message`${text(failure)}`);
170
+ if (timedOut) printError(message`Try increasing the timeout with -T/--timeout or check network connectivity.`);
171
+ else if (!command.bestEffort) printError(message`Use the -b/--best-effort option to try to parse the document anyway.`);
87
172
  process.exit(1);
88
173
  }
89
174
  let layout;
@@ -91,8 +176,7 @@ async function runNodeInfo(command) {
91
176
  if (!command.noFavicon) {
92
177
  spinner.text = "Fetching the favicon...";
93
178
  try {
94
- const faviconUrl = await getFaviconUrl(url, command.userAgent);
95
- const response = await fetch(faviconUrl, { headers: { "User-Agent": command.userAgent == null ? getUserAgent() : command.userAgent } });
179
+ const { response } = await fetchFaviconResource(await getFaviconUrl(url, command.userAgent), command.userAgent);
96
180
  if (response.ok) {
97
181
  const contentType = response.headers.get("Content-Type");
98
182
  let buffer = await response.arrayBuffer();
@@ -171,7 +255,7 @@ function indent(text, depth) {
171
255
  const LINK_REGEXP = /<link((?:\s+(?:[-a-z]+)=(?:"[^"]*"|'[^']*'|[^\s]+))*)\s*\/?>/gi;
172
256
  const LINK_ATTRS_REGEXP = /(?:\s+([-a-z]+)=("[^"]*"|'[^']*'|[^\s]+))/gi;
173
257
  async function getFaviconUrl(url, userAgent) {
174
- const response = await fetch(url, { headers: { "User-Agent": userAgent == null ? getUserAgent() : userAgent } });
258
+ const { response, url: finalUrl } = await fetchFaviconResource(url, userAgent);
175
259
  const text = await response.text();
176
260
  for (const match of text.matchAll(LINK_REGEXP)) {
177
261
  const attrs = {};
@@ -186,12 +270,12 @@ async function getFaviconUrl(url, userAgent) {
186
270
  if (w < 38 || h < 19) continue;
187
271
  }
188
272
  if ("href" in attrs) {
189
- const parsedUrl = new URL(attrs.href, response.url);
273
+ const parsedUrl = new URL(attrs.href, finalUrl);
190
274
  if (parsedUrl.pathname.toLowerCase().endsWith(".svg")) continue;
191
275
  return parsedUrl;
192
276
  }
193
277
  }
194
- return new URL("/favicon.ico", response.url);
278
+ return new URL("/favicon.ico", finalUrl);
195
279
  }
196
280
  function checkTerminalColorSupport() {
197
281
  const noColor = process.env.NO_COLOR;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/cli",
3
- "version": "2.1.30",
3
+ "version": "2.1.32",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "README.md",
@@ -86,14 +86,14 @@
86
86
  "smol-toml": "^1.6.0",
87
87
  "srvx": "^0.8.7",
88
88
  "valibot": "^1.2.0",
89
- "@fedify/relay": "2.1.30",
90
- "@fedify/init": "2.1.30",
91
- "@fedify/fedify": "2.1.30",
92
- "@fedify/vocab-runtime": "2.1.30",
93
- "@fedify/vocab": "2.1.30",
94
- "@fedify/vocab-tools": "2.1.30",
95
- "@fedify/webfinger": "2.1.30",
96
- "@fedify/sqlite": "2.1.30"
89
+ "@fedify/fedify": "2.1.32",
90
+ "@fedify/init": "2.1.32",
91
+ "@fedify/relay": "2.1.32",
92
+ "@fedify/sqlite": "2.1.32",
93
+ "@fedify/vocab": "2.1.32",
94
+ "@fedify/vocab-runtime": "2.1.32",
95
+ "@fedify/vocab-tools": "2.1.32",
96
+ "@fedify/webfinger": "2.1.32"
97
97
  },
98
98
  "devDependencies": {
99
99
  "@types/bun": "^1.2.23",