|
| 1 | +// Copyright (c) 2026 Cloudflare, Inc. |
| 2 | +// Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | +// https://opensource.org/licenses/Apache-2.0 |
| 4 | + |
| 5 | +// How a pipe observes its AbortSignal: only a real abort stops it (the |
| 6 | +// spec's abort algorithm), not the 'abort' event, and nothing a user's |
| 7 | +// 'abort' listener does can prevent it. |
| 8 | + |
| 9 | +import { strictEqual, deepStrictEqual } from 'node:assert'; |
| 10 | + |
| 11 | +function recordingPipe(signal) { |
| 12 | + let rc; |
| 13 | + const rs = new ReadableStream({ |
| 14 | + start(c) { |
| 15 | + rc = c; |
| 16 | + }, |
| 17 | + }); |
| 18 | + const written = []; |
| 19 | + const ws = new WritableStream({ |
| 20 | + write(chunk) { |
| 21 | + written.push(chunk); |
| 22 | + }, |
| 23 | + }); |
| 24 | + const pipe = rs.pipeTo(ws, { signal }); |
| 25 | + return { rs, ws, rc, written, pipe }; |
| 26 | +} |
| 27 | + |
| 28 | +// A synthetic 'abort' event on a signal that is not aborted leaves the |
| 29 | +// pipe running. |
| 30 | +export const syntheticAbortEventIgnored = { |
| 31 | + async test() { |
| 32 | + const ac = new AbortController(); |
| 33 | + const { rs, ws, rc, written, pipe } = recordingPipe(ac.signal); |
| 34 | + await scheduler.wait(0); |
| 35 | + ac.signal.dispatchEvent(new Event('abort')); |
| 36 | + rc.enqueue('a'); |
| 37 | + rc.close(); |
| 38 | + await pipe; |
| 39 | + deepStrictEqual(written, ['a']); |
| 40 | + strictEqual(rs.locked, false); |
| 41 | + strictEqual(ws.locked, false); |
| 42 | + }, |
| 43 | +}; |
| 44 | + |
| 45 | +// A listener registered before the pipe that stops immediate propagation |
| 46 | +// does not keep the abort from the pipe. |
| 47 | +export const stopImmediatePropagationDoesNotBlockAbort = { |
| 48 | + async test() { |
| 49 | + const ac = new AbortController(); |
| 50 | + const reason = new Error('boom'); |
| 51 | + ac.signal.addEventListener('abort', (e) => e.stopImmediatePropagation()); |
| 52 | + const { rs, ws, rc, pipe } = recordingPipe(ac.signal); |
| 53 | + await scheduler.wait(0); |
| 54 | + ac.abort(reason); |
| 55 | + // The C++ pipe notices the abort on its next step. |
| 56 | + rc.enqueue('a'); |
| 57 | + const outcome = await Promise.race([ |
| 58 | + pipe.then( |
| 59 | + () => 'fulfilled', |
| 60 | + (e) => e |
| 61 | + ), |
| 62 | + scheduler.wait(1000).then(() => 'pending'), |
| 63 | + ]); |
| 64 | + strictEqual(outcome, reason); |
| 65 | + strictEqual(rs.locked, false); |
| 66 | + strictEqual(ws.locked, false); |
| 67 | + }, |
| 68 | +}; |
| 69 | + |
| 70 | +// Aborting after the pipe has settled has no effect on either stream. |
| 71 | +export const abortAfterPipeSettled = { |
| 72 | + async test() { |
| 73 | + const ac = new AbortController(); |
| 74 | + const { rs, ws, rc, written, pipe } = recordingPipe(ac.signal); |
| 75 | + rc.enqueue('a'); |
| 76 | + rc.close(); |
| 77 | + await pipe; |
| 78 | + ac.abort(new Error('late')); |
| 79 | + await scheduler.wait(0); |
| 80 | + deepStrictEqual(written, ['a']); |
| 81 | + const writer = ws.getWriter(); |
| 82 | + await writer.closed; |
| 83 | + strictEqual(rs.locked, false); |
| 84 | + }, |
| 85 | +}; |
0 commit comments