|
| 1 | +import { createNextDescribe } from 'e2e-utils' |
| 2 | +import { sleep } from './sleep' |
| 3 | +import { get } from 'http' |
| 4 | + |
| 5 | +createNextDescribe( |
| 6 | + 'streaming responses cancel inner stream after disconnect', |
| 7 | + { |
| 8 | + files: __dirname, |
| 9 | + }, |
| 10 | + ({ next }) => { |
| 11 | + type CancelState = { |
| 12 | + requestAborted: boolean |
| 13 | + streamCleanedUp: boolean |
| 14 | + i: number |
| 15 | + } |
| 16 | + |
| 17 | + function prime(url: string) { |
| 18 | + return new Promise<void>((resolve) => { |
| 19 | + url = new URL(url, next.url).href |
| 20 | + |
| 21 | + // There's a bug in node-fetch v2 where aborting the fetch will never abort |
| 22 | + // the connection, because the body is a transformed stream that doesn't |
| 23 | + // close the connection stream. |
| 24 | + // https://github.com/node-fetch/node-fetch/pull/670 |
| 25 | + const req = get(url, async (res) => { |
| 26 | + while (true) { |
| 27 | + const value = res.read(1) |
| 28 | + if (value) break |
| 29 | + await sleep(5) |
| 30 | + } |
| 31 | + |
| 32 | + res.destroy() |
| 33 | + |
| 34 | + // make sure the connection has finished |
| 35 | + await sleep(100) |
| 36 | + |
| 37 | + resolve() |
| 38 | + }) |
| 39 | + req.end() |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + // The disconnect from our prime request to the server isn't instant, and |
| 44 | + // there's no good signal on the client end for when it happens. So we just |
| 45 | + // fetch multiple times waiting for it to happen. |
| 46 | + async function getTillCancelled(url: string) { |
| 47 | + while (true) { |
| 48 | + const res = await next.fetch(url) |
| 49 | + const json = (await res.json()) as CancelState |
| 50 | + if (json.streamCleanedUp === true) { |
| 51 | + return json |
| 52 | + } |
| 53 | + |
| 54 | + await sleep(10) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + it('Midddleware cancels inner ReadableStream', async () => { |
| 59 | + await prime('/middleware') |
| 60 | + const json = await getTillCancelled('/middleware') |
| 61 | + expect(json).toMatchObject({ |
| 62 | + requestAborted: true, |
| 63 | + streamCleanedUp: true, |
| 64 | + i: (expect as any).toBeWithin(0, 5), |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + it('App Route Handler Edge cancels inner ReadableStream', async () => { |
| 69 | + await prime('/edge-route') |
| 70 | + const json = await getTillCancelled('/edge-route') |
| 71 | + expect(json).toMatchObject({ |
| 72 | + requestAborted: true, |
| 73 | + streamCleanedUp: true, |
| 74 | + i: (expect as any).toBeWithin(0, 5), |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + it('App Route Handler NodeJS cancels inner ReadableStream', async () => { |
| 79 | + await prime('/node-route') |
| 80 | + const json = await getTillCancelled('/node-route') |
| 81 | + expect(json).toMatchObject({ |
| 82 | + requestAborted: true, |
| 83 | + streamCleanedUp: true, |
| 84 | + i: (expect as any).toBeWithin(0, 5), |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + it('Pages Api Route Edge cancels inner ReadableStream', async () => { |
| 89 | + await prime('/api/edge-api') |
| 90 | + const json = await getTillCancelled('/api/edge-api') |
| 91 | + expect(json).toMatchObject({ |
| 92 | + requestAborted: true, |
| 93 | + streamCleanedUp: true, |
| 94 | + i: (expect as any).toBeWithin(0, 5), |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('Pages Api Route NodeJS cancels inner ReadableStream', async () => { |
| 99 | + await prime('/api/node-api') |
| 100 | + const json = await getTillCancelled('/api/node-api') |
| 101 | + expect(json).toMatchObject({ |
| 102 | + requestAborted: true, |
| 103 | + streamCleanedUp: true, |
| 104 | + i: (expect as any).toBeWithin(0, 5), |
| 105 | + }) |
| 106 | + }) |
| 107 | + } |
| 108 | +) |
0 commit comments