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

@lazyduke/ajax-proxy

v1.0.2

Published

Ajax proxy which intercepts XMLHTTPRequest refactor by es6 Proxy.

Downloads

7

Readme

中文简体 | English

ajax-proxy

npm version GitHub npm bundle size

Introduction

ajax-proxy is a repo which intercepts XMLHTTPRequest refactor by es6 Proxy.

Usage

Installation

  • Using CDN
<script>
  https://unpkg.com/@lazyduke/ajax-proxy/dist/index.min.js
</script>
  • Using NPM

    npm install @lazyduke/ajax-proxy

Guide

基于 ES6 的 Proxy ,100 行代码实现一个 XMLHttpRequest 的拦截核心 ajax-proxy

API

ajax-proxy is very easy to use, it has only two methods called proxyAjax and unProxyAjax, you can get started quickly as long as you know of XMLHttpRequest.

proxyAjax(proxyMap)

  • proxyMap: arguments object,key is the property or method which need to be intercepted, value is the intercepting function.

    • Normal property: response, responseText, timeout... those can intercept through setter/getter, we can easily set the setter of property to true while intercepting method to change some readonly property such as response. Notice: xhr in args is the intercepted instance of XMLHttpRequest while this in args is the original instance of XMLHttpRequest.(if not using arrow function)

      • intercept response (getter)
      proxyAjax({
        response: {
          getter: function(value, xhr) {
            try {
              return JSON.parse(value)
            } catch (error) {}
          }
        }
      })
      • intercept timeout (setter)
      proxyAjax({
        timeout: {
          setter: function (v, xhr) {
            return Math.max(v, 1000)
          }
      
      })
      • intercept response (setter) and onload
      proxyAjax({
        response: {
          setter: true
        },
        onload: function(xhr) {
          try {
            /**
             * In the function which intercepts 'event' property,
             * only the property of this can be operate,
             * otherwise it will cause stack overlow.
             * (certainly do not use arrow function )
             */
            xhr.response = JSON.parse(this.response)
          } catch (error) {}
        }
      })
    • 'Event' property: onload, onreadystatechange... those can intercept through getter. Notice: xhr in args is the intercepted instance of XMLHttpRequest while this in args is the original instance of XMLHttpRequest.(if not using arrow function)

    proxyAjax({
      onload: function(xhr) {
        // do some intercepting
      }
    })
    • Method: open, send... those can intercept through getter. Notice: 1.args is an array of original method's arguments, xhr is the intercepted instance of XMLHttpRequest while this is the original instance of XMLHttpRequest.(if not using arrow function) 2.we can terminate intercept by returning value true, if an object is return, it will be the new arguments to pass

      • intercept open
      proxyAjax({
        open: function(args, xhr) {
          // do some intercepting
        }
      })
      • intercept open and terminate it
      proxyAjax({
        open: function(args, xhr) {
          // do some intercepting
          return true
        }
      })
      • intercept open and pass new arguments
      proxyAjax({
        open: function(args, xhr) {
          // do some intercepting
          function changeArgs(args) {
            // change args
          }
          return changeArgs(args)
          // also support function
          // return changeArgs
        }
      })
    • Management of request's context

      Assume we want to share a variable in open and onload, xhr is the context object we can use.

      proxyAjax({
        open: function(args, xhr) {
          xhr.xxx = '...'
        },
        onload: function(xhr) {
          xhr.xxx // ‘...’
        }
      })

unProxyAjax

  • Cancel the Proxy: Cancel intercepting original XMLHttpRequest object.

Notice

  • As for intercepting property, do not try to get access to any property of xhr in getter function as well as doing some assignment in setter function,just do all this operation in this.
  • This repo require browser environment which support ES6 and Proxy object.

Contributing

  1. Clone repo

  2. Type these in CMD

    npm install
    npm test

    to get started

Credits

Inspired by Ajax-hook 原理解析 of Ajax-hook.