Skip to content

Commit 7f2d6db

Browse files
committed
fix: prevent responses in unrelated generators
1 parent 43b3ad0 commit 7f2d6db

2 files changed

Lines changed: 106 additions & 7 deletions

File tree

‎src/core/handlers/request-handler.ts‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export abstract class RequestHandler<
161161
MaybeAsyncResponseResolverReturnType<any>,
162162
MaybeAsyncResponseResolverReturnType<any>
163163
>
164-
private resolverIteratorResult?: Response | HttpResponse<any>
164+
private resolverIteratorResults: WeakMap<object, Response>
165165
private resolverIteratorCleanups?: Array<() => MaybePromise<void>>
166166
private options?: HandlerOptions
167167
private scheduledCleanups: Map<string, Array<() => MaybePromise<void>>>
@@ -180,6 +180,7 @@ export abstract class RequestHandler<
180180
this.resolver = args.resolver
181181
this.options = args.options
182182
this.scheduledCleanups = new Map()
183+
this.resolverIteratorResults = new WeakMap()
183184

184185
const callFrame = getCallFrame(new Error())
185186

@@ -212,7 +213,6 @@ export abstract class RequestHandler<
212213

213214
this.scheduledCleanups.clear()
214215
this.resolverIterator = undefined
215-
this.resolverIteratorResult = undefined
216216
this.resolverIteratorCleanups = undefined
217217

218218
void this.exhaustCleanups(cleanups)
@@ -508,24 +508,36 @@ export abstract class RequestHandler<
508508
// Opt-out from marking this handler as used.
509509
this.isUsed = false
510510

511-
const { done, value } = await this.resolverIterator.next()
511+
const iterator = this.resolverIterator
512+
const { done, value } = await iterator.next()
512513
const nextResponse = await value
513514

514515
if (nextResponse) {
515-
this.resolverIteratorResult = nextResponse.clone()
516+
this.resolverIteratorResults.set(iterator, nextResponse.clone())
516517
}
517518

518519
if (done) {
520+
// Clone the previously stored response so it can be read
521+
// when receiving it repeatedly from the "done" generator.
522+
const response = this.resolverIteratorResults.get(iterator)?.clone()
523+
524+
/**
525+
* @note This generator was abandoned by a reset while it was
526+
* running. Respond with its own result but leave the state of
527+
* the handler to the generator that may have replaced it.
528+
*/
529+
if (iterator !== this.resolverIterator) {
530+
return response
531+
}
532+
519533
// A one-time generator resolver stops affecting the network
520534
// only after it's been completely exhausted.
521535
this.isUsed = true
522536

523-
// Clone the previously stored response so it can be read
524-
// when receiving it repeatedly from the "done" generator.
525537
return this.complete({
526538
request: info.request,
527539
requestId: info.requestId,
528-
response: this.resolverIteratorResult?.clone(),
540+
response,
529541
})
530542
}
531543

‎src/http/http-handler.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,93 @@ describe('run', () => {
269269
await expect(run()).resolves.toBe('complete')
270270
await expect(run()).resolves.toBe('complete')
271271
})
272+
273+
test('keeps the response of the current generator when a detached one yields', async () => {
274+
const generatorStarted = Promise.withResolvers<void>()
275+
const continueGenerator = Promise.withResolvers<void>()
276+
let generatorCount = 0
277+
278+
const handler = new HttpHandler('GET', '/users', async function* () {
279+
generatorCount += 1
280+
const generatorId = generatorCount
281+
282+
if (generatorId === 1) {
283+
generatorStarted.resolve()
284+
await continueGenerator.promise
285+
}
286+
287+
yield HttpResponse.text(`pending ${generatorId}`)
288+
})
289+
290+
const run = async () => {
291+
const result = await handler.run({
292+
request: new Request(new URL('/users', location.href)),
293+
requestId: createRequestId(),
294+
})
295+
return result?.response?.text()
296+
}
297+
298+
const detachedRun = run()
299+
await generatorStarted.promise
300+
301+
// Resetting the handler detaches the generator that is still running.
302+
handler.reset()
303+
await expect(run()).resolves.toBe('pending 2')
304+
305+
continueGenerator.resolve()
306+
await expect(detachedRun).resolves.toBe('pending 1')
307+
308+
// Must respond with the latest response of the current generator.
309+
await expect(run()).resolves.toBe('pending 2')
310+
await expect(run()).resolves.toBe('pending 2')
311+
})
312+
313+
test('keeps the current generator running when a detached one completes', async () => {
314+
const generatorStarted = Promise.withResolvers<void>()
315+
const continueGenerator = Promise.withResolvers<void>()
316+
let generatorCount = 0
317+
318+
const handler = new HttpHandler(
319+
'GET',
320+
'/users',
321+
async function* () {
322+
generatorCount += 1
323+
const generatorId = generatorCount
324+
325+
if (generatorId === 1) {
326+
generatorStarted.resolve()
327+
await continueGenerator.promise
328+
return HttpResponse.text('complete 1')
329+
}
330+
331+
yield HttpResponse.text(`pending ${generatorId}`)
332+
return HttpResponse.text(`complete ${generatorId}`)
333+
},
334+
{ once: true },
335+
)
336+
337+
const run = async () => {
338+
const result = await handler.run({
339+
request: new Request(new URL('/users', location.href)),
340+
requestId: createRequestId(),
341+
})
342+
return result?.response?.text()
343+
}
344+
345+
const detachedRun = run()
346+
await generatorStarted.promise
347+
348+
// Resetting the handler detaches the generator that is still running.
349+
handler.reset()
350+
await expect(run()).resolves.toBe('pending 2')
351+
352+
continueGenerator.resolve()
353+
await expect(detachedRun).resolves.toBe('complete 1')
354+
355+
// Must not consider the handler used until the current generator completes.
356+
await expect(run()).resolves.toBe('complete 2')
357+
await expect(run()).resolves.toBeUndefined()
358+
})
272359
})
273360

274361
describe('finalize', () => {

0 commit comments

Comments
 (0)