@fedify/cli 2.0.28 → 2.0.30

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.0.28";
3
+ var version = "2.0.30";
4
4
  //#endregion
5
5
  export { version };
@@ -0,0 +1,93 @@
1
+ import "@js-temporal/polyfill";
2
+ import { lookupObject } from "@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
+ /** Returns an object and a snapshot of the evidence for a failed lookup. */
34
+ async function lookupWithDiagnostics(identifier, options) {
35
+ const diagnostics = createLookupDiagnostics(options.documentLoader, options.contextLoader);
36
+ try {
37
+ const object = await lookupObject(identifier, {
38
+ ...options,
39
+ ...diagnostics
40
+ });
41
+ return Object.freeze({
42
+ object,
43
+ failure: object == null ? diagnostics.getObjectFailure() ?? diagnostics.getContextFailure() : void 0
44
+ });
45
+ } catch (error) {
46
+ return Object.freeze({
47
+ object: null,
48
+ thrownError: error,
49
+ failure: diagnostics.getContextFailure() ?? diagnostics.getObjectFailure() ?? {
50
+ error,
51
+ source: "other"
52
+ }
53
+ });
54
+ }
55
+ }
56
+ function escapeControlCharacters(message) {
57
+ return message.replace(/[\x00-\x1f\x7f-\x9f]/g, (character) => `\\x${character.charCodeAt(0).toString(16).padStart(2, "0")}`);
58
+ }
59
+ /** Describes a failure without recommending signing unless HTTP supports it. */
60
+ function describeLookupFailure(failure, authorizedFetch) {
61
+ const error = failure?.error;
62
+ const http = error instanceof FetchError ? /^(.+): HTTP (\d{3}): \1$/s.exec(error.message) : null;
63
+ if (http != null) {
64
+ const status = Number(http[2]);
65
+ const suggestsAuthorizedFetch = failure?.source === "object" && !authorizedFetch && [
66
+ 401,
67
+ 403,
68
+ 404
69
+ ].includes(status);
70
+ return {
71
+ message: escapeControlCharacters(`HTTP ${status} from ${http[1]}.`) + (suggestsAuthorizedFetch ? " It may be a private object. Try with -a/--authorized-fetch." : ""),
72
+ suggestsAuthorizedFetch
73
+ };
74
+ }
75
+ const causes = [];
76
+ const seen = /* @__PURE__ */ new Set();
77
+ let cause = error;
78
+ while (cause instanceof Error && !seen.has(cause) && causes.length < 8) {
79
+ seen.add(cause);
80
+ causes.push(cause);
81
+ 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 {
82
+ message: "Could not resolve the host in the URL. Check the URL and your network connection.",
83
+ suggestsAuthorizedFetch: false
84
+ };
85
+ cause = cause.cause;
86
+ }
87
+ return {
88
+ 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)),
89
+ suggestsAuthorizedFetch: false
90
+ };
91
+ }
92
+ //#endregion
93
+ export { createLookupDiagnostics, describeLookupFailure, lookupWithDiagnostics };
package/dist/lookup.js CHANGED
@@ -5,14 +5,15 @@ 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, lookupWithDiagnostics } from "./diagnostics.js";
8
9
  import { renderImages } from "./imagerenderer.js";
9
- import { argument, choice, command, constant, flag, float, map, merge, message, multiple, object, option, optionNames, optional, or, string, withDefault } from "@optique/core";
10
+ import { argument, choice, command, constant, flag, float, map, merge, message, multiple, object, option, optionNames, optional, or, string, text, withDefault } from "@optique/core";
10
11
  import { path, print, printError } from "@optique/run";
11
12
  import { createWriteStream } from "node:fs";
12
13
  import process from "node:process";
13
14
  import { bindConfig } from "@optique/config";
14
15
  import { generateCryptoKeyPair, getAuthenticatedDocumentLoader, respondWithObject } from "@fedify/fedify";
15
- import { Application, Collection, CryptographicKey, Object as Object$1, lookupObject, traverseCollection } from "@fedify/vocab";
16
+ import { Application, Collection, CryptographicKey, Object as Object$1, traverseCollection } from "@fedify/vocab";
16
17
  import { getLogger } from "@logtape/logtape";
17
18
  import ora from "ora";
18
19
  //#region src/lookup.ts
@@ -145,6 +146,9 @@ function wrapDocumentLoaderWithTimeout(loader, timeoutSeconds) {
145
146
  return loader(url, {
146
147
  ...options,
147
148
  signal
149
+ }).catch((error) => {
150
+ if (signal?.aborted) throw signal.reason;
151
+ throw error;
148
152
  }).finally(() => clearTimeoutSignal(signal));
149
153
  };
150
154
  }
@@ -214,56 +218,63 @@ async function runLookup(command) {
214
218
  for (let urlIndex = 0; urlIndex < command.urls.length; urlIndex++) {
215
219
  const url = command.urls[urlIndex];
216
220
  if (urlIndex > 0) spinner.text = `Looking up collection ${urlIndex + 1}/${command.urls.length}...`;
217
- let collection;
218
- try {
219
- collection = await lookupObject(url, {
220
- documentLoader: authLoader ?? documentLoader,
221
- contextLoader,
222
- userAgent: command.userAgent
223
- });
224
- } catch (error) {
225
- if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
221
+ const result = await lookupWithDiagnostics(url, {
222
+ documentLoader: authLoader ?? documentLoader,
223
+ contextLoader,
224
+ userAgent: command.userAgent
225
+ });
226
+ const collection = result.object;
227
+ if (collection == null) {
228
+ if (result.thrownError instanceof TimeoutError || result.failure?.error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
226
229
  else {
227
230
  spinner.fail(`Failed to fetch object: ${colors.red(url)}.`);
228
- if (authLoader == null) printError(message`It may be a private object. Try with -a/--authorized-fetch.`);
231
+ printError(message`${text(describeLookupFailure(result.failure, authLoader != null).message)}`);
229
232
  }
230
233
  await server?.close();
231
234
  process.exit(1);
232
235
  }
233
- if (collection == null) {
234
- spinner.fail(`Failed to fetch object: ${colors.red(url)}.`);
235
- if (authLoader == null) printError(message`It may be a private object. Try with -a/--authorized-fetch.`);
236
- await server?.close();
237
- process.exit(1);
238
- }
239
236
  if (!(collection instanceof Collection)) {
240
237
  spinner.fail(`Not a collection: ${colors.red(url)}. The -t/--traverse option requires a collection.`);
241
238
  await server?.close();
242
239
  process.exit(1);
243
240
  }
244
241
  spinner.succeed(`Fetched collection: ${colors.green(url)}.`);
242
+ const diagnostics = createLookupDiagnostics(authLoader ?? documentLoader, contextLoader);
243
+ let iterating = true;
245
244
  try {
246
245
  let collectionItems = 0;
246
+ diagnostics.clearFailures();
247
247
  for await (const item of traverseCollection(collection, {
248
- documentLoader: authLoader ?? documentLoader,
249
- contextLoader,
248
+ documentLoader: diagnostics.documentLoader,
249
+ contextLoader: diagnostics.contextLoader,
250
250
  suppressError: command.suppressErrors
251
251
  })) {
252
+ iterating = false;
252
253
  if (!command.output && (totalItems > 0 || collectionItems > 0)) print(message`${command.separator}`);
253
254
  await writeObjectToStream(item, command.output, command.format, contextLoader);
254
255
  collectionItems++;
255
256
  totalItems++;
257
+ diagnostics.clearFailures();
258
+ iterating = true;
256
259
  }
257
260
  } catch (error) {
258
261
  logger.error("Failed to complete the traversal for {url}: {error}", {
259
262
  url,
260
263
  error
261
264
  });
262
- if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
265
+ const failure = iterating ? diagnostics.getContextFailure() ?? diagnostics.getObjectFailure() ?? {
266
+ error,
267
+ source: "other"
268
+ } : {
269
+ error,
270
+ source: "other"
271
+ };
272
+ if (error instanceof TimeoutError || failure.error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
263
273
  else {
264
274
  spinner.fail(`Failed to complete the traversal for: ${colors.red(url)}.`);
265
- if (authLoader == null) printError(message`It may be a private object. Try with -a/--authorized-fetch.`);
266
- else printError(message`Use the -S/--suppress-errors option to suppress partial errors.`);
275
+ const diagnostic = describeLookupFailure(failure, authLoader != null);
276
+ printError(message`${text(diagnostic.message)}`);
277
+ if (iterating && !diagnostic.suggestsAuthorizedFetch && !command.suppressErrors) printError(message`Use the -S/--suppress-errors option to suppress partial errors.`);
267
278
  }
268
279
  await server?.close();
269
280
  process.exit(1);
@@ -273,32 +284,25 @@ async function runLookup(command) {
273
284
  await server?.close();
274
285
  process.exit(0);
275
286
  }
276
- const promises = [];
277
- for (const url of command.urls) promises.push(lookupObject(url, {
287
+ const objects = await Promise.all(command.urls.map((url) => lookupWithDiagnostics(url, {
278
288
  documentLoader: authLoader ?? documentLoader,
279
289
  contextLoader,
280
290
  userAgent: command.userAgent
281
- }).catch((error) => {
282
- if (error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
283
- throw error;
284
- }));
285
- let objects;
286
- try {
287
- objects = await Promise.all(promises);
288
- } catch (_error) {
289
- await server?.close();
290
- process.exit(1);
291
- }
291
+ })));
292
292
  spinner.stop();
293
293
  let success = true;
294
294
  let i = 0;
295
- for (const obj of objects) {
295
+ for (const result of objects) {
296
+ const obj = result.object;
296
297
  const url = command.urls[i];
297
298
  if (i > 0) print(message`${command.separator}`);
298
299
  i++;
299
300
  if (obj == null) {
300
- spinner.fail(`Failed to fetch ${colors.red(url)}`);
301
- if (authLoader == null) printError(message`It may be a private object. Try with -a/--authorized-fetch.`);
301
+ if (result.thrownError instanceof TimeoutError || result.failure?.error instanceof TimeoutError) handleTimeoutError(spinner, command.timeout, url);
302
+ else {
303
+ spinner.fail(`Failed to fetch ${colors.red(url)}`);
304
+ printError(message`${text(describeLookupFailure(result.failure, authLoader != null).message)}`);
305
+ }
302
306
  success = false;
303
307
  } else {
304
308
  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.0.28",
3
+ "version": "2.0.30",
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/sqlite": "2.0.28",
90
- "@fedify/vocab": "2.0.28",
91
- "@fedify/vocab-runtime": "2.0.28",
92
- "@fedify/vocab-tools": "2.0.28",
93
- "@fedify/webfinger": "2.0.28",
94
- "@fedify/fedify": "2.0.28",
95
- "@fedify/relay": "2.0.28",
96
- "@fedify/init": "2.0.28"
89
+ "@fedify/init": "2.0.30",
90
+ "@fedify/sqlite": "2.0.30",
91
+ "@fedify/relay": "2.0.30",
92
+ "@fedify/vocab-runtime": "2.0.30",
93
+ "@fedify/vocab-tools": "2.0.30",
94
+ "@fedify/webfinger": "2.0.30",
95
+ "@fedify/vocab": "2.0.30",
96
+ "@fedify/fedify": "2.0.30"
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",