Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update formidable version and adjust for compatibility #2767

Merged
merged 1 commit into from Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 8 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,7 +39,7 @@
"commander": "^12.0.0",
"dot-prop": "^6.0.1",
"etag": "^1.8.1",
"formidable": "^2.1.2",
"formidable": "^3.5.1",
"glob": "^8.1.0",
"ioredis": "^5.3.2",
"mime": "^3.0.0",
Expand Down
27 changes: 26 additions & 1 deletion src/servers/web.ts
Expand Up @@ -729,9 +729,34 @@ export class WebServer extends Server {
"There was an error processing this form.",
);
}
resolve({ fields, files });

// this is for backward compatibility formidable v3 and v2,
// because in v3 was deleted `multiples` option and mechanism
const isMultiples = Boolean(this.config?.formOptions?.multiples);
if (isMultiples) {
resolve({ fields, files });
} else {
// reimplementing firstValues values helper
// @see https://github.com/node-formidable/formidable/blob/master/src/helpers/firstValues.js
// but instead of first we are taking last values, mimicking v2 behavior
const lastValues = (val: Record<string, any>) => {
return Object.fromEntries(
Object.entries(val).map(([key, value]) => {
return [key, Array.isArray(value) ? value.at(-1) : value];
}),
);
};

resolve({
// @ts-expect-error wrong result type
fields: lastValues(fields),
// @ts-expect-error wrong result type
files: lastValues(files),
});
}
},
);
// looks like wrong types here
})) as { fields: string[]; files: string[] };

connection.rawConnection.params.body = fields;
Expand Down