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

kontroller

v0.0.15

Published

Just controller

Downloads

170

Readme

About

Controllers used in CompoundJS. Could be used separately. This package provides efficient way of describing controllers, it works insanely fast (sometimes even faster then manually written express controller). Optimized for high performance.

Installation

npm install kontroller

Basic usage (ExpressJS example)

// create some controller
var Driver = require('kontroller').BaseController.constructClass();

// define action
Driver.actions.accelerate = function accelerate(c) {
    c.send('accelerating!');
};

// and another one
Driver.actions.brake = function brake(c) {
    c.send('braking!');
};

// now let's create express app
var express = require('express);
var app = express();

// and map routes to controller
app.get('/speedup', Driver('accelerate'));
app.get('/slowdown', Driver('brake'));

// run, test
app.listen(3000);

Context-free controllers

There are a lot of hidden features in your new controller. You can rewrite it in compoundjs-style:

before(think, {only: 'accelerate'});

action('accelerate', function () {
    send('accelerating!');
});

action('brake', function () {
    send('braking!');
});

function think() {
    // think 1 second before accelerate
    setTimeout(next, 1000);
}

And use as compoundjs does it:

// create blank controller
var Driver = BaseController.constructClass('Driver');

// instantiate and configure
var ctl = new Driver;
ctl.build(code); // code is string, containing code of controller

// feed to express
app.get('/speedup', Driver('accelerate'));
app.get('/slowdown', Driver('brake'));

Respond to specific format:

action(function index() {
    var fruits = this.fruits = ['apple', 'banana', 'kiwi'];
    respondTo(function (format) {
        format.html(render);
        format.json(function () {
            send(fruits);
        });
    });
});

Extend list of formats:

require('kontroller').Helpers.respondToFormats.push('gif', 'png', 'jpg');

And then you can use it:

action(function image() {
    respondTo(function (format) {
        format.png(renderPNG);
        format.jpg(renderJPG);
        format.git(renderGIF);
    });
    // note: methods renderGIF, renderJPG, renderPNG aren't part of
    // kontroller, this is just some named functions
    // used for example
});

Flow control

You can use hooks for specific actions / all actions

before(function () {
    next(); // call next to get to the next hook/action
});

Next hook only will be executed when action is specialCase:

before(function doSmthSpecial() {
    next();
}, {only: 'specialCase'});

Next hook only will be executed when action is not publicResource:

before(function authorizeUser() {
    if (!session.user) {
        redirect('/login');
    } else {
        next();
    }
}, {except: 'publicResource'});

You also can pass array of strings as only and except settings when you need to hook multiple actions:

// load resource before actions: show, edit, update, destroy
before(loadResource, {only: ['show', 'edit', 'update', 'destroy']});

// only blahBlah if action is not pipa or moka
before(blahBlah, {except: ['pipa', 'moka']});

Controllers pool

Controllers produced via kontroller designed to be fast as possible. Every controller instance could be reused multiple times (to save time for it's instantiation and reduce memory usage). This trick allows to save same performance level as it would be for just function, and not separate object

The only problem here in concurrent requests. Obviously, concurrent requests should be handled using different controller instances, so we have pool for controllers. When request comes, firts of all we trying to pull controller from the pool, and only if pool is empty - creating new instance. After request handling is completed - controller instance pushed back to pool.

MIT License

Copyright (C) 2011 by Anatoliy Chakkaev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.