|
1 | 1 | import { AsyncIteratorClass } from './iterator' |
2 | | -import { asyncIteratorToStream, streamToAsyncIteratorClass } from './stream' |
| 2 | +import { asyncIteratorToStream, asyncIteratorToUnproxiedDataStream, streamToAsyncIteratorClass } from './stream' |
3 | 3 |
|
4 | 4 | describe('streamToAsyncIteratorClass', () => { |
5 | 5 | it('should convert a ReadableStream to AsyncIteratorClass', async () => { |
@@ -187,3 +187,121 @@ it('streamToAsyncIteratorClass + asyncIteratorToStream', async () => { |
187 | 187 |
|
188 | 188 | expect(results).toEqual([1, 2, 3]) |
189 | 189 | }) |
| 190 | + |
| 191 | +describe('asyncIteratorToUnproxiedDataStream', () => { |
| 192 | + const PROXY_SYMBOL = Symbol('proxy') |
| 193 | + |
| 194 | + function proxy(value: object) { |
| 195 | + return new Proxy(value, { |
| 196 | + get(target, prop) { |
| 197 | + if (prop === PROXY_SYMBOL) { |
| 198 | + return true |
| 199 | + } |
| 200 | + |
| 201 | + return Reflect.get(target, prop) |
| 202 | + }, |
| 203 | + }) |
| 204 | + } |
| 205 | + |
| 206 | + function isProxied(value: any) { |
| 207 | + return Boolean(typeof value === 'object' && value && value[PROXY_SYMBOL]) |
| 208 | + } |
| 209 | + |
| 210 | + it('should convert an AsyncIterator to ReadableStream and unproxied data', async () => { |
| 211 | + const date = new Date() |
| 212 | + const set = new Set([date]) |
| 213 | + |
| 214 | + async function* generator() { |
| 215 | + yield 1 |
| 216 | + yield proxy({ order: 2 }) |
| 217 | + yield { order: 3 } |
| 218 | + yield [4] |
| 219 | + yield proxy([5]) |
| 220 | + yield date // support native Date |
| 221 | + yield set // support native Set |
| 222 | + } |
| 223 | + |
| 224 | + const asyncIterator = generator() |
| 225 | + const stream = asyncIteratorToUnproxiedDataStream(asyncIterator) |
| 226 | + |
| 227 | + expect(stream).toBeInstanceOf(ReadableStream) |
| 228 | + |
| 229 | + const reader = stream.getReader() |
| 230 | + const results: any[] = [] |
| 231 | + |
| 232 | + let result = await reader.read() |
| 233 | + while (!result.done) { |
| 234 | + results.push(result.value) |
| 235 | + result = await reader.read() |
| 236 | + } |
| 237 | + |
| 238 | + expect(results).toEqual([ |
| 239 | + 1, |
| 240 | + { order: 2 }, |
| 241 | + { order: 3 }, |
| 242 | + [4], |
| 243 | + [5], |
| 244 | + date, |
| 245 | + set, |
| 246 | + ]) |
| 247 | + |
| 248 | + expect(results.some(isProxied)).toBe(false) |
| 249 | + }) |
| 250 | + |
| 251 | + it('should handle empty async iterator', async () => { |
| 252 | + async function* emptyGenerator() { |
| 253 | + // Empty generator |
| 254 | + } |
| 255 | + |
| 256 | + const asyncIterator = emptyGenerator() |
| 257 | + const stream = asyncIteratorToUnproxiedDataStream(asyncIterator) |
| 258 | + |
| 259 | + const reader = stream.getReader() |
| 260 | + const result = await reader.read() |
| 261 | + |
| 262 | + expect(result.done).toBe(true) |
| 263 | + expect(result.value).toBeUndefined() |
| 264 | + }) |
| 265 | + |
| 266 | + it('should handle async iterator errors', async () => { |
| 267 | + const error = new Error('Iterator error') |
| 268 | + async function* errorGenerator() { |
| 269 | + yield 1 |
| 270 | + throw error |
| 271 | + } |
| 272 | + |
| 273 | + const asyncIterator = errorGenerator() |
| 274 | + const stream = asyncIteratorToUnproxiedDataStream(asyncIterator) |
| 275 | + |
| 276 | + const reader = stream.getReader() |
| 277 | + |
| 278 | + // First read should succeed |
| 279 | + const firstResult = await reader.read() |
| 280 | + expect(firstResult.done).toBe(false) |
| 281 | + expect(firstResult.value).toBe(1) |
| 282 | + |
| 283 | + // Second read should throw the error |
| 284 | + await expect(reader.read()).rejects.toThrow(error) |
| 285 | + }) |
| 286 | + |
| 287 | + it('should call iterator.return when stream is cancelled', async () => { |
| 288 | + let cleanupCalled = false |
| 289 | + |
| 290 | + const stream = asyncIteratorToUnproxiedDataStream(async function* () { |
| 291 | + try { |
| 292 | + yield 1 |
| 293 | + yield 2 |
| 294 | + await new Promise(resolve => setTimeout(resolve, 100)) // Simulate async operation |
| 295 | + } |
| 296 | + finally { |
| 297 | + cleanupCalled = true |
| 298 | + } |
| 299 | + }()) |
| 300 | + |
| 301 | + const reader = stream.getReader() |
| 302 | + await reader.read() |
| 303 | + await reader.cancel() |
| 304 | + |
| 305 | + expect(cleanupCalled).toBe(true) |
| 306 | + }) |
| 307 | +}) |
0 commit comments