busboy icon indicating copy to clipboard operation
busboy copied to clipboard

How to attach busboy file stream to request body without using intermediate buffer?

Open FlameWolf opened this issue 3 years ago • 1 comments

I have a Fastify plugin that reads form data using busboy. Here is the relevant code:

const bus = busboy({ headers: message.headers, limits: options });
bus.on("file", (fieldName: string, file: Readable, fileInfo: FileInfo) => {
	const chunks: Array<Uint8Array> = [];
	const fileObject = new File(fileInfo);
	fileObject.fieldName = fieldName;
	file.on("data", chunk => chunks.push(chunk));
	file.on("close", () => {
		fileObject.data = Readable.from(Buffer.concat(chunks));
		fileList.push(fileObject);
		formData[fieldName] = JSON.stringify(fileInfo);
	});
});

Is there a way I can skip the file.on("data", chunk => chunks.push(chunk)); and Readable.from(Buffer.concat(chunks)) parts and just put the file object in fileObject.data instead? I tried using pipe and all but the close event never fires unless I listen to the data event. This might be a stupid question, but I'm not really familiar with busboy so any help would be greatly appreciated!

FlameWolf avatar Jul 25 '22 15:07 FlameWolf

That's entirely dependent upon whether the 3rd party module you're using (if you're using one) supports file streams or not. busboy just presents a streaming interface for files so you can handle them however you can/want.

mscdex avatar Jul 25 '22 16:07 mscdex