@fedify/cli 2.1.24 → 2.1.25
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 +1 -1
- package/dist/diagnostics.js +70 -0
- package/dist/lookup.js +84 -44
- package/package.json +11 -10
package/dist/deno.js
CHANGED
|
@@ -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
|
@@ -5,8 +5,9 @@ import { configureLogging } from "./log.js";
|
|
|
5
5
|
import { createTunnelServiceOption, userAgentOption } from "./options.js";
|
|
6
6
|
import { spawnTemporaryServer } from "./tempserver.js";
|
|
7
7
|
import { colorEnabled, colors, formatObject } from "./utils.js";
|
|
8
|
+
import { createLookupDiagnostics, describeLookupFailure } from "./diagnostics.js";
|
|
8
9
|
import { renderImages } from "./imagerenderer.js";
|
|
9
|
-
import { argument, choice, command, constant, flag, float, integer, map, merge, message, multiple, object, option, optionNames, optional, or, string, withDefault } from "@optique/core";
|
|
10
|
+
import { argument, choice, command, constant, flag, float, integer, map, merge, message, multiple, object, option, optionNames, optional, or, string, text, withDefault } from "@optique/core";
|
|
10
11
|
import { path, printError } from "@optique/run";
|
|
11
12
|
import { createWriteStream } from "node:fs";
|
|
12
13
|
import process from "node:process";
|
|
@@ -272,6 +273,9 @@ function wrapDocumentLoaderWithTimeout(loader, timeoutSeconds) {
|
|
|
272
273
|
return loader(url, {
|
|
273
274
|
...options,
|
|
274
275
|
signal
|
|
276
|
+
}).catch((error) => {
|
|
277
|
+
if (signal?.aborted) throw signal.reason;
|
|
278
|
+
throw error;
|
|
275
279
|
}).finally(() => clearTimeoutSignal(signal));
|
|
276
280
|
};
|
|
277
281
|
}
|
|
@@ -282,10 +286,11 @@ function handleTimeoutError(spinner, timeoutSeconds, url) {
|
|
|
282
286
|
}
|
|
283
287
|
function isPrivateAddressError(error) {
|
|
284
288
|
const lowerMessage = (error instanceof Error ? error.message : String(error)).toLowerCase();
|
|
285
|
-
if (error instanceof UrlError) return lowerMessage.includes("invalid or private address") || lowerMessage.includes("localhost is not allowed");
|
|
289
|
+
if (error instanceof UrlError) return error.reason === "disallowed" && (lowerMessage.includes("invalid or private address") || lowerMessage.includes("localhost is not allowed"));
|
|
286
290
|
return lowerMessage.includes("private address") || lowerMessage.includes("private ip") || lowerMessage.includes("localhost") || lowerMessage.includes("loopback");
|
|
287
291
|
}
|
|
288
292
|
function getLookupFailureHint(error, options = {}) {
|
|
293
|
+
if (error instanceof UrlError && error.reason === "dns") return "dns";
|
|
289
294
|
if (isPrivateAddressError(error)) return options.recursive ? "recursive-private-address" : "private-address";
|
|
290
295
|
return "authorized-fetch";
|
|
291
296
|
}
|
|
@@ -299,6 +304,9 @@ function printLookupFailureHint(authLoader, error, options = {}) {
|
|
|
299
304
|
const hint = getLookupFailureHint(error, options);
|
|
300
305
|
if (!shouldPrintLookupFailureHint(authLoader, hint)) return;
|
|
301
306
|
switch (hint) {
|
|
307
|
+
case "dns":
|
|
308
|
+
printError(message`DNS lookup failed. Check the hostname and network connectivity.`);
|
|
309
|
+
return;
|
|
302
310
|
case "private-address":
|
|
303
311
|
printError(message`The URL appears to be private or localhost. Try with -p/--allow-private-address.`);
|
|
304
312
|
return;
|
|
@@ -374,6 +382,29 @@ async function runLookup(command, deps = {}) {
|
|
|
374
382
|
exit: (code) => process.exit(code),
|
|
375
383
|
...deps
|
|
376
384
|
};
|
|
385
|
+
const lookupDiagnosed = async (identifier, options) => {
|
|
386
|
+
const diagnostics = createLookupDiagnostics(options.documentLoader, options.contextLoader);
|
|
387
|
+
try {
|
|
388
|
+
const object = await effectiveDeps.lookupObject(identifier, {
|
|
389
|
+
...options,
|
|
390
|
+
...diagnostics
|
|
391
|
+
});
|
|
392
|
+
return {
|
|
393
|
+
object,
|
|
394
|
+
failure: object == null ? diagnostics.getObjectFailure() ?? diagnostics.getContextFailure() : void 0,
|
|
395
|
+
thrownError: void 0
|
|
396
|
+
};
|
|
397
|
+
} catch (error) {
|
|
398
|
+
return {
|
|
399
|
+
object: null,
|
|
400
|
+
thrownError: error,
|
|
401
|
+
failure: diagnostics.getContextFailure() ?? diagnostics.getObjectFailure() ?? {
|
|
402
|
+
error,
|
|
403
|
+
source: "other"
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
};
|
|
377
408
|
if (command.urls.length < 1) {
|
|
378
409
|
printError(message`At least one URL or actor handle must be provided.`);
|
|
379
410
|
effectiveDeps.exit(1);
|
|
@@ -631,39 +662,33 @@ async function runLookup(command, deps = {}) {
|
|
|
631
662
|
for (let urlIndex = 0; urlIndex < command.urls.length; urlIndex++) {
|
|
632
663
|
const url = command.urls[urlIndex];
|
|
633
664
|
if (urlIndex > 0) spinner.text = `Looking up collection ${urlIndex + 1}/${command.urls.length}...`;
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
|
|
665
|
+
const result = await lookupDiagnosed(url, {
|
|
666
|
+
documentLoader: initialAuthLoader ?? initialDocumentLoader,
|
|
667
|
+
contextLoader,
|
|
668
|
+
userAgent: command.userAgent
|
|
669
|
+
});
|
|
670
|
+
const collection = result.object;
|
|
671
|
+
if (collection == null) {
|
|
672
|
+
if (result.thrownError instanceof TimeoutError || result.failure?.error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
|
|
643
673
|
else {
|
|
644
674
|
spinner.fail(`Failed to fetch object: ${colors.red(url)}.`);
|
|
645
|
-
|
|
675
|
+
printError(message`${text(describeLookupFailure(result.failure, authLoader != null).message)}`);
|
|
646
676
|
}
|
|
647
677
|
await finalizeAndExit(1);
|
|
648
678
|
return;
|
|
649
679
|
}
|
|
650
|
-
if (collection == null) {
|
|
651
|
-
spinner.fail(`Failed to fetch object: ${colors.red(url)}.`);
|
|
652
|
-
if (authLoader == null) printError(message`It may be a private object. Try with -a/--authorized-fetch.`);
|
|
653
|
-
await finalizeAndExit(1);
|
|
654
|
-
return;
|
|
655
|
-
}
|
|
656
680
|
if (!(collection instanceof Collection)) {
|
|
657
681
|
spinner.fail(`Not a collection: ${colors.red(url)}. The -t/--traverse option requires a collection.`);
|
|
658
682
|
await finalizeAndExit(1);
|
|
659
683
|
return;
|
|
660
684
|
}
|
|
661
685
|
spinner.succeed(`Fetched collection: ${colors.green(url)}.`);
|
|
686
|
+
const diagnostics = createLookupDiagnostics(authLoader ?? documentLoader, contextLoader);
|
|
662
687
|
try {
|
|
663
688
|
if (command.reverse) {
|
|
664
689
|
const { items: traversedItems, error: traversalError } = await collectAsyncItems(effectiveDeps.traverseCollection(collection, {
|
|
665
|
-
documentLoader:
|
|
666
|
-
contextLoader,
|
|
690
|
+
documentLoader: diagnostics.documentLoader,
|
|
691
|
+
contextLoader: diagnostics.contextLoader,
|
|
667
692
|
suppressError: command.suppressErrors
|
|
668
693
|
}));
|
|
669
694
|
for (let index = traversedItems.length - 1; index >= 0; index--) {
|
|
@@ -676,7 +701,14 @@ async function runLookup(command, deps = {}) {
|
|
|
676
701
|
url,
|
|
677
702
|
error
|
|
678
703
|
});
|
|
679
|
-
|
|
704
|
+
if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
|
|
705
|
+
else {
|
|
706
|
+
spinner.fail(`Failed to write output for: ${colors.red(url)}.`);
|
|
707
|
+
printError(message`${text(describeLookupFailure({
|
|
708
|
+
error,
|
|
709
|
+
source: "other"
|
|
710
|
+
}, authLoader != null).message)}`);
|
|
711
|
+
}
|
|
680
712
|
await finalizeAndExit(1);
|
|
681
713
|
return;
|
|
682
714
|
}
|
|
@@ -684,8 +716,8 @@ async function runLookup(command, deps = {}) {
|
|
|
684
716
|
}
|
|
685
717
|
if (traversalError != null) throw traversalError;
|
|
686
718
|
} else for await (const item of effectiveDeps.traverseCollection(collection, {
|
|
687
|
-
documentLoader:
|
|
688
|
-
contextLoader,
|
|
719
|
+
documentLoader: diagnostics.documentLoader,
|
|
720
|
+
contextLoader: diagnostics.contextLoader,
|
|
689
721
|
suppressError: command.suppressErrors
|
|
690
722
|
})) {
|
|
691
723
|
try {
|
|
@@ -696,23 +728,38 @@ async function runLookup(command, deps = {}) {
|
|
|
696
728
|
url,
|
|
697
729
|
error
|
|
698
730
|
});
|
|
699
|
-
|
|
731
|
+
if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
|
|
732
|
+
else {
|
|
733
|
+
spinner.fail(`Failed to write output for: ${colors.red(url)}.`);
|
|
734
|
+
printError(message`${text(describeLookupFailure({
|
|
735
|
+
error,
|
|
736
|
+
source: "other"
|
|
737
|
+
}, authLoader != null).message)}`);
|
|
738
|
+
}
|
|
700
739
|
await finalizeAndExit(1);
|
|
701
740
|
return;
|
|
702
741
|
}
|
|
703
742
|
totalItems++;
|
|
743
|
+
diagnostics.clearFailures();
|
|
704
744
|
}
|
|
705
745
|
} catch (error) {
|
|
706
746
|
logger.error("Failed to complete the traversal for {url}: {error}", {
|
|
707
747
|
url,
|
|
708
748
|
error
|
|
709
749
|
});
|
|
710
|
-
|
|
750
|
+
const failure = diagnostics.getContextFailure() ?? diagnostics.getObjectFailure() ?? {
|
|
751
|
+
error,
|
|
752
|
+
source: "other"
|
|
753
|
+
};
|
|
754
|
+
if (error instanceof TimeoutError || failure.error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
|
|
711
755
|
else {
|
|
712
756
|
spinner.fail(`Failed to complete the traversal for: ${colors.red(url)}.`);
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
757
|
+
if (isPrivateAddressError(failure.error)) printLookupFailureHint(authLoader, failure.error);
|
|
758
|
+
else {
|
|
759
|
+
const diagnostic = describeLookupFailure(failure, authLoader != null);
|
|
760
|
+
printError(message`${text(diagnostic.message)}`);
|
|
761
|
+
if (!diagnostic.suggestsAuthorizedFetch && !command.suppressErrors) printError(message`Use the -S/--suppress-errors option to suppress partial errors.`);
|
|
762
|
+
}
|
|
716
763
|
}
|
|
717
764
|
await finalizeAndExit(1);
|
|
718
765
|
return;
|
|
@@ -722,31 +769,24 @@ async function runLookup(command, deps = {}) {
|
|
|
722
769
|
await finalizeAndExit(0);
|
|
723
770
|
return;
|
|
724
771
|
}
|
|
725
|
-
const
|
|
726
|
-
for (const url of command.urls) promises.push(effectiveDeps.lookupObject(url, {
|
|
772
|
+
const objects = await Promise.all(command.urls.map((url) => lookupDiagnosed(url, {
|
|
727
773
|
documentLoader: initialAuthLoader ?? initialDocumentLoader,
|
|
728
774
|
contextLoader,
|
|
729
775
|
userAgent: command.userAgent
|
|
730
|
-
})
|
|
731
|
-
if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
|
|
732
|
-
throw error;
|
|
733
|
-
}));
|
|
734
|
-
let objects = [];
|
|
735
|
-
try {
|
|
736
|
-
objects = await Promise.all(promises);
|
|
737
|
-
} catch (_error) {
|
|
738
|
-
await finalizeAndExit(1);
|
|
739
|
-
return;
|
|
740
|
-
}
|
|
776
|
+
})));
|
|
741
777
|
spinner.stop();
|
|
742
778
|
let success = true;
|
|
743
779
|
let printedCount = 0;
|
|
744
780
|
const successfulObjects = [];
|
|
745
|
-
for (const [i,
|
|
781
|
+
for (const [i, result] of objects.entries()) {
|
|
782
|
+
const obj = result.object;
|
|
746
783
|
const url = command.urls[i];
|
|
747
784
|
if (obj == null) {
|
|
748
|
-
|
|
749
|
-
|
|
785
|
+
if (result.thrownError instanceof TimeoutError || result.failure?.error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
|
|
786
|
+
else {
|
|
787
|
+
spinner.fail(`Failed to fetch ${colors.red(url)}`);
|
|
788
|
+
printError(message`${text(describeLookupFailure(result.failure, authLoader != null).message)}`);
|
|
789
|
+
}
|
|
750
790
|
success = false;
|
|
751
791
|
} else {
|
|
752
792
|
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.1.
|
|
3
|
+
"version": "2.1.25",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"README.md",
|
|
@@ -86,20 +86,21 @@
|
|
|
86
86
|
"smol-toml": "^1.6.0",
|
|
87
87
|
"srvx": "^0.8.7",
|
|
88
88
|
"valibot": "^1.2.0",
|
|
89
|
-
"@fedify/relay": "2.1.
|
|
90
|
-
"@fedify/
|
|
91
|
-
"@fedify/
|
|
92
|
-
"@fedify/vocab": "2.1.
|
|
93
|
-
"@fedify/
|
|
94
|
-
"@fedify/
|
|
95
|
-
"@fedify/webfinger": "2.1.
|
|
96
|
-
"@fedify/vocab-tools": "2.1.
|
|
89
|
+
"@fedify/relay": "2.1.25",
|
|
90
|
+
"@fedify/fedify": "2.1.25",
|
|
91
|
+
"@fedify/sqlite": "2.1.25",
|
|
92
|
+
"@fedify/vocab": "2.1.25",
|
|
93
|
+
"@fedify/vocab-runtime": "2.1.25",
|
|
94
|
+
"@fedify/init": "2.1.25",
|
|
95
|
+
"@fedify/webfinger": "2.1.25",
|
|
96
|
+
"@fedify/vocab-tools": "2.1.25"
|
|
97
97
|
},
|
|
98
98
|
"devDependencies": {
|
|
99
99
|
"@types/bun": "^1.2.23",
|
|
100
100
|
"@types/node": "^22.17.0",
|
|
101
101
|
"tsdown": "^0.21.6",
|
|
102
|
-
"typescript": "^6.0.0"
|
|
102
|
+
"typescript": "^6.0.0",
|
|
103
|
+
"@fedify/fixture": "2.0.0"
|
|
103
104
|
},
|
|
104
105
|
"scripts": {
|
|
105
106
|
"build:self": "tsdown",
|