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

faux-jax

v5.0.6

Published

Intercept and respond to requests in the browser (XMLHttpRequest, XDomainRequest) and Node.js (http(s) module)

Downloads

3,539

Readme

faux-jax Version Badge Build Status License Downloads

Browser tests

Intercept and respond to:

npm install faux-jax --save[-dev]

Browser example

var fauxJax = require('faux-jax');

fauxJax.install();

doRequest();
fauxJax.on('request', respond);

// somewhere in your code:
function doRequest() {
  var xhr = new XMLHttpRequest();

  xhr.open('POST', '/dawg');
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.send(
    JSON.stringify({
      YAW: 'dawg'
    })
  );
  xhr.onload = function() {
    console.log(xhr.status); // 200
    console.log(xhr.response); // {zup: 'bro'}
  }
}

// in a test file probably:
function respond(request) {
  request.respond(
    200, { // status
      'Content-Type': 'application/json' // headers
    },
    '{"zup": "bro?"}' //body
  );

  fauxJax.restore();
}

Node.js example

var http = require('http');
var fauxJax = require('faux-jax');

fauxJax.install();

doRequest();
fauxJax.on('request', respond);

function doRequest() {
  http.request('http://www.google.com', function(res) {
    console.log(res.statusCode); // 200

    var chunks = [];
    res.on('data', function(chunk) {
      chunks.push(chunk);
    });

    res.on('end', function() {
      console.log(Buffer.concat(chunks).toString());
    });
  }).end();
}

function respond(request) {
  request.respond(
    200, { // status
      'Content-Type': 'text/plain' // headers
    },
    'Hello Node.js!' //body
  );

  fauxJax.restore();
}

API

fauxJax.install([opts])

Replace global XMLHttpRequest and XDomainRequest with mocks.

  • opts.gzip: boolean. Set to true in nodejs to receive gzipped responses.

fauxJax.on('request', cb)

fauxJax is an EventEmitter.

Everytime a new request is made, you will get a request event.

You can listen to it with cb(request).

All requests have the native properties/methods from the spec.

We also added a couple of handy properties/methods for you to ease testing.

fauxJax.waitFor(nbRequests, cb)

Utility to "wait for n requests". Will call cb(err, requests).

request.requestMethod

request.requestURL

request.requestHeaders

Always {} with XDomainRequest.

request.requestBody

request.respond(status[, headers, body])

request.setResponseHeaders(headers)

request.setResponseBody(body[, cb])

fauxJax.restore()

Sets back global XMLHttpRequest and XDomainRequest to native implementations.

fauxJax.support

Object containing various support flags for your tests, used internally by faux-jax.

Errors

Errors will be emitted when:

  • you try to .install() when already installed
  • you try to .restore() without calling .install()
  • a request was intercepted while no listener set

How

tl;dr; We try to be as close as possible to the mocked native environment.

faux-jax uses feature detection to only expose what's relevant for the current environment.

i.e. on Chrome, we do not intercept nor expose XDomainRequest.

Also if the browser only implement some parts of XMLHttpRequest, we mimic it.

Test

npm test

Develop

npm run dev

Go to http://localhost:8080/__zuul.

Tests are written with tape and run through zuul.

Lint

npm run lint

Uses eslint, see .eslintrc.

Thanks

Inspiration for this module came from:

Many thanks!

Node.js version is using moll/node-mitm.