API: extraction/fetch

better-web-search-mcp


better-web-search-mcp / extraction/fetch

extraction/fetch#

Classes#

FetchTimeoutError#

Defined in: src/extraction/fetch.ts:55

A fetch that was aborted by the timeout.

Extends#

  • Error

Constructors#

Constructor#

new FetchTimeoutError(url, timeoutMs): FetchTimeoutError

Defined in: src/extraction/fetch.ts:56

Parameters#
ParameterType
urlstring
timeoutMsnumber
Returns#

FetchTimeoutError

Overrides#

Error.constructor

Properties#

PropertyModifierTypeDescriptionInherited fromDefined in
cause?publicunknown-Error.causenode_modules/typescript/lib/lib.es2022.error.d.ts:26
messagepublicstring-Error.messagenode_modules/typescript/lib/lib.es5.d.ts:1077
namepublicstring-Error.namenode_modules/typescript/lib/lib.es5.d.ts:1076
stack?publicstring-Error.stacknode_modules/typescript/lib/lib.es5.d.ts:1078
stackTraceLimitstaticnumberThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.Error.stackTraceLimitnode_modules/@types/node/globals.d.ts:68

Methods#

captureStackTrace()#

static captureStackTrace(targetObject, constructorOpt?): void

Defined in: node_modules/@types/node/globals.d.ts:52

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters#
ParameterType
targetObjectobject
constructorOpt?Function
Returns#

void

Inherited from#

Error.captureStackTrace

prepareStackTrace()#

static prepareStackTrace(err, stackTraces): any

Defined in: node_modules/@types/node/globals.d.ts:56

Parameters#
ParameterType
errError
stackTracesCallSite[]
Returns#

any

See#

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from#

Error.prepareStackTrace


NonHtmlError#

Defined in: src/extraction/fetch.ts:71

A fetch that was rejected because the response is not HTML.

Extends#

  • Error

Constructors#

Constructor#

new NonHtmlError(url, contentType): NonHtmlError

Defined in: src/extraction/fetch.ts:72

Parameters#
ParameterType
urlstring
contentTypestring
Returns#

NonHtmlError

Overrides#

Error.constructor

Properties#

PropertyModifierTypeDescriptionInherited fromDefined in
cause?publicunknown-Error.causenode_modules/typescript/lib/lib.es2022.error.d.ts:26
messagepublicstring-Error.messagenode_modules/typescript/lib/lib.es5.d.ts:1077
namepublicstring-Error.namenode_modules/typescript/lib/lib.es5.d.ts:1076
stack?publicstring-Error.stacknode_modules/typescript/lib/lib.es5.d.ts:1078
stackTraceLimitstaticnumberThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.Error.stackTraceLimitnode_modules/@types/node/globals.d.ts:68

Methods#

captureStackTrace()#

static captureStackTrace(targetObject, constructorOpt?): void

Defined in: node_modules/@types/node/globals.d.ts:52

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters#
ParameterType
targetObjectobject
constructorOpt?Function
Returns#

void

Inherited from#

Error.captureStackTrace

prepareStackTrace()#

static prepareStackTrace(err, stackTraces): any

Defined in: node_modules/@types/node/globals.d.ts:56

Parameters#
ParameterType
errError
stackTracesCallSite[]
Returns#

any

See#

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from#

Error.prepareStackTrace


TooManyRedirectsError#

Defined in: src/extraction/fetch.ts:63

A redirect chain that exceeded MAX_REDIRECTS.

Extends#

  • Error

Constructors#

Constructor#

new TooManyRedirectsError(url): TooManyRedirectsError

Defined in: src/extraction/fetch.ts:64

Parameters#
ParameterType
urlstring
Returns#

TooManyRedirectsError

Overrides#

Error.constructor

Properties#

PropertyModifierTypeDescriptionInherited fromDefined in
cause?publicunknown-Error.causenode_modules/typescript/lib/lib.es2022.error.d.ts:26
messagepublicstring-Error.messagenode_modules/typescript/lib/lib.es5.d.ts:1077
namepublicstring-Error.namenode_modules/typescript/lib/lib.es5.d.ts:1076
stack?publicstring-Error.stacknode_modules/typescript/lib/lib.es5.d.ts:1078
stackTraceLimitstaticnumberThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.Error.stackTraceLimitnode_modules/@types/node/globals.d.ts:68

Methods#

captureStackTrace()#

static captureStackTrace(targetObject, constructorOpt?): void

Defined in: node_modules/@types/node/globals.d.ts:52

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters#
ParameterType
targetObjectobject
constructorOpt?Function
Returns#

void

Inherited from#

Error.captureStackTrace

prepareStackTrace()#

static prepareStackTrace(err, stackTraces): any

Defined in: node_modules/@types/node/globals.d.ts:56

Parameters#
ParameterType
errError
stackTracesCallSite[]
Returns#

any

See#

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from#

Error.prepareStackTrace

Interfaces#

FetchedPage#

Defined in: src/extraction/fetch.ts:43

The result of a successful page fetch.

Properties#

PropertyTypeDescriptionDefined in
headersRecord<string, string>Response headers as a plain record.src/extraction/fetch.ts:47
htmlstringThe raw HTML body (truncated to MAX_BODY_BYTES).src/extraction/fetch.ts:45
statusnumberHTTP status code.src/extraction/fetch.ts:49
urlstringThe final URL after redirects.src/extraction/fetch.ts:51

FetchPageOptions#

Defined in: src/extraction/fetch.ts:28

Options controlling a single page fetch.

Properties#

PropertyTypeDescriptionDefined in
allowPrivateHosts?booleanSkip the SSRF guard. Only for tests that point at a local fixture server; never set this for URLs that came from a search result or an agent.src/extraction/fetch.ts:37
headers?Record<string, string>Extra request headers merged over the defaults.src/extraction/fetch.ts:32
ssrf?SsrfDepsInjectable DNS resolution, forwarded to the SSRF guard in tests.src/extraction/fetch.ts:39
timeoutMs?numberTimeout in milliseconds before the request is aborted. Default 10s.src/extraction/fetch.ts:30

Variables#

DEFAULT_TIMEOUT_MS#

const DEFAULT_TIMEOUT_MS: 10000 = 10_000

Defined in: src/extraction/fetch.ts:16

Default timeout for a single page fetch, in milliseconds.


MAX_BODY_BYTES#

const MAX_BODY_BYTES: number

Defined in: src/extraction/fetch.ts:19

Maximum number of bytes of body we are willing to read.


MAX_REDIRECTS#

const MAX_REDIRECTS: 5 = 5

Defined in: src/extraction/fetch.ts:25

Maximum number of redirects to follow before giving up.


USER_AGENT#

const USER_AGENT: "BetterWebSearch-MCP/1.0" = "BetterWebSearch-MCP/1.0"

Defined in: src/extraction/fetch.ts:22

User agent advertised to servers.

Functions#

fetchPage()#

fetchPage(url, opts?): Promise<FetchedPage>

Defined in: src/extraction/fetch.ts:90

Fetch a page as HTML.

  • Uses native fetch with an AbortController timeout.
  • Sends the BetterWebSearch-MCP/1.0 user agent and follows redirects.
  • Guards on content-type: only text/html is accepted; a missing content-type is allowed through.
  • Caps the body at MAX_BODY_BYTES by reading an arrayBuffer slice.

Throws FetchTimeoutError on timeout and NonHtmlError when the content-type is not HTML. Other network errors propagate as-is.

Parameters#

ParameterType
urlstring
optsFetchPageOptions

Returns#

Promise<FetchedPage>