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

cptn

v0.2.2

Published

A webserver that does something with POSTed JSON. Indended for use with GitLab webhooks.

Downloads

4

Readme

Cptn

Cptn is a quick and easy web server that responds to POST requests containing JSON data. It’s meant for use with GitLab webhooks.

Usage

var captain = require('cptn')(9000);
captain.hook('myproject/push', { cwd: '.' }, 'echo "Push received" >> captains-log');
captain.hook('myproject/merge-request', function (data, done) { console.log(data); done(); });

This starts a new Cptn server, makes it listen on port 9000 (a Unix socket can be used instead by passing a string) and attaches two hooks: myproject/push and myproject/merge.

myproject/push will be called when sending a POST request to localhost:9000/myproject/push. It will run the provided unix command.

myproject/merge-request will be called when sending a POST request to localhost:9000/myproject/merge-request. It will log the received data (the JSON body of the POST request).

You can try it with these curl commands:

curl -H "Content-type: application/json" -X POST -d '{}' http://localhost:9000/myproject/push
curl -H "Content-type: application/json" -X POST -d '{"foo": "bar"}' http://localhost:9000/myproject/merge-request

API Documentation

Cptn(port, options)

Starts a new Cptn server and listens on the specified port (a port number or Unix socket name).

Returns a Cptn server instance.

Accepts the following arguments:

  • port (required) A port number or socket to listen on.
  • options (optional) An object with the following options:
    • debug (optional, defaults to false) Whether to log debug output to the console.

captain#hook(url[, options], action)

Attaches a hook that will call the specified action each time a POST request is sent to the path specified with name.

Returns nothing.

Accepts the following arguments:

  • url (required) A url at which the hook can be reached. No leading slash.
  • options (optional) An object that will be passed to child_process.exec() if a Unix command is specified as the action. Currently ignored if a callback is passed as action, but may be used in future versions.
  • action (required) An action that will be called upon a POST request to the specified url. Can be a string, in which case it will be passed – together with the options object – to child_process.exec() or a function with the signature (data, callback). data will contain the body of the POST request. callback() should be called when you're done doing what you had to do. It currently has no effect but most likely will in future versions.

Server Behaviour

The server currently responds with a status code and an empty body. It returns 200 (OK) when a hook was found and successfully invoked, 404 (Not Found) when no hook was found, 405 (Method Not Allowed) when the request was not a POST request, 400 (Bad Request) when the body is not valid JSON, and 500 (Internal Server Error) when there was a problem invoking the hook.

To Do

  • Write tests
  • Add support for basic middleware if needed (but keep it simple)
  • Allow regular expressions as URLs if needed