(options: {
readonly scope: Scope.Scope
readonly exit?: MutableRef.MutableRef<Exit.Exit<never, E | Cause.Done> | undefined> | undefined
readonly latch?: Latch.Latch | undefined
readonly readable: Readable | NodeJS.ReadableStream
readonly onError: (error: unknown) => E
readonly chunkSize: number | undefined
readonly readSize: number | undefined
readonly closeOnDone?: boolean | undefined
})
| 449 | } |
| 450 | |
| 451 | const readableToPullUnsafe = <A, E>(options: { |
| 452 | readonly scope: Scope.Scope |
| 453 | readonly exit?: MutableRef.MutableRef<Exit.Exit<never, E | Cause.Done> | undefined> | undefined |
| 454 | readonly latch?: Latch.Latch | undefined |
| 455 | readonly readable: Readable | NodeJS.ReadableStream |
| 456 | readonly onError: (error: unknown) => E |
| 457 | readonly chunkSize: number | undefined |
| 458 | readonly readSize: number | undefined |
| 459 | readonly closeOnDone?: boolean | undefined |
| 460 | }) => { |
| 461 | const readable = options.readable as Readable |
| 462 | const chunkSize = options.chunkSize ?? 4096 |
| 463 | |
| 464 | const closeOnDone = options.closeOnDone ?? true |
| 465 | const exit = options.exit ?? MutableRef.make(undefined) |
| 466 | const latch = options.latch ?? Latch.makeUnsafe(false) |
| 467 | function onReadable() { |
| 468 | latch.openUnsafe() |
| 469 | } |
| 470 | readable.on("readable", onReadable) |
| 471 | const cleanup = finished(readable, { readable: true, writable: false }, (error) => { |
| 472 | exit.current ??= Exit.fail(error ? options.onError(error) : Cause.Done()) |
| 473 | latch.openUnsafe() |
| 474 | }) |
| 475 | |
| 476 | const pull = Effect.suspend(function loop(): Pull.Pull<Arr.NonEmptyReadonlyArray<A>, E> { |
| 477 | let item = options.readable.read(options.readSize) as A | null |
| 478 | if (item === null) { |
| 479 | if (exit.current) { |
| 480 | return exit.current |
| 481 | } |
| 482 | if (readable.readableEnded) { |
| 483 | return Effect.fail(Cause.Done()) |
| 484 | } |
| 485 | latch.closeUnsafe() |
| 486 | return Effect.flatMap(latch.await, loop) |
| 487 | } |
| 488 | const chunk = Arr.of(item as A) |
| 489 | // read() can synchronously refill the buffer, so null is not a batch bound. |
| 490 | while (chunk.length < chunkSize) { |
| 491 | item = options.readable.read(options.readSize) |
| 492 | if (item === null) break |
| 493 | chunk.push(item) |
| 494 | } |
| 495 | return Effect.succeed(chunk) |
| 496 | }) |
| 497 | |
| 498 | return Effect.as( |
| 499 | Scope.addFinalizer( |
| 500 | options.scope, |
| 501 | Effect.sync(() => { |
| 502 | readable.off("readable", onReadable) |
| 503 | if (closeOnDone) { |
| 504 | destroyReadable(readable, cleanup) |
| 505 | } else { |
| 506 | cleanup() |
| 507 | } |
| 508 | }) |
no test coverage detected