Skip to content

Commit be94939

Browse files
committed
Server: Revert to Formidable v2 to avoid random crashes
Ref: node-formidable/formidable#958
1 parent b26a9f1 commit be94939

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

packages/server/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"bulma-prefers-dark": "0.1.0-beta.1",
3535
"compare-versions": "6.1.0",
3636
"dayjs": "1.11.10",
37-
"formidable": "3.5.1",
37+
"formidable": "2.1.2",
3838
"fs-extra": "11.2.0",
3939
"html-entities": "1.4.0",
4040
"jquery": "3.7.1",
@@ -63,7 +63,7 @@
6363
"@joplin/tools": "~2.14",
6464
"@rmp135/sql-ts": "1.18.1",
6565
"@types/bcryptjs": "2.4.6",
66-
"@types/formidable": "3.4.5",
66+
"@types/formidable": "2.0.6",
6767
"@types/fs-extra": "11.0.4",
6868
"@types/jest": "29.5.8",
6969
"@types/jest-expect-message": "1.0.4",

packages/server/src/utils/requestUtils.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { cookieGet } from './cookies';
22
import { ErrorForbidden } from './errors';
33
import { AppContext } from './types';
4-
import * as formidableFn from 'formidable';
4+
import * as formidable from 'formidable';
55
import { Fields, Files } from 'formidable';
66
import { IncomingMessage } from 'http';
77

8-
const formidable = require('formidable').default as typeof formidableFn;
9-
108
export type BodyFields = Record<string, any>;
119

1210
interface FormParseResult {
@@ -26,16 +24,17 @@ interface FormParseRequest extends IncomingMessage {
2624
body: any;
2725
}
2826

29-
// Previously Formidable would return the files and fields as key/value pairs.
30-
// With v3, the value however is always an array. This is unclear why they did
31-
// this but for example a field `[email protected]` would come out as
32-
// `email: ['[email protected]']`. Since all our code expect simple key/value
33-
// pairs, we use this function to convert back to the old style.
27+
// Previously Formidable would return the files and fields as key/value pairs. With v3, the value
28+
// however is always an array. This is unclear why they did this but for example a field
29+
// `[email protected]` would come out as `email: ['[email protected]']`. Since all our code
30+
// expect simple key/value pairs, we use this function to convert back to the old style.
3431
//
3532
// For the extra challenge, they made this change only if the content-type is
36-
// "application/x-www-form-urlencoded". Other content types such as JSON are not
37-
// modified.
38-
const convertFieldsToKeyValue = (fields: Record<string, any[]>) => {
33+
// "application/x-www-form-urlencoded". Other content types such as JSON are not modified.
34+
//
35+
// As of 2024-01-18, this may no longer be necessary since we reverted to Formidable v2, but keeping
36+
// it anyway just in case.
37+
const convertFieldsToKeyValue = (fields: Files | Fields) => {
3938
const convertedFields: Record<string, any> = {};
4039
for (const [k, v] of Object.entries(fields)) {
4140
if (Array.isArray(v)) {

renovate.json5

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@
135135
"strip-ansi",
136136
"node-emoji",
137137

138+
// Formidable v3 is broken:
139+
// https://github.com/node-formidable/formidable/issues/958
140+
"formidable",
141+
"@types/formidable",
142+
138143
// @koa/cors has undocumented breaking changes, and the package is not
139144
// well supported so we're stuck with latest v3 for now
140145
"@koa/cors",

yarn.lock

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7045,7 +7045,7 @@ __metadata:
70457045
"@koa/cors": 3.4.3
70467046
"@rmp135/sql-ts": 1.18.1
70477047
"@types/bcryptjs": 2.4.6
7048-
"@types/formidable": 3.4.5
7048+
"@types/formidable": 2.0.6
70497049
"@types/fs-extra": 11.0.4
70507050
"@types/jest": 29.5.8
70517051
"@types/jest-expect-message": 1.0.4
@@ -7062,7 +7062,7 @@ __metadata:
70627062
bulma-prefers-dark: 0.1.0-beta.1
70637063
compare-versions: 6.1.0
70647064
dayjs: 1.11.10
7065-
formidable: 3.5.1
7065+
formidable: 2.1.2
70667066
fs-extra: 11.2.0
70677067
gulp: 4.0.2
70687068
html-entities: 1.4.0
@@ -10415,12 +10415,12 @@ __metadata:
1041510415
languageName: node
1041610416
linkType: hard
1041710417

10418-
"@types/formidable@npm:3.4.5":
10419-
version: 3.4.5
10420-
resolution: "@types/formidable@npm:3.4.5"
10418+
"@types/formidable@npm:2.0.6":
10419+
version: 2.0.6
10420+
resolution: "@types/formidable@npm:2.0.6"
1042110421
dependencies:
1042210422
"@types/node": "*"
10423-
checksum: 27384c8459e44f5e167922927bd31fcd1259555062faacaa490b52f8896a0a5614918ee4ec6ec9123c1418ac127908270c1182a2441d49681b37dc34737c3646
10423+
checksum: d6be0ac12bf8dd2e4f8a022271ee6e501c7f6d7dd58d71c68497ca7da84bee1538d1a2a64a90b56dad557ddb291d48c5731206269e9ab53ed91264e68a4d1476
1042410424
languageName: node
1042510425
linkType: hard
1042610426

@@ -21857,14 +21857,15 @@ __metadata:
2185721857
languageName: node
2185821858
linkType: hard
2185921859

21860-
"formidable@npm:3.5.1":
21861-
version: 3.5.1
21862-
resolution: "formidable@npm:3.5.1"
21860+
"formidable@npm:2.1.2":
21861+
version: 2.1.2
21862+
resolution: "formidable@npm:2.1.2"
2186321863
dependencies:
2186421864
dezalgo: ^1.0.4
2186521865
hexoid: ^1.0.0
2186621866
once: ^1.4.0
21867-
checksum: 46b21496f9f985161cf7636163147b6728f9997c7e1d59433680d92619758bf6862330e6d105b5816bafcd1ab32f27ef183455991f93ef836ea731c68db62af9
21867+
qs: ^6.11.0
21868+
checksum: 81c8e5d89f5eb873e992893468f0de22c01678ca3d315db62be0560f9de1c77d4faefc9b1f4575098eb2263b3c81ba1024833a9fc3206297ddbac88a4f69b7a8
2186821869
languageName: node
2186921870
linkType: hard
2187021871

@@ -34662,7 +34663,7 @@ __metadata:
3466234663
languageName: node
3466334664
linkType: hard
3466434665

34665-
"qs@npm:^6.11.2":
34666+
"qs@npm:^6.11.0, qs@npm:^6.11.2":
3466634667
version: 6.11.2
3466734668
resolution: "qs@npm:6.11.2"
3466834669
dependencies:

0 commit comments

Comments
 (0)