@@ -2,6 +2,9 @@ import type { IncomingMessage } from 'http'
22import destr from 'destr'
33import type { Encoding } from '../types/node'
44
5+ const RawBodySymbol = Symbol ( 'h2RawBody' )
6+ const ParsedBodySymbol = Symbol ( 'h2RawBody' )
7+
58/**
69 * Reads body of the request and returns encoded raw string (default) or `Buffer` if encoding if falsy.
710 * @param req {IncomingMessage} An IncomingMessage object is created by
@@ -10,11 +13,11 @@ import type { Encoding } from '../types/node'
1013 *
1114 * @return {String|Buffer } Encoded raw string or raw Buffer of the body
1215 */
13- export function useBody ( req : IncomingMessage , encoding : Encoding = 'utf-8' ) : Encoding extends false ? Buffer : Promise < string > {
16+ export function useRawBody ( req : IncomingMessage , encoding : Encoding = 'utf-8' ) : Encoding extends false ? Buffer : Promise < string > {
1417 // @ts -ignore
15- if ( req . rawBody ) {
18+ if ( req [ RawBodySymbol ] ) {
1619 // @ts -ignore
17- return Promise . resolve ( encoding ? req . rawBody . toString ( encoding ) : req . rawBody )
20+ return Promise . resolve ( encoding ? req [ RawBodySymbol ] . toString ( encoding ) : req [ RawBodySymbol ] )
1821 }
1922
2023 return new Promise < string > ( ( resolve , reject ) => {
@@ -24,35 +27,33 @@ export function useBody (req: IncomingMessage, encoding: Encoding = 'utf-8'): En
2427 . on ( 'data' , ( chunk ) => { bodyData . push ( chunk ) } )
2528 . on ( 'end' , ( ) => {
2629 // @ts -ignore
27- req . rawBody = Buffer . concat ( bodyData )
30+ req [ RawBodySymbol ] = Buffer . concat ( bodyData )
2831 // @ts -ignore
29- resolve ( encoding ? req . rawBody . toString ( encoding ) : req . rawBody )
32+ resolve ( encoding ? req [ RawBodySymbol ] . toString ( encoding ) : req [ RawBodySymbol ] )
3033 } )
3134 } )
3235}
3336
3437/**
35- * Reads body of the request and returns encoded raw string or `Buffer`.
36- * This utility uses {@link https://github.com/nuxt-contrib/destr destr} to parse the body
37- *
38+ * Reads request body and try to safely parse using {@link https://github.com/nuxt-contrib/destr destr}
3839 * @param req {IncomingMessage} An IncomingMessage object is created by
3940 * <a href="https://nodejs.org/api/http.html#http_class_http_server">http.Server</a>
4041 * @param encoding {Encoding} encoding="utf-8" - The character encoding to use.
4142 *
4243 * @return {* } The Object, Array, string, number, boolean, or null value corresponding to the request JSON body
4344 */
44- export async function useBodyJSON < T > ( req : IncomingMessage ) : Promise < T > {
45+ export async function useBody < T = any > ( req : IncomingMessage ) : Promise < T > {
4546 // @ts -ignore
46- if ( req . jsonBody ) {
47+ if ( req [ ParsedBodySymbol ] ) {
4748 // @ts -ignore
48- return req . jsonBody
49+ return req [ ParsedBodySymbol ]
4950 }
5051
5152 const body = await useBody ( req )
5253 const json = destr ( body )
5354
5455 // @ts -ignore
55- req . jsonBody = json
56+ req [ ParsedBodySymbol ] = json
5657
5758 return json
5859}
0 commit comments