Skip to content

Commit

Permalink
[#6] Support streaming proxied responses (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmorten authored Jan 28, 2024
1 parent 9d1384f commit 6eef007
Show file tree
Hide file tree
Showing 19 changed files with 281 additions and 44 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Proxy middleware for Deno Oak HTTP servers.
</p>

```ts
import { proxy } from "https://deno.land/x/oak_http_proxy@2.2.0/mod.ts";
import { proxy } from "https://deno.land/x/oak_http_proxy@2.3.0/mod.ts";
import { Application } from "https://deno.land/x/[email protected]/mod.ts";

const app = new Application();
Expand All @@ -32,13 +32,13 @@ Before importing, [download and install Deno](https://deno.land/#installation).
You can then import oak-http-proxy straight into your project:

```ts
import { proxy } from "https://deno.land/x/oak_http_proxy@2.2.0/mod.ts";
import { proxy } from "https://deno.land/x/oak_http_proxy@2.3.0/mod.ts";
```

oak-http-proxy is also available on [nest.land](https://nest.land/package/oak-http-proxy), a package registry for Deno on the Blockchain.

```ts
import { proxy } from "https://x.nest.land/oak-http-proxy@2.2.0/mod.ts";
import { proxy } from "https://x.nest.land/oak-http-proxy@2.3.0/mod.ts";
```

## Docs
Expand Down Expand Up @@ -67,6 +67,15 @@ router.get(

Note: Unmatched path segments of the incoming request url _are not_ transferred to the outbound proxy URL. For dynamic proxy urls use the function form.

### Streaming

Proxy requests and user responses are piped/streamed/chunked by default.

If you define a response modifier (`srcResDecorator`, `srcResHeaderDecorator`),
or need to inspect the response before continuing (`filterRes`), streaming is
disabled, and the request and response are buffered. This can cause performance
issues with large payloads.

### Proxy Options

You can also provide several options which allow you to filter, customize and decorate proxied requests and responses.
Expand Down
22 changes: 11 additions & 11 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export { STATUS_TEXT } from "https://deno.land/[email protected]/http/status.ts";
export { createState } from "https://deno.land/x/opineHttpProxy@3.1.0/src/createState.ts";
export { createState } from "https://deno.land/x/opineHttpProxy@3.2.0/src/createState.ts";
export type {
ProxyState,
ProxyUrlFunction,
} from "https://deno.land/x/opineHttpProxy@3.1.0/src/createState.ts";
export type { ProxyOptions } from "https://deno.land/x/opineHttpProxy@3.1.0/src/resolveOptions.ts";
export { isUnset } from "https://deno.land/x/opineHttpProxy@3.1.0/src/isUnset.ts";
export { decorateProxyReqUrl } from "https://deno.land/x/opineHttpProxy@3.1.0/src/steps/decorateProxyReqUrl.ts";
export { decorateProxyReqInit } from "https://deno.land/x/opineHttpProxy@3.1.0/src/steps/decorateProxyReqInit.ts";
export { prepareProxyReq } from "https://deno.land/x/opineHttpProxy@3.1.0/src/steps/prepareProxyReq.ts";
export { sendProxyReq } from "https://deno.land/x/opineHttpProxy@3.1.0/src/steps/sendProxyReq.ts";
export { filterProxyRes } from "https://deno.land/x/opineHttpProxy@3.1.0/src/steps/filterProxyRes.ts";
export { decorateSrcResHeaders } from "https://deno.land/x/opineHttpProxy@3.1.0/src/steps/decorateSrcResHeaders.ts";
export { decorateSrcRes } from "https://deno.land/x/opineHttpProxy@3.1.0/src/steps/decorateSrcRes.ts";
} from "https://deno.land/x/opineHttpProxy@3.2.0/src/createState.ts";
export type { ProxyOptions } from "https://deno.land/x/opineHttpProxy@3.2.0/src/resolveOptions.ts";
export { isUnset } from "https://deno.land/x/opineHttpProxy@3.2.0/src/isUnset.ts";
export { decorateProxyReqUrl } from "https://deno.land/x/opineHttpProxy@3.2.0/src/steps/decorateProxyReqUrl.ts";
export { decorateProxyReqInit } from "https://deno.land/x/opineHttpProxy@3.2.0/src/steps/decorateProxyReqInit.ts";
export { prepareProxyReq } from "https://deno.land/x/opineHttpProxy@3.2.0/src/steps/prepareProxyReq.ts";
export { sendProxyReq } from "https://deno.land/x/opineHttpProxy@3.2.0/src/steps/sendProxyReq.ts";
export { filterProxyRes } from "https://deno.land/x/opineHttpProxy@3.2.0/src/steps/filterProxyRes.ts";
export { decorateSrcResHeaders } from "https://deno.land/x/opineHttpProxy@3.2.0/src/steps/decorateSrcResHeaders.ts";
export { decorateSrcRes } from "https://deno.land/x/opineHttpProxy@3.2.0/src/steps/decorateSrcRes.ts";
14 changes: 11 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ <h1>oak-http-proxy</h1>
<a href="https://deno-visualizer.danopia.net/dependencies-of/https/deno.land/x/oak_http_proxy/mod.ts"><img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fdeno-visualizer.danopia.net%2Fshields%2Fupdates%2Fx%2Foak_http_proxy%2Fmod.ts" alt="oak-http-proxy dependency outdatedness" /></a>
<a href="https://deno-visualizer.danopia.net/dependencies-of/https/deno.land/x/oak_http_proxy/mod.ts"><img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fdeno-visualizer.danopia.net%2Fshields%2Fcache-size%2Fx%2Foak_http_proxy%2Fmod.ts" alt="oak-http-proxy cached size" /></a>
</p>
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { proxy } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://deno.land/x/oak_http_proxy@2.2.0/mod.ts&quot;</span>;
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { proxy } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://deno.land/x/oak_http_proxy@2.3.0/mod.ts&quot;</span>;
<span class="hljs-keyword">import</span> { Application } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://deno.land/x/[email protected]/mod.ts&quot;</span>;

<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Application();
Expand All @@ -89,10 +89,10 @@ <h2>Installation</h2>
<p>This is a <a href="https://deno.land/">Deno</a> module available to import direct from this repo and via the <a href="https://deno.land/x">Deno Registry</a>.</p>
<p>Before importing, <a href="https://deno.land/#installation">download and install Deno</a>.</p>
<p>You can then import oak-http-proxy straight into your project:</p>
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { proxy } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://deno.land/x/oak_http_proxy@2.2.0/mod.ts&quot;</span>;
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { proxy } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://deno.land/x/oak_http_proxy@2.3.0/mod.ts&quot;</span>;
</code></pre>
<p>oak-http-proxy is also available on <a href="https://nest.land/package/oak-http-proxy">nest.land</a>, a package registry for Deno on the Blockchain.</p>
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { proxy } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://x.nest.land/oak-http-proxy@2.2.0/mod.ts&quot;</span>;
<pre><code class="language-ts"><span class="hljs-keyword">import</span> { proxy } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://x.nest.land/oak-http-proxy@2.3.0/mod.ts&quot;</span>;
</code></pre>
<a href="#docs" id="docs" style="color: inherit; text-decoration: none;">
<h2>Docs</h2>
Expand Down Expand Up @@ -120,6 +120,14 @@ <h3>URL</h3>
);
</code></pre>
<p>Note: Unmatched path segments of the incoming request url <em>are not</em> transferred to the outbound proxy URL. For dynamic proxy urls use the function form.</p>
<a href="#streaming" id="streaming" style="color: inherit; text-decoration: none;">
<h3>Streaming</h3>
</a>
<p>Proxy requests and user responses are piped/streamed/chunked by default.</p>
<p>If you define a response modifier (<code>srcResDecorator</code>, <code>srcResHeaderDecorator</code>),
or need to inspect the response before continuing (<code>filterRes</code>), streaming is
disabled, and the request and response are buffered. This can cause performance
issues with large payloads.</p>
<a href="#proxy-options" id="proxy-options" style="color: inherit; text-decoration: none;">
<h3>Proxy Options</h3>
</a>
Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces/_types_.proxyoptions.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ <h3><span class="tsd-flag ts-flagOptional">Optional</span> req<wbr>Body<wbr>Limi
<div class="tsd-signature tsd-kind-icon">req<wbr>Body<wbr>Limit<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">number</span></div>
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/types.ts#L9">types.ts:9</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/types.ts#L9">types.ts:9</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/_proxy_.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ <h3>proxy</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/proxy.ts#L42">proxy.ts:42</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/proxy.ts#L42">proxy.ts:42</a></li>
</ul>
</aside>
<div class="tsd-comment tsd-typography">
Expand Down
8 changes: 4 additions & 4 deletions docs/modules/_requestoptions_.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ <h3>create<wbr>Request<wbr>Init</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/requestOptions.ts#L60">requestOptions.ts:60</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/requestOptions.ts#L60">requestOptions.ts:60</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -118,7 +118,7 @@ <h3>extend<wbr>Headers</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/requestOptions.ts#L26">requestOptions.ts:26</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/requestOptions.ts#L26">requestOptions.ts:26</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down Expand Up @@ -147,7 +147,7 @@ <h3>parse<wbr>Url</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/requestOptions.ts#L4">requestOptions.ts:4</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/requestOptions.ts#L4">requestOptions.ts:4</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -173,7 +173,7 @@ <h3>req<wbr>Headers</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/requestOptions.ts#L46">requestOptions.ts:46</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/requestOptions.ts#L46">requestOptions.ts:46</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/_steps_buildproxyreqinit_.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ <h3>build<wbr>Proxy<wbr>Req<wbr>Init</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/steps/buildProxyReqInit.ts#L4">steps/buildProxyReqInit.ts:4</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/steps/buildProxyReqInit.ts#L4">steps/buildProxyReqInit.ts:4</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/_steps_buildproxyurl_.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ <h3>build<wbr>Proxy<wbr>Url</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/steps/buildProxyUrl.ts#L4">steps/buildProxyUrl.ts:4</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/steps/buildProxyUrl.ts#L4">steps/buildProxyUrl.ts:4</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/_steps_copyproxyresheaderstouserres_.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ <h3>copy<wbr>Proxy<wbr>Res<wbr>Headers<wbr>ToUser<wbr>Res</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/steps/copyProxyResHeadersToUserRes.ts#L3">steps/copyProxyResHeadersToUserRes.ts:3</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/steps/copyProxyResHeadersToUserRes.ts#L3">steps/copyProxyResHeadersToUserRes.ts:3</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/_steps_filtersrcreq_.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> default<wbr>Filter</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/steps/filterSrcReq.ts#L3">steps/filterSrcReq.ts:3</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/steps/filterSrcReq.ts#L3">steps/filterSrcReq.ts:3</a></li>
</ul>
</aside>
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">boolean</span></h4>
Expand All @@ -107,7 +107,7 @@ <h3>filter<wbr>Src<wbr>Req</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/steps/filterSrcReq.ts#L5">steps/filterSrcReq.ts:5</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/steps/filterSrcReq.ts#L5">steps/filterSrcReq.ts:5</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/_steps_handleproxyerrors_.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ <h3>connection<wbr>Reset<wbr>Handler</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/steps/handleProxyErrors.ts#L3">steps/handleProxyErrors.ts:3</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/steps/handleProxyErrors.ts#L3">steps/handleProxyErrors.ts:3</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -113,7 +113,7 @@ <h3>handle<wbr>Proxy<wbr>Errors</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/steps/handleProxyErrors.ts#L12">steps/handleProxyErrors.ts:12</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/steps/handleProxyErrors.ts#L12">steps/handleProxyErrors.ts:12</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/_steps_sendsrcres_.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ <h3><span class="tsd-flag ts-flagConst">Const</span> is<wbr>Null<wbr>Body<wbr>St
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/steps/sendSrcRes.ts#L3">steps/sendSrcRes.ts:3</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/steps/sendSrcRes.ts#L3">steps/sendSrcRes.ts:3</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand All @@ -113,7 +113,7 @@ <h3>send<wbr>Src<wbr>Res</h3>
<li class="tsd-description">
<aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cmorten/oak-http-proxy/blob/b4446da/src/steps/sendSrcRes.ts#L6">steps/sendSrcRes.ts:6</a></li>
<li>Defined in <a href="https://github.com/asos-craigmorten/oak-http-proxy/blob/e494f90/src/steps/sendSrcRes.ts#L6">steps/sendSrcRes.ts:6</a></li>
</ul>
</aside>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down
2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "oak-http-proxy",
"description": "Proxy middleware for Deno Oak HTTP servers.",
"version": "2.2.0",
"version": "2.3.0",
"repository": "https://github.com/cmorten/oak-http-proxy",
"stable": true,
"files": [
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ deno run --allow-net --allow-read ./examples/proxy/index.ts
if have the repo cloned locally _OR_

```bash
deno run --allow-net --allow-read https://deno.land/x/oak_http_proxy@2.2.0/examples/basic/index.ts
deno run --allow-net --allow-read https://deno.land/x/oak_http_proxy@2.3.0/examples/basic/index.ts
```

if you don't!
11 changes: 5 additions & 6 deletions examples/basic/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/**
* Run this example using:
*
*
* deno run --allow-net ./examples/basic/index.ts
*
*
* if have the repo cloned locally OR
*
* deno run --allow-net https://deno.land/x/oak_http_proxy@2.2.0/examples/basic/index.ts
*
*
* deno run --allow-net https://deno.land/x/oak_http_proxy@2.3.0/examples/basic/index.ts
*
* if you don't!
*
*/

import { proxy } from "../../mod.ts";
Expand Down
4 changes: 3 additions & 1 deletion src/steps/sendSrcRes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const isNullBodyStatus = (status: number) =>
status === 101 || status === 204 || status === 205 || status === 304;

export function sendSrcRes(state: ProxyState) {
if (!isNullBodyStatus(state.src.res.status)) {
if (state.options.stream) {
state.src.res.body = state.proxy.res?.body;
} else if (!isNullBodyStatus(state.src.res.status)) {
state.src.res.body = state.proxy.resData;
}

Expand Down
Loading

0 comments on commit 6eef007

Please sign in to comment.