|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { readableStreamToAsyncIterable } from "./shared.js"; |
| 3 | + |
| 4 | +describe("readableStreamToAsyncIterable", () => { |
| 5 | + it("yields all values from the stream", async () => { |
| 6 | + const values = [1, 2, 3, 4, 5]; |
| 7 | + const stream = new ReadableStream<number>({ |
| 8 | + start(controller) { |
| 9 | + for (const value of values) { |
| 10 | + controller.enqueue(value); |
| 11 | + } |
| 12 | + controller.close(); |
| 13 | + }, |
| 14 | + }); |
| 15 | + |
| 16 | + const result: number[] = []; |
| 17 | + for await (const value of readableStreamToAsyncIterable(stream)) { |
| 18 | + result.push(value); |
| 19 | + } |
| 20 | + |
| 21 | + expect(result).toEqual(values); |
| 22 | + }); |
| 23 | + |
| 24 | + it("cancels the stream when consumer breaks early", async () => { |
| 25 | + let cancelCalled = false; |
| 26 | + |
| 27 | + const stream = new ReadableStream<number>({ |
| 28 | + start(controller) { |
| 29 | + controller.enqueue(1); |
| 30 | + controller.enqueue(2); |
| 31 | + controller.enqueue(3); |
| 32 | + controller.enqueue(4); |
| 33 | + controller.enqueue(5); |
| 34 | + controller.close(); |
| 35 | + }, |
| 36 | + cancel() { |
| 37 | + cancelCalled = true; |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const result: number[] = []; |
| 42 | + for await (const value of readableStreamToAsyncIterable(stream)) { |
| 43 | + result.push(value); |
| 44 | + if (value === 2) { |
| 45 | + break; // Early termination |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + expect(result).toEqual([1, 2]); |
| 50 | + expect(cancelCalled).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it("cancels the stream when consumer throws an error", async () => { |
| 54 | + let cancelCalled = false; |
| 55 | + |
| 56 | + const stream = new ReadableStream<number>({ |
| 57 | + start(controller) { |
| 58 | + controller.enqueue(1); |
| 59 | + controller.enqueue(2); |
| 60 | + controller.enqueue(3); |
| 61 | + controller.close(); |
| 62 | + }, |
| 63 | + cancel() { |
| 64 | + cancelCalled = true; |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + const result: number[] = []; |
| 69 | + const testError = new Error("Test error"); |
| 70 | + |
| 71 | + await expect(async () => { |
| 72 | + for await (const value of readableStreamToAsyncIterable(stream)) { |
| 73 | + result.push(value); |
| 74 | + if (value === 2) { |
| 75 | + throw testError; |
| 76 | + } |
| 77 | + } |
| 78 | + }).rejects.toThrow(testError); |
| 79 | + |
| 80 | + expect(result).toEqual([1, 2]); |
| 81 | + expect(cancelCalled).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + it("handles stream that produces values asynchronously", async () => { |
| 85 | + const values = ["a", "b", "c"]; |
| 86 | + let index = 0; |
| 87 | + |
| 88 | + const stream = new ReadableStream<string>({ |
| 89 | + async pull(controller) { |
| 90 | + if (index < values.length) { |
| 91 | + // Simulate async data production |
| 92 | + await new Promise((resolve) => setTimeout(resolve, 1)); |
| 93 | + controller.enqueue(values[index]!); |
| 94 | + index++; |
| 95 | + } else { |
| 96 | + controller.close(); |
| 97 | + } |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + const result: string[] = []; |
| 102 | + for await (const value of readableStreamToAsyncIterable(stream)) { |
| 103 | + result.push(value); |
| 104 | + } |
| 105 | + |
| 106 | + expect(result).toEqual(values); |
| 107 | + }); |
| 108 | + |
| 109 | + it("cancels async stream when consumer breaks early", async () => { |
| 110 | + let cancelCalled = false; |
| 111 | + let producedCount = 0; |
| 112 | + |
| 113 | + const stream = new ReadableStream<number>({ |
| 114 | + async pull(controller) { |
| 115 | + // Simulate async data production |
| 116 | + await new Promise((resolve) => setTimeout(resolve, 1)); |
| 117 | + producedCount++; |
| 118 | + controller.enqueue(producedCount); |
| 119 | + // Never close - infinite stream |
| 120 | + }, |
| 121 | + cancel() { |
| 122 | + cancelCalled = true; |
| 123 | + }, |
| 124 | + }); |
| 125 | + |
| 126 | + const result: number[] = []; |
| 127 | + for await (const value of readableStreamToAsyncIterable(stream)) { |
| 128 | + result.push(value); |
| 129 | + if (value >= 3) { |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + expect(result).toEqual([1, 2, 3]); |
| 135 | + expect(cancelCalled).toBe(true); |
| 136 | + }); |
| 137 | + |
| 138 | + it("does not throw when cancelling an already-closed stream", async () => { |
| 139 | + const stream = new ReadableStream<number>({ |
| 140 | + start(controller) { |
| 141 | + controller.enqueue(1); |
| 142 | + controller.close(); |
| 143 | + }, |
| 144 | + }); |
| 145 | + |
| 146 | + // Normal iteration should complete without errors |
| 147 | + const result: number[] = []; |
| 148 | + for await (const value of readableStreamToAsyncIterable(stream)) { |
| 149 | + result.push(value); |
| 150 | + } |
| 151 | + |
| 152 | + expect(result).toEqual([1]); |
| 153 | + }); |
| 154 | + |
| 155 | + it("does not throw when cancelling an errored stream", async () => { |
| 156 | + const streamError = new Error("Stream error"); |
| 157 | + let errorIndex = 0; |
| 158 | + |
| 159 | + const stream = new ReadableStream<number>({ |
| 160 | + pull(controller) { |
| 161 | + errorIndex++; |
| 162 | + if (errorIndex <= 2) { |
| 163 | + controller.enqueue(errorIndex); |
| 164 | + } else { |
| 165 | + controller.error(streamError); |
| 166 | + } |
| 167 | + }, |
| 168 | + }); |
| 169 | + |
| 170 | + const result: number[] = []; |
| 171 | + |
| 172 | + // The stream error should propagate |
| 173 | + await expect(async () => { |
| 174 | + for await (const value of readableStreamToAsyncIterable(stream)) { |
| 175 | + result.push(value); |
| 176 | + } |
| 177 | + }).rejects.toThrow(streamError); |
| 178 | + |
| 179 | + // We should have gotten the values before the error |
| 180 | + expect(result).toEqual([1, 2]); |
| 181 | + }); |
| 182 | + |
| 183 | + it("signals upstream producer to stop via cancel", async () => { |
| 184 | + const producedValues: number[] = []; |
| 185 | + let isProducing = true; |
| 186 | + |
| 187 | + const stream = new ReadableStream<number>({ |
| 188 | + async pull(controller) { |
| 189 | + if (!isProducing) return; |
| 190 | + |
| 191 | + await new Promise((resolve) => setTimeout(resolve, 5)); |
| 192 | + const value = producedValues.length + 1; |
| 193 | + producedValues.push(value); |
| 194 | + controller.enqueue(value); |
| 195 | + }, |
| 196 | + cancel() { |
| 197 | + isProducing = false; |
| 198 | + }, |
| 199 | + }); |
| 200 | + |
| 201 | + const consumed: number[] = []; |
| 202 | + for await (const value of readableStreamToAsyncIterable(stream)) { |
| 203 | + consumed.push(value); |
| 204 | + if (value >= 2) { |
| 205 | + break; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + // Wait a bit to ensure no more values are produced |
| 210 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 211 | + |
| 212 | + expect(consumed).toEqual([1, 2]); |
| 213 | + // Producer should have stopped after cancel |
| 214 | + expect(isProducing).toBe(false); |
| 215 | + // No more values should have been produced after breaking |
| 216 | + expect(producedValues.length).toBeLessThanOrEqual(3); |
| 217 | + }); |
| 218 | +}); |
| 219 | + |
0 commit comments