File tree 2 files changed +38
-3
lines changed
2 files changed +38
-3
lines changed Original file line number Diff line number Diff line change @@ -74,8 +74,19 @@ const newRequestFromIncoming = (
74
74
}
75
75
76
76
if ( ! ( method === 'GET' || method === 'HEAD' ) ) {
77
- // lazy-consume request body
78
- init . body = Readable . toWeb ( incoming ) as ReadableStream < Uint8Array >
77
+ if ( 'rawBody' in incoming && incoming . rawBody instanceof Buffer ) {
78
+ // In some environments (e.g. firebase functions), the body is already consumed.
79
+ // So we need to re-read the request body from `incoming.rawBody` if available.
80
+ init . body = new ReadableStream ( {
81
+ start ( controller ) {
82
+ controller . enqueue ( incoming . rawBody )
83
+ controller . close ( )
84
+ } ,
85
+ } )
86
+ } else {
87
+ // lazy-consume request body
88
+ init . body = Readable . toWeb ( incoming ) as ReadableStream < Uint8Array >
89
+ }
79
90
}
80
91
81
92
return new Request ( url , init )
Original file line number Diff line number Diff line change 1
- import type { IncomingMessage } from 'node:http'
1
+ import { IncomingMessage } from 'node:http'
2
+ import { Socket } from 'node:net'
2
3
import {
3
4
newRequest ,
4
5
Request as LightweightRequest ,
@@ -112,6 +113,29 @@ describe('Request', () => {
112
113
} as IncomingMessage )
113
114
} ) . toThrow ( RequestError )
114
115
} )
116
+
117
+ it ( 'Should be create request body from `req.rawBody` if it exists' , async ( ) => {
118
+ const rawBody = Buffer . from ( 'foo' )
119
+ const socket = new Socket ( )
120
+ const incomingMessage = new IncomingMessage ( socket )
121
+ incomingMessage . method = 'POST'
122
+ incomingMessage . headers = {
123
+ host : 'localhost' ,
124
+ }
125
+ incomingMessage . url = '/foo.txt'
126
+ ; ( incomingMessage as IncomingMessage & { rawBody : Buffer } ) . rawBody = rawBody
127
+ incomingMessage . push ( rawBody )
128
+ incomingMessage . push ( null )
129
+
130
+ for await ( const chunk of incomingMessage ) {
131
+ // consume body
132
+ expect ( chunk ) . toBeDefined ( )
133
+ }
134
+
135
+ const req = newRequest ( incomingMessage )
136
+ const text = await req . text ( )
137
+ expect ( text ) . toBe ( 'foo' )
138
+ } )
115
139
} )
116
140
117
141
describe ( 'GlobalRequest' , ( ) => {
You can’t perform that action at this time.
0 commit comments