Skip to content

Commit d1cea56

Browse files
authored
fix isBase64encoded for external rewrites (#698)
* fix isBase64encoded for external rewrites * changeset * add e2e test * review
1 parent 00ce837 commit d1cea56

File tree

6 files changed

+31
-11
lines changed

6 files changed

+31
-11
lines changed

.changeset/little-badgers-sparkle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
Fix external rewrites for binary data

examples/pages-router/next.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const nextConfig: NextConfig = {
3838
},
3939
],
4040
},
41+
{
42+
source: "/external-on-image",
43+
destination: "https://opennext.js.org/share.png",
44+
},
4145
],
4246
redirects: async () => [
4347
{

packages/open-next/src/overrides/proxyExternalRequest/node.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ const nodeProxy: ProxyExternalRequest = {
4747
rejectUnauthorized: false,
4848
},
4949
(_res) => {
50+
const resHeaders = _res.headers;
5051
const nodeReadableStream =
51-
_res.headers["content-encoding"] === "br"
52+
resHeaders["content-encoding"] === "br"
5253
? _res.pipe(require("node:zlib").createBrotliDecompress())
53-
: _res.headers["content-encoding"] === "gzip"
54+
: resHeaders["content-encoding"] === "gzip"
5455
? _res.pipe(require("node:zlib").createGunzip())
5556
: _res;
56-
5757
const isBase64Encoded =
58-
isBinaryContentType(headers["content-type"]) ||
59-
!!headers["content-encoding"];
58+
isBinaryContentType(resHeaders["content-type"]) ||
59+
!!resHeaders["content-encoding"];
6060
const result: InternalResult = {
6161
type: "core",
62-
headers: filterHeadersForProxy(_res.headers),
62+
headers: filterHeadersForProxy(resHeaders),
6363
statusCode: _res.statusCode ?? 200,
6464
// TODO: check base64 encoding
6565
isBase64Encoded,

packages/tests-e2e/tests/appRouter/og.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import { createHash } from "node:crypto";
21
import { expect, test } from "@playwright/test";
2+
import { validateMd5 } from "../utils";
33

44
// This is the md5sums of the expected PNGs generated with `md5sum <file>`
55
const OG_MD5 = "6e5e794ac0c27598a331690f96f05d00";
66
const API_OG_MD5 = "cac95fc3e2d4d52870c0536bb18ba85b";
77

8-
function validateMd5(data: Buffer, expectedHash: string) {
9-
return createHash("md5").update(data).digest("hex") === expectedHash;
10-
}
11-
128
test("Open-graph image to be in metatags and present", async ({
139
page,
1410
request,

packages/tests-e2e/tests/pagesRouter/rewrite.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { expect, test } from "@playwright/test";
2+
import { validateMd5 } from "../utils";
3+
4+
const EXT_PNG_MD5 = "405f45cc3397b09717a13ebd6f1e027b";
25

36
test("Single Rewrite", async ({ page }) => {
47
await page.goto("/rewrite");
@@ -13,3 +16,10 @@ test("Rewrite with query", async ({ page }) => {
1316
const el = page.getByText("SSR");
1417
await expect(el).toBeVisible();
1518
});
19+
20+
test("Rewrite to external image", async ({ request }) => {
21+
const response = await request.get("/external-on-image");
22+
expect(response.status()).toBe(200);
23+
expect(response.headers()["content-type"]).toBe("image/png");
24+
expect(validateMd5(await response.body(), EXT_PNG_MD5)).toBe(true);
25+
});

packages/tests-e2e/tests/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createHash } from "node:crypto";
2+
3+
export function validateMd5(data: Buffer, expectedHash: string) {
4+
return createHash("md5").update(data).digest("hex") === expectedHash;
5+
}

0 commit comments

Comments
 (0)