Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[#10] Store and pass the proxy response data as an Uint8Array instead of always decoding to a string #11

Merged
merged 4 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# ChangeLog

## [3.0.0] - 21-11-2021

- [#10] Store and pass the proxy response data as an `Uint8Array` instead of
always decoding to a string (#11)

This impacts:

- [`srcResDecorator`](https://github.com/asos-craigmorten/opine-http-proxy/tree/main#srcresdecoratorreq-res-proxyres-proxyresdata-supports-promise)
- [`filterRes`](https://github.com/asos-craigmorten/opine-http-proxy/tree/main#filterresproxyres-proxyresdata-supports-promise-form)

Where the `proxyResData` argument will be of type `Uint8Array|null` and not
`string|null`. If you require the value to be a string, you will need to decode
it yourself using, e.g.

```ts
new TextDecoder().decode(proxyResData);
```

## [2.9.2] - 21-11-2021

- feat: update to Deno `1.16.2`, std `0.115.1`
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-egg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: denolib/setup-deno@v2
with:
deno-version: 1.16.2
- run: deno install -A -f --unstable -n eggs https://x.nest.land/eggs@0.3.8/eggs.ts
- run: deno install -A -f --unstable -n eggs https://x.nest.land/eggs@0.3.10/eggs.ts
- run: |
export PATH="/home/runner/.deno/bin:$PATH"
eggs link ${NEST_LAND_KEY}
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Proxy middleware for Deno Opine HTTP servers.
</p>

```ts
import { proxy } from "https://deno.land/x/opineHttpProxy@2.9.2/mod.ts";
import { proxy } from "https://deno.land/x/opineHttpProxy@3.0.0/mod.ts";
import { opine } from "https://deno.land/x/opine@1.9.1/mod.ts";

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

```ts
import { proxy } from "https://deno.land/x/opineHttpProxy@2.9.2/mod.ts";
import { proxy } from "https://deno.land/x/opineHttpProxy@3.0.0/mod.ts";
```

## Docs
Expand Down Expand Up @@ -120,9 +120,9 @@ Decorate the inbound response object from the proxied request.
```ts
app.use(
"/proxy",
proxy("www.google.com", {
proxy("www.example.com", {
srcResDecorator: (req, res, proxyRes, proxyResData) => {
data = JSON.parse(proxyResData.toString("utf8"));
data = JSON.parse(new TextDecoder().decode(proxyResData));
data.newProperty = "exciting data";

return JSON.stringify(data);
Expand Down
4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ <h1>opine-http-proxy</h1>
<a href="https://deno-visualizer.danopia.net/dependencies-of/https/deno.land/x/opineHttpProxy/mod.ts"><img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fdeno-visualizer.danopia.net%2Fshields%2Fupdates%2Fx%2FopineHttpProxy%2Fmod.ts" alt="opine-http-proxy dependency outdatedness" /></a>
<a href="https://deno-visualizer.danopia.net/dependencies-of/https/deno.land/x/opineHttpProxy/mod.ts"><img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fdeno-visualizer.danopia.net%2Fshields%2Fcache-size%2Fx%2FopineHttpProxy%2Fmod.ts" alt="opine-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/opineHttpProxy@2.9.2/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/opineHttpProxy@3.0.0/mod.ts&quot;</span>;
<span class="hljs-keyword">import</span> { opine } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;https://deno.land/x/opine@1.9.1/mod.ts&quot;</span>;

<span class="hljs-keyword">const</span> app = opine();
Expand All @@ -98,7 +98,7 @@ <h2>Installation</h2>
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 opine-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/opineHttpProxy@2.9.2/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/opineHttpProxy@3.0.0/mod.ts&quot;</span>;
</code></pre>
<a href="#docs" id="docs" style="color: inherit; text-decoration: none;">
<h2>Docs</h2>
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": "opine-http-proxy",
"description": "Proxy middleware for Deno Opine HTTP servers.",
"version": "2.9.2",
"version": "3.0.0",
"repository": "https://github.com/asos-craigmorten/opine-http-proxy",
"stable": true,
"files": [
Expand Down
2 changes: 1 addition & 1 deletion src/requestOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export async function createRequestInit(

const encoder = new TextEncoder();

export function asBuffer(body: any) {
export function asBuffer(body: unknown) {
if (typeof body === "object" && !(body instanceof Uint8Array)) {
return encoder.encode(JSON.stringify(body));
} else if (typeof body === "string") {
Expand Down
4 changes: 1 addition & 3 deletions src/steps/sendProxyReq.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// deno-lint-ignore-file no-explicit-any
import type { ProxyState } from "../createState.ts";

const decoder = new TextDecoder();

export class TimeoutError extends Error {
code = "ECONTIMEDOUT";
name = "TimeoutError";
Expand Down Expand Up @@ -55,7 +53,7 @@ export function sendProxyReq(state: ProxyState) {
const bufferedResponse = await res.arrayBuffer();
state.proxy.resData = bufferedResponse === null
? null
: decoder.decode(bufferedResponse);
: new Uint8Array(bufferedResponse);

return state;
});
Expand Down
2 changes: 1 addition & 1 deletion version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Version of opine-http-proxy.
*/
export const VERSION = "2.9.2";
export const VERSION = "3.0.0";

/**
* Supported versions of Deno.
Expand Down