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 🙏

© 2025 – Pkg Stats / Ryan Hefner

fetch-to-node

v2.1.0

Published

Node.js-compatible request and response objects for WinterTC runtimes

Readme

fetch-to-node

A library providing Node.js-compatible request and response objects for WinterTC (fetch-like) runtimes, such as Cloudflare Workers, Bun, Deno and Fastly Compute.

Useful for when you're using a Node.js library in one of these environments that expects Node.js-style req and res objects, for example Express.

This is basically the inverse of libraries like @mjackson/node-fetch-server that allow the use of Request/Response signatures in Node.js servers.

This library is a copy/fork of Katsuyuki Omuro's great @fastly/http-compute-js project and wouldn't be possible without the hard work put in there. The changes here were largely made to remove dependencies and make the interfaces more generic.

That said, this library does depend on a certain level of Node.js compatibility (Readable, Writable from node:stream and Buffer from node:buffer). So please check out @fastly/http-compute-js if this library doesn't work for you.

Usage

import { toReqRes, toFetchResponse } from "fetch-to-node";

export default {
  async fetch(request: Request): Promise<Response> {
    // Create Node.js-compatible req and res from request
    const { req, res } = toReqRes(request);

    // Use req/res as you would in a Node.js application
    res.writeHead(200, { "Content-Type": "application/json" });
    res.end(
      JSON.stringify({
        data: "Hello World!",
      })
    );

    // Create a Response object based on res, and return it
    return await toFetchResponse(res);
  },
};

NB: If you're using Cloudflare Workers, be sure to set the nodejs_compat flag.

req and res are implementations of IncomingMessage and ServerResponse, respectively, and can be used as in a Node.js program.

API

toReqRes(request)

  • Converts from a Request object to a pair of Node.js-compatible request and response objects.
  • Parameters:
    • request - A Request object. You would typically obtain this from the request received by your fetch handler.
  • Returns: an object with the following properties.
    • req - An http.IncomingMessage object whose Readable interface has been wired to the Request object's body. NOTE: This is an error if the Request's body has already been used.
    • res - An http.ServerResponse object whose Writable interface has been wired to an in-memory buffer.

toFetchResponse(res)

  • Creates a new Response object from the res object above, based on the status code, headers, and body that has been written to it.
  • Parameters:
    • res - An http.ServerResponse object created by toReqRes().
  • Returns: a promise that resolves to a Response object.
  • NOTE: This function returns a Promise that resolves to a Response once the res object emits the 'finish' event, which typically happens when you call res.end(). If your application never signals the end of output, this promise will never resolve, and your application will likely error or time out.
  • If an error occurs, the promise will reject with that error.

Notes / Known Issues

  • HTTP Version is currently always reported as 1.1.
  • The socket property of these objects is always null, and cannot be assigned.
  • The ServerResponse write stream must be finished before the Response object is generated.
  • Trailers are not supported, as there's no support for these in the fetch API.
  • Client APIs not supported: http.Agent, http.ClientRequest, http.get(), http.request(), to name a few.

License

MIT.

In order for this library to function without requiring a direct dependency on Node.js itself, portions of the code in this library are adapted / copied from Node.js. Those portions are Copyright Joyent, Inc. and other Node contributors. See the LICENSE file for details.