-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multipart/form-data content type
- Loading branch information
1 parent
c60f641
commit cea8793
Showing
7 changed files
with
714 additions
and
510 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Form } from 'multiparty'; | ||
import { JsonObject, NextFunction, Request, Response } from '../@types'; | ||
import ReadableStream = NodeJS.ReadableStream; | ||
/** | ||
* @internal | ||
*/ | ||
export function bodyMultipartFormData() { | ||
return (req: Request, _res: Response, next: NextFunction) => { | ||
if (!/^multipart\/form-data/.test(req.headers['content-type'] || '')) { | ||
return next(); | ||
} | ||
|
||
(req as Request & { _body: boolean })._body = true; | ||
|
||
const form = new Form({ | ||
autoFields: false, | ||
autoFiles: false, | ||
}); | ||
|
||
req.body = {} as JsonObject; | ||
|
||
form.on('error', err => { | ||
next(err); | ||
}); | ||
|
||
// Parts are emitted when parsing the form | ||
form.on('part', async part => { | ||
const content = await streamToString(part); | ||
|
||
(req.body as JsonObject)[part.name] = | ||
part.filename === undefined | ||
? content | ||
: { | ||
content, | ||
filename: part.filename, | ||
}; | ||
}); | ||
|
||
// Close emitted after form parsed | ||
form.on('close', () => { | ||
next(); | ||
}); | ||
|
||
form.parse(req); | ||
}; | ||
} | ||
|
||
function streamToString(stream: ReadableStream): Promise<string> { | ||
const chunks: Buffer[] = []; | ||
return new Promise((resolve, reject) => { | ||
stream.on('data', (chunk: ArrayBuffer) => chunks.push(Buffer.from(chunk))); | ||
stream.on('error', (err: Error) => reject(err)); | ||
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.