Skip to content

Commit

Permalink
Updated documentation and CHANGELOG
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Jul 24, 2020
1 parent a060c38 commit 8913767
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.11.1

- Added options for proxy headers

## 0.11.0

- Updated dependencies
Expand Down
26 changes: 26 additions & 0 deletions docs/proxy-injector.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,33 @@ interface ProxyInjectorConfiguration {
active?: boolean;
agentOptions?: any;
proxy?: any;
defaultHeaders?: Array<string>;
discardHeaders?: Array<string>;
permitHeaders?: Array<string>;
followRedirect?: boolean;
}
```

The mapping is already a general configuration, as the targets need to be known as well as their usual counterparts (e.g., to identify the correct URLs in an HAR file). The `agentOptions` can be used to specify more sophisticated options for the proxyed request (e.g., which ciphers to use). The `proxy` option allows us to set a (corporate?) proxy to be used on the local machine (oh the irony - a proxy server that allows setting another proxy ...).

While the `defaultHeaders` provide a way to override the used default set of headers, `permitHeaders` are for explicitly allowing non-default headers and `discardHeaders` may be used to define which headers should never be considered.

If not explicitly specified the default headers are defined to be:

```js
const defaultProxyHeaders = [
'authorization',
'accept',
'content-type',
'cookie',
'accept-language',
'user-agent',
'if-match',
'if-range',
'if-unmodified-since',
'if-none-match',
'if-modified-since',
'pragma',
'range',
];
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kras",
"version": "0.11.0",
"version": "0.11.1",
"description": "Efficient server proxying and mocking in Node.js.",
"main": "dist/server/index.js",
"types": "dist/server/index.d.ts",
Expand Down
19 changes: 18 additions & 1 deletion src/server/helpers/proxy-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ import * as request from 'request';
import { fromNode } from './build-response';
import { KrasInjectorInfo } from '../types';

export const defaultProxyHeaders = [
'authorization',
'accept',
'content-type',
'cookie',
'accept-language',
'user-agent',
'if-match',
'if-range',
'if-unmodified-since',
'if-none-match',
'if-modified-since',
'pragma',
'range',
];

export interface ProxyCallback {
(err?: Error, foo?: any): void;
}
Expand All @@ -14,6 +30,7 @@ export interface ProxyRequestOptions {
agentOptions?: any;
proxy?: any;
injector?: KrasInjectorInfo;
redirect?: boolean;
}

export function proxyRequest(req: ProxyRequestOptions, callback: ProxyCallback) {
Expand All @@ -28,7 +45,7 @@ export function proxyRequest(req: ProxyRequestOptions, callback: ProxyCallback)
agentOptions: req.agentOptions,
headers: req.headers,
body: req.body,
followRedirect: req.followRedirect || true,
followRedirect: req.redirect ?? true,
},
(err, ans, body) => {
if (err) {
Expand Down
31 changes: 18 additions & 13 deletions src/server/injectors/proxy-injector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as WebSocket from 'ws';
import { EventEmitter } from 'events';
import { proxyRequest } from '../helpers';
import { proxyRequest, defaultProxyHeaders } from '../helpers';
import {
KrasInjector,
KrasAnswer,
Expand All @@ -13,12 +13,20 @@ import {
export interface ProxyInjectorConfig {
agentOptions?: any;
proxy?: any;
defaultHeaders?: Array<string>;
discardHeaders?: Array<string>;
permitHeaders?: Array<string>;
followRedirect?: boolean;
}

export interface DynamicProxyInjectorConfig {
[target: string]: string;
}

function normalizeHeader(header: string) {
return header.toLowerCase();
}

interface WebSocketSessions {
[id: string]: WebSocket;
}
Expand Down Expand Up @@ -145,24 +153,21 @@ export default class ProxyInjector implements KrasInjector {
}

handle(req: KrasRequest): Promise<KrasAnswer> | KrasAnswer {
const defaultHeaders = this.config.defaultHeaders || ["authorization", "accept", "content-type", "cookie", "accept-language", "user-agent", "if-match", "if-range", "if-unmodified-since", "if-none-match", "if-modified-since", "pragma", "range"];
const discardHeaders = this.config.discardHeaders || [];
const permitHeaders = this.config.permitHeaders || [];
const allowHeaders = [...defaultHeaders.filter(header => !discardHeaders.includes(header.toLowerCase())), ...permitHeaders];
const headers = allowHeaders.reduce((headers, header) => {
header = header.toLowerCase();
if(req.headers[header]){
headers[header] = req.headers[header];
}
const defaultHeaders = (this.config.defaultHeaders || defaultProxyHeaders).map(normalizeHeader);
const discardHeaders = (this.config.discardHeaders || []).map(normalizeHeader);
const permitHeaders = (this.config.permitHeaders || []).map(normalizeHeader);
const headerNames = [...defaultHeaders.filter(header => !discardHeaders.includes(header)), ...permitHeaders];
const headers = headerNames.reduce((headers, header) => {
headers[header] = req.headers[header];
return headers;
}, {});
}, {} as Record<string, string | Array<string>>);
const [target] = this.connectors.filter(m => m.target === req.target);

if (target) {
return new Promise<KrasAnswer>(resolve =>
proxyRequest(
{
headers: headers,
headers,
url: target.address + req.url,
method: req.method,
body: req.content,
Expand All @@ -172,7 +177,7 @@ export default class ProxyInjector implements KrasInjector {
name: this.name,
host: target,
},
followRedirect: req.config.followRedirect || true,
redirect: this.config.followRedirect,
},
(err, ans) => {
if (err) {
Expand Down

0 comments on commit 8913767

Please sign in to comment.