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
This commit is contained in:
parent 088842ac38
commit 242ad9ce09
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

@ -35,17 +35,23 @@ export function isFicmasHidden(story: IStory): boolean {
);
}
export function stringifyStream(stream: GridFSBucketReadStream): Promise<string> {
export function bufferizeStream(stream: GridFSBucketReadStream): Promise<Buffer> {
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<string> {
let str = await bufferizeStream(stream);
return str.toString("utf-8");
}
export function norm(text: string): string {
return text
.replace(/\n/g, "<br>")