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

strategy

v1.1.1

Published

Real-time transport selection strategy

Downloads

167

Readme

Strategy

Made by unshiftVersion npmBuild StatusDependenciesCoverage StatusIRC channel

Strategy implements a strategy for selecting the correct transport based on a given set of restrictions. The module should work for every transport that is created using the transport-layer module.

Installation

As this module can be used with node.js and browserify it's released in the npm registry and can be installed using:

npm install --save recovery

The --save tells npm to automatically add the installed version to your package.json if one exists.

Usage

In all API examples we assume that you've already required and initialized a new Strategy instance using:

'use strict';

var Strategy = require('strategy')
  , Policy = Strategy.Policy
  , strategy = new Strategy();

The Strategy constructor allows one option argument and that is a list of pre-generated Policy instances (don't worry, you can always add more later). A Policy instance accepts the following arguments:

  1. Name of the policy, It can be omitted if you want to use the .name from the Transport's prototype.
  2. Transport Layer transport.
  3. Additional options that should be used for constructing a new instance.
var TransportLayer = require('transport-layer')
  , WebSockets
  , attempt;

WebSockets = TransportLayer.extend({
  name: 'websockets'
}, {
  readyState: 'complete',
  crossdomain: true,
  readable: true,
  writable: true
});

attempt = new Policy(WebSockets, { /* options * /});

// or:

attempt = new Policy('WebSockets', WebSockets, { /* options */});

Now that you've got some transports created you can assign them supply them to Strategy:

strategy = new Strategy([attempt /*, .. and more .. */]);

strategy.push

Add new Policy to the strategy. If you don't supply a policy instance as first argument we will automatically create a new one. The following arguments are accepted by this method:

  1. Name of the policy your about to push. If this is an Policy instance, we will not create a new one and you can safely ignore all other arguments.
  2. Transport that belongs to this policy.
  3. Options for the transport.
strategy
.push(attempt)
.push('websockets', WebSockets, { foo: 'bar' })
.push(TransportLayer.extend({ 'iframes' }, { readable: true }));

strategy.select

Select a new policy from the strategy. To find a suitable policy we need to know some specifics first. These specifics can be:

  • crossdomain: The transport should work cross domain.
  • not: The transport should not be in the given list, should be an object where the keys are lowercase names of the policies we should exclude.
  • available: The transport should be.
  • readable: The transport should be readable.
  • writable: The transport should be readable.
  • id: The id we should start at.

The method requires 2 arguments:

  • Configuration object with one or multiple properties mentioned.
  • Completion callback which follows an error first callback pattern.
strategy.select({
  crossdomain: true,
  not: { jsonp: true, htmlfile: true },
  readable: true,
  available: 'complete'
}, function (err, policy) {
  console.log(policy.name);       // Name of the policy, in lowercase.
  console.log(policy.transport);  // Reference to transport.
  console.log(policy.options);    // Additional configuration.
});

strategy.destroy

Completely destroy the strategy instance.

strategy.destroy();

License

MIT