npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fetch-xhr-shim

v1.2.0

Published

A globalThis.fetch JavaScript polyfill.

Downloads

583

Readme

fetch-xhr-shim

A comprehensive polyfill for the Fetch API and its entire ecosystem — fetch, Blob, FormData, Headers, Request, Response, AbortController, and more. All network requests go through XMLHttpRequest under the hood, so it works anywhere XHR is available: browsers, Node.js, you name it.

When a native implementation exists, it's used directly. When it doesn't, the polyfill kicks in.

Table of Contents

Features

  • fetch — Promise-based HTTP client built on XMLHttpRequest
  • Request / Response — Full Request and Response objects
  • HeadersHeaders with all standard methods
  • Blob / File — Binary data handling with Blob and File
  • FileReader — Read blob/file contents as text, data URL, array buffer, etc.
  • URLSearchParams — Query string parsing and serialization
  • FormData — Multipart form construction and parsing
  • AbortController / AbortSignal — Request cancellation
  • EventTarget / Event / CustomEvent — DOM event system
  • TextEncoder / TextDecoder — UTF-8 encoding and decoding

Installation

npm install fetch-xhr-shim

Importing

To polyfill globalThis.fetch and all related APIs automatically:

import "fetch-xhr-shim/polyfill";

fetch("http://example.com/movies.json")
    .then((response) => response.json())
    .then((data) => console.log(data));

If you prefer explicit imports, every module has two export variants:

  • The default export (e.g. fetch) returns the native implementation if available, falling back to the polyfill.
  • The P-suffixed export (e.g. fetchP) returns the polyfill implementation directly, bypassing the native check.
import { fetch } from "fetch-xhr-shim";  // native fetch if available, otherwise polyfill
import { fetchP } from "fetch-xhr-shim"; // always the polyfill version

This pattern applies to every API: Blob / BlobP, FormData / FormDataP, Headers / HeadersP, and so on.

Usage

fetch

Example

import { fetch } from "fetch-xhr-shim";

fetch("http://example.com/movies.json")
    .then((response) => response.json())
    .then((data) => console.log(data));
POST JSON
import { fetch } from "fetch-xhr-shim";

const data = { username: "example" };

fetch("https://example.com/profile", {
    method: "POST", // or 'PUT'
    headers: {
        "Content-Type": "application/json",
    },
    body: JSON.stringify(data),
})
    .then((response) => response.json())
    .then((data) => {
        console.log("Success:", data);
    })
    .catch((error) => {
        console.error("Error:", error);
    });
Upload files
import { fetch, File, FormData } from "fetch-xhr-shim";

const formData = new FormData();

formData.append("username", "abc123");
formData.append("file", new File(["foo"], "foo.txt", { type: "text/plain" }));

fetch("https://example.com/profile/avatar", {
    method: "PUT",
    body: formData,
})
    .then((response) => response.json())
    .then((result) => {
        console.log("Success:", result);
    })
    .catch((error) => {
        console.error("Error:", error);
    });

Under the hood: fetch is built on XMLHttpRequest. You can swap in your own XHR implementation — handy for Node.js or custom runtimes.

import { setXMLHttpRequestClass } from "fetch-xhr-shim";

setXMLHttpRequestClass(another_XMLHttpRequest_Class);

| Syntax | Supported | | -------------------------- | --------- | | fetch(resource) | ✔ | | fetch(resource, options) | ✔ |

See Request for option compatibility.

Request

Example

import { fetch, Request } from "fetch-xhr-shim";

const request = new Request("https://example.com/favicon.ico");

console.log(request.url);         // "https://example.com/favicon.ico"
console.log(request.method);      // "GET"
console.log(request.credentials); // "same-origin"

fetch(request)
    .then((response) => response.blob())
    .then((blob) => {
        console.log(blob);
    });
import { fetch, Request } from "fetch-xhr-shim";

const request = new Request("https://example.com", {
    method: "POST",
    body: '{"foo": "bar"}',
});

console.log(request.url);         // "https://example.com"
console.log(request.method);      // "POST"
console.log(request.credentials); // "same-origin"
console.log(request.bodyUsed);    // false

fetch(request)
    .then((response) => {
        if (response.status === 200) {
            return response.json();
        } else {
            throw new Error("Something went wrong on API server!");
        }
    })
    .then((data) => {
        console.debug(data);
    })
    .catch((error) => {
        console.error(error);
    });

Compatibility

Properties

| Property | Supported | | ---------------- | --------- | | body | ✖ | | bodyUsed | ✔ | | cache | ✔ | | credentials | ✔ | | destination | ✖ | | headers | ✔ | | integrity | ✖ | | keepalive | ✖ | | method | ✔ | | mode | ✖ | | redirect | ✖ | | referrer | ✖ | | referrerPolicy | ✖ | | signal | ✔ | | url | ✔ |

Methods

| Method | Supported | | --------------- | --------- | | arrayBuffer() | ✔ | | blob() | ✔ | | bytes() | ✔ | | clone() | ✔ | | formData() | ✔ | | json() | ✔ | | text() | ✔ |

Response

Example

import { Response, Blob, fetch } from "fetch-xhr-shim";

const myBlob = new Blob();
const myOptions = { status: 200, statusText: "SuperSmashingGreat!" };
const myResponse = new Response(myBlob, myOptions);

Compatibility

Properties

| Property | Supported | | ------------ | --------- | | body | ✖ | | bodyUsed | ✔ | | headers | ✔ | | ok | ✔ | | redirected | ✖ | | status | ✔ | | statusText | ✔ | | type | ✖ | | url | ✔ |

Methods

| Method | Supported | | --------------- | --------- | | arrayBuffer() | ✔ | | blob() | ✔ | | bytes() | ✔ | | clone() | ✔ | | formData() | ✔ | | json() | ✔ | | text() | ✔ |

Headers

Example

import { Headers, fetch } from "fetch-xhr-shim";

const myHeaders = new Headers();

myHeaders.append("Content-Type", "text/plain");
myHeaders.get("Content-Type"); // "text/plain"

fetch("https://example.com/headers", {
    headers: myHeaders,
});

You can also pass an object or an array of pairs to the constructor:

import { Headers } from "fetch-xhr-shim";

let myHeaders = new Headers({
    "Content-Type": "text/plain",
});

// or, using an array of arrays:
myHeaders = new Headers([["Content-Type", "text/plain"]]);

myHeaders.get("Content-Type"); // "text/plain"

Compatibility

| Method | Supported | | ------------------------------ | --------- | | append(name, value) | ✔ | | delete(name) | ✔ | | entries() | ✔ | | forEach(callbackFn) | ✔ | | forEach(callbackFn, thisArg) | ✔ | | get(name) | ✔ | | getSetCookie() | ✖ | | has(name) | ✔ | | keys() | ✔ | | set(name, value) | ✔ | | values() | ✔ |

Blob

Example

Create a blob
import { Blob, fetch } from "fetch-xhr-shim";

const obj = { hello: "world" };
const blob = new Blob([JSON.stringify(obj, null, 2)], {
    type: "application/json",
});

const another_blob = new Blob(["Hello, World!"], {
    type: "text/plain"
});

fetch("https://example.com/blob", {
    method: "POST",
    body: another_blob,
});
Extract data from a blob
import { Blob, FileReader, fetch } from "fetch-xhr-shim";

const blob = new Blob([JSON.stringify({ foo: "bar" })], {
    type: "application/json",
});

const reader = new FileReader();
reader.addEventListener("loadend", () => {
    // reader.result contains the contents of blob as a typed array
});
reader.readAsArrayBuffer(blob);

fetch("https://example.com/blob", {
    method: "POST",
    body: blob,
})
    .then(r => r.blob())
    .then(r => {
        const reader2 = new FileReader();
        reader2.onload = () => {
            // reader2.result
        }
        reader2.readAsDataURL(r);   // base64
    });

If you call blob.stream() in an environment without ReadableStream, you can inject your own implementation:

import { setReadableStreamClass } from "fetch-xhr-shim";

setReadableStreamClass(another_ReadableStream_Class);

Compatibility

Properties

| Property | Supported | | -------- | --------- | | size | ✔ | | type | ✔ |

Methods

| Method | Supported | | -------------------------------- | --------- | | arrayBuffer() | ✔ | | bytes() | ✔ | | slice() | ✔ | | slice(start) | ✔ | | slice(start, end) | ✔ | | slice(start, end, contentType) | ✔ | | stream() | ✔ | | text() | ✔ |

File

Example

import { File } from "fetch-xhr-shim";

const file = new File(["foo"], "foo.txt", {
    type: "text/plain",
});

Compatibility

| Property | Supported | | -------------------- | --------- | | lastModified | ✔ | | name | ✔ | | webkitRelativePath | ✖ |

FileReader

Example

import { File, FileReader } from "fetch-xhr-shim";

const file = new File([JSON.stringify({ foo: "bar" })], "test.json", {
    type: "application/json",
});

// Read the file
const reader = new FileReader();
reader.onload = () => {
    console.log(reader.result);
};
reader.readAsText(file);

Compatibility

Properties

| Property | Supported | | ------------ | --------- | | error | ✔ | | readyState | ✔ | | result | ✔ |

Methods

| Method | Supported | Notes | | ---------------------- | --------- | ---------- | | abort() | ✔ | simulated | | readAsArrayBuffer() | ✔ | | | readAsBinaryString() | ✔ | | | readAsDataURL() | ✔ | | | readAsText() | ✔ | UTF-8 only |

URLSearchParams

Example

import { URLSearchParams, fetch } from "fetch-xhr-shim";

const paramsString = "q=URLUtils.searchParams&topic=api";
const searchParams = new URLSearchParams(paramsString);

// Iterating the search parameters
for (const p of searchParams) {
    console.log(p);
}

console.log(searchParams.has("topic")); // true
console.log(searchParams.has("topic", "fish")); // false
console.log(searchParams.get("topic") === "api"); // true
console.log(searchParams.getAll("topic")); // ["api"]
console.log(searchParams.get("foo") === null); // true
searchParams.append("topic", "webdev");
console.log(searchParams.toString()); // "q=URLUtils.searchParams&topic=api&topic=webdev"
searchParams.set("topic", "More webdev");
console.log(searchParams.toString()); // "q=URLUtils.searchParams&topic=More+webdev"
searchParams.delete("topic");
console.log(searchParams.toString()); // "q=URLUtils.searchParams"

// GET
fetch("https://example.com/get" + `?${searchParams.toString()}`);

// POST
fetch("https://example.com/post", {
    method: "POST",
    body: searchParams,
});
From an object

Search params can also be initialized from a plain object.

import { URLSearchParams } from "fetch-xhr-shim";

const paramsObj = { foo: "bar", baz: "bar" };
const searchParams = new URLSearchParams(paramsObj);

console.log(searchParams.toString()); // "foo=bar&baz=bar"
console.log(searchParams.has("foo")); // true
console.log(searchParams.get("foo")); // "bar"

Compatibility

Properties

| Property | Supported | | -------- | --------- | | size | ✔ |

Methods

| Method | Supported | | ---------------------------- | --------- | | append(name, value) | ✔ | | delete(name) | ✔ | | delete(name, value) | ✔ | | entries() | ✔ | | forEach(callback) | ✔ | | forEach(callback, thisArg) | ✔ | | get(name) | ✔ | | getAll(name) | ✔ | | has(name) | ✔ | | has(name, value) | ✔ | | keys() | ✔ | | set(name, value) | ✔ | | sort() | ✔ | | toString() | ✔ | | values() | ✔ |

FormData

Example

import { FormData, fetch } from "fetch-xhr-shim";

const formData = new FormData();
formData.append("username", "Chris");

const file = new File(["Hello, World!"], "file.txt", {
    type: "text/plain",
});
formData.append("file", file);

fetch("https://example.com/formdata", {
    method: "POST",
    body: formData,
});

Compatibility

Constructors

| Constructor | Supported | | ------------------------------- | --------- | | new FormData() | ✔ | | new FormData(form) | ✔ | | new FormData(form, submitter) | ✖ |

Methods

| Method | Supported | | ------------------------------- | --------- | | append(name, value) | ✔ | | append(name, value, filename) | ✔ | | delete(name) | ✔ | | entries() | ✔ | | get(name) | ✔ | | getAll(name) | ✔ | | has(name) | ✔ | | keys() | ✔ | | set(name, value) | ✔ | | set(name, value, filename) | ✔ | | values() | ✔ |

AbortController

Example

import { AbortController, fetch } from "fetch-xhr-shim";

const controller = new AbortController();

fetch("https://example.com/abort", {
    signal: controller.signal,
});
import { AbortController, AbortSignal, Request, fetch } from "fetch-xhr-shim";

async function get() {
    const controller = new AbortController();
    const request = new Request("https://example.org/get", {
        signal: controller.signal,
    });

    const response = await fetch(request);
    controller.abort();
    // The next line will throw `AbortError`
    const text = await response.text();
    console.log(text);
}

Compatibility

AbortController

| Member | Supported | | ------------------- | --------- | | signal (property) | ✔ | | abort() | ✔ | | abort(reason) | ✔ |

AbortSignal

| Member | Supported | | -------------------- | --------- | | aborted (property) | ✔ | | reason (property) | ✔ | | throwIfAborted() | ✔ |

EventTarget

Example

import { EventTarget, Event, CustomEvent } from "fetch-xhr-shim";

const target = new EventTarget();

target.addEventListener("foo", function (evt) {
    console.log(evt);
});

const evt = new Event("foo");
target.dispatchEvent(evt);

target.addEventListener("animalfound", function (evt) {
    console.log(evt.detail.name);
});

const catFound = new CustomEvent("animalfound", {
    detail: {
        name: "cat",
    },
});
target.dispatchEvent(catFound);

Compatibility

| Method | Supported | | ------------------------------------------------- | --------- | | addEventListener(type, listener) | ✔ | | addEventListener(type, listener, options) | ✔ | | addEventListener(type, listener, useCapture) | ✔ | | dispatchEvent(event) | ✔ | | removeEventListener(type, listener) | ✔ | | removeEventListener(type, listener, options) | ✔ | | removeEventListener(type, listener, useCapture) | ✔ |

TextEncoder

Example

import { TextEncoder } from "fetch-xhr-shim";

const encoder = new TextEncoder();
const encoded = encoder.encode("€");

console.log(encoded); // Uint8Array(3) [226, 130, 172]

Compatibility

| Member | Supported | Notes | | -------------------------------- | --------- | --------- | | encoding (property) | ✔ | "utf-8" | | encode(string) | ✔ | | | encodeInto(string, uint8Array) | ✔ | |

TextDecoder

Example

import { TextDecoder } from "fetch-xhr-shim";

const utf8decoder = new TextDecoder(); // default 'utf-8'
const encodedText = new Uint8Array([240, 160, 174, 183]);

console.log(utf8decoder.decode(encodedText)); // 𠮷

Compatibility

| Member | Supported | Notes | | ------------------------- | --------- | ---------- | | encoding (property) | ✔ | UTF-8 only | | fatal (property) | ✔ | | | ignoreBOM (property) | ✔ | | | decode() | ✔ | | | decode(buffer) | ✔ | | | decode(buffer, options) | ✔ | |

Fix Functions

fixFetch

Wraps a native fetch so it can accept polyfill Blob, FormData, Request, and Headers objects as input. Useful when you have a native fetch but want to pass polyfill types to it.

import { fixFetch } from "fetch-xhr-shim";

const _fetch = fixFetch();   // You can also pass a specific fetch function to fix.

_fetch("https://example.com/flowers.jpg")
    .then((response) => response.blob());

fixXMLHttpRequest

Patches XMLHttpRequest.prototype so the native XHR can send polyfill Blob and FormData bodies.

import { fixXMLHttpRequest } from "fetch-xhr-shim";

fixXMLHttpRequest();   // You can also pass a specific XHR class to fix.

fixWebSocket

Patches WebSocket.prototype so the native WebSocket can send polyfill Blob data.

import { fixWebSocket } from "fetch-xhr-shim";

fixWebSocket();   // You can also pass a specific WebSocket class to fix.

TextMode

Some environments have an XHR implementation that only accepts strings in send(), not ArrayBuffer. When textMode is enabled, the polyfill will automatically decode ArrayBuffer bodies to strings before sending.

import { setTextMode } from "fetch-xhr-shim";

setTextMode(true);   // polyfill types will be sent as strings instead of ArrayBuffer

Node.js

By providing an XMLHttpRequest implementation, the polyfill fetch works in any environment — not just browsers.

For example, using the xhr2 package:

npm install xhr2
import XMLHttpRequest from "xhr2";
import { setXMLHttpRequestClass } from "fetch-xhr-shim";

setXMLHttpRequestClass(XMLHttpRequest);

License

MIT License

Copyright (c) 2026

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.