-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
669 additions
and
524 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
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
54 changes: 54 additions & 0 deletions
54
packages/app/src/TransformRequest/MultipartFormDataTransform.ts
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,54 @@ | ||
import {randomBytes} from "crypto"; | ||
import {tmpdir} from "os"; | ||
import {join} from "path"; | ||
import {createWriteStream} from "fs"; | ||
import {Defer} from "@bunt/util"; | ||
import busboy from "busboy"; | ||
import {IRequest} from "../interfaces"; | ||
|
||
export const MultipartFormDataTransform = async <T = unknown>(request: IRequest): Promise<T> => { | ||
request.headers.assert("Content-Type", (value) => value.startsWith("multipart/form-data")); | ||
const bb = busboy({headers: request.headers.toJSON()}); | ||
const rs = await request.createReadableStream(); | ||
const defer = new Defer<unknown>(); | ||
const result: Record<string, any> = {}; | ||
const pending: Defer<void>[] = []; | ||
|
||
bb | ||
.on("file", (name, file, info) => { | ||
const {encoding, filename, mimeType} = info; | ||
const tmpname = join( | ||
tmpdir(), | ||
randomBytes(4).toString("hex"), | ||
Buffer.from(filename, "utf-8").toString("hex"), | ||
); | ||
|
||
const value = { | ||
filename, | ||
encoding, | ||
mimeType, | ||
tmpname, | ||
}; | ||
|
||
if (name.endsWith("[]")) { | ||
result[name] = [...(result[name] ?? []), value]; | ||
} else { | ||
result[name] = value; | ||
} | ||
|
||
const def = new Defer<void>(); | ||
pending.push(def); | ||
|
||
file | ||
.pipe(createWriteStream(tmpname)) | ||
.on("close", () => def.resolve()); | ||
}) | ||
.on("field", (name, value) => result[name] = value) | ||
.on("close", () => defer.resolve(result)); | ||
|
||
rs.pipe(bb); | ||
|
||
await Promise.all(pending); | ||
|
||
return defer.then((res) => res as T); | ||
}; |
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,9 +1,9 @@ | ||
import {parse, ParsedUrlQuery} from "querystring"; | ||
import {parse} from "querystring"; | ||
import {IRequest} from "../interfaces"; | ||
|
||
export const URLEncodedFormTransform = async (request: IRequest): Promise<ParsedUrlQuery> => { | ||
export const URLEncodedFormTransform = async <T = unknown>(request: IRequest): Promise<T> => { | ||
request.headers.assert("Content-Type", "application/x-www-form-urlencoded"); | ||
const buffer = await request.getBuffer(); | ||
return parse(buffer.toString("utf-8")); | ||
|
||
return parse(buffer.toString("utf-8")) as any; | ||
}; |
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,2 +1,3 @@ | ||
export * from "./URLEncodedFormTransform"; | ||
export * from "./JSONTransform"; | ||
export * from "./MultipartFormDataTransform"; |
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.