File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed
Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -30,8 +30,12 @@ export function readRawBody<E extends Encoding = "utf8">(
3030 ( event . node . req as any ) [ RawBodySymbol ] ||
3131 ( event . node . req as any ) . body ; /* unjs/unenv #8 */
3232 if ( _rawBody ) {
33- const promise = Promise . resolve ( _rawBody ) ;
34- return encoding ? promise . then ( ( buff ) => buff . toString ( encoding ) ) : promise ;
33+ const promise = Promise . resolve (
34+ Buffer . isBuffer ( _rawBody ) ? _rawBody : Buffer . from ( _rawBody )
35+ ) ;
36+ return encoding
37+ ? promise . then ( ( buff ) => buff . toString ( encoding ) )
38+ : ( promise as Promise < any > ) ;
3539 }
3640
3741 if ( ! Number . parseInt ( event . node . req . headers [ "content-length" ] || "" ) ) {
Original file line number Diff line number Diff line change @@ -174,7 +174,7 @@ describe("", () => {
174174 expect ( result . text ) . toBe ( "200" ) ;
175175 } ) ;
176176
177- it ( "handle raw body with buffer type (unenv)" , async ( ) => {
177+ it ( "handle readBody with buffer type (unenv)" , async ( ) => {
178178 app . use (
179179 "/" ,
180180 eventHandler ( async ( event ) => {
@@ -194,6 +194,23 @@ describe("", () => {
194194 expect ( result . text ) . toBe ( "200" ) ;
195195 } ) ;
196196
197+ it ( "handle readRawBody with array buffer type (unenv)" , async ( ) => {
198+ app . use (
199+ "/" ,
200+ eventHandler ( async ( event ) => {
201+ // Emulate unenv
202+ // @ts -ignore
203+ event . node . req . body = new Uint8Array ( [ 1 , 2 , 3 ] ) ;
204+ const body = await readRawBody ( event , false ) ;
205+ expect ( body ) . toBeInstanceOf ( Buffer ) ;
206+ expect ( body ) . toMatchObject ( Buffer . from ( [ 1 , 2 , 3 ] ) ) ;
207+ return "200" ;
208+ } )
209+ ) ;
210+ const result = await request . post ( "/api/test" ) . send ( ) ;
211+ expect ( result . text ) . toBe ( "200" ) ;
212+ } ) ;
213+
197214 it ( "parses multipart form data" , async ( ) => {
198215 app . use (
199216 "/" ,
You can’t perform that action at this time.
0 commit comments