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

schildwall

v0.3.2

Published

gateway for microservice infrastructures

Downloads

7

Readme

Schildwall

Version License Size 996.icu

Schildwall is a service to manage security and accessibility of API-Networks. It aims to provide a modular, simple way of building complex gateways for big APIs. It supports

  • Custom Middleware
  • Multiple Endpoints

installation

npm i --save @vampyreio/schildwall

How To Use

The example can be ran using 'npm run-script example' and starts a proxy server on port 8000 with an endpoint

  • /test/ which points to localhost:8888
// gateway.class.ts

import { ListenerErrorHandler, GatewayApp, MasterGateway } from '@vampyreio/schildwall';
import { HeaderMiddleware } from './app/middleware';

@GatewayApp({
    log: true,
    listenerErrorHandler: new ListenerErrorHandler,
    endpoints: [
        { endpoint: 'http://localhost:58', name: 'local58' },
        { endpoint: 'http://google.com', name: 'google' },
    ],
    middlewares: [new HeaderMiddleware()],
})
export class Gateway extends MasterGateway { }

// index.ts
import { Gateway } from './gateway.class';

const gateway = new Gateway();

async function bootstrap() {
    const server = await gateway.init();
    server.listen(3000);
}

If the gateway now runs on localhost:8000, a request to Google with path /search would be made with

GET /google/search => GET http://google.com/search
GET /local58/entity/abcde => GET http://localhost:58/entity/abcde

because the first parameter of the path will be mapped onto the provided endpoint list and cut from the incoming url.

Middleware

Defining a Middleware

Middleware can be attached to the gateway easily. A Middleware contains methods that control what it should do on startup, on shutdown and on execution. It has access to the servers request and response via the ctx object.

to let a header block the request and answer it itself with an error, it's not required to set the answer manually. Schildwall provides Error Objects for all 4xx and 5xx errors. when such an error is thrown, an internal error handler does this itself. It will me overwritable in upcoming versions to allow manual error handling.

Middleware will be executed in the order of binding.

In upcoming versions, middlewares will be able to pass data to following middlewares.

import {Middleware, IGatewayContext, BadGatewayException} from '../lib';

/**
 * checks for a 'kill-me' header and if it is true, throw an exception
 */
class HeaderScanner extends Middleware {

    /**
     * executes when a request enters
     */
    public async execute(ctx: IGatewayContext): Promise<any> {
        if (ctx.request.headers['kill-me'] === 'true') {
            throw new BadGatewayException('kill-me header was found');
        }
    }

    /**
     * executes when a middleware is bound
     */
    public async start(): Promise<any> {
        // Setup, initialisation, precondition checking, ...
    }

    /**
     * executes when the server should stop
     */
    public async stop(): Promise<any> { 
        // Cleanup, stopping processes, deletions, ...
    }

}

Handle HTTP-Errors manually

If you want to futher manipulate how your gateway behaves when an error is thrown, you can insert a custom error handler. Error classes implement the MiddlewareErrorHandler which takes following parameters:

  • errorCodeToCatch: number is the error code for which the error should be handled.
  • execute: (ctx: IGatewayContext, error: HttpError) => Promise<void> is the handler itself.

In future versions, instead of a manual error code, an error class could be passed by a decorator function

import { GatewayApp, MasterGateway, ListenerErrorHandler, Middleware, IGatewayContext, BadGatewayException, MiddlewareErrorHandler, HttpError } from '../lib';

/**
 * handles 502 errors 
 */
class MyFiveZeroTwo implements MiddlewareErrorHandler {

    public errorCodeToCatch: number = 502;

    public async execute(ctx: IGatewayContext, error: HttpError) {

        ctx.response.write('There is actually text in this reply!');
        ctx.response.statusCode = 502;
        return ctx.response.end();

    }
}

@GatewayApp({
    log: true,
    listenerErrorHandler: new ListenerErrorHandler,
    endpoints: [
        { endpoint: 'http://localhost:3000', name: 'mock' }
    ],
    middlewares: [new HeaderScanner()],
    middlewareErrorHandlers: [new MyFiveZeroTwo()],
})
class Gateway extends MasterGateway { }

const gateway = new Gateway();
gateway.init().then((data) => {
    data.server.listen(8000);
});

API

Schildwall supports accessing and manipulating the gateway via an controlling API. Following routes are currently supported:

Miidleware

  • [x] GET /middlewares - Lists metadata about
  • [ ] GET /middlewares/:middlewareId' - list metadata of a specific middleware
  • [ ] POST /middleware/:middlewareId/toggle - starts or stops a middleware
  • [ ] PATCH /middleware/:middlewareId - changes a middlewares settings
  • [ ] DELETE /middleware/:middlewareId - removes a selected middlware

Endpoints

  • [x] GET /endpoints - Lists endpoints
  • [ ] GET /endpoints/:endpointId - describes a single endpoint
  • [x] POST /endpoints - Creates a new Endpoint Params:
  • endpoint: an url to a new endpoint
  • name: is used as prefix in the gateway

Minimal with API

@GatewayApp({
    log: true,
    listenerErrorHandler: new ListenerErrorHandler,
    endpoints: [
        { endpoint: 'http://localhost:3000', name: 'mock' }
    ],
})
class Gateway extends MasterGateway { }

const gateway = new Gateway();
gateway.init().then((data) => {

    // create Server
    data.server.listen(8000);

    // create API
    const api = new GatewayApiServer(data.manager as ListenerManager).run(8081);
});

Error Handling