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

grunt-connect-delay

v0.2.4

Published

Provides a delay proxy for grunt connect.

Downloads

21

Readme

grunt-connect-delay Build Status

A delay proxy middleware for grunt-contrib-connect

This plugin provides a delay middleware for Grunt Connect / Express. It can be used for introducing artificial delays before proxying certain URLs based on RegExp rules.

By default, it includes the rule that any URL which matches the pattern ^/delay/([0-9]+)/(.*)$ will be delayed by $1 milliseconds and be redirected to /$2.

Hence, to check your app's behavior when a particular request is slow, just prefix /delay/5000/ to the URL.

Getting started

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins.

npm install grunt-connect-delay --save-dev

Options

useDefaultRule

Type: Boolean

Default Value: true

This adds the following default rule to the configuration:

{ rule    : '/delay/([0-9])+/(.*)$'
, delay   : '$1'
, rewrite : '/$2'
}

This means that to delay a link, you only need to prefix it with /delay/:milliseconds/<original URL>. grunt-connect-delay will wait for the said number of milliseconds before proceeding to the <original URL>

rulesProvider

Type: String

Default value: connect.delay

This is the section in the Gruntfile from where the delay rules will be read.

Simple rule format

{url: 'FROM', delay: MILLISECONDS }

Where:

  • FROM: RegExp string to match
  • MILLISECONDS: The delay in milliseconds to introduce before proceeding with proxying
Extended rule format

{url: 'FROM_REGEXP', delay: 'NUMBER_OR_REPLACEMENT', rewrite: 'REPLACEMENT' }

Where:

  • FROM_REGEXP: RegExp string to match the URL and create groups to be used for deducing delay and the true proxied URL. e.g. ^/delay/([0-9]*)/(.*)$.
  • NUMBER_OR_REPLACEMENT: The number of milliseconds (e.g. 1000) to wait or a string which can be parsed to an integer after a URL (which matches url) is replaced by it (e.g. $1).
  • REPLACEMENT: A string which maps to the destination URL after a URL is replaced by it (e.g. /$2).

Usage

In your project's Gruntfile:

  • Include the delayRequest snippet: var delayRequest = require('grunt-connect-delay/lib/utils').delayRequest.
  • Load the plugin: grunt.loadNpmTasks('grunt-connect-delay')
  • Add configureDelayRules before the web server task.

Optional (advanced usage):

  • Configure options in configureDelayRules.options in initConfig, or,
  • Add a section named delay to your existing Connect definition

To see some logs about the proxying, use the --verbose flag while running grunt.

var delayRequestSnippet = require('grunt-connect-delay/lib/utils').delayRequest;
grunt.initConfig({
    connect: {
        delay: [
            { url: '^/api/.*$', delay: 10000 } // Delay calls to API by 10sec
        ],
        dev: {
            options: {
                middleware: function (connect, options, middlewares) {
                    middlewares.unshift(delayRequestSnippet);
                    return middlewares;

                    // Old style:
                    // See: https://github.com/gruntjs/grunt-contrib-connect/issues/114
                    // 
                    // return [
                    //        delayRequestSnippet
                    //      , rewriteRequestSnippet
                    //      , proxyRequestSnippet
                    //      , connect.static(path.resolve(dir))
                    // ];
                }
            }
        }
    },
    /* Optional */
    configureDelay: {
        options: {
            rulesProvider  : 'connect.delay'
          , useDefaultRule : true
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-connect-delay');

// "configureDelayRules" should be before the "connect"/"express" task
grunt.registerTask('server', function (target) {
    grunt.task.run([
        'configureDelayRules',
        'connect:dev'
    ]);
});

Though I haven't tested it, it should work in a similar fashion with grunt-express task as well.

You can see how to use this plugin at musically-ut/grunt-connect-delay-example.

Credits

The structure and documentation of this plugin is inspired by grunt-connect-rewrite plugin.

Release Notes

  • 0.2.3: Update usage instuctions
  • 0.2.2: Bump version to resolve npm bug
  • 0.2.1: Improve documentation
  • 0.2.0: Add extended rules and a default rule
  • 0.1.1: Improve logging
  • 0.1.0: Initial release