From 304a422ef4db88aa8efd6c8d59c5a65abdb61e1a Mon Sep 17 00:00:00 2001 From: Sydney Achinger <78113809+squid-ney@users.noreply.github.com> Date: Wed, 19 Mar 2025 09:12:31 -0400 Subject: [PATCH] Replace S3 streamToBuffer with pipe. (#28993) * Replace S3 streamToBuffer with pipe. Signed-off-by: Sydney Achinger --- .changeset/cuddly-dodos-allow.md | 5 +++++ plugins/techdocs-node/src/stages/publish/awsS3.ts | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .changeset/cuddly-dodos-allow.md diff --git a/.changeset/cuddly-dodos-allow.md b/.changeset/cuddly-dodos-allow.md new file mode 100644 index 0000000000..0901d630f1 --- /dev/null +++ b/.changeset/cuddly-dodos-allow.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-techdocs-node': patch +--- + +Update AWS file retrieval logic from storing file in buffer array to piping to res for better memory efficiency. diff --git a/plugins/techdocs-node/src/stages/publish/awsS3.ts b/plugins/techdocs-node/src/stages/publish/awsS3.ts index 395dee3688..bd8b96ee14 100644 --- a/plugins/techdocs-node/src/stages/publish/awsS3.ts +++ b/plugins/techdocs-node/src/stages/publish/awsS3.ts @@ -483,7 +483,18 @@ export class AwsS3Publish implements PublisherBase { res.setHeader(headerKey, headerValue); } - res.send(await streamToBuffer(resp.Body as Readable)); + (resp.Body as Readable) + .on('error', err => { + this.logger.warn( + `TechDocs S3 router failed to serve static files from bucket ${this.bucketName} at key ${filePath}: ${err.message}`, + ); + if (!res.headersSent) { + res.status(404).send('File Not Found'); + } else { + res.destroy(); + } + }) + .pipe(res); } catch (err) { assertError(err); this.logger.warn(