@fedify/cli 2.2.14-dev.2082 → 2.2.14

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/deno.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "@js-temporal/polyfill";
2
2
  //#region deno.json
3
- var version = "2.2.14-dev.2082+79cd52e9";
3
+ var version = "2.2.14";
4
4
  //#endregion
5
5
  export { version };
@@ -0,0 +1,70 @@
1
+ import "@js-temporal/polyfill";
2
+ import "@fedify/vocab";
3
+ import { FetchError, UrlError } from "@fedify/vocab-runtime";
4
+ //#region src/diagnostics.ts
5
+ /** Records loader failures that lookupObject would otherwise swallow or wrap. */
6
+ function createLookupDiagnostics(documentLoader, contextLoader) {
7
+ let objectFailure;
8
+ let contextFailure;
9
+ const wrap = (loader, source, record) => async (url, options) => {
10
+ try {
11
+ const document = await loader(url, options);
12
+ record(void 0);
13
+ return document;
14
+ } catch (error) {
15
+ record({
16
+ error,
17
+ source
18
+ });
19
+ throw error;
20
+ }
21
+ };
22
+ return {
23
+ documentLoader: wrap(documentLoader, "object", (e) => objectFailure = e),
24
+ contextLoader: wrap(contextLoader, "context", (e) => contextFailure = e),
25
+ clearFailures() {
26
+ objectFailure = void 0;
27
+ contextFailure = void 0;
28
+ },
29
+ getObjectFailure: () => objectFailure,
30
+ getContextFailure: () => contextFailure
31
+ };
32
+ }
33
+ function escapeControlCharacters(message) {
34
+ return message.replace(/[\x00-\x1f\x7f-\x9f]/g, (character) => `\\x${character.charCodeAt(0).toString(16).padStart(2, "0")}`);
35
+ }
36
+ /** Describes a failure without recommending signing unless HTTP supports it. */
37
+ function describeLookupFailure(failure, authorizedFetch) {
38
+ const error = failure?.error;
39
+ const http = error instanceof FetchError ? /^(.+): HTTP (\d{3}): \1$/s.exec(error.message) : null;
40
+ if (http != null) {
41
+ const status = Number(http[2]);
42
+ const suggestsAuthorizedFetch = failure?.source === "object" && !authorizedFetch && [
43
+ 401,
44
+ 403,
45
+ 404
46
+ ].includes(status);
47
+ return {
48
+ message: escapeControlCharacters(`HTTP ${status} from ${http[1]}.`) + (suggestsAuthorizedFetch ? " It may be a private object. Try with -a/--authorized-fetch." : ""),
49
+ suggestsAuthorizedFetch
50
+ };
51
+ }
52
+ const causes = [];
53
+ const seen = /* @__PURE__ */ new Set();
54
+ let cause = error;
55
+ while (cause instanceof Error && !seen.has(cause) && causes.length < 8) {
56
+ seen.add(cause);
57
+ causes.push(cause);
58
+ if (cause instanceof UrlError && cause.reason === "dns" || cause instanceof TypeError && /dns error|failed to lookup address information/i.test(cause.message) || "code" in cause && (cause.code === "ENOTFOUND" || cause.code === "EAI_AGAIN")) return {
59
+ message: "Could not resolve the host in the URL. Check the URL and your network connection.",
60
+ suggestsAuthorizedFetch: false
61
+ };
62
+ cause = cause.cause;
63
+ }
64
+ return {
65
+ message: escapeControlCharacters(causes.length > 0 ? causes.map((e) => e.message).join(": ") : error == null ? "Could not fetch or parse the object. Check the URL or actor handle." : String(error)),
66
+ suggestsAuthorizedFetch: false
67
+ };
68
+ }
69
+ //#endregion
70
+ export { createLookupDiagnostics, describeLookupFailure };
package/dist/lookup.js CHANGED
@@ -4,12 +4,13 @@ import { configureLogging } from "./log.js";
4
4
  import { spawnTemporaryServer } from "./tempserver.js";
5
5
  import { colorEnabled, colors, formatObject } from "./utils.js";
6
6
  import { configContext } from "./config.js";
7
+ import { createLookupDiagnostics, describeLookupFailure } from "./diagnostics.js";
7
8
  import { createTunnelServiceOption, userAgentOption } from "./options.js";
8
9
  import { renderImages } from "./imagerenderer.js";
9
10
  import { url } from "@optique/core/message";
10
11
  import { path, printError } from "@optique/run";
11
12
  import process from "node:process";
12
- import { argument, choice, command, constant, flag, float, integer, map, merge, message as message$1, multiple, object, option, optionNames, optional, or, string, withDefault } from "@optique/core";
13
+ import { argument, choice, command, constant, flag, float, integer, map, merge, message as message$1, multiple, object, option, optionNames, optional, or, string, text, withDefault } from "@optique/core";
13
14
  import { generateCryptoKeyPair, getAuthenticatedDocumentLoader, respondWithObject } from "@fedify/fedify";
14
15
  import { Application, Collection, CryptographicKey, Object as Object$1, lookupObject, traverseCollection } from "@fedify/vocab";
15
16
  import { getLogger } from "@logtape/logtape";
@@ -276,6 +277,9 @@ function wrapDocumentLoaderWithTimeout(loader, timeoutSeconds) {
276
277
  return loader(url, {
277
278
  ...options,
278
279
  signal
280
+ }).catch((error) => {
281
+ if (signal?.aborted) throw signal.reason;
282
+ throw error;
279
283
  }).finally(() => clearTimeoutSignal(signal));
280
284
  };
281
285
  }
@@ -286,7 +290,7 @@ function handleTimeoutError(spinner, timeoutSeconds, url) {
286
290
  }
287
291
  function isPrivateAddressError(error) {
288
292
  const lowerMessage = (error instanceof Error ? error.message : String(error)).toLowerCase();
289
- if (error instanceof UrlError) return lowerMessage.includes("invalid or private address") || lowerMessage.includes("localhost is not allowed");
293
+ if (error instanceof UrlError) return error.reason === "disallowed" && (lowerMessage.includes("invalid or private address") || lowerMessage.includes("localhost is not allowed"));
290
294
  return lowerMessage.includes("private address") || lowerMessage.includes("private ip") || lowerMessage.includes("localhost") || lowerMessage.includes("loopback");
291
295
  }
292
296
  function getPrivateUrlCandidate(candidate) {
@@ -324,6 +328,7 @@ function printRecursivePrivateContextHint(privateContextUrl) {
324
328
  printError(message$1`Recursive JSON-LD context URL ${url(privateContextUrl)} is always blocked, even with ${optionNames(["-p", "--allow-private-address"])}. Use ${optionNames(["-S", "--suppress-errors"])} to skip blocked steps.`);
325
329
  }
326
330
  function getLookupFailureHint(error, options = {}) {
331
+ if (error instanceof UrlError && error.reason === "dns") return "dns";
327
332
  if (isPrivateAddressError(error)) return options.recursive ? "recursive-private-address" : "private-address";
328
333
  return "authorized-fetch";
329
334
  }
@@ -337,6 +342,9 @@ function printLookupFailureHint(authLoader, error, options = {}) {
337
342
  const hint = getLookupFailureHint(error, options);
338
343
  if (!shouldPrintLookupFailureHint(authLoader, hint)) return;
339
344
  switch (hint) {
345
+ case "dns":
346
+ printError(message$1`DNS lookup failed. Check the hostname and network connectivity.`);
347
+ return;
340
348
  case "private-address":
341
349
  printError(message$1`The URL appears to be private or localhost. Try with ${optionNames(["-p", "--allow-private-address"])}.`);
342
350
  return;
@@ -417,6 +425,29 @@ async function runLookup(command, deps = {}) {
417
425
  exit: (code) => process.exit(code),
418
426
  ...deps
419
427
  };
428
+ const lookupDiagnosed = async (identifier, options) => {
429
+ const diagnostics = createLookupDiagnostics(options.documentLoader, options.contextLoader);
430
+ try {
431
+ const object = await effectiveDeps.lookupObject(identifier, {
432
+ ...options,
433
+ ...diagnostics
434
+ });
435
+ return {
436
+ object,
437
+ failure: object == null ? diagnostics.getObjectFailure() ?? diagnostics.getContextFailure() : void 0,
438
+ thrownError: void 0
439
+ };
440
+ } catch (error) {
441
+ return {
442
+ object: null,
443
+ thrownError: error,
444
+ failure: diagnostics.getContextFailure() ?? diagnostics.getObjectFailure() ?? {
445
+ error,
446
+ source: "other"
447
+ }
448
+ };
449
+ }
450
+ };
420
451
  if (command.urls.length < 1) {
421
452
  printError(message$1`At least one URL or actor handle must be provided.`);
422
453
  effectiveDeps.exit(1);
@@ -667,39 +698,33 @@ async function runLookup(command, deps = {}) {
667
698
  for (let urlIndex = 0; urlIndex < command.urls.length; urlIndex++) {
668
699
  const url = command.urls[urlIndex];
669
700
  if (urlIndex > 0) spinner.text = `Looking up collection ${urlIndex + 1}/${command.urls.length}...`;
670
- let collection = null;
671
- try {
672
- collection = await effectiveDeps.lookupObject(url, {
673
- documentLoader: initialAuthLoader ?? initialDocumentLoader,
674
- contextLoader,
675
- userAgent: command.userAgent
676
- });
677
- } catch (error) {
678
- if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
701
+ const result = await lookupDiagnosed(url, {
702
+ documentLoader: initialAuthLoader ?? initialDocumentLoader,
703
+ contextLoader,
704
+ userAgent: command.userAgent
705
+ });
706
+ const collection = result.object;
707
+ if (collection == null) {
708
+ if (result.thrownError instanceof TimeoutError || result.failure?.error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
679
709
  else {
680
710
  spinner.fail(`Failed to fetch object: ${colors.red(url)}.`);
681
- printLookupFailureHint(authLoader, error);
711
+ printError(message$1`${text(describeLookupFailure(result.failure, authLoader != null).message)}`);
682
712
  }
683
713
  await finalizeAndExit(1);
684
714
  return;
685
715
  }
686
- if (collection == null) {
687
- spinner.fail(`Failed to fetch object: ${colors.red(url)}.`);
688
- if (authLoader == null) printError(message$1`It may be a private object. Try with ${optionNames(["-a", "--authorized-fetch"])}.`);
689
- await finalizeAndExit(1);
690
- return;
691
- }
692
716
  if (!(collection instanceof Collection)) {
693
717
  spinner.fail(`Not a collection: ${colors.red(url)}. The -t/--traverse option requires a collection.`);
694
718
  await finalizeAndExit(1);
695
719
  return;
696
720
  }
697
721
  spinner.succeed(`Fetched collection: ${colors.green(url)}.`);
722
+ const diagnostics = createLookupDiagnostics(authLoader ?? documentLoader, contextLoader);
698
723
  try {
699
724
  if (command.reverse) {
700
725
  const { items: traversedItems, error: traversalError } = await collectAsyncItems(effectiveDeps.traverseCollection(collection, {
701
- documentLoader: authLoader ?? documentLoader,
702
- contextLoader,
726
+ documentLoader: diagnostics.documentLoader,
727
+ contextLoader: diagnostics.contextLoader,
703
728
  suppressError: command.suppressErrors
704
729
  }));
705
730
  for (let index = traversedItems.length - 1; index >= 0; index--) {
@@ -712,7 +737,14 @@ async function runLookup(command, deps = {}) {
712
737
  url,
713
738
  error
714
739
  });
715
- spinner.fail(`Failed to write output for: ${colors.red(url)}.`);
740
+ if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
741
+ else {
742
+ spinner.fail(`Failed to write output for: ${colors.red(url)}.`);
743
+ printError(message$1`${text(describeLookupFailure({
744
+ error,
745
+ source: "other"
746
+ }, authLoader != null).message)}`);
747
+ }
716
748
  await finalizeAndExit(1);
717
749
  return;
718
750
  }
@@ -720,8 +752,8 @@ async function runLookup(command, deps = {}) {
720
752
  }
721
753
  if (traversalError != null) throw traversalError;
722
754
  } else for await (const item of effectiveDeps.traverseCollection(collection, {
723
- documentLoader: authLoader ?? documentLoader,
724
- contextLoader,
755
+ documentLoader: diagnostics.documentLoader,
756
+ contextLoader: diagnostics.contextLoader,
725
757
  suppressError: command.suppressErrors
726
758
  })) {
727
759
  try {
@@ -732,23 +764,38 @@ async function runLookup(command, deps = {}) {
732
764
  url,
733
765
  error
734
766
  });
735
- spinner.fail(`Failed to write output for: ${colors.red(url)}.`);
767
+ if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
768
+ else {
769
+ spinner.fail(`Failed to write output for: ${colors.red(url)}.`);
770
+ printError(message$1`${text(describeLookupFailure({
771
+ error,
772
+ source: "other"
773
+ }, authLoader != null).message)}`);
774
+ }
736
775
  await finalizeAndExit(1);
737
776
  return;
738
777
  }
739
778
  totalItems++;
779
+ diagnostics.clearFailures();
740
780
  }
741
781
  } catch (error) {
742
782
  logger.error("Failed to complete the traversal for {url}: {error}", {
743
783
  url,
744
784
  error
745
785
  });
746
- if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
786
+ const failure = diagnostics.getContextFailure() ?? diagnostics.getObjectFailure() ?? {
787
+ error,
788
+ source: "other"
789
+ };
790
+ if (error instanceof TimeoutError || failure.error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
747
791
  else {
748
792
  spinner.fail(`Failed to complete the traversal for: ${colors.red(url)}.`);
749
- const hint = getLookupFailureHint(error);
750
- if (shouldSuggestSuppressErrorsForLookupFailure(authLoader, hint)) printError(message$1`Use the ${optionNames(["-S", "--suppress-errors"])} option to suppress partial errors.`);
751
- else printLookupFailureHint(authLoader, error);
793
+ if (isPrivateAddressError(failure.error)) printLookupFailureHint(authLoader, failure.error);
794
+ else {
795
+ const diagnostic = describeLookupFailure(failure, authLoader != null);
796
+ printError(message$1`${text(diagnostic.message)}`);
797
+ if (!diagnostic.suggestsAuthorizedFetch && !command.suppressErrors) printError(message$1`Use the -S/--suppress-errors option to suppress partial errors.`);
798
+ }
752
799
  }
753
800
  await finalizeAndExit(1);
754
801
  return;
@@ -758,31 +805,24 @@ async function runLookup(command, deps = {}) {
758
805
  await finalizeAndExit(0);
759
806
  return;
760
807
  }
761
- const promises = [];
762
- for (const url of command.urls) promises.push(effectiveDeps.lookupObject(url, {
808
+ const objects = await Promise.all(command.urls.map((url) => lookupDiagnosed(url, {
763
809
  documentLoader: initialAuthLoader ?? initialDocumentLoader,
764
810
  contextLoader,
765
811
  userAgent: command.userAgent
766
- }).catch((error) => {
767
- if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
768
- throw error;
769
- }));
770
- let objects = [];
771
- try {
772
- objects = await Promise.all(promises);
773
- } catch (_error) {
774
- await finalizeAndExit(1);
775
- return;
776
- }
812
+ })));
777
813
  spinner.stop();
778
814
  let success = true;
779
815
  let printedCount = 0;
780
816
  const successfulObjects = [];
781
- for (const [i, obj] of objects.entries()) {
817
+ for (const [i, result] of objects.entries()) {
818
+ const obj = result.object;
782
819
  const url = command.urls[i];
783
820
  if (obj == null) {
784
- spinner.fail(`Failed to fetch ${colors.red(url)}`);
785
- if (authLoader == null) printError(message$1`It may be a private object. Try with ${optionNames(["-a", "--authorized-fetch"])}.`);
821
+ if (result.thrownError instanceof TimeoutError || result.failure?.error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
822
+ else {
823
+ spinner.fail(`Failed to fetch ${colors.red(url)}`);
824
+ printError(message$1`${text(describeLookupFailure(result.failure, authLoader != null).message)}`);
825
+ }
786
826
  success = false;
787
827
  } else {
788
828
  spinner.succeed(`Fetched object: ${colors.green(url)}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/cli",
3
- "version": "2.2.14-dev.2082+79cd52e9",
3
+ "version": "2.2.14",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "README.md",
@@ -87,20 +87,21 @@
87
87
  "smol-toml": "^1.6.0",
88
88
  "srvx": "^0.8.7",
89
89
  "valibot": "^1.2.0",
90
- "@fedify/init": "2.2.14-dev.2082+79cd52e9",
91
- "@fedify/fedify": "2.2.14-dev.2082+79cd52e9",
92
- "@fedify/relay": "2.2.14-dev.2082+79cd52e9",
93
- "@fedify/sqlite": "2.2.14-dev.2082+79cd52e9",
94
- "@fedify/vocab": "2.2.14-dev.2082+79cd52e9",
95
- "@fedify/vocab-runtime": "2.2.14-dev.2082+79cd52e9",
96
- "@fedify/webfinger": "2.2.14-dev.2082+79cd52e9",
97
- "@fedify/vocab-tools": "2.2.14-dev.2082+79cd52e9"
90
+ "@fedify/fedify": "2.2.14",
91
+ "@fedify/relay": "2.2.14",
92
+ "@fedify/sqlite": "2.2.14",
93
+ "@fedify/vocab-runtime": "2.2.14",
94
+ "@fedify/init": "2.2.14",
95
+ "@fedify/vocab-tools": "2.2.14",
96
+ "@fedify/webfinger": "2.2.14",
97
+ "@fedify/vocab": "2.2.14"
98
98
  },
99
99
  "devDependencies": {
100
100
  "@types/bun": "^1.2.23",
101
101
  "@types/node": "^22.17.0",
102
102
  "tsdown": "^0.21.6",
103
- "typescript": "^6.0.0"
103
+ "typescript": "^6.0.0",
104
+ "@fedify/fixture": "2.0.0"
104
105
  },
105
106
  "scripts": {
106
107
  "build:self": "tsdown",