reserve 1.1.1
reserve: ^1.1.1 copied to clipboard
Server that can forward requests from one location to another while reprocessing the responses.
ReServe #

Table of Contents
What is ReServe #
ReServe is a server that acts as a reverse proxy, but can also alter the responses for things like secure cookies and redirects where the backend service provides domain names.
It supports either HTTP or HTTPS. It is designed for web developers who need to test against live APIs that don't support CORS for local development or utilize cookies that are locked to a domain or are secure.
Usage #
Installing
dart pub global activate reserve
Running
reserve [(-c || --config) configfile.yaml]
For more information on the configuration, see the config section below.
Config #
The ReServe configuration has a search path. It utilizes a search path to locate the confiration which is:
- If a
configparam is set, the file passed in that parameter. reserve.yamlin the current directory.web_dev_config.yamlin the current directory, utilizing thereservekey.pubspec_overrides.yamlin the current directory, utilizing thereservekey.pubspec.yamlin the current directory, utilizing thereservekey.
Schema
| Key | Default | Example | Description |
|---|---|---|---|
vars |
n/a | See Vars | Variables that can be utilized throughout the config and the set-response interceptor. |
host |
localhost |
localhost.direct |
The host name the server will listen on. |
port |
5433 |
8080 |
The port the server will listen on. |
log |
config |
finest |
The log level to emit server level events. |
origin |
n/a | https://www.example.com |
Used if this is a middleware proxy. The origin will be used as the origin, referer, and cookie domains when communicating between the caller and the target. |
proxy |
n/a | localhost:8888 |
Used if a web debugging proxy is being used to assist in debugging network issues. Omit otherwise. |
https |
n/a | See HTTPS | Include if, and only if, you wish ReServe to serve via HTTPS. |
interceptors |
n/a | See Interceptors | The list of interceptors to apply to all of the proxied requests and responses. |
Example
vars:
api-server: https://api.example.com
host: localhost
port: 8080
web-server: https://www.example.com
host: ${vars.host}
port: ${vars.port}
routes:
/api/:
log: fine
redirect: ${vars['api-server']}
interceptors:
- type: cookie
with:
allow-secure: false
/:
log: warn
redirect: ${vars['web-server']}
interceptors:
- type: remove-headers
with:
headers:
- cache-control
- pragma
- type: cookie
with:
allow-secure: false
- type: replace-body
with:
from: ${vars['api-server']}
replace: http://${vars.host}:${vars.port}
Vars #
A section that contains variables. The viarbles may not reference other variables, however, they can be template expressions.
For example, let's say you have a local configuration file named: dev_config.yaml...
api-server: https://api.example.com
key-not-for-an-api: dEfinateLyN0t@nAp!K3y
... and you want to store that in a variable named "config". To do so, you could create a vars section like:
vars:
# yaon.decode() // handles either YAML or JSON seamlessly as an input
# File(path).readAsStringSync // Reads a file using the synchronous I/O
config: ${yaon.decode(File('dev_config.yaml').readAsStringSync())}
Then if you wanted to reference those entries, you could create a route as follows:
routes:
/api/:
redirect: ${vars.config['api-server']}
interceptors:
- type: set-headers
with:
headers:
x-key-not-for-apis: ${vars.config['key-not-for-an-api']}
Routes #
The routes are a key / value pair. The key is the path to listen on for the route. Every URL prefixed by that value will be matched to the route. In RegExp form, it's effectively ${key}.*.
Routes are evaluated in order and the first match is used. If no route matches then a 404 will be thrown.
Schema
| Key | Default | Example | Description |
|---|---|---|---|
log |
config |
finest |
The log level to emit server level events. |
redirect |
n/a | https://api.example.com |
The URL to redirect the route to. |
interceptors |
n/a | See Interceptors | The interceptors to apply to just this route. |
Interceptors #
| Key | Example | Description |
|---|---|---|
type |
cookie |
The specific identifier for the interceptor. |
with |
Map | The parameters to pass to the interceptor. |
Built In Interceptors #
| Type | Request | Response | Description |
|---|---|---|---|
| cookie | ❌ | ✅ | Alters the cookie headers to make them seem as if they are passed directly between the source and target. |
| cors | ✅ | ✅ | Adds CORS related headers to requests and automatically handles OPTIONS calls. |
| remove-headers | ✅ | ✅ | Removes a set of headers. |
| replace-body | ❌ | ✅ | Replaces text within the response body. |
| replace-headers | ✅ | ✅ | Replaces the value of headers. |
| set-headers | ✅ | ✅ | Sets a mapping of key / value pairs on the request and/or response. |
| set-response | ❌ | ✅ | Sets the entire response. Used mainly for mock responses. |
cookie
Performs cookie modifications to make it appear as if the cookies are sent directly between the source and target without any middleware.
Parameters
| Key | Default | Description |
|---|---|---|
allow-secure |
true |
Set to false to remove the Secure attribute from set-cookie headers so that the cookies can be used by HTTP sites. |
cors
Automatically responds to OPTIONS based requests with CORS information and augments responses with the same CORS information.
Parameters
| Key | Default | Description |
|---|---|---|
additional-headers |
[] |
Convenience option to easily set additional headers in addition to the default headers. |
allow-credentials |
false |
Set to true to allow credentials to be sent from the client. |
allow-headers |
accept, accept-encoding, accept-language, content-type, dnt, if-none-match, origin, user-agent |
Set to the base list of headers to allow. The additional-headers will be sent in addition to this value. |
allow-methods |
DELETE, GET, OPTIONS, PATCH, POST, PUT |
The list of methods to send to the client to be allowed. |
expose-headers |
[] |
Which response headers should be made available to scripts running in the browser. |
max-age |
24 Hours | Number of seconds the CORS request is valid for before a client should re-request. |
remove-headers
Removes the headers with the given keys from the request and / or response.
Parameters
| Key | Default | Description |
|---|---|---|
headers |
[] |
The set of case-insensitive keys to remove. |
request |
true |
Alters the request headers when true. |
response |
true |
Alters the response headers when true. |
replace-body
Replaces all instances of from in the response body with the value in replace. No-ops when the body is not a textual content type.
Parameters
| Key | Default | Description |
|---|---|---|
from |
n/a | The value to find to replace. |
replace |
n/a | The replacement to apply to the found value. |
replace-headers
Replaces all instances of from in the headers with the value in replace.
Parameters
| Key | Default | Description |
|---|---|---|
from |
n/a | The value to find to replace. |
replace |
n/a | The replacement to apply to the found value. |
request |
true |
Alters the request headers when true. |
response |
true |
Alters the response headers when true. |
set-headers
Sets the given headers on the request and / or response.
Parameters
| Key | Default | Description |
|---|---|---|
headers |
{} |
The key / value pair map of headers to set. |
request |
true |
Alters the request headers when true. |
response |
true |
Alters the response headers when true. |
set-response
Performs cookie modifications to make it appear as if the cookies are sent directly between the source and target without any middleware.
Parameters
| Key | Default | Description |
|---|---|---|
body |
n/a | The file name of the file to use as the response body. |
headers |
[] |
The key / value pair map of headers to set. |
status-code |
200 |
The status code to set. |
HTTPS #
Information related to the security context that is built from a certificate chain and private key.
Parameters
| Key | Description |
|---|---|
certfile |
Path to the certificate chain file. |
certpass |
Optional password for the certificate chain file. |
keyfile |
Path to the private key file. |
keypass |
Optional password for the private key file. |