| 263 | * @since 4.0.0 |
| 264 | */ |
| 265 | export const readerString = ( |
| 266 | self: Socket, |
| 267 | encoding?: string | undefined |
| 268 | ): Effect.Effect< |
| 269 | Effect.Effect<NonEmptyReadonlyArray<string>, SocketError>, |
| 270 | SocketError, |
| 271 | Scope.Scope |
| 272 | > => |
| 273 | Effect.map(self.reader, ({ pull }) => { |
| 274 | const decoder = new TextDecoder(encoding) |
| 275 | const streamOptions = { stream: true } |
| 276 | let needsFlush = false |
| 277 | return Effect.map(pull, (chunk) => { |
| 278 | const out = new Array<string>(chunk.length) |
| 279 | for (let i = 0; i < chunk.length; i++) { |
| 280 | const item = chunk[i] |
| 281 | if (typeof item === "string") { |
| 282 | out[i] = needsFlush ? decoder.decode() + item : item |
| 283 | needsFlush = false |
| 284 | } else { |
| 285 | out[i] = decoder.decode(item, streamOptions) |
| 286 | needsFlush = true |
| 287 | } |
| 288 | } |
| 289 | return out as unknown as NonEmptyReadonlyArray<string> |
| 290 | }).pipe( |
| 291 | Effect.catch((error) => { |
| 292 | if (needsFlush && error.reason._tag === "SocketCloseError" && error.reason.code === 1000) { |
| 293 | needsFlush = false |
| 294 | const last = decoder.decode() |
| 295 | if (last.length > 0) return Effect.succeed([last] as const) |
| 296 | } |
| 297 | return Effect.fail(error) |
| 298 | }) |
| 299 | ) |
| 300 | }) |
| 301 | |
| 302 | const CloseEventTypeId = "~effect/socket/Socket/CloseEvent" |
| 303 | |