defuddle icon indicating copy to clipboard operation
defuddle copied to clipboard

Library writes raw stack traces to console.error, which corrupts inline-viewport TUIs; profileStep is unguarded and the null return is undocumented

Open mjy23823-coder opened this issue 3 weeks ago • 0 comments

Environment

  |   -- | -- defuddle | 0.19.2 linkedom | 0.18.13 Node | 22 (bundled runtime) Host | a terminal UI app using an inline viewport (incremental redraw of its own lines)

options.debug already exists and gates some of this, but the failure paths above are unconditional.

Problem 2 — profileStep lets exceptions escape

// dist/defuddle.js:685-692
const profileStep = (name, fn) => {
   if (!doProfile)
       return fn();            // line 687 — direct call, no try/catch
   const t = performance.now();
   const result = fn();        // line 689 — same
   profile[name] = Math.round(performance.now() - t);
   return result;
};

Every step runs through this wrapper, so a throw from any of them (cloneDocument, flattenShadowRoots, findMainContent, standardizeContent, …) unwinds straight to the outer catch, losing which step failed. A DOM traversal that throws — e.g. linkedom query on a cloned document whose nodes were dropped while sanitising a bot-blocked or empty page — takes the whole extraction down.

Problem 3 — silent null return is a footgun

The outer catch returns null without telling the caller anything. Callers reasonably assume an object:

const result = Defuddle(document, url, { markdown: true });
return result.content;                  // TypeError: Cannot read properties of null

That TypeError replaces the real cause, so the actual extraction error is only visible in the console line above — exactly the output that a TUI host cannot render.

Suggested changes

  1. Never write to the console from library code. Accept a logger, default to silent:

    export interface DefuddleOptions {
     onLog?: (level: 'debug' | 'warn' | 'error', message: string, error?: unknown) => void;
     // or: logger?: { debug(...a): void; warn(...a): void; error(...a): void };
    }

    With no logger supplied, drop the message. Keep the existing debug flag as sugar over the same channel.

  2. Guard profileStep so the failing step is named:

    const profileStep = (name, fn) => {
     const t = doProfile ? performance.now() : 0;
     try {
       const result = fn();
       if (doProfile) profile[name] = Math.round(performance.now() - t);
       return result;
    } catch (error) {
       this.log?.('error', `defuddle: step "${name}" failed`, error);
       throw new Error(`defuddle: step "${name}" failed: ${(error as Error)?.message ?? error}`, { cause: error });
    }
    };
  3. Either throw or document the null. Preferred: throw a typed DefuddleError. If null must stay for compatibility, mark the return type DefuddleResult | null in the public types so callers are forced to check.

Not asking for

Behaviour changes to extraction quality or selectors — this is only about error reporting and the null contract.

Happy to submit a PR for 1 and 2 against 0.19.x if that helps; the reporter is the maintainer of a pi plugin (@pi-plugins/webfetch) that calls Defuddle(document, url, { markdown: true }) and wraps it in its own error type, so we can validate the fix end to end.

mjy23823-coder avatar Nov 11 '26 16:11 mjy23823-coder