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

handshake

v0.0.3

Published

Handshake processing for unshift.io

Downloads

18

Readme

handshake

Made by unshiftVersion npmBuild StatusDependenciesCoverage StatusIRC channel

Handshake is a small library which helps you create, validate handshakes between client and server. To make this module as flexible and extendible as possible we don't make any assumptions about properties the data that should be returned in the handshake the only requirements we set are:

  • It should be an object with keys so we can transform the key and value's to a query string.

So you might wonder why we went with a query string. There a couple of good reasons about this. When we are handshaking we do not know which protocols the server and clients support so we cannot encode and decode the data yet. So we need to have an encoding format which is super easy to parse for both the server and clients. In addition to that it's easy to debug as it's a human readable format.

Installation

The module is released in the public npm registry and can be installed using:

npm install --save handshake

Usage

In all the examples we assume that you've already required and setup the Handshake instance using:

'use strict';

var Handshake = require('handshake');

To construct a new Handshake instance we need two things:

  1. A context/scope/this value for all the callbacks that we execute. Which is required but can be set to null, undefined or whatever.
  2. Options for further configuring the handshake.

So for the optional options, you can supply the following properties:

  • handshake timeout Maximum time a user is allowed to spend to modifying the handshake data. As the last thing we want to do is introduce extra latency. Defaults to 5 seconds.
  • stringify Custom stringify method which will be used to completely encode the handshake. Defaults to querystringify.stringify.
  • id Unique id generator which will be called for each handshake.get call. Defaults to uuid.v4.

For our examples we just assume it has been setup as following:

var handshake = new Handshake();

set

The set method allows you to assign properties on the handshake that should be added to every .get call. The method requires 2 arguments.

  1. key The name of the property which should be added to the handshake.
  2. value The value that needs to be stored. If you supply a function it will be called every time the handshake.update() method is called. The returned result of the function will be set as result.
handshake
.set('version', require('./package.json').version)
.set('another', 'value')
.set('generated', function () {
  return 'foo';
});

update

Configure/compile the default values of the handshake payload. This way we don't have to re-compile the data every single time a handshake is requested. You should call the update method once you are done with setting all your properties using the .set method.

handshake
.set('version', require('./package.json').version)
.set('another', 'value');

handshake.update();

get

This is where all the magic happens, this is the api what it's all about. It gets the handshake payload. The get method requires 2 arguments:

  1. Modify A function which is called with the handshake object before it's encoded. This allows you to modify and add values to the handshake. If the function has 1 argument we assume it's a synchronous call, if it has 2 arguments we see it as an async call and add a callback function as last argument.
  2. Complete The completion callback which follows an error first callback pattern.

Errors are automatically handled by this function. When you return an error in sync mode, provide an error as first argument in async mode or if we fail to encode the object we will encode a special object which will have an error property set to the error message and we will supply this as data argument to the complete function. Don't worry, we will also still set the error argument but this way you will always have data to return.

handshake.get(function modify(payload) {
  payload.foo = 'bar';
}, function generated(err, data) {
  // do things
});

destroy

Destroy the created handshake instance. This releases all internal references so it can be reclaimed by the garbage collector.

handshake.destroy();

License

MIT