Via the combination of a CLI and a browser extension, define sets of browser-side redirects (overrides) programmatically from the command line.
It was designed to allow one to develop web client-side apps on top of an external environment (ex: test or production) using assets served from one (or multiple) local dev-server(s).
Additionally, by having the command line as a source of truth, one can ensure that a set of overrides are only in place while the related process is running (ex: dev-server).
npm i network-overrides
npx network-overrides wrap-command 'npx webpack serve' my-first-override '[{"from":"https://live-cdn\\.com/(.*)","to":"http://localhost:8080/$1"}]' --ensure-backend
What does that command do?
- Ensure that the Network Overrides shared backend is running, which in turn will transmit the current overrides to the browser extension when necessary
- Add a single override which redirects assets from
https://live-cdn.com/
to the corresponding path underhttp://localhost:8080/
. It belongs to an override set called my-first-override - Run
npx webpack serve
which will bootstrap the local webpack-dev-server - Later, upon exiting, remove the override under my-first-override
See CLI commands for more details on the available API.
- Chrome:
- From Chrome's webstore
- Manually:
- Download the
network-overrides-<version>.crx
from the latest release - Open
chrome://extensions/
, enable Developer Mode and drag-and-drop the downloaded extension over the extensions page.
- Download the
Click the extension's icon and then the Connect button inside the extension's popover.
An override is described by a couple of properties:
from
: RegExp string that is used as a matching pattern for request urls, optionally defining the capture groups to be used in replacements withto
to
: Replacement string, able to support the typical patterns.
Essentially, the override is used within the extension as follows:
const pattern = new RegExp(override.from);
if(interceptedRequestUrl.match(pattern)){
const redirectUrl = interceptedRequestUrl.replace(pattern, override.to);
...
}
See CLI commands.
The network-overrides
node package is the core API in terms of controlling the active overrides.
In the middle of it all we have a shared backend process (typically running in the background) that:
- keeps in memory the list of current overrides
- is a REST server that exposes endpoints for listing, adding and removing overrides
- is a websocket server that allows the browser extension to be in sync with the current overrides
The CLI then serves as the API for that shared backend process, providing several commands that deal with bootstrapping the latter and adding/removing overrides.
The browser extension is responsible for:
- Allowing the user connect/disconnect with the shared backend's websockets
- Display the current sets of overrides
- Using the webRequest API intercept requests and conditionally redirect them based on the overrides in place
- add
network-overrides stop-backend
command - automatically stop shared backend when running in a background process and becomes idle
- add tests
- convert to TypeScript