-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
43 additions
and
27 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 |
---|---|---|
@@ -1,29 +1,24 @@ | ||
import { type Request, type Response, type NextFunction } from "express" | ||
import { PassThrough } from "stream" | ||
import Responses from "../responses" | ||
|
||
export default function body(req: Request, _: Response, next: NextFunction): void { | ||
const passThrough = new PassThrough() | ||
let size = 0 | ||
|
||
req.bodyStream = passThrough | ||
|
||
req.on("data", chunk => { | ||
if (chunk instanceof Buffer) { | ||
size += chunk.byteLength | ||
export default function body(req: Request, res: Response, next: NextFunction): void { | ||
if (!["POST", "PUT"].includes(req.method)) { | ||
next() | ||
|
||
passThrough.write(chunk) | ||
} | ||
}) | ||
return | ||
} | ||
|
||
req.on("end", () => { | ||
req.bodySize = size | ||
req.once("readable", () => { | ||
try { | ||
const chunk = req.read(1) | ||
|
||
passThrough.end() | ||
req.firstBodyChunk = chunk instanceof Buffer ? chunk : null | ||
|
||
next() | ||
}) | ||
req.resume() | ||
|
||
req.on("error", err => { | ||
passThrough.emit("error", err) | ||
next() | ||
} catch { | ||
Responses.internalError(res).catch(() => {}) | ||
} | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import { type PassThrough } from "stream" | ||
|
||
declare global { | ||
namespace Express { | ||
interface Request { | ||
username?: string | ||
bodyStream?: PassThrough | ||
bodySize?: number | ||
firstBodyChunk?: Buffer | null | ||
} | ||
} | ||
} | ||
|
||
export {} |