Skip to content

Commit

Permalink
fix: duplex only has a getter (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenerme committed Feb 27, 2024
1 parent 826b77b commit 6d5f527
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export class Request extends GlobalRequest {
}
if (options?.body instanceof ReadableStream) {
// node 18 fetch needs half duplex mode when request body is stream
;(options as any).duplex = 'half'
// 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'
}
super(input, options)
}
Expand Down
16 changes: 16 additions & 0 deletions test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,21 @@ describe('Request', () => {
expect(req2).toBeInstanceOf(GlobalRequest)
expect(req2.text()).resolves.toBe('bar')
})

it('should skip to set `duplex: "half"` if init option is a Request object', async () => {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode('bar'))
controller.close()
},
})
const req = new Request('http://localhost', {
method: 'POST',
body: stream,
})
const req2 = new Request('http://localhost/subapp', req)
expect(req2).toBeInstanceOf(GlobalRequest)
expect(req2.text()).resolves.toBe('bar')
})
})
})

0 comments on commit 6d5f527

Please sign in to comment.