Skip to content

Commit

Permalink
Merge pull request #3424 from armanbilge/issue/3423
Browse files Browse the repository at this point in the history
Fix corking when writing large strings to stdout/stderr on Node.js
  • Loading branch information
armanbilge authored Feb 16, 2023
2 parents 21ca76d + 358ce1d commit 6820f01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 16 additions & 4 deletions std/js/src/main/scala/cats/effect/std/Console.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,22 @@ object Console extends ConsoleCompanionCrossPlatform {
}

private def writeln(writable: js.Dynamic, s: String): F[Unit] =
F.delay(writable.cork()) *> // buffers until uncork
write(writable, s) *>
write(writable, "\n") *>
F.delay(writable.uncork()).void
F.async { cb =>
F.delay {
try {
writable.cork() // buffers until uncork
writable.write(s)
if (writable.write("\n").asInstanceOf[Boolean]) // no backpressure
cb(Right(()))
else // wait for drain event
writable.once("drain", () => cb(Right(())))
} finally {
writable.uncork()
()
}
None
}
}

def error[A](a: A)(implicit S: cats.Show[A]): F[Unit] = write(stderr, S.show(a))

Expand Down
3 changes: 3 additions & 0 deletions tests/js/src/test/scala/cats/effect/std/ConsoleJSSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class ConsoleJSSpec extends BaseSpec {
"work in any JS environment" in real {
Console[IO].println("printing") *> Console[IO].errorln("erroring") *> IO(true)
}
"println should not hang for large strings" in real {
Console[IO].println("foo" * 10000).as(true)
}
}

}

0 comments on commit 6820f01

Please sign in to comment.