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

kaph

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kaph

Loose-coupled set of tools for handle requests under node.js

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Kaph*

Kaph is loose-coupled set of tools for handle requests under node.js. It's not framework.

*In the Phoenician alphabet letter "kaph" indicates palm.

Design

Design of kaph was inspired by Connect, Tornado and many others. But unlike them, it was designed to make all components as independent from each other without large overhead.

Installation

You can install kaph as usual - by copy "kaph" directory in your ~/.node_libraries or via npm

npm install kaph

Usage

To handle request Kaph executes chain of defined operations.

var http = require('http');
var HttpHandler = require('kaph/http').Handler;

// Make some operations
OpA = {
    DEFAULT: function() {
        this.response.writeHead(200, 
                {'Content-Type': "text/html; charset=UTF-8"});
        this.next();
    }    
};
OpB = {
    GET: function(arg) {
        this.response.end('OpB ' + arg);
    },
    ERROR: function(code, message) {
        return code + ' ' + message;
    }
};

var chain = [OpA, OpB]; // Our chain
var arg = ['Some arg']; // Optional arguments

http.createServer(function(request, response) {
    // Just make new kaph handler and call the method "next"
    (new HttpHandler(request, response, chain, arg)).next();
}).listen(9080);

Kaph handler receives four arguments:

  • request is standart node.js http.ServerRequest
  • response is standart node.js http.ServerResponse
  • chain Array of operations.
  • args Optional Array of arguments.

request and response may not be instances of node.js http module. They simply must have the same behavior. Handler never interfere with their implementation. It use only request.method property to choose operation method (see below) as well as response's writeHead and end methods on exceptions.

Now about chain. It's just Array of Objects. On each iteration kaph handler invokes operation methods by following these rules:

  • If operation has method with name matching request.method, it performed as a native method of handler (yes apply) with optional arguments.
  • If operation hasn't named method, handler performs method with name DEFAULT.

To invoke next operation in chain you must call next method of handler.

Error handling

Kaph trying to handle all throwed exceptions without crash entire server. On exception kaph handler ends request, logs error and try to send it to client. To give a better description of exception you can throw new kaph.HandlerError with two arguments: status code and optional message. Another way to throw exception is handler's error method with same arguments.

By default kaph handler generates client error message with own kaph.ERROR function. You can replace it with method ERROR of operation. ERROR method also takes two code and message arguments and returns String.

Handler properties

Because handler performs the methods of operations as own, it's implementation is important. Each instance has next properties:

  • request ... well, you understand :)
  • response ... well, it is also clear :)
  • logger is just node.js console.

Bundled operations

As stated in the beginning, kaph is not framework. But in directory chain you find a few things that can make life much more pleasant:

  • cookie - work with cookies.
  • writer - facade for working with http.ServerResponse

Each of them can be used independently of kaph. And of course kaph, not depend on them.

How about router?

I'm use my own daleth

FAQs


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