| 591 | } |
| 592 | |
| 593 | const toChannelWithReader = <A extends Uint8Array | string, IE>( |
| 594 | self: Socket, |
| 595 | reader: Effect.Effect< |
| 596 | Effect.Effect<NonEmptyReadonlyArray<A>, SocketError>, |
| 597 | SocketError, |
| 598 | Scope.Scope |
| 599 | > |
| 600 | ): Channel.Channel< |
| 601 | NonEmptyReadonlyArray<A>, |
| 602 | SocketError | IE, |
| 603 | void, |
| 604 | NonEmptyReadonlyArray<Uint8Array | string | CloseEvent>, |
| 605 | IE |
| 606 | > => |
| 607 | Channel.fromTransform(Effect.fnUntraced(function*(upstream, scope) { |
| 608 | const readScope = yield* Scope.fork(scope) |
| 609 | const pull = yield* Scope.provide(reader, readScope) |
| 610 | const writeScope = yield* Scope.fork(scope) |
| 611 | const writer = yield* Scope.provide(self.writer, writeScope) |
| 612 | |
| 613 | let writeFailure: Cause.Cause<SocketError | IE> | undefined |
| 614 | |
| 615 | yield* upstream.pipe( |
| 616 | Effect.flatMap((chunk) => writeChunk(writer, chunk)), |
| 617 | Effect.forever({ disableYield: true }), |
| 618 | Effect.catchCauseFilter( |
| 619 | Pull.filterNoDone, |
| 620 | (cause) => |
| 621 | Effect.suspend(() => { |
| 622 | writeFailure = cause as Cause.Cause<SocketError | IE> |
| 623 | // closing the read scope fails a suspended pull, which is the |
| 624 | // reader contract, so the read side needs no per-pull failure race |
| 625 | return Scope.close(readScope, Exit.void) |
| 626 | }) |
| 627 | ), |
| 628 | Effect.ensuring(Scope.close(writeScope, Exit.void)), |
| 629 | Effect.forkIn(scope) |
| 630 | ) |
| 631 | |
| 632 | // @effect-diagnostics-next-line returnEffectInGen:off |
| 633 | return Effect.catchCause( |
| 634 | Effect.suspend( |
| 635 | (): Pull.Pull<NonEmptyReadonlyArray<A>, SocketError | IE> => |
| 636 | writeFailure !== undefined ? Effect.failCause(writeFailure) : pull |
| 637 | ), |
| 638 | (cause) => |
| 639 | Effect.failCause( |
| 640 | (writeFailure ?? cause) as Cause.Cause<SocketError | IE | Cause.Done<void>> |
| 641 | ) |
| 642 | ) |
| 643 | })) |
| 644 | |
| 645 | /** |
| 646 | * Converts a `Socket` into a bidirectional binary `Channel`, encoding |