@fedify/cli 2.5.0-dev.2614 → 2.5.0-dev.2619

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.
@@ -7,8 +7,8 @@ import { COMPARE_REPORT_SCHEMA_ID } from "./compare/schema.js";
7
7
  import process from "node:process";
8
8
  import { tmpdir } from "node:os";
9
9
  import { join } from "node:path";
10
- import { spawn } from "node:child_process";
11
10
  import { mkdtemp, rm, writeFile } from "node:fs/promises";
11
+ import { spawn } from "node:child_process";
12
12
  //#region src/bench/compare.ts
13
13
  const ZERO_BASE_LATENCY_ALLOWANCE_MS = 1;
14
14
  /** Runs `fedify bench compare`. */
package/dist/deno.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "temporal-polyfill";
2
2
  //#region deno.json
3
- var version = "2.5.0-dev.2614+a776599e";
3
+ var version = "2.5.0-dev.2619+88aa7efd";
4
4
  //#endregion
5
5
  export { version };
package/dist/nodeinfo.js CHANGED
@@ -15,6 +15,7 @@ import { getLogger } from "@logtape/logtape";
15
15
  import { isICO, parseICO } from "icojs";
16
16
  import { defaultFormats, defaultPlugins, intToRGBA } from "jimp";
17
17
  import { Buffer } from "node:buffer";
18
+ import { readFile } from "node:fs/promises";
18
19
  import ora from "ora";
19
20
  import { PNG } from "pngjs";
20
21
  //#region src/nodeinfo.ts
@@ -24,6 +25,7 @@ const logger = getLogger([
24
25
  "nodeinfo"
25
26
  ]);
26
27
  const MAX_FAVICON_REDIRECTS = 5;
28
+ const SVG_SNIFF_LENGTH = 1024;
27
29
  async function fetchFaviconResource(url, userAgent) {
28
30
  let current = typeof url === "string" ? new URL(url) : url;
29
31
  for (let redirects = 0;; redirects++) {
@@ -60,6 +62,51 @@ const PNG_SIGNATURE = [
60
62
  26,
61
63
  10
62
64
  ];
65
+ let svgRasterizer;
66
+ function loadSvgRasterizer() {
67
+ return svgRasterizer ??= (async () => {
68
+ try {
69
+ const resvg = await import("@resvg/resvg-wasm");
70
+ const wasmUrl = import.meta.resolve("@resvg/resvg-wasm/index_bg.wasm");
71
+ await resvg.initWasm(await readFile(new URL(wasmUrl)));
72
+ return resvg;
73
+ } catch (error) {
74
+ logger.debug("Failed to load the SVG rasterizer: {error}", { error });
75
+ throw error;
76
+ }
77
+ })();
78
+ }
79
+ async function rasterizeSvg(buffer) {
80
+ const svg = toUint8Array(buffer);
81
+ const { Resvg } = await loadSvgRasterizer();
82
+ const probe = new Resvg(svg);
83
+ const { width, height } = probe;
84
+ probe.free();
85
+ if (width < 1 || height < 1) throw new RangeError(`Unusable SVG dimensions: ${width}x${height}`);
86
+ const resvg = new Resvg(svg, { fitTo: {
87
+ mode: width >= height ? "width" : "height",
88
+ value: DEFAULT_IMAGE_WIDTH
89
+ } });
90
+ try {
91
+ const rendered = resvg.render();
92
+ try {
93
+ if (!hasVisiblePixel(rendered.pixels)) throw new Error("The SVG favicon has no visible pixels.");
94
+ return rendered.asPng();
95
+ } finally {
96
+ rendered.free();
97
+ }
98
+ } finally {
99
+ resvg.free();
100
+ }
101
+ }
102
+ function hasVisiblePixel(pixels) {
103
+ for (let i = 3; i < pixels.length; i += 4) if (pixels[i] !== 0) return true;
104
+ return false;
105
+ }
106
+ function isSvg(buffer) {
107
+ const text = new TextDecoder().decode(toUint8Array(buffer).subarray(0, SVG_SNIFF_LENGTH));
108
+ return /<svg[\s/>]/i.test(text);
109
+ }
63
110
  function toUint8Array(buffer) {
64
111
  return buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
65
112
  }
@@ -119,6 +166,23 @@ const nodeInfoMetadata = {
119
166
  The argument is the hostname of the remote node, or the URL of the remote node.`
120
167
  };
121
168
  command("nodeinfo", nodeInfoOptions, nodeInfoMetadata);
169
+ async function renderFavicon(url, userAgent) {
170
+ const { response } = await fetchFaviconResource(url, userAgent);
171
+ if (!response.ok) {
172
+ await response.body?.cancel();
173
+ throw new Error(`Failed to fetch the favicon: ${response.status} ${response.statusText}`);
174
+ }
175
+ const contentType = response.headers.get("Content-Type")?.split(";")[0].trim().toLowerCase();
176
+ let buffer = await response.arrayBuffer();
177
+ if (contentType === "image/vnd.microsoft.icon" || contentType === "image/x-icon" || isICO(buffer)) {
178
+ const images = await parseICO(buffer);
179
+ if (images.length < 1) throw new Error("No images found in the ICO file.");
180
+ buffer = images[0].buffer;
181
+ } else if (!isPng(buffer) && (contentType === "image/svg+xml" || isSvg(buffer))) buffer = await rasterizeSvg(buffer);
182
+ const image = await readImage(buffer);
183
+ const colorSupport = checkTerminalColorSupport();
184
+ return getAsciiArt(image, DEFAULT_IMAGE_WIDTH, colorSupport, colors).split("\n").map((line) => ` ${line} `);
185
+ }
122
186
  async function runNodeInfo(command) {
123
187
  const spinner = ora({
124
188
  text: "Fetching a NodeInfo document...",
@@ -155,26 +219,20 @@ async function runNodeInfo(command) {
155
219
  if (!command.noFavicon) {
156
220
  spinner.text = "Fetching the favicon...";
157
221
  try {
158
- const { response } = await fetchFaviconResource(await getFaviconUrl(url, command.userAgent), command.userAgent);
159
- if (response.ok) {
160
- const contentType = response.headers.get("Content-Type");
161
- let buffer = await response.arrayBuffer();
162
- if (contentType === "image/vnd.microsoft.icon" || contentType === "image/x-icon" || isICO(buffer)) {
163
- const images = await parseICO(buffer);
164
- if (images.length < 1) throw new Error("No images found in the ICO file.");
165
- buffer = images[0].buffer;
166
- }
167
- const image = await readImage(buffer);
168
- const colorSupport = checkTerminalColorSupport();
169
- layout = getAsciiArt(image, DEFAULT_IMAGE_WIDTH, colorSupport, colors).split("\n").map((line) => ` ${line} `);
170
- defaultWidth = 41;
171
- } else {
172
- logger.error("Failed to fetch the favicon: {status} {statusText}", {
173
- status: response.status,
174
- statusText: response.statusText
222
+ const faviconUrl = await getFaviconUrl(url, command.userAgent);
223
+ const fallbackUrl = new URL("/favicon.ico", url);
224
+ try {
225
+ layout = await renderFavicon(faviconUrl, command.userAgent);
226
+ } catch (error) {
227
+ if (faviconUrl.href === fallbackUrl.href) throw error;
228
+ logger.debug("Failed to fetch or render {url}; retrying {fallbackUrl}: {error}", {
229
+ url: faviconUrl.href,
230
+ fallbackUrl: fallbackUrl.href,
231
+ error
175
232
  });
176
- layout = [""];
233
+ layout = await renderFavicon(fallbackUrl, command.userAgent);
177
234
  }
235
+ defaultWidth = 41;
178
236
  } catch (error) {
179
237
  logger.error("Failed to fetch or render the favicon: {error}", { error });
180
238
  layout = [""];
@@ -238,6 +296,7 @@ const LINK_ATTRS_REGEXP = /(?:\s+([-a-z]+)=("[^"]*"|'[^']*'|[^\s]+))/gi;
238
296
  async function getFaviconUrl(url, userAgent) {
239
297
  const { response, url: finalUrl } = await fetchFaviconResource(url, userAgent);
240
298
  const text = await response.text();
299
+ let svgCandidate;
241
300
  for (const match of text.matchAll(LINK_REGEXP)) {
242
301
  const attrs = {};
243
302
  for (const attrMatch of match[1].matchAll(LINK_ATTRS_REGEXP)) {
@@ -246,19 +305,21 @@ async function getFaviconUrl(url, userAgent) {
246
305
  }
247
306
  const rel = attrs.rel?.toLowerCase()?.trim()?.split(/\s+/) ?? [];
248
307
  if (!rel.includes("icon") && !rel.includes("apple-touch-icon")) continue;
249
- const tokens = attrs.sizes?.match(/\d+x\d+/g);
250
- if (tokens != null && !tokens.some((token) => {
251
- const [w, h] = token.split("x").map((v) => Number.parseInt(v));
252
- return w >= 38 && h >= 19;
253
- })) continue;
254
308
  if ("href" in attrs) {
255
309
  const parsedUrl = new URL(attrs.href, finalUrl);
256
- if (parsedUrl.pathname.toLowerCase().endsWith(".svg")) continue;
257
- if (attrs.type?.toLowerCase()?.trim() === "image/svg+xml") continue;
310
+ if (parsedUrl.pathname.toLowerCase().endsWith(".svg") || attrs.type?.toLowerCase()?.trim() === "image/svg+xml") {
311
+ svgCandidate ??= parsedUrl;
312
+ continue;
313
+ }
314
+ const tokens = attrs.sizes?.match(/\d+x\d+/g);
315
+ if (tokens != null && !tokens.some((token) => {
316
+ const [w, h] = token.split("x").map((v) => Number.parseInt(v));
317
+ return w >= 38 && h >= 19;
318
+ })) continue;
258
319
  return parsedUrl;
259
320
  }
260
321
  }
261
- return new URL("/favicon.ico", finalUrl);
322
+ return svgCandidate ?? new URL("/favicon.ico", finalUrl);
262
323
  }
263
324
  function checkTerminalColorSupport() {
264
325
  const noColor = process.env.NO_COLOR;
@@ -339,4 +400,4 @@ function getAsciiArt(image, width = DEFAULT_IMAGE_WIDTH, colorSupport, colors) {
339
400
  return art;
340
401
  }
341
402
  //#endregion
342
- export { Jimp, getAsciiArt, getFaviconUrl, nodeInfoMetadata, nodeInfoOptions, readImage, rgbTo256Color, runNodeInfo };
403
+ export { Jimp, getAsciiArt, getFaviconUrl, nodeInfoMetadata, nodeInfoOptions, rasterizeSvg, readImage, rgbTo256Color, runNodeInfo };
package/dist/utils.js CHANGED
@@ -4,12 +4,12 @@ import process from "node:process";
4
4
  import { message } from "@optique/core";
5
5
  import { print, printError } from "@optique/run";
6
6
  import { flow } from "es-toolkit";
7
+ import "node:fs/promises";
7
8
  import { getActorHandle } from "@fedify/vocab";
8
9
  import "@fxts/core";
9
10
  import { Chalk } from "chalk";
10
11
  import { highlight } from "cli-highlight";
11
12
  import "node:child_process";
12
- import "node:fs/promises";
13
13
  import util from "node:util";
14
14
  //#region src/utils.ts
15
15
  const colorEnabled = process.stdout.isTTY && !("NO_COLOR" in process.env && process.env.NO_COLOR !== "");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/cli",
3
- "version": "2.5.0-dev.2614+a776599e",
3
+ "version": "2.5.0-dev.2619+88aa7efd",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "README.md",
@@ -69,6 +69,7 @@
69
69
  "@logtape/file": "^2.2.0",
70
70
  "@logtape/logtape": "^2.2.0",
71
71
  "@poppanator/http-constants": "^1.1.1",
72
+ "@resvg/resvg-wasm": "^2.6.2",
72
73
  "byte-encodings": "^1.0.11",
73
74
  "chalk": "^5.6.2",
74
75
  "cli-highlight": "^2.1.11",
@@ -89,14 +90,14 @@
89
90
  "temporal-polyfill": "^1.0.1",
90
91
  "valibot": "^1.4.0",
91
92
  "yaml": "^2.9.0",
92
- "@fedify/fedify": "2.5.0-dev.2614+a776599e",
93
- "@fedify/init": "2.5.0-dev.2614+a776599e",
94
- "@fedify/sqlite": "2.5.0-dev.2614+a776599e",
95
- "@fedify/vocab-runtime": "2.5.0-dev.2614+a776599e",
96
- "@fedify/vocab-tools": "2.5.0-dev.2614+a776599e",
97
- "@fedify/vocab": "2.5.0-dev.2614+a776599e",
98
- "@fedify/webfinger": "2.5.0-dev.2614+a776599e",
99
- "@fedify/relay": "2.5.0-dev.2614+a776599e"
93
+ "@fedify/fedify": "2.5.0-dev.2619+88aa7efd",
94
+ "@fedify/vocab": "2.5.0-dev.2619+88aa7efd",
95
+ "@fedify/relay": "2.5.0-dev.2619+88aa7efd",
96
+ "@fedify/sqlite": "2.5.0-dev.2619+88aa7efd",
97
+ "@fedify/vocab-tools": "2.5.0-dev.2619+88aa7efd",
98
+ "@fedify/init": "2.5.0-dev.2619+88aa7efd",
99
+ "@fedify/webfinger": "2.5.0-dev.2619+88aa7efd",
100
+ "@fedify/vocab-runtime": "2.5.0-dev.2619+88aa7efd"
100
101
  },
101
102
  "devDependencies": {
102
103
  "@types/bun": "^1.2.23",