Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit response event before waiting for data #699

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/utils/handleRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export async function handleRequest(
// for that event are finished (e.g. async listeners awaited).
// By the end of this promise, the developer cannot affect the
// request anymore.
const requestListtenersPromise = emitAsync(options.emitter, 'request', {
const requestListenersPromise = emitAsync(options.emitter, 'request', {
requestId: options.requestId,
request: options.request,
controller: options.controller,
Expand All @@ -125,7 +125,7 @@ export async function handleRequest(
await Promise.race([
// Short-circuit the request handling promise if the request gets aborted.
requestAbortPromise,
requestListtenersPromise,
requestListenersPromise,
options.controller[kResponsePromise],
])

Expand Down
44 changes: 44 additions & 0 deletions test/modules/http/compliance/http-res-event-order.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// @vitest-environment node
import { it, expect, beforeAll, afterEach, afterAll, vi } from 'vitest'
import http from 'node:http'
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest'
import { DeferredPromise } from '@open-draft/deferred-promise'

const interceptor = new ClientRequestInterceptor()

beforeAll(() => {
interceptor.apply()
})

afterEach(() => {
interceptor.removeAllListeners()
})

afterAll(() => {
interceptor.dispose()
})

it('should fire response event before waiting for the response body', async () => {
interceptor.on('request', ({ controller }) => {
const stream = new ReadableStream({
start(controller) {
setTimeout(() => {
controller.enqueue(new TextEncoder().encode('hello world'))
controller.close()
}, 100)
},
})
controller.respondWith(new Response(stream))
})

const responseReceived = new DeferredPromise<number>()

http.get('http://localhost', (response) => {
const start = Date.now()
response.on('data', () => {
responseReceived.resolve(Date.now() - start)
})
})

expect(await responseReceived).toBeGreaterThan(100)

Check failure on line 43 in test/modules/http/compliance/http-res-event-order.test.ts

View workflow job for this annotation

GitHub Actions / build (18)

modules/http/compliance/http-res-event-order.test.ts > should fire response event before waiting for the response body

AssertionError: expected 0 to be greater than 100 ❯ modules/http/compliance/http-res-event-order.test.ts:43:34

Check failure on line 43 in test/modules/http/compliance/http-res-event-order.test.ts

View workflow job for this annotation

GitHub Actions / build (20)

modules/http/compliance/http-res-event-order.test.ts > should fire response event before waiting for the response body

AssertionError: expected 1 to be greater than 100 ❯ modules/http/compliance/http-res-event-order.test.ts:43:34
})
Loading