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
					
				| @ -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[] = []; | 	let chunks: Buffer[] = []; | ||||||
|  | 
 | ||||||
| 	return new Promise((res, rej) => { | 	return new Promise((res, rej) => { | ||||||
| 		stream.on("data", (c) => chunks.push(Buffer.from(c))); | 		stream.on("data", (c) => chunks.push(Buffer.from(c))); | ||||||
| 		stream.on("error", (err) => { | 		stream.on("error", (err) => { | ||||||
| 			rej(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 { | export function norm(text: string): string { | ||||||
| 	return text | 	return text | ||||||
| 		.replace(/\n/g, "<br>") | 		.replace(/\n/g, "<br>") | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user