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 🙏

© 2024 – Pkg Stats / Ryan Hefner

edge-master

v0.9.3

Published

A Micro Framework for Edges

Downloads

219

Readme


EdgeMaster is a flexible microframework designed to handle routing and intercept requests/responses in cloud based environments such, as Cloudflare Workers or serverless functions. It offers a robust set of tools that can greatly simplify the development of your edge applications.


Table of Contents


Features

  • Lightweight: EdgeMaster is incredibly small in size, allowing you to keep your application's footprint minimal.
  • Fully-Typed: Enjoy the benefits of TypeScript with complete type definitions.
  • Simplified Routing: Create concise and expressive routes with ease.
  • Interception Support: Implement interceptors for Request/Response effortlessly, whether built-in or custom.
  • Platform-Agnostic: Functions seamlessly in diverse cloud-based environments such as Cloudflare Workers and serverless platforms, while providing full compatibility with the Fetch API.
  • Error Handling: Gracefully handle errors and define error responses.

Installation

To get started with EdgeMaster, you can install it via npm:

npm install edge-master

Getting Started

Here's a simple example of setting up EdgeMaster in a Cloudflare Worker module:


import {
  EdgeController,
  RouteHandler,
  Task,
  routePattern,
  initResponse
} from 'edge-master';

// Cloudflare Worker
export default {
  async fetch(req) {
    return EdgeApp.handleRequest({ req });
  },
}

const EdgeApp = new EdgeController();

EdgeApp.addRoute(
  routePattern('*/users/profile/*'),
  new RouteHandler().registerTask(new Task({
    do: async ({ req }) => {
      const tempRes = await fetch(req);
      // some transform on response
      return new Response(tempRes.body, {
        ...tempRes,
        headers: {
          ...tempRes.headers,
          'My-Res-Status': 'transformed'
        }
      });
    }
  }))
);

EdgeApp.addRoute(
  routePattern('*'),
  new RouteHandler().registerTask(initResponse())
);


Routing

EdgeMaster streamlines routing in your application with built-in route matchers, and we're continually adding more. If your project requires custom, intricate route matching, you can easily implement it with the flexibility our framework offers. At EdgeMaster, we aim to make complex route matching straightforward and accessible for developers.

import {
  EdgeController,
  routePattern, // You can use wildcards and optional segments to create flexible route patterns.
  pathStartWith,
  urlStartWith,
} from 'edge-master';

const App = new EdgeController();

App.addRoute(routePattern('*/user/*/info'), {/** RouteHandler of UserInfo  */});

App.addRoute(pathStartWith('/api/v2/user'), {/** RouteHandler of User  */});

App.addRoute(urlStartWith('https://origin.com/api/v2/user'), {/** RouteHandler of User  */});

// This handler will be executed when none of the other registered routes don't match the request.
App.addRoute(() => true, {/** RouteHandler of Default or Non-matching routes */});

Custom Route Matcher

EdgeMaster allows you to define your own route matcher.


type Matcher = (req: Request) => boolean;

const onlyGetMethod = (matcher: Matcher): Matcher => {
  return (req) => (req.method === "GET" && matcher(req))
}

App.addRoute(
  onlyGetMethod(routePattern('*/some/*/wildcard')),
  {/** RouteHandler */ }
);

Interceptors

EdgeMaster provides a convenient way to intercept incoming requests and outgoing responses using interceptors. Interceptors can be used for tasks such as authentication, logging, and response modification.

Request Interceptors

Request interceptors are executed before your route handlers, allowing you to modify the incoming request.

const authenticationInterceptor = {
  type: InterceptorType.Request,
  async intercept(context) {
    // Check authentication and modify the request if needed
    const { reqCtx: { req }, responder } = context;
    if (/** req is ok*/)
      return req;
    else
      // responder will break all next steps and respond instnatly the request.
      responder(new Response('Not Authenticated', { status: 401 }))
  },
};

router.addInterceptor(authenticationInterceptor);

Response Interceptors

Response interceptors are executed after your route handlers, enabling you to modify the response before it's sent back to the client.

const loggingInterceptor = {
  type: InterceptorType.Response,
  async intercept(context) {
    // Log the response or make changes before sending it
    const { res } = context;
    // ...
    return res;
  },
};

router.addInterceptor(loggingInterceptor);

Examples

Check out more examples and use cases in the EdgeMaster Examples.


Contributing

We welcome contributions from the community to make EdgeMaster even better. If you'd like to contribute, please follow these steps:

  1. Fork the repository.
  2. Install dependencies npm install.
  3. Start the test runner npm run test.
  4. Add your code and tests.
  5. Commit your changes && Submit a pull request.

We'll review your contribution and, if

everything looks good, merge it into the main codebase.


Credits

EdgeMaster is developed and maintained by Sharif Eiri. It's inspired by the simplicity of microframeworks like Express.js and is built to be highly compatible with cloud-based serverless environments. Special thanks to the open-source community for their contributions and support.


Thank you for choosing EdgeMaster for your edge application development! If you have any questions or encounter issues, please don't hesitate to reach out on our GitHub repository.

Happy coding! 🚀