Commit 19d133d 1 parent 8a92bcd commit 19d133d Copy full SHA for 19d133d
File tree 2 files changed +24
-3
lines changed
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">(
30
30
( event . node . req as any ) [ RawBodySymbol ] ||
31
31
( event . node . req as any ) . body ; /* unjs/unenv #8 */
32
32
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 )
Has conversations. Original line has conversations.
35
+ ) ;
36
+ return encoding
37
+ ? promise . then ( ( buff ) => buff . toString ( encoding ) )
38
+ : ( promise as Promise < any > ) ;
35
39
}
36
40
37
41
if ( ! Number . parseInt ( event . node . req . headers [ "content-length" ] || "" ) ) {
Original file line number Diff line number Diff line change @@ -174,7 +174,7 @@ describe("", () => {
174
174
expect ( result . text ) . toBe ( "200" ) ;
175
175
} ) ;
176
176
177
- it ( "handle raw body with buffer type (unenv)" , async ( ) => {
177
+ it ( "handle readBody with buffer type (unenv)" , async ( ) => {
178
178
app . use (
179
179
"/" ,
180
180
eventHandler ( async ( event ) => {
@@ -194,6 +194,23 @@ describe("", () => {
194
194
expect ( result . text ) . toBe ( "200" ) ;
195
195
} ) ;
196
196
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
+
197
214
it ( "parses multipart form data" , async ( ) => {
198
215
app . use (
199
216
"/" ,
You can’t perform that action at this time.