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

xhook

v1.6.2

Published

Easily intercept and modify XHR request and response

Downloads

17,678

Readme

XHook

Easily intercept and modify XHR ("AJAX") request and response

With XHook, you could easily implement functionality to:

  • Cache requests in memory, localStorage, etc.
  • Insert authentication headers
  • Simulate responses
    • Create fake transparent backends for testing purposes
  • Sending Error statistics to Google Analytics
  • Create a client-side alternative to CORS by offloading requests to an iframe then splicing the response back in, see XDomain
  • Devious practical jokes
  • Supports RequiresJS and Browserify
  • Preflight GZip compression, see XZip (Incomplete)

Features

  • Intercept and modify XMLHttpRequest ("AJAX") request and response
  • Simulate responses transparently
  • Backwards compatible addEventListener removeEventListener
  • Backwards compatible user controlled progress (download/upload) events

Browser Support

Support Modern Browser.

Demos

http://jpillora.com/xhook

Usage

:warning: It's important to include XHook first as other libraries may store a reference to XMLHttpRequest before XHook can patch it

Using script link to load xhook and use it, like so:

<script src="//unpkg.com/xhook@latest/dist/xhook.min.js"></script>
<script>
  xhook.after(function (request, response) {
    if (request.url.match(/example\.txt$/))
      response.text = response.text.replace(/[aeiou]/g, "z");
  });
</script>

We can also install xhook via npm.

npm install xhook

Then use ESM syntax to load xhook.

import xhook from "xhook";
//modify 'responseText' of 'example2.txt'
xhook.after(function (request, response) {
  if (request.url.match(/example\.txt$/))
    response.text = response.text.replace(/[aeiou]/g, "z");
});

API

xhook.before(handler(request[, callback])[, index])

Modifying any property of the request object will modify the underlying XHR before it is sent.

To make the handler is asynchronous, just include the optional callback function, which accepts an optional response object.

To provide a fake response, return or callback() a response object.

xhook.after(handler(request, response[, callback]) [, index])

Modifying any property of the response object will modify the underlying XHR before it is received.

To make the handler is asynchronous, just include the optional callback function.

xhook.enable()

Enables XHook (swaps out the native XMLHttpRequest class). XHook is enabled be default.

xhook.disable()

Disables XHook (swaps the native XMLHttpRequest class back in)


request Object

  • method (String) (open(method,url))
  • url (String) (open(method,url))
  • body (String) (send(body))
  • headers (Object) (Contains Name-Value pairs set with setRequestHeader(name,value))
  • timeout (Number) (timeout)
  • type (String) (responseType)
  • withCredentials (String) (withCredentials)

response Object

  • status (Number) Required when for fake responses (status)
  • statusText (String) (statusText)
  • text (String) (responseText)
  • headers (Object) (Contains Name-Value pairs retrieved with getAllResponseHeaders())
  • xml (XML) (responseXML)
  • data (Varies) (response)

Overview

The dark red before hook is returning a response object, which will trigger the after hooks, then trigger the appropriate events, so it appears as if response came from the server.

Reference

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

http://www.w3.org/TR/XMLHttpRequest/

http://www.w3.org/TR/XMLHttpRequest2/

Issues

  • XHook does not attempt to resolve any browser compatibility issues. Libraries like jQuery and https://github.com/ilinsky/xmlhttprequest will attempt to do this. XHook simply proxies to and from XMLHttpRequest, so you may use any library conjunction with XHook, just make sure to load XHook first.

  • You may use synchronous XHR, though this will cause asynchronous hooks to be skipped.

Contributing

See CONTRIBUTING for instructions on how to build and run XHook locally.

License

MIT License Copyright © 2022 Jaime Pillora [email protected]