Gathering detailed insights and metrics for @hattip/adapter-netlify-edge
Gathering detailed insights and metrics for @hattip/adapter-netlify-edge
Gathering detailed insights and metrics for @hattip/adapter-netlify-edge
Gathering detailed insights and metrics for @hattip/adapter-netlify-edge
npm install @hattip/adapter-netlify-edge
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,282 Stars
308 Commits
18 Forks
6 Watching
4 Branches
12 Contributors
Updated on 26 Nov 2024
TypeScript (86.25%)
JavaScript (13.46%)
Shell (0.15%)
CSS (0.09%)
HTML (0.05%)
Cumulative downloads
Total Downloads
Last day
-55%
9
Compared to previous day
Last week
13.6%
92
Compared to previous week
Last month
-13.2%
453
Compared to previous month
Last year
45%
7,824
Compared to previous year
1
6
(nothing) Like Express.js.
Follow: Twitter > @cyco130 & Twitter > @brillout
Chat: Discord > CubesHattip
Why Hattip?
Instead of writing server code that only works with Express.js, write server code that can be deployed anywhere: AWS, Cloudflare Workers, Fastly, Vercel, VPS, ...
What is Hattip?
Hattip is a set of JavaScript packages for building HTTP server applications.
It aims to build an ecosystem of universal middlewares that can be used across the entire JavaScript universe.
1// handler.js
2
3// This request handler works anywhere, e.g. Node.js, Cloudflare Workers, and Fastly.
4
5export default (context) => {
6 const { pathname } = new URL(context.request.url);
7 if (pathname === "/") {
8 return new Response("Hello from Hattip.");
9 }
10 if (pathname === "/about") {
11 return new Response(
12 "This HTTP handler works in Node.js, Cloudflare Workers, and Fastly.",
13 );
14 }
15 return new Response("Not found.", { status: 404 });
16};
A Hattip handler is passed a context
object which represents the request context and contains context.request
which is a standard Request
object. It returns a standard Response
object (or a promise of one). Response
and Request
follow the Fetch API standard. So if you're familiar with service workers, Cloudflare Workers, or Deno, you'll feel right at home. If not, learn today the standard that will tomorrow be ubiquitous.
We believe in a diverse but interoperable future for the JavaScript ecosystem and we're closely following the WinterCG which lays the foundation beyond the Fetch API.
Adapters let you run Hattip on any platform. Here's how you can use Hattip with Node.js:
1// entry-node.js 2import { createServer } from "@hattip/adapter-node"; 3import handler from "./handler.js"; 4 5createServer(handler).listen(3000, "localhost", () => { 6 console.log("Server listening on http://localhost:3000"); 7});
...and on Cloudflare Workers:
1// entry-cfw.js 2import cloudflareWorkersAdapter from "@hattip/adapter-cloudflare-workers"; 3import handler from "./handler.js"; 4 5export default { 6 fetch: cloudflareWorkersAdapter(handler), 7};
You can even use your Hattip application as an Express middleware when you have to use that one Express library that doesn't have a replacement anywhere else:
1// entry-express.js 2import { createMiddleware } from "@hattip/adapter-node"; 3import handler from "./handler.js"; 4import express from "express"; 5import oldAndRustyExpressMiddleware from "old-and-rusty-express-middleware"; 6 7const hattip = createMiddleware(handler); 8const app = express(); 9 10// TODO: Replace with shinyNewHatTipMiddleware once ready 11app.use(oldAndRustyExpressMiddleware()); 12app.use(hattip); 13 14app.listen(3000, "localhost", () => { 15 console.log("Server listening on http://localhost:3000"); 16});
The compose
function from the @hattip/compose
package can be used to compose multiple handlers into a single one, creating a simple but powerful middleware system. Each handler is called in sequence until one returns a response. A handler can pass control to the next handler either by not returning anything or calling ctx.next()
. The latter allows the handler to modify the response before returning:
1import { compose } from "@hattip/compose"; 2 3// Example of making things available in `ctx` 4// Middleware to parse the URL into a URL object 5const urlParser = (ctx) => { 6 ctx.url = new URL(ctx.request.url); 7}; 8 9// Example of modifying the response 10// Middleware to add an X-Powered-By header 11const poweredBy = async (ctx) => { 12 const response = await ctx.next(); 13 response.headers.set("X-Powered-By", "Hattip"); 14 return response; 15}; 16 17// Hattip does have a router, this is to illustrate the basics 18const homeHandler = (ctx) => { 19 if (ctx.url.pathname === "/") { 20 return new Response("Home"); 21 } 22}; 23 24const fooHandler = (ctx) => { 25 if (ctx.url.pathname === "/foo") { 26 return new Response("Foo"); 27 } 28}; 29 30const barHandler = (ctx) => { 31 if (ctx.url.pathname === "/bar") { 32 return new Response("Bar"); 33 } 34}; 35 36export default compose( 37 urlParser, 38 poweredBy, 39 homeHandler, 40 fooHandler, 41 barHandler, 42);
A handler can return or throw a Response
or anything with a toResponse
method when used with the compose
function. Handlers can also set context.handleError
to handle uncaught errors.
That's it. This is the entirety of the Hattip API. Everything else is middleware functions similar the above that add various features and development tools to make your life easier.
Hattip is extremely modular so you can use as little or as much as you need:
core
: A type-only package that defines the interface between your application and platform adaptersadapter-node
: Node.js, either as a standalone server or as a middleware function that can be used with Express and similar frameworks. Also works for Vercel Serverless Functions.adapter-cloudflare-workers
: Cloudflare Workersadapter-vercel-edge
: Vercel Edge Functionsadapter-netlify-functions
: Netlify Functionsadapter-netlify-edge
: Netlify Edge Functionsadapter-deno
: Denoadapter-bun
: Bunadapter-fastly
: Fastlyadapter-lagon
: Lagonadapter-uwebsockets
: uWebSockets.jsbundler-cloudflare-workers
: Cloudflare Workersbundler-vercel
: Vercel edge and serverless functionsbundler-netlify
: Netlify edge and Netlify functionsbundler-deno
: Denorouter
: Express-style imperative routerresponse
: Utility functions for creating text, JSON, HTML, and server-sent event responsesheaders
: Header value parsing and content negotiation utilitiesmultipart
: Experimental multipart parser (e.g. for form data with file uploads)cookie
: Cookie handling middlewarecors
: CORS middlewaregraphql
: GraphQL middlewaresession
: Session middlewareA zero-config development environment based on Vite is also in the works.
cors
package is a port of koajs/cors by koajs and contributors under the MIT License. They are not affiliated with Hattip.graphql
package comes bundled with graphql-yoga
which is part of the GraphQL Yoga project by Graphcool, Prisma, and The Guild, under the MIT License. They are not affiliated with Hattip.No vulnerabilities found.
No security vulnerabilities found.