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

@pixul/attache

v3.3.1

Published

Hapi plugin to register as a Consul service

Downloads

6

Readme

attaché

Forked from https://github.com/kanongil/attache

A hapi.js plugin that registers a Consul service.

Build Status

Example

const Attache = require('@pixul/attache');
const Hapi = require('@hapi/hapi');

const server = Hapi.Server();

const provision = async () => {

    await server.register({
        register: require('@pixul/attache'),
        options: {
            service: {
                name: 'myservice'
            }
        }
    });

    server.route({
        method: 'GET',
        path: '/',
        handler() => {

            return 'Hello World!';
        }
    });

    server.route({
        method: 'GET',
        path: '/_health',
        handler() => {

            return 'OK';
        }
    });

    await server.start();

    console.log('Server started at', server.info.uri);
});

This will register a "myservice" service with Consul.

The service can be discovered using the standard Consul interfaces (eg. DNS or HTTP).

Usage

Install using npm install @pixul/attache, and register with Hapi using server.register().

Service registration & de-registration

Service registration is performed automatically as part of the server.start() processing.

Service de-registration is performed as part of the server.stop() processing. Once the server has started, it is important that server.stop() is called (and completed) before exiting the process. Otherwise, the service won't be deregistered. Note that server.stop() must also be called when server.start() returns with an error.

If the service is not de-registered, it will linger in the Consul registry. If configured with a health check, it will eventually be marked as unhealthy.

Plugin registration options

All configuration is optional:

  • service - Object containing service registration information:
    • name - String with service name, as registered with Consul. Default: "hapi".
    • check - Object describing optional Consul health check:
      • path - String with path to http health check endpoint. Return any 2xx code to indicate "passing", 429 for "warning", and any other response for "failure". If false, disables health check. Default: "/_health".
      • ttl - Number with check interval in ms, or a String. Default: undefined.
      • interval - Number with check interval in ms, or a String. Default: "5s".
      • deregisterAfter - Number with automatic critical service de-registration interval in ms, or a String. Default: "120m".
      • startHealthy - Boolean, when set starts service in "passing" state. Default: true.
  • retryStrategy - async function that is passed (consul, err, details) when an automatic registration or de-registration fails. If a Number is returned, it is used as a delay before retrying.
  • lowProfile- Boolean that enables a low profile mode, which registers in the background and silences errors.
  • consul - Object with consul agent connection information:
    • host - String with agent address. Default: "127.0.0.1".
    • port - Number with agent HTTP(s) port. Default: 8500.
    • secure - Boolean indicating if HTTPS is required. Default: false
    • ca - Array of Strings or Buffers of trusted certificates in PEM format.

If a http health check is configured, you can either point it to an existing path, or preferably create a custom route:

server.route({
    method: 'GET',
    path: '/_health',
    handler() => {

        return 'OK';
    }
});

Note that Consul does not use the returned value but it is recorded as part of the health check, useable for debugging, etc.

For ttl checks, a periodic check-in within the ttl timeout is required. This can be done using the consul object on the server, eg.:

setInterval(() => {

    server.consul.checkin(true);
}

Custom service id options

The service is registered as a unique service instance. The instance has a host unique ids, generated based on the service name, listening port, and process PID. Alternatively, a custom id and/or tags can be specified with the connection plugin option:

Hapi.Server({ plugins: { attache: { id: 'myservice-1', tags: ['hello'] } } });

Consul class interface

Available at server.consul once the plugin has been registered.

`await checkin(isOk, [note])

Reports current status of the consul service when TTL checks are used. If it has not been called successfully within the TTL interval since the last checkin, the health check will fail.

  • isOk - true indicates a "passing" state, false a "failing", and anything else is a "warning".
  • note - String with optional note that is recorded by consul.

Logging

All internal logging is tagged with attache and performed at the server level. Any de-registration errors are only exposed through this mechanism.