MCPcopy Create free account
hub / github.com/Effect-TS/effect / readerString

Function readerString

packages/effect/src/socket/Socket.ts:265–300  ·  view source on GitHub ↗
(
  self: Socket,
  encoding?: string | undefined
)

Source from the content-addressed store, hash-verified

263 * @since 4.0.0
264 */
265export 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
302const CloseEventTypeId = "~effect/socket/Socket/CloseEvent"
303

Callers 1

Socket.tsFile · 0.85

Calls 5

pipeMethod · 0.65
failMethod · 0.65
mapMethod · 0.45
decodeMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected