API: extraction/fetch
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#
| Parameter | Type |
|---|---|
url | string |
timeoutMs | number |
Returns#
Overrides#
Error.constructor
Properties#
Methods#
captureStackTrace()#
staticcaptureStackTrace(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#
| Parameter | Type |
|---|---|
targetObject | object |
constructorOpt? | Function |
Returns#
void
Inherited from#
Error.captureStackTrace
prepareStackTrace()#
staticprepareStackTrace(err,stackTraces):any
Defined in: node_modules/@types/node/globals.d.ts:56
Parameters#
| Parameter | Type |
|---|---|
err | Error |
stackTraces | CallSite[] |
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#
| Parameter | Type |
|---|---|
url | string |
contentType | string |
Returns#
Overrides#
Error.constructor
Properties#
Methods#
captureStackTrace()#
staticcaptureStackTrace(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#
| Parameter | Type |
|---|---|
targetObject | object |
constructorOpt? | Function |
Returns#
void
Inherited from#
Error.captureStackTrace
prepareStackTrace()#
staticprepareStackTrace(err,stackTraces):any
Defined in: node_modules/@types/node/globals.d.ts:56
Parameters#
| Parameter | Type |
|---|---|
err | Error |
stackTraces | CallSite[] |
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#
| Parameter | Type |
|---|---|
url | string |
Returns#
Overrides#
Error.constructor
Properties#
Methods#
captureStackTrace()#
staticcaptureStackTrace(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#
| Parameter | Type |
|---|---|
targetObject | object |
constructorOpt? | Function |
Returns#
void
Inherited from#
Error.captureStackTrace
prepareStackTrace()#
staticprepareStackTrace(err,stackTraces):any
Defined in: node_modules/@types/node/globals.d.ts:56
Parameters#
| Parameter | Type |
|---|---|
err | Error |
stackTraces | CallSite[] |
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#
| Property | Type | Description | Defined in |
|---|---|---|---|
headers | Record<string, string> | Response headers as a plain record. | src/extraction/fetch.ts:47 |
html | string | The raw HTML body (truncated to MAX_BODY_BYTES). | src/extraction/fetch.ts:45 |
status | number | HTTP status code. | src/extraction/fetch.ts:49 |
url | string | The final URL after redirects. | src/extraction/fetch.ts:51 |
FetchPageOptions#
Defined in: src/extraction/fetch.ts:28
Options controlling a single page fetch.
Properties#
| Property | Type | Description | Defined in |
|---|---|---|---|
allowPrivateHosts? | boolean | Skip 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? | SsrfDeps | Injectable DNS resolution, forwarded to the SSRF guard in tests. | src/extraction/fetch.ts:39 |
timeoutMs? | number | Timeout in milliseconds before the request is aborted. Default 10s. | src/extraction/fetch.ts:30 |
Variables#
DEFAULT_TIMEOUT_MS#
constDEFAULT_TIMEOUT_MS:10000=10_000
Defined in: src/extraction/fetch.ts:16
Default timeout for a single page fetch, in milliseconds.
MAX_BODY_BYTES#
constMAX_BODY_BYTES:number
Defined in: src/extraction/fetch.ts:19
Maximum number of bytes of body we are willing to read.
MAX_REDIRECTS#
constMAX_REDIRECTS:5=5
Defined in: src/extraction/fetch.ts:25
Maximum number of redirects to follow before giving up.
USER_AGENT#
constUSER_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
fetchwith anAbortControllertimeout. - Sends the
BetterWebSearch-MCP/1.0user agent and follows redirects. - Guards on
content-type: onlytext/htmlis accepted; a missing content-type is allowed through. - Caps the body at MAX_BODY_BYTES by reading an
arrayBufferslice.
Throws FetchTimeoutError on timeout and NonHtmlError when the content-type is not HTML. Other network errors propagate as-is.
Parameters#
| Parameter | Type |
|---|---|
url | string |
opts | FetchPageOptions |
Returns#
Promise<FetchedPage>