From fd90b260f5fe2c02bb1ca1f818b9c5c5d9937a1b Mon Sep 17 00:00:00 2001 From: Dylan Irion <61515823+dylanirion@users.noreply.github.com> Date: Fri, 5 Jan 2024 17:22:28 +0200 Subject: [PATCH] Changes encoding on cache.body from utf8 to base64 (#329) * changes encoding on cache.body from utf8 to base64 * retain utf8 for json content-type * opting for less greedy base64 * use isBinaryContentType * changeset --------- Co-authored-by: Dorseuil Nicolas --- .changeset/wicked-comics-trade.md | 5 +++++ packages/open-next/src/adapters/cache.ts | 14 ++++++++++++-- packages/open-next/src/build.ts | 18 ++++++++++++++---- 3 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 .changeset/wicked-comics-trade.md diff --git a/.changeset/wicked-comics-trade.md b/.changeset/wicked-comics-trade.md new file mode 100644 index 00000000..a15538d4 --- /dev/null +++ b/.changeset/wicked-comics-trade.md @@ -0,0 +1,5 @@ +--- +"open-next": patch +--- + +Changes encoding on cache.body for binary data diff --git a/packages/open-next/src/adapters/cache.ts b/packages/open-next/src/adapters/cache.ts index 48e52d9a..347a0662 100644 --- a/packages/open-next/src/adapters/cache.ts +++ b/packages/open-next/src/adapters/cache.ts @@ -13,6 +13,7 @@ import { } from "@aws-sdk/client-s3"; import path from "path"; +import { isBinaryContentType } from "./binary.js"; import { MAX_DYNAMO_BATCH_WRITE_ITEM_COUNT } from "./constants.js"; import { debug, error, warn } from "./logger.js"; import { chunk } from "./util.js"; @@ -226,7 +227,12 @@ export default class S3Cache { lastModified: LastModified?.getTime(), value: { kind: "ROUTE", - body: Buffer.from(cacheData.body ?? Buffer.alloc(0)), + body: Buffer.from( + cacheData.body ?? Buffer.alloc(0), + isBinaryContentType(String(meta?.headers?.["content-type"])) + ? "base64" + : "utf8", + ), status: meta?.status, headers: meta?.headers, }, @@ -276,7 +282,11 @@ export default class S3Cache { "cache", JSON.stringify({ type: "route", - body: body.toString("utf8"), + body: body.toString( + isBinaryContentType(String(headers["content-type"])) + ? "base64" + : "utf8", + ), meta: { status, headers, diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts index a184e56d..079626f2 100755 --- a/packages/open-next/src/build.ts +++ b/packages/open-next/src/build.ts @@ -10,6 +10,7 @@ import { buildSync, } from "esbuild"; +import { isBinaryContentType } from "./adapters/binary.js"; import logger from "./logger.js"; import { minifyAll } from "./minimize-js.js"; import openNextPlugin from "./plugin.js"; @@ -494,17 +495,26 @@ function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) { // Generate cache file Object.entries(cacheFilesPath).forEach(([cacheFilePath, files]) => { + const cacheFileMeta = files.meta + ? JSON.parse(fs.readFileSync(files.meta, "utf8")) + : undefined; const cacheFileContent = { type: files.body ? "route" : files.json ? "page" : "app", - meta: files.meta - ? JSON.parse(fs.readFileSync(files.meta, "utf8")) - : undefined, + meta: cacheFileMeta, html: files.html ? fs.readFileSync(files.html, "utf8") : undefined, json: files.json ? JSON.parse(fs.readFileSync(files.json, "utf8")) : undefined, rsc: files.rsc ? fs.readFileSync(files.rsc, "utf8") : undefined, - body: files.body ? fs.readFileSync(files.body, "utf8") : undefined, + body: files.body + ? fs + .readFileSync(files.body) + .toString( + isBinaryContentType(cacheFileMeta.headers["content-type"]) + ? "base64" + : "utf8", + ) + : undefined, }; fs.writeFileSync(cacheFilePath, JSON.stringify(cacheFileContent)); });