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

siren-js

v0.2.4

Published

A koa-based Node.js web application bootstrapping module, based on kraken-js.

Downloads

17

Readme

Siren.js

Siren.js builds upon koa and Kraken.js and enables environment-aware, dynamic configuration, advanced middleware capabilities, security, app lifecycle events and integrates socket.io.

Basic Usage

'use strict';

var siren = require('siren-js');

var app = siren();
app.listen(8000);

##NOTE

Unlike Kraken.js,

  • it's NOT recommanded to mount the siren instance. Because it won't emit the life cycle events on the parent app and the middleware won't configure against the parent.

  • you should ONLY call app.callback or app.listen when the app is READY (start event is fired) (PS: Of course we can hack on the app.callback method to respond "server is starting" before ready, but I don't think it is fit for koa).

API

siren([options])

siren-js will return a koa application instance.

Options

Pass the following options to siren via a config object such as this:

var options = {
    onconfig: function *(config) {
        // do stuff
    }
};

// ...

siren(options);

basedir (String, optional)

The working directory for siren-js to use. siren-js loads configuration files, routes, and registers middleware so this directory is the path against all relative paths are resolved. The default value is the directory of the file that uses siren-js, which is generally index.js (or server.js).

onconfig (Generator Function, optional)

Provides an asynchronous hook for loading additional configuration. When invoked, a confit configuration object containing all loaded configuration value passed. The signature of this handler is function *(config) or a function that returns a yieldable target.

protocols (Object, optional)

Please refer to protocols object.

uncaughtException (Function, optional)

Handler for uncaughtException errors. See the endgame module for defaults.

Config Protocols

Please refer to config protocols.

Features

Configuration

Environment-aware

Please refer to environment-aware.

Middleware

Much like configuration, you shouldn't need to write a lot of code to determine what's in your middleware chain. siren-meddleware is used internally to read, resolve, and register middleware with your express application. You can either specify the middleware in your config.json or {environment}.json, (or) import it from a separate json file using the import protocol mentioned above.

Included Middleware

Siren.js comes with common middleware already included in its config.json file. The following is a list of the included middleware and their default configurations which can be overriden in your app's configuration:

  • responseTime - adds response time to header.

    • Priority - 0
    • Module - "koa-response-time" (npm)
  • "logger" - internal middleware which logs websocket connections and http requests

    • Priority - 10
    • Enabled - false but true in a development environment
    • Module - "siren-js/middleware/logger"
      • Arguments (Array)
        • String - the app instance placeholder
  • "shutdown" - internal middleware which handles graceful shutdowns in production environments

    • Priority - 20
    • Enabled - true if not in a development environment
    • Module - "siren-js/middleware/shutdown"
      • Arguments (Array)
        • String - the app instance placeholder
        • Object
          • "timeout" - milliseconds (default: 30000)
          • "template" - template to render (default: null, related to app.views)
  • "compress" - adds compression to server responses

    • Priority - 30
    • Enabled - false (disabled in all environments by default)
    • Module - "koa-compress" (npm)
  • "favicon" - serves the site's favicon

    • Priority - 40
    • Module - "serve-favicon" (npm)
      • Arguments (Array)
        • String - local path to the favicon file (default: "path:./public/favicon.ico")
  • "error" - internal middleware which handles graceful error response

    • Priority - 50
    • Module - "siren-js/middleware/error"
      • Arguments (Array)
        • String - template to render (default: null, realted to app.views)
  • "404" - internal middleware which handles graceful 404 response

    • Priority - 60
    • Module - "siren-js/middleware/404"
      • Arguments (Array)
        • String - template to render (default: null, related to app.views)
  • "static" - serves static files from a specific folder

    • Priority - 70
    • Module - "koa-file-server" (npm)
      • Arguments (Array)
        • String - local path to serve static files from (default: "path:./public")
  • "body" - parses request body

    • Priority - 80
    • Module - "koa-bodyparser" (npm)
      • Arguments (Array)
        • Object
          • "extendTypes" (Object) - extended types
  • "session" - internal middleware which maintains cookie session state based on koa-generic-session

    • Priority - 90
    • Module - "siren-js/middleware/session"
      • Arguments (Array)
        • String - the app instance placeholder
        • Object
          • "key" (String) - cookie name (default: "koa.sid")
          • "prefix" (String) - session prefix for store (default: "koa:sess:")
          • "cookie" (Object) - cookie options
  • "appsec" - secures the application against common vulnerabilities

    • Priority - 100
    • Module - "lusca" (github)
      • Arguments (Array)
        • Object
          • "csrf" (Boolean|Object) - value indicating whether to require CSRF tokens for non GET, HEAD, or OPTIONS requests, or an options object to configure CSRF protection (default: true)
          • "xframe" (String) - value for the X-Frame-Options header (default: "SAMEORIGIN")
          • "p3p" (String|Boolean) - the Compact Privacy Policy value or false if not used (default: false)
          • "csp" (Object|Boolean) - options configuring Content Security Policy headers or false if not used (default: false)
  • "router" - routes traffic to the applicable controller

    • Priority - 110
    • Module - "siren-enrouten" (npm)
      • Arguments (Array)
        • String - the app instance placeholder
        • Object
          • "index" (String) - path to the single file to load (default: "path:./routes")

Socket.io

The integration for socket.io is based on koa.io, but in default the io is disable. You can enable it through the config file, for example:

{
  "app": {
    // ...
  },

  "io": {
    "enabled": true,
    // other socket.io options.
  }
}

Once you enable socket.io, you can access it through app.io, for more information, please refer to koa.io.

Aditional Notes:

  • Unlike koa.io, there is no app.session, but instead, we wrap it into the middleware siren-js/middleware/session.js, which is used as middleware in default
  • The dependency for socket.io is marked as dev-dependency, so if you enable the socket.io, you should install socket.io package

Lifecycle Events

Siren.js adds support for additional events to your koa app instance:

  • start - the application has safely started and is ready to accept requests
  • shutdown - the application is shutting down, no longer accepting requests
  • stop - the http server is no longer connected or the shutdown timeout has expired

Configuration-based app Settings

You can configure the koa instance through the app field and these field can be access through app.settings. For example:

{
    "view engines": {
      // engine map
    },
    "app": {
        "env": "", // NOTE: `env` is managed by the framework. This value will be overwritten.
        "proxy": false,
        "port": 8000,
        "view engine": null,
        "view enabled": true,
        "views": "path:./views",
        "keys": ["siren-secret-key"]
    }
}

Additional Note:

  • The app:env, app:keys and app:proxy will auto set into the koa instance.
  • The app:view engine, view engines and app:views will decorate as
    {
      "default": "path:./views", // the value of `app:views`
      "map": { // the value of `view engines`
          
      }
    }
    and pass into siren-views as arguments when app:view enabled is not false.

Tests

$ npm test

Coverage

$ npm run-script test-cov
```