New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@sntran/serve

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

@sntran/serve

Cross-runtime HTTP server

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

serve

Provides a common HTTP server API for all runtimes. Currently supports Node.js, Deno, Bun, and Cloudflare Workers (no-op).

Why?

Different runtimes have different APIs for creating HTTP servers. For Node.js, http.createServer takes a handler with its own incoming and outgoing message objects. Deno.serve takes a handler with web standard Request and Response objects. Bun.serve takes an object with a fetch method, similar to Cloudflare Workers. Workers, however. handles the serving.

This library takes the most common denominator of these APIs and provides a serve API that can be used across all runtimes. Similar to Bun and Cloudflare Workers, it takes an object with fetch function. The abstration across runtimes are handled by conditional exports defined in package.json.

The default uses Node.js's http.createServer and normalizes the incoming and outgoing messages to web standard Request and Response objects, and let Deno polyfill to Deno.serve.

A separate export for Bun to explicitly use Bun.serve.

For Cloudflare Workers, it's a no-op, provided to avoid runtime errors. To re-use codebase, it is recommend to export the handler object in the default export.

Examples

import { serve } from "@sntran/serve";

const abortController = new AbortController();

const handler = {
  fetch(request) {
    return new Response("Hello, world!");
  },
  hostname: "0.0.0.0",
  port: 8080,
  signal: abortController.signal,
  onListen({ hostname, port }) {
    console.log(`Server running at http://${hostname}:${port}`);
  },
};

export default handler;

const server = serve(handler);

// To stop the server
abortController.abort();

Keywords

FAQs

Package last updated on 29 Jun 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc