form.parse callback prevents form.on('part') from running
Closed this issue · 2 comments
Issue: I get two different logs from the following examples, where it should make no difference
request-handler.ts - Example 1 (w/o callback)
import { Request, Response } from "express";
import form from './lib/form-handler';
export async function handleRequest(request: Request, res: Response) {
// ... other code is here
// parse form to get file(s) to upload to B2
form.parse(request);
}
Expected output:
{ ETag: 'd9528738...', VersionId: '4_z51d4ef22801c394...' }
(output from form-handler file)
Actual output:
{ ETag: 'd9528738...', VersionId: '4_z51d4ef22801c394...' }
(output from form-handler file)
request-handler.ts - Example 2 (w/ callback)
import { Request, Response } from "express";
import form from './lib/form-handler';
export async function handleRequest(request: Request, res: Response) {
// ... other code is here
// parse form to get file(s) to upload to B2
form.parse(request, (err, fields, files) => {
console.log("hello from callback");
});
}
Expected output:
{ ETag: 'd9528738...', VersionId: '4_z51d4ef22801c394...' }
(output from form-handler file)
hello from callback
Actual output:
hello from callback
The actual output w/ a callback excludes console logs from my form.on('part)
configuration in my form-handler.ts file.
Here is my form handler
/lib/form-handler.ts
Note: b2Client uses the aws-sdk S3() class to send data to Backblaze B2
import multiparty from 'multiparty';
import { b2Client } from './b2-client';
/** Multiparty form that handles multipart/form-data (including files from form) */
const form = new multiparty.Form();
// for each part of the file stream, send part to B2
form.on('part', (part) => {
b2Client.putObject({
Bucket: process.env.B2_BUCKET_NAME!,
Key: part.filename,
Body: part,
ContentLength: part.byteCount,
}, (err, data) => {
if(err) throw err;
console.log(data);
});
});
export default form;
I don't think adding a callback to form.parse
should prevent the form.on('part', ...)
from running. Is this a bug, or am I doing something wrong?
https://github.com/pillarjs/multiparty#multipartyform
autoFields - Enables field events and disables part events for fields. This is automatically set to true if you add a field listener.
autoFiles - Enables file events and disables part events for files. This is automatically set to true if you add a file listener.
https://github.com/pillarjs/multiparty#formparserequest-cb
If cb is provided, autoFields and autoFiles are set to true and all fields and files are collected and passed to the callback, removing the need to listen to any events on form.
Ah ok I see. Thanks.