Skip to content

Commit

Permalink
fix: Update Request to work with ReadableStream polyfills (#164)
Browse files Browse the repository at this point in the history
* Update Request to work with ReadableStream polyfills

Some web frameworks (like Remix) inject polyfills for ReadableStream and this change makes sure that the server will work correctly with those polyfills.

* Update response.ts to work with readablestream polyfill

* lint and format

* fixing types
  • Loading branch information
nicksrandall authored Apr 26, 2024
1 parent 8dfa6c8 commit d62cbbb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export class Request extends GlobalRequest {
if (typeof input === 'object' && getRequestCache in input) {
input = (input as any)[getRequestCache]()
}
if (options?.body instanceof ReadableStream) {
// Check if body is ReadableStream like. This makes it compatbile with ReadableStream polyfills.
if (typeof (options?.body as ReadableStream)?.getReader !== 'undefined') {
// node 18 fetch needs half duplex mode when request body is stream
// if already set, do nothing since a Request object was passed to the options or explicitly set by the user.
;(options as any).duplex ??= 'half'
Expand Down
2 changes: 1 addition & 1 deletion src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Response {
this.#init = init
}

if (typeof body === 'string' || body instanceof ReadableStream) {
if (typeof body === 'string' || typeof (body as ReadableStream)?.getReader !== 'undefined') {
let headers = (init?.headers || { 'content-type': 'text/plain; charset=UTF-8' }) as
| Record<string, string>
| Headers
Expand Down

0 comments on commit d62cbbb

Please sign in to comment.