From 242ad9ce09aece5baf28a6dc1b3eba75bca617a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Sat, 30 Dec 2023 17:01:43 -0500 Subject: [PATCH] refactor(server/utils): split `stringifyStream` function into two reusable functions - bufferizeStream this concatenates stream data into a buffer - stringifyStream this invokes `bufferizeStream` and converts the return value to a string --- lib/functions.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/functions.ts b/lib/functions.ts index 50add7e..a5a72a9 100644 --- a/lib/functions.ts +++ b/lib/functions.ts @@ -35,17 +35,23 @@ export function isFicmasHidden(story: IStory): boolean { ); } -export function stringifyStream(stream: GridFSBucketReadStream): Promise { +export function bufferizeStream(stream: GridFSBucketReadStream): Promise { let chunks: Buffer[] = []; + return new Promise((res, rej) => { stream.on("data", (c) => chunks.push(Buffer.from(c))); stream.on("error", (err) => { rej(err); }); - stream.on("end", () => res(Buffer.concat(chunks).toString("utf-8"))); + stream.on("end", () => res(Buffer.concat(chunks))); }); } +export async function stringifyStream(stream: GridFSBucketReadStream): Promise { + let str = await bufferizeStream(stream); + return str.toString("utf-8"); +} + export function norm(text: string): string { return text .replace(/\n/g, "
")