Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@whatwg-node/fetch

ardatan26.3mMIT0.10.5TypeScript support: included

Cross Platform Smart Fetch Ponyfill

readme

@whatwg-node/fetch

A ponyfill package for the Fetch Standard. If your JavaScript environment doesn't implement this standard natively, this package automatically ponyfills the missing parts, and export them as a module; otherwise it exports the native ones without touching the environment's internals. It also exports some additional standard APIs that are required by the Fetch Standard.

Installation

yarn add @whatwg-node/fetch

Why Fetch API and why this ponyfill in general?

If you are building a JavaScript library, and you want it to support all JavaScript environments not only Node.js. Fetch API is the best choice for you. Because it's a standard, and it's implemented by the most environments out there expect Node.js :). So you can use Fetch API in your library, and your users can use it in their browsers, Deno, Bun, Cloudflare Works, and in Node.js.

This is how we support all JavaScript environments in GraphQL Yoga. In GraphQL Yoga, we don't care which JavaScript environment you prefer, we support all of them.

Why we should still use these for Node.js even if it already implements them natively

Even if newer Node.js already implements Fetch API and Data Text Encoding API natively, we still recommend to use this package, because this package implements them for Node.js in more efficient way.

  • See problems with the global fetch/undici in Node.js
    • We offer a patched version of node-fetch that doesn't use undici and Node.js streams internally, so it's more efficient than the native one.
  • See problems with text encoding API in Node.js
    • We use Buffer instead of the native one, because Buffer is faster than the native one unfortunately.
  • Body.formData() is not implemented by Node.js, so we implement it with busboy internally. So you can consume incoming multipart(file uploads) requests with .formData in Node.js.
  • fetch implementation of Node.js uses undici and it doesn't support HTTP 2, our implementation supports it natively thanks to node-libcurl.

Faster HTTP Client in Node.js with HTTP/2 support

If you install node-libcurl seperately, @whatwg-node/fetch will select libcurl instead of node:http which is faster.

See benchmarks

Handling file uploads with Fetch API

import { Request } from '@whatwg-node/fetch'

// See how you can handle file uploads with Fetch API
http.createServer(async (req, res) => {
  const request = new Request(req)
  const formData = await request.formData()
  const file = formData.get('file')
  // ...
})

If you want to limit the size of the multipart form data, you can use createFetch. See the API section for more details.

API

The following are exported by this package:

WHATWG Fetch Standard

Web Streams API

URL Standard

Data Types

Data Encoding/Decoding API

Web Crypto API

Create variations of the implementation

  • createFetch

createFetch allows you to create an API with some specific flags that are not available in the actual API.

Limit the multipart form data size

This is useful if you parse the multipart request bodies with .formData().

import { createFetch } from '@whatwg-node/fetch'

const fetchAPI = createFetch({
  formDataLimits: {
    // Maximum allowed file size (in bytes)
    fileSize: 1000000,
    // Maximum allowed number of files
    files: 10,
    // Maximum allowed size of content (operations, variables etc...)
    fieldSize: 1000000,
    // Maximum allowed header size for form data
    headerSize: 1000000
  }
})

// See how you can handle file uploads with Fetch API
http.createServer(async (req, res) => {
  const request = new Request(req)
  const formData = await request.formData()
  const file = formData.get('file')
  // ...
})

changelog

@whatwg-node/fetch

0.10.5

Patch Changes

  • #2093 31f021a Thanks @ardatan! - Fixes the TypeError: bodyInit.stream is not a function error thrown when @whatwg-node/server is used with node:http2 and attempts the incoming HTTP/2 request to parse with Request.json, Request.text, Request.formData, or Request.blob methods.

  • Updated dependencies [31f021a]:

0.10.4

Patch Changes

0.10.3

Patch Changes

  • #1961 2785c80 Thanks @ardatan! - ReadableStream's Symbol.asyncIterator now returns AsyncIterableIterator like before even if it is ok to return AsyncIterator right now. It is safer to return AsyncIterableIterator because it is a common mistake to use AsyncIterator as AsyncIterable.
  • Updated dependencies [2785c80]:

0.10.2

Patch Changes

0.10.1

Patch Changes

0.10.0

Minor Changes

Patch Changes

0.9.23

Patch Changes

0.9.22

Patch Changes

0.9.21

Patch Changes

  • #1577 99c4344 Thanks @ardatan! - - Improve native ReadableStream handling inside ponyfills
    • Use waitUntil instead of floating promises
    • Handle early termination in WritableStream
    • Handle waitUntil correctly within a dummy call of ServerAdapter.fetch method
  • Updated dependencies [99c4344]:

0.9.20

Patch Changes

0.9.19

Patch Changes

0.9.18

Patch Changes

  • #1328 36904b4 Thanks @ardatan! - Add skipPonyfill flag to createFetch to skip ponyfills and use the native Fetch implementation for Node.js

0.9.17

Patch Changes

0.9.16

Patch Changes

0.9.15

Patch Changes

0.9.14

Patch Changes

0.9.13

Patch Changes

  • 854b778 Thanks @ardatan! - Do not try to import node-libcurl in Deno and Bun

0.9.12

Patch Changes

0.9.11

Patch Changes

0.9.10

Patch Changes

0.9.9

Patch Changes

0.9.8

Patch Changes

0.9.7

Patch Changes

0.9.6

Patch Changes

0.9.5

Patch Changes

0.9.4

Patch Changes

0.9.3

Patch Changes

0.9.2

Patch Changes

0.9.1

Patch Changes

0.9.0

Minor Changes

Patch Changes

0.8.8

Patch Changes

0.8.7

Patch Changes

0.8.6

Patch Changes

0.8.5

Patch Changes

0.8.4

Patch Changes

  • 207ee1d Thanks @ardatan! - Detect Deno if the module is imported via 'npm:' or any other Node compatibility method

0.8.3

Patch Changes

0.8.2

Patch Changes

0.8.1

Patch Changes

0.8.0

Minor Changes

Patch Changes

0.7.1

Patch Changes

0.7.0

Minor Changes

Patch Changes

0.6.9

Patch Changes

0.6.8

Patch Changes

0.6.7

Patch Changes

0.6.6

Patch Changes

0.6.5

Patch Changes

0.6.4

Patch Changes

0.6.3

Patch Changes

0.6.2

Patch Changes

0.6.1

Patch Changes

  • 9752cca Thanks @ardatan! - Remove unnecessary ponyfill for the envs supporting Fetch by default

0.6.0

Minor Changes

  • #241 563cfaa Thanks @ardatan! - Drop itty-router in favor of new URLPattern in the fetch ponyfill

Patch Changes

0.5.4

Patch Changes

0.5.3

Patch Changes

0.5.2

Patch Changes

0.5.1

Patch Changes

0.5.0

Minor Changes

0.4.7

Patch Changes

  • e59cbb6 Thanks @ardatan! - Do not patch global Headers if it is native, and support URL as a first parameter of fetch

0.4.6

Patch Changes

  • c918527 Thanks @ardatan! - Bump Undici

  • #148 eb10500 Thanks @ardatan! - - On Node 14, fix the return method of Response.body's AsyncIterator to close HTTP connection correctly

    • On Node 14, handle ReadableStream's cancel correctly if Response.body is a ReadableStream
    • Do not modify ReadableStream.cancel's behavior but handle it internally
    • On Node 18, do not combine Response.body's return and AbortController which causes a memory leak

0.4.5

Patch Changes

0.4.4

Patch Changes

  • 9502102 Thanks @ardatan! - Breaking Change: Event API is no longer available in this ponyfill; use @whatwg-node/events instead.

0.4.3

Patch Changes

  • c9f05f2 Thanks @ardatan! - Add ponyfills for Response.redirect, Response.json and Response.error

0.4.2

Patch Changes

  • 7f37b6d Thanks @ardatan! - fix(fetch): respect filesLimit even with fieldsFirst

0.4.1

Patch Changes

0.4.0

Minor Changes

  • 005937c Thanks @ardatan! - feat(fetch): new fieldsFirst option to allow async stream consumption for multipart forms

  • effc03d Thanks @ardatan! - Bun Support

0.3.2

Patch Changes

  • 982fa96 Thanks @ardatan! - fix(ponyfill/btoa): handle incoming value as binary encoding

0.3.1

Patch Changes

0.3.0

Minor Changes

0.2.9

Patch Changes

0.2.3

Minor Changes

  • 486c35d: Export Event API

0.1.1

Patch Changes

  • 16aff71: Fix missing TextEncoder and TextDecoder in the default ponyfill

0.1.0

Minor Changes

  • b83d7f3: Faster TextEncoder & TextDecoder with Buffer in Node
  • b83d7f3: Ponyfill for WebCrypto API

Patch Changes

  • b83d7f3: Bump undici version
  • b83d7f3: Now ponyfills Event API

0.0.2

Patch Changes

  • 3207383: Fix TS typings

0.0.1

Patch Changes

  • 889eccf: NEW RELEASES