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

@atlas.js/koa

v3.3.0

Published

Koa.js service for @atlas.js

Downloads

86

Readme

@atlas.js/koa

Koa.js service and a Middleware hook loader for @atlas.js.

Installation

npm i @atlas.js/koa

Usage

Service

The service configuration allows you to define three things:

import { Atlas } from '@atlas.js/atlas'
import * as Koa from '@atlas.js/koa'

const atlas = new Atlas({
  config: {
    services: {
      http: {
        // Listen on this port and network interface
        listen: {
          port: 3000,
          hostname: '127.0.0.1',
        },
        // These are set on the http.Server instance
        server: {
          timeout: 30000,
        },
        // These are set on the Koa instance
        koa: {
          proxy: true,
        },
        // If set to an object, will be used to load all middleware found in this module into the
        // Koa instance
        middleware: {
          module: 'path/to/middleware',
          config: {
            bodyparser: {},
            // ...
          }
        }
      }
    }
  }
})
atlas.service('http', Koa.Server)
await atlas.start()

// The Koa instance is now available here:
atlas.services.http
// And the http.Server instance is also exposed:
atlas.services.http.server

Example middleware module

Here is an example middleware module that the service supports.

// middleware.js
import forcehttps from './forcehttps'
import routes from './routes'
import notfound from './notfound'

export default {
  forcehttps,
  routes,
  notfound,
}

Accessing the Atlas instance

The Atlas instance can be accessed through ctx.atlas in middleware or routes.

Here is an example middleware that makes use of the Atlas instance inside the route handler. It returns 400 status code with a custom message when the request is made on an insecure protocol.

// middleware/forcehttps.js
export default function mkforcehttps(config) {
  return function forcehttps(ctx, next) {
    // Here you can access the Atlas instance via `ctx.atlas`
    if (ctx.atlas.env === 'production' && !ctx.secure) {
      ctx.response.status = 426
      ctx.response.set({
        Upgrade: 'TLS/1.2, HTTP/1.1',
        Connection: 'Upgrade',
      })
      ctx.response.body = {
        message: 'I refuse to talk to you while anyone may be listening.',
      }

      return
    }

    await next()
  }
}

ContextHook

This hook allows you to extend the Koa context object prototype with custom functions or properties. It might be useful to define response type aliases, such as ctx.ok(), or ctx.forbidden().

ContextHook Dependencies

  • service:koa: A Koa service on which to extend the context
const atlas = new Atlas({
  config: {
    hooks: {
      context: {
        // The path to the module, relative to root, which should be loaded and
        // properties/functions from that module added to koa.context
        module: 'server/context',
      }
    }
  }
})

atlas.service('http', Koa.Server)
atlas.hook('context', Koa.ContextHook, {
  aliases: {
    'service:koa': 'http'
  }
})
await atlas.start()

// server/context.js
export default {
  ok(body = {}) {
    this.status = 200
    this.body = body
  },

  forbidden() {
    this.status = 403
    this.body = {
      error: 'Forbidden'
    }
  }
}

WebsocketHook

This hook extends the Koa instance with websocket protocol support, using koa-websocket.

WebsocketHook Dependencies

  • service:koa: A Koa service on which to add the websocket protocol support
const atlas = new Atlas({
  config: {
    hooks: {
      websocket: {
        // This has the same structure and purpose as the Koa service's middleware config: it allows
        // you to add websocket-specific middleware to the server.
        middleware: {
          module: 'path/to/websocket/middleware',
          config: {}
        },
        // This goes directly to the websocket protocol's constructor. Note that you should not
        // use `host`, `port` or `server` options since the server instance is re-used from the
        // underlying Koa server and creating a new http server could cause unwanted side-effects.
        // See: https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
        listen: {},
      }
    }
  }
})

Once attached to a Koa servise, the websocket interface is accessible as per the library's definition via koa.ws, which in Atlas it would be:

atlas.services.http.ws

License

See the LICENSE file for information.