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

rewriting-proxy

v0.5.2

Published

Proxy for rewriting JavaScript on-the-fly

Downloads

120

Readme

rewriting-proxy

Simple proxy library for rewriting JavaScript on-the-fly.

Installation

npm install rewriting-proxy

Usage

From proxy-example.js:

var proxy = require("rewriting-proxy");

var options = {
    rewriter: function (src, metadata) {
        console.log("instrumenting " + metadata.url);
        return src;		
    },
    rewriteOptions: {
        onNodeVisited: function (node) {
            // Tag every element in the HTML with its source location
            if (node.tagName) {
                var location = node.__location;
                node.attrs.push({ name: "data-loc", value: location.line + ":" + location.col })
            }
        },
        locationInfo: true
    },
    headerHTML: "<script>alert(\"hi\");</script>",
    port: 8080
};

proxy.start(options);

The start(options) function starts the proxy server on localhost. Possible options are:

  • rewriter: A function that takes JavaScript code as its first parameter and returns the instrumented version of the code. Any response with a MIME type suggesting JavaScript content is passed to rewriter. The metadata object passed as the second parameter includes properties:
    • type: the type of the script, either 'script' for inline scripts or script files, 'event-handler' for event handlers, or 'javascript-url' for JavaScript URLs.
    • inline: For scripts of type 'script', indicates whether the script was inline.
    • url: A URL for the script; these are auto-generated for inline scripts, event handlers, etc.
  • rewriteOptions: An object that allows instrumenting the HTML. The object includes properties:
    • onBeforeNodeVisited: A function that is called with a HTML node when the entire subtree rooted at the node has been visited. The function return value is ignored; instead the function should mutate the subtree rooted at the node argument. Is unlike onNodeVisited invoked prior to any JavaScript instrumentation performed by rewriter.
    • onNodeVisited: A function that is called with a HTML node when the entire subtree rooted at the node has been visited. The function return value is ignored; instead the function should mutate the subtree rooted at the node argument.
    • locationInfo: A boolean that is parsed to the parse5 HTML parser, indicating whether node locations should be made available. (Disabled by default for performance.)
  • headerHTML: HTML string to be injected at the beginning of any requested HTML file.
  • headerURLs: an Array of script URLs. These URLs will be loaded via <script> tags at the beginning of any HTML file.
  • port: The port on which the proxy server should listen, default 8080.

The library also exposes a rewriteHTML(html, url, rewriter, headerHTML, headerURLs, options) function that rewrites the inline scripts (and HTML) in a given html string with URL url, using the rewriter function, headerHTML string and headerURLs array as described above.

To see a real-world use of the library, look at jalangi_proxy.js from the Jalangi framework.

Example

Input:

<!DOCTYPE html>
<html>
  <head>
    <title>Proxy test</title>
  </head>
  <body>
    <h1>Proxy test</h1>
  </body>
</html>

Output using proxy-example.js (modulo indentation):

<!DOCTYPE html>
<html data-loc="2:1">
  <head data-loc="3:3">
    <script>alert("hi");</script>
    <title data-loc="4:5">Proxy test</title>
  </head>
  <body data-loc="6:3">
    <h1 data-loc="7:5">Proxy test</h1>
  </body>
</html>

Limitations

  • Can't rewrite scripts requested via HTTPS.

  • Can't rewrite inline scripts inserted by other scripts, e.g.:

      var x = "<script>" + ... + "</script>";
      node.innerHTML = x;

License

This software is distributed under the Eclipse Public License.