Skip to content

Commit

Permalink
Proxy headers from server request to Got (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Apr 11, 2019
1 parent b5e443b commit 00e5fd5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
16 changes: 10 additions & 6 deletions migration-guides.md
Expand Up @@ -134,19 +134,23 @@ Hooks are powerful, aren't they? [Read more](readme.md#hooks) to see what else y
Let's take a quick look at another example from Request's readme:

```js
http.createServer((req, res) => {
if (req.url === '/doodle.png') {
req.pipe(request('https://example.com/doodle.png')).pipe(res);
http.createServer((request, response) => {
if (request.url === '/doodle.png') {
request.pipe(request('https://example.com/doodle.png')).pipe(response);
}
});
```

The cool feature here is that Request can proxy headers with the stream, but Got can do that too:

```js
http.createServer((req, res) => {
if (req.url === '/doodle.png') {
req.pipe(got.stream('https://example.com/doodle.png')).pipe(res);
http.createServer((request, response) => {
if (request.url === '/doodle.png') {
// When someone makes a request to our server, we receive a body and some headers.
// These are passed to Got. Got proxies received data to our server response,
// so you don't have to do `response.writeHead(statusCode, headers)` and `response.end(body)`.
// It's done automatically.
request.pipe(got.stream('https://example.com/doodle.png')).pipe(response);
}
});
```
Expand Down
10 changes: 10 additions & 0 deletions source/as-stream.ts
@@ -1,4 +1,5 @@
import {PassThrough as PassThroughStream, Duplex as DuplexStream} from 'stream';
import {IncomingMessage} from 'http';
import duplexer3 from 'duplexer3';
import requestAsEventEmitter from './request-as-event-emitter';
import {HTTPError, ReadError} from './errors';
Expand Down Expand Up @@ -105,6 +106,15 @@ export default function asStream(options: MergedOptions) {
return unpipe(stream);
};

proxy.on('pipe', source => {
if (source instanceof IncomingMessage) {
options.headers = {
...source.headers,
...options.headers
};
}
});

proxy.isFromCache = undefined;

return proxy;
Expand Down
2 changes: 1 addition & 1 deletion source/utils/types.ts
Expand Up @@ -139,7 +139,7 @@ export interface Options extends Omit<https.RequestOptions, 'agent'> {
agent: http.Agent | https.Agent | boolean | AgentByProtocol;
gotTimeout?: number | Delays;
cache?: string | StorageAdapter;
headers?: {[key: string]: string};
headers?: {[key: string]: string | string[]};
// TODO: Remove this once TS migration is complete and all options are defined.
[key: string]: unknown;
}
Expand Down
18 changes: 18 additions & 0 deletions test/stream.ts
Expand Up @@ -30,6 +30,10 @@ const errorHandler = (_request, response) => {
response.end();
};

const headersHandler = (request, response) => {
response.end(JSON.stringify(request.headers));
};

test('`options.responseType` is ignored', withServer, async (t, server, got) => {
server.get('/', defaultHandler);

Expand Down Expand Up @@ -157,6 +161,20 @@ test('proxying headers works', withServer, async (t, server, got) => {
t.is(body, 'ok');
});

test('piping server request to Got proxies also headers', withServer, async (t, server, got) => {
server.get('/', headersHandler);
server.get('/proxy', (request, response) => {
request.pipe(got.stream('')).pipe(response);
});

const {foo} = await got('proxy', {
headers: {
foo: 'bar'
}
}).json();
t.is(foo, 'bar');
});

test('skips proxying headers after server has sent them already', withServer, async (t, server, got) => {
server.get('/', defaultHandler);
server.get('/proxy', (_request, response) => {
Expand Down

0 comments on commit 00e5fd5

Please sign in to comment.