Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
routify-hapi-controllers
Advanced tools
A TypesScript or ES7 decorators library that generates Hapi routes for controllers based on decorators.
First and foremost your project needs Typescript
or at least support to ES7 Decorators.
npm install hapi@16.1.0 --save
// For Typescript you also need
npm install @types/hapi --save-dev
Needed for dependency injection.
npm install inversify --save
// For Typescript you also need
npm install @types/inversify --save-dev
It is actually possible to use the library without Inversify instantiating the controllers and their dependencies by hand and loading the routes for the controllers individually.
A polyfill for reflection necessary for both Inversify to work and for this library's decorators.
npm install reflect-metadata --save
// For Typescript you also need
npm install @types/reflect-metadata --save-dev
npm install routify-hapi-controllers --save
This library already has typings built-in.
In your main TS or JS file load reflect-metadata
, it has to be before every other dependencies.
import 'reflect-metadata';
import { Server } from 'hapi';
(...)
For a controller you need to implement the IController
interface (if in Typescript), load the Controller
decorator as well as Inversify's injectable
decorators and the method decorators.
import { injectable } from 'inversify';
import { Controller, Get, Post } from 'routify-hapi-controllers';
@injectable()
@Controller('/v1/lemmings'/* , { (Optional Hapi route config for all endpoints) } */)
export default class LemmingsController {
@Get()
public get(request: Request) {
// Results in "GET /v1/lemmings"
}
@Get('/alive')
public getAlive(request: Request) {
// Results in "GET /v1/lemmings/alive"
}
@Post('/explode')
public postExplode(request: Request) {
// Results in "POST /v1/lemmings/explode"
}
}
You can have the optional Hapi config in individual endpoints as well, they are merged with the controller's configs for that endpoint.
You can also have automatically resolved dependencies in your controllers, check out Inversify's documentation for that.
If you don't want to use inversify, you can get rid of the
@injectable
decorator.
You can do this anywhere, but it's probably better to have a file dedicated to that as your list of registrations might become big.
import { Container } from 'inversify';
import LemmingsController from './controllers/lemmings.controller';
import { TYPES, registerController, IController } from 'routify-hapi-controllers';
const container = new Container();
// You can either register the controller using the inversify bind function:
container.bind<IController>(TYPES.CONTROLLER).to(LemmingsController);
// Or alternatively you can register with our helper function that makes things look nicer.
registerController(container, LemmingsController);
import 'reflect-metadata'; // This was loaded in the first step.
import { container } from './di.container.ts';
import { retrieveAllRoutes } from 'routify-hapi-controllers';
// This will retrieve all routes for all of the registered Controllers
const routes = retrieveAllRoutes(container);
/*
Alternatively, if you didn't use Inversify, you can load the routes for specific Controllers.
const lemmingsController = new LemmingsController();
const potatoController = new PotatoController();
const routes = [
...getRoutesFromController(lemmingsController),
...getRoutesFromController(potatoController)
];
*/
FAQs
Load your controllers as Hapi routes
The npm package routify-hapi-controllers receives a total of 1 weekly downloads. As such, routify-hapi-controllers popularity was classified as not popular.
We found that routify-hapi-controllers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.