• Stars
    star
    140
  • Rank 261,473 (Top 6 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Replay server responses from a HAR file

server-replay

Replay server responses from a .har file.

Useful if…

  • …you want to develop offline and your development server isn't local
  • …your development server is very slow and you want to go faster
  • …you are developing against an API with rate limits

It works by starting a proxy server and serving content from a previously saved .har file overlayed with files from your local system, configurable with mappings.

Installation

npm install -g server-replay

Running

You need to have a .har file, run server-replay, and then set up your browser to use it as a proxy.

A .server-replay.json in the current directory is used by default if no config option is given.

server-replay [options] <.har file>

Options:
  -c, --config  The config file to use
  -p, --port    The port to run the proxy server on  [default: 8080]
  -d, --debug   Turn on debug logging

See the Setup section for details on creating .har file and using the proxy in your browser.

Configuration

The config file is a JSON file (plus // comments) with the following properties:

  • version – currently 1
  • mappings – Maps from URLs to file system paths. It is an array of {match, path} objects. match is a string or regex, and path is a string. path can contain $n references to substitute capture groups from match.
  • replacements – Replaces strings in the body of textual content. It is array of {match, replacement} objects. match is a string, regex or variable, and replacement is a string or variable. replacement can contain $n references to substitute capture groups from match.

Full example:

{
    "version": 1,
    "mappings": [
        {
            "match": {"regex": ".*\\/static\\/(.*)"},
            "path": "./public/$1"
        }
    ],
    "replacements": [
        // For JSONP requests where callback name is randomly generated
        {
            "match": {"var": "entry.request.parsedUrl.query.callback"},
            "replace": {"var": "request.parsedUrl.query.callback"}
        },
        // Proxy only works over http
        {"match": "https", "replace": "http"}
    ]
}

Types

The types mentioned above take the following forms:

  • string – a plain string. Example: "something".
  • regex – an object with a regex property, and an optional flags property. If you don't provide the flags property, unlike JavaScript, the regex has the global flag set. Example: {"regex": "user\\/([a-z]+)", "flags": "ig"}
  • variable – an object with a var property which contains a path to a value. See below for the available variables. Example: {"var": "request.parsedUrl.query.q"}

Variables

  • request – the Node request object with the addition of a parsedUrl property, containing the parsed url and query.
  • entry – the HAR entry that is being used to respond to this request. It also has a parsedUrl property, and an indexedHeaders property which maps each header name to it's value(s).

Setup

Getting a .har file

The easiest way is with the Chrome DevTools. In the Network panel disable the cache, refresh the page and interact with the page to generate the network requests that you want to capture. Then right click and select "Save as HAR with Content".

Creating a .har file

Note: Extensions such as AdBlock and Ghostery interfere with the recorded responses. Be aware of this and, if necessary, disable them before loading the page.

Browser proxy configuration

Chrome

Launch with the --proxy-server argument:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --proxy-server=127.0.0.1:8080

Firefox

Preferences > Advanced > Network > Settings… > Manual proxy configuration

Firefox proxy settings

License

Copyright 2015 Adobe Systems Incorporated

This software is licensed under the Apache License, Version 2.0 (see LICENSE file).

More Repositories

1

jszip

Create, read and edit .zip files with Javascript
JavaScript
9,729
star
2

require1k

A minimal, and yet practically useful, CommonJS/Node.js `require` module loader for the browser in under 1000 bytes
JavaScript
345
star
3

jszip-utils

JavaScript
228
star
4

eslint-plugin-header

ESLint plugin to ensure that files begin with given comment
JavaScript
71
star
5

gooss

Use a Google spreadsheet as a database, no server-side code needed.
JavaScript
62
star
6

promise-me

Code transformer to change Node-style callbacks into promises.
JavaScript
46
star
7

Bubble-cursor

A bookmarket to add bubble cursor functionality to any website.
JavaScript
25
star
8

generate-words

Generate random (English) words.
JavaScript
14
star
9

sublime-edit-history

Deprecated, see ST3 note – Navigate back and forward between edit locations in the current file.
Python
13
star
10

jquery-reticulator

Put a grid over your layout!
JavaScript
5
star
11

phantom-wd

A webdriver wrapper around PhantomJS
JavaScript
5
star
12

foundry-heroku

Run Foundry Virtual Tabletop on Heroku and save data to GitHub, all for free.
JavaScript
4
star
13

tweet-page-chrome-extension

JavaScript
3
star
14

gs-desktop

A simple Python wrapper around the Grooveshark website, with support for media keys
Python
3
star
15

gooss-quickstart

Everything you need to get started with gooss.js, https://github.com/Stuk/gooss
JavaScript
3
star
16

bundle3

A Javascript-powered website listing all of the many indie bundles being released
JavaScript
3
star
17

js1k-2010

My entry for the 2010 JS1K competition
JavaScript
2
star
18

broken-app

JavaScript
1
star
19

sublime-recent-tab-sort

Keep your tabs/open files sorted in order of activation
Python
1
star
20

asdf

JavaScript
1
star
21

montage-compile-css-demo

A proof of concept to show how CSS could be compiled in Montage
JavaScript
1
star