vael:fetch
SSRF-hardened HTTP/HTTPS client and streaming atomic file downloader.
Available in the vjs (vaeljs) runtime (fetch, fetchSync, download,
downloadSync); vjs-lite (vaeljs-lite) exposes synchronous fetchSync
and downloadSync only. Never installed on globalThis — import explicitly
from "vael:fetch".
Enforces http:/https:-only URLs without embedded credentials, blocks
private, loopback, link-local, and cloud-metadata IP ranges unless
allowPrivateIps: true is specified, validates every redirect hop and strips
sensitive credentials across origins, and bounds response/download memory.
import ... from "vael:fetch"
vael-fetch (enabled by default in vaeljs-core and vaeljs-lite-core)fetch and download are V8-only (vjs). Synchronous fetchSync and downloadSync run on both vjs and vjs-lite via blocking ureq + rustls.Example
Section titled “Example”import { download, fetch, fetchSync } from "vael:fetch";
const res = await fetch("https://example.com/data.json", { timeoutMs: 10_000,});if (res.ok) { console.log(res.status, res.json());}
const syncRes = fetchSync("https://example.com/health");console.log(syncRes.status, syncRes.text());
const dl = await download("https://example.com/archive.tar.gz", "./archive.tar.gz", { overwrite: true,});console.log(dl.bytesWritten, dl.sha256);fetchvjs onlyv0.0.0-alpha.2fetchSyncv0.0.0-alpha.2downloadvjs onlyv0.0.0-alpha.2GET response directly to a sibling temporary file, verify optional SHA-256 integrity, and atomically commit to...downloadSyncv0.0.0-alpha.2GET response directly to a sibling temporary file, verify optional SHA-256 integrity, and atomically...FetchOptionsv0.0.0-alpha.2fetch and fetchSync.FetchResponsev0.0.0-alpha.2fetch and fetchSync.DownloadProgressv0.0.0-alpha.2DownloadOptions.onProgress.DownloadOptionsv0.0.0-alpha.2download and downloadSync.DownloadResultv0.0.0-alpha.2download and downloadSync.Functions
Section titled “Functions”export function fetch(url: string, options?: FetchOptions): Promise<FetchResponse>;
Perform an asynchronous SSRF-hardened HTTP/HTTPS request on a worker thread and buffer the bounded response.
Available in vjs (vaeljs) only; use fetchSync in vjs-lite.
| Name | Type | Description |
|---|---|---|
url | string | Target http: or https: URL without embedded userinfo. |
options optional | FetchOptions | Optional request method, headers, query, body, and limits. |
Promise<FetchResponse> — Promise resolving to the buffered FetchResponse.TypeError— on wrong argument types.FetchError— on invalid URL/options, SSRF block, timeout, redirect limit, body limit, or network error.
import { fetch } from "vael:fetch";
const res = await fetch("https://example.com/api", { method: "POST", json: { ping: true },});console.log(res.status, res.text());fetchSync
Section titled “fetchSync”export function fetchSync(url: string, options?: FetchOptions): FetchResponse;
Perform a synchronous SSRF-hardened HTTP/HTTPS request and buffer the bounded response.
Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite).
| Name | Type | Description |
|---|---|---|
url | string | Target http: or https: URL without embedded userinfo. |
options optional | FetchOptions | Optional request method, headers, query, body, and limits. |
FetchResponse — Buffered FetchResponse.TypeError— on wrong argument types.FetchError— on invalid URL/options, SSRF block, timeout, redirect limit, body limit, or network error.
import { fetchSync } from "vael:fetch";
const res = fetchSync("https://example.com/health");console.log(res.ok, res.status);download
Section titled “download”export function download(
url: string,
destPath: string,
options?: DownloadOptions,
): Promise<DownloadResult>;
Stream an HTTP/HTTPS GET response directly to a sibling temporary file,
verify optional SHA-256 integrity, and atomically commit to destPath on a
worker thread.
Non-2xx status codes reject with FetchError ("VAEL_FETCH_HTTP_STATUS")
without creating or modifying destPath. Available in vjs (vaeljs)
only; use downloadSync in vjs-lite.
| Name | Type | Description |
|---|---|---|
url | string | Target http: or https: URL without embedded userinfo. |
destPath | string | Destination regular file path (parent must be a non-symlink directory). |
options optional | DownloadOptions | Optional headers, timeout, size limit, SHA-256 digest, overwrite, and progress callback. |
Promise<DownloadResult> — Promise resolving to DownloadResult metadata.TypeError— on wrong argument types.FetchError— on invalid URL/options, SSRF block, non-2xx status, checksum mismatch, existing target, or I/O error.
import { download } from "vael:fetch";
const res = await download("https://example.com/release.bin", "./release.bin", { expectedSha256: "0".repeat(64), overwrite: true,});console.log(res.bytesWritten, res.sha256);downloadSync
Section titled “downloadSync”export function downloadSync(
url: string,
destPath: string,
options?: DownloadOptions,
): DownloadResult;
Synchronously stream an HTTP/HTTPS GET response directly to a sibling
temporary file, verify optional SHA-256 integrity, and atomically commit
to destPath.
Non-2xx status codes throw FetchError ("VAEL_FETCH_HTTP_STATUS")
without creating or modifying destPath. Available in both vjs
(vaeljs) and vjs-lite (vaeljs-lite).
| Name | Type | Description |
|---|---|---|
url | string | Target http: or https: URL without embedded userinfo. |
destPath | string | Destination regular file path (parent must be a non-symlink directory). |
options optional | DownloadOptions | Optional headers, timeout, size limit, SHA-256 digest, overwrite, and progress callback. |
DownloadResult — DownloadResult metadata.TypeError— on wrong argument types.FetchError— on invalid URL/options, SSRF block, non-2xx status, checksum mismatch, existing target, or I/O error.
import { downloadSync } from "vael:fetch";
const res = downloadSync("https://example.com/release.bin", "./release.bin", { overwrite: true,});console.log(res.bytesWritten, res.sha256);Classes
Section titled “Classes”FetchError
Section titled “FetchError”export class FetchError extends Error {
override name: "FetchError";
code: FetchErrorCode;
readonly status?: number;
readonly url?: string;
constructor(code: FetchErrorCode, message: string);
}
Redacted HTTP client or download error carrying a stable VAEL_FETCH_*
error code.
Error messages redact URL query strings, embedded credentials, bearer tokens, and sensitive header values.
constructor(code: FetchErrorCode, message: string);
Construct a FetchError.
| Name | Type | Description |
|---|---|---|
code | FetchErrorCode | Machine-readable FetchErrorCode. |
message | string | Human-readable redacted error message. |
| Property | Type | Description | Since |
|---|---|---|---|
name | "FetchError" | Error name ("FetchError"). | v0.0.0-alpha.2 |
code | FetchErrorCode | Machine-readable VAEL_FETCH_* error classification. | v0.0.0-alpha.2 |
status readonly optional | number | HTTP status code when the error resulted from a non-2xx download status (VAEL_FETCH_HTTP_STATUS). | v0.0.0-alpha.2 |
url readonly optional | string | Redacted target URL (scheme://host[:port]/path) associated with the failure, when applicable. | v0.0.0-alpha.2 |
Interfaces
Section titled “Interfaces”FetchOptions
Section titled “FetchOptions”export interface FetchOptions {
method?: HttpMethod;
headers?: Record<string, string> | ReadonlyArray<readonly [string, string]>;
query?: Record<string, string> | ReadonlyArray<readonly [string, string]>;
bearerToken?: string;
body?: string | Uint8Array;
json?: unknown;
timeoutMs?: number;
maxResponseBytes?: number;
maxRedirects?: number;
allowPrivateIps?: boolean;
userAgent?: string | null;
}
Request options accepted by fetch and fetchSync.
body and json are mutually exclusive; specifying both throws
FetchError with code "VAEL_FETCH_INVALID_OPTION".
| Property | Type | Description | Since |
|---|---|---|---|
method optional | HttpMethod | HTTP request method (case-insensitive, default "GET"). | v0.0.0-alpha.2 |
headers optional | Record<string, string> | ReadonlyArray<readonly [string, string]> | Additional HTTP request headers as a plain object or [name, value] pairs. Header names must be valid RFC 7230 tokens and values must not contain CR, LF, NUL, or ASCII control characters. | v0.0.0-alpha.2 |
query optional | Record<string, string> | ReadonlyArray<readonly [string, string]> | Query parameters safely URL-encoded and appended to the request URL. | v0.0.0-alpha.2 |
bearerToken optional | string | Bearer token sent as Authorization: Bearer <token> when no explicit Authorization header is present. Stripped on cross-origin redirects. | v0.0.0-alpha.2 |
body optional | string | Uint8Array | Raw UTF-8 text or binary request body. Mutually exclusive with json, and rejected on GET and HEAD requests. | v0.0.0-alpha.2 |
json optional | unknown | JSON-serializable request payload. Mutually exclusive with body, rejected on GET and HEAD requests, and sets Content-Type: application/json; charset=utf-8 when absent. | v0.0.0-alpha.2 |
timeoutMs optional | number | Overall request deadline in milliseconds across redirects and body read (default 30_000, must be > 0 and <= 600_000). | v0.0.0-alpha.2 |
maxResponseBytes optional | number | Maximum buffered response body size in bytes (default 16_777_216 / 16 MiB, must be > 0 and <= 268_435_456 / 256 MiB). | v0.0.0-alpha.2 |
maxRedirects optional | number | Maximum number of HTTP redirects (301, 302, 303, 307, 308) to follow (default 10, maximum 20). | v0.0.0-alpha.2 |
allowPrivateIps optional | boolean | Allow connections to private, loopback, link-local, or cloud-metadata IP addresses (default false, SSRF protection active). | v0.0.0-alpha.2 |
userAgent optional | string | null | Custom User-Agent header value, or null to omit the default vaeljs/<version> header. | v0.0.0-alpha.2 |
FetchResponse
Section titled “FetchResponse”export interface FetchResponse {
readonly status: number;
readonly statusText: string;
readonly ok: boolean;
readonly url: string;
readonly redirected: boolean;
readonly headers: Readonly<Record<string, string>>;
readonly body: Uint8Array;
header(name: string): string | undefined;
headerValues(name: string): string[];
bytes(): Uint8Array;
text(): string;
json<T = unknown>(): T;
}
Buffered HTTP response returned by fetch and fetchSync.
| Property | Type | Description | Since |
|---|---|---|---|
status readonly | number | HTTP status code (for example 200 or 404). | v0.0.0-alpha.2 |
statusText readonly | string | HTTP status reason phrase (for example "OK"). | v0.0.0-alpha.2 |
ok readonly | boolean | true when status is in the 200..=299 range. | v0.0.0-alpha.2 |
url readonly | string | Final resolved URL after following any redirects (without fragment). | v0.0.0-alpha.2 |
redirected readonly | boolean | true if at least one HTTP redirect hop was followed. | v0.0.0-alpha.2 |
headers readonly | Readonly<Record<string, string>> | Lowercased response header map. | v0.0.0-alpha.2 |
body readonly | Uint8Array | Buffered response body bytes (bounded by maxResponseBytes). | v0.0.0-alpha.2 |
FetchResponse.header
Section titled “FetchResponse.header”header(name: string): string | undefined;
Look up the first header value matching name case-insensitively.
| Name | Type | Description |
|---|---|---|
name | string | HTTP header field name. |
string | undefined — Header value, or undefined if absent.FetchResponse.headerValues
Section titled “FetchResponse.headerValues”headerValues(name: string): string[];
Return all response header values matching name case-insensitively in
emission order (useful for multi-value headers like set-cookie).
| Name | Type | Description |
|---|---|---|
name | string | HTTP header field name. |
string[] — Array of matching header values.FetchResponse.bytes
Section titled “FetchResponse.bytes”bytes(): Uint8Array;
Return a defensive copy of the buffered response body bytes.
Uint8Array — Freshly copied Uint8Array of the response body.FetchResponse.text
Section titled “FetchResponse.text”text(): string;
Decode body as strict UTF-8 text.
string — Decoded UTF-8 response body string.FetchError— with code"VAEL_FETCH_INVALID_UTF8"ifbodyis not valid UTF-8.
FetchResponse.json
Section titled “FetchResponse.json”json<T = unknown>(): T;
Parse body as JSON.
T — Parsed JSON value.FetchError— with code"VAEL_FETCH_INVALID_JSON"ifbodyis not valid JSON.
DownloadProgress
Section titled “DownloadProgress”export interface DownloadProgress {
readonly bytesWritten: number;
readonly totalBytes: number | null;
}
Incremental streaming download progress snapshot passed to
DownloadOptions.onProgress.
| Property | Type | Description | Since |
|---|---|---|---|
bytesWritten readonly | number | Total bytes written to the staging temporary file so far. | v0.0.0-alpha.2 |
totalBytes readonly | number | null | Declared Content-Length byte count from the server, or null if absent. | v0.0.0-alpha.2 |
DownloadOptions
Section titled “DownloadOptions”export interface DownloadOptions {
headers?: Record<string, string> | ReadonlyArray<readonly [string, string]>;
query?: Record<string, string> | ReadonlyArray<readonly [string, string]>;
bearerToken?: string;
timeoutMs?: number;
maxRedirects?: number;
allowPrivateIps?: boolean;
userAgent?: string | null;
maxBytes?: number;
expectedSha256?: string;
overwrite?: boolean;
onProgress?: (progress: DownloadProgress) => void;
}
Options accepted by download and downloadSync.
| Property | Type | Description | Since |
|---|---|---|---|
headers optional | Record<string, string> | ReadonlyArray<readonly [string, string]> | Additional HTTP request headers as a plain object or [name, value] pairs. | v0.0.0-alpha.2 |
query optional | Record<string, string> | ReadonlyArray<readonly [string, string]> | Query parameters safely URL-encoded and appended to the request URL. | v0.0.0-alpha.2 |
bearerToken optional | string | Bearer token sent as Authorization: Bearer <token> when no explicit Authorization header is present. Stripped on cross-origin redirects. | v0.0.0-alpha.2 |
timeoutMs optional | number | Overall download deadline in milliseconds across redirects and disk streaming (default 30_000, must be > 0 and <= 600_000). | v0.0.0-alpha.2 |
maxRedirects optional | number | Maximum number of HTTP redirects to follow (default 10, maximum 20). | v0.0.0-alpha.2 |
allowPrivateIps optional | boolean | Allow connections to private, loopback, link-local, or cloud-metadata IP addresses (default false, SSRF protection active). | v0.0.0-alpha.2 |
userAgent optional | string | null | Custom User-Agent header value, or null to omit the default vaeljs/<version> header. | v0.0.0-alpha.2 |
maxBytes optional | number | Maximum bytes allowed to be written to disk (default 268_435_456 / 256 MiB, must be > 0 and <= 4_294_967_296 / 4 GiB). | v0.0.0-alpha.2 |
expectedSha256 optional | string | Optional 64-character ASCII hexadecimal SHA-256 digest verified in constant time before committing the downloaded file. | v0.0.0-alpha.2 |
overwrite optional | boolean | Atomically replace destPath if it already exists (default false, which fails with "VAEL_FETCH_ALREADY_EXISTS"). | v0.0.0-alpha.2 |
onProgress optional | (progress: DownloadProgress) => void | Optional callback invoked after each streamed chunk is written to the staging file. | v0.0.0-alpha.2 |
DownloadResult
Section titled “DownloadResult”export interface DownloadResult {
readonly path: string;
readonly status: number;
readonly url: string;
readonly bytesWritten: number;
readonly sha256: string;
readonly contentType: string | null;
readonly etag: string | null;
}
Completion metadata returned by download and downloadSync.
| Property | Type | Description | Since |
|---|---|---|---|
path readonly | string | Destination filesystem path written atomically. | v0.0.0-alpha.2 |
status readonly | number | HTTP status code (200..=299). | v0.0.0-alpha.2 |
url readonly | string | Final resolved URL after following any redirects (without fragment). | v0.0.0-alpha.2 |
bytesWritten readonly | number | Total number of bytes written to path. | v0.0.0-alpha.2 |
sha256 readonly | string | Lowercase 64-character hexadecimal SHA-256 digest of the downloaded file. | v0.0.0-alpha.2 |
contentType readonly | string | null | Response Content-Type header value, or null if absent. | v0.0.0-alpha.2 |
etag readonly | string | null | Response ETag header value, or null if absent. | v0.0.0-alpha.2 |
Type Aliases
Section titled “Type Aliases”FetchErrorCode
Section titled “FetchErrorCode”export type FetchErrorCode =
| "VAEL_FETCH_INVALID_URL"
| "VAEL_FETCH_INVALID_OPTION"
| "VAEL_FETCH_SSRF_BLOCKED"
| "VAEL_FETCH_DNS_TIMEOUT"
| "VAEL_FETCH_TIMEOUT"
| "VAEL_FETCH_TOO_MANY_REDIRECTS"
| "VAEL_FETCH_BODY_TOO_LARGE"
| "VAEL_FETCH_HTTP_STATUS"
| "VAEL_FETCH_CHECKSUM_MISMATCH"
| "VAEL_FETCH_ALREADY_EXISTS"
| "VAEL_FETCH_NETWORK"
| "VAEL_FETCH_IO"
| "VAEL_FETCH_INVALID_UTF8"
| "VAEL_FETCH_INVALID_JSON";
Stable machine-readable VAEL_FETCH_* error codes exposed on
FetchError.code.
HttpMethod
Section titled “HttpMethod”export type HttpMethod = "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE" | "OPTIONS";
Supported HTTP request methods accepted by FetchOptions.method.
