🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

connect-to-fetch

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-to-fetch

Use Connect-style Node handlers with Fetch environments such as `Bun.serve` and `Deno.serve`.

1.0.0-rc.2
latest
Source
npm
Version published
Weekly downloads
1
-50%
Maintainers
0
Weekly downloads
 
Created
Source

connect-to-fetch

Use Connect-style Node handlers with Fetch environments such as Bun.serve and Deno.serve.

NPM Link Language: TypeScript No Dependencies Code Coverage Tree shakeable ISC License

Motivation

Use Vite's dev-server middleware with application that uses Bun.serve.

Installation

bun add connect-to-fetch

Usage

Pass one or more handlers or middleware to connectToFetch.

Example with single handler:

import { connectToFetch } from 'connect-to-fetch';

const getResponse = connectToFetch((req, res) => {
  res.setHeader('Content-type', 'text/plain');
  res.end('Hello world');
});

Bun.serve({
  async fetch(request: Request) {
    try {
      return await getResponse(request);
    } catch (e) {
      const error = e as Error;
      if (error.message === 'UNHANDLED') {
        return new Response('Not Found', { status: 404 });
      }
      console.error(error);
      return new Response('Server Error', { status: 500 });
    }
  },
});

Example with multiple handlers:

import { connectToFetch } from 'connect-to-fetch';

const getResponse = connectToFetch([
  (req, res, next) => {
    console.log(req.method, req.path);
    next();
  },
  (req, res) => {
    res.setHeader('Content-type', 'text/plain');
    res.end('Hello world');
  },
]);

Prior art

Adapted from vike-node, MIT License.

Testing

Tests use Bun.serve, so you'll need to have Bun installed to test.

History

Changelog

Keywords

Connect to Fetch

FAQs

Package last updated on 29 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts