(
open: Effect.Effect<Duplex, Socket.SocketError, RO>,
options?: {
readonly openTimeout?: Duration.Input | undefined
/**
* Maximum chunks per pull. Defaults to 4096 unless a positive safe integer or Infinity is supplied.
* Use Infinity for unbounded batching.
*/
readonly chunkSize?: number | undefined
readonly tlsServer?: boolean | undefined
/** Set to true for a single connection that cannot be reacquired. Defaults to false. */
readonly disableReconnects?: boolean | undefined
}
)
| 148 | * @since 4.0.0 |
| 149 | */ |
| 150 | export const fromDuplex = <RO>( |
| 151 | open: Effect.Effect<Duplex, Socket.SocketError, RO>, |
| 152 | options?: { |
| 153 | readonly openTimeout?: Duration.Input | undefined |
| 154 | /** |
| 155 | * Maximum chunks per pull. Defaults to 4096 unless a positive safe integer or Infinity is supplied. |
| 156 | * Use Infinity for unbounded batching. |
| 157 | */ |
| 158 | readonly chunkSize?: number | undefined |
| 159 | readonly tlsServer?: boolean | undefined |
| 160 | /** Set to true for a single connection that cannot be reacquired. Defaults to false. */ |
| 161 | readonly disableReconnects?: boolean | undefined |
| 162 | } |
| 163 | ): Effect.Effect<Socket.Socket, never, Exclude<RO, Scope.Scope>> => |
| 164 | Effect.withFiber<Socket.Socket, never, Exclude<RO, Scope.Scope>>((fiber) => { |
| 165 | let currentSocket: Duplex | undefined |
| 166 | let terminalError: Socket.SocketError | undefined |
| 167 | const latch = Latch.makeUnsafe(false) |
| 168 | const disableReconnects = options?.disableReconnects === true |
| 169 | const terminate = disableReconnects |
| 170 | ? (error: Socket.SocketError) => { |
| 171 | terminalError ??= error |
| 172 | latch.openUnsafe() |
| 173 | } |
| 174 | : () => latch.closeUnsafe() |
| 175 | const openServices = fiber.context as Context.Context<RO> |
| 176 | const isServer = options?.tlsServer === true |
| 177 | const requestedChunkSize = options?.chunkSize ?? 4096 |
| 178 | const chunkSize = requestedChunkSize === Infinity || |
| 179 | (Number.isSafeInteger(requestedChunkSize) && requestedChunkSize > 0) |
| 180 | ? requestedChunkSize |
| 181 | : 4096 |
| 182 | const secureEvent = isServer ? "secure" : "secureConnect" |
| 183 | |
| 184 | const connect: Socket.Socket["reader"] = Effect.gen(function*() { |
| 185 | const scope = yield* Effect.scope |
| 186 | let conn = yield* Scope.provide(open, scope).pipe( |
| 187 | options?.openTimeout !== undefined ? |
| 188 | Effect.timeoutOrElse({ |
| 189 | duration: options.openTimeout, |
| 190 | orElse: () => |
| 191 | Effect.fail( |
| 192 | new Socket.SocketError({ |
| 193 | reason: new Socket.SocketOpenError({ kind: "Timeout", cause: new Error("Connection timed out") }) |
| 194 | }) |
| 195 | ) |
| 196 | }) : |
| 197 | identity |
| 198 | ) |
| 199 | |
| 200 | type ReadResume = ( |
| 201 | effect: Effect.Effect<Arr.NonEmptyReadonlyArray<Uint8Array | string>, Socket.SocketError> |
| 202 | ) => void |
| 203 | |
| 204 | let error: Socket.SocketError | undefined |
| 205 | let waiter: ReadResume | undefined |
| 206 | // Bytes consumed by read() while the parked pull was interrupted. |
| 207 | let pending: Arr.NonEmptyReadonlyArray<Uint8Array | string> | undefined |
no test coverage detected